xref: /freebsd/sys/netgraph/bluetooth/socket/ng_btsocket_hci_raw.c (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
1 /*
2  * ng_btsocket_hci_raw.c
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
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  * $FreeBSD$
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bitstring.h>
39 #include <sys/domain.h>
40 #include <sys/endian.h>
41 #include <sys/errno.h>
42 #include <sys/filedesc.h>
43 #include <sys/ioccom.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/mutex.h>
49 #include <sys/priv.h>
50 #include <sys/protosw.h>
51 #include <sys/queue.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/taskqueue.h>
56 
57 #include <net/vnet.h>
58 
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/bluetooth/include/ng_bluetooth.h>
62 #include <netgraph/bluetooth/include/ng_hci.h>
63 #include <netgraph/bluetooth/include/ng_l2cap.h>
64 #include <netgraph/bluetooth/include/ng_btsocket.h>
65 #include <netgraph/bluetooth/include/ng_btsocket_hci_raw.h>
66 
67 /* MALLOC define */
68 #ifdef NG_SEPARATE_MALLOC
69 static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_HCI_RAW, "netgraph_btsocks_hci_raw",
70 	"Netgraph Bluetooth raw HCI sockets");
71 #else
72 #define M_NETGRAPH_BTSOCKET_HCI_RAW M_NETGRAPH
73 #endif /* NG_SEPARATE_MALLOC */
74 
75 /* Netgraph node methods */
76 static ng_constructor_t	ng_btsocket_hci_raw_node_constructor;
77 static ng_rcvmsg_t	ng_btsocket_hci_raw_node_rcvmsg;
78 static ng_shutdown_t	ng_btsocket_hci_raw_node_shutdown;
79 static ng_newhook_t	ng_btsocket_hci_raw_node_newhook;
80 static ng_connect_t	ng_btsocket_hci_raw_node_connect;
81 static ng_rcvdata_t	ng_btsocket_hci_raw_node_rcvdata;
82 static ng_disconnect_t	ng_btsocket_hci_raw_node_disconnect;
83 
84 static void 		ng_btsocket_hci_raw_input (void *, int);
85 static void 		ng_btsocket_hci_raw_output(node_p, hook_p, void *, int);
86 static void		ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p,
87 						   struct mbuf **,
88 						   struct mbuf *);
89 static int		ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p,
90 						   struct mbuf *, int);
91 
92 #define ng_btsocket_hci_raw_wakeup_input_task() \
93 	taskqueue_enqueue(taskqueue_swi, &ng_btsocket_hci_raw_task)
94 
95 /* Security filter */
96 struct ng_btsocket_hci_raw_sec_filter {
97 	bitstr_t	bit_decl(events, 0xff);
98 	bitstr_t	bit_decl(commands[0x3f], 0x3ff);
99 };
100 
101 /* Netgraph type descriptor */
102 static struct ng_type typestruct = {
103 	.version =	NG_ABI_VERSION,
104 	.name =		NG_BTSOCKET_HCI_RAW_NODE_TYPE,
105 	.constructor =	ng_btsocket_hci_raw_node_constructor,
106 	.rcvmsg =	ng_btsocket_hci_raw_node_rcvmsg,
107 	.shutdown =	ng_btsocket_hci_raw_node_shutdown,
108 	.newhook =	ng_btsocket_hci_raw_node_newhook,
109 	.connect =	ng_btsocket_hci_raw_node_connect,
110 	.rcvdata =	ng_btsocket_hci_raw_node_rcvdata,
111 	.disconnect =	ng_btsocket_hci_raw_node_disconnect,
112 };
113 
114 /* Globals */
115 static u_int32_t				ng_btsocket_hci_raw_debug_level;
116 static u_int32_t				ng_btsocket_hci_raw_ioctl_timeout;
117 static node_p					ng_btsocket_hci_raw_node;
118 static struct ng_bt_itemq			ng_btsocket_hci_raw_queue;
119 static struct mtx				ng_btsocket_hci_raw_queue_mtx;
120 static struct task				ng_btsocket_hci_raw_task;
121 static LIST_HEAD(, ng_btsocket_hci_raw_pcb)	ng_btsocket_hci_raw_sockets;
122 static struct mtx				ng_btsocket_hci_raw_sockets_mtx;
123 static u_int32_t				ng_btsocket_hci_raw_token;
124 static struct mtx				ng_btsocket_hci_raw_token_mtx;
125 static struct ng_btsocket_hci_raw_sec_filter	*ng_btsocket_hci_raw_sec_filter;
126 static struct timeval				ng_btsocket_hci_raw_lasttime;
127 static int					ng_btsocket_hci_raw_curpps;
128 
129 /* Sysctl tree */
130 SYSCTL_DECL(_net_bluetooth_hci_sockets);
131 static SYSCTL_NODE(_net_bluetooth_hci_sockets, OID_AUTO, raw, CTLFLAG_RW,
132         0, "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
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
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
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
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
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
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
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
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
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
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
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((caddr_t) &dir, sizeof(dir),
463 					SCM_HCI_RAW_DIRECTION, SOL_HCI_RAW);
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((caddr_t) &tv, sizeof(tv),
471 					SCM_TIMESTAMP, SOL_SOCKET);
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
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 
502 		mtx_lock(&pcb->pcb_mtx);
503 
504 		/*
505 		 * If socket was bound then check address and
506 		 *  make sure it matches.
507 		 */
508 
509 		if (pcb->addr.hci_node[0] != 0 &&
510 		    strcmp(sa->hci_node, pcb->addr.hci_node) != 0)
511 			goto next;
512 
513 		/*
514 		 * Check packet against filters
515 		 * XXX do we have to call m_pullup() here?
516 		 */
517 
518 		if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0)
519 			goto next;
520 
521 		/*
522 		 * Make a copy of the packet, append to the socket's
523 		 * receive queue and wakeup socket. sbappendaddr()
524 		 * will check if socket has enough buffer space.
525 		 */
526 
527 		m = m_dup(m0, M_NOWAIT);
528 		if (m != NULL) {
529 			struct mbuf	*ctl = NULL;
530 
531 			ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
532 
533 			if (sbappendaddr(&pcb->so->so_rcv,
534 					(struct sockaddr *) sa, m, ctl))
535 				sorwakeup(pcb->so);
536 			else {
537 				NG_BTSOCKET_HCI_RAW_INFO(
538 "%s: sbappendaddr() failed\n", __func__);
539 
540 				NG_FREE_M(m);
541 				NG_FREE_M(ctl);
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
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
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
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
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 void
731 ng_btsocket_hci_raw_init(void)
732 {
733 	bitstr_t	*f = NULL;
734 	int		 error = 0;
735 
736 	/* Skip initialization of globals for non-default instances. */
737 	if (!IS_DEFAULT_VNET(curvnet))
738 		return;
739 
740 	ng_btsocket_hci_raw_node = NULL;
741 	ng_btsocket_hci_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
742 	ng_btsocket_hci_raw_ioctl_timeout = 5;
743 
744 	/* Register Netgraph node type */
745 	error = ng_newtype(&typestruct);
746 	if (error != 0) {
747 		NG_BTSOCKET_HCI_RAW_ALERT(
748 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
749 
750 		return;
751 	}
752 
753 	/* Create Netgrapg node */
754 	error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
755 	if (error != 0) {
756 		NG_BTSOCKET_HCI_RAW_ALERT(
757 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
758 
759 		ng_btsocket_hci_raw_node = NULL;
760 
761 		return;
762         }
763 
764 	error = ng_name_node(ng_btsocket_hci_raw_node,
765 				NG_BTSOCKET_HCI_RAW_NODE_TYPE);
766 	if (error != 0) {
767 		NG_BTSOCKET_HCI_RAW_ALERT(
768 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
769 
770 		NG_NODE_UNREF(ng_btsocket_hci_raw_node);
771 		ng_btsocket_hci_raw_node = NULL;
772 
773 		return;
774 	}
775 
776 	/* Create input queue */
777 	NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, 300);
778 	mtx_init(&ng_btsocket_hci_raw_queue_mtx,
779 		"btsocks_hci_raw_queue_mtx", NULL, MTX_DEF);
780 	TASK_INIT(&ng_btsocket_hci_raw_task, 0,
781 		ng_btsocket_hci_raw_input, NULL);
782 
783 	/* Create list of sockets */
784 	LIST_INIT(&ng_btsocket_hci_raw_sockets);
785 	mtx_init(&ng_btsocket_hci_raw_sockets_mtx,
786 		"btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF);
787 
788 	/* Tokens */
789 	ng_btsocket_hci_raw_token = 0;
790 	mtx_init(&ng_btsocket_hci_raw_token_mtx,
791 		"btsocks_hci_raw_token_mtx", NULL, MTX_DEF);
792 
793 	/*
794 	 * Security filter
795 	 * XXX never free()ed
796 	 */
797 	ng_btsocket_hci_raw_sec_filter =
798 	    malloc(sizeof(struct ng_btsocket_hci_raw_sec_filter),
799 		M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
800 	if (ng_btsocket_hci_raw_sec_filter == NULL) {
801 		printf("%s: Could not allocate security filter!\n", __func__);
802 		return;
803 	}
804 
805 	/*
806 	 * XXX How paranoid can we get?
807 	 *
808 	 * Initialize security filter. If bit is set in the mask then
809 	 * unprivileged socket is allowed to send (receive) this command
810 	 * (event).
811 	 */
812 
813 	/* Enable all events */
814 	memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
815 		sizeof(ng_btsocket_hci_raw_sec_filter->events)/
816 			sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
817 
818 	/* Disable some critical events */
819 	f = ng_btsocket_hci_raw_sec_filter->events;
820 	bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
821 	bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
822 	bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
823 
824 	/* Commands - Link control */
825 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
826 	bit_set(f, NG_HCI_OCF_INQUIRY - 1);
827 	bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
828 	bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
829 	bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
830 	bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
831 	bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
832 	bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
833 	bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
834 
835 	/* Commands - Link policy */
836 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
837 	bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
838 	bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
839 
840 	/* Commands - Host controller and baseband */
841 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
842 	bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
843 	bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
844 	bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
845 	bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
846 	bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
847 	bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
848 	bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
849 	bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
850 	bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
851 	bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
852 	bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
853 	bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
854 	bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
855 	bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
856 	bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
857 	bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
858 	bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
859 	bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
860 	bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
861 	bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
862 	bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
863 
864 	/* Commands - Informational */
865 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
866 	bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
867 	bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
868 	bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
869 	bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
870 	bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
871 
872 	/* Commands - Status */
873 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
874 	bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
875 	bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
876 	bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
877 
878 	/* Commands - Testing */
879 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
880 	bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
881 	/*Commands - LE*/
882 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LE -1];
883 
884 } /* ng_btsocket_hci_raw_init */
885 
886 /*
887  * Abort connection on socket
888  */
889 
890 void
891 ng_btsocket_hci_raw_abort(struct socket *so)
892 {
893 } /* ng_btsocket_hci_raw_abort */
894 
895 void
896 ng_btsocket_hci_raw_close(struct socket *so)
897 {
898 } /* ng_btsocket_hci_raw_close */
899 
900 /*
901  * Create new raw HCI socket
902  */
903 
904 int
905 ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
906 {
907 	ng_btsocket_hci_raw_pcb_p	pcb = so2hci_raw_pcb(so);
908 	int				error = 0;
909 
910 	if (pcb != NULL)
911 		return (EISCONN);
912 
913 	if (ng_btsocket_hci_raw_node == NULL)
914 		return (EPROTONOSUPPORT);
915 	if (proto != BLUETOOTH_PROTO_HCI)
916 		return (EPROTONOSUPPORT);
917 	if (so->so_type != SOCK_RAW)
918 		return (ESOCKTNOSUPPORT);
919 
920 	error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
921 				NG_BTSOCKET_HCI_RAW_RECVSPACE);
922 	if (error != 0)
923 		return (error);
924 
925 	pcb = malloc(sizeof(*pcb),
926 		M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
927 	if (pcb == NULL)
928 		return (ENOMEM);
929 
930 	so->so_pcb = (caddr_t) pcb;
931 	pcb->so = so;
932 
933 	if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
934 		pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
935 
936 	/*
937 	 * Set default socket filter. By default socket only accepts HCI
938 	 * Command_Complete and Command_Status event packets.
939 	 */
940 
941 	bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
942 	bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
943 
944 	mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
945 
946 	mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
947 	LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
948 	mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
949 
950 	return (0);
951 } /* ng_btsocket_hci_raw_attach */
952 
953 /*
954  * Bind raw HCI socket
955  */
956 
957 int
958 ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
959 		struct thread *td)
960 {
961 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
962 	struct sockaddr_hci		*sa = (struct sockaddr_hci *) nam;
963 
964 	if (pcb == NULL)
965 		return (EINVAL);
966 	if (ng_btsocket_hci_raw_node == NULL)
967 		return (EINVAL);
968 
969 	if (sa == NULL)
970 		return (EINVAL);
971 	if (sa->hci_family != AF_BLUETOOTH)
972 		return (EAFNOSUPPORT);
973 	if (sa->hci_len != sizeof(*sa))
974 		return (EINVAL);
975 	if (sa->hci_node[0] == 0)
976 		return (EINVAL);
977 
978 	mtx_lock(&pcb->pcb_mtx);
979 	bcopy(sa, &pcb->addr, sizeof(pcb->addr));
980 	mtx_unlock(&pcb->pcb_mtx);
981 
982 	return (0);
983 } /* ng_btsocket_hci_raw_bind */
984 
985 /*
986  * Connect raw HCI socket
987  */
988 
989 int
990 ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
991 		struct thread *td)
992 {
993 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
994 	struct sockaddr_hci		*sa = (struct sockaddr_hci *) nam;
995 
996 	if (pcb == NULL)
997 		return (EINVAL);
998 	if (ng_btsocket_hci_raw_node == NULL)
999 		return (EINVAL);
1000 
1001 	if (sa == NULL)
1002 		return (EINVAL);
1003 	if (sa->hci_family != AF_BLUETOOTH)
1004 		return (EAFNOSUPPORT);
1005 	if (sa->hci_len != sizeof(*sa))
1006 		return (EINVAL);
1007 	if (sa->hci_node[0] == 0)
1008 		return (EDESTADDRREQ);
1009 
1010 	mtx_lock(&pcb->pcb_mtx);
1011 
1012 	if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
1013 		mtx_unlock(&pcb->pcb_mtx);
1014 		return (EADDRNOTAVAIL);
1015 	}
1016 
1017 	soisconnected(so);
1018 
1019 	mtx_unlock(&pcb->pcb_mtx);
1020 
1021 	return (0);
1022 } /* ng_btsocket_hci_raw_connect */
1023 
1024 /*
1025  * Process ioctl on socket
1026  */
1027 
1028 int
1029 ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
1030 		struct ifnet *ifp, struct thread *td)
1031 {
1032 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
1033 	char				 path[NG_NODESIZ + 1];
1034 	struct ng_mesg			*msg = NULL;
1035 	int				 error = 0;
1036 
1037 	if (pcb == NULL)
1038 		return (EINVAL);
1039 	if (ng_btsocket_hci_raw_node == NULL)
1040 		return (EINVAL);
1041 
1042 	mtx_lock(&pcb->pcb_mtx);
1043 
1044 	/* Check if we have device name */
1045 	if (pcb->addr.hci_node[0] == 0) {
1046 		mtx_unlock(&pcb->pcb_mtx);
1047 		return (EHOSTUNREACH);
1048 	}
1049 
1050 	/* Check if we have pending ioctl() */
1051 	if (pcb->token != 0) {
1052 		mtx_unlock(&pcb->pcb_mtx);
1053 		return (EBUSY);
1054 	}
1055 
1056 	snprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1057 
1058 	switch (cmd) {
1059 	case SIOC_HCI_RAW_NODE_GET_STATE: {
1060 		struct ng_btsocket_hci_raw_node_state	*p =
1061 			(struct ng_btsocket_hci_raw_node_state *) data;
1062 
1063 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1064 				NGM_HCI_NODE_GET_STATE,
1065 				&p->state, sizeof(p->state));
1066 		} break;
1067 
1068 	case SIOC_HCI_RAW_NODE_INIT:
1069 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1070 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1071 					NGM_HCI_NODE_INIT, NULL, 0);
1072 		else
1073 			error = EPERM;
1074 		break;
1075 
1076 	case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1077 		struct ng_btsocket_hci_raw_node_debug	*p =
1078 			(struct ng_btsocket_hci_raw_node_debug *) data;
1079 
1080 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1081 				NGM_HCI_NODE_GET_DEBUG,
1082 				&p->debug, sizeof(p->debug));
1083 		} break;
1084 
1085 	case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1086 		struct ng_btsocket_hci_raw_node_debug	*p =
1087 			(struct ng_btsocket_hci_raw_node_debug *) data;
1088 
1089 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1090 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1091 					NGM_HCI_NODE_SET_DEBUG, &p->debug,
1092 					sizeof(p->debug));
1093 		else
1094 			error = EPERM;
1095 		} break;
1096 
1097 	case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1098 		struct ng_btsocket_hci_raw_node_buffer	*p =
1099 			(struct ng_btsocket_hci_raw_node_buffer *) data;
1100 
1101 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1102 				NGM_HCI_NODE_GET_BUFFER,
1103 				&p->buffer, sizeof(p->buffer));
1104 		} break;
1105 
1106 	case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1107 		struct ng_btsocket_hci_raw_node_bdaddr	*p =
1108 			(struct ng_btsocket_hci_raw_node_bdaddr *) data;
1109 
1110 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1111 				NGM_HCI_NODE_GET_BDADDR,
1112 				&p->bdaddr, sizeof(p->bdaddr));
1113 		} break;
1114 
1115 	case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1116 		struct ng_btsocket_hci_raw_node_features	*p =
1117 			(struct ng_btsocket_hci_raw_node_features *) data;
1118 
1119 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1120 				NGM_HCI_NODE_GET_FEATURES,
1121 				&p->features, sizeof(p->features));
1122 		} break;
1123 
1124 	case SIOC_HCI_RAW_NODE_GET_STAT: {
1125 		struct ng_btsocket_hci_raw_node_stat	*p =
1126 			(struct ng_btsocket_hci_raw_node_stat *) data;
1127 
1128 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1129 				NGM_HCI_NODE_GET_STAT,
1130 				&p->stat, sizeof(p->stat));
1131 		} break;
1132 
1133 	case SIOC_HCI_RAW_NODE_RESET_STAT:
1134 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1135 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1136 					NGM_HCI_NODE_RESET_STAT, NULL, 0);
1137 		else
1138 			error = EPERM;
1139 		break;
1140 
1141 	case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1142 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1143 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1144 					NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1145 					NULL, 0);
1146 		else
1147 			error = EPERM;
1148 		break;
1149 
1150 	case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE:  {
1151 		struct ng_btsocket_hci_raw_node_neighbor_cache	*p =
1152 			(struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1153 		ng_hci_node_get_neighbor_cache_ep		*p1 = NULL;
1154 		ng_hci_node_neighbor_cache_entry_ep		*p2 = NULL;
1155 
1156 		if (p->num_entries <= 0 ||
1157 		    p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1158 		    p->entries == NULL) {
1159 			error = EINVAL;
1160 			break;
1161 		}
1162 
1163 		NG_MKMESSAGE(msg, NGM_HCI_COOKIE,
1164 			NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_NOWAIT);
1165 		if (msg == NULL) {
1166 			error = ENOMEM;
1167 			break;
1168 		}
1169 		ng_btsocket_hci_raw_get_token(&msg->header.token);
1170 		pcb->token = msg->header.token;
1171 		pcb->msg = NULL;
1172 
1173 		NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1174 		if (error != 0) {
1175 			pcb->token = 0;
1176 			break;
1177 		}
1178 
1179 		error = msleep(&pcb->msg, &pcb->pcb_mtx,
1180 				PZERO|PCATCH, "hcictl",
1181 				ng_btsocket_hci_raw_ioctl_timeout * hz);
1182 		pcb->token = 0;
1183 
1184 		if (error != 0)
1185 			break;
1186 
1187 		if (pcb->msg != NULL &&
1188 		    pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1189 			/* Return data back to user space */
1190 			p1 = (ng_hci_node_get_neighbor_cache_ep *)
1191 				(pcb->msg->data);
1192 			p2 = (ng_hci_node_neighbor_cache_entry_ep *)
1193 				(p1 + 1);
1194 
1195 			p->num_entries = min(p->num_entries, p1->num_entries);
1196 			if (p->num_entries > 0)
1197 				error = copyout((caddr_t) p2,
1198 						(caddr_t) p->entries,
1199 						p->num_entries * sizeof(*p2));
1200 		} else
1201 			error = EINVAL;
1202 
1203 		NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1204 		}break;
1205 
1206 	case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1207 		struct ng_btsocket_hci_raw_con_list	*p =
1208 			(struct ng_btsocket_hci_raw_con_list *) data;
1209 		ng_hci_node_con_list_ep			*p1 = NULL;
1210 		ng_hci_node_con_ep			*p2 = NULL;
1211 
1212 		if (p->num_connections == 0 ||
1213 		    p->num_connections > NG_HCI_MAX_CON_NUM ||
1214 		    p->connections == NULL) {
1215 			error = EINVAL;
1216 			break;
1217 		}
1218 
1219 		NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1220 			0, M_NOWAIT);
1221 		if (msg == NULL) {
1222 			error = ENOMEM;
1223 			break;
1224 		}
1225 		ng_btsocket_hci_raw_get_token(&msg->header.token);
1226 		pcb->token = msg->header.token;
1227 		pcb->msg = NULL;
1228 
1229 		NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1230 		if (error != 0) {
1231 			pcb->token = 0;
1232 			break;
1233 		}
1234 
1235 		error = msleep(&pcb->msg, &pcb->pcb_mtx,
1236 				PZERO|PCATCH, "hcictl",
1237 				ng_btsocket_hci_raw_ioctl_timeout * hz);
1238 		pcb->token = 0;
1239 
1240 		if (error != 0)
1241 			break;
1242 
1243 		if (pcb->msg != NULL &&
1244 		    pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1245 			/* Return data back to user space */
1246 			p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data);
1247 			p2 = (ng_hci_node_con_ep *)(p1 + 1);
1248 
1249 			p->num_connections = min(p->num_connections,
1250 						p1->num_connections);
1251 			if (p->num_connections > 0)
1252 				error = copyout((caddr_t) p2,
1253 					(caddr_t) p->connections,
1254 					p->num_connections * sizeof(*p2));
1255 		} else
1256 			error = EINVAL;
1257 
1258 		NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1259 		} break;
1260 
1261 	case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: {
1262 		struct ng_btsocket_hci_raw_node_link_policy_mask	*p =
1263 			(struct ng_btsocket_hci_raw_node_link_policy_mask *)
1264 				data;
1265 
1266 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1267 				NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1268 				&p->policy_mask, sizeof(p->policy_mask));
1269 		} break;
1270 
1271 	case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1272 		struct ng_btsocket_hci_raw_node_link_policy_mask	*p =
1273 			(struct ng_btsocket_hci_raw_node_link_policy_mask *)
1274 				data;
1275 
1276 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1277 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1278 					NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1279 					&p->policy_mask,
1280 					sizeof(p->policy_mask));
1281 		else
1282 			error = EPERM;
1283 		} break;
1284 
1285 	case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1286 		struct ng_btsocket_hci_raw_node_packet_mask	*p =
1287 			(struct ng_btsocket_hci_raw_node_packet_mask *) data;
1288 
1289 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1290 				NGM_HCI_NODE_GET_PACKET_MASK,
1291 				&p->packet_mask, sizeof(p->packet_mask));
1292 		} break;
1293 
1294 	case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1295 		struct ng_btsocket_hci_raw_node_packet_mask	*p =
1296 			(struct ng_btsocket_hci_raw_node_packet_mask *) data;
1297 
1298 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1299 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1300 					NGM_HCI_NODE_SET_PACKET_MASK,
1301 					&p->packet_mask,
1302 					sizeof(p->packet_mask));
1303 		else
1304 			error = EPERM;
1305 		} break;
1306 
1307 	case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1308 		struct ng_btsocket_hci_raw_node_role_switch	*p =
1309 			(struct ng_btsocket_hci_raw_node_role_switch *) data;
1310 
1311 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1312 				NGM_HCI_NODE_GET_ROLE_SWITCH,
1313 				&p->role_switch, sizeof(p->role_switch));
1314 		} break;
1315 
1316 	case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1317 		struct ng_btsocket_hci_raw_node_role_switch	*p =
1318 			(struct ng_btsocket_hci_raw_node_role_switch *) data;
1319 
1320 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1321 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1322 					NGM_HCI_NODE_SET_ROLE_SWITCH,
1323 					&p->role_switch,
1324 					sizeof(p->role_switch));
1325 		else
1326 			error = EPERM;
1327 		} break;
1328 
1329 	case SIOC_HCI_RAW_NODE_LIST_NAMES: {
1330 		struct ng_btsocket_hci_raw_node_list_names	*nl =
1331 			(struct ng_btsocket_hci_raw_node_list_names *) data;
1332 		struct nodeinfo					*ni = nl->names;
1333 
1334 		if (nl->num_names == 0) {
1335 			error = EINVAL;
1336 			break;
1337 		}
1338 
1339 		NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
1340 			0, M_NOWAIT);
1341 		if (msg == NULL) {
1342 			error = ENOMEM;
1343 			break;
1344 		}
1345 		ng_btsocket_hci_raw_get_token(&msg->header.token);
1346 		pcb->token = msg->header.token;
1347 		pcb->msg = NULL;
1348 
1349 		NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, ".:", 0);
1350 		if (error != 0) {
1351 			pcb->token = 0;
1352 			break;
1353 		}
1354 
1355 		error = msleep(&pcb->msg, &pcb->pcb_mtx,
1356 				PZERO|PCATCH, "hcictl",
1357 				ng_btsocket_hci_raw_ioctl_timeout * hz);
1358 		pcb->token = 0;
1359 
1360 		if (error != 0)
1361 			break;
1362 
1363 		if (pcb->msg != NULL && pcb->msg->header.cmd == NGM_LISTNAMES) {
1364 			/* Return data back to user space */
1365 			struct namelist	*nl1 = (struct namelist *) pcb->msg->data;
1366 			struct nodeinfo	*ni1 = &nl1->nodeinfo[0];
1367 
1368 			while (nl->num_names > 0 && nl1->numnames > 0) {
1369 				if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1370 					error = copyout((caddr_t) ni1,
1371 							(caddr_t) ni,
1372 							sizeof(*ni));
1373 					if (error != 0)
1374 						break;
1375 
1376 					nl->num_names --;
1377 					ni ++;
1378 				}
1379 
1380 				nl1->numnames --;
1381 				ni1 ++;
1382 			}
1383 
1384 			nl->num_names = ni - nl->names;
1385 		} else
1386 			error = EINVAL;
1387 
1388 		NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1389 		} break;
1390 
1391 	default:
1392 		error = EINVAL;
1393 		break;
1394 	}
1395 
1396 	mtx_unlock(&pcb->pcb_mtx);
1397 
1398 	return (error);
1399 } /* ng_btsocket_hci_raw_control */
1400 
1401 /*
1402  * Process getsockopt/setsockopt system calls
1403  */
1404 
1405 int
1406 ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1407 {
1408 	ng_btsocket_hci_raw_pcb_p		pcb = so2hci_raw_pcb(so);
1409 	struct ng_btsocket_hci_raw_filter	filter;
1410 	int					error = 0, dir;
1411 
1412 	if (pcb == NULL)
1413 		return (EINVAL);
1414 	if (ng_btsocket_hci_raw_node == NULL)
1415 		return (EINVAL);
1416 
1417 	if (sopt->sopt_level != SOL_HCI_RAW)
1418 		return (0);
1419 
1420 	mtx_lock(&pcb->pcb_mtx);
1421 
1422 	switch (sopt->sopt_dir) {
1423 	case SOPT_GET:
1424 		switch (sopt->sopt_name) {
1425 		case SO_HCI_RAW_FILTER:
1426 			error = sooptcopyout(sopt, &pcb->filter,
1427 						sizeof(pcb->filter));
1428 			break;
1429 
1430 		case SO_HCI_RAW_DIRECTION:
1431 			dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1432 			error = sooptcopyout(sopt, &dir, sizeof(dir));
1433 			break;
1434 
1435 		default:
1436 			error = EINVAL;
1437 			break;
1438 		}
1439 		break;
1440 
1441 	case SOPT_SET:
1442 		switch (sopt->sopt_name) {
1443 		case SO_HCI_RAW_FILTER:
1444 			error = sooptcopyin(sopt, &filter, sizeof(filter),
1445 						sizeof(filter));
1446 			if (error == 0)
1447 				bcopy(&filter, &pcb->filter,
1448 						sizeof(pcb->filter));
1449 			break;
1450 
1451 		case SO_HCI_RAW_DIRECTION:
1452 			error = sooptcopyin(sopt, &dir, sizeof(dir),
1453 						sizeof(dir));
1454 			if (error != 0)
1455 				break;
1456 
1457 			if (dir)
1458 				pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1459 			else
1460 				pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1461 			break;
1462 
1463 		default:
1464 			error = EINVAL;
1465 			break;
1466 		}
1467 		break;
1468 
1469 	default:
1470 		error = EINVAL;
1471 		break;
1472 	}
1473 
1474 	mtx_unlock(&pcb->pcb_mtx);
1475 
1476 	return (error);
1477 } /* ng_btsocket_hci_raw_ctloutput */
1478 
1479 /*
1480  * Detach raw HCI socket
1481  */
1482 
1483 void
1484 ng_btsocket_hci_raw_detach(struct socket *so)
1485 {
1486 	ng_btsocket_hci_raw_pcb_p	pcb = so2hci_raw_pcb(so);
1487 
1488 	KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1489 
1490 	if (ng_btsocket_hci_raw_node == NULL)
1491 		return;
1492 
1493 	mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
1494 	mtx_lock(&pcb->pcb_mtx);
1495 
1496 	LIST_REMOVE(pcb, next);
1497 
1498 	mtx_unlock(&pcb->pcb_mtx);
1499 	mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
1500 
1501 	mtx_destroy(&pcb->pcb_mtx);
1502 
1503 	bzero(pcb, sizeof(*pcb));
1504 	free(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1505 
1506 	so->so_pcb = NULL;
1507 } /* ng_btsocket_hci_raw_detach */
1508 
1509 /*
1510  * Disconnect raw HCI socket
1511  */
1512 
1513 int
1514 ng_btsocket_hci_raw_disconnect(struct socket *so)
1515 {
1516 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
1517 
1518 	if (pcb == NULL)
1519 		return (EINVAL);
1520 	if (ng_btsocket_hci_raw_node == NULL)
1521 		return (EINVAL);
1522 
1523 	mtx_lock(&pcb->pcb_mtx);
1524 	soisdisconnected(so);
1525 	mtx_unlock(&pcb->pcb_mtx);
1526 
1527 	return (0);
1528 } /* ng_btsocket_hci_raw_disconnect */
1529 
1530 /*
1531  * Get socket peer's address
1532  */
1533 
1534 int
1535 ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1536 {
1537 	return (ng_btsocket_hci_raw_sockaddr(so, nam));
1538 } /* ng_btsocket_hci_raw_peeraddr */
1539 
1540 /*
1541  * Send data
1542  */
1543 
1544 int
1545 ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
1546 		struct sockaddr *sa, struct mbuf *control, struct thread *td)
1547 {
1548 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
1549 	struct mbuf			*nam = NULL;
1550 	int				 error = 0;
1551 
1552 	if (ng_btsocket_hci_raw_node == NULL) {
1553 		error = ENETDOWN;
1554 		goto drop;
1555 	}
1556 	if (pcb == NULL) {
1557 		error = EINVAL;
1558 		goto drop;
1559 	}
1560 	if (control != NULL) {
1561 		error = EINVAL;
1562 		goto drop;
1563 	}
1564 
1565 	if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1566 	    m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1567 		error = EMSGSIZE;
1568 		goto drop;
1569 	}
1570 
1571 	if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1572 		if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1573 			error = ENOBUFS;
1574 			goto drop;
1575 		}
1576 	}
1577 	if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1578 		error = ENOTSUP;
1579 		goto drop;
1580 	}
1581 
1582 	mtx_lock(&pcb->pcb_mtx);
1583 
1584 	error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1585 	if (error != 0) {
1586 		mtx_unlock(&pcb->pcb_mtx);
1587 		goto drop;
1588 	}
1589 
1590 	if (sa == NULL) {
1591 		if (pcb->addr.hci_node[0] == 0) {
1592 			mtx_unlock(&pcb->pcb_mtx);
1593 			error = EDESTADDRREQ;
1594 			goto drop;
1595 		}
1596 
1597 		sa = (struct sockaddr *) &pcb->addr;
1598 	}
1599 
1600 	MGET(nam, M_NOWAIT, MT_SONAME);
1601 	if (nam == NULL) {
1602 		mtx_unlock(&pcb->pcb_mtx);
1603 		error = ENOBUFS;
1604 		goto drop;
1605 	}
1606 
1607 	nam->m_len = sizeof(struct sockaddr_hci);
1608 	bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1609 
1610 	nam->m_next = m;
1611 	m = NULL;
1612 
1613 	mtx_unlock(&pcb->pcb_mtx);
1614 
1615 	return (ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1616 				ng_btsocket_hci_raw_output, nam, 0));
1617 drop:
1618 	NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1619 	NG_FREE_M(nam);
1620 	NG_FREE_M(m);
1621 
1622 	return (error);
1623 } /* ng_btsocket_hci_raw_send */
1624 
1625 /*
1626  * Get socket address
1627  */
1628 
1629 int
1630 ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1631 {
1632 	ng_btsocket_hci_raw_pcb_p	pcb = so2hci_raw_pcb(so);
1633 	struct sockaddr_hci		sa;
1634 
1635 	if (pcb == NULL)
1636 		return (EINVAL);
1637 	if (ng_btsocket_hci_raw_node == NULL)
1638 		return (EINVAL);
1639 
1640 	bzero(&sa, sizeof(sa));
1641 	sa.hci_len = sizeof(sa);
1642 	sa.hci_family = AF_BLUETOOTH;
1643 
1644 	mtx_lock(&pcb->pcb_mtx);
1645 	strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1646 	mtx_unlock(&pcb->pcb_mtx);
1647 
1648 	*nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1649 
1650 	return ((*nam == NULL)? ENOMEM : 0);
1651 } /* ng_btsocket_hci_raw_sockaddr */
1652 
1653