hci_socket.c revision 1.11.14.2 1 1.11.14.2 plunky /* $NetBSD: hci_socket.c,v 1.11.14.2 2007/07/19 20:48:52 plunky Exp $ */
2 1.11.14.2 plunky
3 1.11.14.2 plunky /*-
4 1.11.14.2 plunky * Copyright (c) 2005 Iain Hibbert.
5 1.11.14.2 plunky * Copyright (c) 2006 Itronix Inc.
6 1.11.14.2 plunky * All rights reserved.
7 1.11.14.2 plunky *
8 1.11.14.2 plunky * Redistribution and use in source and binary forms, with or without
9 1.11.14.2 plunky * modification, are permitted provided that the following conditions
10 1.11.14.2 plunky * are met:
11 1.11.14.2 plunky * 1. Redistributions of source code must retain the above copyright
12 1.11.14.2 plunky * notice, this list of conditions and the following disclaimer.
13 1.11.14.2 plunky * 2. Redistributions in binary form must reproduce the above copyright
14 1.11.14.2 plunky * notice, this list of conditions and the following disclaimer in the
15 1.11.14.2 plunky * documentation and/or other materials provided with the distribution.
16 1.11.14.2 plunky * 3. The name of Itronix Inc. may not be used to endorse
17 1.11.14.2 plunky * or promote products derived from this software without specific
18 1.11.14.2 plunky * prior written permission.
19 1.11.14.2 plunky *
20 1.11.14.2 plunky * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 1.11.14.2 plunky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.11.14.2 plunky * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.11.14.2 plunky * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 1.11.14.2 plunky * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 1.11.14.2 plunky * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.11.14.2 plunky * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 1.11.14.2 plunky * ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.11.14.2 plunky * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.11.14.2 plunky * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.11.14.2 plunky * POSSIBILITY OF SUCH DAMAGE.
31 1.11.14.2 plunky */
32 1.11.14.2 plunky
33 1.11.14.2 plunky #include <sys/cdefs.h>
34 1.11.14.2 plunky __KERNEL_RCSID(0, "$NetBSD: hci_socket.c,v 1.11.14.2 2007/07/19 20:48:52 plunky Exp $");
35 1.11.14.2 plunky
36 1.11.14.2 plunky /* load symbolic names */
37 1.11.14.2 plunky #ifdef BLUETOOTH_DEBUG
38 1.11.14.2 plunky #define PRUREQUESTS
39 1.11.14.2 plunky #define PRCOREQUESTS
40 1.11.14.2 plunky #endif
41 1.11.14.2 plunky
42 1.11.14.2 plunky #include <sys/param.h>
43 1.11.14.2 plunky #include <sys/domain.h>
44 1.11.14.2 plunky #include <sys/kauth.h>
45 1.11.14.2 plunky #include <sys/kernel.h>
46 1.11.14.2 plunky #include <sys/mbuf.h>
47 1.11.14.2 plunky #include <sys/proc.h>
48 1.11.14.2 plunky #include <sys/protosw.h>
49 1.11.14.2 plunky #include <sys/socket.h>
50 1.11.14.2 plunky #include <sys/socketvar.h>
51 1.11.14.2 plunky #include <sys/systm.h>
52 1.11.14.2 plunky
53 1.11.14.2 plunky #include <netbt/bluetooth.h>
54 1.11.14.2 plunky #include <netbt/hci.h>
55 1.11.14.2 plunky
56 1.11.14.2 plunky /*******************************************************************************
57 1.11.14.2 plunky *
58 1.11.14.2 plunky * HCI SOCK_RAW Sockets - for control of Bluetooth Devices
59 1.11.14.2 plunky *
60 1.11.14.2 plunky */
61 1.11.14.2 plunky
62 1.11.14.2 plunky /*
63 1.11.14.2 plunky * the raw HCI protocol control block
64 1.11.14.2 plunky */
65 1.11.14.2 plunky struct hci_pcb {
66 1.11.14.2 plunky struct socket *hp_socket; /* socket */
67 1.11.14.2 plunky unsigned int hp_flags; /* flags */
68 1.11.14.2 plunky bdaddr_t hp_laddr; /* local address */
69 1.11.14.2 plunky bdaddr_t hp_raddr; /* remote address */
70 1.11.14.2 plunky struct hci_filter hp_efilter; /* user event filter */
71 1.11.14.2 plunky struct hci_filter hp_pfilter; /* user packet filter */
72 1.11.14.2 plunky LIST_ENTRY(hci_pcb) hp_next; /* next HCI pcb */
73 1.11.14.2 plunky };
74 1.11.14.2 plunky
75 1.11.14.2 plunky /* hp_flags */
76 1.11.14.2 plunky #define HCI_PRIVILEGED (1<<0) /* no security filter for root */
77 1.11.14.2 plunky #define HCI_DIRECTION (1<<1) /* direction control messages */
78 1.11.14.2 plunky #define HCI_PROMISCUOUS (1<<2) /* listen to all units */
79 1.11.14.2 plunky
80 1.11.14.2 plunky LIST_HEAD(hci_pcb_list, hci_pcb) hci_pcb = LIST_HEAD_INITIALIZER(hci_pcb);
81 1.11.14.2 plunky
82 1.11.14.2 plunky /* sysctl defaults */
83 1.11.14.2 plunky int hci_sendspace = HCI_CMD_PKT_SIZE;
84 1.11.14.2 plunky int hci_recvspace = 4096;
85 1.11.14.2 plunky
86 1.11.14.2 plunky /*
87 1.11.14.2 plunky * Security filter routines for unprivileged users.
88 1.11.14.2 plunky * Allow all but a few critical events, and only permit read commands.
89 1.11.14.2 plunky */
90 1.11.14.2 plunky
91 1.11.14.2 plunky static int
92 1.11.14.2 plunky hci_security_check_opcode(uint16_t opcode)
93 1.11.14.2 plunky {
94 1.11.14.2 plunky
95 1.11.14.2 plunky switch (opcode) {
96 1.11.14.2 plunky /* Link control */
97 1.11.14.2 plunky case HCI_CMD_INQUIRY:
98 1.11.14.2 plunky return sizeof(hci_inquiry_cp);
99 1.11.14.2 plunky case HCI_CMD_REMOTE_NAME_REQ:
100 1.11.14.2 plunky return sizeof(hci_remote_name_req_cp);
101 1.11.14.2 plunky case HCI_CMD_READ_REMOTE_FEATURES:
102 1.11.14.2 plunky return sizeof(hci_read_remote_features_cp);
103 1.11.14.2 plunky case HCI_CMD_READ_REMOTE_EXTENDED_FEATURES:
104 1.11.14.2 plunky return sizeof(hci_read_remote_extended_features_cp);
105 1.11.14.2 plunky case HCI_CMD_READ_REMOTE_VER_INFO:
106 1.11.14.2 plunky return sizeof(hci_read_remote_ver_info_cp);
107 1.11.14.2 plunky case HCI_CMD_READ_CLOCK_OFFSET:
108 1.11.14.2 plunky return sizeof(hci_read_clock_offset_cp);
109 1.11.14.2 plunky case HCI_CMD_READ_LMP_HANDLE:
110 1.11.14.2 plunky return sizeof(hci_read_lmp_handle_cp);
111 1.11.14.2 plunky
112 1.11.14.2 plunky /* Link policy */
113 1.11.14.2 plunky case HCI_CMD_ROLE_DISCOVERY:
114 1.11.14.2 plunky return sizeof(hci_role_discovery_cp);
115 1.11.14.2 plunky case HCI_CMD_READ_LINK_POLICY_SETTINGS:
116 1.11.14.2 plunky return sizeof(hci_read_link_policy_settings_cp);
117 1.11.14.2 plunky case HCI_CMD_READ_DEFAULT_LINK_POLICY_SETTINGS:
118 1.11.14.2 plunky return 0; /* No command parameters */
119 1.11.14.2 plunky
120 1.11.14.2 plunky /* Host controller and baseband */
121 1.11.14.2 plunky case HCI_CMD_READ_PIN_TYPE:
122 1.11.14.2 plunky case HCI_CMD_READ_LOCAL_NAME:
123 1.11.14.2 plunky case HCI_CMD_READ_CON_ACCEPT_TIMEOUT:
124 1.11.14.2 plunky case HCI_CMD_READ_PAGE_TIMEOUT:
125 1.11.14.2 plunky case HCI_CMD_READ_SCAN_ENABLE:
126 1.11.14.2 plunky case HCI_CMD_READ_PAGE_SCAN_ACTIVITY:
127 1.11.14.2 plunky case HCI_CMD_READ_INQUIRY_SCAN_ACTIVITY:
128 1.11.14.2 plunky case HCI_CMD_READ_AUTH_ENABLE:
129 1.11.14.2 plunky case HCI_CMD_READ_ENCRYPTION_MODE:
130 1.11.14.2 plunky case HCI_CMD_READ_UNIT_CLASS:
131 1.11.14.2 plunky case HCI_CMD_READ_VOICE_SETTING:
132 1.11.14.2 plunky return 0; /* No command parameters */
133 1.11.14.2 plunky case HCI_CMD_READ_AUTO_FLUSH_TIMEOUT:
134 1.11.14.2 plunky return sizeof(hci_read_auto_flush_timeout_cp);
135 1.11.14.2 plunky case HCI_CMD_READ_NUM_BROADCAST_RETRANS:
136 1.11.14.2 plunky case HCI_CMD_READ_HOLD_MODE_ACTIVITY:
137 1.11.14.2 plunky return 0; /* No command parameters */
138 1.11.14.2 plunky case HCI_CMD_READ_XMIT_LEVEL:
139 1.11.14.2 plunky return sizeof(hci_read_xmit_level_cp);
140 1.11.14.2 plunky case HCI_CMD_READ_SCO_FLOW_CONTROL:
141 1.11.14.2 plunky return 0; /* No command parameters */
142 1.11.14.2 plunky case HCI_CMD_READ_LINK_SUPERVISION_TIMEOUT:
143 1.11.14.2 plunky return sizeof(hci_read_link_supervision_timeout_cp);
144 1.11.14.2 plunky case HCI_CMD_READ_NUM_SUPPORTED_IAC:
145 1.11.14.2 plunky case HCI_CMD_READ_IAC_LAP:
146 1.11.14.2 plunky case HCI_CMD_READ_PAGE_SCAN_PERIOD:
147 1.11.14.2 plunky case HCI_CMD_READ_PAGE_SCAN:
148 1.11.14.2 plunky case HCI_CMD_READ_INQUIRY_SCAN_TYPE:
149 1.11.14.2 plunky case HCI_CMD_READ_INQUIRY_MODE:
150 1.11.14.2 plunky case HCI_CMD_READ_PAGE_SCAN_TYPE:
151 1.11.14.2 plunky case HCI_CMD_READ_AFH_ASSESSMENT:
152 1.11.14.2 plunky return 0; /* No command parameters */
153 1.11.14.2 plunky
154 1.11.14.2 plunky /* Informational */
155 1.11.14.2 plunky case HCI_CMD_READ_LOCAL_VER:
156 1.11.14.2 plunky case HCI_CMD_READ_LOCAL_COMMANDS:
157 1.11.14.2 plunky case HCI_CMD_READ_LOCAL_FEATURES:
158 1.11.14.2 plunky return 0; /* No command parameters */
159 1.11.14.2 plunky case HCI_CMD_READ_LOCAL_EXTENDED_FEATURES:
160 1.11.14.2 plunky return sizeof(hci_read_local_extended_features_cp);
161 1.11.14.2 plunky case HCI_CMD_READ_BUFFER_SIZE:
162 1.11.14.2 plunky case HCI_CMD_READ_COUNTRY_CODE:
163 1.11.14.2 plunky case HCI_CMD_READ_BDADDR:
164 1.11.14.2 plunky return 0; /* No command parameters */
165 1.11.14.2 plunky
166 1.11.14.2 plunky /* Status */
167 1.11.14.2 plunky case HCI_CMD_READ_FAILED_CONTACT_CNTR:
168 1.11.14.2 plunky return sizeof(hci_read_failed_contact_cntr_cp);
169 1.11.14.2 plunky case HCI_CMD_READ_LINK_QUALITY:
170 1.11.14.2 plunky return sizeof(hci_read_link_quality_cp);
171 1.11.14.2 plunky case HCI_CMD_READ_RSSI:
172 1.11.14.2 plunky return sizeof(hci_read_rssi_cp);
173 1.11.14.2 plunky case HCI_CMD_READ_AFH_CHANNEL_MAP:
174 1.11.14.2 plunky return sizeof(hci_read_afh_channel_map_cp);
175 1.11.14.2 plunky case HCI_CMD_READ_CLOCK:
176 1.11.14.2 plunky return sizeof(hci_read_clock_cp);
177 1.11.14.2 plunky
178 1.11.14.2 plunky /* Testing */
179 1.11.14.2 plunky case HCI_CMD_READ_LOOPBACK_MODE:
180 1.11.14.2 plunky return 0; /* No command parameters */
181 1.11.14.2 plunky }
182 1.11.14.2 plunky
183 1.11.14.2 plunky return -1; /* disallowed */
184 1.11.14.2 plunky }
185 1.11.14.2 plunky
186 1.11.14.2 plunky static int
187 1.11.14.2 plunky hci_security_check_event(uint8_t event)
188 1.11.14.2 plunky {
189 1.11.14.2 plunky
190 1.11.14.2 plunky switch (event) {
191 1.11.14.2 plunky case HCI_EVENT_RETURN_LINK_KEYS:
192 1.11.14.2 plunky case HCI_EVENT_LINK_KEY_NOTIFICATION:
193 1.11.14.2 plunky case HCI_EVENT_VENDOR:
194 1.11.14.2 plunky return -1; /* disallowed */
195 1.11.14.2 plunky }
196 1.11.14.2 plunky
197 1.11.14.2 plunky return 0; /* ok */
198 1.11.14.2 plunky }
199 1.11.14.2 plunky
200 1.11.14.2 plunky /*
201 1.11.14.2 plunky * When command packet reaches the device, we can drop
202 1.11.14.2 plunky * it from the socket buffer (called from hci_output_acl)
203 1.11.14.2 plunky */
204 1.11.14.2 plunky void
205 1.11.14.2 plunky hci_drop(void *arg)
206 1.11.14.2 plunky {
207 1.11.14.2 plunky struct socket *so = arg;
208 1.11.14.2 plunky
209 1.11.14.2 plunky sbdroprecord(&so->so_snd);
210 1.11.14.2 plunky sowwakeup(so);
211 1.11.14.2 plunky }
212 1.11.14.2 plunky
213 1.11.14.2 plunky /*
214 1.11.14.2 plunky * HCI socket is going away and has some pending packets. We let them
215 1.11.14.2 plunky * go by design, but remove the context pointer as it will be invalid
216 1.11.14.2 plunky * and we no longer need to be notified.
217 1.11.14.2 plunky */
218 1.11.14.2 plunky static void
219 1.11.14.2 plunky hci_cmdwait_flush(struct socket *so)
220 1.11.14.2 plunky {
221 1.11.14.2 plunky struct hci_unit *unit;
222 1.11.14.2 plunky struct socket *ctx;
223 1.11.14.2 plunky struct mbuf *m;
224 1.11.14.2 plunky
225 1.11.14.2 plunky DPRINTF("flushing %p\n", so);
226 1.11.14.2 plunky
227 1.11.14.2 plunky SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
228 1.11.14.2 plunky m = MBUFQ_FIRST(&unit->hci_cmdwait);
229 1.11.14.2 plunky while (m != NULL) {
230 1.11.14.2 plunky ctx = M_GETCTX(m, struct socket *);
231 1.11.14.2 plunky if (ctx == so)
232 1.11.14.2 plunky M_SETCTX(m, NULL);
233 1.11.14.2 plunky
234 1.11.14.2 plunky m = MBUFQ_NEXT(m);
235 1.11.14.2 plunky }
236 1.11.14.2 plunky }
237 1.11.14.2 plunky }
238 1.11.14.2 plunky
239 1.11.14.2 plunky /*
240 1.11.14.2 plunky * HCI send packet
241 1.11.14.2 plunky * This came from userland, so check it out.
242 1.11.14.2 plunky */
243 1.11.14.2 plunky static int
244 1.11.14.2 plunky hci_send(struct hci_pcb *pcb, struct mbuf *m, bdaddr_t *addr)
245 1.11.14.2 plunky {
246 1.11.14.2 plunky struct hci_unit *unit;
247 1.11.14.2 plunky struct mbuf *m0;
248 1.11.14.2 plunky hci_cmd_hdr_t hdr;
249 1.11.14.2 plunky int err;
250 1.11.14.2 plunky
251 1.11.14.2 plunky KASSERT(m != NULL);
252 1.11.14.2 plunky KASSERT(addr != NULL);
253 1.11.14.2 plunky
254 1.11.14.2 plunky /* wants at least a header to start with */
255 1.11.14.2 plunky if (m->m_pkthdr.len < sizeof(hdr)) {
256 1.11.14.2 plunky err = EMSGSIZE;
257 1.11.14.2 plunky goto bad;
258 1.11.14.2 plunky }
259 1.11.14.2 plunky m_copydata(m, 0, sizeof(hdr), &hdr);
260 1.11.14.2 plunky
261 1.11.14.2 plunky /* only allows CMD packets to be sent */
262 1.11.14.2 plunky if (hdr.type != HCI_CMD_PKT) {
263 1.11.14.2 plunky err = EINVAL;
264 1.11.14.2 plunky goto bad;
265 1.11.14.2 plunky }
266 1.11.14.2 plunky
267 1.11.14.2 plunky /* validates packet length */
268 1.11.14.2 plunky if (m->m_pkthdr.len != sizeof(hdr) + hdr.length) {
269 1.11.14.2 plunky err = EMSGSIZE;
270 1.11.14.2 plunky goto bad;
271 1.11.14.2 plunky }
272 1.11.14.2 plunky
273 1.11.14.2 plunky /* security checks for unprivileged users */
274 1.11.14.2 plunky if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
275 1.11.14.2 plunky && hci_security_check_opcode(le16toh(hdr.opcode)) != hdr.length) {
276 1.11.14.2 plunky err = EPERM;
277 1.11.14.2 plunky goto bad;
278 1.11.14.2 plunky }
279 1.11.14.2 plunky
280 1.11.14.2 plunky /* finds destination */
281 1.11.14.2 plunky unit = hci_unit_lookup(addr);
282 1.11.14.2 plunky if (unit == NULL) {
283 1.11.14.2 plunky err = ENETDOWN;
284 1.11.14.2 plunky goto bad;
285 1.11.14.2 plunky }
286 1.11.14.2 plunky
287 1.11.14.2 plunky /* makess a copy for precious to keep */
288 1.11.14.2 plunky m0 = m_copypacket(m, M_DONTWAIT);
289 1.11.14.2 plunky if (m0 == NULL) {
290 1.11.14.2 plunky err = ENOMEM;
291 1.11.14.2 plunky goto bad;
292 1.11.14.2 plunky }
293 1.11.14.2 plunky sbappendrecord(&pcb->hp_socket->so_snd, m0);
294 1.11.14.2 plunky M_SETCTX(m, pcb->hp_socket); /* enable drop callback */
295 1.11.14.2 plunky
296 1.11.14.2 plunky DPRINTFN(2, "(%s) opcode (%03x|%04x)\n", unit->hci_devname,
297 1.11.14.2 plunky HCI_OGF(le16toh(hdr.opcode)), HCI_OCF(le16toh(hdr.opcode)));
298 1.11.14.2 plunky
299 1.11.14.2 plunky /* Sendss it */
300 1.11.14.2 plunky if (unit->hci_num_cmd_pkts == 0)
301 1.11.14.2 plunky MBUFQ_ENQUEUE(&unit->hci_cmdwait, m);
302 1.11.14.2 plunky else
303 1.11.14.2 plunky hci_output_cmd(unit, m);
304 1.11.14.2 plunky
305 1.11.14.2 plunky return 0;
306 1.11.14.2 plunky
307 1.11.14.2 plunky bad:
308 1.11.14.2 plunky DPRINTF("packet (%d bytes) not sent (error %d)\n",
309 1.11.14.2 plunky m->m_pkthdr.len, err);
310 1.11.14.2 plunky if (m) m_freem(m);
311 1.11.14.2 plunky return err;
312 1.11.14.2 plunky }
313 1.11.14.2 plunky
314 1.11.14.2 plunky /*
315 1.11.14.2 plunky * User Request.
316 1.11.14.2 plunky * up is socket
317 1.11.14.2 plunky * m is either
318 1.11.14.2 plunky * optional mbuf chain containing message
319 1.11.14.2 plunky * ioctl command (PRU_CONTROL)
320 1.11.14.2 plunky * nam is either
321 1.11.14.2 plunky * optional mbuf chain containing an address
322 1.11.14.2 plunky * ioctl data (PRU_CONTROL)
323 1.11.14.2 plunky * optionally, protocol number (PRU_ATTACH)
324 1.11.14.2 plunky * ctl is optional mbuf chain containing socket options
325 1.11.14.2 plunky * l is pointer to process requesting action (if any)
326 1.11.14.2 plunky *
327 1.11.14.2 plunky * we are responsible for disposing of m and ctl if
328 1.11.14.2 plunky * they are mbuf chains
329 1.11.14.2 plunky */
330 1.11.14.2 plunky int
331 1.11.14.2 plunky hci_usrreq(struct socket *up, int req, struct mbuf *m,
332 1.11.14.2 plunky struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
333 1.11.14.2 plunky {
334 1.11.14.2 plunky struct hci_pcb *pcb = (struct hci_pcb *)up->so_pcb;
335 1.11.14.2 plunky struct sockaddr_bt *sa;
336 1.11.14.2 plunky int err = 0;
337 1.11.14.2 plunky
338 1.11.14.2 plunky DPRINTFN(2, "%s\n", prurequests[req]);
339 1.11.14.2 plunky
340 1.11.14.2 plunky switch(req) {
341 1.11.14.2 plunky case PRU_CONTROL:
342 1.11.14.2 plunky return hci_ioctl((unsigned long)m, (void *)nam, l);
343 1.11.14.2 plunky
344 1.11.14.2 plunky case PRU_PURGEIF:
345 1.11.14.2 plunky return EOPNOTSUPP;
346 1.11.14.2 plunky
347 1.11.14.2 plunky case PRU_ATTACH:
348 1.11.14.2 plunky if (pcb)
349 1.11.14.2 plunky return EINVAL;
350 1.11.14.2 plunky
351 1.11.14.2 plunky err = soreserve(up, hci_sendspace, hci_recvspace);
352 1.11.14.2 plunky if (err)
353 1.11.14.2 plunky return err;
354 1.11.14.2 plunky
355 1.11.14.2 plunky pcb = malloc(sizeof(struct hci_pcb), M_PCB, M_NOWAIT | M_ZERO);
356 1.11.14.2 plunky if (pcb == NULL)
357 1.11.14.2 plunky return ENOMEM;
358 1.11.14.2 plunky
359 1.11.14.2 plunky up->so_pcb = pcb;
360 1.11.14.2 plunky pcb->hp_socket = up;
361 1.11.14.2 plunky
362 1.11.14.2 plunky if (l == NULL || kauth_authorize_generic(l->l_cred,
363 1.11.14.2 plunky KAUTH_GENERIC_ISSUSER, NULL) == 0)
364 1.11.14.2 plunky pcb->hp_flags |= HCI_PRIVILEGED;
365 1.11.14.2 plunky
366 1.11.14.2 plunky /*
367 1.11.14.2 plunky * Set default user filter. By default, socket only passes
368 1.11.14.2 plunky * Command_Complete and Command_Status Events.
369 1.11.14.2 plunky */
370 1.11.14.2 plunky hci_filter_set(HCI_EVENT_COMMAND_COMPL, &pcb->hp_efilter);
371 1.11.14.2 plunky hci_filter_set(HCI_EVENT_COMMAND_STATUS, &pcb->hp_efilter);
372 1.11.14.2 plunky hci_filter_set(HCI_EVENT_PKT, &pcb->hp_pfilter);
373 1.11.14.2 plunky
374 1.11.14.2 plunky LIST_INSERT_HEAD(&hci_pcb, pcb, hp_next);
375 1.11.14.2 plunky
376 1.11.14.2 plunky return 0;
377 1.11.14.2 plunky }
378 1.11.14.2 plunky
379 1.11.14.2 plunky /* anything after here *requires* a pcb */
380 1.11.14.2 plunky if (pcb == NULL) {
381 1.11.14.2 plunky err = EINVAL;
382 1.11.14.2 plunky goto release;
383 1.11.14.2 plunky }
384 1.11.14.2 plunky
385 1.11.14.2 plunky switch(req) {
386 1.11.14.2 plunky case PRU_DISCONNECT:
387 1.11.14.2 plunky bdaddr_copy(&pcb->hp_raddr, BDADDR_ANY);
388 1.11.14.2 plunky
389 1.11.14.2 plunky /* XXX we cannot call soisdisconnected() here, as it sets
390 1.11.14.2 plunky * SS_CANTRCVMORE and SS_CANTSENDMORE. The problem being,
391 1.11.14.2 plunky * that soisconnected() does not clear these and if you
392 1.11.14.2 plunky * try to reconnect this socket (which is permitted) you
393 1.11.14.2 plunky * get a broken pipe when you try to write any data.
394 1.11.14.2 plunky */
395 1.11.14.2 plunky up->so_state &= ~SS_ISCONNECTED;
396 1.11.14.2 plunky break;
397 1.11.14.2 plunky
398 1.11.14.2 plunky case PRU_ABORT:
399 1.11.14.2 plunky soisdisconnected(up);
400 1.11.14.2 plunky /* fall through to */
401 1.11.14.2 plunky case PRU_DETACH:
402 1.11.14.2 plunky if (up->so_snd.sb_mb != NULL)
403 1.11.14.2 plunky hci_cmdwait_flush(up);
404 1.11.14.2 plunky
405 1.11.14.2 plunky up->so_pcb = NULL;
406 1.11.14.2 plunky LIST_REMOVE(pcb, hp_next);
407 1.11.14.2 plunky free(pcb, M_PCB);
408 1.11.14.2 plunky return 0;
409 1.11.14.2 plunky
410 1.11.14.2 plunky case PRU_BIND:
411 1.11.14.2 plunky KASSERT(nam != NULL);
412 1.11.14.2 plunky sa = mtod(nam, struct sockaddr_bt *);
413 1.11.14.2 plunky
414 1.11.14.2 plunky if (sa->bt_len != sizeof(struct sockaddr_bt))
415 1.11.14.2 plunky return EINVAL;
416 1.11.14.2 plunky
417 1.11.14.2 plunky if (sa->bt_family != AF_BLUETOOTH)
418 1.11.14.2 plunky return EAFNOSUPPORT;
419 1.11.14.2 plunky
420 1.11.14.2 plunky bdaddr_copy(&pcb->hp_laddr, &sa->bt_bdaddr);
421 1.11.14.2 plunky
422 1.11.14.2 plunky if (bdaddr_any(&sa->bt_bdaddr))
423 1.11.14.2 plunky pcb->hp_flags |= HCI_PROMISCUOUS;
424 1.11.14.2 plunky else
425 1.11.14.2 plunky pcb->hp_flags &= ~HCI_PROMISCUOUS;
426 1.11.14.2 plunky
427 1.11.14.2 plunky return 0;
428 1.11.14.2 plunky
429 1.11.14.2 plunky case PRU_CONNECT:
430 1.11.14.2 plunky KASSERT(nam != NULL);
431 1.11.14.2 plunky sa = mtod(nam, struct sockaddr_bt *);
432 1.11.14.2 plunky
433 1.11.14.2 plunky if (sa->bt_len != sizeof(struct sockaddr_bt))
434 1.11.14.2 plunky return EINVAL;
435 1.11.14.2 plunky
436 1.11.14.2 plunky if (sa->bt_family != AF_BLUETOOTH)
437 1.11.14.2 plunky return EAFNOSUPPORT;
438 1.11.14.2 plunky
439 1.11.14.2 plunky if (hci_unit_lookup(&sa->bt_bdaddr) == NULL)
440 1.11.14.2 plunky return EADDRNOTAVAIL;
441 1.11.14.2 plunky
442 1.11.14.2 plunky bdaddr_copy(&pcb->hp_raddr, &sa->bt_bdaddr);
443 1.11.14.2 plunky soisconnected(up);
444 1.11.14.2 plunky return 0;
445 1.11.14.2 plunky
446 1.11.14.2 plunky case PRU_PEERADDR:
447 1.11.14.2 plunky KASSERT(nam != NULL);
448 1.11.14.2 plunky sa = mtod(nam, struct sockaddr_bt *);
449 1.11.14.2 plunky
450 1.11.14.2 plunky memset(sa, 0, sizeof(struct sockaddr_bt));
451 1.11.14.2 plunky nam->m_len =
452 1.11.14.2 plunky sa->bt_len = sizeof(struct sockaddr_bt);
453 1.11.14.2 plunky sa->bt_family = AF_BLUETOOTH;
454 1.11.14.2 plunky bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_raddr);
455 1.11.14.2 plunky return 0;
456 1.11.14.2 plunky
457 1.11.14.2 plunky case PRU_SOCKADDR:
458 1.11.14.2 plunky KASSERT(nam != NULL);
459 1.11.14.2 plunky sa = mtod(nam, struct sockaddr_bt *);
460 1.11.14.2 plunky
461 1.11.14.2 plunky memset(sa, 0, sizeof(struct sockaddr_bt));
462 1.11.14.2 plunky nam->m_len =
463 1.11.14.2 plunky sa->bt_len = sizeof(struct sockaddr_bt);
464 1.11.14.2 plunky sa->bt_family = AF_BLUETOOTH;
465 1.11.14.2 plunky bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_laddr);
466 1.11.14.2 plunky return 0;
467 1.11.14.2 plunky
468 1.11.14.2 plunky case PRU_SHUTDOWN:
469 1.11.14.2 plunky socantsendmore(up);
470 1.11.14.2 plunky break;
471 1.11.14.2 plunky
472 1.11.14.2 plunky case PRU_SEND:
473 1.11.14.2 plunky sa = NULL;
474 1.11.14.2 plunky if (nam) {
475 1.11.14.2 plunky sa = mtod(nam, struct sockaddr_bt *);
476 1.11.14.2 plunky
477 1.11.14.2 plunky if (sa->bt_len != sizeof(struct sockaddr_bt)) {
478 1.11.14.2 plunky err = EINVAL;
479 1.11.14.2 plunky goto release;
480 1.11.14.2 plunky }
481 1.11.14.2 plunky
482 1.11.14.2 plunky if (sa->bt_family != AF_BLUETOOTH) {
483 1.11.14.2 plunky err = EAFNOSUPPORT;
484 1.11.14.2 plunky goto release;
485 1.11.14.2 plunky }
486 1.11.14.2 plunky }
487 1.11.14.2 plunky
488 1.11.14.2 plunky if (ctl) /* have no use for this */
489 1.11.14.2 plunky m_freem(ctl);
490 1.11.14.2 plunky
491 1.11.14.2 plunky return hci_send(pcb, m, (sa ? &sa->bt_bdaddr : &pcb->hp_raddr));
492 1.11.14.2 plunky
493 1.11.14.2 plunky case PRU_SENSE:
494 1.11.14.2 plunky return 0; /* (no sense - Doh!) */
495 1.11.14.2 plunky
496 1.11.14.2 plunky case PRU_RCVD:
497 1.11.14.2 plunky case PRU_RCVOOB:
498 1.11.14.2 plunky return EOPNOTSUPP; /* (no release) */
499 1.11.14.2 plunky
500 1.11.14.2 plunky case PRU_ACCEPT:
501 1.11.14.2 plunky case PRU_CONNECT2:
502 1.11.14.2 plunky case PRU_LISTEN:
503 1.11.14.2 plunky case PRU_SENDOOB:
504 1.11.14.2 plunky case PRU_FASTTIMO:
505 1.11.14.2 plunky case PRU_SLOWTIMO:
506 1.11.14.2 plunky case PRU_PROTORCV:
507 1.11.14.2 plunky case PRU_PROTOSEND:
508 1.11.14.2 plunky err = EOPNOTSUPP;
509 1.11.14.2 plunky break;
510 1.11.14.2 plunky
511 1.11.14.2 plunky default:
512 1.11.14.2 plunky UNKNOWN(req);
513 1.11.14.2 plunky err = EOPNOTSUPP;
514 1.11.14.2 plunky break;
515 1.11.14.2 plunky }
516 1.11.14.2 plunky
517 1.11.14.2 plunky release:
518 1.11.14.2 plunky if (m)
519 1.11.14.2 plunky m_freem(m);
520 1.11.14.2 plunky if (ctl)
521 1.11.14.2 plunky m_freem(ctl);
522 1.11.14.2 plunky return err;
523 1.11.14.2 plunky }
524 1.11.14.2 plunky
525 1.11.14.2 plunky /*
526 1.11.14.2 plunky * get/set socket options
527 1.11.14.2 plunky */
528 1.11.14.2 plunky int
529 1.11.14.2 plunky hci_ctloutput(int req, struct socket *so, int level,
530 1.11.14.2 plunky int optname, struct mbuf **opt)
531 1.11.14.2 plunky {
532 1.11.14.2 plunky struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
533 1.11.14.2 plunky struct mbuf *m;
534 1.11.14.2 plunky int err = 0;
535 1.11.14.2 plunky
536 1.11.14.2 plunky DPRINTFN(2, "req %s\n", prcorequests[req]);
537 1.11.14.2 plunky
538 1.11.14.2 plunky if (pcb == NULL)
539 1.11.14.2 plunky return EINVAL;
540 1.11.14.2 plunky
541 1.11.14.2 plunky if (level != BTPROTO_HCI)
542 1.11.14.2 plunky return ENOPROTOOPT;
543 1.11.14.2 plunky
544 1.11.14.2 plunky switch(req) {
545 1.11.14.2 plunky case PRCO_GETOPT:
546 1.11.14.2 plunky m = m_get(M_WAIT, MT_SOOPTS);
547 1.11.14.2 plunky switch (optname) {
548 1.11.14.2 plunky case SO_HCI_EVT_FILTER:
549 1.11.14.2 plunky m->m_len = sizeof(struct hci_filter);
550 1.11.14.2 plunky memcpy(mtod(m, void *), &pcb->hp_efilter, m->m_len);
551 1.11.14.2 plunky break;
552 1.11.14.2 plunky
553 1.11.14.2 plunky case SO_HCI_PKT_FILTER:
554 1.11.14.2 plunky m->m_len = sizeof(struct hci_filter);
555 1.11.14.2 plunky memcpy(mtod(m, void *), &pcb->hp_pfilter, m->m_len);
556 1.11.14.2 plunky break;
557 1.11.14.2 plunky
558 1.11.14.2 plunky case SO_HCI_DIRECTION:
559 1.11.14.2 plunky m->m_len = sizeof(int);
560 1.11.14.2 plunky if (pcb->hp_flags & HCI_DIRECTION)
561 1.11.14.2 plunky *mtod(m, int *) = 1;
562 1.11.14.2 plunky else
563 1.11.14.2 plunky *mtod(m, int *) = 0;
564 1.11.14.2 plunky break;
565 1.11.14.2 plunky
566 1.11.14.2 plunky default:
567 1.11.14.2 plunky err = ENOPROTOOPT;
568 1.11.14.2 plunky m_freem(m);
569 1.11.14.2 plunky m = NULL;
570 1.11.14.2 plunky break;
571 1.11.14.2 plunky }
572 1.11.14.2 plunky *opt = m;
573 1.11.14.2 plunky break;
574 1.11.14.2 plunky
575 1.11.14.2 plunky case PRCO_SETOPT:
576 1.11.14.2 plunky m = *opt;
577 1.11.14.2 plunky if (m) switch (optname) {
578 1.11.14.2 plunky case SO_HCI_EVT_FILTER: /* set event filter */
579 1.11.14.2 plunky m->m_len = min(m->m_len, sizeof(struct hci_filter));
580 1.11.14.2 plunky memcpy(&pcb->hp_efilter, mtod(m, void *), m->m_len);
581 1.11.14.2 plunky break;
582 1.11.14.2 plunky
583 1.11.14.2 plunky case SO_HCI_PKT_FILTER: /* set packet filter */
584 1.11.14.2 plunky m->m_len = min(m->m_len, sizeof(struct hci_filter));
585 1.11.14.2 plunky memcpy(&pcb->hp_pfilter, mtod(m, void *), m->m_len);
586 1.11.14.2 plunky break;
587 1.11.14.2 plunky
588 1.11.14.2 plunky case SO_HCI_DIRECTION: /* request direction ctl messages */
589 1.11.14.2 plunky if (*mtod(m, int *))
590 1.11.14.2 plunky pcb->hp_flags |= HCI_DIRECTION;
591 1.11.14.2 plunky else
592 1.11.14.2 plunky pcb->hp_flags &= ~HCI_DIRECTION;
593 1.11.14.2 plunky break;
594 1.11.14.2 plunky
595 1.11.14.2 plunky default:
596 1.11.14.2 plunky err = ENOPROTOOPT;
597 1.11.14.2 plunky break;
598 1.11.14.2 plunky }
599 1.11.14.2 plunky m_freem(m);
600 1.11.14.2 plunky break;
601 1.11.14.2 plunky
602 1.11.14.2 plunky default:
603 1.11.14.2 plunky err = ENOPROTOOPT;
604 1.11.14.2 plunky break;
605 1.11.14.2 plunky }
606 1.11.14.2 plunky
607 1.11.14.2 plunky return err;
608 1.11.14.2 plunky }
609 1.11.14.2 plunky
610 1.11.14.2 plunky /*
611 1.11.14.2 plunky * HCI mbuf tap routine
612 1.11.14.2 plunky *
613 1.11.14.2 plunky * copy packets to any raw HCI sockets that wish (and are
614 1.11.14.2 plunky * permitted) to see them
615 1.11.14.2 plunky */
616 1.11.14.2 plunky void
617 1.11.14.2 plunky hci_mtap(struct mbuf *m, struct hci_unit *unit)
618 1.11.14.2 plunky {
619 1.11.14.2 plunky struct hci_pcb *pcb;
620 1.11.14.2 plunky struct mbuf *m0, *ctlmsg, **ctl;
621 1.11.14.2 plunky struct sockaddr_bt sa;
622 1.11.14.2 plunky uint8_t type;
623 1.11.14.2 plunky uint8_t event;
624 1.11.14.2 plunky uint16_t opcode;
625 1.11.14.2 plunky
626 1.11.14.2 plunky KASSERT(m->m_len >= sizeof(type));
627 1.11.14.2 plunky
628 1.11.14.2 plunky type = *mtod(m, uint8_t *);
629 1.11.14.2 plunky
630 1.11.14.2 plunky memset(&sa, 0, sizeof(sa));
631 1.11.14.2 plunky sa.bt_len = sizeof(struct sockaddr_bt);
632 1.11.14.2 plunky sa.bt_family = AF_BLUETOOTH;
633 1.11.14.2 plunky bdaddr_copy(&sa.bt_bdaddr, &unit->hci_bdaddr);
634 1.11.14.2 plunky
635 1.11.14.2 plunky LIST_FOREACH(pcb, &hci_pcb, hp_next) {
636 1.11.14.2 plunky /*
637 1.11.14.2 plunky * filter according to source address
638 1.11.14.2 plunky */
639 1.11.14.2 plunky if ((pcb->hp_flags & HCI_PROMISCUOUS) == 0
640 1.11.14.2 plunky && bdaddr_same(&pcb->hp_laddr, &sa.bt_bdaddr) == 0)
641 1.11.14.2 plunky continue;
642 1.11.14.2 plunky
643 1.11.14.2 plunky /*
644 1.11.14.2 plunky * filter according to packet type filter
645 1.11.14.2 plunky */
646 1.11.14.2 plunky if (hci_filter_test(type, &pcb->hp_pfilter) == 0)
647 1.11.14.2 plunky continue;
648 1.11.14.2 plunky
649 1.11.14.2 plunky /*
650 1.11.14.2 plunky * filter according to event/security filters
651 1.11.14.2 plunky */
652 1.11.14.2 plunky switch(type) {
653 1.11.14.2 plunky case HCI_EVENT_PKT:
654 1.11.14.2 plunky KASSERT(m->m_len >= sizeof(hci_event_hdr_t));
655 1.11.14.2 plunky
656 1.11.14.2 plunky event = mtod(m, hci_event_hdr_t *)->event;
657 1.11.14.2 plunky
658 1.11.14.2 plunky if (hci_filter_test(event, &pcb->hp_efilter) == 0)
659 1.11.14.2 plunky continue;
660 1.11.14.2 plunky
661 1.11.14.2 plunky if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
662 1.11.14.2 plunky && hci_security_check_event(event) == -1)
663 1.11.14.2 plunky continue;
664 1.11.14.2 plunky break;
665 1.11.14.2 plunky
666 1.11.14.2 plunky case HCI_CMD_PKT:
667 1.11.14.2 plunky KASSERT(m->m_len >= sizeof(hci_cmd_hdr_t));
668 1.11.14.2 plunky
669 1.11.14.2 plunky opcode = le16toh(mtod(m, hci_cmd_hdr_t *)->opcode);
670 1.11.14.2 plunky
671 1.11.14.2 plunky if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
672 1.11.14.2 plunky && hci_security_check_opcode(opcode) == -1)
673 1.11.14.2 plunky continue;
674 1.11.14.2 plunky break;
675 1.11.14.2 plunky
676 1.11.14.2 plunky case HCI_ACL_DATA_PKT:
677 1.11.14.2 plunky case HCI_SCO_DATA_PKT:
678 1.11.14.2 plunky default:
679 1.11.14.2 plunky if ((pcb->hp_flags & HCI_PRIVILEGED) == 0)
680 1.11.14.2 plunky continue;
681 1.11.14.2 plunky
682 1.11.14.2 plunky break;
683 1.11.14.2 plunky }
684 1.11.14.2 plunky
685 1.11.14.2 plunky /*
686 1.11.14.2 plunky * create control messages
687 1.11.14.2 plunky */
688 1.11.14.2 plunky ctlmsg = NULL;
689 1.11.14.2 plunky ctl = &ctlmsg;
690 1.11.14.2 plunky if (pcb->hp_flags & HCI_DIRECTION) {
691 1.11.14.2 plunky int dir = m->m_flags & M_LINK0 ? 1 : 0;
692 1.11.14.2 plunky
693 1.11.14.2 plunky *ctl = sbcreatecontrol(&dir, sizeof(dir),
694 1.11.14.2 plunky SCM_HCI_DIRECTION, BTPROTO_HCI);
695 1.11.14.2 plunky
696 1.11.14.2 plunky if (*ctl != NULL)
697 1.11.14.2 plunky ctl = &((*ctl)->m_next);
698 1.11.14.2 plunky }
699 1.11.14.2 plunky
700 1.11.14.2 plunky /*
701 1.11.14.2 plunky * copy to socket
702 1.11.14.2 plunky */
703 1.11.14.2 plunky m0 = m_copypacket(m, M_DONTWAIT);
704 1.11.14.2 plunky if (m0 && sbappendaddr(&pcb->hp_socket->so_rcv,
705 1.11.14.2 plunky (struct sockaddr *)&sa, m0, ctlmsg)) {
706 1.11.14.2 plunky sorwakeup(pcb->hp_socket);
707 1.11.14.2 plunky } else {
708 1.11.14.2 plunky m_freem(ctlmsg);
709 1.11.14.2 plunky m_freem(m0);
710 1.11.14.2 plunky }
711 1.11.14.2 plunky }
712 1.11.14.2 plunky }
713