AIX's BPF, and thus its tcpdump, appears to use 24 as the link-layer
[obnox/wireshark/wip.git] / wiretap / libpcap.c
1 /* libpcap.c
2  *
3  * $Id: libpcap.c,v 1.95 2003/03/25 06:04:54 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdlib.h>
28 #include <errno.h>
29 #include "wtap-int.h"
30 #include "file_wrappers.h"
31 #include "buffer.h"
32 #include "atm.h"
33 #include "libpcap.h"
34
35 #ifdef HAVE_PCAP_H
36 # ifdef HAVE_SYS_TYPES_H
37 #   include <sys/types.h>
38 # endif
39 #include <pcap.h>
40 #include "wtap-capture.h"
41 #endif
42
43 /*
44  * The link-layer header on ATM packets.
45  */
46 struct sunatm_hdr {
47         guint8  flags;          /* destination and traffic type */
48         guint8  vpi;            /* VPI */
49         guint16 vci;            /* VCI */
50 };
51
52 /* See source to the "libpcap" library for information on the "libpcap"
53    file format. */
54
55 /* On some systems, the FDDI MAC addresses are bit-swapped. */
56 #if !defined(ultrix) && !defined(__alpha) && !defined(__bsdi__)
57 #define BIT_SWAPPED_MAC_ADDRS
58 #endif
59
60 /* Try to read the first two records of the capture file. */
61 typedef enum {
62         THIS_FORMAT,            /* the reads succeeded, assume it's this format */
63         BAD_READ,               /* the file is probably not valid */
64         OTHER_FORMAT            /* the file may be valid, but not in this format */
65 } libpcap_try_t;
66 static libpcap_try_t libpcap_try(wtap *wth, int *err);
67
68 static gboolean libpcap_read(wtap *wth, int *err, long *data_offset);
69 static gboolean libpcap_seek_read(wtap *wth, long seek_off,
70     union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err);
71 static int libpcap_read_header(wtap *wth, int *err,
72     struct pcaprec_ss990915_hdr *hdr, gboolean silent);
73 static void adjust_header(wtap *wth, struct pcaprec_hdr *hdr);
74 static void libpcap_get_atm_pseudoheader(const struct sunatm_hdr *atm_phdr,
75     union wtap_pseudo_header *pseudo_header);
76 static gboolean libpcap_read_atm_pseudoheader(FILE_T fh,
77     union wtap_pseudo_header *pseudo_header, int *err);
78 static gboolean libpcap_read_rec_data(FILE_T fh, guchar *pd, int length,
79     int *err);
80 static void libpcap_close(wtap *wth);
81 static gboolean libpcap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
82     const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err);
83
84 /*
85  * Either LBL NRG wasn't an adequate central registry (e.g., because of
86  * the slow rate of releases from them), or nobody bothered using them
87  * as a central registry, as many different groups have patched libpcap
88  * (and BPF, on the BSDs) to add new encapsulation types, and have ended
89  * up using the same DLT_ values for different encapsulation types.
90  *
91  * For those numerical encapsulation type values that everybody uses for
92  * the same encapsulation type (which inclues those that some platforms
93  * specify different DLT_ names for but don't appear to use), we map
94  * those values to the appropriate Wiretap values.
95  *
96  * For those numerical encapsulation type values that different libpcap
97  * variants use for different encapsulation types, we check what
98  * <pcap.h> defined to determine how to interpret them, so that we
99  * interpret them the way the libpcap with which we're building
100  * Ethereal/Wiretap interprets them (which, if it doesn't support
101  * them at all, means we don't support them either - any capture files
102  * using them are foreign, and we don't hazard a guess as to which
103  * platform they came from; we could, I guess, choose the most likely
104  * platform).
105  */
106
107 static const struct {
108         int     dlt_value;
109         int     wtap_encap_value;
110 } pcap_to_wtap_map[] = {
111         /*
112          * These are the values that are almost certainly the same
113          * in all libpcaps (I've yet to find one where the values
114          * in question are used for some purpose other than the
115          * one below, but...), and that Wiretap and Ethereal
116          * currently support.
117          */
118         { 0,            WTAP_ENCAP_NULL },      /* null encapsulation */
119         { 1,            WTAP_ENCAP_ETHERNET },
120         { 6,            WTAP_ENCAP_TOKEN_RING },        /* IEEE 802 Networks - assume token ring */
121         { 7,            WTAP_ENCAP_ARCNET },
122         { 8,            WTAP_ENCAP_SLIP },
123         { 9,            WTAP_ENCAP_PPP },
124 #ifdef BIT_SWAPPED_MAC_ADDRS
125         { 10,           WTAP_ENCAP_FDDI_BITSWAPPED },
126 #else
127         { 10,           WTAP_ENCAP_FDDI },
128 #endif
129
130         /*
131          * 50 is DLT_PPP_SERIAL in NetBSD; it appears that DLT_PPP
132          * on BSD (at least according to standard tcpdump) has, as
133          * the first octet, an indication of whether the packet was
134          * transmitted or received (rather than having the standard
135          * PPP address value of 0xff), but that DLT_PPP_SERIAL puts
136          * a real live PPP header there, or perhaps a Cisco PPP header
137          * as per section 4.3.1 of RFC 1547 (implementations of this
138          * exist in various BSDs in "sys/net/if_spppsubr.c", and
139          * I think also exist either in standard Linux or in
140          * various Linux patches; the implementations show how to handle
141          * Cisco keepalive packets).
142          *
143          * However, I don't see any obvious place in FreeBSD "if_ppp.c"
144          * where anything other than the standard PPP header would be
145          * passed up.  I see some stuff that sets the first octet
146          * to 0 for incoming and 1 for outgoing packets before applying
147          * a BPF filter to see whether to drop packets whose protocol
148          * field has the 0x8000 bit set, i.e. network control protocols -
149          * those are handed up to userland - but that code puts the
150          * address field back before passing the packet up.
151          *
152          * I also don't see anything immediately obvious that munges
153          * the address field for sync PPP, either.
154          *
155          * Ethereal currently assumes that if the first octet of a
156          * PPP frame is 0xFF, it's the address field and is followed
157          * by a control field and a 2-byte protocol, otherwise the
158          * address and control fields are absent and the frame begins
159          * with a protocol field.  If we ever see a BSD/OS PPP
160          * capture, we'll have to handle it differently, and we may
161          * have to handle standard BSD captures differently if, in fact,
162          * they don't have 0xff 0x03 as the first two bytes - but, as per
163          * the two paragraphs preceding this, it's not clear that
164          * the address field *is* munged into an incoming/outgoing
165          * field when the packet is handed to the BPF device.
166          *
167          * For now, we just map DLT_PPP_SERIAL to WTAP_ENCAP_PPP, as
168          * we treat WTAP_ENCAP_PPP packets as if those beginning with
169          * 0xff have the standard RFC 1662 "PPP in HDLC-like Framing"
170          * 0xff 0x03 address/control header, and DLT_PPP_SERIAL frames
171          * appear to contain that unless they're Cisco frames (if we
172          * ever see a capture with them, we'd need to implement the
173          * RFC 1547 stuff, and the keepalive protocol stuff).
174          *
175          * We may have to distinguish between "PPP where if it doesn't
176          * begin with 0xff there's no HDLC encapsulation and the frame
177          * begins with the protocol field" (which is how we handle
178          * WTAP_ENCAP_PPP now) and "PPP where there's either HDLC
179          * encapsulation or Cisco PPP" (which is what DLT_PPP_SERIAL
180          * is) at some point.
181          *
182          * XXX - NetBSD has DLT_HDLC, which appears to be used for
183          * Cisco HDLC.  Ideally, they should use DLT_PPP_SERIAL
184          * only for real live HDLC-encapsulated PPP, not for Cisco
185          * HDLC.
186          */
187         { 50,           WTAP_ENCAP_PPP },
188
189         /*
190          * These are the values that libpcap 0.5 and later use in
191          * capture file headers, in an attempt to work around the
192          * confusion decried above, and that Wiretap and Ethereal
193          * currently support.
194          */
195         { 100,          WTAP_ENCAP_ATM_RFC1483 },
196         { 101,          WTAP_ENCAP_RAW_IP },
197 #if 0
198         /*
199          * More values used by libpcap 0.5 as DLT_ values and used by the
200          * current CVS version of libpcap in capture file headers.
201          * They are not yet handled in Ethereal.
202          * If we get a capture that contains them, we'll implement them.
203          */
204         { 102,          WTAP_ENCAP_SLIP_BSDOS },
205         { 103,          WTAP_ENCAP_PPP_BSDOS },
206 #endif
207
208         /*
209          * These ones are handled in Ethereal, though.
210          */
211         { 104,          WTAP_ENCAP_CHDLC },     /* Cisco HDLC */
212         { 105,          WTAP_ENCAP_IEEE_802_11 }, /* IEEE 802.11 */
213         { 106,          WTAP_ENCAP_LINUX_ATM_CLIP },
214         { 107,          WTAP_ENCAP_FRELAY },    /* Frame Relay */
215         { 108,          WTAP_ENCAP_NULL },      /* OpenBSD loopback */
216         { 109,          WTAP_ENCAP_ENC },       /* OpenBSD IPSEC enc */
217 #if 0
218         { 110,          WTAP_ENCAP_LANE_802_3 },/* ATM LANE 802.3 */
219         { 111,          WTAP_ENCAP_HIPPI },     /* NetBSD HIPPI */
220 #endif
221         { 112,          WTAP_ENCAP_CHDLC },     /* NetBSD HDLC framing */
222
223         /*
224          * Linux "cooked mode" captures, used by the current CVS version
225          * of libpcap.
226          */
227         { 113,          WTAP_ENCAP_SLL },       /* Linux cooked capture */
228
229         { 114,          WTAP_ENCAP_LOCALTALK }, /* Localtalk */
230
231         /*
232          * The tcpdump.org version of libpcap uses 117, rather than 17,
233          * for OpenBSD packet filter logging, so as to avoid conflicting
234          * with DLT_LANE8023 in SuSE 6.3 libpcap.
235          */
236         { 117,          WTAP_ENCAP_PFLOG },
237
238         { 118,          WTAP_ENCAP_CISCO_IOS },
239         { 119,          WTAP_ENCAP_PRISM_HEADER }, /* Prism monitor mode hdr */
240         { 121,          WTAP_ENCAP_HHDLC },     /* HiPath HDLC */
241         { 122,          WTAP_ENCAP_IP_OVER_FC },   /* RFC 2625 IP-over-FC */
242         { 123,          WTAP_ENCAP_ATM_PDUS },  /* SunATM */
243         { 127,          WTAP_ENCAP_WLAN_HEADER },  /* 802.11 plus WLAN header */
244         { 128,          WTAP_ENCAP_TZSP },      /* Tazmen Sniffer Protocol */
245         { 129,          WTAP_ENCAP_ARCNET_LINUX },
246
247         /*
248          * The following are entries for libpcap type values that have
249          * different meanings on different OSes.
250          *
251          * We put these *after* the entries for the platform-independent
252          * libpcap type values for those Wiretap encapsulation types, so
253          * that Ethereal chooses the platform-independent libpcap type
254          * value for those encapsulatioin types, not the platform-dependent
255          * one.
256          */
257
258         /*
259          * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
260          * seen that define anything other than DLT_ATM_RFC1483 as 11 are
261          * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
262          * which define it as 100, mapping the kernel's value to 100, in
263          * an attempt to hide the different values used on different
264          * platforms.
265          *
266          * If this is a platform where DLT_FR is defined as 11, we
267          * don't handle 11 at all; otherwise, we handle it as
268          * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
269          * captures from BSD/OS if running on platforms other than BSD/OS,
270          * but
271          *
272          *      1) we don't yet support DLT_FR
273          *
274          * and
275          *
276          *      2) nothing short of a heuristic would let us interpret
277          *         them correctly).
278          */
279 #if defined(DLT_FR) && (DLT_FR == 11)
280         { 11,           WTAP_ENCAP_FRELAY },
281 #else
282         { 11,           WTAP_ENCAP_ATM_RFC1483 },
283 #endif
284
285         /*
286          * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
287          * BSD/OS, and DLT_LOOP on OpenBSD.
288          *
289          * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
290          * (it's just like DLT_NULL, only with the AF_ value in network
291          * rather than host byte order - Ethereal figures out the
292          * byte order from the data, so we don't care what byte order
293          * it's in), so if DLT_LOOP is defined as 12, interpret 12
294          * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
295          * as 12, interpret it as WTAP_ENCAP_RAW_IP.
296          */
297 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
298         { 12,           WTAP_ENCAP_NULL },
299 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
300         /*
301          * Put entry for Cisco HDLC here.
302          * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
303          * start with a 4-byte Cisco HDLC header?
304          */
305 #else
306         { 12,           WTAP_ENCAP_RAW_IP },
307 #endif
308
309         /*
310          * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
311          * don't actually generate it.  I infer that BSD/OS translates
312          * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
313          * libpcap, as the BSD/OS link-layer header is different;
314          * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
315          *
316          * From this, I infer that there's no point in handling 13
317          * as DLT_SLIP_BSDOS.
318          *
319          * 13 is DLT_ATM_RFC1483 on BSD/OS.
320          *
321          * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
322          * of decrypted IPSEC traffic.
323          */
324 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
325         { 13,           WTAP_ENCAP_ATM_RFC1483 },
326 #elif defined(DLT_ENC) && (DLT_ENC == 13)
327         { 13,           WTAP_ENCAP_ENC },
328 #endif
329
330         /*
331          * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
332          * don't actually generate it.  I infer that BSD/OS translates
333          * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
334          * libpcap, as the BSD/OS link-layer header is different;
335          * however, in BSD/OS, DLT_PPP_BSDOS is 16.
336          *
337          * From this, I infer that there's no point in handling 14
338          * as DLT_PPP_BSDOS.
339          *
340          * 14 is DLT_RAW on BSD/OS and OpenBSD.
341          */
342         { 14,           WTAP_ENCAP_RAW_IP },
343
344         /*
345          * 15 is:
346          *
347          *      DLT_SLIP_BSDOS on BSD/OS;
348          *
349          *      DLT_HIPPI on NetBSD;
350          *
351          *      DLT_LANE8023 with Alexey Kuznetzov's patches for
352          *      Linux libpcap;
353          *
354          *      DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
355          *      (and on SuSE 6.3);
356          *
357          * but we don't currently handle any of those.
358          */
359
360         /*
361          * 16 is:
362          *
363          *      DLT_PPP_BSDOS on BSD/OS;
364          *
365          *      DLT_HDLC on NetBSD (Cisco HDLC);
366          *
367          *      DLT_CIP with Alexey Kuznetzov's patches for
368          *      Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
369          *
370          *      DLT_I4L_IP with the ISDN4Linux patches for libpcap
371          *      (and on SuSE 6.3).
372          */
373 #if defined(DLT_CIP) && (DLT_CIP == 16)
374         { 16,           WTAP_ENCAP_LINUX_ATM_CLIP },
375 #endif
376 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
377         { 16,           WTAP_ENCAP_CHDLC },
378 #endif
379
380         /*
381          * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
382          * handle it.
383          * It is also used as the PF (Packet Filter) logging format beginning
384          * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
385          * defined with the value 17.
386          */
387 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
388         { 17,           WTAP_ENCAP_PFLOG },
389 #endif
390
391         /*
392          * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
393          * DLT_CIP of 16 that the Alexey Kuznetzov patches for
394          * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
395          * I've not found any libpcap that uses it for any other purpose -
396          * hopefully nobody will do so in the future.
397          */
398         { 18,           WTAP_ENCAP_LINUX_ATM_CLIP },
399
400         /*
401          * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
402          * recent versions I've seen of the Linux ATM distribution;
403          * I've not yet found any libpcap that uses it for any other
404          * purpose - hopefully nobody will do so in the future.
405          */
406         { 19,           WTAP_ENCAP_LINUX_ATM_CLIP },
407 };
408 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
409
410 int wtap_pcap_encap_to_wtap_encap(int encap)
411 {
412         unsigned int i;
413
414         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
415                 if (pcap_to_wtap_map[i].dlt_value == encap)
416                         return pcap_to_wtap_map[i].wtap_encap_value;
417         }
418         return WTAP_ENCAP_UNKNOWN;
419 }
420
421
422 int libpcap_open(wtap *wth, int *err)
423 {
424         int bytes_read;
425         guint32 magic;
426         struct pcap_hdr hdr;
427         gboolean byte_swapped;
428         gboolean modified;
429         gboolean aix;
430         int file_encap;
431
432         /* Read in the number that should be at the start of a "libpcap" file */
433         errno = WTAP_ERR_CANT_READ;
434         bytes_read = file_read(&magic, 1, sizeof magic, wth->fh);
435         if (bytes_read != sizeof magic) {
436                 *err = file_error(wth->fh);
437                 if (*err != 0)
438                         return -1;
439                 return 0;
440         }
441         wth->data_offset += sizeof magic;
442
443         switch (magic) {
444
445         case PCAP_MAGIC:
446                 /* Host that wrote it has our byte order, and was running
447                    a program using either standard or ss990417 libpcap. */
448                 byte_swapped = FALSE;
449                 modified = FALSE;
450                 break;
451
452         case PCAP_MODIFIED_MAGIC:
453                 /* Host that wrote it has our byte order, and was running
454                    a program using either ss990915 or ss991029 libpcap. */
455                 byte_swapped = FALSE;
456                 modified = TRUE;
457                 break;
458
459         case PCAP_SWAPPED_MAGIC:
460                 /* Host that wrote it has a byte order opposite to ours,
461                    and was running a program using either standard or
462                    ss990417 libpcap. */
463                 byte_swapped = TRUE;
464                 modified = FALSE;
465                 break;
466
467         case PCAP_SWAPPED_MODIFIED_MAGIC:
468                 /* Host that wrote it out has a byte order opposite to
469                    ours, and was running a program using either ss990915
470                    or ss991029 libpcap. */
471                 byte_swapped = TRUE;
472                 modified = TRUE;
473                 break;
474
475         default:
476                 /* Not a "libpcap" type we know about. */
477                 return 0;
478         }
479
480         /* Read the rest of the header. */
481         errno = WTAP_ERR_CANT_READ;
482         bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
483         if (bytes_read != sizeof hdr) {
484                 *err = file_error(wth->fh);
485                 if (*err != 0)
486                         return -1;
487                 return 0;
488         }
489         wth->data_offset += sizeof hdr;
490
491         if (byte_swapped) {
492                 /* Byte-swap the header fields about which we care. */
493                 hdr.version_major = BSWAP16(hdr.version_major);
494                 hdr.version_minor = BSWAP16(hdr.version_minor);
495                 hdr.snaplen = BSWAP32(hdr.snaplen);
496                 hdr.network = BSWAP32(hdr.network);
497         }
498         if (hdr.version_major < 2) {
499                 /* We only support version 2.0 and later. */
500                 g_message("pcap: major version %u unsupported",
501                     hdr.version_major);
502                 *err = WTAP_ERR_UNSUPPORTED;
503                 return -1;
504         }
505
506         /*
507          * AIX's non-standard tcpdump uses a minor version number of 2.
508          * Unfortunately, older versions of libpcap might have used
509          * that as well.
510          *
511          * The AIX libpcap uses RFC 1573 ifType values rather than
512          * DLT_ values in the header; the ifType values for LAN devices
513          * are:
514          *
515          *      Ethernet        6
516          *      Token Ring      9
517          *      FDDI            15
518          *
519          * which correspond to DLT_IEEE802 (used for Token Ring),
520          * DLT_PPP, and DLT_SLIP_BSDOS, respectively.  The ifType value
521          * for a loopback interface is 24, which currently isn't
522          * used by any version of libpcap I know about (and, as
523          * tcpdump.org are assigning DLT_ values above 100, and
524          * NetBSD started assigning values starting at 50, and
525          * the values chosen by other libpcaps appear to stop at
526          * 19, it's probably not going to be used by any libpcap
527          * in the future).
528          *
529          * We shall assume that if the minor version number is 2, and
530          * the network type is 6, 9, 15, or 24, that it's AIX libpcap.
531          *
532          * I'm assuming those older versions of libpcap didn't
533          * use DLT_IEEE802 for Token Ring, and didn't use DLT_SLIP_BSDOS
534          * as that came later.  It may have used DLT_PPP, however, in
535          * which case we're out of luck; we assume it's Token Ring
536          * in AIX libpcap rather than PPP in standard libpcap, as
537          * you're probably more likely to be handing an AIX libpcap
538          * token-ring capture than an old (pre-libpcap 0.4) PPP capture
539          * to Ethereal.
540          */
541         aix = FALSE;    /* assume it's not AIX */
542         if (hdr.version_major == 2 && hdr.version_minor == 2) {
543                 switch (hdr.network) {
544
545                 case 6:
546                         hdr.network = 1;        /* DLT_EN10MB, Ethernet */
547                         aix = TRUE;
548                         break;
549
550                 case 9:
551                         hdr.network = 6;        /* DLT_IEEE802, Token Ring */
552                         aix = TRUE;
553                         break;
554
555                 case 15:
556                         hdr.network = 10;       /* DLT_FDDI, FDDI */
557                         aix = TRUE;
558                         break;
559
560                 case 24:
561                         hdr.network = 0;        /* DLT_NULL, loopback */
562                         aix = TRUE;
563                         break;
564                 }
565         }
566         file_encap = wtap_pcap_encap_to_wtap_encap(hdr.network);
567         if (file_encap == WTAP_ENCAP_UNKNOWN) {
568                 g_message("pcap: network type %u unknown or unsupported",
569                     hdr.network);
570                 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
571                 return -1;
572         }
573
574         /* This is a libpcap file */
575         wth->capture.pcap = g_malloc(sizeof(libpcap_t));
576         wth->capture.pcap->byte_swapped = byte_swapped;
577         wth->capture.pcap->version_major = hdr.version_major;
578         wth->capture.pcap->version_minor = hdr.version_minor;
579         wth->subtype_read = libpcap_read;
580         wth->subtype_seek_read = libpcap_seek_read;
581         wth->subtype_close = libpcap_close;
582         wth->file_encap = file_encap;
583         wth->snapshot_length = hdr.snaplen;
584
585         /*
586          * Is this AIX format?
587          */
588         if (aix) {
589                 /*
590                  * Yes.  Skip all the tests for other mutant formats.
591                  */
592                 wth->file_type = WTAP_FILE_PCAP_AIX;
593                 return 1;
594         }
595
596         /*
597          * No.  Let's look at the header for the first record,
598          * and see if, interpreting it as a standard header (if the
599          * magic number was standard) or a modified header (if the
600          * magic number was modified), the position where it says the
601          * header for the *second* record is contains a corrupted header.
602          *
603          * If so, then:
604          *
605          *      If this file had the standard magic number, it may be
606          *      an ss990417 capture file - in that version of Alexey's
607          *      patch, the packet header format was changed but the
608          *      magic number wasn't, and, alas, Red Hat appear to have
609          *      picked up that version of the patch for RH 6.1, meaning
610          *      RH 6.1 has a tcpdump that writes out files that can't
611          *      be read by any software that expects non-modified headers
612          *      if the magic number isn't the modified magic number (e.g.,
613          *      any normal version of tcpdump, and Ethereal if we don't
614          *      do this gross heuristic).
615          *
616          *      If this file had the modified magic number, it may be
617          *      an ss990915 capture file - in that version of Alexey's
618          *      patch, the magic number was changed, but the record
619          *      header had some extra fields, and, alas, SuSE appear
620          *      to have picked up that version of the patch for SuSE
621          *      6.3, meaning that programs expecting the standard per-
622          *      packet header in captures with the modified magic number
623          *      can't read dumps from its tcpdump.
624          *
625          * Oh, and if it has the standard magic number, it might, instead,
626          * be a Nokia libpcap file, so we may need to try that if
627          * neither normal nor ss990417 headers work.
628          */
629         if (modified) {
630                 /*
631                  * Well, we have the magic number from Alexey's
632                  * later two patches.
633                  *
634                  * Try ss991029, the last of his patches, first.
635                  */
636                 wth->file_type = WTAP_FILE_PCAP_SS991029;
637                 switch (libpcap_try(wth, err)) {
638
639                 case BAD_READ:
640                         /*
641                          * Well, we couldn't even read it.
642                          * Give up.
643                          */
644                         g_free(wth->capture.pcap);
645                         return -1;
646
647                 case THIS_FORMAT:
648                         /*
649                          * Well, it looks as if it might be 991029.
650                          * Put the seek pointer back, and return success.
651                          */
652                         if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
653                                 g_free(wth->capture.pcap);
654                                 return -1;
655                         }
656                         return 1;
657
658                 case OTHER_FORMAT:
659                         /*
660                          * Try the next format.
661                          */
662                         break;
663                 }
664
665                 /*
666                  * Well, it's not completely unreadable,
667                  * but it's not ss991029.  Try ss990915;
668                  * there are no other types to try after that,
669                  * so we put the seek pointer back and treat
670                  * it as 990915.
671                  */
672                 wth->file_type = WTAP_FILE_PCAP_SS990915;
673                 if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
674                         g_free(wth->capture.pcap);
675                         return -1;
676                 }
677         } else {
678                 /*
679                  * Well, we have the standard magic number.
680                  *
681                  * Try the standard format first.
682                  */
683                 wth->file_type = WTAP_FILE_PCAP;
684                 switch (libpcap_try(wth, err)) {
685
686                 case BAD_READ:
687                         /*
688                          * Well, we couldn't even read it.
689                          * Give up.
690                          */
691                         g_free(wth->capture.pcap);
692                         return -1;
693
694                 case THIS_FORMAT:
695                         /*
696                          * Well, it looks as if it might be a standard
697                          * libpcap file.
698                          * Put the seek pointer back, and return success.
699                          */
700                         if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
701                                 g_free(wth->capture.pcap);
702                                 return -1;
703                         }
704                         return 1;
705
706                 case OTHER_FORMAT:
707                         /*
708                          * Try the next format.
709                          */
710                         break;
711                 }
712
713                 /*
714                  * Well, it's not completely unreadable, but it's not
715                  * a standard file.  Put the seek pointer back and try
716                  * ss990417.
717                  */
718                 wth->file_type = WTAP_FILE_PCAP_SS990417;
719                 if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
720                         g_free(wth->capture.pcap);
721                         return -1;
722                 }
723                 switch (libpcap_try(wth, err)) {
724
725                 case BAD_READ:
726                         /*
727                          * Well, we couldn't even read it.
728                          * Give up.
729                          */
730                         g_free(wth->capture.pcap);
731                         return -1;
732
733                 case THIS_FORMAT:
734                         /*
735                          * Well, it looks as if it might be ss990417.
736                          * Put the seek pointer back, and return success.
737                          */
738                         if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
739                                 g_free(wth->capture.pcap);
740                                 return -1;
741                         }
742                         return 1;
743
744                 case OTHER_FORMAT:
745                         /*
746                          * Try the next format.
747                          */
748                         break;
749                 }
750
751                 /*
752                  * Well, it's not completely unreadable,
753                  * but it's not a standard file *nor* is it ss990417.
754                  * Try it as a Nokia file; there are no other types
755                  * to try after that, so we put the seek pointer back
756                  * and treat it as a Nokia file.
757                  */
758                 wth->file_type = WTAP_FILE_PCAP_NOKIA;
759                 if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
760                         g_free(wth->capture.pcap);
761                         return -1;
762                 }
763         }
764
765         return 1;
766 }
767
768 /* Try to read the first two records of the capture file. */
769 static libpcap_try_t libpcap_try(wtap *wth, int *err)
770 {
771         /*
772          * pcaprec_ss990915_hdr is the largest header type.
773          */
774         struct pcaprec_ss990915_hdr first_rec_hdr, second_rec_hdr;
775
776         /*
777          * Attempt to read the first record's header.
778          */
779         if (libpcap_read_header(wth, err, &first_rec_hdr, TRUE) == -1) {
780                 if (*err == 0 || *err == WTAP_ERR_SHORT_READ) {
781                         /*
782                          * EOF or short read - assume the file is in this
783                          * format.
784                          * When our client tries to read the first packet
785                          * they will presumably get the same EOF or short
786                          * read.
787                          */
788                         return THIS_FORMAT;
789                 }
790
791                 if (*err == WTAP_ERR_BAD_RECORD) {
792                         /*
793                          * The first record is bogus, so this is probably
794                          * a corrupt file.  Assume the file is in this
795                          * format.  When our client tries to read the
796                          * first packet they will presumably get the
797                          * same bogus record.
798                          */
799                         return THIS_FORMAT;
800                 }
801
802                 /*
803                  * Some other error, e.g. an I/O error; just give up.
804                  */
805                 return BAD_READ;
806         }
807
808         /*
809          * Now skip over the first record's data, under the assumption
810          * that the header is sane.
811          */
812         if (file_seek(wth->fh, first_rec_hdr.hdr.incl_len, SEEK_CUR, err) == -1)
813                 return BAD_READ;
814
815         /*
816          * Now attempt to read the second record's header.
817          */
818         if (libpcap_read_header(wth, err, &second_rec_hdr, TRUE) == -1) {
819                 if (*err == 0 || *err == WTAP_ERR_SHORT_READ) {
820                         /*
821                          * EOF or short read - assume the file is in this
822                          * format.
823                          * When our client tries to read the second packet
824                          * they will presumably get the same EOF or short
825                          * read.
826                          */
827                         return THIS_FORMAT;
828                 }
829
830                 if (*err == WTAP_ERR_BAD_RECORD) {
831                         /*
832                          * The second record is bogus; maybe it's a
833                          * Capture File From Hell, and what looks like
834                          * the "header" of the next packet is actually
835                          * random junk from the middle of a packet.
836                          * Try the next format; if we run out of formats,
837                          * it probably *is* a corrupt file.
838                          */
839                         return OTHER_FORMAT;
840                 }
841
842                 /*
843                  * Some other error, e.g. an I/O error; just give up.
844                  */
845                 return BAD_READ;
846         }
847
848         /*
849          * OK, the first two records look OK; assume this is the
850          * right format.
851          */
852         return THIS_FORMAT;
853 }
854
855 /* Read the next packet */
856 static gboolean libpcap_read(wtap *wth, int *err, long *data_offset)
857 {
858         struct pcaprec_ss990915_hdr hdr;
859         guint packet_size;
860         guint orig_size;
861         int bytes_read;
862         char fddi_padding[3];
863
864         bytes_read = libpcap_read_header(wth, err, &hdr, FALSE);
865         if (bytes_read == -1) {
866                 /*
867                  * We failed to read the header.
868                  */
869                 return FALSE;
870         }
871
872         wth->data_offset += bytes_read;
873         packet_size = hdr.hdr.incl_len;
874         orig_size = hdr.hdr.orig_len;
875
876         /*
877          * AIX appears to put 3 bytes of padding in front of FDDI
878          * frames; strip that crap off.
879          */
880         if (wth->file_type == WTAP_FILE_PCAP_AIX &&
881             (wth->file_encap == WTAP_ENCAP_FDDI ||
882              wth->file_encap == WTAP_ENCAP_FDDI_BITSWAPPED)) {
883                 /*
884                  * The packet size is really a record size and includes
885                  * the padding.
886                  */
887                 packet_size -= 3;
888                 orig_size -= 3;
889                 wth->data_offset += 3;
890
891                 /*
892                  * Read the padding.
893                  */
894                 if (!libpcap_read_rec_data(wth->fh, fddi_padding, 3, err))
895                         return FALSE;   /* Read error */
896         }
897
898         *data_offset = wth->data_offset;
899
900         /*
901          * If this is an ATM packet, the first four bytes are the
902          * direction of the packet (transmit/receive), the VPI, and
903          * the VCI; read them and generate the pseudo-header from
904          * them.
905          */
906         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS) {
907                 if (packet_size < sizeof (struct sunatm_hdr)) {
908                         /*
909                          * Uh-oh, the packet isn't big enough to even
910                          * have a pseudo-header.
911                          */
912                         g_message("libpcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header\n",
913                             packet_size);
914                         *err = WTAP_ERR_BAD_RECORD;
915                         return FALSE;
916                 }
917                 if (!libpcap_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
918                     err))
919                         return FALSE;   /* Read error */
920
921                 /*
922                  * Don't count the pseudo-header as part of the packet.
923                  */
924                 orig_size -= sizeof (struct sunatm_hdr);
925                 packet_size -= sizeof (struct sunatm_hdr);
926                 wth->data_offset += sizeof (struct sunatm_hdr);
927         }
928
929         buffer_assure_space(wth->frame_buffer, packet_size);
930         if (!libpcap_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
931             packet_size, err))
932                 return FALSE;   /* Read error */
933         wth->data_offset += packet_size;
934
935         wth->phdr.ts.tv_sec = hdr.hdr.ts_sec;
936         wth->phdr.ts.tv_usec = hdr.hdr.ts_usec;
937         wth->phdr.caplen = packet_size;
938         wth->phdr.len = orig_size;
939         wth->phdr.pkt_encap = wth->file_encap;
940
941         /*
942          * If this is ATM LANE traffic, try to guess what type of LANE
943          * traffic it is based on the packet contents.
944          */
945         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
946             wth->pseudo_header.atm.type == TRAF_LANE) {
947                 atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
948                     wth->phdr.caplen, &wth->pseudo_header);
949         }
950
951         return TRUE;
952 }
953
954 static gboolean
955 libpcap_seek_read(wtap *wth, long seek_off,
956     union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err)
957 {
958         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
959                 return FALSE;
960
961         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS) {
962                 if (!libpcap_read_atm_pseudoheader(wth->random_fh, pseudo_header,
963                     err)) {
964                         /* Read error */
965                         return FALSE;
966                 }
967         }
968
969         /*
970          * Read the packet data.
971          */
972         if (!libpcap_read_rec_data(wth->random_fh, pd, length, err))
973                 return FALSE;   /* failed */
974
975         /*
976          * If this is ATM LANE traffic, try to guess what type of LANE
977          * traffic it is based on the packet contents.
978          */
979         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
980             pseudo_header->atm.type == TRAF_LANE)
981                 atm_guess_lane_type(pd, length, pseudo_header);
982         return TRUE;
983 }
984
985 /* Read the header of the next packet; if "silent" is TRUE, don't complain
986    to the console, as we're testing to see if the file appears to be of a
987    particular type.
988
989    Return -1 on an error, or the number of bytes of header read on success. */
990 static int libpcap_read_header(wtap *wth, int *err,
991     struct pcaprec_ss990915_hdr *hdr, gboolean silent)
992 {
993         int     bytes_to_read, bytes_read;
994
995         /* Read record header. */
996         errno = WTAP_ERR_CANT_READ;
997         switch (wth->file_type) {
998
999         case WTAP_FILE_PCAP:
1000         case WTAP_FILE_PCAP_AIX:
1001                 bytes_to_read = sizeof (struct pcaprec_hdr);
1002                 break;
1003
1004         case WTAP_FILE_PCAP_SS990417:
1005         case WTAP_FILE_PCAP_SS991029:
1006                 bytes_to_read = sizeof (struct pcaprec_modified_hdr);
1007                 break;
1008
1009         case WTAP_FILE_PCAP_SS990915:
1010                 bytes_to_read = sizeof (struct pcaprec_ss990915_hdr);
1011                 break;
1012
1013         case WTAP_FILE_PCAP_NOKIA:
1014                 bytes_to_read = sizeof (struct pcaprec_nokia_hdr);
1015                 break;
1016
1017         default:
1018                 g_assert_not_reached();
1019                 bytes_to_read = 0;
1020         }
1021         bytes_read = file_read(hdr, 1, bytes_to_read, wth->fh);
1022         if (bytes_read != bytes_to_read) {
1023                 *err = file_error(wth->fh);
1024                 if (*err == 0 && bytes_read != 0) {
1025                         *err = WTAP_ERR_SHORT_READ;
1026                 }
1027                 return -1;
1028         }
1029
1030         adjust_header(wth, &hdr->hdr);
1031
1032         if (hdr->hdr.incl_len > WTAP_MAX_PACKET_SIZE) {
1033                 /*
1034                  * Probably a corrupt capture file; return an error,
1035                  * so that our caller doesn't blow up trying to allocate
1036                  * space for an immensely-large packet, and so that
1037                  * the code to try to guess what type of libpcap file
1038                  * this is can tell when it's not the type we're guessing
1039                  * it is.
1040                  */
1041                 if (!silent) {
1042                         g_message("pcap: File has %u-byte packet, bigger than maximum of %u",
1043                             hdr->hdr.incl_len, WTAP_MAX_PACKET_SIZE);
1044                 }
1045                 *err = WTAP_ERR_BAD_RECORD;
1046                 return -1;
1047         }
1048
1049         if (hdr->hdr.orig_len > WTAP_MAX_PACKET_SIZE) {
1050                 /*
1051                  * Probably a corrupt capture file; return an error,
1052                  * so that our caller doesn't blow up trying to
1053                  * cope with a huge "real" packet length, and so that
1054                  * the code to try to guess what type of libpcap file
1055                  * this is can tell when it's not the type we're guessing
1056                  * it is.
1057                  */
1058                 if (!silent) {
1059                         g_message("pcap: File has %u-byte packet, bigger than maximum of %u",
1060                             hdr->hdr.orig_len, WTAP_MAX_PACKET_SIZE);
1061                 }
1062                 *err = WTAP_ERR_BAD_RECORD;
1063                 return -1;
1064         }
1065
1066         return bytes_read;
1067 }
1068
1069 static void
1070 adjust_header(wtap *wth, struct pcaprec_hdr *hdr)
1071 {
1072         if (wth->capture.pcap->byte_swapped) {
1073                 /* Byte-swap the record header fields. */
1074                 hdr->ts_sec = BSWAP32(hdr->ts_sec);
1075                 hdr->ts_usec = BSWAP32(hdr->ts_usec);
1076                 hdr->incl_len = BSWAP32(hdr->incl_len);
1077                 hdr->orig_len = BSWAP32(hdr->orig_len);
1078         }
1079
1080         /* If this is AIX, convert the time stamp from seconds/nanoseconds
1081            to seconds/microseconds.  */
1082         if (wth->file_type == WTAP_FILE_PCAP_AIX)
1083                 hdr->ts_usec = hdr->ts_usec/1000;
1084
1085         /* In file format version 2.3, the "incl_len" and "orig_len" fields
1086            were swapped, in order to match the BPF header layout.
1087
1088            Unfortunately, some files were, according to a comment in the
1089            "libpcap" source, written with version 2.3 in their headers
1090            but without the interchanged fields, so if "incl_len" is
1091            greater than "orig_len" - which would make no sense - we
1092            assume that we need to swap them.  */
1093         if (wth->capture.pcap->version_major == 2 &&
1094             (wth->capture.pcap->version_minor < 3 ||
1095              (wth->capture.pcap->version_minor == 3 &&
1096               hdr->incl_len > hdr->orig_len))) {
1097                 guint32 temp;
1098
1099                 temp = hdr->orig_len;
1100                 hdr->orig_len = hdr->incl_len;
1101                 hdr->incl_len = temp;
1102         }
1103 }
1104
1105 static void
1106 libpcap_get_atm_pseudoheader(const struct sunatm_hdr *atm_phdr,
1107     union wtap_pseudo_header *pseudo_header)
1108 {
1109         guint8  vpi;
1110         guint16 vci;
1111
1112         vpi = atm_phdr->vpi;
1113         vci = pntohs(&atm_phdr->vci);
1114
1115         /*
1116          * The lower 4 bits of the first byte of the header indicate
1117          * the type of traffic, as per the "atmioctl.h" header in
1118          * SunATM.
1119          */
1120         switch (atm_phdr->flags & 0x0F) {
1121
1122         case 0x01:      /* LANE */
1123                 pseudo_header->atm.aal = AAL_5;
1124                 pseudo_header->atm.type = TRAF_LANE;
1125                 break;
1126
1127         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
1128                 pseudo_header->atm.aal = AAL_5;
1129                 pseudo_header->atm.type = TRAF_LLCMX;
1130                 break;
1131
1132         case 0x05:      /* ILMI */
1133                 pseudo_header->atm.aal = AAL_5;
1134                 pseudo_header->atm.type = TRAF_ILMI;
1135                 break;
1136
1137         case 0x06:      /* Q.2931 */
1138                 pseudo_header->atm.aal = AAL_SIGNALLING;
1139                 pseudo_header->atm.type = TRAF_UNKNOWN;
1140                 break;
1141
1142         case 0x03:      /* MARS (RFC 2022) */
1143                 pseudo_header->atm.aal = AAL_5;
1144                 pseudo_header->atm.type = TRAF_UNKNOWN;
1145                 break;
1146
1147         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
1148                 pseudo_header->atm.aal = AAL_5;
1149                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
1150                 break;
1151
1152         default:
1153                 /*
1154                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
1155                  * case assume it's AAL_SIGNALLING; we know nothing more
1156                  * about it.
1157                  *
1158                  * XXX - is this necessary?  Or are we guaranteed that
1159                  * all signalling traffic has a type of 0x06?
1160                  *
1161                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
1162                  * 0x00 ("raw"), might it be non-AAL5 traffic?
1163                  */
1164                 if (vpi == 0 && vci == 5)
1165                         pseudo_header->atm.aal = AAL_SIGNALLING;
1166                 else
1167                         pseudo_header->atm.aal = AAL_5;
1168                 pseudo_header->atm.type = TRAF_UNKNOWN;
1169                 break;
1170         }
1171         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1172
1173         pseudo_header->atm.vpi = vpi;
1174         pseudo_header->atm.vci = vci;
1175         pseudo_header->atm.channel = (atm_phdr->flags & 0x80) ? 0 : 1;
1176
1177         /* We don't have this information */
1178         pseudo_header->atm.flags = 0;
1179         pseudo_header->atm.cells = 0;
1180         pseudo_header->atm.aal5t_u2u = 0;
1181         pseudo_header->atm.aal5t_len = 0;
1182         pseudo_header->atm.aal5t_chksum = 0;
1183 }
1184
1185 static gboolean
1186 libpcap_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1187     int *err)
1188 {
1189         struct sunatm_hdr atm_phdr;
1190         int     bytes_read;
1191
1192         errno = WTAP_ERR_CANT_READ;
1193         bytes_read = file_read(&atm_phdr, 1, sizeof (struct sunatm_hdr), fh);
1194         if (bytes_read != sizeof (struct sunatm_hdr)) {
1195                 *err = file_error(fh);
1196                 if (*err == 0)
1197                         *err = WTAP_ERR_SHORT_READ;
1198                 return FALSE;
1199         }
1200
1201         libpcap_get_atm_pseudoheader(&atm_phdr, pseudo_header);
1202
1203         return TRUE;
1204 }
1205
1206 static gboolean
1207 libpcap_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
1208 {
1209         int     bytes_read;
1210
1211         errno = WTAP_ERR_CANT_READ;
1212         bytes_read = file_read(pd, 1, length, fh);
1213
1214         if (bytes_read != length) {
1215                 *err = file_error(fh);
1216                 if (*err == 0)
1217                         *err = WTAP_ERR_SHORT_READ;
1218                 return FALSE;
1219         }
1220         return TRUE;
1221 }
1222
1223 static void
1224 libpcap_close(wtap *wth)
1225 {
1226         g_free(wth->capture.pcap);
1227 }
1228
1229 static int wtap_wtap_encap_to_pcap_encap(int encap)
1230 {
1231         unsigned int i;
1232
1233         switch (encap) {
1234
1235         case WTAP_ENCAP_FDDI:
1236         case WTAP_ENCAP_FDDI_BITSWAPPED:
1237                 /*
1238                  * Special-case WTAP_ENCAP_FDDI and
1239                  * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
1240                  * to DLT_FDDI (even though that may mean that the bit
1241                  * order in the FDDI MAC addresses is wrong; so it goes
1242                  * - libpcap format doesn't record the byte order,
1243                  * so that's not fixable).
1244                  */
1245                 return 10;      /* that's DLT_FDDI */
1246
1247         case WTAP_ENCAP_PPP_WITH_PHDR:
1248                 /*
1249                  * Also special-case PPP and Frame Relay with direction
1250                  * bits; map them to PPP and Frame Relay, even though
1251                  * that means that the direction of the packet is lost.
1252                  */
1253                 return 9;
1254
1255         case WTAP_ENCAP_FRELAY_WITH_PHDR:
1256                 return 107;
1257         }
1258
1259         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
1260                 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
1261                         return pcap_to_wtap_map[i].dlt_value;
1262         }
1263         return -1;
1264 }
1265
1266 #ifdef HAVE_PCAP_H
1267 /*
1268  * Given a Wiretap encapsulation type, and raw packet data and the packet
1269  * header from libpcap, process any pseudo-header in the packet,
1270  * fill in the Wiretap packet header, and return a pointer to the
1271  * beginning of the non-pseudo-header data in the packet.
1272  */
1273 const guchar *
1274 wtap_process_pcap_packet(gint linktype, const struct pcap_pkthdr *phdr,
1275     const guchar *pd, union wtap_pseudo_header *pseudo_header,
1276     struct wtap_pkthdr *whdr, int *err)
1277 {
1278         /* "phdr->ts" may not necessarily be a "struct timeval" - it may
1279            be a "struct bpf_timeval", with member sizes wired to 32
1280            bits - and we may go that way ourselves in the future, so
1281            copy the members individually. */
1282         whdr->ts.tv_sec = phdr->ts.tv_sec;
1283         whdr->ts.tv_usec = phdr->ts.tv_usec;
1284         whdr->caplen = phdr->caplen;
1285         whdr->len = phdr->len;
1286         whdr->pkt_encap = linktype;
1287
1288         /*
1289          * If this is an ATM packet, the first four bytes are the
1290          * direction of the packet (transmit/receive), the VPI, and
1291          * the VCI; read them and generate the pseudo-header from
1292          * them.
1293          */
1294         if (linktype == WTAP_ENCAP_ATM_PDUS) {
1295                 if (whdr->caplen < sizeof (struct sunatm_hdr)) {
1296                         /*
1297                          * Uh-oh, the packet isn't big enough to even
1298                          * have a pseudo-header.
1299                          */
1300                         g_message("libpcap: SunATM capture has a %u-byte packet, too small to have even an ATM pseudo-header\n",
1301                             whdr->caplen);
1302                         *err = WTAP_ERR_BAD_RECORD;
1303                         return NULL;
1304                 }
1305                 libpcap_get_atm_pseudoheader((const struct sunatm_hdr *)pd,
1306                     pseudo_header);
1307
1308                 /*
1309                  * Don't count the pseudo-header as part of the packet.
1310                  */
1311                 whdr->len -= sizeof (struct sunatm_hdr);
1312                 whdr->caplen -= sizeof (struct sunatm_hdr);
1313                 pd += sizeof (struct sunatm_hdr);
1314
1315                 /*
1316                  * If this is ATM LANE traffic, try to guess what type of
1317                  * LANE traffic it is based on the packet contents.
1318                  */
1319                 if (pseudo_header->atm.type == TRAF_LANE)
1320                         atm_guess_lane_type(pd, whdr->caplen, pseudo_header);
1321         }
1322         return pd;
1323 }
1324 #endif
1325
1326 /* Returns 0 if we could write the specified encapsulation type,
1327    an error indication otherwise. */
1328 int libpcap_dump_can_write_encap(int encap)
1329 {
1330         /* Per-packet encapsulations aren't supported. */
1331         if (encap == WTAP_ENCAP_PER_PACKET)
1332                 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
1333
1334         if (wtap_wtap_encap_to_pcap_encap(encap) == -1)
1335                 return WTAP_ERR_UNSUPPORTED_ENCAP;
1336
1337         return 0;
1338 }
1339
1340 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
1341    failure */
1342 gboolean libpcap_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
1343 {
1344         guint32 magic;
1345         struct pcap_hdr file_hdr;
1346         size_t nwritten;
1347
1348         /* This is a libpcap file */
1349         wdh->subtype_write = libpcap_dump;
1350         wdh->subtype_close = NULL;
1351
1352         /* Write the file header. */
1353         switch (wdh->file_type) {
1354
1355         case WTAP_FILE_PCAP:
1356         case WTAP_FILE_PCAP_SS990417:   /* modified, but with the old magic, sigh */
1357         case WTAP_FILE_PCAP_NOKIA:      /* Nokia libpcap of some sort */
1358                 magic = PCAP_MAGIC;
1359                 break;
1360
1361         case WTAP_FILE_PCAP_SS990915:   /* new magic, extra crap */
1362         case WTAP_FILE_PCAP_SS991029:
1363                 magic = PCAP_MODIFIED_MAGIC;
1364                 break;
1365
1366         default:
1367                 /* We should never get here - our open routine
1368                    should only get called for the types above. */
1369                 *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
1370                 return FALSE;
1371         }
1372
1373         nwritten = fwrite(&magic, 1, sizeof magic, wdh->fh);
1374         if (nwritten != sizeof magic) {
1375                 if (nwritten == 0 && ferror(wdh->fh))
1376                         *err = errno;
1377                 else
1378                         *err = WTAP_ERR_SHORT_WRITE;
1379                 return FALSE;
1380         }
1381         wdh->bytes_dumped += sizeof magic;
1382
1383         /* current "libpcap" format is 2.4 */
1384         file_hdr.version_major = 2;
1385         file_hdr.version_minor = 4;
1386         file_hdr.thiszone = 0;  /* XXX - current offset? */
1387         file_hdr.sigfigs = 0;   /* unknown, but also apparently unused */
1388         /*
1389          * Tcpdump cannot handle capture files with a snapshot length of 0,
1390          * as BPF filters return either 0 if they fail or the snapshot length
1391          * if they succeed, and a snapshot length of 0 means success is
1392          * indistinguishable from failure and the filter expression would
1393          * reject all packets.
1394          *
1395          * A snapshot length of 0, inside Wiretap, means "snapshot length
1396          * unknown"; if the snapshot length supplied to us is 0, we make
1397          * the snapshot length in the header file WTAP_MAX_PACKET_SIZE.
1398          */
1399         file_hdr.snaplen = (wdh->snaplen != 0) ? wdh->snaplen :
1400                                                  WTAP_MAX_PACKET_SIZE;
1401         file_hdr.network = wtap_wtap_encap_to_pcap_encap(wdh->encap);
1402         nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
1403         if (nwritten != sizeof file_hdr) {
1404                 if (nwritten == 0 && ferror(wdh->fh))
1405                         *err = errno;
1406                 else
1407                         *err = WTAP_ERR_SHORT_WRITE;
1408                 return FALSE;
1409         }
1410         wdh->bytes_dumped += sizeof file_hdr;
1411
1412         return TRUE;
1413 }
1414
1415 /* Write a record for a packet to a dump file.
1416    Returns TRUE on success, FALSE on failure. */
1417 static gboolean libpcap_dump(wtap_dumper *wdh,
1418         const struct wtap_pkthdr *phdr,
1419         const union wtap_pseudo_header *pseudo_header _U_,
1420         const guchar *pd, int *err)
1421 {
1422         struct pcaprec_ss990915_hdr rec_hdr;
1423         size_t hdr_size;
1424         size_t nwritten;
1425         struct sunatm_hdr atm_hdr;
1426         int atm_hdrsize;
1427
1428         if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
1429                 atm_hdrsize = sizeof (struct sunatm_hdr);
1430         else
1431                 atm_hdrsize = 0;
1432
1433         rec_hdr.hdr.ts_sec = phdr->ts.tv_sec;
1434         rec_hdr.hdr.ts_usec = phdr->ts.tv_usec;
1435         rec_hdr.hdr.incl_len = phdr->caplen + atm_hdrsize;
1436         rec_hdr.hdr.orig_len = phdr->len + atm_hdrsize;
1437         switch (wdh->file_type) {
1438
1439         case WTAP_FILE_PCAP:
1440                 hdr_size = sizeof (struct pcaprec_hdr);
1441                 break;
1442
1443         case WTAP_FILE_PCAP_SS990417:   /* modified, but with the old magic, sigh */
1444         case WTAP_FILE_PCAP_SS991029:
1445                 /* XXX - what should we supply here?
1446
1447                    Alexey's "libpcap" looks up the interface in the system's
1448                    interface list if "ifindex" is non-zero, and prints
1449                    the interface name.  It ignores "protocol", and uses
1450                    "pkt_type" to tag the packet as "host", "broadcast",
1451                    "multicast", "other host", "outgoing", or "none of the
1452                    above", but that's it.
1453
1454                    If the capture we're writing isn't a modified or
1455                    RH 6.1 capture, we'd have to do some work to
1456                    generate the packet type and interface index - and
1457                    we can't generate the interface index unless we
1458                    just did the capture ourselves in any case.
1459
1460                    I'm inclined to continue to punt; systems other than
1461                    those with the older patch can read standard "libpcap"
1462                    files, and systems with the older patch, e.g. RH 6.1,
1463                    will just have to live with this. */
1464                 rec_hdr.ifindex = 0;
1465                 rec_hdr.protocol = 0;
1466                 rec_hdr.pkt_type = 0;
1467                 hdr_size = sizeof (struct pcaprec_modified_hdr);
1468                 break;
1469
1470         case WTAP_FILE_PCAP_SS990915:   /* new magic, extra crap at the end */
1471                 rec_hdr.ifindex = 0;
1472                 rec_hdr.protocol = 0;
1473                 rec_hdr.pkt_type = 0;
1474                 rec_hdr.cpu1 = 0;
1475                 rec_hdr.cpu2 = 0;
1476                 hdr_size = sizeof (struct pcaprec_ss990915_hdr);
1477                 break;
1478
1479         case WTAP_FILE_PCAP_NOKIA:      /* old magic, extra crap at the end */
1480                 rec_hdr.ifindex = 0;
1481                 rec_hdr.protocol = 0;
1482                 rec_hdr.pkt_type = 0;
1483                 rec_hdr.cpu1 = 0;
1484                 rec_hdr.cpu2 = 0;
1485                 hdr_size = sizeof (struct pcaprec_nokia_hdr);
1486                 break;
1487
1488         default:
1489                 /* We should never get here - our open routine
1490                    should only get called for the types above. */
1491                 g_assert_not_reached();
1492                 *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
1493                 return FALSE;
1494         }
1495
1496         nwritten = fwrite(&rec_hdr, 1, hdr_size, wdh->fh);
1497         if (nwritten != hdr_size) {
1498                 if (nwritten == 0 && ferror(wdh->fh))
1499                         *err = errno;
1500                 else
1501                         *err = WTAP_ERR_SHORT_WRITE;
1502                 return FALSE;
1503         }
1504         wdh->bytes_dumped += hdr_size;
1505
1506         if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
1507                 /*
1508                  * Write the ATM header.
1509                  */
1510                 atm_hdr.flags =
1511                     (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
1512                 switch (pseudo_header->atm.aal) {
1513
1514                 case AAL_SIGNALLING:
1515                         /* Q.2931 */
1516                         atm_hdr.flags |= 0x06;
1517                         break;
1518
1519                 case AAL_5:
1520                         switch (pseudo_header->atm.type) {
1521
1522                         case TRAF_LANE:
1523                                 /* LANE */
1524                                 atm_hdr.flags |= 0x01;
1525                                 break;
1526
1527                         case TRAF_LLCMX:
1528                                 /* RFC 1483 LLC multiplexed traffic */
1529                                 atm_hdr.flags |= 0x02;
1530                                 break;
1531
1532                         case TRAF_ILMI:
1533                                 /* ILMI */
1534                                 atm_hdr.flags |= 0x05;
1535                                 break;
1536                         }
1537                         break;
1538                 }
1539                 atm_hdr.vpi = pseudo_header->atm.vpi;
1540                 atm_hdr.vci = phtons(&pseudo_header->atm.vci);
1541                 nwritten = fwrite(&atm_hdr, 1, sizeof atm_hdr, wdh->fh);
1542                 if (nwritten != sizeof atm_hdr) {
1543                         if (nwritten == 0 && ferror(wdh->fh))
1544                                 *err = errno;
1545                         else
1546                                 *err = WTAP_ERR_SHORT_WRITE;
1547                         return FALSE;
1548                 }
1549                 wdh->bytes_dumped += sizeof atm_hdr;
1550         }
1551
1552         nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
1553         if (nwritten != phdr->caplen) {
1554                 if (nwritten == 0 && ferror(wdh->fh))
1555                         *err = errno;
1556                 else
1557                         *err = WTAP_ERR_SHORT_WRITE;
1558                 return FALSE;
1559         }
1560         wdh->bytes_dumped += phdr->caplen;
1561         return TRUE;
1562 }