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 static const char rcsid
[] _U_
=
23 "@(#) $Header: /tcpdump/master/tcpdump/print-sll.c,v 1.19 2005-11-13 12:12:43 guy Exp $ (LBL)";
30 #include <tcpdump-stdinc.h>
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "ethertype.h"
44 * For captures on Linux cooked sockets, we construct a fake header
47 * a 2-byte "packet type" which is one of:
49 * LINUX_SLL_HOST packet was sent to us
50 * LINUX_SLL_BROADCAST packet was broadcast
51 * LINUX_SLL_MULTICAST packet was multicast
52 * LINUX_SLL_OTHERHOST packet was sent to somebody else
53 * LINUX_SLL_OUTGOING packet was sent *by* us;
55 * a 2-byte Ethernet protocol field;
57 * a 2-byte link-layer type;
59 * a 2-byte link-layer address length;
61 * an 8-byte source link-layer address, whose actual length is
62 * specified by the previous value.
64 * All fields except for the link-layer address are in network byte order.
66 * DO NOT change the layout of this structure, or change any of the
67 * LINUX_SLL_ values below. If you must change the link-layer header
68 * for a "cooked" Linux capture, introduce a new DLT_ type (ask
69 * "tcpdump-workers@lists.tcpdump.org" for one, so that you don't give it
70 * a value that collides with a value already being used), and use the
71 * new header in captures of that type, so that programs that can
72 * handle DLT_LINUX_SLL captures will continue to handle them correctly
73 * without any change, and so that capture files with different headers
74 * can be told apart and programs that read them can dissect the
77 * This structure, and the #defines below, must be the same in the
78 * libpcap and tcpdump versions of "sll.h".
82 * A DLT_LINUX_SLL fake link-layer header.
84 #define SLL_HDR_LEN 16 /* total header length */
85 #define SLL_ADDRLEN 8 /* length of address field */
88 u_int16_t sll_pkttype
; /* packet type */
89 u_int16_t sll_hatype
; /* link-layer address type */
90 u_int16_t sll_halen
; /* link-layer address length */
91 u_int8_t sll_addr
[SLL_ADDRLEN
]; /* link-layer address */
92 u_int16_t sll_protocol
; /* protocol */
96 * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
97 * PACKET_ values on Linux, but are defined here so that they're
98 * available even on systems other than Linux, and so that they
99 * don't change even if the PACKET_ values change.
101 #define LINUX_SLL_HOST 0
102 #define LINUX_SLL_BROADCAST 1
103 #define LINUX_SLL_MULTICAST 2
104 #define LINUX_SLL_OTHERHOST 3
105 #define LINUX_SLL_OUTGOING 4
108 * The LINUX_SLL_ values for "sll_protocol"; these correspond to the
109 * ETH_P_ values on Linux, but are defined here so that they're
110 * available even on systems other than Linux. We assume, for now,
111 * that the ETH_P_ values won't change in Linux; if they do, then:
113 * if we don't translate them in "pcap-linux.c", capture files
114 * won't necessarily be readable if captured on a system that
115 * defines ETH_P_ values that don't match these values;
117 * if we do translate them in "pcap-linux.c", that makes life
118 * unpleasant for the BPF code generator, as the values you test
119 * for in the kernel aren't the values that you test for when
120 * reading a capture file, so the fixup code run on BPF programs
121 * handed to the kernel ends up having to do more work.
123 * Add other values here as necessary, for handling packet types that
124 * might show up on non-Ethernet, non-802.x networks. (Not all the ones
125 * in the Linux "if_ether.h" will, I suspect, actually show up in
128 #define LINUX_SLL_P_802_3 0x0001 /* Novell 802.3 frames without 802.2 LLC header */
129 #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */
131 static const struct tok sll_pkttype_values
[] = {
132 { LINUX_SLL_HOST
, "In" },
133 { LINUX_SLL_BROADCAST
, "B" },
134 { LINUX_SLL_MULTICAST
, "M" },
135 { LINUX_SLL_OTHERHOST
, "P" },
136 { LINUX_SLL_OUTGOING
, "Out" },
141 sll_print(register const struct sll_header
*sllp
, u_int length
)
145 printf("%3s ",tok2str(sll_pkttype_values
,"?",EXTRACT_16BITS(&sllp
->sll_pkttype
)));
148 * XXX - check the link-layer address type value?
149 * For now, we just assume 6 means Ethernet.
150 * XXX - print others as strings of hex?
152 if (EXTRACT_16BITS(&sllp
->sll_halen
) == 6)
153 (void)printf("%s ", etheraddr_string(sllp
->sll_addr
));
156 ether_type
= EXTRACT_16BITS(&sllp
->sll_protocol
);
158 if (ether_type
<= ETHERMTU
) {
160 * Not an Ethernet type; what type is it?
162 switch (ether_type
) {
164 case LINUX_SLL_P_802_3
:
166 * Ethernet_802.3 IPX frame.
168 (void)printf("802.3");
171 case LINUX_SLL_P_802_2
:
175 (void)printf("802.2");
182 (void)printf("ethertype Unknown (0x%04x)",
187 (void)printf("ethertype %s (0x%04x)",
188 tok2str(ethertype_values
, "Unknown", ether_type
),
191 (void)printf(", length %u: ", length
);
196 * This is the top level routine of the printer. 'p' points to the
197 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
198 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
199 * is the number of bytes actually captured.
202 sll_if_print(const struct pcap_pkthdr
*h
, const u_char
*p
)
204 u_int caplen
= h
->caplen
;
205 u_int length
= h
->len
;
206 register const struct sll_header
*sllp
;
208 u_short extracted_ethertype
;
210 if (caplen
< SLL_HDR_LEN
) {
212 * XXX - this "can't happen" because "pcap-linux.c" always
213 * adds this many bytes of header to every packet in a
214 * cooked socket capture.
220 sllp
= (const struct sll_header
*)p
;
223 sll_print(sllp
, length
);
226 * Go past the cooked-mode header.
228 length
-= SLL_HDR_LEN
;
229 caplen
-= SLL_HDR_LEN
;
232 ether_type
= EXTRACT_16BITS(&sllp
->sll_protocol
);
236 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
239 if (ether_type
<= ETHERMTU
) {
241 * Yes - what type is it?
243 switch (ether_type
) {
245 case LINUX_SLL_P_802_3
:
247 * Ethernet_802.3 IPX frame.
249 ipx_print(p
, length
);
252 case LINUX_SLL_P_802_2
:
255 * Try to print the LLC-layer header & higher layers.
257 if (llc_print(p
, length
, caplen
, NULL
, NULL
,
258 &extracted_ethertype
) == 0)
259 goto unknown
; /* unknown LLC type */
263 extracted_ethertype
= 0;
267 /* ether_type not known, print raw packet */
269 sll_print(sllp
, length
+ SLL_HDR_LEN
);
270 if (extracted_ethertype
) {
272 etherproto_string(htons(extracted_ethertype
)));
274 if (!suppress_default_print
)
275 default_print(p
, caplen
);
278 } else if (ether_type
== ETHERTYPE_8021Q
) {
280 * Print VLAN information, and then go back and process
281 * the enclosed type field.
283 if (caplen
< 4 || length
< 4) {
285 return (SLL_HDR_LEN
);
288 u_int16_t tag
= EXTRACT_16BITS(p
);
290 printf("vlan %u, p %u%s, ",
293 (tag
& 0x1000) ? ", CFI" : "");
296 ether_type
= EXTRACT_16BITS(p
+ 2);
297 if (ether_type
<= ETHERMTU
)
298 ether_type
= LINUX_SLL_P_802_2
;
300 (void)printf("ethertype %s, ",
301 tok2str(ethertype_values
, "Unknown", ether_type
));
308 if (ethertype_print(gndo
, ether_type
, p
, length
, caplen
) == 0) {
309 /* ether_type not known, print raw packet */
311 sll_print(sllp
, length
+ SLL_HDR_LEN
);
312 if (!suppress_default_print
)
313 default_print(p
, caplen
);
317 return (SLL_HDR_LEN
);