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 #include <config.h>
25
26 #include "netdissect-stdinc.h"
27
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
30 #include "addrtoname.h"
31 #include "extract.h"
32
33 #define ETHER_TYPE_LEN 2
34
35 #define BRCM_TAG_LEN 4
36 #define BRCM_OPCODE_SHIFT 5
37 #define BRCM_OPCODE_MASK 0x7
38
39 /* Ingress fields */
40 #define BRCM_IG_TC_SHIFT 2
41 #define BRCM_IG_TC_MASK 0x7
42 #define BRCM_IG_TE_MASK 0x3
43 #define BRCM_IG_TS_SHIFT 7
44 #define BRCM_IG_DSTMAP_MASK 0x1ff
45
46 /* Egress fields */
47 #define BRCM_EG_CID_MASK 0xff
48 #define BRCM_EG_RC_MASK 0xff
49 #define BRCM_EG_RC_RSVD (3 << 6)
50 #define BRCM_EG_RC_EXCEPTION (1 << 5)
51 #define BRCM_EG_RC_PROT_SNOOP (1 << 4)
52 #define BRCM_EG_RC_PROT_TERM (1 << 3)
53 #define BRCM_EG_RC_SWITCH (1 << 2)
54 #define BRCM_EG_RC_MAC_LEARN (1 << 1)
55 #define BRCM_EG_RC_MIRROR (1 << 0)
56 #define BRCM_EG_TC_SHIFT 5
57 #define BRCM_EG_TC_MASK 0x7
58 #define BRCM_EG_PID_MASK 0x1f
59
60 static const struct tok brcm_tag_te_values[] = {
61 { 0, "None" },
62 { 1, "Untag" },
63 { 2, "Header"},
64 { 3, "Reserved" },
65 { 0, NULL }
66 };
67
68 static const struct tok brcm_tag_rc_values[] = {
69 { 1, "mirror" },
70 { 2, "MAC learning" },
71 { 4, "switching" },
72 { 8, "prot term" },
73 { 16, "prot snoop" },
74 { 32, "exception" },
75 { 0, NULL }
76 };
77
78 static void
brcm_tag_print(netdissect_options * ndo,const u_char * bp)79 brcm_tag_print(netdissect_options *ndo, const u_char *bp)
80 {
81 uint8_t tag[BRCM_TAG_LEN];
82 uint16_t dst_map;
83 unsigned int i;
84
85 for (i = 0; i < BRCM_TAG_LEN; i++)
86 tag[i] = GET_U_1(bp + i);
87
88 ND_PRINT("BRCM tag OP: %s", tag[0] ? "IG" : "EG");
89 if (tag[0] & (1 << BRCM_OPCODE_SHIFT)) {
90 /* Ingress Broadcom tag */
91 ND_PRINT(", TC: %d", (tag[1] >> BRCM_IG_TC_SHIFT) &
92 BRCM_IG_TC_MASK);
93 ND_PRINT(", TE: %s",
94 tok2str(brcm_tag_te_values, "unknown",
95 (tag[1] & BRCM_IG_TE_MASK)));
96 ND_PRINT(", TS: %d", tag[1] >> BRCM_IG_TS_SHIFT);
97 dst_map = (uint16_t)tag[2] << 8 | tag[3];
98 ND_PRINT(", DST map: 0x%04x", dst_map & BRCM_IG_DSTMAP_MASK);
99 } else {
100 /* Egress Broadcom tag */
101 ND_PRINT(", CID: %d", tag[1]);
102 ND_PRINT(", RC: %s", tok2str(brcm_tag_rc_values,
103 "reserved", tag[2]));
104 ND_PRINT(", TC: %d", (tag[3] >> BRCM_EG_TC_SHIFT) &
105 BRCM_EG_TC_MASK);
106 ND_PRINT(", port: %d", tag[3] & BRCM_EG_PID_MASK);
107 }
108 ND_PRINT(", ");
109 }
110
111 void
brcm_tag_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)112 brcm_tag_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
113 const u_char *p)
114 {
115 u_int caplen = h->caplen;
116 u_int length = h->len;
117
118 ndo->ndo_protocol = "brcm-tag";
119 ndo->ndo_ll_hdr_len +=
120 ether_switch_tag_print(ndo, p, length, caplen,
121 brcm_tag_print, BRCM_TAG_LEN);
122 }
123
124 void
brcm_tag_prepend_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)125 brcm_tag_prepend_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
126 const u_char *p)
127 {
128 u_int caplen = h->caplen;
129 u_int length = h->len;
130
131 ndo->ndo_protocol = "brcm-tag-prepend";
132 ND_TCHECK_LEN(p, BRCM_TAG_LEN);
133 ndo->ndo_ll_hdr_len += BRCM_TAG_LEN;
134
135 if (ndo->ndo_eflag) {
136 /* Print the prepended Broadcom tag. */
137 brcm_tag_print(ndo, p);
138 }
139 p += BRCM_TAG_LEN;
140 length -= BRCM_TAG_LEN;
141 caplen -= BRCM_TAG_LEN;
142
143 /*
144 * Now print the Ethernet frame following it.
145 */
146 ndo->ndo_ll_hdr_len +=
147 ether_print(ndo, p, length, caplen, NULL, NULL);
148 }
149