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