Remove write capabilities from wtap_optionblocks.
[metze/wireshark/wip.git] / wiretap / ipfix.c
1 /* ipfix.c
2  *
3  * Wiretap Library
4  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
5  *
6  * File format support for ipfix file format
7  * Copyright (c) 2010 by Hadriel Kaplan <hadrielk@yahoo.com>
8  *   with generous copying from other wiretaps, such as pcapng
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 /* File format reference:
26  *   RFC 5655 and 5101
27  *   http://tools.ietf.org/rfc/rfc5655
28  *   http://tools.ietf.org/rfc/rfc5101
29  *
30  * This wiretap is for an ipfix file format reader, per RFC 5655/5101.
31  * All "records" in the file are IPFIX messages, beginning with an IPFIX
32  *  message header of 16 bytes as follows from RFC 5101:
33     0                   1                   2                   3
34     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
35    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36    |       Version Number          |            Length             |
37    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38    |                           Export Time                         |
39    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40    |                       Sequence Number                         |
41    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42    |                    Observation Domain ID                      |
43    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44
45    Figure F: IPFIX Message Header Format
46
47  * which is then followed by one or more "Sets": Data Sets, Template Sets,
48  * and Options Template Sets.  Each Set then has one or more Records in
49  * it.
50  *
51  * All IPFIX files are recorded in big-endian form (network byte order),
52  * per the RFCs.  That means if we're on a little-endian system, all
53  * hell will break loose if we don't g_ntohX.
54  *
55  * Since wireshark already has an IPFIX dissector (implemented in
56  * packet-netflow.c), this reader will just set that dissector upon
57  * reading each message.  Thus, an IPFIX Message is treated as a packet
58  * as far as the dissector is concerned.
59  */
60
61 #include "config.h"
62
63 #include <stdlib.h>
64 #include <string.h>
65 #include <errno.h>
66 #include "wtap-int.h"
67 #include "file_wrappers.h"
68 #include "ipfix.h"
69
70 #if 0
71 #define ipfix_debug(...) g_warning(__VA_ARGS__)
72 #else
73 #define ipfix_debug(...)
74 #endif
75
76 #define RECORDS_FOR_IPFIX_CHECK 20
77
78 static gboolean
79 ipfix_read(wtap *wth, int *err, gchar **err_info,
80     gint64 *data_offset);
81 static gboolean
82 ipfix_seek_read(wtap *wth, gint64 seek_off,
83     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
84
85 #define IPFIX_VERSION 10
86
87 /* ipfix: message header */
88 typedef struct ipfix_message_header_s {
89     guint16 version;
90     guint16 message_length;
91     guint32 export_time_secs;
92     guint32 sequence_number;
93     guint32 observation_id; /* might be 0 for none */
94     /* x bytes msg_body */
95 } ipfix_message_header_t;
96 #define IPFIX_MSG_HDR_SIZE 16
97
98 /* ipfix: common Set header for every Set type */
99 typedef struct ipfix_set_header_s {
100     guint16 set_type;
101     guint16 set_length;
102     /* x bytes set_body */
103 } ipfix_set_header_t;
104 #define IPFIX_SET_HDR_SIZE 4
105
106
107 /* Read IPFIX message header from file.  Return true on success.  Set *err to
108  * 0 on EOF, any other value for "real" errors (EOF is ok, since return
109  * value is still FALSE)
110  */
111 static gboolean
112 ipfix_read_message_header(ipfix_message_header_t *pfx_hdr, FILE_T fh, int *err, gchar **err_info)
113 {
114     if (!wtap_read_bytes_or_eof(fh, pfx_hdr, IPFIX_MSG_HDR_SIZE, err, err_info))
115         return FALSE;
116
117     /* fix endianess, because IPFIX files are always big-endian */
118     pfx_hdr->version = g_ntohs(pfx_hdr->version);
119     pfx_hdr->message_length = g_ntohs(pfx_hdr->message_length);
120     pfx_hdr->export_time_secs = g_ntohl(pfx_hdr->export_time_secs);
121     pfx_hdr->sequence_number = g_ntohl(pfx_hdr->sequence_number);
122     pfx_hdr->observation_id = g_ntohl(pfx_hdr->observation_id);
123
124     /* is the version number one we expect? */
125     if (pfx_hdr->version != IPFIX_VERSION) {
126         /* Not an ipfix file. */
127         *err = WTAP_ERR_BAD_FILE;
128         *err_info = g_strdup_printf("ipfix: wrong version %d", pfx_hdr->version);
129         return FALSE;
130     }
131
132     if (pfx_hdr->message_length < 16) {
133         *err = WTAP_ERR_BAD_FILE;
134         *err_info = g_strdup_printf("ipfix: message length %u is too short", pfx_hdr->message_length);
135         return FALSE;
136     }
137
138     /* go back to before header */
139     if (file_seek(fh, 0 - IPFIX_MSG_HDR_SIZE, SEEK_CUR, err) == -1) {
140         ipfix_debug("ipfix_read: couldn't go back in file before header");
141         return FALSE;
142     }
143
144     return TRUE;
145 }
146
147
148 /* Read IPFIX message header from file and fill in the struct wtap_pkthdr
149  * for the packet, and, if that succeeds, read the packet data.
150  * Return true on success.  Set *err to 0 on EOF, any other value for "real"
151  * errors (EOF is ok, since return value is still FALSE).
152  */
153 static gboolean
154 ipfix_read_message(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
155 {
156     ipfix_message_header_t msg_hdr;
157
158     if (!ipfix_read_message_header(&msg_hdr, fh, err, err_info))
159         return FALSE;
160     /*
161      * The maximum value of msg_hdr.message_length is 65535, which is
162      * less than WTAP_MAX_PACKET_SIZE will ever be, so we don't need
163      * to check it.
164      */
165
166     phdr->rec_type = REC_TYPE_PACKET;
167     phdr->presence_flags = WTAP_HAS_TS;
168     phdr->len = msg_hdr.message_length;
169     phdr->caplen = msg_hdr.message_length;
170     phdr->ts.secs = msg_hdr.export_time_secs;
171     phdr->ts.nsecs = 0;
172
173     return wtap_read_packet_bytes(fh, buf, msg_hdr.message_length, err, err_info);
174 }
175
176
177
178 /* classic wtap: open capture file.  Return WTAP_OPEN_MINE on success,
179  * WTAP_OPEN_NOT_MINE on normal failure like malformed format,
180  * WTAP_OPEN_ERROR on bad error like file system
181  */
182 wtap_open_return_val
183 ipfix_open(wtap *wth, int *err, gchar **err_info)
184 {
185     gint i, n, records_for_ipfix_check = RECORDS_FOR_IPFIX_CHECK;
186     gchar *s;
187     guint16 checked_len = 0;
188     ipfix_message_header_t msg_hdr;
189     ipfix_set_header_t set_hdr;
190
191     ipfix_debug("ipfix_open: opening file");
192
193     /* number of records to scan before deciding if this really is IPFIX */
194     if ((s = getenv("IPFIX_RECORDS_TO_CHECK")) != NULL) {
195         if ((n = atoi(s)) > 0 && n < 101) {
196             records_for_ipfix_check = n;
197         }
198     }
199
200     /*
201      * IPFIX is a little hard because there's no magic number; we look at
202      * the first few records and see if they look enough like IPFIX
203      * records.
204      */
205     for (i = 0; i < records_for_ipfix_check; i++) {
206         /* read first message header to check version */
207         if (!ipfix_read_message_header(&msg_hdr, wth->fh, err, err_info)) {
208             ipfix_debug("ipfix_open: couldn't read message header #%d with err code #%d (%s)",
209                          i, *err, *err_info);
210             if (*err == WTAP_ERR_BAD_FILE) {
211                 *err = 0;            /* not actually an error in this case */
212                 g_free(*err_info);
213                 *err_info = NULL;
214                 return WTAP_OPEN_NOT_MINE;
215             }
216             if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
217                 return WTAP_OPEN_ERROR; /* real failure */
218             /* else it's EOF */
219             if (i < 1) {
220                 /* we haven't seen enough to prove this is a ipfix file */
221                 return WTAP_OPEN_NOT_MINE;
222             }
223             /*
224              * If we got here, it's EOF and we haven't yet seen anything
225              * that doesn't look like an IPFIX record - i.e. everything
226              * we've seen looks like an IPFIX record - so we assume this
227              * is an IPFIX file.
228              */
229             break;
230         }
231         if (file_seek(wth->fh, IPFIX_MSG_HDR_SIZE, SEEK_CUR, err) == -1) {
232             ipfix_debug("ipfix_open: failed seek to next message in file, %d bytes away",
233                          msg_hdr.message_length);
234             return WTAP_OPEN_NOT_MINE;
235         }
236         checked_len = IPFIX_MSG_HDR_SIZE;
237
238         /* check each Set in IPFIX Message for sanity */
239         while (checked_len < msg_hdr.message_length) {
240             if (!wtap_read_bytes(wth->fh, &set_hdr, IPFIX_SET_HDR_SIZE,
241                                  err, err_info)) {
242                 if (*err == WTAP_ERR_SHORT_READ) {
243                     /* Not a valid IPFIX Set, so not an IPFIX file. */
244                     ipfix_debug("ipfix_open: error %d reading set", *err);
245                     return WTAP_OPEN_NOT_MINE;
246                 }
247
248                 /* A real I/O error; fail. */
249                 return WTAP_OPEN_ERROR;
250             }
251             set_hdr.set_length = g_ntohs(set_hdr.set_length);
252             if ((set_hdr.set_length < IPFIX_SET_HDR_SIZE) ||
253                 ((set_hdr.set_length + checked_len) > msg_hdr.message_length))  {
254                 ipfix_debug("ipfix_open: found invalid set_length of %d",
255                              set_hdr.set_length);
256                 return WTAP_OPEN_NOT_MINE;
257             }
258
259             if (file_seek(wth->fh, set_hdr.set_length - IPFIX_SET_HDR_SIZE,
260                  SEEK_CUR, err) == -1)
261             {
262                 ipfix_debug("ipfix_open: failed seek to next set in file, %d bytes away",
263                              set_hdr.set_length - IPFIX_SET_HDR_SIZE);
264                 return WTAP_OPEN_ERROR;
265             }
266             checked_len += set_hdr.set_length;
267         }
268     }
269
270     /* go back to beginning of file */
271     if (file_seek (wth->fh, 0, SEEK_SET, err) != 0)
272     {
273         return WTAP_OPEN_ERROR;
274     }
275
276     /* all's good, this is a IPFIX file */
277     wth->file_encap = WTAP_ENCAP_RAW_IPFIX;
278     wth->snapshot_length = 0;
279     wth->file_tsprec = WTAP_TSPREC_SEC;
280     wth->subtype_read = ipfix_read;
281     wth->subtype_seek_read = ipfix_seek_read;
282     wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_IPFIX;
283
284     return WTAP_OPEN_MINE;
285 }
286
287
288 /* classic wtap: read packet */
289 static gboolean
290 ipfix_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
291 {
292     *data_offset = file_tell(wth->fh);
293     ipfix_debug("ipfix_read: data_offset is initially %" G_GINT64_MODIFIER "d", *data_offset);
294
295     if (!ipfix_read_message(wth->fh, &wth->phdr, wth->frame_buffer, err, err_info)) {
296         ipfix_debug("ipfix_read: couldn't read message header with code: %d\n, and error '%s'",
297                      *err, *err_info);
298         return FALSE;
299     }
300
301     return TRUE;
302 }
303
304
305 /* classic wtap: seek to file position and read packet */
306 static gboolean
307 ipfix_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
308     Buffer *buf, int *err, gchar **err_info)
309 {
310     /* seek to the right file position */
311     if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
312         ipfix_debug("ipfix_seek_read: couldn't read message header with code: %d\n, and error '%s'",
313                      *err, *err_info);
314         return FALSE;   /* Seek error */
315     }
316
317     ipfix_debug("ipfix_seek_read: reading at offset %" G_GINT64_MODIFIER "u", seek_off);
318
319     if (!ipfix_read_message(wth->random_fh, phdr, buf, err, err_info)) {
320         ipfix_debug("ipfix_seek_read: couldn't read message header");
321         if (*err == 0)
322             *err = WTAP_ERR_SHORT_READ;
323         return FALSE;
324     }
325     return TRUE;
326 }
327
328 /*
329  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
330  *
331  * Local variables:
332  * c-basic-offset: 4
333  * tab-width: 8
334  * indent-tabs-mode: nil
335  * End:
336  *
337  * vi: set shiftwidth=4 tabstop=8 expandtab:
338  * :indentSize=4:tabSize=8:noTabs=true:
339  */