Fix Uninitialized argument value found by Clang Analyzer
[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 "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  * Once upon a time
172  *
173  *      http://web.archive.org/web/20010906213807/http://www.shomiti.com/support/TNCapFileFormat.htm
174  *
175  * gave information on Shomiti's mutant flavor of snoop; Shomiti's Web site
176  * is no longer available on the Wayback Machine.  For some unknown reason,
177  * they decided not to just Go With The DLPI Flow, and instead used the types
178  * unspecified in RFC 1461 for their own nefarious purposes, such as
179  * distinguishing 10MB from 100MB from 1000MB Ethernet and distinguishing
180  * 4MB from 16MB Token Ring, and distinguishing both of them from the
181  * "Shomiti" versions of same.
182  */
183 int snoop_open(wtap *wth, int *err, gchar **err_info)
184 {
185         int bytes_read;
186         char magic[sizeof snoop_magic];
187         struct snoop_hdr hdr;
188         struct snooprec_hdr rec_hdr;
189         guint padbytes;
190         gboolean is_shomiti;
191         static const int snoop_encap[] = {
192                 WTAP_ENCAP_ETHERNET,    /* IEEE 802.3 */
193                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.4 Token Bus */
194                 WTAP_ENCAP_TOKEN_RING,
195                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.6 Metro Net */
196                 WTAP_ENCAP_ETHERNET,
197                 WTAP_ENCAP_UNKNOWN,     /* HDLC */
198                 WTAP_ENCAP_UNKNOWN,     /* Character Synchronous, e.g. bisync */
199                 WTAP_ENCAP_UNKNOWN,     /* IBM Channel-to-Channel */
200                 WTAP_ENCAP_FDDI_BITSWAPPED,
201                 WTAP_ENCAP_NULL,        /* Other */
202                 WTAP_ENCAP_UNKNOWN,     /* Frame Relay LAPF */
203                 WTAP_ENCAP_UNKNOWN,     /* Multi-protocol over Frame Relay */
204                 WTAP_ENCAP_UNKNOWN,     /* Character Async (e.g., SLIP and PPP?) */
205                 WTAP_ENCAP_UNKNOWN,     /* X.25 Classical IP */
206                 WTAP_ENCAP_NULL,        /* software loopback */
207                 WTAP_ENCAP_UNKNOWN,     /* not defined in "dlpi.h" */
208                 WTAP_ENCAP_IP_OVER_FC,  /* Fibre Channel */
209                 WTAP_ENCAP_UNKNOWN,     /* ATM */
210                 WTAP_ENCAP_ATM_PDUS,    /* ATM Classical IP */
211                 WTAP_ENCAP_UNKNOWN,     /* X.25 LAPB */
212                 WTAP_ENCAP_UNKNOWN,     /* ISDN */
213                 WTAP_ENCAP_UNKNOWN,     /* HIPPI */
214                 WTAP_ENCAP_UNKNOWN,     /* 100VG-AnyLAN Ethernet */
215                 WTAP_ENCAP_UNKNOWN,     /* 100VG-AnyLAN Token Ring */
216                 WTAP_ENCAP_UNKNOWN,     /* "ISO 8802/3 and Ethernet" */
217                 WTAP_ENCAP_UNKNOWN,     /* 100BaseT (but that's just Ethernet) */
218                 WTAP_ENCAP_IP_OVER_IB,  /* Infiniband */
219         };
220         #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
221         #define SNOOP_PRIVATE_BIT 0x80000000
222         static const int snoop_private_encap[] = {
223                 WTAP_ENCAP_UNKNOWN,     /* Not Used */
224                 WTAP_ENCAP_UNKNOWN,     /* IPv4 Tunnel Link */
225                 WTAP_ENCAP_UNKNOWN,     /* IPv6 Tunnel Link */
226                 WTAP_ENCAP_UNKNOWN,     /* Virtual network interface */
227                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.11 */
228                 WTAP_ENCAP_IPNET,       /* ipnet(7D) link */
229                 WTAP_ENCAP_UNKNOWN,     /* IPMP stub interface */
230                 WTAP_ENCAP_UNKNOWN,     /* 6to4 Tunnel Link */
231         };
232         #define NUM_SNOOP_PRIVATE_ENCAPS (sizeof snoop_private_encap / sizeof snoop_private_encap[0])
233         static const int shomiti_encap[] = {
234                 WTAP_ENCAP_ETHERNET,    /* IEEE 802.3 */
235                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.4 Token Bus */
236                 WTAP_ENCAP_TOKEN_RING,
237                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.6 Metro Net */
238                 WTAP_ENCAP_ETHERNET,
239                 WTAP_ENCAP_UNKNOWN,     /* HDLC */
240                 WTAP_ENCAP_UNKNOWN,     /* Character Synchronous, e.g. bisync */
241                 WTAP_ENCAP_UNKNOWN,     /* IBM Channel-to-Channel */
242                 WTAP_ENCAP_FDDI_BITSWAPPED,
243                 WTAP_ENCAP_UNKNOWN,     /* Other */
244                 WTAP_ENCAP_ETHERNET,    /* Fast Ethernet */
245                 WTAP_ENCAP_TOKEN_RING,  /* 4MB 802.5 token ring */
246                 WTAP_ENCAP_ETHERNET,    /* Gigabit Ethernet */
247                 WTAP_ENCAP_TOKEN_RING,  /* "IEEE 802.5 Shomiti" */
248                 WTAP_ENCAP_TOKEN_RING,  /* "4MB IEEE 802.5 Shomiti" */
249                 WTAP_ENCAP_UNKNOWN,     /* Other */
250                 WTAP_ENCAP_UNKNOWN,     /* Other */
251                 WTAP_ENCAP_UNKNOWN,     /* Other */
252                 WTAP_ENCAP_IEEE_802_11_WITH_RADIO, /* IEEE 802.11 with Radio Header */
253                 WTAP_ENCAP_ETHERNET,    /* 10 Gigabit Ethernet */
254         };
255         #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
256         int file_encap;
257         gint64 saved_offset;
258
259         /* Read in the string that should be at the start of a "snoop" file */
260         errno = WTAP_ERR_CANT_READ;
261         bytes_read = file_read(magic, sizeof magic, wth->fh);
262         if (bytes_read != sizeof magic) {
263                 *err = file_error(wth->fh, err_info);
264                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
265                         return -1;
266                 return 0;
267         }
268
269         if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
270                 return 0;
271         }
272
273         /* Read the rest of the header. */
274         errno = WTAP_ERR_CANT_READ;
275         bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
276         if (bytes_read != sizeof hdr) {
277                 *err = file_error(wth->fh, err_info);
278                 if (*err == 0)
279                         *err = WTAP_ERR_SHORT_READ;
280                 return -1;
281         }
282
283         /*
284          * Make sure it's a version we support.
285          */
286         hdr.version = g_ntohl(hdr.version);
287         switch (hdr.version) {
288
289         case 2:         /* Solaris 2.x and later snoop, and Shomiti
290                            Surveyor prior to 3.0, or 3.0 and later
291                            with NDIS card */
292         case 3:         /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
293         case 4:         /* Surveyor 3.0 and later, with Shomiti GAM hardware */
294         case 5:         /* Surveyor 3.0 and later, with Shomiti THG hardware */
295                 break;
296
297         default:
298                 *err = WTAP_ERR_UNSUPPORTED;
299                 *err_info = g_strdup_printf("snoop: version %u unsupported", hdr.version);
300                 return -1;
301         }
302
303         /*
304          * Oh, this is lovely.
305          *
306          * I suppose Shomiti could give a bunch of lawyerly noise about
307          * how "well, RFC 1761 said they were unassigned, and that's
308          * the standard, not the DLPI header file, so it's perfectly OK
309          * for us to use them, blah blah blah", but it's still irritating
310          * as hell that they used the unassigned-in-RFC-1761 values for
311          * their own purposes - especially given that Sun also used
312          * one of them in atmsnoop.
313          *
314          * We can't determine whether it's a Shomiti capture based on
315          * the version number, as, according to their documentation on
316          * their capture file format, Shomiti uses a version number of 2
317          * if the data "was captured using an NDIS card", which presumably
318          * means "captured with an ordinary boring network card via NDIS"
319          * as opposed to "captured with our whizzo special capture
320          * hardware".
321          *
322          * The only way I can see to determine that is to check how much
323          * padding there is in the first packet - if there's enough
324          * padding for a Shomiti trailer, it's probably a Shomiti
325          * capture, and otherwise, it's probably from Snoop.
326          */
327
328         /*
329          * Start out assuming it's not a Shomiti capture.
330          */
331         is_shomiti = FALSE;
332
333         /* Read first record header. */
334         saved_offset = file_tell(wth->fh);
335         errno = WTAP_ERR_CANT_READ;
336         bytes_read = file_read(&rec_hdr, sizeof rec_hdr, wth->fh);
337         if (bytes_read != sizeof rec_hdr) {
338                 *err = file_error(wth->fh, err_info);
339                 if (*err == 0)
340                         *err = WTAP_ERR_SHORT_READ;
341                 return -1;
342
343                 /*
344                  * The file ends after the record header, which means this
345                  * is a capture with no packets.
346                  *
347                  * We assume it's a snoop file; the actual type of file is
348                  * irrelevant, as there are no records in it, and thus no
349                  * extra information if it's a Shomiti capture, and no
350                  * link-layer headers whose type we have to know, and no
351                  * Ethernet frames that might have an FCS.
352                  */
353         } else {
354                 /*
355                  * Compute the number of bytes of padding in the
356                  * record.  If it's at least the size of a Shomiti
357                  * trailer record, we assume this is a Shomiti
358                  * capture.  (Some atmsnoop captures appear
359                  * to have 4 bytes of padding, and at least one
360                  * snoop capture appears to have 6 bytes of padding;
361                  * the Shomiti header is larger than either of those.)
362                  */
363                 if (g_ntohl(rec_hdr.rec_len) >
364                     (sizeof rec_hdr + g_ntohl(rec_hdr.incl_len))) {
365                         /*
366                          * Well, we have padding; how much?
367                          */
368                         padbytes = g_ntohl(rec_hdr.rec_len) -
369                             ((guint)sizeof rec_hdr + g_ntohl(rec_hdr.incl_len));
370
371                         /*
372                          * Is it at least the size of a Shomiti trailer?
373                          */
374                         is_shomiti =
375                             (padbytes >= sizeof (struct shomiti_trailer));
376                 }
377         }
378
379         /*
380          * Seek back to the beginning of the first record.
381          */
382         if (file_seek(wth->fh, saved_offset, SEEK_SET, err) == -1)
383                 return -1;
384
385         hdr.network = g_ntohl(hdr.network);
386         if (is_shomiti) {
387                 if (hdr.network >= NUM_SHOMITI_ENCAPS
388                     || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
389                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
390                         *err_info = g_strdup_printf("snoop: Shomiti network type %u unknown or unsupported",
391                             hdr.network);
392                         return -1;
393                 }
394                 file_encap = shomiti_encap[hdr.network];
395
396                 /* This is a Shomiti file */
397                 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SHOMITI;
398         } else if (hdr.network & SNOOP_PRIVATE_BIT) {
399                 if ((hdr.network^SNOOP_PRIVATE_BIT) >= NUM_SNOOP_PRIVATE_ENCAPS
400                     || snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT] == WTAP_ENCAP_UNKNOWN) {
401                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
402                         *err_info = g_strdup_printf("snoop: private network type %u unknown or unsupported",
403                             hdr.network);
404                         return -1;
405                 }
406                 file_encap = snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT];
407
408                 /* This is a snoop file */
409                 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
410         } else {
411                 if (hdr.network >= NUM_SNOOP_ENCAPS
412                     || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
413                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
414                         *err_info = g_strdup_printf("snoop: network type %u unknown or unsupported",
415                             hdr.network);
416                         return -1;
417                 }
418                 file_encap = snoop_encap[hdr.network];
419
420                 /* This is a snoop file */
421                 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
422         }
423
424         /*
425          * We don't currently use the extra information in Shomiti
426          * records, so we use the same routines to read snoop and
427          * Shomiti files.
428          */
429         wth->subtype_read = snoop_read;
430         wth->subtype_seek_read = snoop_seek_read;
431         wth->file_encap = file_encap;
432         wth->snapshot_length = 0;       /* not available in header */
433         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
434         return 1;
435 }
436
437 typedef struct {
438         guint8 pad[4];
439         guint8 undecrypt[2];
440         guint8 rate;
441         guint8 preamble;
442         guint8 code;
443         guint8 signal;
444         guint8 qual;
445         guint8 channel;
446 } shomiti_wireless_header;
447
448
449 /* Read the next packet */
450 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
451     gint64 *data_offset)
452 {
453         int     padbytes;
454         int     bytes_read;
455         char    padbuf[4];
456         int     bytes_to_read;
457
458         *data_offset = file_tell(wth->fh);
459
460         padbytes = snoop_read_packet(wth, wth->fh, &wth->phdr,
461             wth->frame_buffer, err, err_info);
462         if (padbytes == -1)
463                 return FALSE;
464
465         /*
466          * Skip over the padding (don't "fseek()", as the standard
467          * I/O library on some platforms discards buffered data if
468          * you do that, which means it does a lot more reads).
469          *
470          * XXX - is that still true?
471          *
472          * There's probably not much padding (it's probably padded only
473          * to a 4-byte boundary), so we probably need only do one read.
474          */
475         while (padbytes != 0) {
476                 bytes_to_read = padbytes;
477                 if ((unsigned)bytes_to_read > sizeof padbuf)
478                         bytes_to_read = sizeof padbuf;
479                 errno = WTAP_ERR_CANT_READ;
480                 bytes_read = file_read(padbuf, bytes_to_read, wth->fh);
481                 if (bytes_read != bytes_to_read) {
482                         *err = file_error(wth->fh, err_info);
483                         if (*err == 0)
484                                 *err = WTAP_ERR_SHORT_READ;
485                         return FALSE;
486                 }
487                 padbytes -= bytes_read;
488         }
489
490         return TRUE;
491 }
492
493 static gboolean
494 snoop_seek_read(wtap *wth, gint64 seek_off,
495     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
496 {
497         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
498                 return FALSE;
499
500         if (snoop_read_packet(wth, wth->random_fh, phdr, buf, err, err_info) == -1) {
501                 if (*err == 0)
502                         *err = WTAP_ERR_SHORT_READ;
503                 return FALSE;
504         }
505         return TRUE;
506 }
507
508 static int
509 snoop_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
510     Buffer *buf, int *err, gchar **err_info)
511 {
512         struct snooprec_hdr hdr;
513         int     bytes_read;
514         guint32 rec_size;
515         guint32 packet_size;
516         guint32 orig_size;
517         int header_size;
518
519         /* Read record header. */
520         errno = WTAP_ERR_CANT_READ;
521         bytes_read = file_read(&hdr, sizeof hdr, fh);
522         if (bytes_read != sizeof hdr) {
523                 *err = file_error(fh, err_info);
524                 if (*err == 0 && bytes_read != 0)
525                         *err = WTAP_ERR_SHORT_READ;
526                 return -1;
527         }
528
529         rec_size = g_ntohl(hdr.rec_len);
530         orig_size = g_ntohl(hdr.orig_len);
531         packet_size = g_ntohl(hdr.incl_len);
532         if (orig_size > WTAP_MAX_PACKET_SIZE) {
533                 /*
534                  * Probably a corrupt capture file; don't blow up trying
535                  * to allocate space for an immensely-large packet.
536                  */
537                 *err = WTAP_ERR_BAD_FILE;
538                 *err_info = g_strdup_printf("snoop: File has %u-byte original length, bigger than maximum of %u",
539                     orig_size, WTAP_MAX_PACKET_SIZE);
540                 return -1;
541         }
542         if (packet_size > WTAP_MAX_PACKET_SIZE) {
543                 /*
544                  * Probably a corrupt capture file; don't blow up trying
545                  * to allocate space for an immensely-large packet.
546                  */
547                 *err = WTAP_ERR_BAD_FILE;
548                 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than maximum of %u",
549                     packet_size, WTAP_MAX_PACKET_SIZE);
550                 return -1;
551         }
552         if (packet_size > rec_size) {
553                 /*
554                  * Probably a corrupt capture file.
555                  */
556                 *err = WTAP_ERR_BAD_FILE;
557                 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than record size %u",
558                     packet_size, rec_size);
559                 return -1;
560         }
561
562         switch (wth->file_encap) {
563
564         case WTAP_ENCAP_ATM_PDUS:
565                 /*
566                  * This is an ATM packet, so the first four bytes are
567                  * the direction of the packet (transmit/receive), the
568                  * VPI, and the VCI; read them and generate the
569                  * pseudo-header from them.
570                  */
571                 if (packet_size < sizeof (struct snoop_atm_hdr)) {
572                         /*
573                          * Uh-oh, the packet isn't big enough to even
574                          * have a pseudo-header.
575                          */
576                         *err = WTAP_ERR_BAD_FILE;
577                         *err_info = g_strdup_printf("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header",
578                             packet_size);
579                         return -1;
580                 }
581                 if (!snoop_read_atm_pseudoheader(fh, &phdr->pseudo_header,
582                     err, err_info))
583                         return -1;      /* Read error */
584
585                 /*
586                  * Don't count the pseudo-header as part of the packet.
587                  */
588                 rec_size -= (guint32)sizeof (struct snoop_atm_hdr);
589                 orig_size -= (guint32)sizeof (struct snoop_atm_hdr);
590                 packet_size -= (guint32)sizeof (struct snoop_atm_hdr);
591                 break;
592
593         case WTAP_ENCAP_ETHERNET:
594                 /*
595                  * If this is a snoop file, we assume there's no FCS in
596                  * this frame; if this is a Shomit file, we assume there
597                  * is.  (XXX - or should we treat it a "maybe"?)
598                  */
599                 if (wth->file_type_subtype == WTAP_FILE_TYPE_SUBTYPE_SHOMITI)
600                         phdr->pseudo_header.eth.fcs_len = 4;
601                 else
602                         phdr->pseudo_header.eth.fcs_len = 0;
603                 break;
604
605         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
606                 if (packet_size < sizeof (shomiti_wireless_header)) {
607                         /*
608                          * Uh-oh, the packet isn't big enough to even
609                          * have a pseudo-header.
610                          */
611                         *err = WTAP_ERR_BAD_FILE;
612                         *err_info = g_strdup_printf("snoop: Shomiti wireless file has a %u-byte packet, too small to have even a wireless pseudo-header",
613                             packet_size);
614                         return -1;
615                 }
616                 if (!snoop_read_shomiti_wireless_pseudoheader(fh,
617                     &phdr->pseudo_header, err, err_info, &header_size))
618                         return -1;      /* Read error */
619
620                 /*
621                  * Don't count the pseudo-header as part of the packet.
622                  */
623                 rec_size -= header_size;
624                 orig_size -= header_size;
625                 packet_size -= header_size;
626                 break;
627         }
628
629         phdr->rec_type = REC_TYPE_PACKET;
630         phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
631         phdr->ts.secs = g_ntohl(hdr.ts_sec);
632         phdr->ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
633         phdr->caplen = packet_size;
634         phdr->len = orig_size;
635
636         if (rec_size < (sizeof hdr + packet_size)) {
637                 /*
638                  * What, *negative* padding?  Bogus.
639                  */
640                 *err = WTAP_ERR_BAD_FILE;
641                 *err_info = g_strdup_printf("snoop: File has %u-byte record with packet size of %u",
642                     rec_size, packet_size);
643                 return -1;
644         }
645
646         /*
647          * Read the packet data.
648          */
649         if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
650                 return -1;      /* failed */
651
652         /*
653          * If this is ATM LANE traffic, try to guess what type of LANE
654          * traffic it is based on the packet contents.
655          */
656         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
657             phdr->pseudo_header.atm.type == TRAF_LANE) {
658                 atm_guess_lane_type(phdr, buffer_start_ptr(buf));
659         }
660
661         return rec_size - ((guint)sizeof hdr + packet_size);
662 }
663
664 static gboolean
665 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
666     int *err, gchar **err_info)
667 {
668         struct snoop_atm_hdr atm_phdr;
669         int     bytes_read;
670         guint8  vpi;
671         guint16 vci;
672
673         errno = WTAP_ERR_CANT_READ;
674         bytes_read = file_read(&atm_phdr, sizeof (struct snoop_atm_hdr), fh);
675         if (bytes_read != sizeof (struct snoop_atm_hdr)) {
676                 *err = file_error(fh, err_info);
677                 if (*err == 0)
678                         *err = WTAP_ERR_SHORT_READ;
679                 return FALSE;
680         }
681
682         vpi = atm_phdr.vpi;
683         vci = pntoh16(&atm_phdr.vci);
684
685         /*
686          * The lower 4 bits of the first byte of the header indicate
687          * the type of traffic, as per the "atmioctl.h" header in
688          * SunATM.
689          */
690         switch (atm_phdr.flags & 0x0F) {
691
692         case 0x01:      /* LANE */
693                 pseudo_header->atm.aal = AAL_5;
694                 pseudo_header->atm.type = TRAF_LANE;
695                 break;
696
697         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
698                 pseudo_header->atm.aal = AAL_5;
699                 pseudo_header->atm.type = TRAF_LLCMX;
700                 break;
701
702         case 0x05:      /* ILMI */
703                 pseudo_header->atm.aal = AAL_5;
704                 pseudo_header->atm.type = TRAF_ILMI;
705                 break;
706
707         case 0x06:      /* Signalling AAL */
708                 pseudo_header->atm.aal = AAL_SIGNALLING;
709                 pseudo_header->atm.type = TRAF_UNKNOWN;
710                 break;
711
712         case 0x03:      /* MARS (RFC 2022) */
713                 pseudo_header->atm.aal = AAL_5;
714                 pseudo_header->atm.type = TRAF_UNKNOWN;
715                 break;
716
717         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
718                 pseudo_header->atm.aal = AAL_5;
719                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
720                 break;
721
722         default:
723                 /*
724                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
725                  * case assume it's AAL_SIGNALLING; we know nothing more
726                  * about it.
727                  *
728                  * XXX - is this necessary?  Or are we guaranteed that
729                  * all signalling traffic has a type of 0x06?
730                  *
731                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
732                  * 0x00 ("raw"), might it be non-AAL5 traffic?
733                  */
734                 if (vpi == 0 && vci == 5)
735                         pseudo_header->atm.aal = AAL_SIGNALLING;
736                 else
737                         pseudo_header->atm.aal = AAL_5;
738                 pseudo_header->atm.type = TRAF_UNKNOWN;
739                 break;
740         }
741         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
742
743         pseudo_header->atm.vpi = vpi;
744         pseudo_header->atm.vci = vci;
745         pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 0 : 1;
746
747         /* We don't have this information */
748         pseudo_header->atm.flags = 0;
749         pseudo_header->atm.cells = 0;
750         pseudo_header->atm.aal5t_u2u = 0;
751         pseudo_header->atm.aal5t_len = 0;
752         pseudo_header->atm.aal5t_chksum = 0;
753
754         return TRUE;
755 }
756
757 static gboolean
758 snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
759     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
760     int *header_size)
761 {
762         shomiti_wireless_header whdr;
763         int     bytes_read;
764         int     rsize;
765
766         errno = WTAP_ERR_CANT_READ;
767         bytes_read = file_read(&whdr, sizeof (shomiti_wireless_header), fh);
768         if (bytes_read != sizeof (shomiti_wireless_header)) {
769                 *err = file_error(fh, err_info);
770                 if (*err == 0)
771                         *err = WTAP_ERR_SHORT_READ;
772                 return FALSE;
773         }
774
775         /* the 4th byte of the pad is actually a header length,
776          * we've already read 8 bytes of it, and it must never
777          * be less than 8.
778          *
779          * XXX - presumably that means that the header length
780          * doesn't include the length field, as we've read
781          * 12 bytes total.
782          *
783          * XXX - what's in the other 3 bytes of the padding?  Is it a
784          * 4-byte length field?
785          * XXX - is there anything in the rest of the header of interest?
786          * XXX - are there any files where the header is shorter than
787          * 4 bytes of length plus 8 bytes of information?
788          */
789         if (whdr.pad[3] < 8) {
790                 *err = WTAP_ERR_BAD_FILE;
791                 *err_info = g_strdup_printf("snoop: Header length in Surveyor record is %u, less than minimum of 8",
792                     whdr.pad[3]);
793                 return FALSE;
794         }
795         /* Skip the header. */
796         rsize = ((int) whdr.pad[3]) - 8;
797         if (file_seek(fh, rsize, SEEK_CUR, err) == -1)
798                 return FALSE;
799
800         pseudo_header->ieee_802_11.fcs_len = 4;
801         pseudo_header->ieee_802_11.channel = whdr.channel;
802         pseudo_header->ieee_802_11.data_rate = whdr.rate;
803         pseudo_header->ieee_802_11.signal_level = whdr.signal;
804
805         /* add back the header and don't forget the pad as well */
806         *header_size = rsize + 8 + 4;
807
808         return TRUE;
809 }
810
811 static const int wtap_encap[] = {
812         -1,             /* WTAP_ENCAP_UNKNOWN -> unsupported */
813         0x04,           /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
814         0x02,           /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
815         -1,             /* WTAP_ENCAP_SLIP -> unsupported */
816         -1,             /* WTAP_ENCAP_PPP -> unsupported */
817         0x08,           /* WTAP_ENCAP_FDDI -> DL_FDDI */
818         0x08,           /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
819         -1,             /* WTAP_ENCAP_RAW_IP -> unsupported */
820         -1,             /* WTAP_ENCAP_ARCNET -> unsupported */
821         -1,             /* WTAP_ENCAP_ARCNET_LINUX -> unsupported */
822         -1,             /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
823         -1,             /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
824         -1,             /* WTAP_ENCAP_LAPB -> unsupported*/
825         0x12,           /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
826 };
827 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
828
829 /* Returns 0 if we could write the specified encapsulation type,
830    an error indication otherwise. */
831 int snoop_dump_can_write_encap(int encap)
832 {
833         /* Per-packet encapsulations aren't supported. */
834         if (encap == WTAP_ENCAP_PER_PACKET)
835                 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
836
837         if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
838                 return WTAP_ERR_UNSUPPORTED_ENCAP;
839
840         return 0;
841 }
842
843 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
844    failure */
845 gboolean snoop_dump_open(wtap_dumper *wdh, int *err)
846 {
847         struct snoop_hdr file_hdr;
848
849         /* This is a snoop file */
850         wdh->subtype_write = snoop_dump;
851         wdh->subtype_close = NULL;
852
853         /* Write the file header. */
854         if (!wtap_dump_file_write(wdh, &snoop_magic, sizeof snoop_magic, err))
855                 return FALSE;
856
857         /* current "snoop" format is 2 */
858         file_hdr.version = g_htonl(2);
859         file_hdr.network = g_htonl(wtap_encap[wdh->encap]);
860         if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
861                 return FALSE;
862
863         return TRUE;
864 }
865
866 /* Write a record for a packet to a dump file.
867    Returns TRUE on success, FALSE on failure. */
868 static gboolean snoop_dump(wtap_dumper *wdh,
869         const struct wtap_pkthdr *phdr,
870         const guint8 *pd, int *err)
871 {
872         const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
873         struct snooprec_hdr rec_hdr;
874         int reclen;
875         guint padlen;
876         static const char zeroes[4] = {0};
877         struct snoop_atm_hdr atm_hdr;
878         int atm_hdrsize;
879
880         /* We can only write packet records. */
881         if (phdr->rec_type != REC_TYPE_PACKET) {
882                 *err = WTAP_ERR_REC_TYPE_UNSUPPORTED;
883                 return FALSE;
884         }
885
886         if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
887                 atm_hdrsize = sizeof (struct snoop_atm_hdr);
888         else
889                 atm_hdrsize = 0;
890
891         /* Record length = header length plus data length... */
892         reclen = (int)sizeof rec_hdr + phdr->caplen + atm_hdrsize;
893
894
895         /* ... plus enough bytes to pad it to a 4-byte boundary. */
896         padlen = ((reclen + 3) & ~3) - reclen;
897         reclen += padlen;
898
899         /* Don't write anything we're not willing to read. */
900         if (phdr->caplen + atm_hdrsize > WTAP_MAX_PACKET_SIZE) {
901                 *err = WTAP_ERR_PACKET_TOO_LARGE;
902                 return FALSE;
903         }
904
905         rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
906         rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
907         rec_hdr.rec_len = g_htonl(reclen);
908         rec_hdr.cum_drops = 0;
909         rec_hdr.ts_sec = g_htonl(phdr->ts.secs);
910         rec_hdr.ts_usec = g_htonl(phdr->ts.nsecs / 1000);
911         if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
912                 return FALSE;
913
914         if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
915                 /*
916                  * Write the ATM header.
917                  */
918                 atm_hdr.flags =
919                     (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
920                 switch (pseudo_header->atm.aal) {
921
922                 case AAL_SIGNALLING:
923                         /* Signalling AAL */
924                         atm_hdr.flags |= 0x06;
925                         break;
926
927                 case AAL_5:
928                         switch (pseudo_header->atm.type) {
929
930                         case TRAF_LANE:
931                                 /* LANE */
932                                 atm_hdr.flags |= 0x01;
933                                 break;
934
935                         case TRAF_LLCMX:
936                                 /* RFC 1483 LLC multiplexed traffic */
937                                 atm_hdr.flags |= 0x02;
938                                 break;
939
940                         case TRAF_ILMI:
941                                 /* ILMI */
942                                 atm_hdr.flags |= 0x05;
943                                 break;
944                         }
945                         break;
946                 }
947                 atm_hdr.vpi = (guint8) pseudo_header->atm.vpi;
948                 atm_hdr.vci = g_htons(pseudo_header->atm.vci);
949                 if (!wtap_dump_file_write(wdh, &atm_hdr, sizeof atm_hdr, err))
950                         return FALSE;
951         }
952
953         if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
954                 return FALSE;
955
956         /* Now write the padding. */
957         if (!wtap_dump_file_write(wdh, zeroes, padlen, err))
958                 return FALSE;
959         return TRUE;
960 }