X-Git-Url: https://git.tcpdump.org/libpcap/blobdiff_plain/f3a6505ba53fc932d810d9578bd7ef6db1da8172..09b51d326c38ea8e10ce4da09c09d50e08c5aeb8:/pcap-bt-monitor-linux.c diff --git a/pcap-bt-monitor-linux.c b/pcap-bt-monitor-linux.c index 521d6d50..206e65b5 100644 --- a/pcap-bt-monitor-linux.c +++ b/pcap-bt-monitor-linux.c @@ -30,7 +30,7 @@ */ #ifdef HAVE_CONFIG_H -#include "config.h" +#include #endif #include @@ -49,6 +49,14 @@ #define BT_CONTROL_SIZE 32 #define INTERFACE_NAME "bluetooth-monitor" +/* + * Private data. + * Currently contains nothing. + */ +struct pcap_bt_monitor { + int dummy; +}; + /* * Fields and alignment must match the declaration in the Linux kernel 3.4+. * See struct hci_mon_hdr in include/net/bluetooth/hci_mon.h. @@ -60,12 +68,21 @@ struct hci_mon_hdr { } __attribute__((packed)); int -bt_monitor_findalldevs(pcap_if_t **alldevsp, char *err_str) +bt_monitor_findalldevs(pcap_if_list_t *devlistp, char *err_str) { int ret = 0; - if (pcap_add_if(alldevsp, INTERFACE_NAME, 0, - "Bluetooth Linux Monitor", err_str) < 0) + /* + * Bluetooth is a wireless technology. + * + * This is a device to monitor all Bluetooth interfaces, so + * there's no notion of "connected" or "disconnected", any + * more than there's a notion of "connected" or "disconnected" + * for the "any" device. + */ + if (add_dev(devlistp, INTERFACE_NAME, + PCAP_IF_WIRELESS|PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, + "Bluetooth Linux Monitor", err_str) == NULL) { ret = -1; } @@ -110,12 +127,16 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch } while ((ret == -1) && (errno == EINTR)); if (ret < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't receive packet: %s", strerror(errno)); + if (errno == EAGAIN || errno == EWOULDBLOCK) { + /* Nonblocking mode, no data */ + return 0; + } + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't receive packet"); return -1; } - pkth.caplen = ret - sizeof(hdr) + sizeof(pcap_bluetooth_linux_monitor_header); + pkth.caplen = (bpf_u_int32)(ret - sizeof(hdr) + sizeof(pcap_bluetooth_linux_monitor_header)); pkth.len = pkth.caplen; for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { @@ -130,7 +151,7 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch bthdr->opcode = htons(hdr.opcode); if (handle->fcode.bf_insns == NULL || - bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) { + pcap_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) { callback(user, &pkth, pktd); return 1; } @@ -138,19 +159,13 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch } static int -bt_monitor_inject(pcap_t *handle, const void *buf _U_, size_t size _U_) +bt_monitor_inject(pcap_t *handle, const void *buf _U_, int size _U_) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported yet"); + snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, + "Packet injection is not supported yet on Bluetooth monitor devices"); return -1; } -static int -bt_monitor_setdirection(pcap_t *p, pcap_direction_t d) -{ - p->direction = d; - return 0; -} - static int bt_monitor_stats(pcap_t *handle _U_, struct pcap_stat *stats) { @@ -173,13 +188,24 @@ bt_monitor_activate(pcap_t* handle) return PCAP_ERROR_RFMON_NOTSUP; } + /* + * Turn a negative snapshot value (invalid), a snapshot value of + * 0 (unspecified), or a value bigger than the normal maximum + * value, into the maximum allowed value. + * + * If some application really *needs* a bigger snapshot + * length, we should just increase MAXIMUM_SNAPLEN. + */ + if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN) + handle->snapshot = MAXIMUM_SNAPLEN; + handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot; handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR; handle->read_op = bt_monitor_read; handle->inject_op = bt_monitor_inject; handle->setfilter_op = install_bpf_program; /* no kernel filtering */ - handle->setdirection_op = bt_monitor_setdirection; + handle->setdirection_op = NULL; /* Not implemented */ handle->set_datalink_op = NULL; /* can't change data link type */ handle->getnonblock_op = pcap_getnonblock_fd; handle->setnonblock_op = pcap_setnonblock_fd; @@ -187,15 +213,15 @@ bt_monitor_activate(pcap_t* handle) handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (handle->fd < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't create raw socket: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't create raw socket"); return PCAP_ERROR; } handle->buffer = malloc(handle->bufsize); if (!handle->buffer) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't allocate dump buffer"); goto close_fail; } @@ -205,15 +231,15 @@ bt_monitor_activate(pcap_t* handle) addr.hci_channel = HCI_CHANNEL_MONITOR; if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't attach to interface: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't attach to interface"); goto close_fail; } opt = 1; if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't enable time stamp: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't enable time stamp"); goto close_fail; } @@ -242,7 +268,7 @@ bt_monitor_create(const char *device, char *ebuf, int *is_ours) } *is_ours = 1; - p = pcap_create_common(ebuf, 0); + p = PCAP_CREATE_COMMON(ebuf, struct pcap_bt_monitor); if (p == NULL) return NULL;