xref: /freebsd/sys/netgraph/bluetooth/socket/ng_btsocket_l2cap_raw.c (revision 2616144e4345e4cdab56fd7c673fbb69074a9ce2)
1 /*
2  * ng_btsocket_l2cap_raw.c
3  */
4 
5 /*-
6  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: ng_btsocket_l2cap_raw.c,v 1.12 2003/09/14 23:29:06 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bitstring.h>
37 #include <sys/domain.h>
38 #include <sys/errno.h>
39 #include <sys/filedesc.h>
40 #include <sys/ioccom.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/mutex.h>
46 #include <sys/priv.h>
47 #include <sys/protosw.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/taskqueue.h>
53 #include <netgraph/ng_message.h>
54 #include <netgraph/netgraph.h>
55 #include <netgraph/bluetooth/include/ng_bluetooth.h>
56 #include <netgraph/bluetooth/include/ng_hci.h>
57 #include <netgraph/bluetooth/include/ng_l2cap.h>
58 #include <netgraph/bluetooth/include/ng_btsocket.h>
59 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
60 
61 /* MALLOC define */
62 #ifdef NG_SEPARATE_MALLOC
63 MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP_RAW, "netgraph_btsocks_l2cap_raw",
64 		"Netgraph Bluetooth raw L2CAP sockets");
65 #else
66 #define M_NETGRAPH_BTSOCKET_L2CAP_RAW M_NETGRAPH
67 #endif /* NG_SEPARATE_MALLOC */
68 
69 /* Netgraph node methods */
70 static ng_constructor_t	ng_btsocket_l2cap_raw_node_constructor;
71 static ng_rcvmsg_t	ng_btsocket_l2cap_raw_node_rcvmsg;
72 static ng_shutdown_t	ng_btsocket_l2cap_raw_node_shutdown;
73 static ng_newhook_t	ng_btsocket_l2cap_raw_node_newhook;
74 static ng_connect_t	ng_btsocket_l2cap_raw_node_connect;
75 static ng_rcvdata_t	ng_btsocket_l2cap_raw_node_rcvdata;
76 static ng_disconnect_t	ng_btsocket_l2cap_raw_node_disconnect;
77 
78 static void		ng_btsocket_l2cap_raw_input     (void *, int);
79 static void		ng_btsocket_l2cap_raw_rtclean   (void *, int);
80 static void		ng_btsocket_l2cap_raw_get_token (u_int32_t *);
81 
82 static int		ng_btsocket_l2cap_raw_send_ngmsg
83 				(hook_p, int, void *, int);
84 static int		ng_btsocket_l2cap_raw_send_sync_ngmsg
85 				(ng_btsocket_l2cap_raw_pcb_p, int, void *, int);
86 
87 #define ng_btsocket_l2cap_raw_wakeup_input_task() \
88 	taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_queue_task)
89 
90 #define ng_btsocket_l2cap_raw_wakeup_route_task() \
91 	taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_rt_task)
92 
93 /* Netgraph type descriptor */
94 static struct ng_type	typestruct = {
95 	.version =	NG_ABI_VERSION,
96 	.name =		NG_BTSOCKET_L2CAP_RAW_NODE_TYPE,
97 	.constructor =	ng_btsocket_l2cap_raw_node_constructor,
98 	.rcvmsg =	ng_btsocket_l2cap_raw_node_rcvmsg,
99 	.shutdown =	ng_btsocket_l2cap_raw_node_shutdown,
100 	.newhook =	ng_btsocket_l2cap_raw_node_newhook,
101 	.connect =	ng_btsocket_l2cap_raw_node_connect,
102 	.rcvdata =	ng_btsocket_l2cap_raw_node_rcvdata,
103 	.disconnect =	ng_btsocket_l2cap_raw_node_disconnect,
104 };
105 
106 /* Globals */
107 extern int					ifqmaxlen;
108 static u_int32_t				ng_btsocket_l2cap_raw_debug_level;
109 static u_int32_t				ng_btsocket_l2cap_raw_ioctl_timeout;
110 static node_p					ng_btsocket_l2cap_raw_node;
111 static struct ng_bt_itemq			ng_btsocket_l2cap_raw_queue;
112 static struct mtx				ng_btsocket_l2cap_raw_queue_mtx;
113 static struct task				ng_btsocket_l2cap_raw_queue_task;
114 static LIST_HEAD(, ng_btsocket_l2cap_raw_pcb)	ng_btsocket_l2cap_raw_sockets;
115 static struct mtx				ng_btsocket_l2cap_raw_sockets_mtx;
116 static u_int32_t				ng_btsocket_l2cap_raw_token;
117 static struct mtx				ng_btsocket_l2cap_raw_token_mtx;
118 static LIST_HEAD(, ng_btsocket_l2cap_rtentry)	ng_btsocket_l2cap_raw_rt;
119 static struct mtx				ng_btsocket_l2cap_raw_rt_mtx;
120 static struct task				ng_btsocket_l2cap_raw_rt_task;
121 static struct timeval				ng_btsocket_l2cap_raw_lasttime;
122 static int					ng_btsocket_l2cap_raw_curpps;
123 
124 /* Sysctl tree */
125 SYSCTL_DECL(_net_bluetooth_l2cap_sockets);
126 SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, raw, CTLFLAG_RW,
127 	0, "Bluetooth raw L2CAP sockets family");
128 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, debug_level,
129 	CTLFLAG_RW,
130 	&ng_btsocket_l2cap_raw_debug_level, NG_BTSOCKET_WARN_LEVEL,
131 	"Bluetooth raw L2CAP sockets debug level");
132 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, ioctl_timeout,
133 	CTLFLAG_RW,
134 	&ng_btsocket_l2cap_raw_ioctl_timeout, 5,
135 	"Bluetooth raw L2CAP sockets ioctl timeout");
136 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_len,
137 	CTLFLAG_RD,
138 	&ng_btsocket_l2cap_raw_queue.len, 0,
139 	"Bluetooth raw L2CAP sockets input queue length");
140 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_maxlen,
141 	CTLFLAG_RD,
142 	&ng_btsocket_l2cap_raw_queue.maxlen, 0,
143 	"Bluetooth raw L2CAP sockets input queue max. length");
144 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_drops,
145 	CTLFLAG_RD,
146 	&ng_btsocket_l2cap_raw_queue.drops, 0,
147 	"Bluetooth raw L2CAP sockets input queue drops");
148 
149 /* Debug */
150 #define NG_BTSOCKET_L2CAP_RAW_INFO \
151 	if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
152 	    ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
153 		printf
154 
155 #define NG_BTSOCKET_L2CAP_RAW_WARN \
156 	if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
157 	    ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
158 		printf
159 
160 #define NG_BTSOCKET_L2CAP_RAW_ERR \
161 	if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
162 	    ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
163 		printf
164 
165 #define NG_BTSOCKET_L2CAP_RAW_ALERT \
166 	if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
167 	    ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
168 		printf
169 
170 /*****************************************************************************
171  *****************************************************************************
172  **                        Netgraph node interface
173  *****************************************************************************
174  *****************************************************************************/
175 
176 /*
177  * Netgraph node constructor. Do not allow to create node of this type.
178  */
179 
180 static int
181 ng_btsocket_l2cap_raw_node_constructor(node_p node)
182 {
183 	return (EINVAL);
184 } /* ng_btsocket_l2cap_raw_node_constructor */
185 
186 /*
187  * Do local shutdown processing. Let old node go and create new fresh one.
188  */
189 
190 static int
191 ng_btsocket_l2cap_raw_node_shutdown(node_p node)
192 {
193 	int	error = 0;
194 
195 	NG_NODE_UNREF(node);
196 
197 	/* Create new node */
198 	error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node);
199 	if (error != 0) {
200 		NG_BTSOCKET_L2CAP_RAW_ALERT(
201 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
202 
203 		ng_btsocket_l2cap_raw_node = NULL;
204 
205 		return (error);
206 	}
207 
208 	error = ng_name_node(ng_btsocket_l2cap_raw_node,
209 				NG_BTSOCKET_L2CAP_RAW_NODE_TYPE);
210 	if (error != 0) {
211 		NG_BTSOCKET_L2CAP_RAW_ALERT(
212 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
213 
214 		NG_NODE_UNREF(ng_btsocket_l2cap_raw_node);
215 		ng_btsocket_l2cap_raw_node = NULL;
216 
217 		return (error);
218 	}
219 
220 	return (0);
221 } /* ng_btsocket_l2cap_raw_node_shutdown */
222 
223 /*
224  * We allow any hook to be connected to the node.
225  */
226 
227 static int
228 ng_btsocket_l2cap_raw_node_newhook(node_p node, hook_p hook, char const *name)
229 {
230 	return (0);
231 } /* ng_btsocket_l2cap_raw_node_newhook */
232 
233 /*
234  * Just say "YEP, that's OK by me!"
235  */
236 
237 static int
238 ng_btsocket_l2cap_raw_node_connect(hook_p hook)
239 {
240 	NG_HOOK_SET_PRIVATE(hook, NULL);
241 	NG_HOOK_REF(hook); /* Keep extra reference to the hook */
242 
243 	return (0);
244 } /* ng_btsocket_l2cap_raw_node_connect */
245 
246 /*
247  * Hook disconnection. Schedule route cleanup task
248  */
249 
250 static int
251 ng_btsocket_l2cap_raw_node_disconnect(hook_p hook)
252 {
253 	/*
254 	 * If hook has private information than we must have this hook in
255 	 * the routing table and must schedule cleaning for the routing table.
256 	 * Otherwise hook was connected but we never got "hook_info" message,
257 	 * so we have never added this hook to the routing table and it save
258 	 * to just delete it.
259 	 */
260 
261 	if (NG_HOOK_PRIVATE(hook) != NULL)
262 		return (ng_btsocket_l2cap_raw_wakeup_route_task());
263 
264 	NG_HOOK_UNREF(hook); /* Remove extra reference */
265 
266 	return (0);
267 } /* ng_btsocket_l2cap_raw_node_disconnect */
268 
269 /*
270  * Process incoming messages
271  */
272 
273 static int
274 ng_btsocket_l2cap_raw_node_rcvmsg(node_p node, item_p item, hook_p hook)
275 {
276 	struct ng_mesg	*msg = NGI_MSG(item); /* item still has message */
277 	int		 error = 0;
278 
279 	if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) {
280 
281 		/*
282 		 * NGM_L2CAP_NODE_HOOK_INFO is special message initiated by
283 		 * L2CAP layer. Ignore all other messages if they are not
284 		 * replies or token is zero
285 		 */
286 
287 		if (msg->header.cmd != NGM_L2CAP_NODE_HOOK_INFO) {
288 			if (msg->header.token == 0 ||
289 			    !(msg->header.flags & NGF_RESP)) {
290 				NG_FREE_ITEM(item);
291 				return (0);
292 			}
293 		}
294 
295 		mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
296 		if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_raw_queue)) {
297 			NG_BTSOCKET_L2CAP_RAW_ERR(
298 "%s: Input queue is full\n", __func__);
299 
300 			NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_raw_queue);
301 			NG_FREE_ITEM(item);
302 			error = ENOBUFS;
303 		} else {
304 			if (hook != NULL) {
305 				NG_HOOK_REF(hook);
306 				NGI_SET_HOOK(item, hook);
307 			}
308 
309 			NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_raw_queue, item);
310 			error = ng_btsocket_l2cap_raw_wakeup_input_task();
311 		}
312 		mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
313 	} else {
314 		NG_FREE_ITEM(item);
315 		error = EINVAL;
316 	}
317 
318 	return (error);
319 } /* ng_btsocket_l2cap_raw_node_rcvmsg */
320 
321 /*
322  * Receive data on a hook
323  */
324 
325 static int
326 ng_btsocket_l2cap_raw_node_rcvdata(hook_p hook, item_p item)
327 {
328 	NG_FREE_ITEM(item);
329 
330 	return (EINVAL);
331 } /* ng_btsocket_l2cap_raw_node_rcvdata */
332 
333 /*****************************************************************************
334  *****************************************************************************
335  **                              Socket interface
336  *****************************************************************************
337  *****************************************************************************/
338 
339 /*
340  * L2CAP sockets input routine
341  */
342 
343 static void
344 ng_btsocket_l2cap_raw_input(void *context, int pending)
345 {
346 	item_p		 item = NULL;
347 	hook_p		 hook = NULL;
348 	struct ng_mesg  *msg = NULL;
349 
350 	for (;;) {
351 		mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
352 		NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_raw_queue, item);
353 		mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
354 
355 		if (item == NULL)
356 			break;
357 
358 		KASSERT((item->el_flags & NGQF_TYPE) == NGQF_MESG,
359 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
360 
361 		NGI_GET_MSG(item, msg);
362 		NGI_GET_HOOK(item, hook);
363 		NG_FREE_ITEM(item);
364 
365 		switch (msg->header.cmd) {
366 		case NGM_L2CAP_NODE_HOOK_INFO: {
367 			ng_btsocket_l2cap_rtentry_t	*rt = NULL;
368 
369 			if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
370 			    msg->header.arglen != sizeof(bdaddr_t))
371 				break;
372 
373 			if (bcmp(msg->data, NG_HCI_BDADDR_ANY,
374 					sizeof(bdaddr_t)) == 0)
375 				break;
376 
377 			rt = (ng_btsocket_l2cap_rtentry_t *)
378 				NG_HOOK_PRIVATE(hook);
379 			if (rt == NULL) {
380 				MALLOC(rt, ng_btsocket_l2cap_rtentry_p,
381 					sizeof(*rt),
382 					M_NETGRAPH_BTSOCKET_L2CAP_RAW,
383 					M_NOWAIT|M_ZERO);
384 				if (rt == NULL)
385 					break;
386 
387 				NG_HOOK_SET_PRIVATE(hook, rt);
388 
389 				mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
390 
391 				LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_rt,
392 					rt, next);
393 			} else
394 				mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
395 
396 			bcopy(msg->data, &rt->src, sizeof(rt->src));
397 			rt->hook = hook;
398 
399 			NG_BTSOCKET_L2CAP_RAW_INFO(
400 "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n",
401 				__func__, NG_HOOK_NAME(hook),
402 				rt->src.b[5], rt->src.b[4], rt->src.b[3],
403 				rt->src.b[2], rt->src.b[1], rt->src.b[0]);
404 
405 			mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
406 			} break;
407 
408 		case NGM_L2CAP_NODE_GET_FLAGS:
409 		case NGM_L2CAP_NODE_GET_DEBUG:
410 		case NGM_L2CAP_NODE_GET_CON_LIST:
411 		case NGM_L2CAP_NODE_GET_CHAN_LIST:
412 		case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
413 		case NGM_L2CAP_L2CA_PING:
414 		case NGM_L2CAP_L2CA_GET_INFO: {
415 			ng_btsocket_l2cap_raw_pcb_p	pcb = NULL;
416 
417 			mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
418 
419 			LIST_FOREACH(pcb,&ng_btsocket_l2cap_raw_sockets,next) {
420 				mtx_lock(&pcb->pcb_mtx);
421 
422 				if (pcb->token == msg->header.token) {
423 					pcb->msg = msg;
424 					msg = NULL;
425 					wakeup(&pcb->msg);
426 					mtx_unlock(&pcb->pcb_mtx);
427 					break;
428 				}
429 
430 				mtx_unlock(&pcb->pcb_mtx);
431 			}
432 
433 			mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
434 			} break;
435 
436 		default:
437 			NG_BTSOCKET_L2CAP_RAW_WARN(
438 "%s: Unknown message, cmd=%d\n", __func__, msg->header.cmd);
439 			break;
440 		}
441 
442 		if (hook != NULL)
443 			NG_HOOK_UNREF(hook); /* remove extra reference */
444 
445 		NG_FREE_MSG(msg); /* Checks for msg != NULL */
446 	}
447 } /* ng_btsocket_l2cap_raw_input */
448 
449 /*
450  * Route cleanup task. Gets scheduled when hook is disconnected. Here we
451  * will find all sockets that use "invalid" hook and disconnect them.
452  */
453 
454 static void
455 ng_btsocket_l2cap_raw_rtclean(void *context, int pending)
456 {
457 	ng_btsocket_l2cap_raw_pcb_p	pcb = NULL;
458 	ng_btsocket_l2cap_rtentry_p	rt = NULL;
459 
460 	/*
461 	 * First disconnect all sockets that use "invalid" hook
462 	 */
463 
464 	mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
465 
466 	LIST_FOREACH(pcb, &ng_btsocket_l2cap_raw_sockets, next) {
467 		mtx_lock(&pcb->pcb_mtx);
468 
469 		if (pcb->rt != NULL &&
470 		    pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) {
471 			if (pcb->so != NULL &&
472 			    pcb->so->so_state & SS_ISCONNECTED)
473 				soisdisconnected(pcb->so);
474 
475 			pcb->rt = NULL;
476 		}
477 
478 		mtx_unlock(&pcb->pcb_mtx);
479 	}
480 
481 	mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
482 
483 	/*
484 	 * Now cleanup routing table
485 	 */
486 
487 	mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
488 
489 	for (rt = LIST_FIRST(&ng_btsocket_l2cap_raw_rt); rt != NULL; ) {
490 		ng_btsocket_l2cap_rtentry_p	rt_next = LIST_NEXT(rt, next);
491 
492 		if (rt->hook != NULL && NG_HOOK_NOT_VALID(rt->hook)) {
493 			LIST_REMOVE(rt, next);
494 
495 			NG_HOOK_SET_PRIVATE(rt->hook, NULL);
496 			NG_HOOK_UNREF(rt->hook); /* Remove extra reference */
497 
498 			bzero(rt, sizeof(*rt));
499 			FREE(rt, M_NETGRAPH_BTSOCKET_L2CAP_RAW);
500 		}
501 
502 		rt = rt_next;
503 	}
504 
505 	mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
506 } /* ng_btsocket_l2cap_raw_rtclean */
507 
508 /*
509  * Initialize everything
510  */
511 
512 void
513 ng_btsocket_l2cap_raw_init(void)
514 {
515 	int	error = 0;
516 
517 	ng_btsocket_l2cap_raw_node = NULL;
518 	ng_btsocket_l2cap_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
519 	ng_btsocket_l2cap_raw_ioctl_timeout = 5;
520 
521 	/* Register Netgraph node type */
522 	error = ng_newtype(&typestruct);
523 	if (error != 0) {
524 		NG_BTSOCKET_L2CAP_RAW_ALERT(
525 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
526 
527                 return;
528 	}
529 
530 	/* Create Netgrapg node */
531 	error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node);
532 	if (error != 0) {
533 		NG_BTSOCKET_L2CAP_RAW_ALERT(
534 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
535 
536 		ng_btsocket_l2cap_raw_node = NULL;
537 
538 		return;
539 	}
540 
541 	error = ng_name_node(ng_btsocket_l2cap_raw_node,
542 				NG_BTSOCKET_L2CAP_RAW_NODE_TYPE);
543 	if (error != 0) {
544 		NG_BTSOCKET_L2CAP_RAW_ALERT(
545 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
546 
547 		NG_NODE_UNREF(ng_btsocket_l2cap_raw_node);
548 		ng_btsocket_l2cap_raw_node = NULL;
549 
550 		return;
551 	}
552 
553 	/* Create input queue */
554 	NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_raw_queue, ifqmaxlen);
555 	mtx_init(&ng_btsocket_l2cap_raw_queue_mtx,
556 		"btsocks_l2cap_raw_queue_mtx", NULL, MTX_DEF);
557 	TASK_INIT(&ng_btsocket_l2cap_raw_queue_task, 0,
558 		ng_btsocket_l2cap_raw_input, NULL);
559 
560 	/* Create list of sockets */
561 	LIST_INIT(&ng_btsocket_l2cap_raw_sockets);
562 	mtx_init(&ng_btsocket_l2cap_raw_sockets_mtx,
563 		"btsocks_l2cap_raw_sockets_mtx", NULL, MTX_DEF);
564 
565 	/* Tokens */
566 	ng_btsocket_l2cap_raw_token = 0;
567 	mtx_init(&ng_btsocket_l2cap_raw_token_mtx,
568 		"btsocks_l2cap_raw_token_mtx", NULL, MTX_DEF);
569 
570 	/* Routing table */
571 	LIST_INIT(&ng_btsocket_l2cap_raw_rt);
572 	mtx_init(&ng_btsocket_l2cap_raw_rt_mtx,
573 		"btsocks_l2cap_raw_rt_mtx", NULL, MTX_DEF);
574 	TASK_INIT(&ng_btsocket_l2cap_raw_rt_task, 0,
575 		ng_btsocket_l2cap_raw_rtclean, NULL);
576 } /* ng_btsocket_l2cap_raw_init */
577 
578 /*
579  * Abort connection on socket
580  */
581 
582 void
583 ng_btsocket_l2cap_raw_abort(struct socket *so)
584 {
585 
586 	(void)ng_btsocket_l2cap_raw_disconnect(so);
587 } /* ng_btsocket_l2cap_raw_abort */
588 
589 void
590 ng_btsocket_l2cap_raw_close(struct socket *so)
591 {
592 
593 	(void)ng_btsocket_l2cap_raw_disconnect(so);
594 } /* ng_btsocket_l2cap_raw_close */
595 
596 /*
597  * Create and attach new socket
598  */
599 
600 int
601 ng_btsocket_l2cap_raw_attach(struct socket *so, int proto, struct thread *td)
602 {
603 	ng_btsocket_l2cap_raw_pcb_p	pcb = so2l2cap_raw_pcb(so);
604 	int				error;
605 
606 	if (pcb != NULL)
607 		return (EISCONN);
608 
609 	if (ng_btsocket_l2cap_raw_node == NULL)
610 		return (EPROTONOSUPPORT);
611 	if (so->so_type != SOCK_RAW)
612 		return (ESOCKTNOSUPPORT);
613 
614 	/* Reserve send and receive space if it is not reserved yet */
615 	error = soreserve(so, NG_BTSOCKET_L2CAP_RAW_SENDSPACE,
616 			NG_BTSOCKET_L2CAP_RAW_RECVSPACE);
617 	if (error != 0)
618 		return (error);
619 
620 	/* Allocate the PCB */
621         MALLOC(pcb, ng_btsocket_l2cap_raw_pcb_p, sizeof(*pcb),
622 		M_NETGRAPH_BTSOCKET_L2CAP_RAW, M_NOWAIT|M_ZERO);
623         if (pcb == NULL)
624                 return (ENOMEM);
625 
626 	/* Link the PCB and the socket */
627 	so->so_pcb = (caddr_t) pcb;
628 	pcb->so = so;
629 
630 	if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
631 		pcb->flags |= NG_BTSOCKET_L2CAP_RAW_PRIVILEGED;
632 
633 	mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_raw_pcb_mtx", NULL, MTX_DEF);
634 
635         /* Add the PCB to the list */
636 	mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
637 	LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_sockets, pcb, next);
638 	mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
639 
640         return (0);
641 } /* ng_btsocket_l2cap_raw_attach */
642 
643 /*
644  * Bind socket
645  */
646 
647 int
648 ng_btsocket_l2cap_raw_bind(struct socket *so, struct sockaddr *nam,
649 		struct thread *td)
650 {
651 	ng_btsocket_l2cap_raw_pcb_t	*pcb = so2l2cap_raw_pcb(so);
652 	struct sockaddr_l2cap		*sa = (struct sockaddr_l2cap *) nam;
653 	ng_btsocket_l2cap_rtentry_t	*rt = NULL;
654 
655 	if (pcb == NULL)
656 		return (EINVAL);
657 	if (ng_btsocket_l2cap_raw_node == NULL)
658 		return (EINVAL);
659 
660 	if (sa == NULL)
661 		return (EINVAL);
662 	if (sa->l2cap_family != AF_BLUETOOTH)
663 		return (EAFNOSUPPORT);
664 	if (sa->l2cap_len != sizeof(*sa))
665 		return (EINVAL);
666 
667 	if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY,
668 				sizeof(sa->l2cap_bdaddr)) != 0) {
669 		mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
670 
671 		LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
672 			if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
673 				continue;
674 
675 			if (bcmp(&sa->l2cap_bdaddr, &rt->src,
676 					sizeof(rt->src)) == 0)
677 				break;
678 		}
679 
680 		mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
681 
682 		if (rt == NULL)
683 			return (ENETDOWN);
684 	} else
685 		rt = NULL;
686 
687 	mtx_lock(&pcb->pcb_mtx);
688 	bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src));
689 	pcb->rt = rt;
690 	mtx_unlock(&pcb->pcb_mtx);
691 
692 	return (0);
693 } /* ng_btsocket_l2cap_raw_bind */
694 
695 /*
696  * Connect socket
697  */
698 
699 int
700 ng_btsocket_l2cap_raw_connect(struct socket *so, struct sockaddr *nam,
701 		struct thread *td)
702 {
703 	ng_btsocket_l2cap_raw_pcb_t	*pcb = so2l2cap_raw_pcb(so);
704 	struct sockaddr_l2cap		*sa = (struct sockaddr_l2cap *) nam;
705 	ng_btsocket_l2cap_rtentry_t	*rt = NULL;
706 	int				 error;
707 
708 	if (pcb == NULL)
709 		return (EINVAL);
710 	if (ng_btsocket_l2cap_raw_node == NULL)
711 		return (EINVAL);
712 
713 	if (sa == NULL)
714 		return (EINVAL);
715 	if (sa->l2cap_family != AF_BLUETOOTH)
716 		return (EAFNOSUPPORT);
717 	if (sa->l2cap_len != sizeof(*sa))
718 		return (EINVAL);
719 	if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
720 		return (EINVAL);
721 
722 	mtx_lock(&pcb->pcb_mtx);
723 
724 	bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst));
725 
726 	if (bcmp(&pcb->src, &pcb->dst, sizeof(pcb->src)) == 0) {
727 		mtx_unlock(&pcb->pcb_mtx);
728 
729 		return (EADDRNOTAVAIL);
730 	}
731 
732 	/*
733 	 * If there is route already - use it
734 	 */
735 
736 	if (pcb->rt != NULL) {
737 		soisconnected(so);
738 		mtx_unlock(&pcb->pcb_mtx);
739 
740 		return (0);
741 	}
742 
743 	/*
744 	 * Find the first hook that does not match specified destination address
745 	 */
746 
747 	mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
748 
749 	LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
750 		if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
751 			continue;
752 
753 		if (bcmp(&pcb->dst, &rt->src, sizeof(rt->src)) != 0)
754 			break;
755 	}
756 
757 	if (rt != NULL) {
758 		soisconnected(so);
759 
760 		pcb->rt = rt;
761 		bcopy(&rt->src, &pcb->src, sizeof(pcb->src));
762 
763 		error = 0;
764 	} else
765 		error = ENETDOWN;
766 
767 	mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
768 	mtx_unlock(&pcb->pcb_mtx);
769 
770 	return  (error);
771 } /* ng_btsocket_l2cap_raw_connect */
772 
773 /*
774  * Process ioctl's calls on socket
775  */
776 
777 int
778 ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
779 		struct ifnet *ifp, struct thread *td)
780 {
781 	ng_btsocket_l2cap_raw_pcb_p	 pcb = so2l2cap_raw_pcb(so);
782 	struct ng_mesg			*msg = NULL;
783 	int				 error = 0;
784 
785 	if (pcb == NULL)
786 		return (EINVAL);
787 	if (ng_btsocket_l2cap_raw_node == NULL)
788 		return (EINVAL);
789 
790 	mtx_lock(&pcb->pcb_mtx);
791 
792 	/* Check if we route info */
793 	if (pcb->rt == NULL) {
794 		mtx_unlock(&pcb->pcb_mtx);
795 		return (EHOSTUNREACH);
796 	}
797 
798 	/* Check if we have pending ioctl() */
799 	if (pcb->token != 0) {
800 		mtx_unlock(&pcb->pcb_mtx);
801 		return (EBUSY);
802 	}
803 
804 	switch (cmd) {
805 	case SIOC_L2CAP_NODE_GET_FLAGS: {
806 		struct ng_btsocket_l2cap_raw_node_flags	*p =
807 			(struct ng_btsocket_l2cap_raw_node_flags *) data;
808 
809 		error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
810 				NGM_L2CAP_NODE_GET_FLAGS,
811 				&p->flags, sizeof(p->flags));
812 		} break;
813 
814 	case SIOC_L2CAP_NODE_GET_DEBUG: {
815 		struct ng_btsocket_l2cap_raw_node_debug	*p =
816 			(struct ng_btsocket_l2cap_raw_node_debug *) data;
817 
818 		error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
819 				NGM_L2CAP_NODE_GET_DEBUG,
820 				&p->debug, sizeof(p->debug));
821 		} break;
822 
823 	case SIOC_L2CAP_NODE_SET_DEBUG: {
824 		struct ng_btsocket_l2cap_raw_node_debug	*p =
825 			(struct ng_btsocket_l2cap_raw_node_debug *) data;
826 
827 		if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)
828 			error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook,
829 					NGM_L2CAP_NODE_SET_DEBUG,
830 					&p->debug, sizeof(p->debug));
831 		else
832 			error = EPERM;
833 		} break;
834 
835 	case SIOC_L2CAP_NODE_GET_CON_LIST: {
836 		struct ng_btsocket_l2cap_raw_con_list	*p =
837 			(struct ng_btsocket_l2cap_raw_con_list *) data;
838 		ng_l2cap_node_con_list_ep		*p1 = NULL;
839                 ng_l2cap_node_con_ep			*p2 = NULL;
840 
841 		if (p->num_connections == 0 ||
842 		    p->num_connections > NG_L2CAP_MAX_CON_NUM ||
843 		    p->connections == NULL) {
844 			error = EINVAL;
845 			break;
846 		}
847 
848 		NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_NODE_GET_CON_LIST,
849 			0, M_NOWAIT);
850 		if (msg == NULL) {
851 			error = ENOMEM;
852 			break;
853 		}
854 		ng_btsocket_l2cap_raw_get_token(&msg->header.token);
855 		pcb->token = msg->header.token;
856 		pcb->msg = NULL;
857 
858 		NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
859 			pcb->rt->hook, 0);
860 		if (error != 0) {
861 			pcb->token = 0;
862 			break;
863 		}
864 
865 		error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
866 				ng_btsocket_l2cap_raw_ioctl_timeout * hz);
867 		pcb->token = 0;
868 
869 		if (error != 0)
870 			break;
871 
872 		if (pcb->msg != NULL &&
873 		    pcb->msg->header.cmd == NGM_L2CAP_NODE_GET_CON_LIST) {
874 			/* Return data back to user space */
875 			p1 = (ng_l2cap_node_con_list_ep *)(pcb->msg->data);
876 			p2 = (ng_l2cap_node_con_ep *)(p1 + 1);
877 
878 			p->num_connections = min(p->num_connections,
879 						p1->num_connections);
880 			if (p->num_connections > 0)
881 				error = copyout((caddr_t) p2,
882 					(caddr_t) p->connections,
883 					p->num_connections * sizeof(*p2));
884 		} else
885 			error = EINVAL;
886 
887 		NG_FREE_MSG(pcb->msg); /* checks for != NULL */
888 		} break;
889 
890 	case SIOC_L2CAP_NODE_GET_CHAN_LIST: {
891 		struct ng_btsocket_l2cap_raw_chan_list	*p =
892 			(struct ng_btsocket_l2cap_raw_chan_list *) data;
893 		ng_l2cap_node_chan_list_ep		*p1 = NULL;
894                 ng_l2cap_node_chan_ep			*p2 = NULL;
895 
896 		if (p->num_channels == 0 ||
897 		    p->num_channels > NG_L2CAP_MAX_CHAN_NUM ||
898 		    p->channels == NULL) {
899 			error = EINVAL;
900 			break;
901 		}
902 
903 		NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
904 			NGM_L2CAP_NODE_GET_CHAN_LIST, 0, M_NOWAIT);
905 		if (msg == NULL) {
906 			error = ENOMEM;
907 			break;
908 		}
909 		ng_btsocket_l2cap_raw_get_token(&msg->header.token);
910 		pcb->token = msg->header.token;
911 		pcb->msg = NULL;
912 
913 		NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
914 			pcb->rt->hook, 0);
915 		if (error != 0) {
916 			pcb->token = 0;
917 			break;
918 		}
919 
920 		error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
921 				ng_btsocket_l2cap_raw_ioctl_timeout * hz);
922 		pcb->token = 0;
923 
924 		if (error != 0)
925 			break;
926 
927 		if (pcb->msg != NULL &&
928 		    pcb->msg->header.cmd == NGM_L2CAP_NODE_GET_CHAN_LIST) {
929 			/* Return data back to user space */
930 			p1 = (ng_l2cap_node_chan_list_ep *)(pcb->msg->data);
931 			p2 = (ng_l2cap_node_chan_ep *)(p1 + 1);
932 
933 			p->num_channels = min(p->num_channels,
934 						p1->num_channels);
935 			if (p->num_channels > 0)
936 				error = copyout((caddr_t) p2,
937 						(caddr_t) p->channels,
938 						p->num_channels * sizeof(*p2));
939 		} else
940 			error = EINVAL;
941 
942 		NG_FREE_MSG(pcb->msg); /* checks for != NULL */
943 		} break;
944 
945 	case SIOC_L2CAP_L2CA_PING: {
946 		struct ng_btsocket_l2cap_raw_ping	*p =
947 			(struct ng_btsocket_l2cap_raw_ping *) data;
948 		ng_l2cap_l2ca_ping_ip			*ip = NULL;
949 		ng_l2cap_l2ca_ping_op			*op = NULL;
950 
951 		if (!(pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)) {
952 			error = EPERM;
953 			break;
954 		}
955 
956 		if ((p->echo_size != 0 && p->echo_data == NULL) ||
957 		     p->echo_size > NG_L2CAP_MAX_ECHO_SIZE) {
958 			error = EINVAL;
959 			break;
960 		}
961 
962 		NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
963 			NGM_L2CAP_L2CA_PING, sizeof(*ip) + p->echo_size,
964 			M_NOWAIT);
965 		if (msg == NULL) {
966 			error = ENOMEM;
967 			break;
968 		}
969 		ng_btsocket_l2cap_raw_get_token(&msg->header.token);
970 		pcb->token = msg->header.token;
971 		pcb->msg = NULL;
972 
973 		ip = (ng_l2cap_l2ca_ping_ip *)(msg->data);
974 		bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
975 		ip->echo_size = p->echo_size;
976 
977 		if (ip->echo_size > 0) {
978 			error = copyin(p->echo_data, ip + 1, p->echo_size);
979 			if (error != 0) {
980 				NG_FREE_MSG(msg);
981 				pcb->token = 0;
982 				break;
983 			}
984 		}
985 
986 		NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
987 			pcb->rt->hook, 0);
988 		if (error != 0) {
989 			pcb->token = 0;
990 			break;
991 		}
992 
993 		error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
994 				bluetooth_l2cap_rtx_timeout());
995 		pcb->token = 0;
996 
997 		if (error != 0)
998 			break;
999 
1000 		if (pcb->msg != NULL &&
1001 		    pcb->msg->header.cmd == NGM_L2CAP_L2CA_PING) {
1002 			/* Return data back to the user space */
1003 			op = (ng_l2cap_l2ca_ping_op *)(pcb->msg->data);
1004 			p->result = op->result;
1005 			p->echo_size = min(p->echo_size, op->echo_size);
1006 
1007 			if (p->echo_size > 0)
1008 				error = copyout(op + 1, p->echo_data,
1009 						p->echo_size);
1010 		} else
1011 			error = EINVAL;
1012 
1013 		NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1014 		} break;
1015 
1016 	case SIOC_L2CAP_L2CA_GET_INFO: {
1017 		struct ng_btsocket_l2cap_raw_get_info	*p =
1018 			(struct ng_btsocket_l2cap_raw_get_info *) data;
1019 		ng_l2cap_l2ca_get_info_ip		*ip = NULL;
1020 		ng_l2cap_l2ca_get_info_op		*op = NULL;
1021 
1022 		if (!(pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)) {
1023 			error = EPERM;
1024 			break;
1025 		}
1026 
1027 		if (p->info_size != 0 && p->info_data == NULL) {
1028 			error = EINVAL;
1029 			break;
1030 		}
1031 
1032 		NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
1033 			NGM_L2CAP_L2CA_GET_INFO, sizeof(*ip) + p->info_size,
1034 			M_NOWAIT);
1035 		if (msg == NULL) {
1036 			error = ENOMEM;
1037 			break;
1038 		}
1039 		ng_btsocket_l2cap_raw_get_token(&msg->header.token);
1040 		pcb->token = msg->header.token;
1041 		pcb->msg = NULL;
1042 
1043 		ip = (ng_l2cap_l2ca_get_info_ip *)(msg->data);
1044 		bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
1045 		ip->info_type = p->info_type;
1046 
1047 		NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
1048 			pcb->rt->hook, 0);
1049 		if (error != 0) {
1050 			pcb->token = 0;
1051 			break;
1052 		}
1053 
1054 		error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
1055 				bluetooth_l2cap_rtx_timeout());
1056 		pcb->token = 0;
1057 
1058 		if (error != 0)
1059 			break;
1060 
1061 		if (pcb->msg != NULL &&
1062 		    pcb->msg->header.cmd == NGM_L2CAP_L2CA_GET_INFO) {
1063 			/* Return data back to the user space */
1064 			op = (ng_l2cap_l2ca_get_info_op *)(pcb->msg->data);
1065 			p->result = op->result;
1066 			p->info_size = min(p->info_size, op->info_size);
1067 
1068 			if (p->info_size > 0)
1069 				error = copyout(op + 1, p->info_data,
1070 						p->info_size);
1071 		} else
1072 			error = EINVAL;
1073 
1074 		NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1075 		} break;
1076 
1077 	case SIOC_L2CAP_NODE_GET_AUTO_DISCON_TIMO: {
1078 		struct ng_btsocket_l2cap_raw_auto_discon_timo	*p =
1079 			(struct ng_btsocket_l2cap_raw_auto_discon_timo *) data;
1080 
1081 		error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
1082 				NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO,
1083 				&p->timeout, sizeof(p->timeout));
1084 		} break;
1085 
1086 	case SIOC_L2CAP_NODE_SET_AUTO_DISCON_TIMO: {
1087 		struct ng_btsocket_l2cap_raw_auto_discon_timo	*p =
1088 			(struct ng_btsocket_l2cap_raw_auto_discon_timo *) data;
1089 
1090 		if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)
1091 			error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook,
1092 					NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO,
1093 					&p->timeout, sizeof(p->timeout));
1094 		else
1095 			error = EPERM;
1096 		} break;
1097 
1098 	default:
1099 		error = EINVAL;
1100 		break;
1101 	}
1102 
1103 	mtx_unlock(&pcb->pcb_mtx);
1104 
1105 	return (error);
1106 } /* ng_btsocket_l2cap_raw_control */
1107 
1108 /*
1109  * Detach and destroy socket
1110  */
1111 
1112 void
1113 ng_btsocket_l2cap_raw_detach(struct socket *so)
1114 {
1115 	ng_btsocket_l2cap_raw_pcb_p	pcb = so2l2cap_raw_pcb(so);
1116 
1117 	KASSERT(pcb != NULL, ("nt_btsocket_l2cap_raw_detach: pcb == NULL"));
1118 	if (ng_btsocket_l2cap_raw_node == NULL)
1119 		return;
1120 
1121 	mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
1122 	mtx_lock(&pcb->pcb_mtx);
1123 
1124 	LIST_REMOVE(pcb, next);
1125 
1126 	mtx_unlock(&pcb->pcb_mtx);
1127 	mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
1128 
1129 	mtx_destroy(&pcb->pcb_mtx);
1130 
1131 	bzero(pcb, sizeof(*pcb));
1132 	FREE(pcb, M_NETGRAPH_BTSOCKET_L2CAP_RAW);
1133 
1134 	so->so_pcb = NULL;
1135 } /* ng_btsocket_l2cap_raw_detach */
1136 
1137 /*
1138  * Disconnect socket
1139  */
1140 
1141 int
1142 ng_btsocket_l2cap_raw_disconnect(struct socket *so)
1143 {
1144 	ng_btsocket_l2cap_raw_pcb_p	pcb = so2l2cap_raw_pcb(so);
1145 
1146 	if (pcb == NULL)
1147 		return (EINVAL);
1148 	if (ng_btsocket_l2cap_raw_node == NULL)
1149 		return (EINVAL);
1150 
1151 	mtx_lock(&pcb->pcb_mtx);
1152 	pcb->rt = NULL;
1153 	soisdisconnected(so);
1154 	mtx_unlock(&pcb->pcb_mtx);
1155 
1156 	return (0);
1157 } /* ng_btsocket_l2cap_raw_disconnect */
1158 
1159 /*
1160  * Get peer address
1161  */
1162 
1163 int
1164 ng_btsocket_l2cap_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1165 {
1166 	ng_btsocket_l2cap_raw_pcb_p	pcb = so2l2cap_raw_pcb(so);
1167 	struct sockaddr_l2cap		sa;
1168 
1169 	if (pcb == NULL)
1170 		return (EINVAL);
1171 	if (ng_btsocket_l2cap_raw_node == NULL)
1172 		return (EINVAL);
1173 
1174 	mtx_lock(&pcb->pcb_mtx);
1175 	bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
1176 	mtx_unlock(&pcb->pcb_mtx);
1177 
1178 	sa.l2cap_psm = 0;
1179 	sa.l2cap_len = sizeof(sa);
1180 	sa.l2cap_family = AF_BLUETOOTH;
1181 
1182 	*nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1183 
1184 	return ((*nam == NULL)? ENOMEM : 0);
1185 } /* ng_btsocket_l2cap_raw_peeraddr */
1186 
1187 /*
1188  * Send data to socket
1189  */
1190 
1191 int
1192 ng_btsocket_l2cap_raw_send(struct socket *so, int flags, struct mbuf *m,
1193 		struct sockaddr *nam, struct mbuf *control, struct thread *td)
1194 {
1195 	NG_FREE_M(m); /* Checks for m != NULL */
1196 	NG_FREE_M(control);
1197 
1198 	return (EOPNOTSUPP);
1199 } /* ng_btsocket_l2cap_raw_send */
1200 
1201 /*
1202  * Get socket address
1203  */
1204 
1205 int
1206 ng_btsocket_l2cap_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1207 {
1208 	ng_btsocket_l2cap_raw_pcb_p	pcb = so2l2cap_raw_pcb(so);
1209 	struct sockaddr_l2cap		sa;
1210 
1211 	if (pcb == NULL)
1212 		return (EINVAL);
1213 	if (ng_btsocket_l2cap_raw_node == NULL)
1214 		return (EINVAL);
1215 
1216 	mtx_lock(&pcb->pcb_mtx);
1217 	bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
1218 	mtx_unlock(&pcb->pcb_mtx);
1219 
1220 	sa.l2cap_psm = 0;
1221 	sa.l2cap_len = sizeof(sa);
1222 	sa.l2cap_family = AF_BLUETOOTH;
1223 
1224 	*nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1225 
1226 	return ((*nam == NULL)? ENOMEM : 0);
1227 } /* ng_btsocket_l2cap_raw_sockaddr */
1228 
1229 /*
1230  * Get next token
1231  */
1232 
1233 static void
1234 ng_btsocket_l2cap_raw_get_token(u_int32_t *token)
1235 {
1236 	mtx_lock(&ng_btsocket_l2cap_raw_token_mtx);
1237 
1238 	if (++ ng_btsocket_l2cap_raw_token == 0)
1239 		ng_btsocket_l2cap_raw_token = 1;
1240 
1241 	*token = ng_btsocket_l2cap_raw_token;
1242 
1243 	mtx_unlock(&ng_btsocket_l2cap_raw_token_mtx);
1244 } /* ng_btsocket_l2cap_raw_get_token */
1245 
1246 /*
1247  * Send Netgraph message to the node - do not expect reply
1248  */
1249 
1250 static int
1251 ng_btsocket_l2cap_raw_send_ngmsg(hook_p hook, int cmd, void *arg, int arglen)
1252 {
1253 	struct ng_mesg	*msg = NULL;
1254 	int		 error = 0;
1255 
1256 	NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, arglen, M_NOWAIT);
1257 	if (msg == NULL)
1258 		return (ENOMEM);
1259 
1260 	if (arg != NULL && arglen > 0)
1261 		bcopy(arg, msg->data, arglen);
1262 
1263 	NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, hook, 0);
1264 
1265 	return (error);
1266 } /* ng_btsocket_l2cap_raw_send_ngmsg */
1267 
1268 /*
1269  * Send Netgraph message to the node (no data) and wait for reply
1270  */
1271 
1272 static int
1273 ng_btsocket_l2cap_raw_send_sync_ngmsg(ng_btsocket_l2cap_raw_pcb_p pcb,
1274 		int cmd, void *rsp, int rsplen)
1275 {
1276 	struct ng_mesg	*msg = NULL;
1277 	int		 error = 0;
1278 
1279 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1280 
1281 	NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, 0, M_NOWAIT);
1282 	if (msg == NULL)
1283 		return (ENOMEM);
1284 
1285 	ng_btsocket_l2cap_raw_get_token(&msg->header.token);
1286 	pcb->token = msg->header.token;
1287 	pcb->msg = NULL;
1288 
1289 	NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
1290 		pcb->rt->hook, 0);
1291 	if (error != 0) {
1292 		pcb->token = 0;
1293 		return (error);
1294 	}
1295 
1296 	error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
1297 			ng_btsocket_l2cap_raw_ioctl_timeout * hz);
1298 	pcb->token = 0;
1299 
1300 	if (error != 0)
1301 		return (error);
1302 
1303 	if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
1304 		bcopy(pcb->msg->data, rsp, rsplen);
1305 	else
1306 		error = EINVAL;
1307 
1308 	NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1309 
1310 	return (0);
1311 } /* ng_btsocket_l2cap_raw_send_sync_ngmsg */
1312 
1313