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 */
32 #include "netdissect-stdinc.h"
34 #define ND_LONGJMP_FROM_TCHECK
35 #include "netdissect.h"
36 #include "addrtoname.h"
37 #include "ethertype.h"
41 * For captures on Linux cooked sockets, we construct a fake header
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
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 */
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 */
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
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" },
153 sll_print(netdissect_options
*ndo
, const struct sll_header
*sllp
, u_int length
)
157 ndo
->ndo_protocol
= "sll";
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.
185 case LINUX_SLL_P_802_2
:
196 ND_PRINT("ethertype Unknown (0x%04x)",
201 ND_PRINT("ethertype %s (0x%04x)",
202 tok2str(ethertype_values
, "Unknown", 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.
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
;
226 ndo
->ndo_protocol
= "sll";
227 ND_TCHECK_LEN(p
, SLL_HDR_LEN
);
229 sllp
= (const struct sll_header
*)p
;
232 sll_print(ndo
, sllp
, length
);
235 * Go past the cooked-mode header.
237 length
-= SLL_HDR_LEN
;
238 caplen
-= SLL_HDR_LEN
;
240 hdrlen
= SLL_HDR_LEN
;
242 hatype
= GET_BE_U_2(sllp
->sll_hatype
);
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
);
254 ether_type
= GET_BE_U_2(sllp
->sll_protocol
);
258 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
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
);
274 case LINUX_SLL_P_802_2
:
277 * Try to print the LLC-layer header & higher layers.
279 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, NULL
, NULL
);
281 goto unknown
; /* unknown LLC type */
282 hdrlen
+= llc_hdrlen
;
289 /* packet type not known, print raw packet */
290 if (!ndo
->ndo_suppress_default_print
)
291 ND_DEFAULTPRINT(p
, caplen
);
294 } else if (ether_type
== ETHERTYPE_8021Q
) {
296 * Print VLAN information, and then go back and process
297 * the enclosed type field.
300 ndo
->ndo_protocol
= "vlan";
302 ndo
->ndo_ll_hdr_len
+= hdrlen
+ caplen
;
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
));
324 if (ethertype_print(ndo
, ether_type
, p
, length
, caplen
, NULL
, NULL
) == 0) {
325 /* ether_type not known, print raw packet */
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
;
337 sll2_print(netdissect_options
*ndo
, const struct sll2_header
*sllp
, u_int length
)
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.
368 case LINUX_SLL_P_802_2
:
379 ND_PRINT("ethertype Unknown (0x%04x)",
384 ND_PRINT("ethertype %s (0x%04x)",
385 tok2str(ethertype_values
, "Unknown", 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.
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
;
410 char ifname
[IF_NAMESIZE
];
413 ndo
->ndo_protocol
= "sll2";
414 ND_TCHECK_LEN(p
, SLL2_HDR_LEN
);
416 sllp
= (const struct sll2_header
*)p
;
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
);
425 tok2str(sll_pkttype_values
, "?", GET_U_1(sllp
->sll2_pkttype
)));
428 sll2_print(ndo
, sllp
, length
);
431 * Go past the cooked-mode header.
433 length
-= SLL2_HDR_LEN
;
434 caplen
-= SLL2_HDR_LEN
;
436 hdrlen
= SLL2_HDR_LEN
;
438 hatype
= GET_BE_U_2(sllp
->sll2_hatype
);
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
);
450 ether_type
= GET_BE_U_2(sllp
->sll2_protocol
);
454 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
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
);
470 case LINUX_SLL_P_802_2
:
473 * Try to print the LLC-layer header & higher layers.
475 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, NULL
, NULL
);
477 goto unknown
; /* unknown LLC type */
478 hdrlen
+= llc_hdrlen
;
485 /* packet type not known, print raw packet */
486 if (!ndo
->ndo_suppress_default_print
)
487 ND_DEFAULTPRINT(p
, caplen
);
490 } else if (ether_type
== ETHERTYPE_8021Q
) {
492 * Print VLAN information, and then go back and process
493 * the enclosed type field.
496 ndo
->ndo_protocol
= "vlan";
498 ndo
->ndo_ll_hdr_len
+= hdrlen
+ caplen
;
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
));
520 if (ethertype_print(ndo
, ether_type
, p
, length
, caplen
, NULL
, NULL
) == 0) {
521 /* ether_type not known, print raw packet */
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
;