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, 1997 |
| 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 | */ |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 21 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 22 | /* \summary: Linux cooked sockets capture printer */ |
| 23 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include "config.h" |
| 26 | #endif |
| 27 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 28 | #include <netdissect-stdinc.h> |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 29 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 30 | #include "netdissect.h" |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 31 | #include "addrtoname.h" |
| 32 | #include "ethertype.h" |
| 33 | #include "extract.h" |
| 34 | |
| 35 | #include "ether.h" |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * For captures on Linux cooked sockets, we construct a fake header |
| 39 | * that includes: |
| 40 | * |
| 41 | * a 2-byte "packet type" which is one of: |
| 42 | * |
| 43 | * LINUX_SLL_HOST packet was sent to us |
| 44 | * LINUX_SLL_BROADCAST packet was broadcast |
| 45 | * LINUX_SLL_MULTICAST packet was multicast |
| 46 | * LINUX_SLL_OTHERHOST packet was sent to somebody else |
| 47 | * LINUX_SLL_OUTGOING packet was sent *by* us; |
| 48 | * |
| 49 | * a 2-byte Ethernet protocol field; |
| 50 | * |
| 51 | * a 2-byte link-layer type; |
| 52 | * |
| 53 | * a 2-byte link-layer address length; |
| 54 | * |
| 55 | * an 8-byte source link-layer address, whose actual length is |
| 56 | * specified by the previous value. |
| 57 | * |
| 58 | * All fields except for the link-layer address are in network byte order. |
| 59 | * |
| 60 | * DO NOT change the layout of this structure, or change any of the |
| 61 | * LINUX_SLL_ values below. If you must change the link-layer header |
| 62 | * for a "cooked" Linux capture, introduce a new DLT_ type (ask |
| 63 | * "tcpdump-workers@lists.tcpdump.org" for one, so that you don't give it |
| 64 | * a value that collides with a value already being used), and use the |
| 65 | * new header in captures of that type, so that programs that can |
| 66 | * handle DLT_LINUX_SLL captures will continue to handle them correctly |
| 67 | * without any change, and so that capture files with different headers |
| 68 | * can be told apart and programs that read them can dissect the |
| 69 | * packets in them. |
| 70 | * |
| 71 | * This structure, and the #defines below, must be the same in the |
| 72 | * libpcap and tcpdump versions of "sll.h". |
| 73 | */ |
| 74 | |
| 75 | /* |
| 76 | * A DLT_LINUX_SLL fake link-layer header. |
| 77 | */ |
| 78 | #define SLL_HDR_LEN 16 /* total header length */ |
| 79 | #define SLL_ADDRLEN 8 /* length of address field */ |
| 80 | |
| 81 | struct sll_header { |
| 82 | uint16_t sll_pkttype; /* packet type */ |
| 83 | uint16_t sll_hatype; /* link-layer address type */ |
| 84 | uint16_t sll_halen; /* link-layer address length */ |
| 85 | uint8_t sll_addr[SLL_ADDRLEN]; /* link-layer address */ |
| 86 | uint16_t sll_protocol; /* protocol */ |
| 87 | }; |
| 88 | |
| 89 | /* |
| 90 | * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the |
| 91 | * PACKET_ values on Linux, but are defined here so that they're |
| 92 | * available even on systems other than Linux, and so that they |
| 93 | * don't change even if the PACKET_ values change. |
| 94 | */ |
| 95 | #define LINUX_SLL_HOST 0 |
| 96 | #define LINUX_SLL_BROADCAST 1 |
| 97 | #define LINUX_SLL_MULTICAST 2 |
| 98 | #define LINUX_SLL_OTHERHOST 3 |
| 99 | #define LINUX_SLL_OUTGOING 4 |
| 100 | |
| 101 | /* |
| 102 | * The LINUX_SLL_ values for "sll_protocol"; these correspond to the |
| 103 | * ETH_P_ values on Linux, but are defined here so that they're |
| 104 | * available even on systems other than Linux. We assume, for now, |
| 105 | * that the ETH_P_ values won't change in Linux; if they do, then: |
| 106 | * |
| 107 | * if we don't translate them in "pcap-linux.c", capture files |
| 108 | * won't necessarily be readable if captured on a system that |
| 109 | * defines ETH_P_ values that don't match these values; |
| 110 | * |
| 111 | * if we do translate them in "pcap-linux.c", that makes life |
| 112 | * unpleasant for the BPF code generator, as the values you test |
| 113 | * for in the kernel aren't the values that you test for when |
| 114 | * reading a capture file, so the fixup code run on BPF programs |
| 115 | * handed to the kernel ends up having to do more work. |
| 116 | * |
| 117 | * Add other values here as necessary, for handling packet types that |
| 118 | * might show up on non-Ethernet, non-802.x networks. (Not all the ones |
| 119 | * in the Linux "if_ether.h" will, I suspect, actually show up in |
| 120 | * captures.) |
| 121 | */ |
| 122 | #define LINUX_SLL_P_802_3 0x0001 /* Novell 802.3 frames without 802.2 LLC header */ |
| 123 | #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */ |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 124 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 125 | static const struct tok sll_pkttype_values[] = { |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 126 | { LINUX_SLL_HOST, "In" }, |
| 127 | { LINUX_SLL_BROADCAST, "B" }, |
| 128 | { LINUX_SLL_MULTICAST, "M" }, |
| 129 | { LINUX_SLL_OTHERHOST, "P" }, |
| 130 | { LINUX_SLL_OUTGOING, "Out" }, |
| 131 | { 0, NULL} |
| 132 | }; |
| 133 | |
| 134 | static inline void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 135 | sll_print(netdissect_options *ndo, register const struct sll_header *sllp, u_int length) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 136 | { |
| 137 | u_short ether_type; |
| 138 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 139 | ND_PRINT((ndo, "%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_16BITS(&sllp->sll_pkttype)))); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * XXX - check the link-layer address type value? |
| 143 | * For now, we just assume 6 means Ethernet. |
| 144 | * XXX - print others as strings of hex? |
| 145 | */ |
| 146 | if (EXTRACT_16BITS(&sllp->sll_halen) == 6) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 147 | ND_PRINT((ndo, "%s ", etheraddr_string(ndo, sllp->sll_addr))); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 148 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 149 | if (!ndo->ndo_qflag) { |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 150 | ether_type = EXTRACT_16BITS(&sllp->sll_protocol); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 151 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 152 | if (ether_type <= ETHERMTU) { |
| 153 | /* |
| 154 | * Not an Ethernet type; what type is it? |
| 155 | */ |
| 156 | switch (ether_type) { |
| 157 | |
| 158 | case LINUX_SLL_P_802_3: |
| 159 | /* |
| 160 | * Ethernet_802.3 IPX frame. |
| 161 | */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 162 | ND_PRINT((ndo, "802.3")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 163 | break; |
| 164 | |
| 165 | case LINUX_SLL_P_802_2: |
| 166 | /* |
| 167 | * 802.2. |
| 168 | */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 169 | ND_PRINT((ndo, "802.2")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 170 | break; |
| 171 | |
| 172 | default: |
| 173 | /* |
| 174 | * What is it? |
| 175 | */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 176 | ND_PRINT((ndo, "ethertype Unknown (0x%04x)", |
| 177 | ether_type)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | } else { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 181 | ND_PRINT((ndo, "ethertype %s (0x%04x)", |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 182 | tok2str(ethertype_values, "Unknown", ether_type), |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 183 | ether_type)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 184 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 185 | ND_PRINT((ndo, ", length %u: ", length)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * This is the top level routine of the printer. 'p' points to the |
| 191 | * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp, |
| 192 | * 'h->len' is the length of the packet off the wire, and 'h->caplen' |
| 193 | * is the number of bytes actually captured. |
| 194 | */ |
| 195 | u_int |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 196 | sll_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] | 197 | { |
| 198 | u_int caplen = h->caplen; |
| 199 | u_int length = h->len; |
| 200 | register const struct sll_header *sllp; |
| 201 | u_short ether_type; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 202 | int llc_hdrlen; |
| 203 | u_int hdrlen; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 204 | |
| 205 | if (caplen < SLL_HDR_LEN) { |
| 206 | /* |
| 207 | * XXX - this "can't happen" because "pcap-linux.c" always |
| 208 | * adds this many bytes of header to every packet in a |
| 209 | * cooked socket capture. |
| 210 | */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 211 | ND_PRINT((ndo, "[|sll]")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 212 | return (caplen); |
| 213 | } |
| 214 | |
| 215 | sllp = (const struct sll_header *)p; |
| 216 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 217 | if (ndo->ndo_eflag) |
| 218 | sll_print(ndo, sllp, length); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Go past the cooked-mode header. |
| 222 | */ |
| 223 | length -= SLL_HDR_LEN; |
| 224 | caplen -= SLL_HDR_LEN; |
| 225 | p += SLL_HDR_LEN; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 226 | hdrlen = SLL_HDR_LEN; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 227 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 228 | ether_type = EXTRACT_16BITS(&sllp->sll_protocol); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 229 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 230 | recurse: |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 231 | /* |
| 232 | * Is it (gag) an 802.3 encapsulation, or some non-Ethernet |
| 233 | * packet type? |
| 234 | */ |
| 235 | if (ether_type <= ETHERMTU) { |
| 236 | /* |
| 237 | * Yes - what type is it? |
| 238 | */ |
| 239 | switch (ether_type) { |
| 240 | |
| 241 | case LINUX_SLL_P_802_3: |
| 242 | /* |
| 243 | * Ethernet_802.3 IPX frame. |
| 244 | */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 245 | ipx_print(ndo, p, length); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | case LINUX_SLL_P_802_2: |
| 249 | /* |
| 250 | * 802.2. |
| 251 | * Try to print the LLC-layer header & higher layers. |
| 252 | */ |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 253 | llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL); |
| 254 | if (llc_hdrlen < 0) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 255 | goto unknown; /* unknown LLC type */ |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 256 | hdrlen += llc_hdrlen; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | default: |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 260 | /*FALLTHROUGH*/ |
| 261 | |
| 262 | unknown: |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 263 | /* packet type not known, print raw packet */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 264 | if (!ndo->ndo_suppress_default_print) |
| 265 | ND_DEFAULTPRINT(p, caplen); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 266 | break; |
| 267 | } |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 268 | } else if (ether_type == ETHERTYPE_8021Q) { |
| 269 | /* |
| 270 | * Print VLAN information, and then go back and process |
| 271 | * the enclosed type field. |
| 272 | */ |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 273 | if (caplen < 4) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 274 | ND_PRINT((ndo, "[|vlan]")); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 275 | return (hdrlen + caplen); |
| 276 | } |
| 277 | if (length < 4) { |
| 278 | ND_PRINT((ndo, "[|vlan]")); |
| 279 | return (hdrlen + length); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 280 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 281 | if (ndo->ndo_eflag) { |
| 282 | uint16_t tag = EXTRACT_16BITS(p); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 283 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 284 | ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag))); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | ether_type = EXTRACT_16BITS(p + 2); |
| 288 | if (ether_type <= ETHERMTU) |
| 289 | ether_type = LINUX_SLL_P_802_2; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 290 | if (!ndo->ndo_qflag) { |
| 291 | ND_PRINT((ndo, "ethertype %s, ", |
| 292 | tok2str(ethertype_values, "Unknown", ether_type))); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 293 | } |
| 294 | p += 4; |
| 295 | length -= 4; |
| 296 | caplen -= 4; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 297 | hdrlen += 4; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 298 | goto recurse; |
| 299 | } else { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 300 | if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) { |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 301 | /* ether_type not known, print raw packet */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 302 | if (!ndo->ndo_eflag) |
| 303 | sll_print(ndo, sllp, length + SLL_HDR_LEN); |
| 304 | if (!ndo->ndo_suppress_default_print) |
| 305 | ND_DEFAULTPRINT(p, caplen); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 306 | } |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 309 | return (hdrlen); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 310 | } |