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