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