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