]> The Tcpdump Group git mirrors - tcpdump/blob - print-sll.c
Merge pull request #703 from nmap/update-gitignore
[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 #ifdef HAVE_NET_IF_H
29 #include <net/if.h>
30 #endif
31
32 #include "netdissect-stdinc.h"
33
34 #include "netdissect.h"
35 #include "addrtoname.h"
36 #include "ethertype.h"
37 #include "extract.h"
38
39 /*
40 * For captures on Linux cooked sockets, we construct a fake header
41 * that includes:
42 *
43 * a 2-byte "packet type" which is one of:
44 *
45 * LINUX_SLL_HOST packet was sent to us
46 * LINUX_SLL_BROADCAST packet was broadcast
47 * LINUX_SLL_MULTICAST packet was multicast
48 * LINUX_SLL_OTHERHOST packet was sent to somebody else
49 * LINUX_SLL_OUTGOING packet was sent *by* us;
50 *
51 * a 2-byte Ethernet protocol field;
52 *
53 * a 2-byte link-layer type;
54 *
55 * a 2-byte link-layer address length;
56 *
57 * an 8-byte source link-layer address, whose actual length is
58 * specified by the previous value.
59 *
60 * All fields except for the link-layer address are in network byte order.
61 *
62 * DO NOT change the layout of this structure, or change any of the
63 * LINUX_SLL_ values below. If you must change the link-layer header
64 * for a "cooked" Linux capture, introduce a new DLT_ type (ask
65 * "tcpdump-workers@lists.tcpdump.org" for one, so that you don't give it
66 * a value that collides with a value already being used), and use the
67 * new header in captures of that type, so that programs that can
68 * handle DLT_LINUX_SLL captures will continue to handle them correctly
69 * without any change, and so that capture files with different headers
70 * can be told apart and programs that read them can dissect the
71 * packets in them.
72 *
73 * This structure, and the #defines below, must be the same in the
74 * libpcap and tcpdump versions of "sll.h".
75 */
76
77 /*
78 * A DLT_LINUX_SLL fake link-layer header.
79 */
80 #define SLL_HDR_LEN 16 /* total header length */
81 #define SLL_ADDRLEN 8 /* length of address field */
82
83 struct sll_header {
84 nd_uint16_t sll_pkttype; /* packet type */
85 nd_uint16_t sll_hatype; /* link-layer address type */
86 nd_uint16_t sll_halen; /* link-layer address length */
87 nd_byte sll_addr[SLL_ADDRLEN]; /* link-layer address */
88 nd_uint16_t sll_protocol; /* protocol */
89 };
90
91 /*
92 * A DLT_LINUX_SLL2 fake link-layer header.
93 */
94 #define SLL2_HDR_LEN 20 /* total header length */
95
96 struct sll2_header {
97 nd_uint16_t sll2_protocol; /* protocol */
98 nd_uint16_t sll2_reserved_mbz; /* reserved - must be zero */
99 nd_uint32_t sll2_if_index; /* 1-based interface index */
100 nd_uint16_t sll2_hatype; /* link-layer address type */
101 nd_uint8_t sll2_pkttype; /* packet type */
102 nd_uint8_t sll2_halen; /* link-layer address length */
103 nd_byte sll2_addr[SLL_ADDRLEN]; /* link-layer address */
104 };
105
106 /*
107 * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
108 * PACKET_ values on Linux, but are defined here so that they're
109 * available even on systems other than Linux, and so that they
110 * don't change even if the PACKET_ values change.
111 */
112 #define LINUX_SLL_HOST 0
113 #define LINUX_SLL_BROADCAST 1
114 #define LINUX_SLL_MULTICAST 2
115 #define LINUX_SLL_OTHERHOST 3
116 #define LINUX_SLL_OUTGOING 4
117
118 /*
119 * The LINUX_SLL_ values for "sll_protocol"; these correspond to the
120 * ETH_P_ values on Linux, but are defined here so that they're
121 * available even on systems other than Linux. We assume, for now,
122 * that the ETH_P_ values won't change in Linux; if they do, then:
123 *
124 * if we don't translate them in "pcap-linux.c", capture files
125 * won't necessarily be readable if captured on a system that
126 * defines ETH_P_ values that don't match these values;
127 *
128 * if we do translate them in "pcap-linux.c", that makes life
129 * unpleasant for the BPF code generator, as the values you test
130 * for in the kernel aren't the values that you test for when
131 * reading a capture file, so the fixup code run on BPF programs
132 * handed to the kernel ends up having to do more work.
133 *
134 * Add other values here as necessary, for handling packet types that
135 * might show up on non-Ethernet, non-802.x networks. (Not all the ones
136 * in the Linux "if_ether.h" will, I suspect, actually show up in
137 * captures.)
138 */
139 #define LINUX_SLL_P_802_3 0x0001 /* Novell 802.3 frames without 802.2 LLC header */
140 #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */
141
142 static const struct tok sll_pkttype_values[] = {
143 { LINUX_SLL_HOST, "In" },
144 { LINUX_SLL_BROADCAST, "B" },
145 { LINUX_SLL_MULTICAST, "M" },
146 { LINUX_SLL_OTHERHOST, "P" },
147 { LINUX_SLL_OUTGOING, "Out" },
148 { 0, NULL}
149 };
150
151 static void
152 sll_print(netdissect_options *ndo, const struct sll_header *sllp, u_int length)
153 {
154 u_short ether_type;
155
156 ndo->ndo_protocol = "sll";
157 ND_PRINT("%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_BE_U_2(sllp->sll_pkttype)));
158
159 /*
160 * XXX - check the link-layer address type value?
161 * For now, we just assume 6 means Ethernet.
162 * XXX - print others as strings of hex?
163 */
164 if (EXTRACT_BE_U_2(sllp->sll_halen) == 6)
165 ND_PRINT("%s ", etheraddr_string(ndo, sllp->sll_addr));
166
167 if (!ndo->ndo_qflag) {
168 ether_type = EXTRACT_BE_U_2(sllp->sll_protocol);
169
170 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
171 /*
172 * Not an Ethernet type; what type is it?
173 */
174 switch (ether_type) {
175
176 case LINUX_SLL_P_802_3:
177 /*
178 * Ethernet_802.3 IPX frame.
179 */
180 ND_PRINT("802.3");
181 break;
182
183 case LINUX_SLL_P_802_2:
184 /*
185 * 802.2.
186 */
187 ND_PRINT("802.2");
188 break;
189
190 default:
191 /*
192 * What is it?
193 */
194 ND_PRINT("ethertype Unknown (0x%04x)",
195 ether_type);
196 break;
197 }
198 } else {
199 ND_PRINT("ethertype %s (0x%04x)",
200 tok2str(ethertype_values, "Unknown", ether_type),
201 ether_type);
202 }
203 ND_PRINT(", length %u: ", length);
204 }
205 }
206
207 /*
208 * This is the top level routine of the printer. 'p' points to the
209 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
210 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
211 * is the number of bytes actually captured.
212 */
213 u_int
214 sll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
215 {
216 u_int caplen = h->caplen;
217 u_int length = h->len;
218 const struct sll_header *sllp;
219 u_short hatype;
220 u_short ether_type;
221 int llc_hdrlen;
222 u_int hdrlen;
223
224 ndo->ndo_protocol = "sll_if";
225 if (caplen < SLL_HDR_LEN) {
226 /*
227 * XXX - this "can't happen" because "pcap-linux.c" always
228 * adds this many bytes of header to every packet in a
229 * cooked socket capture.
230 */
231 nd_print_trunc(ndo);
232 return (caplen);
233 }
234
235 sllp = (const struct sll_header *)p;
236
237 if (ndo->ndo_eflag)
238 sll_print(ndo, sllp, length);
239
240 /*
241 * Go past the cooked-mode header.
242 */
243 length -= SLL_HDR_LEN;
244 caplen -= SLL_HDR_LEN;
245 p += SLL_HDR_LEN;
246 hdrlen = SLL_HDR_LEN;
247
248 hatype = EXTRACT_BE_U_2(sllp->sll_hatype);
249 switch (hatype) {
250
251 case 803:
252 /*
253 * This is an packet with a radiotap header;
254 * just dissect the payload as such.
255 */
256 return (SLL_HDR_LEN + ieee802_11_radio_print(ndo, p, length, caplen));
257 }
258 ether_type = EXTRACT_BE_U_2(sllp->sll_protocol);
259
260 recurse:
261 /*
262 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
263 * packet type?
264 */
265 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
266 /*
267 * Yes - what type is it?
268 */
269 switch (ether_type) {
270
271 case LINUX_SLL_P_802_3:
272 /*
273 * Ethernet_802.3 IPX frame.
274 */
275 ipx_print(ndo, p, length);
276 break;
277
278 case LINUX_SLL_P_802_2:
279 /*
280 * 802.2.
281 * Try to print the LLC-layer header & higher layers.
282 */
283 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
284 if (llc_hdrlen < 0)
285 goto unknown; /* unknown LLC type */
286 hdrlen += llc_hdrlen;
287 break;
288
289 default:
290 /*FALLTHROUGH*/
291
292 unknown:
293 /* packet type not known, print raw packet */
294 if (!ndo->ndo_suppress_default_print)
295 ND_DEFAULTPRINT(p, caplen);
296 break;
297 }
298 } else if (ether_type == ETHERTYPE_8021Q) {
299 /*
300 * Print VLAN information, and then go back and process
301 * the enclosed type field.
302 */
303 if (caplen < 4) {
304 ndo->ndo_protocol = "vlan";
305 nd_print_trunc(ndo);
306 return (hdrlen + caplen);
307 }
308 if (ndo->ndo_eflag) {
309 uint16_t tag = EXTRACT_BE_U_2(p);
310
311 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
312 }
313
314 ether_type = EXTRACT_BE_U_2(p + 2);
315 if (ether_type <= MAX_ETHERNET_LENGTH_VAL)
316 ether_type = LINUX_SLL_P_802_2;
317 if (!ndo->ndo_qflag) {
318 ND_PRINT("ethertype %s, ",
319 tok2str(ethertype_values, "Unknown", ether_type));
320 }
321 p += 4;
322 length -= 4;
323 caplen -= 4;
324 hdrlen += 4;
325 goto recurse;
326 } else {
327 if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
328 /* ether_type not known, print raw packet */
329 if (!ndo->ndo_eflag)
330 sll_print(ndo, sllp, length + SLL_HDR_LEN);
331 if (!ndo->ndo_suppress_default_print)
332 ND_DEFAULTPRINT(p, caplen);
333 }
334 }
335
336 return (hdrlen);
337 }
338
339 static void
340 sll2_print(netdissect_options *ndo, const struct sll2_header *sllp, u_int length)
341 {
342 u_short ether_type;
343
344 ndo->ndo_protocol = "sll2";
345 ND_PRINT("%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_U_1(sllp->sll2_pkttype)));
346
347 /*
348 * XXX - check the link-layer address type value?
349 * For now, we just assume 6 means Ethernet.
350 * XXX - print others as strings of hex?
351 */
352 if (EXTRACT_U_1(sllp->sll2_halen) == 6)
353 ND_PRINT("%s ", etheraddr_string(ndo, sllp->sll2_addr));
354
355 if (!ndo->ndo_qflag) {
356 ether_type = EXTRACT_BE_U_2(sllp->sll2_protocol);
357
358 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
359 /*
360 * Not an Ethernet type; what type is it?
361 */
362 switch (ether_type) {
363
364 case LINUX_SLL_P_802_3:
365 /*
366 * Ethernet_802.3 IPX frame.
367 */
368 ND_PRINT("802.3");
369 break;
370
371 case LINUX_SLL_P_802_2:
372 /*
373 * 802.2.
374 */
375 ND_PRINT("802.2");
376 break;
377
378 default:
379 /*
380 * What is it?
381 */
382 ND_PRINT("ethertype Unknown (0x%04x)",
383 ether_type);
384 break;
385 }
386 } else {
387 ND_PRINT("ethertype %s (0x%04x)",
388 tok2str(ethertype_values, "Unknown", ether_type),
389 ether_type);
390 }
391 ND_PRINT(", length %u: ", length);
392 }
393 }
394
395 /*
396 * This is the top level routine of the printer. 'p' points to the
397 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
398 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
399 * is the number of bytes actually captured.
400 */
401 u_int
402 sll2_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
403 {
404 u_int caplen = h->caplen;
405 u_int length = h->len;
406 const struct sll2_header *sllp;
407 u_short hatype;
408 u_short ether_type;
409 int llc_hdrlen;
410 u_int hdrlen;
411 #ifdef HAVE_NET_IF_H
412 uint32_t if_index;
413 char ifname[IF_NAMESIZE];
414 #endif
415
416 ndo->ndo_protocol = "sll2_if";
417 if (caplen < SLL2_HDR_LEN) {
418 /*
419 * XXX - this "can't happen" because "pcap-linux.c" always
420 * adds this many bytes of header to every packet in a
421 * cooked socket capture.
422 */
423 nd_print_trunc(ndo);
424 return (caplen);
425 }
426
427 sllp = (const struct sll2_header *)p;
428 #ifdef HAVE_NET_IF_H
429 if_index = EXTRACT_BE_U_4(sllp->sll2_if_index);
430 if (if_indextoname(if_index, ifname))
431 ND_PRINT("ifindex %u (%s) ", if_index, ifname);
432 else
433 ND_PRINT("ifindex %u ", if_index);
434 #endif
435
436 if (ndo->ndo_eflag)
437 sll2_print(ndo, sllp, length);
438
439 /*
440 * Go past the cooked-mode header.
441 */
442 length -= SLL2_HDR_LEN;
443 caplen -= SLL2_HDR_LEN;
444 p += SLL2_HDR_LEN;
445 hdrlen = SLL2_HDR_LEN;
446
447 hatype = EXTRACT_BE_U_2(sllp->sll2_hatype);
448 switch (hatype) {
449
450 case 803:
451 /*
452 * This is an packet with a radiotap header;
453 * just dissect the payload as such.
454 */
455 return (SLL_HDR_LEN + ieee802_11_radio_print(ndo, p, length, caplen));
456 }
457 ether_type = EXTRACT_BE_U_2(sllp->sll2_protocol);
458
459 recurse:
460 /*
461 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
462 * packet type?
463 */
464 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
465 /*
466 * Yes - what type is it?
467 */
468 switch (ether_type) {
469
470 case LINUX_SLL_P_802_3:
471 /*
472 * Ethernet_802.3 IPX frame.
473 */
474 ipx_print(ndo, p, length);
475 break;
476
477 case LINUX_SLL_P_802_2:
478 /*
479 * 802.2.
480 * Try to print the LLC-layer header & higher layers.
481 */
482 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
483 if (llc_hdrlen < 0)
484 goto unknown; /* unknown LLC type */
485 hdrlen += llc_hdrlen;
486 break;
487
488 default:
489 /*FALLTHROUGH*/
490
491 unknown:
492 /* packet type not known, print raw packet */
493 if (!ndo->ndo_suppress_default_print)
494 ND_DEFAULTPRINT(p, caplen);
495 break;
496 }
497 } else if (ether_type == ETHERTYPE_8021Q) {
498 /*
499 * Print VLAN information, and then go back and process
500 * the enclosed type field.
501 */
502 if (caplen < 4) {
503 ndo->ndo_protocol = "vlan";
504 nd_print_trunc(ndo);
505 return (hdrlen + caplen);
506 }
507 if (ndo->ndo_eflag) {
508 uint16_t tag = EXTRACT_BE_U_2(p);
509
510 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
511 }
512
513 ether_type = EXTRACT_BE_U_2(p + 2);
514 if (ether_type <= MAX_ETHERNET_LENGTH_VAL)
515 ether_type = LINUX_SLL_P_802_2;
516 if (!ndo->ndo_qflag) {
517 ND_PRINT("ethertype %s, ",
518 tok2str(ethertype_values, "Unknown", ether_type));
519 }
520 p += 4;
521 length -= 4;
522 caplen -= 4;
523 hdrlen += 4;
524 goto recurse;
525 } else {
526 if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
527 /* ether_type not known, print raw packet */
528 if (!ndo->ndo_eflag)
529 sll2_print(ndo, sllp, length + SLL_HDR_LEN);
530 if (!ndo->ndo_suppress_default_print)
531 ND_DEFAULTPRINT(p, caplen);
532 }
533 }
534
535 return (hdrlen);
536 }