Don't write out packets that have a "captured length" bigger than we're
[metze/wireshark/wip.git] / wiretap / btsnoop.c
1 /* btsnoop.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "config.h"
24 #include <errno.h>
25 #include <string.h>
26 #include "wtap-int.h"
27 #include "file_wrappers.h"
28 #include "buffer.h"
29 #include "atm.h"
30 #include "btsnoop.h"
31
32 /*
33  * Symbian's btsnoop format is derived from Sun's snoop format.
34  * See RFC 1761 for a description of the "snoop" file format.
35  */
36
37 /* Magic number in "btsnoop" files. */
38 static const char btsnoop_magic[] = {
39         'b', 't', 's', 'n', 'o', 'o', 'p', '\0'
40 };
41
42 /* "btsnoop" file header (minus magic number). */
43 struct btsnoop_hdr {
44         guint32 version;        /* version number (should be 1) */
45         guint32 datalink;       /* datalink type */
46 };
47
48 /* "btsnoop" record header. */
49 struct btsnooprec_hdr {
50         guint32 orig_len;       /* actual length of packet */
51         guint32 incl_len;       /* number of octets captured in file */
52         guint32 flags;          /* packet flags */
53         guint32 cum_drops;      /* cumulative number of dropped packets */
54         gint64  ts_usec;        /* timestamp microseconds */
55 };
56
57 /* H1 is unframed data with the packet type encoded in the flags field of capture header */
58 /* It can be used for any datalink by placing logging above the datalink layer of HCI */
59 #define KHciLoggerDatalinkTypeH1                1001
60 /* H4 is the serial HCI with packet type encoded in the first byte of each packet */
61 #define KHciLoggerDatalinkTypeH4                1002
62 /* CSR's PPP derived bluecore serial protocol - in practice we log in H1 format after deframing */
63 #define KHciLoggerDatalinkTypeBCSP              1003
64 /* H5 is the official three wire serial protocol derived from BCSP*/
65 #define KHciLoggerDatalinkTypeH5                1004
66 /* Linux Monitor */
67 #define KHciLoggerDatalinkLinuxMonitor   2001
68 /* BlueZ 5 Simulator */
69 #define KHciLoggerDatalinkBlueZ5Simulator       2002
70
71 #define KHciLoggerHostToController              0
72 #define KHciLoggerControllerToHost              0x00000001
73 #define KHciLoggerACLDataFrame                  0
74 #define KHciLoggerCommandOrEvent                0x00000002
75
76 static const gint64 KUnixTimeBase = G_GINT64_CONSTANT(0x00dcddb30f2f8000); /* offset from symbian - unix time */
77
78 static gboolean btsnoop_read(wtap *wth, int *err, gchar **err_info,
79     gint64 *data_offset);
80 static gboolean btsnoop_seek_read(wtap *wth, gint64 seek_off,
81     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
82 static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
83     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
84
85 int btsnoop_open(wtap *wth, int *err, gchar **err_info)
86 {
87         int bytes_read;
88         char magic[sizeof btsnoop_magic];
89         struct btsnoop_hdr hdr;
90
91         int file_encap=WTAP_ENCAP_UNKNOWN;
92
93         /* Read in the string that should be at the start of a "btsnoop" file */
94         errno = WTAP_ERR_CANT_READ;
95         bytes_read = file_read(magic, sizeof magic, wth->fh);
96         if (bytes_read != sizeof magic) {
97                 *err = file_error(wth->fh, err_info);
98                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
99                         return -1;
100                 return 0;
101         }
102
103         if (memcmp(magic, btsnoop_magic, sizeof btsnoop_magic) != 0) {
104                 return 0;
105         }
106
107         /* Read the rest of the header. */
108         errno = WTAP_ERR_CANT_READ;
109         bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
110         if (bytes_read != sizeof hdr) {
111                 *err = file_error(wth->fh, err_info);
112                 if (*err == 0)
113                         *err = WTAP_ERR_SHORT_READ;
114                 return -1;
115         }
116
117         /*
118          * Make sure it's a version we support.
119          */
120         hdr.version = g_ntohl(hdr.version);
121         if (hdr.version != 1) {
122                 *err = WTAP_ERR_UNSUPPORTED;
123                 *err_info = g_strdup_printf("btsnoop: version %u unsupported", hdr.version);
124                 return -1;
125         }
126
127         hdr.datalink = g_ntohl(hdr.datalink);
128         switch (hdr.datalink) {
129         case KHciLoggerDatalinkTypeH1:
130                 file_encap=WTAP_ENCAP_BLUETOOTH_HCI;
131                 break;
132         case KHciLoggerDatalinkTypeH4:
133                 file_encap=WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
134                 break;
135         case KHciLoggerDatalinkTypeBCSP:
136                 *err = WTAP_ERR_UNSUPPORTED;
137                 *err_info = g_strdup_printf("btsnoop: BCSP capture logs unsupported");
138                 return -1;
139         case KHciLoggerDatalinkTypeH5:
140                 *err = WTAP_ERR_UNSUPPORTED;
141                 *err_info = g_strdup_printf("btsnoop: H5 capture logs unsupported");
142                 return -1;
143         case KHciLoggerDatalinkLinuxMonitor:
144                 file_encap=WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR;
145                 break;
146         case KHciLoggerDatalinkBlueZ5Simulator:
147                 *err = WTAP_ERR_UNSUPPORTED;
148                 *err_info = g_strdup_printf("btsnoop: BlueZ 5 Simulator capture logs unsupported");
149                 return -1;
150         default:
151                 *err = WTAP_ERR_UNSUPPORTED;
152                 *err_info = g_strdup_printf("btsnoop: datalink type %u unknown or unsupported", hdr.datalink);
153                 return -1;
154         }
155
156         wth->subtype_read = btsnoop_read;
157         wth->subtype_seek_read = btsnoop_seek_read;
158         wth->file_encap = file_encap;
159         wth->snapshot_length = 0;       /* not available in header */
160         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
161         wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_BTSNOOP;
162         return 1;
163 }
164
165 static gboolean btsnoop_read(wtap *wth, int *err, gchar **err_info,
166     gint64 *data_offset)
167 {
168         *data_offset = file_tell(wth->fh);
169
170         return btsnoop_read_record(wth, wth->fh, &wth->phdr, wth->frame_buffer,
171             err, err_info);
172 }
173
174 static gboolean btsnoop_seek_read(wtap *wth, gint64 seek_off,
175     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
176 {
177         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
178                 return FALSE;
179
180         return btsnoop_read_record(wth, wth->random_fh, phdr, buf, err, err_info);
181 }
182
183 static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
184     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
185 {
186         int     bytes_read;
187         struct btsnooprec_hdr hdr;
188         guint32 packet_size;
189         guint32 flags;
190         guint32 orig_size;
191         gint64 ts;
192
193         /* Read record header. */
194
195         errno = WTAP_ERR_CANT_READ;
196         bytes_read = file_read(&hdr, sizeof hdr, fh);
197         if (bytes_read != sizeof hdr) {
198                 *err = file_error(fh, err_info);
199                 if (*err == 0 && bytes_read != 0)
200                         *err = WTAP_ERR_SHORT_READ;
201                 return FALSE;
202         }
203
204         packet_size = g_ntohl(hdr.incl_len);
205         orig_size = g_ntohl(hdr.orig_len);
206         flags = g_ntohl(hdr.flags);
207         if (packet_size > WTAP_MAX_PACKET_SIZE) {
208                 /*
209                  * Probably a corrupt capture file; don't blow up trying
210                  * to allocate space for an immensely-large packet.
211                  */
212                 *err = WTAP_ERR_BAD_FILE;
213                 *err_info = g_strdup_printf("btsnoop: File has %u-byte packet, bigger than maximum of %u",
214                     packet_size, WTAP_MAX_PACKET_SIZE);
215                 return FALSE;
216         }
217
218         ts = GINT64_FROM_BE(hdr.ts_usec);
219         ts -= KUnixTimeBase;
220
221         phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
222         phdr->ts.secs = (guint)(ts / 1000000);
223         phdr->ts.nsecs = (guint)((ts % 1000000) * 1000);
224         phdr->caplen = packet_size;
225         phdr->len = orig_size;
226         if(wth->file_encap == WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR)
227         {
228                 phdr->pseudo_header.p2p.sent = (flags & KHciLoggerControllerToHost) ? FALSE : TRUE;
229         } else if(wth->file_encap == WTAP_ENCAP_BLUETOOTH_HCI) {
230                 phdr->pseudo_header.bthci.sent = (flags & KHciLoggerControllerToHost) ? FALSE : TRUE;
231                 if(flags & KHciLoggerCommandOrEvent)
232                 {
233                         if(phdr->pseudo_header.bthci.sent)
234                         {
235                                 phdr->pseudo_header.bthci.channel = BTHCI_CHANNEL_COMMAND;
236                         }
237                         else
238                         {
239                                 phdr->pseudo_header.bthci.channel = BTHCI_CHANNEL_EVENT;
240                         }
241                 }
242                 else
243                 {
244                         phdr->pseudo_header.bthci.channel = BTHCI_CHANNEL_ACL;
245                 }
246         } else  if (wth->file_encap == WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR) {
247                 phdr->pseudo_header.btmon.opcode = flags & 0xFFFF;
248                 phdr->pseudo_header.btmon.adapter_id = flags >> 16;
249         }
250
251
252         /* Read packet data. */
253         return wtap_read_packet_bytes(fh, buf, phdr->caplen, err, err_info);
254 }
255
256 /* Returns 0 if we could write the specified encapsulation type,
257    an error indication otherwise. */
258 int btsnoop_dump_can_write_encap(int encap)
259 {
260     /* Per-packet encapsulations aren't supported. */
261     if (encap == WTAP_ENCAP_PER_PACKET)
262         return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
263
264     /* XXX - for now we only support WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR and WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR */
265     if (encap != WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR && encap != WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR)
266         return WTAP_ERR_UNSUPPORTED_ENCAP;
267
268     return 0;
269 }
270
271 struct hci_flags_mapping
272 {
273     guint8 hci_type;
274     guint8 sent;
275     guint8 flags;
276 };
277
278 static const struct hci_flags_mapping hci_flags[] =
279 {
280     { 0x02, TRUE,   KHciLoggerHostToController|KHciLoggerACLDataFrame   }, /* HCI_H4_TYPE_ACL */
281     { 0x02, FALSE,  KHciLoggerControllerToHost|KHciLoggerACLDataFrame   }, /* HCI_H4_TYPE_ACL */
282     { 0x01, TRUE,   KHciLoggerHostToController|KHciLoggerCommandOrEvent }, /* HCI_H4_TYPE_CMD */
283     { 0x04, FALSE,  KHciLoggerControllerToHost|KHciLoggerCommandOrEvent }, /* HCI_H4_TYPE_EVT */
284 };
285
286 static guint8 btsnoop_lookup_flags(guint8 hci_type, gboolean sent, guint8 *flags)
287 {
288     guint8 i;
289
290     for (i=0; i < G_N_ELEMENTS(hci_flags); ++i)
291     {
292         if (hci_flags[i].hci_type == hci_type &&
293             hci_flags[i].sent == sent)
294         {
295             *flags = hci_flags[i].flags;
296             return TRUE;
297         }
298     }
299     return FALSE;
300 }
301
302 static gboolean btsnoop_dump_partial_rec_hdr(wtap_dumper *wdh _U_,
303     const struct wtap_pkthdr *phdr,
304     const union wtap_pseudo_header *pseudo_header,
305     const guint8 *pd, int *err,
306     struct btsnooprec_hdr *rec_hdr)
307 {
308     gint64 ts_usec;
309     gint64 nsecs;
310     guint8 flags = 0;
311
312     if (!btsnoop_lookup_flags(*pd, pseudo_header->p2p.sent, &flags)) {
313         *err = WTAP_ERR_UNSUPPORTED;
314         return FALSE;
315     }
316
317     nsecs = phdr->ts.nsecs;
318     ts_usec  = ((gint64) phdr->ts.secs * 1000000) + (nsecs / 1000);
319     ts_usec += KUnixTimeBase;
320
321     rec_hdr->flags = GUINT32_TO_BE(flags);
322     rec_hdr->cum_drops = GUINT32_TO_BE(0);
323     rec_hdr->ts_usec = GINT64_TO_BE(ts_usec);
324
325     return TRUE;
326 }
327
328 /* FIXME: How do we support multiple backends?*/
329 static gboolean btsnoop_dump_h1(wtap_dumper *wdh,
330     const struct wtap_pkthdr *phdr,
331     const guint8 *pd, int *err)
332 {
333     const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
334     struct btsnooprec_hdr rec_hdr;
335
336     /*
337      * Don't write out anything bigger than we can read.
338      * (This will also fail on a caplen of 0, as it should.)
339      */ 
340     if (phdr->caplen-1 > WTAP_MAX_PACKET_SIZE) {
341         *err = WTAP_ERR_PACKET_TOO_LARGE;
342         return FALSE;
343     }
344
345     if (!btsnoop_dump_partial_rec_hdr(wdh, phdr, pseudo_header, pd, err, &rec_hdr))
346         return FALSE;
347
348     rec_hdr.incl_len = GUINT32_TO_BE(phdr->caplen-1);
349     rec_hdr.orig_len = GUINT32_TO_BE(phdr->len-1);
350
351     if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
352         return FALSE;
353
354     wdh->bytes_dumped += sizeof rec_hdr;
355
356     /* Skip HCI packet type */
357     ++pd;
358
359     if (!wtap_dump_file_write(wdh, pd, phdr->caplen-1, err))
360         return FALSE;
361
362     wdh->bytes_dumped += phdr->caplen-1;
363
364     return TRUE;
365 }
366
367 static gboolean btsnoop_dump_h4(wtap_dumper *wdh,
368     const struct wtap_pkthdr *phdr,
369     const guint8 *pd, int *err)
370 {
371     const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
372     struct btsnooprec_hdr rec_hdr;
373
374     /* Don't write out anything bigger than we can read. */
375     if (phdr->caplen > WTAP_MAX_PACKET_SIZE) {
376         *err = WTAP_ERR_PACKET_TOO_LARGE;
377         return FALSE;
378     }
379
380     if (!btsnoop_dump_partial_rec_hdr(wdh, phdr, pseudo_header, pd, err, &rec_hdr))
381         return FALSE;
382
383     rec_hdr.incl_len = GUINT32_TO_BE(phdr->caplen);
384     rec_hdr.orig_len = GUINT32_TO_BE(phdr->len);
385
386     if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
387         return FALSE;
388
389     wdh->bytes_dumped += sizeof rec_hdr;
390
391     if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
392         return FALSE;
393
394     wdh->bytes_dumped += phdr->caplen;
395
396     return TRUE;
397 }
398
399 /* FIXME: How do we support multiple backends?*/
400 gboolean btsnoop_dump_open_h1(wtap_dumper *wdh, int *err)
401 {
402     struct btsnoop_hdr file_hdr;
403
404     /* This is a libpcap file */
405     wdh->subtype_write = btsnoop_dump_h1;
406     wdh->subtype_close = NULL;
407
408     /* Write the file header. */
409     switch (wdh->file_type_subtype) {
410
411     case WTAP_FILE_TYPE_SUBTYPE_BTSNOOP:
412         wdh->tsprecision = WTAP_FILE_TSPREC_USEC;
413         break;
414
415     default:
416         /* We should never get here - our open routine
417            should only get called for the types above. */
418         *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
419         return FALSE;
420     }
421
422     if (!wtap_dump_file_write(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
423         return FALSE;
424
425     wdh->bytes_dumped += sizeof btsnoop_magic;
426
427     /* current "btsnoop" format is 1 */
428     file_hdr.version  = GUINT32_TO_BE(1);
429     /* HCI type encoded in first byte */
430     file_hdr.datalink = GUINT32_TO_BE(KHciLoggerDatalinkTypeH1);
431
432     if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
433         return FALSE;
434
435     wdh->bytes_dumped += sizeof file_hdr;
436
437     return TRUE;
438 }
439
440 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
441    failure */
442 gboolean btsnoop_dump_open_h4(wtap_dumper *wdh, int *err)
443 {
444     struct btsnoop_hdr file_hdr;
445
446     /* This is a libpcap file */
447     wdh->subtype_write = btsnoop_dump_h4;
448     wdh->subtype_close = NULL;
449
450     /* Write the file header. */
451     switch (wdh->file_type_subtype) {
452
453     case WTAP_FILE_TYPE_SUBTYPE_BTSNOOP:
454         wdh->tsprecision = WTAP_FILE_TSPREC_USEC;
455         break;
456
457     default:
458         /* We should never get here - our open routine
459            should only get called for the types above. */
460         *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
461         return FALSE;
462     }
463
464     if (!wtap_dump_file_write(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
465         return FALSE;
466
467     wdh->bytes_dumped += sizeof btsnoop_magic;
468
469     /* current "btsnoop" format is 1 */
470     file_hdr.version  = GUINT32_TO_BE(1);
471     /* HCI type encoded in first byte */
472     file_hdr.datalink = GUINT32_TO_BE(KHciLoggerDatalinkTypeH4);
473
474     if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
475         return FALSE;
476
477     wdh->bytes_dumped += sizeof file_hdr;
478
479     return TRUE;
480 }