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