xref: /netbsd-src/external/bsd/tcpdump/dist/print-token.c (revision 88fcb00c0357f2d7c1774f86a352637bfda96184)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
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  * Hacked version of print-ether.c  Larry Lile <lile@stdio.com>
22  *
23  * Further tweaked to more closely resemble print-fddi.c
24  *	Guy Harris <guy@alum.mit.edu>
25  */
26 #include <sys/cdefs.h>
27 #ifndef lint
28 #if 0
29 static const char rcsid[] _U_ =
30     "@(#) Header: /tcpdump/master/tcpdump/print-token.c,v 1.27 2005-11-13 12:12:43 guy Exp";
31 #else
32 __RCSID("$NetBSD: print-token.c,v 1.2 2010/12/05 05:11:31 christos Exp $");
33 #endif
34 #endif
35 
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #include <tcpdump-stdinc.h>
41 
42 #include <pcap.h>
43 #include <stdio.h>
44 #include <string.h>
45 
46 #include "interface.h"
47 #include "extract.h"
48 #include "addrtoname.h"
49 #include "ethertype.h"
50 
51 #include "ether.h"
52 #include "token.h"
53 
54 /* Extract src, dst addresses */
55 static inline void
56 extract_token_addrs(const struct token_header *trp, char *fsrc, char *fdst)
57 {
58 	memcpy(fdst, (const char *)trp->token_dhost, 6);
59 	memcpy(fsrc, (const char *)trp->token_shost, 6);
60 }
61 
62 /*
63  * Print the TR MAC header
64  */
65 static inline void
66 token_hdr_print(register const struct token_header *trp, register u_int length,
67 	   register const u_char *fsrc, register const u_char *fdst)
68 {
69 	const char *srcname, *dstname;
70 
71 	srcname = etheraddr_string(fsrc);
72 	dstname = etheraddr_string(fdst);
73 
74 	if (vflag)
75 		(void) printf("%02x %02x %s %s %d: ",
76 		       trp->token_ac,
77 		       trp->token_fc,
78 		       srcname, dstname,
79 		       length);
80 	else
81 		printf("%s %s %d: ", srcname, dstname, length);
82 }
83 
84 static const char *broadcast_indicator[] = {
85 	"Non-Broadcast", "Non-Broadcast",
86 	"Non-Broadcast", "Non-Broadcast",
87 	"All-routes",    "All-routes",
88 	"Single-route",  "Single-route"
89 };
90 
91 static const char *direction[] = {
92 	"Forward", "Backward"
93 };
94 
95 static const char *largest_frame[] = {
96 	"516",
97 	"1500",
98 	"2052",
99 	"4472",
100 	"8144",
101 	"11407",
102 	"17800",
103 	"??"
104 };
105 
106 u_int
107 token_print(const u_char *p, u_int length, u_int caplen)
108 {
109 	const struct token_header *trp;
110 	u_short extracted_ethertype;
111 	struct ether_header ehdr;
112 	u_int route_len = 0, hdr_len = TOKEN_HDRLEN;
113 	int seg;
114 
115 	trp = (const struct token_header *)p;
116 
117 	if (caplen < TOKEN_HDRLEN) {
118 		printf("[|token-ring]");
119 		return hdr_len;
120 	}
121 
122 	/*
123 	 * Get the TR addresses into a canonical form
124 	 */
125 	extract_token_addrs(trp, (char*)ESRC(&ehdr), (char*)EDST(&ehdr));
126 
127 	/* Adjust for source routing information in the MAC header */
128 	if (IS_SOURCE_ROUTED(trp)) {
129 		/* Clear source-routed bit */
130 		*ESRC(&ehdr) &= 0x7f;
131 
132 		if (eflag)
133 			token_hdr_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
134 
135 		if (caplen < TOKEN_HDRLEN + 2) {
136 			printf("[|token-ring]");
137 			return hdr_len;
138 		}
139 		route_len = RIF_LENGTH(trp);
140 		hdr_len += route_len;
141 		if (caplen < hdr_len) {
142 			printf("[|token-ring]");
143 			return hdr_len;
144 		}
145 		if (vflag) {
146 			printf("%s ", broadcast_indicator[BROADCAST(trp)]);
147 			printf("%s", direction[DIRECTION(trp)]);
148 
149 			for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
150 				printf(" [%d:%d]", RING_NUMBER(trp, seg),
151 				    BRIDGE_NUMBER(trp, seg));
152 		} else {
153 			printf("rt = %x", EXTRACT_16BITS(&trp->token_rcf));
154 
155 			for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
156 				printf(":%x", EXTRACT_16BITS(&trp->token_rseg[seg]));
157 		}
158 		printf(" (%s) ", largest_frame[LARGEST_FRAME(trp)]);
159 	} else {
160 		if (eflag)
161 			token_hdr_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
162 	}
163 
164 	/* Skip over token ring MAC header and routing information */
165 	length -= hdr_len;
166 	p += hdr_len;
167 	caplen -= hdr_len;
168 
169 	/* Frame Control field determines interpretation of packet */
170 	if (FRAME_TYPE(trp) == TOKEN_FC_LLC) {
171 		/* Try to print the LLC-layer header & higher layers */
172 		if (llc_print(p, length, caplen, ESRC(&ehdr), EDST(&ehdr),
173 		    &extracted_ethertype) == 0) {
174 			/* ether_type not known, print raw packet */
175 			if (!eflag)
176 				token_hdr_print(trp,
177 				    length + TOKEN_HDRLEN + route_len,
178 				    ESRC(&ehdr), EDST(&ehdr));
179 			if (extracted_ethertype) {
180 				printf("(LLC %s) ",
181 			etherproto_string(htons(extracted_ethertype)));
182 			}
183 			if (!suppress_default_print)
184 				default_print(p, caplen);
185 		}
186 	} else {
187 		/* Some kinds of TR packet we cannot handle intelligently */
188 		/* XXX - dissect MAC packets if frame type is 0 */
189 		if (!eflag)
190 			token_hdr_print(trp, length + TOKEN_HDRLEN + route_len,
191 			    ESRC(&ehdr), EDST(&ehdr));
192 		if (!suppress_default_print)
193 			default_print(p, caplen);
194 	}
195 	return (hdr_len);
196 }
197 
198 /*
199  * This is the top level routine of the printer.  'p' points
200  * to the TR header of the packet, 'h->ts' is the timestamp,
201  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
202  * is the number of bytes actually captured.
203  */
204 u_int
205 token_if_print(const struct pcap_pkthdr *h, const u_char *p)
206 {
207 	return (token_print(p, h->len, h->caplen));
208 }
209