]> The Tcpdump Group git mirrors - tcpdump/blob - print-sll.c
Merge branch 'master' of git+ssh://bpf.tcpdump.org/tcpdump/master/git/tcpdump
[tcpdump] / print-sll.c
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.
4 *
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.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include "interface.h"
29 #include "addrtoname.h"
30 #include "ethertype.h"
31 #include "extract.h"
32
33 #include "ether.h"
34
35 /*
36 * For captures on Linux cooked sockets, we construct a fake header
37 * that includes:
38 *
39 * a 2-byte "packet type" which is one of:
40 *
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;
46 *
47 * a 2-byte Ethernet protocol field;
48 *
49 * a 2-byte link-layer type;
50 *
51 * a 2-byte link-layer address length;
52 *
53 * an 8-byte source link-layer address, whose actual length is
54 * specified by the previous value.
55 *
56 * All fields except for the link-layer address are in network byte order.
57 *
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
67 * packets in them.
68 *
69 * This structure, and the #defines below, must be the same in the
70 * libpcap and tcpdump versions of "sll.h".
71 */
72
73 /*
74 * A DLT_LINUX_SLL fake link-layer header.
75 */
76 #define SLL_HDR_LEN 16 /* total header length */
77 #define SLL_ADDRLEN 8 /* length of address field */
78
79 struct sll_header {
80 uint16_t sll_pkttype; /* packet type */
81 uint16_t sll_hatype; /* link-layer address type */
82 uint16_t sll_halen; /* link-layer address length */
83 uint8_t sll_addr[SLL_ADDRLEN]; /* link-layer address */
84 uint16_t sll_protocol; /* protocol */
85 };
86
87 /*
88 * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
89 * PACKET_ values on Linux, but are defined here so that they're
90 * available even on systems other than Linux, and so that they
91 * don't change even if the PACKET_ values change.
92 */
93 #define LINUX_SLL_HOST 0
94 #define LINUX_SLL_BROADCAST 1
95 #define LINUX_SLL_MULTICAST 2
96 #define LINUX_SLL_OTHERHOST 3
97 #define LINUX_SLL_OUTGOING 4
98
99 /*
100 * The LINUX_SLL_ values for "sll_protocol"; these correspond to the
101 * ETH_P_ values on Linux, but are defined here so that they're
102 * available even on systems other than Linux. We assume, for now,
103 * that the ETH_P_ values won't change in Linux; if they do, then:
104 *
105 * if we don't translate them in "pcap-linux.c", capture files
106 * won't necessarily be readable if captured on a system that
107 * defines ETH_P_ values that don't match these values;
108 *
109 * if we do translate them in "pcap-linux.c", that makes life
110 * unpleasant for the BPF code generator, as the values you test
111 * for in the kernel aren't the values that you test for when
112 * reading a capture file, so the fixup code run on BPF programs
113 * handed to the kernel ends up having to do more work.
114 *
115 * Add other values here as necessary, for handling packet types that
116 * might show up on non-Ethernet, non-802.x networks. (Not all the ones
117 * in the Linux "if_ether.h" will, I suspect, actually show up in
118 * captures.)
119 */
120 #define LINUX_SLL_P_802_3 0x0001 /* Novell 802.3 frames without 802.2 LLC header */
121 #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */
122
123 static const struct tok sll_pkttype_values[] = {
124 { LINUX_SLL_HOST, "In" },
125 { LINUX_SLL_BROADCAST, "B" },
126 { LINUX_SLL_MULTICAST, "M" },
127 { LINUX_SLL_OTHERHOST, "P" },
128 { LINUX_SLL_OUTGOING, "Out" },
129 { 0, NULL}
130 };
131
132 static inline void
133 sll_print(netdissect_options *ndo, register const struct sll_header *sllp, u_int length)
134 {
135 u_short ether_type;
136
137 ND_PRINT((ndo, "%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_16BITS(&sllp->sll_pkttype))));
138
139 /*
140 * XXX - check the link-layer address type value?
141 * For now, we just assume 6 means Ethernet.
142 * XXX - print others as strings of hex?
143 */
144 if (EXTRACT_16BITS(&sllp->sll_halen) == 6)
145 ND_PRINT((ndo, "%s ", etheraddr_string(ndo, sllp->sll_addr)));
146
147 if (!ndo->ndo_qflag) {
148 ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
149
150 if (ether_type <= ETHERMTU) {
151 /*
152 * Not an Ethernet type; what type is it?
153 */
154 switch (ether_type) {
155
156 case LINUX_SLL_P_802_3:
157 /*
158 * Ethernet_802.3 IPX frame.
159 */
160 ND_PRINT((ndo, "802.3"));
161 break;
162
163 case LINUX_SLL_P_802_2:
164 /*
165 * 802.2.
166 */
167 ND_PRINT((ndo, "802.2"));
168 break;
169
170 default:
171 /*
172 * What is it?
173 */
174 ND_PRINT((ndo, "ethertype Unknown (0x%04x)",
175 ether_type));
176 break;
177 }
178 } else {
179 ND_PRINT((ndo, "ethertype %s (0x%04x)",
180 tok2str(ethertype_values, "Unknown", ether_type),
181 ether_type));
182 }
183 ND_PRINT((ndo, ", length %u: ", length));
184 }
185 }
186
187 /*
188 * This is the top level routine of the printer. 'p' points to the
189 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
190 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
191 * is the number of bytes actually captured.
192 */
193 u_int
194 sll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
195 {
196 u_int caplen = h->caplen;
197 u_int length = h->len;
198 register const struct sll_header *sllp;
199 u_short ether_type;
200 int llc_hdrlen;
201 u_int hdrlen;
202
203 if (caplen < SLL_HDR_LEN) {
204 /*
205 * XXX - this "can't happen" because "pcap-linux.c" always
206 * adds this many bytes of header to every packet in a
207 * cooked socket capture.
208 */
209 ND_PRINT((ndo, "[|sll]"));
210 return (caplen);
211 }
212
213 sllp = (const struct sll_header *)p;
214
215 if (ndo->ndo_eflag)
216 sll_print(ndo, sllp, length);
217
218 /*
219 * Go past the cooked-mode header.
220 */
221 length -= SLL_HDR_LEN;
222 caplen -= SLL_HDR_LEN;
223 p += SLL_HDR_LEN;
224 hdrlen = SLL_HDR_LEN;
225
226 ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
227
228 recurse:
229 /*
230 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
231 * packet type?
232 */
233 if (ether_type <= ETHERMTU) {
234 /*
235 * Yes - what type is it?
236 */
237 switch (ether_type) {
238
239 case LINUX_SLL_P_802_3:
240 /*
241 * Ethernet_802.3 IPX frame.
242 */
243 ipx_print(ndo, p, length);
244 break;
245
246 case LINUX_SLL_P_802_2:
247 /*
248 * 802.2.
249 * Try to print the LLC-layer header & higher layers.
250 */
251 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
252 if (llc_hdrlen < 0)
253 goto unknown; /* unknown LLC type */
254 hdrlen += llc_hdrlen;
255 break;
256
257 default:
258 /*FALLTHROUGH*/
259
260 unknown:
261 /* packet type not known, print raw packet */
262 if (!ndo->ndo_suppress_default_print)
263 ND_DEFAULTPRINT(p, caplen);
264 break;
265 }
266 } else if (ether_type == ETHERTYPE_8021Q) {
267 /*
268 * Print VLAN information, and then go back and process
269 * the enclosed type field.
270 */
271 if (caplen < 4) {
272 ND_PRINT((ndo, "[|vlan]"));
273 return (hdrlen + caplen);
274 }
275 if (length < 4) {
276 ND_PRINT((ndo, "[|vlan]"));
277 return (hdrlen + length);
278 }
279 if (ndo->ndo_eflag) {
280 uint16_t tag = EXTRACT_16BITS(p);
281
282 ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
283 }
284
285 ether_type = EXTRACT_16BITS(p + 2);
286 if (ether_type <= ETHERMTU)
287 ether_type = LINUX_SLL_P_802_2;
288 if (!ndo->ndo_qflag) {
289 ND_PRINT((ndo, "ethertype %s, ",
290 tok2str(ethertype_values, "Unknown", ether_type)));
291 }
292 p += 4;
293 length -= 4;
294 caplen -= 4;
295 hdrlen += 4;
296 goto recurse;
297 } else {
298 if (ethertype_print(ndo, ether_type, p, length, caplen) == 0) {
299 /* ether_type not known, print raw packet */
300 if (!ndo->ndo_eflag)
301 sll_print(ndo, sllp, length + SLL_HDR_LEN);
302 if (!ndo->ndo_suppress_default_print)
303 ND_DEFAULTPRINT(p, caplen);
304 }
305 }
306
307 return (hdrlen);
308 }