]> The Tcpdump Group git mirrors - tcpdump/blob - print-realtek.c
416d041e69f642b187f1315e26cb292724f6836c
[tcpdump] / print-realtek.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), Realtek
21 * Loop Detection Protocol (RLDP), and Realtek Echo Protocol (REP) packets,
22 * as well as tag formats used by some Realtek switch chips to supply
23 * tag information to a host CPU for a switch.
24 */
25
26 /* \summary: printer for various Realtek protocols */
27
28 /*
29 * See, for example, section 8.20 "Realtek Remote Control Protocol" of
30 *
31 * http://realtek.info/pdf/rtl8324.pdf
32 *
33 * and section 7.22 "Realtek Remote Control Protocol" of
34 *
35 * http://realtek.info/pdf/rtl8326.pdf
36 *
37 * and this page on the OpenRRCP Wiki:
38 *
39 * http://openrrcp.org.ru/wiki/rrcp_protocol
40 *
41 * for information on RRCP.
42 *
43 * See, for example, section 8.21 "Network Loop Connection Fault
44 * Detection" of
45 *
46 * http://realtek.info/pdf/rtl8324.pdf
47 *
48 * and section 7.23 "Network Loop Connection Fault Detection" of
49 *
50 * http://realtek.info/pdf/rtl8326.pdf
51 *
52 * for information on RLDP.
53 *
54 * See, for example, section 8.22 "Realtek Echo Protocol" of
55 *
56 * http://realtek.info/pdf/rtl8324.pdf
57 *
58 * and section 7.24 "Realtek Echo Protocol" of
59 *
60 * http://realtek.info/pdf/rtl8326.pdf
61 *
62 * for information on REP.
63 *
64 * NOTE: none of them indicate the byte order of multi-byte fields in any
65 * obvious fashion.
66 *
67 * See section 8.10 "CPU Tag Function" of
68 *
69 * http://realtek.info/pdf/rtl8306sd%28m%29_datasheet_1.1.pdf
70 *
71 * for the RTL8306 DSA protocol tag format.
72 */
73
74 #ifdef HAVE_CONFIG_H
75 #include <config.h>
76 #endif
77
78 #include "netdissect-stdinc.h"
79
80 #include "netdissect.h"
81 #include "addrtoname.h"
82 #include "extract.h"
83
84 #define RTL_FRAME_TYPE_OFFSET 0 /* frame type and other data - 1 byte */
85
86 /*
87 * The upper 4 bits of the first octet of Realtek 0x8899 frames indicates
88 * the frame type.
89 */
90 #define RTL_FRAME_TYPE_MASK 0xF0
91 #define RTL_FRAME_TYPE_SUBTYPE 0x00 /* lower 4 bits are a subtype */
92 #define RTL_FRAME_TYPE_8306_DSA 0x90 /* RTL8306 DSA protocol */
93 #define RTL_FRAME_TYPE_8366RB_DSA 0xA0 /* RTL8366RB DSA protocol */
94 #define RTL_FRAME_TYPE_SHIFT 4
95
96 /*
97 * The lower 4 bits are a subtype if the upper 4 bits are 0.
98 */
99 #define RTL_FRAME_SUBTYPE_MASK 0x0F
100 #define RTL_FRAME_SUBTYPE_RRCP 0x01 /* RRCP */
101 #define RTL_FRAME_SUBTYPE_REP 0x02 /* REP */
102 #define RTL_FRAME_SUBTYPE_RLDP 0x03 /* RLDP */
103 #define RTL_FRAME_SUBTYPE_XXX_DSA 0x04 /* DSA protocol for some chip(s) */
104
105 #define RRCP_OPCODE_ISREPLY_OFFSET 1 /* opcode and isreply flag - 1 byte */
106
107 #define RRCP_OPCODE_MASK 0x7F /* 0x00 = hello, 0x01 = get, 0x02 = set */
108 #define RRCP_ISREPLY 0x80 /* 0 = request to switch, 0x80 = reply from switch */
109
110 #define RRCP_OPCODE_HELLO 0x00
111 #define RRCP_OPCODE_GET_CONFIGURATION 0x01
112 #define RRCP_OPCODE_SET_CONFIGURATION 0x02
113
114 #define RRCP_AUTHKEY_OFFSET 2 /* authorization key - 2 bytes, 0x2379 by default */
115
116 /* most packets */
117 #define RRCP_REG_ADDR_OFFSET 4 /* register address - 2 bytes */
118 #define RRCP_REG_DATA_OFFSET 6 /* register data - 4 bytes */
119 #define RRCP_COOKIE1_OFFSET 10 /* 4 bytes */
120 #define RRCP_COOKIE2_OFFSET 14 /* 4 bytes */
121
122 /* hello reply packets */
123 #define RRCP_DOWNLINK_PORT_OFFSET 4 /* 1 byte */
124 #define RRCP_UPLINK_PORT_OFFSET 5 /* 1 byte */
125 #define RRCP_UPLINK_MAC_OFFSET 6 /* 6 byte MAC address */
126 #define RRCP_CHIP_ID_OFFSET 12 /* 2 bytes */
127 #define RRCP_VENDOR_ID_OFFSET 14 /* 4 bytes */
128
129 static const struct tok opcode_values[] = {
130 { RRCP_OPCODE_HELLO, "hello" },
131 { RRCP_OPCODE_GET_CONFIGURATION, "get" },
132 { RRCP_OPCODE_SET_CONFIGURATION, "set" },
133 { 0, NULL }
134 };
135
136 /*
137 * Print RRCP packets
138 */
139 static void
140 rrcp_print(netdissect_options *ndo,
141 const u_char *cp)
142 {
143 uint8_t rrcp_opcode;
144
145 ndo->ndo_protocol = "rrcp";
146 rrcp_opcode = GET_U_1((cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_OPCODE_MASK;
147 ND_PRINT("RRCP %s: %s",
148 ((GET_U_1(cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY) ? "reply" : "query",
149 tok2str(opcode_values,"unknown opcode (0x%02x)",rrcp_opcode));
150 if (rrcp_opcode==RRCP_OPCODE_GET_CONFIGURATION ||
151 rrcp_opcode==RRCP_OPCODE_SET_CONFIGURATION){
152 ND_PRINT(" addr=0x%04x, data=0x%08x",
153 GET_LE_U_2(cp + RRCP_REG_ADDR_OFFSET),
154 GET_LE_U_4(cp + RRCP_REG_DATA_OFFSET));
155 }
156 ND_PRINT(", auth=0x%04x",
157 GET_BE_U_2(cp + RRCP_AUTHKEY_OFFSET));
158 if (rrcp_opcode==RRCP_OPCODE_HELLO &&
159 ((GET_U_1(cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY)){
160 ND_PRINT(" downlink_port=%u, uplink_port=%u, uplink_mac=%s, vendor_id=%08x ,chip_id=%04x ",
161 GET_U_1(cp + RRCP_DOWNLINK_PORT_OFFSET),
162 GET_U_1(cp + RRCP_UPLINK_PORT_OFFSET),
163 GET_ETHERADDR_STRING(cp + RRCP_UPLINK_MAC_OFFSET),
164 GET_BE_U_4(cp + RRCP_VENDOR_ID_OFFSET),
165 GET_BE_U_2(cp + RRCP_CHIP_ID_OFFSET));
166 }else if (rrcp_opcode==RRCP_OPCODE_GET_CONFIGURATION ||
167 rrcp_opcode==RRCP_OPCODE_SET_CONFIGURATION){
168 ND_PRINT(", cookie=0x%08x%08x ",
169 GET_BE_U_4(cp + RRCP_COOKIE2_OFFSET),
170 GET_BE_U_4(cp + RRCP_COOKIE1_OFFSET));
171 }
172 }
173
174 /*
175 * Print Realtek packets
176 */
177 void
178 rtl_print(netdissect_options *ndo,
179 const u_char *cp,
180 u_int length _U_,
181 const struct lladdr_info *src,
182 const struct lladdr_info *dst)
183 {
184 uint8_t rtl_proto;
185
186 ndo->ndo_protocol = "rtl";
187
188 if (src != NULL && dst != NULL) {
189 ND_PRINT("%s > %s, ",
190 (src->addr_string)(ndo, src->addr),
191 (dst->addr_string)(ndo, dst->addr));
192 }
193
194 rtl_proto = GET_U_1(cp + RTL_FRAME_TYPE_OFFSET);
195
196 switch (rtl_proto & RTL_FRAME_TYPE_MASK) {
197
198 case RTL_FRAME_TYPE_SUBTYPE:
199 /*
200 * Test the subtype.
201 */
202 switch (rtl_proto & RTL_FRAME_SUBTYPE_MASK) {
203
204 case RTL_FRAME_SUBTYPE_RRCP:
205 rrcp_print(ndo, cp);
206 break;
207
208 case RTL_FRAME_SUBTYPE_REP:
209 /*
210 * REP packets have no payload.
211 */
212 ND_PRINT("REP");
213 break;
214
215 case RTL_FRAME_SUBTYPE_RLDP:
216 /*
217 * RLDP packets have no payload.
218 */
219 ND_PRINT("RLDP");
220 break;
221
222 case RTL_FRAME_SUBTYPE_XXX_DSA:
223 ND_PRINT("Realtek 8-byte DSA tag");
224 break;
225
226 default:
227 ND_PRINT("Realtek unknown subtype 0x%01x",
228 rtl_proto & RTL_FRAME_SUBTYPE_MASK);
229 break;
230 }
231 break;
232
233 case RTL_FRAME_TYPE_8306_DSA:
234 ND_PRINT("Realtek RTL8306 4-byte DSA tag");
235 break;
236
237 case RTL_FRAME_TYPE_8366RB_DSA:
238 ND_PRINT("Realtek RTL8366RB 4-byte DSA tag");
239 break;
240
241 default:
242 ND_PRINT("Realtek unknown type 0x%01x",
243 (rtl_proto & RTL_FRAME_TYPE_MASK) >> RTL_FRAME_TYPE_SHIFT);
244 break;
245 }
246 }