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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "file_wrappers.h"
33 #define IPTRACE_IFT_HF 0x3d /* Support for PERCS IP-HFI*/
34 #define IPTRACE_IFT_IB 0xc7 /* IP over Infiniband. Number by IANA */
36 static gboolean
iptrace_read_1_0(wtap
*wth
, int *err
, gchar
**err_info
,
38 static gboolean
iptrace_seek_read_1_0(wtap
*wth
, gint64 seek_off
,
39 struct wtap_pkthdr
*phdr
, Buffer
*buf
, int packet_size
,
40 int *err
, gchar
**err_info
);
42 static gboolean
iptrace_read_2_0(wtap
*wth
, int *err
, gchar
**err_info
,
44 static gboolean
iptrace_seek_read_2_0(wtap
*wth
, gint64 seek_off
,
45 struct wtap_pkthdr
*phdr
, Buffer
*buf
, int packet_size
,
46 int *err
, gchar
**err_info
);
48 static int iptrace_read_rec_header(FILE_T fh
, guint8
*header
, int header_len
,
49 int *err
, gchar
**err_info
);
50 static gboolean
iptrace_read_rec_data(FILE_T fh
, Buffer
*buf
,
51 struct wtap_pkthdr
*phdr
, int *err
, gchar
**err_info
);
52 static void fill_in_pseudo_header(int encap
,
53 union wtap_pseudo_header
*pseudo_header
, guint8
*header
);
54 static int wtap_encap_ift(unsigned int ift
);
56 int iptrace_open(wtap
*wth
, int *err
, gchar
**err_info
)
61 errno
= WTAP_ERR_CANT_READ
;
62 bytes_read
= file_read(name
, 11, wth
->fh
);
63 if (bytes_read
!= 11) {
64 *err
= file_error(wth
->fh
, err_info
);
65 if (*err
!= 0 && *err
!= WTAP_ERR_SHORT_READ
)
71 if (strcmp(name
, "iptrace 1.0") == 0) {
72 wth
->file_type_subtype
= WTAP_FILE_TYPE_SUBTYPE_IPTRACE_1_0
;
73 wth
->subtype_read
= iptrace_read_1_0
;
74 wth
->subtype_seek_read
= iptrace_seek_read_1_0
;
75 wth
->tsprecision
= WTAP_FILE_TSPREC_SEC
;
77 else if (strcmp(name
, "iptrace 2.0") == 0) {
78 wth
->file_type_subtype
= WTAP_FILE_TYPE_SUBTYPE_IPTRACE_2_0
;
79 wth
->subtype_read
= iptrace_read_2_0
;
80 wth
->subtype_seek_read
= iptrace_seek_read_2_0
;
81 wth
->tsprecision
= WTAP_FILE_TSPREC_NSEC
;
90 /***********************************************************
92 ***********************************************************/
95 * iptrace 1.0, discovered through inspection
97 * Packet record contains:
99 * an initial header, with a length field and a time stamp, in
100 * seconds since the Epoch;
102 * data, with the specified length.
106 * a bunch of information about the packet;
108 * padding, at least for FDDI;
110 * the raw packet data.
113 /* 0-3 */ guint32 pkt_length
; /* packet length + 0x16 */
114 /* 4-7 */ guint32 tv_sec
; /* time stamp, seconds since the Epoch */
115 /* 8-11 */ guint32 junk1
; /* ???, not time */
116 /* 12-15 */ char if_name
[4]; /* null-terminated */
117 /* 16-27 */ char junk2
[12]; /* ??? */
118 /* 28 */ guint8 if_type
; /* BSD net/if_types.h */
119 /* 29 */ guint8 tx_flag
; /* 0=receive, 1=transmit */
122 #define IPTRACE_1_0_PHDR_SIZE 30 /* initial header plus packet data */
123 #define IPTRACE_1_0_PDATA_SIZE 22 /* packet data */
126 iptrace_read_rec_1_0(FILE_T fh
, struct wtap_pkthdr
*phdr
, Buffer
*buf
,
127 int *err
, gchar
**err_info
)
129 guint8 header
[IPTRACE_1_0_PHDR_SIZE
];
131 iptrace_1_0_phdr pkt_hdr
;
134 ret
= iptrace_read_rec_header(fh
, header
, IPTRACE_1_0_PHDR_SIZE
,
137 /* Read error or EOF */
142 * Byte 28 of the frame header appears to be a BSD-style IFT_xxx
143 * value giving the type of the interface. Check out the
144 * <net/if_types.h> header file.
146 pkt_hdr
.if_type
= header
[28];
147 phdr
->pkt_encap
= wtap_encap_ift(pkt_hdr
.if_type
);
148 if (phdr
->pkt_encap
== WTAP_ENCAP_UNKNOWN
) {
149 *err
= WTAP_ERR_UNSUPPORTED_ENCAP
;
150 *err_info
= g_strdup_printf("iptrace: interface type IFT=0x%02x unknown or unsupported",
155 /* Read the packet metadata */
156 packet_size
= pntohl(&header
[0]);
157 if (packet_size
< IPTRACE_1_0_PDATA_SIZE
) {
159 * Uh-oh, the record isn't big enough to even have a
160 * packet meta-data header.
162 *err
= WTAP_ERR_BAD_FILE
;
163 *err_info
= g_strdup_printf("iptrace: file has a %u-byte record, too small to have even a packet meta-data header",
167 packet_size
-= IPTRACE_1_0_PDATA_SIZE
;
170 * AIX appears to put 3 bytes of padding in front of FDDI
171 * frames; strip that crap off.
173 if (phdr
->pkt_encap
== WTAP_ENCAP_FDDI_BITSWAPPED
) {
175 * The packet size is really a record size and includes
178 if (packet_size
< 3) {
180 * Uh-oh, the record isn't big enough to even have
183 *err
= WTAP_ERR_BAD_FILE
;
184 *err_info
= g_strdup_printf("iptrace: file has a %u-byte record, too small to have even a packet meta-data header",
185 packet_size
+ IPTRACE_1_0_PDATA_SIZE
);
193 if (!file_skip(fh
, 3, err
))
196 if (packet_size
> WTAP_MAX_PACKET_SIZE
) {
198 * Probably a corrupt capture file; don't blow up trying
199 * to allocate space for an immensely-large packet.
201 *err
= WTAP_ERR_BAD_FILE
;
202 *err_info
= g_strdup_printf("iptrace: File has %u-byte packet, bigger than maximum of %u",
203 packet_size
, WTAP_MAX_PACKET_SIZE
);
207 phdr
->presence_flags
= WTAP_HAS_TS
;
208 phdr
->len
= packet_size
;
209 phdr
->caplen
= packet_size
;
210 phdr
->ts
.secs
= pntohl(&header
[4]);
213 /* Fill in the pseudo-header. */
214 fill_in_pseudo_header(phdr
->pkt_encap
, &phdr
->pseudo_header
, header
);
216 /* Get the packet data */
217 return iptrace_read_rec_data(fh
, buf
, phdr
, err
, err_info
);
220 /* Read the next packet */
221 static gboolean
iptrace_read_1_0(wtap
*wth
, int *err
, gchar
**err_info
,
224 *data_offset
= file_tell(wth
->fh
);
226 /* Read the packet */
227 if (!iptrace_read_rec_1_0(wth
->fh
, &wth
->phdr
, wth
->frame_buffer
,
229 /* Read error or EOF */
233 /* If the per-file encapsulation isn't known, set it to this
234 packet's encapsulation.
236 If it *is* known, and it isn't this packet's encapsulation,
237 set it to WTAP_ENCAP_PER_PACKET, as this file doesn't
238 have a single encapsulation for all packets in the file. */
239 if (wth
->file_encap
== WTAP_ENCAP_UNKNOWN
)
240 wth
->file_encap
= wth
->phdr
.pkt_encap
;
242 if (wth
->file_encap
!= wth
->phdr
.pkt_encap
)
243 wth
->file_encap
= WTAP_ENCAP_PER_PACKET
;
249 static gboolean
iptrace_seek_read_1_0(wtap
*wth
, gint64 seek_off
,
250 struct wtap_pkthdr
*phdr
, Buffer
*buf
, int packet_size _U_
,
251 int *err
, gchar
**err_info
)
253 if (file_seek(wth
->random_fh
, seek_off
, SEEK_SET
, err
) == -1)
256 /* Read the packet */
257 if (!iptrace_read_rec_1_0(wth
->random_fh
, phdr
, buf
, err
, err_info
)) {
259 *err
= WTAP_ERR_SHORT_READ
;
265 /***********************************************************
267 ***********************************************************/
270 * iptrace 2.0, discovered through inspection
272 * Packet record contains:
274 * an initial header, with a length field and a time stamp, in
275 * seconds since the Epoch;
277 * data, with the specified length.
281 * a bunch of information about the packet;
283 * padding, at least for FDDI;
285 * the raw packet data.
288 /* 0-3 */ guint32 pkt_length
; /* packet length + 32 */
289 /* 4-7 */ guint32 tv_sec0
; /* time stamp, seconds since the Epoch */
290 /* 8-11 */ guint32 junk1
; /* ?? */
291 /* 12-15 */ char if_name
[4]; /* null-terminated */
292 /* 16-27 */ char if_desc
[12]; /* interface description. */
293 /* 28 */ guint8 if_type
; /* BSD net/if_types.h */
294 /* 29 */ guint8 tx_flag
; /* 0=receive, 1=transmit */
295 /* 30-31 */ guint16 junk3
;
296 /* 32-35 */ guint32 tv_sec
; /* time stamp, seconds since the Epoch */
297 /* 36-39 */ guint32 tv_nsec
; /* nanoseconds since that second */
300 #define IPTRACE_2_0_PHDR_SIZE 40 /* initial header plus packet data */
301 #define IPTRACE_2_0_PDATA_SIZE 32 /* packet data */
304 iptrace_read_rec_2_0(FILE_T fh
, struct wtap_pkthdr
*phdr
, Buffer
*buf
,
305 int *err
, gchar
**err_info
)
307 guint8 header
[IPTRACE_2_0_PHDR_SIZE
];
309 iptrace_2_0_phdr pkt_hdr
;
312 ret
= iptrace_read_rec_header(fh
, header
, IPTRACE_2_0_PHDR_SIZE
,
315 /* Read error or EOF */
320 * Byte 28 of the frame header appears to be a BSD-style IFT_xxx
321 * value giving the type of the interface. Check out the
322 * <net/if_types.h> header file.
324 pkt_hdr
.if_type
= header
[28];
325 phdr
->pkt_encap
= wtap_encap_ift(pkt_hdr
.if_type
);
328 * We used to error out if the interface type in iptrace was
329 * unknown/unhandled, but an iptrace may contain packets
330 * from a variety of interfaces, some known, and others
333 * It is better to display the data even for unknown interface
334 * types, isntead of erroring out. In the future, it would be
335 * nice to be able to flag which frames are shown as data
336 * because their interface type is unknown, and also present
337 * the interface type number to the user so that it can be
338 * reported easily back to the Wireshark developer.
340 * XXX - what types are there that are used in files but
341 * that we don't handle?
343 if (phdr
->pkt_encap
== WTAP_ENCAP_UNKNOWN
) {
344 *err
= WTAP_ERR_UNSUPPORTED_ENCAP
;
345 *err_info
= g_strdup_printf("iptrace: interface type IFT=0x%02x unknown or unsupported",
351 /* Read the packet metadata */
352 packet_size
= pntohl(&header
[0]);
353 if (packet_size
< IPTRACE_2_0_PDATA_SIZE
) {
355 * Uh-oh, the record isn't big enough to even have a
356 * packet meta-data header.
358 *err
= WTAP_ERR_BAD_FILE
;
359 *err_info
= g_strdup_printf("iptrace: file has a %u-byte record, too small to have even a packet meta-data header",
363 packet_size
-= IPTRACE_2_0_PDATA_SIZE
;
366 * AIX appears to put 3 bytes of padding in front of FDDI
367 * frames; strip that crap off.
369 if (phdr
->pkt_encap
== WTAP_ENCAP_FDDI_BITSWAPPED
) {
371 * The packet size is really a record size and includes
374 if (packet_size
< 3) {
376 * Uh-oh, the record isn't big enough to even have
379 *err
= WTAP_ERR_BAD_FILE
;
380 *err_info
= g_strdup_printf("iptrace: file has a %u-byte record, too small to have even a packet meta-data header",
381 packet_size
+ IPTRACE_2_0_PDATA_SIZE
);
389 if (!file_skip(fh
, 3, err
))
392 if (packet_size
> WTAP_MAX_PACKET_SIZE
) {
394 * Probably a corrupt capture file; don't blow up trying
395 * to allocate space for an immensely-large packet.
397 *err
= WTAP_ERR_BAD_FILE
;
398 *err_info
= g_strdup_printf("iptrace: File has %u-byte packet, bigger than maximum of %u",
399 packet_size
, WTAP_MAX_PACKET_SIZE
);
403 phdr
->presence_flags
= WTAP_HAS_TS
;
404 phdr
->len
= packet_size
;
405 phdr
->caplen
= packet_size
;
406 phdr
->ts
.secs
= pntohl(&header
[32]);
407 phdr
->ts
.nsecs
= pntohl(&header
[36]);
409 /* Fill in the pseudo_header. */
410 fill_in_pseudo_header(phdr
->pkt_encap
, &phdr
->pseudo_header
, header
);
412 /* Get the packet data */
413 return iptrace_read_rec_data(fh
, buf
, phdr
, err
, err_info
);
416 /* Read the next packet */
417 static gboolean
iptrace_read_2_0(wtap
*wth
, int *err
, gchar
**err_info
,
420 *data_offset
= file_tell(wth
->fh
);
422 /* Read the packet */
423 if (!iptrace_read_rec_2_0(wth
->fh
, &wth
->phdr
, wth
->frame_buffer
,
425 /* Read error or EOF */
429 /* If the per-file encapsulation isn't known, set it to this
430 packet's encapsulation.
432 If it *is* known, and it isn't this packet's encapsulation,
433 set it to WTAP_ENCAP_PER_PACKET, as this file doesn't
434 have a single encapsulation for all packets in the file. */
435 if (wth
->file_encap
== WTAP_ENCAP_UNKNOWN
)
436 wth
->file_encap
= wth
->phdr
.pkt_encap
;
438 if (wth
->file_encap
!= wth
->phdr
.pkt_encap
)
439 wth
->file_encap
= WTAP_ENCAP_PER_PACKET
;
445 static gboolean
iptrace_seek_read_2_0(wtap
*wth
, gint64 seek_off
,
446 struct wtap_pkthdr
*phdr
, Buffer
*buf
, int packet_size _U_
,
447 int *err
, gchar
**err_info
)
449 if (file_seek(wth
->random_fh
, seek_off
, SEEK_SET
, err
) == -1)
452 /* Read the packet */
453 if (!iptrace_read_rec_2_0(wth
->random_fh
, phdr
, buf
, err
, err_info
)) {
455 *err
= WTAP_ERR_SHORT_READ
;
462 iptrace_read_rec_header(FILE_T fh
, guint8
*header
, int header_len
, int *err
,
467 errno
= WTAP_ERR_CANT_READ
;
468 bytes_read
= file_read(header
, header_len
, fh
);
469 if (bytes_read
!= header_len
) {
470 *err
= file_error(fh
, err_info
);
473 if (bytes_read
!= 0) {
474 *err
= WTAP_ERR_SHORT_READ
;
483 iptrace_read_rec_data(FILE_T fh
, Buffer
*buf
, struct wtap_pkthdr
*phdr
,
484 int *err
, gchar
**err_info
)
486 if (!wtap_read_packet_bytes(fh
, buf
, phdr
->caplen
, err
, err_info
))
489 if (phdr
->pkt_encap
== WTAP_ENCAP_ATM_PDUS
) {
491 * Attempt to guess from the packet data, the VPI,
492 * and the VCI information about the type of traffic.
494 atm_guess_traffic_type(buffer_start_ptr(buf
), phdr
->caplen
,
495 &phdr
->pseudo_header
);
502 * Fill in the pseudo-header information we can.
504 * For ATM traffic, "iptrace", alas, doesn't tell us what type of traffic
505 * is in the packet - it was presumably run on a machine that was one of
506 * the endpoints of the connection, so in theory it could presumably have
507 * told us, but, for whatever reason, it failed to do so - perhaps the
508 * low-level mechanism that feeds the presumably-AAL5 frames to us doesn't
509 * have access to that information (e.g., because it's in the ATM driver,
510 * and the ATM driver merely knows that stuff on VPI/VCI X.Y should be
511 * handed up to some particular client, it doesn't know what that client is).
513 * We let our caller try to figure out what kind of traffic it is, either
514 * by guessing based on the VPI/VCI, guessing based on the header of the
515 * packet, seeing earlier traffic that set up the circuit and specified
516 * in some fashion what sort of traffic it is, or being told by the user.
519 fill_in_pseudo_header(int encap
, union wtap_pseudo_header
*pseudo_header
,
529 case WTAP_ENCAP_ATM_PDUS
:
530 /* Rip apart the "x.y" text into Vpi/Vci numbers */
531 memcpy(if_text
, &header
[20], 8);
533 decimal
= strchr(if_text
, '.');
536 Vpi
= (int)strtoul(if_text
, NULL
, 10);
538 Vci
= (int)strtoul(decimal
, NULL
, 10);
542 * OK, which value means "DTE->DCE" and which value means
545 pseudo_header
->atm
.channel
= header
[29];
547 pseudo_header
->atm
.vpi
= Vpi
;
548 pseudo_header
->atm
.vci
= Vci
;
550 /* We don't have this information */
551 pseudo_header
->atm
.flags
= 0;
552 pseudo_header
->atm
.cells
= 0;
553 pseudo_header
->atm
.aal5t_u2u
= 0;
554 pseudo_header
->atm
.aal5t_len
= 0;
555 pseudo_header
->atm
.aal5t_chksum
= 0;
558 case WTAP_ENCAP_ETHERNET
:
559 /* We assume there's no FCS in this frame. */
560 pseudo_header
->eth
.fcs_len
= 0;
565 /* Given an RFC1573 (SNMP ifType) interface type,
566 * return the appropriate Wiretap Encapsulation Type.
569 wtap_encap_ift(unsigned int ift
)
572 static const int ift_encap
[] = {
573 /* 0x0 */ WTAP_ENCAP_UNKNOWN
, /* nothing */
574 /* 0x1 */ WTAP_ENCAP_UNKNOWN
, /* IFT_OTHER */
575 /* 0x2 */ WTAP_ENCAP_UNKNOWN
, /* IFT_1822 */
576 /* 0x3 */ WTAP_ENCAP_UNKNOWN
, /* IFT_HDH1822 */
577 /* 0x4 */ WTAP_ENCAP_RAW_IP
, /* IFT_X25DDN */
578 /* 0x5 */ WTAP_ENCAP_UNKNOWN
, /* IFT_X25 */
579 /* 0x6 */ WTAP_ENCAP_ETHERNET
, /* IFT_ETHER */
580 /* 0x7 */ WTAP_ENCAP_ETHERNET
, /* IFT_ISO88023 */
581 /* 0x8 */ WTAP_ENCAP_UNKNOWN
, /* IFT_ISO88024 */
582 /* 0x9 */ WTAP_ENCAP_TOKEN_RING
, /* IFT_ISO88025 */
583 /* 0xa */ WTAP_ENCAP_UNKNOWN
, /* IFT_ISO88026 */
584 /* 0xb */ WTAP_ENCAP_UNKNOWN
, /* IFT_STARLAN */
585 /* 0xc */ WTAP_ENCAP_RAW_IP
, /* IFT_P10, IBM SP switch */
586 /* 0xd */ WTAP_ENCAP_UNKNOWN
, /* IFT_P80 */
587 /* 0xe */ WTAP_ENCAP_UNKNOWN
, /* IFT_HY */
588 /* 0xf */ WTAP_ENCAP_FDDI_BITSWAPPED
, /* IFT_FDDI */
589 /* 0x10 */ WTAP_ENCAP_LAPB
, /* IFT_LAPB */ /* no data to back this up */
590 /* 0x11 */ WTAP_ENCAP_UNKNOWN
, /* IFT_SDLC */
591 /* 0x12 */ WTAP_ENCAP_UNKNOWN
, /* IFT_T1 */
592 /* 0x13 */ WTAP_ENCAP_UNKNOWN
, /* IFT_CEPT */
593 /* 0x14 */ WTAP_ENCAP_UNKNOWN
, /* IFT_ISDNBASIC */
594 /* 0x15 */ WTAP_ENCAP_UNKNOWN
, /* IFT_ISDNPRIMARY */
595 /* 0x16 */ WTAP_ENCAP_UNKNOWN
, /* IFT_PTPSERIAL */
596 /* 0x17 */ WTAP_ENCAP_UNKNOWN
, /* IFT_PPP */
597 /* 0x18 */ WTAP_ENCAP_RAW_IP
, /* IFT_LOOP */
598 /* 0x19 */ WTAP_ENCAP_UNKNOWN
, /* IFT_EON */
599 /* 0x1a */ WTAP_ENCAP_UNKNOWN
, /* IFT_XETHER */
600 /* 0x1b */ WTAP_ENCAP_UNKNOWN
, /* IFT_NSIP */
601 /* 0x1c */ WTAP_ENCAP_UNKNOWN
, /* IFT_SLIP */
602 /* 0x1d */ WTAP_ENCAP_UNKNOWN
, /* IFT_ULTRA */
603 /* 0x1e */ WTAP_ENCAP_UNKNOWN
, /* IFT_DS3 */
604 /* 0x1f */ WTAP_ENCAP_UNKNOWN
, /* IFT_SIP */
605 /* 0x20 */ WTAP_ENCAP_UNKNOWN
, /* IFT_FRELAY */
606 /* 0x21 */ WTAP_ENCAP_UNKNOWN
, /* IFT_RS232 */
607 /* 0x22 */ WTAP_ENCAP_UNKNOWN
, /* IFT_PARA */
608 /* 0x23 */ WTAP_ENCAP_UNKNOWN
, /* IFT_ARCNET */
609 /* 0x24 */ WTAP_ENCAP_UNKNOWN
, /* IFT_ARCNETPLUS */
610 /* 0x25 */ WTAP_ENCAP_ATM_PDUS
, /* IFT_ATM */
612 #define NUM_IFT_ENCAPS (sizeof ift_encap / sizeof ift_encap[0])
614 if (ift
< NUM_IFT_ENCAPS
) {
615 return ift_encap
[ift
];
621 return WTAP_ENCAP_INFINIBAND
;
624 /* Host Fabric Interface */
626 /* The HFI interface on AIX provides raw IP
627 in the packet trace. It's unclear if the HFI
628 can be configured for any other protocol, and if
629 any field in the iptrace header indicates what
630 that protocol is. For now, we are hard-coding
631 this as RAW_IP, but if we find another iptrace file
632 using HFI that provides another protocol, we will
633 have to figure out which field in the iptrace file
635 return WTAP_ENCAP_RAW_IP
;
639 return WTAP_ENCAP_UNKNOWN
;