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