1 /*
2 * ng_btsocket_hci_raw.c
3 */
4
5 /*-
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * Copyright (c) 2001-2002 Maksim Yevmenkin <[email protected]>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $Id: ng_btsocket_hci_raw.c,v 1.14 2003/09/14 23:29:06 max Exp $
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bitstring.h>
38 #include <sys/domain.h>
39 #include <sys/endian.h>
40 #include <sys/errno.h>
41 #include <sys/filedesc.h>
42 #include <sys/ioccom.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/mutex.h>
48 #include <sys/priv.h>
49 #include <sys/protosw.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/taskqueue.h>
55
56 #include <net/vnet.h>
57
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/bluetooth/include/ng_bluetooth.h>
61 #include <netgraph/bluetooth/include/ng_hci.h>
62 #include <netgraph/bluetooth/include/ng_l2cap.h>
63 #include <netgraph/bluetooth/include/ng_btsocket.h>
64 #include <netgraph/bluetooth/include/ng_btsocket_hci_raw.h>
65
66 /* MALLOC define */
67 #ifdef NG_SEPARATE_MALLOC
68 static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_HCI_RAW, "netgraph_btsocks_hci_raw",
69 "Netgraph Bluetooth raw HCI sockets");
70 #else
71 #define M_NETGRAPH_BTSOCKET_HCI_RAW M_NETGRAPH
72 #endif /* NG_SEPARATE_MALLOC */
73
74 /* Netgraph node methods */
75 static ng_constructor_t ng_btsocket_hci_raw_node_constructor;
76 static ng_rcvmsg_t ng_btsocket_hci_raw_node_rcvmsg;
77 static ng_shutdown_t ng_btsocket_hci_raw_node_shutdown;
78 static ng_newhook_t ng_btsocket_hci_raw_node_newhook;
79 static ng_connect_t ng_btsocket_hci_raw_node_connect;
80 static ng_rcvdata_t ng_btsocket_hci_raw_node_rcvdata;
81 static ng_disconnect_t ng_btsocket_hci_raw_node_disconnect;
82
83 static void ng_btsocket_hci_raw_input (void *, int);
84 static void ng_btsocket_hci_raw_output(node_p, hook_p, void *, int);
85 static void ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p,
86 struct mbuf **,
87 struct mbuf *);
88 static int ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p,
89 struct mbuf *, int);
90
91 #define ng_btsocket_hci_raw_wakeup_input_task() \
92 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_hci_raw_task)
93
94 /* Security filter */
95 struct ng_btsocket_hci_raw_sec_filter {
96 bitstr_t bit_decl(events, 0xff);
97 bitstr_t bit_decl(commands[0x3f], 0x3ff);
98 };
99
100 /* Netgraph type descriptor */
101 static struct ng_type typestruct = {
102 .version = NG_ABI_VERSION,
103 .name = NG_BTSOCKET_HCI_RAW_NODE_TYPE,
104 .constructor = ng_btsocket_hci_raw_node_constructor,
105 .rcvmsg = ng_btsocket_hci_raw_node_rcvmsg,
106 .shutdown = ng_btsocket_hci_raw_node_shutdown,
107 .newhook = ng_btsocket_hci_raw_node_newhook,
108 .connect = ng_btsocket_hci_raw_node_connect,
109 .rcvdata = ng_btsocket_hci_raw_node_rcvdata,
110 .disconnect = ng_btsocket_hci_raw_node_disconnect,
111 };
112
113 /* Globals */
114 static u_int32_t ng_btsocket_hci_raw_debug_level;
115 static u_int32_t ng_btsocket_hci_raw_ioctl_timeout;
116 static node_p ng_btsocket_hci_raw_node;
117 static struct ng_bt_itemq ng_btsocket_hci_raw_queue;
118 static struct mtx ng_btsocket_hci_raw_queue_mtx;
119 static struct task ng_btsocket_hci_raw_task;
120 static LIST_HEAD(, ng_btsocket_hci_raw_pcb) ng_btsocket_hci_raw_sockets;
121 static struct mtx ng_btsocket_hci_raw_sockets_mtx;
122 static u_int32_t ng_btsocket_hci_raw_token;
123 static struct mtx ng_btsocket_hci_raw_token_mtx;
124 static struct ng_btsocket_hci_raw_sec_filter *ng_btsocket_hci_raw_sec_filter;
125 static struct timeval ng_btsocket_hci_raw_lasttime;
126 static int ng_btsocket_hci_raw_curpps;
127
128 /* Sysctl tree */
129 SYSCTL_DECL(_net_bluetooth_hci_sockets);
130 static SYSCTL_NODE(_net_bluetooth_hci_sockets, OID_AUTO, raw,
131 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
132 "Bluetooth raw HCI sockets family");
133 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, debug_level, CTLFLAG_RW,
134 &ng_btsocket_hci_raw_debug_level, NG_BTSOCKET_WARN_LEVEL,
135 "Bluetooth raw HCI sockets debug level");
136 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, ioctl_timeout, CTLFLAG_RW,
137 &ng_btsocket_hci_raw_ioctl_timeout, 5,
138 "Bluetooth raw HCI sockets ioctl timeout");
139 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_len, CTLFLAG_RD,
140 &ng_btsocket_hci_raw_queue.len, 0,
141 "Bluetooth raw HCI sockets input queue length");
142 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_maxlen, CTLFLAG_RD,
143 &ng_btsocket_hci_raw_queue.maxlen, 0,
144 "Bluetooth raw HCI sockets input queue max. length");
145 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_drops, CTLFLAG_RD,
146 &ng_btsocket_hci_raw_queue.drops, 0,
147 "Bluetooth raw HCI sockets input queue drops");
148
149 /* Debug */
150 #define NG_BTSOCKET_HCI_RAW_INFO \
151 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
152 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
153 printf
154
155 #define NG_BTSOCKET_HCI_RAW_WARN \
156 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
157 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
158 printf
159
160 #define NG_BTSOCKET_HCI_RAW_ERR \
161 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
162 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
163 printf
164
165 #define NG_BTSOCKET_HCI_RAW_ALERT \
166 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
167 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
168 printf
169
170 /****************************************************************************
171 ****************************************************************************
172 ** Netgraph specific
173 ****************************************************************************
174 ****************************************************************************/
175
176 /*
177 * Netgraph node constructor. Do not allow to create node of this type.
178 */
179
180 static int
ng_btsocket_hci_raw_node_constructor(node_p node)181 ng_btsocket_hci_raw_node_constructor(node_p node)
182 {
183 return (EINVAL);
184 } /* ng_btsocket_hci_raw_node_constructor */
185
186 /*
187 * Netgraph node destructor. Just let old node go and create new fresh one.
188 */
189
190 static int
ng_btsocket_hci_raw_node_shutdown(node_p node)191 ng_btsocket_hci_raw_node_shutdown(node_p node)
192 {
193 int error = 0;
194
195 NG_NODE_UNREF(node);
196
197 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
198 if (error != 0) {
199 NG_BTSOCKET_HCI_RAW_ALERT(
200 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
201
202 ng_btsocket_hci_raw_node = NULL;
203
204 return (ENOMEM);
205 }
206
207 error = ng_name_node(ng_btsocket_hci_raw_node,
208 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
209 if (error != 0) {
210 NG_BTSOCKET_HCI_RAW_ALERT(
211 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
212
213 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
214 ng_btsocket_hci_raw_node = NULL;
215
216 return (EINVAL);
217 }
218
219 return (0);
220 } /* ng_btsocket_hci_raw_node_shutdown */
221
222 /*
223 * Create new hook. Just say "yes"
224 */
225
226 static int
ng_btsocket_hci_raw_node_newhook(node_p node,hook_p hook,char const * name)227 ng_btsocket_hci_raw_node_newhook(node_p node, hook_p hook, char const *name)
228 {
229 return (0);
230 } /* ng_btsocket_hci_raw_node_newhook */
231
232 /*
233 * Connect hook. Just say "yes"
234 */
235
236 static int
ng_btsocket_hci_raw_node_connect(hook_p hook)237 ng_btsocket_hci_raw_node_connect(hook_p hook)
238 {
239 return (0);
240 } /* ng_btsocket_hci_raw_node_connect */
241
242 /*
243 * Disconnect hook
244 */
245
246 static int
ng_btsocket_hci_raw_node_disconnect(hook_p hook)247 ng_btsocket_hci_raw_node_disconnect(hook_p hook)
248 {
249 return (0);
250 } /* ng_btsocket_hci_raw_node_disconnect */
251
252 /*
253 * Receive control message.
254 * Make sure it is a message from HCI node and it is a response.
255 * Enqueue item and schedule input task.
256 */
257
258 static int
ng_btsocket_hci_raw_node_rcvmsg(node_p node,item_p item,hook_p lasthook)259 ng_btsocket_hci_raw_node_rcvmsg(node_p node, item_p item, hook_p lasthook)
260 {
261 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
262 int error = 0;
263
264 /*
265 * Check for empty sockets list creates LOR when both sender and
266 * receiver device are connected to the same host, so remove it
267 * for now
268 */
269
270 if (msg != NULL &&
271 (msg->header.typecookie == NGM_HCI_COOKIE ||
272 msg->header.typecookie == NGM_GENERIC_COOKIE) &&
273 msg->header.flags & NGF_RESP) {
274 if (msg->header.token == 0) {
275 NG_FREE_ITEM(item);
276 return (0);
277 }
278
279 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
280 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
281 NG_BTSOCKET_HCI_RAW_ERR(
282 "%s: Input queue is full\n", __func__);
283
284 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
285 NG_FREE_ITEM(item);
286 error = ENOBUFS;
287 } else {
288 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
289 error = ng_btsocket_hci_raw_wakeup_input_task();
290 }
291 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
292 } else {
293 NG_FREE_ITEM(item);
294 error = EINVAL;
295 }
296
297 return (error);
298 } /* ng_btsocket_hci_raw_node_rcvmsg */
299
300 /*
301 * Receive packet from the one of our hook.
302 * Prepend every packet with sockaddr_hci and record sender's node name.
303 * Enqueue item and schedule input task.
304 */
305
306 static int
ng_btsocket_hci_raw_node_rcvdata(hook_p hook,item_p item)307 ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
308 {
309 struct mbuf *nam = NULL;
310 int error;
311
312 /*
313 * Check for empty sockets list creates LOR when both sender and
314 * receiver device are connected to the same host, so remove it
315 * for now
316 */
317
318 MGET(nam, M_NOWAIT, MT_SONAME);
319 if (nam != NULL) {
320 struct sockaddr_hci *sa = mtod(nam, struct sockaddr_hci *);
321
322 nam->m_len = sizeof(struct sockaddr_hci);
323
324 sa->hci_len = sizeof(*sa);
325 sa->hci_family = AF_BLUETOOTH;
326 strlcpy(sa->hci_node, NG_PEER_NODE_NAME(hook),
327 sizeof(sa->hci_node));
328
329 NGI_GET_M(item, nam->m_next);
330 NGI_M(item) = nam;
331
332 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
333 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
334 NG_BTSOCKET_HCI_RAW_ERR(
335 "%s: Input queue is full\n", __func__);
336
337 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
338 NG_FREE_ITEM(item);
339 error = ENOBUFS;
340 } else {
341 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
342 error = ng_btsocket_hci_raw_wakeup_input_task();
343 }
344 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
345 } else {
346 NG_BTSOCKET_HCI_RAW_ERR(
347 "%s: Failed to allocate address mbuf\n", __func__);
348
349 NG_FREE_ITEM(item);
350 error = ENOBUFS;
351 }
352
353 return (error);
354 } /* ng_btsocket_hci_raw_node_rcvdata */
355
356 /****************************************************************************
357 ****************************************************************************
358 ** Sockets specific
359 ****************************************************************************
360 ****************************************************************************/
361
362 /*
363 * Get next token. We need token to avoid theoretical race where process
364 * submits ioctl() message then interrupts ioctl() and re-submits another
365 * ioctl() on the same socket *before* first ioctl() complete.
366 */
367
368 static void
ng_btsocket_hci_raw_get_token(u_int32_t * token)369 ng_btsocket_hci_raw_get_token(u_int32_t *token)
370 {
371 mtx_lock(&ng_btsocket_hci_raw_token_mtx);
372
373 if (++ ng_btsocket_hci_raw_token == 0)
374 ng_btsocket_hci_raw_token = 1;
375
376 *token = ng_btsocket_hci_raw_token;
377
378 mtx_unlock(&ng_btsocket_hci_raw_token_mtx);
379 } /* ng_btsocket_hci_raw_get_token */
380
381 /*
382 * Send Netgraph message to the node - do not expect reply
383 */
384
385 static int
ng_btsocket_hci_raw_send_ngmsg(char * path,int cmd,void * arg,int arglen)386 ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen)
387 {
388 struct ng_mesg *msg = NULL;
389 int error = 0;
390
391 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, arglen, M_NOWAIT);
392 if (msg == NULL)
393 return (ENOMEM);
394
395 if (arg != NULL && arglen > 0)
396 bcopy(arg, msg->data, arglen);
397
398 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
399
400 return (error);
401 } /* ng_btsocket_hci_raw_send_ngmsg */
402
403 /*
404 * Send Netgraph message to the node (no data) and wait for reply
405 */
406
407 static int
ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb,char * path,int cmd,void * rsp,int rsplen)408 ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path,
409 int cmd, void *rsp, int rsplen)
410 {
411 struct ng_mesg *msg = NULL;
412 int error = 0;
413
414 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
415
416 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_NOWAIT);
417 if (msg == NULL)
418 return (ENOMEM);
419
420 ng_btsocket_hci_raw_get_token(&msg->header.token);
421 pcb->token = msg->header.token;
422 pcb->msg = NULL;
423
424 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
425 if (error != 0) {
426 pcb->token = 0;
427 return (error);
428 }
429
430 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "hcictl",
431 ng_btsocket_hci_raw_ioctl_timeout * hz);
432 pcb->token = 0;
433
434 if (error != 0)
435 return (error);
436
437 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
438 bcopy(pcb->msg->data, rsp, rsplen);
439 else
440 error = EINVAL;
441
442 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
443
444 return (0);
445 } /* ng_btsocket_hci_raw_send_sync_ngmsg */
446
447 /*
448 * Create control information for the packet
449 */
450
451 static void
ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb,struct mbuf ** ctl,struct mbuf * m)452 ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf **ctl,
453 struct mbuf *m)
454 {
455 int dir;
456 struct timeval tv;
457
458 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
459
460 if (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION) {
461 dir = (m->m_flags & M_PROTO1)? 1 : 0;
462 *ctl = sbcreatecontrol(&dir, sizeof(dir),
463 SCM_HCI_RAW_DIRECTION, SOL_HCI_RAW, M_NOWAIT);
464 if (*ctl != NULL)
465 ctl = &((*ctl)->m_next);
466 }
467
468 if (pcb->so->so_options & SO_TIMESTAMP) {
469 microtime(&tv);
470 *ctl = sbcreatecontrol(&tv, sizeof(tv), SCM_TIMESTAMP,
471 SOL_SOCKET, M_NOWAIT);
472 if (*ctl != NULL)
473 ctl = &((*ctl)->m_next);
474 }
475 } /* ng_btsocket_hci_raw_savctl */
476
477 /*
478 * Raw HCI sockets data input routine
479 */
480
481 static void
ng_btsocket_hci_raw_data_input(struct mbuf * nam)482 ng_btsocket_hci_raw_data_input(struct mbuf *nam)
483 {
484 ng_btsocket_hci_raw_pcb_p pcb = NULL;
485 struct mbuf *m0 = NULL, *m = NULL;
486 struct sockaddr_hci *sa = NULL;
487
488 m0 = nam->m_next;
489 nam->m_next = NULL;
490
491 KASSERT((nam->m_type == MT_SONAME),
492 ("%s: m_type=%d\n", __func__, nam->m_type));
493 KASSERT((m0->m_flags & M_PKTHDR),
494 ("%s: m_flags=%#x\n", __func__, m0->m_flags));
495
496 sa = mtod(nam, struct sockaddr_hci *);
497
498 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
499
500 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
501 mtx_lock(&pcb->pcb_mtx);
502
503 /*
504 * If socket was bound then check address and
505 * make sure it matches.
506 */
507
508 if (pcb->addr.hci_node[0] != 0 &&
509 strcmp(sa->hci_node, pcb->addr.hci_node) != 0)
510 goto next;
511
512 /*
513 * Check packet against filters
514 * XXX do we have to call m_pullup() here?
515 */
516
517 if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0)
518 goto next;
519
520 /*
521 * Make a copy of the packet, append to the socket's
522 * receive queue and wakeup socket. sbappendaddr()
523 * will check if socket has enough buffer space.
524 */
525
526 m = m_dup(m0, M_NOWAIT);
527 if (m != NULL) {
528 struct mbuf *ctl = NULL;
529
530 ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
531
532 if (sbappendaddr(&pcb->so->so_rcv,
533 (struct sockaddr *) sa, m, ctl))
534 sorwakeup(pcb->so);
535 else {
536 NG_BTSOCKET_HCI_RAW_INFO(
537 "%s: sbappendaddr() failed\n", __func__);
538
539 NG_FREE_M(m);
540 NG_FREE_M(ctl);
541 soroverflow(pcb->so);
542 }
543 }
544 next:
545 mtx_unlock(&pcb->pcb_mtx);
546 }
547
548 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
549
550 NG_FREE_M(nam);
551 NG_FREE_M(m0);
552 } /* ng_btsocket_hci_raw_data_input */
553
554 /*
555 * Raw HCI sockets message input routine
556 */
557
558 static void
ng_btsocket_hci_raw_msg_input(struct ng_mesg * msg)559 ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg)
560 {
561 ng_btsocket_hci_raw_pcb_p pcb = NULL;
562
563 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
564
565 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
566 mtx_lock(&pcb->pcb_mtx);
567
568 if (msg->header.token == pcb->token) {
569 pcb->msg = msg;
570 wakeup(&pcb->msg);
571
572 mtx_unlock(&pcb->pcb_mtx);
573 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
574
575 return;
576 }
577
578 mtx_unlock(&pcb->pcb_mtx);
579 }
580
581 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
582
583 NG_FREE_MSG(msg); /* checks for != NULL */
584 } /* ng_btsocket_hci_raw_msg_input */
585
586 /*
587 * Raw HCI sockets input routines
588 */
589
590 static void
ng_btsocket_hci_raw_input(void * context,int pending)591 ng_btsocket_hci_raw_input(void *context, int pending)
592 {
593 item_p item = NULL;
594
595 for (;;) {
596 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
597 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_hci_raw_queue, item);
598 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
599
600 if (item == NULL)
601 break;
602
603 switch(item->el_flags & NGQF_TYPE) {
604 case NGQF_DATA: {
605 struct mbuf *m = NULL;
606
607 NGI_GET_M(item, m);
608 ng_btsocket_hci_raw_data_input(m);
609 } break;
610
611 case NGQF_MESG: {
612 struct ng_mesg *msg = NULL;
613
614 NGI_GET_MSG(item, msg);
615 ng_btsocket_hci_raw_msg_input(msg);
616 } break;
617
618 default:
619 KASSERT(0,
620 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
621 break;
622 }
623
624 NG_FREE_ITEM(item);
625 }
626 } /* ng_btsocket_hci_raw_input */
627
628 /*
629 * Raw HCI sockets output routine
630 */
631
632 static void
ng_btsocket_hci_raw_output(node_p node,hook_p hook,void * arg1,int arg2)633 ng_btsocket_hci_raw_output(node_p node, hook_p hook, void *arg1, int arg2)
634 {
635 struct mbuf *nam = (struct mbuf *) arg1, *m = NULL;
636 struct sockaddr_hci *sa = NULL;
637 int error;
638
639 m = nam->m_next;
640 nam->m_next = NULL;
641
642 KASSERT((nam->m_type == MT_SONAME),
643 ("%s: m_type=%d\n", __func__, nam->m_type));
644 KASSERT((m->m_flags & M_PKTHDR),
645 ("%s: m_flags=%#x\n", __func__, m->m_flags));
646
647 sa = mtod(nam, struct sockaddr_hci *);
648
649 /*
650 * Find downstream hook
651 * XXX For now access node hook list directly. Should be safe because
652 * we used ng_send_fn() and we should have exclusive lock on the node.
653 */
654
655 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
656 if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
657 NG_NODE_NOT_VALID(NG_PEER_NODE(hook)))
658 continue;
659
660 if (strcmp(sa->hci_node, NG_PEER_NODE_NAME(hook)) == 0) {
661 NG_SEND_DATA_ONLY(error, hook, m); /* sets m to NULL */
662 break;
663 }
664 }
665
666 NG_FREE_M(nam); /* check for != NULL */
667 NG_FREE_M(m);
668 } /* ng_btsocket_hci_raw_output */
669
670 /*
671 * Check frame against security and socket filters.
672 * d (direction bit) == 1 means incoming frame.
673 */
674
675 static int
ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb,struct mbuf * m,int d)676 ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf *m, int d)
677 {
678 int type, event, opcode;
679
680 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
681
682 switch ((type = *mtod(m, u_int8_t *))) {
683 case NG_HCI_CMD_PKT:
684 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)) {
685 opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
686
687 if (!bit_test(
688 ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF(opcode) - 1],
689 NG_HCI_OCF(opcode) - 1))
690 return (EPERM);
691 }
692
693 if (d && !bit_test(pcb->filter.packet_mask, NG_HCI_CMD_PKT - 1))
694 return (EPERM);
695 break;
696
697 case NG_HCI_ACL_DATA_PKT:
698 case NG_HCI_SCO_DATA_PKT:
699 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) ||
700 !bit_test(pcb->filter.packet_mask, type - 1) ||
701 !d)
702 return (EPERM);
703 break;
704
705 case NG_HCI_EVENT_PKT:
706 if (!d)
707 return (EINVAL);
708
709 event = mtod(m, ng_hci_event_pkt_t *)->event - 1;
710
711 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED))
712 if (!bit_test(ng_btsocket_hci_raw_sec_filter->events, event))
713 return (EPERM);
714
715 if (!bit_test(pcb->filter.event_mask, event))
716 return (EPERM);
717 break;
718
719 default:
720 return (EINVAL);
721 }
722
723 return (0);
724 } /* ng_btsocket_hci_raw_filter */
725
726 /*
727 * Initialize everything
728 */
729
730 static void
ng_btsocket_hci_raw_init(void * arg __unused)731 ng_btsocket_hci_raw_init(void *arg __unused)
732 {
733 bitstr_t *f = NULL;
734 int error = 0;
735
736 ng_btsocket_hci_raw_node = NULL;
737 ng_btsocket_hci_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
738 ng_btsocket_hci_raw_ioctl_timeout = 5;
739
740 /* Register Netgraph node type */
741 error = ng_newtype(&typestruct);
742 if (error != 0) {
743 NG_BTSOCKET_HCI_RAW_ALERT(
744 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
745
746 return;
747 }
748
749 /* Create Netgrapg node */
750 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
751 if (error != 0) {
752 NG_BTSOCKET_HCI_RAW_ALERT(
753 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
754
755 ng_btsocket_hci_raw_node = NULL;
756
757 return;
758 }
759
760 error = ng_name_node(ng_btsocket_hci_raw_node,
761 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
762 if (error != 0) {
763 NG_BTSOCKET_HCI_RAW_ALERT(
764 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
765
766 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
767 ng_btsocket_hci_raw_node = NULL;
768
769 return;
770 }
771
772 /* Create input queue */
773 NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, 300);
774 mtx_init(&ng_btsocket_hci_raw_queue_mtx,
775 "btsocks_hci_raw_queue_mtx", NULL, MTX_DEF);
776 TASK_INIT(&ng_btsocket_hci_raw_task, 0,
777 ng_btsocket_hci_raw_input, NULL);
778
779 /* Create list of sockets */
780 LIST_INIT(&ng_btsocket_hci_raw_sockets);
781 mtx_init(&ng_btsocket_hci_raw_sockets_mtx,
782 "btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF);
783
784 /* Tokens */
785 ng_btsocket_hci_raw_token = 0;
786 mtx_init(&ng_btsocket_hci_raw_token_mtx,
787 "btsocks_hci_raw_token_mtx", NULL, MTX_DEF);
788
789 /*
790 * Security filter
791 * XXX never free()ed
792 */
793 ng_btsocket_hci_raw_sec_filter =
794 malloc(sizeof(struct ng_btsocket_hci_raw_sec_filter),
795 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
796 if (ng_btsocket_hci_raw_sec_filter == NULL) {
797 printf("%s: Could not allocate security filter!\n", __func__);
798 return;
799 }
800
801 /*
802 * XXX How paranoid can we get?
803 *
804 * Initialize security filter. If bit is set in the mask then
805 * unprivileged socket is allowed to send (receive) this command
806 * (event).
807 */
808
809 /* Enable all events */
810 memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
811 sizeof(ng_btsocket_hci_raw_sec_filter->events)/
812 sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
813
814 /* Disable some critical events */
815 f = ng_btsocket_hci_raw_sec_filter->events;
816 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
817 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
818 bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
819
820 /* Commands - Link control */
821 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
822 bit_set(f, NG_HCI_OCF_INQUIRY - 1);
823 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
824 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
825 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
826 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
827 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
828 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
829 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
830
831 /* Commands - Link policy */
832 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
833 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
834 bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
835
836 /* Commands - Host controller and baseband */
837 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
838 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
839 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
840 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
841 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
842 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
843 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
844 bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
845 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
846 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
847 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
848 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
849 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
850 bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
851 bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
852 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
853 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
854 bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
855 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
856 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
857 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
858 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
859 bit_set(f, NG_HCI_OCF_READ_LE_HOST_SUPPORTED -1);
860
861 /* Commands - Informational */
862 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
863 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
864 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
865 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
866 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
867 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
868
869 /* Commands - Status */
870 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
871 bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
872 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
873 bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
874
875 /* Commands - Testing */
876 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
877 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
878 /*Commands - LE*/
879 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LE -1];
880 bit_set(f, NG_HCI_OCF_LE_SET_SCAN_ENABLE - 1);
881 bit_set(f, NG_HCI_OCF_LE_SET_SCAN_PARAMETERS - 1);
882 bit_set(f, NG_HCI_OCF_LE_READ_LOCAL_SUPPORTED_FEATURES - 1);
883 bit_set(f, NG_HCI_OCF_LE_READ_BUFFER_SIZE - 1);
884 bit_set(f, NG_HCI_OCF_LE_READ_WHITE_LIST_SIZE - 1);
885
886 } /* ng_btsocket_hci_raw_init */
887 SYSINIT(ng_btsocket_hci_raw_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
888 ng_btsocket_hci_raw_init, NULL);
889
890 /*
891 * Abort connection on socket
892 */
893
894 void
ng_btsocket_hci_raw_abort(struct socket * so)895 ng_btsocket_hci_raw_abort(struct socket *so)
896 {
897 } /* ng_btsocket_hci_raw_abort */
898
899 void
ng_btsocket_hci_raw_close(struct socket * so)900 ng_btsocket_hci_raw_close(struct socket *so)
901 {
902 } /* ng_btsocket_hci_raw_close */
903
904 /*
905 * Create new raw HCI socket
906 */
907
908 int
ng_btsocket_hci_raw_attach(struct socket * so,int proto,struct thread * td)909 ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
910 {
911 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
912 int error = 0;
913
914 if (pcb != NULL)
915 return (EISCONN);
916
917 if (ng_btsocket_hci_raw_node == NULL)
918 return (EPROTONOSUPPORT);
919 if (proto != BLUETOOTH_PROTO_HCI)
920 return (EPROTONOSUPPORT);
921 if (so->so_type != SOCK_RAW)
922 return (ESOCKTNOSUPPORT);
923
924 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
925 NG_BTSOCKET_HCI_RAW_RECVSPACE);
926 if (error != 0)
927 return (error);
928
929 pcb = malloc(sizeof(*pcb),
930 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
931 if (pcb == NULL)
932 return (ENOMEM);
933
934 so->so_pcb = (caddr_t) pcb;
935 pcb->so = so;
936
937 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
938 pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
939
940 /*
941 * Set default socket filter. By default socket only accepts HCI
942 * Command_Complete and Command_Status event packets.
943 */
944
945 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
946 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
947
948 mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
949
950 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
951 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
952 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
953
954 return (0);
955 } /* ng_btsocket_hci_raw_attach */
956
957 /*
958 * Bind raw HCI socket
959 */
960
961 int
ng_btsocket_hci_raw_bind(struct socket * so,struct sockaddr * nam,struct thread * td)962 ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
963 struct thread *td)
964 {
965 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
966 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
967
968 if (pcb == NULL)
969 return (EINVAL);
970 if (ng_btsocket_hci_raw_node == NULL)
971 return (EINVAL);
972
973 if (sa == NULL)
974 return (EINVAL);
975 if (sa->hci_family != AF_BLUETOOTH)
976 return (EAFNOSUPPORT);
977 if (sa->hci_len != sizeof(*sa))
978 return (EINVAL);
979 if (sa->hci_node[0] == 0)
980 return (EINVAL);
981
982 mtx_lock(&pcb->pcb_mtx);
983 bcopy(sa, &pcb->addr, sizeof(pcb->addr));
984 mtx_unlock(&pcb->pcb_mtx);
985
986 return (0);
987 } /* ng_btsocket_hci_raw_bind */
988
989 /*
990 * Connect raw HCI socket
991 */
992
993 int
ng_btsocket_hci_raw_connect(struct socket * so,struct sockaddr * nam,struct thread * td)994 ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
995 struct thread *td)
996 {
997 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
998 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
999
1000 if (pcb == NULL)
1001 return (EINVAL);
1002 if (ng_btsocket_hci_raw_node == NULL)
1003 return (EINVAL);
1004
1005 if (sa == NULL)
1006 return (EINVAL);
1007 if (sa->hci_family != AF_BLUETOOTH)
1008 return (EAFNOSUPPORT);
1009 if (sa->hci_len != sizeof(*sa))
1010 return (EINVAL);
1011 if (sa->hci_node[0] == 0)
1012 return (EDESTADDRREQ);
1013
1014 mtx_lock(&pcb->pcb_mtx);
1015
1016 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
1017 mtx_unlock(&pcb->pcb_mtx);
1018 return (EADDRNOTAVAIL);
1019 }
1020
1021 soisconnected(so);
1022
1023 mtx_unlock(&pcb->pcb_mtx);
1024
1025 return (0);
1026 } /* ng_btsocket_hci_raw_connect */
1027
1028 /*
1029 * Process ioctl on socket
1030 */
1031
1032 int
ng_btsocket_hci_raw_control(struct socket * so,u_long cmd,void * data,struct ifnet * ifp,struct thread * td)1033 ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, void *data,
1034 struct ifnet *ifp, struct thread *td)
1035 {
1036 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1037 char path[NG_NODESIZ + 1];
1038 struct ng_mesg *msg = NULL;
1039 int error = 0;
1040
1041 if (pcb == NULL)
1042 return (EINVAL);
1043 if (ng_btsocket_hci_raw_node == NULL)
1044 return (EINVAL);
1045
1046 mtx_lock(&pcb->pcb_mtx);
1047
1048 /* Check if we have device name */
1049 if (pcb->addr.hci_node[0] == 0) {
1050 mtx_unlock(&pcb->pcb_mtx);
1051 return (EHOSTUNREACH);
1052 }
1053
1054 /* Check if we have pending ioctl() */
1055 if (pcb->token != 0) {
1056 mtx_unlock(&pcb->pcb_mtx);
1057 return (EBUSY);
1058 }
1059
1060 snprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1061
1062 switch (cmd) {
1063 case SIOC_HCI_RAW_NODE_GET_STATE: {
1064 struct ng_btsocket_hci_raw_node_state *p =
1065 (struct ng_btsocket_hci_raw_node_state *) data;
1066
1067 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1068 NGM_HCI_NODE_GET_STATE,
1069 &p->state, sizeof(p->state));
1070 } break;
1071
1072 case SIOC_HCI_RAW_NODE_INIT:
1073 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1074 error = ng_btsocket_hci_raw_send_ngmsg(path,
1075 NGM_HCI_NODE_INIT, NULL, 0);
1076 else
1077 error = EPERM;
1078 break;
1079
1080 case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1081 struct ng_btsocket_hci_raw_node_debug *p =
1082 (struct ng_btsocket_hci_raw_node_debug *) data;
1083
1084 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1085 NGM_HCI_NODE_GET_DEBUG,
1086 &p->debug, sizeof(p->debug));
1087 } break;
1088
1089 case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1090 struct ng_btsocket_hci_raw_node_debug *p =
1091 (struct ng_btsocket_hci_raw_node_debug *) data;
1092
1093 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1094 error = ng_btsocket_hci_raw_send_ngmsg(path,
1095 NGM_HCI_NODE_SET_DEBUG, &p->debug,
1096 sizeof(p->debug));
1097 else
1098 error = EPERM;
1099 } break;
1100
1101 case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1102 struct ng_btsocket_hci_raw_node_buffer *p =
1103 (struct ng_btsocket_hci_raw_node_buffer *) data;
1104
1105 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1106 NGM_HCI_NODE_GET_BUFFER,
1107 &p->buffer, sizeof(p->buffer));
1108 } break;
1109
1110 case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1111 struct ng_btsocket_hci_raw_node_bdaddr *p =
1112 (struct ng_btsocket_hci_raw_node_bdaddr *) data;
1113
1114 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1115 NGM_HCI_NODE_GET_BDADDR,
1116 &p->bdaddr, sizeof(p->bdaddr));
1117 } break;
1118
1119 case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1120 struct ng_btsocket_hci_raw_node_features *p =
1121 (struct ng_btsocket_hci_raw_node_features *) data;
1122
1123 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1124 NGM_HCI_NODE_GET_FEATURES,
1125 &p->features, sizeof(p->features));
1126 } break;
1127
1128 case SIOC_HCI_RAW_NODE_GET_STAT: {
1129 struct ng_btsocket_hci_raw_node_stat *p =
1130 (struct ng_btsocket_hci_raw_node_stat *) data;
1131
1132 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1133 NGM_HCI_NODE_GET_STAT,
1134 &p->stat, sizeof(p->stat));
1135 } break;
1136
1137 case SIOC_HCI_RAW_NODE_RESET_STAT:
1138 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1139 error = ng_btsocket_hci_raw_send_ngmsg(path,
1140 NGM_HCI_NODE_RESET_STAT, NULL, 0);
1141 else
1142 error = EPERM;
1143 break;
1144
1145 case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1146 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1147 error = ng_btsocket_hci_raw_send_ngmsg(path,
1148 NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1149 NULL, 0);
1150 else
1151 error = EPERM;
1152 break;
1153
1154 case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE: {
1155 struct ng_btsocket_hci_raw_node_neighbor_cache *p =
1156 (struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1157 ng_hci_node_get_neighbor_cache_ep *p1 = NULL;
1158 ng_hci_node_neighbor_cache_entry_ep *p2 = NULL;
1159
1160 if (p->num_entries <= 0 ||
1161 p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1162 p->entries == NULL) {
1163 mtx_unlock(&pcb->pcb_mtx);
1164 return (EINVAL);
1165 }
1166
1167 NG_MKMESSAGE(msg, NGM_HCI_COOKIE,
1168 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_NOWAIT);
1169 if (msg == NULL) {
1170 mtx_unlock(&pcb->pcb_mtx);
1171 return (ENOMEM);
1172 }
1173 ng_btsocket_hci_raw_get_token(&msg->header.token);
1174 pcb->token = msg->header.token;
1175 pcb->msg = NULL;
1176
1177 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1178 if (error != 0) {
1179 pcb->token = 0;
1180 mtx_unlock(&pcb->pcb_mtx);
1181 return (error);
1182 }
1183
1184 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1185 PZERO|PCATCH, "hcictl",
1186 ng_btsocket_hci_raw_ioctl_timeout * hz);
1187 pcb->token = 0;
1188
1189 if (error != 0) {
1190 mtx_unlock(&pcb->pcb_mtx);
1191 return (error);
1192 }
1193
1194 msg = pcb->msg;
1195 pcb->msg = NULL;
1196
1197 mtx_unlock(&pcb->pcb_mtx);
1198
1199 if (msg != NULL &&
1200 msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1201 /* Return data back to user space */
1202 p1 = (ng_hci_node_get_neighbor_cache_ep *)(msg->data);
1203 p2 = (ng_hci_node_neighbor_cache_entry_ep *)(p1 + 1);
1204
1205 p->num_entries = min(p->num_entries, p1->num_entries);
1206 if (p->num_entries > 0)
1207 error = copyout((caddr_t) p2,
1208 (caddr_t) p->entries,
1209 p->num_entries * sizeof(*p2));
1210 } else
1211 error = EINVAL;
1212
1213 NG_FREE_MSG(msg); /* checks for != NULL */
1214 return (error);
1215 } /* NOTREACHED */
1216
1217 case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1218 struct ng_btsocket_hci_raw_con_list *p =
1219 (struct ng_btsocket_hci_raw_con_list *) data;
1220 ng_hci_node_con_list_ep *p1 = NULL;
1221 ng_hci_node_con_ep *p2 = NULL;
1222
1223 if (p->num_connections == 0 ||
1224 p->num_connections > NG_HCI_MAX_CON_NUM ||
1225 p->connections == NULL) {
1226 mtx_unlock(&pcb->pcb_mtx);
1227 return (EINVAL);
1228 }
1229
1230 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1231 0, M_NOWAIT);
1232 if (msg == NULL) {
1233 mtx_unlock(&pcb->pcb_mtx);
1234 return (ENOMEM);
1235 }
1236 ng_btsocket_hci_raw_get_token(&msg->header.token);
1237 pcb->token = msg->header.token;
1238 pcb->msg = NULL;
1239
1240 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1241 if (error != 0) {
1242 pcb->token = 0;
1243 mtx_unlock(&pcb->pcb_mtx);
1244 return (error);
1245 }
1246
1247 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1248 PZERO|PCATCH, "hcictl",
1249 ng_btsocket_hci_raw_ioctl_timeout * hz);
1250 pcb->token = 0;
1251
1252 if (error != 0) {
1253 mtx_unlock(&pcb->pcb_mtx);
1254 return (error);
1255 }
1256
1257 msg = pcb->msg;
1258 pcb->msg = NULL;
1259
1260 mtx_unlock(&pcb->pcb_mtx);
1261
1262 if (msg != NULL &&
1263 msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1264 /* Return data back to user space */
1265 p1 = (ng_hci_node_con_list_ep *)(msg->data);
1266 p2 = (ng_hci_node_con_ep *)(p1 + 1);
1267
1268 p->num_connections = min(p->num_connections,
1269 p1->num_connections);
1270 if (p->num_connections > 0)
1271 error = copyout((caddr_t) p2,
1272 (caddr_t) p->connections,
1273 p->num_connections * sizeof(*p2));
1274 } else
1275 error = EINVAL;
1276
1277 NG_FREE_MSG(msg); /* checks for != NULL */
1278 return (error);
1279 } /* NOTREACHED */
1280
1281 case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: {
1282 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1283 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1284 data;
1285
1286 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1287 NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1288 &p->policy_mask, sizeof(p->policy_mask));
1289 } break;
1290
1291 case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1292 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1293 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1294 data;
1295
1296 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1297 error = ng_btsocket_hci_raw_send_ngmsg(path,
1298 NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1299 &p->policy_mask,
1300 sizeof(p->policy_mask));
1301 else
1302 error = EPERM;
1303 } break;
1304
1305 case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1306 struct ng_btsocket_hci_raw_node_packet_mask *p =
1307 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1308
1309 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1310 NGM_HCI_NODE_GET_PACKET_MASK,
1311 &p->packet_mask, sizeof(p->packet_mask));
1312 } break;
1313
1314 case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1315 struct ng_btsocket_hci_raw_node_packet_mask *p =
1316 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1317
1318 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1319 error = ng_btsocket_hci_raw_send_ngmsg(path,
1320 NGM_HCI_NODE_SET_PACKET_MASK,
1321 &p->packet_mask,
1322 sizeof(p->packet_mask));
1323 else
1324 error = EPERM;
1325 } break;
1326
1327 case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1328 struct ng_btsocket_hci_raw_node_role_switch *p =
1329 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1330
1331 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1332 NGM_HCI_NODE_GET_ROLE_SWITCH,
1333 &p->role_switch, sizeof(p->role_switch));
1334 } break;
1335
1336 case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1337 struct ng_btsocket_hci_raw_node_role_switch *p =
1338 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1339
1340 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1341 error = ng_btsocket_hci_raw_send_ngmsg(path,
1342 NGM_HCI_NODE_SET_ROLE_SWITCH,
1343 &p->role_switch,
1344 sizeof(p->role_switch));
1345 else
1346 error = EPERM;
1347 } break;
1348
1349 case SIOC_HCI_RAW_NODE_LIST_NAMES: {
1350 struct ng_btsocket_hci_raw_node_list_names *nl =
1351 (struct ng_btsocket_hci_raw_node_list_names *) data;
1352 struct nodeinfo *ni = nl->names;
1353
1354 if (nl->num_names == 0) {
1355 mtx_unlock(&pcb->pcb_mtx);
1356 return (EINVAL);
1357 }
1358
1359 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
1360 0, M_NOWAIT);
1361 if (msg == NULL) {
1362 mtx_unlock(&pcb->pcb_mtx);
1363 return (ENOMEM);
1364 }
1365 ng_btsocket_hci_raw_get_token(&msg->header.token);
1366 pcb->token = msg->header.token;
1367 pcb->msg = NULL;
1368
1369 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, ".:", 0);
1370 if (error != 0) {
1371 pcb->token = 0;
1372 mtx_unlock(&pcb->pcb_mtx);
1373 return (error);
1374 }
1375
1376 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1377 PZERO|PCATCH, "hcictl",
1378 ng_btsocket_hci_raw_ioctl_timeout * hz);
1379 pcb->token = 0;
1380
1381 if (error != 0) {
1382 mtx_unlock(&pcb->pcb_mtx);
1383 return (error);
1384 }
1385
1386 msg = pcb->msg;
1387 pcb->msg = NULL;
1388
1389 mtx_unlock(&pcb->pcb_mtx);
1390
1391 if (msg != NULL && msg->header.cmd == NGM_LISTNAMES) {
1392 /* Return data back to user space */
1393 struct namelist *nl1 = (struct namelist *) msg->data;
1394 struct nodeinfo *ni1 = &nl1->nodeinfo[0];
1395
1396 while (nl->num_names > 0 && nl1->numnames > 0) {
1397 if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1398 error = copyout((caddr_t) ni1,
1399 (caddr_t) ni,
1400 sizeof(*ni));
1401 if (error != 0)
1402 break;
1403
1404 nl->num_names --;
1405 ni ++;
1406 }
1407
1408 nl1->numnames --;
1409 ni1 ++;
1410 }
1411
1412 nl->num_names = ni - nl->names;
1413 } else
1414 error = EINVAL;
1415
1416 NG_FREE_MSG(msg); /* checks for != NULL */
1417 return (error);
1418 } /* NOTREACHED */
1419
1420 default:
1421 error = EINVAL;
1422 break;
1423 }
1424
1425 mtx_unlock(&pcb->pcb_mtx);
1426
1427 return (error);
1428 } /* ng_btsocket_hci_raw_control */
1429
1430 /*
1431 * Process getsockopt/setsockopt system calls
1432 */
1433
1434 int
ng_btsocket_hci_raw_ctloutput(struct socket * so,struct sockopt * sopt)1435 ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1436 {
1437 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1438 struct ng_btsocket_hci_raw_filter filter;
1439 int error = 0, dir;
1440
1441 if (pcb == NULL)
1442 return (EINVAL);
1443 if (ng_btsocket_hci_raw_node == NULL)
1444 return (EINVAL);
1445
1446 if (sopt->sopt_level != SOL_HCI_RAW)
1447 return (0);
1448
1449 mtx_lock(&pcb->pcb_mtx);
1450
1451 switch (sopt->sopt_dir) {
1452 case SOPT_GET:
1453 switch (sopt->sopt_name) {
1454 case SO_HCI_RAW_FILTER:
1455 error = sooptcopyout(sopt, &pcb->filter,
1456 sizeof(pcb->filter));
1457 break;
1458
1459 case SO_HCI_RAW_DIRECTION:
1460 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1461 error = sooptcopyout(sopt, &dir, sizeof(dir));
1462 break;
1463
1464 default:
1465 error = EINVAL;
1466 break;
1467 }
1468 break;
1469
1470 case SOPT_SET:
1471 switch (sopt->sopt_name) {
1472 case SO_HCI_RAW_FILTER:
1473 error = sooptcopyin(sopt, &filter, sizeof(filter),
1474 sizeof(filter));
1475 if (error == 0)
1476 bcopy(&filter, &pcb->filter,
1477 sizeof(pcb->filter));
1478 break;
1479
1480 case SO_HCI_RAW_DIRECTION:
1481 error = sooptcopyin(sopt, &dir, sizeof(dir),
1482 sizeof(dir));
1483 if (error != 0)
1484 break;
1485
1486 if (dir)
1487 pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1488 else
1489 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1490 break;
1491
1492 default:
1493 error = EINVAL;
1494 break;
1495 }
1496 break;
1497
1498 default:
1499 error = EINVAL;
1500 break;
1501 }
1502
1503 mtx_unlock(&pcb->pcb_mtx);
1504
1505 return (error);
1506 } /* ng_btsocket_hci_raw_ctloutput */
1507
1508 /*
1509 * Detach raw HCI socket
1510 */
1511
1512 void
ng_btsocket_hci_raw_detach(struct socket * so)1513 ng_btsocket_hci_raw_detach(struct socket *so)
1514 {
1515 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1516
1517 KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1518
1519 if (ng_btsocket_hci_raw_node == NULL)
1520 return;
1521
1522 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
1523 mtx_lock(&pcb->pcb_mtx);
1524
1525 LIST_REMOVE(pcb, next);
1526
1527 mtx_unlock(&pcb->pcb_mtx);
1528 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
1529
1530 mtx_destroy(&pcb->pcb_mtx);
1531
1532 bzero(pcb, sizeof(*pcb));
1533 free(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1534
1535 so->so_pcb = NULL;
1536 } /* ng_btsocket_hci_raw_detach */
1537
1538 /*
1539 * Disconnect raw HCI socket
1540 */
1541
1542 int
ng_btsocket_hci_raw_disconnect(struct socket * so)1543 ng_btsocket_hci_raw_disconnect(struct socket *so)
1544 {
1545 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1546
1547 if (pcb == NULL)
1548 return (EINVAL);
1549 if (ng_btsocket_hci_raw_node == NULL)
1550 return (EINVAL);
1551
1552 mtx_lock(&pcb->pcb_mtx);
1553 soisdisconnected(so);
1554 mtx_unlock(&pcb->pcb_mtx);
1555
1556 return (0);
1557 } /* ng_btsocket_hci_raw_disconnect */
1558
1559 /*
1560 * Get socket peer's address
1561 */
1562
1563 int
ng_btsocket_hci_raw_peeraddr(struct socket * so,struct sockaddr ** nam)1564 ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1565 {
1566 return (ng_btsocket_hci_raw_sockaddr(so, nam));
1567 } /* ng_btsocket_hci_raw_peeraddr */
1568
1569 /*
1570 * Send data
1571 */
1572
1573 int
ng_btsocket_hci_raw_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * sa,struct mbuf * control,struct thread * td)1574 ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
1575 struct sockaddr *sa, struct mbuf *control, struct thread *td)
1576 {
1577 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1578 struct mbuf *nam = NULL;
1579 int error = 0;
1580
1581 if (ng_btsocket_hci_raw_node == NULL) {
1582 error = ENETDOWN;
1583 goto drop;
1584 }
1585 if (pcb == NULL) {
1586 error = EINVAL;
1587 goto drop;
1588 }
1589 if (control != NULL) {
1590 error = EINVAL;
1591 goto drop;
1592 }
1593
1594 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1595 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1596 error = EMSGSIZE;
1597 goto drop;
1598 }
1599
1600 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1601 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1602 error = ENOBUFS;
1603 goto drop;
1604 }
1605 }
1606 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1607 error = ENOTSUP;
1608 goto drop;
1609 }
1610
1611 if (sa != NULL) {
1612 if (sa->sa_family != AF_BLUETOOTH) {
1613 error = EAFNOSUPPORT;
1614 goto drop;
1615 }
1616 if (sa->sa_len != sizeof(struct sockaddr_hci)) {
1617 error = EINVAL;
1618 goto drop;
1619 }
1620 }
1621
1622 mtx_lock(&pcb->pcb_mtx);
1623
1624 error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1625 if (error != 0) {
1626 mtx_unlock(&pcb->pcb_mtx);
1627 goto drop;
1628 }
1629
1630 if (sa == NULL) {
1631 if (pcb->addr.hci_node[0] == 0) {
1632 mtx_unlock(&pcb->pcb_mtx);
1633 error = EDESTADDRREQ;
1634 goto drop;
1635 }
1636
1637 sa = (struct sockaddr *) &pcb->addr;
1638 }
1639
1640 MGET(nam, M_NOWAIT, MT_SONAME);
1641 if (nam == NULL) {
1642 mtx_unlock(&pcb->pcb_mtx);
1643 error = ENOBUFS;
1644 goto drop;
1645 }
1646
1647 nam->m_len = sizeof(struct sockaddr_hci);
1648 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1649
1650 nam->m_next = m;
1651 m = NULL;
1652
1653 mtx_unlock(&pcb->pcb_mtx);
1654
1655 return (ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1656 ng_btsocket_hci_raw_output, nam, 0));
1657 drop:
1658 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1659 NG_FREE_M(nam);
1660 NG_FREE_M(m);
1661
1662 return (error);
1663 } /* ng_btsocket_hci_raw_send */
1664
1665 /*
1666 * Get socket address
1667 */
1668
1669 int
ng_btsocket_hci_raw_sockaddr(struct socket * so,struct sockaddr ** nam)1670 ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1671 {
1672 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1673 struct sockaddr_hci sa;
1674
1675 if (pcb == NULL)
1676 return (EINVAL);
1677 if (ng_btsocket_hci_raw_node == NULL)
1678 return (EINVAL);
1679
1680 bzero(&sa, sizeof(sa));
1681 sa.hci_len = sizeof(sa);
1682 sa.hci_family = AF_BLUETOOTH;
1683
1684 mtx_lock(&pcb->pcb_mtx);
1685 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1686 mtx_unlock(&pcb->pcb_mtx);
1687
1688 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1689
1690 return ((*nam == NULL)? ENOMEM : 0);
1691 } /* ng_btsocket_hci_raw_sockaddr */
1692