kernel - Fix auto port assignment collision in network code
[dragonfly.git] / contrib / tcpdump / print-sll.c
blob19d2973edadec81cc1ad0fa18c25eb0ba96e2e03
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.
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.
22 /* \summary: Linux cooked sockets capture printer */
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #ifdef HAVE_NET_IF_H
29 #include <net/if.h>
30 #endif
32 #include "netdissect-stdinc.h"
34 #define ND_LONGJMP_FROM_TCHECK
35 #include "netdissect.h"
36 #include "addrtoname.h"
37 #include "ethertype.h"
38 #include "extract.h"
41 * For captures on Linux cooked sockets, we construct a fake header
42 * that includes:
44 * a 2-byte "packet type" which is one of:
46 * LINUX_SLL_HOST packet was sent to us
47 * LINUX_SLL_BROADCAST packet was broadcast
48 * LINUX_SLL_MULTICAST packet was multicast
49 * LINUX_SLL_OTHERHOST packet was sent to somebody else
50 * LINUX_SLL_OUTGOING packet was sent *by* us;
52 * a 2-byte Ethernet protocol field;
54 * a 2-byte link-layer type;
56 * a 2-byte link-layer address length;
58 * an 8-byte source link-layer address, whose actual length is
59 * specified by the previous value.
61 * All fields except for the link-layer address are in network byte order.
63 * DO NOT change the layout of this structure, or change any of the
64 * LINUX_SLL_ values below. If you must change the link-layer header
65 * for a "cooked" Linux capture, introduce a new DLT_ type (ask
66 * "tcpdump-workers@lists.tcpdump.org" for one, so that you don't give it
67 * a value that collides with a value already being used), and use the
68 * new header in captures of that type, so that programs that can
69 * handle DLT_LINUX_SLL captures will continue to handle them correctly
70 * without any change, and so that capture files with different headers
71 * can be told apart and programs that read them can dissect the
72 * packets in them.
74 * This structure, and the #defines below, must be the same in the
75 * libpcap and tcpdump versions of "sll.h".
79 * A DLT_LINUX_SLL fake link-layer header.
81 #define SLL_HDR_LEN 16 /* total header length */
82 #define SLL_ADDRLEN 8 /* length of address field */
84 struct sll_header {
85 nd_uint16_t sll_pkttype; /* packet type */
86 nd_uint16_t sll_hatype; /* link-layer address type */
87 nd_uint16_t sll_halen; /* link-layer address length */
88 nd_byte sll_addr[SLL_ADDRLEN]; /* link-layer address */
89 nd_uint16_t sll_protocol; /* protocol */
93 * A DLT_LINUX_SLL2 fake link-layer header.
95 #define SLL2_HDR_LEN 20 /* total header length */
97 struct sll2_header {
98 nd_uint16_t sll2_protocol; /* protocol */
99 nd_uint16_t sll2_reserved_mbz; /* reserved - must be zero */
100 nd_uint32_t sll2_if_index; /* 1-based interface index */
101 nd_uint16_t sll2_hatype; /* link-layer address type */
102 nd_uint8_t sll2_pkttype; /* packet type */
103 nd_uint8_t sll2_halen; /* link-layer address length */
104 nd_byte sll2_addr[SLL_ADDRLEN]; /* link-layer address */
108 * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
109 * PACKET_ values on Linux, but are defined here so that they're
110 * available even on systems other than Linux, and so that they
111 * don't change even if the PACKET_ values change.
113 #define LINUX_SLL_HOST 0
114 #define LINUX_SLL_BROADCAST 1
115 #define LINUX_SLL_MULTICAST 2
116 #define LINUX_SLL_OTHERHOST 3
117 #define LINUX_SLL_OUTGOING 4
120 * The LINUX_SLL_ values for "sll_protocol"; these correspond to the
121 * ETH_P_ values on Linux, but are defined here so that they're
122 * available even on systems other than Linux. We assume, for now,
123 * that the ETH_P_ values won't change in Linux; if they do, then:
125 * if we don't translate them in "pcap-linux.c", capture files
126 * won't necessarily be readable if captured on a system that
127 * defines ETH_P_ values that don't match these values;
129 * if we do translate them in "pcap-linux.c", that makes life
130 * unpleasant for the BPF code generator, as the values you test
131 * for in the kernel aren't the values that you test for when
132 * reading a capture file, so the fixup code run on BPF programs
133 * handed to the kernel ends up having to do more work.
135 * Add other values here as necessary, for handling packet types that
136 * might show up on non-Ethernet, non-802.x networks. (Not all the ones
137 * in the Linux "if_ether.h" will, I suspect, actually show up in
138 * captures.)
140 #define LINUX_SLL_P_802_3 0x0001 /* Novell 802.3 frames without 802.2 LLC header */
141 #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */
143 static const struct tok sll_pkttype_values[] = {
144 { LINUX_SLL_HOST, "In" },
145 { LINUX_SLL_BROADCAST, "B" },
146 { LINUX_SLL_MULTICAST, "M" },
147 { LINUX_SLL_OTHERHOST, "P" },
148 { LINUX_SLL_OUTGOING, "Out" },
149 { 0, NULL}
152 static void
153 sll_print(netdissect_options *ndo, const struct sll_header *sllp, u_int length)
155 u_short ether_type;
157 ndo->ndo_protocol = "sll";
158 ND_PRINT("%3s ",
159 tok2str(sll_pkttype_values,"?",GET_BE_U_2(sllp->sll_pkttype)));
162 * XXX - check the link-layer address type value?
163 * For now, we just assume 6 means Ethernet.
164 * XXX - print others as strings of hex?
166 if (GET_BE_U_2(sllp->sll_halen) == MAC_ADDR_LEN)
167 ND_PRINT("%s ", GET_ETHERADDR_STRING(sllp->sll_addr));
169 if (!ndo->ndo_qflag) {
170 ether_type = GET_BE_U_2(sllp->sll_protocol);
172 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
174 * Not an Ethernet type; what type is it?
176 switch (ether_type) {
178 case LINUX_SLL_P_802_3:
180 * Ethernet_802.3 IPX frame.
182 ND_PRINT("802.3");
183 break;
185 case LINUX_SLL_P_802_2:
187 * 802.2.
189 ND_PRINT("802.2");
190 break;
192 default:
194 * What is it?
196 ND_PRINT("ethertype Unknown (0x%04x)",
197 ether_type);
198 break;
200 } else {
201 ND_PRINT("ethertype %s (0x%04x)",
202 tok2str(ethertype_values, "Unknown", ether_type),
203 ether_type);
205 ND_PRINT(", length %u: ", length);
210 * This is the top level routine of the printer. 'p' points to the
211 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
212 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
213 * is the number of bytes actually captured.
215 void
216 sll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
218 u_int caplen = h->caplen;
219 u_int length = h->len;
220 const struct sll_header *sllp;
221 u_short hatype;
222 u_short ether_type;
223 int llc_hdrlen;
224 u_int hdrlen;
226 ndo->ndo_protocol = "sll";
227 ND_TCHECK_LEN(p, SLL_HDR_LEN);
229 sllp = (const struct sll_header *)p;
231 if (ndo->ndo_eflag)
232 sll_print(ndo, sllp, length);
235 * Go past the cooked-mode header.
237 length -= SLL_HDR_LEN;
238 caplen -= SLL_HDR_LEN;
239 p += SLL_HDR_LEN;
240 hdrlen = SLL_HDR_LEN;
242 hatype = GET_BE_U_2(sllp->sll_hatype);
243 switch (hatype) {
245 case 803:
247 * This is an packet with a radiotap header;
248 * just dissect the payload as such.
250 ndo->ndo_ll_hdr_len += SLL_HDR_LEN;
251 ndo->ndo_ll_hdr_len += ieee802_11_radio_print(ndo, p, length, caplen);
252 return;
254 ether_type = GET_BE_U_2(sllp->sll_protocol);
256 recurse:
258 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
259 * packet type?
261 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
263 * Yes - what type is it?
265 switch (ether_type) {
267 case LINUX_SLL_P_802_3:
269 * Ethernet_802.3 IPX frame.
271 ipx_print(ndo, p, length);
272 break;
274 case LINUX_SLL_P_802_2:
276 * 802.2.
277 * Try to print the LLC-layer header & higher layers.
279 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
280 if (llc_hdrlen < 0)
281 goto unknown; /* unknown LLC type */
282 hdrlen += llc_hdrlen;
283 break;
285 default:
286 /*FALLTHROUGH*/
288 unknown:
289 /* packet type not known, print raw packet */
290 if (!ndo->ndo_suppress_default_print)
291 ND_DEFAULTPRINT(p, caplen);
292 break;
294 } else if (ether_type == ETHERTYPE_8021Q) {
296 * Print VLAN information, and then go back and process
297 * the enclosed type field.
299 if (caplen < 4) {
300 ndo->ndo_protocol = "vlan";
301 nd_print_trunc(ndo);
302 ndo->ndo_ll_hdr_len += hdrlen + caplen;
303 return;
305 if (ndo->ndo_eflag) {
306 uint16_t tag = GET_BE_U_2(p);
308 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
311 ether_type = GET_BE_U_2(p + 2);
312 if (ether_type <= MAX_ETHERNET_LENGTH_VAL)
313 ether_type = LINUX_SLL_P_802_2;
314 if (!ndo->ndo_qflag) {
315 ND_PRINT("ethertype %s, ",
316 tok2str(ethertype_values, "Unknown", ether_type));
318 p += 4;
319 length -= 4;
320 caplen -= 4;
321 hdrlen += 4;
322 goto recurse;
323 } else {
324 if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
325 /* ether_type not known, print raw packet */
326 if (!ndo->ndo_eflag)
327 sll_print(ndo, sllp, length + SLL_HDR_LEN);
328 if (!ndo->ndo_suppress_default_print)
329 ND_DEFAULTPRINT(p, caplen);
333 ndo->ndo_ll_hdr_len += hdrlen;
336 static void
337 sll2_print(netdissect_options *ndo, const struct sll2_header *sllp, u_int length)
339 u_short ether_type;
341 ndo->ndo_protocol = "sll2";
342 ND_PRINT("ifindex %u ", GET_BE_U_4(sllp->sll2_if_index));
345 * XXX - check the link-layer address type value?
346 * For now, we just assume 6 means Ethernet.
347 * XXX - print others as strings of hex?
349 if (GET_U_1(sllp->sll2_halen) == MAC_ADDR_LEN)
350 ND_PRINT("%s ", GET_ETHERADDR_STRING(sllp->sll2_addr));
352 if (!ndo->ndo_qflag) {
353 ether_type = GET_BE_U_2(sllp->sll2_protocol);
355 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
357 * Not an Ethernet type; what type is it?
359 switch (ether_type) {
361 case LINUX_SLL_P_802_3:
363 * Ethernet_802.3 IPX frame.
365 ND_PRINT("802.3");
366 break;
368 case LINUX_SLL_P_802_2:
370 * 802.2.
372 ND_PRINT("802.2");
373 break;
375 default:
377 * What is it?
379 ND_PRINT("ethertype Unknown (0x%04x)",
380 ether_type);
381 break;
383 } else {
384 ND_PRINT("ethertype %s (0x%04x)",
385 tok2str(ethertype_values, "Unknown", ether_type),
386 ether_type);
388 ND_PRINT(", length %u: ", length);
393 * This is the top level routine of the printer. 'p' points to the
394 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
395 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
396 * is the number of bytes actually captured.
398 void
399 sll2_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
401 u_int caplen = h->caplen;
402 u_int length = h->len;
403 const struct sll2_header *sllp;
404 u_short hatype;
405 u_short ether_type;
406 int llc_hdrlen;
407 u_int hdrlen;
408 #ifdef HAVE_NET_IF_H
409 uint32_t if_index;
410 char ifname[IF_NAMESIZE];
411 #endif
413 ndo->ndo_protocol = "sll2";
414 ND_TCHECK_LEN(p, SLL2_HDR_LEN);
416 sllp = (const struct sll2_header *)p;
417 #ifdef HAVE_NET_IF_H
418 if_index = GET_BE_U_4(sllp->sll2_if_index);
419 if (!if_indextoname(if_index, ifname))
420 strncpy(ifname, "?", 2);
421 ND_PRINT("%-5s ", ifname);
422 #endif
424 ND_PRINT("%-3s ",
425 tok2str(sll_pkttype_values, "?", GET_U_1(sllp->sll2_pkttype)));
427 if (ndo->ndo_eflag)
428 sll2_print(ndo, sllp, length);
431 * Go past the cooked-mode header.
433 length -= SLL2_HDR_LEN;
434 caplen -= SLL2_HDR_LEN;
435 p += SLL2_HDR_LEN;
436 hdrlen = SLL2_HDR_LEN;
438 hatype = GET_BE_U_2(sllp->sll2_hatype);
439 switch (hatype) {
441 case 803:
443 * This is an packet with a radiotap header;
444 * just dissect the payload as such.
446 ndo->ndo_ll_hdr_len += SLL2_HDR_LEN;
447 ndo->ndo_ll_hdr_len += ieee802_11_radio_print(ndo, p, length, caplen);
448 return;
450 ether_type = GET_BE_U_2(sllp->sll2_protocol);
452 recurse:
454 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
455 * packet type?
457 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
459 * Yes - what type is it?
461 switch (ether_type) {
463 case LINUX_SLL_P_802_3:
465 * Ethernet_802.3 IPX frame.
467 ipx_print(ndo, p, length);
468 break;
470 case LINUX_SLL_P_802_2:
472 * 802.2.
473 * Try to print the LLC-layer header & higher layers.
475 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
476 if (llc_hdrlen < 0)
477 goto unknown; /* unknown LLC type */
478 hdrlen += llc_hdrlen;
479 break;
481 default:
482 /*FALLTHROUGH*/
484 unknown:
485 /* packet type not known, print raw packet */
486 if (!ndo->ndo_suppress_default_print)
487 ND_DEFAULTPRINT(p, caplen);
488 break;
490 } else if (ether_type == ETHERTYPE_8021Q) {
492 * Print VLAN information, and then go back and process
493 * the enclosed type field.
495 if (caplen < 4) {
496 ndo->ndo_protocol = "vlan";
497 nd_print_trunc(ndo);
498 ndo->ndo_ll_hdr_len += hdrlen + caplen;
499 return;
501 if (ndo->ndo_eflag) {
502 uint16_t tag = GET_BE_U_2(p);
504 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
507 ether_type = GET_BE_U_2(p + 2);
508 if (ether_type <= MAX_ETHERNET_LENGTH_VAL)
509 ether_type = LINUX_SLL_P_802_2;
510 if (!ndo->ndo_qflag) {
511 ND_PRINT("ethertype %s, ",
512 tok2str(ethertype_values, "Unknown", ether_type));
514 p += 4;
515 length -= 4;
516 caplen -= 4;
517 hdrlen += 4;
518 goto recurse;
519 } else {
520 if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
521 /* ether_type not known, print raw packet */
522 if (!ndo->ndo_eflag)
523 sll2_print(ndo, sllp, length + SLL2_HDR_LEN);
524 if (!ndo->ndo_suppress_default_print)
525 ND_DEFAULTPRINT(p, caplen);
529 ndo->ndo_ll_hdr_len += hdrlen;