2008-02-03 21:11:53 +00:00
|
|
|
/* btsnoop.c
|
|
|
|
*
|
|
|
|
* Wiretap Library
|
|
|
|
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
|
|
|
|
*
|
2018-02-07 11:26:45 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2008-02-03 21:11:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "wtap-int.h"
|
|
|
|
#include "file_wrappers.h"
|
|
|
|
#include "btsnoop.h"
|
2008-06-24 18:00:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Symbian's btsnoop format is derived from Sun's snoop format.
|
|
|
|
* See RFC 1761 for a description of the "snoop" file format.
|
|
|
|
*/
|
2008-02-03 21:11:53 +00:00
|
|
|
|
|
|
|
/* Magic number in "btsnoop" files. */
|
|
|
|
static const char btsnoop_magic[] = {
|
2015-01-02 00:45:22 +00:00
|
|
|
'b', 't', 's', 'n', 'o', 'o', 'p', '\0'
|
2008-02-03 21:11:53 +00:00
|
|
|
};
|
|
|
|
|
2008-06-24 18:00:14 +00:00
|
|
|
/* "btsnoop" file header (minus magic number). */
|
2008-02-03 21:11:53 +00:00
|
|
|
struct btsnoop_hdr {
|
2015-01-02 00:45:22 +00:00
|
|
|
guint32 version; /* version number (should be 1) */
|
|
|
|
guint32 datalink; /* datalink type */
|
2008-02-03 21:11:53 +00:00
|
|
|
};
|
|
|
|
|
2008-06-24 18:00:14 +00:00
|
|
|
/* "btsnoop" record header. */
|
2008-02-03 21:11:53 +00:00
|
|
|
struct btsnooprec_hdr {
|
2015-01-02 00:45:22 +00:00
|
|
|
guint32 orig_len; /* actual length of packet */
|
|
|
|
guint32 incl_len; /* number of octets captured in file */
|
|
|
|
guint32 flags; /* packet flags */
|
|
|
|
guint32 cum_drops; /* cumulative number of dropped packets */
|
|
|
|
gint64 ts_usec; /* timestamp microseconds */
|
2008-02-03 21:11:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* H1 is unframed data with the packet type encoded in the flags field of capture header */
|
|
|
|
/* It can be used for any datalink by placing logging above the datalink layer of HCI */
|
2015-01-02 00:45:22 +00:00
|
|
|
#define KHciLoggerDatalinkTypeH1 1001
|
2008-02-03 21:11:53 +00:00
|
|
|
/* H4 is the serial HCI with packet type encoded in the first byte of each packet */
|
2015-01-02 00:45:22 +00:00
|
|
|
#define KHciLoggerDatalinkTypeH4 1002
|
2008-02-03 21:11:53 +00:00
|
|
|
/* CSR's PPP derived bluecore serial protocol - in practice we log in H1 format after deframing */
|
2015-01-02 00:45:22 +00:00
|
|
|
#define KHciLoggerDatalinkTypeBCSP 1003
|
2008-02-03 21:11:53 +00:00
|
|
|
/* H5 is the official three wire serial protocol derived from BCSP*/
|
2015-01-02 00:45:22 +00:00
|
|
|
#define KHciLoggerDatalinkTypeH5 1004
|
2014-01-12 00:50:41 +00:00
|
|
|
/* Linux Monitor */
|
2015-01-02 00:45:22 +00:00
|
|
|
#define KHciLoggerDatalinkLinuxMonitor 2001
|
2013-11-23 21:41:08 +00:00
|
|
|
/* BlueZ 5 Simulator */
|
2015-01-02 00:45:22 +00:00
|
|
|
#define KHciLoggerDatalinkBlueZ5Simulator 2002
|
2008-02-03 21:11:53 +00:00
|
|
|
|
2015-01-02 00:45:22 +00:00
|
|
|
#define KHciLoggerHostToController 0
|
|
|
|
#define KHciLoggerControllerToHost 0x00000001
|
|
|
|
#define KHciLoggerACLDataFrame 0
|
|
|
|
#define KHciLoggerCommandOrEvent 0x00000002
|
2008-02-03 21:11:53 +00:00
|
|
|
|
2013-05-16 22:14:11 +00:00
|
|
|
static const gint64 KUnixTimeBase = G_GINT64_CONSTANT(0x00dcddb30f2f8000); /* offset from symbian - unix time */
|
2008-02-03 21:11:53 +00:00
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean btsnoop_read(wtap *wth, int *err, gchar **err_info,
|
2014-05-09 05:18:49 +00:00
|
|
|
gint64 *data_offset);
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean btsnoop_seek_read(wtap *wth, gint64 seek_off,
|
2018-02-09 00:19:12 +00:00
|
|
|
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
|
2018-02-09 00:19:12 +00:00
|
|
|
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
|
2008-02-03 21:11:53 +00:00
|
|
|
|
2014-10-09 23:44:15 +00:00
|
|
|
wtap_open_return_val btsnoop_open(wtap *wth, int *err, gchar **err_info)
|
2008-02-03 21:11:53 +00:00
|
|
|
{
|
2015-01-02 00:45:22 +00:00
|
|
|
char magic[sizeof btsnoop_magic];
|
|
|
|
struct btsnoop_hdr hdr;
|
|
|
|
|
|
|
|
int file_encap=WTAP_ENCAP_UNKNOWN;
|
|
|
|
|
|
|
|
/* Read in the string that should be at the start of a "btsnoop" file */
|
|
|
|
if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
|
|
|
|
if (*err != WTAP_ERR_SHORT_READ)
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
return WTAP_OPEN_NOT_MINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(magic, btsnoop_magic, sizeof btsnoop_magic) != 0) {
|
|
|
|
return WTAP_OPEN_NOT_MINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the rest of the header. */
|
|
|
|
if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info))
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure it's a version we support.
|
|
|
|
*/
|
|
|
|
hdr.version = g_ntohl(hdr.version);
|
|
|
|
if (hdr.version != 1) {
|
|
|
|
*err = WTAP_ERR_UNSUPPORTED;
|
|
|
|
*err_info = g_strdup_printf("btsnoop: version %u unsupported", hdr.version);
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr.datalink = g_ntohl(hdr.datalink);
|
|
|
|
switch (hdr.datalink) {
|
|
|
|
case KHciLoggerDatalinkTypeH1:
|
|
|
|
file_encap=WTAP_ENCAP_BLUETOOTH_HCI;
|
|
|
|
break;
|
|
|
|
case KHciLoggerDatalinkTypeH4:
|
|
|
|
file_encap=WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
|
|
|
|
break;
|
|
|
|
case KHciLoggerDatalinkTypeBCSP:
|
|
|
|
*err = WTAP_ERR_UNSUPPORTED;
|
2016-06-06 02:24:47 +00:00
|
|
|
*err_info = g_strdup("btsnoop: BCSP capture logs unsupported");
|
2015-01-02 00:45:22 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
case KHciLoggerDatalinkTypeH5:
|
|
|
|
*err = WTAP_ERR_UNSUPPORTED;
|
2016-06-06 02:24:47 +00:00
|
|
|
*err_info = g_strdup("btsnoop: H5 capture logs unsupported");
|
2015-01-02 00:45:22 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
case KHciLoggerDatalinkLinuxMonitor:
|
|
|
|
file_encap=WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR;
|
|
|
|
break;
|
|
|
|
case KHciLoggerDatalinkBlueZ5Simulator:
|
|
|
|
*err = WTAP_ERR_UNSUPPORTED;
|
2016-06-06 02:24:47 +00:00
|
|
|
*err_info = g_strdup("btsnoop: BlueZ 5 Simulator capture logs unsupported");
|
2015-01-02 00:45:22 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
default:
|
|
|
|
*err = WTAP_ERR_UNSUPPORTED;
|
|
|
|
*err_info = g_strdup_printf("btsnoop: datalink type %u unknown or unsupported", hdr.datalink);
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
wth->subtype_read = btsnoop_read;
|
|
|
|
wth->subtype_seek_read = btsnoop_seek_read;
|
|
|
|
wth->file_encap = file_encap;
|
|
|
|
wth->snapshot_length = 0; /* not available in header */
|
|
|
|
wth->file_tsprec = WTAP_TSPREC_USEC;
|
|
|
|
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_BTSNOOP;
|
|
|
|
return WTAP_OPEN_MINE;
|
2008-02-03 21:11:53 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean btsnoop_read(wtap *wth, int *err, gchar **err_info,
|
2015-01-02 00:45:22 +00:00
|
|
|
gint64 *data_offset)
|
2008-02-03 21:11:53 +00:00
|
|
|
{
|
2015-01-02 00:45:22 +00:00
|
|
|
*data_offset = file_tell(wth->fh);
|
2013-05-16 22:14:11 +00:00
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
return btsnoop_read_record(wth, wth->fh, &wth->rec, wth->rec_data,
|
2015-01-02 00:45:22 +00:00
|
|
|
err, err_info);
|
2013-05-16 22:14:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean btsnoop_seek_read(wtap *wth, gint64 seek_off,
|
2018-02-09 00:19:12 +00:00
|
|
|
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
|
2013-05-16 22:14:11 +00:00
|
|
|
{
|
2015-01-02 00:45:22 +00:00
|
|
|
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
|
|
|
|
return FALSE;
|
2013-05-16 22:14:11 +00:00
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
return btsnoop_read_record(wth, wth->random_fh, rec, buf, err, err_info);
|
2013-05-16 22:14:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
|
2018-02-09 00:19:12 +00:00
|
|
|
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
|
2013-05-16 22:14:11 +00:00
|
|
|
{
|
2015-01-02 00:45:22 +00:00
|
|
|
struct btsnooprec_hdr hdr;
|
|
|
|
guint32 packet_size;
|
|
|
|
guint32 flags;
|
|
|
|
guint32 orig_size;
|
|
|
|
gint64 ts;
|
|
|
|
|
|
|
|
/* Read record header. */
|
|
|
|
|
|
|
|
if (!wtap_read_bytes_or_eof(fh, &hdr, sizeof hdr, err, err_info))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
packet_size = g_ntohl(hdr.incl_len);
|
|
|
|
orig_size = g_ntohl(hdr.orig_len);
|
|
|
|
flags = g_ntohl(hdr.flags);
|
Allow bigger snapshot lengths for D-Bus captures.
Use WTAP_MAX_PACKET_SIZE_STANDARD, set to 256KB, for everything except
for D-Bus captures. Use WTAP_MAX_PACKET_SIZE_DBUS, set to 128MB, for
them, because that's the largest possible D-Bus message size. See
https://bugs.freedesktop.org/show_bug.cgi?id=100220
for an example of the problems caused by limiting the snapshot length to
256KB for D-Bus.
Have a snapshot length of 0 in a capture_file structure mean "there is
no snapshot length for the file"; we don't need the has_snap field in
that case, a value of 0 mean "no, we don't have a snapshot length".
In dumpcap, start out with a pipe buffer size of 2KB, and grow it as
necessary. When checking for a too-big packet from a pipe, check
against the appropriate maximum - 128MB for DLT_DBUS, 256KB for
everything else.
Change-Id: Ib2ce7a0cf37b971fbc0318024fd011e18add8b20
Reviewed-on: https://code.wireshark.org/review/21952
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2017-06-05 01:58:40 +00:00
|
|
|
if (packet_size > WTAP_MAX_PACKET_SIZE_STANDARD) {
|
2015-01-02 00:45:22 +00:00
|
|
|
/*
|
|
|
|
* Probably a corrupt capture file; don't blow up trying
|
|
|
|
* to allocate space for an immensely-large packet.
|
|
|
|
*/
|
|
|
|
*err = WTAP_ERR_BAD_FILE;
|
|
|
|
*err_info = g_strdup_printf("btsnoop: File has %u-byte packet, bigger than maximum of %u",
|
Allow bigger snapshot lengths for D-Bus captures.
Use WTAP_MAX_PACKET_SIZE_STANDARD, set to 256KB, for everything except
for D-Bus captures. Use WTAP_MAX_PACKET_SIZE_DBUS, set to 128MB, for
them, because that's the largest possible D-Bus message size. See
https://bugs.freedesktop.org/show_bug.cgi?id=100220
for an example of the problems caused by limiting the snapshot length to
256KB for D-Bus.
Have a snapshot length of 0 in a capture_file structure mean "there is
no snapshot length for the file"; we don't need the has_snap field in
that case, a value of 0 mean "no, we don't have a snapshot length".
In dumpcap, start out with a pipe buffer size of 2KB, and grow it as
necessary. When checking for a too-big packet from a pipe, check
against the appropriate maximum - 128MB for DLT_DBUS, 256KB for
everything else.
Change-Id: Ib2ce7a0cf37b971fbc0318024fd011e18add8b20
Reviewed-on: https://code.wireshark.org/review/21952
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2017-06-05 01:58:40 +00:00
|
|
|
packet_size, WTAP_MAX_PACKET_SIZE_STANDARD);
|
2015-01-02 00:45:22 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ts = GINT64_FROM_BE(hdr.ts_usec);
|
|
|
|
ts -= KUnixTimeBase;
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->rec_type = REC_TYPE_PACKET;
|
|
|
|
rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
|
|
|
|
rec->ts.secs = (guint)(ts / 1000000);
|
|
|
|
rec->ts.nsecs = (guint)((ts % 1000000) * 1000);
|
|
|
|
rec->rec_header.packet_header.caplen = packet_size;
|
|
|
|
rec->rec_header.packet_header.len = orig_size;
|
2015-01-02 00:45:22 +00:00
|
|
|
if(wth->file_encap == WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR)
|
|
|
|
{
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->rec_header.packet_header.pseudo_header.p2p.sent = (flags & KHciLoggerControllerToHost) ? FALSE : TRUE;
|
2015-01-02 00:45:22 +00:00
|
|
|
} else if(wth->file_encap == WTAP_ENCAP_BLUETOOTH_HCI) {
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->rec_header.packet_header.pseudo_header.bthci.sent = (flags & KHciLoggerControllerToHost) ? FALSE : TRUE;
|
2015-01-02 00:45:22 +00:00
|
|
|
if(flags & KHciLoggerCommandOrEvent)
|
|
|
|
{
|
2018-02-09 00:19:12 +00:00
|
|
|
if(rec->rec_header.packet_header.pseudo_header.bthci.sent)
|
2015-01-02 00:45:22 +00:00
|
|
|
{
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->rec_header.packet_header.pseudo_header.bthci.channel = BTHCI_CHANNEL_COMMAND;
|
2015-01-02 00:45:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->rec_header.packet_header.pseudo_header.bthci.channel = BTHCI_CHANNEL_EVENT;
|
2015-01-02 00:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->rec_header.packet_header.pseudo_header.bthci.channel = BTHCI_CHANNEL_ACL;
|
2015-01-02 00:45:22 +00:00
|
|
|
}
|
|
|
|
} else if (wth->file_encap == WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR) {
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->rec_header.packet_header.pseudo_header.btmon.opcode = flags & 0xFFFF;
|
|
|
|
rec->rec_header.packet_header.pseudo_header.btmon.adapter_id = flags >> 16;
|
2015-01-02 00:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Read packet data. */
|
2018-02-09 00:19:12 +00:00
|
|
|
return wtap_read_packet_bytes(fh, buf, rec->rec_header.packet_header.caplen, err, err_info);
|
2008-02-03 21:11:53 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 00:05:19 +00:00
|
|
|
/* Returns 0 if we could write the specified encapsulation type,
|
|
|
|
an error indication otherwise. */
|
|
|
|
int btsnoop_dump_can_write_encap(int encap)
|
|
|
|
{
|
|
|
|
/* Per-packet encapsulations aren't supported. */
|
|
|
|
if (encap == WTAP_ENCAP_PER_PACKET)
|
|
|
|
return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
|
|
|
|
|
2014-01-12 00:50:41 +00:00
|
|
|
/* XXX - for now we only support WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR and WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR */
|
|
|
|
if (encap != WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR && encap != WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR)
|
2014-12-17 06:40:45 +00:00
|
|
|
return WTAP_ERR_UNWRITABLE_ENCAP;
|
2009-05-22 00:05:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hci_flags_mapping
|
|
|
|
{
|
|
|
|
guint8 hci_type;
|
|
|
|
guint8 sent;
|
|
|
|
guint8 flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct hci_flags_mapping hci_flags[] =
|
|
|
|
{
|
|
|
|
{ 0x02, TRUE, KHciLoggerHostToController|KHciLoggerACLDataFrame }, /* HCI_H4_TYPE_ACL */
|
|
|
|
{ 0x02, FALSE, KHciLoggerControllerToHost|KHciLoggerACLDataFrame }, /* HCI_H4_TYPE_ACL */
|
|
|
|
{ 0x01, TRUE, KHciLoggerHostToController|KHciLoggerCommandOrEvent }, /* HCI_H4_TYPE_CMD */
|
|
|
|
{ 0x04, FALSE, KHciLoggerControllerToHost|KHciLoggerCommandOrEvent }, /* HCI_H4_TYPE_EVT */
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint8 btsnoop_lookup_flags(guint8 hci_type, gboolean sent, guint8 *flags)
|
|
|
|
{
|
|
|
|
guint8 i;
|
|
|
|
|
|
|
|
for (i=0; i < G_N_ELEMENTS(hci_flags); ++i)
|
|
|
|
{
|
|
|
|
if (hci_flags[i].hci_type == hci_type &&
|
|
|
|
hci_flags[i].sent == sent)
|
|
|
|
{
|
|
|
|
*flags = hci_flags[i].flags;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-12-18 00:02:50 +00:00
|
|
|
static gboolean btsnoop_format_partial_rec_hdr(
|
2018-02-09 00:19:12 +00:00
|
|
|
const wtap_rec *rec,
|
2009-05-22 00:05:19 +00:00
|
|
|
const union wtap_pseudo_header *pseudo_header,
|
2014-12-18 00:02:50 +00:00
|
|
|
const guint8 *pd, int *err, gchar **err_info,
|
2009-05-22 00:05:19 +00:00
|
|
|
struct btsnooprec_hdr *rec_hdr)
|
|
|
|
{
|
|
|
|
gint64 ts_usec;
|
2012-12-21 02:30:35 +00:00
|
|
|
gint64 nsecs;
|
2009-05-22 00:05:19 +00:00
|
|
|
guint8 flags = 0;
|
|
|
|
|
|
|
|
if (!btsnoop_lookup_flags(*pd, pseudo_header->p2p.sent, &flags)) {
|
2014-12-18 00:02:50 +00:00
|
|
|
*err = WTAP_ERR_UNWRITABLE_REC_DATA;
|
|
|
|
*err_info = g_strdup_printf("btsnoop: hci_type 0x%02x for %s data isn't supported",
|
|
|
|
*pd,
|
|
|
|
pseudo_header->p2p.sent ? "sent" : "received");
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
nsecs = rec->ts.nsecs;
|
|
|
|
ts_usec = ((gint64) rec->ts.secs * 1000000) + (nsecs / 1000);
|
2009-05-22 00:05:19 +00:00
|
|
|
ts_usec += KUnixTimeBase;
|
|
|
|
|
|
|
|
rec_hdr->flags = GUINT32_TO_BE(flags);
|
|
|
|
rec_hdr->cum_drops = GUINT32_TO_BE(0);
|
|
|
|
rec_hdr->ts_usec = GINT64_TO_BE(ts_usec);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: How do we support multiple backends?*/
|
2014-05-09 05:18:49 +00:00
|
|
|
static gboolean btsnoop_dump_h1(wtap_dumper *wdh,
|
2018-02-09 00:19:12 +00:00
|
|
|
const wtap_rec *rec,
|
2014-12-18 00:02:50 +00:00
|
|
|
const guint8 *pd, int *err, gchar **err_info)
|
2009-05-22 00:05:19 +00:00
|
|
|
{
|
2018-02-09 00:19:12 +00:00
|
|
|
const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
|
2009-05-22 00:05:19 +00:00
|
|
|
struct btsnooprec_hdr rec_hdr;
|
|
|
|
|
2014-05-24 18:28:30 +00:00
|
|
|
/* We can only write packet records. */
|
2018-02-09 00:19:12 +00:00
|
|
|
if (rec->rec_type != REC_TYPE_PACKET) {
|
2014-12-18 00:31:49 +00:00
|
|
|
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
|
2014-05-24 18:28:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
Catch attempts to write multiple encapsulation types if unsupported.
If, in the process of opening the input file, we determine that it has
packets of more than one link-layer type, we can catch attempts to write
that file to a file of a format that doesn't support more than one
link-layer type at the time we try to open the output file.
If, however, we don't discover that the file has more than one
link-layer type until we've already created the output file - for
example, if we have a pcapng file with a new IDB, with a different
link-layer type from previous IDBs, after packet blocks for the earlier
interfces - we can't catch that until we try to write the packet.
Currently, that causes the packet's data to be written out as is, so the
output file claims it's of the file's link-layer type, causing programs
reading the file to misdissect the packet.
Report WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED on the write attempt
instead, and have a nicer error message for
WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED on a write.
Change-Id: Ic41f2e4367cfe5667eb30c88cc6d3bfe422462f6
Reviewed-on: https://code.wireshark.org/review/30617
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2018-11-14 03:38:12 +00:00
|
|
|
/*
|
|
|
|
* Make sure this packet doesn't have a link-layer type that
|
|
|
|
* differs from the one for the file.
|
|
|
|
*/
|
|
|
|
if (wdh->encap != rec->rec_header.packet_header.pkt_encap) {
|
|
|
|
*err = WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-01-22 00:26:36 +00:00
|
|
|
/*
|
|
|
|
* Don't write out anything bigger than we can read.
|
|
|
|
* (This will also fail on a caplen of 0, as it should.)
|
2014-02-25 20:42:35 +00:00
|
|
|
*/
|
2018-02-09 00:19:12 +00:00
|
|
|
if (rec->rec_header.packet_header.caplen-1 > WTAP_MAX_PACKET_SIZE_STANDARD) {
|
2014-01-22 00:26:36 +00:00
|
|
|
*err = WTAP_ERR_PACKET_TOO_LARGE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
if (!btsnoop_format_partial_rec_hdr(rec, pseudo_header, pd, err, err_info,
|
2014-12-18 00:02:50 +00:00
|
|
|
&rec_hdr))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
rec_hdr.incl_len = GUINT32_TO_BE(rec->rec_header.packet_header.caplen-1);
|
|
|
|
rec_hdr.orig_len = GUINT32_TO_BE(rec->rec_header.packet_header.len-1);
|
2009-05-22 00:05:19 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
wdh->bytes_dumped += sizeof rec_hdr;
|
|
|
|
|
|
|
|
/* Skip HCI packet type */
|
|
|
|
++pd;
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, pd, rec->rec_header.packet_header.caplen-1, err))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
wdh->bytes_dumped += rec->rec_header.packet_header.caplen-1;
|
2009-05-22 00:05:19 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
static gboolean btsnoop_dump_h4(wtap_dumper *wdh,
|
2018-02-09 00:19:12 +00:00
|
|
|
const wtap_rec *rec,
|
2014-12-18 00:02:50 +00:00
|
|
|
const guint8 *pd, int *err, gchar **err_info)
|
2009-05-22 00:05:19 +00:00
|
|
|
{
|
2018-02-09 00:19:12 +00:00
|
|
|
const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
|
2009-05-22 00:05:19 +00:00
|
|
|
struct btsnooprec_hdr rec_hdr;
|
|
|
|
|
2014-05-24 18:28:30 +00:00
|
|
|
/* We can only write packet records. */
|
2018-02-09 00:19:12 +00:00
|
|
|
if (rec->rec_type != REC_TYPE_PACKET) {
|
2014-12-18 00:31:49 +00:00
|
|
|
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
|
2014-05-24 18:28:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
Catch attempts to write multiple encapsulation types if unsupported.
If, in the process of opening the input file, we determine that it has
packets of more than one link-layer type, we can catch attempts to write
that file to a file of a format that doesn't support more than one
link-layer type at the time we try to open the output file.
If, however, we don't discover that the file has more than one
link-layer type until we've already created the output file - for
example, if we have a pcapng file with a new IDB, with a different
link-layer type from previous IDBs, after packet blocks for the earlier
interfces - we can't catch that until we try to write the packet.
Currently, that causes the packet's data to be written out as is, so the
output file claims it's of the file's link-layer type, causing programs
reading the file to misdissect the packet.
Report WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED on the write attempt
instead, and have a nicer error message for
WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED on a write.
Change-Id: Ic41f2e4367cfe5667eb30c88cc6d3bfe422462f6
Reviewed-on: https://code.wireshark.org/review/30617
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2018-11-14 03:38:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure this packet doesn't have a link-layer type that
|
|
|
|
* differs from the one for the file.
|
|
|
|
*/
|
|
|
|
if (wdh->encap != rec->rec_header.packet_header.pkt_encap) {
|
|
|
|
*err = WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-01-22 00:26:36 +00:00
|
|
|
/* Don't write out anything bigger than we can read. */
|
2018-02-09 00:19:12 +00:00
|
|
|
if (rec->rec_header.packet_header.caplen > WTAP_MAX_PACKET_SIZE_STANDARD) {
|
2014-01-22 00:26:36 +00:00
|
|
|
*err = WTAP_ERR_PACKET_TOO_LARGE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
if (!btsnoop_format_partial_rec_hdr(rec, pseudo_header, pd, err, err_info,
|
2014-12-18 00:02:50 +00:00
|
|
|
&rec_hdr))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
rec_hdr.incl_len = GUINT32_TO_BE(rec->rec_header.packet_header.caplen);
|
|
|
|
rec_hdr.orig_len = GUINT32_TO_BE(rec->rec_header.packet_header.len);
|
2009-05-22 00:05:19 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
wdh->bytes_dumped += sizeof rec_hdr;
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, pd, rec->rec_header.packet_header.caplen, err))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
wdh->bytes_dumped += rec->rec_header.packet_header.caplen;
|
2009-05-22 00:05:19 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: How do we support multiple backends?*/
|
2014-05-09 05:18:49 +00:00
|
|
|
gboolean btsnoop_dump_open_h1(wtap_dumper *wdh, int *err)
|
2009-05-22 00:05:19 +00:00
|
|
|
{
|
|
|
|
struct btsnoop_hdr file_hdr;
|
|
|
|
|
2015-11-09 19:54:18 +00:00
|
|
|
/* This is a btsnoop file */
|
2009-05-22 00:05:19 +00:00
|
|
|
wdh->subtype_write = btsnoop_dump_h1;
|
|
|
|
|
|
|
|
/* Write the file header. */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
wdh->bytes_dumped += sizeof btsnoop_magic;
|
|
|
|
|
|
|
|
/* current "btsnoop" format is 1 */
|
|
|
|
file_hdr.version = GUINT32_TO_BE(1);
|
|
|
|
/* HCI type encoded in first byte */
|
|
|
|
file_hdr.datalink = GUINT32_TO_BE(KHciLoggerDatalinkTypeH1);
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
wdh->bytes_dumped += sizeof file_hdr;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
|
|
|
|
failure */
|
2014-05-09 05:18:49 +00:00
|
|
|
gboolean btsnoop_dump_open_h4(wtap_dumper *wdh, int *err)
|
2009-05-22 00:05:19 +00:00
|
|
|
{
|
|
|
|
struct btsnoop_hdr file_hdr;
|
|
|
|
|
2015-11-09 19:54:18 +00:00
|
|
|
/* This is a btsnoop file */
|
2009-05-22 00:05:19 +00:00
|
|
|
wdh->subtype_write = btsnoop_dump_h4;
|
|
|
|
|
|
|
|
/* Write the file header. */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
wdh->bytes_dumped += sizeof btsnoop_magic;
|
|
|
|
|
|
|
|
/* current "btsnoop" format is 1 */
|
|
|
|
file_hdr.version = GUINT32_TO_BE(1);
|
|
|
|
/* HCI type encoded in first byte */
|
|
|
|
file_hdr.datalink = GUINT32_TO_BE(KHciLoggerDatalinkTypeH4);
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
|
2009-05-22 00:05:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
wdh->bytes_dumped += sizeof file_hdr;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-01-02 00:45:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|