]> The Tcpdump Group git mirrors - tcpdump/blob - print-token.c
917c225b914fc394694472030d7809dd11ace68e
[tcpdump] / print-token.c
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[] =
28 "@(#) $Header: /tcpdump/master/tcpdump/print-token.c,v 1.3 2000-09-23 08:03:41 guy Exp $";
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/socket.h>
38
39 struct mbuf;
40 struct rtentry;
41 #include <net/if.h>
42
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46
47 #include <pcap.h>
48 #include <stdio.h>
49
50 #include "interface.h"
51 #include "addrtoname.h"
52 #include "ethertype.h"
53
54 #include "ether.h"
55 #include "token.h"
56
57 /* Extract src, dst addresses */
58 static inline void
59 extract_token_addrs(const struct token_header *trp, char *fsrc, char *fdst)
60 {
61 memcpy(fdst, (char *)trp->token_dhost, 6);
62 memcpy(fsrc, (char *)trp->token_shost, 6);
63 }
64
65 /*
66 * Print the TR MAC header
67 */
68 static inline void
69 token_print(register const struct token_header *trp, register u_int length,
70 register const u_char *fsrc, register const u_char *fdst)
71 {
72 char *srcname, *dstname;
73
74 srcname = etheraddr_string(fsrc);
75 dstname = etheraddr_string(fdst);
76
77 if (vflag)
78 (void) printf("%02x %02x %s %s %d: ",
79 trp->token_ac,
80 trp->token_fc,
81 srcname, dstname,
82 length);
83 else
84 printf("%s %s %d: ", srcname, dstname, length);
85 }
86
87 static char *broadcast_indicator[] = {
88 "Non-Broadcast", "Non-Broadcast",
89 "Non-Broadcast", "Non-Broadcast",
90 "All-routes", "All-routes",
91 "Single-route", "Single-route"
92 };
93
94 static char *direction[] = {
95 "Forward", "Backward"
96 };
97
98 static char *largest_frame[] = {
99 "516",
100 "1500",
101 "2052",
102 "4472",
103 "8144",
104 "11407",
105 "17800",
106 "??"
107 };
108
109 /*
110 * This is the top level routine of the printer. 'p' is the points
111 * to the TR header of the packet, 'tvp' is the timestamp,
112 * 'length' is the length of the packet off the wire, and 'caplen'
113 * is the number of bytes actually captured.
114 */
115 void
116 token_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
117 {
118 u_int caplen = h->caplen;
119 u_int length = h->len;
120 struct token_header *trp;
121 u_short extracted_ethertype;
122 struct ether_header ehdr;
123 u_int route_len = 0, seg;
124
125 trp = (struct token_header *)p;
126
127 ts_print(&h->ts);
128
129 if (caplen < TOKEN_HDRLEN) {
130 printf("[|token-ring]");
131 goto out;
132 }
133 /*
134 * Get the TR addresses into a canonical form
135 */
136 extract_token_addrs(trp, (char*)ESRC(&ehdr), (char*)EDST(&ehdr));
137 /*
138 * Some printers want to get back at the ethernet addresses,
139 * and/or check that they're not walking off the end of the packet.
140 * Rather than pass them all the way down, we set these globals.
141 */
142 snapend = p + caplen;
143 /*
144 * Actually, the only printer that uses packetp is print-bootp.c,
145 * and it assumes that packetp points to an Ethernet header. The
146 * right thing to do is to fix print-bootp.c to know which link
147 * type is in use when it excavates. XXX
148 */
149 packetp = (u_char *)&ehdr;
150
151 /* Adjust for source routing information in the MAC header */
152 if (IS_SOURCE_ROUTED(trp)) {
153 /* Clear source-routed bit */
154 *ESRC(&ehdr) &= 0x7f;
155
156 if (eflag)
157 token_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
158
159 route_len = RIF_LENGTH(trp);
160 if (vflag) {
161 printf("%s ", broadcast_indicator[BROADCAST(trp)]);
162 printf("%s", direction[DIRECTION(trp)]);
163
164 for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
165 printf(" [%d:%d]", RING_NUMBER(trp, seg),
166 BRIDGE_NUMBER(trp, seg));
167 } else {
168 printf("rt = %x", ntohs(trp->token_rcf));
169
170 for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
171 printf(":%x", ntohs(trp->token_rseg[seg]));
172 }
173 printf(" (%s) ", largest_frame[LARGEST_FRAME(trp)]);
174 } else {
175 if (eflag)
176 token_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
177 }
178
179 /* Skip over token ring MAC header and routing information */
180 length -= TOKEN_HDRLEN + route_len;
181 p += TOKEN_HDRLEN + route_len;
182 caplen -= TOKEN_HDRLEN + route_len;
183
184 /* Frame Control field determines interpretation of packet */
185 extracted_ethertype = 0;
186 if (FRAME_TYPE(trp) == TOKEN_FC_LLC) {
187 /* Try to print the LLC-layer header & higher layers */
188 if (llc_print(p, length, caplen, ESRC(&ehdr), EDST(&ehdr))
189 == 0) {
190 /* ether_type not known, print raw packet */
191 if (!eflag)
192 token_print(trp, length,
193 ESRC(&ehdr), EDST(&ehdr));
194 if (extracted_ethertype) {
195 printf("(LLC %s) ",
196 etherproto_string(htons(extracted_ethertype)));
197 }
198 if (!xflag && !qflag)
199 default_print(p, caplen);
200 }
201 } else {
202 /* Some kinds of TR packet we cannot handle intelligently */
203 /* XXX - dissect MAC packets if frame type is 0 */
204 if (!eflag)
205 token_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
206 if (!xflag && !qflag)
207 default_print(p, caplen);
208 }
209 if (xflag)
210 default_print(p, caplen);
211 out:
212 putchar('\n');
213 }