]> The Tcpdump Group git mirrors - tcpdump/blob - print-sll.c
Use the new GET_ macros instead of the EXTRACT_ ones
[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 ",
158 tok2str(sll_pkttype_values,"?",GET_BE_U_2(sllp->sll_pkttype)));
159
160 /*
161 * XXX - check the link-layer address type value?
162 * For now, we just assume 6 means Ethernet.
163 * XXX - print others as strings of hex?
164 */
165 if (GET_BE_U_2(sllp->sll_halen) == 6)
166 ND_PRINT("%s ", etheraddr_string(ndo, sllp->sll_addr));
167
168 if (!ndo->ndo_qflag) {
169 ether_type = GET_BE_U_2(sllp->sll_protocol);
170
171 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
172 /*
173 * Not an Ethernet type; what type is it?
174 */
175 switch (ether_type) {
176
177 case LINUX_SLL_P_802_3:
178 /*
179 * Ethernet_802.3 IPX frame.
180 */
181 ND_PRINT("802.3");
182 break;
183
184 case LINUX_SLL_P_802_2:
185 /*
186 * 802.2.
187 */
188 ND_PRINT("802.2");
189 break;
190
191 default:
192 /*
193 * What is it?
194 */
195 ND_PRINT("ethertype Unknown (0x%04x)",
196 ether_type);
197 break;
198 }
199 } else {
200 ND_PRINT("ethertype %s (0x%04x)",
201 tok2str(ethertype_values, "Unknown", ether_type),
202 ether_type);
203 }
204 ND_PRINT(", length %u: ", length);
205 }
206 }
207
208 /*
209 * This is the top level routine of the printer. 'p' points to the
210 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
211 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
212 * is the number of bytes actually captured.
213 */
214 u_int
215 sll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
216 {
217 u_int caplen = h->caplen;
218 u_int length = h->len;
219 const struct sll_header *sllp;
220 u_short hatype;
221 u_short ether_type;
222 int llc_hdrlen;
223 u_int hdrlen;
224
225 ndo->ndo_protocol = "sll_if";
226 if (caplen < SLL_HDR_LEN) {
227 /*
228 * XXX - this "can't happen" because "pcap-linux.c" always
229 * adds this many bytes of header to every packet in a
230 * cooked socket capture.
231 */
232 nd_print_trunc(ndo);
233 return (caplen);
234 }
235
236 sllp = (const struct sll_header *)p;
237
238 if (ndo->ndo_eflag)
239 sll_print(ndo, sllp, length);
240
241 /*
242 * Go past the cooked-mode header.
243 */
244 length -= SLL_HDR_LEN;
245 caplen -= SLL_HDR_LEN;
246 p += SLL_HDR_LEN;
247 hdrlen = SLL_HDR_LEN;
248
249 hatype = GET_BE_U_2(sllp->sll_hatype);
250 switch (hatype) {
251
252 case 803:
253 /*
254 * This is an packet with a radiotap header;
255 * just dissect the payload as such.
256 */
257 return (SLL_HDR_LEN + ieee802_11_radio_print(ndo, p, length, caplen));
258 }
259 ether_type = GET_BE_U_2(sllp->sll_protocol);
260
261 recurse:
262 /*
263 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
264 * packet type?
265 */
266 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
267 /*
268 * Yes - what type is it?
269 */
270 switch (ether_type) {
271
272 case LINUX_SLL_P_802_3:
273 /*
274 * Ethernet_802.3 IPX frame.
275 */
276 ipx_print(ndo, p, length);
277 break;
278
279 case LINUX_SLL_P_802_2:
280 /*
281 * 802.2.
282 * Try to print the LLC-layer header & higher layers.
283 */
284 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
285 if (llc_hdrlen < 0)
286 goto unknown; /* unknown LLC type */
287 hdrlen += llc_hdrlen;
288 break;
289
290 default:
291 /*FALLTHROUGH*/
292
293 unknown:
294 /* packet type not known, print raw packet */
295 if (!ndo->ndo_suppress_default_print)
296 ND_DEFAULTPRINT(p, caplen);
297 break;
298 }
299 } else if (ether_type == ETHERTYPE_8021Q) {
300 /*
301 * Print VLAN information, and then go back and process
302 * the enclosed type field.
303 */
304 if (caplen < 4) {
305 ndo->ndo_protocol = "vlan";
306 nd_print_trunc(ndo);
307 return (hdrlen + caplen);
308 }
309 if (ndo->ndo_eflag) {
310 uint16_t tag = GET_BE_U_2(p);
311
312 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
313 }
314
315 ether_type = GET_BE_U_2(p + 2);
316 if (ether_type <= MAX_ETHERNET_LENGTH_VAL)
317 ether_type = LINUX_SLL_P_802_2;
318 if (!ndo->ndo_qflag) {
319 ND_PRINT("ethertype %s, ",
320 tok2str(ethertype_values, "Unknown", ether_type));
321 }
322 p += 4;
323 length -= 4;
324 caplen -= 4;
325 hdrlen += 4;
326 goto recurse;
327 } else {
328 if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
329 /* ether_type not known, print raw packet */
330 if (!ndo->ndo_eflag)
331 sll_print(ndo, sllp, length + SLL_HDR_LEN);
332 if (!ndo->ndo_suppress_default_print)
333 ND_DEFAULTPRINT(p, caplen);
334 }
335 }
336
337 return (hdrlen);
338 }
339
340 static void
341 sll2_print(netdissect_options *ndo, const struct sll2_header *sllp, u_int length)
342 {
343 u_short ether_type;
344
345 ndo->ndo_protocol = "sll2";
346 ND_PRINT("%3s ",
347 tok2str(sll_pkttype_values,"?",GET_U_1(sllp->sll2_pkttype)));
348
349 /*
350 * XXX - check the link-layer address type value?
351 * For now, we just assume 6 means Ethernet.
352 * XXX - print others as strings of hex?
353 */
354 if (GET_U_1(sllp->sll2_halen) == 6)
355 ND_PRINT("%s ", etheraddr_string(ndo, sllp->sll2_addr));
356
357 if (!ndo->ndo_qflag) {
358 ether_type = GET_BE_U_2(sllp->sll2_protocol);
359
360 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
361 /*
362 * Not an Ethernet type; what type is it?
363 */
364 switch (ether_type) {
365
366 case LINUX_SLL_P_802_3:
367 /*
368 * Ethernet_802.3 IPX frame.
369 */
370 ND_PRINT("802.3");
371 break;
372
373 case LINUX_SLL_P_802_2:
374 /*
375 * 802.2.
376 */
377 ND_PRINT("802.2");
378 break;
379
380 default:
381 /*
382 * What is it?
383 */
384 ND_PRINT("ethertype Unknown (0x%04x)",
385 ether_type);
386 break;
387 }
388 } else {
389 ND_PRINT("ethertype %s (0x%04x)",
390 tok2str(ethertype_values, "Unknown", ether_type),
391 ether_type);
392 }
393 ND_PRINT(", length %u: ", length);
394 }
395 }
396
397 /*
398 * This is the top level routine of the printer. 'p' points to the
399 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
400 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
401 * is the number of bytes actually captured.
402 */
403 u_int
404 sll2_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
405 {
406 u_int caplen = h->caplen;
407 u_int length = h->len;
408 const struct sll2_header *sllp;
409 u_short hatype;
410 u_short ether_type;
411 int llc_hdrlen;
412 u_int hdrlen;
413 #ifdef HAVE_NET_IF_H
414 uint32_t if_index;
415 char ifname[IF_NAMESIZE];
416 #endif
417
418 ndo->ndo_protocol = "sll2_if";
419 if (caplen < SLL2_HDR_LEN) {
420 /*
421 * XXX - this "can't happen" because "pcap-linux.c" always
422 * adds this many bytes of header to every packet in a
423 * cooked socket capture.
424 */
425 nd_print_trunc(ndo);
426 return (caplen);
427 }
428
429 sllp = (const struct sll2_header *)p;
430 #ifdef HAVE_NET_IF_H
431 if_index = GET_BE_U_4(sllp->sll2_if_index);
432 if (if_indextoname(if_index, ifname))
433 ND_PRINT("ifindex %u (%s) ", if_index, ifname);
434 else
435 ND_PRINT("ifindex %u ", if_index);
436 #endif
437
438 if (ndo->ndo_eflag)
439 sll2_print(ndo, sllp, length);
440
441 /*
442 * Go past the cooked-mode header.
443 */
444 length -= SLL2_HDR_LEN;
445 caplen -= SLL2_HDR_LEN;
446 p += SLL2_HDR_LEN;
447 hdrlen = SLL2_HDR_LEN;
448
449 hatype = GET_BE_U_2(sllp->sll2_hatype);
450 switch (hatype) {
451
452 case 803:
453 /*
454 * This is an packet with a radiotap header;
455 * just dissect the payload as such.
456 */
457 return (SLL_HDR_LEN + ieee802_11_radio_print(ndo, p, length, caplen));
458 }
459 ether_type = GET_BE_U_2(sllp->sll2_protocol);
460
461 recurse:
462 /*
463 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
464 * packet type?
465 */
466 if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
467 /*
468 * Yes - what type is it?
469 */
470 switch (ether_type) {
471
472 case LINUX_SLL_P_802_3:
473 /*
474 * Ethernet_802.3 IPX frame.
475 */
476 ipx_print(ndo, p, length);
477 break;
478
479 case LINUX_SLL_P_802_2:
480 /*
481 * 802.2.
482 * Try to print the LLC-layer header & higher layers.
483 */
484 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
485 if (llc_hdrlen < 0)
486 goto unknown; /* unknown LLC type */
487 hdrlen += llc_hdrlen;
488 break;
489
490 default:
491 /*FALLTHROUGH*/
492
493 unknown:
494 /* packet type not known, print raw packet */
495 if (!ndo->ndo_suppress_default_print)
496 ND_DEFAULTPRINT(p, caplen);
497 break;
498 }
499 } else if (ether_type == ETHERTYPE_8021Q) {
500 /*
501 * Print VLAN information, and then go back and process
502 * the enclosed type field.
503 */
504 if (caplen < 4) {
505 ndo->ndo_protocol = "vlan";
506 nd_print_trunc(ndo);
507 return (hdrlen + caplen);
508 }
509 if (ndo->ndo_eflag) {
510 uint16_t tag = GET_BE_U_2(p);
511
512 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
513 }
514
515 ether_type = GET_BE_U_2(p + 2);
516 if (ether_type <= MAX_ETHERNET_LENGTH_VAL)
517 ether_type = LINUX_SLL_P_802_2;
518 if (!ndo->ndo_qflag) {
519 ND_PRINT("ethertype %s, ",
520 tok2str(ethertype_values, "Unknown", ether_type));
521 }
522 p += 4;
523 length -= 4;
524 caplen -= 4;
525 hdrlen += 4;
526 goto recurse;
527 } else {
528 if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
529 /* ether_type not known, print raw packet */
530 if (!ndo->ndo_eflag)
531 sll2_print(ndo, sllp, length + SLL_HDR_LEN);
532 if (!ndo->ndo_suppress_default_print)
533 ND_DEFAULTPRINT(p, caplen);
534 }
535 }
536
537 return (hdrlen);
538 }