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