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