From Ivan Sy (with minor modifications):
[obnox/wireshark/wip.git] / wiretap / erf.c
1 /*
2  *
3  * Copyright (c) 2003 Endace Technology Ltd, Hamilton, New Zealand.
4  * All rights reserved.
5  *
6  * This software and documentation has been developed by Endace Technology Ltd.
7  * along with the DAG PCI network capture cards. For further information please
8  * visit http://www.endace.com/.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  *  1. Redistributions of source code must retain the above copyright notice,
14  *  this list of conditions and the following disclaimer.
15  *
16  *  2. Redistributions in binary form must reproduce the above copyright
17  *  notice, this list of conditions and the following disclaimer in the
18  *  documentation and/or other materials provided with the distribution.
19  *
20  *  3. The name of Endace Technology Ltd may not be used to endorse or promote
21  *  products derived from this software without specific prior written
22  *  permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY ENDACE TECHNOLOGY LTD ``AS IS'' AND ANY EXPRESS
25  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
27  * EVENT SHALL ENDACE TECHNOLOGY LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $Id$
36  */
37
38 /*
39  * erf - Endace ERF (Extensible Record Format)
40  *
41  * See
42  *
43  *      http://www.endace.com/support/EndaceRecordFormat.pdf
44  */
45
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "wtap-int.h"
54 #include "file_wrappers.h"
55 #include "buffer.h"
56 #include "atm.h"
57 #include "erf.h"
58
59 #ifndef min
60 #define min(a, b) ((a) > (b) ? (b) : (a))
61 #endif
62
63 static int erf_read_header(FILE_T fh,
64                            struct wtap_pkthdr *phdr,
65                            union wtap_pseudo_header *pseudo_header,
66                            erf_header_t *erf_header,
67                            int *err,
68                            gchar **err_info,
69                            guint32 *bytes_read,
70                            guint32 *packet_size);
71 static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
72                          gint64 *data_offset);
73 static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
74                               union wtap_pseudo_header *pseudo_header, guchar *pd,
75                               int length, int *err, gchar **err_info);
76
77 extern int erf_open(wtap *wth, int *err, gchar **err_info _U_)
78 {
79   int i, n, records_for_erf_check = RECORDS_FOR_ERF_CHECK;
80   char *s;
81   erf_timestamp_t prevts,ts; 
82   erf_header_t header;
83   guint32 mc_hdr;
84   guint16 eth_hdr;
85   guint32 packet_size;
86   guint16 rlen,wlen;
87   guint64 erf_ext_header;
88   guint8 type;
89   size_t r;
90   gchar * buffer;
91
92   memset(&prevts, 0, sizeof(prevts));
93
94   /* number of records to scan before deciding if this really is ERF */
95   if ((s = getenv("ERF_RECORDS_TO_CHECK")) != NULL) {
96     if ((n = atoi(s)) > 0 && n < 101) {
97       records_for_erf_check = n;
98     }
99   }
100
101   /*
102    * ERF is a little hard because there's no magic number; we look at
103    * the first few records and see if they look enough like ERF
104    * records.
105    */
106
107   for (i = 0; i < records_for_erf_check; i++) {  /* records_for_erf_check */
108
109     r = file_read(&header,1,sizeof(header),wth->fh);
110
111     if (r == 0 ) break;
112     if (r != sizeof(header)) {
113       if ((*err = file_error(wth->fh)) != 0) {
114         return -1;
115       } else {
116         /* ERF header too short accept the file,
117            only if the very first records have been successfully checked */
118         if (i < MIN_RECORDS_FOR_ERF_CHECK) {
119           return 0;
120         } else {
121           /* BREAK, the last record is too short, and will be ignored */
122           break;
123         }
124       }
125     }
126
127     rlen=g_ntohs(header.rlen);
128     wlen=g_ntohs(header.wlen);
129
130     /* fail on invalid record type, invalid rlen, timestamps decreasing, or incrementing too far */
131     
132     /* Test valid rlen >= 16 */
133     if (rlen < 16) {
134       return 0;
135     }
136     
137     packet_size = rlen - (guint32)sizeof(header);
138     if (packet_size > WTAP_MAX_PACKET_SIZE) {
139       /*
140        * Probably a corrupt capture file or a file that's not an ERF file
141        * but that passed earlier tests; don't blow up trying
142        * to allocate space for an immensely-large packet.
143        */
144       return 0;
145     }
146
147     /* Skip PAD records, timestamps may not be set */
148     if ((header.type & 0x7F) == ERF_TYPE_PAD) {
149       if (file_seek(wth->fh, packet_size, SEEK_CUR, err) == -1) {
150         return -1;
151       }
152       continue;
153     }
154
155     /* fail on invalid record type, decreasing timestamps or non-zero pad-bits */
156     /* Not all types within this range are decoded, but it is a first filter */
157     if ((header.type & 0x7F) == 0 || (header.type & 0x7F) > ERF_TYPE_MAX ) {
158       return 0;
159     }
160     
161     /* The ERF_TYPE_MAX is the PAD record, but the last used type is ERF_TYPE_RAW_LINK */
162     if ((header.type & 0x7F) > ERF_TYPE_RAW_LINK) {
163       return 0;
164     }
165     
166     if ((ts = pletohll(&header.ts)) < prevts) {
167       /* reassembled AALx records may not be in time order, also records are not in strict time order between physical interfaces, so allow 1 sec fudge */
168       if ( ((prevts-ts)>>32) > 1 ) {
169         return 0;
170       }
171     }
172     
173     /* Check to see if timestamp increment is > 1 week */
174     if ( (i) && (ts > prevts) && (((ts-prevts)>>32) > 3600*24*7) ) {
175       return 0;
176     }
177     
178     memcpy(&prevts, &ts, sizeof(prevts));
179
180     /* Read over the extension headers */
181     type = header.type;
182     while (type & 0x80){
183             if (file_read(&erf_ext_header, 1, sizeof(erf_ext_header),wth->fh) != sizeof(erf_ext_header)) {
184                     *err = file_error(wth->fh);
185                     return -1;
186             }
187             packet_size -= (guint32)sizeof(erf_ext_header);
188             memcpy(&type, &erf_ext_header, sizeof(type));
189     }
190     
191
192     /* Read over MC or ETH subheader */
193     switch(header.type & 0x7F) {
194     case ERF_TYPE_MC_HDLC:
195     case ERF_TYPE_MC_RAW:
196     case ERF_TYPE_MC_ATM:
197     case ERF_TYPE_MC_RAW_CHANNEL:
198     case ERF_TYPE_MC_AAL5:
199     case ERF_TYPE_MC_AAL2:
200     case ERF_TYPE_COLOR_MC_HDLC_POS:
201       if (file_read(&mc_hdr,1,sizeof(mc_hdr),wth->fh) != sizeof(mc_hdr)) {
202         *err = file_error(wth->fh);
203         return -1;
204       }
205       packet_size -= (guint32)sizeof(mc_hdr);
206       break;
207     case ERF_TYPE_ETH:
208     case ERF_TYPE_COLOR_ETH:
209     case ERF_TYPE_DSM_COLOR_ETH:
210       if (file_read(&eth_hdr,1,sizeof(eth_hdr),wth->fh) != sizeof(eth_hdr)) {
211         *err = file_error(wth->fh);
212         return -1;
213       }
214       packet_size -= (guint32)sizeof(eth_hdr);
215       break;
216     default:
217       break;
218     }
219
220     /* The file_seek function do not return an error if the end of file
221        is reached whereas the record is truncated */
222     if (packet_size > WTAP_MAX_PACKET_SIZE) {
223       /*
224        * Probably a corrupt capture file; don't blow up trying
225        * to allocate space for an immensely-large packet.
226        */
227       return 0;
228     }
229     buffer=g_malloc(packet_size);
230     r = file_read(buffer, 1, packet_size, wth->fh);
231     g_free(buffer);
232
233     if (r != packet_size) { 
234       /* ERF record too short, accept the file,
235          only if the very first records have been successfully checked */
236       if (i < MIN_RECORDS_FOR_ERF_CHECK) {
237         return 0;
238       }
239
240     }
241   } /* records_for_erf_check */
242
243   if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) {    /* rewind */
244     return -1;
245   }
246
247   wth->data_offset = 0;
248
249   /* This is an ERF file */
250   wth->file_type = WTAP_FILE_ERF;
251   wth->snapshot_length = 0;     /* not available in header, only in frame */
252
253   /*
254    * Use the encapsulation for ERF records.
255    */
256   wth->file_encap = WTAP_ENCAP_ERF;
257
258   wth->subtype_read = erf_read;
259   wth->subtype_seek_read = erf_seek_read;
260   wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
261
262   return 1;
263 }
264
265 /* Read the next packet */
266 static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
267                          gint64 *data_offset)
268 {
269   erf_header_t erf_header;
270   guint32 packet_size, bytes_read;
271
272   *data_offset = wth->data_offset;
273
274   do {
275     if (!erf_read_header(wth->fh,
276                          &wth->phdr, &wth->pseudo_header, &erf_header,
277                          err, err_info, &bytes_read, &packet_size)) {
278       return FALSE;
279     }
280     wth->data_offset += bytes_read;
281   } while ( erf_header.type == ERF_TYPE_PAD );
282
283   buffer_assure_space(wth->frame_buffer, packet_size);
284
285   wtap_file_read_expected_bytes(buffer_start_ptr(wth->frame_buffer),
286                                 (gint32)(packet_size), wth->fh, err );
287   wth->data_offset += packet_size;
288
289   return TRUE;
290 }
291
292 static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
293                               union wtap_pseudo_header *pseudo_header, guchar *pd,
294                               int length _U_, int *err, gchar **err_info)
295 {
296   erf_header_t erf_header;
297   guint32 packet_size;
298
299   if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
300     return FALSE;
301
302   do {
303     if (!erf_read_header(wth->random_fh, NULL, pseudo_header, &erf_header,
304                          err, err_info, NULL, &packet_size))
305       return FALSE;
306   } while ( erf_header.type == ERF_TYPE_PAD );
307
308   wtap_file_read_expected_bytes(pd, (int)packet_size, wth->random_fh, err);
309
310   return TRUE;
311 }
312
313 static int erf_read_header(FILE_T fh,
314                            struct wtap_pkthdr *phdr,
315                            union wtap_pseudo_header *pseudo_header,
316                            erf_header_t *erf_header,
317                            int *err,
318                            gchar **err_info,
319                            guint32 *bytes_read,
320                            guint32 *packet_size)
321 {
322   guint32 mc_hdr;
323   guint8 erf_exhdr[8];
324   guint64 erf_exhdr_sw;
325   guint8 type = 0;
326   guint16 eth_hdr;
327   guint32 skiplen=0;
328   int i = 0 , max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
329
330   wtap_file_read_expected_bytes(erf_header, sizeof(*erf_header), fh, err);
331   if (bytes_read != NULL) {
332     *bytes_read = sizeof(*erf_header);
333   }
334
335   *packet_size =  g_ntohs(erf_header->rlen) - (guint32)sizeof(*erf_header);
336
337   if (*packet_size > WTAP_MAX_PACKET_SIZE) {
338     /*
339      * Probably a corrupt capture file; don't blow up trying
340      * to allocate space for an immensely-large packet.
341      */
342     *err = WTAP_ERR_BAD_RECORD;
343     *err_info = g_strdup_printf("erf: File has %u-byte packet, bigger than maximum of %u",
344                                 *packet_size, WTAP_MAX_PACKET_SIZE);
345     return FALSE;
346   }
347
348   if (phdr != NULL) {
349     guint64 ts = pletohll(&erf_header->ts);
350
351     phdr->ts.secs = (long) (ts >> 32);
352     ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
353     ts += (ts & 0x80000000) << 1; /* rounding */
354     phdr->ts.nsecs = ((int) (ts >> 32));
355     if (phdr->ts.nsecs >= 1000000000) {
356       phdr->ts.nsecs -= 1000000000;
357       phdr->ts.secs += 1;
358     }
359   }
360
361   /* Copy the ERF pseudo header */
362   pseudo_header->erf.phdr.ts = pletohll(&erf_header->ts);
363   pseudo_header->erf.phdr.type = erf_header->type;
364   pseudo_header->erf.phdr.flags = erf_header->flags;
365   pseudo_header->erf.phdr.rlen = g_ntohs(erf_header->rlen);
366   pseudo_header->erf.phdr.lctr = g_ntohs(erf_header->lctr);
367   pseudo_header->erf.phdr.wlen = g_ntohs(erf_header->wlen);
368
369   /* Copy the ERF extension header into the pseudo header */
370   type = erf_header->type;
371   while (type & 0x80){
372           wtap_file_read_expected_bytes(&erf_exhdr, sizeof(erf_exhdr), fh, err);
373           if (bytes_read != NULL)
374                   *bytes_read += (guint32)sizeof(erf_exhdr);
375           *packet_size -=  (guint32)sizeof(erf_exhdr);
376           skiplen += (guint32)sizeof(erf_exhdr);
377           erf_exhdr_sw = pntohll((guint64*) &(erf_exhdr[0]));
378           if (i < max)
379             memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
380           type = erf_exhdr[0];
381           i++;
382   }
383
384   switch (erf_header->type & 0x7F) {
385   case ERF_TYPE_IPV4:
386   case ERF_TYPE_IPV6:
387   case ERF_TYPE_RAW_LINK:
388   case ERF_TYPE_INFINIBAND:
389     /***
390     if (phdr != NULL) {
391       phdr->len =  g_htons(erf_header->wlen);
392       phdr->caplen = g_htons(erf_header->wlen); 
393     }  
394     return TRUE;
395     ***/
396     break;
397   case ERF_TYPE_PAD:
398   case ERF_TYPE_HDLC_POS:
399   case ERF_TYPE_COLOR_HDLC_POS:
400   case ERF_TYPE_DSM_COLOR_HDLC_POS:
401   case ERF_TYPE_ATM:
402   case ERF_TYPE_AAL5:
403   case ERF_TYPE_AAL2:
404     break;
405
406   case ERF_TYPE_ETH:
407   case ERF_TYPE_COLOR_ETH:
408   case ERF_TYPE_DSM_COLOR_ETH:
409     wtap_file_read_expected_bytes(&eth_hdr, sizeof(eth_hdr), fh, err);
410     if (bytes_read != NULL)
411       *bytes_read += (guint32)sizeof(eth_hdr);
412     *packet_size -=  (guint32)sizeof(eth_hdr);
413     skiplen += (guint32)sizeof(eth_hdr);
414     pseudo_header->erf.subhdr.eth_hdr = g_htons(eth_hdr);
415     break;
416
417   case ERF_TYPE_MC_HDLC:
418   case ERF_TYPE_MC_RAW:
419   case ERF_TYPE_MC_ATM:
420   case ERF_TYPE_MC_RAW_CHANNEL:
421   case ERF_TYPE_MC_AAL5:
422   case ERF_TYPE_MC_AAL2:
423   case ERF_TYPE_COLOR_MC_HDLC_POS:
424     wtap_file_read_expected_bytes(&mc_hdr, sizeof(mc_hdr), fh, err);
425     if (bytes_read != NULL)
426       *bytes_read += (guint32)sizeof(mc_hdr);
427     *packet_size -=  (guint32)sizeof(mc_hdr);
428     skiplen += (guint32)sizeof(mc_hdr);
429     pseudo_header->erf.subhdr.mc_hdr = g_htonl(mc_hdr);
430     break;
431
432   case ERF_TYPE_IP_COUNTER:
433   case ERF_TYPE_TCP_FLOW_COUNTER:
434     /* unsupported, continue with default: */
435   default:
436     *err = WTAP_ERR_UNSUPPORTED_ENCAP;
437     *err_info = g_strdup_printf("erf: unknown record encapsulation %u",
438                                 erf_header->type);
439     return FALSE;
440   }
441
442   if (phdr != NULL) {
443     phdr->len = g_htons(erf_header->wlen);
444     phdr->caplen = min( g_htons(erf_header->wlen),
445                         g_htons(erf_header->rlen) - (guint32)sizeof(*erf_header) - skiplen );
446   }
447   return TRUE;
448 }