3 * Copyright (c) 2000 by Mike Hall <mlh@io.com>
4 * Copyright (c) 2000 by Cisco Systems
6 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "file_wrappers.h"
18 * This module reads the output from the Cisco Secure Intrusion Detection
19 * System iplogging facility. The term iplogging is misleading since this
20 * logger will only output TCP. There is no link layer information.
21 * Packet format is 4 byte timestamp (seconds since epoch), and a 4 byte size
22 * of data following for that packet.
24 * For a time there was an error in iplogging and the ip length, flags, and id
25 * were byteswapped. We will check for this and handle it before handing to
33 static bool csids_read(wtap
*wth
, wtap_rec
*rec
, Buffer
*buf
,
34 int *err
, char **err_info
, int64_t *data_offset
);
35 static bool csids_seek_read(wtap
*wth
, int64_t seek_off
,
36 wtap_rec
*rec
, Buffer
*buf
, int *err
, char **err_info
);
37 static bool csids_read_packet(FILE_T fh
, csids_t
*csids
,
38 wtap_rec
*rec
, Buffer
*buf
, int *err
, char **err_info
);
41 uint32_t seconds
; /* seconds since epoch */
42 uint16_t zeropad
; /* 2 byte zero'ed pads */
43 uint16_t caplen
; /* the capture length */
46 static int csids_file_type_subtype
= -1;
48 void register_csids(void);
50 wtap_open_return_val
csids_open(wtap
*wth
, int *err
, char **err_info
)
52 /* There is no file header. There is only a header for each packet
53 * so we read a packet header and compare the caplen with iplen. They
54 * should always be equal except with the weird byteswap version.
56 * THIS IS BROKEN-- anytime the caplen is 0x0101 or 0x0202 up to 0x0505
57 * this will byteswap it. I need to fix this. XXX --mlh
62 bool byteswap
= false;
63 struct csids_header hdr
;
66 /* check the file to make sure it is a csids file. */
67 if( !wtap_read_bytes( wth
->fh
, &hdr
, sizeof( struct csids_header
), err
, err_info
) ) {
68 if( *err
!= WTAP_ERR_SHORT_READ
) {
69 return WTAP_OPEN_ERROR
;
71 return WTAP_OPEN_NOT_MINE
;
73 if( hdr
.zeropad
!= 0 || hdr
.caplen
== 0 ) {
74 return WTAP_OPEN_NOT_MINE
;
76 hdr
.seconds
= pntoh32( &hdr
.seconds
);
77 hdr
.caplen
= pntoh16( &hdr
.caplen
);
78 if( !wtap_read_bytes( wth
->fh
, &tmp
, 2, err
, err_info
) ) {
79 if( *err
!= WTAP_ERR_SHORT_READ
) {
80 return WTAP_OPEN_ERROR
;
82 return WTAP_OPEN_NOT_MINE
;
84 if( !wtap_read_bytes(wth
->fh
, &iplen
, 2, err
, err_info
) ) {
85 if( *err
!= WTAP_ERR_SHORT_READ
) {
86 return WTAP_OPEN_ERROR
;
88 return WTAP_OPEN_NOT_MINE
;
90 iplen
= pntoh16(&iplen
);
93 return WTAP_OPEN_NOT_MINE
;
95 /* if iplen and hdr.caplen are equal, default to no byteswap. */
96 if( iplen
> hdr
.caplen
) {
97 /* maybe this is just a byteswapped version. the iplen ipflags */
98 /* and ipid are swapped. We cannot use the normal swaps because */
99 /* we don't know the host */
100 iplen
= GUINT16_SWAP_LE_BE(iplen
);
101 if( iplen
<= hdr
.caplen
) {
102 /* we know this format */
105 /* don't know this one */
106 return WTAP_OPEN_NOT_MINE
;
112 /* no file header. So reset the fh to 0 so we can read the first packet */
113 if (file_seek(wth
->fh
, 0, SEEK_SET
, err
) == -1)
114 return WTAP_OPEN_ERROR
;
116 csids
= g_new(csids_t
, 1);
117 wth
->priv
= (void *)csids
;
118 csids
->byteswapped
= byteswap
;
119 wth
->file_encap
= WTAP_ENCAP_RAW_IP
;
120 wth
->file_type_subtype
= csids_file_type_subtype
;
121 wth
->snapshot_length
= 0; /* not known */
122 wth
->subtype_read
= csids_read
;
123 wth
->subtype_seek_read
= csids_seek_read
;
124 wth
->file_tsprec
= WTAP_TSPREC_SEC
;
127 * Add an IDB; we don't know how many interfaces were
128 * involved, so we just say one interface, about which
129 * we only know the link-layer type, snapshot length,
130 * and time stamp resolution.
132 wtap_add_generated_idb(wth
);
134 return WTAP_OPEN_MINE
;
137 /* Find the next packet and parse it; called from wtap_read(). */
138 static bool csids_read(wtap
*wth
, wtap_rec
*rec
, Buffer
*buf
,
139 int *err
, char **err_info
, int64_t *data_offset
)
141 csids_t
*csids
= (csids_t
*)wth
->priv
;
143 *data_offset
= file_tell(wth
->fh
);
145 return csids_read_packet( wth
->fh
, csids
, rec
, buf
, err
, err_info
);
148 /* Used to read packets in random-access fashion */
150 csids_seek_read(wtap
*wth
,
157 csids_t
*csids
= (csids_t
*)wth
->priv
;
159 if( file_seek( wth
->random_fh
, seek_off
, SEEK_SET
, err
) == -1 )
162 if( !csids_read_packet( wth
->random_fh
, csids
, rec
, buf
, err
, err_info
) ) {
164 *err
= WTAP_ERR_SHORT_READ
;
171 csids_read_packet(FILE_T fh
, csids_t
*csids
, wtap_rec
*rec
,
172 Buffer
*buf
, int *err
, char **err_info
)
174 struct csids_header hdr
;
177 if( !wtap_read_bytes_or_eof( fh
, &hdr
, sizeof( struct csids_header
), err
, err_info
) )
179 hdr
.seconds
= pntoh32(&hdr
.seconds
);
180 hdr
.caplen
= pntoh16(&hdr
.caplen
);
182 * The maximum value of hdr.caplen is 65535, which is less than
183 * WTAP_MAX_PACKET_SIZE_STANDARD will ever be, so we don't need to check
187 rec
->rec_type
= REC_TYPE_PACKET
;
188 rec
->block
= wtap_block_create(WTAP_BLOCK_PACKET
);
189 rec
->presence_flags
= WTAP_HAS_TS
;
190 rec
->rec_header
.packet_header
.len
= hdr
.caplen
;
191 rec
->rec_header
.packet_header
.caplen
= hdr
.caplen
;
192 rec
->ts
.secs
= hdr
.seconds
;
195 if( !wtap_read_packet_bytes( fh
, buf
, rec
->rec_header
.packet_header
.caplen
, err
, err_info
) )
198 pd
= ws_buffer_start_ptr( buf
);
199 if( csids
->byteswapped
) {
200 if( rec
->rec_header
.packet_header
.caplen
>= 2 ) {
201 PBSWAP16(pd
); /* the ip len */
202 if( rec
->rec_header
.packet_header
.caplen
>= 4 ) {
203 PBSWAP16(pd
+2); /* ip id */
204 if( rec
->rec_header
.packet_header
.caplen
>= 6 )
205 PBSWAP16(pd
+4); /* ip flags and fragoff */
213 static const struct supported_block_type csids_blocks_supported
[] = {
215 * We support packet blocks, with no comments or other options.
217 { WTAP_BLOCK_PACKET
, MULTIPLE_BLOCKS_SUPPORTED
, NO_OPTIONS_SUPPORTED
}
220 static const struct file_type_subtype_info csids_info
= {
221 "CSIDS IPLog", "csids", NULL
, NULL
,
222 false, BLOCKS_SUPPORTED(csids_blocks_supported
),
226 void register_csids(void)
228 csids_file_type_subtype
= wtap_register_file_type_subtype(&csids_info
);
231 * Register name for backwards compatibility with the
232 * wtap_filetypes table in Lua.
234 wtap_register_backwards_compatibility_lua_name("CSIDS",
235 csids_file_type_subtype
);
239 * Editor modelines - https://www.wireshark.org/tools/modelines.html
244 * indent-tabs-mode: nil
247 * vi: set shiftwidth=2 tabstop=8 expandtab:
248 * :indentSize=2:tabSize=8:noTabs=true: