1*481d3881Srin /* $NetBSD: hci_socket.c,v 1.48 2024/07/05 04:31:53 rin Exp $ */
2a5c89047Sgdamore
3a5c89047Sgdamore /*-
4a5c89047Sgdamore * Copyright (c) 2005 Iain Hibbert.
5a5c89047Sgdamore * Copyright (c) 2006 Itronix Inc.
6a5c89047Sgdamore * All rights reserved.
7a5c89047Sgdamore *
8a5c89047Sgdamore * Redistribution and use in source and binary forms, with or without
9a5c89047Sgdamore * modification, are permitted provided that the following conditions
10a5c89047Sgdamore * are met:
11a5c89047Sgdamore * 1. Redistributions of source code must retain the above copyright
12a5c89047Sgdamore * notice, this list of conditions and the following disclaimer.
13a5c89047Sgdamore * 2. Redistributions in binary form must reproduce the above copyright
14a5c89047Sgdamore * notice, this list of conditions and the following disclaimer in the
15a5c89047Sgdamore * documentation and/or other materials provided with the distribution.
16a5c89047Sgdamore * 3. The name of Itronix Inc. may not be used to endorse
17a5c89047Sgdamore * or promote products derived from this software without specific
18a5c89047Sgdamore * prior written permission.
19a5c89047Sgdamore *
20a5c89047Sgdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21a5c89047Sgdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22a5c89047Sgdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23a5c89047Sgdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24a5c89047Sgdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25a5c89047Sgdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26a5c89047Sgdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27a5c89047Sgdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
28a5c89047Sgdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29a5c89047Sgdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30a5c89047Sgdamore * POSSIBILITY OF SUCH DAMAGE.
31a5c89047Sgdamore */
32a5c89047Sgdamore
33a5c89047Sgdamore #include <sys/cdefs.h>
34*481d3881Srin __KERNEL_RCSID(0, "$NetBSD: hci_socket.c,v 1.48 2024/07/05 04:31:53 rin Exp $");
35a5c89047Sgdamore
36fb76dbd4Splunky /* load symbolic names */
37a5c89047Sgdamore #ifdef BLUETOOTH_DEBUG
38fb76dbd4Splunky #define PRUREQUESTS
39a5c89047Sgdamore #define PRCOREQUESTS
40a5c89047Sgdamore #endif
41a5c89047Sgdamore
42a5c89047Sgdamore #include <sys/param.h>
43a5c89047Sgdamore #include <sys/domain.h>
44a5c89047Sgdamore #include <sys/kauth.h>
45a5c89047Sgdamore #include <sys/kernel.h>
464ae03c18Srmind #include <sys/kmem.h>
47a5c89047Sgdamore #include <sys/mbuf.h>
48a5c89047Sgdamore #include <sys/proc.h>
49a5c89047Sgdamore #include <sys/protosw.h>
50a5c89047Sgdamore #include <sys/socket.h>
51a5c89047Sgdamore #include <sys/socketvar.h>
52a5c89047Sgdamore #include <sys/systm.h>
53a5c89047Sgdamore
54a5c89047Sgdamore #include <netbt/bluetooth.h>
55a5c89047Sgdamore #include <netbt/hci.h>
56a5c89047Sgdamore
57a5c89047Sgdamore /*******************************************************************************
58a5c89047Sgdamore *
59a5c89047Sgdamore * HCI SOCK_RAW Sockets - for control of Bluetooth Devices
60a5c89047Sgdamore *
61a5c89047Sgdamore */
62a5c89047Sgdamore
63a5c89047Sgdamore /*
64a5c89047Sgdamore * the raw HCI protocol control block
65a5c89047Sgdamore */
66a5c89047Sgdamore struct hci_pcb {
67a5c89047Sgdamore struct socket *hp_socket; /* socket */
6880c6ec5dSplunky kauth_cred_t hp_cred; /* owner credential */
69a5c89047Sgdamore unsigned int hp_flags; /* flags */
70a5c89047Sgdamore bdaddr_t hp_laddr; /* local address */
71a5c89047Sgdamore bdaddr_t hp_raddr; /* remote address */
72a5c89047Sgdamore struct hci_filter hp_efilter; /* user event filter */
73a5c89047Sgdamore struct hci_filter hp_pfilter; /* user packet filter */
74a5c89047Sgdamore LIST_ENTRY(hci_pcb) hp_next; /* next HCI pcb */
75a5c89047Sgdamore };
76a5c89047Sgdamore
77a5c89047Sgdamore /* hp_flags */
78a5c89047Sgdamore #define HCI_DIRECTION (1<<1) /* direction control messages */
79a5c89047Sgdamore #define HCI_PROMISCUOUS (1<<2) /* listen to all units */
80a5c89047Sgdamore
81a5c89047Sgdamore LIST_HEAD(hci_pcb_list, hci_pcb) hci_pcb = LIST_HEAD_INITIALIZER(hci_pcb);
82a5c89047Sgdamore
83a5c89047Sgdamore /* sysctl defaults */
84a5c89047Sgdamore int hci_sendspace = HCI_CMD_PKT_SIZE;
85a5c89047Sgdamore int hci_recvspace = 4096;
86a5c89047Sgdamore
8780c6ec5dSplunky /* unprivileged commands opcode table */
88aeab3db8Splunky static const struct {
89aeab3db8Splunky uint16_t opcode;
90aeab3db8Splunky uint8_t offs; /* 0 - 63 */
91aeab3db8Splunky uint8_t mask; /* bit 0 - 7 */
9280c6ec5dSplunky uint8_t length; /* approved length */
93aeab3db8Splunky } hci_cmds[] = {
94aeab3db8Splunky { HCI_CMD_INQUIRY,
95aeab3db8Splunky 0, 0x01, sizeof(hci_inquiry_cp) },
96aeab3db8Splunky { HCI_CMD_REMOTE_NAME_REQ,
97aeab3db8Splunky 2, 0x08, sizeof(hci_remote_name_req_cp) },
98aeab3db8Splunky { HCI_CMD_READ_REMOTE_FEATURES,
99aeab3db8Splunky 2, 0x20, sizeof(hci_read_remote_features_cp) },
100aeab3db8Splunky { HCI_CMD_READ_REMOTE_EXTENDED_FEATURES,
101aeab3db8Splunky 2, 0x40, sizeof(hci_read_remote_extended_features_cp) },
102aeab3db8Splunky { HCI_CMD_READ_REMOTE_VER_INFO,
103aeab3db8Splunky 2, 0x80, sizeof(hci_read_remote_ver_info_cp) },
104aeab3db8Splunky { HCI_CMD_READ_CLOCK_OFFSET,
105aeab3db8Splunky 3, 0x01, sizeof(hci_read_clock_offset_cp) },
106aeab3db8Splunky { HCI_CMD_READ_LMP_HANDLE,
107aeab3db8Splunky 3, 0x02, sizeof(hci_read_lmp_handle_cp) },
108aeab3db8Splunky { HCI_CMD_ROLE_DISCOVERY,
109aeab3db8Splunky 4, 0x80, sizeof(hci_role_discovery_cp) },
110aeab3db8Splunky { HCI_CMD_READ_LINK_POLICY_SETTINGS,
111aeab3db8Splunky 5, 0x02, sizeof(hci_read_link_policy_settings_cp) },
112aeab3db8Splunky { HCI_CMD_READ_DEFAULT_LINK_POLICY_SETTINGS,
113aeab3db8Splunky 5, 0x08, 0 },
114aeab3db8Splunky { HCI_CMD_READ_PIN_TYPE,
115aeab3db8Splunky 6, 0x04, 0 },
116aeab3db8Splunky { HCI_CMD_READ_LOCAL_NAME,
117aeab3db8Splunky 7, 0x02, 0 },
118aeab3db8Splunky { HCI_CMD_READ_CON_ACCEPT_TIMEOUT,
119aeab3db8Splunky 7, 0x04, 0 },
120aeab3db8Splunky { HCI_CMD_READ_PAGE_TIMEOUT,
121aeab3db8Splunky 7, 0x10, 0 },
122aeab3db8Splunky { HCI_CMD_READ_SCAN_ENABLE,
123aeab3db8Splunky 7, 0x40, 0 },
124aeab3db8Splunky { HCI_CMD_READ_PAGE_SCAN_ACTIVITY,
125aeab3db8Splunky 8, 0x01, 0 },
126aeab3db8Splunky { HCI_CMD_READ_INQUIRY_SCAN_ACTIVITY,
127aeab3db8Splunky 8, 0x04, 0 },
128aeab3db8Splunky { HCI_CMD_READ_AUTH_ENABLE,
129aeab3db8Splunky 8, 0x10, 0 },
130aeab3db8Splunky { HCI_CMD_READ_ENCRYPTION_MODE,
131aeab3db8Splunky 8, 0x40, 0 },
132aeab3db8Splunky { HCI_CMD_READ_UNIT_CLASS,
133aeab3db8Splunky 9, 0x01, 0 },
134aeab3db8Splunky { HCI_CMD_READ_VOICE_SETTING,
135aeab3db8Splunky 9, 0x04, 0 },
136aeab3db8Splunky { HCI_CMD_READ_AUTO_FLUSH_TIMEOUT,
137aeab3db8Splunky 9, 0x10, sizeof(hci_read_auto_flush_timeout_cp) },
138aeab3db8Splunky { HCI_CMD_READ_NUM_BROADCAST_RETRANS,
139aeab3db8Splunky 9, 0x40, 0 },
140aeab3db8Splunky { HCI_CMD_READ_HOLD_MODE_ACTIVITY,
141aeab3db8Splunky 10, 0x01, 0 },
142aeab3db8Splunky { HCI_CMD_READ_XMIT_LEVEL,
143aeab3db8Splunky 10, 0x04, sizeof(hci_read_xmit_level_cp) },
144aeab3db8Splunky { HCI_CMD_READ_SCO_FLOW_CONTROL,
145aeab3db8Splunky 10, 0x08, 0 },
146aeab3db8Splunky { HCI_CMD_READ_LINK_SUPERVISION_TIMEOUT,
147aeab3db8Splunky 11, 0x01, sizeof(hci_read_link_supervision_timeout_cp) },
148aeab3db8Splunky { HCI_CMD_READ_NUM_SUPPORTED_IAC,
149aeab3db8Splunky 11, 0x04, 0 },
150aeab3db8Splunky { HCI_CMD_READ_IAC_LAP,
151aeab3db8Splunky 11, 0x08, 0 },
152aeab3db8Splunky { HCI_CMD_READ_PAGE_SCAN_PERIOD,
153aeab3db8Splunky 11, 0x20, 0 },
154aeab3db8Splunky { HCI_CMD_READ_PAGE_SCAN,
155aeab3db8Splunky 11, 0x80, 0 },
156aeab3db8Splunky { HCI_CMD_READ_INQUIRY_SCAN_TYPE,
157aeab3db8Splunky 12, 0x10, 0 },
158aeab3db8Splunky { HCI_CMD_READ_INQUIRY_MODE,
159aeab3db8Splunky 12, 0x40, 0 },
160aeab3db8Splunky { HCI_CMD_READ_PAGE_SCAN_TYPE,
161aeab3db8Splunky 13, 0x01, 0 },
162aeab3db8Splunky { HCI_CMD_READ_AFH_ASSESSMENT,
163aeab3db8Splunky 13, 0x04, 0 },
164aeab3db8Splunky { HCI_CMD_READ_LOCAL_VER,
165aeab3db8Splunky 14, 0x08, 0 },
166aeab3db8Splunky { HCI_CMD_READ_LOCAL_COMMANDS,
167aeab3db8Splunky 14, 0x10, 0 },
168aeab3db8Splunky { HCI_CMD_READ_LOCAL_FEATURES,
169aeab3db8Splunky 14, 0x20, 0 },
170aeab3db8Splunky { HCI_CMD_READ_LOCAL_EXTENDED_FEATURES,
171aeab3db8Splunky 14, 0x40, sizeof(hci_read_local_extended_features_cp) },
172aeab3db8Splunky { HCI_CMD_READ_BUFFER_SIZE,
173aeab3db8Splunky 14, 0x80, 0 },
174aeab3db8Splunky { HCI_CMD_READ_COUNTRY_CODE,
175aeab3db8Splunky 15, 0x01, 0 },
176aeab3db8Splunky { HCI_CMD_READ_BDADDR,
177aeab3db8Splunky 15, 0x02, 0 },
178aeab3db8Splunky { HCI_CMD_READ_FAILED_CONTACT_CNTR,
179aeab3db8Splunky 15, 0x04, sizeof(hci_read_failed_contact_cntr_cp) },
180aeab3db8Splunky { HCI_CMD_READ_LINK_QUALITY,
181aeab3db8Splunky 15, 0x10, sizeof(hci_read_link_quality_cp) },
182aeab3db8Splunky { HCI_CMD_READ_RSSI,
183aeab3db8Splunky 15, 0x20, sizeof(hci_read_rssi_cp) },
184aeab3db8Splunky { HCI_CMD_READ_AFH_CHANNEL_MAP,
185aeab3db8Splunky 15, 0x40, sizeof(hci_read_afh_channel_map_cp) },
186aeab3db8Splunky { HCI_CMD_READ_CLOCK,
187aeab3db8Splunky 15, 0x80, sizeof(hci_read_clock_cp) },
188aeab3db8Splunky { HCI_CMD_READ_LOOPBACK_MODE,
189aeab3db8Splunky 16, 0x01, 0 },
1907acc9392Splunky { HCI_CMD_READ_EXTENDED_INQUIRY_RSP,
1917acc9392Splunky 17, 0x01, 0 },
1927acc9392Splunky { HCI_CMD_READ_SIMPLE_PAIRING_MODE,
1937acc9392Splunky 17, 0x20, 0 },
1947acc9392Splunky { HCI_CMD_READ_INQUIRY_RSP_XMIT_POWER,
1957acc9392Splunky 18, 0x01, 0 },
1967acc9392Splunky { HCI_CMD_READ_DEFAULT_ERRDATA_REPORTING,
1977acc9392Splunky 18, 0x04, 0 },
1981f4a29eaSplunky { HCI_CMD_READ_ENCRYPTION_KEY_SIZE,
1991f4a29eaSplunky 20, 0x10, sizeof(hci_read_encryption_key_size_cp) },
200aeab3db8Splunky };
201aeab3db8Splunky
202a5c89047Sgdamore /*
20380c6ec5dSplunky * supply a basic device send/recv policy
20480c6ec5dSplunky */
20580c6ec5dSplunky static int
hci_device_cb(kauth_cred_t cred,kauth_action_t action,void * cookie,void * arg0,void * arg1,void * arg2,void * arg3)20680c6ec5dSplunky hci_device_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
20780c6ec5dSplunky void *arg0, void *arg1, void *arg2, void *arg3)
20880c6ec5dSplunky {
20980c6ec5dSplunky int i, result;
21080c6ec5dSplunky
21180c6ec5dSplunky result = KAUTH_RESULT_DEFER;
21280c6ec5dSplunky
21380c6ec5dSplunky switch (action) {
2144f6ac133Splunky case KAUTH_DEVICE_BLUETOOTH_SEND: {
21580c6ec5dSplunky struct hci_unit *unit = (struct hci_unit *)arg0;
21680c6ec5dSplunky hci_cmd_hdr_t *hdr = (hci_cmd_hdr_t *)arg1;
21780c6ec5dSplunky
21880c6ec5dSplunky /*
21980c6ec5dSplunky * Allow sending unprivileged commands if the packet size
22080c6ec5dSplunky * is correct and the unit claims to support it
221a5c89047Sgdamore */
222a5c89047Sgdamore
2234f6ac133Splunky if (hdr->type != HCI_CMD_PKT)
2244f6ac133Splunky break;
2254f6ac133Splunky
226aeab3db8Splunky for (i = 0; i < __arraycount(hci_cmds); i++) {
22780c6ec5dSplunky if (hdr->opcode == hci_cmds[i].opcode
22880c6ec5dSplunky && hdr->length == hci_cmds[i].length
22980c6ec5dSplunky && (unit->hci_cmds[hci_cmds[i].offs] & hci_cmds[i].mask)) {
23080c6ec5dSplunky result = KAUTH_RESULT_ALLOW;
23180c6ec5dSplunky break;
23280c6ec5dSplunky }
23380c6ec5dSplunky }
234b1b95bf6Splunky
235aeab3db8Splunky break;
236a5c89047Sgdamore }
237a5c89047Sgdamore
2384f6ac133Splunky case KAUTH_DEVICE_BLUETOOTH_RECV:
2394f6ac133Splunky switch((uint8_t)(uintptr_t)arg0) {
2404f6ac133Splunky case HCI_CMD_PKT: {
2414f6ac133Splunky uint16_t opcode = (uint16_t)(uintptr_t)arg1;
24280c6ec5dSplunky
24380c6ec5dSplunky /*
24480c6ec5dSplunky * Allow to see any unprivileged command packet
24580c6ec5dSplunky */
24680c6ec5dSplunky
24780c6ec5dSplunky for (i = 0; i < __arraycount(hci_cmds); i++) {
24880c6ec5dSplunky if (opcode == hci_cmds[i].opcode) {
24980c6ec5dSplunky result = KAUTH_RESULT_ALLOW;
25080c6ec5dSplunky break;
25180c6ec5dSplunky }
252a5c89047Sgdamore }
253a5c89047Sgdamore
25480c6ec5dSplunky break;
25580c6ec5dSplunky }
25680c6ec5dSplunky
2574f6ac133Splunky case HCI_EVENT_PKT: {
2584f6ac133Splunky uint8_t event = (uint8_t)(uintptr_t)arg1;
25980c6ec5dSplunky
26080c6ec5dSplunky /*
26180c6ec5dSplunky * Allow to receive most events
26280c6ec5dSplunky */
263a5c89047Sgdamore
264b1b95bf6Splunky switch (event) {
265b1b95bf6Splunky case HCI_EVENT_RETURN_LINK_KEYS:
266b1b95bf6Splunky case HCI_EVENT_LINK_KEY_NOTIFICATION:
2677acc9392Splunky case HCI_EVENT_USER_CONFIRM_REQ:
2687acc9392Splunky case HCI_EVENT_USER_PASSKEY_NOTIFICATION:
269b1b95bf6Splunky case HCI_EVENT_VENDOR:
27080c6ec5dSplunky break;
27180c6ec5dSplunky
27280c6ec5dSplunky default:
27380c6ec5dSplunky result = KAUTH_RESULT_ALLOW;
27480c6ec5dSplunky break;
275a5c89047Sgdamore }
276a5c89047Sgdamore
27780c6ec5dSplunky break;
27880c6ec5dSplunky }
27980c6ec5dSplunky
2804f6ac133Splunky case HCI_ACL_DATA_PKT:
2814f6ac133Splunky case HCI_SCO_DATA_PKT: {
2824f6ac133Splunky /* uint16_t handle = (uint16_t)(uintptr_t)arg1; */
28380c6ec5dSplunky /*
28480c6ec5dSplunky * don't normally allow receiving data packets
28580c6ec5dSplunky */
28680c6ec5dSplunky break;
2874f6ac133Splunky }
2884f6ac133Splunky
2894f6ac133Splunky default:
2904f6ac133Splunky break;
2914f6ac133Splunky }
2924f6ac133Splunky
2934f6ac133Splunky break;
29480c6ec5dSplunky
29580c6ec5dSplunky default:
29680c6ec5dSplunky break;
29780c6ec5dSplunky }
29880c6ec5dSplunky
29980c6ec5dSplunky return result;
30080c6ec5dSplunky }
30180c6ec5dSplunky
30280c6ec5dSplunky /*
30380c6ec5dSplunky * HCI protocol init routine,
30480c6ec5dSplunky * - set up a kauth listener to provide basic packet access policy
30580c6ec5dSplunky */
30680c6ec5dSplunky void
hci_init(void)30780c6ec5dSplunky hci_init(void)
30880c6ec5dSplunky {
30980c6ec5dSplunky
31080c6ec5dSplunky if (kauth_listen_scope(KAUTH_SCOPE_DEVICE, hci_device_cb, NULL) == NULL)
31180c6ec5dSplunky panic("Bluetooth HCI: cannot listen on device scope");
312a5c89047Sgdamore }
313a5c89047Sgdamore
314a5c89047Sgdamore /*
315a5c89047Sgdamore * When command packet reaches the device, we can drop
316a5c89047Sgdamore * it from the socket buffer (called from hci_output_acl)
317a5c89047Sgdamore */
318a5c89047Sgdamore void
hci_drop(void * arg)319a5c89047Sgdamore hci_drop(void *arg)
320a5c89047Sgdamore {
321a5c89047Sgdamore struct socket *so = arg;
322a5c89047Sgdamore
323a5c89047Sgdamore sbdroprecord(&so->so_snd);
324a5c89047Sgdamore sowwakeup(so);
325a5c89047Sgdamore }
326a5c89047Sgdamore
327a5c89047Sgdamore /*
328a5c89047Sgdamore * HCI socket is going away and has some pending packets. We let them
329a5c89047Sgdamore * go by design, but remove the context pointer as it will be invalid
330a5c89047Sgdamore * and we no longer need to be notified.
331a5c89047Sgdamore */
332a5c89047Sgdamore static void
hci_cmdwait_flush(struct socket * so)333a5c89047Sgdamore hci_cmdwait_flush(struct socket *so)
334a5c89047Sgdamore {
335a5c89047Sgdamore struct hci_unit *unit;
336a5c89047Sgdamore struct socket *ctx;
337a5c89047Sgdamore struct mbuf *m;
338a5c89047Sgdamore
339a5c89047Sgdamore DPRINTF("flushing %p\n", so);
340a5c89047Sgdamore
341a5c89047Sgdamore SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
342a5c89047Sgdamore m = MBUFQ_FIRST(&unit->hci_cmdwait);
343a5c89047Sgdamore while (m != NULL) {
344a5c89047Sgdamore ctx = M_GETCTX(m, struct socket *);
345a5c89047Sgdamore if (ctx == so)
346a5c89047Sgdamore M_SETCTX(m, NULL);
347a5c89047Sgdamore
348a5c89047Sgdamore m = MBUFQ_NEXT(m);
349a5c89047Sgdamore }
350a5c89047Sgdamore }
351a5c89047Sgdamore }
352a5c89047Sgdamore
3534ae03c18Srmind static int
hci_attach(struct socket * so,int proto)35456a73a7dSrmind hci_attach(struct socket *so, int proto)
3554ae03c18Srmind {
3564ae03c18Srmind struct hci_pcb *pcb;
3574ae03c18Srmind int error;
3584ae03c18Srmind
3594ae03c18Srmind KASSERT(so->so_pcb == NULL);
3604ae03c18Srmind
3614ae03c18Srmind if (so->so_lock == NULL) {
3624ae03c18Srmind mutex_obj_hold(bt_lock);
3634ae03c18Srmind so->so_lock = bt_lock;
3644ae03c18Srmind solock(so);
3654ae03c18Srmind }
3664ae03c18Srmind KASSERT(solocked(so));
3674ae03c18Srmind
3684ae03c18Srmind error = soreserve(so, hci_sendspace, hci_recvspace);
3694ae03c18Srmind if (error) {
3704ae03c18Srmind return error;
3714ae03c18Srmind }
3724ae03c18Srmind
3734ae03c18Srmind pcb = kmem_zalloc(sizeof(struct hci_pcb), KM_SLEEP);
3744ae03c18Srmind pcb->hp_cred = kauth_cred_dup(curlwp->l_cred);
3754ae03c18Srmind pcb->hp_socket = so;
3764ae03c18Srmind
3774ae03c18Srmind /*
3784ae03c18Srmind * Set default user filter. By default, socket only passes
3794ae03c18Srmind * Command_Complete and Command_Status Events.
3804ae03c18Srmind */
3814ae03c18Srmind hci_filter_set(HCI_EVENT_COMMAND_COMPL, &pcb->hp_efilter);
3824ae03c18Srmind hci_filter_set(HCI_EVENT_COMMAND_STATUS, &pcb->hp_efilter);
3834ae03c18Srmind hci_filter_set(HCI_EVENT_PKT, &pcb->hp_pfilter);
3844ae03c18Srmind
3854ae03c18Srmind LIST_INSERT_HEAD(&hci_pcb, pcb, hp_next);
3864ae03c18Srmind so->so_pcb = pcb;
3874ae03c18Srmind
3884ae03c18Srmind return 0;
3894ae03c18Srmind }
3904ae03c18Srmind
3914ae03c18Srmind static void
hci_detach(struct socket * so)39256a73a7dSrmind hci_detach(struct socket *so)
3934ae03c18Srmind {
3944ae03c18Srmind struct hci_pcb *pcb;
3954ae03c18Srmind
3964ae03c18Srmind pcb = (struct hci_pcb *)so->so_pcb;
3974ae03c18Srmind KASSERT(pcb != NULL);
3984ae03c18Srmind
3994ae03c18Srmind if (so->so_snd.sb_mb != NULL)
4004ae03c18Srmind hci_cmdwait_flush(so);
4014ae03c18Srmind
4024ae03c18Srmind if (pcb->hp_cred != NULL)
4034ae03c18Srmind kauth_cred_free(pcb->hp_cred);
4044ae03c18Srmind
4054ae03c18Srmind so->so_pcb = NULL;
4064ae03c18Srmind LIST_REMOVE(pcb, hp_next);
4074ae03c18Srmind kmem_free(pcb, sizeof(*pcb));
4084ae03c18Srmind }
4094ae03c18Srmind
410d54d7ab2Srtr static int
hci_accept(struct socket * so,struct sockaddr * nam)411eddf3af3Srtr hci_accept(struct socket *so, struct sockaddr *nam)
412d27b133dSrtr {
413d27b133dSrtr KASSERT(solocked(so));
414d27b133dSrtr
415d27b133dSrtr return EOPNOTSUPP;
416d27b133dSrtr }
417d27b133dSrtr
418d27b133dSrtr static int
hci_bind(struct socket * so,struct sockaddr * nam,struct lwp * l)419a2ba5e69Srtr hci_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
4206dd8eef0Srtr {
4216dd8eef0Srtr struct hci_pcb *pcb = so->so_pcb;
422a2ba5e69Srtr struct sockaddr_bt *sa = (struct sockaddr_bt *)nam;
4236dd8eef0Srtr
4246dd8eef0Srtr KASSERT(solocked(so));
4256dd8eef0Srtr KASSERT(pcb != NULL);
4266dd8eef0Srtr KASSERT(nam != NULL);
4276dd8eef0Srtr
4286dd8eef0Srtr if (sa->bt_len != sizeof(struct sockaddr_bt))
4296dd8eef0Srtr return EINVAL;
4306dd8eef0Srtr
4316dd8eef0Srtr if (sa->bt_family != AF_BLUETOOTH)
4326dd8eef0Srtr return EAFNOSUPPORT;
4336dd8eef0Srtr
4346dd8eef0Srtr bdaddr_copy(&pcb->hp_laddr, &sa->bt_bdaddr);
4356dd8eef0Srtr
4366dd8eef0Srtr if (bdaddr_any(&sa->bt_bdaddr))
4376dd8eef0Srtr pcb->hp_flags |= HCI_PROMISCUOUS;
4386dd8eef0Srtr else
4396dd8eef0Srtr pcb->hp_flags &= ~HCI_PROMISCUOUS;
4406dd8eef0Srtr
4416dd8eef0Srtr return 0;
4426dd8eef0Srtr }
4436dd8eef0Srtr
4446dd8eef0Srtr static int
hci_listen(struct socket * so,struct lwp * l)445ce6a5ff6Srtr hci_listen(struct socket *so, struct lwp *l)
4466dd8eef0Srtr {
4476dd8eef0Srtr KASSERT(solocked(so));
4486dd8eef0Srtr
4496dd8eef0Srtr return EOPNOTSUPP;
4506dd8eef0Srtr }
4516dd8eef0Srtr
4526dd8eef0Srtr static int
hci_connect(struct socket * so,struct sockaddr * nam,struct lwp * l)453fd12cf39Srtr hci_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
454ad6ae402Srtr {
455ad6ae402Srtr struct hci_pcb *pcb = so->so_pcb;
456fd12cf39Srtr struct sockaddr_bt *sa = (struct sockaddr_bt *)nam;
457ad6ae402Srtr
458ad6ae402Srtr KASSERT(solocked(so));
459ad6ae402Srtr KASSERT(pcb != NULL);
460ad6ae402Srtr KASSERT(nam != NULL);
461ad6ae402Srtr
462ad6ae402Srtr if (sa->bt_len != sizeof(struct sockaddr_bt))
463ad6ae402Srtr return EINVAL;
464ad6ae402Srtr
465ad6ae402Srtr if (sa->bt_family != AF_BLUETOOTH)
466ad6ae402Srtr return EAFNOSUPPORT;
467ad6ae402Srtr
468ad6ae402Srtr if (hci_unit_lookup(&sa->bt_bdaddr) == NULL)
469ad6ae402Srtr return EADDRNOTAVAIL;
470ad6ae402Srtr
471ad6ae402Srtr bdaddr_copy(&pcb->hp_raddr, &sa->bt_bdaddr);
472ad6ae402Srtr soisconnected(so);
473ad6ae402Srtr return 0;
474ad6ae402Srtr }
475ad6ae402Srtr
476ad6ae402Srtr static int
hci_connect2(struct socket * so,struct socket * so2)4778cf67cc6Srtr hci_connect2(struct socket *so, struct socket *so2)
4788cf67cc6Srtr {
4798cf67cc6Srtr KASSERT(solocked(so));
4808cf67cc6Srtr
4818cf67cc6Srtr return EOPNOTSUPP;
4828cf67cc6Srtr }
4838cf67cc6Srtr
4848cf67cc6Srtr static int
hci_disconnect(struct socket * so)485892163b8Srtr hci_disconnect(struct socket *so)
486892163b8Srtr {
487892163b8Srtr struct hci_pcb *pcb = so->so_pcb;
488892163b8Srtr
489892163b8Srtr KASSERT(solocked(so));
490892163b8Srtr KASSERT(pcb != NULL);
491892163b8Srtr
492892163b8Srtr bdaddr_copy(&pcb->hp_raddr, BDADDR_ANY);
493892163b8Srtr
494892163b8Srtr /* XXX we cannot call soisdisconnected() here, as it sets
495892163b8Srtr * SS_CANTRCVMORE and SS_CANTSENDMORE. The problem being,
496892163b8Srtr * that soisconnected() does not clear these and if you
497892163b8Srtr * try to reconnect this socket (which is permitted) you
498892163b8Srtr * get a broken pipe when you try to write any data.
499892163b8Srtr */
500892163b8Srtr so->so_state &= ~SS_ISCONNECTED;
501892163b8Srtr return 0;
502892163b8Srtr }
503892163b8Srtr
504892163b8Srtr static int
hci_shutdown(struct socket * so)505892163b8Srtr hci_shutdown(struct socket *so)
506892163b8Srtr {
507892163b8Srtr KASSERT(solocked(so));
508892163b8Srtr
509892163b8Srtr socantsendmore(so);
510892163b8Srtr return 0;
511892163b8Srtr }
512892163b8Srtr
513892163b8Srtr static int
hci_abort(struct socket * so)514892163b8Srtr hci_abort(struct socket *so)
515892163b8Srtr {
516892163b8Srtr KASSERT(solocked(so));
517892163b8Srtr
518892163b8Srtr soisdisconnected(so);
519892163b8Srtr hci_detach(so);
520892163b8Srtr return 0;
521892163b8Srtr }
522892163b8Srtr
523892163b8Srtr static int
hci_ioctl(struct socket * so,u_long cmd,void * nam,struct ifnet * ifp)524ff90c29dSrtr hci_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
525d54d7ab2Srtr {
526d54d7ab2Srtr int err;
527d54d7ab2Srtr mutex_enter(bt_lock);
5280dedd977Srtr err = hci_ioctl_pcb(cmd, nam);
529d54d7ab2Srtr mutex_exit(bt_lock);
530d54d7ab2Srtr return err;
531d54d7ab2Srtr }
532d54d7ab2Srtr
533a60320caSrtr static int
hci_stat(struct socket * so,struct stat * ub)534a60320caSrtr hci_stat(struct socket *so, struct stat *ub)
535a60320caSrtr {
536ff90c29dSrtr KASSERT(solocked(so));
537ff90c29dSrtr
538909a1fc6Srtr return 0;
539a60320caSrtr }
540a60320caSrtr
541d575eb54Srtr static int
hci_peeraddr(struct socket * so,struct sockaddr * nam)542eddf3af3Srtr hci_peeraddr(struct socket *so, struct sockaddr *nam)
543d575eb54Srtr {
544d575eb54Srtr struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
545eddf3af3Srtr struct sockaddr_bt *sa = (struct sockaddr_bt *)nam;
546d575eb54Srtr
547d575eb54Srtr KASSERT(solocked(so));
548d575eb54Srtr KASSERT(pcb != NULL);
549d575eb54Srtr KASSERT(nam != NULL);
550d575eb54Srtr
551d575eb54Srtr memset(sa, 0, sizeof(struct sockaddr_bt));
552d575eb54Srtr sa->bt_len = sizeof(struct sockaddr_bt);
553d575eb54Srtr sa->bt_family = AF_BLUETOOTH;
554d575eb54Srtr bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_raddr);
555d575eb54Srtr return 0;
556d575eb54Srtr }
557d575eb54Srtr
558d575eb54Srtr static int
hci_sockaddr(struct socket * so,struct sockaddr * nam)559eddf3af3Srtr hci_sockaddr(struct socket *so, struct sockaddr *nam)
560d575eb54Srtr {
561d575eb54Srtr struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
562eddf3af3Srtr struct sockaddr_bt *sa = (struct sockaddr_bt *)nam;
563d575eb54Srtr
564d575eb54Srtr KASSERT(solocked(so));
565d575eb54Srtr KASSERT(pcb != NULL);
566d575eb54Srtr KASSERT(nam != NULL);
567d575eb54Srtr
568d575eb54Srtr memset(sa, 0, sizeof(struct sockaddr_bt));
569d575eb54Srtr sa->bt_len = sizeof(struct sockaddr_bt);
570d575eb54Srtr sa->bt_family = AF_BLUETOOTH;
571d575eb54Srtr bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_laddr);
572d575eb54Srtr return 0;
573d575eb54Srtr }
574d575eb54Srtr
57535b22fa9Srtr static int
hci_rcvd(struct socket * so,int flags,struct lwp * l)576822872eaSrtr hci_rcvd(struct socket *so, int flags, struct lwp *l)
577822872eaSrtr {
578822872eaSrtr KASSERT(solocked(so));
579822872eaSrtr
580822872eaSrtr return EOPNOTSUPP;
581822872eaSrtr }
582822872eaSrtr
583822872eaSrtr static int
hci_recvoob(struct socket * so,struct mbuf * m,int flags)58435b22fa9Srtr hci_recvoob(struct socket *so, struct mbuf *m, int flags)
58535b22fa9Srtr {
58635b22fa9Srtr KASSERT(solocked(so));
58735b22fa9Srtr
58835b22fa9Srtr return EOPNOTSUPP;
58935b22fa9Srtr }
59035b22fa9Srtr
59135b22fa9Srtr static int
hci_send(struct socket * so,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct lwp * l)592fd12cf39Srtr hci_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
593651e5bd3Srtr struct mbuf *control, struct lwp *l)
594651e5bd3Srtr {
595651e5bd3Srtr struct hci_pcb *pcb = so->so_pcb;
596fd12cf39Srtr struct sockaddr_bt *sa = (struct sockaddr_bt *)nam;
5970f446f14Splunky struct hci_unit *unit;
5980f446f14Splunky struct mbuf *m0;
5990f446f14Splunky hci_cmd_hdr_t hdr;
600651e5bd3Srtr int err = 0;
601651e5bd3Srtr
602651e5bd3Srtr KASSERT(solocked(so));
603651e5bd3Srtr KASSERT(pcb != NULL);
6040f446f14Splunky KASSERT(m != NULL);
605651e5bd3Srtr
606*481d3881Srin m_freem(control); /* have no use for this */
607651e5bd3Srtr
6080f446f14Splunky if (sa) {
609651e5bd3Srtr if (sa->bt_len != sizeof(struct sockaddr_bt)) {
610651e5bd3Srtr err = EINVAL;
6110f446f14Splunky goto bad;
612651e5bd3Srtr }
613651e5bd3Srtr
614651e5bd3Srtr if (sa->bt_family != AF_BLUETOOTH) {
615651e5bd3Srtr err = EAFNOSUPPORT;
6160f446f14Splunky goto bad;
617651e5bd3Srtr }
618651e5bd3Srtr }
619651e5bd3Srtr
6200f446f14Splunky /*
6210f446f14Splunky * this came from userland, so we check it out first
6220f446f14Splunky */
623651e5bd3Srtr
6240f446f14Splunky /* wants at least a header to start with */
6250f446f14Splunky if (m->m_pkthdr.len < sizeof(hdr)) {
6260f446f14Splunky err = EMSGSIZE;
6270f446f14Splunky goto bad;
6280f446f14Splunky }
6290f446f14Splunky m_copydata(m, 0, sizeof(hdr), &hdr);
6300f446f14Splunky hdr.opcode = le16toh(hdr.opcode);
6310f446f14Splunky
6320f446f14Splunky /* only allows CMD packets to be sent */
6330f446f14Splunky if (hdr.type != HCI_CMD_PKT) {
6340f446f14Splunky err = EINVAL;
6350f446f14Splunky goto bad;
6360f446f14Splunky }
6370f446f14Splunky
6380f446f14Splunky /* validates packet length */
6390f446f14Splunky if (m->m_pkthdr.len != sizeof(hdr) + hdr.length) {
6400f446f14Splunky err = EMSGSIZE;
6410f446f14Splunky goto bad;
6420f446f14Splunky }
6430f446f14Splunky
6440f446f14Splunky /* finds destination */
6450f446f14Splunky unit = hci_unit_lookup((sa ? &sa->bt_bdaddr : &pcb->hp_raddr));
6460f446f14Splunky if (unit == NULL) {
6470f446f14Splunky err = ENETDOWN;
6480f446f14Splunky goto bad;
6490f446f14Splunky }
6500f446f14Splunky
6510f446f14Splunky /* security checks for unprivileged users */
6520f446f14Splunky if (pcb->hp_cred != NULL
6530f446f14Splunky && kauth_authorize_device(pcb->hp_cred,
6540f446f14Splunky KAUTH_DEVICE_BLUETOOTH_SEND,
6550f446f14Splunky unit, &hdr, NULL, NULL) != 0) {
6560f446f14Splunky err = EPERM;
6570f446f14Splunky goto bad;
6580f446f14Splunky }
6590f446f14Splunky
6600f446f14Splunky /* makess a copy for precious to keep */
6610f446f14Splunky m0 = m_copypacket(m, M_DONTWAIT);
6620f446f14Splunky if (m0 == NULL) {
6630f446f14Splunky err = ENOMEM;
6640f446f14Splunky goto bad;
6650f446f14Splunky }
6660f446f14Splunky sbappendrecord(&pcb->hp_socket->so_snd, m0);
6670f446f14Splunky M_SETCTX(m, pcb->hp_socket); /* enable drop callback */
6680f446f14Splunky
6690f446f14Splunky DPRINTFN(2, "(%s) opcode (%03x|%04x)\n", device_xname(unit->hci_dev),
6700f446f14Splunky HCI_OGF(hdr.opcode), HCI_OCF(hdr.opcode));
6710f446f14Splunky
6720f446f14Splunky /* Sendss it */
6730f446f14Splunky if (unit->hci_num_cmd_pkts == 0)
6740f446f14Splunky MBUFQ_ENQUEUE(&unit->hci_cmdwait, m);
6750f446f14Splunky else
6760f446f14Splunky hci_output_cmd(unit, m);
6770f446f14Splunky
6780f446f14Splunky return 0;
6790f446f14Splunky
6800f446f14Splunky bad:
6810f446f14Splunky DPRINTF("packet (%d bytes) not sent (error %d)\n",
6820f446f14Splunky m->m_pkthdr.len, err);
683651e5bd3Srtr m_freem(m);
684651e5bd3Srtr
685651e5bd3Srtr return err;
686651e5bd3Srtr }
687651e5bd3Srtr
688651e5bd3Srtr static int
hci_sendoob(struct socket * so,struct mbuf * m,struct mbuf * control)68935b22fa9Srtr hci_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
69035b22fa9Srtr {
69135b22fa9Srtr KASSERT(solocked(so));
69235b22fa9Srtr
69335b22fa9Srtr m_freem(m);
69435b22fa9Srtr m_freem(control);
69535b22fa9Srtr
69635b22fa9Srtr return EOPNOTSUPP;
69735b22fa9Srtr }
69835b22fa9Srtr
6998cf67cc6Srtr static int
hci_purgeif(struct socket * so,struct ifnet * ifp)7008cf67cc6Srtr hci_purgeif(struct socket *so, struct ifnet *ifp)
7018cf67cc6Srtr {
7028cf67cc6Srtr
7038cf67cc6Srtr return EOPNOTSUPP;
7048cf67cc6Srtr }
7058cf67cc6Srtr
706a5c89047Sgdamore /*
707a5c89047Sgdamore * get/set socket options
708a5c89047Sgdamore */
709a5c89047Sgdamore int
hci_ctloutput(int req,struct socket * so,struct sockopt * sopt)710fd7356a9Splunky hci_ctloutput(int req, struct socket *so, struct sockopt *sopt)
711a5c89047Sgdamore {
712a5c89047Sgdamore struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
713fd7356a9Splunky int optval, err = 0;
714a5c89047Sgdamore
715a5c89047Sgdamore DPRINTFN(2, "req %s\n", prcorequests[req]);
716a5c89047Sgdamore
717a5c89047Sgdamore if (pcb == NULL)
718a5c89047Sgdamore return EINVAL;
719a5c89047Sgdamore
720fd7356a9Splunky if (sopt->sopt_level != BTPROTO_HCI)
721162ec756Splunky return ENOPROTOOPT;
722a5c89047Sgdamore
723a5c89047Sgdamore switch(req) {
724a5c89047Sgdamore case PRCO_GETOPT:
725fd7356a9Splunky switch (sopt->sopt_name) {
726a5c89047Sgdamore case SO_HCI_EVT_FILTER:
727fd7356a9Splunky err = sockopt_set(sopt, &pcb->hp_efilter,
728fd7356a9Splunky sizeof(struct hci_filter));
729fd7356a9Splunky
730a5c89047Sgdamore break;
731a5c89047Sgdamore
732a5c89047Sgdamore case SO_HCI_PKT_FILTER:
733fd7356a9Splunky err = sockopt_set(sopt, &pcb->hp_pfilter,
734fd7356a9Splunky sizeof(struct hci_filter));
735fd7356a9Splunky
736a5c89047Sgdamore break;
737a5c89047Sgdamore
738a5c89047Sgdamore case SO_HCI_DIRECTION:
739fd7356a9Splunky err = sockopt_setint(sopt,
740fd7356a9Splunky (pcb->hp_flags & HCI_DIRECTION ? 1 : 0));
741fd7356a9Splunky
742a5c89047Sgdamore break;
743a5c89047Sgdamore
744a5c89047Sgdamore default:
745162ec756Splunky err = ENOPROTOOPT;
746a5c89047Sgdamore break;
747a5c89047Sgdamore }
748a5c89047Sgdamore break;
749a5c89047Sgdamore
750a5c89047Sgdamore case PRCO_SETOPT:
751fd7356a9Splunky switch (sopt->sopt_name) {
752a5c89047Sgdamore case SO_HCI_EVT_FILTER: /* set event filter */
753fd7356a9Splunky err = sockopt_get(sopt, &pcb->hp_efilter,
754fd7356a9Splunky sizeof(pcb->hp_efilter));
755fd7356a9Splunky
756a5c89047Sgdamore break;
757a5c89047Sgdamore
758a5c89047Sgdamore case SO_HCI_PKT_FILTER: /* set packet filter */
759fd7356a9Splunky err = sockopt_get(sopt, &pcb->hp_pfilter,
760fd7356a9Splunky sizeof(pcb->hp_pfilter));
761fd7356a9Splunky
762a5c89047Sgdamore break;
763a5c89047Sgdamore
764a5c89047Sgdamore case SO_HCI_DIRECTION: /* request direction ctl messages */
765fd7356a9Splunky err = sockopt_getint(sopt, &optval);
766fd7356a9Splunky if (err)
767fd7356a9Splunky break;
768fd7356a9Splunky
769fd7356a9Splunky if (optval)
770a5c89047Sgdamore pcb->hp_flags |= HCI_DIRECTION;
771a5c89047Sgdamore else
772a5c89047Sgdamore pcb->hp_flags &= ~HCI_DIRECTION;
773a5c89047Sgdamore break;
774a5c89047Sgdamore
775a5c89047Sgdamore default:
776162ec756Splunky err = ENOPROTOOPT;
777a5c89047Sgdamore break;
778a5c89047Sgdamore }
779a5c89047Sgdamore break;
780a5c89047Sgdamore
781a5c89047Sgdamore default:
782162ec756Splunky err = ENOPROTOOPT;
783a5c89047Sgdamore break;
784a5c89047Sgdamore }
785a5c89047Sgdamore
786a5c89047Sgdamore return err;
787a5c89047Sgdamore }
788a5c89047Sgdamore
789a5c89047Sgdamore /*
790a5c89047Sgdamore * HCI mbuf tap routine
791a5c89047Sgdamore *
792a5c89047Sgdamore * copy packets to any raw HCI sockets that wish (and are
793a5c89047Sgdamore * permitted) to see them
794a5c89047Sgdamore */
795a5c89047Sgdamore void
hci_mtap(struct mbuf * m,struct hci_unit * unit)796a5c89047Sgdamore hci_mtap(struct mbuf *m, struct hci_unit *unit)
797a5c89047Sgdamore {
798a5c89047Sgdamore struct hci_pcb *pcb;
799a5c89047Sgdamore struct mbuf *m0, *ctlmsg, **ctl;
800a5c89047Sgdamore struct sockaddr_bt sa;
801a5c89047Sgdamore uint8_t type;
802a5c89047Sgdamore uint8_t event;
8034f6ac133Splunky uint16_t arg1;
804a5c89047Sgdamore
805a5c89047Sgdamore KASSERT(m->m_len >= sizeof(type));
806a5c89047Sgdamore
807a5c89047Sgdamore type = *mtod(m, uint8_t *);
808a5c89047Sgdamore
809a5c89047Sgdamore memset(&sa, 0, sizeof(sa));
810a5c89047Sgdamore sa.bt_len = sizeof(struct sockaddr_bt);
811a5c89047Sgdamore sa.bt_family = AF_BLUETOOTH;
812a5c89047Sgdamore bdaddr_copy(&sa.bt_bdaddr, &unit->hci_bdaddr);
813a5c89047Sgdamore
814a5c89047Sgdamore LIST_FOREACH(pcb, &hci_pcb, hp_next) {
815a5c89047Sgdamore /*
816a5c89047Sgdamore * filter according to source address
817a5c89047Sgdamore */
818a5c89047Sgdamore if ((pcb->hp_flags & HCI_PROMISCUOUS) == 0
819a5c89047Sgdamore && bdaddr_same(&pcb->hp_laddr, &sa.bt_bdaddr) == 0)
820a5c89047Sgdamore continue;
821a5c89047Sgdamore
822a5c89047Sgdamore /*
823a5c89047Sgdamore * filter according to packet type filter
824a5c89047Sgdamore */
825a5c89047Sgdamore if (hci_filter_test(type, &pcb->hp_pfilter) == 0)
826a5c89047Sgdamore continue;
827a5c89047Sgdamore
828a5c89047Sgdamore /*
829a5c89047Sgdamore * filter according to event/security filters
830a5c89047Sgdamore */
831a5c89047Sgdamore switch(type) {
832a5c89047Sgdamore case HCI_EVENT_PKT:
833a5c89047Sgdamore KASSERT(m->m_len >= sizeof(hci_event_hdr_t));
834a5c89047Sgdamore
835a5c89047Sgdamore event = mtod(m, hci_event_hdr_t *)->event;
836a5c89047Sgdamore
837a5c89047Sgdamore if (hci_filter_test(event, &pcb->hp_efilter) == 0)
838a5c89047Sgdamore continue;
839a5c89047Sgdamore
8404f6ac133Splunky arg1 = event;
841a5c89047Sgdamore break;
842a5c89047Sgdamore
843a5c89047Sgdamore case HCI_CMD_PKT:
844a5c89047Sgdamore KASSERT(m->m_len >= sizeof(hci_cmd_hdr_t));
8454f6ac133Splunky arg1 = le16toh(mtod(m, hci_cmd_hdr_t *)->opcode);
846a5c89047Sgdamore break;
847a5c89047Sgdamore
848a5c89047Sgdamore case HCI_ACL_DATA_PKT:
8494f6ac133Splunky KASSERT(m->m_len >= sizeof(hci_acldata_hdr_t));
8504f6ac133Splunky arg1 = le16toh(mtod(m, hci_acldata_hdr_t *)->con_handle);
8514f6ac133Splunky arg1 = HCI_CON_HANDLE(arg1);
8524f6ac133Splunky break;
853a5c89047Sgdamore
8544f6ac133Splunky case HCI_SCO_DATA_PKT:
8554f6ac133Splunky KASSERT(m->m_len >= sizeof(hci_scodata_hdr_t));
8564f6ac133Splunky arg1 = le16toh(mtod(m, hci_scodata_hdr_t *)->con_handle);
8574f6ac133Splunky arg1 = HCI_CON_HANDLE(arg1);
8584f6ac133Splunky break;
8594f6ac133Splunky
8604f6ac133Splunky default:
8614f6ac133Splunky arg1 = 0;
862a5c89047Sgdamore break;
863a5c89047Sgdamore }
864a5c89047Sgdamore
8654f6ac133Splunky if (pcb->hp_cred != NULL
8664f6ac133Splunky && kauth_authorize_device(pcb->hp_cred,
8674f6ac133Splunky KAUTH_DEVICE_BLUETOOTH_RECV,
8684f6ac133Splunky KAUTH_ARG(type), KAUTH_ARG(arg1), NULL, NULL) != 0)
8694f6ac133Splunky continue;
8704f6ac133Splunky
871a5c89047Sgdamore /*
872a5c89047Sgdamore * create control messages
873a5c89047Sgdamore */
874a5c89047Sgdamore ctlmsg = NULL;
875a5c89047Sgdamore ctl = &ctlmsg;
876a5c89047Sgdamore if (pcb->hp_flags & HCI_DIRECTION) {
877a5c89047Sgdamore int dir = m->m_flags & M_LINK0 ? 1 : 0;
878a5c89047Sgdamore
8798e15db75Splunky *ctl = sbcreatecontrol(&dir, sizeof(dir),
880a5c89047Sgdamore SCM_HCI_DIRECTION, BTPROTO_HCI);
881a5c89047Sgdamore
882a5c89047Sgdamore if (*ctl != NULL)
883a5c89047Sgdamore ctl = &((*ctl)->m_next);
884a5c89047Sgdamore }
885dcea4289Splunky if (pcb->hp_socket->so_options & SO_TIMESTAMP) {
886dcea4289Splunky struct timeval tv;
887dcea4289Splunky
888dcea4289Splunky microtime(&tv);
889dcea4289Splunky *ctl = sbcreatecontrol(&tv, sizeof(tv),
890dcea4289Splunky SCM_TIMESTAMP, SOL_SOCKET);
891dcea4289Splunky
892dcea4289Splunky if (*ctl != NULL)
893dcea4289Splunky ctl = &((*ctl)->m_next);
894dcea4289Splunky }
895a5c89047Sgdamore
896a5c89047Sgdamore /*
897a5c89047Sgdamore * copy to socket
898a5c89047Sgdamore */
899a5c89047Sgdamore m0 = m_copypacket(m, M_DONTWAIT);
900a5c89047Sgdamore if (m0 && sbappendaddr(&pcb->hp_socket->so_rcv,
901a5c89047Sgdamore (struct sockaddr *)&sa, m0, ctlmsg)) {
902a5c89047Sgdamore sorwakeup(pcb->hp_socket);
903a5c89047Sgdamore } else {
904a5c89047Sgdamore m_freem(ctlmsg);
905a5c89047Sgdamore m_freem(m0);
906a5c89047Sgdamore }
907a5c89047Sgdamore }
908a5c89047Sgdamore }
90939bd8deeSrmind
910e401453fSrmind PR_WRAP_USRREQS(hci)
91139bd8deeSrmind
912e401453fSrmind #define hci_attach hci_attach_wrapper
913e401453fSrmind #define hci_detach hci_detach_wrapper
914d27b133dSrtr #define hci_accept hci_accept_wrapper
9156dd8eef0Srtr #define hci_bind hci_bind_wrapper
9166dd8eef0Srtr #define hci_listen hci_listen_wrapper
917ad6ae402Srtr #define hci_connect hci_connect_wrapper
9188cf67cc6Srtr #define hci_connect2 hci_connect2_wrapper
919892163b8Srtr #define hci_disconnect hci_disconnect_wrapper
920892163b8Srtr #define hci_shutdown hci_shutdown_wrapper
921892163b8Srtr #define hci_abort hci_abort_wrapper
922d54d7ab2Srtr #define hci_ioctl hci_ioctl_wrapper
923a60320caSrtr #define hci_stat hci_stat_wrapper
924d575eb54Srtr #define hci_peeraddr hci_peeraddr_wrapper
925d575eb54Srtr #define hci_sockaddr hci_sockaddr_wrapper
926822872eaSrtr #define hci_rcvd hci_rcvd_wrapper
92735b22fa9Srtr #define hci_recvoob hci_recvoob_wrapper
928651e5bd3Srtr #define hci_send hci_send_wrapper
92935b22fa9Srtr #define hci_sendoob hci_sendoob_wrapper
9308cf67cc6Srtr #define hci_purgeif hci_purgeif_wrapper
93139bd8deeSrmind
93239bd8deeSrmind const struct pr_usrreqs hci_usrreqs = {
93356a73a7dSrmind .pr_attach = hci_attach,
93456a73a7dSrmind .pr_detach = hci_detach,
935d27b133dSrtr .pr_accept = hci_accept,
9366dd8eef0Srtr .pr_bind = hci_bind,
9376dd8eef0Srtr .pr_listen = hci_listen,
938ad6ae402Srtr .pr_connect = hci_connect,
9398cf67cc6Srtr .pr_connect2 = hci_connect2,
940892163b8Srtr .pr_disconnect = hci_disconnect,
941892163b8Srtr .pr_shutdown = hci_shutdown,
942892163b8Srtr .pr_abort = hci_abort,
943d54d7ab2Srtr .pr_ioctl = hci_ioctl,
944a60320caSrtr .pr_stat = hci_stat,
945d575eb54Srtr .pr_peeraddr = hci_peeraddr,
946d575eb54Srtr .pr_sockaddr = hci_sockaddr,
947822872eaSrtr .pr_rcvd = hci_rcvd,
94835b22fa9Srtr .pr_recvoob = hci_recvoob,
949651e5bd3Srtr .pr_send = hci_send,
95035b22fa9Srtr .pr_sendoob = hci_sendoob,
9518cf67cc6Srtr .pr_purgeif = hci_purgeif,
95239bd8deeSrmind };
953