hci_socket.c revision 1.17 1 1.17 plunky /* $NetBSD: hci_socket.c,v 1.17 2008/08/06 15:01:24 plunky 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.17 plunky __KERNEL_RCSID(0, "$NetBSD: hci_socket.c,v 1.17 2008/08/06 15:01:24 plunky Exp $");
35 1.1 gdamore
36 1.10 plunky /* load symbolic names */
37 1.1 gdamore #ifdef BLUETOOTH_DEBUG
38 1.10 plunky #define PRUREQUESTS
39 1.1 gdamore #define PRCOREQUESTS
40 1.1 gdamore #endif
41 1.1 gdamore
42 1.1 gdamore #include <sys/param.h>
43 1.1 gdamore #include <sys/domain.h>
44 1.1 gdamore #include <sys/kauth.h>
45 1.1 gdamore #include <sys/kernel.h>
46 1.1 gdamore #include <sys/mbuf.h>
47 1.1 gdamore #include <sys/proc.h>
48 1.1 gdamore #include <sys/protosw.h>
49 1.1 gdamore #include <sys/socket.h>
50 1.1 gdamore #include <sys/socketvar.h>
51 1.1 gdamore #include <sys/systm.h>
52 1.1 gdamore
53 1.1 gdamore #include <netbt/bluetooth.h>
54 1.1 gdamore #include <netbt/hci.h>
55 1.1 gdamore
56 1.1 gdamore /*******************************************************************************
57 1.1 gdamore *
58 1.1 gdamore * HCI SOCK_RAW Sockets - for control of Bluetooth Devices
59 1.1 gdamore *
60 1.1 gdamore */
61 1.1 gdamore
62 1.1 gdamore /*
63 1.1 gdamore * the raw HCI protocol control block
64 1.1 gdamore */
65 1.1 gdamore struct hci_pcb {
66 1.1 gdamore struct socket *hp_socket; /* socket */
67 1.1 gdamore unsigned int hp_flags; /* flags */
68 1.1 gdamore bdaddr_t hp_laddr; /* local address */
69 1.1 gdamore bdaddr_t hp_raddr; /* remote address */
70 1.1 gdamore struct hci_filter hp_efilter; /* user event filter */
71 1.1 gdamore struct hci_filter hp_pfilter; /* user packet filter */
72 1.1 gdamore LIST_ENTRY(hci_pcb) hp_next; /* next HCI pcb */
73 1.1 gdamore };
74 1.1 gdamore
75 1.1 gdamore /* hp_flags */
76 1.1 gdamore #define HCI_PRIVILEGED (1<<0) /* no security filter for root */
77 1.1 gdamore #define HCI_DIRECTION (1<<1) /* direction control messages */
78 1.1 gdamore #define HCI_PROMISCUOUS (1<<2) /* listen to all units */
79 1.1 gdamore
80 1.1 gdamore LIST_HEAD(hci_pcb_list, hci_pcb) hci_pcb = LIST_HEAD_INITIALIZER(hci_pcb);
81 1.1 gdamore
82 1.1 gdamore /* sysctl defaults */
83 1.1 gdamore int hci_sendspace = HCI_CMD_PKT_SIZE;
84 1.1 gdamore int hci_recvspace = 4096;
85 1.1 gdamore
86 1.13 plunky /* supported commands opcode table */
87 1.13 plunky static const struct {
88 1.13 plunky uint16_t opcode;
89 1.13 plunky uint8_t offs; /* 0 - 63 */
90 1.13 plunky uint8_t mask; /* bit 0 - 7 */
91 1.13 plunky int16_t length; /* -1 if privileged */
92 1.13 plunky } hci_cmds[] = {
93 1.13 plunky { HCI_CMD_INQUIRY,
94 1.13 plunky 0, 0x01, sizeof(hci_inquiry_cp) },
95 1.13 plunky { HCI_CMD_INQUIRY_CANCEL,
96 1.13 plunky 0, 0x02, -1 },
97 1.13 plunky { HCI_CMD_PERIODIC_INQUIRY,
98 1.13 plunky 0, 0x04, -1 },
99 1.13 plunky { HCI_CMD_EXIT_PERIODIC_INQUIRY,
100 1.13 plunky 0, 0x08, -1 },
101 1.13 plunky { HCI_CMD_CREATE_CON,
102 1.13 plunky 0, 0x10, -1 },
103 1.13 plunky { HCI_CMD_DISCONNECT,
104 1.13 plunky 0, 0x20, -1 },
105 1.13 plunky { HCI_CMD_ADD_SCO_CON,
106 1.13 plunky 0, 0x40, -1 },
107 1.13 plunky { HCI_CMD_CREATE_CON_CANCEL,
108 1.13 plunky 0, 0x80, -1 },
109 1.13 plunky { HCI_CMD_ACCEPT_CON,
110 1.13 plunky 1, 0x01, -1 },
111 1.13 plunky { HCI_CMD_REJECT_CON,
112 1.13 plunky 1, 0x02, -1 },
113 1.13 plunky { HCI_CMD_LINK_KEY_REP,
114 1.13 plunky 1, 0x04, -1 },
115 1.13 plunky { HCI_CMD_LINK_KEY_NEG_REP,
116 1.13 plunky 1, 0x08, -1 },
117 1.13 plunky { HCI_CMD_PIN_CODE_REP,
118 1.13 plunky 1, 0x10, -1 },
119 1.13 plunky { HCI_CMD_PIN_CODE_NEG_REP,
120 1.13 plunky 1, 0x20, -1 },
121 1.13 plunky { HCI_CMD_CHANGE_CON_PACKET_TYPE,
122 1.13 plunky 1, 0x40, -1 },
123 1.13 plunky { HCI_CMD_AUTH_REQ,
124 1.13 plunky 1, 0x80, -1 },
125 1.13 plunky { HCI_CMD_SET_CON_ENCRYPTION,
126 1.13 plunky 2, 0x01, -1 },
127 1.13 plunky { HCI_CMD_CHANGE_CON_LINK_KEY,
128 1.13 plunky 2, 0x02, -1 },
129 1.13 plunky { HCI_CMD_MASTER_LINK_KEY,
130 1.13 plunky 2, 0x04, -1 },
131 1.13 plunky { HCI_CMD_REMOTE_NAME_REQ,
132 1.13 plunky 2, 0x08, sizeof(hci_remote_name_req_cp) },
133 1.13 plunky { HCI_CMD_REMOTE_NAME_REQ_CANCEL,
134 1.13 plunky 2, 0x10, -1 },
135 1.13 plunky { HCI_CMD_READ_REMOTE_FEATURES,
136 1.13 plunky 2, 0x20, sizeof(hci_read_remote_features_cp) },
137 1.13 plunky { HCI_CMD_READ_REMOTE_EXTENDED_FEATURES,
138 1.13 plunky 2, 0x40, sizeof(hci_read_remote_extended_features_cp) },
139 1.13 plunky { HCI_CMD_READ_REMOTE_VER_INFO,
140 1.13 plunky 2, 0x80, sizeof(hci_read_remote_ver_info_cp) },
141 1.13 plunky { HCI_CMD_READ_CLOCK_OFFSET,
142 1.13 plunky 3, 0x01, sizeof(hci_read_clock_offset_cp) },
143 1.13 plunky { HCI_CMD_READ_LMP_HANDLE,
144 1.13 plunky 3, 0x02, sizeof(hci_read_lmp_handle_cp) },
145 1.13 plunky { HCI_CMD_HOLD_MODE,
146 1.13 plunky 4, 0x02, -1 },
147 1.13 plunky { HCI_CMD_SNIFF_MODE,
148 1.13 plunky 4, 0x04, -1 },
149 1.13 plunky { HCI_CMD_EXIT_SNIFF_MODE,
150 1.13 plunky 4, 0x08, -1 },
151 1.13 plunky { HCI_CMD_PARK_MODE,
152 1.13 plunky 4, 0x10, -1 },
153 1.13 plunky { HCI_CMD_EXIT_PARK_MODE,
154 1.13 plunky 4, 0x20, -1 },
155 1.13 plunky { HCI_CMD_QOS_SETUP,
156 1.13 plunky 4, 0x40, -1 },
157 1.13 plunky { HCI_CMD_ROLE_DISCOVERY,
158 1.13 plunky 4, 0x80, sizeof(hci_role_discovery_cp) },
159 1.13 plunky { HCI_CMD_SWITCH_ROLE,
160 1.13 plunky 5, 0x01, -1 },
161 1.13 plunky { HCI_CMD_READ_LINK_POLICY_SETTINGS,
162 1.13 plunky 5, 0x02, sizeof(hci_read_link_policy_settings_cp) },
163 1.13 plunky { HCI_CMD_WRITE_LINK_POLICY_SETTINGS,
164 1.13 plunky 5, 0x04, -1 },
165 1.13 plunky { HCI_CMD_READ_DEFAULT_LINK_POLICY_SETTINGS,
166 1.13 plunky 5, 0x08, 0 },
167 1.13 plunky { HCI_CMD_WRITE_DEFAULT_LINK_POLICY_SETTINGS,
168 1.13 plunky 5, 0x10, -1 },
169 1.13 plunky { HCI_CMD_FLOW_SPECIFICATION,
170 1.13 plunky 5, 0x20, -1 },
171 1.13 plunky { HCI_CMD_SET_EVENT_MASK,
172 1.13 plunky 5, 0x40, -1 },
173 1.13 plunky { HCI_CMD_RESET,
174 1.13 plunky 5, 0x80, -1 },
175 1.13 plunky { HCI_CMD_SET_EVENT_FILTER,
176 1.13 plunky 6, 0x01, -1 },
177 1.13 plunky { HCI_CMD_FLUSH,
178 1.13 plunky 6, 0x02, -1 },
179 1.13 plunky { HCI_CMD_READ_PIN_TYPE,
180 1.13 plunky 6, 0x04, 0 },
181 1.13 plunky { HCI_CMD_WRITE_PIN_TYPE,
182 1.13 plunky 6, 0x08, -1 },
183 1.13 plunky { HCI_CMD_CREATE_NEW_UNIT_KEY,
184 1.13 plunky 6, 0x10, -1 },
185 1.13 plunky { HCI_CMD_READ_STORED_LINK_KEY,
186 1.13 plunky 6, 0x20, -1 },
187 1.13 plunky { HCI_CMD_WRITE_STORED_LINK_KEY,
188 1.13 plunky 6, 0x40, -1 },
189 1.13 plunky { HCI_CMD_DELETE_STORED_LINK_KEY,
190 1.13 plunky 6, 0x80, -1 },
191 1.13 plunky { HCI_CMD_WRITE_LOCAL_NAME,
192 1.13 plunky 7, 0x01, -1 },
193 1.13 plunky { HCI_CMD_READ_LOCAL_NAME,
194 1.13 plunky 7, 0x02, 0 },
195 1.13 plunky { HCI_CMD_READ_CON_ACCEPT_TIMEOUT,
196 1.13 plunky 7, 0x04, 0 },
197 1.13 plunky { HCI_CMD_WRITE_CON_ACCEPT_TIMEOUT,
198 1.13 plunky 7, 0x08, -1 },
199 1.13 plunky { HCI_CMD_READ_PAGE_TIMEOUT,
200 1.13 plunky 7, 0x10, 0 },
201 1.13 plunky { HCI_CMD_WRITE_PAGE_TIMEOUT,
202 1.13 plunky 7, 0x20, -1 },
203 1.13 plunky { HCI_CMD_READ_SCAN_ENABLE,
204 1.13 plunky 7, 0x40, 0 },
205 1.13 plunky { HCI_CMD_WRITE_SCAN_ENABLE,
206 1.13 plunky 7, 0x80, -1 },
207 1.13 plunky { HCI_CMD_READ_PAGE_SCAN_ACTIVITY,
208 1.13 plunky 8, 0x01, 0 },
209 1.13 plunky { HCI_CMD_WRITE_PAGE_SCAN_ACTIVITY,
210 1.13 plunky 8, 0x02, -1 },
211 1.13 plunky { HCI_CMD_READ_INQUIRY_SCAN_ACTIVITY,
212 1.13 plunky 8, 0x04, 0 },
213 1.13 plunky { HCI_CMD_WRITE_INQUIRY_SCAN_ACTIVITY,
214 1.13 plunky 8, 0x08, -1 },
215 1.13 plunky { HCI_CMD_READ_AUTH_ENABLE,
216 1.13 plunky 8, 0x10, 0 },
217 1.13 plunky { HCI_CMD_WRITE_AUTH_ENABLE,
218 1.13 plunky 8, 0x20, -1 },
219 1.13 plunky { HCI_CMD_READ_ENCRYPTION_MODE,
220 1.13 plunky 8, 0x40, 0 },
221 1.13 plunky { HCI_CMD_WRITE_ENCRYPTION_MODE,
222 1.13 plunky 8, 0x80, -1 },
223 1.13 plunky { HCI_CMD_READ_UNIT_CLASS,
224 1.13 plunky 9, 0x01, 0 },
225 1.13 plunky { HCI_CMD_WRITE_UNIT_CLASS,
226 1.13 plunky 9, 0x02, -1 },
227 1.13 plunky { HCI_CMD_READ_VOICE_SETTING,
228 1.13 plunky 9, 0x04, 0 },
229 1.13 plunky { HCI_CMD_WRITE_VOICE_SETTING,
230 1.13 plunky 9, 0x08, -1 },
231 1.13 plunky { HCI_CMD_READ_AUTO_FLUSH_TIMEOUT,
232 1.13 plunky 9, 0x10, sizeof(hci_read_auto_flush_timeout_cp) },
233 1.13 plunky { HCI_CMD_WRITE_AUTO_FLUSH_TIMEOUT,
234 1.13 plunky 9, 0x20, -1 },
235 1.13 plunky { HCI_CMD_READ_NUM_BROADCAST_RETRANS,
236 1.13 plunky 9, 0x40, 0 },
237 1.13 plunky { HCI_CMD_WRITE_NUM_BROADCAST_RETRANS,
238 1.13 plunky 9, 0x80, -1 },
239 1.13 plunky { HCI_CMD_READ_HOLD_MODE_ACTIVITY,
240 1.13 plunky 10, 0x01, 0 },
241 1.13 plunky { HCI_CMD_WRITE_HOLD_MODE_ACTIVITY,
242 1.13 plunky 10, 0x02, -1 },
243 1.13 plunky { HCI_CMD_READ_XMIT_LEVEL,
244 1.13 plunky 10, 0x04, sizeof(hci_read_xmit_level_cp) },
245 1.13 plunky { HCI_CMD_READ_SCO_FLOW_CONTROL,
246 1.13 plunky 10, 0x08, 0 },
247 1.13 plunky { HCI_CMD_WRITE_SCO_FLOW_CONTROL,
248 1.13 plunky 10, 0x10, -1 },
249 1.13 plunky { HCI_CMD_HC2H_FLOW_CONTROL,
250 1.13 plunky 10, 0x20, -1 },
251 1.13 plunky { HCI_CMD_HOST_BUFFER_SIZE,
252 1.13 plunky 10, 0x40, -1 },
253 1.13 plunky { HCI_CMD_HOST_NUM_COMPL_PKTS,
254 1.13 plunky 10, 0x80, -1 },
255 1.13 plunky { HCI_CMD_READ_LINK_SUPERVISION_TIMEOUT,
256 1.13 plunky 11, 0x01, sizeof(hci_read_link_supervision_timeout_cp) },
257 1.13 plunky { HCI_CMD_WRITE_LINK_SUPERVISION_TIMEOUT,
258 1.13 plunky 11, 0x02, -1 },
259 1.13 plunky { HCI_CMD_READ_NUM_SUPPORTED_IAC,
260 1.13 plunky 11, 0x04, 0 },
261 1.13 plunky { HCI_CMD_READ_IAC_LAP,
262 1.13 plunky 11, 0x08, 0 },
263 1.13 plunky { HCI_CMD_WRITE_IAC_LAP,
264 1.13 plunky 11, 0x10, -1 },
265 1.13 plunky { HCI_CMD_READ_PAGE_SCAN_PERIOD,
266 1.13 plunky 11, 0x20, 0 },
267 1.13 plunky { HCI_CMD_WRITE_PAGE_SCAN_PERIOD,
268 1.13 plunky 11, 0x40, -1 },
269 1.13 plunky { HCI_CMD_READ_PAGE_SCAN,
270 1.13 plunky 11, 0x80, 0 },
271 1.13 plunky { HCI_CMD_WRITE_PAGE_SCAN,
272 1.13 plunky 12, 0x01, -1 },
273 1.13 plunky { HCI_CMD_SET_AFH_CLASSIFICATION,
274 1.13 plunky 12, 0x02, -1 },
275 1.13 plunky { HCI_CMD_READ_INQUIRY_SCAN_TYPE,
276 1.13 plunky 12, 0x10, 0 },
277 1.13 plunky { HCI_CMD_WRITE_INQUIRY_SCAN_TYPE,
278 1.13 plunky 12, 0x20, -1 },
279 1.13 plunky { HCI_CMD_READ_INQUIRY_MODE,
280 1.13 plunky 12, 0x40, 0 },
281 1.13 plunky { HCI_CMD_WRITE_INQUIRY_MODE,
282 1.13 plunky 12, 0x80, -1 },
283 1.13 plunky { HCI_CMD_READ_PAGE_SCAN_TYPE,
284 1.13 plunky 13, 0x01, 0 },
285 1.13 plunky { HCI_CMD_WRITE_PAGE_SCAN_TYPE,
286 1.13 plunky 13, 0x02, -1 },
287 1.13 plunky { HCI_CMD_READ_AFH_ASSESSMENT,
288 1.13 plunky 13, 0x04, 0 },
289 1.13 plunky { HCI_CMD_WRITE_AFH_ASSESSMENT,
290 1.13 plunky 13, 0x08, -1 },
291 1.13 plunky { HCI_CMD_READ_LOCAL_VER,
292 1.13 plunky 14, 0x08, 0 },
293 1.13 plunky { HCI_CMD_READ_LOCAL_COMMANDS,
294 1.13 plunky 14, 0x10, 0 },
295 1.13 plunky { HCI_CMD_READ_LOCAL_FEATURES,
296 1.13 plunky 14, 0x20, 0 },
297 1.13 plunky { HCI_CMD_READ_LOCAL_EXTENDED_FEATURES,
298 1.13 plunky 14, 0x40, sizeof(hci_read_local_extended_features_cp) },
299 1.13 plunky { HCI_CMD_READ_BUFFER_SIZE,
300 1.13 plunky 14, 0x80, 0 },
301 1.13 plunky { HCI_CMD_READ_COUNTRY_CODE,
302 1.13 plunky 15, 0x01, 0 },
303 1.13 plunky { HCI_CMD_READ_BDADDR,
304 1.13 plunky 15, 0x02, 0 },
305 1.13 plunky { HCI_CMD_READ_FAILED_CONTACT_CNTR,
306 1.13 plunky 15, 0x04, sizeof(hci_read_failed_contact_cntr_cp) },
307 1.13 plunky { HCI_CMD_RESET_FAILED_CONTACT_CNTR,
308 1.13 plunky 15, 0x08, -1 },
309 1.13 plunky { HCI_CMD_READ_LINK_QUALITY,
310 1.13 plunky 15, 0x10, sizeof(hci_read_link_quality_cp) },
311 1.13 plunky { HCI_CMD_READ_RSSI,
312 1.13 plunky 15, 0x20, sizeof(hci_read_rssi_cp) },
313 1.13 plunky { HCI_CMD_READ_AFH_CHANNEL_MAP,
314 1.13 plunky 15, 0x40, sizeof(hci_read_afh_channel_map_cp) },
315 1.13 plunky { HCI_CMD_READ_CLOCK,
316 1.13 plunky 15, 0x80, sizeof(hci_read_clock_cp) },
317 1.13 plunky { HCI_CMD_READ_LOOPBACK_MODE,
318 1.13 plunky 16, 0x01, 0 },
319 1.13 plunky { HCI_CMD_WRITE_LOOPBACK_MODE,
320 1.13 plunky 16, 0x02, -1 },
321 1.13 plunky { HCI_CMD_ENABLE_UNIT_UNDER_TEST,
322 1.13 plunky 16, 0x04, -1 },
323 1.13 plunky { HCI_CMD_SETUP_SCO_CON,
324 1.13 plunky 16, 0x08, -1 },
325 1.13 plunky { HCI_CMD_ACCEPT_SCO_CON_REQ,
326 1.13 plunky 16, 0x10, -1 },
327 1.13 plunky { HCI_CMD_REJECT_SCO_CON_REQ,
328 1.13 plunky 16, 0x20, -1 },
329 1.14 plunky { HCI_CMD_READ_EXTENDED_INQUIRY_RSP,
330 1.14 plunky 17, 0x01, 0 },
331 1.14 plunky { HCI_CMD_WRITE_EXTENDED_INQUIRY_RSP,
332 1.14 plunky 17, 0x02, -1 },
333 1.14 plunky { HCI_CMD_REFRESH_ENCRYPTION_KEY,
334 1.14 plunky 17, 0x04, -1 },
335 1.14 plunky { HCI_CMD_SNIFF_SUBRATING,
336 1.14 plunky 17, 0x10, -1 },
337 1.14 plunky { HCI_CMD_READ_SIMPLE_PAIRING_MODE,
338 1.14 plunky 17, 0x20, 0 },
339 1.14 plunky { HCI_CMD_WRITE_SIMPLE_PAIRING_MODE,
340 1.14 plunky 17, 0x40, -1 },
341 1.14 plunky { HCI_CMD_READ_LOCAL_OOB_DATA,
342 1.14 plunky 17, 0x80, -1 },
343 1.14 plunky { HCI_CMD_READ_INQUIRY_RSP_XMIT_POWER,
344 1.14 plunky 18, 0x01, 0 },
345 1.14 plunky { HCI_CMD_WRITE_INQUIRY_RSP_XMIT_POWER,
346 1.14 plunky 18, 0x02, -1 },
347 1.14 plunky { HCI_CMD_READ_DEFAULT_ERRDATA_REPORTING,
348 1.14 plunky 18, 0x04, 0 },
349 1.14 plunky { HCI_CMD_WRITE_DEFAULT_ERRDATA_REPORTING,
350 1.14 plunky 18, 0x08, -1 },
351 1.14 plunky { HCI_CMD_IO_CAPABILITY_REP,
352 1.14 plunky 18, 0x80, -1 },
353 1.14 plunky { HCI_CMD_USER_CONFIRM_REP,
354 1.14 plunky 19, 0x01, -1 },
355 1.14 plunky { HCI_CMD_USER_CONFIRM_NEG_REP,
356 1.14 plunky 19, 0x02, -1 },
357 1.14 plunky { HCI_CMD_USER_PASSKEY_REP,
358 1.14 plunky 19, 0x04, -1 },
359 1.14 plunky { HCI_CMD_USER_PASSKEY_NEG_REP,
360 1.14 plunky 19, 0x08, -1 },
361 1.14 plunky { HCI_CMD_OOB_DATA_REP,
362 1.14 plunky 19, 0x10, -1 },
363 1.14 plunky { HCI_CMD_WRITE_SIMPLE_PAIRING_DEBUG_MODE,
364 1.14 plunky 19, 0x20, -1 },
365 1.14 plunky { HCI_CMD_ENHANCED_FLUSH,
366 1.14 plunky 19, 0x40, -1 },
367 1.14 plunky { HCI_CMD_OOB_DATA_NEG_REP,
368 1.14 plunky 19, 0x80, -1 },
369 1.14 plunky { HCI_CMD_SEND_KEYPRESS_NOTIFICATION,
370 1.14 plunky 20, 0x40, -1 },
371 1.14 plunky { HCI_CMD_IO_CAPABILITY_NEG_REP,
372 1.14 plunky 20, 0x80, -1 },
373 1.13 plunky };
374 1.13 plunky
375 1.1 gdamore /*
376 1.4 plunky * Security filter routines for unprivileged users.
377 1.4 plunky * Allow all but a few critical events, and only permit read commands.
378 1.13 plunky * If a unit is given, verify the command is supported.
379 1.1 gdamore */
380 1.1 gdamore
381 1.4 plunky static int
382 1.13 plunky hci_security_check_opcode(struct hci_unit *unit, uint16_t opcode)
383 1.1 gdamore {
384 1.13 plunky int i;
385 1.13 plunky
386 1.13 plunky for (i = 0 ; i < __arraycount(hci_cmds) ; i++) {
387 1.13 plunky if (opcode != hci_cmds[i].opcode)
388 1.13 plunky continue;
389 1.13 plunky
390 1.13 plunky if (unit == NULL
391 1.13 plunky || (unit->hci_cmds[hci_cmds[i].offs] & hci_cmds[i].mask))
392 1.13 plunky return hci_cmds[i].length;
393 1.1 gdamore
394 1.13 plunky break;
395 1.4 plunky }
396 1.1 gdamore
397 1.13 plunky return -1;
398 1.1 gdamore }
399 1.1 gdamore
400 1.1 gdamore static int
401 1.4 plunky hci_security_check_event(uint8_t event)
402 1.1 gdamore {
403 1.1 gdamore
404 1.4 plunky switch (event) {
405 1.4 plunky case HCI_EVENT_RETURN_LINK_KEYS:
406 1.4 plunky case HCI_EVENT_LINK_KEY_NOTIFICATION:
407 1.14 plunky case HCI_EVENT_USER_CONFIRM_REQ:
408 1.14 plunky case HCI_EVENT_USER_PASSKEY_NOTIFICATION:
409 1.4 plunky case HCI_EVENT_VENDOR:
410 1.8 plunky return -1; /* disallowed */
411 1.4 plunky }
412 1.1 gdamore
413 1.8 plunky return 0; /* ok */
414 1.1 gdamore }
415 1.1 gdamore
416 1.1 gdamore /*
417 1.1 gdamore * When command packet reaches the device, we can drop
418 1.1 gdamore * it from the socket buffer (called from hci_output_acl)
419 1.1 gdamore */
420 1.1 gdamore void
421 1.1 gdamore hci_drop(void *arg)
422 1.1 gdamore {
423 1.1 gdamore struct socket *so = arg;
424 1.1 gdamore
425 1.1 gdamore sbdroprecord(&so->so_snd);
426 1.1 gdamore sowwakeup(so);
427 1.1 gdamore }
428 1.1 gdamore
429 1.1 gdamore /*
430 1.1 gdamore * HCI socket is going away and has some pending packets. We let them
431 1.1 gdamore * go by design, but remove the context pointer as it will be invalid
432 1.1 gdamore * and we no longer need to be notified.
433 1.1 gdamore */
434 1.1 gdamore static void
435 1.1 gdamore hci_cmdwait_flush(struct socket *so)
436 1.1 gdamore {
437 1.1 gdamore struct hci_unit *unit;
438 1.1 gdamore struct socket *ctx;
439 1.1 gdamore struct mbuf *m;
440 1.1 gdamore
441 1.1 gdamore DPRINTF("flushing %p\n", so);
442 1.1 gdamore
443 1.1 gdamore SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
444 1.1 gdamore m = MBUFQ_FIRST(&unit->hci_cmdwait);
445 1.1 gdamore while (m != NULL) {
446 1.1 gdamore ctx = M_GETCTX(m, struct socket *);
447 1.1 gdamore if (ctx == so)
448 1.1 gdamore M_SETCTX(m, NULL);
449 1.1 gdamore
450 1.1 gdamore m = MBUFQ_NEXT(m);
451 1.1 gdamore }
452 1.1 gdamore }
453 1.1 gdamore }
454 1.1 gdamore
455 1.1 gdamore /*
456 1.1 gdamore * HCI send packet
457 1.1 gdamore * This came from userland, so check it out.
458 1.1 gdamore */
459 1.1 gdamore static int
460 1.1 gdamore hci_send(struct hci_pcb *pcb, struct mbuf *m, bdaddr_t *addr)
461 1.1 gdamore {
462 1.1 gdamore struct hci_unit *unit;
463 1.1 gdamore struct mbuf *m0;
464 1.1 gdamore hci_cmd_hdr_t hdr;
465 1.1 gdamore int err;
466 1.1 gdamore
467 1.9 plunky KASSERT(m != NULL);
468 1.9 plunky KASSERT(addr != NULL);
469 1.1 gdamore
470 1.1 gdamore /* wants at least a header to start with */
471 1.1 gdamore if (m->m_pkthdr.len < sizeof(hdr)) {
472 1.1 gdamore err = EMSGSIZE;
473 1.1 gdamore goto bad;
474 1.1 gdamore }
475 1.1 gdamore m_copydata(m, 0, sizeof(hdr), &hdr);
476 1.13 plunky hdr.opcode = le16toh(hdr.opcode);
477 1.1 gdamore
478 1.1 gdamore /* only allows CMD packets to be sent */
479 1.1 gdamore if (hdr.type != HCI_CMD_PKT) {
480 1.1 gdamore err = EINVAL;
481 1.1 gdamore goto bad;
482 1.1 gdamore }
483 1.1 gdamore
484 1.1 gdamore /* validates packet length */
485 1.1 gdamore if (m->m_pkthdr.len != sizeof(hdr) + hdr.length) {
486 1.1 gdamore err = EMSGSIZE;
487 1.1 gdamore goto bad;
488 1.1 gdamore }
489 1.1 gdamore
490 1.1 gdamore /* finds destination */
491 1.1 gdamore unit = hci_unit_lookup(addr);
492 1.1 gdamore if (unit == NULL) {
493 1.1 gdamore err = ENETDOWN;
494 1.1 gdamore goto bad;
495 1.1 gdamore }
496 1.1 gdamore
497 1.13 plunky /* security checks for unprivileged users */
498 1.13 plunky if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
499 1.13 plunky && hci_security_check_opcode(unit, hdr.opcode) != hdr.length) {
500 1.13 plunky err = EPERM;
501 1.13 plunky goto bad;
502 1.13 plunky }
503 1.13 plunky
504 1.1 gdamore /* makess a copy for precious to keep */
505 1.1 gdamore m0 = m_copypacket(m, M_DONTWAIT);
506 1.1 gdamore if (m0 == NULL) {
507 1.1 gdamore err = ENOMEM;
508 1.1 gdamore goto bad;
509 1.1 gdamore }
510 1.1 gdamore sbappendrecord(&pcb->hp_socket->so_snd, m0);
511 1.1 gdamore M_SETCTX(m, pcb->hp_socket); /* enable drop callback */
512 1.1 gdamore
513 1.12 plunky DPRINTFN(2, "(%s) opcode (%03x|%04x)\n", device_xname(unit->hci_dev),
514 1.13 plunky HCI_OGF(hdr.opcode), HCI_OCF(hdr.opcode));
515 1.1 gdamore
516 1.1 gdamore /* Sendss it */
517 1.1 gdamore if (unit->hci_num_cmd_pkts == 0)
518 1.1 gdamore MBUFQ_ENQUEUE(&unit->hci_cmdwait, m);
519 1.1 gdamore else
520 1.1 gdamore hci_output_cmd(unit, m);
521 1.1 gdamore
522 1.1 gdamore return 0;
523 1.1 gdamore
524 1.1 gdamore bad:
525 1.1 gdamore DPRINTF("packet (%d bytes) not sent (error %d)\n",
526 1.1 gdamore m->m_pkthdr.len, err);
527 1.1 gdamore if (m) m_freem(m);
528 1.1 gdamore return err;
529 1.1 gdamore }
530 1.1 gdamore
531 1.1 gdamore /*
532 1.1 gdamore * User Request.
533 1.1 gdamore * up is socket
534 1.1 gdamore * m is either
535 1.1 gdamore * optional mbuf chain containing message
536 1.1 gdamore * ioctl command (PRU_CONTROL)
537 1.1 gdamore * nam is either
538 1.1 gdamore * optional mbuf chain containing an address
539 1.1 gdamore * ioctl data (PRU_CONTROL)
540 1.1 gdamore * optionally, protocol number (PRU_ATTACH)
541 1.1 gdamore * ctl is optional mbuf chain containing socket options
542 1.1 gdamore * l is pointer to process requesting action (if any)
543 1.1 gdamore *
544 1.1 gdamore * we are responsible for disposing of m and ctl if
545 1.1 gdamore * they are mbuf chains
546 1.1 gdamore */
547 1.1 gdamore int
548 1.1 gdamore hci_usrreq(struct socket *up, int req, struct mbuf *m,
549 1.1 gdamore struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
550 1.1 gdamore {
551 1.1 gdamore struct hci_pcb *pcb = (struct hci_pcb *)up->so_pcb;
552 1.1 gdamore struct sockaddr_bt *sa;
553 1.4 plunky int err = 0;
554 1.1 gdamore
555 1.1 gdamore DPRINTFN(2, "%s\n", prurequests[req]);
556 1.1 gdamore
557 1.1 gdamore switch(req) {
558 1.1 gdamore case PRU_CONTROL:
559 1.16 ad mutex_enter(bt_lock);
560 1.16 ad err = hci_ioctl((unsigned long)m, (void *)nam, l);
561 1.16 ad mutex_exit(bt_lock);
562 1.16 ad return err;
563 1.1 gdamore
564 1.1 gdamore case PRU_PURGEIF:
565 1.1 gdamore return EOPNOTSUPP;
566 1.1 gdamore
567 1.1 gdamore case PRU_ATTACH:
568 1.15 ad if (up->so_lock == NULL) {
569 1.15 ad mutex_obj_hold(bt_lock);
570 1.15 ad up->so_lock = bt_lock;
571 1.15 ad solock(up);
572 1.15 ad }
573 1.15 ad KASSERT(solocked(up));
574 1.1 gdamore if (pcb)
575 1.1 gdamore return EINVAL;
576 1.1 gdamore err = soreserve(up, hci_sendspace, hci_recvspace);
577 1.1 gdamore if (err)
578 1.1 gdamore return err;
579 1.1 gdamore
580 1.1 gdamore pcb = malloc(sizeof(struct hci_pcb), M_PCB, M_NOWAIT | M_ZERO);
581 1.1 gdamore if (pcb == NULL)
582 1.1 gdamore return ENOMEM;
583 1.1 gdamore
584 1.1 gdamore up->so_pcb = pcb;
585 1.1 gdamore pcb->hp_socket = up;
586 1.4 plunky
587 1.5 elad if (l == NULL || kauth_authorize_generic(l->l_cred,
588 1.5 elad KAUTH_GENERIC_ISSUSER, NULL) == 0)
589 1.4 plunky pcb->hp_flags |= HCI_PRIVILEGED;
590 1.1 gdamore
591 1.1 gdamore /*
592 1.1 gdamore * Set default user filter. By default, socket only passes
593 1.1 gdamore * Command_Complete and Command_Status Events.
594 1.1 gdamore */
595 1.1 gdamore hci_filter_set(HCI_EVENT_COMMAND_COMPL, &pcb->hp_efilter);
596 1.1 gdamore hci_filter_set(HCI_EVENT_COMMAND_STATUS, &pcb->hp_efilter);
597 1.1 gdamore hci_filter_set(HCI_EVENT_PKT, &pcb->hp_pfilter);
598 1.1 gdamore
599 1.1 gdamore LIST_INSERT_HEAD(&hci_pcb, pcb, hp_next);
600 1.1 gdamore
601 1.1 gdamore return 0;
602 1.1 gdamore }
603 1.1 gdamore
604 1.1 gdamore /* anything after here *requires* a pcb */
605 1.1 gdamore if (pcb == NULL) {
606 1.1 gdamore err = EINVAL;
607 1.1 gdamore goto release;
608 1.1 gdamore }
609 1.1 gdamore
610 1.1 gdamore switch(req) {
611 1.1 gdamore case PRU_DISCONNECT:
612 1.1 gdamore bdaddr_copy(&pcb->hp_raddr, BDADDR_ANY);
613 1.1 gdamore
614 1.1 gdamore /* XXX we cannot call soisdisconnected() here, as it sets
615 1.1 gdamore * SS_CANTRCVMORE and SS_CANTSENDMORE. The problem being,
616 1.1 gdamore * that soisconnected() does not clear these and if you
617 1.1 gdamore * try to reconnect this socket (which is permitted) you
618 1.1 gdamore * get a broken pipe when you try to write any data.
619 1.1 gdamore */
620 1.1 gdamore up->so_state &= ~SS_ISCONNECTED;
621 1.1 gdamore break;
622 1.1 gdamore
623 1.1 gdamore case PRU_ABORT:
624 1.1 gdamore soisdisconnected(up);
625 1.1 gdamore /* fall through to */
626 1.1 gdamore case PRU_DETACH:
627 1.1 gdamore if (up->so_snd.sb_mb != NULL)
628 1.1 gdamore hci_cmdwait_flush(up);
629 1.1 gdamore
630 1.1 gdamore up->so_pcb = NULL;
631 1.1 gdamore LIST_REMOVE(pcb, hp_next);
632 1.1 gdamore free(pcb, M_PCB);
633 1.1 gdamore return 0;
634 1.1 gdamore
635 1.1 gdamore case PRU_BIND:
636 1.9 plunky KASSERT(nam != NULL);
637 1.1 gdamore sa = mtod(nam, struct sockaddr_bt *);
638 1.1 gdamore
639 1.1 gdamore if (sa->bt_len != sizeof(struct sockaddr_bt))
640 1.1 gdamore return EINVAL;
641 1.1 gdamore
642 1.1 gdamore if (sa->bt_family != AF_BLUETOOTH)
643 1.1 gdamore return EAFNOSUPPORT;
644 1.1 gdamore
645 1.1 gdamore bdaddr_copy(&pcb->hp_laddr, &sa->bt_bdaddr);
646 1.1 gdamore
647 1.1 gdamore if (bdaddr_any(&sa->bt_bdaddr))
648 1.1 gdamore pcb->hp_flags |= HCI_PROMISCUOUS;
649 1.1 gdamore else
650 1.1 gdamore pcb->hp_flags &= ~HCI_PROMISCUOUS;
651 1.1 gdamore
652 1.1 gdamore return 0;
653 1.1 gdamore
654 1.1 gdamore case PRU_CONNECT:
655 1.9 plunky KASSERT(nam != NULL);
656 1.1 gdamore sa = mtod(nam, struct sockaddr_bt *);
657 1.1 gdamore
658 1.1 gdamore if (sa->bt_len != sizeof(struct sockaddr_bt))
659 1.1 gdamore return EINVAL;
660 1.1 gdamore
661 1.1 gdamore if (sa->bt_family != AF_BLUETOOTH)
662 1.1 gdamore return EAFNOSUPPORT;
663 1.1 gdamore
664 1.1 gdamore if (hci_unit_lookup(&sa->bt_bdaddr) == NULL)
665 1.1 gdamore return EADDRNOTAVAIL;
666 1.1 gdamore
667 1.1 gdamore bdaddr_copy(&pcb->hp_raddr, &sa->bt_bdaddr);
668 1.1 gdamore soisconnected(up);
669 1.1 gdamore return 0;
670 1.1 gdamore
671 1.1 gdamore case PRU_PEERADDR:
672 1.9 plunky KASSERT(nam != NULL);
673 1.1 gdamore sa = mtod(nam, struct sockaddr_bt *);
674 1.1 gdamore
675 1.1 gdamore memset(sa, 0, sizeof(struct sockaddr_bt));
676 1.1 gdamore nam->m_len =
677 1.1 gdamore sa->bt_len = sizeof(struct sockaddr_bt);
678 1.1 gdamore sa->bt_family = AF_BLUETOOTH;
679 1.1 gdamore bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_raddr);
680 1.1 gdamore return 0;
681 1.1 gdamore
682 1.1 gdamore case PRU_SOCKADDR:
683 1.9 plunky KASSERT(nam != NULL);
684 1.1 gdamore sa = mtod(nam, struct sockaddr_bt *);
685 1.1 gdamore
686 1.1 gdamore memset(sa, 0, sizeof(struct sockaddr_bt));
687 1.1 gdamore nam->m_len =
688 1.1 gdamore sa->bt_len = sizeof(struct sockaddr_bt);
689 1.1 gdamore sa->bt_family = AF_BLUETOOTH;
690 1.1 gdamore bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_laddr);
691 1.1 gdamore return 0;
692 1.1 gdamore
693 1.1 gdamore case PRU_SHUTDOWN:
694 1.1 gdamore socantsendmore(up);
695 1.1 gdamore break;
696 1.1 gdamore
697 1.1 gdamore case PRU_SEND:
698 1.1 gdamore sa = NULL;
699 1.1 gdamore if (nam) {
700 1.1 gdamore sa = mtod(nam, struct sockaddr_bt *);
701 1.1 gdamore
702 1.1 gdamore if (sa->bt_len != sizeof(struct sockaddr_bt)) {
703 1.1 gdamore err = EINVAL;
704 1.1 gdamore goto release;
705 1.1 gdamore }
706 1.1 gdamore
707 1.1 gdamore if (sa->bt_family != AF_BLUETOOTH) {
708 1.1 gdamore err = EAFNOSUPPORT;
709 1.1 gdamore goto release;
710 1.1 gdamore }
711 1.1 gdamore }
712 1.1 gdamore
713 1.1 gdamore if (ctl) /* have no use for this */
714 1.1 gdamore m_freem(ctl);
715 1.1 gdamore
716 1.1 gdamore return hci_send(pcb, m, (sa ? &sa->bt_bdaddr : &pcb->hp_raddr));
717 1.1 gdamore
718 1.1 gdamore case PRU_SENSE:
719 1.1 gdamore return 0; /* (no sense - Doh!) */
720 1.1 gdamore
721 1.1 gdamore case PRU_RCVD:
722 1.1 gdamore case PRU_RCVOOB:
723 1.1 gdamore return EOPNOTSUPP; /* (no release) */
724 1.1 gdamore
725 1.1 gdamore case PRU_ACCEPT:
726 1.1 gdamore case PRU_CONNECT2:
727 1.1 gdamore case PRU_LISTEN:
728 1.1 gdamore case PRU_SENDOOB:
729 1.1 gdamore case PRU_FASTTIMO:
730 1.1 gdamore case PRU_SLOWTIMO:
731 1.1 gdamore case PRU_PROTORCV:
732 1.1 gdamore case PRU_PROTOSEND:
733 1.1 gdamore err = EOPNOTSUPP;
734 1.1 gdamore break;
735 1.1 gdamore
736 1.1 gdamore default:
737 1.1 gdamore UNKNOWN(req);
738 1.1 gdamore err = EOPNOTSUPP;
739 1.1 gdamore break;
740 1.1 gdamore }
741 1.1 gdamore
742 1.1 gdamore release:
743 1.2 ad if (m)
744 1.2 ad m_freem(m);
745 1.2 ad if (ctl)
746 1.2 ad m_freem(ctl);
747 1.1 gdamore return err;
748 1.1 gdamore }
749 1.1 gdamore
750 1.1 gdamore /*
751 1.1 gdamore * get/set socket options
752 1.1 gdamore */
753 1.1 gdamore int
754 1.17 plunky hci_ctloutput(int req, struct socket *so, struct sockopt *sopt)
755 1.1 gdamore {
756 1.1 gdamore struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
757 1.17 plunky int optval, err = 0;
758 1.1 gdamore
759 1.1 gdamore DPRINTFN(2, "req %s\n", prcorequests[req]);
760 1.1 gdamore
761 1.1 gdamore if (pcb == NULL)
762 1.1 gdamore return EINVAL;
763 1.1 gdamore
764 1.17 plunky if (sopt->sopt_level != BTPROTO_HCI)
765 1.7 plunky return ENOPROTOOPT;
766 1.1 gdamore
767 1.1 gdamore switch(req) {
768 1.1 gdamore case PRCO_GETOPT:
769 1.17 plunky switch (sopt->sopt_name) {
770 1.1 gdamore case SO_HCI_EVT_FILTER:
771 1.17 plunky err = sockopt_set(sopt, &pcb->hp_efilter,
772 1.17 plunky sizeof(struct hci_filter));
773 1.17 plunky
774 1.1 gdamore break;
775 1.1 gdamore
776 1.1 gdamore case SO_HCI_PKT_FILTER:
777 1.17 plunky err = sockopt_set(sopt, &pcb->hp_pfilter,
778 1.17 plunky sizeof(struct hci_filter));
779 1.17 plunky
780 1.1 gdamore break;
781 1.1 gdamore
782 1.1 gdamore case SO_HCI_DIRECTION:
783 1.17 plunky err = sockopt_setint(sopt,
784 1.17 plunky (pcb->hp_flags & HCI_DIRECTION ? 1 : 0));
785 1.17 plunky
786 1.1 gdamore break;
787 1.1 gdamore
788 1.1 gdamore default:
789 1.7 plunky err = ENOPROTOOPT;
790 1.1 gdamore break;
791 1.1 gdamore }
792 1.1 gdamore break;
793 1.1 gdamore
794 1.1 gdamore case PRCO_SETOPT:
795 1.17 plunky switch (sopt->sopt_name) {
796 1.1 gdamore case SO_HCI_EVT_FILTER: /* set event filter */
797 1.17 plunky err = sockopt_get(sopt, &pcb->hp_efilter,
798 1.17 plunky sizeof(pcb->hp_efilter));
799 1.17 plunky
800 1.1 gdamore break;
801 1.1 gdamore
802 1.1 gdamore case SO_HCI_PKT_FILTER: /* set packet filter */
803 1.17 plunky err = sockopt_get(sopt, &pcb->hp_pfilter,
804 1.17 plunky sizeof(pcb->hp_pfilter));
805 1.17 plunky
806 1.1 gdamore break;
807 1.1 gdamore
808 1.1 gdamore case SO_HCI_DIRECTION: /* request direction ctl messages */
809 1.17 plunky err = sockopt_getint(sopt, &optval);
810 1.17 plunky if (err)
811 1.17 plunky break;
812 1.17 plunky
813 1.17 plunky if (optval)
814 1.1 gdamore pcb->hp_flags |= HCI_DIRECTION;
815 1.1 gdamore else
816 1.1 gdamore pcb->hp_flags &= ~HCI_DIRECTION;
817 1.1 gdamore break;
818 1.1 gdamore
819 1.1 gdamore default:
820 1.7 plunky err = ENOPROTOOPT;
821 1.1 gdamore break;
822 1.1 gdamore }
823 1.1 gdamore break;
824 1.1 gdamore
825 1.1 gdamore default:
826 1.7 plunky err = ENOPROTOOPT;
827 1.1 gdamore break;
828 1.1 gdamore }
829 1.1 gdamore
830 1.1 gdamore return err;
831 1.1 gdamore }
832 1.1 gdamore
833 1.1 gdamore /*
834 1.1 gdamore * HCI mbuf tap routine
835 1.1 gdamore *
836 1.1 gdamore * copy packets to any raw HCI sockets that wish (and are
837 1.1 gdamore * permitted) to see them
838 1.1 gdamore */
839 1.1 gdamore void
840 1.1 gdamore hci_mtap(struct mbuf *m, struct hci_unit *unit)
841 1.1 gdamore {
842 1.1 gdamore struct hci_pcb *pcb;
843 1.1 gdamore struct mbuf *m0, *ctlmsg, **ctl;
844 1.1 gdamore struct sockaddr_bt sa;
845 1.1 gdamore uint8_t type;
846 1.1 gdamore uint8_t event;
847 1.1 gdamore uint16_t opcode;
848 1.1 gdamore
849 1.1 gdamore KASSERT(m->m_len >= sizeof(type));
850 1.1 gdamore
851 1.1 gdamore type = *mtod(m, uint8_t *);
852 1.1 gdamore
853 1.1 gdamore memset(&sa, 0, sizeof(sa));
854 1.1 gdamore sa.bt_len = sizeof(struct sockaddr_bt);
855 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
856 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &unit->hci_bdaddr);
857 1.1 gdamore
858 1.1 gdamore LIST_FOREACH(pcb, &hci_pcb, hp_next) {
859 1.1 gdamore /*
860 1.1 gdamore * filter according to source address
861 1.1 gdamore */
862 1.1 gdamore if ((pcb->hp_flags & HCI_PROMISCUOUS) == 0
863 1.1 gdamore && bdaddr_same(&pcb->hp_laddr, &sa.bt_bdaddr) == 0)
864 1.1 gdamore continue;
865 1.1 gdamore
866 1.1 gdamore /*
867 1.1 gdamore * filter according to packet type filter
868 1.1 gdamore */
869 1.1 gdamore if (hci_filter_test(type, &pcb->hp_pfilter) == 0)
870 1.1 gdamore continue;
871 1.1 gdamore
872 1.1 gdamore /*
873 1.1 gdamore * filter according to event/security filters
874 1.1 gdamore */
875 1.1 gdamore switch(type) {
876 1.1 gdamore case HCI_EVENT_PKT:
877 1.1 gdamore KASSERT(m->m_len >= sizeof(hci_event_hdr_t));
878 1.1 gdamore
879 1.1 gdamore event = mtod(m, hci_event_hdr_t *)->event;
880 1.1 gdamore
881 1.1 gdamore if (hci_filter_test(event, &pcb->hp_efilter) == 0)
882 1.1 gdamore continue;
883 1.1 gdamore
884 1.1 gdamore if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
885 1.8 plunky && hci_security_check_event(event) == -1)
886 1.1 gdamore continue;
887 1.1 gdamore break;
888 1.1 gdamore
889 1.1 gdamore case HCI_CMD_PKT:
890 1.1 gdamore KASSERT(m->m_len >= sizeof(hci_cmd_hdr_t));
891 1.1 gdamore
892 1.1 gdamore opcode = le16toh(mtod(m, hci_cmd_hdr_t *)->opcode);
893 1.1 gdamore
894 1.1 gdamore if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
895 1.13 plunky && hci_security_check_opcode(NULL, opcode) == -1)
896 1.1 gdamore continue;
897 1.1 gdamore break;
898 1.1 gdamore
899 1.1 gdamore case HCI_ACL_DATA_PKT:
900 1.1 gdamore case HCI_SCO_DATA_PKT:
901 1.1 gdamore default:
902 1.1 gdamore if ((pcb->hp_flags & HCI_PRIVILEGED) == 0)
903 1.1 gdamore continue;
904 1.1 gdamore
905 1.1 gdamore break;
906 1.1 gdamore }
907 1.1 gdamore
908 1.1 gdamore /*
909 1.1 gdamore * create control messages
910 1.1 gdamore */
911 1.1 gdamore ctlmsg = NULL;
912 1.1 gdamore ctl = &ctlmsg;
913 1.1 gdamore if (pcb->hp_flags & HCI_DIRECTION) {
914 1.1 gdamore int dir = m->m_flags & M_LINK0 ? 1 : 0;
915 1.1 gdamore
916 1.11 plunky *ctl = sbcreatecontrol(&dir, sizeof(dir),
917 1.1 gdamore SCM_HCI_DIRECTION, BTPROTO_HCI);
918 1.1 gdamore
919 1.1 gdamore if (*ctl != NULL)
920 1.1 gdamore ctl = &((*ctl)->m_next);
921 1.1 gdamore }
922 1.1 gdamore
923 1.1 gdamore /*
924 1.1 gdamore * copy to socket
925 1.1 gdamore */
926 1.1 gdamore m0 = m_copypacket(m, M_DONTWAIT);
927 1.1 gdamore if (m0 && sbappendaddr(&pcb->hp_socket->so_rcv,
928 1.1 gdamore (struct sockaddr *)&sa, m0, ctlmsg)) {
929 1.1 gdamore sorwakeup(pcb->hp_socket);
930 1.1 gdamore } else {
931 1.1 gdamore m_freem(ctlmsg);
932 1.1 gdamore m_freem(m0);
933 1.1 gdamore }
934 1.1 gdamore }
935 1.1 gdamore }
936