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