b023c327bfdc4b18bb8a6b5adeaf16a7f55d1344
[metze/wireshark/wip.git] / wiretap / snoop.c
1 /* snoop.c
2  *
3  * Wiretap Library
4  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "config.h"
22 #include <errno.h>
23 #include <string.h>
24 #include "wtap-int.h"
25 #include "file_wrappers.h"
26 #include <wsutil/buffer.h>
27 #include "atm.h"
28 #include "snoop.h"
29 /* See RFC 1761 for a description of the "snoop" file format. */
30
31 /* Magic number in "snoop" files. */
32 static const char snoop_magic[] = {
33         's', 'n', 'o', 'o', 'p', '\0', '\0', '\0'
34 };
35
36 /* "snoop" file header (minus magic number). */
37 struct snoop_hdr {
38         guint32 version;        /* version number (should be 2) */
39         guint32 network;        /* network type */
40 };
41
42 /* "snoop" record header. */
43 struct snooprec_hdr {
44         guint32 orig_len;       /* actual length of packet */
45         guint32 incl_len;       /* number of octets captured in file */
46         guint32 rec_len;        /* length of record */
47         guint32 cum_drops;      /* cumulative number of dropped packets */
48         guint32 ts_sec;         /* timestamp seconds */
49         guint32 ts_usec;        /* timestamp microseconds */
50 };
51
52 /*
53  * The link-layer header on ATM packets.
54  */
55 struct snoop_atm_hdr {
56         guint8  flags;          /* destination and traffic type */
57         guint8  vpi;            /* VPI */
58         guint16 vci;            /* VCI */
59 };
60
61 /*
62  * Extra information stuffed into the padding in Shomiti/Finisar Surveyor
63  * captures.
64  */
65 struct shomiti_trailer {
66         guint16 phy_rx_length;  /* length on the wire, including FCS? */
67         guint16 phy_rx_status;  /* status flags */
68         guint32 ts_40_ns_lsb;   /* 40 ns time stamp, low-order bytes? */
69         guint32 ts_40_ns_msb;   /* 40 ns time stamp, low-order bytes? */
70         gint32  frame_id;       /* "FrameID"? */
71 };
72
73 /*
74  * phy_rx_status flags.
75  */
76 #define RX_STATUS_OVERFLOW              0x8000  /* overflow error */
77 #define RX_STATUS_BAD_CRC               0x4000  /* CRC error */
78 #define RX_STATUS_DRIBBLE_NIBBLE        0x2000  /* dribble/nibble bits? */
79 #define RX_STATUS_SHORT_FRAME           0x1000  /* frame < 64 bytes */
80 #define RX_STATUS_OVERSIZE_FRAME        0x0800  /* frame > 1518 bytes */
81 #define RX_STATUS_GOOD_FRAME            0x0400  /* frame OK */
82 #define RX_STATUS_N12_BYTES_RECEIVED    0x0200  /* first 12 bytes of frame received? */
83 #define RX_STATUS_RXABORT               0x0100  /* RXABORT during reception */
84 #define RX_STATUS_FIFO_ERROR            0x0080  /* receive FIFO error */
85 #define RX_STATUS_TRIGGERED             0x0001  /* frame did trigger */
86
87 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
88     gint64 *data_offset);
89 static gboolean snoop_seek_read(wtap *wth, gint64 seek_off,
90     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
91 static int snoop_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
92     Buffer *buf, int *err, gchar **err_info);
93 static gboolean snoop_read_atm_pseudoheader(FILE_T fh,
94     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
95 static gboolean snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
96     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
97     int *header_size);
98 static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
99     const guint8 *pd, int *err);
100
101 /*
102  * See
103  *
104  *      http://www.opengroup.org/onlinepubs/9638599/apdxf.htm
105  *
106  * for the "dlpi.h" header file specified by The Open Group, which lists
107  * the DL_ values for various protocols; Solaris 7 uses the same values.
108  *
109  * See
110  *
111  *      http://www.iana.org/assignments/snoop-datalink-types/snoop-datalink-types.xml
112  *
113  * for the IETF list of snoop datalink types.
114  *
115  * The page at
116  *
117  *      http://mrpink.lerc.nasa.gov/118x/support.html
118  *
119  * had links to modified versions of "tcpdump" and "libpcap" for SUNatm
120  * DLPI support; they suggested that the 3.0 verson of SUNatm uses those
121  * values.  The Wayback Machine archived that page, but not the stuff
122  * to which it linked, unfortunately.
123  *
124  * It also has a link to "convert.c", which is a program to convert files
125  * from the format written by the "atmsnoop" program that comes with the
126  * SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
127  * to be DL_FDDI (don't ask why).  SunATM 3.0 claims to be DL_IPATM, which
128  * is 0x12".
129  *
130  * It also says that "ATM Mac header is 12 bytes long.", and seems to imply
131  * that in an "atmsnoop" file, the header contains 2 bytes (direction and
132  * VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
133  * type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
134  * of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
135  * pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
136  * 1 byte of VPI, and 2 bytes of VCI.
137  *
138  * The aforementioned page also has a link to some capture files from
139  * "atmsnoop"; this version of "snoop.c" appears to be able to read them.
140  *
141  * Source to an "atmdump" package, which includes a modified version of
142  * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
143  * also includes "atmdump", which is a modified "tcpdump", was available
144  * at
145  *
146  *      ftp://ftp.cs.ndsu.nodak.edu/pub/freebsd/atm/atm-bpf.tgz
147  *
148  * (the host name is no longer valid) and that code also indicated that
149  * DL_IPATM is used, and that an ATM packet handed up from the Sun driver
150  * for the Sun SBus ATM card on Solaris 2.5.1 has 1 byte of direction,
151  * 1 byte of VPI, 2 bytes of VCI, and then the ATM PDU, and suggests that
152  * the direction flag is 0x80 for "transmitted" (presumably meaning
153  * DTE->DCE) and presumably not 0x80 for "received" (presumably meaning
154  * DCE->DTE).  That code was used as the basis for the SunATM support in
155  * later versions of libpcap and tcpdump, and it worked at the time the
156  * development was done with the SunATM code on the system on which the
157  * development was done.
158  *
159  * In fact, the "direction" byte appears to have some other stuff, perhaps
160  * a traffic type, in the lower 7 bits, with the 8th bit indicating the
161  * direction.  That appears to be the case.
162  *
163  * I don't know what the encapsulation of any of the other types is, so I
164  * leave them all as WTAP_ENCAP_UNKNOWN, except for those for which Brian
165  * Ginsbach has supplied information about the way UNICOS/mp uses them.
166  * I also don't know whether "snoop" can handle any of them (it presumably
167  * can't handle ATM, otherwise Sun wouldn't have supplied "atmsnoop"; even
168  * if it can't, this may be useful reference information for anybody doing
169  * code to use DLPI to do raw packet captures on those network types.
170  *
171  *      http://web.archive.org/web/20010906213807/http://www.shomiti.com/support/TNCapFileFormat.htm
172  *
173  * gives information on Shomiti's mutant flavor of snoop.  For some unknown
174  * reason, they decided not to just Go With The DLPI Flow, and instead used
175  * the types unspecified in RFC 1461 for their own nefarious purposes, such
176  * as distinguishing 10MB from 100MB from 1000MB Ethernet and distinguishing
177  * 4MB from 16MB Token Ring, and distinguishing both of them from the
178  * "Shomiti" versions of same.
179  */
180 int snoop_open(wtap *wth, int *err, gchar **err_info)
181 {
182         int bytes_read;
183         char magic[sizeof snoop_magic];
184         struct snoop_hdr hdr;
185         struct snooprec_hdr rec_hdr;
186         guint padbytes;
187         gboolean is_shomiti;
188         static const int snoop_encap[] = {
189                 WTAP_ENCAP_ETHERNET,    /* IEEE 802.3 */
190                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.4 Token Bus */
191                 WTAP_ENCAP_TOKEN_RING,
192                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.6 Metro Net */
193                 WTAP_ENCAP_ETHERNET,
194                 WTAP_ENCAP_UNKNOWN,     /* HDLC */
195                 WTAP_ENCAP_UNKNOWN,     /* Character Synchronous, e.g. bisync */
196                 WTAP_ENCAP_UNKNOWN,     /* IBM Channel-to-Channel */
197                 WTAP_ENCAP_FDDI_BITSWAPPED,
198                 WTAP_ENCAP_NULL,        /* Other */
199                 WTAP_ENCAP_UNKNOWN,     /* Frame Relay LAPF */
200                 WTAP_ENCAP_UNKNOWN,     /* Multi-protocol over Frame Relay */
201                 WTAP_ENCAP_UNKNOWN,     /* Character Async (e.g., SLIP and PPP?) */
202                 WTAP_ENCAP_UNKNOWN,     /* X.25 Classical IP */
203                 WTAP_ENCAP_NULL,        /* software loopback */
204                 WTAP_ENCAP_UNKNOWN,     /* not defined in "dlpi.h" */
205                 WTAP_ENCAP_IP_OVER_FC,  /* Fibre Channel */
206                 WTAP_ENCAP_UNKNOWN,     /* ATM */
207                 WTAP_ENCAP_ATM_PDUS,    /* ATM Classical IP */
208                 WTAP_ENCAP_UNKNOWN,     /* X.25 LAPB */
209                 WTAP_ENCAP_UNKNOWN,     /* ISDN */
210                 WTAP_ENCAP_UNKNOWN,     /* HIPPI */
211                 WTAP_ENCAP_UNKNOWN,     /* 100VG-AnyLAN Ethernet */
212                 WTAP_ENCAP_UNKNOWN,     /* 100VG-AnyLAN Token Ring */
213                 WTAP_ENCAP_UNKNOWN,     /* "ISO 8802/3 and Ethernet" */
214                 WTAP_ENCAP_UNKNOWN,     /* 100BaseT (but that's just Ethernet) */
215                 WTAP_ENCAP_IP_OVER_IB,  /* Infiniband */
216         };
217         #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
218         #define SNOOP_PRIVATE_BIT 0x80000000
219         static const int snoop_private_encap[] = {
220                 WTAP_ENCAP_UNKNOWN,     /* Not Used */
221                 WTAP_ENCAP_UNKNOWN,     /* IPv4 Tunnel Link */
222                 WTAP_ENCAP_UNKNOWN,     /* IPv6 Tunnel Link */
223                 WTAP_ENCAP_UNKNOWN,     /* Virtual network interface */
224                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.11 */
225                 WTAP_ENCAP_IPNET,       /* ipnet(7D) link */
226                 WTAP_ENCAP_UNKNOWN,     /* IPMP stub interface */
227                 WTAP_ENCAP_UNKNOWN,     /* 6to4 Tunnel Link */
228         };
229         #define NUM_SNOOP_PRIVATE_ENCAPS (sizeof snoop_private_encap / sizeof snoop_private_encap[0])
230         static const int shomiti_encap[] = {
231                 WTAP_ENCAP_ETHERNET,    /* IEEE 802.3 */
232                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.4 Token Bus */
233                 WTAP_ENCAP_TOKEN_RING,
234                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.6 Metro Net */
235                 WTAP_ENCAP_ETHERNET,
236                 WTAP_ENCAP_UNKNOWN,     /* HDLC */
237                 WTAP_ENCAP_UNKNOWN,     /* Character Synchronous, e.g. bisync */
238                 WTAP_ENCAP_UNKNOWN,     /* IBM Channel-to-Channel */
239                 WTAP_ENCAP_FDDI_BITSWAPPED,
240                 WTAP_ENCAP_UNKNOWN,     /* Other */
241                 WTAP_ENCAP_ETHERNET,    /* Fast Ethernet */
242                 WTAP_ENCAP_TOKEN_RING,  /* 4MB 802.5 token ring */
243                 WTAP_ENCAP_ETHERNET,    /* Gigabit Ethernet */
244                 WTAP_ENCAP_TOKEN_RING,  /* "IEEE 802.5 Shomiti" */
245                 WTAP_ENCAP_TOKEN_RING,  /* "4MB IEEE 802.5 Shomiti" */
246                 WTAP_ENCAP_UNKNOWN,     /* Other */
247                 WTAP_ENCAP_UNKNOWN,     /* Other */
248                 WTAP_ENCAP_UNKNOWN,     /* Other */
249                 WTAP_ENCAP_IEEE_802_11_WITH_RADIO, /* IEEE 802.11 with Radio Header */
250                 WTAP_ENCAP_ETHERNET,    /* 10 Gigabit Ethernet */
251         };
252         #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
253         int file_encap;
254         gint64 saved_offset;
255
256         /* Read in the string that should be at the start of a "snoop" file */
257         errno = WTAP_ERR_CANT_READ;
258         bytes_read = file_read(magic, sizeof magic, wth->fh);
259         if (bytes_read != sizeof magic) {
260                 *err = file_error(wth->fh, err_info);
261                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
262                         return -1;
263                 return 0;
264         }
265
266         if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
267                 return 0;
268         }
269
270         /* Read the rest of the header. */
271         errno = WTAP_ERR_CANT_READ;
272         bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
273         if (bytes_read != sizeof hdr) {
274                 *err = file_error(wth->fh, err_info);
275                 if (*err == 0)
276                         *err = WTAP_ERR_SHORT_READ;
277                 return -1;
278         }
279
280         /*
281          * Make sure it's a version we support.
282          */
283         hdr.version = g_ntohl(hdr.version);
284         switch (hdr.version) {
285
286         case 2:         /* Solaris 2.x and later snoop, and Shomiti
287                            Surveyor prior to 3.0, or 3.0 and later
288                            with NDIS card */
289         case 3:         /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
290         case 4:         /* Surveyor 3.0 and later, with Shomiti GAM hardware */
291         case 5:         /* Surveyor 3.0 and later, with Shomiti THG hardware */
292                 break;
293
294         default:
295                 *err = WTAP_ERR_UNSUPPORTED;
296                 *err_info = g_strdup_printf("snoop: version %u unsupported", hdr.version);
297                 return -1;
298         }
299
300         /*
301          * Oh, this is lovely.
302          *
303          * I suppose Shomiti could give a bunch of lawyerly noise about
304          * how "well, RFC 1761 said they were unassigned, and that's
305          * the standard, not the DLPI header file, so it's perfectly OK
306          * for us to use them, blah blah blah", but it's still irritating
307          * as hell that they used the unassigned-in-RFC-1761 values for
308          * their own purposes - especially given that Sun also used
309          * one of them in atmsnoop.
310          *
311          * We can't determine whether it's a Shomiti capture based on
312          * the version number, as, according to their documentation on
313          * their capture file format, Shomiti uses a version number of 2
314          * if the data "was captured using an NDIS card", which presumably
315          * means "captured with an ordinary boring network card via NDIS"
316          * as opposed to "captured with our whizzo special capture
317          * hardware".
318          *
319          * The only way I can see to determine that is to check how much
320          * padding there is in the first packet - if there's enough
321          * padding for a Shomiti trailer, it's probably a Shomiti
322          * capture, and otherwise, it's probably from Snoop.
323          */
324
325         /*
326          * Start out assuming it's not a Shomiti capture.
327          */
328         is_shomiti = FALSE;
329
330         /* Read first record header. */
331         saved_offset = file_tell(wth->fh);
332         errno = WTAP_ERR_CANT_READ;
333         bytes_read = file_read(&rec_hdr, sizeof rec_hdr, wth->fh);
334         if (bytes_read != sizeof rec_hdr) {
335                 *err = file_error(wth->fh, err_info);
336                 if (*err == 0)
337                         *err = WTAP_ERR_SHORT_READ;
338                 return -1;
339
340                 /*
341                  * The file ends after the record header, which means this
342                  * is a capture with no packets.
343                  *
344                  * We assume it's a snoop file; the actual type of file is
345                  * irrelevant, as there are no records in it, and thus no
346                  * extra information if it's a Shomiti capture, and no
347                  * link-layer headers whose type we have to know, and no
348                  * Ethernet frames that might have an FCS.
349                  */
350         } else {
351                 /*
352                  * Compute the number of bytes of padding in the
353                  * record.  If it's at least the size of a Shomiti
354                  * trailer record, we assume this is a Shomiti
355                  * capture.  (Some atmsnoop captures appear
356                  * to have 4 bytes of padding, and at least one
357                  * snoop capture appears to have 6 bytes of padding;
358                  * the Shomiti header is larger than either of those.)
359                  */
360                 if (g_ntohl(rec_hdr.rec_len) >
361                     (sizeof rec_hdr + g_ntohl(rec_hdr.incl_len))) {
362                         /*
363                          * Well, we have padding; how much?
364                          */
365                         padbytes = g_ntohl(rec_hdr.rec_len) -
366                             ((guint)sizeof rec_hdr + g_ntohl(rec_hdr.incl_len));
367
368                         /*
369                          * Is it at least the size of a Shomiti trailer?
370                          */
371                         is_shomiti =
372                             (padbytes >= sizeof (struct shomiti_trailer));
373                 }
374         }
375
376         /*
377          * Seek back to the beginning of the first record.
378          */
379         if (file_seek(wth->fh, saved_offset, SEEK_SET, err) == -1)
380                 return -1;
381
382         hdr.network = g_ntohl(hdr.network);
383         if (is_shomiti) {
384                 if (hdr.network >= NUM_SHOMITI_ENCAPS
385                     || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
386                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
387                         *err_info = g_strdup_printf("snoop: Shomiti network type %u unknown or unsupported",
388                             hdr.network);
389                         return -1;
390                 }
391                 file_encap = shomiti_encap[hdr.network];
392
393                 /* This is a Shomiti file */
394                 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SHOMITI;
395         } else if (hdr.network & SNOOP_PRIVATE_BIT) {
396                 if ((hdr.network^SNOOP_PRIVATE_BIT) >= NUM_SNOOP_PRIVATE_ENCAPS
397                     || snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT] == WTAP_ENCAP_UNKNOWN) {
398                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
399                         *err_info = g_strdup_printf("snoop: private network type %u unknown or unsupported",
400                             hdr.network);
401                         return -1;
402                 }
403                 file_encap = snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT];
404
405                 /* This is a snoop file */
406                 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
407         } else {
408                 if (hdr.network >= NUM_SNOOP_ENCAPS
409                     || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
410                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
411                         *err_info = g_strdup_printf("snoop: network type %u unknown or unsupported",
412                             hdr.network);
413                         return -1;
414                 }
415                 file_encap = snoop_encap[hdr.network];
416
417                 /* This is a snoop file */
418                 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
419         }
420
421         /*
422          * We don't currently use the extra information in Shomiti
423          * records, so we use the same routines to read snoop and
424          * Shomiti files.
425          */
426         wth->subtype_read = snoop_read;
427         wth->subtype_seek_read = snoop_seek_read;
428         wth->file_encap = file_encap;
429         wth->snapshot_length = 0;       /* not available in header */
430         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
431         return 1;
432 }
433
434 typedef struct {
435         guint8 pad[4];
436         guint8 undecrypt[2];
437         guint8 rate;
438         guint8 preamble;
439         guint8 code;
440         guint8 signal;
441         guint8 qual;
442         guint8 channel;
443 } shomiti_wireless_header;
444
445
446 /* Read the next packet */
447 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
448     gint64 *data_offset)
449 {
450         int     padbytes;
451         int     bytes_read;
452         char    padbuf[4];
453         int     bytes_to_read;
454
455         *data_offset = file_tell(wth->fh);
456
457         padbytes = snoop_read_packet(wth, wth->fh, &wth->phdr,
458             wth->frame_buffer, err, err_info);
459         if (padbytes == -1)
460                 return FALSE;
461
462         /*
463          * Skip over the padding (don't "fseek()", as the standard
464          * I/O library on some platforms discards buffered data if
465          * you do that, which means it does a lot more reads).
466          *
467          * XXX - is that still true?
468          *
469          * There's probably not much padding (it's probably padded only
470          * to a 4-byte boundary), so we probably need only do one read.
471          */
472         while (padbytes != 0) {
473                 bytes_to_read = padbytes;
474                 if ((unsigned)bytes_to_read > sizeof padbuf)
475                         bytes_to_read = sizeof padbuf;
476                 errno = WTAP_ERR_CANT_READ;
477                 bytes_read = file_read(padbuf, bytes_to_read, wth->fh);
478                 if (bytes_read != bytes_to_read) {
479                         *err = file_error(wth->fh, err_info);
480                         if (*err == 0)
481                                 *err = WTAP_ERR_SHORT_READ;
482                         return FALSE;
483                 }
484                 padbytes -= bytes_read;
485         }
486
487         return TRUE;
488 }
489
490 static gboolean
491 snoop_seek_read(wtap *wth, gint64 seek_off,
492     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
493 {
494         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
495                 return FALSE;
496
497         if (snoop_read_packet(wth, wth->random_fh, phdr, buf, err, err_info) == -1) {
498                 if (*err == 0)
499                         *err = WTAP_ERR_SHORT_READ;
500                 return FALSE;
501         }
502         return TRUE;
503 }
504
505 static int
506 snoop_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
507     Buffer *buf, int *err, gchar **err_info)
508 {
509         struct snooprec_hdr hdr;
510         int     bytes_read;
511         guint32 rec_size;
512         guint32 packet_size;
513         guint32 orig_size;
514         int header_size;
515
516         /* Read record header. */
517         errno = WTAP_ERR_CANT_READ;
518         bytes_read = file_read(&hdr, sizeof hdr, fh);
519         if (bytes_read != sizeof hdr) {
520                 *err = file_error(fh, err_info);
521                 if (*err == 0 && bytes_read != 0)
522                         *err = WTAP_ERR_SHORT_READ;
523                 return -1;
524         }
525
526         rec_size = g_ntohl(hdr.rec_len);
527         orig_size = g_ntohl(hdr.orig_len);
528         packet_size = g_ntohl(hdr.incl_len);
529         if (orig_size > WTAP_MAX_PACKET_SIZE) {
530                 /*
531                  * Probably a corrupt capture file; don't blow up trying
532                  * to allocate space for an immensely-large packet.
533                  */
534                 *err = WTAP_ERR_BAD_FILE;
535                 *err_info = g_strdup_printf("snoop: File has %u-byte original length, bigger than maximum of %u",
536                     orig_size, WTAP_MAX_PACKET_SIZE);
537                 return -1;
538         }
539         if (packet_size > WTAP_MAX_PACKET_SIZE) {
540                 /*
541                  * Probably a corrupt capture file; don't blow up trying
542                  * to allocate space for an immensely-large packet.
543                  */
544                 *err = WTAP_ERR_BAD_FILE;
545                 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than maximum of %u",
546                     packet_size, WTAP_MAX_PACKET_SIZE);
547                 return -1;
548         }
549         if (packet_size > rec_size) {
550                 /*
551                  * Probably a corrupt capture file.
552                  */
553                 *err = WTAP_ERR_BAD_FILE;
554                 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than record size %u",
555                     packet_size, rec_size);
556                 return -1;
557         }
558
559         switch (wth->file_encap) {
560
561         case WTAP_ENCAP_ATM_PDUS:
562                 /*
563                  * This is an ATM packet, so the first four bytes are
564                  * the direction of the packet (transmit/receive), the
565                  * VPI, and the VCI; read them and generate the
566                  * pseudo-header from them.
567                  */
568                 if (packet_size < sizeof (struct snoop_atm_hdr)) {
569                         /*
570                          * Uh-oh, the packet isn't big enough to even
571                          * have a pseudo-header.
572                          */
573                         *err = WTAP_ERR_BAD_FILE;
574                         *err_info = g_strdup_printf("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header",
575                             packet_size);
576                         return -1;
577                 }
578                 if (!snoop_read_atm_pseudoheader(fh, &phdr->pseudo_header,
579                     err, err_info))
580                         return -1;      /* Read error */
581
582                 /*
583                  * Don't count the pseudo-header as part of the packet.
584                  */
585                 rec_size -= (guint32)sizeof (struct snoop_atm_hdr);
586                 orig_size -= (guint32)sizeof (struct snoop_atm_hdr);
587                 packet_size -= (guint32)sizeof (struct snoop_atm_hdr);
588                 break;
589
590         case WTAP_ENCAP_ETHERNET:
591                 /*
592                  * If this is a snoop file, we assume there's no FCS in
593                  * this frame; if this is a Shomit file, we assume there
594                  * is.  (XXX - or should we treat it a "maybe"?)
595                  */
596                 if (wth->file_type_subtype == WTAP_FILE_TYPE_SUBTYPE_SHOMITI)
597                         phdr->pseudo_header.eth.fcs_len = 4;
598                 else
599                         phdr->pseudo_header.eth.fcs_len = 0;
600                 break;
601
602         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
603                 if (packet_size < sizeof (shomiti_wireless_header)) {
604                         /*
605                          * Uh-oh, the packet isn't big enough to even
606                          * have a pseudo-header.
607                          */
608                         *err = WTAP_ERR_BAD_FILE;
609                         *err_info = g_strdup_printf("snoop: Shomiti wireless file has a %u-byte packet, too small to have even a wireless pseudo-header",
610                             packet_size);
611                         return -1;
612                 }
613                 if (!snoop_read_shomiti_wireless_pseudoheader(fh,
614                     &phdr->pseudo_header, err, err_info, &header_size))
615                         return -1;      /* Read error */
616
617                 /*
618                  * Don't count the pseudo-header as part of the packet.
619                  */
620                 rec_size -= header_size;
621                 orig_size -= header_size;
622                 packet_size -= header_size;
623                 break;
624         }
625
626         phdr->rec_type = REC_TYPE_PACKET;
627         phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
628         phdr->ts.secs = g_ntohl(hdr.ts_sec);
629         phdr->ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
630         phdr->caplen = packet_size;
631         phdr->len = orig_size;
632
633         if (rec_size < (sizeof hdr + packet_size)) {
634                 /*
635                  * What, *negative* padding?  Bogus.
636                  */
637                 *err = WTAP_ERR_BAD_FILE;
638                 *err_info = g_strdup_printf("snoop: File has %u-byte record with packet size of %u",
639                     rec_size, packet_size);
640                 return -1;
641         }
642
643         /*
644          * Read the packet data.
645          */
646         if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
647                 return -1;      /* failed */
648
649         /*
650          * If this is ATM LANE traffic, try to guess what type of LANE
651          * traffic it is based on the packet contents.
652          */
653         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
654             phdr->pseudo_header.atm.type == TRAF_LANE) {
655                 atm_guess_lane_type(phdr, ws_buffer_start_ptr(buf));
656         }
657
658         return rec_size - ((guint)sizeof hdr + packet_size);
659 }
660
661 static gboolean
662 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
663     int *err, gchar **err_info)
664 {
665         struct snoop_atm_hdr atm_phdr;
666         int     bytes_read;
667         guint8  vpi;
668         guint16 vci;
669
670         errno = WTAP_ERR_CANT_READ;
671         bytes_read = file_read(&atm_phdr, sizeof (struct snoop_atm_hdr), fh);
672         if (bytes_read != sizeof (struct snoop_atm_hdr)) {
673                 *err = file_error(fh, err_info);
674                 if (*err == 0)
675                         *err = WTAP_ERR_SHORT_READ;
676                 return FALSE;
677         }
678
679         vpi = atm_phdr.vpi;
680         vci = pntoh16(&atm_phdr.vci);
681
682         /*
683          * The lower 4 bits of the first byte of the header indicate
684          * the type of traffic, as per the "atmioctl.h" header in
685          * SunATM.
686          */
687         switch (atm_phdr.flags & 0x0F) {
688
689         case 0x01:      /* LANE */
690                 pseudo_header->atm.aal = AAL_5;
691                 pseudo_header->atm.type = TRAF_LANE;
692                 break;
693
694         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
695                 pseudo_header->atm.aal = AAL_5;
696                 pseudo_header->atm.type = TRAF_LLCMX;
697                 break;
698
699         case 0x05:      /* ILMI */
700                 pseudo_header->atm.aal = AAL_5;
701                 pseudo_header->atm.type = TRAF_ILMI;
702                 break;
703
704         case 0x06:      /* Signalling AAL */
705                 pseudo_header->atm.aal = AAL_SIGNALLING;
706                 pseudo_header->atm.type = TRAF_UNKNOWN;
707                 break;
708
709         case 0x03:      /* MARS (RFC 2022) */
710                 pseudo_header->atm.aal = AAL_5;
711                 pseudo_header->atm.type = TRAF_UNKNOWN;
712                 break;
713
714         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
715                 pseudo_header->atm.aal = AAL_5;
716                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
717                 break;
718
719         default:
720                 /*
721                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
722                  * case assume it's AAL_SIGNALLING; we know nothing more
723                  * about it.
724                  *
725                  * XXX - is this necessary?  Or are we guaranteed that
726                  * all signalling traffic has a type of 0x06?
727                  *
728                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
729                  * 0x00 ("raw"), might it be non-AAL5 traffic?
730                  */
731                 if (vpi == 0 && vci == 5)
732                         pseudo_header->atm.aal = AAL_SIGNALLING;
733                 else
734                         pseudo_header->atm.aal = AAL_5;
735                 pseudo_header->atm.type = TRAF_UNKNOWN;
736                 break;
737         }
738         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
739
740         pseudo_header->atm.vpi = vpi;
741         pseudo_header->atm.vci = vci;
742         pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 0 : 1;
743
744         /* We don't have this information */
745         pseudo_header->atm.flags = 0;
746         pseudo_header->atm.cells = 0;
747         pseudo_header->atm.aal5t_u2u = 0;
748         pseudo_header->atm.aal5t_len = 0;
749         pseudo_header->atm.aal5t_chksum = 0;
750
751         return TRUE;
752 }
753
754 static gboolean
755 snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
756     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
757     int *header_size)
758 {
759         shomiti_wireless_header whdr;
760         int     bytes_read;
761         int     rsize;
762
763         errno = WTAP_ERR_CANT_READ;
764         bytes_read = file_read(&whdr, sizeof (shomiti_wireless_header), fh);
765         if (bytes_read != sizeof (shomiti_wireless_header)) {
766                 *err = file_error(fh, err_info);
767                 if (*err == 0)
768                         *err = WTAP_ERR_SHORT_READ;
769                 return FALSE;
770         }
771
772         /* the 4th byte of the pad is actually a header length,
773          * we've already read 8 bytes of it, and it must never
774          * be less than 8.
775          *
776          * XXX - presumably that means that the header length
777          * doesn't include the length field, as we've read
778          * 12 bytes total.
779          *
780          * XXX - what's in the other 3 bytes of the padding?  Is it a
781          * 4-byte length field?
782          * XXX - is there anything in the rest of the header of interest?
783          * XXX - are there any files where the header is shorter than
784          * 4 bytes of length plus 8 bytes of information?
785          */
786         if (whdr.pad[3] < 8) {
787                 *err = WTAP_ERR_BAD_FILE;
788                 *err_info = g_strdup_printf("snoop: Header length in Surveyor record is %u, less than minimum of 8",
789                     whdr.pad[3]);
790                 return FALSE;
791         }
792         /* Skip the header. */
793         rsize = ((int) whdr.pad[3]) - 8;
794         if (file_seek(fh, rsize, SEEK_CUR, err) == -1)
795                 return FALSE;
796
797         pseudo_header->ieee_802_11.fcs_len = 4;
798         pseudo_header->ieee_802_11.channel = whdr.channel;
799         pseudo_header->ieee_802_11.data_rate = whdr.rate;
800         pseudo_header->ieee_802_11.signal_level = whdr.signal;
801
802         /* add back the header and don't forget the pad as well */
803         *header_size = rsize + 8 + 4;
804
805         return TRUE;
806 }
807
808 static const int wtap_encap[] = {
809         -1,             /* WTAP_ENCAP_UNKNOWN -> unsupported */
810         0x04,           /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
811         0x02,           /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
812         -1,             /* WTAP_ENCAP_SLIP -> unsupported */
813         -1,             /* WTAP_ENCAP_PPP -> unsupported */
814         0x08,           /* WTAP_ENCAP_FDDI -> DL_FDDI */
815         0x08,           /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
816         -1,             /* WTAP_ENCAP_RAW_IP -> unsupported */
817         -1,             /* WTAP_ENCAP_ARCNET -> unsupported */
818         -1,             /* WTAP_ENCAP_ARCNET_LINUX -> unsupported */
819         -1,             /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
820         -1,             /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
821         -1,             /* WTAP_ENCAP_LAPB -> unsupported*/
822         0x12,           /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
823 };
824 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
825
826 /* Returns 0 if we could write the specified encapsulation type,
827    an error indication otherwise. */
828 int snoop_dump_can_write_encap(int encap)
829 {
830         /* Per-packet encapsulations aren't supported. */
831         if (encap == WTAP_ENCAP_PER_PACKET)
832                 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
833
834         if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
835                 return WTAP_ERR_UNSUPPORTED_ENCAP;
836
837         return 0;
838 }
839
840 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
841    failure */
842 gboolean snoop_dump_open(wtap_dumper *wdh, int *err)
843 {
844         struct snoop_hdr file_hdr;
845
846         /* This is a snoop file */
847         wdh->subtype_write = snoop_dump;
848         wdh->subtype_close = NULL;
849
850         /* Write the file header. */
851         if (!wtap_dump_file_write(wdh, &snoop_magic, sizeof snoop_magic, err))
852                 return FALSE;
853
854         /* current "snoop" format is 2 */
855         file_hdr.version = g_htonl(2);
856         file_hdr.network = g_htonl(wtap_encap[wdh->encap]);
857         if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
858                 return FALSE;
859
860         return TRUE;
861 }
862
863 /* Write a record for a packet to a dump file.
864    Returns TRUE on success, FALSE on failure. */
865 static gboolean snoop_dump(wtap_dumper *wdh,
866         const struct wtap_pkthdr *phdr,
867         const guint8 *pd, int *err)
868 {
869         const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
870         struct snooprec_hdr rec_hdr;
871         int reclen;
872         guint padlen;
873         static const char zeroes[4] = {0};
874         struct snoop_atm_hdr atm_hdr;
875         int atm_hdrsize;
876
877         /* We can only write packet records. */
878         if (phdr->rec_type != REC_TYPE_PACKET) {
879                 *err = WTAP_ERR_REC_TYPE_UNSUPPORTED;
880                 return FALSE;
881         }
882
883         if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
884                 atm_hdrsize = sizeof (struct snoop_atm_hdr);
885         else
886                 atm_hdrsize = 0;
887
888         /* Record length = header length plus data length... */
889         reclen = (int)sizeof rec_hdr + phdr->caplen + atm_hdrsize;
890
891
892         /* ... plus enough bytes to pad it to a 4-byte boundary. */
893         padlen = ((reclen + 3) & ~3) - reclen;
894         reclen += padlen;
895
896         /* Don't write anything we're not willing to read. */
897         if (phdr->caplen + atm_hdrsize > WTAP_MAX_PACKET_SIZE) {
898                 *err = WTAP_ERR_PACKET_TOO_LARGE;
899                 return FALSE;
900         }
901
902         rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
903         rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
904         rec_hdr.rec_len = g_htonl(reclen);
905         rec_hdr.cum_drops = 0;
906         rec_hdr.ts_sec = g_htonl(phdr->ts.secs);
907         rec_hdr.ts_usec = g_htonl(phdr->ts.nsecs / 1000);
908         if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
909                 return FALSE;
910
911         if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
912                 /*
913                  * Write the ATM header.
914                  */
915                 atm_hdr.flags =
916                     (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
917                 switch (pseudo_header->atm.aal) {
918
919                 case AAL_SIGNALLING:
920                         /* Signalling AAL */
921                         atm_hdr.flags |= 0x06;
922                         break;
923
924                 case AAL_5:
925                         switch (pseudo_header->atm.type) {
926
927                         case TRAF_LANE:
928                                 /* LANE */
929                                 atm_hdr.flags |= 0x01;
930                                 break;
931
932                         case TRAF_LLCMX:
933                                 /* RFC 1483 LLC multiplexed traffic */
934                                 atm_hdr.flags |= 0x02;
935                                 break;
936
937                         case TRAF_ILMI:
938                                 /* ILMI */
939                                 atm_hdr.flags |= 0x05;
940                                 break;
941                         }
942                         break;
943                 }
944                 atm_hdr.vpi = (guint8) pseudo_header->atm.vpi;
945                 atm_hdr.vci = g_htons(pseudo_header->atm.vci);
946                 if (!wtap_dump_file_write(wdh, &atm_hdr, sizeof atm_hdr, err))
947                         return FALSE;
948         }
949
950         if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
951                 return FALSE;
952
953         /* Now write the padding. */
954         if (!wtap_dump_file_write(wdh, zeroes, padlen, err))
955                 return FALSE;
956         return TRUE;
957 }