blob: faffd4b13ce472194f17744fa02ff26c4fd9772a [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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 */
The Android Open Source Project2949f582009-03-03 19:30:46 -080026
Elliott Hughese2e3bd12017-05-15 10:59:29 -070027/* \summary: Token Ring printer */
28
The Android Open Source Project2949f582009-03-03 19:30:46 -080029#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
Elliott Hughese2e3bd12017-05-15 10:59:29 -070033#include <netdissect-stdinc.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080034
The Android Open Source Project2949f582009-03-03 19:30:46 -080035#include <string.h>
36
Elliott Hughese2e3bd12017-05-15 10:59:29 -070037#include "netdissect.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080038#include "extract.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080039#include "addrtoname.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080040#include "ether.h"
Elliott Hughes892a68b2015-10-19 14:43:53 -070041
42/*
43 * Copyright (c) 1998, Larry Lile
44 * All rights reserved.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice unmodified, this list of conditions, and the following
51 * disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 */
69
70#define TOKEN_HDRLEN 14
71#define TOKEN_RING_MAC_LEN 6
72#define ROUTING_SEGMENT_MAX 16
73#define IS_SOURCE_ROUTED(trp) ((trp)->token_shost[0] & 0x80)
74#define FRAME_TYPE(trp) (((trp)->token_fc & 0xC0) >> 6)
75#define TOKEN_FC_LLC 1
76
77#define BROADCAST(trp) ((EXTRACT_16BITS(&(trp)->token_rcf) & 0xE000) >> 13)
78#define RIF_LENGTH(trp) ((EXTRACT_16BITS(&(trp)->token_rcf) & 0x1f00) >> 8)
79#define DIRECTION(trp) ((EXTRACT_16BITS(&(trp)->token_rcf) & 0x0080) >> 7)
80#define LARGEST_FRAME(trp) ((EXTRACT_16BITS(&(trp)->token_rcf) & 0x0070) >> 4)
81#define RING_NUMBER(trp, x) ((EXTRACT_16BITS(&(trp)->token_rseg[x]) & 0xfff0) >> 4)
82#define BRIDGE_NUMBER(trp, x) ((EXTRACT_16BITS(&(trp)->token_rseg[x]) & 0x000f))
83#define SEGMENT_COUNT(trp) ((int)((RIF_LENGTH(trp) - 2) / 2))
84
85struct token_header {
86 uint8_t token_ac;
87 uint8_t token_fc;
88 uint8_t token_dhost[TOKEN_RING_MAC_LEN];
89 uint8_t token_shost[TOKEN_RING_MAC_LEN];
90 uint16_t token_rcf;
91 uint16_t token_rseg[ROUTING_SEGMENT_MAX];
92};
93
94static const char tstr[] = "[|token-ring]";
The Android Open Source Project2949f582009-03-03 19:30:46 -080095
96/* Extract src, dst addresses */
97static inline void
98extract_token_addrs(const struct token_header *trp, char *fsrc, char *fdst)
99{
100 memcpy(fdst, (const char *)trp->token_dhost, 6);
101 memcpy(fsrc, (const char *)trp->token_shost, 6);
102}
103
104/*
105 * Print the TR MAC header
106 */
107static inline void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700108token_hdr_print(netdissect_options *ndo,
109 register const struct token_header *trp, register u_int length,
110 register const u_char *fsrc, register const u_char *fdst)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800111{
112 const char *srcname, *dstname;
113
Elliott Hughes892a68b2015-10-19 14:43:53 -0700114 srcname = etheraddr_string(ndo, fsrc);
115 dstname = etheraddr_string(ndo, fdst);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800116
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700117 if (!ndo->ndo_qflag)
118 ND_PRINT((ndo, "%02x %02x ",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800119 trp->token_ac,
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700120 trp->token_fc));
121 ND_PRINT((ndo, "%s > %s, length %u: ",
122 srcname, dstname,
123 length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800124}
125
126static const char *broadcast_indicator[] = {
127 "Non-Broadcast", "Non-Broadcast",
128 "Non-Broadcast", "Non-Broadcast",
129 "All-routes", "All-routes",
130 "Single-route", "Single-route"
131};
132
133static const char *direction[] = {
134 "Forward", "Backward"
135};
136
137static const char *largest_frame[] = {
138 "516",
139 "1500",
140 "2052",
141 "4472",
142 "8144",
143 "11407",
144 "17800",
145 "??"
146};
147
148u_int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700149token_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800150{
151 const struct token_header *trp;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700152 int llc_hdrlen;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800153 struct ether_header ehdr;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700154 struct lladdr_info src, dst;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800155 u_int route_len = 0, hdr_len = TOKEN_HDRLEN;
156 int seg;
157
158 trp = (const struct token_header *)p;
159
160 if (caplen < TOKEN_HDRLEN) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700161 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800162 return hdr_len;
163 }
164
165 /*
166 * Get the TR addresses into a canonical form
167 */
168 extract_token_addrs(trp, (char*)ESRC(&ehdr), (char*)EDST(&ehdr));
169
170 /* Adjust for source routing information in the MAC header */
171 if (IS_SOURCE_ROUTED(trp)) {
172 /* Clear source-routed bit */
173 *ESRC(&ehdr) &= 0x7f;
174
Elliott Hughes892a68b2015-10-19 14:43:53 -0700175 if (ndo->ndo_eflag)
176 token_hdr_print(ndo, trp, length, ESRC(&ehdr), EDST(&ehdr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800177
JP Abgrall53f17a92014-02-12 14:02:41 -0800178 if (caplen < TOKEN_HDRLEN + 2) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700179 ND_PRINT((ndo, "%s", tstr));
JP Abgrall53f17a92014-02-12 14:02:41 -0800180 return hdr_len;
181 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800182 route_len = RIF_LENGTH(trp);
JP Abgrall53f17a92014-02-12 14:02:41 -0800183 hdr_len += route_len;
184 if (caplen < hdr_len) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700185 ND_PRINT((ndo, "%s", tstr));
JP Abgrall53f17a92014-02-12 14:02:41 -0800186 return hdr_len;
187 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700188 if (ndo->ndo_vflag) {
189 ND_PRINT((ndo, "%s ", broadcast_indicator[BROADCAST(trp)]));
190 ND_PRINT((ndo, "%s", direction[DIRECTION(trp)]));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800191
192 for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700193 ND_PRINT((ndo, " [%d:%d]", RING_NUMBER(trp, seg),
194 BRIDGE_NUMBER(trp, seg)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800195 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700196 ND_PRINT((ndo, "rt = %x", EXTRACT_16BITS(&trp->token_rcf)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800197
198 for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700199 ND_PRINT((ndo, ":%x", EXTRACT_16BITS(&trp->token_rseg[seg])));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800200 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700201 ND_PRINT((ndo, " (%s) ", largest_frame[LARGEST_FRAME(trp)]));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800202 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700203 if (ndo->ndo_eflag)
204 token_hdr_print(ndo, trp, length, ESRC(&ehdr), EDST(&ehdr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800205 }
206
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700207 src.addr = ESRC(&ehdr);
208 src.addr_string = etheraddr_string;
209 dst.addr = EDST(&ehdr);
210 dst.addr_string = etheraddr_string;
211
The Android Open Source Project2949f582009-03-03 19:30:46 -0800212 /* Skip over token ring MAC header and routing information */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800213 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 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700220 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
221 if (llc_hdrlen < 0) {
222 /* packet type not known, print raw packet */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700223 if (!ndo->ndo_suppress_default_print)
224 ND_DEFAULTPRINT(p, caplen);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700225 llc_hdrlen = -llc_hdrlen;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800226 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700227 hdr_len += llc_hdrlen;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800228 } else {
229 /* Some kinds of TR packet we cannot handle intelligently */
230 /* XXX - dissect MAC packets if frame type is 0 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700231 if (!ndo->ndo_eflag)
232 token_hdr_print(ndo, trp, length + TOKEN_HDRLEN + route_len,
The Android Open Source Project2949f582009-03-03 19:30:46 -0800233 ESRC(&ehdr), EDST(&ehdr));
Elliott Hughes892a68b2015-10-19 14:43:53 -0700234 if (!ndo->ndo_suppress_default_print)
235 ND_DEFAULTPRINT(p, caplen);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800236 }
237 return (hdr_len);
238}
239
240/*
241 * This is the top level routine of the printer. 'p' points
242 * to the TR header of the packet, 'h->ts' is the timestamp,
243 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
244 * is the number of bytes actually captured.
245 */
246u_int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700247token_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800248{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700249 return (token_print(ndo, p, h->len, h->caplen));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800250}