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 */
28 #include "netdissect-stdinc.h"
30 #include "netdissect.h"
31 #include "addrtoname.h"
32 #include "ethertype.h"
36 * For captures on Linux cooked sockets, we construct a fake header
39 * a 2-byte "packet type" which is one of:
41 * LINUX_SLL_HOST packet was sent to us
42 * LINUX_SLL_BROADCAST packet was broadcast
43 * LINUX_SLL_MULTICAST packet was multicast
44 * LINUX_SLL_OTHERHOST packet was sent to somebody else
45 * LINUX_SLL_OUTGOING packet was sent *by* us;
47 * a 2-byte Ethernet protocol field;
49 * a 2-byte link-layer type;
51 * a 2-byte link-layer address length;
53 * an 8-byte source link-layer address, whose actual length is
54 * specified by the previous value.
56 * All fields except for the link-layer address are in network byte order.
58 * DO NOT change the layout of this structure, or change any of the
59 * LINUX_SLL_ values below. If you must change the link-layer header
60 * for a "cooked" Linux capture, introduce a new DLT_ type (ask
61 * "tcpdump-workers@lists.tcpdump.org" for one, so that you don't give it
62 * a value that collides with a value already being used), and use the
63 * new header in captures of that type, so that programs that can
64 * handle DLT_LINUX_SLL captures will continue to handle them correctly
65 * without any change, and so that capture files with different headers
66 * can be told apart and programs that read them can dissect the
69 * This structure, and the #defines below, must be the same in the
70 * libpcap and tcpdump versions of "sll.h".
74 * A DLT_LINUX_SLL fake link-layer header.
76 #define SLL_HDR_LEN 16 /* total header length */
77 #define SLL_ADDRLEN 8 /* length of address field */
80 nd_uint16_t sll_pkttype
; /* packet type */
81 nd_uint16_t sll_hatype
; /* link-layer address type */
82 nd_uint16_t sll_halen
; /* link-layer address length */
83 nd_byte sll_addr
[SLL_ADDRLEN
]; /* link-layer address */
84 nd_uint16_t sll_protocol
; /* protocol */
88 * A DLT_LINUX_SLL2 fake link-layer header.
90 #define SLL2_HDR_LEN 20 /* total header length */
93 nd_uint16_t sll2_protocol
; /* protocol */
94 nd_uint16_t sll2_reserved_mbz
; /* reserved - must be zero */
95 nd_uint32_t sll2_if_index
; /* 1-based interface index */
96 nd_uint16_t sll2_hatype
; /* link-layer address type */
97 nd_uint8_t sll2_pkttype
; /* packet type */
98 nd_uint8_t sll2_halen
; /* link-layer address length */
99 nd_byte sll2_addr
[SLL_ADDRLEN
]; /* link-layer address */
103 * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
104 * PACKET_ values on Linux, but are defined here so that they're
105 * available even on systems other than Linux, and so that they
106 * don't change even if the PACKET_ values change.
108 #define LINUX_SLL_HOST 0
109 #define LINUX_SLL_BROADCAST 1
110 #define LINUX_SLL_MULTICAST 2
111 #define LINUX_SLL_OTHERHOST 3
112 #define LINUX_SLL_OUTGOING 4
115 * The LINUX_SLL_ values for "sll_protocol"; these correspond to the
116 * ETH_P_ values on Linux, but are defined here so that they're
117 * available even on systems other than Linux. We assume, for now,
118 * that the ETH_P_ values won't change in Linux; if they do, then:
120 * if we don't translate them in "pcap-linux.c", capture files
121 * won't necessarily be readable if captured on a system that
122 * defines ETH_P_ values that don't match these values;
124 * if we do translate them in "pcap-linux.c", that makes life
125 * unpleasant for the BPF code generator, as the values you test
126 * for in the kernel aren't the values that you test for when
127 * reading a capture file, so the fixup code run on BPF programs
128 * handed to the kernel ends up having to do more work.
130 * Add other values here as necessary, for handling packet types that
131 * might show up on non-Ethernet, non-802.x networks. (Not all the ones
132 * in the Linux "if_ether.h" will, I suspect, actually show up in
135 #define LINUX_SLL_P_802_3 0x0001 /* Novell 802.3 frames without 802.2 LLC header */
136 #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */
138 static const struct tok sll_pkttype_values
[] = {
139 { LINUX_SLL_HOST
, "In" },
140 { LINUX_SLL_BROADCAST
, "B" },
141 { LINUX_SLL_MULTICAST
, "M" },
142 { LINUX_SLL_OTHERHOST
, "P" },
143 { LINUX_SLL_OUTGOING
, "Out" },
148 sll_print(netdissect_options
*ndo
, const struct sll_header
*sllp
, u_int length
)
152 ndo
->ndo_protocol
= "sll";
153 ND_PRINT("%3s ",tok2str(sll_pkttype_values
,"?",EXTRACT_BE_U_2(sllp
->sll_pkttype
)));
156 * XXX - check the link-layer address type value?
157 * For now, we just assume 6 means Ethernet.
158 * XXX - print others as strings of hex?
160 if (EXTRACT_BE_U_2(sllp
->sll_halen
) == 6)
161 ND_PRINT("%s ", etheraddr_string(ndo
, sllp
->sll_addr
));
163 if (!ndo
->ndo_qflag
) {
164 ether_type
= EXTRACT_BE_U_2(sllp
->sll_protocol
);
166 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
) {
168 * Not an Ethernet type; what type is it?
170 switch (ether_type
) {
172 case LINUX_SLL_P_802_3
:
174 * Ethernet_802.3 IPX frame.
179 case LINUX_SLL_P_802_2
:
190 ND_PRINT("ethertype Unknown (0x%04x)",
195 ND_PRINT("ethertype %s (0x%04x)",
196 tok2str(ethertype_values
, "Unknown", ether_type
),
199 ND_PRINT(", length %u: ", length
);
204 * This is the top level routine of the printer. 'p' points to the
205 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
206 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
207 * is the number of bytes actually captured.
210 sll_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
, const u_char
*p
)
212 u_int caplen
= h
->caplen
;
213 u_int length
= h
->len
;
214 const struct sll_header
*sllp
;
219 ndo
->ndo_protocol
= "sll_if";
220 if (caplen
< SLL_HDR_LEN
) {
222 * XXX - this "can't happen" because "pcap-linux.c" always
223 * adds this many bytes of header to every packet in a
224 * cooked socket capture.
230 sllp
= (const struct sll_header
*)p
;
233 sll_print(ndo
, sllp
, length
);
236 * Go past the cooked-mode header.
238 length
-= SLL_HDR_LEN
;
239 caplen
-= SLL_HDR_LEN
;
241 hdrlen
= SLL_HDR_LEN
;
243 ether_type
= EXTRACT_BE_U_2(sllp
->sll_protocol
);
247 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
250 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
) {
252 * Yes - what type is it?
254 switch (ether_type
) {
256 case LINUX_SLL_P_802_3
:
258 * Ethernet_802.3 IPX frame.
260 ipx_print(ndo
, p
, length
);
263 case LINUX_SLL_P_802_2
:
266 * Try to print the LLC-layer header & higher layers.
268 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, NULL
, NULL
);
270 goto unknown
; /* unknown LLC type */
271 hdrlen
+= llc_hdrlen
;
278 /* packet type not known, print raw packet */
279 if (!ndo
->ndo_suppress_default_print
)
280 ND_DEFAULTPRINT(p
, caplen
);
283 } else if (ether_type
== ETHERTYPE_8021Q
) {
285 * Print VLAN information, and then go back and process
286 * the enclosed type field.
290 return (hdrlen
+ caplen
);
294 return (hdrlen
+ length
);
296 if (ndo
->ndo_eflag
) {
297 uint16_t tag
= EXTRACT_BE_U_2(p
);
299 ND_PRINT("%s, ", ieee8021q_tci_string(tag
));
302 ether_type
= EXTRACT_BE_U_2(p
+ 2);
303 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
)
304 ether_type
= LINUX_SLL_P_802_2
;
305 if (!ndo
->ndo_qflag
) {
306 ND_PRINT("ethertype %s, ",
307 tok2str(ethertype_values
, "Unknown", ether_type
));
315 if (ethertype_print(ndo
, ether_type
, p
, length
, caplen
, NULL
, NULL
) == 0) {
316 /* ether_type not known, print raw packet */
318 sll_print(ndo
, sllp
, length
+ SLL_HDR_LEN
);
319 if (!ndo
->ndo_suppress_default_print
)
320 ND_DEFAULTPRINT(p
, caplen
);
328 sll2_print(netdissect_options
*ndo
, const struct sll2_header
*sllp
, u_int length
)
332 ndo
->ndo_protocol
= "sll2";
333 ND_PRINT("%3s ",tok2str(sll_pkttype_values
,"?",EXTRACT_BE_U_2(sllp
->sll2_pkttype
)));
336 * XXX - check the link-layer address type value?
337 * For now, we just assume 6 means Ethernet.
338 * XXX - print others as strings of hex?
340 if (EXTRACT_U_1(sllp
->sll2_halen
) == 6)
341 ND_PRINT("%s ", etheraddr_string(ndo
, sllp
->sll2_addr
));
343 if (!ndo
->ndo_qflag
) {
344 ether_type
= EXTRACT_BE_U_2(sllp
->sll2_protocol
);
346 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
) {
348 * Not an Ethernet type; what type is it?
350 switch (ether_type
) {
352 case LINUX_SLL_P_802_3
:
354 * Ethernet_802.3 IPX frame.
359 case LINUX_SLL_P_802_2
:
370 ND_PRINT("ethertype Unknown (0x%04x)",
375 ND_PRINT("ethertype %s (0x%04x)",
376 tok2str(ethertype_values
, "Unknown", ether_type
),
379 ND_PRINT(", length %u: ", length
);
384 * This is the top level routine of the printer. 'p' points to the
385 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
386 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
387 * is the number of bytes actually captured.
390 sll2_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
, const u_char
*p
)
392 u_int caplen
= h
->caplen
;
393 u_int length
= h
->len
;
394 const struct sll2_header
*sllp
;
399 ndo
->ndo_protocol
= "sll2_if";
400 if (caplen
< SLL2_HDR_LEN
) {
402 * XXX - this "can't happen" because "pcap-linux.c" always
403 * adds this many bytes of header to every packet in a
404 * cooked socket capture.
410 sllp
= (const struct sll2_header
*)p
;
413 sll2_print(ndo
, sllp
, length
);
416 * Go past the cooked-mode header.
418 length
-= SLL2_HDR_LEN
;
419 caplen
-= SLL2_HDR_LEN
;
421 hdrlen
= SLL2_HDR_LEN
;
423 ether_type
= EXTRACT_BE_U_2(sllp
->sll2_protocol
);
427 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
430 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
) {
432 * Yes - what type is it?
434 switch (ether_type
) {
436 case LINUX_SLL_P_802_3
:
438 * Ethernet_802.3 IPX frame.
440 ipx_print(ndo
, p
, length
);
443 case LINUX_SLL_P_802_2
:
446 * Try to print the LLC-layer header & higher layers.
448 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, NULL
, NULL
);
450 goto unknown
; /* unknown LLC type */
451 hdrlen
+= llc_hdrlen
;
458 /* packet type not known, print raw packet */
459 if (!ndo
->ndo_suppress_default_print
)
460 ND_DEFAULTPRINT(p
, caplen
);
463 } else if (ether_type
== ETHERTYPE_8021Q
) {
465 * Print VLAN information, and then go back and process
466 * the enclosed type field.
470 return (hdrlen
+ caplen
);
474 return (hdrlen
+ length
);
476 if (ndo
->ndo_eflag
) {
477 uint16_t tag
= EXTRACT_BE_U_2(p
);
479 ND_PRINT("%s, ", ieee8021q_tci_string(tag
));
482 ether_type
= EXTRACT_BE_U_2(p
+ 2);
483 if (ether_type
<= MAX_ETHERNET_LENGTH_VAL
)
484 ether_type
= LINUX_SLL_P_802_2
;
485 if (!ndo
->ndo_qflag
) {
486 ND_PRINT("ethertype %s, ",
487 tok2str(ethertype_values
, "Unknown", ether_type
));
495 if (ethertype_print(ndo
, ether_type
, p
, length
, caplen
, NULL
, NULL
) == 0) {
496 /* ether_type not known, print raw packet */
498 sll2_print(ndo
, sllp
, length
+ SLL_HDR_LEN
);
499 if (!ndo
->ndo_suppress_default_print
)
500 ND_DEFAULTPRINT(p
, caplen
);