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