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