1 /* 2 * Copyright (c) 2014 Michal Labedzki for Tieto Corporation 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 */ 31 32 #include <config.h> 33 34 #include <errno.h> 35 #include <stdint.h> 36 #include <stdlib.h> 37 #include <string.h> 38 39 #include <bluetooth/bluetooth.h> 40 #include <bluetooth/hci.h> 41 42 #include "pcap/bluetooth.h" 43 #include "pcap-int.h" 44 #include "diag-control.h" 45 46 #include "pcap-bt-monitor-linux.h" 47 48 #define BT_CONTROL_SIZE 32 49 #define INTERFACE_NAME "bluetooth-monitor" 50 51 /* 52 * Private data. 53 * Currently contains nothing. 54 */ 55 struct pcap_bt_monitor { 56 int dummy; 57 }; 58 59 /* 60 * Fields and alignment must match the declaration in the Linux kernel 3.4+. 61 * See struct hci_mon_hdr in include/net/bluetooth/hci_mon.h. 62 */ 63 struct hci_mon_hdr { 64 uint16_t opcode; 65 uint16_t index; 66 uint16_t len; 67 } __attribute__((packed)); 68 69 int 70 bt_monitor_findalldevs(pcap_if_list_t *devlistp, char *err_str) 71 { 72 int ret = 0; 73 74 /* 75 * Bluetooth is a wireless technology. 76 * 77 * This is a device to monitor all Bluetooth interfaces, so 78 * there's no notion of "connected" or "disconnected", any 79 * more than there's a notion of "connected" or "disconnected" 80 * for the "any" device. 81 */ 82 if (pcapint_add_dev(devlistp, INTERFACE_NAME, 83 PCAP_IF_WIRELESS|PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, 84 "Bluetooth Linux Monitor", err_str) == NULL) 85 { 86 ret = PCAP_ERROR; 87 } 88 89 return ret; 90 } 91 92 static int 93 bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user) 94 { 95 struct cmsghdr *cmsg; 96 struct msghdr msg; 97 struct iovec iv[2]; 98 ssize_t ret; 99 struct pcap_pkthdr pkth; 100 pcap_bluetooth_linux_monitor_header *bthdr; 101 u_char *pktd; 102 struct hci_mon_hdr hdr; 103 104 pktd = (u_char *)handle->buffer + BT_CONTROL_SIZE; 105 bthdr = (pcap_bluetooth_linux_monitor_header*)(void *)pktd; 106 107 iv[0].iov_base = &hdr; 108 iv[0].iov_len = sizeof(hdr); 109 iv[1].iov_base = pktd + sizeof(pcap_bluetooth_linux_monitor_header); 110 iv[1].iov_len = handle->snapshot; 111 112 memset(&pkth.ts, 0, sizeof(pkth.ts)); 113 memset(&msg, 0, sizeof(msg)); 114 msg.msg_iov = iv; 115 msg.msg_iovlen = 2; 116 msg.msg_control = handle->buffer; 117 msg.msg_controllen = BT_CONTROL_SIZE; 118 119 do { 120 if (handle->break_loop) 121 { 122 handle->break_loop = 0; 123 return PCAP_ERROR_BREAK; 124 } 125 ret = recvmsg(handle->fd, &msg, 0); 126 } while ((ret == -1) && (errno == EINTR)); 127 128 if (ret < 0) { 129 if (errno == EAGAIN || errno == EWOULDBLOCK) { 130 /* Nonblocking mode, no data */ 131 return 0; 132 } 133 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 134 errno, "Can't receive packet"); 135 return PCAP_ERROR; 136 } 137 138 pkth.caplen = (bpf_u_int32)(ret - sizeof(hdr) + sizeof(pcap_bluetooth_linux_monitor_header)); 139 pkth.len = pkth.caplen; 140 141 // for musl libc CMSG_NXTHDR() 142 DIAG_OFF_SIGN_COMPARE 143 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 144 DIAG_ON_SIGN_COMPARE 145 if (cmsg->cmsg_level != SOL_SOCKET) continue; 146 147 if (cmsg->cmsg_type == SCM_TIMESTAMP) { 148 memcpy(&pkth.ts, CMSG_DATA(cmsg), sizeof(pkth.ts)); 149 } 150 } 151 152 bthdr->adapter_id = htons(hdr.index); 153 bthdr->opcode = htons(hdr.opcode); 154 155 if (handle->fcode.bf_insns == NULL || 156 pcapint_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) { 157 callback(user, &pkth, pktd); 158 return 1; 159 } 160 return 0; /* didn't pass filter */ 161 } 162 163 static int 164 bt_monitor_inject(pcap_t *handle, const void *buf _U_, int size _U_) 165 { 166 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 167 "Packet injection is not supported yet on Bluetooth monitor devices"); 168 return PCAP_ERROR; 169 } 170 171 static int 172 bt_monitor_stats(pcap_t *handle _U_, struct pcap_stat *stats) 173 { 174 stats->ps_recv = 0; 175 stats->ps_drop = 0; 176 stats->ps_ifdrop = 0; 177 178 return 0; 179 } 180 181 static int 182 bt_monitor_activate(pcap_t* handle) 183 { 184 struct sockaddr_hci addr; 185 int err = PCAP_ERROR; 186 int opt; 187 188 if (handle->opt.rfmon) { 189 /* monitor mode doesn't apply here */ 190 return PCAP_ERROR_RFMON_NOTSUP; 191 } 192 193 /* 194 * Turn a negative snapshot value (invalid), a snapshot value of 195 * 0 (unspecified), or a value bigger than the normal maximum 196 * value, into the maximum allowed value. 197 * 198 * If some application really *needs* a bigger snapshot 199 * length, we should just increase MAXIMUM_SNAPLEN. 200 */ 201 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN) 202 handle->snapshot = MAXIMUM_SNAPLEN; 203 204 handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot; 205 handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR; 206 207 handle->read_op = bt_monitor_read; 208 handle->inject_op = bt_monitor_inject; 209 handle->setfilter_op = pcapint_install_bpf_program; /* no kernel filtering */ 210 handle->setdirection_op = NULL; /* Not implemented */ 211 handle->set_datalink_op = NULL; /* can't change data link type */ 212 handle->getnonblock_op = pcapint_getnonblock_fd; 213 handle->setnonblock_op = pcapint_setnonblock_fd; 214 handle->stats_op = bt_monitor_stats; 215 216 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); 217 if (handle->fd < 0) { 218 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 219 errno, "Can't create raw socket"); 220 return PCAP_ERROR; 221 } 222 223 handle->buffer = malloc(handle->bufsize); 224 if (!handle->buffer) { 225 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 226 errno, "Can't allocate dump buffer"); 227 goto close_fail; 228 } 229 230 /* Bind socket to the HCI device */ 231 addr.hci_family = AF_BLUETOOTH; 232 addr.hci_dev = HCI_DEV_NONE; 233 addr.hci_channel = HCI_CHANNEL_MONITOR; 234 235 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 236 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 237 errno, "Can't attach to interface"); 238 goto close_fail; 239 } 240 241 opt = 1; 242 if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) { 243 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 244 errno, "Can't enable time stamp"); 245 goto close_fail; 246 } 247 248 handle->selectable_fd = handle->fd; 249 250 return 0; 251 252 close_fail: 253 pcapint_cleanup_live_common(handle); 254 return err; 255 } 256 257 pcap_t * 258 bt_monitor_create(const char *device, char *ebuf, int *is_ours) 259 { 260 pcap_t *p; 261 const char *cp; 262 263 cp = strrchr(device, '/'); 264 if (cp == NULL) 265 cp = device; 266 267 if (strcmp(cp, INTERFACE_NAME) != 0) { 268 *is_ours = 0; 269 return NULL; 270 } 271 272 *is_ours = 1; 273 p = PCAP_CREATE_COMMON(ebuf, struct pcap_bt_monitor); 274 if (p == NULL) 275 return NULL; 276 277 p->activate_op = bt_monitor_activate; 278 279 return p; 280 } 281