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