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