1*05b00f60SXin Li /*
2*05b00f60SXin Li * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
3*05b00f60SXin Li * The Regents of the University of California. All rights reserved.
4*05b00f60SXin Li *
5*05b00f60SXin Li * Redistribution and use in source and binary forms, with or without
6*05b00f60SXin Li * modification, are permitted provided that: (1) source code distributions
7*05b00f60SXin Li * retain the above copyright notice and this paragraph in its entirety, (2)
8*05b00f60SXin Li * distributions including binary code include the above copyright notice and
9*05b00f60SXin Li * this paragraph in its entirety in the documentation or other materials
10*05b00f60SXin Li * provided with the distribution, and (3) all advertising materials mentioning
11*05b00f60SXin Li * features or use of this software display the following acknowledgement:
12*05b00f60SXin Li * ``This product includes software developed by the University of California,
13*05b00f60SXin Li * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*05b00f60SXin Li * the University nor the names of its contributors may be used to endorse
15*05b00f60SXin Li * or promote products derived from this software without specific prior
16*05b00f60SXin Li * written permission.
17*05b00f60SXin Li * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*05b00f60SXin Li * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*05b00f60SXin Li * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*05b00f60SXin Li *
21*05b00f60SXin Li * Hacked version of print-ether.c Larry Lile <[email protected]>
22*05b00f60SXin Li *
23*05b00f60SXin Li * Further tweaked to more closely resemble print-fddi.c
24*05b00f60SXin Li * Guy Harris <[email protected]>
25*05b00f60SXin Li */
26*05b00f60SXin Li
27*05b00f60SXin Li /* \summary: Token Ring printer */
28*05b00f60SXin Li
29*05b00f60SXin Li #ifdef HAVE_CONFIG_H
30*05b00f60SXin Li #include <config.h>
31*05b00f60SXin Li #endif
32*05b00f60SXin Li
33*05b00f60SXin Li #include "netdissect-stdinc.h"
34*05b00f60SXin Li
35*05b00f60SXin Li #include <string.h>
36*05b00f60SXin Li
37*05b00f60SXin Li #include "netdissect.h"
38*05b00f60SXin Li #include "extract.h"
39*05b00f60SXin Li #include "addrtoname.h"
40*05b00f60SXin Li
41*05b00f60SXin Li /*
42*05b00f60SXin Li * Copyright (c) 1998, Larry Lile
43*05b00f60SXin Li * All rights reserved.
44*05b00f60SXin Li *
45*05b00f60SXin Li * Redistribution and use in source and binary forms, with or without
46*05b00f60SXin Li * modification, are permitted provided that the following conditions
47*05b00f60SXin Li * are met:
48*05b00f60SXin Li * 1. Redistributions of source code must retain the above copyright
49*05b00f60SXin Li * notice unmodified, this list of conditions, and the following
50*05b00f60SXin Li * disclaimer.
51*05b00f60SXin Li * 2. Redistributions in binary form must reproduce the above copyright
52*05b00f60SXin Li * notice, this list of conditions and the following disclaimer in the
53*05b00f60SXin Li * documentation and/or other materials provided with the distribution.
54*05b00f60SXin Li *
55*05b00f60SXin Li * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56*05b00f60SXin Li * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57*05b00f60SXin Li * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58*05b00f60SXin Li * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59*05b00f60SXin Li * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60*05b00f60SXin Li * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61*05b00f60SXin Li * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62*05b00f60SXin Li * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63*05b00f60SXin Li * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64*05b00f60SXin Li * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65*05b00f60SXin Li * SUCH DAMAGE.
66*05b00f60SXin Li *
67*05b00f60SXin Li */
68*05b00f60SXin Li
69*05b00f60SXin Li #define TOKEN_HDRLEN 14
70*05b00f60SXin Li #define ROUTING_SEGMENT_MAX 16
71*05b00f60SXin Li #define IS_SOURCE_ROUTED(trp) ((trp)->token_shost[0] & 0x80)
72*05b00f60SXin Li #define FRAME_TYPE(trp) ((GET_U_1((trp)->token_fc) & 0xC0) >> 6)
73*05b00f60SXin Li #define TOKEN_FC_LLC 1
74*05b00f60SXin Li
75*05b00f60SXin Li #define BROADCAST(trp) ((GET_BE_U_2((trp)->token_rcf) & 0xE000) >> 13)
76*05b00f60SXin Li #define RIF_LENGTH(trp) ((GET_BE_U_2((trp)->token_rcf) & 0x1f00) >> 8)
77*05b00f60SXin Li #define DIRECTION(trp) ((GET_BE_U_2((trp)->token_rcf) & 0x0080) >> 7)
78*05b00f60SXin Li #define LARGEST_FRAME(trp) ((GET_BE_U_2((trp)->token_rcf) & 0x0070) >> 4)
79*05b00f60SXin Li #define RING_NUMBER(trp, x) ((GET_BE_U_2((trp)->token_rseg[x]) & 0xfff0) >> 4)
80*05b00f60SXin Li #define BRIDGE_NUMBER(trp, x) (GET_BE_U_2((trp)->token_rseg[x]) & 0x000f)
81*05b00f60SXin Li #define SEGMENT_COUNT(trp) ((int)((RIF_LENGTH(trp) - 2) / 2))
82*05b00f60SXin Li
83*05b00f60SXin Li struct token_header {
84*05b00f60SXin Li nd_uint8_t token_ac;
85*05b00f60SXin Li nd_uint8_t token_fc;
86*05b00f60SXin Li nd_mac_addr token_dhost;
87*05b00f60SXin Li nd_mac_addr token_shost;
88*05b00f60SXin Li nd_uint16_t token_rcf;
89*05b00f60SXin Li nd_uint16_t token_rseg[ROUTING_SEGMENT_MAX];
90*05b00f60SXin Li };
91*05b00f60SXin Li
92*05b00f60SXin Li
93*05b00f60SXin Li /* Extract src, dst addresses */
94*05b00f60SXin Li static void
extract_token_addrs(const struct token_header * trp,char * fsrc,char * fdst)95*05b00f60SXin Li extract_token_addrs(const struct token_header *trp, char *fsrc, char *fdst)
96*05b00f60SXin Li {
97*05b00f60SXin Li memcpy(fdst, (const char *)trp->token_dhost, 6);
98*05b00f60SXin Li memcpy(fsrc, (const char *)trp->token_shost, 6);
99*05b00f60SXin Li }
100*05b00f60SXin Li
101*05b00f60SXin Li /*
102*05b00f60SXin Li * Print the TR MAC header
103*05b00f60SXin Li */
104*05b00f60SXin Li static void
token_hdr_print(netdissect_options * ndo,const struct token_header * trp,u_int length,const u_char * fsrc,const u_char * fdst)105*05b00f60SXin Li token_hdr_print(netdissect_options *ndo,
106*05b00f60SXin Li const struct token_header *trp, u_int length,
107*05b00f60SXin Li const u_char *fsrc, const u_char *fdst)
108*05b00f60SXin Li {
109*05b00f60SXin Li const char *srcname, *dstname;
110*05b00f60SXin Li
111*05b00f60SXin Li srcname = etheraddr_string(ndo, fsrc);
112*05b00f60SXin Li dstname = etheraddr_string(ndo, fdst);
113*05b00f60SXin Li
114*05b00f60SXin Li if (!ndo->ndo_qflag)
115*05b00f60SXin Li ND_PRINT("%02x %02x ",
116*05b00f60SXin Li GET_U_1(trp->token_ac),
117*05b00f60SXin Li GET_U_1(trp->token_fc));
118*05b00f60SXin Li ND_PRINT("%s > %s, length %u: ",
119*05b00f60SXin Li srcname, dstname,
120*05b00f60SXin Li length);
121*05b00f60SXin Li }
122*05b00f60SXin Li
123*05b00f60SXin Li static const char *broadcast_indicator[] = {
124*05b00f60SXin Li "Non-Broadcast", "Non-Broadcast",
125*05b00f60SXin Li "Non-Broadcast", "Non-Broadcast",
126*05b00f60SXin Li "All-routes", "All-routes",
127*05b00f60SXin Li "Single-route", "Single-route"
128*05b00f60SXin Li };
129*05b00f60SXin Li
130*05b00f60SXin Li static const char *direction[] = {
131*05b00f60SXin Li "Forward", "Backward"
132*05b00f60SXin Li };
133*05b00f60SXin Li
134*05b00f60SXin Li static const char *largest_frame[] = {
135*05b00f60SXin Li "516",
136*05b00f60SXin Li "1500",
137*05b00f60SXin Li "2052",
138*05b00f60SXin Li "4472",
139*05b00f60SXin Li "8144",
140*05b00f60SXin Li "11407",
141*05b00f60SXin Li "17800",
142*05b00f60SXin Li "??"
143*05b00f60SXin Li };
144*05b00f60SXin Li
145*05b00f60SXin Li u_int
token_print(netdissect_options * ndo,const u_char * p,u_int length,u_int caplen)146*05b00f60SXin Li token_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
147*05b00f60SXin Li {
148*05b00f60SXin Li const struct token_header *trp;
149*05b00f60SXin Li int llc_hdrlen;
150*05b00f60SXin Li nd_mac_addr srcmac, dstmac;
151*05b00f60SXin Li struct lladdr_info src, dst;
152*05b00f60SXin Li u_int route_len = 0, hdr_len = TOKEN_HDRLEN;
153*05b00f60SXin Li int seg;
154*05b00f60SXin Li
155*05b00f60SXin Li ndo->ndo_protocol = "token-ring";
156*05b00f60SXin Li trp = (const struct token_header *)p;
157*05b00f60SXin Li
158*05b00f60SXin Li if (caplen < TOKEN_HDRLEN) {
159*05b00f60SXin Li nd_print_trunc(ndo);
160*05b00f60SXin Li return hdr_len;
161*05b00f60SXin Li }
162*05b00f60SXin Li
163*05b00f60SXin Li /*
164*05b00f60SXin Li * Get the TR addresses into a canonical form
165*05b00f60SXin Li */
166*05b00f60SXin Li extract_token_addrs(trp, (char*)srcmac, (char*)dstmac);
167*05b00f60SXin Li
168*05b00f60SXin Li /* Adjust for source routing information in the MAC header */
169*05b00f60SXin Li if (IS_SOURCE_ROUTED(trp)) {
170*05b00f60SXin Li /* Clear source-routed bit */
171*05b00f60SXin Li srcmac[0] &= 0x7f;
172*05b00f60SXin Li
173*05b00f60SXin Li if (ndo->ndo_eflag)
174*05b00f60SXin Li token_hdr_print(ndo, trp, length, srcmac, dstmac);
175*05b00f60SXin Li
176*05b00f60SXin Li if (caplen < TOKEN_HDRLEN + 2) {
177*05b00f60SXin Li nd_print_trunc(ndo);
178*05b00f60SXin Li return hdr_len;
179*05b00f60SXin Li }
180*05b00f60SXin Li route_len = RIF_LENGTH(trp);
181*05b00f60SXin Li hdr_len += route_len;
182*05b00f60SXin Li if (caplen < hdr_len) {
183*05b00f60SXin Li nd_print_trunc(ndo);
184*05b00f60SXin Li return hdr_len;
185*05b00f60SXin Li }
186*05b00f60SXin Li if (ndo->ndo_vflag) {
187*05b00f60SXin Li ND_PRINT("%s ", broadcast_indicator[BROADCAST(trp)]);
188*05b00f60SXin Li ND_PRINT("%s", direction[DIRECTION(trp)]);
189*05b00f60SXin Li
190*05b00f60SXin Li for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
191*05b00f60SXin Li ND_PRINT(" [%u:%u]", RING_NUMBER(trp, seg),
192*05b00f60SXin Li BRIDGE_NUMBER(trp, seg));
193*05b00f60SXin Li } else {
194*05b00f60SXin Li ND_PRINT("rt = %x", GET_BE_U_2(trp->token_rcf));
195*05b00f60SXin Li
196*05b00f60SXin Li for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
197*05b00f60SXin Li ND_PRINT(":%x",
198*05b00f60SXin Li GET_BE_U_2(trp->token_rseg[seg]));
199*05b00f60SXin Li }
200*05b00f60SXin Li ND_PRINT(" (%s) ", largest_frame[LARGEST_FRAME(trp)]);
201*05b00f60SXin Li } else {
202*05b00f60SXin Li if (ndo->ndo_eflag)
203*05b00f60SXin Li token_hdr_print(ndo, trp, length, srcmac, dstmac);
204*05b00f60SXin Li }
205*05b00f60SXin Li
206*05b00f60SXin Li src.addr = srcmac;
207*05b00f60SXin Li src.addr_string = etheraddr_string;
208*05b00f60SXin Li dst.addr = dstmac;
209*05b00f60SXin Li dst.addr_string = etheraddr_string;
210*05b00f60SXin Li
211*05b00f60SXin Li /* Skip over token ring MAC header and routing information */
212*05b00f60SXin Li length -= hdr_len;
213*05b00f60SXin Li p += hdr_len;
214*05b00f60SXin Li caplen -= hdr_len;
215*05b00f60SXin Li
216*05b00f60SXin Li /* Frame Control field determines interpretation of packet */
217*05b00f60SXin Li if (FRAME_TYPE(trp) == TOKEN_FC_LLC) {
218*05b00f60SXin Li /* Try to print the LLC-layer header & higher layers */
219*05b00f60SXin Li llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
220*05b00f60SXin Li if (llc_hdrlen < 0) {
221*05b00f60SXin Li /* packet type not known, print raw packet */
222*05b00f60SXin Li if (!ndo->ndo_suppress_default_print)
223*05b00f60SXin Li ND_DEFAULTPRINT(p, caplen);
224*05b00f60SXin Li llc_hdrlen = -llc_hdrlen;
225*05b00f60SXin Li }
226*05b00f60SXin Li hdr_len += llc_hdrlen;
227*05b00f60SXin Li } else {
228*05b00f60SXin Li /* Some kinds of TR packet we cannot handle intelligently */
229*05b00f60SXin Li /* XXX - dissect MAC packets if frame type is 0 */
230*05b00f60SXin Li if (!ndo->ndo_eflag)
231*05b00f60SXin Li token_hdr_print(ndo, trp, length + TOKEN_HDRLEN + route_len,
232*05b00f60SXin Li srcmac, dstmac);
233*05b00f60SXin Li if (!ndo->ndo_suppress_default_print)
234*05b00f60SXin Li ND_DEFAULTPRINT(p, caplen);
235*05b00f60SXin Li }
236*05b00f60SXin Li return (hdr_len);
237*05b00f60SXin Li }
238*05b00f60SXin Li
239*05b00f60SXin Li /*
240*05b00f60SXin Li * This is the top level routine of the printer. 'p' points
241*05b00f60SXin Li * to the TR header of the packet, 'h->ts' is the timestamp,
242*05b00f60SXin Li * 'h->len' is the length of the packet off the wire, and 'h->caplen'
243*05b00f60SXin Li * is the number of bytes actually captured.
244*05b00f60SXin Li */
245*05b00f60SXin Li void
token_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)246*05b00f60SXin Li token_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
247*05b00f60SXin Li {
248*05b00f60SXin Li ndo->ndo_protocol = "token-ring";
249*05b00f60SXin Li ndo->ndo_ll_hdr_len += token_print(ndo, p, h->len, h->caplen);
250*05b00f60SXin Li }
251