2 * Copyright (c) 2006 Paolo Abeni (Italy)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Bluetooth sniffing API implementation for Linux platform
31 * By Paolo Abeni <paolo.abeni@email.it>
38 #include "pcap-bt-linux.h"
39 #include "pcap/bluetooth.h"
40 #include "diag-control.h"
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
49 #include <arpa/inet.h>
51 #include <bluetooth/bluetooth.h>
52 #include <bluetooth/hci.h>
54 #define BT_IFACE "bluetooth"
55 #define BT_CTRL_SIZE 128
57 /* forward declaration */
58 static int bt_activate(pcap_t
*);
59 static int bt_read_linux(pcap_t
*, int , pcap_handler
, u_char
*);
60 static int bt_inject_linux(pcap_t
*, const void *, int);
61 static int bt_setdirection_linux(pcap_t
*, pcap_direction_t
);
62 static int bt_stats_linux(pcap_t
*, struct pcap_stat
*);
65 * Private data for capturing on Linux Bluetooth devices.
68 int dev_id
; /* device ID of device we're bound to */
72 bt_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
74 struct hci_dev_list_req
*dev_list
;
75 struct hci_dev_req
*dev_req
;
80 sock
= socket(AF_BLUETOOTH
, SOCK_RAW
, BTPROTO_HCI
);
83 /* if bluetooth is not supported this is not fatal*/
84 if (errno
== EAFNOSUPPORT
)
86 pcapint_fmt_errmsg_for_errno(err_str
, PCAP_ERRBUF_SIZE
,
87 errno
, "Can't open raw Bluetooth socket");
91 dev_list
= malloc(HCI_MAX_DEV
* sizeof(*dev_req
) + sizeof(*dev_list
));
94 snprintf(err_str
, PCAP_ERRBUF_SIZE
, "Can't allocate %zu bytes for Bluetooth device list",
95 HCI_MAX_DEV
* sizeof(*dev_req
) + sizeof(*dev_list
));
101 * Zero the complete header, which is larger than dev_num because of tail
102 * padding, to silence Valgrind, which overshoots validating that dev_num
104 * https://github.com/the-tcpdump-group/libpcap/issues/1083
105 * https://bugs.kde.org/show_bug.cgi?id=448464
107 memset(dev_list
, 0, sizeof(*dev_list
));
108 dev_list
->dev_num
= HCI_MAX_DEV
;
110 if (ioctl(sock
, HCIGETDEVLIST
, (void *) dev_list
) < 0)
112 pcapint_fmt_errmsg_for_errno(err_str
, PCAP_ERRBUF_SIZE
,
113 errno
, "Can't get Bluetooth device list via ioctl");
118 dev_req
= dev_list
->dev_req
;
119 for (i
= 0; i
< dev_list
->dev_num
; i
++, dev_req
++) {
120 char dev_name
[20], dev_descr
[40];
122 snprintf(dev_name
, sizeof(dev_name
), BT_IFACE
"%u", dev_req
->dev_id
);
123 snprintf(dev_descr
, sizeof(dev_descr
), "Bluetooth adapter number %u", i
);
126 * Bluetooth is a wireless technology.
127 * XXX - if there's the notion of associating with a
128 * network, and we can determine whether the interface
129 * is associated with a network, check that and set
130 * the status to PCAP_IF_CONNECTION_STATUS_CONNECTED
131 * or PCAP_IF_CONNECTION_STATUS_DISCONNECTED.
133 if (pcapint_add_dev(devlistp
, dev_name
, PCAP_IF_WIRELESS
, dev_descr
, err_str
) == NULL
)
149 bt_create(const char *device
, char *ebuf
, int *is_ours
)
156 /* Does this look like a Bluetooth device? */
158 /* Does it begin with BT_IFACE? */
159 if (strncmp(cp
, BT_IFACE
, sizeof BT_IFACE
- 1) != 0) {
160 /* Nope, doesn't begin with BT_IFACE */
164 /* Yes - is BT_IFACE followed by a number? */
165 cp
+= sizeof BT_IFACE
- 1;
166 devnum
= strtol(cp
, &cpend
, 10);
167 if (cpend
== cp
|| *cpend
!= '\0') {
168 /* Not followed by a number. */
173 /* Followed by a non-valid number. */
178 /* OK, it's probably ours. */
181 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_bt
);
185 p
->activate_op
= bt_activate
;
190 bt_activate(pcap_t
* handle
)
192 struct pcap_bt
*handlep
= handle
->priv
;
193 struct sockaddr_hci addr
;
196 struct hci_filter flt
;
197 int err
= PCAP_ERROR
;
199 /* get bt interface id */
200 if (sscanf(handle
->opt
.device
, BT_IFACE
"%d", &dev_id
) != 1)
202 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
203 "Can't get Bluetooth device index from %s",
209 * Turn a negative snapshot value (invalid), a snapshot value of
210 * 0 (unspecified), or a value bigger than the normal maximum
211 * value, into the maximum allowed value.
213 * If some application really *needs* a bigger snapshot
214 * length, we should just increase MAXIMUM_SNAPLEN.
216 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
217 handle
->snapshot
= MAXIMUM_SNAPLEN
;
219 /* Initialize some components of the pcap structure. */
220 handle
->bufsize
= BT_CTRL_SIZE
+sizeof(pcap_bluetooth_h4_header
)+handle
->snapshot
;
221 handle
->linktype
= DLT_BLUETOOTH_HCI_H4_WITH_PHDR
;
223 handle
->read_op
= bt_read_linux
;
224 handle
->inject_op
= bt_inject_linux
;
225 handle
->setfilter_op
= pcapint_install_bpf_program
; /* no kernel filtering */
226 handle
->setdirection_op
= bt_setdirection_linux
;
227 handle
->set_datalink_op
= NULL
; /* can't change data link type */
228 handle
->getnonblock_op
= pcapint_getnonblock_fd
;
229 handle
->setnonblock_op
= pcapint_setnonblock_fd
;
230 handle
->stats_op
= bt_stats_linux
;
231 handlep
->dev_id
= dev_id
;
233 /* Create HCI socket */
234 handle
->fd
= socket(AF_BLUETOOTH
, SOCK_RAW
, BTPROTO_HCI
);
235 if (handle
->fd
< 0) {
236 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
237 errno
, "Can't create raw socket");
241 handle
->buffer
= malloc(handle
->bufsize
);
242 if (!handle
->buffer
) {
243 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
244 errno
, "Can't allocate dump buffer");
249 if (setsockopt(handle
->fd
, SOL_HCI
, HCI_DATA_DIR
, &opt
, sizeof(opt
)) < 0) {
250 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
251 errno
, "Can't enable data direction info");
256 if (setsockopt(handle
->fd
, SOL_HCI
, HCI_TIME_STAMP
, &opt
, sizeof(opt
)) < 0) {
257 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
258 errno
, "Can't enable time stamp");
262 /* Setup filter, do not call hci function to avoid dependence on
264 memset(&flt
, 0, sizeof(flt
));
265 memset((void *) &flt
.type_mask
, 0xff, sizeof(flt
.type_mask
));
266 memset((void *) &flt
.event_mask
, 0xff, sizeof(flt
.event_mask
));
267 if (setsockopt(handle
->fd
, SOL_HCI
, HCI_FILTER
, &flt
, sizeof(flt
)) < 0) {
268 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
269 errno
, "Can't set filter");
274 /* Bind socket to the HCI device */
275 addr
.hci_family
= AF_BLUETOOTH
;
276 addr
.hci_dev
= handlep
->dev_id
;
277 #ifdef HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL
278 addr
.hci_channel
= HCI_CHANNEL_RAW
;
280 if (bind(handle
->fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
281 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
282 errno
, "Can't attach to device %d", handlep
->dev_id
);
286 if (handle
->opt
.rfmon
) {
288 * Monitor mode doesn't apply to Bluetooth devices.
290 err
= PCAP_ERROR_RFMON_NOTSUP
;
294 if (handle
->opt
.buffer_size
!= 0) {
296 * Set the socket buffer size to the specified value.
298 if (setsockopt(handle
->fd
, SOL_SOCKET
, SO_RCVBUF
,
299 &handle
->opt
.buffer_size
,
300 sizeof(handle
->opt
.buffer_size
)) == -1) {
301 pcapint_fmt_errmsg_for_errno(handle
->errbuf
,
302 errno
, PCAP_ERRBUF_SIZE
, "SO_RCVBUF");
307 handle
->selectable_fd
= handle
->fd
;
311 pcapint_cleanup_live_common(handle
);
316 bt_read_linux(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
318 struct cmsghdr
*cmsg
;
322 struct pcap_pkthdr pkth
;
323 pcap_bluetooth_h4_header
* bthdr
;
327 pktd
= handle
->buffer
+ BT_CTRL_SIZE
;
328 bthdr
= (pcap_bluetooth_h4_header
*)(void *)pktd
;
329 iv
.iov_base
= pktd
+ sizeof(pcap_bluetooth_h4_header
);
330 iv
.iov_len
= handle
->snapshot
;
332 memset(&msg
, 0, sizeof(msg
));
335 msg
.msg_control
= handle
->buffer
;
336 msg
.msg_controllen
= BT_CTRL_SIZE
;
338 /* ignore interrupt system call error */
340 if (handle
->break_loop
)
342 handle
->break_loop
= 0;
343 return PCAP_ERROR_BREAK
;
345 ret
= recvmsg(handle
->fd
, &msg
, 0);
346 } while ((ret
== -1) && (errno
== EINTR
));
349 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
350 /* Nonblocking mode, no data */
353 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
354 errno
, "Can't receive packet");
358 pkth
.caplen
= (bpf_u_int32
)ret
;
360 /* get direction and timestamp*/
361 cmsg
= CMSG_FIRSTHDR(&msg
);
363 switch (cmsg
->cmsg_type
) {
365 memcpy(&in
, CMSG_DATA(cmsg
), sizeof in
);
367 case HCI_CMSG_TSTAMP
:
368 memcpy(&pkth
.ts
, CMSG_DATA(cmsg
),
372 // for musl libc CMSG_NXTHDR()
373 DIAG_OFF_SIGN_COMPARE
374 cmsg
= CMSG_NXTHDR(&msg
, cmsg
);
377 switch (handle
->direction
) {
393 bthdr
->direction
= htonl(in
!= 0);
394 pkth
.caplen
+=sizeof(pcap_bluetooth_h4_header
);
395 pkth
.len
= pkth
.caplen
;
396 if (handle
->fcode
.bf_insns
== NULL
||
397 pcapint_filter(handle
->fcode
.bf_insns
, pktd
, pkth
.len
, pkth
.caplen
)) {
398 callback(user
, &pkth
, pktd
);
401 return 0; /* didn't pass filter */
405 bt_inject_linux(pcap_t
*handle
, const void *buf _U_
, int size _U_
)
407 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
408 "Packet injection is not supported on Bluetooth devices");
414 bt_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
416 struct pcap_bt
*handlep
= handle
->priv
;
418 struct hci_dev_info dev_info
;
419 struct hci_dev_stats
* s
= &dev_info
.stat
;
420 dev_info
.dev_id
= handlep
->dev_id
;
424 ret
= ioctl(handle
->fd
, HCIGETDEVINFO
, (void *)&dev_info
);
425 } while ((ret
== -1) && (errno
== EINTR
));
428 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
429 errno
, "Can't get stats via ioctl");
434 /* we receive both rx and tx frames, so cumulate all stats */
435 stats
->ps_recv
= s
->evt_rx
+ s
->acl_rx
+ s
->sco_rx
+ s
->cmd_tx
+
436 s
->acl_tx
+s
->sco_tx
;
437 stats
->ps_drop
= s
->err_rx
+ s
->err_tx
;
438 stats
->ps_ifdrop
= 0;
443 bt_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
446 * It's guaranteed, at this point, that d is a valid