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