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 #ifndef lint 35 static const char rcsid[] _U_ = 36 "@(#) $Header: /tcpdump/master/libpcap/pcap-bt-linux.c,v 1.15 2008-07-01 07:05:54 guy Exp $ (LBL)"; 37 #endif 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 #ifdef NEED_STRERROR_H 48 #include "strerror.h" 49 #endif 50 51 #include <errno.h> 52 #include <stdlib.h> 53 #include <unistd.h> 54 #include <fcntl.h> 55 #include <string.h> 56 #include <sys/ioctl.h> 57 #include <sys/socket.h> 58 #include <arpa/inet.h> 59 60 #include <bluetooth/bluetooth.h> 61 #include <bluetooth/hci.h> 62 63 #define BT_IFACE "bluetooth" 64 #define BT_CTRL_SIZE 128 65 66 /* forward declaration */ 67 static int bt_activate(pcap_t *); 68 static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *); 69 static int bt_inject_linux(pcap_t *, const void *, size_t); 70 static int bt_setdirection_linux(pcap_t *, pcap_direction_t); 71 static int bt_stats_linux(pcap_t *, struct pcap_stat *); 72 73 int 74 bt_findalldevs(pcap_if_t **alldevsp, char *err_str) 75 { 76 pcap_if_t *found_dev = *alldevsp; 77 struct hci_dev_list_req *dev_list; 78 struct hci_dev_req *dev_req; 79 int i, sock; 80 int ret = 0; 81 82 sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); 83 if (sock < 0) 84 { 85 /* if bluetooth is not supported this this is not fatal*/ 86 if (errno == EAFNOSUPPORT) 87 return 0; 88 snprintf(err_str, PCAP_ERRBUF_SIZE, 89 "Can't open raw Bluetooth socket: %s", strerror(errno)); 90 return -1; 91 } 92 93 dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list)); 94 if (!dev_list) 95 { 96 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %zu bytes for Bluetooth device list", 97 HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list)); 98 ret = -1; 99 goto done; 100 } 101 102 dev_list->dev_num = HCI_MAX_DEV; 103 104 if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0) 105 { 106 snprintf(err_str, PCAP_ERRBUF_SIZE, 107 "Can't get Bluetooth device list via ioctl: %s", 108 strerror(errno)); 109 ret = -1; 110 goto free; 111 } 112 113 dev_req = dev_list->dev_req; 114 for (i = 0; i < dev_list->dev_num; i++, dev_req++) { 115 char dev_name[20], dev_descr[30]; 116 117 snprintf(dev_name, 20, BT_IFACE"%d", dev_req->dev_id); 118 snprintf(dev_descr, 30, "Bluetooth adapter number %d", i); 119 120 if (pcap_add_if(&found_dev, dev_name, 0, 121 dev_descr, err_str) < 0) 122 { 123 ret = -1; 124 break; 125 } 126 127 } 128 129 free: 130 free(dev_list); 131 132 done: 133 close(sock); 134 return ret; 135 } 136 137 pcap_t * 138 bt_create(const char *device, char *ebuf, int *is_ours) 139 { 140 const char *cp; 141 char *cpend; 142 long devnum; 143 pcap_t *p; 144 145 /* Does this look like a Bluetooth device? */ 146 cp = strrchr(device, '/'); 147 if (cp == NULL) 148 cp = device; 149 /* Does it begin with BT_IFACE? */ 150 if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) { 151 /* Nope, doesn't begin with BT_IFACE */ 152 *is_ours = 0; 153 return NULL; 154 } 155 /* Yes - is BT_IFACE followed by a number? */ 156 cp += sizeof BT_IFACE - 1; 157 devnum = strtol(cp, &cpend, 10); 158 if (cpend == cp || *cpend != '\0') { 159 /* Not followed by a number. */ 160 *is_ours = 0; 161 return NULL; 162 } 163 if (devnum < 0) { 164 /* Followed by a non-valid number. */ 165 *is_ours = 0; 166 return NULL; 167 } 168 169 /* OK, it's probably ours. */ 170 *is_ours = 1; 171 172 p = pcap_create_common(device, ebuf); 173 if (p == NULL) 174 return (NULL); 175 176 p->activate_op = bt_activate; 177 return (p); 178 } 179 180 static int 181 bt_activate(pcap_t* handle) 182 { 183 struct sockaddr_hci addr; 184 int opt; 185 int dev_id; 186 struct hci_filter flt; 187 int err = PCAP_ERROR; 188 189 /* get bt interface id */ 190 if (sscanf(handle->opt.source, BT_IFACE"%d", &dev_id) != 1) 191 { 192 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 193 "Can't get Bluetooth device index from %s", 194 handle->opt.source); 195 return PCAP_ERROR; 196 } 197 198 /* Initialize some components of the pcap structure. */ 199 handle->bufsize = handle->snapshot+BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header); 200 handle->offset = BT_CTRL_SIZE; 201 handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR; 202 203 handle->read_op = bt_read_linux; 204 handle->inject_op = bt_inject_linux; 205 handle->setfilter_op = install_bpf_program; /* no kernel filtering */ 206 handle->setdirection_op = bt_setdirection_linux; 207 handle->set_datalink_op = NULL; /* can't change data link type */ 208 handle->getnonblock_op = pcap_getnonblock_fd; 209 handle->setnonblock_op = pcap_setnonblock_fd; 210 handle->stats_op = bt_stats_linux; 211 handle->md.ifindex = dev_id; 212 213 /* Create HCI socket */ 214 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); 215 if (handle->fd < 0) { 216 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 217 "Can't create raw socket: %s", strerror(errno)); 218 return PCAP_ERROR; 219 } 220 221 handle->buffer = malloc(handle->bufsize); 222 if (!handle->buffer) { 223 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s", 224 pcap_strerror(errno)); 225 goto close_fail; 226 } 227 228 opt = 1; 229 if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) { 230 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 231 "Can't enable data direction info: %s", strerror(errno)); 232 goto close_fail; 233 } 234 235 opt = 1; 236 if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) { 237 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 238 "Can't enable time stamp: %s", strerror(errno)); 239 goto close_fail; 240 } 241 242 /* Setup filter, do not call hci function to avoid dependence on 243 * external libs */ 244 memset(&flt, 0, sizeof(flt)); 245 memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask)); 246 memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask)); 247 if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { 248 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 249 "Can't set filter: %s", strerror(errno)); 250 goto close_fail; 251 } 252 253 254 /* Bind socket to the HCI device */ 255 addr.hci_family = AF_BLUETOOTH; 256 addr.hci_dev = handle->md.ifindex; 257 #ifdef SOCKADDR_HCI_HAS_HCI_CHANNEL 258 addr.hci_channel = HCI_CHANNEL_RAW; 259 #endif 260 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 261 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 262 "Can't attach to device %d: %s", handle->md.ifindex, 263 strerror(errno)); 264 goto close_fail; 265 } 266 267 if (handle->opt.rfmon) { 268 /* 269 * Monitor mode doesn't apply to Bluetooth devices. 270 */ 271 err = PCAP_ERROR_RFMON_NOTSUP; 272 goto close_fail; 273 } 274 275 if (handle->opt.buffer_size != 0) { 276 /* 277 * Set the socket buffer size to the specified value. 278 */ 279 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, 280 &handle->opt.buffer_size, 281 sizeof(handle->opt.buffer_size)) == -1) { 282 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 283 "SO_RCVBUF: %s", pcap_strerror(errno)); 284 goto close_fail; 285 } 286 } 287 288 handle->selectable_fd = handle->fd; 289 return 0; 290 291 close_fail: 292 pcap_cleanup_live_common(handle); 293 return err; 294 } 295 296 static int 297 bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) 298 { 299 struct cmsghdr *cmsg; 300 struct msghdr msg; 301 struct iovec iv; 302 ssize_t ret; 303 struct pcap_pkthdr pkth; 304 pcap_bluetooth_h4_header* bthdr; 305 306 bthdr = (pcap_bluetooth_h4_header*) &handle->buffer[handle->offset]; 307 iv.iov_base = &handle->buffer[handle->offset+sizeof(pcap_bluetooth_h4_header)]; 308 iv.iov_len = handle->snapshot; 309 310 memset(&msg, 0, sizeof(msg)); 311 msg.msg_iov = &iv; 312 msg.msg_iovlen = 1; 313 msg.msg_control = handle->buffer; 314 msg.msg_controllen = handle->offset; 315 316 /* ignore interrupt system call error */ 317 do { 318 ret = recvmsg(handle->fd, &msg, 0); 319 if (handle->break_loop) 320 { 321 handle->break_loop = 0; 322 return -2; 323 } 324 } while ((ret == -1) && (errno == EINTR)); 325 326 if (ret < 0) { 327 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 328 "Can't receive packet: %s", strerror(errno)); 329 return -1; 330 } 331 332 pkth.caplen = ret; 333 334 /* get direction and timestamp*/ 335 cmsg = CMSG_FIRSTHDR(&msg); 336 int in=0; 337 while (cmsg) { 338 switch (cmsg->cmsg_type) { 339 case HCI_CMSG_DIR: 340 memcpy(&in, CMSG_DATA(cmsg), sizeof in); 341 break; 342 case HCI_CMSG_TSTAMP: 343 memcpy(&pkth.ts, CMSG_DATA(cmsg), 344 sizeof pkth.ts); 345 break; 346 } 347 cmsg = CMSG_NXTHDR(&msg, cmsg); 348 } 349 if ((in && (handle->direction == PCAP_D_OUT)) || 350 ((!in) && (handle->direction == PCAP_D_IN))) 351 return 0; 352 353 bthdr->direction = htonl(in != 0); 354 pkth.caplen+=sizeof(pcap_bluetooth_h4_header); 355 pkth.len = pkth.caplen; 356 if (handle->fcode.bf_insns == NULL || 357 bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset], 358 pkth.len, pkth.caplen)) { 359 callback(user, &pkth, &handle->buffer[handle->offset]); 360 return 1; 361 } 362 return 0; /* didn't pass filter */ 363 } 364 365 static int 366 bt_inject_linux(pcap_t *handle, const void *buf, size_t size) 367 { 368 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on " 369 "bluetooth devices"); 370 return (-1); 371 } 372 373 374 static int 375 bt_stats_linux(pcap_t *handle, struct pcap_stat *stats) 376 { 377 int ret; 378 struct hci_dev_info dev_info; 379 struct hci_dev_stats * s = &dev_info.stat; 380 dev_info.dev_id = handle->md.ifindex; 381 382 /* ignore eintr */ 383 do { 384 ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info); 385 } while ((ret == -1) && (errno == EINTR)); 386 387 if (ret < 0) { 388 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 389 "Can't get stats via ioctl: %s", strerror(errno)); 390 return (-1); 391 392 } 393 394 /* we receive both rx and tx frames, so comulate all stats */ 395 stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx + 396 s->acl_tx +s->sco_tx; 397 stats->ps_drop = s->err_rx + s->err_tx; 398 stats->ps_ifdrop = 0; 399 return 0; 400 } 401 402 static int 403 bt_setdirection_linux(pcap_t *p, pcap_direction_t d) 404 { 405 p->direction = d; 406 return 0; 407 } 408