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