]> The Tcpdump Group git mirrors - tcpdump/blob - print-rrcp.c
From Toshihiro Kanda, via FreeBSD: fix printing of address in AARP
[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 #ifndef lint
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-rrcp.c,v 1.2 2008-04-11 17:21:34 gianluca Exp $";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "netdissect.h"
39 #include "addrtoname.h"
40 #include "extract.h"
41 #include "ether.h"
42
43 #ifndef ETH_ALEN
44 #define ETH_ALEN 6
45 #endif
46
47 #define RRCP_OPCODE_MASK 0x7F /* 0x00 = hello, 0x01 = get, 0x02 = set */
48 #define RRCP_ISREPLY 0x80 /* 0 = request to switch, 0x80 = reply from switch */
49
50 #define RRCP_PROTO_OFFSET 0 /* proto - 1 byte, must be 1 */
51 #define RRCP_OPCODE_ISREPLY_OFFSET 1 /* opcode and isreply flag - 1 byte */
52 #define RRCP_AUTHKEY_OFFSET 2 /* authorization key - 2 bytes, 0x2379 by default */
53
54 /* most packets */
55 #define RRCP_REG_ADDR_OFFSET 4 /* register address - 2 bytes */
56 #define RRCP_REG_DATA_OFFSET 6 /* register data - 4 bytes */
57 #define RRCP_COOKIE1_OFFSET 10 /* 4 bytes */
58 #define RRCP_COOKIE2_OFFSET 14 /* 4 bytes */
59
60 /* hello reply packets */
61 #define RRCP_DOWNLINK_PORT_OFFSET 4 /* 1 byte */
62 #define RRCP_UPLINK_PORT_OFFSET 5 /* 1 byte */
63 #define RRCP_UPLINK_MAC_OFFSET 6 /* 6 byte MAC address */
64 #define RRCP_CHIP_ID_OFFSET 12 /* 2 bytes */
65 #define RRCP_VENDOR_ID_OFFSET 14 /* 4 bytes */
66
67 /*
68 * Print RRCP requests
69 */
70 void
71 rrcp_print(netdissect_options *ndo,
72 register const u_char *cp,
73 u_int length _U_)
74 {
75 const u_char *rrcp;
76 u_int8_t rrcp_proto;
77 u_int8_t rrcp_opcode;
78 register const struct ether_header *ep;
79 char proto_str[16];
80 char opcode_str[32];
81
82 ep = (const struct ether_header *)cp;
83 rrcp = cp + ETHER_HDRLEN;
84
85 ND_TCHECK(*(rrcp + RRCP_PROTO_OFFSET));
86 rrcp_proto = *(rrcp + RRCP_PROTO_OFFSET);
87 if (rrcp_proto==1){
88 strcpy(proto_str,"RRCP");
89 }else if ( rrcp_proto==2 ){
90 strcpy(proto_str,"RRCP-REP");
91 }else{
92 sprintf(proto_str,"RRCP-0x%02d",rrcp_proto);
93 }
94 ND_TCHECK(*(rrcp + RRCP_OPCODE_ISREPLY_OFFSET));
95 rrcp_opcode = (*(rrcp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_OPCODE_MASK;
96 if (rrcp_opcode==0){
97 strcpy(opcode_str,"hello");
98 }else if ( rrcp_opcode==1 ){
99 strcpy(opcode_str,"get");
100 }else if ( rrcp_opcode==2 ){
101 strcpy(opcode_str,"set");
102 }else{
103 sprintf(opcode_str,"unknown opcode (0x%02d)",rrcp_opcode);
104 }
105 ND_PRINT((ndo, "%s > %s, %s %s",
106 etheraddr_string(ESRC(ep)),
107 etheraddr_string(EDST(ep)),
108 proto_str,
109 ((*(rrcp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY) ? "reply" : "query"));
110 if (rrcp_proto==1){
111 ND_PRINT((ndo, ": %s", opcode_str));
112 }
113 if (rrcp_opcode==1 || rrcp_opcode==2){
114 ND_TCHECK2(*(rrcp + RRCP_REG_ADDR_OFFSET), 6);
115 ND_PRINT((ndo, " addr=0x%04x, data=0x%08x",
116 EXTRACT_16BITS(rrcp + RRCP_REG_ADDR_OFFSET),
117 EXTRACT_32BITS(rrcp + RRCP_REG_DATA_OFFSET)));
118 }
119 if (rrcp_proto==1){
120 ND_TCHECK2(*(rrcp + RRCP_AUTHKEY_OFFSET), 2);
121 ND_PRINT((ndo, ", auth=0x%04x",
122 EXTRACT_16BITS(rrcp + RRCP_AUTHKEY_OFFSET)));
123 }
124 if (rrcp_proto==1 && rrcp_opcode==0 &&
125 ((*(rrcp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY)){
126 ND_TCHECK2(*(rrcp + RRCP_VENDOR_ID_OFFSET), 4);
127 ND_PRINT((ndo, " downlink_port=%d, uplink_port=%d, uplink_mac=%s, vendor_id=%08x ,chip_id=%04x ",
128 *(rrcp + RRCP_DOWNLINK_PORT_OFFSET),
129 *(rrcp + RRCP_UPLINK_PORT_OFFSET),
130 etheraddr_string(rrcp + RRCP_UPLINK_MAC_OFFSET),
131 EXTRACT_32BITS(rrcp + RRCP_VENDOR_ID_OFFSET),
132 EXTRACT_16BITS(rrcp + RRCP_CHIP_ID_OFFSET)));
133 }else if (rrcp_opcode==1 || rrcp_opcode==2 || rrcp_proto==2){
134 ND_TCHECK2(*(rrcp + RRCP_COOKIE2_OFFSET), 4);
135 ND_PRINT((ndo, ", cookie=0x%08x%08x ",
136 EXTRACT_32BITS(rrcp + RRCP_COOKIE2_OFFSET),
137 EXTRACT_32BITS(rrcp + RRCP_COOKIE1_OFFSET)));
138 }
139 if (!ndo->ndo_vflag)
140 return;
141 return;
142
143 trunc:
144 ND_PRINT((ndo, "[|rrcp]"));
145 }