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
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 */
30 * Include diag-control.h before <net/if.h>, which too defines a macro
31 * named ND_UNREACHABLE.
33 #include "diag-control.h"
37 #include "netdissect-stdinc.h"
39 #define ND_LONGJMP_FROM_TCHECK
40 #include "netdissect.h"
41 #include "addrtoname.h"
42 #include "ethertype.h"
46 * For captures on Linux cooked sockets, we construct a fake header
49 * a 2-byte "packet type" which is one of:
51 * LINUX_SLL_HOST packet was sent to us
52 * LINUX_SLL_BROADCAST packet was broadcast
53 * LINUX_SLL_MULTICAST packet was multicast
54 * LINUX_SLL_OTHERHOST packet was sent to somebody else
55 * LINUX_SLL_OUTGOING packet was sent *by* us;
57 * a 2-byte Ethernet protocol field;
59 * a 2-byte link-layer type;
61 * a 2-byte link-layer address length;
63 * an 8-byte source link-layer address, whose actual length is
64 * specified by the previous value.
66 * All fields except for the link-layer address are in network byte order.
68 * DO NOT change the layout of this structure, or change any of the
69 * LINUX_SLL_ values below. If you must change the link-layer header
70 * for a "cooked" Linux capture, introduce a new DLT_ type (ask
71 * "tcpdump-workers@lists.tcpdump.org" for one, so that you don't give it
72 * a value that collides with a value already being used), and use the
73 * new header in captures of that type, so that programs that can
74 * handle DLT_LINUX_SLL captures will continue to handle them correctly
75 * without any change, and so that capture files with different headers
76 * can be told apart and programs that read them can dissect the
79 * This structure, and the #defines below, must be the same in the
80 * libpcap and tcpdump versions of "sll.h".
84 * A DLT_LINUX_SLL fake link-layer header.
86 #define SLL_HDR_LEN 16 /* total header length */
87 #define SLL_ADDRLEN 8 /* length of address field */
90 nd_uint16_t sll_pkttype
; /* packet type */
91 nd_uint16_t sll_hatype
; /* link-layer address type */
92 nd_uint16_t sll_halen
; /* link-layer address length */
93 nd_byte sll_addr
[SLL_ADDRLEN
]; /* link-layer address */
94 nd_uint16_t sll_protocol
; /* protocol */
98 * A DLT_LINUX_SLL2 fake link-layer header.
100 #define SLL2_HDR_LEN 20 /* total header length */
103 nd_uint16_t sll2_protocol
; /* protocol */
104 nd_uint16_t sll2_reserved_mbz
; /* reserved - must be zero */
105 nd_uint32_t sll2_if_index
; /* 1-based interface index */
106 nd_uint16_t sll2_hatype
; /* link-layer address type */
107 nd_uint8_t sll2_pkttype
; /* packet type */
108 nd_uint8_t sll2_halen
; /* link-layer address length */
109 nd_byte sll2_addr
[SLL_ADDRLEN
]; /* link-layer address */
113 * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
114 * PACKET_ values on Linux, but are defined here so that they're
115 * available even on systems other than Linux, and so that they
116 * don't change even if the PACKET_ values change.
118 #define LINUX_SLL_HOST 0
119 #define LINUX_SLL_BROADCAST 1
120 #define LINUX_SLL_MULTICAST 2
121 #define LINUX_SLL_OTHERHOST 3
122 #define LINUX_SLL_OUTGOING 4
125 * The LINUX_SLL_ values for "sll_protocol"; these correspond to the
126 * ETH_P_ values on Linux, but are defined here so that they're
127 * available even on systems other than Linux. We assume, for now,
128 * that the ETH_P_ values won't change in Linux; if they do, then:
130 * if we don't translate them in "pcap-linux.c", capture files
131 * won't necessarily be readable if captured on a system that
132 * defines ETH_P_ values that don't match these values;
134 * if we do translate them in "pcap-linux.c", that makes life
135 * unpleasant for the BPF code generator, as the values you test
136 * for in the kernel aren't the values that you test for when
137 * reading a capture file, so the fixup code run on BPF programs
138 * handed to the kernel ends up having to do more work.
140 * Add other values here as necessary, for handling packet types that
141 * might show up on non-Ethernet, non-802.x networks. (Not all the ones
142 * in the Linux "if_ether.h" will, I suspect, actually show up in
145 #define LINUX_SLL_P_802_3 0x0001 /* Novell 802.3 frames without 802.2 LLC header */
146 #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */
148 static const struct tok sll_pkttype_values
[] = {
149 { LINUX_SLL_HOST
, "In" },
150 { LINUX_SLL_BROADCAST
, "B" },
151 { LINUX_SLL_MULTICAST
, "M" },
152 { LINUX_SLL_OTHERHOST
, "P" },
153 { LINUX_SLL_OUTGOING
, "Out" },
158 sll_print(netdissect_options
*ndo
, const struct sll_header
*sllp
, u_int length
)
162 ndo
->ndo_protocol
= "sll";
164 tok2str(sll_pkttype_values
,"?",GET_BE_U_2(sllp
->sll_pkttype
)));
167 * XXX - check the link-layer address type value?
168 * For now, we just assume 6 means Ethernet.
169 * XXX - print others as strings of hex?
171 if (GET_BE_U_2(sllp
->sll_halen
) == MAC_ADDR_LEN
)
172 ND_PRINT("%s ", GET_ETHERADDR_STRING(sllp
->sll_addr
));
174 if (!ndo
->ndo_qflag
) {
175 ether_type
= GET_BE_U_2(sllp
->sll_protocol
);
177 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
) {
179 * Not an Ethernet type; what type is it?
181 switch (ether_type
) {
183 case LINUX_SLL_P_802_3
:
185 * Ethernet_802.3 IPX frame.
190 case LINUX_SLL_P_802_2
:
201 ND_PRINT("ethertype Unknown (0x%04x)",
206 ND_PRINT("ethertype %s (0x%04x)",
207 tok2str(ethertype_values
, "Unknown", ether_type
),
210 ND_PRINT(", length %u: ", length
);
215 * This is the top level routine of the printer. 'p' points to the
216 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
217 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
218 * is the number of bytes actually captured.
221 sll_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
, const u_char
*p
)
223 u_int caplen
= h
->caplen
;
224 u_int length
= h
->len
;
225 const struct sll_header
*sllp
;
231 ndo
->ndo_protocol
= "sll";
232 ND_TCHECK_LEN(p
, SLL_HDR_LEN
);
234 sllp
= (const struct sll_header
*)p
;
237 sll_print(ndo
, sllp
, length
);
240 * Go past the cooked-mode header.
242 length
-= SLL_HDR_LEN
;
243 caplen
-= SLL_HDR_LEN
;
245 hdrlen
= SLL_HDR_LEN
;
247 hatype
= GET_BE_U_2(sllp
->sll_hatype
);
252 * This is an packet with a radiotap header;
253 * just dissect the payload as such.
255 ndo
->ndo_ll_hdr_len
+= SLL_HDR_LEN
;
256 ndo
->ndo_ll_hdr_len
+= ieee802_11_radio_print(ndo
, p
, length
, caplen
);
259 ether_type
= GET_BE_U_2(sllp
->sll_protocol
);
263 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
266 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
) {
268 * Yes - what type is it?
270 switch (ether_type
) {
272 case LINUX_SLL_P_802_3
:
274 * Ethernet_802.3 IPX frame.
276 ipx_print(ndo
, p
, length
);
279 case LINUX_SLL_P_802_2
:
282 * Try to print the LLC-layer header & higher layers.
284 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, NULL
, NULL
);
286 goto unknown
; /* unknown LLC type */
287 hdrlen
+= llc_hdrlen
;
294 /* packet type not known, print raw packet */
295 if (!ndo
->ndo_suppress_default_print
)
296 ND_DEFAULTPRINT(p
, caplen
);
299 } else if (ether_type
== ETHERTYPE_8021Q
) {
301 * Print VLAN information, and then go back and process
302 * the enclosed type field.
305 ndo
->ndo_protocol
= "vlan";
307 ndo
->ndo_ll_hdr_len
+= hdrlen
+ caplen
;
310 if (ndo
->ndo_eflag
) {
311 uint16_t tag
= GET_BE_U_2(p
);
313 ND_PRINT("%s, ", ieee8021q_tci_string(tag
));
316 ether_type
= GET_BE_U_2(p
+ 2);
317 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
)
318 ether_type
= LINUX_SLL_P_802_2
;
319 if (!ndo
->ndo_qflag
) {
320 ND_PRINT("ethertype %s, ",
321 tok2str(ethertype_values
, "Unknown", ether_type
));
329 if (ethertype_print(ndo
, ether_type
, p
, length
, caplen
, NULL
, NULL
) == 0) {
330 /* ether_type not known, print raw packet */
332 sll_print(ndo
, sllp
, length
+ SLL_HDR_LEN
);
333 if (!ndo
->ndo_suppress_default_print
)
334 ND_DEFAULTPRINT(p
, caplen
);
338 ndo
->ndo_ll_hdr_len
+= hdrlen
;
342 sll2_print(netdissect_options
*ndo
, const struct sll2_header
*sllp
, u_int length
)
346 ndo
->ndo_protocol
= "sll2";
347 ND_PRINT("ifindex %u ", GET_BE_U_4(sllp
->sll2_if_index
));
350 * XXX - check the link-layer address type value?
351 * For now, we just assume 6 means Ethernet.
352 * XXX - print others as strings of hex?
354 if (GET_U_1(sllp
->sll2_halen
) == MAC_ADDR_LEN
)
355 ND_PRINT("%s ", GET_ETHERADDR_STRING(sllp
->sll2_addr
));
357 if (!ndo
->ndo_qflag
) {
358 ether_type
= GET_BE_U_2(sllp
->sll2_protocol
);
360 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
) {
362 * Not an Ethernet type; what type is it?
364 switch (ether_type
) {
366 case LINUX_SLL_P_802_3
:
368 * Ethernet_802.3 IPX frame.
373 case LINUX_SLL_P_802_2
:
384 ND_PRINT("ethertype Unknown (0x%04x)",
389 ND_PRINT("ethertype %s (0x%04x)",
390 tok2str(ethertype_values
, "Unknown", ether_type
),
393 ND_PRINT(", length %u: ", length
);
398 * This is the top level routine of the printer. 'p' points to the
399 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
400 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
401 * is the number of bytes actually captured.
404 sll2_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
, const u_char
*p
)
406 u_int caplen
= h
->caplen
;
407 u_int length
= h
->len
;
408 const struct sll2_header
*sllp
;
415 char ifname
[IF_NAMESIZE
];
418 ndo
->ndo_protocol
= "sll2";
419 ND_TCHECK_LEN(p
, SLL2_HDR_LEN
);
421 sllp
= (const struct sll2_header
*)p
;
423 if_index
= GET_BE_U_4(sllp
->sll2_if_index
);
424 if (!if_indextoname(if_index
, ifname
))
425 strncpy(ifname
, "?", 2);
426 ND_PRINT("%-5s ", ifname
);
430 tok2str(sll_pkttype_values
, "?", GET_U_1(sllp
->sll2_pkttype
)));
433 sll2_print(ndo
, sllp
, length
);
436 * Go past the cooked-mode header.
438 length
-= SLL2_HDR_LEN
;
439 caplen
-= SLL2_HDR_LEN
;
441 hdrlen
= SLL2_HDR_LEN
;
443 hatype
= GET_BE_U_2(sllp
->sll2_hatype
);
448 * This is an packet with a radiotap header;
449 * just dissect the payload as such.
451 ndo
->ndo_ll_hdr_len
+= SLL2_HDR_LEN
;
452 ndo
->ndo_ll_hdr_len
+= ieee802_11_radio_print(ndo
, p
, length
, caplen
);
455 ether_type
= GET_BE_U_2(sllp
->sll2_protocol
);
459 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
462 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
) {
464 * Yes - what type is it?
466 switch (ether_type
) {
468 case LINUX_SLL_P_802_3
:
470 * Ethernet_802.3 IPX frame.
472 ipx_print(ndo
, p
, length
);
475 case LINUX_SLL_P_802_2
:
478 * Try to print the LLC-layer header & higher layers.
480 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, NULL
, NULL
);
482 goto unknown
; /* unknown LLC type */
483 hdrlen
+= llc_hdrlen
;
490 /* packet type not known, print raw packet */
491 if (!ndo
->ndo_suppress_default_print
)
492 ND_DEFAULTPRINT(p
, caplen
);
495 } else if (ether_type
== ETHERTYPE_8021Q
) {
497 * Print VLAN information, and then go back and process
498 * the enclosed type field.
501 ndo
->ndo_protocol
= "vlan";
503 ndo
->ndo_ll_hdr_len
+= hdrlen
+ caplen
;
506 if (ndo
->ndo_eflag
) {
507 uint16_t tag
= GET_BE_U_2(p
);
509 ND_PRINT("%s, ", ieee8021q_tci_string(tag
));
512 ether_type
= GET_BE_U_2(p
+ 2);
513 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
)
514 ether_type
= LINUX_SLL_P_802_2
;
515 if (!ndo
->ndo_qflag
) {
516 ND_PRINT("ethertype %s, ",
517 tok2str(ethertype_values
, "Unknown", ether_type
));
525 if (ethertype_print(ndo
, ether_type
, p
, length
, caplen
, NULL
, NULL
) == 0) {
526 /* ether_type not known, print raw packet */
528 sll2_print(ndo
, sllp
, length
+ SLL2_HDR_LEN
);
529 if (!ndo
->ndo_suppress_default_print
)
530 ND_DEFAULTPRINT(p
, caplen
);
534 ndo
->ndo_ll_hdr_len
+= hdrlen
;