1 /* $NetBSD: pcap-bt-linux.c,v 1.6 2019/10/01 16:02:12 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Paolo Abeni (Italy) 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote 17 * products derived from this software without specific prior written 18 * permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 * Bluetooth sniffing API implementation for Linux platform 33 * By Paolo Abeni <paolo.abeni@email.it> 34 * 35 */ 36 #include <sys/cdefs.h> 37 __RCSID("$NetBSD: pcap-bt-linux.c,v 1.6 2019/10/01 16:02:12 christos Exp $"); 38 39 #ifdef HAVE_CONFIG_H 40 #include <config.h> 41 #endif 42 43 #include "pcap-int.h" 44 #include "pcap-bt-linux.h" 45 #include "pcap/bluetooth.h" 46 47 #include <errno.h> 48 #include <stdlib.h> 49 #include <unistd.h> 50 #include <fcntl.h> 51 #include <string.h> 52 #include <sys/ioctl.h> 53 #include <sys/socket.h> 54 #include <arpa/inet.h> 55 56 #include <bluetooth/bluetooth.h> 57 #include <bluetooth/hci.h> 58 59 #define BT_IFACE "bluetooth" 60 #define BT_CTRL_SIZE 128 61 62 /* forward declaration */ 63 static int bt_activate(pcap_t *); 64 static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *); 65 static int bt_inject_linux(pcap_t *, const void *, size_t); 66 static int bt_setdirection_linux(pcap_t *, pcap_direction_t); 67 static int bt_stats_linux(pcap_t *, struct pcap_stat *); 68 69 /* 70 * Private data for capturing on Linux Bluetooth devices. 71 */ 72 struct pcap_bt { 73 int dev_id; /* device ID of device we're bound to */ 74 }; 75 76 int 77 bt_findalldevs(pcap_if_list_t *devlistp, char *err_str) 78 { 79 struct hci_dev_list_req *dev_list; 80 struct hci_dev_req *dev_req; 81 int sock; 82 unsigned i; 83 int ret = 0; 84 85 sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); 86 if (sock < 0) 87 { 88 /* if bluetooth is not supported this is not fatal*/ 89 if (errno == EAFNOSUPPORT) 90 return 0; 91 pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE, 92 errno, "Can't open raw Bluetooth socket"); 93 return -1; 94 } 95 96 dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list)); 97 if (!dev_list) 98 { 99 pcap_snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %zu bytes for Bluetooth device list", 100 HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list)); 101 ret = -1; 102 goto done; 103 } 104 105 dev_list->dev_num = HCI_MAX_DEV; 106 107 if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0) 108 { 109 pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE, 110 errno, "Can't get Bluetooth device list via ioctl"); 111 ret = -1; 112 goto free; 113 } 114 115 dev_req = dev_list->dev_req; 116 for (i = 0; i < dev_list->dev_num; i++, dev_req++) { 117 char dev_name[20], dev_descr[40]; 118 119 pcap_snprintf(dev_name, sizeof(dev_name), BT_IFACE"%u", dev_req->dev_id); 120 pcap_snprintf(dev_descr, sizeof(dev_descr), "Bluetooth adapter number %u", i); 121 122 /* 123 * Bluetooth is a wireless technology. 124 * XXX - if there's the notion of associating with a 125 * network, and we can determine whether the interface 126 * is associated with a network, check that and set 127 * the status to PCAP_IF_CONNECTION_STATUS_CONNECTED 128 * or PCAP_IF_CONNECTION_STATUS_DISCONNECTED. 129 */ 130 if (add_dev(devlistp, dev_name, PCAP_IF_WIRELESS, dev_descr, err_str) == NULL) 131 { 132 ret = -1; 133 break; 134 } 135 } 136 137 free: 138 free(dev_list); 139 140 done: 141 close(sock); 142 return ret; 143 } 144 145 pcap_t * 146 bt_create(const char *device, char *ebuf, int *is_ours) 147 { 148 const char *cp; 149 char *cpend; 150 long devnum; 151 pcap_t *p; 152 153 /* Does this look like a Bluetooth device? */ 154 cp = strrchr(device, '/'); 155 if (cp == NULL) 156 cp = device; 157 /* Does it begin with BT_IFACE? */ 158 if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) { 159 /* Nope, doesn't begin with BT_IFACE */ 160 *is_ours = 0; 161 return NULL; 162 } 163 /* Yes - is BT_IFACE followed by a number? */ 164 cp += sizeof BT_IFACE - 1; 165 devnum = strtol(cp, &cpend, 10); 166 if (cpend == cp || *cpend != '\0') { 167 /* Not followed by a number. */ 168 *is_ours = 0; 169 return NULL; 170 } 171 if (devnum < 0) { 172 /* Followed by a non-valid number. */ 173 *is_ours = 0; 174 return NULL; 175 } 176 177 /* OK, it's probably ours. */ 178 *is_ours = 1; 179 180 p = pcap_create_common(ebuf, sizeof (struct pcap_bt)); 181 if (p == NULL) 182 return (NULL); 183 184 p->activate_op = bt_activate; 185 return (p); 186 } 187 188 static int 189 bt_activate(pcap_t* handle) 190 { 191 struct pcap_bt *handlep = handle->priv; 192 struct sockaddr_hci addr; 193 int opt; 194 int dev_id; 195 struct hci_filter flt; 196 int err = PCAP_ERROR; 197 198 /* get bt interface id */ 199 if (sscanf(handle->opt.device, BT_IFACE"%d", &dev_id) != 1) 200 { 201 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 202 "Can't get Bluetooth device index from %s", 203 handle->opt.device); 204 return PCAP_ERROR; 205 } 206 207 /* 208 * Turn a negative snapshot value (invalid), a snapshot value of 209 * 0 (unspecified), or a value bigger than the normal maximum 210 * value, into the maximum allowed value. 211 * 212 * If some application really *needs* a bigger snapshot 213 * length, we should just increase MAXIMUM_SNAPLEN. 214 */ 215 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN) 216 handle->snapshot = MAXIMUM_SNAPLEN; 217 218 /* Initialize some components of the pcap structure. */ 219 handle->bufsize = BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header)+handle->snapshot; 220 handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR; 221 222 handle->read_op = bt_read_linux; 223 handle->inject_op = bt_inject_linux; 224 handle->setfilter_op = install_bpf_program; /* no kernel filtering */ 225 handle->setdirection_op = bt_setdirection_linux; 226 handle->set_datalink_op = NULL; /* can't change data link type */ 227 handle->getnonblock_op = pcap_getnonblock_fd; 228 handle->setnonblock_op = pcap_setnonblock_fd; 229 handle->stats_op = bt_stats_linux; 230 handlep->dev_id = dev_id; 231 232 /* Create HCI socket */ 233 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); 234 if (handle->fd < 0) { 235 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 236 errno, "Can't create raw socket"); 237 return PCAP_ERROR; 238 } 239 240 handle->buffer = malloc(handle->bufsize); 241 if (!handle->buffer) { 242 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 243 errno, "Can't allocate dump buffer"); 244 goto close_fail; 245 } 246 247 opt = 1; 248 if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) { 249 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 250 errno, "Can't enable data direction info"); 251 goto close_fail; 252 } 253 254 opt = 1; 255 if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) { 256 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 257 errno, "Can't enable time stamp"); 258 goto close_fail; 259 } 260 261 /* Setup filter, do not call hci function to avoid dependence on 262 * external libs */ 263 memset(&flt, 0, sizeof(flt)); 264 memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask)); 265 memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask)); 266 if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { 267 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 268 errno, "Can't set filter"); 269 goto close_fail; 270 } 271 272 273 /* Bind socket to the HCI device */ 274 addr.hci_family = AF_BLUETOOTH; 275 addr.hci_dev = handlep->dev_id; 276 #ifdef HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL 277 addr.hci_channel = HCI_CHANNEL_RAW; 278 #endif 279 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 280 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 281 errno, "Can't attach to device %d", handlep->dev_id); 282 goto close_fail; 283 } 284 285 if (handle->opt.rfmon) { 286 /* 287 * Monitor mode doesn't apply to Bluetooth devices. 288 */ 289 err = PCAP_ERROR_RFMON_NOTSUP; 290 goto close_fail; 291 } 292 293 if (handle->opt.buffer_size != 0) { 294 /* 295 * Set the socket buffer size to the specified value. 296 */ 297 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, 298 &handle->opt.buffer_size, 299 sizeof(handle->opt.buffer_size)) == -1) { 300 pcap_fmt_errmsg_for_errno(handle->errbuf, 301 errno, PCAP_ERRBUF_SIZE, "SO_RCVBUF"); 302 goto close_fail; 303 } 304 } 305 306 handle->selectable_fd = handle->fd; 307 return 0; 308 309 close_fail: 310 pcap_cleanup_live_common(handle); 311 return err; 312 } 313 314 static int 315 bt_read_linux(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user) 316 { 317 struct cmsghdr *cmsg; 318 struct msghdr msg; 319 struct iovec iv; 320 ssize_t ret; 321 struct pcap_pkthdr pkth; 322 pcap_bluetooth_h4_header* bthdr; 323 u_char *pktd; 324 int in = 0; 325 326 pktd = (u_char *)handle->buffer + BT_CTRL_SIZE; 327 bthdr = (pcap_bluetooth_h4_header*)(void *)pktd; 328 iv.iov_base = pktd + sizeof(pcap_bluetooth_h4_header); 329 iv.iov_len = handle->snapshot; 330 331 memset(&msg, 0, sizeof(msg)); 332 msg.msg_iov = &iv; 333 msg.msg_iovlen = 1; 334 msg.msg_control = handle->buffer; 335 msg.msg_controllen = BT_CTRL_SIZE; 336 337 /* ignore interrupt system call error */ 338 do { 339 ret = recvmsg(handle->fd, &msg, 0); 340 if (handle->break_loop) 341 { 342 handle->break_loop = 0; 343 return -2; 344 } 345 } while ((ret == -1) && (errno == EINTR)); 346 347 if (ret < 0) { 348 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 349 errno, "Can't receive packet"); 350 return -1; 351 } 352 353 pkth.caplen = ret; 354 355 /* get direction and timestamp*/ 356 cmsg = CMSG_FIRSTHDR(&msg); 357 while (cmsg) { 358 switch (cmsg->cmsg_type) { 359 case HCI_CMSG_DIR: 360 memcpy(&in, CMSG_DATA(cmsg), sizeof in); 361 break; 362 case HCI_CMSG_TSTAMP: 363 memcpy(&pkth.ts, CMSG_DATA(cmsg), 364 sizeof pkth.ts); 365 break; 366 } 367 cmsg = CMSG_NXTHDR(&msg, cmsg); 368 } 369 if ((in && (handle->direction == PCAP_D_OUT)) || 370 ((!in) && (handle->direction == PCAP_D_IN))) 371 return 0; 372 373 bthdr->direction = htonl(in != 0); 374 pkth.caplen+=sizeof(pcap_bluetooth_h4_header); 375 pkth.len = pkth.caplen; 376 if (handle->fcode.bf_insns == NULL || 377 bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) { 378 callback(user, &pkth, pktd); 379 return 1; 380 } 381 return 0; /* didn't pass filter */ 382 } 383 384 static int 385 bt_inject_linux(pcap_t *handle, const void *buf _U_, size_t size _U_) 386 { 387 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 388 "Packet injection is not supported on Bluetooth devices"); 389 return (-1); 390 } 391 392 393 static int 394 bt_stats_linux(pcap_t *handle, struct pcap_stat *stats) 395 { 396 struct pcap_bt *handlep = handle->priv; 397 int ret; 398 struct hci_dev_info dev_info; 399 struct hci_dev_stats * s = &dev_info.stat; 400 dev_info.dev_id = handlep->dev_id; 401 402 /* ignore eintr */ 403 do { 404 ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info); 405 } while ((ret == -1) && (errno == EINTR)); 406 407 if (ret < 0) { 408 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 409 errno, "Can't get stats via ioctl"); 410 return (-1); 411 412 } 413 414 /* we receive both rx and tx frames, so comulate all stats */ 415 stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx + 416 s->acl_tx +s->sco_tx; 417 stats->ps_drop = s->err_rx + s->err_tx; 418 stats->ps_ifdrop = 0; 419 return 0; 420 } 421 422 static int 423 bt_setdirection_linux(pcap_t *p, pcap_direction_t d) 424 { 425 p->direction = d; 426 return 0; 427 } 428