3 * $Id: snoop.c,v 1.43 2002/03/05 05:58:40 guy Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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.
29 #include "file_wrappers.h"
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
36 /* See RFC 1761 for a description of the "snoop" file format. */
38 /* Magic number in "snoop" files. */
39 static const char snoop_magic[] = {
40 's', 'n', 'o', 'o', 'p', '\0', '\0', '\0'
43 /* "snoop" file header (minus magic number). */
45 guint32 version; /* version number (should be 2) */
46 guint32 network; /* network type */
49 /* "snoop" record header. */
51 guint32 orig_len; /* actual length of packet */
52 guint32 incl_len; /* number of octets captured in file */
53 guint32 rec_len; /* length of record */
54 guint32 cum_drops; /* cumulative number of dropped packets */
55 guint32 ts_sec; /* timestamp seconds */
56 guint32 ts_usec; /* timestamp microseconds */
59 static gboolean snoop_read(wtap *wth, int *err, long *data_offset);
60 static int snoop_seek_read(wtap *wth, long seek_off,
61 union wtap_pseudo_header *pseudo_header, u_char *pd, int length, int *err);
62 static int snoop_read_atm_pseudoheader(FILE_T fh,
63 union wtap_pseudo_header *pseudo_header, int *err);
64 static int snoop_read_rec_data(FILE_T fh, u_char *pd, int length, int *err);
65 static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
66 const union wtap_pseudo_header *pseudo_header, const u_char *pd, int *err);
71 * http://www.opengroup.org/onlinepubs/9638599/apdxf.htm
73 * for the "dlpi.h" header file specified by The Open Group, which lists
74 * the DL_ values for various protocols; Solaris 7 uses the same values.
78 * http://mrpink.lerc.nasa.gov/118x/support.html
80 * has links to modified versions of "tcpdump" and "libpcap" for SUNatm
81 * DLPI support; they suggest the 3.0 verson of SUNatm uses those
84 * It also has a link to "convert.c", which is a program to convert files
85 * from the format written by the "atmsnoop" program that comes with the
86 * SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
87 * to be DL_FDDI (don't ask why). SunATM 3.0 claims to be DL_IPATM, which
90 * It also says that "ATM Mac header is 12 bytes long.", and seems to imply
91 * that in an "atmsnoop" file, the header contains 2 bytes (direction and
92 * VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
93 * type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
94 * of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
95 * pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
96 * 1 byte of VPI, and 2 bytes of VCI.
98 * The aforementioned page also has a link to some capture files from
99 * "atmsnoop"; this version of "snoop.c" appears to be able to read them.
101 * Source to an "atmdump" package, which includes a modified version of
102 * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
103 * also includes "atmdump", which is a modified "tcpdump", says that an
104 * ATM packet handed up from the Sun driver for the Sun SBus ATM card on
105 * Solaris 2.5.1 has 1 byte of direction, 1 byte of VPI, 2 bytes of VCI,
106 * and then the ATM PDU, and suggests that the direction byte is 0x80 for
107 * "transmitted" (presumably meaning DTE->DCE) and presumably not 0x80 for
108 * "received" (presumably meaning DCE->DTE).
110 * In fact, the "direction" byte appears to have some other stuff, perhaps
111 * a traffic type, in the lower 7 bits, with the 8th bit indicating the
114 * I don't know what the encapsulation of any of the other types is, so I
115 * leave them all as WTAP_ENCAP_UNKNOWN. I also don't know whether "snoop"
116 * can handle any of them (it presumably can't handle ATM, otherwise Sun
117 * wouldn't have supplied "atmsnoop"; even if it can't, this may be useful
118 * reference information for anybody doing code to use DLPI to do raw packet
119 * captures on those network types.
123 * http://www.shomiti.com/support/TNCapFileFormat.htm
125 * for information on Shomiti's mutant flavor of snoop. For some unknown
126 * unknown reason, they decided not to just Go With The DLPI Flow, and
127 * instead used the types unspecified in RFC 1461 for their own nefarious
128 * purposes, such as distinguishing 10MB from 100MB from 1000MB Ethernet
129 * and distinguishing 4MB from 16MB Token Ring, and distinguishing both
130 * of them from the "Shomiti" versions of same.
132 int snoop_open(wtap *wth, int *err)
135 char magic[sizeof snoop_magic];
136 struct snoop_hdr hdr;
137 static const int snoop_encap[] = {
138 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
139 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
140 WTAP_ENCAP_TOKEN_RING,
141 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
143 WTAP_ENCAP_UNKNOWN, /* HDLC */
144 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
145 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
146 WTAP_ENCAP_FDDI_BITSWAPPED,
147 WTAP_ENCAP_UNKNOWN, /* Other */
148 WTAP_ENCAP_UNKNOWN, /* Frame Relay LAPF */
149 WTAP_ENCAP_UNKNOWN, /* Multi-protocol over Frame Relay */
150 WTAP_ENCAP_UNKNOWN, /* Character Async (e.g., SLIP and PPP?) */
151 WTAP_ENCAP_UNKNOWN, /* X.25 Classical IP */
152 WTAP_ENCAP_UNKNOWN, /* software loopback */
153 WTAP_ENCAP_UNKNOWN, /* not defined in "dlpi.h" */
154 WTAP_ENCAP_UNKNOWN, /* Fibre Channel */
155 WTAP_ENCAP_UNKNOWN, /* ATM */
156 WTAP_ENCAP_ATM_SNIFFER, /* ATM Classical IP */
157 WTAP_ENCAP_UNKNOWN, /* X.25 LAPB */
158 WTAP_ENCAP_UNKNOWN, /* ISDN */
159 WTAP_ENCAP_UNKNOWN, /* HIPPI */
160 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Ethernet */
161 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Token Ring */
162 WTAP_ENCAP_UNKNOWN, /* "ISO 8802/3 and Ethernet" */
163 WTAP_ENCAP_UNKNOWN, /* 100BaseT (but that's just Ethernet) */
165 #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
166 static const int shomiti_encap[] = {
167 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
168 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
169 WTAP_ENCAP_TOKEN_RING,
170 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
172 WTAP_ENCAP_UNKNOWN, /* HDLC */
173 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
174 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
175 WTAP_ENCAP_FDDI_BITSWAPPED,
176 WTAP_ENCAP_UNKNOWN, /* Other */
177 WTAP_ENCAP_ETHERNET, /* Fast Ethernet */
178 WTAP_ENCAP_TOKEN_RING, /* 4MB 802.5 token ring */
179 WTAP_ENCAP_ETHERNET, /* Gigabit Ethernet */
180 WTAP_ENCAP_TOKEN_RING, /* "IEEE 802.5 Shomiti" */
181 WTAP_ENCAP_TOKEN_RING, /* "4MB IEEE 802.5 Shomiti" */
183 #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
186 /* Read in the string that should be at the start of a "snoop" file */
187 errno = WTAP_ERR_CANT_READ;
188 bytes_read = file_read(magic, 1, sizeof magic, wth->fh);
189 if (bytes_read != sizeof magic) {
190 *err = file_error(wth->fh);
195 wth->data_offset += sizeof magic;
197 if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
201 /* Read the rest of the header. */
202 errno = WTAP_ERR_CANT_READ;
203 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
204 if (bytes_read != sizeof hdr) {
205 *err = file_error(wth->fh);
210 wth->data_offset += sizeof hdr;
213 * Oh, this is lovely.
215 * I suppose Shomiti could give a bunch of lawyerly noise about
216 * how "well, RFC 1761 said they were unassigned, and that's
217 * the standard, not the DLPI header file, so it's perfectly OK
218 * for us to use them, blah blah blah", but it's still irritating
219 * as hell that they used the unassigned-in-RFC-1761 values for
220 * their own purposes - especially given that Sun also used
221 * one of them in atmsnoop.
223 * For now, we treat a version number of 2 as indicating that
224 * this is a Sun snoop file, and version numbers of 3, 4, and 5
225 * as indicating that this is a Shomiti file, even though
226 * their capture file format documentation claims that they
227 * use 2 if the data "was captured using an NDIS card", which
228 * presumably means "captured with an ordinary boring network
229 * card via NDIS" as opposed to "captured with our whizzo
230 * special capture hardware".
232 * This runs the risk that we may misinterpret the network
233 * type of Shomiti captures not done using their hardware.
234 * Currently, the only not-in-RFC-1761 type we interpret in
235 * Sun snoop files is 18, for atmsnoop, and that's not used
236 * by Shomiti, but if any of the types used by Shomiti are
237 * also used by Snoop or a variant thereof - e.g.:
239 * value snoop Shomiti
240 * 10 Frame Relay 100MB Ethernet
241 * 11 MP over Frame Relay 4MB 802.5
242 * 12 "Character Async" 1000MB Ethernet
243 * 13 X.25 Classical IP "IEEE 802.5 Shomiti"
244 * 14 "software loopback" "4MB IEEE 802.5 Shomiti"
246 * then we have a problem that may be resolvable only by checking
247 * how much padding there is in the first packet - if there're 3
248 * bytes or less, it's probably Sun snoop, which uses the padding
249 * only for padding, but if there's more, it's probably a Shomiti
250 * tool, which uses the padding for additional information.
252 hdr.version = ntohl(hdr.version);
253 hdr.network = ntohl(hdr.network);
254 switch (hdr.version) {
256 case 2: /* Solaris 2.x and later snoop, and Shomiti
257 Surveyor prior to 3.0 (or 3.x with NDIS card?) */
258 if (hdr.network >= NUM_SNOOP_ENCAPS
259 || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
260 g_message("snoop: network type %u unknown or unsupported",
262 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
265 file_encap = snoop_encap[hdr.network];
268 case 3: /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
269 case 4: /* Surveyor 3.0 and later, with Shomiti GAM hardware */
270 case 5: /* Surveyor 3.0 and later, with Shomiti THG hardware */
271 if (hdr.network >= NUM_SHOMITI_ENCAPS
272 || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
273 g_message("snoop: Shomiti network type %u unknown or unsupported",
275 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
278 file_encap = shomiti_encap[hdr.network];
282 g_message("snoop: version %u unsupported", hdr.version);
283 *err = WTAP_ERR_UNSUPPORTED;
287 /* This is a snoop file */
288 wth->file_type = WTAP_FILE_SNOOP;
289 wth->subtype_read = snoop_read;
290 wth->subtype_seek_read = snoop_seek_read;
291 wth->file_encap = file_encap;
292 wth->snapshot_length = 0; /* not available in header */
296 /* Read the next packet */
297 static gboolean snoop_read(wtap *wth, int *err, long *data_offset)
303 struct snooprec_hdr hdr;
308 /* Read record header. */
309 errno = WTAP_ERR_CANT_READ;
310 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
311 if (bytes_read != sizeof hdr) {
312 *err = file_error(wth->fh);
313 if (*err == 0 && bytes_read != 0) {
314 *err = WTAP_ERR_SHORT_READ;
318 wth->data_offset += sizeof hdr;
320 rec_size = ntohl(hdr.rec_len);
321 orig_size = ntohl(hdr.orig_len);
322 packet_size = ntohl(hdr.incl_len);
323 if (packet_size > WTAP_MAX_PACKET_SIZE) {
325 * Probably a corrupt capture file; don't blow up trying
326 * to allocate space for an immensely-large packet.
328 g_message("snoop: File has %u-byte packet, bigger than maximum of %u",
329 packet_size, WTAP_MAX_PACKET_SIZE);
330 *err = WTAP_ERR_BAD_RECORD;
334 *data_offset = wth->data_offset;
337 * If this is an ATM packet, the first four bytes are the
338 * direction of the packet (transmit/receive), the VPI, and
339 * the VCI; read them and generate the pseudo-header from
342 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER) {
343 if (packet_size < 4) {
345 * Uh-oh, the packet isn't big enough to even
346 * have a pseudo-header.
348 g_message("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header\n",
350 *err = WTAP_ERR_BAD_RECORD;
353 if (snoop_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
355 return FALSE; /* Read error */
358 * Don't count the pseudo-header as part of the packet.
363 wth->data_offset += 4;
366 buffer_assure_space(wth->frame_buffer, packet_size);
367 if (snoop_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
368 packet_size, err) < 0)
369 return FALSE; /* Read error */
370 wth->data_offset += packet_size;
372 wth->phdr.ts.tv_sec = ntohl(hdr.ts_sec);
373 wth->phdr.ts.tv_usec = ntohl(hdr.ts_usec);
374 wth->phdr.caplen = packet_size;
375 wth->phdr.len = orig_size;
376 wth->phdr.pkt_encap = wth->file_encap;
379 * Skip over the padding (don't "fseek()", as the standard
380 * I/O library on some platforms discards buffered data if
381 * you do that, which means it does a lot more reads).
382 * There's probably not much padding (it's probably padded only
383 * to a 4-byte boundary), so we probably need only do one read.
385 padbytes = rec_size - (sizeof hdr + packet_size);
386 while (padbytes != 0) {
387 bytes_to_read = padbytes;
388 if ((unsigned)bytes_to_read > sizeof padbuf)
389 bytes_to_read = sizeof padbuf;
390 errno = WTAP_ERR_CANT_READ;
391 bytes_read = file_read(padbuf, 1, bytes_to_read, wth->fh);
392 if (bytes_read != bytes_to_read) {
393 *err = file_error(wth->fh);
395 *err = WTAP_ERR_SHORT_READ;
398 wth->data_offset += bytes_read;
399 padbytes -= bytes_read;
406 snoop_seek_read(wtap *wth, long seek_off,
407 union wtap_pseudo_header *pseudo_header, u_char *pd, int length, int *err)
411 if (file_seek(wth->random_fh, seek_off, SEEK_SET) == -1) {
412 *err = file_error(wth->random_fh);
416 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER) {
417 ret = snoop_read_atm_pseudoheader(wth->random_fh, pseudo_header,
426 * Read the packet data.
428 return snoop_read_rec_data(wth->random_fh, pd, length, err);
432 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
438 errno = WTAP_ERR_CANT_READ;
439 bytes_read = file_read(atm_phdr, 1, 4, fh);
440 if (bytes_read != 4) {
441 *err = file_error(fh);
443 *err = WTAP_ERR_SHORT_READ;
447 pseudo_header->ngsniffer_atm.channel = (atm_phdr[0] & 0x80) ? 1 : 0;
448 pseudo_header->ngsniffer_atm.Vpi = atm_phdr[1];
449 pseudo_header->ngsniffer_atm.Vci = pntohs(&atm_phdr[2]);
451 /* We don't have this information */
452 pseudo_header->ngsniffer_atm.cells = 0;
453 pseudo_header->ngsniffer_atm.aal5t_u2u = 0;
454 pseudo_header->ngsniffer_atm.aal5t_len = 0;
455 pseudo_header->ngsniffer_atm.aal5t_chksum = 0;
458 * Assume it's AAL5; we know nothing more about it.
460 * For what it's worth, in one "atmsnoop" capture,
461 * the lower 7 bits of the first byte of the header
462 * were 0x05 for ILMI traffic, 0x06 for Signalling
463 * AAL traffic, and 0x02 for at least some RFC 1483-style
464 * LLC multiplexed traffic.
466 pseudo_header->ngsniffer_atm.AppTrafType = ATT_AAL5|ATT_HL_UNKNOWN;
467 pseudo_header->ngsniffer_atm.AppHLType = AHLT_UNKNOWN;
473 snoop_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
477 errno = WTAP_ERR_CANT_READ;
478 bytes_read = file_read(pd, 1, length, fh);
480 if (bytes_read != length) {
481 *err = file_error(fh);
483 *err = WTAP_ERR_SHORT_READ;
489 static const int wtap_encap[] = {
490 -1, /* WTAP_ENCAP_UNKNOWN -> unsupported */
491 0x04, /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
492 0x02, /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
493 -1, /* WTAP_ENCAP_SLIP -> unsupported */
494 -1, /* WTAP_ENCAP_PPP -> unsupported */
495 0x08, /* WTAP_ENCAP_FDDI -> DL_FDDI */
496 0x08, /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
497 -1, /* WTAP_ENCAP_RAW_IP -> unsupported */
498 -1, /* WTAP_ENCAP_ARCNET -> unsupported */
499 -1, /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
500 -1, /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
501 -1, /* WTAP_ENCAP_LAPB -> unsupported*/
502 -1, /* WTAP_ENCAP_ATM_SNIFFER -> unsupported */
503 0 /* WTAP_ENCAP_NULL -> DLT_NULL */
505 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
507 /* Returns 0 if we could write the specified encapsulation type,
508 an error indication otherwise. */
509 int snoop_dump_can_write_encap(int encap)
511 /* Per-packet encapsulations aren't supported. */
512 if (encap == WTAP_ENCAP_PER_PACKET)
513 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
515 if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
516 return WTAP_ERR_UNSUPPORTED_ENCAP;
521 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
523 gboolean snoop_dump_open(wtap_dumper *wdh, int *err)
525 struct snoop_hdr file_hdr;
528 /* This is a snoop file */
529 wdh->subtype_write = snoop_dump;
530 wdh->subtype_close = NULL;
532 /* Write the file header. */
533 nwritten = fwrite(&snoop_magic, 1, sizeof snoop_magic, wdh->fh);
534 if (nwritten != sizeof snoop_magic) {
535 if (nwritten == 0 && ferror(wdh->fh))
538 *err = WTAP_ERR_SHORT_WRITE;
542 /* current "snoop" format is 2 */
543 file_hdr.version = htonl(2);
544 file_hdr.network = htonl(wtap_encap[wdh->encap]);
545 nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
546 if (nwritten != sizeof file_hdr) {
547 if (nwritten == 0 && ferror(wdh->fh))
550 *err = WTAP_ERR_SHORT_WRITE;
557 /* Write a record for a packet to a dump file.
558 Returns TRUE on success, FALSE on failure. */
559 static gboolean snoop_dump(wtap_dumper *wdh,
560 const struct wtap_pkthdr *phdr,
561 const union wtap_pseudo_header *pseudo_header _U_,
562 const u_char *pd, int *err)
564 struct snooprec_hdr rec_hdr;
568 static char zeroes[4];
570 /* Record length = header length plus data length... */
571 reclen = sizeof rec_hdr + phdr->caplen;
573 /* ... plus enough bytes to pad it to a 4-byte boundary. */
574 padlen = ((reclen + 3) & ~3) - reclen;
577 rec_hdr.orig_len = htonl(phdr->len);
578 rec_hdr.incl_len = htonl(phdr->caplen);
579 rec_hdr.rec_len = htonl(reclen);
580 rec_hdr.cum_drops = 0;
581 rec_hdr.ts_sec = htonl(phdr->ts.tv_sec);
582 rec_hdr.ts_usec = htonl(phdr->ts.tv_usec);
583 nwritten = fwrite(&rec_hdr, 1, sizeof rec_hdr, wdh->fh);
584 if (nwritten != sizeof rec_hdr) {
585 if (nwritten == 0 && ferror(wdh->fh))
588 *err = WTAP_ERR_SHORT_WRITE;
591 nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
592 if (nwritten != phdr->caplen) {
593 if (nwritten == 0 && ferror(wdh->fh))
596 *err = WTAP_ERR_SHORT_WRITE;
600 /* Now write the padding. */
601 nwritten = fwrite(zeroes, 1, padlen, wdh->fh);
602 if (nwritten != padlen) {
603 if (nwritten == 0 && ferror(wdh->fh))
606 *err = WTAP_ERR_SHORT_WRITE;