xref: /freebsd/sys/netgraph/bluetooth/socket/ng_btsocket_hci_raw.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
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, &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		 error = 0;
252 
253 	/*
254 	 * Check for empty sockets list creates LOR when both sender and
255 	 * receiver device are connected to the same host, so remove it
256 	 * for now
257 	 */
258 
259 	if (msg != NULL &&
260 	    msg->header.typecookie == NGM_HCI_COOKIE &&
261 	    msg->header.flags & NGF_RESP) {
262 		if (msg->header.token == 0) {
263 			NG_FREE_ITEM(item);
264 			return (0);
265 		}
266 
267 		mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
268 		if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
269 			NG_BTSOCKET_HCI_RAW_ERR(
270 "%s: Input queue is full\n", __func__);
271 
272 			NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
273 			NG_FREE_ITEM(item);
274 			error = ENOBUFS;
275 		} else {
276 			NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
277 			error = ng_btsocket_hci_raw_wakeup_input_task();
278 		}
279 		mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
280 	} else {
281 		NG_FREE_ITEM(item);
282 		error = EINVAL;
283 	}
284 
285 	return (error);
286 } /* ng_btsocket_hci_raw_node_rcvmsg */
287 
288 /*
289  * Receive packet from the one of our hook.
290  * Prepend every packet with sockaddr_hci and record sender's node name.
291  * Enqueue item and schedule input task.
292  */
293 
294 static int
295 ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
296 {
297 	struct mbuf	*nam = NULL;
298 	int		 error;
299 
300 	/*
301 	 * Check for empty sockets list creates LOR when both sender and
302 	 * receiver device are connected to the same host, so remove it
303 	 * for now
304 	 */
305 
306 	MGET(nam, M_DONTWAIT, MT_SONAME);
307 	if (nam != NULL) {
308 		struct sockaddr_hci	*sa = mtod(nam, struct sockaddr_hci *);
309 
310 		nam->m_len = sizeof(struct sockaddr_hci);
311 
312 		sa->hci_len = sizeof(*sa);
313 		sa->hci_family = AF_BLUETOOTH;
314 		strlcpy(sa->hci_node, NG_PEER_NODE_NAME(hook),
315 			sizeof(sa->hci_node));
316 
317 		NGI_GET_M(item, nam->m_next);
318 		NGI_M(item) = nam;
319 
320 		mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
321 		if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
322 			NG_BTSOCKET_HCI_RAW_ERR(
323 "%s: Input queue is full\n", __func__);
324 
325 			NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
326 			NG_FREE_ITEM(item);
327 			error = ENOBUFS;
328 		} else {
329 			NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
330 			error = ng_btsocket_hci_raw_wakeup_input_task();
331 		}
332 		mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
333 	} else {
334 		NG_BTSOCKET_HCI_RAW_ERR(
335 "%s: Failed to allocate address mbuf\n", __func__);
336 
337 		NG_FREE_ITEM(item);
338 		error = ENOBUFS;
339 	}
340 
341 	return (error);
342 } /* ng_btsocket_hci_raw_node_rcvdata */
343 
344 /****************************************************************************
345  ****************************************************************************
346  **                              Sockets specific
347  ****************************************************************************
348  ****************************************************************************/
349 
350 /*
351  * Get next token. We need token to avoid theoretical race where process
352  * submits ioctl() message then interrupts ioctl() and re-submits another
353  * ioctl() on the same socket *before* first ioctl() complete.
354  */
355 
356 static void
357 ng_btsocket_hci_raw_get_token(u_int32_t *token)
358 {
359 	mtx_lock(&ng_btsocket_hci_raw_token_mtx);
360 
361 	if (++ ng_btsocket_hci_raw_token == 0)
362 		ng_btsocket_hci_raw_token = 1;
363 
364 	*token = ng_btsocket_hci_raw_token;
365 
366 	mtx_unlock(&ng_btsocket_hci_raw_token_mtx);
367 } /* ng_btsocket_hci_raw_get_token */
368 
369 /*
370  * Send Netgraph message to the node - do not expect reply
371  */
372 
373 static int
374 ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen)
375 {
376 	struct ng_mesg	*msg = NULL;
377 	int		 error = 0;
378 
379 	NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, arglen, M_NOWAIT);
380 	if (msg == NULL)
381 		return (ENOMEM);
382 
383 	if (arg != NULL && arglen > 0)
384 		bcopy(arg, msg->data, arglen);
385 
386 	NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
387 
388 	return (error);
389 } /* ng_btsocket_hci_raw_send_ngmsg */
390 
391 /*
392  * Send Netgraph message to the node (no data) and wait for reply
393  */
394 
395 static int
396 ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path,
397 		int cmd, void *rsp, int rsplen)
398 {
399 	struct ng_mesg	*msg = NULL;
400 	int		 error = 0;
401 
402 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
403 
404 	NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_NOWAIT);
405 	if (msg == NULL)
406 		return (ENOMEM);
407 
408 	ng_btsocket_hci_raw_get_token(&msg->header.token);
409 	pcb->token = msg->header.token;
410 	pcb->msg = NULL;
411 
412 	NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
413 	if (error != 0) {
414 		pcb->token = 0;
415 		return (error);
416 	}
417 
418 	error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "hcictl",
419 			ng_btsocket_hci_raw_ioctl_timeout * hz);
420 	pcb->token = 0;
421 
422 	if (error != 0)
423 		return (error);
424 
425 	if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
426 		bcopy(pcb->msg->data, rsp, rsplen);
427 	else
428 		error = EINVAL;
429 
430 	NG_FREE_MSG(pcb->msg); /* checks for != NULL */
431 
432 	return (0);
433 } /* ng_btsocket_hci_raw_send_sync_ngmsg */
434 
435 /*
436  * Create control information for the packet
437  */
438 
439 static void
440 ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf **ctl,
441 		struct mbuf *m)
442 {
443 	int		dir;
444 	struct timeval	tv;
445 
446 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
447 
448 	if (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION) {
449 		dir = (m->m_flags & M_PROTO1)? 1 : 0;
450 		*ctl = sbcreatecontrol((caddr_t) &dir, sizeof(dir),
451 					SCM_HCI_RAW_DIRECTION, SOL_HCI_RAW);
452 		if (*ctl != NULL)
453 			ctl = &((*ctl)->m_next);
454 	}
455 
456 	if (pcb->so->so_options & SO_TIMESTAMP) {
457 		microtime(&tv);
458 		*ctl = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
459 					SCM_TIMESTAMP, SOL_SOCKET);
460 		if (*ctl != NULL)
461 			ctl = &((*ctl)->m_next);
462 	}
463 } /* ng_btsocket_hci_raw_savctl */
464 
465 /*
466  * Raw HCI sockets data input routine
467  */
468 
469 static void
470 ng_btsocket_hci_raw_data_input(struct mbuf *nam)
471 {
472 	ng_btsocket_hci_raw_pcb_p	 pcb = NULL;
473 	struct mbuf			*m0 = NULL, *m = NULL;
474 	struct sockaddr_hci		*sa = NULL;
475 
476 	m0 = nam->m_next;
477 	nam->m_next = NULL;
478 
479 	KASSERT((nam->m_type == MT_SONAME),
480 		("%s: m_type=%d\n", __func__, nam->m_type));
481 	KASSERT((m0->m_flags & M_PKTHDR),
482 		("%s: m_flags=%#x\n", __func__, m0->m_flags));
483 
484 	sa = mtod(nam, struct sockaddr_hci *);
485 
486 	mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
487 
488 	LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
489 
490 		mtx_lock(&pcb->pcb_mtx);
491 
492 		/*
493 		 * If socket was bound then check address and
494 		 *  make sure it matches.
495 		 */
496 
497 		if (pcb->addr.hci_node[0] != 0 &&
498 		    strcmp(sa->hci_node, pcb->addr.hci_node) != 0)
499 			goto next;
500 
501 		/*
502 		 * Check packet against filters
503 		 * XXX do we have to call m_pullup() here?
504 		 */
505 
506 		if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0)
507 			goto next;
508 
509 		/*
510 		 * Make a copy of the packet, append to the socket's
511 		 * receive queue and wakeup socket. sbappendaddr()
512 		 * will check if socket has enough buffer space.
513 		 */
514 
515 		m = m_dup(m0, M_DONTWAIT);
516 		if (m != NULL) {
517 			struct mbuf	*ctl = NULL;
518 
519 			ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
520 
521 			if (sbappendaddr(&pcb->so->so_rcv,
522 					(struct sockaddr *) sa, m, ctl))
523 				sorwakeup(pcb->so);
524 			else {
525 				NG_BTSOCKET_HCI_RAW_INFO(
526 "%s: sbappendaddr() failed\n", __func__);
527 
528 				NG_FREE_M(m);
529 				NG_FREE_M(ctl);
530 			}
531 		}
532 next:
533 		mtx_unlock(&pcb->pcb_mtx);
534 	}
535 
536 	mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
537 
538 	NG_FREE_M(nam);
539 	NG_FREE_M(m0);
540 } /* ng_btsocket_hci_raw_data_input */
541 
542 /*
543  * Raw HCI sockets message input routine
544  */
545 
546 static void
547 ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg)
548 {
549 	ng_btsocket_hci_raw_pcb_p	pcb = NULL;
550 
551 	mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
552 
553 	LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
554 		mtx_lock(&pcb->pcb_mtx);
555 
556 		if (msg->header.token == pcb->token) {
557 			pcb->msg = msg;
558 			wakeup(&pcb->msg);
559 
560 			mtx_unlock(&pcb->pcb_mtx);
561 			mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
562 
563 			return;
564 		}
565 
566 		mtx_unlock(&pcb->pcb_mtx);
567 	}
568 
569 	mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
570 
571 	NG_FREE_MSG(msg); /* checks for != NULL */
572 } /* ng_btsocket_hci_raw_msg_input */
573 
574 /*
575  * Raw HCI sockets input routines
576  */
577 
578 static void
579 ng_btsocket_hci_raw_input(void *context, int pending)
580 {
581 	item_p	item = NULL;
582 
583 	for (;;) {
584 		mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
585 		NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_hci_raw_queue, item);
586 		mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
587 
588 		if (item == NULL)
589 			break;
590 
591 		switch(item->el_flags & NGQF_TYPE) {
592 		case NGQF_DATA: {
593 			struct mbuf	*m = NULL;
594 
595 			NGI_GET_M(item, m);
596 			ng_btsocket_hci_raw_data_input(m);
597 			} break;
598 
599 		case NGQF_MESG: {
600 			struct ng_mesg	*msg = NULL;
601 
602 			NGI_GET_MSG(item, msg);
603 			ng_btsocket_hci_raw_msg_input(msg);
604 			} break;
605 
606 		default:
607 			KASSERT(0,
608 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
609 			break;
610 		}
611 
612 		NG_FREE_ITEM(item);
613 	}
614 } /* ng_btsocket_hci_raw_input */
615 
616 /*
617  * Raw HCI sockets output routine
618  */
619 
620 static void
621 ng_btsocket_hci_raw_output(node_p node, hook_p hook, void *arg1, int arg2)
622 {
623 	struct mbuf		*nam = (struct mbuf *) arg1, *m = NULL;
624 	struct sockaddr_hci	*sa = NULL;
625 	int			 error;
626 
627 	m = nam->m_next;
628 	nam->m_next = NULL;
629 
630 	KASSERT((nam->m_type == MT_SONAME),
631 		("%s: m_type=%d\n", __func__, nam->m_type));
632 	KASSERT((m->m_flags & M_PKTHDR),
633 		("%s: m_flags=%#x\n", __func__, m->m_flags));
634 
635 	sa = mtod(nam, struct sockaddr_hci *);
636 
637 	/*
638 	 * Find downstream hook
639 	 * XXX For now access node hook list directly. Should be safe because
640 	 * we used ng_send_fn() and we should have exclusive lock on the node.
641 	 */
642 
643 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
644 		if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
645 		    NG_NODE_NOT_VALID(NG_PEER_NODE(hook)))
646 			continue;
647 
648 		if (strcmp(sa->hci_node, NG_PEER_NODE_NAME(hook)) == 0) {
649 			NG_SEND_DATA_ONLY(error, hook, m); /* sets m to NULL */
650 			break;
651 		}
652 	}
653 
654 	NG_FREE_M(nam); /* check for != NULL */
655 	NG_FREE_M(m);
656 } /* ng_btsocket_hci_raw_output */
657 
658 /*
659  * Check frame against security and socket filters.
660  * d (direction bit) == 1 means incoming frame.
661  */
662 
663 static int
664 ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf *m, int d)
665 {
666 	int	type, event, opcode;
667 
668 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
669 
670 	switch ((type = *mtod(m, u_int8_t *))) {
671 	case NG_HCI_CMD_PKT:
672 		if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)) {
673 			opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
674 
675 			if (!bit_test(
676 ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF(opcode) - 1],
677 NG_HCI_OCF(opcode) - 1))
678 				return (EPERM);
679 		}
680 
681 		if (d && !bit_test(pcb->filter.packet_mask, NG_HCI_CMD_PKT - 1))
682 			return (EPERM);
683 		break;
684 
685 	case NG_HCI_ACL_DATA_PKT:
686 	case NG_HCI_SCO_DATA_PKT:
687 		if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) ||
688 		    !bit_test(pcb->filter.packet_mask, type - 1) ||
689 		    !d)
690 			return (EPERM);
691 		break;
692 
693 	case NG_HCI_EVENT_PKT:
694 		if (!d)
695 			return (EINVAL);
696 
697 		event = mtod(m, ng_hci_event_pkt_t *)->event - 1;
698 
699 		if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED))
700 			if (!bit_test(ng_btsocket_hci_raw_sec_filter->events, event))
701 				return (EPERM);
702 
703 		if (!bit_test(pcb->filter.event_mask, event))
704 			return (EPERM);
705 		break;
706 
707 	default:
708 		return (EINVAL);
709 	}
710 
711 	return (0);
712 } /* ng_btsocket_hci_raw_filter */
713 
714 /*
715  * Initialize everything
716  */
717 
718 void
719 ng_btsocket_hci_raw_init(void)
720 {
721 	bitstr_t	*f = NULL;
722 	int		 error = 0;
723 
724 	ng_btsocket_hci_raw_node = NULL;
725 	ng_btsocket_hci_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
726 	ng_btsocket_hci_raw_ioctl_timeout = 5;
727 
728 	/* Register Netgraph node type */
729 	error = ng_newtype(&typestruct);
730 	if (error != 0) {
731 		NG_BTSOCKET_HCI_RAW_ALERT(
732 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
733 
734 		return;
735 	}
736 
737 	/* Create Netgrapg node */
738 	error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
739 	if (error != 0) {
740 		NG_BTSOCKET_HCI_RAW_ALERT(
741 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
742 
743 		ng_btsocket_hci_raw_node = NULL;
744 
745 		return;
746         }
747 
748 	error = ng_name_node(ng_btsocket_hci_raw_node,
749 				NG_BTSOCKET_HCI_RAW_NODE_TYPE);
750 	if (error != 0) {
751 		NG_BTSOCKET_HCI_RAW_ALERT(
752 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
753 
754 		NG_NODE_UNREF(ng_btsocket_hci_raw_node);
755 		ng_btsocket_hci_raw_node = NULL;
756 
757 		return;
758 	}
759 
760 	/* Create input queue */
761 	NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, ifqmaxlen);
762 	mtx_init(&ng_btsocket_hci_raw_queue_mtx,
763 		"btsocks_hci_raw_queue_mtx", NULL, MTX_DEF);
764 	TASK_INIT(&ng_btsocket_hci_raw_task, 0,
765 		ng_btsocket_hci_raw_input, NULL);
766 
767 	/* Create list of sockets */
768 	LIST_INIT(&ng_btsocket_hci_raw_sockets);
769 	mtx_init(&ng_btsocket_hci_raw_sockets_mtx,
770 		"btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF);
771 
772 	/* Tokens */
773 	ng_btsocket_hci_raw_token = 0;
774 	mtx_init(&ng_btsocket_hci_raw_token_mtx,
775 		"btsocks_hci_raw_token_mtx", NULL, MTX_DEF);
776 
777 	/*
778 	 * Security filter
779 	 * XXX never FREE()ed
780 	 */
781 
782 	ng_btsocket_hci_raw_sec_filter = NULL;
783 
784 	MALLOC(ng_btsocket_hci_raw_sec_filter,
785 		struct ng_btsocket_hci_raw_sec_filter *,
786 		sizeof(struct ng_btsocket_hci_raw_sec_filter),
787 		M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
788 	if (ng_btsocket_hci_raw_sec_filter == NULL) {
789 		printf("%s: Could not allocate security filter!\n", __func__);
790 		return;
791 	}
792 
793 	/*
794 	 * XXX How paranoid can we get?
795 	 *
796 	 * Initialize security filter. If bit is set in the mask then
797 	 * unprivileged socket is allowed to send (receive) this command
798 	 * (event).
799 	 */
800 
801 	/* Enable all events */
802 	memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
803 		sizeof(ng_btsocket_hci_raw_sec_filter->events)/
804 			sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
805 
806 	/* Disable some critical events */
807 	f = ng_btsocket_hci_raw_sec_filter->events;
808 	bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
809 	bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
810 	bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
811 
812 	/* Commands - Link control */
813 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
814 	bit_set(f, NG_HCI_OCF_INQUIRY - 1);
815 	bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
816 	bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
817 	bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
818 	bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
819 	bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
820 	bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
821 	bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
822 
823 	/* Commands - Link policy */
824 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
825 	bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
826 	bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
827 
828 	/* Commands - Host controller and baseband */
829 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
830 	bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
831 	bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
832 	bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
833 	bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
834 	bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
835 	bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
836 	bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
837 	bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
838 	bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
839 	bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
840 	bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
841 	bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
842 	bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
843 	bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
844 	bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
845 	bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
846 	bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
847 	bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
848 	bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
849 	bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
850 	bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
851 
852 	/* Commands - Informational */
853 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
854 	bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
855 	bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
856 	bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
857 	bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
858 	bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
859 
860 	/* Commands - Status */
861 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
862 	bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
863 	bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
864 	bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
865 
866 	/* Commands - Testing */
867 	f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
868 	bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
869 } /* ng_btsocket_hci_raw_init */
870 
871 /*
872  * Abort connection on socket
873  */
874 
875 int
876 ng_btsocket_hci_raw_abort(struct socket *so)
877 {
878 	return (ng_btsocket_hci_raw_detach(so));
879 } /* ng_btsocket_hci_raw_abort */
880 
881 /*
882  * Create new raw HCI socket
883  */
884 
885 int
886 ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
887 {
888 	ng_btsocket_hci_raw_pcb_p	pcb = so2hci_raw_pcb(so);
889 	int				error = 0;
890 
891 	if (pcb != NULL)
892 		return (EISCONN);
893 
894 	if (ng_btsocket_hci_raw_node == NULL)
895 		return (EPROTONOSUPPORT);
896 	if (proto != BLUETOOTH_PROTO_HCI)
897 		return (EPROTONOSUPPORT);
898 	if (so->so_type != SOCK_RAW)
899 		return (ESOCKTNOSUPPORT);
900 
901 	error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
902 				NG_BTSOCKET_HCI_RAW_RECVSPACE);
903 	if (error != 0)
904 		return (error);
905 
906 	MALLOC(pcb, ng_btsocket_hci_raw_pcb_p, sizeof(*pcb),
907 		M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
908 	if (pcb == NULL)
909 		return (ENOMEM);
910 
911 	so->so_pcb = (caddr_t) pcb;
912 	pcb->so = so;
913 
914 	if (suser(td) == 0)
915 		pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
916 
917 	/*
918 	 * Set default socket filter. By default socket only accepts HCI
919 	 * Command_Complete and Command_Status event packets.
920 	 */
921 
922 	bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
923 	bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
924 
925 	mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
926 
927 	mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
928 	LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
929 	mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
930 
931 	return (0);
932 } /* ng_btsocket_hci_raw_attach */
933 
934 /*
935  * Bind raw HCI socket
936  */
937 
938 int
939 ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
940 		struct thread *td)
941 {
942 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
943 	struct sockaddr_hci		*sa = (struct sockaddr_hci *) nam;
944 
945 	if (pcb == NULL)
946 		return (EINVAL);
947 	if (ng_btsocket_hci_raw_node == NULL)
948 		return (EINVAL);
949 
950 	if (sa == NULL)
951 		return (EINVAL);
952 	if (sa->hci_family != AF_BLUETOOTH)
953 		return (EAFNOSUPPORT);
954 	if (sa->hci_len != sizeof(*sa))
955 		return (EINVAL);
956 	if (sa->hci_node[0] == 0)
957 		return (EINVAL);
958 
959 	mtx_lock(&pcb->pcb_mtx);
960 	bcopy(sa, &pcb->addr, sizeof(pcb->addr));
961 	mtx_unlock(&pcb->pcb_mtx);
962 
963 	return (0);
964 } /* ng_btsocket_hci_raw_bind */
965 
966 /*
967  * Connect raw HCI socket
968  */
969 
970 int
971 ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
972 		struct thread *td)
973 {
974 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
975 	struct sockaddr_hci		*sa = (struct sockaddr_hci *) nam;
976 
977 	if (pcb == NULL)
978 		return (EINVAL);
979 	if (ng_btsocket_hci_raw_node == NULL)
980 		return (EINVAL);
981 
982 	if (sa == NULL)
983 		return (EINVAL);
984 	if (sa->hci_family != AF_BLUETOOTH)
985 		return (EAFNOSUPPORT);
986 	if (sa->hci_len != sizeof(*sa))
987 		return (EINVAL);
988 	if (sa->hci_node[0] == 0)
989 		return (EDESTADDRREQ);
990 
991 	mtx_lock(&pcb->pcb_mtx);
992 
993 	if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
994 		mtx_unlock(&pcb->pcb_mtx);
995 		return (EADDRNOTAVAIL);
996 	}
997 
998 	soisconnected(so);
999 
1000 	mtx_unlock(&pcb->pcb_mtx);
1001 
1002 	return (0);
1003 } /* ng_btsocket_hci_raw_connect */
1004 
1005 /*
1006  * Process ioctl on socket
1007  */
1008 
1009 int
1010 ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
1011 		struct ifnet *ifp, struct thread *td)
1012 {
1013 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
1014 	char				 path[NG_NODESIZ + 1];
1015 	struct ng_mesg			*msg = NULL;
1016 	int				 error = 0;
1017 
1018 	if (pcb == NULL)
1019 		return (EINVAL);
1020 	if (ng_btsocket_hci_raw_node == NULL)
1021 		return (EINVAL);
1022 
1023 	mtx_lock(&pcb->pcb_mtx);
1024 
1025 	/* Check if we have device name */
1026 	if (pcb->addr.hci_node[0] == 0) {
1027 		mtx_unlock(&pcb->pcb_mtx);
1028 		return (EHOSTUNREACH);
1029 	}
1030 
1031 	/* Check if we have pending ioctl() */
1032 	if (pcb->token != 0) {
1033 		mtx_unlock(&pcb->pcb_mtx);
1034 		return (EBUSY);
1035 	}
1036 
1037 	snprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1038 
1039 	switch (cmd) {
1040 	case SIOC_HCI_RAW_NODE_GET_STATE: {
1041 		struct ng_btsocket_hci_raw_node_state	*p =
1042 			(struct ng_btsocket_hci_raw_node_state *) data;
1043 
1044 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1045 				NGM_HCI_NODE_GET_STATE,
1046 				&p->state, sizeof(p->state));
1047 		} break;
1048 
1049 	case SIOC_HCI_RAW_NODE_INIT:
1050 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1051 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1052 					NGM_HCI_NODE_INIT, NULL, 0);
1053 		else
1054 			error = EPERM;
1055 		break;
1056 
1057 	case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1058 		struct ng_btsocket_hci_raw_node_debug	*p =
1059 			(struct ng_btsocket_hci_raw_node_debug *) data;
1060 
1061 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1062 				NGM_HCI_NODE_GET_DEBUG,
1063 				&p->debug, sizeof(p->debug));
1064 		} break;
1065 
1066 	case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1067 		struct ng_btsocket_hci_raw_node_debug	*p =
1068 			(struct ng_btsocket_hci_raw_node_debug *) data;
1069 
1070 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1071 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1072 					NGM_HCI_NODE_SET_DEBUG, &p->debug,
1073 					sizeof(p->debug));
1074 		else
1075 			error = EPERM;
1076 		} break;
1077 
1078 	case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1079 		struct ng_btsocket_hci_raw_node_buffer	*p =
1080 			(struct ng_btsocket_hci_raw_node_buffer *) data;
1081 
1082 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1083 				NGM_HCI_NODE_GET_BUFFER,
1084 				&p->buffer, sizeof(p->buffer));
1085 		} break;
1086 
1087 	case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1088 		struct ng_btsocket_hci_raw_node_bdaddr	*p =
1089 			(struct ng_btsocket_hci_raw_node_bdaddr *) data;
1090 
1091 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1092 				NGM_HCI_NODE_GET_BDADDR,
1093 				&p->bdaddr, sizeof(p->bdaddr));
1094 		} break;
1095 
1096 	case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1097 		struct ng_btsocket_hci_raw_node_features	*p =
1098 			(struct ng_btsocket_hci_raw_node_features *) data;
1099 
1100 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1101 				NGM_HCI_NODE_GET_FEATURES,
1102 				&p->features, sizeof(p->features));
1103 		} break;
1104 
1105 	case SIOC_HCI_RAW_NODE_GET_STAT: {
1106 		struct ng_btsocket_hci_raw_node_stat	*p =
1107 			(struct ng_btsocket_hci_raw_node_stat *) data;
1108 
1109 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1110 				NGM_HCI_NODE_GET_STAT,
1111 				&p->stat, sizeof(p->stat));
1112 		} break;
1113 
1114 	case SIOC_HCI_RAW_NODE_RESET_STAT:
1115 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1116 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1117 					NGM_HCI_NODE_RESET_STAT, NULL, 0);
1118 		else
1119 			error = EPERM;
1120 		break;
1121 
1122 	case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1123 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1124 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1125 					NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1126 					NULL, 0);
1127 		else
1128 			error = EPERM;
1129 		break;
1130 
1131 	case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE:  {
1132 		struct ng_btsocket_hci_raw_node_neighbor_cache	*p =
1133 			(struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1134 		ng_hci_node_get_neighbor_cache_ep		*p1 = NULL;
1135 		ng_hci_node_neighbor_cache_entry_ep		*p2 = NULL;
1136 
1137 		if (p->num_entries <= 0 ||
1138 		    p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1139 		    p->entries == NULL) {
1140 			error = EINVAL;
1141 			break;
1142 		}
1143 
1144 		NG_MKMESSAGE(msg, NGM_HCI_COOKIE,
1145 			NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_NOWAIT);
1146 		if (msg == NULL) {
1147 			error = ENOMEM;
1148 			break;
1149 		}
1150 		ng_btsocket_hci_raw_get_token(&msg->header.token);
1151 		pcb->token = msg->header.token;
1152 		pcb->msg = NULL;
1153 
1154 		NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1155 		if (error != 0) {
1156 			pcb->token = 0;
1157 			break;
1158 		}
1159 
1160 		error = msleep(&pcb->msg, &pcb->pcb_mtx,
1161 				PZERO|PCATCH, "hcictl",
1162 				ng_btsocket_hci_raw_ioctl_timeout * hz);
1163 		pcb->token = 0;
1164 
1165 		if (error != 0)
1166 			break;
1167 
1168 		if (pcb->msg != NULL &&
1169 		    pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1170 			/* Return data back to user space */
1171 			p1 = (ng_hci_node_get_neighbor_cache_ep *)
1172 				(pcb->msg->data);
1173 			p2 = (ng_hci_node_neighbor_cache_entry_ep *)
1174 				(p1 + 1);
1175 
1176 			p->num_entries = min(p->num_entries, p1->num_entries);
1177 			if (p->num_entries > 0)
1178 				error = copyout((caddr_t) p2,
1179 						(caddr_t) p->entries,
1180 						p->num_entries * sizeof(*p2));
1181 		} else
1182 			error = EINVAL;
1183 
1184 		NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1185 		}break;
1186 
1187 	case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1188 		struct ng_btsocket_hci_raw_con_list	*p =
1189 			(struct ng_btsocket_hci_raw_con_list *) data;
1190 		ng_hci_node_con_list_ep			*p1 = NULL;
1191 		ng_hci_node_con_ep			*p2 = NULL;
1192 
1193 		if (p->num_connections == 0 ||
1194 		    p->num_connections > NG_HCI_MAX_CON_NUM ||
1195 		    p->connections == NULL) {
1196 			error = EINVAL;
1197 			break;
1198 		}
1199 
1200 		NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1201 			0, M_NOWAIT);
1202 		if (msg == NULL) {
1203 			error = ENOMEM;
1204 			break;
1205 		}
1206 		ng_btsocket_hci_raw_get_token(&msg->header.token);
1207 		pcb->token = msg->header.token;
1208 		pcb->msg = NULL;
1209 
1210 		NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1211 		if (error != 0) {
1212 			pcb->token = 0;
1213 			break;
1214 		}
1215 
1216 		error = msleep(&pcb->msg, &pcb->pcb_mtx,
1217 				PZERO|PCATCH, "hcictl",
1218 				ng_btsocket_hci_raw_ioctl_timeout * hz);
1219 		pcb->token = 0;
1220 
1221 		if (error != 0)
1222 			break;
1223 
1224 		if (pcb->msg != NULL &&
1225 		    pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1226 			/* Return data back to user space */
1227 			p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data);
1228 			p2 = (ng_hci_node_con_ep *)(p1 + 1);
1229 
1230 			p->num_connections = min(p->num_connections,
1231 						p1->num_connections);
1232 			if (p->num_connections > 0)
1233 				error = copyout((caddr_t) p2,
1234 					(caddr_t) p->connections,
1235 					p->num_connections * sizeof(*p2));
1236 		} else
1237 			error = EINVAL;
1238 
1239 		NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1240 		} break;
1241 
1242 	case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: {
1243 		struct ng_btsocket_hci_raw_node_link_policy_mask	*p =
1244 			(struct ng_btsocket_hci_raw_node_link_policy_mask *)
1245 				data;
1246 
1247 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1248 				NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1249 				&p->policy_mask, sizeof(p->policy_mask));
1250 		} break;
1251 
1252 	case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1253 		struct ng_btsocket_hci_raw_node_link_policy_mask	*p =
1254 			(struct ng_btsocket_hci_raw_node_link_policy_mask *)
1255 				data;
1256 
1257 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1258 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1259 					NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1260 					&p->policy_mask,
1261 					sizeof(p->policy_mask));
1262 		else
1263 			error = EPERM;
1264 		} break;
1265 
1266 	case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1267 		struct ng_btsocket_hci_raw_node_packet_mask	*p =
1268 			(struct ng_btsocket_hci_raw_node_packet_mask *) data;
1269 
1270 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1271 				NGM_HCI_NODE_GET_PACKET_MASK,
1272 				&p->packet_mask, sizeof(p->packet_mask));
1273 		} break;
1274 
1275 	case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1276 		struct ng_btsocket_hci_raw_node_packet_mask	*p =
1277 			(struct ng_btsocket_hci_raw_node_packet_mask *) data;
1278 
1279 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1280 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1281 					NGM_HCI_NODE_SET_PACKET_MASK,
1282 					&p->packet_mask,
1283 					sizeof(p->packet_mask));
1284 		else
1285 			error = EPERM;
1286 		} break;
1287 
1288 	case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1289 		struct ng_btsocket_hci_raw_node_role_switch	*p =
1290 			(struct ng_btsocket_hci_raw_node_role_switch *) data;
1291 
1292 		error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1293 				NGM_HCI_NODE_GET_ROLE_SWITCH,
1294 				&p->role_switch, sizeof(p->role_switch));
1295 		} break;
1296 
1297 	case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1298 		struct ng_btsocket_hci_raw_node_role_switch	*p =
1299 			(struct ng_btsocket_hci_raw_node_role_switch *) data;
1300 
1301 		if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1302 			error = ng_btsocket_hci_raw_send_ngmsg(path,
1303 					NGM_HCI_NODE_SET_ROLE_SWITCH,
1304 					&p->role_switch,
1305 					sizeof(p->role_switch));
1306 		else
1307 			error = EPERM;
1308 		} break;
1309 
1310 	default:
1311 		error = EINVAL;
1312 		break;
1313 	}
1314 
1315 	mtx_unlock(&pcb->pcb_mtx);
1316 
1317 	return (error);
1318 } /* ng_btsocket_hci_raw_control */
1319 
1320 /*
1321  * Process getsockopt/setsockopt system calls
1322  */
1323 
1324 int
1325 ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1326 {
1327 	ng_btsocket_hci_raw_pcb_p		pcb = so2hci_raw_pcb(so);
1328 	struct ng_btsocket_hci_raw_filter	filter;
1329 	int					error = 0, dir;
1330 
1331 	if (pcb == NULL)
1332 		return (EINVAL);
1333 	if (ng_btsocket_hci_raw_node == NULL)
1334 		return (EINVAL);
1335 
1336 	if (sopt->sopt_level != SOL_HCI_RAW)
1337 		return (0);
1338 
1339 	mtx_lock(&pcb->pcb_mtx);
1340 
1341 	switch (sopt->sopt_dir) {
1342 	case SOPT_GET:
1343 		switch (sopt->sopt_name) {
1344 		case SO_HCI_RAW_FILTER:
1345 			error = sooptcopyout(sopt, &pcb->filter,
1346 						sizeof(pcb->filter));
1347 			break;
1348 
1349 		case SO_HCI_RAW_DIRECTION:
1350 			dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1351 			error = sooptcopyout(sopt, &dir, sizeof(dir));
1352 			break;
1353 
1354 		default:
1355 			error = EINVAL;
1356 			break;
1357 		}
1358 		break;
1359 
1360 	case SOPT_SET:
1361 		switch (sopt->sopt_name) {
1362 		case SO_HCI_RAW_FILTER:
1363 			error = sooptcopyin(sopt, &filter, sizeof(filter),
1364 						sizeof(filter));
1365 			if (error == 0)
1366 				bcopy(&filter, &pcb->filter,
1367 						sizeof(pcb->filter));
1368 			break;
1369 
1370 		case SO_HCI_RAW_DIRECTION:
1371 			error = sooptcopyin(sopt, &dir, sizeof(dir),
1372 						sizeof(dir));
1373 			if (error != 0)
1374 				break;
1375 
1376 			if (dir)
1377 				pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1378 			else
1379 				pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1380 			break;
1381 
1382 		default:
1383 			error = EINVAL;
1384 			break;
1385 		}
1386 		break;
1387 
1388 	default:
1389 		error = EINVAL;
1390 		break;
1391 	}
1392 
1393 	mtx_unlock(&pcb->pcb_mtx);
1394 
1395 	return (error);
1396 } /* ng_btsocket_hci_raw_ctloutput */
1397 
1398 /*
1399  * Detach raw HCI socket
1400  */
1401 
1402 int
1403 ng_btsocket_hci_raw_detach(struct socket *so)
1404 {
1405 	ng_btsocket_hci_raw_pcb_p	pcb = so2hci_raw_pcb(so);
1406 
1407 	if (pcb == NULL)
1408 		return (EINVAL);
1409 	if (ng_btsocket_hci_raw_node == NULL)
1410 		return (EINVAL);
1411 
1412 	mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
1413 	mtx_lock(&pcb->pcb_mtx);
1414 
1415 	LIST_REMOVE(pcb, next);
1416 
1417 	mtx_unlock(&pcb->pcb_mtx);
1418 	mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
1419 
1420 	mtx_destroy(&pcb->pcb_mtx);
1421 
1422 	bzero(pcb, sizeof(*pcb));
1423 	FREE(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1424 
1425 	ACCEPT_LOCK();
1426 	SOCK_LOCK(so);
1427 	so->so_pcb = NULL;
1428 	sotryfree(so);
1429 
1430 	return (0);
1431 } /* ng_btsocket_hci_raw_detach */
1432 
1433 /*
1434  * Disconnect raw HCI socket
1435  */
1436 
1437 int
1438 ng_btsocket_hci_raw_disconnect(struct socket *so)
1439 {
1440 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
1441 
1442 	if (pcb == NULL)
1443 		return (EINVAL);
1444 	if (ng_btsocket_hci_raw_node == NULL)
1445 		return (EINVAL);
1446 
1447 	mtx_lock(&pcb->pcb_mtx);
1448 	soisdisconnected(so);
1449 	mtx_unlock(&pcb->pcb_mtx);
1450 
1451 	return (0);
1452 } /* ng_btsocket_hci_raw_disconnect */
1453 
1454 /*
1455  * Get socket peer's address
1456  */
1457 
1458 int
1459 ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1460 {
1461 	return (ng_btsocket_hci_raw_sockaddr(so, nam));
1462 } /* ng_btsocket_hci_raw_peeraddr */
1463 
1464 /*
1465  * Send data
1466  */
1467 
1468 int
1469 ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
1470 		struct sockaddr *sa, struct mbuf *control, struct thread *td)
1471 {
1472 	ng_btsocket_hci_raw_pcb_p	 pcb = so2hci_raw_pcb(so);
1473 	struct mbuf			*nam = NULL;
1474 	int				 error = 0;
1475 
1476 	if (ng_btsocket_hci_raw_node == NULL) {
1477 		error = ENETDOWN;
1478 		goto drop;
1479 	}
1480 	if (pcb == NULL) {
1481 		error = EINVAL;
1482 		goto drop;
1483 	}
1484 	if (control != NULL) {
1485 		error = EINVAL;
1486 		goto drop;
1487 	}
1488 
1489 	if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1490 	    m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1491 		error = EMSGSIZE;
1492 		goto drop;
1493 	}
1494 
1495 	if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1496 		if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1497 			error = ENOBUFS;
1498 			goto drop;
1499 		}
1500 	}
1501 	if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1502 		error = ENOTSUP;
1503 		goto drop;
1504 	}
1505 
1506 	mtx_lock(&pcb->pcb_mtx);
1507 
1508 	error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1509 	if (error != 0) {
1510 		mtx_unlock(&pcb->pcb_mtx);
1511 		goto drop;
1512 	}
1513 
1514 	if (sa == NULL) {
1515 		if (pcb->addr.hci_node[0] == 0) {
1516 			mtx_unlock(&pcb->pcb_mtx);
1517 			error = EDESTADDRREQ;
1518 			goto drop;
1519 		}
1520 
1521 		sa = (struct sockaddr *) &pcb->addr;
1522 	}
1523 
1524 	MGET(nam, M_DONTWAIT, MT_SONAME);
1525 	if (nam == NULL) {
1526 		mtx_unlock(&pcb->pcb_mtx);
1527 		error = ENOBUFS;
1528 		goto drop;
1529 	}
1530 
1531 	nam->m_len = sizeof(struct sockaddr_hci);
1532 	bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1533 
1534 	nam->m_next = m;
1535 	m = NULL;
1536 
1537 	mtx_unlock(&pcb->pcb_mtx);
1538 
1539 	return (ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1540 				ng_btsocket_hci_raw_output, nam, 0));
1541 drop:
1542 	NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1543 	NG_FREE_M(nam);
1544 	NG_FREE_M(m);
1545 
1546 	return (error);
1547 } /* ng_btsocket_hci_raw_send */
1548 
1549 /*
1550  * Get socket address
1551  */
1552 
1553 int
1554 ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1555 {
1556 	ng_btsocket_hci_raw_pcb_p	pcb = so2hci_raw_pcb(so);
1557 	struct sockaddr_hci		sa;
1558 
1559 	if (pcb == NULL)
1560 		return (EINVAL);
1561 	if (ng_btsocket_hci_raw_node == NULL)
1562 		return (EINVAL);
1563 
1564 	bzero(&sa, sizeof(sa));
1565 	sa.hci_len = sizeof(sa);
1566 	sa.hci_family = AF_BLUETOOTH;
1567 
1568 	mtx_lock(&pcb->pcb_mtx);
1569 	strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1570 	mtx_unlock(&pcb->pcb_mtx);
1571 
1572 	*nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1573 
1574 	return ((*nam == NULL)? ENOMEM : 0);
1575 } /* ng_btsocket_hci_raw_sockaddr */
1576 
1577