xref: /netbsd-src/external/bsd/tcpdump/dist/print-token.c (revision cc576e1d8e4f4078fd4e81238abca9fca216f6ec)
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 
27 #include <sys/cdefs.h>
28 #ifndef lint
29 __RCSID("$NetBSD: print-token.c,v 1.5 2017/01/24 23:29:14 christos Exp $");
30 #endif
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <netdissect-stdinc.h>
37 
38 #include <string.h>
39 
40 #include "netdissect.h"
41 #include "extract.h"
42 #include "addrtoname.h"
43 #include "ether.h"
44 
45 /*
46  * Copyright (c) 1998, Larry Lile
47  * All rights reserved.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  *    notice unmodified, this list of conditions, and the following
54  *    disclaimer.
55  * 2. Redistributions in binary form must reproduce the above copyright
56  *    notice, this list of conditions and the following disclaimer in the
57  *    documentation and/or other materials provided with the distribution.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  */
72 
73 #define TOKEN_HDRLEN		14
74 #define TOKEN_RING_MAC_LEN	6
75 #define ROUTING_SEGMENT_MAX	16
76 #define IS_SOURCE_ROUTED(trp)	((trp)->token_shost[0] & 0x80)
77 #define FRAME_TYPE(trp)		(((trp)->token_fc & 0xC0) >> 6)
78 #define TOKEN_FC_LLC		1
79 
80 #define BROADCAST(trp)		((EXTRACT_16BITS(&(trp)->token_rcf) & 0xE000) >> 13)
81 #define RIF_LENGTH(trp)		((EXTRACT_16BITS(&(trp)->token_rcf) & 0x1f00) >> 8)
82 #define DIRECTION(trp)		((EXTRACT_16BITS(&(trp)->token_rcf) & 0x0080) >> 7)
83 #define LARGEST_FRAME(trp)	((EXTRACT_16BITS(&(trp)->token_rcf) & 0x0070) >> 4)
84 #define RING_NUMBER(trp, x)	((EXTRACT_16BITS(&(trp)->token_rseg[x]) & 0xfff0) >> 4)
85 #define BRIDGE_NUMBER(trp, x)	((EXTRACT_16BITS(&(trp)->token_rseg[x]) & 0x000f))
86 #define SEGMENT_COUNT(trp)	((int)((RIF_LENGTH(trp) - 2) / 2))
87 
88 struct token_header {
89 	uint8_t  token_ac;
90 	uint8_t  token_fc;
91 	uint8_t  token_dhost[TOKEN_RING_MAC_LEN];
92 	uint8_t  token_shost[TOKEN_RING_MAC_LEN];
93 	uint16_t token_rcf;
94 	uint16_t token_rseg[ROUTING_SEGMENT_MAX];
95 };
96 
97 static const char tstr[] = "[|token-ring]";
98 
99 /* Extract src, dst addresses */
100 static inline void
101 extract_token_addrs(const struct token_header *trp, char *fsrc, char *fdst)
102 {
103 	memcpy(fdst, (const char *)trp->token_dhost, 6);
104 	memcpy(fsrc, (const char *)trp->token_shost, 6);
105 }
106 
107 /*
108  * Print the TR MAC header
109  */
110 static inline void
111 token_hdr_print(netdissect_options *ndo,
112                 register const struct token_header *trp, register u_int length,
113                 register const u_char *fsrc, register const u_char *fdst)
114 {
115 	const char *srcname, *dstname;
116 
117 	srcname = etheraddr_string(ndo, fsrc);
118 	dstname = etheraddr_string(ndo, fdst);
119 
120 	if (!ndo->ndo_qflag)
121 		ND_PRINT((ndo, "%02x %02x ",
122 		       trp->token_ac,
123 		       trp->token_fc));
124 	ND_PRINT((ndo, "%s > %s, length %u: ",
125 	       srcname, dstname,
126 	       length));
127 }
128 
129 static const char *broadcast_indicator[] = {
130 	"Non-Broadcast", "Non-Broadcast",
131 	"Non-Broadcast", "Non-Broadcast",
132 	"All-routes",    "All-routes",
133 	"Single-route",  "Single-route"
134 };
135 
136 static const char *direction[] = {
137 	"Forward", "Backward"
138 };
139 
140 static const char *largest_frame[] = {
141 	"516",
142 	"1500",
143 	"2052",
144 	"4472",
145 	"8144",
146 	"11407",
147 	"17800",
148 	"??"
149 };
150 
151 u_int
152 token_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
153 {
154 	const struct token_header *trp;
155 	int llc_hdrlen;
156 	struct ether_header ehdr;
157 	u_int route_len = 0, hdr_len = TOKEN_HDRLEN;
158 	int seg;
159 
160 	trp = (const struct token_header *)p;
161 
162 	if (caplen < TOKEN_HDRLEN) {
163 		ND_PRINT((ndo, "%s", tstr));
164 		return hdr_len;
165 	}
166 
167 	/*
168 	 * Get the TR addresses into a canonical form
169 	 */
170 	extract_token_addrs(trp, (char*)ESRC(&ehdr), (char*)EDST(&ehdr));
171 
172 	/* Adjust for source routing information in the MAC header */
173 	if (IS_SOURCE_ROUTED(trp)) {
174 		/* Clear source-routed bit */
175 		*ESRC(&ehdr) &= 0x7f;
176 
177 		if (ndo->ndo_eflag)
178 			token_hdr_print(ndo, trp, length, ESRC(&ehdr), EDST(&ehdr));
179 
180 		if (caplen < TOKEN_HDRLEN + 2) {
181 			ND_PRINT((ndo, "%s", tstr));
182 			return hdr_len;
183 		}
184 		route_len = RIF_LENGTH(trp);
185 		hdr_len += route_len;
186 		if (caplen < hdr_len) {
187 			ND_PRINT((ndo, "%s", tstr));
188 			return hdr_len;
189 		}
190 		if (ndo->ndo_vflag) {
191 			ND_PRINT((ndo, "%s ", broadcast_indicator[BROADCAST(trp)]));
192 			ND_PRINT((ndo, "%s", direction[DIRECTION(trp)]));
193 
194 			for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
195 				ND_PRINT((ndo, " [%d:%d]", RING_NUMBER(trp, seg),
196 				    BRIDGE_NUMBER(trp, seg)));
197 		} else {
198 			ND_PRINT((ndo, "rt = %x", EXTRACT_16BITS(&trp->token_rcf)));
199 
200 			for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
201 				ND_PRINT((ndo, ":%x", EXTRACT_16BITS(&trp->token_rseg[seg])));
202 		}
203 		ND_PRINT((ndo, " (%s) ", largest_frame[LARGEST_FRAME(trp)]));
204 	} else {
205 		if (ndo->ndo_eflag)
206 			token_hdr_print(ndo, trp, length, ESRC(&ehdr), EDST(&ehdr));
207 	}
208 
209 	/* Skip over token ring MAC header and routing information */
210 	length -= hdr_len;
211 	p += hdr_len;
212 	caplen -= hdr_len;
213 
214 	/* Frame Control field determines interpretation of packet */
215 	if (FRAME_TYPE(trp) == TOKEN_FC_LLC) {
216 		/* Try to print the LLC-layer header & higher layers */
217 		llc_hdrlen = llc_print(ndo, p, length, caplen, ESRC(&ehdr),
218 		    EDST(&ehdr));
219 		if (llc_hdrlen < 0) {
220 			/* packet type not known, print raw packet */
221 			if (!ndo->ndo_suppress_default_print)
222 				ND_DEFAULTPRINT(p, caplen);
223 			llc_hdrlen = -llc_hdrlen;
224 		}
225 		hdr_len += llc_hdrlen;
226 	} else {
227 		/* Some kinds of TR packet we cannot handle intelligently */
228 		/* XXX - dissect MAC packets if frame type is 0 */
229 		if (!ndo->ndo_eflag)
230 			token_hdr_print(ndo, trp, length + TOKEN_HDRLEN + route_len,
231 			    ESRC(&ehdr), EDST(&ehdr));
232 		if (!ndo->ndo_suppress_default_print)
233 			ND_DEFAULTPRINT(p, caplen);
234 	}
235 	return (hdr_len);
236 }
237 
238 /*
239  * This is the top level routine of the printer.  'p' points
240  * to the TR header of the packet, 'h->ts' is the timestamp,
241  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
242  * is the number of bytes actually captured.
243  */
244 u_int
245 token_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
246 {
247 	return (token_print(ndo, p, h->len, h->caplen));
248 }
249