xref: /minix/external/bsd/libpcap/dist/pcap-common.c (revision d56f51ea)
1 /*	$NetBSD: pcap-common.c,v 1.4 2015/08/28 11:20:55 joerg Exp $	*/
2 
3 /*
4  * Copyright (c) 1993, 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  *
23  * pcap-common.c - common code for pcap and pcap-ng files
24  */
25 
26 #include <sys/cdefs.h>
27 __RCSID("$NetBSD: pcap-common.c,v 1.4 2015/08/28 11:20:55 joerg Exp $");
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #ifdef WIN32
34 #include <pcap-stdinc.h>
35 #else /* WIN32 */
36 #if HAVE_INTTYPES_H
37 #include <inttypes.h>
38 #elif HAVE_STDINT_H
39 #include <stdint.h>
40 #endif
41 #ifdef HAVE_SYS_BITYPES_H
42 #include <sys/bitypes.h>
43 #endif
44 #include <sys/types.h>
45 #endif /* WIN32 */
46 
47 #include "pcap-int.h"
48 #include "pcap/usb.h"
49 #include "pcap/nflog.h"
50 
51 #include "pcap-common.h"
52 
53 /*
54  * We don't write DLT_* values to capture files, because they're not the
55  * same on all platforms.
56  *
57  * Unfortunately, the various flavors of BSD have not always used the same
58  * numerical values for the same data types, and various patches to
59  * libpcap for non-BSD OSes have added their own DLT_* codes for link
60  * layer encapsulation types seen on those OSes, and those codes have had,
61  * in some cases, values that were also used, on other platforms, for other
62  * link layer encapsulation types.
63  *
64  * This means that capture files of a type whose numerical DLT_* code
65  * means different things on different BSDs, or with different versions
66  * of libpcap, can't always be read on systems other than those like
67  * the one running on the machine on which the capture was made.
68  *
69  * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
70  * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
71  * codes to DLT_* codes when reading a savefile header.
72  *
73  * For those DLT_* codes that have, as far as we know, the same values on
74  * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
75  * DLT_xxx; that way, captures of those types can still be read by
76  * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
77  * captures of those types written by versions of libpcap that map DLT_
78  * values to LINKTYPE_ values can still be read by older versions
79  * of libpcap.
80  *
81  * The other LINKTYPE_* codes are given values starting at 100, in the
82  * hopes that no DLT_* code will be given one of those values.
83  *
84  * In order to ensure that a given LINKTYPE_* code's value will refer to
85  * the same encapsulation type on all platforms, you should not allocate
86  * a new LINKTYPE_* value without consulting
87  * "tcpdump-workers@lists.tcpdump.org".  The tcpdump developers will
88  * allocate a value for you, and will not subsequently allocate it to
89  * anybody else; that value will be added to the "pcap.h" in the
90  * tcpdump.org Git repository, so that a future libpcap release will
91  * include it.
92  *
93  * You should, if possible, also contribute patches to libpcap and tcpdump
94  * to handle the new encapsulation type, so that they can also be checked
95  * into the tcpdump.org Git repository and so that they will appear in
96  * future libpcap and tcpdump releases.
97  *
98  * Do *NOT* assume that any values after the largest value in this file
99  * are available; you might not have the most up-to-date version of this
100  * file, and new values after that one might have been assigned.  Also,
101  * do *NOT* use any values below 100 - those might already have been
102  * taken by one (or more!) organizations.
103  *
104  * Any platform that defines additional DLT_* codes should:
105  *
106  *	request a LINKTYPE_* code and value from tcpdump.org,
107  *	as per the above;
108  *
109  *	add, in their version of libpcap, an entry to map
110  *	those DLT_* codes to the corresponding LINKTYPE_*
111  *	code;
112  *
113  *	redefine, in their "net/bpf.h", any DLT_* values
114  *	that collide with the values used by their additional
115  *	DLT_* codes, to remove those collisions (but without
116  *	making them collide with any of the LINKTYPE_*
117  *	values equal to 50 or above; they should also avoid
118  *	defining DLT_* values that collide with those
119  *	LINKTYPE_* values, either).
120  */
121 #define LINKTYPE_NULL		DLT_NULL
122 #define LINKTYPE_ETHERNET	DLT_EN10MB	/* also for 100Mb and up */
123 #define LINKTYPE_EXP_ETHERNET	DLT_EN3MB	/* 3Mb experimental Ethernet */
124 #define LINKTYPE_AX25		DLT_AX25
125 #define LINKTYPE_PRONET		DLT_PRONET
126 #define LINKTYPE_CHAOS		DLT_CHAOS
127 #define LINKTYPE_IEEE802_5	DLT_IEEE802	/* DLT_IEEE802 is used for 802.5 Token Ring */
128 #define LINKTYPE_ARCNET_BSD	DLT_ARCNET	/* BSD-style headers */
129 #define LINKTYPE_SLIP		DLT_SLIP
130 #define LINKTYPE_PPP		DLT_PPP
131 #define LINKTYPE_FDDI		DLT_FDDI
132 
133 /*
134  * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
135  * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
136  * field) at the beginning of the packet.
137  *
138  * This is for use when there is always such a header; the address field
139  * might be 0xff, for regular PPP, or it might be an address field for Cisco
140  * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
141  * HDLC").  This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
142  *
143  * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
144  * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
145  * captures will be written out with a link type that NetBSD's tcpdump
146  * can read.
147  */
148 #define LINKTYPE_PPP_HDLC	50		/* PPP in HDLC-like framing */
149 
150 #define LINKTYPE_PPP_ETHER	51		/* NetBSD PPP-over-Ethernet */
151 
152 #define LINKTYPE_SYMANTEC_FIREWALL 99		/* Symantec Enterprise Firewall */
153 
154 /*
155  * These correspond to DLT_s that have different values on different
156  * platforms; we map between these values in capture files and
157  * the DLT_ values as returned by pcap_datalink() and passed to
158  * pcap_open_dead().
159  */
160 #define LINKTYPE_ATM_RFC1483	100		/* LLC/SNAP-encapsulated ATM */
161 #define LINKTYPE_RAW		101		/* raw IP */
162 #define LINKTYPE_SLIP_BSDOS	102		/* BSD/OS SLIP BPF header */
163 #define LINKTYPE_PPP_BSDOS	103		/* BSD/OS PPP BPF header */
164 
165 /*
166  * Values starting with 104 are used for newly-assigned link-layer
167  * header type values; for those link-layer header types, the DLT_
168  * value returned by pcap_datalink() and passed to pcap_open_dead(),
169  * and the LINKTYPE_ value that appears in capture files, are the
170  * same.
171  *
172  * LINKTYPE_MATCHING_MIN is the lowest such value; LINKTYPE_MATCHING_MAX
173  * is the highest such value.
174  */
175 #define LINKTYPE_MATCHING_MIN	104		/* lowest value in the "matching" range */
176 
177 #define LINKTYPE_C_HDLC		104		/* Cisco HDLC */
178 #define LINKTYPE_IEEE802_11	105		/* IEEE 802.11 (wireless) */
179 #define LINKTYPE_ATM_CLIP	106		/* Linux Classical IP over ATM */
180 #define LINKTYPE_FRELAY		107		/* Frame Relay */
181 #define LINKTYPE_LOOP		108		/* OpenBSD loopback */
182 #define LINKTYPE_ENC		109		/* OpenBSD IPSEC enc */
183 
184 /*
185  * These three types are reserved for future use.
186  */
187 #define LINKTYPE_LANE8023	110		/* ATM LANE + 802.3 */
188 #define LINKTYPE_HIPPI		111		/* NetBSD HIPPI */
189 #define LINKTYPE_HDLC		112		/* NetBSD HDLC framing */
190 
191 #define LINKTYPE_LINUX_SLL	113		/* Linux cooked socket capture */
192 #define LINKTYPE_LTALK		114		/* Apple LocalTalk hardware */
193 #define LINKTYPE_ECONET		115		/* Acorn Econet */
194 
195 /*
196  * Reserved for use with OpenBSD ipfilter.
197  */
198 #define LINKTYPE_IPFILTER	116
199 
200 #define LINKTYPE_PFLOG		117		/* OpenBSD DLT_PFLOG */
201 #define LINKTYPE_CISCO_IOS	118		/* For Cisco-internal use */
202 #define LINKTYPE_IEEE802_11_PRISM 119		/* 802.11 plus Prism II monitor mode radio metadata header */
203 #define LINKTYPE_IEEE802_11_AIRONET 120		/* 802.11 plus FreeBSD Aironet driver radio metadata header */
204 
205 /*
206  * Reserved for Siemens HiPath HDLC.
207  */
208 #define LINKTYPE_HHDLC		121
209 
210 #define LINKTYPE_IP_OVER_FC	122		/* RFC 2625 IP-over-Fibre Channel */
211 #define LINKTYPE_SUNATM		123		/* Solaris+SunATM */
212 
213 /*
214  * Reserved as per request from Kent Dahlgren <kent@praesum.com>
215  * for private use.
216  */
217 #define LINKTYPE_RIO		124		/* RapidIO */
218 #define LINKTYPE_PCI_EXP	125		/* PCI Express */
219 #define LINKTYPE_AURORA		126		/* Xilinx Aurora link layer */
220 
221 #define LINKTYPE_IEEE802_11_RADIOTAP 127	/* 802.11 plus radiotap radio metadata header */
222 
223 /*
224  * Reserved for the TZSP encapsulation, as per request from
225  * Chris Waters <chris.waters@networkchemistry.com>
226  * TZSP is a generic encapsulation for any other link type,
227  * which includes a means to include meta-information
228  * with the packet, e.g. signal strength and channel
229  * for 802.11 packets.
230  */
231 #define LINKTYPE_TZSP		128		/* Tazmen Sniffer Protocol */
232 
233 #define LINKTYPE_ARCNET_LINUX	129		/* Linux-style headers */
234 
235 /*
236  * Juniper-private data link types, as per request from
237  * Hannes Gredler <hannes@juniper.net>.  The corresponding
238  * DLT_s are used for passing on chassis-internal
239  * metainformation such as QOS profiles, etc..
240  */
241 #define LINKTYPE_JUNIPER_MLPPP  130
242 #define LINKTYPE_JUNIPER_MLFR   131
243 #define LINKTYPE_JUNIPER_ES     132
244 #define LINKTYPE_JUNIPER_GGSN   133
245 #define LINKTYPE_JUNIPER_MFR    134
246 #define LINKTYPE_JUNIPER_ATM2   135
247 #define LINKTYPE_JUNIPER_SERVICES 136
248 #define LINKTYPE_JUNIPER_ATM1   137
249 
250 #define LINKTYPE_APPLE_IP_OVER_IEEE1394 138	/* Apple IP-over-IEEE 1394 cooked header */
251 
252 #define LINKTYPE_MTP2_WITH_PHDR	139
253 #define LINKTYPE_MTP2		140
254 #define LINKTYPE_MTP3		141
255 #define LINKTYPE_SCCP		142
256 
257 #define LINKTYPE_DOCSIS		143		/* DOCSIS MAC frames */
258 
259 #define LINKTYPE_LINUX_IRDA	144		/* Linux-IrDA */
260 
261 /*
262  * Reserved for IBM SP switch and IBM Next Federation switch.
263  */
264 #define LINKTYPE_IBM_SP		145
265 #define LINKTYPE_IBM_SN		146
266 
267 /*
268  * Reserved for private use.  If you have some link-layer header type
269  * that you want to use within your organization, with the capture files
270  * using that link-layer header type not ever be sent outside your
271  * organization, you can use these values.
272  *
273  * No libpcap release will use these for any purpose, nor will any
274  * tcpdump release use them, either.
275  *
276  * Do *NOT* use these in capture files that you expect anybody not using
277  * your private versions of capture-file-reading tools to read; in
278  * particular, do *NOT* use them in products, otherwise you may find that
279  * people won't be able to use tcpdump, or snort, or Ethereal, or... to
280  * read capture files from your firewall/intrusion detection/traffic
281  * monitoring/etc. appliance, or whatever product uses that LINKTYPE_ value,
282  * and you may also find that the developers of those applications will
283  * not accept patches to let them read those files.
284  *
285  * Also, do not use them if somebody might send you a capture using them
286  * for *their* private type and tools using them for *your* private type
287  * would have to read them.
288  *
289  * Instead, in those cases, ask "tcpdump-workers@lists.tcpdump.org" for a
290  * new DLT_ and LINKTYPE_ value, as per the comment in pcap/bpf.h, and use
291  * the type you're given.
292  */
293 #define LINKTYPE_USER0		147
294 #define LINKTYPE_USER1		148
295 #define LINKTYPE_USER2		149
296 #define LINKTYPE_USER3		150
297 #define LINKTYPE_USER4		151
298 #define LINKTYPE_USER5		152
299 #define LINKTYPE_USER6		153
300 #define LINKTYPE_USER7		154
301 #define LINKTYPE_USER8		155
302 #define LINKTYPE_USER9		156
303 #define LINKTYPE_USER10		157
304 #define LINKTYPE_USER11		158
305 #define LINKTYPE_USER12		159
306 #define LINKTYPE_USER13		160
307 #define LINKTYPE_USER14		161
308 #define LINKTYPE_USER15		162
309 
310 /*
311  * For future use with 802.11 captures - defined by AbsoluteValue
312  * Systems to store a number of bits of link-layer information
313  * including radio information:
314  *
315  *	http://www.shaftnet.org/~pizza/software/capturefrm.txt
316  */
317 #define LINKTYPE_IEEE802_11_AVS	163	/* 802.11 plus AVS radio metadata header */
318 
319 /*
320  * Juniper-private data link type, as per request from
321  * Hannes Gredler <hannes@juniper.net>.  The corresponding
322  * DLT_s are used for passing on chassis-internal
323  * metainformation such as QOS profiles, etc..
324  */
325 #define LINKTYPE_JUNIPER_MONITOR 164
326 
327 /*
328  * BACnet MS/TP frames.
329  */
330 #define LINKTYPE_BACNET_MS_TP	165
331 
332 /*
333  * Another PPP variant as per request from Karsten Keil <kkeil@suse.de>.
334  *
335  * This is used in some OSes to allow a kernel socket filter to distinguish
336  * between incoming and outgoing packets, on a socket intended to
337  * supply pppd with outgoing packets so it can do dial-on-demand and
338  * hangup-on-lack-of-demand; incoming packets are filtered out so they
339  * don't cause pppd to hold the connection up (you don't want random
340  * input packets such as port scans, packets from old lost connections,
341  * etc. to force the connection to stay up).
342  *
343  * The first byte of the PPP header (0xff03) is modified to accomodate
344  * the direction - 0x00 = IN, 0x01 = OUT.
345  */
346 #define LINKTYPE_PPP_PPPD	166
347 
348 /*
349  * Juniper-private data link type, as per request from
350  * Hannes Gredler <hannes@juniper.net>.  The DLT_s are used
351  * for passing on chassis-internal metainformation such as
352  * QOS profiles, cookies, etc..
353  */
354 #define LINKTYPE_JUNIPER_PPPOE     167
355 #define LINKTYPE_JUNIPER_PPPOE_ATM 168
356 
357 #define LINKTYPE_GPRS_LLC	169		/* GPRS LLC */
358 #define LINKTYPE_GPF_T		170		/* GPF-T (ITU-T G.7041/Y.1303) */
359 #define LINKTYPE_GPF_F		171		/* GPF-T (ITU-T G.7041/Y.1303) */
360 
361 /*
362  * Requested by Oolan Zimmer <oz@gcom.com> for use in Gcom's T1/E1 line
363  * monitoring equipment.
364  */
365 #define LINKTYPE_GCOM_T1E1	172
366 #define LINKTYPE_GCOM_SERIAL	173
367 
368 /*
369  * Juniper-private data link type, as per request from
370  * Hannes Gredler <hannes@juniper.net>.  The DLT_ is used
371  * for internal communication to Physical Interface Cards (PIC)
372  */
373 #define LINKTYPE_JUNIPER_PIC_PEER    174
374 
375 /*
376  * Link types requested by Gregor Maier <gregor@endace.com> of Endace
377  * Measurement Systems.  They add an ERF header (see
378  * http://www.endace.com/support/EndaceRecordFormat.pdf) in front of
379  * the link-layer header.
380  */
381 #define LINKTYPE_ERF_ETH	175	/* Ethernet */
382 #define LINKTYPE_ERF_POS	176	/* Packet-over-SONET */
383 
384 /*
385  * Requested by Daniele Orlandi <daniele@orlandi.com> for raw LAPD
386  * for vISDN (http://www.orlandi.com/visdn/).  Its link-layer header
387  * includes additional information before the LAPD header, so it's
388  * not necessarily a generic LAPD header.
389  */
390 #define LINKTYPE_LINUX_LAPD	177
391 
392 /*
393  * Juniper-private data link type, as per request from
394  * Hannes Gredler <hannes@juniper.net>.
395  * The Link Types are used for prepending meta-information
396  * like interface index, interface name
397  * before standard Ethernet, PPP, Frelay & C-HDLC Frames
398  */
399 #define LINKTYPE_JUNIPER_ETHER  178
400 #define LINKTYPE_JUNIPER_PPP    179
401 #define LINKTYPE_JUNIPER_FRELAY 180
402 #define LINKTYPE_JUNIPER_CHDLC  181
403 
404 /*
405  * Multi Link Frame Relay (FRF.16)
406  */
407 #define LINKTYPE_MFR            182
408 
409 /*
410  * Juniper-private data link type, as per request from
411  * Hannes Gredler <hannes@juniper.net>.
412  * The DLT_ is used for internal communication with a
413  * voice Adapter Card (PIC)
414  */
415 #define LINKTYPE_JUNIPER_VP     183
416 
417 /*
418  * Arinc 429 frames.
419  * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
420  * Every frame contains a 32bit A429 label.
421  * More documentation on Arinc 429 can be found at
422  * http://www.condoreng.com/support/downloads/tutorials/ARINCTutorial.pdf
423  */
424 #define LINKTYPE_A429           184
425 
426 /*
427  * Arinc 653 Interpartition Communication messages.
428  * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
429  * Please refer to the A653-1 standard for more information.
430  */
431 #define LINKTYPE_A653_ICM       185
432 
433 /*
434  * USB packets, beginning with a USB setup header; requested by
435  * Paolo Abeni <paolo.abeni@email.it>.
436  */
437 #define LINKTYPE_USB		186
438 
439 /*
440  * Bluetooth HCI UART transport layer (part H:4); requested by
441  * Paolo Abeni.
442  */
443 #define LINKTYPE_BLUETOOTH_HCI_H4	187
444 
445 /*
446  * IEEE 802.16 MAC Common Part Sublayer; requested by Maria Cruz
447  * <cruz_petagay@bah.com>.
448  */
449 #define LINKTYPE_IEEE802_16_MAC_CPS	188
450 
451 /*
452  * USB packets, beginning with a Linux USB header; requested by
453  * Paolo Abeni <paolo.abeni@email.it>.
454  */
455 #define LINKTYPE_USB_LINUX		189
456 
457 /*
458  * Controller Area Network (CAN) v. 2.0B packets.
459  * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
460  * Used to dump CAN packets coming from a CAN Vector board.
461  * More documentation on the CAN v2.0B frames can be found at
462  * http://www.can-cia.org/downloads/?269
463  */
464 #define LINKTYPE_CAN20B         190
465 
466 /*
467  * IEEE 802.15.4, with address fields padded, as is done by Linux
468  * drivers; requested by Juergen Schimmer.
469  */
470 #define LINKTYPE_IEEE802_15_4_LINUX	191
471 
472 /*
473  * Per Packet Information encapsulated packets.
474  * LINKTYPE_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
475  */
476 #define LINKTYPE_PPI			192
477 
478 /*
479  * Header for 802.16 MAC Common Part Sublayer plus a radiotap radio header;
480  * requested by Charles Clancy.
481  */
482 #define LINKTYPE_IEEE802_16_MAC_CPS_RADIO	193
483 
484 /*
485  * Juniper-private data link type, as per request from
486  * Hannes Gredler <hannes@juniper.net>.
487  * The DLT_ is used for internal communication with a
488  * integrated service module (ISM).
489  */
490 #define LINKTYPE_JUNIPER_ISM    194
491 
492 /*
493  * IEEE 802.15.4, exactly as it appears in the spec (no padding, no
494  * nothing); requested by Mikko Saarnivala <mikko.saarnivala@sensinode.com>.
495  */
496 #define LINKTYPE_IEEE802_15_4	195
497 
498 /*
499  * Various link-layer types, with a pseudo-header, for SITA
500  * (http://www.sita.aero/); requested by Fulko Hew (fulko.hew@gmail.com).
501  */
502 #define LINKTYPE_SITA		196
503 
504 /*
505  * Various link-layer types, with a pseudo-header, for Endace DAG cards;
506  * encapsulates Endace ERF records.  Requested by Stephen Donnelly
507  * <stephen@endace.com>.
508  */
509 #define LINKTYPE_ERF		197
510 
511 /*
512  * Special header prepended to Ethernet packets when capturing from a
513  * u10 Networks board.  Requested by Phil Mulholland
514  * <phil@u10networks.com>.
515  */
516 #define LINKTYPE_RAIF1		198
517 
518 /*
519  * IPMB packet for IPMI, beginning with the I2C slave address, followed
520  * by the netFn and LUN, etc..  Requested by Chanthy Toeung
521  * <chanthy.toeung@ca.kontron.com>.
522  */
523 #define LINKTYPE_IPMB		199
524 
525 /*
526  * Juniper-private data link type, as per request from
527  * Hannes Gredler <hannes@juniper.net>.
528  * The DLT_ is used for capturing data on a secure tunnel interface.
529  */
530 #define LINKTYPE_JUNIPER_ST     200
531 
532 /*
533  * Bluetooth HCI UART transport layer (part H:4), with pseudo-header
534  * that includes direction information; requested by Paolo Abeni.
535  */
536 #define LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR	201
537 
538 /*
539  * AX.25 packet with a 1-byte KISS header; see
540  *
541  *	http://www.ax25.net/kiss.htm
542  *
543  * as per Richard Stearn <richard@rns-stearn.demon.co.uk>.
544  */
545 #define LINKTYPE_AX25_KISS	202
546 
547 /*
548  * LAPD packets from an ISDN channel, starting with the address field,
549  * with no pseudo-header.
550  * Requested by Varuna De Silva <varunax@gmail.com>.
551  */
552 #define LINKTYPE_LAPD		203
553 
554 /*
555  * Variants of various link-layer headers, with a one-byte direction
556  * pseudo-header prepended - zero means "received by this host",
557  * non-zero (any non-zero value) means "sent by this host" - as per
558  * Will Barker <w.barker@zen.co.uk>.
559  */
560 #define LINKTYPE_PPP_WITH_DIR	204	/* PPP */
561 #define LINKTYPE_C_HDLC_WITH_DIR 205	/* Cisco HDLC */
562 #define LINKTYPE_FRELAY_WITH_DIR 206	/* Frame Relay */
563 #define LINKTYPE_LAPB_WITH_DIR	207	/* LAPB */
564 
565 /*
566  * 208 is reserved for an as-yet-unspecified proprietary link-layer
567  * type, as requested by Will Barker.
568  */
569 
570 /*
571  * IPMB with a Linux-specific pseudo-header; as requested by Alexey Neyman
572  * <avn@pigeonpoint.com>.
573  */
574 #define LINKTYPE_IPMB_LINUX	209
575 
576 /*
577  * FlexRay automotive bus - http://www.flexray.com/ - as requested
578  * by Hannes Kaelber <hannes.kaelber@x2e.de>.
579  */
580 #define LINKTYPE_FLEXRAY	210
581 
582 /*
583  * Media Oriented Systems Transport (MOST) bus for multimedia
584  * transport - http://www.mostcooperation.com/ - as requested
585  * by Hannes Kaelber <hannes.kaelber@x2e.de>.
586  */
587 #define LINKTYPE_MOST		211
588 
589 /*
590  * Local Interconnect Network (LIN) bus for vehicle networks -
591  * http://www.lin-subbus.org/ - as requested by Hannes Kaelber
592  * <hannes.kaelber@x2e.de>.
593  */
594 #define LINKTYPE_LIN		212
595 
596 /*
597  * X2E-private data link type used for serial line capture,
598  * as requested by Hannes Kaelber <hannes.kaelber@x2e.de>.
599  */
600 #define LINKTYPE_X2E_SERIAL	213
601 
602 /*
603  * X2E-private data link type used for the Xoraya data logger
604  * family, as requested by Hannes Kaelber <hannes.kaelber@x2e.de>.
605  */
606 #define LINKTYPE_X2E_XORAYA	214
607 
608 /*
609  * IEEE 802.15.4, exactly as it appears in the spec (no padding, no
610  * nothing), but with the PHY-level data for non-ASK PHYs (4 octets
611  * of 0 as preamble, one octet of SFD, one octet of frame length+
612  * reserved bit, and then the MAC-layer data, starting with the
613  * frame control field).
614  *
615  * Requested by Max Filippov <jcmvbkbc@gmail.com>.
616  */
617 #define LINKTYPE_IEEE802_15_4_NONASK_PHY	215
618 
619 /*
620  * David Gibson <david@gibson.dropbear.id.au> requested this for
621  * captures from the Linux kernel /dev/input/eventN devices. This
622  * is used to communicate keystrokes and mouse movements from the
623  * Linux kernel to display systems, such as Xorg.
624  */
625 #define LINKTYPE_LINUX_EVDEV	216
626 
627 /*
628  * GSM Um and Abis interfaces, preceded by a "gsmtap" header.
629  *
630  * Requested by Harald Welte <laforge@gnumonks.org>.
631  */
632 #define LINKTYPE_GSMTAP_UM	217
633 #define LINKTYPE_GSMTAP_ABIS	218
634 
635 /*
636  * MPLS, with an MPLS label as the link-layer header.
637  * Requested by Michele Marchetto <michele@openbsd.org> on behalf
638  * of OpenBSD.
639  */
640 #define LINKTYPE_MPLS		219
641 
642 /*
643  * USB packets, beginning with a Linux USB header, with the USB header
644  * padded to 64 bytes; required for memory-mapped access.
645  */
646 #define LINKTYPE_USB_LINUX_MMAPPED		220
647 
648 /*
649  * DECT packets, with a pseudo-header; requested by
650  * Matthias Wenzel <tcpdump@mazzoo.de>.
651  */
652 #define LINKTYPE_DECT		221
653 
654 /*
655  * From: "Lidwa, Eric (GSFC-582.0)[SGT INC]" <eric.lidwa-1@nasa.gov>
656  * Date: Mon, 11 May 2009 11:18:30 -0500
657  *
658  * DLT_AOS. We need it for AOS Space Data Link Protocol.
659  *   I have already written dissectors for but need an OK from
660  *   legal before I can submit a patch.
661  *
662  */
663 #define LINKTYPE_AOS		222
664 
665 /*
666  * Wireless HART (Highway Addressable Remote Transducer)
667  * From the HART Communication Foundation
668  * IES/PAS 62591
669  *
670  * Requested by Sam Roberts <vieuxtech@gmail.com>.
671  */
672 #define LINKTYPE_WIHART		223
673 
674 /*
675  * Fibre Channel FC-2 frames, beginning with a Frame_Header.
676  * Requested by Kahou Lei <kahou82@gmail.com>.
677  */
678 #define LINKTYPE_FC_2		224
679 
680 /*
681  * Fibre Channel FC-2 frames, beginning with an encoding of the
682  * SOF, and ending with an encoding of the EOF.
683  *
684  * The encodings represent the frame delimiters as 4-byte sequences
685  * representing the corresponding ordered sets, with K28.5
686  * represented as 0xBC, and the D symbols as the corresponding
687  * byte values; for example, SOFi2, which is K28.5 - D21.5 - D1.2 - D21.2,
688  * is represented as 0xBC 0xB5 0x55 0x55.
689  *
690  * Requested by Kahou Lei <kahou82@gmail.com>.
691  */
692 #define LINKTYPE_FC_2_WITH_FRAME_DELIMS		225
693 
694 /*
695  * Solaris ipnet pseudo-header; requested by Darren Reed <Darren.Reed@Sun.COM>.
696  *
697  * The pseudo-header starts with a one-byte version number; for version 2,
698  * the pseudo-header is:
699  *
700  * struct dl_ipnetinfo {
701  *     u_int8_t   dli_version;
702  *     u_int8_t   dli_family;
703  *     u_int16_t  dli_htype;
704  *     u_int32_t  dli_pktlen;
705  *     u_int32_t  dli_ifindex;
706  *     u_int32_t  dli_grifindex;
707  *     u_int32_t  dli_zsrc;
708  *     u_int32_t  dli_zdst;
709  * };
710  *
711  * dli_version is 2 for the current version of the pseudo-header.
712  *
713  * dli_family is a Solaris address family value, so it's 2 for IPv4
714  * and 26 for IPv6.
715  *
716  * dli_htype is a "hook type" - 0 for incoming packets, 1 for outgoing
717  * packets, and 2 for packets arriving from another zone on the same
718  * machine.
719  *
720  * dli_pktlen is the length of the packet data following the pseudo-header
721  * (so the captured length minus dli_pktlen is the length of the
722  * pseudo-header, assuming the entire pseudo-header was captured).
723  *
724  * dli_ifindex is the interface index of the interface on which the
725  * packet arrived.
726  *
727  * dli_grifindex is the group interface index number (for IPMP interfaces).
728  *
729  * dli_zsrc is the zone identifier for the source of the packet.
730  *
731  * dli_zdst is the zone identifier for the destination of the packet.
732  *
733  * A zone number of 0 is the global zone; a zone number of 0xffffffff
734  * means that the packet arrived from another host on the network, not
735  * from another zone on the same machine.
736  *
737  * An IPv4 or IPv6 datagram follows the pseudo-header; dli_family indicates
738  * which of those it is.
739  */
740 #define LINKTYPE_IPNET		226
741 
742 /*
743  * CAN (Controller Area Network) frames, with a pseudo-header as supplied
744  * by Linux SocketCAN.  See Documentation/networking/can.txt in the Linux
745  * source.
746  *
747  * Requested by Felix Obenhuber <felix@obenhuber.de>.
748  */
749 #define LINKTYPE_CAN_SOCKETCAN	227
750 
751 /*
752  * Raw IPv4/IPv6; different from DLT_RAW in that the DLT_ value specifies
753  * whether it's v4 or v6.  Requested by Darren Reed <Darren.Reed@Sun.COM>.
754  */
755 #define LINKTYPE_IPV4		228
756 #define LINKTYPE_IPV6		229
757 
758 /*
759  * IEEE 802.15.4, exactly as it appears in the spec (no padding, no
760  * nothing), and with no FCS at the end of the frame; requested by
761  * Jon Smirl <jonsmirl@gmail.com>.
762  */
763 #define LINKTYPE_IEEE802_15_4_NOFCS		230
764 
765 /*
766  * Raw D-Bus:
767  *
768  *	http://www.freedesktop.org/wiki/Software/dbus
769  *
770  * messages:
771  *
772  *	http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages
773  *
774  * starting with the endianness flag, followed by the message type, etc.,
775  * but without the authentication handshake before the message sequence:
776  *
777  *	http://dbus.freedesktop.org/doc/dbus-specification.html#auth-protocol
778  *
779  * Requested by Martin Vidner <martin@vidner.net>.
780  */
781 #define LINKTYPE_DBUS		231
782 
783 /*
784  * Juniper-private data link type, as per request from
785  * Hannes Gredler <hannes@juniper.net>.
786  */
787 #define LINKTYPE_JUNIPER_VS			232
788 #define LINKTYPE_JUNIPER_SRX_E2E		233
789 #define LINKTYPE_JUNIPER_FIBRECHANNEL		234
790 
791 /*
792  * DVB-CI (DVB Common Interface for communication between a PC Card
793  * module and a DVB receiver).  See
794  *
795  *	http://www.kaiser.cx/pcap-dvbci.html
796  *
797  * for the specification.
798  *
799  * Requested by Martin Kaiser <martin@kaiser.cx>.
800  */
801 #define LINKTYPE_DVB_CI		235
802 
803 /*
804  * Variant of 3GPP TS 27.010 multiplexing protocol.  Requested
805  * by Hans-Christoph Schemmel <hans-christoph.schemmel@cinterion.com>.
806  */
807 #define LINKTYPE_MUX27010	236
808 
809 /*
810  * STANAG 5066 D_PDUs.  Requested by M. Baris Demiray
811  * <barisdemiray@gmail.com>.
812  */
813 #define LINKTYPE_STANAG_5066_D_PDU		237
814 
815 /*
816  * Juniper-private data link type, as per request from
817  * Hannes Gredler <hannes@juniper.net>.
818  */
819 #define LINKTYPE_JUNIPER_ATM_CEMIC		238
820 
821 /*
822  * NetFilter LOG messages
823  * (payload of netlink NFNL_SUBSYS_ULOG/NFULNL_MSG_PACKET packets)
824  *
825  * Requested by Jakub Zawadzki <darkjames-ws@darkjames.pl>
826  */
827 #define LINKTYPE_NFLOG		239
828 
829 /*
830  * Hilscher Gesellschaft fuer Systemautomation mbH link-layer type
831  * for Ethernet packets with a 4-byte pseudo-header and always
832  * with the payload including the FCS, as supplied by their
833  * netANALYZER hardware and software.
834  *
835  * Requested by Holger P. Frommer <HPfrommer@hilscher.com>
836  */
837 #define LINKTYPE_NETANALYZER	240
838 
839 /*
840  * Hilscher Gesellschaft fuer Systemautomation mbH link-layer type
841  * for Ethernet packets with a 4-byte pseudo-header and FCS and
842  * 1 byte of SFD, as supplied by their netANALYZER hardware and
843  * software.
844  *
845  * Requested by Holger P. Frommer <HPfrommer@hilscher.com>
846  */
847 #define LINKTYPE_NETANALYZER_TRANSPARENT	241
848 
849 /*
850  * IP-over-InfiniBand, as specified by RFC 4391.
851  *
852  * Requested by Petr Sumbera <petr.sumbera@oracle.com>.
853  */
854 #define LINKTYPE_IPOIB		242
855 
856 /*
857  * MPEG-2 transport stream (ISO 13818-1/ITU-T H.222.0).
858  *
859  * Requested by Guy Martin <gmsoft@tuxicoman.be>.
860  */
861 #define LINKTYPE_MPEG_2_TS	243
862 
863 /*
864  * ng4T GmbH's UMTS Iub/Iur-over-ATM and Iub/Iur-over-IP format as
865  * used by their ng40 protocol tester.
866  *
867  * Requested by Jens Grimmer <jens.grimmer@ng4t.com>.
868  */
869 #define LINKTYPE_NG40		244
870 
871 /*
872  * Pseudo-header giving adapter number and flags, followed by an NFC
873  * (Near-Field Communications) Logical Link Control Protocol (LLCP) PDU,
874  * as specified by NFC Forum Logical Link Control Protocol Technical
875  * Specification LLCP 1.1.
876  *
877  * Requested by Mike Wakerly <mikey@google.com>.
878  */
879 #define LINKTYPE_NFC_LLCP	245
880 
881 /*
882  * pfsync output; DLT_PFSYNC is 18, which collides with DLT_CIP in
883  * SuSE 6.3, on OpenBSD, NetBSD, DragonFly BSD, and Mac OS X, and
884  * is 121, which collides with DLT_HHDLC, in FreeBSD.  We pick a
885  * shiny new link-layer header type value that doesn't collide with
886  * anything, in the hopes that future pfsync savefiles, if any,
887  * won't require special hacks to distinguish from other savefiles.
888  *
889  */
890 #define LINKTYPE_PFSYNC		246
891 
892 /*
893  * Raw InfiniBand packets, starting with the Local Routing Header.
894  *
895  * Requested by Oren Kladnitsky <orenk@mellanox.com>.
896  */
897 #define LINKTYPE_INFINIBAND	247
898 
899 /*
900  * SCTP, with no lower-level protocols (i.e., no IPv4 or IPv6).
901  *
902  * Requested by Michael Tuexen <Michael.Tuexen@lurchi.franken.de>.
903  */
904 #define LINKTYPE_SCTP		248
905 
906 /*
907  * USB packets, beginning with a USBPcap header.
908  *
909  * Requested by Tomasz Mon <desowin@gmail.com>
910  */
911 #define LINKTYPE_USBPCAP	249
912 
913 /*
914  * Schweitzer Engineering Laboratories "RTAC" product serial-line
915  * packets.
916  *
917  * Requested by Chris Bontje <chris_bontje@selinc.com>.
918  */
919 #define DLT_RTAC_SERIAL		250
920 
921 /*
922  * Bluetooth Low Energy air interface link-layer packets.
923  *
924  * Requested by Mike Kershaw <dragorn@kismetwireless.net>.
925  */
926 #define LINKTYPE_BLUETOOTH_LE_LL	251
927 
928 /*
929  * Link-layer header type for upper-protocol layer PDU saves from wireshark.
930  *
931  * the actual contents are determined by two TAGs stored with each
932  * packet:
933  *   EXP_PDU_TAG_LINKTYPE          the link type (LINKTYPE_ value) of the
934  *				   original packet.
935  *
936  *   EXP_PDU_TAG_PROTO_NAME        the name of the wireshark dissector
937  * 				   that can make sense of the data stored.
938  */
939 #define LINKTYPE_WIRESHARK_UPPER_PDU	252
940 
941 /*
942  * Link-layer header type for the netlink protocol (nlmon devices).
943  */
944 #define LINKTYPE_NETLINK		253
945 
946 /*
947  * Bluetooth Linux Monitor headers for the BlueZ stack.
948  */
949 #define LINKTYPE_BLUETOOTH_LINUX_MONITOR	254
950 
951 /*
952  * Bluetooth Basic Rate/Enhanced Data Rate baseband packets, as
953  * captured by Ubertooth.
954  */
955 #define LINKTYPE_BLUETOOTH_BREDR_BB	255
956 
957 /*
958  * Bluetooth Low Energy link layer packets, as captured by Ubertooth.
959  */
960 #define LINKTYPE_BLUETOOTH_LE_LL_WITH_PHDR	256
961 
962 /*
963  * PROFIBUS data link layer.
964  */
965 #define LINKTYPE_PROFIBUS_DL		257
966 
967 
968 /*
969  * Apple's DLT_PKTAP headers.
970  *
971  * Sadly, the folks at Apple either had no clue that the DLT_USERn values
972  * are for internal use within an organization and partners only, and
973  * didn't know that the right way to get a link-layer header type is to
974  * ask tcpdump.org for one, or knew and didn't care, so they just
975  * used DLT_USER2, which causes problems for everything except for
976  * their version of tcpdump.
977  *
978  * So I'll just give them one; hopefully this will show up in a
979  * libpcap release in time for them to get this into 10.10 Big Sur
980  * or whatever Mavericks' successor is called.  LINKTYPE_PKTAP
981  * will be 258 *even on OS X*; that is *intentional*, so that
982  * PKTAP files look the same on *all* OSes (different OSes can have
983  * different numerical values for a given DLT_, but *MUST NOT* have
984  * different values for what goes in a file, as files can be moved
985  * between OSes!).
986  */
987 #define LINKTYPE_PKTAP		258
988 
989 /*
990  * Ethernet packets preceded by a header giving the last 6 octets
991  * of the preamble specified by 802.3-2012 Clause 65, section
992  * 65.1.3.2 "Transmit".
993  */
994 #define LINKTYPE_EPON		259
995 
996 /*
997  * IPMI trace packets, as specified by Table 3-20 "Trace Data Block Format"
998  * in the PICMG HPM.2 specification.
999  */
1000 #define LINKTYPE_IPMI_HPM_2	260
1001 
1002 /*
1003  * per  Joshua Wright <jwright@hasborg.com>, formats for Zwave captures.
1004  */
1005 #define LINKTYPE_ZWAVE_R1_R2	261
1006 #define LINKTYPE_ZWAVE_R3	262
1007 
1008 #define LINKTYPE_MATCHING_MAX	262		/* highest value in the "matching" range */
1009 
1010 static struct linktype_map {
1011 	int	dlt;
1012 	int	linktype;
1013 } map[] = {
1014 	/*
1015 	 * These DLT_* codes have LINKTYPE_* codes with values identical
1016 	 * to the values of the corresponding DLT_* code.
1017 	 */
1018 	{ DLT_NULL,		LINKTYPE_NULL },
1019 	{ DLT_EN10MB,		LINKTYPE_ETHERNET },
1020 	{ DLT_EN3MB,		LINKTYPE_EXP_ETHERNET },
1021 	{ DLT_AX25,		LINKTYPE_AX25 },
1022 	{ DLT_PRONET,		LINKTYPE_PRONET },
1023 	{ DLT_CHAOS,		LINKTYPE_CHAOS },
1024 	{ DLT_IEEE802,		LINKTYPE_IEEE802_5 },
1025 	{ DLT_ARCNET,		LINKTYPE_ARCNET_BSD },
1026 	{ DLT_SLIP,		LINKTYPE_SLIP },
1027 	{ DLT_PPP,		LINKTYPE_PPP },
1028 	{ DLT_FDDI,	 	LINKTYPE_FDDI },
1029 	{ DLT_SYMANTEC_FIREWALL, LINKTYPE_SYMANTEC_FIREWALL },
1030 
1031 	/*
1032 	 * These DLT_* codes have different values on different
1033 	 * platforms; we map them to LINKTYPE_* codes that
1034 	 * have values that should never be equal to any DLT_*
1035 	 * code.
1036 	 */
1037 #ifdef DLT_FR
1038 	/* BSD/OS Frame Relay */
1039 	{ DLT_FR,		LINKTYPE_FRELAY },
1040 #endif
1041 
1042 	{ DLT_ATM_RFC1483, 	LINKTYPE_ATM_RFC1483 },
1043 	{ DLT_RAW,		LINKTYPE_RAW },
1044 	{ DLT_SLIP_BSDOS,	LINKTYPE_SLIP_BSDOS },
1045 	{ DLT_PPP_BSDOS,	LINKTYPE_PPP_BSDOS },
1046 
1047 	/* BSD/OS Cisco HDLC */
1048 	{ DLT_C_HDLC,		LINKTYPE_C_HDLC },
1049 
1050 	/*
1051 	 * These DLT_* codes are not on all platforms, but, so far,
1052 	 * there don't appear to be any platforms that define
1053 	 * other codes with those values; we map them to
1054 	 * different LINKTYPE_* values anyway, just in case.
1055 	 */
1056 
1057 	/* Linux ATM Classical IP */
1058 	{ DLT_ATM_CLIP,		LINKTYPE_ATM_CLIP },
1059 
1060 	/* NetBSD sync/async serial PPP (or Cisco HDLC) */
1061 	{ DLT_PPP_SERIAL,	LINKTYPE_PPP_HDLC },
1062 
1063 	/* NetBSD PPP over Ethernet */
1064 	{ DLT_PPP_ETHER,	LINKTYPE_PPP_ETHER },
1065 
1066 	/*
1067 	 * All LINKTYPE_ values between LINKTYPE_MATCHING_MIN
1068 	 * and LINKTYPE_MATCHING_MAX are mapped to identical
1069 	 * DLT_ values.
1070 	 */
1071 
1072 	{ -1,			-1 }
1073 };
1074 
1075 int
dlt_to_linktype(int dlt)1076 dlt_to_linktype(int dlt)
1077 {
1078 	int i;
1079 
1080 	/*
1081 	 * DLTs that, on some platforms, have values in the matching range
1082 	 * but that *don't* have the same value as the corresponding
1083 	 * LINKTYPE because, for some reason, not all OSes have the
1084 	 * same value for that DLT (note that the DLT's value might be
1085 	 * outside the matching range on some of those OSes).
1086 	 */
1087 	if (dlt == DLT_PFSYNC)
1088 		return (LINKTYPE_PFSYNC);
1089 	if (dlt == DLT_PKTAP)
1090 		return (LINKTYPE_PKTAP);
1091 
1092 	/*
1093 	 * For all other values in the matching range, the DLT
1094 	 * value is the same as the LINKTYPE value.
1095 	 */
1096 	if (dlt >= DLT_MATCHING_MIN && dlt <= DLT_MATCHING_MAX)
1097 		return (dlt);
1098 
1099 	/*
1100 	 * Map the values outside that range.
1101 	 */
1102 	for (i = 0; map[i].dlt != -1; i++) {
1103 		if (map[i].dlt == dlt)
1104 			return (map[i].linktype);
1105 	}
1106 
1107 	/*
1108 	 * If we don't have a mapping for this DLT, return an
1109 	 * error; that means that this is a value with no corresponding
1110 	 * LINKTYPE, and we need to assign one.
1111 	 */
1112 	return (-1);
1113 }
1114 
1115 int
linktype_to_dlt(int linktype)1116 linktype_to_dlt(int linktype)
1117 {
1118 	int i;
1119 
1120 	/*
1121 	 * LINKTYPEs in the matching range that *don't*
1122 	 * have the same value as the corresponding DLTs
1123 	 * because, for some reason, not all OSes have the
1124 	 * same value for that DLT.
1125 	 */
1126 	if (linktype == LINKTYPE_PFSYNC)
1127 		return (DLT_PFSYNC);
1128 	if (linktype == LINKTYPE_PKTAP)
1129 		return (DLT_PKTAP);
1130 
1131 	/*
1132 	 * For all other values in the matching range, the LINKTYPE
1133 	 * value is the same as the DLT value.
1134 	 */
1135 	if (linktype >= LINKTYPE_MATCHING_MIN &&
1136 	    linktype <= LINKTYPE_MATCHING_MAX)
1137 		return (linktype);
1138 
1139 	/*
1140 	 * Map the values outside that range.
1141 	 */
1142 	for (i = 0; map[i].linktype != -1; i++) {
1143 		if (map[i].linktype == linktype)
1144 			return (map[i].dlt);
1145 	}
1146 
1147 	/*
1148 	 * If we don't have an entry for this LINKTYPE, return
1149 	 * the link type value; it may be a DLT from an older
1150 	 * version of libpcap.
1151 	 */
1152 	return linktype;
1153 }
1154 
1155 /*
1156  * The DLT_USB_LINUX and DLT_USB_LINUX_MMAPPED headers are in host
1157  * byte order when capturing (it's supplied directly from a
1158  * memory-mapped buffer shared by the kernel).
1159  *
1160  * When reading a DLT_USB_LINUX or DLT_USB_LINUX_MMAPPED capture file,
1161  * we need to convert it from the byte order of the host that wrote
1162  * the file to this host's byte order.
1163  */
1164 static void
swap_linux_usb_header(const struct pcap_pkthdr * hdr,u_char * buf,int header_len_64_bytes)1165 swap_linux_usb_header(const struct pcap_pkthdr *hdr, u_char *buf,
1166     int header_len_64_bytes)
1167 {
1168 	pcap_usb_header_mmapped *uhdr = (pcap_usb_header_mmapped *)buf;
1169 	bpf_u_int32 offset = 0;
1170 
1171 	/*
1172 	 * "offset" is the offset *past* the field we're swapping;
1173 	 * we skip the field *before* checking to make sure
1174 	 * the captured data length includes the entire field.
1175 	 */
1176 
1177 	/*
1178 	 * The URB id is a totally opaque value; do we really need to
1179 	 * convert it to the reading host's byte order???
1180 	 */
1181 	offset += 8;			/* skip past id */
1182 	if (hdr->caplen < offset)
1183 		return;
1184 	uhdr->id = SWAPLL(uhdr->id);
1185 
1186 	offset += 4;			/* skip past various 1-byte fields */
1187 
1188 	offset += 2;			/* skip past bus_id */
1189 	if (hdr->caplen < offset)
1190 		return;
1191 	uhdr->bus_id = SWAPSHORT(uhdr->bus_id);
1192 
1193 	offset += 2;			/* skip past various 1-byte fields */
1194 
1195 	offset += 8;			/* skip past ts_sec */
1196 	if (hdr->caplen < offset)
1197 		return;
1198 	uhdr->ts_sec = SWAPLL(uhdr->ts_sec);
1199 
1200 	offset += 4;			/* skip past ts_usec */
1201 	if (hdr->caplen < offset)
1202 		return;
1203 	uhdr->ts_usec = SWAPLONG(uhdr->ts_usec);
1204 
1205 	offset += 4;			/* skip past status */
1206 	if (hdr->caplen < offset)
1207 		return;
1208 	uhdr->status = SWAPLONG(uhdr->status);
1209 
1210 	offset += 4;			/* skip past urb_len */
1211 	if (hdr->caplen < offset)
1212 		return;
1213 	uhdr->urb_len = SWAPLONG(uhdr->urb_len);
1214 
1215 	offset += 4;			/* skip past data_len */
1216 	if (hdr->caplen < offset)
1217 		return;
1218 	uhdr->data_len = SWAPLONG(uhdr->data_len);
1219 
1220 	if (uhdr->transfer_type == URB_ISOCHRONOUS) {
1221 		offset += 4;			/* skip past s.iso.error_count */
1222 		if (hdr->caplen < offset)
1223 			return;
1224 		uhdr->s.iso.error_count = SWAPLONG(uhdr->s.iso.error_count);
1225 
1226 		offset += 4;			/* skip past s.iso.numdesc */
1227 		if (hdr->caplen < offset)
1228 			return;
1229 		uhdr->s.iso.numdesc = SWAPLONG(uhdr->s.iso.numdesc);
1230 	} else
1231 		offset += 8;			/* skip USB setup header */
1232 
1233 	/*
1234 	 * With the old header, there are no isochronous descriptors
1235 	 * after the header.
1236 	 *
1237 	 * With the new header, the actual number of descriptors in
1238 	 * the header is not s.iso.numdesc, it's ndesc - only the
1239 	 * first N descriptors, for some value of N, are put into
1240 	 * the header, and ndesc is set to the actual number copied.
1241 	 * In addition, if s.iso.numdesc is negative, no descriptors
1242 	 * are captured, and ndesc is set to 0.
1243 	 */
1244 	if (header_len_64_bytes) {
1245 		/*
1246 		 * This is either the "version 1" header, with
1247 		 * 16 bytes of additional fields at the end, or
1248 		 * a "version 0" header from a memory-mapped
1249 		 * capture, with 16 bytes of zeroed-out padding
1250 		 * at the end.  Byte swap them as if this were
1251 		 * a "version 1" header.
1252 		 */
1253 		offset += 4;			/* skip past interval */
1254 		if (hdr->caplen < offset)
1255 			return;
1256 		uhdr->interval = SWAPLONG(uhdr->interval);
1257 
1258 		offset += 4;			/* skip past start_frame */
1259 		if (hdr->caplen < offset)
1260 			return;
1261 		uhdr->start_frame = SWAPLONG(uhdr->start_frame);
1262 
1263 		offset += 4;			/* skip past xfer_flags */
1264 		if (hdr->caplen < offset)
1265 			return;
1266 		uhdr->xfer_flags = SWAPLONG(uhdr->xfer_flags);
1267 
1268 		offset += 4;			/* skip past ndesc */
1269 		if (hdr->caplen < offset)
1270 			return;
1271 		uhdr->ndesc = SWAPLONG(uhdr->ndesc);
1272 
1273 		if (uhdr->transfer_type == URB_ISOCHRONOUS) {
1274 			/* swap the values in struct linux_usb_isodesc */
1275 			usb_isodesc *pisodesc;
1276 			u_int32_t i;
1277 
1278 			pisodesc = (usb_isodesc *)(void *)(buf+offset);
1279 			for (i = 0; i < uhdr->ndesc; i++) {
1280 				offset += 4;		/* skip past status */
1281 				if (hdr->caplen < offset)
1282 					return;
1283 				pisodesc->status = SWAPLONG(pisodesc->status);
1284 
1285 				offset += 4;		/* skip past offset */
1286 				if (hdr->caplen < offset)
1287 					return;
1288 				pisodesc->offset = SWAPLONG(pisodesc->offset);
1289 
1290 				offset += 4;		/* skip past len */
1291 				if (hdr->caplen < offset)
1292 					return;
1293 				pisodesc->len = SWAPLONG(pisodesc->len);
1294 
1295 				offset += 4;		/* skip past padding */
1296 
1297 				pisodesc++;
1298 			}
1299 		}
1300 	}
1301 }
1302 
1303 /*
1304  * The DLT_NFLOG "packets" have a mixture of big-endian and host-byte-order
1305  * data.  They begin with a fixed-length header with big-endian fields,
1306  * followed by a set of TLVs, where the type and length are in host
1307  * byte order but the values are either big-endian or are a raw byte
1308  * sequence that's the same regardless of the host's byte order.
1309  *
1310  * When reading a DLT_NFLOG capture file, we need to convert the type
1311  * and length values from the byte order of the host that wrote the
1312  * file to the byte order of this host.
1313  */
1314 static void
swap_nflog_header(const struct pcap_pkthdr * hdr,u_char * buf)1315 swap_nflog_header(const struct pcap_pkthdr *hdr, u_char *buf)
1316 {
1317 	u_char *p = buf;
1318 	nflog_hdr_t *nfhdr = (nflog_hdr_t *)buf;
1319 	nflog_tlv_t *tlv;
1320 	u_int caplen = hdr->caplen;
1321 	u_int length = hdr->len;
1322 	u_int16_t size;
1323 
1324 	if (caplen < (int) sizeof(nflog_hdr_t) || length < (int) sizeof(nflog_hdr_t)) {
1325 		/* Not enough data to have any TLVs. */
1326 		return;
1327 	}
1328 
1329 	if (nfhdr->nflog_version != 0) {
1330 		/* Unknown NFLOG version */
1331 		return;
1332 	}
1333 
1334 	length -= sizeof(nflog_hdr_t);
1335 	caplen -= sizeof(nflog_hdr_t);
1336 	p += sizeof(nflog_hdr_t);
1337 
1338 	while (caplen >= sizeof(nflog_tlv_t)) {
1339 		tlv = (nflog_tlv_t *) p;
1340 
1341 		/* Swap the type and length. */
1342 		tlv->tlv_type = SWAPSHORT(tlv->tlv_type);
1343 		tlv->tlv_length = SWAPSHORT(tlv->tlv_length);
1344 
1345 		/* Get the length of the TLV. */
1346 		size = tlv->tlv_length;
1347 		if (size % 4 != 0)
1348 			size += 4 - size % 4;
1349 
1350 		/* Is the TLV's length less than the minimum? */
1351 		if (size < sizeof(nflog_tlv_t)) {
1352 			/* Yes. Give up now. */
1353 			return;
1354 		}
1355 
1356 		/* Do we have enough data for the full TLV? */
1357 		if (caplen < size || length < size) {
1358 			/* No. */
1359 			return;
1360 		}
1361 
1362 		/* Skip over the TLV. */
1363 		length -= size;
1364 		caplen -= size;
1365 		p += size;
1366 	}
1367 }
1368 
1369 void
swap_pseudo_headers(int linktype,struct pcap_pkthdr * hdr,u_char * data)1370 swap_pseudo_headers(int linktype, struct pcap_pkthdr *hdr, u_char *data)
1371 {
1372 	/*
1373 	 * Convert pseudo-headers from the byte order of
1374 	 * the host on which the file was saved to our
1375 	 * byte order, as necessary.
1376 	 */
1377 	switch (linktype) {
1378 
1379 	case DLT_USB_LINUX:
1380 		swap_linux_usb_header(hdr, data, 0);
1381 		break;
1382 
1383 	case DLT_USB_LINUX_MMAPPED:
1384 		swap_linux_usb_header(hdr, data, 1);
1385 		break;
1386 
1387 	case DLT_NFLOG:
1388 		swap_nflog_header(hdr, data);
1389 		break;
1390 	}
1391 }
1392