1 1.15 christos /* $NetBSD: hci_ioctl.c,v 1.15 2021/09/21 15:03:08 christos Exp $ */ 2 1.1 gdamore 3 1.1 gdamore /*- 4 1.1 gdamore * Copyright (c) 2005 Iain Hibbert. 5 1.1 gdamore * Copyright (c) 2006 Itronix Inc. 6 1.1 gdamore * All rights reserved. 7 1.1 gdamore * 8 1.1 gdamore * Redistribution and use in source and binary forms, with or without 9 1.1 gdamore * modification, are permitted provided that the following conditions 10 1.1 gdamore * are met: 11 1.1 gdamore * 1. Redistributions of source code must retain the above copyright 12 1.1 gdamore * notice, this list of conditions and the following disclaimer. 13 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 gdamore * notice, this list of conditions and the following disclaimer in the 15 1.1 gdamore * documentation and/or other materials provided with the distribution. 16 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse 17 1.1 gdamore * or promote products derived from this software without specific 18 1.1 gdamore * prior written permission. 19 1.1 gdamore * 20 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 21 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 24 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE. 31 1.1 gdamore */ 32 1.1 gdamore 33 1.1 gdamore #include <sys/cdefs.h> 34 1.15 christos __KERNEL_RCSID(0, "$NetBSD: hci_ioctl.c,v 1.15 2021/09/21 15:03:08 christos Exp $"); 35 1.1 gdamore 36 1.1 gdamore #include <sys/param.h> 37 1.1 gdamore #include <sys/domain.h> 38 1.1 gdamore #include <sys/ioctl.h> 39 1.1 gdamore #include <sys/kauth.h> 40 1.1 gdamore #include <sys/kernel.h> 41 1.1 gdamore #include <sys/mbuf.h> 42 1.1 gdamore #include <sys/proc.h> 43 1.1 gdamore #include <sys/systm.h> 44 1.1 gdamore 45 1.1 gdamore #include <netbt/bluetooth.h> 46 1.1 gdamore #include <netbt/hci.h> 47 1.1 gdamore #include <netbt/l2cap.h> 48 1.1 gdamore #include <netbt/rfcomm.h> 49 1.1 gdamore 50 1.1 gdamore #ifdef BLUETOOTH_DEBUG 51 1.1 gdamore #define BDADDR(bd) (bd).b[5], (bd).b[4], (bd).b[3], \ 52 1.1 gdamore (bd).b[2], (bd).b[1], (bd).b[0] 53 1.1 gdamore 54 1.1 gdamore static void 55 1.1 gdamore hci_dump(void) 56 1.1 gdamore { 57 1.1 gdamore struct hci_unit *unit; 58 1.1 gdamore struct hci_link *link; 59 1.1 gdamore struct l2cap_channel *chan; 60 1.1 gdamore struct rfcomm_session *rs; 61 1.1 gdamore struct rfcomm_dlc *dlc; 62 1.1 gdamore 63 1.7 plunky uprintf("HCI:\n"); 64 1.1 gdamore SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) { 65 1.7 plunky uprintf("UNIT %s: flags 0x%4.4x, " 66 1.1 gdamore "num_cmd=%d, num_acl=%d, num_sco=%d\n", 67 1.6 plunky device_xname(unit->hci_dev), unit->hci_flags, 68 1.1 gdamore unit->hci_num_cmd_pkts, 69 1.1 gdamore unit->hci_num_acl_pkts, 70 1.1 gdamore unit->hci_num_sco_pkts); 71 1.1 gdamore TAILQ_FOREACH(link, &unit->hci_links, hl_next) { 72 1.7 plunky uprintf("+HANDLE #%d: %s " 73 1.1 gdamore "raddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, " 74 1.1 gdamore "state %d, refcnt %d\n", 75 1.1 gdamore link->hl_handle, 76 1.1 gdamore (link->hl_type == HCI_LINK_ACL ? "ACL":"SCO"), 77 1.1 gdamore BDADDR(link->hl_bdaddr), 78 1.1 gdamore link->hl_state, link->hl_refcnt); 79 1.1 gdamore } 80 1.1 gdamore } 81 1.1 gdamore 82 1.7 plunky uprintf("L2CAP:\n"); 83 1.1 gdamore LIST_FOREACH(chan, &l2cap_active_list, lc_ncid) { 84 1.7 plunky uprintf("CID #%d state %d, psm=0x%4.4x, " 85 1.1 gdamore "laddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, " 86 1.1 gdamore "raddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", 87 1.1 gdamore chan->lc_lcid, chan->lc_state, chan->lc_raddr.bt_psm, 88 1.1 gdamore BDADDR(chan->lc_laddr.bt_bdaddr), 89 1.1 gdamore BDADDR(chan->lc_raddr.bt_bdaddr)); 90 1.1 gdamore } 91 1.1 gdamore 92 1.1 gdamore LIST_FOREACH(chan, &l2cap_listen_list, lc_ncid) { 93 1.7 plunky uprintf("LISTEN psm=0x%4.4x, " 94 1.1 gdamore "laddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", 95 1.1 gdamore chan->lc_laddr.bt_psm, 96 1.1 gdamore BDADDR(chan->lc_laddr.bt_bdaddr)); 97 1.1 gdamore } 98 1.1 gdamore 99 1.7 plunky uprintf("RFCOMM:\n"); 100 1.1 gdamore LIST_FOREACH(rs, &rfcomm_session_active, rs_next) { 101 1.1 gdamore chan = rs->rs_l2cap; 102 1.7 plunky uprintf("SESSION: state=%d, flags=0x%4.4x, psm 0x%4.4x " 103 1.1 gdamore "laddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, " 104 1.1 gdamore "raddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", 105 1.1 gdamore rs->rs_state, rs->rs_flags, chan->lc_raddr.bt_psm, 106 1.1 gdamore BDADDR(chan->lc_laddr.bt_bdaddr), 107 1.1 gdamore BDADDR(chan->lc_raddr.bt_bdaddr)); 108 1.1 gdamore LIST_FOREACH(dlc, &rs->rs_dlcs, rd_next) { 109 1.7 plunky uprintf("+DLC channel=%d, dlci=%d, " 110 1.4 plunky "state=%d, flags=0x%4.4x, rxcred=%d, rxsize=%ld, " 111 1.1 gdamore "txcred=%d, pending=%d, txqlen=%d\n", 112 1.1 gdamore dlc->rd_raddr.bt_channel, dlc->rd_dlci, 113 1.1 gdamore dlc->rd_state, dlc->rd_flags, 114 1.4 plunky dlc->rd_rxcred, (unsigned long)dlc->rd_rxsize, 115 1.1 gdamore dlc->rd_txcred, dlc->rd_pending, 116 1.1 gdamore (dlc->rd_txbuf ? dlc->rd_txbuf->m_pkthdr.len : 0)); 117 1.1 gdamore } 118 1.1 gdamore } 119 1.1 gdamore 120 1.1 gdamore LIST_FOREACH(rs, &rfcomm_session_listen, rs_next) { 121 1.1 gdamore chan = rs->rs_l2cap; 122 1.7 plunky uprintf("LISTEN: psm 0x%4.4x, " 123 1.1 gdamore "laddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", 124 1.1 gdamore chan->lc_laddr.bt_psm, 125 1.1 gdamore BDADDR(chan->lc_laddr.bt_bdaddr)); 126 1.1 gdamore LIST_FOREACH(dlc, &rs->rs_dlcs, rd_next) 127 1.7 plunky uprintf("+DLC channel=%d\n", dlc->rd_laddr.bt_channel); 128 1.1 gdamore } 129 1.1 gdamore } 130 1.1 gdamore 131 1.1 gdamore #undef BDADDR 132 1.1 gdamore #endif 133 1.1 gdamore 134 1.1 gdamore int 135 1.12 rtr hci_ioctl_pcb(unsigned long cmd, void *data) 136 1.1 gdamore { 137 1.1 gdamore struct btreq *btr = data; 138 1.1 gdamore struct hci_unit *unit; 139 1.7 plunky int err = 0; 140 1.1 gdamore 141 1.1 gdamore DPRINTFN(1, "cmd %#lx\n", cmd); 142 1.1 gdamore 143 1.1 gdamore switch(cmd) { 144 1.1 gdamore #ifdef BLUETOOTH_DEBUG 145 1.1 gdamore case SIOCBTDUMP: 146 1.1 gdamore hci_dump(); 147 1.1 gdamore return 0; 148 1.1 gdamore #endif 149 1.1 gdamore /* 150 1.1 gdamore * Get unit info based on address rather than name 151 1.1 gdamore */ 152 1.1 gdamore case SIOCGBTINFOA: 153 1.1 gdamore unit = hci_unit_lookup(&btr->btr_bdaddr); 154 1.1 gdamore if (unit == NULL) 155 1.1 gdamore return ENXIO; 156 1.1 gdamore 157 1.1 gdamore break; 158 1.1 gdamore 159 1.1 gdamore /* 160 1.1 gdamore * The remaining ioctl's all use the same btreq structure and 161 1.1 gdamore * index on the name of the device, so we look that up first. 162 1.1 gdamore */ 163 1.1 gdamore case SIOCNBTINFO: 164 1.1 gdamore /* empty name means give the first unit */ 165 1.1 gdamore if (btr->btr_name[0] == '\0') { 166 1.1 gdamore unit = NULL; 167 1.1 gdamore break; 168 1.1 gdamore } 169 1.1 gdamore 170 1.1 gdamore /* else fall through and look it up */ 171 1.14 mrg /* FALLTHROUGH */ 172 1.1 gdamore case SIOCGBTINFO: 173 1.1 gdamore case SIOCSBTFLAGS: 174 1.1 gdamore case SIOCSBTPOLICY: 175 1.1 gdamore case SIOCSBTPTYPE: 176 1.1 gdamore case SIOCGBTSTATS: 177 1.1 gdamore case SIOCZBTSTATS: 178 1.3 plunky case SIOCSBTSCOMTU: 179 1.10 plunky case SIOCGBTFEAT: 180 1.1 gdamore SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) { 181 1.6 plunky if (strncmp(device_xname(unit->hci_dev), 182 1.6 plunky btr->btr_name, HCI_DEVNAME_SIZE) == 0) 183 1.1 gdamore break; 184 1.1 gdamore } 185 1.1 gdamore 186 1.1 gdamore if (unit == NULL) 187 1.1 gdamore return ENXIO; 188 1.1 gdamore 189 1.1 gdamore break; 190 1.1 gdamore 191 1.1 gdamore default: /* not one of mine */ 192 1.1 gdamore return EPASSTHROUGH; 193 1.1 gdamore } 194 1.1 gdamore 195 1.1 gdamore switch(cmd) { 196 1.1 gdamore case SIOCNBTINFO: /* get next info */ 197 1.1 gdamore if (unit) 198 1.1 gdamore unit = SIMPLEQ_NEXT(unit, hci_next); 199 1.1 gdamore else 200 1.1 gdamore unit = SIMPLEQ_FIRST(&hci_unit_list); 201 1.1 gdamore 202 1.1 gdamore if (unit == NULL) { 203 1.1 gdamore err = ENXIO; 204 1.1 gdamore break; 205 1.1 gdamore } 206 1.1 gdamore 207 1.14 mrg /* FALLTHROUGH */ 208 1.1 gdamore case SIOCGBTINFO: /* get unit info */ 209 1.14 mrg /* FALLTHROUGH */ 210 1.1 gdamore case SIOCGBTINFOA: /* get info by address */ 211 1.1 gdamore memset(btr, 0, sizeof(struct btreq)); 212 1.6 plunky strlcpy(btr->btr_name, device_xname(unit->hci_dev), HCI_DEVNAME_SIZE); 213 1.1 gdamore bdaddr_copy(&btr->btr_bdaddr, &unit->hci_bdaddr); 214 1.1 gdamore 215 1.1 gdamore btr->btr_flags = unit->hci_flags; 216 1.1 gdamore 217 1.1 gdamore btr->btr_num_cmd = unit->hci_num_cmd_pkts; 218 1.1 gdamore btr->btr_num_acl = unit->hci_num_acl_pkts; 219 1.1 gdamore btr->btr_num_sco = unit->hci_num_sco_pkts; 220 1.1 gdamore btr->btr_acl_mtu = unit->hci_max_acl_size; 221 1.1 gdamore btr->btr_sco_mtu = unit->hci_max_sco_size; 222 1.10 plunky btr->btr_max_acl = unit->hci_max_acl_pkts; 223 1.10 plunky btr->btr_max_sco = unit->hci_max_sco_pkts; 224 1.1 gdamore 225 1.1 gdamore btr->btr_packet_type = unit->hci_packet_type; 226 1.1 gdamore btr->btr_link_policy = unit->hci_link_policy; 227 1.1 gdamore break; 228 1.1 gdamore 229 1.1 gdamore case SIOCSBTFLAGS: /* set unit flags (privileged) */ 230 1.15 christos err = kauth_authorize_device(kauth_cred_get(), 231 1.8 elad KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd), 232 1.8 elad btr, NULL); 233 1.1 gdamore if (err) 234 1.1 gdamore break; 235 1.1 gdamore 236 1.1 gdamore if ((unit->hci_flags & BTF_UP) 237 1.1 gdamore && (btr->btr_flags & BTF_UP) == 0) { 238 1.1 gdamore hci_disable(unit); 239 1.1 gdamore unit->hci_flags &= ~BTF_UP; 240 1.1 gdamore } 241 1.1 gdamore 242 1.9 plunky unit->hci_flags &= ~BTF_MASTER; 243 1.9 plunky unit->hci_flags |= (btr->btr_flags & (BTF_INIT | BTF_MASTER)); 244 1.1 gdamore 245 1.1 gdamore if ((unit->hci_flags & BTF_UP) == 0 246 1.1 gdamore && (btr->btr_flags & BTF_UP)) { 247 1.1 gdamore err = hci_enable(unit); 248 1.1 gdamore if (err) 249 1.1 gdamore break; 250 1.1 gdamore 251 1.1 gdamore unit->hci_flags |= BTF_UP; 252 1.1 gdamore } 253 1.1 gdamore 254 1.1 gdamore btr->btr_flags = unit->hci_flags; 255 1.1 gdamore break; 256 1.1 gdamore 257 1.1 gdamore case SIOCSBTPOLICY: /* set unit link policy (privileged) */ 258 1.15 christos err = kauth_authorize_device(kauth_cred_get(), 259 1.8 elad KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd), 260 1.8 elad btr, NULL); 261 1.1 gdamore if (err) 262 1.1 gdamore break; 263 1.1 gdamore 264 1.1 gdamore unit->hci_link_policy = btr->btr_link_policy; 265 1.1 gdamore unit->hci_link_policy &= unit->hci_lmp_mask; 266 1.1 gdamore btr->btr_link_policy = unit->hci_link_policy; 267 1.1 gdamore break; 268 1.1 gdamore 269 1.1 gdamore case SIOCSBTPTYPE: /* set unit packet types (privileged) */ 270 1.15 christos err = kauth_authorize_device(kauth_cred_get(), 271 1.8 elad KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd), 272 1.8 elad btr, NULL); 273 1.1 gdamore if (err) 274 1.1 gdamore break; 275 1.1 gdamore 276 1.1 gdamore unit->hci_packet_type = btr->btr_packet_type; 277 1.1 gdamore unit->hci_packet_type &= unit->hci_acl_mask; 278 1.1 gdamore btr->btr_packet_type = unit->hci_packet_type; 279 1.1 gdamore break; 280 1.1 gdamore 281 1.1 gdamore case SIOCGBTSTATS: /* get unit statistics */ 282 1.7 plunky (*unit->hci_if->get_stats)(unit->hci_dev, &btr->btr_stats, 0); 283 1.1 gdamore break; 284 1.1 gdamore 285 1.1 gdamore case SIOCZBTSTATS: /* get & reset unit statistics */ 286 1.15 christos err = kauth_authorize_device(kauth_cred_get(), 287 1.8 elad KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd), 288 1.8 elad btr, NULL); 289 1.1 gdamore if (err) 290 1.1 gdamore break; 291 1.1 gdamore 292 1.7 plunky (*unit->hci_if->get_stats)(unit->hci_dev, &btr->btr_stats, 1); 293 1.1 gdamore break; 294 1.1 gdamore 295 1.3 plunky case SIOCSBTSCOMTU: /* set sco_mtu value for unit */ 296 1.3 plunky /* 297 1.3 plunky * This is a temporary ioctl and may not be supported 298 1.3 plunky * in the future. The need is that if SCO packets are 299 1.3 plunky * sent to USB bluetooth controllers that are not an 300 1.3 plunky * integer number of frame sizes, the USB bus locks up. 301 1.3 plunky */ 302 1.15 christos err = kauth_authorize_device(kauth_cred_get(), 303 1.8 elad KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd), 304 1.8 elad btr, NULL); 305 1.3 plunky if (err) 306 1.3 plunky break; 307 1.3 plunky 308 1.3 plunky unit->hci_max_sco_size = btr->btr_sco_mtu; 309 1.3 plunky break; 310 1.3 plunky 311 1.10 plunky case SIOCGBTFEAT: /* get unit features */ 312 1.10 plunky memset(btr, 0, sizeof(struct btreq)); 313 1.10 plunky strlcpy(btr->btr_name, device_xname(unit->hci_dev), HCI_DEVNAME_SIZE); 314 1.10 plunky memcpy(btr->btr_features0, unit->hci_feat0, HCI_FEATURES_SIZE); 315 1.10 plunky memcpy(btr->btr_features1, unit->hci_feat1, HCI_FEATURES_SIZE); 316 1.13 plunky memcpy(btr->btr_features2, unit->hci_feat2, HCI_FEATURES_SIZE); 317 1.10 plunky break; 318 1.10 plunky 319 1.1 gdamore default: 320 1.1 gdamore err = EFAULT; 321 1.1 gdamore break; 322 1.1 gdamore } 323 1.1 gdamore 324 1.1 gdamore return err; 325 1.1 gdamore } 326