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