]> The Tcpdump Group git mirrors - tcpdump/blob - print-rrcp.c
Rename EXTRACT_ macros
[tcpdump] / print-rrcp.c
1 /*
2 * Copyright (c) 2007 - Andrey "nording" Chernyak <andrew@nording.ru>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code distributions
6 * retain the above copyright notice and this paragraph in its entirety, (2)
7 * distributions including binary code include the above copyright notice and
8 * this paragraph in its entirety in the documentation or other materials
9 * provided with the distribution, and (3) all advertising materials mentioning
10 * features or use of this software display the following acknowledgement:
11 * ``This product includes software developed by the University of California,
12 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
13 * the University nor the names of its contributors may be used to endorse
14 * or promote products derived from this software without specific prior
15 * written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * Format and print Realtek Remote Control Protocol (RRCP)
21 * and Realtek Echo Protocol (RRCP-REP) packets.
22 */
23
24 /* \summary: Realtek Remote Control Protocol (RRCP) printer */
25
26 /*
27 * See, for example, section 8.20 "Realtek Remote Control Protocol" of
28 *
29 * http://realtek.info/pdf/rtl8324.pdf
30 *
31 * and section 7.22 "Realtek Remote Control Protocol" of
32 *
33 * http://realtek.info/pdf/rtl8326.pdf
34 *
35 * and this page on the OpenRRCP Wiki:
36 *
37 * http://openrrcp.org.ru/wiki/rrcp_protocol
38 *
39 * NOTE: none of them indicate the byte order of multi-byte fields in any
40 * obvious fashion.
41 */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <netdissect-stdinc.h>
48
49 #include "netdissect.h"
50 #include "addrtoname.h"
51 #include "extract.h"
52 #include "ether.h"
53
54 #define RRCP_OPCODE_MASK 0x7F /* 0x00 = hello, 0x01 = get, 0x02 = set */
55 #define RRCP_ISREPLY 0x80 /* 0 = request to switch, 0x80 = reply from switch */
56
57 #define RRCP_PROTO_OFFSET 0 /* proto - 1 byte, must be 1 */
58 #define RRCP_OPCODE_ISREPLY_OFFSET 1 /* opcode and isreply flag - 1 byte */
59 #define RRCP_AUTHKEY_OFFSET 2 /* authorization key - 2 bytes, 0x2379 by default */
60
61 /* most packets */
62 #define RRCP_REG_ADDR_OFFSET 4 /* register address - 2 bytes */
63 #define RRCP_REG_DATA_OFFSET 6 /* register data - 4 bytes */
64 #define RRCP_COOKIE1_OFFSET 10 /* 4 bytes */
65 #define RRCP_COOKIE2_OFFSET 14 /* 4 bytes */
66
67 /* hello reply packets */
68 #define RRCP_DOWNLINK_PORT_OFFSET 4 /* 1 byte */
69 #define RRCP_UPLINK_PORT_OFFSET 5 /* 1 byte */
70 #define RRCP_UPLINK_MAC_OFFSET 6 /* 6 byte MAC address */
71 #define RRCP_CHIP_ID_OFFSET 12 /* 2 bytes */
72 #define RRCP_VENDOR_ID_OFFSET 14 /* 4 bytes */
73
74 static const struct tok proto_values[] = {
75 { 1, "RRCP" },
76 { 2, "RRCP-REP" },
77 { 0, NULL }
78 };
79
80 static const struct tok opcode_values[] = {
81 { 0, "hello" },
82 { 1, "get" },
83 { 2, "set" },
84 { 0, NULL }
85 };
86
87 /*
88 * Print RRCP requests
89 */
90 void
91 rrcp_print(netdissect_options *ndo,
92 register const u_char *cp,
93 u_int length _U_,
94 const struct lladdr_info *src,
95 const struct lladdr_info *dst)
96 {
97 uint8_t rrcp_proto;
98 uint8_t rrcp_opcode;
99
100 ND_TCHECK(*(cp + RRCP_PROTO_OFFSET));
101 rrcp_proto = EXTRACT_U_1(cp + RRCP_PROTO_OFFSET);
102 ND_TCHECK(*(cp + RRCP_OPCODE_ISREPLY_OFFSET));
103 rrcp_opcode = EXTRACT_U_1((cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_OPCODE_MASK;
104 if (src != NULL && dst != NULL) {
105 ND_PRINT((ndo, "%s > %s, ",
106 (src->addr_string)(ndo, src->addr),
107 (dst->addr_string)(ndo, dst->addr)));
108 }
109 ND_PRINT((ndo, "%s %s",
110 tok2str(proto_values,"RRCP-0x%02x",rrcp_proto),
111 ((*(cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY) ? "reply" : "query"));
112 if (rrcp_proto==1){
113 ND_PRINT((ndo, ": %s",
114 tok2str(opcode_values,"unknown opcode (0x%02x)",rrcp_opcode)));
115 }
116 if (rrcp_opcode==1 || rrcp_opcode==2){
117 ND_TCHECK2(*(cp + RRCP_REG_ADDR_OFFSET), 6);
118 ND_PRINT((ndo, " addr=0x%04x, data=0x%08x",
119 EXTRACT_LE_U_2(cp + RRCP_REG_ADDR_OFFSET),
120 EXTRACT_LE_U_4(cp + RRCP_REG_DATA_OFFSET)));
121 }
122 if (rrcp_proto==1){
123 ND_TCHECK2(*(cp + RRCP_AUTHKEY_OFFSET), 2);
124 ND_PRINT((ndo, ", auth=0x%04x",
125 EXTRACT_BE_U_2(cp + RRCP_AUTHKEY_OFFSET)));
126 }
127 if (rrcp_proto==1 && rrcp_opcode==0 &&
128 ((*(cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY)){
129 ND_TCHECK2(*(cp + RRCP_VENDOR_ID_OFFSET), 4);
130 ND_PRINT((ndo, " downlink_port=%d, uplink_port=%d, uplink_mac=%s, vendor_id=%08x ,chip_id=%04x ",
131 *(cp + RRCP_DOWNLINK_PORT_OFFSET),
132 *(cp + RRCP_UPLINK_PORT_OFFSET),
133 etheraddr_string(ndo, cp + RRCP_UPLINK_MAC_OFFSET),
134 EXTRACT_BE_U_4(cp + RRCP_VENDOR_ID_OFFSET),
135 EXTRACT_BE_U_2(cp + RRCP_CHIP_ID_OFFSET)));
136 }else if (rrcp_opcode==1 || rrcp_opcode==2 || rrcp_proto==2){
137 ND_TCHECK2(*(cp + RRCP_COOKIE2_OFFSET), 4);
138 ND_PRINT((ndo, ", cookie=0x%08x%08x ",
139 EXTRACT_BE_U_4(cp + RRCP_COOKIE2_OFFSET),
140 EXTRACT_BE_U_4(cp + RRCP_COOKIE1_OFFSET)));
141 }
142 return;
143
144 trunc:
145 ND_PRINT((ndo, "[|rrcp]"));
146 }