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