2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * Hacked version of print-ether.c Larry Lile <lile@stdio.com>
23 * Further tweaked to more closely resemble print-fddi.c
24 * Guy Harris <guy@alum.mit.edu>
27 /* \summary: Token Ring printer */
31 #include "netdissect-stdinc.h"
35 #include "netdissect.h"
37 #include "addrtoname.h"
40 * Copyright (c) 1998, Larry Lile
41 * All rights reserved.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice unmodified, this list of conditions, and the following
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 #define TOKEN_HDRLEN 14
68 #define ROUTING_SEGMENT_MAX 16
69 #define IS_SOURCE_ROUTED(trp) ((trp)->token_shost[0] & 0x80)
70 #define FRAME_TYPE(trp) ((GET_U_1((trp)->token_fc) & 0xC0) >> 6)
71 #define TOKEN_FC_LLC 1
73 #define BROADCAST(trp) ((GET_BE_U_2((trp)->token_rcf) & 0xE000) >> 13)
74 #define RIF_LENGTH(trp) ((GET_BE_U_2((trp)->token_rcf) & 0x1f00) >> 8)
75 #define DIRECTION(trp) ((GET_BE_U_2((trp)->token_rcf) & 0x0080) >> 7)
76 #define LARGEST_FRAME(trp) ((GET_BE_U_2((trp)->token_rcf) & 0x0070) >> 4)
77 #define RING_NUMBER(trp, x) ((GET_BE_U_2((trp)->token_rseg[x]) & 0xfff0) >> 4)
78 #define BRIDGE_NUMBER(trp, x) (GET_BE_U_2((trp)->token_rseg[x]) & 0x000f)
79 #define SEGMENT_COUNT(trp) ((int)((RIF_LENGTH(trp) - 2) / 2))
86 nd_uint16_t token_rcf
;
87 nd_uint16_t token_rseg
[ROUTING_SEGMENT_MAX
];
91 /* Extract src, dst addresses */
93 extract_token_addrs(const struct token_header
*trp
, char *fsrc
, char *fdst
)
95 memcpy(fdst
, (const char *)trp
->token_dhost
, 6);
96 memcpy(fsrc
, (const char *)trp
->token_shost
, 6);
100 * Print the TR MAC header
103 token_hdr_print(netdissect_options
*ndo
,
104 const struct token_header
*trp
, u_int length
,
105 const u_char
*fsrc
, const u_char
*fdst
)
107 const char *srcname
, *dstname
;
109 srcname
= mac48_string(ndo
, fsrc
);
110 dstname
= mac48_string(ndo
, fdst
);
113 ND_PRINT("%02x %02x ",
114 GET_U_1(trp
->token_ac
),
115 GET_U_1(trp
->token_fc
));
116 ND_PRINT("%s > %s, length %u: ",
121 static const char *broadcast_indicator
[] = {
122 "Non-Broadcast", "Non-Broadcast",
123 "Non-Broadcast", "Non-Broadcast",
124 "All-routes", "All-routes",
125 "Single-route", "Single-route"
128 static const char *direction
[] = {
129 "Forward", "Backward"
132 static const char *largest_frame
[] = {
144 token_print(netdissect_options
*ndo
, const u_char
*p
, u_int length
, u_int caplen
)
146 const struct token_header
*trp
;
148 nd_mac48 srcmac
, dstmac
;
149 struct lladdr_info src
, dst
;
150 u_int route_len
= 0, hdr_len
= TOKEN_HDRLEN
;
153 ndo
->ndo_protocol
= "token-ring";
154 trp
= (const struct token_header
*)p
;
156 if (caplen
< TOKEN_HDRLEN
) {
162 * Get the TR addresses into a canonical form
164 extract_token_addrs(trp
, (char*)srcmac
, (char*)dstmac
);
166 /* Adjust for source routing information in the MAC header */
167 if (IS_SOURCE_ROUTED(trp
)) {
168 /* Clear source-routed bit */
172 token_hdr_print(ndo
, trp
, length
, srcmac
, dstmac
);
174 if (caplen
< TOKEN_HDRLEN
+ 2) {
178 route_len
= RIF_LENGTH(trp
);
179 hdr_len
+= route_len
;
180 if (caplen
< hdr_len
) {
184 if (ndo
->ndo_vflag
) {
185 ND_PRINT("%s ", broadcast_indicator
[BROADCAST(trp
)]);
186 ND_PRINT("%s", direction
[DIRECTION(trp
)]);
188 for (seg
= 0; seg
< SEGMENT_COUNT(trp
); seg
++)
189 ND_PRINT(" [%u:%u]", RING_NUMBER(trp
, seg
),
190 BRIDGE_NUMBER(trp
, seg
));
192 ND_PRINT("rt = %x", GET_BE_U_2(trp
->token_rcf
));
194 for (seg
= 0; seg
< SEGMENT_COUNT(trp
); seg
++)
196 GET_BE_U_2(trp
->token_rseg
[seg
]));
198 ND_PRINT(" (%s) ", largest_frame
[LARGEST_FRAME(trp
)]);
201 token_hdr_print(ndo
, trp
, length
, srcmac
, dstmac
);
205 src
.addr_string
= mac48_string
;
207 dst
.addr_string
= mac48_string
;
209 /* Skip over token ring MAC header and routing information */
214 /* Frame Control field determines interpretation of packet */
215 if (FRAME_TYPE(trp
) == TOKEN_FC_LLC
) {
216 /* Try to print the LLC-layer header & higher layers */
217 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
218 if (llc_hdrlen
< 0) {
219 /* packet type not known, print raw packet */
220 if (!ndo
->ndo_suppress_default_print
)
221 ND_DEFAULTPRINT(p
, caplen
);
222 llc_hdrlen
= -llc_hdrlen
;
224 hdr_len
+= llc_hdrlen
;
226 /* Some kinds of TR packet we cannot handle intelligently */
227 /* XXX - dissect MAC packets if frame type is 0 */
229 token_hdr_print(ndo
, trp
, length
+ TOKEN_HDRLEN
+ route_len
,
231 if (!ndo
->ndo_suppress_default_print
)
232 ND_DEFAULTPRINT(p
, caplen
);
238 * This is the top level routine of the printer. 'p' points
239 * to the TR header of the packet, 'h->ts' is the timestamp,
240 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
241 * is the number of bytes actually captured.
244 token_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
, const u_char
*p
)
246 ndo
->ndo_protocol
= "token-ring";
247 ndo
->ndo_ll_hdr_len
+= token_print(ndo
, p
, h
->len
, h
->caplen
);