From Harald Welte:
[obnox/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.ts.secs = g_ntohl(hdr.ts_sec);
595         wth->phdr.ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
596         wth->phdr.caplen = packet_size;
597         wth->phdr.len = orig_size;
598
599         /*
600          * If this is ATM LANE traffic, try to guess what type of LANE
601          * traffic it is based on the packet contents.
602          */
603         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
604             wth->pseudo_header.atm.type == TRAF_LANE) {
605                 atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
606                     wth->phdr.caplen, &wth->pseudo_header);
607         }
608
609         /*
610          * Skip over the padding (don't "fseek()", as the standard
611          * I/O library on some platforms discards buffered data if
612          * you do that, which means it does a lot more reads).
613          * There's probably not much padding (it's probably padded only
614          * to a 4-byte boundary), so we probably need only do one read.
615          */
616         if (rec_size < (sizeof hdr + packet_size)) {
617                 /*
618                  * What, *negative* padding?  Bogus.
619                  */
620                 *err = WTAP_ERR_BAD_FILE;
621                 *err_info = g_strdup_printf("snoop: File has %u-byte record with packet size of %u",
622                     rec_size, packet_size);
623                 return FALSE;
624         }
625         padbytes = rec_size - ((guint)sizeof hdr + packet_size);
626         while (padbytes != 0) {
627                 bytes_to_read = padbytes;
628                 if ((unsigned)bytes_to_read > sizeof padbuf)
629                         bytes_to_read = sizeof padbuf;
630                 errno = WTAP_ERR_CANT_READ;
631                 bytes_read = file_read(padbuf, bytes_to_read, wth->fh);
632                 if (bytes_read != bytes_to_read) {
633                         *err = file_error(wth->fh, err_info);
634                         if (*err == 0)
635                                 *err = WTAP_ERR_SHORT_READ;
636                         return FALSE;
637                 }
638                 wth->data_offset += bytes_read;
639                 padbytes -= bytes_read;
640         }
641
642         return TRUE;
643 }
644
645 static gboolean
646 snoop_seek_read(wtap *wth, gint64 seek_off,
647     union wtap_pseudo_header *pseudo_header, guint8 *pd, int length,
648     int *err, gchar **err_info)
649 {
650         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
651                 return FALSE;
652
653         switch (wth->file_encap) {
654
655         case WTAP_ENCAP_ATM_PDUS:
656                 if (!snoop_read_atm_pseudoheader(wth->random_fh, pseudo_header,
657                     err, err_info)) {
658                         /* Read error */
659                         return FALSE;
660                 }
661                 break;
662
663         case WTAP_ENCAP_ETHERNET:
664                 /*
665                  * If this is a snoop file, we assume there's no FCS in
666                  * this frame; if this is a Shomit file, we assume there
667                  * is.  (XXX - or should we treat it a "maybe"?)
668                  */
669                 if (wth->file_type == WTAP_FILE_SHOMITI)
670                         pseudo_header->eth.fcs_len = 4;
671                 else
672                         pseudo_header->eth.fcs_len = 0;
673                 break;
674
675         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
676                 if (!snoop_read_shomiti_wireless_pseudoheader(wth->random_fh,
677                     pseudo_header, err, err_info, NULL)) {
678                         /* Read error */
679                         return FALSE;
680                 }
681                 break;
682         }
683
684         /*
685          * Read the packet data.
686          */
687         if (!snoop_read_rec_data(wth->random_fh, pd, length, err, err_info))
688                 return FALSE;   /* failed */
689
690         /*
691          * If this is ATM LANE traffic, try to guess what type of LANE
692          * traffic it is based on the packet contents.
693          */
694         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
695             pseudo_header->atm.type == TRAF_LANE)
696                 atm_guess_lane_type(pd, length, pseudo_header);
697         return TRUE;
698 }
699
700 static gboolean
701 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
702     int *err, gchar **err_info)
703 {
704         struct snoop_atm_hdr atm_phdr;
705         int     bytes_read;
706         guint8  vpi;
707         guint16 vci;
708
709         errno = WTAP_ERR_CANT_READ;
710         bytes_read = file_read(&atm_phdr, sizeof (struct snoop_atm_hdr), fh);
711         if (bytes_read != sizeof (struct snoop_atm_hdr)) {
712                 *err = file_error(fh, err_info);
713                 if (*err == 0)
714                         *err = WTAP_ERR_SHORT_READ;
715                 return FALSE;
716         }
717
718         vpi = atm_phdr.vpi;
719         vci = pntohs(&atm_phdr.vci);
720
721         /*
722          * The lower 4 bits of the first byte of the header indicate
723          * the type of traffic, as per the "atmioctl.h" header in
724          * SunATM.
725          */
726         switch (atm_phdr.flags & 0x0F) {
727
728         case 0x01:      /* LANE */
729                 pseudo_header->atm.aal = AAL_5;
730                 pseudo_header->atm.type = TRAF_LANE;
731                 break;
732
733         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
734                 pseudo_header->atm.aal = AAL_5;
735                 pseudo_header->atm.type = TRAF_LLCMX;
736                 break;
737
738         case 0x05:      /* ILMI */
739                 pseudo_header->atm.aal = AAL_5;
740                 pseudo_header->atm.type = TRAF_ILMI;
741                 break;
742
743         case 0x06:      /* Signalling AAL */
744                 pseudo_header->atm.aal = AAL_SIGNALLING;
745                 pseudo_header->atm.type = TRAF_UNKNOWN;
746                 break;
747
748         case 0x03:      /* MARS (RFC 2022) */
749                 pseudo_header->atm.aal = AAL_5;
750                 pseudo_header->atm.type = TRAF_UNKNOWN;
751                 break;
752
753         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
754                 pseudo_header->atm.aal = AAL_5;
755                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
756                 break;
757
758         default:
759                 /*
760                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
761                  * case assume it's AAL_SIGNALLING; we know nothing more
762                  * about it.
763                  *
764                  * XXX - is this necessary?  Or are we guaranteed that
765                  * all signalling traffic has a type of 0x06?
766                  *
767                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
768                  * 0x00 ("raw"), might it be non-AAL5 traffic?
769                  */
770                 if (vpi == 0 && vci == 5)
771                         pseudo_header->atm.aal = AAL_SIGNALLING;
772                 else
773                         pseudo_header->atm.aal = AAL_5;
774                 pseudo_header->atm.type = TRAF_UNKNOWN;
775                 break;
776         }
777         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
778
779         pseudo_header->atm.vpi = vpi;
780         pseudo_header->atm.vci = vci;
781         pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 0 : 1;
782
783         /* We don't have this information */
784         pseudo_header->atm.flags = 0;
785         pseudo_header->atm.cells = 0;
786         pseudo_header->atm.aal5t_u2u = 0;
787         pseudo_header->atm.aal5t_len = 0;
788         pseudo_header->atm.aal5t_chksum = 0;
789
790         return TRUE;
791 }
792
793 static gboolean
794 snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
795     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
796     int *header_size)
797 {
798         shomiti_wireless_header whdr;
799         int     bytes_read;
800         int     rsize;
801
802         errno = WTAP_ERR_CANT_READ;
803         bytes_read = file_read(&whdr, sizeof (shomiti_wireless_header), fh);
804         if (bytes_read != sizeof (shomiti_wireless_header)) {
805                 *err = file_error(fh, err_info);
806                 if (*err == 0)
807                         *err = WTAP_ERR_SHORT_READ;
808                 return FALSE;
809         }
810
811         /* the 4th byte of the pad is actually a header length,
812          * we've already read 8 bytes of it, and it must never
813          * be less than 8.
814          *
815          * XXX - presumably that means that the header length
816          * doesn't include the length field, as we've read
817          * 12 bytes total.
818          *
819          * XXX - what's in the other 3 bytes of the padding?  Is it a
820          * 4-byte length field?
821          * XXX - is there anything in the rest of the header of interest?
822          * XXX - are there any files where the header is shorter than
823          * 4 bytes of length plus 8 bytes of information?
824          */
825         if (whdr.pad[3] < 8) {
826                 *err = WTAP_ERR_BAD_FILE;
827                 *err_info = g_strdup_printf("snoop: Header length in Surveyor record is %u, less than minimum of 8",
828                     whdr.pad[3]);
829                 return FALSE;
830         }
831         /* Skip the header. */
832         rsize = ((int) whdr.pad[3]) - 8;
833         if (file_seek(fh, rsize, SEEK_CUR, err) == -1)
834                 return FALSE;
835
836         pseudo_header->ieee_802_11.fcs_len = 4;
837         pseudo_header->ieee_802_11.channel = whdr.channel;
838         pseudo_header->ieee_802_11.data_rate = whdr.rate;
839         pseudo_header->ieee_802_11.signal_level = whdr.signal;
840
841         /* add back the header and don't forget the pad as well */
842         if(header_size != NULL)
843             *header_size = rsize + 8 + 4;
844
845     return TRUE;
846 }
847
848 static gboolean
849 snoop_read_rec_data(FILE_T fh, guint8 *pd, int length, int *err,
850     gchar **err_info)
851 {
852         int     bytes_read;
853
854         errno = WTAP_ERR_CANT_READ;
855         bytes_read = file_read(pd, length, fh);
856
857         if (bytes_read != length) {
858                 *err = file_error(fh, err_info);
859                 if (*err == 0)
860                         *err = WTAP_ERR_SHORT_READ;
861                 return FALSE;
862         }
863         return TRUE;
864 }
865
866 static const int wtap_encap[] = {
867         -1,             /* WTAP_ENCAP_UNKNOWN -> unsupported */
868         0x04,           /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
869         0x02,           /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
870         -1,             /* WTAP_ENCAP_SLIP -> unsupported */
871         -1,             /* WTAP_ENCAP_PPP -> unsupported */
872         0x08,           /* WTAP_ENCAP_FDDI -> DL_FDDI */
873         0x08,           /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
874         -1,             /* WTAP_ENCAP_RAW_IP -> unsupported */
875         -1,             /* WTAP_ENCAP_ARCNET -> unsupported */
876         -1,             /* WTAP_ENCAP_ARCNET_LINUX -> unsupported */
877         -1,             /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
878         -1,             /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
879         -1,             /* WTAP_ENCAP_LAPB -> unsupported*/
880         0x12,           /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
881 };
882 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
883
884 /* Returns 0 if we could write the specified encapsulation type,
885    an error indication otherwise. */
886 int snoop_dump_can_write_encap(int encap)
887 {
888         /* Per-packet encapsulations aren't supported. */
889         if (encap == WTAP_ENCAP_PER_PACKET)
890                 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
891
892         if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
893                 return WTAP_ERR_UNSUPPORTED_ENCAP;
894
895         return 0;
896 }
897
898 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
899    failure */
900 gboolean snoop_dump_open(wtap_dumper *wdh, int *err)
901 {
902         struct snoop_hdr file_hdr;
903
904         /* This is a snoop file */
905         wdh->subtype_write = snoop_dump;
906         wdh->subtype_close = NULL;
907
908         /* Write the file header. */
909         if (!wtap_dump_file_write(wdh, &snoop_magic, sizeof snoop_magic, err))
910                 return FALSE;
911
912         /* current "snoop" format is 2 */
913         file_hdr.version = g_htonl(2);
914         file_hdr.network = g_htonl(wtap_encap[wdh->encap]);
915         if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
916                 return FALSE;
917
918         return TRUE;
919 }
920
921 /* Write a record for a packet to a dump file.
922    Returns TRUE on success, FALSE on failure. */
923 static gboolean snoop_dump(wtap_dumper *wdh,
924         const struct wtap_pkthdr *phdr,
925         const union wtap_pseudo_header *pseudo_header _U_,
926         const guint8 *pd, int *err)
927 {
928         struct snooprec_hdr rec_hdr;
929         int reclen;
930         guint padlen;
931         static char zeroes[4];
932         struct snoop_atm_hdr atm_hdr;
933         int atm_hdrsize;
934
935         if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
936                 atm_hdrsize = sizeof (struct snoop_atm_hdr);
937         else
938                 atm_hdrsize = 0;
939
940         /* Record length = header length plus data length... */
941         reclen = (int)sizeof rec_hdr + phdr->caplen + atm_hdrsize;
942
943         /* ... plus enough bytes to pad it to a 4-byte boundary. */
944         padlen = ((reclen + 3) & ~3) - reclen;
945         reclen += padlen;
946
947         rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
948         rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
949         rec_hdr.rec_len = g_htonl(reclen);
950         rec_hdr.cum_drops = 0;
951         rec_hdr.ts_sec = g_htonl(phdr->ts.secs);
952         rec_hdr.ts_usec = g_htonl(phdr->ts.nsecs / 1000);
953         if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
954                 return FALSE;
955
956         if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
957                 /*
958                  * Write the ATM header.
959                  */
960                 atm_hdr.flags =
961                     (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
962                 switch (pseudo_header->atm.aal) {
963
964                 case AAL_SIGNALLING:
965                         /* Signalling AAL */
966                         atm_hdr.flags |= 0x06;
967                         break;
968
969                 case AAL_5:
970                         switch (pseudo_header->atm.type) {
971
972                         case TRAF_LANE:
973                                 /* LANE */
974                                 atm_hdr.flags |= 0x01;
975                                 break;
976
977                         case TRAF_LLCMX:
978                                 /* RFC 1483 LLC multiplexed traffic */
979                                 atm_hdr.flags |= 0x02;
980                                 break;
981
982                         case TRAF_ILMI:
983                                 /* ILMI */
984                                 atm_hdr.flags |= 0x05;
985                                 break;
986                         }
987                         break;
988                 }
989                 atm_hdr.vpi = (guint8) pseudo_header->atm.vpi;
990                 atm_hdr.vci = g_htons(pseudo_header->atm.vci);
991                 if (!wtap_dump_file_write(wdh, &atm_hdr, sizeof atm_hdr, err))
992                         return FALSE;
993         }
994
995         if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
996                 return FALSE;
997
998         /* Now write the padding. */
999         if (!wtap_dump_file_write(wdh, zeroes, padlen, err))
1000                 return FALSE;
1001         return TRUE;
1002 }