xref: /freebsd/contrib/tcpdump/print-token.c (revision 9207b4cff7b8d483f4dd3c62266c2b58819eb7f9)
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  * $FreeBSD$
24  */
25 #ifndef lint
26 static const char rcsid[] =
27     "@(#) $Header: /home/ncvs/src/contrib/tcpdump/print-token.c,v 1.1 1999/02/20 11:17:55 julian Exp $";
28 #endif
29 
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/socket.h>
33 
34 #if __STDC__
35 struct mbuf;
36 struct rtentry;
37 #endif
38 #include <net/if.h>
39 
40 #include "token.h"
41 
42 #include <netinet/in.h>
43 #include <net/ethernet.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip_var.h>
47 #include <netinet/udp.h>
48 #include <netinet/udp_var.h>
49 #include <netinet/tcp.h>
50 #include <netinet/tcpip.h>
51 
52 #include <stdio.h>
53 #include <pcap.h>
54 
55 #include "interface.h"
56 #include "addrtoname.h"
57 #include "ethertype.h"
58 #include "llc.h"
59 
60 const u_char *packetp;
61 const u_char *snapend;
62 
63 static inline void
64 token_print(register const u_char *bp, u_int length)
65 {
66 	register const struct token_header *tp;
67         register const struct llc *lp;
68         u_short ether_type;
69 
70         tp = (const struct token_header *)bp;
71         lp = (struct llc *)(bp + TOKEN_HDR_LEN);
72 
73         if (IS_SOURCE_ROUTED) {
74             tp->ether_shost[0] = tp->ether_shost[0] & 0x7f;
75                 lp = (struct llc *)(bp + TOKEN_HDR_LEN + RIF_LENGTH);
76         }
77 
78         /*
79          * Ethertype on ethernet is a short, but ethertype in an llc-snap has
80          * been defined as 2 u_chars.  This is a stupid little hack to fix
81          * this for now but something better should be done using ntohs()
82          * XXX
83          */
84          ether_type = ((u_short)lp->ethertype[1] << 16) | lp->ethertype[0];
85 
86 	if (qflag)
87 		(void)printf("%s %s %d: ",
88 			     etheraddr_string(ESRC(tp)),
89 			     etheraddr_string(EDST(tp)),
90 			     length);
91 	else
92 		(void)printf("%s %s %s %d: ",
93 			     etheraddr_string(ESRC(tp)),
94 			     etheraddr_string(EDST(tp)),
95 			     etherproto_string(ether_type),
96 			     length);
97 }
98 
99 /*
100  * This is the top level routine of the printer.  'p' is the points
101  * to the ether header of the packet, 'tvp' is the timestamp,
102  * 'length' is the length of the packet off the wire, and 'caplen'
103  * is the number of bytes actually captured.
104  */
105 void
106 token_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
107 {
108 	u_int caplen = h->caplen;
109 	u_int length = h->len;
110 	struct token_header *tp;
111 	u_short ether_type;
112 	u_short extracted_ethertype;
113         u_int route_len = 0, seg;
114         struct llc *lp;
115 
116         tp = (struct token_header *)p;
117 
118 	ts_print(&h->ts);
119 
120 	if (caplen < TOKEN_HDR_LEN) {
121 		printf("[|token-ring]");
122 		goto out;
123 	}
124 
125 	/*
126 	 * Some printers want to get back at the ethernet addresses,
127 	 * and/or check that they're not walking off the end of the packet.
128 	 * Rather than pass them all the way down, we set these globals.
129 	 */
130 	tp = (struct token_header *)p;
131 
132         /* Adjust for source routing information in the MAC header */
133         if (IS_SOURCE_ROUTED) {
134 
135             if (eflag)
136                 token_print(p, length);
137 
138                 route_len =  RIF_LENGTH;
139             if (vflag) {
140                 if (vflag > 1)
141                     printf("ac %x fc %x ", tp->ac, tp->fc);
142                 ether_type = ntohs((int)lp->ethertype);
143 
144                 printf("%s ", broadcast_indicator[BROADCAST]);
145                 printf("%s", direction[DIRECTION]);
146 
147                 for (seg = 0; seg < SEGMENT_COUNT; seg++)
148                     printf(" [%d:%d]", RING_NUMBER(seg), BRIDGE_NUMBER(seg));
149             } else {
150                 printf("rt = %x", ntohs(tp->rcf));
151 
152                 for (seg = 0; seg < SEGMENT_COUNT; seg++)
153                     printf(":%x", ntohs(tp->rseg[seg]));
154             }
155             printf(" (%s) ", largest_frame[LARGEST_FRAME]);
156         } else {
157             if (eflag)
158                 token_print(p, length);
159         }
160 
161         /* Set pointer to llc header, adjusted for routing information */
162         lp = (struct llc *)(p + TOKEN_HDR_LEN + route_len);
163 
164         packetp = p;
165 	snapend = p + caplen;
166 
167         /* Skip over token ring MAC header */
168 	length -= TOKEN_HDR_LEN + route_len;
169 	caplen -= TOKEN_HDR_LEN + route_len;
170 	p += TOKEN_HDR_LEN + route_len;
171 
172 	extracted_ethertype = 0;
173 	/* Try to print the LLC-layer header & higher layers */
174 	if (llc_print(p, length, caplen, ESRC(tp), EDST(tp), &extracted_ethertype) == 0) {
175 		/* ether_type not known, print raw packet */
176 		if (!eflag)
177 			token_print((u_char *)tp, length);
178 		if (extracted_ethertype) {
179 			printf("(LLC %s) ",
180 		       etherproto_string(htons(extracted_ethertype)));
181 		}
182 		if (!xflag && !qflag)
183 			default_print(p, caplen);
184 	}
185 	if (xflag)
186 		default_print(p, caplen);
187  out:
188 	putchar('\n');
189 }
190