]> The Tcpdump Group git mirrors - tcpdump/blob - print-brcmtag.c
IPoIB: Add the output with -e for a capture test
[tcpdump] / print-brcmtag.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: Broadcom Ethernet switches tag (4 bytes) 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 #define ETHER_TYPE_LEN 2
36
37 #define BRCM_TAG_LEN 4
38 #define BRCM_OPCODE_SHIFT 5
39 #define BRCM_OPCODE_MASK 0x7
40
41 /* Ingress fields */
42 #define BRCM_IG_TC_SHIFT 2
43 #define BRCM_IG_TC_MASK 0x7
44 #define BRCM_IG_TE_MASK 0x3
45 #define BRCM_IG_TS_SHIFT 7
46 #define BRCM_IG_DSTMAP_MASK 0x1ff
47
48 /* Egress fields */
49 #define BRCM_EG_CID_MASK 0xff
50 #define BRCM_EG_RC_MASK 0xff
51 #define BRCM_EG_RC_RSVD (3 << 6)
52 #define BRCM_EG_RC_EXCEPTION (1 << 5)
53 #define BRCM_EG_RC_PROT_SNOOP (1 << 4)
54 #define BRCM_EG_RC_PROT_TERM (1 << 3)
55 #define BRCM_EG_RC_SWITCH (1 << 2)
56 #define BRCM_EG_RC_MAC_LEARN (1 << 1)
57 #define BRCM_EG_RC_MIRROR (1 << 0)
58 #define BRCM_EG_TC_SHIFT 5
59 #define BRCM_EG_TC_MASK 0x7
60 #define BRCM_EG_PID_MASK 0x1f
61
62 static const struct tok brcm_tag_te_values[] = {
63 { 0, "None" },
64 { 1, "Untag" },
65 { 2, "Header"},
66 { 3, "Reserved" },
67 { 0, NULL }
68 };
69
70 static const struct tok brcm_tag_rc_values[] = {
71 { 1, "mirror" },
72 { 2, "MAC learning" },
73 { 4, "switching" },
74 { 8, "prot term" },
75 { 16, "prot snoop" },
76 { 32, "exception" },
77 { 0, NULL }
78 };
79
80 static int brcm_tag_print_full(netdissect_options *ndo, const u_char *bp,
81 u_int length)
82 {
83 uint8_t tag[BRCM_TAG_LEN];
84 uint16_t dst_map;
85 unsigned int i;
86
87 if (length < BRCM_TAG_LEN)
88 return (1);
89
90 for (i = 0; i < BRCM_TAG_LEN; i++)
91 tag[i] = GET_U_1(bp + i);
92
93 ND_PRINT("BRCM tag OP: %s", tag[0] ? "IG" : "EG");
94 if (tag[0] & (1 << BRCM_OPCODE_SHIFT)) {
95 /* Ingress Broadcom tag */
96 ND_PRINT(", TC: %d", (tag[1] >> BRCM_IG_TC_SHIFT) &
97 BRCM_IG_TC_MASK);
98 ND_PRINT(", TE: %s",
99 tok2str(brcm_tag_te_values, "unknown",
100 (tag[1] & BRCM_IG_TE_MASK)));
101 ND_PRINT(", TS: %d", tag[1] >> BRCM_IG_TS_SHIFT);
102 dst_map = (uint16_t)tag[2] << 8 | tag[3];
103 ND_PRINT(", DST map: 0x%04x", dst_map & BRCM_IG_DSTMAP_MASK);
104 } else {
105 /* Egress Broadcom tag */
106 ND_PRINT(", CID: %d", tag[1]);
107 ND_PRINT(", RC: %s", tok2str(brcm_tag_rc_values,
108 "reserved", tag[2]));
109 ND_PRINT(", TC: %d", (tag[3] >> BRCM_EG_TC_SHIFT) &
110 BRCM_EG_TC_MASK);
111 ND_PRINT(", port: %d", tag[3] & BRCM_EG_PID_MASK);
112 }
113 ND_PRINT(", ");
114
115 return (0);
116 }
117
118 u_int
119 brcm_tag_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
120 const u_char *p)
121 {
122 u_int caplen = h->caplen;
123 u_int length = h->len;
124 int save_eflag;
125 int ret;
126
127 ndo->ndo_protocol = "brcm-tag";
128 if (caplen < 2*MAC_ADDR_LEN + BRCM_TAG_LEN) {
129 nd_print_trunc(ndo);
130 return (caplen);
131 }
132
133 if (length < 2*MAC_ADDR_LEN + BRCM_TAG_LEN) {
134 nd_print_trunc(ndo);
135 return (length);
136 }
137
138 if (ndo->ndo_eflag)
139 ND_PRINT("%s > %s, ",
140 etheraddr_string(ndo, p + MAC_ADDR_LEN),
141 etheraddr_string(ndo, p));
142
143 if (brcm_tag_print_full(ndo, p + 2*MAC_ADDR_LEN,
144 caplen - 2*MAC_ADDR_LEN))
145 return (1);
146
147 /* We printed the Ethernet destination and source addresses already */
148 save_eflag = ndo->ndo_eflag;
149 ndo->ndo_eflag = 0;
150
151 /* Parse the rest of the Ethernet header, and the frame payload,
152 * telling ether_hdr_len_print() how big the non-standard Ethernet
153 * header is.
154 *
155 * +-----------+-----------+----------------+--------------+
156 * | MAC DA (6)| MAC SA (6)|Broadcom tag (4)|Type/Length(2)|
157 * +-----------+-----------+----------------+--------------+
158 */
159 ret = ether_hdr_len_print(ndo, p, length, caplen, NULL, NULL,
160 2*MAC_ADDR_LEN + BRCM_TAG_LEN + ETHER_TYPE_LEN);
161 ndo->ndo_eflag = save_eflag;
162 return ret;
163 }
164
165 static void brcm_tag_print_encap(netdissect_options *ndo,
166 const u_char *p)
167 {
168 brcm_tag_print_full(ndo, p, BRCM_TAG_LEN);
169 }
170
171 u_int
172 brcm_tag_prepend_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
173 const u_char *p)
174 {
175 u_int caplen = h->caplen;
176 u_int length = h->len;
177
178 ndo->ndo_protocol = "brcm-tag-prepend";
179 if (caplen < BRCM_TAG_LEN) {
180 nd_print_trunc(ndo);
181 return (caplen);
182 }
183
184 if (length < BRCM_TAG_LEN) {
185 nd_print_trunc(ndo);
186 return (length);
187 }
188
189 /* Parse the Ethernet frame regularly and utilize the encapsulation
190 * header printing facility to pring the pre-pended Broadcom tag.
191 *
192 * +-----------------++-----------++-----------++--------------+
193 * | Broadcom tag (4)|| MAC DA (6)|| MAC SA (6)||Type/Length(2)|
194 * +-----------------++-----------++-----------++--------------+
195 */
196 return ether_print(ndo, p + BRCM_TAG_LEN,
197 length - BRCM_TAG_LEN,
198 caplen - BRCM_TAG_LEN, brcm_tag_print_encap, p);
199 }