xref: /aosp_15_r20/external/tcpdump/print-realtek.c (revision 05b00f6010a2396e3db2409989fc67270046269f)
1*05b00f60SXin Li /*
2*05b00f60SXin Li  * Copyright (c) 2007 - Andrey "nording" Chernyak <[email protected]>
3*05b00f60SXin Li  *
4*05b00f60SXin Li  * Redistribution and use in source and binary forms, with or without
5*05b00f60SXin Li  * modification, are permitted provided that: (1) source code distributions
6*05b00f60SXin Li  * retain the above copyright notice and this paragraph in its entirety, (2)
7*05b00f60SXin Li  * distributions including binary code include the above copyright notice and
8*05b00f60SXin Li  * this paragraph in its entirety in the documentation or other materials
9*05b00f60SXin Li  * provided with the distribution, and (3) all advertising materials mentioning
10*05b00f60SXin Li  * features or use of this software display the following acknowledgement:
11*05b00f60SXin Li  * ``This product includes software developed by the University of California,
12*05b00f60SXin Li  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
13*05b00f60SXin Li  * the University nor the names of its contributors may be used to endorse
14*05b00f60SXin Li  * or promote products derived from this software without specific prior
15*05b00f60SXin Li  * written permission.
16*05b00f60SXin Li  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
17*05b00f60SXin Li  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
18*05b00f60SXin Li  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19*05b00f60SXin Li  *
20*05b00f60SXin Li  * Format and print Realtek Remote Control Protocol (RRCP), Realtek
21*05b00f60SXin Li  * Loop Detection Protocol (RLDP), and Realtek Echo Protocol (REP) packets,
22*05b00f60SXin Li  * as well as tag formats used by some Realtek switch chips to supply
23*05b00f60SXin Li  * tag information to a host CPU for a switch.
24*05b00f60SXin Li  */
25*05b00f60SXin Li 
26*05b00f60SXin Li /* \summary: printer for various Realtek protocols */
27*05b00f60SXin Li 
28*05b00f60SXin Li #ifdef HAVE_CONFIG_H
29*05b00f60SXin Li #include <config.h>
30*05b00f60SXin Li #endif
31*05b00f60SXin Li 
32*05b00f60SXin Li #include "netdissect-stdinc.h"
33*05b00f60SXin Li 
34*05b00f60SXin Li #include "netdissect.h"
35*05b00f60SXin Li #include "addrtoname.h"
36*05b00f60SXin Li #include "extract.h"
37*05b00f60SXin Li 
38*05b00f60SXin Li #define RTL_PROTOCOL_OFFSET	0	/* Protocol and possibly other data - 1 byte */
39*05b00f60SXin Li 
40*05b00f60SXin Li #define RTL_PROTOCOL_RRCP	0x01	/* RRCP */
41*05b00f60SXin Li #define RTL_PROTOCOL_REP	0x02	/* REP */
42*05b00f60SXin Li #define RTL_PROTOCOL_RLDP	0x03	/* RLDP */
43*05b00f60SXin Li #define RTL_PROTOCOL_RLDP2	0x23	/* also RLDP */
44*05b00f60SXin Li #define RTL_PROTOCOL_XXX_DSA	0x04	/* DSA protocol for some chip(s) */
45*05b00f60SXin Li 
46*05b00f60SXin Li /*
47*05b00f60SXin Li  * Values for the upper 4 bits of the protocol field, for
48*05b00f60SXin Li  * protocols where the lower 4 bits contain protocol data.
49*05b00f60SXin Li  *
50*05b00f60SXin Li  * See section 8.10 "CPU Tag Function" of
51*05b00f60SXin Li  *
52*05b00f60SXin Li  *    http://realtek.info/pdf/rtl8306sd%28m%29_datasheet_1.1.pdf
53*05b00f60SXin Li  *
54*05b00f60SXin Li  * for the RTL8306 DSA protocol tag format.
55*05b00f60SXin Li  */
56*05b00f60SXin Li #define RTL_PROTOCOL_8306_DSA		0x90	/* RTL8306 DSA protocol */
57*05b00f60SXin Li #define RTL_PROTOCOL_8366RB_DSA		0xA0	/* RTL8366RB DSA protocol */
58*05b00f60SXin Li 
59*05b00f60SXin Li #define RRCP_OPCODE_ISREPLY_OFFSET	1	/* opcode and isreply flag - 1 byte */
60*05b00f60SXin Li 
61*05b00f60SXin Li #define RRCP_OPCODE_MASK		0x7F	/* 0x00 = hello, 0x01 = get, 0x02 = set */
62*05b00f60SXin Li #define RRCP_ISREPLY			0x80	/* 0 = request to switch, 0x80 = reply from switch */
63*05b00f60SXin Li 
64*05b00f60SXin Li #define RRCP_OPCODE_HELLO		0x00
65*05b00f60SXin Li #define RRCP_OPCODE_GET_CONFIGURATION	0x01
66*05b00f60SXin Li #define RRCP_OPCODE_SET_CONFIGURATION	0x02
67*05b00f60SXin Li 
68*05b00f60SXin Li #define RRCP_AUTHKEY_OFFSET		2	/* authorization key - 2 bytes, 0x2379 by default */
69*05b00f60SXin Li 
70*05b00f60SXin Li /* most packets */
71*05b00f60SXin Li #define RRCP_REG_ADDR_OFFSET		4	/* register address - 2 bytes */
72*05b00f60SXin Li #define RRCP_REG_DATA_OFFSET		6	/* register data - 4 bytes */
73*05b00f60SXin Li #define RRCP_COOKIE1_OFFSET		10	/* 4 bytes */
74*05b00f60SXin Li #define RRCP_COOKIE2_OFFSET		14	/* 4 bytes */
75*05b00f60SXin Li 
76*05b00f60SXin Li /* hello reply packets */
77*05b00f60SXin Li #define RRCP_DOWNLINK_PORT_OFFSET	4	/* 1 byte */
78*05b00f60SXin Li #define RRCP_UPLINK_PORT_OFFSET		5	/* 1 byte */
79*05b00f60SXin Li #define RRCP_UPLINK_MAC_OFFSET		6	/* 6 byte MAC address */
80*05b00f60SXin Li #define RRCP_CHIP_ID_OFFSET		12	/* 2 bytes */
81*05b00f60SXin Li #define RRCP_VENDOR_ID_OFFSET		14	/* 4 bytes */
82*05b00f60SXin Li 
83*05b00f60SXin Li static const struct tok opcode_values[] = {
84*05b00f60SXin Li 	{ RRCP_OPCODE_HELLO,             "hello" },
85*05b00f60SXin Li 	{ RRCP_OPCODE_GET_CONFIGURATION, "get" },
86*05b00f60SXin Li 	{ RRCP_OPCODE_SET_CONFIGURATION, "set" },
87*05b00f60SXin Li 	{ 0, NULL }
88*05b00f60SXin Li };
89*05b00f60SXin Li 
90*05b00f60SXin Li /*
91*05b00f60SXin Li  * Print RRCP packets.
92*05b00f60SXin Li  *
93*05b00f60SXin Li  * See, for example, section 8.20 "Realtek Remote Control Protocol" of
94*05b00f60SXin Li  *
95*05b00f60SXin Li  *    http://realtek.info/pdf/rtl8324.pdf
96*05b00f60SXin Li  *
97*05b00f60SXin Li  * and section 7.22 "Realtek Remote Control Protocol" of
98*05b00f60SXin Li  *
99*05b00f60SXin Li  *    http://realtek.info/pdf/rtl8326.pdf
100*05b00f60SXin Li  *
101*05b00f60SXin Li  * and this page on the OpenRRCP Wiki:
102*05b00f60SXin Li  *
103*05b00f60SXin Li  *    http://openrrcp.org.ru/wiki/rrcp_protocol
104*05b00f60SXin Li  *
105*05b00f60SXin Li  * for information on RRCP.
106*05b00f60SXin Li  */
107*05b00f60SXin Li static void
rrcp_print(netdissect_options * ndo,const u_char * cp)108*05b00f60SXin Li rrcp_print(netdissect_options *ndo,
109*05b00f60SXin Li 	  const u_char *cp)
110*05b00f60SXin Li {
111*05b00f60SXin Li 	uint8_t rrcp_opcode;
112*05b00f60SXin Li 
113*05b00f60SXin Li 	ndo->ndo_protocol = "rrcp";
114*05b00f60SXin Li 	rrcp_opcode = GET_U_1((cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_OPCODE_MASK;
115*05b00f60SXin Li 	ND_PRINT("RRCP %s: %s",
116*05b00f60SXin Li 		((GET_U_1(cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY) ? "reply" : "query",
117*05b00f60SXin Li 		tok2str(opcode_values,"unknown opcode (0x%02x)",rrcp_opcode));
118*05b00f60SXin Li 	if (rrcp_opcode==RRCP_OPCODE_GET_CONFIGURATION ||
119*05b00f60SXin Li 	    rrcp_opcode==RRCP_OPCODE_SET_CONFIGURATION){
120*05b00f60SXin Li 	    ND_PRINT(" addr=0x%04x, data=0x%08x",
121*05b00f60SXin Li 		     GET_LE_U_2(cp + RRCP_REG_ADDR_OFFSET),
122*05b00f60SXin Li 		     GET_LE_U_4(cp + RRCP_REG_DATA_OFFSET));
123*05b00f60SXin Li 	}
124*05b00f60SXin Li 	ND_PRINT(", auth=0x%04x",
125*05b00f60SXin Li 	         GET_BE_U_2(cp + RRCP_AUTHKEY_OFFSET));
126*05b00f60SXin Li 	if (rrcp_opcode==RRCP_OPCODE_HELLO &&
127*05b00f60SXin Li 	     ((GET_U_1(cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY)){
128*05b00f60SXin Li 	    ND_PRINT(" downlink_port=%u, uplink_port=%u, uplink_mac=%s, vendor_id=%08x ,chip_id=%04x ",
129*05b00f60SXin Li 		     GET_U_1(cp + RRCP_DOWNLINK_PORT_OFFSET),
130*05b00f60SXin Li 		     GET_U_1(cp + RRCP_UPLINK_PORT_OFFSET),
131*05b00f60SXin Li 		     GET_ETHERADDR_STRING(cp + RRCP_UPLINK_MAC_OFFSET),
132*05b00f60SXin Li 		     GET_BE_U_4(cp + RRCP_VENDOR_ID_OFFSET),
133*05b00f60SXin Li 		     GET_BE_U_2(cp + RRCP_CHIP_ID_OFFSET));
134*05b00f60SXin Li 	}else if (rrcp_opcode==RRCP_OPCODE_GET_CONFIGURATION ||
135*05b00f60SXin Li 	          rrcp_opcode==RRCP_OPCODE_SET_CONFIGURATION){
136*05b00f60SXin Li 	    ND_PRINT(", cookie=0x%08x%08x ",
137*05b00f60SXin Li 		    GET_BE_U_4(cp + RRCP_COOKIE2_OFFSET),
138*05b00f60SXin Li 		    GET_BE_U_4(cp + RRCP_COOKIE1_OFFSET));
139*05b00f60SXin Li 	}
140*05b00f60SXin Li }
141*05b00f60SXin Li 
142*05b00f60SXin Li /*
143*05b00f60SXin Li  * Print Realtek packets.
144*05b00f60SXin Li  *
145*05b00f60SXin Li  * See, for example, section 8.22 "Realtek Echo Protocol" of
146*05b00f60SXin Li  *
147*05b00f60SXin Li  *    http://realtek.info/pdf/rtl8324.pdf
148*05b00f60SXin Li  *
149*05b00f60SXin Li  * and section 7.24 "Realtek Echo Protocol" of
150*05b00f60SXin Li  *
151*05b00f60SXin Li  *    http://realtek.info/pdf/rtl8326.pdf
152*05b00f60SXin Li  *
153*05b00f60SXin Li  * for information on REP.
154*05b00f60SXin Li  *
155*05b00f60SXin Li  * See section 8.21 "Network Loop Connection Fault Detection" of
156*05b00f60SXin Li  *
157*05b00f60SXin Li  *    http://realtek.info/pdf/rtl8324.pdf
158*05b00f60SXin Li  *
159*05b00f60SXin Li  * and section 7.23 "Network Loop Connection Fault Detection" of
160*05b00f60SXin Li  *
161*05b00f60SXin Li  *    http://realtek.info/pdf/rtl8326.pdf
162*05b00f60SXin Li  *
163*05b00f60SXin Li  * for information on RLDP.
164*05b00f60SXin Li  *
165*05b00f60SXin Li  * See also section 7.3.8 "Loop Detection" of
166*05b00f60SXin Li  *
167*05b00f60SXin Li  *    http://www.ibselectronics.com/ibsstore/datasheet/RTL8306E-CG.pdf
168*05b00f60SXin Li  *
169*05b00f60SXin Li  * (revision 1.1 of the RTL8306E-CG datasheet), which describes a loop
170*05b00f60SXin Li  * detection protocol for which the payload has a 16-bit (presumably
171*05b00f60SXin Li  * big-endian) field containing the value 0x0300, followed by what is
172*05b00f60SXin Li  * presumably a 16-bit big-endian field the upper 12 bits of which are 0
173*05b00f60SXin Li  * and the lower 4 bits of which are a TTL value, followed by zeroes to
174*05b00f60SXin Li  * pad the packet out to the minimum Ethernet packet size.
175*05b00f60SXin Li  *
176*05b00f60SXin Li  * See also section 7.3.13 "Loop Detection" of
177*05b00f60SXin Li  *
178*05b00f60SXin Li  *    http://realtek.info/pdf/rtl8305sb.pdf
179*05b00f60SXin Li  *
180*05b00f60SXin Li  * (revision 1.3 of the RTL8305SB datasheet), which describes a similar
181*05b00f60SXin Li  * loop detection protocol that lacks the TTL field - all the bytes
182*05b00f60SXin Li  * after 0x0300 are zero.
183*05b00f60SXin Li  *
184*05b00f60SXin Li  * See also section 7.3.7 "Loop Detection" of
185*05b00f60SXin Li  *
186*05b00f60SXin Li  *    https://datasheet.lcsc.com/lcsc/1810221720_Realtek-Semicon-RTL8305NB-CG_C52146.pdf
187*05b00f60SXin Li  *
188*05b00f60SXin Li  * (revision 1.0 of the RTL8305NB-CT datasheet), which describes a loop
189*05b00f60SXin Li  * detection protocol similar to the one from the RTL8306E-CG datasheet,
190*05b00f60SXin Li  * except that the first value is 0x2300, not 0x0300.
191*05b00f60SXin Li  *
192*05b00f60SXin Li  * And, on top of all that, I've seen packets where the first octet of
193*05b00f60SXin Li  * the packet is 0x23, and that's followed by 6 unknown octets (a MAC
194*05b00f60SXin Li  * address of some sort?  It differs from packet to packet in a capture),
195*05b00f60SXin Li  * followed by the MAC address that appears in the source address in the
196*05b00f60SXin Li  * Ethernet header (possibly the originator, in case the packet is forwarded,
197*05b00f60SXin Li  * in which case the forwarded packets won't have the source address from
198*05b00f60SXin Li  * the Ethernet header there), followed by unknown stuff (0x0d followed by
199*05b00f60SXin Li  * zeroes for all such packets in one capture, 0x01 followed by zeroes for
200*05b00f60SXin Li  * all such packets in another capture, 0x07 followed by 0x20's for all
201*05b00f60SXin Li  * such packets in yet another capture).  The OpenRRCP issue at
202*05b00f60SXin Li  * https://github.com/illarionov/OpenRRCP/issues/3 shows a capture
203*05b00f60SXin Li  * similar to the last of those, but with 0x02 instead of 0x07.  Or is that
204*05b00f60SXin Li  * just crap in the buffer in which the chip constructed the packet, left
205*05b00f60SXin Li  * over from something else?
206*05b00f60SXin Li  */
207*05b00f60SXin Li void
rtl_print(netdissect_options * ndo,const u_char * cp,u_int length _U_,const struct lladdr_info * src,const struct lladdr_info * dst)208*05b00f60SXin Li rtl_print(netdissect_options *ndo,
209*05b00f60SXin Li 	  const u_char *cp,
210*05b00f60SXin Li 	  u_int length _U_,
211*05b00f60SXin Li 	  const struct lladdr_info *src,
212*05b00f60SXin Li 	  const struct lladdr_info *dst)
213*05b00f60SXin Li {
214*05b00f60SXin Li 	uint8_t rtl_proto;
215*05b00f60SXin Li 
216*05b00f60SXin Li 	ndo->ndo_protocol = "rtl";
217*05b00f60SXin Li 
218*05b00f60SXin Li 	if (src != NULL && dst != NULL) {
219*05b00f60SXin Li 		ND_PRINT("%s > %s, ",
220*05b00f60SXin Li 			(src->addr_string)(ndo, src->addr),
221*05b00f60SXin Li 			(dst->addr_string)(ndo, dst->addr));
222*05b00f60SXin Li 	}
223*05b00f60SXin Li 
224*05b00f60SXin Li 	rtl_proto = GET_U_1(cp + RTL_PROTOCOL_OFFSET);
225*05b00f60SXin Li 
226*05b00f60SXin Li 	if (rtl_proto == RTL_PROTOCOL_RRCP)
227*05b00f60SXin Li 		rrcp_print(ndo, cp);
228*05b00f60SXin Li 	else if (rtl_proto == RTL_PROTOCOL_REP) {
229*05b00f60SXin Li 		/*
230*05b00f60SXin Li 		 * REP packets have no payload.
231*05b00f60SXin Li 		 */
232*05b00f60SXin Li 		ND_PRINT("REP");
233*05b00f60SXin Li 	} else if (rtl_proto == RTL_PROTOCOL_RLDP ||
234*05b00f60SXin Li 	           rtl_proto == RTL_PROTOCOL_RLDP2) {
235*05b00f60SXin Li 		/*
236*05b00f60SXin Li 		 * RLDP packets have no payload.
237*05b00f60SXin Li 		 * (XXX - except when they do?  See above.)
238*05b00f60SXin Li 		 */
239*05b00f60SXin Li 		ND_PRINT("RLDP");
240*05b00f60SXin Li 	} else if (rtl_proto == RTL_PROTOCOL_XXX_DSA)
241*05b00f60SXin Li 		ND_PRINT("Realtek 8-byte DSA tag");
242*05b00f60SXin Li 	else if ((rtl_proto & 0xF0) == RTL_PROTOCOL_8306_DSA)
243*05b00f60SXin Li 		ND_PRINT("Realtek RTL8306 4-byte DSA tag");
244*05b00f60SXin Li 	else if ((rtl_proto & 0xF0) == RTL_PROTOCOL_8366RB_DSA)
245*05b00f60SXin Li 		ND_PRINT("Realtek RTL8366RB 4-byte DSA tag");
246*05b00f60SXin Li 	else
247*05b00f60SXin Li 		ND_PRINT("Realtek unknown type 0x%02x", rtl_proto);
248*05b00f60SXin Li }
249