]> The Tcpdump Group git mirrors - libpcap/blob - pcap-bt-linux.c
CI: Call print_so_deps() on rpcapd in remote enabled build
[libpcap] / pcap-bt-linux.c
1 /*
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.
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
16 * 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 */
34
35 #include <config.h>
36
37 #include "pcap-int.h"
38 #include "pcap-bt-linux.h"
39 #include "pcap/bluetooth.h"
40 #include "diag-control.h"
41
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <string.h>
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
49 #include <arpa/inet.h>
50
51 #include <bluetooth/bluetooth.h>
52 #include <bluetooth/hci.h>
53
54 #define BT_IFACE "bluetooth"
55 #define BT_CTRL_SIZE 128
56
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 *);
63
64 /*
65 * Private data for capturing on Linux Bluetooth devices.
66 */
67 struct pcap_bt {
68 int dev_id; /* device ID of device we're bound to */
69 };
70
71 int
72 bt_findalldevs(pcap_if_list_t *devlistp, char *err_str)
73 {
74 struct hci_dev_list_req *dev_list;
75 struct hci_dev_req *dev_req;
76 int sock;
77 unsigned i;
78 int ret = 0;
79
80 sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
81 if (sock < 0)
82 {
83 /* if bluetooth is not supported this is not fatal*/
84 if (errno == EAFNOSUPPORT)
85 return 0;
86 pcapint_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
87 errno, "Can't open raw Bluetooth socket");
88 return PCAP_ERROR;
89 }
90
91 dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
92 if (!dev_list)
93 {
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));
96 ret = PCAP_ERROR;
97 goto done;
98 }
99
100 /*
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
103 * has been set.
104 * https://github.com/the-tcpdump-group/libpcap/issues/1083
105 * https://bugs.kde.org/show_bug.cgi?id=448464
106 */
107 memset(dev_list, 0, sizeof(*dev_list));
108 dev_list->dev_num = HCI_MAX_DEV;
109
110 if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
111 {
112 pcapint_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
113 errno, "Can't get Bluetooth device list via ioctl");
114 ret = PCAP_ERROR;
115 goto free;
116 }
117
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];
121
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);
124
125 /*
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.
132 */
133 if (pcapint_add_dev(devlistp, dev_name, PCAP_IF_WIRELESS, dev_descr, err_str) == NULL)
134 {
135 ret = PCAP_ERROR;
136 break;
137 }
138 }
139
140 free:
141 free(dev_list);
142
143 done:
144 close(sock);
145 return ret;
146 }
147
148 pcap_t *
149 bt_create(const char *device, char *ebuf, int *is_ours)
150 {
151 const char *cp;
152 char *cpend;
153 long devnum;
154 pcap_t *p;
155
156 /* Does this look like a Bluetooth device? */
157 cp = 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 */
161 *is_ours = 0;
162 return NULL;
163 }
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. */
169 *is_ours = 0;
170 return NULL;
171 }
172 if (devnum < 0) {
173 /* Followed by a non-valid number. */
174 *is_ours = 0;
175 return NULL;
176 }
177
178 /* OK, it's probably ours. */
179 *is_ours = 1;
180
181 p = PCAP_CREATE_COMMON(ebuf, struct pcap_bt);
182 if (p == NULL)
183 return (NULL);
184
185 p->activate_op = bt_activate;
186 return (p);
187 }
188
189 static int
190 bt_activate(pcap_t* handle)
191 {
192 struct pcap_bt *handlep = handle->priv;
193 struct sockaddr_hci addr;
194 int opt;
195 int dev_id;
196 struct hci_filter flt;
197 int err = PCAP_ERROR;
198
199 /* get bt interface id */
200 if (sscanf(handle->opt.device, BT_IFACE"%d", &dev_id) != 1)
201 {
202 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
203 "Can't get Bluetooth device index from %s",
204 handle->opt.device);
205 return PCAP_ERROR;
206 }
207
208 /*
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.
212 *
213 * If some application really *needs* a bigger snapshot
214 * length, we should just increase MAXIMUM_SNAPLEN.
215 */
216 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
217 handle->snapshot = MAXIMUM_SNAPLEN;
218
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;
222
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;
232
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");
238 return PCAP_ERROR;
239 }
240
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");
245 goto close_fail;
246 }
247
248 opt = 1;
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");
252 goto close_fail;
253 }
254
255 opt = 1;
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");
259 goto close_fail;
260 }
261
262 /* Setup filter, do not call hci function to avoid dependence on
263 * external libs */
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");
270 goto close_fail;
271 }
272
273
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;
279 #endif
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);
283 goto close_fail;
284 }
285
286 if (handle->opt.rfmon) {
287 /*
288 * Monitor mode doesn't apply to Bluetooth devices.
289 */
290 err = PCAP_ERROR_RFMON_NOTSUP;
291 goto close_fail;
292 }
293
294 if (handle->opt.buffer_size != 0) {
295 /*
296 * Set the socket buffer size to the specified value.
297 */
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");
303 goto close_fail;
304 }
305 }
306
307 handle->selectable_fd = handle->fd;
308 return 0;
309
310 close_fail:
311 pcapint_cleanup_live_common(handle);
312 return err;
313 }
314
315 static int
316 bt_read_linux(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
317 {
318 struct cmsghdr *cmsg;
319 struct msghdr msg;
320 struct iovec iv;
321 ssize_t ret;
322 struct pcap_pkthdr pkth;
323 pcap_bluetooth_h4_header* bthdr;
324 u_char *pktd;
325 int in = 0;
326
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;
331
332 memset(&msg, 0, sizeof(msg));
333 msg.msg_iov = &iv;
334 msg.msg_iovlen = 1;
335 msg.msg_control = handle->buffer;
336 msg.msg_controllen = BT_CTRL_SIZE;
337
338 /* ignore interrupt system call error */
339 do {
340 if (handle->break_loop)
341 {
342 handle->break_loop = 0;
343 return PCAP_ERROR_BREAK;
344 }
345 ret = recvmsg(handle->fd, &msg, 0);
346 } while ((ret == -1) && (errno == EINTR));
347
348 if (ret < 0) {
349 if (errno == EAGAIN || errno == EWOULDBLOCK) {
350 /* Nonblocking mode, no data */
351 return 0;
352 }
353 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
354 errno, "Can't receive packet");
355 return PCAP_ERROR;
356 }
357
358 pkth.caplen = (bpf_u_int32)ret;
359
360 /* get direction and timestamp*/
361 cmsg = CMSG_FIRSTHDR(&msg);
362 while (cmsg) {
363 switch (cmsg->cmsg_type) {
364 case HCI_CMSG_DIR:
365 memcpy(&in, CMSG_DATA(cmsg), sizeof in);
366 break;
367 case HCI_CMSG_TSTAMP:
368 memcpy(&pkth.ts, CMSG_DATA(cmsg),
369 sizeof pkth.ts);
370 break;
371 }
372 // for musl libc CMSG_NXTHDR()
373 DIAG_OFF_SIGN_COMPARE
374 cmsg = CMSG_NXTHDR(&msg, cmsg);
375 DIAG_ON_SIGN_COMPARE
376 }
377 switch (handle->direction) {
378
379 case PCAP_D_IN:
380 if (!in)
381 return 0;
382 break;
383
384 case PCAP_D_OUT:
385 if (in)
386 return 0;
387 break;
388
389 default:
390 break;
391 }
392
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);
399 return 1;
400 }
401 return 0; /* didn't pass filter */
402 }
403
404 static int
405 bt_inject_linux(pcap_t *handle, const void *buf _U_, int size _U_)
406 {
407 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
408 "Packet injection is not supported on Bluetooth devices");
409 return (-1);
410 }
411
412
413 static int
414 bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
415 {
416 struct pcap_bt *handlep = handle->priv;
417 int ret;
418 struct hci_dev_info dev_info;
419 struct hci_dev_stats * s = &dev_info.stat;
420 dev_info.dev_id = handlep->dev_id;
421
422 /* ignore eintr */
423 do {
424 ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
425 } while ((ret == -1) && (errno == EINTR));
426
427 if (ret < 0) {
428 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
429 errno, "Can't get stats via ioctl");
430 return (-1);
431
432 }
433
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;
439 return 0;
440 }
441
442 static int
443 bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
444 {
445 /*
446 * It's guaranteed, at this point, that d is a valid
447 * direction value.
448 */
449 p->direction = d;
450 return 0;
451 }