]> The Tcpdump Group git mirrors - tcpdump/blob - print-sll.c
DLT_LINUX_SLL2 support.
[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 /* \summary: Linux cooked sockets capture printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include "netdissect.h"
31 #include "addrtoname.h"
32 #include "ethertype.h"
33 #include "extract.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 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 */
85 };
86
87 /*
88 * A DLT_LINUX_SLL2 fake link-layer header.
89 */
90 #define SLL2_HDR_LEN 20 /* total header length */
91
92 struct sll2_header {
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 */
100 };
101
102 /*
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.
107 */
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
113
114 /*
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:
119 *
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;
123 *
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.
129 *
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
133 * captures.)
134 */
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) */
137
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" },
144 { 0, NULL}
145 };
146
147 static void
148 sll_print(netdissect_options *ndo, const struct sll_header *sllp, u_int length)
149 {
150 u_short ether_type;
151
152 ndo->ndo_protocol = "sll";
153 ND_PRINT("%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_BE_U_2(sllp->sll_pkttype)));
154
155 /*
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?
159 */
160 if (EXTRACT_BE_U_2(sllp->sll_halen) == 6)
161 ND_PRINT("%s ", etheraddr_string(ndo, sllp->sll_addr));
162
163 if (!ndo->ndo_qflag) {
164 ether_type = EXTRACT_BE_U_2(sllp->sll_protocol);
165
166 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
167 /*
168 * Not an Ethernet type; what type is it?
169 */
170 switch (ether_type) {
171
172 case LINUX_SLL_P_802_3:
173 /*
174 * Ethernet_802.3 IPX frame.
175 */
176 ND_PRINT("802.3");
177 break;
178
179 case LINUX_SLL_P_802_2:
180 /*
181 * 802.2.
182 */
183 ND_PRINT("802.2");
184 break;
185
186 default:
187 /*
188 * What is it?
189 */
190 ND_PRINT("ethertype Unknown (0x%04x)",
191 ether_type);
192 break;
193 }
194 } else {
195 ND_PRINT("ethertype %s (0x%04x)",
196 tok2str(ethertype_values, "Unknown", ether_type),
197 ether_type);
198 }
199 ND_PRINT(", length %u: ", length);
200 }
201 }
202
203 /*
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.
208 */
209 u_int
210 sll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
211 {
212 u_int caplen = h->caplen;
213 u_int length = h->len;
214 const struct sll_header *sllp;
215 u_short ether_type;
216 int llc_hdrlen;
217 u_int hdrlen;
218
219 ndo->ndo_protocol = "sll_if";
220 if (caplen < SLL_HDR_LEN) {
221 /*
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.
225 */
226 nd_print_trunc(ndo);
227 return (caplen);
228 }
229
230 sllp = (const struct sll_header *)p;
231
232 if (ndo->ndo_eflag)
233 sll_print(ndo, sllp, length);
234
235 /*
236 * Go past the cooked-mode header.
237 */
238 length -= SLL_HDR_LEN;
239 caplen -= SLL_HDR_LEN;
240 p += SLL_HDR_LEN;
241 hdrlen = SLL_HDR_LEN;
242
243 ether_type = EXTRACT_BE_U_2(sllp->sll_protocol);
244
245 recurse:
246 /*
247 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
248 * packet type?
249 */
250 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
251 /*
252 * Yes - what type is it?
253 */
254 switch (ether_type) {
255
256 case LINUX_SLL_P_802_3:
257 /*
258 * Ethernet_802.3 IPX frame.
259 */
260 ipx_print(ndo, p, length);
261 break;
262
263 case LINUX_SLL_P_802_2:
264 /*
265 * 802.2.
266 * Try to print the LLC-layer header & higher layers.
267 */
268 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
269 if (llc_hdrlen < 0)
270 goto unknown; /* unknown LLC type */
271 hdrlen += llc_hdrlen;
272 break;
273
274 default:
275 /*FALLTHROUGH*/
276
277 unknown:
278 /* packet type not known, print raw packet */
279 if (!ndo->ndo_suppress_default_print)
280 ND_DEFAULTPRINT(p, caplen);
281 break;
282 }
283 } else if (ether_type == ETHERTYPE_8021Q) {
284 /*
285 * Print VLAN information, and then go back and process
286 * the enclosed type field.
287 */
288 if (caplen < 4) {
289 ND_PRINT("[|vlan]");
290 return (hdrlen + caplen);
291 }
292 if (length < 4) {
293 ND_PRINT("[|vlan]");
294 return (hdrlen + length);
295 }
296 if (ndo->ndo_eflag) {
297 uint16_t tag = EXTRACT_BE_U_2(p);
298
299 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
300 }
301
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));
308 }
309 p += 4;
310 length -= 4;
311 caplen -= 4;
312 hdrlen += 4;
313 goto recurse;
314 } else {
315 if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
316 /* ether_type not known, print raw packet */
317 if (!ndo->ndo_eflag)
318 sll_print(ndo, sllp, length + SLL_HDR_LEN);
319 if (!ndo->ndo_suppress_default_print)
320 ND_DEFAULTPRINT(p, caplen);
321 }
322 }
323
324 return (hdrlen);
325 }
326
327 static void
328 sll2_print(netdissect_options *ndo, const struct sll2_header *sllp, u_int length)
329 {
330 u_short ether_type;
331
332 ndo->ndo_protocol = "sll2";
333 ND_PRINT("%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_BE_U_2(sllp->sll2_pkttype)));
334
335 /*
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?
339 */
340 if (EXTRACT_U_1(sllp->sll2_halen) == 6)
341 ND_PRINT("%s ", etheraddr_string(ndo, sllp->sll2_addr));
342
343 if (!ndo->ndo_qflag) {
344 ether_type = EXTRACT_BE_U_2(sllp->sll2_protocol);
345
346 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
347 /*
348 * Not an Ethernet type; what type is it?
349 */
350 switch (ether_type) {
351
352 case LINUX_SLL_P_802_3:
353 /*
354 * Ethernet_802.3 IPX frame.
355 */
356 ND_PRINT("802.3");
357 break;
358
359 case LINUX_SLL_P_802_2:
360 /*
361 * 802.2.
362 */
363 ND_PRINT("802.2");
364 break;
365
366 default:
367 /*
368 * What is it?
369 */
370 ND_PRINT("ethertype Unknown (0x%04x)",
371 ether_type);
372 break;
373 }
374 } else {
375 ND_PRINT("ethertype %s (0x%04x)",
376 tok2str(ethertype_values, "Unknown", ether_type),
377 ether_type);
378 }
379 ND_PRINT(", length %u: ", length);
380 }
381 }
382
383 /*
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.
388 */
389 u_int
390 sll2_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
391 {
392 u_int caplen = h->caplen;
393 u_int length = h->len;
394 const struct sll2_header *sllp;
395 u_short ether_type;
396 int llc_hdrlen;
397 u_int hdrlen;
398
399 ndo->ndo_protocol = "sll2_if";
400 if (caplen < SLL2_HDR_LEN) {
401 /*
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.
405 */
406 nd_print_trunc(ndo);
407 return (caplen);
408 }
409
410 sllp = (const struct sll2_header *)p;
411
412 if (ndo->ndo_eflag)
413 sll2_print(ndo, sllp, length);
414
415 /*
416 * Go past the cooked-mode header.
417 */
418 length -= SLL2_HDR_LEN;
419 caplen -= SLL2_HDR_LEN;
420 p += SLL2_HDR_LEN;
421 hdrlen = SLL2_HDR_LEN;
422
423 ether_type = EXTRACT_BE_U_2(sllp->sll2_protocol);
424
425 recurse:
426 /*
427 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
428 * packet type?
429 */
430 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
431 /*
432 * Yes - what type is it?
433 */
434 switch (ether_type) {
435
436 case LINUX_SLL_P_802_3:
437 /*
438 * Ethernet_802.3 IPX frame.
439 */
440 ipx_print(ndo, p, length);
441 break;
442
443 case LINUX_SLL_P_802_2:
444 /*
445 * 802.2.
446 * Try to print the LLC-layer header & higher layers.
447 */
448 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
449 if (llc_hdrlen < 0)
450 goto unknown; /* unknown LLC type */
451 hdrlen += llc_hdrlen;
452 break;
453
454 default:
455 /*FALLTHROUGH*/
456
457 unknown:
458 /* packet type not known, print raw packet */
459 if (!ndo->ndo_suppress_default_print)
460 ND_DEFAULTPRINT(p, caplen);
461 break;
462 }
463 } else if (ether_type == ETHERTYPE_8021Q) {
464 /*
465 * Print VLAN information, and then go back and process
466 * the enclosed type field.
467 */
468 if (caplen < 4) {
469 ND_PRINT("[|vlan]");
470 return (hdrlen + caplen);
471 }
472 if (length < 4) {
473 ND_PRINT("[|vlan]");
474 return (hdrlen + length);
475 }
476 if (ndo->ndo_eflag) {
477 uint16_t tag = EXTRACT_BE_U_2(p);
478
479 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
480 }
481
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));
488 }
489 p += 4;
490 length -= 4;
491 caplen -= 4;
492 hdrlen += 4;
493 goto recurse;
494 } else {
495 if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
496 /* ether_type not known, print raw packet */
497 if (!ndo->ndo_eflag)
498 sll2_print(ndo, sllp, length + SLL_HDR_LEN);
499 if (!ndo->ndo_suppress_default_print)
500 ND_DEFAULTPRINT(p, caplen);
501 }
502 }
503
504 return (hdrlen);
505 }