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