JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 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. |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 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 |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 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 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 34 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 35 | #ifdef HAVE_CONFIG_H |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 36 | #include <config.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | #include "pcap-int.h" |
| 40 | #include "pcap-bt-linux.h" |
| 41 | #include "pcap/bluetooth.h" |
| 42 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 43 | #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 */ |
| 59 | static int bt_activate(pcap_t *); |
| 60 | static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 61 | static int bt_inject_linux(pcap_t *, const void *, int); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 62 | static int bt_setdirection_linux(pcap_t *, pcap_direction_t); |
| 63 | static int bt_stats_linux(pcap_t *, struct pcap_stat *); |
| 64 | |
| 65 | /* |
| 66 | * Private data for capturing on Linux Bluetooth devices. |
| 67 | */ |
| 68 | struct pcap_bt { |
| 69 | int dev_id; /* device ID of device we're bound to */ |
| 70 | }; |
| 71 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 72 | int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 73 | bt_findalldevs(pcap_if_list_t *devlistp, char *err_str) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 74 | { |
| 75 | struct hci_dev_list_req *dev_list; |
| 76 | struct hci_dev_req *dev_req; |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 77 | int sock; |
| 78 | unsigned i; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 79 | int ret = 0; |
| 80 | |
| 81 | sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); |
| 82 | if (sock < 0) |
| 83 | { |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 84 | /* if bluetooth is not supported this is not fatal*/ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 85 | if (errno == EAFNOSUPPORT) |
| 86 | return 0; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 87 | pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE, |
| 88 | errno, "Can't open raw Bluetooth socket"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list)); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 93 | if (!dev_list) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 94 | { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 95 | snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %zu bytes for Bluetooth device list", |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 96 | HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list)); |
| 97 | ret = -1; |
| 98 | goto done; |
| 99 | } |
| 100 | |
| 101 | dev_list->dev_num = HCI_MAX_DEV; |
| 102 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 103 | if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 104 | { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 105 | pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE, |
| 106 | errno, "Can't get Bluetooth device list via ioctl"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 107 | ret = -1; |
| 108 | goto free; |
| 109 | } |
| 110 | |
| 111 | dev_req = dev_list->dev_req; |
| 112 | for (i = 0; i < dev_list->dev_num; i++, dev_req++) { |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 113 | char dev_name[20], dev_descr[40]; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 114 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 115 | snprintf(dev_name, sizeof(dev_name), BT_IFACE"%u", dev_req->dev_id); |
| 116 | snprintf(dev_descr, sizeof(dev_descr), "Bluetooth adapter number %u", i); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 117 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 118 | /* |
| 119 | * Bluetooth is a wireless technology. |
| 120 | * XXX - if there's the notion of associating with a |
| 121 | * network, and we can determine whether the interface |
| 122 | * is associated with a network, check that and set |
| 123 | * the status to PCAP_IF_CONNECTION_STATUS_CONNECTED |
| 124 | * or PCAP_IF_CONNECTION_STATUS_DISCONNECTED. |
| 125 | */ |
| 126 | if (add_dev(devlistp, dev_name, PCAP_IF_WIRELESS, dev_descr, err_str) == NULL) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 127 | { |
| 128 | ret = -1; |
| 129 | break; |
| 130 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | free: |
| 134 | free(dev_list); |
| 135 | |
| 136 | done: |
| 137 | close(sock); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | pcap_t * |
| 142 | bt_create(const char *device, char *ebuf, int *is_ours) |
| 143 | { |
| 144 | const char *cp; |
| 145 | char *cpend; |
| 146 | long devnum; |
| 147 | pcap_t *p; |
| 148 | |
| 149 | /* Does this look like a Bluetooth device? */ |
| 150 | cp = strrchr(device, '/'); |
| 151 | if (cp == NULL) |
| 152 | cp = device; |
| 153 | /* Does it begin with BT_IFACE? */ |
| 154 | if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) { |
| 155 | /* Nope, doesn't begin with BT_IFACE */ |
| 156 | *is_ours = 0; |
| 157 | return NULL; |
| 158 | } |
| 159 | /* Yes - is BT_IFACE followed by a number? */ |
| 160 | cp += sizeof BT_IFACE - 1; |
| 161 | devnum = strtol(cp, &cpend, 10); |
| 162 | if (cpend == cp || *cpend != '\0') { |
| 163 | /* Not followed by a number. */ |
| 164 | *is_ours = 0; |
| 165 | return NULL; |
| 166 | } |
| 167 | if (devnum < 0) { |
| 168 | /* Followed by a non-valid number. */ |
| 169 | *is_ours = 0; |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | /* OK, it's probably ours. */ |
| 174 | *is_ours = 1; |
| 175 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 176 | p = PCAP_CREATE_COMMON(ebuf, struct pcap_bt); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 177 | if (p == NULL) |
| 178 | return (NULL); |
| 179 | |
| 180 | p->activate_op = bt_activate; |
| 181 | return (p); |
| 182 | } |
| 183 | |
| 184 | static int |
| 185 | bt_activate(pcap_t* handle) |
| 186 | { |
| 187 | struct pcap_bt *handlep = handle->priv; |
| 188 | struct sockaddr_hci addr; |
| 189 | int opt; |
| 190 | int dev_id; |
| 191 | struct hci_filter flt; |
| 192 | int err = PCAP_ERROR; |
| 193 | |
| 194 | /* get bt interface id */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 195 | if (sscanf(handle->opt.device, BT_IFACE"%d", &dev_id) != 1) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 196 | { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 197 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 198 | "Can't get Bluetooth device index from %s", |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 199 | handle->opt.device); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 200 | return PCAP_ERROR; |
| 201 | } |
| 202 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 203 | /* |
| 204 | * Turn a negative snapshot value (invalid), a snapshot value of |
| 205 | * 0 (unspecified), or a value bigger than the normal maximum |
| 206 | * value, into the maximum allowed value. |
| 207 | * |
| 208 | * If some application really *needs* a bigger snapshot |
| 209 | * length, we should just increase MAXIMUM_SNAPLEN. |
| 210 | */ |
| 211 | if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN) |
| 212 | handle->snapshot = MAXIMUM_SNAPLEN; |
| 213 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 214 | /* Initialize some components of the pcap structure. */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 215 | handle->bufsize = BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header)+handle->snapshot; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 216 | handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR; |
| 217 | |
| 218 | handle->read_op = bt_read_linux; |
| 219 | handle->inject_op = bt_inject_linux; |
| 220 | handle->setfilter_op = install_bpf_program; /* no kernel filtering */ |
| 221 | handle->setdirection_op = bt_setdirection_linux; |
| 222 | handle->set_datalink_op = NULL; /* can't change data link type */ |
| 223 | handle->getnonblock_op = pcap_getnonblock_fd; |
| 224 | handle->setnonblock_op = pcap_setnonblock_fd; |
| 225 | handle->stats_op = bt_stats_linux; |
| 226 | handlep->dev_id = dev_id; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 227 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 228 | /* Create HCI socket */ |
| 229 | handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); |
| 230 | if (handle->fd < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 231 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 232 | errno, "Can't create raw socket"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 233 | return PCAP_ERROR; |
| 234 | } |
| 235 | |
| 236 | handle->buffer = malloc(handle->bufsize); |
| 237 | if (!handle->buffer) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 238 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 239 | errno, "Can't allocate dump buffer"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 240 | goto close_fail; |
| 241 | } |
| 242 | |
| 243 | opt = 1; |
| 244 | if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 245 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 246 | errno, "Can't enable data direction info"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 247 | goto close_fail; |
| 248 | } |
| 249 | |
| 250 | opt = 1; |
| 251 | if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 252 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 253 | errno, "Can't enable time stamp"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 254 | goto close_fail; |
| 255 | } |
| 256 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 257 | /* Setup filter, do not call hci function to avoid dependence on |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 258 | * external libs */ |
| 259 | memset(&flt, 0, sizeof(flt)); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 260 | memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 261 | memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask)); |
| 262 | if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 263 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 264 | errno, "Can't set filter"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 265 | goto close_fail; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /* Bind socket to the HCI device */ |
| 270 | addr.hci_family = AF_BLUETOOTH; |
| 271 | addr.hci_dev = handlep->dev_id; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 272 | #ifdef HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 273 | addr.hci_channel = HCI_CHANNEL_RAW; |
| 274 | #endif |
| 275 | if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 276 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 277 | errno, "Can't attach to device %d", handlep->dev_id); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 278 | goto close_fail; |
| 279 | } |
| 280 | |
| 281 | if (handle->opt.rfmon) { |
| 282 | /* |
| 283 | * Monitor mode doesn't apply to Bluetooth devices. |
| 284 | */ |
| 285 | err = PCAP_ERROR_RFMON_NOTSUP; |
| 286 | goto close_fail; |
| 287 | } |
| 288 | |
| 289 | if (handle->opt.buffer_size != 0) { |
| 290 | /* |
| 291 | * Set the socket buffer size to the specified value. |
| 292 | */ |
| 293 | if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, |
| 294 | &handle->opt.buffer_size, |
| 295 | sizeof(handle->opt.buffer_size)) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 296 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 297 | errno, PCAP_ERRBUF_SIZE, "SO_RCVBUF"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 298 | goto close_fail; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | handle->selectable_fd = handle->fd; |
| 303 | return 0; |
| 304 | |
| 305 | close_fail: |
| 306 | pcap_cleanup_live_common(handle); |
| 307 | return err; |
| 308 | } |
| 309 | |
| 310 | static int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 311 | bt_read_linux(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 312 | { |
| 313 | struct cmsghdr *cmsg; |
| 314 | struct msghdr msg; |
| 315 | struct iovec iv; |
| 316 | ssize_t ret; |
| 317 | struct pcap_pkthdr pkth; |
| 318 | pcap_bluetooth_h4_header* bthdr; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 319 | u_char *pktd; |
| 320 | int in = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 321 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 322 | pktd = (u_char *)handle->buffer + BT_CTRL_SIZE; |
| 323 | bthdr = (pcap_bluetooth_h4_header*)(void *)pktd; |
| 324 | iv.iov_base = pktd + sizeof(pcap_bluetooth_h4_header); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 325 | iv.iov_len = handle->snapshot; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 326 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 327 | memset(&msg, 0, sizeof(msg)); |
| 328 | msg.msg_iov = &iv; |
| 329 | msg.msg_iovlen = 1; |
| 330 | msg.msg_control = handle->buffer; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 331 | msg.msg_controllen = BT_CTRL_SIZE; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 332 | |
| 333 | /* ignore interrupt system call error */ |
| 334 | do { |
| 335 | ret = recvmsg(handle->fd, &msg, 0); |
| 336 | if (handle->break_loop) |
| 337 | { |
| 338 | handle->break_loop = 0; |
| 339 | return -2; |
| 340 | } |
| 341 | } while ((ret == -1) && (errno == EINTR)); |
| 342 | |
| 343 | if (ret < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 344 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 345 | errno, "Can't receive packet"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 346 | return -1; |
| 347 | } |
| 348 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 349 | pkth.caplen = (bpf_u_int32)ret; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 350 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 351 | /* get direction and timestamp*/ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 352 | cmsg = CMSG_FIRSTHDR(&msg); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 353 | while (cmsg) { |
| 354 | switch (cmsg->cmsg_type) { |
| 355 | case HCI_CMSG_DIR: |
| 356 | memcpy(&in, CMSG_DATA(cmsg), sizeof in); |
| 357 | break; |
| 358 | case HCI_CMSG_TSTAMP: |
| 359 | memcpy(&pkth.ts, CMSG_DATA(cmsg), |
| 360 | sizeof pkth.ts); |
| 361 | break; |
| 362 | } |
| 363 | cmsg = CMSG_NXTHDR(&msg, cmsg); |
| 364 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 365 | switch (handle->direction) { |
| 366 | |
| 367 | case PCAP_D_IN: |
| 368 | if (!in) |
| 369 | return 0; |
| 370 | break; |
| 371 | |
| 372 | case PCAP_D_OUT: |
| 373 | if (in) |
| 374 | return 0; |
| 375 | break; |
| 376 | |
| 377 | default: |
| 378 | break; |
| 379 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 380 | |
| 381 | bthdr->direction = htonl(in != 0); |
| 382 | pkth.caplen+=sizeof(pcap_bluetooth_h4_header); |
| 383 | pkth.len = pkth.caplen; |
| 384 | if (handle->fcode.bf_insns == NULL || |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 385 | pcap_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 386 | callback(user, &pkth, pktd); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 387 | return 1; |
| 388 | } |
| 389 | return 0; /* didn't pass filter */ |
| 390 | } |
| 391 | |
| 392 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 393 | bt_inject_linux(pcap_t *handle, const void *buf _U_, int size _U_) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 394 | { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 395 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 396 | "Packet injection is not supported on Bluetooth devices"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 397 | return (-1); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 398 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 399 | |
| 400 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 401 | static int |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 402 | bt_stats_linux(pcap_t *handle, struct pcap_stat *stats) |
| 403 | { |
| 404 | struct pcap_bt *handlep = handle->priv; |
| 405 | int ret; |
| 406 | struct hci_dev_info dev_info; |
| 407 | struct hci_dev_stats * s = &dev_info.stat; |
| 408 | dev_info.dev_id = handlep->dev_id; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 409 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 410 | /* ignore eintr */ |
| 411 | do { |
| 412 | ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info); |
| 413 | } while ((ret == -1) && (errno == EINTR)); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 414 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 415 | if (ret < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 416 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 417 | errno, "Can't get stats via ioctl"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 418 | return (-1); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 419 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 422 | /* we receive both rx and tx frames, so comulate all stats */ |
| 423 | stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx + |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 424 | s->acl_tx +s->sco_tx; |
| 425 | stats->ps_drop = s->err_rx + s->err_tx; |
| 426 | stats->ps_ifdrop = 0; |
| 427 | return 0; |
| 428 | } |
| 429 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 430 | static int |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 431 | bt_setdirection_linux(pcap_t *p, pcap_direction_t d) |
| 432 | { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 433 | /* |
| 434 | * It's guaranteed, at this point, that d is a valid |
| 435 | * direction value. |
| 436 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 437 | p->direction = d; |
| 438 | return 0; |
| 439 | } |