blob: 07ed1c73de5a47cdec8601c5116641a3b0d2c208 [file] [log] [blame]
JP Abgrall511eca32014-02-12 13:46:45 -08001/*
2 * Copyright (c) 2006 Paolo Abeni (Italy)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
Elliott Hughesd8845d72015-10-19 18:07:04 -070014 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
JP Abgrall511eca32014-02-12 13:46:45 -080016 * permission.
17 *
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.
29 *
30 * Bluetooth sniffing API implementation for Linux platform
31 * By Paolo Abeni <paolo.abeni@email.it>
32 *
33 */
Elliott Hughesd8845d72015-10-19 18:07:04 -070034
JP Abgrall511eca32014-02-12 13:46:45 -080035#ifdef HAVE_CONFIG_H
Haibo Huang165065a2018-07-23 17:26:52 -070036#include <config.h>
JP Abgrall511eca32014-02-12 13:46:45 -080037#endif
38
39#include "pcap-int.h"
40#include "pcap-bt-linux.h"
41#include "pcap/bluetooth.h"
42
JP Abgrall511eca32014-02-12 13:46:45 -080043#include <errno.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include <fcntl.h>
47#include <string.h>
48#include <sys/ioctl.h>
49#include <sys/socket.h>
50#include <arpa/inet.h>
51
52#include <bluetooth/bluetooth.h>
53#include <bluetooth/hci.h>
54
55#define BT_IFACE "bluetooth"
56#define BT_CTRL_SIZE 128
57
58/* forward declaration */
59static int bt_activate(pcap_t *);
60static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *);
61static int bt_inject_linux(pcap_t *, const void *, size_t);
62static int bt_setdirection_linux(pcap_t *, pcap_direction_t);
63static int bt_stats_linux(pcap_t *, struct pcap_stat *);
64
65/*
66 * Private data for capturing on Linux Bluetooth devices.
67 */
68struct pcap_bt {
69 int dev_id; /* device ID of device we're bound to */
70};
71
Elliott Hughesd8845d72015-10-19 18:07:04 -070072int
Haibo Huang165065a2018-07-23 17:26:52 -070073bt_findalldevs(pcap_if_list_t *devlistp, char *err_str)
JP Abgrall511eca32014-02-12 13:46:45 -080074{
75 struct hci_dev_list_req *dev_list;
76 struct hci_dev_req *dev_req;
77 int i, sock;
78 int ret = 0;
79
80 sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
81 if (sock < 0)
82 {
Elliott Hughesd8845d72015-10-19 18:07:04 -070083 /* if bluetooth is not supported this this is not fatal*/
JP Abgrall511eca32014-02-12 13:46:45 -080084 if (errno == EAFNOSUPPORT)
85 return 0;
Haibo Huang165065a2018-07-23 17:26:52 -070086 pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
87 errno, "Can't open raw Bluetooth socket");
JP Abgrall511eca32014-02-12 13:46:45 -080088 return -1;
89 }
90
91 dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
Elliott Hughesd8845d72015-10-19 18:07:04 -070092 if (!dev_list)
JP Abgrall511eca32014-02-12 13:46:45 -080093 {
Elliott Hughes965a4b52017-05-15 10:37:39 -070094 pcap_snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %zu bytes for Bluetooth device list",
JP Abgrall511eca32014-02-12 13:46:45 -080095 HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
96 ret = -1;
97 goto done;
98 }
99
100 dev_list->dev_num = HCI_MAX_DEV;
101
Elliott Hughesd8845d72015-10-19 18:07:04 -0700102 if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
JP Abgrall511eca32014-02-12 13:46:45 -0800103 {
Haibo Huang165065a2018-07-23 17:26:52 -0700104 pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
105 errno, "Can't get Bluetooth device list via ioctl");
JP Abgrall511eca32014-02-12 13:46:45 -0800106 ret = -1;
107 goto free;
108 }
109
110 dev_req = dev_list->dev_req;
111 for (i = 0; i < dev_list->dev_num; i++, dev_req++) {
112 char dev_name[20], dev_descr[30];
Elliott Hughesd8845d72015-10-19 18:07:04 -0700113
Elliott Hughes965a4b52017-05-15 10:37:39 -0700114 pcap_snprintf(dev_name, 20, BT_IFACE"%d", dev_req->dev_id);
115 pcap_snprintf(dev_descr, 30, "Bluetooth adapter number %d", i);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700116
Haibo Huang165065a2018-07-23 17:26:52 -0700117 /*
118 * Bluetooth is a wireless technology.
119 * XXX - if there's the notion of associating with a
120 * network, and we can determine whether the interface
121 * is associated with a network, check that and set
122 * the status to PCAP_IF_CONNECTION_STATUS_CONNECTED
123 * or PCAP_IF_CONNECTION_STATUS_DISCONNECTED.
124 */
125 if (add_dev(devlistp, dev_name, PCAP_IF_WIRELESS, dev_descr, err_str) == NULL)
JP Abgrall511eca32014-02-12 13:46:45 -0800126 {
127 ret = -1;
128 break;
129 }
JP Abgrall511eca32014-02-12 13:46:45 -0800130 }
131
132free:
133 free(dev_list);
134
135done:
136 close(sock);
137 return ret;
138}
139
140pcap_t *
141bt_create(const char *device, char *ebuf, int *is_ours)
142{
143 const char *cp;
144 char *cpend;
145 long devnum;
146 pcap_t *p;
147
148 /* Does this look like a Bluetooth device? */
149 cp = strrchr(device, '/');
150 if (cp == NULL)
151 cp = device;
152 /* Does it begin with BT_IFACE? */
153 if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) {
154 /* Nope, doesn't begin with BT_IFACE */
155 *is_ours = 0;
156 return NULL;
157 }
158 /* Yes - is BT_IFACE followed by a number? */
159 cp += sizeof BT_IFACE - 1;
160 devnum = strtol(cp, &cpend, 10);
161 if (cpend == cp || *cpend != '\0') {
162 /* Not followed by a number. */
163 *is_ours = 0;
164 return NULL;
165 }
166 if (devnum < 0) {
167 /* Followed by a non-valid number. */
168 *is_ours = 0;
169 return NULL;
170 }
171
172 /* OK, it's probably ours. */
173 *is_ours = 1;
174
Elliott Hughes965a4b52017-05-15 10:37:39 -0700175 p = pcap_create_common(ebuf, sizeof (struct pcap_bt));
JP Abgrall511eca32014-02-12 13:46:45 -0800176 if (p == NULL)
177 return (NULL);
178
179 p->activate_op = bt_activate;
180 return (p);
181}
182
183static int
184bt_activate(pcap_t* handle)
185{
186 struct pcap_bt *handlep = handle->priv;
187 struct sockaddr_hci addr;
188 int opt;
189 int dev_id;
190 struct hci_filter flt;
191 int err = PCAP_ERROR;
192
193 /* get bt interface id */
Elliott Hughes965a4b52017-05-15 10:37:39 -0700194 if (sscanf(handle->opt.device, BT_IFACE"%d", &dev_id) != 1)
JP Abgrall511eca32014-02-12 13:46:45 -0800195 {
Elliott Hughes965a4b52017-05-15 10:37:39 -0700196 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
Elliott Hughesd8845d72015-10-19 18:07:04 -0700197 "Can't get Bluetooth device index from %s",
Elliott Hughes965a4b52017-05-15 10:37:39 -0700198 handle->opt.device);
JP Abgrall511eca32014-02-12 13:46:45 -0800199 return PCAP_ERROR;
200 }
201
Haibo Huang165065a2018-07-23 17:26:52 -0700202 /*
203 * Turn a negative snapshot value (invalid), a snapshot value of
204 * 0 (unspecified), or a value bigger than the normal maximum
205 * value, into the maximum allowed value.
206 *
207 * If some application really *needs* a bigger snapshot
208 * length, we should just increase MAXIMUM_SNAPLEN.
209 */
210 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
211 handle->snapshot = MAXIMUM_SNAPLEN;
212
JP Abgrall511eca32014-02-12 13:46:45 -0800213 /* Initialize some components of the pcap structure. */
Elliott Hughes965a4b52017-05-15 10:37:39 -0700214 handle->bufsize = BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header)+handle->snapshot;
JP Abgrall511eca32014-02-12 13:46:45 -0800215 handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
216
217 handle->read_op = bt_read_linux;
218 handle->inject_op = bt_inject_linux;
219 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
220 handle->setdirection_op = bt_setdirection_linux;
221 handle->set_datalink_op = NULL; /* can't change data link type */
222 handle->getnonblock_op = pcap_getnonblock_fd;
223 handle->setnonblock_op = pcap_setnonblock_fd;
224 handle->stats_op = bt_stats_linux;
225 handlep->dev_id = dev_id;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700226
JP Abgrall511eca32014-02-12 13:46:45 -0800227 /* Create HCI socket */
228 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
229 if (handle->fd < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700230 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
231 errno, "Can't create raw socket");
JP Abgrall511eca32014-02-12 13:46:45 -0800232 return PCAP_ERROR;
233 }
234
235 handle->buffer = malloc(handle->bufsize);
236 if (!handle->buffer) {
Haibo Huang165065a2018-07-23 17:26:52 -0700237 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
238 errno, "Can't allocate dump buffer");
JP Abgrall511eca32014-02-12 13:46:45 -0800239 goto close_fail;
240 }
241
242 opt = 1;
243 if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700244 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
245 errno, "Can't enable data direction info");
JP Abgrall511eca32014-02-12 13:46:45 -0800246 goto close_fail;
247 }
248
249 opt = 1;
250 if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700251 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
252 errno, "Can't enable time stamp");
JP Abgrall511eca32014-02-12 13:46:45 -0800253 goto close_fail;
254 }
255
Elliott Hughesd8845d72015-10-19 18:07:04 -0700256 /* Setup filter, do not call hci function to avoid dependence on
JP Abgrall511eca32014-02-12 13:46:45 -0800257 * external libs */
258 memset(&flt, 0, sizeof(flt));
Elliott Hughesd8845d72015-10-19 18:07:04 -0700259 memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
JP Abgrall511eca32014-02-12 13:46:45 -0800260 memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
261 if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700262 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
263 errno, "Can't set filter");
JP Abgrall511eca32014-02-12 13:46:45 -0800264 goto close_fail;
265 }
266
267
268 /* Bind socket to the HCI device */
269 addr.hci_family = AF_BLUETOOTH;
270 addr.hci_dev = handlep->dev_id;
Haibo Huang165065a2018-07-23 17:26:52 -0700271#ifdef HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL
JP Abgrall511eca32014-02-12 13:46:45 -0800272 addr.hci_channel = HCI_CHANNEL_RAW;
273#endif
274 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700275 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
276 errno, "Can't attach to device %d", handlep->dev_id);
JP Abgrall511eca32014-02-12 13:46:45 -0800277 goto close_fail;
278 }
279
280 if (handle->opt.rfmon) {
281 /*
282 * Monitor mode doesn't apply to Bluetooth devices.
283 */
284 err = PCAP_ERROR_RFMON_NOTSUP;
285 goto close_fail;
286 }
287
288 if (handle->opt.buffer_size != 0) {
289 /*
290 * Set the socket buffer size to the specified value.
291 */
292 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
293 &handle->opt.buffer_size,
294 sizeof(handle->opt.buffer_size)) == -1) {
Haibo Huang165065a2018-07-23 17:26:52 -0700295 pcap_fmt_errmsg_for_errno(handle->errbuf,
296 errno, PCAP_ERRBUF_SIZE, "SO_RCVBUF");
JP Abgrall511eca32014-02-12 13:46:45 -0800297 goto close_fail;
298 }
299 }
300
301 handle->selectable_fd = handle->fd;
302 return 0;
303
304close_fail:
305 pcap_cleanup_live_common(handle);
306 return err;
307}
308
309static int
Haibo Huang165065a2018-07-23 17:26:52 -0700310bt_read_linux(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
JP Abgrall511eca32014-02-12 13:46:45 -0800311{
312 struct cmsghdr *cmsg;
313 struct msghdr msg;
314 struct iovec iv;
315 ssize_t ret;
316 struct pcap_pkthdr pkth;
317 pcap_bluetooth_h4_header* bthdr;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700318 u_char *pktd;
319 int in = 0;
JP Abgrall511eca32014-02-12 13:46:45 -0800320
Elliott Hughes965a4b52017-05-15 10:37:39 -0700321 pktd = (u_char *)handle->buffer + BT_CTRL_SIZE;
322 bthdr = (pcap_bluetooth_h4_header*)(void *)pktd;
323 iv.iov_base = pktd + sizeof(pcap_bluetooth_h4_header);
JP Abgrall511eca32014-02-12 13:46:45 -0800324 iv.iov_len = handle->snapshot;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700325
JP Abgrall511eca32014-02-12 13:46:45 -0800326 memset(&msg, 0, sizeof(msg));
327 msg.msg_iov = &iv;
328 msg.msg_iovlen = 1;
329 msg.msg_control = handle->buffer;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700330 msg.msg_controllen = BT_CTRL_SIZE;
JP Abgrall511eca32014-02-12 13:46:45 -0800331
332 /* ignore interrupt system call error */
333 do {
334 ret = recvmsg(handle->fd, &msg, 0);
335 if (handle->break_loop)
336 {
337 handle->break_loop = 0;
338 return -2;
339 }
340 } while ((ret == -1) && (errno == EINTR));
341
342 if (ret < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700343 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
344 errno, "Can't receive packet");
JP Abgrall511eca32014-02-12 13:46:45 -0800345 return -1;
346 }
347
348 pkth.caplen = ret;
349
Elliott Hughesd8845d72015-10-19 18:07:04 -0700350 /* get direction and timestamp*/
JP Abgrall511eca32014-02-12 13:46:45 -0800351 cmsg = CMSG_FIRSTHDR(&msg);
JP Abgrall511eca32014-02-12 13:46:45 -0800352 while (cmsg) {
353 switch (cmsg->cmsg_type) {
354 case HCI_CMSG_DIR:
355 memcpy(&in, CMSG_DATA(cmsg), sizeof in);
356 break;
357 case HCI_CMSG_TSTAMP:
358 memcpy(&pkth.ts, CMSG_DATA(cmsg),
359 sizeof pkth.ts);
360 break;
361 }
362 cmsg = CMSG_NXTHDR(&msg, cmsg);
363 }
Elliott Hughesd8845d72015-10-19 18:07:04 -0700364 if ((in && (handle->direction == PCAP_D_OUT)) ||
JP Abgrall511eca32014-02-12 13:46:45 -0800365 ((!in) && (handle->direction == PCAP_D_IN)))
366 return 0;
367
368 bthdr->direction = htonl(in != 0);
369 pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
370 pkth.len = pkth.caplen;
371 if (handle->fcode.bf_insns == NULL ||
Elliott Hughes965a4b52017-05-15 10:37:39 -0700372 bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
373 callback(user, &pkth, pktd);
JP Abgrall511eca32014-02-12 13:46:45 -0800374 return 1;
375 }
376 return 0; /* didn't pass filter */
377}
378
379static int
Haibo Huang165065a2018-07-23 17:26:52 -0700380bt_inject_linux(pcap_t *handle, const void *buf _U_, size_t size _U_)
JP Abgrall511eca32014-02-12 13:46:45 -0800381{
Elliott Hughes965a4b52017-05-15 10:37:39 -0700382 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
JP Abgrall511eca32014-02-12 13:46:45 -0800383 "bluetooth devices");
384 return (-1);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700385}
JP Abgrall511eca32014-02-12 13:46:45 -0800386
387
Elliott Hughesd8845d72015-10-19 18:07:04 -0700388static int
JP Abgrall511eca32014-02-12 13:46:45 -0800389bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
390{
391 struct pcap_bt *handlep = handle->priv;
392 int ret;
393 struct hci_dev_info dev_info;
394 struct hci_dev_stats * s = &dev_info.stat;
395 dev_info.dev_id = handlep->dev_id;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700396
JP Abgrall511eca32014-02-12 13:46:45 -0800397 /* ignore eintr */
398 do {
399 ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
400 } while ((ret == -1) && (errno == EINTR));
Elliott Hughesd8845d72015-10-19 18:07:04 -0700401
JP Abgrall511eca32014-02-12 13:46:45 -0800402 if (ret < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700403 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
404 errno, "Can't get stats via ioctl");
JP Abgrall511eca32014-02-12 13:46:45 -0800405 return (-1);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700406
JP Abgrall511eca32014-02-12 13:46:45 -0800407 }
408
Elliott Hughesd8845d72015-10-19 18:07:04 -0700409 /* we receive both rx and tx frames, so comulate all stats */
410 stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
JP Abgrall511eca32014-02-12 13:46:45 -0800411 s->acl_tx +s->sco_tx;
412 stats->ps_drop = s->err_rx + s->err_tx;
413 stats->ps_ifdrop = 0;
414 return 0;
415}
416
Elliott Hughesd8845d72015-10-19 18:07:04 -0700417static int
JP Abgrall511eca32014-02-12 13:46:45 -0800418bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
419{
420 p->direction = d;
421 return 0;
422}