xref: /dflybsd-src/contrib/tcpdump/print-token.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*
241c99275SPeter Avalos  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
341c99275SPeter Avalos  *	The Regents of the University of California.  All rights reserved.
441c99275SPeter Avalos  *
541c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
641c99275SPeter Avalos  * modification, are permitted provided that: (1) source code distributions
741c99275SPeter Avalos  * retain the above copyright notice and this paragraph in its entirety, (2)
841c99275SPeter Avalos  * distributions including binary code include the above copyright notice and
941c99275SPeter Avalos  * this paragraph in its entirety in the documentation or other materials
1041c99275SPeter Avalos  * provided with the distribution, and (3) all advertising materials mentioning
1141c99275SPeter Avalos  * features or use of this software display the following acknowledgement:
1241c99275SPeter Avalos  * ``This product includes software developed by the University of California,
1341c99275SPeter Avalos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1441c99275SPeter Avalos  * the University nor the names of its contributors may be used to endorse
1541c99275SPeter Avalos  * or promote products derived from this software without specific prior
1641c99275SPeter Avalos  * written permission.
1741c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1841c99275SPeter Avalos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1941c99275SPeter Avalos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2041c99275SPeter Avalos  *
2141c99275SPeter Avalos  * Hacked version of print-ether.c  Larry Lile <lile@stdio.com>
2241c99275SPeter Avalos  *
2341c99275SPeter Avalos  * Further tweaked to more closely resemble print-fddi.c
2441c99275SPeter Avalos  *	Guy Harris <guy@alum.mit.edu>
2541c99275SPeter Avalos  */
26411677aeSAaron LI 
27411677aeSAaron LI /* \summary: Token Ring printer */
2841c99275SPeter Avalos 
2941c99275SPeter Avalos #ifdef HAVE_CONFIG_H
30*ed775ee7SAntonio Huete Jimenez #include <config.h>
3141c99275SPeter Avalos #endif
3241c99275SPeter Avalos 
33*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
3441c99275SPeter Avalos 
3541c99275SPeter Avalos #include <string.h>
3641c99275SPeter Avalos 
37411677aeSAaron LI #include "netdissect.h"
3827bfbee1SPeter Avalos #include "extract.h"
3941c99275SPeter Avalos #include "addrtoname.h"
40411677aeSAaron LI 
41411677aeSAaron LI /*
42411677aeSAaron LI  * Copyright (c) 1998, Larry Lile
43411677aeSAaron LI  * All rights reserved.
44411677aeSAaron LI  *
45411677aeSAaron LI  * Redistribution and use in source and binary forms, with or without
46411677aeSAaron LI  * modification, are permitted provided that the following conditions
47411677aeSAaron LI  * are met:
48411677aeSAaron LI  * 1. Redistributions of source code must retain the above copyright
49411677aeSAaron LI  *    notice unmodified, this list of conditions, and the following
50411677aeSAaron LI  *    disclaimer.
51411677aeSAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
52411677aeSAaron LI  *    notice, this list of conditions and the following disclaimer in the
53411677aeSAaron LI  *    documentation and/or other materials provided with the distribution.
54411677aeSAaron LI  *
55411677aeSAaron LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56411677aeSAaron LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57411677aeSAaron LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58411677aeSAaron LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59411677aeSAaron LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60411677aeSAaron LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61411677aeSAaron LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62411677aeSAaron LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63411677aeSAaron LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64411677aeSAaron LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65411677aeSAaron LI  * SUCH DAMAGE.
66411677aeSAaron LI  *
67411677aeSAaron LI  */
68411677aeSAaron LI 
69411677aeSAaron LI #define TOKEN_HDRLEN		14
70411677aeSAaron LI #define ROUTING_SEGMENT_MAX	16
71411677aeSAaron LI #define IS_SOURCE_ROUTED(trp)	((trp)->token_shost[0] & 0x80)
72*ed775ee7SAntonio Huete Jimenez #define FRAME_TYPE(trp)		((GET_U_1((trp)->token_fc) & 0xC0) >> 6)
73411677aeSAaron LI #define TOKEN_FC_LLC		1
74411677aeSAaron LI 
75*ed775ee7SAntonio Huete Jimenez #define BROADCAST(trp)		((GET_BE_U_2((trp)->token_rcf) & 0xE000) >> 13)
76*ed775ee7SAntonio Huete Jimenez #define RIF_LENGTH(trp)		((GET_BE_U_2((trp)->token_rcf) & 0x1f00) >> 8)
77*ed775ee7SAntonio Huete Jimenez #define DIRECTION(trp)		((GET_BE_U_2((trp)->token_rcf) & 0x0080) >> 7)
78*ed775ee7SAntonio Huete Jimenez #define LARGEST_FRAME(trp)	((GET_BE_U_2((trp)->token_rcf) & 0x0070) >> 4)
79*ed775ee7SAntonio Huete Jimenez #define RING_NUMBER(trp, x)	((GET_BE_U_2((trp)->token_rseg[x]) & 0xfff0) >> 4)
80*ed775ee7SAntonio Huete Jimenez #define BRIDGE_NUMBER(trp, x)	(GET_BE_U_2((trp)->token_rseg[x]) & 0x000f)
81411677aeSAaron LI #define SEGMENT_COUNT(trp)	((int)((RIF_LENGTH(trp) - 2) / 2))
82411677aeSAaron LI 
83411677aeSAaron LI struct token_header {
84*ed775ee7SAntonio Huete Jimenez 	nd_uint8_t   token_ac;
85*ed775ee7SAntonio Huete Jimenez 	nd_uint8_t   token_fc;
86*ed775ee7SAntonio Huete Jimenez 	nd_mac_addr  token_dhost;
87*ed775ee7SAntonio Huete Jimenez 	nd_mac_addr  token_shost;
88*ed775ee7SAntonio Huete Jimenez 	nd_uint16_t  token_rcf;
89*ed775ee7SAntonio Huete Jimenez 	nd_uint16_t  token_rseg[ROUTING_SEGMENT_MAX];
90411677aeSAaron LI };
91411677aeSAaron LI 
9241c99275SPeter Avalos 
9341c99275SPeter Avalos /* Extract src, dst addresses */
94*ed775ee7SAntonio Huete Jimenez static void
extract_token_addrs(const struct token_header * trp,char * fsrc,char * fdst)9541c99275SPeter Avalos extract_token_addrs(const struct token_header *trp, char *fsrc, char *fdst)
9641c99275SPeter Avalos {
9741c99275SPeter Avalos 	memcpy(fdst, (const char *)trp->token_dhost, 6);
9841c99275SPeter Avalos 	memcpy(fsrc, (const char *)trp->token_shost, 6);
9941c99275SPeter Avalos }
10041c99275SPeter Avalos 
10141c99275SPeter Avalos /*
10241c99275SPeter Avalos  * Print the TR MAC header
10341c99275SPeter Avalos  */
104*ed775ee7SAntonio Huete Jimenez static void
token_hdr_print(netdissect_options * ndo,const struct token_header * trp,u_int length,const u_char * fsrc,const u_char * fdst)105411677aeSAaron LI token_hdr_print(netdissect_options *ndo,
106*ed775ee7SAntonio Huete Jimenez                 const struct token_header *trp, u_int length,
107*ed775ee7SAntonio Huete Jimenez                 const u_char *fsrc, const u_char *fdst)
10841c99275SPeter Avalos {
10941c99275SPeter Avalos 	const char *srcname, *dstname;
11041c99275SPeter Avalos 
111411677aeSAaron LI 	srcname = etheraddr_string(ndo, fsrc);
112411677aeSAaron LI 	dstname = etheraddr_string(ndo, fdst);
11341c99275SPeter Avalos 
114411677aeSAaron LI 	if (!ndo->ndo_qflag)
115*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%02x %02x ",
116*ed775ee7SAntonio Huete Jimenez 		       GET_U_1(trp->token_ac),
117*ed775ee7SAntonio Huete Jimenez 		       GET_U_1(trp->token_fc));
118*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("%s > %s, length %u: ",
11941c99275SPeter Avalos 	       srcname, dstname,
120*ed775ee7SAntonio Huete Jimenez 	       length);
12141c99275SPeter Avalos }
12241c99275SPeter Avalos 
12341c99275SPeter Avalos static const char *broadcast_indicator[] = {
12441c99275SPeter Avalos 	"Non-Broadcast", "Non-Broadcast",
12541c99275SPeter Avalos 	"Non-Broadcast", "Non-Broadcast",
12641c99275SPeter Avalos 	"All-routes",    "All-routes",
12741c99275SPeter Avalos 	"Single-route",  "Single-route"
12841c99275SPeter Avalos };
12941c99275SPeter Avalos 
13041c99275SPeter Avalos static const char *direction[] = {
13141c99275SPeter Avalos 	"Forward", "Backward"
13241c99275SPeter Avalos };
13341c99275SPeter Avalos 
13441c99275SPeter Avalos static const char *largest_frame[] = {
13541c99275SPeter Avalos 	"516",
13641c99275SPeter Avalos 	"1500",
13741c99275SPeter Avalos 	"2052",
13841c99275SPeter Avalos 	"4472",
13941c99275SPeter Avalos 	"8144",
14041c99275SPeter Avalos 	"11407",
14141c99275SPeter Avalos 	"17800",
14241c99275SPeter Avalos 	"??"
14341c99275SPeter Avalos };
14441c99275SPeter Avalos 
14541c99275SPeter Avalos u_int
token_print(netdissect_options * ndo,const u_char * p,u_int length,u_int caplen)146411677aeSAaron LI token_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
14741c99275SPeter Avalos {
14841c99275SPeter Avalos 	const struct token_header *trp;
149411677aeSAaron LI 	int llc_hdrlen;
150*ed775ee7SAntonio Huete Jimenez 	nd_mac_addr srcmac, dstmac;
151411677aeSAaron LI 	struct lladdr_info src, dst;
15241c99275SPeter Avalos 	u_int route_len = 0, hdr_len = TOKEN_HDRLEN;
15341c99275SPeter Avalos 	int seg;
15441c99275SPeter Avalos 
155*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "token-ring";
15641c99275SPeter Avalos 	trp = (const struct token_header *)p;
15741c99275SPeter Avalos 
15841c99275SPeter Avalos 	if (caplen < TOKEN_HDRLEN) {
159*ed775ee7SAntonio Huete Jimenez 		nd_print_trunc(ndo);
16041c99275SPeter Avalos 		return hdr_len;
16141c99275SPeter Avalos 	}
16241c99275SPeter Avalos 
16341c99275SPeter Avalos 	/*
16441c99275SPeter Avalos 	 * Get the TR addresses into a canonical form
16541c99275SPeter Avalos 	 */
166*ed775ee7SAntonio Huete Jimenez 	extract_token_addrs(trp, (char*)srcmac, (char*)dstmac);
16741c99275SPeter Avalos 
16841c99275SPeter Avalos 	/* Adjust for source routing information in the MAC header */
16941c99275SPeter Avalos 	if (IS_SOURCE_ROUTED(trp)) {
17041c99275SPeter Avalos 		/* Clear source-routed bit */
171*ed775ee7SAntonio Huete Jimenez 		srcmac[0] &= 0x7f;
17241c99275SPeter Avalos 
173411677aeSAaron LI 		if (ndo->ndo_eflag)
174*ed775ee7SAntonio Huete Jimenez 			token_hdr_print(ndo, trp, length, srcmac, dstmac);
17541c99275SPeter Avalos 
17627bfbee1SPeter Avalos 		if (caplen < TOKEN_HDRLEN + 2) {
177*ed775ee7SAntonio Huete Jimenez 			nd_print_trunc(ndo);
17827bfbee1SPeter Avalos 			return hdr_len;
17927bfbee1SPeter Avalos 		}
18041c99275SPeter Avalos 		route_len = RIF_LENGTH(trp);
18127bfbee1SPeter Avalos 		hdr_len += route_len;
18227bfbee1SPeter Avalos 		if (caplen < hdr_len) {
183*ed775ee7SAntonio Huete Jimenez 			nd_print_trunc(ndo);
18427bfbee1SPeter Avalos 			return hdr_len;
18527bfbee1SPeter Avalos 		}
186411677aeSAaron LI 		if (ndo->ndo_vflag) {
187*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("%s ", broadcast_indicator[BROADCAST(trp)]);
188*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("%s", direction[DIRECTION(trp)]);
18941c99275SPeter Avalos 
19041c99275SPeter Avalos 			for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
191*ed775ee7SAntonio Huete Jimenez 				ND_PRINT(" [%u:%u]", RING_NUMBER(trp, seg),
192*ed775ee7SAntonio Huete Jimenez 				    BRIDGE_NUMBER(trp, seg));
19341c99275SPeter Avalos 		} else {
194*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("rt = %x", GET_BE_U_2(trp->token_rcf));
19541c99275SPeter Avalos 
19641c99275SPeter Avalos 			for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
197*ed775ee7SAntonio Huete Jimenez 				ND_PRINT(":%x",
198*ed775ee7SAntonio Huete Jimenez 					 GET_BE_U_2(trp->token_rseg[seg]));
19941c99275SPeter Avalos 		}
200*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" (%s) ", largest_frame[LARGEST_FRAME(trp)]);
20141c99275SPeter Avalos 	} else {
202411677aeSAaron LI 		if (ndo->ndo_eflag)
203*ed775ee7SAntonio Huete Jimenez 			token_hdr_print(ndo, trp, length, srcmac, dstmac);
20441c99275SPeter Avalos 	}
20541c99275SPeter Avalos 
206*ed775ee7SAntonio Huete Jimenez 	src.addr = srcmac;
207411677aeSAaron LI 	src.addr_string = etheraddr_string;
208*ed775ee7SAntonio Huete Jimenez 	dst.addr = dstmac;
209411677aeSAaron LI 	dst.addr_string = etheraddr_string;
210411677aeSAaron LI 
21141c99275SPeter Avalos 	/* Skip over token ring MAC header and routing information */
21241c99275SPeter Avalos 	length -= hdr_len;
21341c99275SPeter Avalos 	p += hdr_len;
21441c99275SPeter Avalos 	caplen -= hdr_len;
21541c99275SPeter Avalos 
21641c99275SPeter Avalos 	/* Frame Control field determines interpretation of packet */
21741c99275SPeter Avalos 	if (FRAME_TYPE(trp) == TOKEN_FC_LLC) {
21841c99275SPeter Avalos 		/* Try to print the LLC-layer header & higher layers */
219411677aeSAaron LI 		llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
220411677aeSAaron LI 		if (llc_hdrlen < 0) {
221411677aeSAaron LI 			/* packet type not known, print raw packet */
222411677aeSAaron LI 			if (!ndo->ndo_suppress_default_print)
223411677aeSAaron LI 				ND_DEFAULTPRINT(p, caplen);
224411677aeSAaron LI 			llc_hdrlen = -llc_hdrlen;
22541c99275SPeter Avalos 		}
226411677aeSAaron LI 		hdr_len += llc_hdrlen;
22741c99275SPeter Avalos 	} else {
22841c99275SPeter Avalos 		/* Some kinds of TR packet we cannot handle intelligently */
22941c99275SPeter Avalos 		/* XXX - dissect MAC packets if frame type is 0 */
230411677aeSAaron LI 		if (!ndo->ndo_eflag)
231411677aeSAaron LI 			token_hdr_print(ndo, trp, length + TOKEN_HDRLEN + route_len,
232*ed775ee7SAntonio Huete Jimenez 			    srcmac, dstmac);
233411677aeSAaron LI 		if (!ndo->ndo_suppress_default_print)
234411677aeSAaron LI 			ND_DEFAULTPRINT(p, caplen);
23541c99275SPeter Avalos 	}
23641c99275SPeter Avalos 	return (hdr_len);
23741c99275SPeter Avalos }
23841c99275SPeter Avalos 
23941c99275SPeter Avalos /*
24041c99275SPeter Avalos  * This is the top level routine of the printer.  'p' points
24141c99275SPeter Avalos  * to the TR header of the packet, 'h->ts' is the timestamp,
24241c99275SPeter Avalos  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
24341c99275SPeter Avalos  * is the number of bytes actually captured.
24441c99275SPeter Avalos  */
245*ed775ee7SAntonio Huete Jimenez void
token_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)246411677aeSAaron LI token_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
24741c99275SPeter Avalos {
248*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "token-ring";
249*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_ll_hdr_len += token_print(ndo, p, h->len, h->caplen);
25041c99275SPeter Avalos }
251