]> The Tcpdump Group git mirrors - tcpdump/blob - print-dsa.c
Clean up the switch-tagged header code.
[tcpdump] / print-dsa.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 /* \summary: Marvell (Ethertype) Distributed Switch Architecture printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include "netdissect.h"
31 #include "ethertype.h"
32 #include "addrtoname.h"
33 #include "extract.h"
34
35 /*
36 * Format of (Ethertyped or not) DSA tagged frames:
37 *
38 * 7 6 5 4 3 2 1 0
39 * . . . . . . . . .
40 * 0 +---+---+---+---+---+---+---+---+
41 * | Ether Destination Address |
42 * +6 +---+---+---+---+---+---+---+---+
43 * | Ether Source Address |
44 * +6 +---+---+---+---+---+---+---+---+ +-
45 * | Prog. DSA Ether Type [15:8] | | (8-byte) EDSA Tag
46 * +1 +---+---+---+---+---+---+---+---+ | Contains a programmable Ether type,
47 * | Prog. DSA Ether Type [7:0] | | two reserved bytes (always 0),
48 * +1 +---+---+---+---+---+---+---+---+ | and a standard DSA tag.
49 * | Reserved (0x00 0x00) | |
50 * +2 +---+---+---+---+---+---+---+---+ | +-
51 * | Mode |b29| Src/Trg Dev | | | (4-byte) DSA Tag
52 * +1 +---+---+---+---+---+---+---+---+ | | Contains a DSA tag mode,
53 * |Src/Trg Port/Trunk |b18|b17|b16| | | source or target switch device,
54 * +1 +---+---+---+---+---+---+---+---+ | | source or target port or trunk,
55 * | PRI [2:0] |b12| VID [11:8] | | | and misc (IEEE and FPri) bits.
56 * +1 +---+---+---+---+---+---+---+---+ | |
57 * | VID [7:0] | | |
58 * +1 +---+---+---+---+---+---+---+---+ +- +-
59 * | Ether Length/Type |
60 * +2 +---+---+---+---+---+---+---+---+
61 * . . . . . . . . .
62 *
63 * Mode: Forward, To_CPU, From_CPU, To_Sniffer
64 * b29: (Source or Target) IEEE Tag Mode
65 * b18: Forward's Src_Is_Trunk, To_CPU's Code[2], To_Sniffer's Rx_Sniff
66 * b17: To_CPU's Code[1]
67 * b16: Original frame's CFI
68 * b12: To_CPU's Code[0]
69 */
70
71 #define TOK(tag, byte, mask, shift) ((GET_U_1(&(((const u_char *) tag)[byte])) & (mask)) >> (shift))
72
73 #define DSA_LEN 4
74 #define DSA_MODE(tag) TOK(tag, 0, 0xc0, 6)
75 #define DSA_MODE_TO_CPU 0x0
76 #define DSA_MODE_FROM_CPU 0x1
77 #define DSA_MODE_TO_SNIFFER 0x2
78 #define DSA_MODE_FORWARD 0x3
79 #define DSA_TAGGED(tag) TOK(tag, 0, 0x20, 5)
80 #define DSA_DEV(tag) TOK(tag, 0, 0x1f, 0)
81 #define DSA_PORT(tag) TOK(tag, 1, 0xf8, 3)
82 #define DSA_TRUNK(tag) TOK(tag, 1, 0x04, 2)
83 #define DSA_RX_SNIFF(tag) TOK(tag, 1, 0x04, 2)
84 #define DSA_CFI(tag) TOK(tag, 1, 0x01, 0)
85 #define DSA_PRI(tag) TOK(tag, 2, 0xe0, 5)
86 #define DSA_VID(tag) ((u_short)((TOK(tag, 2, 0xe0, 5) << 8) | (TOK(tag, 3, 0xff, 0))))
87 #define DSA_CODE(tag) ((TOK(tag, 1, 0x06, 1) << 1) | TOK(tag, 2, 0x10, 4))
88
89 #define EDSA_LEN 8
90 #define EDSA_ETYPE(tag) ((u_short)((TOK(tag, 0, 0xff, 0) << 8) | (TOK(tag, 1, 0xff, 0))))
91
92 static const struct tok dsa_mode_values[] = {
93 { DSA_MODE_TO_CPU, "To CPU" },
94 { DSA_MODE_FROM_CPU, "From CPU" },
95 { DSA_MODE_TO_SNIFFER, "To Sniffer"},
96 { DSA_MODE_FORWARD, "Forward" },
97 { 0, NULL }
98 };
99
100 static const struct tok dsa_code_values[] = {
101 { 0x0, "BPDU (MGMT) Trap" },
102 { 0x1, "Frame2Reg" },
103 { 0x2, "IGMP/MLD Trap" },
104 { 0x3, "Policy Trap" },
105 { 0x4, "ARP Mirror" },
106 { 0x5, "Policy Mirror" },
107 { 0, NULL }
108 };
109
110 static u_int
111 dsa_if_print_full(netdissect_options *ndo, const struct pcap_pkthdr *h,
112 const u_char *p, u_int taglen)
113 {
114 const u_char *edsa, *dsa;
115 int save_eflag;
116 int ret;
117
118 if (h->caplen < 2*MAC_ADDR_LEN + taglen) {
119 nd_print_trunc(ndo);
120 return (h->caplen);
121 }
122
123 if (h->len < 2*MAC_ADDR_LEN + taglen) {
124 nd_print_trunc(ndo);
125 return (h->len);
126 }
127
128 if (taglen == EDSA_LEN) {
129 edsa = p + 2*MAC_ADDR_LEN;
130 dsa = edsa + 4;
131 } else {
132 edsa = NULL;
133 dsa = p + 2*MAC_ADDR_LEN;
134 }
135
136 if (ndo->ndo_eflag) {
137 ND_PRINT("%s > %s, ",
138 etheraddr_string(ndo, p + MAC_ADDR_LEN),
139 etheraddr_string(ndo, p));
140
141 if (edsa) {
142 ND_PRINT("Marvell EDSA ethertype 0x%04x (%s), ", EDSA_ETYPE(edsa),
143 tok2str(ethertype_values, "Unknown", EDSA_ETYPE(edsa)));
144 ND_PRINT("rsvd %u %u, ", edsa[2], edsa[3]);
145 } else {
146 ND_PRINT("Marvell DSA ");
147 }
148
149 ND_PRINT("mode %s, ", tok2str(dsa_mode_values, "unknown", DSA_MODE(dsa)));
150
151 switch (DSA_MODE(dsa)) {
152 case DSA_MODE_FORWARD:
153 ND_PRINT("dev %u, %s %u, ", DSA_DEV(dsa),
154 DSA_TRUNK(dsa) ? "trunk" : "port", DSA_PORT(dsa));
155 break;
156 case DSA_MODE_FROM_CPU:
157 ND_PRINT("target dev %u, port %u, ",
158 DSA_DEV(dsa), DSA_PORT(dsa));
159 break;
160 case DSA_MODE_TO_CPU:
161 ND_PRINT("source dev %u, port %u, ",
162 DSA_DEV(dsa), DSA_PORT(dsa));
163 ND_PRINT("code %s, ",
164 tok2str(dsa_code_values, "reserved", DSA_CODE(dsa)));
165 break;
166 case DSA_MODE_TO_SNIFFER:
167 ND_PRINT("source dev %u, port %u, ",
168 DSA_DEV(dsa), DSA_PORT(dsa));
169 ND_PRINT("%s sniff, ",
170 DSA_RX_SNIFF(dsa) ? "ingress" : "egress");
171 break;
172 default:
173 break;
174 }
175
176 ND_PRINT("%s, ", DSA_TAGGED(dsa) ? "tagged" : "untagged");
177 ND_PRINT("%s", DSA_CFI(dsa) ? "CFI, " : "");
178 ND_PRINT("VID %u, ", DSA_VID(dsa));
179 ND_PRINT("FPri %u, ", DSA_PRI(dsa));
180 } else {
181 if (edsa) {
182 ND_PRINT("EDSA 0x%04x, ", EDSA_ETYPE(edsa));
183 } else {
184 ND_PRINT("DSA ");
185 }
186
187 switch (DSA_MODE(dsa)) {
188 case DSA_MODE_FORWARD:
189 ND_PRINT("Forward %s %u.%u, ",
190 DSA_TRUNK(dsa) ? "trunk" : "port",
191 DSA_DEV(dsa), DSA_PORT(dsa));
192 break;
193 case DSA_MODE_FROM_CPU:
194 ND_PRINT("CPU > port %u.%u, ",
195 DSA_DEV(dsa), DSA_PORT(dsa));
196 break;
197 case DSA_MODE_TO_CPU:
198 ND_PRINT("port %u.%u > CPU, ",
199 DSA_DEV(dsa), DSA_PORT(dsa));
200 break;
201 case DSA_MODE_TO_SNIFFER:
202 ND_PRINT("port %u.%u > %s Sniffer, ",
203 DSA_DEV(dsa), DSA_PORT(dsa),
204 DSA_RX_SNIFF(dsa) ? "Rx" : "Tx");
205 break;
206 default:
207 break;
208 }
209
210 ND_PRINT("VLAN %u%c, ", DSA_VID(dsa), DSA_TAGGED(dsa) ? 't' : 'u');
211 }
212
213 /* We printed the Ethernet destination and source addresses already */
214 save_eflag = ndo->ndo_eflag;
215 ndo->ndo_eflag = 0;
216
217 /* Parse the rest of the Ethernet header, and the frame payload,
218 * telling ether_hdr_len_print() how big the non-standard Ethernet
219 * header is.
220 *
221 * +-----------+-----------+---------------------+--------------+
222 * | MAC DA (6)| MAC SA (6)|DSA/EDSA tag (taglen)|Type/Length(2)|
223 * +-----------+-----------+---------------------+--------------+
224 */
225 ret = ether_hdr_len_print(ndo, p, h->len, h->caplen, NULL, NULL,
226 2*MAC_ADDR_LEN + taglen + 2);
227
228 ndo->ndo_eflag = save_eflag;
229
230 return ret;
231 }
232
233 u_int
234 dsa_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
235 {
236 ndo->ndo_protocol = "dsa";
237
238 return dsa_if_print_full(ndo, h, p, DSA_LEN);
239 }
240
241 u_int
242 edsa_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
243 {
244 ndo->ndo_protocol = "edsa";
245
246 return dsa_if_print_full(ndo, h, p, EDSA_LEN);
247 }