xref: /freebsd/sys/netgraph/bluetooth/socket/ng_btsocket_l2cap.c (revision a812392203d7c4c3f0db9d8a0f3391374c49c71f)
1 /*
2  * ng_btsocket_l2cap.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.c,v 1.16 2003/09/14 23:29:06 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bitstring.h>
37 #include <sys/domain.h>
38 #include <sys/endian.h>
39 #include <sys/errno.h>
40 #include <sys/filedesc.h>
41 #include <sys/ioccom.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/mutex.h>
47 #include <sys/protosw.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/taskqueue.h>
53 
54 #include <net/vnet.h>
55 
56 #include <netgraph/ng_message.h>
57 #include <netgraph/netgraph.h>
58 #include <netgraph/bluetooth/include/ng_bluetooth.h>
59 #include <netgraph/bluetooth/include/ng_hci.h>
60 #include <netgraph/bluetooth/include/ng_l2cap.h>
61 #include <netgraph/bluetooth/include/ng_btsocket.h>
62 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
63 
64 /* MALLOC define */
65 #ifdef NG_SEPARATE_MALLOC
66 static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP, "netgraph_btsocks_l2cap",
67 		"Netgraph Bluetooth L2CAP sockets");
68 #else
69 #define M_NETGRAPH_BTSOCKET_L2CAP M_NETGRAPH
70 #endif /* NG_SEPARATE_MALLOC */
71 
72 /* Netgraph node methods */
73 static ng_constructor_t	ng_btsocket_l2cap_node_constructor;
74 static ng_rcvmsg_t	ng_btsocket_l2cap_node_rcvmsg;
75 static ng_shutdown_t	ng_btsocket_l2cap_node_shutdown;
76 static ng_newhook_t	ng_btsocket_l2cap_node_newhook;
77 static ng_connect_t	ng_btsocket_l2cap_node_connect;
78 static ng_rcvdata_t	ng_btsocket_l2cap_node_rcvdata;
79 static ng_disconnect_t	ng_btsocket_l2cap_node_disconnect;
80 
81 static void		ng_btsocket_l2cap_input   (void *, int);
82 static void		ng_btsocket_l2cap_rtclean (void *, int);
83 
84 /* Netgraph type descriptor */
85 static struct ng_type	typestruct = {
86 	.version =	NG_ABI_VERSION,
87 	.name =		NG_BTSOCKET_L2CAP_NODE_TYPE,
88 	.constructor =	ng_btsocket_l2cap_node_constructor,
89 	.rcvmsg =	ng_btsocket_l2cap_node_rcvmsg,
90 	.shutdown =	ng_btsocket_l2cap_node_shutdown,
91 	.newhook =	ng_btsocket_l2cap_node_newhook,
92 	.connect =	ng_btsocket_l2cap_node_connect,
93 	.rcvdata =	ng_btsocket_l2cap_node_rcvdata,
94 	.disconnect =	ng_btsocket_l2cap_node_disconnect,
95 };
96 
97 /* Globals */
98 extern int					ifqmaxlen;
99 static u_int32_t				ng_btsocket_l2cap_debug_level;
100 static node_p					ng_btsocket_l2cap_node;
101 static struct ng_bt_itemq			ng_btsocket_l2cap_queue;
102 static struct mtx				ng_btsocket_l2cap_queue_mtx;
103 static struct task				ng_btsocket_l2cap_queue_task;
104 static LIST_HEAD(, ng_btsocket_l2cap_pcb)	ng_btsocket_l2cap_sockets;
105 static struct mtx				ng_btsocket_l2cap_sockets_mtx;
106 static LIST_HEAD(, ng_btsocket_l2cap_rtentry)	ng_btsocket_l2cap_rt;
107 static struct mtx				ng_btsocket_l2cap_rt_mtx;
108 static struct task				ng_btsocket_l2cap_rt_task;
109 static struct timeval				ng_btsocket_l2cap_lasttime;
110 static int					ng_btsocket_l2cap_curpps;
111 
112 /* Sysctl tree */
113 SYSCTL_DECL(_net_bluetooth_l2cap_sockets);
114 static SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, seq, CTLFLAG_RW,
115 	0, "Bluetooth SEQPACKET L2CAP sockets family");
116 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, debug_level,
117 	CTLFLAG_RW,
118 	&ng_btsocket_l2cap_debug_level, NG_BTSOCKET_WARN_LEVEL,
119 	"Bluetooth SEQPACKET L2CAP sockets debug level");
120 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_len,
121 	CTLFLAG_RD,
122 	&ng_btsocket_l2cap_queue.len, 0,
123 	"Bluetooth SEQPACKET L2CAP sockets input queue length");
124 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_maxlen,
125 	CTLFLAG_RD,
126 	&ng_btsocket_l2cap_queue.maxlen, 0,
127 	"Bluetooth SEQPACKET L2CAP sockets input queue max. length");
128 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_drops,
129 	CTLFLAG_RD,
130 	&ng_btsocket_l2cap_queue.drops, 0,
131 	"Bluetooth SEQPACKET L2CAP sockets input queue drops");
132 
133 /* Debug */
134 #define NG_BTSOCKET_L2CAP_INFO \
135 	if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
136 	    ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
137 		printf
138 
139 #define NG_BTSOCKET_L2CAP_WARN \
140 	if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
141 	    ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
142 		printf
143 
144 #define NG_BTSOCKET_L2CAP_ERR \
145 	if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
146 	    ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
147 		printf
148 
149 #define NG_BTSOCKET_L2CAP_ALERT \
150 	if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
151 	    ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
152 		printf
153 
154 /*
155  * Netgraph message processing routines
156  */
157 
158 static int ng_btsocket_l2cap_process_l2ca_con_req_rsp
159 	(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
160 static int ng_btsocket_l2cap_process_l2ca_con_rsp_rsp
161 	(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
162 static int ng_btsocket_l2cap_process_l2ca_con_ind
163 	(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
164 
165 static int ng_btsocket_l2cap_process_l2ca_cfg_req_rsp
166 	(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
167 static int ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp
168 	(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
169 static int ng_btsocket_l2cap_process_l2ca_cfg_ind
170 	(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
171 
172 static int ng_btsocket_l2cap_process_l2ca_discon_rsp
173 	(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
174 static int ng_btsocket_l2cap_process_l2ca_discon_ind
175 	(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
176 
177 static int ng_btsocket_l2cap_process_l2ca_write_rsp
178 	(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
179 
180 /*
181  * Send L2CA_xxx messages to the lower layer
182  */
183 
184 static int  ng_btsocket_l2cap_send_l2ca_con_req
185 	(ng_btsocket_l2cap_pcb_p);
186 static int  ng_btsocket_l2cap_send_l2ca_con_rsp_req
187 	(u_int32_t, ng_btsocket_l2cap_rtentry_p, bdaddr_p, int, int, int);
188 static int  ng_btsocket_l2cap_send_l2ca_cfg_req
189 	(ng_btsocket_l2cap_pcb_p);
190 static int  ng_btsocket_l2cap_send_l2ca_cfg_rsp
191 	(ng_btsocket_l2cap_pcb_p);
192 static int  ng_btsocket_l2cap_send_l2ca_discon_req
193 	(u_int32_t, ng_btsocket_l2cap_pcb_p);
194 
195 static int ng_btsocket_l2cap_send2
196 	(ng_btsocket_l2cap_pcb_p);
197 
198 /*
199  * Timeout processing routines
200  */
201 
202 static void ng_btsocket_l2cap_timeout         (ng_btsocket_l2cap_pcb_p);
203 static void ng_btsocket_l2cap_untimeout       (ng_btsocket_l2cap_pcb_p);
204 static void ng_btsocket_l2cap_process_timeout (void *);
205 
206 /*
207  * Other stuff
208  */
209 
210 static ng_btsocket_l2cap_pcb_p     ng_btsocket_l2cap_pcb_by_addr(bdaddr_p, int);
211 static ng_btsocket_l2cap_pcb_p     ng_btsocket_l2cap_pcb_by_token(u_int32_t);
212 static ng_btsocket_l2cap_pcb_p     ng_btsocket_l2cap_pcb_by_cid (bdaddr_p, int);
213 static int                         ng_btsocket_l2cap_result2errno(int);
214 
215 #define ng_btsocket_l2cap_wakeup_input_task() \
216 	taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_queue_task)
217 
218 #define ng_btsocket_l2cap_wakeup_route_task() \
219 	taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_rt_task)
220 
221 /*****************************************************************************
222  *****************************************************************************
223  **                        Netgraph node interface
224  *****************************************************************************
225  *****************************************************************************/
226 
227 /*
228  * Netgraph node constructor. Do not allow to create node of this type.
229  */
230 
231 static int
232 ng_btsocket_l2cap_node_constructor(node_p node)
233 {
234 	return (EINVAL);
235 } /* ng_btsocket_l2cap_node_constructor */
236 
237 /*
238  * Do local shutdown processing. Let old node go and create new fresh one.
239  */
240 
241 static int
242 ng_btsocket_l2cap_node_shutdown(node_p node)
243 {
244 	int	error = 0;
245 
246 	NG_NODE_UNREF(node);
247 
248 	/* Create new node */
249 	error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_node);
250 	if (error != 0) {
251 		NG_BTSOCKET_L2CAP_ALERT(
252 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
253 
254 		ng_btsocket_l2cap_node = NULL;
255 
256 		return (error);
257 	}
258 
259 	error = ng_name_node(ng_btsocket_l2cap_node,
260 				NG_BTSOCKET_L2CAP_NODE_TYPE);
261 	if (error != 0) {
262 		NG_BTSOCKET_L2CAP_ALERT(
263 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
264 
265 		NG_NODE_UNREF(ng_btsocket_l2cap_node);
266 		ng_btsocket_l2cap_node = NULL;
267 
268 		return (error);
269 	}
270 
271 	return (0);
272 } /* ng_btsocket_l2cap_node_shutdown */
273 
274 /*
275  * We allow any hook to be connected to the node.
276  */
277 
278 static int
279 ng_btsocket_l2cap_node_newhook(node_p node, hook_p hook, char const *name)
280 {
281 	return (0);
282 } /* ng_btsocket_l2cap_node_newhook */
283 
284 /*
285  * Just say "YEP, that's OK by me!"
286  */
287 
288 static int
289 ng_btsocket_l2cap_node_connect(hook_p hook)
290 {
291 	NG_HOOK_SET_PRIVATE(hook, NULL);
292 	NG_HOOK_REF(hook); /* Keep extra reference to the hook */
293 
294 #if 0
295 	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
296 	NG_HOOK_FORCE_QUEUE(hook);
297 #endif
298 
299 	return (0);
300 } /* ng_btsocket_l2cap_node_connect */
301 
302 /*
303  * Hook disconnection. Schedule route cleanup task
304  */
305 
306 static int
307 ng_btsocket_l2cap_node_disconnect(hook_p hook)
308 {
309 	/*
310 	 * If hook has private information than we must have this hook in
311 	 * the routing table and must schedule cleaning for the routing table.
312 	 * Otherwise hook was connected but we never got "hook_info" message,
313 	 * so we have never added this hook to the routing table and it save
314 	 * to just delete it.
315 	 */
316 
317 	if (NG_HOOK_PRIVATE(hook) != NULL)
318 		return (ng_btsocket_l2cap_wakeup_route_task());
319 
320 	NG_HOOK_UNREF(hook); /* Remove extra reference */
321 
322 	return (0);
323 } /* ng_btsocket_l2cap_node_disconnect */
324 
325 /*
326  * Process incoming messages
327  */
328 
329 static int
330 ng_btsocket_l2cap_node_rcvmsg(node_p node, item_p item, hook_p hook)
331 {
332 	struct ng_mesg	*msg = NGI_MSG(item); /* item still has message */
333 	int		 error = 0;
334 
335 	if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) {
336 		mtx_lock(&ng_btsocket_l2cap_queue_mtx);
337 		if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
338 			NG_BTSOCKET_L2CAP_ERR(
339 "%s: Input queue is full (msg)\n", __func__);
340 
341 			NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue);
342 			NG_FREE_ITEM(item);
343 			error = ENOBUFS;
344 		} else {
345 			if (hook != NULL) {
346 				NG_HOOK_REF(hook);
347 				NGI_SET_HOOK(item, hook);
348 			}
349 
350 			NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
351 			error = ng_btsocket_l2cap_wakeup_input_task();
352 		}
353 		mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
354 	} else {
355 		NG_FREE_ITEM(item);
356 		error = EINVAL;
357 	}
358 
359 	return (error);
360 } /* ng_btsocket_l2cap_node_rcvmsg */
361 
362 /*
363  * Receive data on a hook
364  */
365 
366 static int
367 ng_btsocket_l2cap_node_rcvdata(hook_p hook, item_p item)
368 {
369 	int	error = 0;
370 
371 	mtx_lock(&ng_btsocket_l2cap_queue_mtx);
372 	if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
373 		NG_BTSOCKET_L2CAP_ERR(
374 "%s: Input queue is full (data)\n", __func__);
375 
376 		NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue);
377 		NG_FREE_ITEM(item);
378 		error = ENOBUFS;
379 	} else {
380 		NG_HOOK_REF(hook);
381 		NGI_SET_HOOK(item, hook);
382 
383 		NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
384 		error = ng_btsocket_l2cap_wakeup_input_task();
385 	}
386 	mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
387 
388 	return (error);
389 } /* ng_btsocket_l2cap_node_rcvdata */
390 
391 /*
392  * Process L2CA_Connect respose. Socket layer must have initiated connection,
393  * so we have to have a socket associated with message token.
394  */
395 
396 static int
397 ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg *msg,
398 		ng_btsocket_l2cap_rtentry_p rt)
399 {
400 	ng_l2cap_l2ca_con_op	*op = NULL;
401 	ng_btsocket_l2cap_pcb_t	*pcb = NULL;
402 	int			 error = 0;
403 
404 	if (msg->header.arglen != sizeof(*op))
405 		return (EMSGSIZE);
406 
407 	op = (ng_l2cap_l2ca_con_op *)(msg->data);
408 
409 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
410 
411 	/* Look for the socket with the token */
412 	pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
413 	if (pcb == NULL) {
414 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
415 		return (ENOENT);
416 	}
417 
418 	mtx_lock(&pcb->pcb_mtx);
419 
420 	NG_BTSOCKET_L2CAP_INFO(
421 "%s: Got L2CA_Connect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
422 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, status=%d, " \
423 "state=%d\n",	__func__, msg->header.token,
424 		pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
425 		pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
426 		pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
427 		pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
428 		pcb->psm, op->lcid, op->result, op->status,
429 		pcb->state);
430 
431 	if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
432 		mtx_unlock(&pcb->pcb_mtx);
433 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
434 
435 		return (ENOENT);
436 	}
437 
438 	ng_btsocket_l2cap_untimeout(pcb);
439 
440 	if (op->result == NG_L2CAP_PENDING) {
441 		ng_btsocket_l2cap_timeout(pcb);
442 		mtx_unlock(&pcb->pcb_mtx);
443 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
444 
445 		return (0);
446 	}
447 
448 	if (op->result == NG_L2CAP_SUCCESS) {
449 		/*
450 		 * Channel is now open, so update local channel ID and
451 		 * start configuration process. Source and destination
452 		 * addresses as well as route must be already set.
453 		 */
454 
455 		pcb->cid = op->lcid;
456 
457 		error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb);
458 		if (error != 0) {
459 			/* Send disconnect request with "zero" token */
460 			ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
461 
462 			/* ... and close the socket */
463 			pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
464 			soisdisconnected(pcb->so);
465 		} else {
466 			pcb->cfg_state = NG_BTSOCKET_L2CAP_CFG_IN_SENT;
467 			pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING;
468 
469 			ng_btsocket_l2cap_timeout(pcb);
470 		}
471 	} else {
472 		/*
473 		 * We have failed to open connection, so convert result
474 		 * code to "errno" code and disconnect the socket. Channel
475 		 * already has been closed.
476 		 */
477 
478 		pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
479 		pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
480 		soisdisconnected(pcb->so);
481 	}
482 
483 	mtx_unlock(&pcb->pcb_mtx);
484 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
485 
486 	return (error);
487 } /* ng_btsocket_l2cap_process_l2ca_con_req_rsp */
488 
489 /*
490  * Process L2CA_ConnectRsp response
491  */
492 
493 static int
494 ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(struct ng_mesg *msg,
495 		ng_btsocket_l2cap_rtentry_p rt)
496 {
497 	ng_l2cap_l2ca_con_rsp_op	*op = NULL;
498 	ng_btsocket_l2cap_pcb_t		*pcb = NULL;
499 
500 	if (msg->header.arglen != sizeof(*op))
501 		return (EMSGSIZE);
502 
503 	op = (ng_l2cap_l2ca_con_rsp_op *)(msg->data);
504 
505 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
506 
507 	/* Look for the socket with the token */
508 	pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
509 	if (pcb == NULL) {
510 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
511 		return (ENOENT);
512 	}
513 
514 	mtx_lock(&pcb->pcb_mtx);
515 
516 	NG_BTSOCKET_L2CAP_INFO(
517 "%s: Got L2CA_ConnectRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
518 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d\n",
519 		__func__, msg->header.token,
520 		pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
521 		pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
522 		pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
523 		pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
524 		pcb->psm, pcb->cid, op->result, pcb->state);
525 
526 	if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
527 		mtx_unlock(&pcb->pcb_mtx);
528 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
529 
530 		return (ENOENT);
531 	}
532 
533 	ng_btsocket_l2cap_untimeout(pcb);
534 
535 	/* Check the result and disconnect the socket on failure */
536 	if (op->result != NG_L2CAP_SUCCESS) {
537 		/* Close the socket - channel already closed */
538 		pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
539 		pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
540 		soisdisconnected(pcb->so);
541 	} else {
542 		/* Move to CONFIGURING state and wait for CONFIG_IND */
543 		pcb->cfg_state = 0;
544 		pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING;
545 		ng_btsocket_l2cap_timeout(pcb);
546 	}
547 
548 	mtx_unlock(&pcb->pcb_mtx);
549 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
550 
551 	return (0);
552 } /* ng_btsocket_process_l2ca_con_rsp_rsp */
553 
554 /*
555  * Process L2CA_Connect indicator. Find socket that listens on address
556  * and PSM. Find exact or closest match. Create new socket and initiate
557  * connection.
558  */
559 
560 static int
561 ng_btsocket_l2cap_process_l2ca_con_ind(struct ng_mesg *msg,
562 		ng_btsocket_l2cap_rtentry_p rt)
563 {
564 	ng_l2cap_l2ca_con_ind_ip	*ip = NULL;
565 	ng_btsocket_l2cap_pcb_t		*pcb = NULL, *pcb1 = NULL;
566 	int				 error = 0;
567 	u_int32_t			 token = 0;
568 	u_int16_t			 result = 0;
569 
570 	if (msg->header.arglen != sizeof(*ip))
571 		return (EMSGSIZE);
572 
573 	ip = (ng_l2cap_l2ca_con_ind_ip *)(msg->data);
574 
575 	NG_BTSOCKET_L2CAP_INFO(
576 "%s: Got L2CA_Connect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
577 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, ident=%d\n",
578 		__func__,
579 		rt->src.b[5], rt->src.b[4], rt->src.b[3],
580 		rt->src.b[2], rt->src.b[1], rt->src.b[0],
581 		ip->bdaddr.b[5], ip->bdaddr.b[4], ip->bdaddr.b[3],
582 		ip->bdaddr.b[2], ip->bdaddr.b[1], ip->bdaddr.b[0],
583 		ip->psm, ip->lcid, ip->ident);
584 
585 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
586 
587 	pcb = ng_btsocket_l2cap_pcb_by_addr(&rt->src, ip->psm);
588 	if (pcb != NULL) {
589 		struct socket	*so1 = NULL;
590 
591 		mtx_lock(&pcb->pcb_mtx);
592 
593 		/*
594 		 * First check the pending connections queue and if we have
595 		 * space then create new socket and set proper source address.
596 		 */
597 
598 		if (pcb->so->so_qlen <= pcb->so->so_qlimit) {
599 			CURVNET_SET(pcb->so->so_vnet);
600 			so1 = sonewconn(pcb->so, 0);
601 			CURVNET_RESTORE();
602 		}
603 
604 		if (so1 == NULL) {
605 			result = NG_L2CAP_NO_RESOURCES;
606 			goto respond;
607 		}
608 
609 		/*
610 		 * If we got here than we have created new socket. So complete
611 		 * connection. If we we listening on specific address then copy
612 		 * source address from listening socket, otherwise copy source
613 		 * address from hook's routing information.
614 		 */
615 
616 		pcb1 = so2l2cap_pcb(so1);
617 		KASSERT((pcb1 != NULL),
618 ("%s: pcb1 == NULL\n", __func__));
619 
620  		mtx_lock(&pcb1->pcb_mtx);
621 
622 		if (bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src)) != 0)
623 			bcopy(&pcb->src, &pcb1->src, sizeof(pcb1->src));
624 		else
625 			bcopy(&rt->src, &pcb1->src, sizeof(pcb1->src));
626 
627 		pcb1->flags &= ~NG_BTSOCKET_L2CAP_CLIENT;
628 
629 		bcopy(&ip->bdaddr, &pcb1->dst, sizeof(pcb1->dst));
630 		pcb1->psm = ip->psm;
631 		pcb1->cid = ip->lcid;
632 		pcb1->rt = rt;
633 
634 		/* Copy socket settings */
635 		pcb1->imtu = pcb->imtu;
636 		bcopy(&pcb->oflow, &pcb1->oflow, sizeof(pcb1->oflow));
637 		pcb1->flush_timo = pcb->flush_timo;
638 
639 		token = pcb1->token;
640 	} else
641 		/* Nobody listens on requested BDADDR/PSM */
642 		result = NG_L2CAP_PSM_NOT_SUPPORTED;
643 
644 respond:
645 	error = ng_btsocket_l2cap_send_l2ca_con_rsp_req(token, rt,
646 			&ip->bdaddr, ip->ident, ip->lcid, result);
647 	if (pcb1 != NULL) {
648 		if (error != 0) {
649 			pcb1->so->so_error = error;
650 			pcb1->state = NG_BTSOCKET_L2CAP_CLOSED;
651 			soisdisconnected(pcb1->so);
652 		} else {
653 			pcb1->state = NG_BTSOCKET_L2CAP_CONNECTING;
654 			soisconnecting(pcb1->so);
655 
656 			ng_btsocket_l2cap_timeout(pcb1);
657 		}
658 
659 		mtx_unlock(&pcb1->pcb_mtx);
660 	}
661 
662 	if (pcb != NULL)
663 		mtx_unlock(&pcb->pcb_mtx);
664 
665 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
666 
667 	return (error);
668 } /* ng_btsocket_l2cap_process_l2ca_con_ind */
669 
670 /*
671  * Process L2CA_Config response
672  */
673 
674 static int
675 ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg *msg,
676 		ng_btsocket_l2cap_rtentry_p rt)
677 {
678 	ng_l2cap_l2ca_cfg_op	*op = NULL;
679 	ng_btsocket_l2cap_pcb_p	 pcb = NULL;
680 
681 	if (msg->header.arglen != sizeof(*op))
682 		return (EMSGSIZE);
683 
684 	op = (ng_l2cap_l2ca_cfg_op *)(msg->data);
685 
686 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
687 
688 	/*
689 	 * Socket must have issued a Configure request, so we must have a
690 	 * socket that wants to be configured. Use Netgraph message token
691 	 * to find it
692 	 */
693 
694 	pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
695 	if (pcb == NULL) {
696 		/*
697 		 * XXX FIXME what to do here? We could not find a
698 		 * socket with requested token. We even can not send
699 		 * Disconnect, because we do not know channel ID
700 		 */
701 
702 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
703 		return (ENOENT);
704 	}
705 
706 	mtx_lock(&pcb->pcb_mtx);
707 
708         NG_BTSOCKET_L2CAP_INFO(
709 "%s: Got L2CA_Config response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
710 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \
711 "cfg_state=%x\n",
712 		__func__, msg->header.token,
713 		pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
714 		pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
715 		pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
716 		pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
717 		pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
718 
719 	if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
720 		mtx_unlock(&pcb->pcb_mtx);
721 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
722 
723 		return (ENOENT);
724 	}
725 
726 	if (op->result == NG_L2CAP_SUCCESS) {
727 		/*
728 		 * XXX FIXME Actually set flush and link timeout.
729 		 * Set QoS here if required. Resolve conficts (flush_timo).
730 		 * Save incoming MTU (peer's outgoing MTU) and outgoing flow
731 		 * spec.
732 		 */
733 
734 		pcb->imtu = op->imtu;
735 		bcopy(&op->oflow, &pcb->oflow, sizeof(pcb->oflow));
736 		pcb->flush_timo = op->flush_timo;
737 
738 		/*
739 		 * We have configured incoming side, so record it and check
740 		 * if configuration is complete. If complete then mark socket
741 		 * as connected, otherwise wait for the peer.
742 		 */
743 
744 		pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_IN_SENT;
745 		pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN;
746 
747 		if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) {
748 			/* Configuration complete - mark socket as open */
749 			ng_btsocket_l2cap_untimeout(pcb);
750 			pcb->state = NG_BTSOCKET_L2CAP_OPEN;
751 			soisconnected(pcb->so);
752 		}
753 	} else {
754 		/*
755 		 * Something went wrong. Could be unacceptable parameters,
756 		 * reject or unknown option. That's too bad, but we will
757 		 * not negotiate. Send Disconnect and close the channel.
758 		 */
759 
760 		ng_btsocket_l2cap_untimeout(pcb);
761 
762 		switch (op->result) {
763 		case NG_L2CAP_UNACCEPTABLE_PARAMS:
764 		case NG_L2CAP_UNKNOWN_OPTION:
765 			pcb->so->so_error = EINVAL;
766 			break;
767 
768 		default:
769 			pcb->so->so_error = ECONNRESET;
770 			break;
771 		}
772 
773 		/* Send disconnect with "zero" token */
774 		ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
775 
776 		/* ... and close the socket */
777 		pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
778 		soisdisconnected(pcb->so);
779 	}
780 
781 	mtx_unlock(&pcb->pcb_mtx);
782 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
783 
784 	return (0);
785 } /* ng_btsocket_l2cap_process_l2ca_cfg_req_rsp */
786 
787 /*
788  * Process L2CA_ConfigRsp response
789  */
790 
791 static int
792 ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(struct ng_mesg *msg,
793 		ng_btsocket_l2cap_rtentry_p rt)
794 {
795 	ng_l2cap_l2ca_cfg_rsp_op	*op = NULL;
796 	ng_btsocket_l2cap_pcb_t		*pcb = NULL;
797 	int				 error = 0;
798 
799 	if (msg->header.arglen != sizeof(*op))
800 		return (EMSGSIZE);
801 
802 	op = (ng_l2cap_l2ca_cfg_rsp_op *)(msg->data);
803 
804 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
805 
806 	/* Look for the socket with the token */
807 	pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
808 	if (pcb == NULL) {
809 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
810 		return (ENOENT);
811 	}
812 
813 	mtx_lock(&pcb->pcb_mtx);
814 
815         NG_BTSOCKET_L2CAP_INFO(
816 "%s: Got L2CA_ConfigRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
817 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \
818 "cfg_state=%x\n",
819 		__func__, msg->header.token,
820 		pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
821 		pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
822 		pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
823 		pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
824 		pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
825 
826 	if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
827 		mtx_unlock(&pcb->pcb_mtx);
828 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
829 
830 		return (ENOENT);
831 	}
832 
833 	/* Check the result and disconnect socket of failure */
834 	if (op->result != NG_L2CAP_SUCCESS)
835 		goto disconnect;
836 
837 	/*
838 	 * Now we done with remote side configuration. Configure local
839 	 * side if we have not done it yet.
840 	 */
841 
842 	pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_OUT_SENT;
843 	pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT;
844 
845 	if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) {
846 		/* Configuration complete - mask socket as open */
847 		ng_btsocket_l2cap_untimeout(pcb);
848 		pcb->state = NG_BTSOCKET_L2CAP_OPEN;
849 		soisconnected(pcb->so);
850 	} else {
851 		if (!(pcb->cfg_state & NG_BTSOCKET_L2CAP_CFG_IN_SENT)) {
852 			/* Send L2CA_Config request - incoming path */
853 			error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb);
854 			if (error != 0)
855 				goto disconnect;
856 
857 			pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN_SENT;
858 		}
859 	}
860 
861 	mtx_unlock(&pcb->pcb_mtx);
862 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
863 
864 	return (error);
865 
866 disconnect:
867 	ng_btsocket_l2cap_untimeout(pcb);
868 
869 	/* Send disconnect with "zero" token */
870 	ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
871 
872 	/* ... and close the socket */
873 	pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
874 	soisdisconnected(pcb->so);
875 
876 	mtx_unlock(&pcb->pcb_mtx);
877 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
878 
879 	return (error);
880 } /* ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp */
881 
882 /*
883  * Process L2CA_Config indicator
884  */
885 
886 static int
887 ng_btsocket_l2cap_process_l2ca_cfg_ind(struct ng_mesg *msg,
888 		ng_btsocket_l2cap_rtentry_p rt)
889 {
890 	ng_l2cap_l2ca_cfg_ind_ip	*ip = NULL;
891 	ng_btsocket_l2cap_pcb_t		*pcb = NULL;
892 	int				 error = 0;
893 
894 	if (msg->header.arglen != sizeof(*ip))
895 		return (EMSGSIZE);
896 
897 	ip = (ng_l2cap_l2ca_cfg_ind_ip *)(msg->data);
898 
899 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
900 
901 	/* Check for the open socket that has given channel ID */
902 	pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid);
903 	if (pcb == NULL) {
904 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
905 		return (ENOENT);
906 	}
907 
908 	mtx_lock(&pcb->pcb_mtx);
909 
910         NG_BTSOCKET_L2CAP_INFO(
911 "%s: Got L2CA_Config indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
912 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, state=%d, cfg_state=%x\n",
913 		__func__,
914 		pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
915 		pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
916 		pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
917 		pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
918 		pcb->psm, pcb->cid, pcb->state, pcb->cfg_state);
919 
920 	/* XXX FIXME re-configuration on open socket */
921  	if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
922 		mtx_unlock(&pcb->pcb_mtx);
923 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
924 
925 		return (ENOENT);
926 	}
927 
928 	/*
929 	 * XXX FIXME Actually set flush and link timeout. Set QoS here if
930 	 * required. Resolve conficts (flush_timo). Note outgoing MTU (peer's
931 	 * incoming MTU) and incoming flow spec.
932 	 */
933 
934 	pcb->omtu = ip->omtu;
935 	bcopy(&ip->iflow, &pcb->iflow, sizeof(pcb->iflow));
936 	pcb->flush_timo = ip->flush_timo;
937 
938 	/*
939 	 * Send L2CA_Config response to our peer and check for the errors,
940 	 * if any send disconnect to close the channel.
941 	 */
942 
943 	if (!(pcb->cfg_state & NG_BTSOCKET_L2CAP_CFG_OUT_SENT)) {
944 		error = ng_btsocket_l2cap_send_l2ca_cfg_rsp(pcb);
945 		if (error != 0) {
946 			ng_btsocket_l2cap_untimeout(pcb);
947 
948 			pcb->so->so_error = error;
949 
950 			/* Send disconnect with "zero" token */
951 			ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
952 
953 			/* ... and close the socket */
954 			pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
955 			soisdisconnected(pcb->so);
956 		} else
957 			pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT_SENT;
958 	}
959 
960 	mtx_unlock(&pcb->pcb_mtx);
961 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
962 
963 	return (error);
964 } /* ng_btsocket_l2cap_process_l2cap_cfg_ind */
965 
966 /*
967  * Process L2CA_Disconnect response
968  */
969 
970 static int
971 ng_btsocket_l2cap_process_l2ca_discon_rsp(struct ng_mesg *msg,
972 		ng_btsocket_l2cap_rtentry_p rt)
973 {
974 	ng_l2cap_l2ca_discon_op	*op = NULL;
975 	ng_btsocket_l2cap_pcb_t	*pcb = NULL;
976 
977 	/* Check message */
978 	if (msg->header.arglen != sizeof(*op))
979 		return (EMSGSIZE);
980 
981 	op = (ng_l2cap_l2ca_discon_op *)(msg->data);
982 
983 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
984 
985 	/*
986 	 * Socket layer must have issued L2CA_Disconnect request, so there
987 	 * must be a socket that wants to be disconnected. Use Netgraph
988 	 * message token to find it.
989 	 */
990 
991 	pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
992 	if (pcb == NULL) {
993 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
994 		return (0);
995 	}
996 
997 	mtx_lock(&pcb->pcb_mtx);
998 
999 	/* XXX Close socket no matter what op->result says */
1000 	if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
1001        		NG_BTSOCKET_L2CAP_INFO(
1002 "%s: Got L2CA_Disconnect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1003 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d\n",
1004 			__func__, msg->header.token,
1005 			pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
1006 			pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
1007 			pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
1008 			pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
1009 			pcb->psm, pcb->cid, op->result, pcb->state);
1010 
1011 		ng_btsocket_l2cap_untimeout(pcb);
1012 
1013 		pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1014 		soisdisconnected(pcb->so);
1015 	}
1016 
1017 	mtx_unlock(&pcb->pcb_mtx);
1018 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1019 
1020 	return (0);
1021 } /* ng_btsocket_l2cap_process_l2ca_discon_rsp */
1022 
1023 /*
1024  * Process L2CA_Disconnect indicator
1025  */
1026 
1027 static int
1028 ng_btsocket_l2cap_process_l2ca_discon_ind(struct ng_mesg *msg,
1029 		ng_btsocket_l2cap_rtentry_p rt)
1030 {
1031 	ng_l2cap_l2ca_discon_ind_ip	*ip = NULL;
1032 	ng_btsocket_l2cap_pcb_t		*pcb = NULL;
1033 
1034 	/* Check message */
1035 	if (msg->header.arglen != sizeof(*ip))
1036 		return (EMSGSIZE);
1037 
1038 	ip = (ng_l2cap_l2ca_discon_ind_ip *)(msg->data);
1039 
1040 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1041 
1042 	/* Look for the socket with given channel ID */
1043 	pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid);
1044 	if (pcb == NULL) {
1045 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1046 		return (0);
1047 	}
1048 
1049 	/*
1050 	 * Channel has already been destroyed, so disconnect the socket
1051 	 * and be done with it. If there was any pending request we can
1052 	 * not do anything here anyway.
1053 	 */
1054 
1055 	mtx_lock(&pcb->pcb_mtx);
1056 
1057        	NG_BTSOCKET_L2CAP_INFO(
1058 "%s: Got L2CA_Disconnect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1059 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, state=%d\n",
1060 		__func__,
1061 		pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
1062 		pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
1063 		pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
1064 		pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
1065 		pcb->psm, pcb->cid, pcb->state);
1066 
1067 	if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
1068 		ng_btsocket_l2cap_untimeout(pcb);
1069 
1070 	pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1071 	soisdisconnected(pcb->so);
1072 
1073 	mtx_unlock(&pcb->pcb_mtx);
1074 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1075 
1076 	return (0);
1077 } /* ng_btsocket_l2cap_process_l2ca_discon_ind */
1078 
1079 /*
1080  * Process L2CA_Write response
1081  */
1082 
1083 static int
1084 ng_btsocket_l2cap_process_l2ca_write_rsp(struct ng_mesg *msg,
1085 		ng_btsocket_l2cap_rtentry_p rt)
1086 {
1087 	ng_l2cap_l2ca_write_op	*op = NULL;
1088 	ng_btsocket_l2cap_pcb_t	*pcb = NULL;
1089 
1090 	/* Check message */
1091 	if (msg->header.arglen != sizeof(*op))
1092 		return (EMSGSIZE);
1093 
1094 	op = (ng_l2cap_l2ca_write_op *)(msg->data);
1095 
1096 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1097 
1098 	/* Look for the socket with given token */
1099 	pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
1100 	if (pcb == NULL) {
1101 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1102 		return (ENOENT);
1103 	}
1104 
1105 	mtx_lock(&pcb->pcb_mtx);
1106 
1107        	NG_BTSOCKET_L2CAP_INFO(
1108 "%s: Got L2CA_Write response, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1109 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, length=%d, " \
1110 "state=%d\n",		__func__,
1111 			pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
1112 			pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
1113 			pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
1114 			pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
1115 			pcb->psm, pcb->cid, op->result, op->length,
1116 			pcb->state);
1117 
1118 	if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
1119 		mtx_unlock(&pcb->pcb_mtx);
1120 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1121 
1122 		return (ENOENT);
1123 	}
1124 
1125 	ng_btsocket_l2cap_untimeout(pcb);
1126 
1127 	/*
1128  	 * Check if we have more data to send
1129  	 */
1130 	sbdroprecord(&pcb->so->so_snd);
1131 	if (sbavail(&pcb->so->so_snd) > 0) {
1132 		if (ng_btsocket_l2cap_send2(pcb) == 0)
1133 			ng_btsocket_l2cap_timeout(pcb);
1134 		else
1135 			sbdroprecord(&pcb->so->so_snd); /* XXX */
1136 	}
1137 
1138 	/*
1139 	 * Now set the result, drop packet from the socket send queue and
1140 	 * ask for more (wakeup sender)
1141 	 */
1142 
1143 	pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
1144 	sowwakeup(pcb->so);
1145 
1146 	mtx_unlock(&pcb->pcb_mtx);
1147 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1148 
1149 	return (0);
1150 } /* ng_btsocket_l2cap_process_l2ca_write_rsp */
1151 
1152 /*
1153  * Send L2CA_Connect request
1154  */
1155 
1156 static int
1157 ng_btsocket_l2cap_send_l2ca_con_req(ng_btsocket_l2cap_pcb_p pcb)
1158 {
1159 	struct ng_mesg		*msg = NULL;
1160 	ng_l2cap_l2ca_con_ip	*ip = NULL;
1161 	int			 error = 0;
1162 
1163 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1164 
1165 	if (pcb->rt == NULL ||
1166 	    pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1167 		return (ENETDOWN);
1168 
1169 	NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CON,
1170 		sizeof(*ip), M_NOWAIT);
1171 	if (msg == NULL)
1172 		return (ENOMEM);
1173 
1174 	msg->header.token = pcb->token;
1175 
1176 	ip = (ng_l2cap_l2ca_con_ip *)(msg->data);
1177 	bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
1178 	ip->psm = pcb->psm;
1179 
1180 	NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0);
1181 
1182 	return (error);
1183 } /* ng_btsocket_l2cap_send_l2ca_con_req */
1184 
1185 /*
1186  * Send L2CA_Connect response
1187  */
1188 
1189 static int
1190 ng_btsocket_l2cap_send_l2ca_con_rsp_req(u_int32_t token,
1191 		ng_btsocket_l2cap_rtentry_p rt, bdaddr_p dst, int ident,
1192 		int lcid, int result)
1193 {
1194 	struct ng_mesg			*msg = NULL;
1195 	ng_l2cap_l2ca_con_rsp_ip	*ip = NULL;
1196 	int				 error = 0;
1197 
1198 	if (rt == NULL || rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
1199 		return (ENETDOWN);
1200 
1201 	NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CON_RSP,
1202 		sizeof(*ip), M_NOWAIT);
1203 	if (msg == NULL)
1204 		return (ENOMEM);
1205 
1206 	msg->header.token = token;
1207 
1208 	ip = (ng_l2cap_l2ca_con_rsp_ip *)(msg->data);
1209 	bcopy(dst, &ip->bdaddr, sizeof(ip->bdaddr));
1210 	ip->ident = ident;
1211 	ip->lcid = lcid;
1212 	ip->result = result;
1213 	ip->status = 0;
1214 
1215 	NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg, rt->hook, 0);
1216 
1217 	return (error);
1218 } /* ng_btsocket_l2cap_send_l2ca_con_rsp_req */
1219 
1220 /*
1221  * Send L2CA_Config request
1222  */
1223 
1224 static int
1225 ng_btsocket_l2cap_send_l2ca_cfg_req(ng_btsocket_l2cap_pcb_p pcb)
1226 {
1227 	struct ng_mesg		*msg = NULL;
1228 	ng_l2cap_l2ca_cfg_ip	*ip = NULL;
1229 	int			 error = 0;
1230 
1231 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1232 
1233 	if (pcb->rt == NULL ||
1234 	    pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1235 		return (ENETDOWN);
1236 
1237 	NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CFG,
1238 		sizeof(*ip), M_NOWAIT);
1239 	if (msg == NULL)
1240 		return (ENOMEM);
1241 
1242 	msg->header.token = pcb->token;
1243 
1244 	ip = (ng_l2cap_l2ca_cfg_ip *)(msg->data);
1245 	ip->lcid = pcb->cid;
1246 	ip->imtu = pcb->imtu;
1247 	bcopy(&pcb->oflow, &ip->oflow, sizeof(ip->oflow));
1248 	ip->flush_timo = pcb->flush_timo;
1249 	ip->link_timo = pcb->link_timo;
1250 
1251 	NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0);
1252 
1253 	return (error);
1254 } /* ng_btsocket_l2cap_send_l2ca_cfg_req */
1255 
1256 /*
1257  * Send L2CA_Config response
1258  */
1259 
1260 static int
1261 ng_btsocket_l2cap_send_l2ca_cfg_rsp(ng_btsocket_l2cap_pcb_p pcb)
1262 {
1263 	struct ng_mesg			*msg = NULL;
1264 	ng_l2cap_l2ca_cfg_rsp_ip	*ip = NULL;
1265 	int				 error = 0;
1266 
1267 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1268 
1269 	if (pcb->rt == NULL ||
1270 	    pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1271 		return (ENETDOWN);
1272 
1273 	NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CFG_RSP,
1274 		sizeof(*ip), M_NOWAIT);
1275 	if (msg == NULL)
1276 		return (ENOMEM);
1277 
1278 	msg->header.token = pcb->token;
1279 
1280 	ip = (ng_l2cap_l2ca_cfg_rsp_ip *)(msg->data);
1281 	ip->lcid = pcb->cid;
1282 	ip->omtu = pcb->omtu;
1283 	bcopy(&pcb->iflow, &ip->iflow, sizeof(ip->iflow));
1284 
1285 	NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg, pcb->rt->hook, 0);
1286 
1287 	return (error);
1288 } /* ng_btsocket_l2cap_send_l2ca_cfg_rsp */
1289 
1290 /*
1291  * Send L2CA_Disconnect request
1292  */
1293 
1294 static int
1295 ng_btsocket_l2cap_send_l2ca_discon_req(u_int32_t token,
1296 		ng_btsocket_l2cap_pcb_p pcb)
1297 {
1298 	struct ng_mesg		*msg = NULL;
1299 	ng_l2cap_l2ca_discon_ip	*ip = NULL;
1300 	int			 error = 0;
1301 
1302 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1303 
1304 	if (pcb->rt == NULL ||
1305 	    pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1306 		return (ENETDOWN);
1307 
1308 	NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_DISCON,
1309 		sizeof(*ip), M_NOWAIT);
1310 	if (msg == NULL)
1311 		return (ENOMEM);
1312 
1313 	msg->header.token = token;
1314 
1315 	ip = (ng_l2cap_l2ca_discon_ip *)(msg->data);
1316 	ip->lcid = pcb->cid;
1317 
1318 	NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0);
1319 
1320 	return (error);
1321 } /* ng_btsocket_l2cap_send_l2ca_discon_req */
1322 
1323 /*****************************************************************************
1324  *****************************************************************************
1325  **                              Socket interface
1326  *****************************************************************************
1327  *****************************************************************************/
1328 
1329 /*
1330  * L2CAP sockets data input routine
1331  */
1332 
1333 static void
1334 ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook)
1335 {
1336 	ng_l2cap_hdr_t			*hdr = NULL;
1337 	ng_l2cap_clt_hdr_t		*clt_hdr = NULL;
1338 	ng_btsocket_l2cap_pcb_t		*pcb = NULL;
1339 	ng_btsocket_l2cap_rtentry_t	*rt = NULL;
1340 
1341 	if (hook == NULL) {
1342 		NG_BTSOCKET_L2CAP_ALERT(
1343 "%s: Invalid source hook for L2CAP data packet\n", __func__);
1344 		goto drop;
1345 	}
1346 
1347 	rt = (ng_btsocket_l2cap_rtentry_t *) NG_HOOK_PRIVATE(hook);
1348 	if (rt == NULL) {
1349 		NG_BTSOCKET_L2CAP_ALERT(
1350 "%s: Could not find out source bdaddr for L2CAP data packet\n", __func__);
1351 		goto drop;
1352 	}
1353 
1354 	/* Make sure we can access header */
1355 	if (m->m_pkthdr.len < sizeof(*hdr)) {
1356 		NG_BTSOCKET_L2CAP_ERR(
1357 "%s: L2CAP data packet too small, len=%d\n", __func__, m->m_pkthdr.len);
1358 		goto drop;
1359 	}
1360 
1361 	if (m->m_len < sizeof(*hdr)) {
1362 		m = m_pullup(m, sizeof(*hdr));
1363 		if (m == NULL)
1364 			goto drop;
1365 	}
1366 
1367 	/* Strip L2CAP packet header and verify packet length */
1368 	hdr = mtod(m, ng_l2cap_hdr_t *);
1369 	m_adj(m, sizeof(*hdr));
1370 
1371 	if (hdr->length != m->m_pkthdr.len) {
1372 		NG_BTSOCKET_L2CAP_ERR(
1373 "%s: Bad L2CAP data packet length, len=%d, length=%d\n",
1374 			__func__, m->m_pkthdr.len, hdr->length);
1375 		goto drop;
1376 	}
1377 
1378 	/*
1379 	 * Now process packet. Two cases:
1380 	 *
1381 	 * 1) Normal packet (cid != 2) then find connected socket and append
1382 	 *    mbuf to the socket queue. Wakeup socket.
1383 	 *
1384 	 * 2) Broadcast packet (cid == 2) then find all sockets that connected
1385 	 *    to the given PSM and have SO_BROADCAST bit set and append mbuf
1386 	 *    to the socket queue. Wakeup socket.
1387 	 */
1388 
1389 	NG_BTSOCKET_L2CAP_INFO(
1390 "%s: Received L2CAP data packet: src bdaddr=%x:%x:%x:%x:%x:%x, " \
1391 "dcid=%d, length=%d\n",
1392 		__func__,
1393 		rt->src.b[5], rt->src.b[4], rt->src.b[3],
1394 		rt->src.b[2], rt->src.b[1], rt->src.b[0],
1395 		hdr->dcid, hdr->length);
1396 
1397 	if (hdr->dcid >= NG_L2CAP_FIRST_CID) {
1398 
1399 		mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1400 
1401 		/* Normal packet: find connected socket */
1402 		pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, hdr->dcid);
1403 		if (pcb == NULL) {
1404 			mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1405 			goto drop;
1406 		}
1407 
1408 		mtx_lock(&pcb->pcb_mtx);
1409 
1410 		if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
1411 			NG_BTSOCKET_L2CAP_ERR(
1412 "%s: No connected socket found, src bdaddr=%x:%x:%x:%x:%x:%x, dcid=%d, " \
1413 "state=%d\n",			__func__,
1414 				rt->src.b[5], rt->src.b[4], rt->src.b[3],
1415 				rt->src.b[2], rt->src.b[1], rt->src.b[0],
1416 				hdr->dcid, pcb->state);
1417 
1418 			mtx_unlock(&pcb->pcb_mtx);
1419 			mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1420 			goto drop;
1421 		}
1422 
1423 		/* Check packet size against socket's incoming MTU */
1424 		if (hdr->length > pcb->imtu) {
1425 			NG_BTSOCKET_L2CAP_ERR(
1426 "%s: L2CAP data packet too big, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1427 "dcid=%d, length=%d, imtu=%d\n",
1428 				__func__,
1429 				rt->src.b[5], rt->src.b[4], rt->src.b[3],
1430 				rt->src.b[2], rt->src.b[1], rt->src.b[0],
1431 				hdr->dcid, hdr->length, pcb->imtu);
1432 
1433 			mtx_unlock(&pcb->pcb_mtx);
1434 			mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1435 			goto drop;
1436 		}
1437 
1438 		/* Check if we have enough space in socket receive queue */
1439 		if (m->m_pkthdr.len > sbspace(&pcb->so->so_rcv)) {
1440 
1441 			/*
1442 			 * This is really bad. Receive queue on socket does
1443 			 * not have enough space for the packet. We do not
1444 			 * have any other choice but drop the packet. L2CAP
1445 			 * does not provide any flow control.
1446 			 */
1447 
1448 			NG_BTSOCKET_L2CAP_ERR(
1449 "%s: Not enough space in socket receive queue. Dropping L2CAP data packet, " \
1450 "src bdaddr=%x:%x:%x:%x:%x:%x, dcid=%d, len=%d, space=%ld\n",
1451 				__func__,
1452 				rt->src.b[5], rt->src.b[4], rt->src.b[3],
1453 				rt->src.b[2], rt->src.b[1], rt->src.b[0],
1454 				hdr->dcid, m->m_pkthdr.len,
1455 				sbspace(&pcb->so->so_rcv));
1456 
1457 			mtx_unlock(&pcb->pcb_mtx);
1458 			mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1459 			goto drop;
1460 		}
1461 
1462 		/* Append packet to the socket receive queue and wakeup */
1463 		sbappendrecord(&pcb->so->so_rcv, m);
1464 		m = NULL;
1465 
1466 		sorwakeup(pcb->so);
1467 
1468 		mtx_unlock(&pcb->pcb_mtx);
1469 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1470 	} else if (hdr->dcid == NG_L2CAP_CLT_CID) {
1471 		/* Broadcast packet: give packet to all sockets  */
1472 
1473 		/* Check packet size against connectionless MTU */
1474 		if (hdr->length > NG_L2CAP_MTU_DEFAULT) {
1475 			NG_BTSOCKET_L2CAP_ERR(
1476 "%s: Connectionless L2CAP data packet too big, " \
1477 "src bdaddr=%x:%x:%x:%x:%x:%x, length=%d\n",
1478 				__func__,
1479 				rt->src.b[5], rt->src.b[4], rt->src.b[3],
1480 				rt->src.b[2], rt->src.b[1], rt->src.b[0],
1481 				hdr->length);
1482 			goto drop;
1483 		}
1484 
1485 		/* Make sure we can access connectionless header */
1486 		if (m->m_pkthdr.len < sizeof(*clt_hdr)) {
1487 			NG_BTSOCKET_L2CAP_ERR(
1488 "%s: Can not get L2CAP connectionless packet header, " \
1489 "src bdaddr=%x:%x:%x:%x:%x:%x, length=%d\n",
1490 				__func__,
1491 				rt->src.b[5], rt->src.b[4], rt->src.b[3],
1492 				rt->src.b[2], rt->src.b[1], rt->src.b[0],
1493 				hdr->length);
1494 			goto drop;
1495 		}
1496 
1497 		if (m->m_len < sizeof(*clt_hdr)) {
1498 			m = m_pullup(m, sizeof(*clt_hdr));
1499 			if (m == NULL)
1500 				goto drop;
1501 		}
1502 
1503 		/* Strip connectionless header and deliver packet */
1504 		clt_hdr = mtod(m, ng_l2cap_clt_hdr_t *);
1505 		m_adj(m, sizeof(*clt_hdr));
1506 
1507 		NG_BTSOCKET_L2CAP_INFO(
1508 "%s: Got L2CAP connectionless data packet, " \
1509 "src bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, length=%d\n",
1510 			__func__,
1511 			rt->src.b[5], rt->src.b[4], rt->src.b[3],
1512 			rt->src.b[2], rt->src.b[1], rt->src.b[0],
1513 			clt_hdr->psm, hdr->length);
1514 
1515 		mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1516 
1517 		LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next) {
1518 			struct mbuf	*copy = NULL;
1519 
1520 			mtx_lock(&pcb->pcb_mtx);
1521 
1522 			if (bcmp(&rt->src, &pcb->src, sizeof(pcb->src)) != 0 ||
1523 			    pcb->psm != clt_hdr->psm ||
1524 			    pcb->state != NG_BTSOCKET_L2CAP_OPEN ||
1525 			    (pcb->so->so_options & SO_BROADCAST) == 0 ||
1526 			    m->m_pkthdr.len > sbspace(&pcb->so->so_rcv))
1527 				goto next;
1528 
1529 			/*
1530 			 * Create a copy of the packet and append it to the
1531 			 * socket's queue. If m_dup() failed - no big deal
1532 			 * it is a broadcast traffic after all
1533 			 */
1534 
1535 			copy = m_dup(m, M_NOWAIT);
1536 			if (copy != NULL) {
1537 				sbappendrecord(&pcb->so->so_rcv, copy);
1538 				sorwakeup(pcb->so);
1539 			}
1540 next:
1541 			mtx_unlock(&pcb->pcb_mtx);
1542 		}
1543 
1544 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1545 	}
1546 drop:
1547 	NG_FREE_M(m); /* checks for m != NULL */
1548 } /* ng_btsocket_l2cap_data_input */
1549 
1550 /*
1551  * L2CAP sockets default message input routine
1552  */
1553 
1554 static void
1555 ng_btsocket_l2cap_default_msg_input(struct ng_mesg *msg, hook_p hook)
1556 {
1557 	switch (msg->header.cmd) {
1558 	case NGM_L2CAP_NODE_HOOK_INFO: {
1559 		ng_btsocket_l2cap_rtentry_t	*rt = NULL;
1560 
1561 		if (hook == NULL || msg->header.arglen != sizeof(bdaddr_t))
1562 			break;
1563 
1564 		if (bcmp(msg->data, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
1565 			break;
1566 
1567 		mtx_lock(&ng_btsocket_l2cap_rt_mtx);
1568 
1569 		rt = (ng_btsocket_l2cap_rtentry_t *) NG_HOOK_PRIVATE(hook);
1570 		if (rt == NULL) {
1571 			rt = malloc(sizeof(*rt),
1572 				M_NETGRAPH_BTSOCKET_L2CAP, M_NOWAIT|M_ZERO);
1573 			if (rt == NULL) {
1574 				mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
1575 				break;
1576 			}
1577 
1578 			LIST_INSERT_HEAD(&ng_btsocket_l2cap_rt, rt, next);
1579 
1580 			NG_HOOK_SET_PRIVATE(hook, rt);
1581 		}
1582 
1583 		bcopy(msg->data, &rt->src, sizeof(rt->src));
1584 		rt->hook = hook;
1585 
1586 		mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
1587 
1588 		NG_BTSOCKET_L2CAP_INFO(
1589 "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n",
1590 			__func__, NG_HOOK_NAME(hook),
1591 			rt->src.b[5], rt->src.b[4], rt->src.b[3],
1592 			rt->src.b[2], rt->src.b[1], rt->src.b[0]);
1593 		} break;
1594 
1595 	default:
1596 		NG_BTSOCKET_L2CAP_WARN(
1597 "%s: Unknown message, cmd=%d\n", __func__, msg->header.cmd);
1598 		break;
1599 	}
1600 
1601 	NG_FREE_MSG(msg); /* Checks for msg != NULL */
1602 } /* ng_btsocket_l2cap_default_msg_input */
1603 
1604 /*
1605  * L2CAP sockets L2CA message input routine
1606  */
1607 
1608 static void
1609 ng_btsocket_l2cap_l2ca_msg_input(struct ng_mesg *msg, hook_p hook)
1610 {
1611 	ng_btsocket_l2cap_rtentry_p	rt = NULL;
1612 
1613 	if (hook == NULL) {
1614 		NG_BTSOCKET_L2CAP_ALERT(
1615 "%s: Invalid source hook for L2CA message\n", __func__);
1616 		goto drop;
1617 	}
1618 
1619 	rt = (ng_btsocket_l2cap_rtentry_p) NG_HOOK_PRIVATE(hook);
1620 	if (rt == NULL) {
1621 		NG_BTSOCKET_L2CAP_ALERT(
1622 "%s: Could not find out source bdaddr for L2CA message\n", __func__);
1623 		goto drop;
1624 	}
1625 
1626 	switch (msg->header.cmd) {
1627 	case NGM_L2CAP_L2CA_CON: /* L2CA_Connect response */
1628 		ng_btsocket_l2cap_process_l2ca_con_req_rsp(msg, rt);
1629 		break;
1630 
1631 	case NGM_L2CAP_L2CA_CON_RSP: /* L2CA_ConnectRsp response */
1632 		ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(msg, rt);
1633 		break;
1634 
1635 	case NGM_L2CAP_L2CA_CON_IND: /* L2CA_Connect indicator */
1636 		ng_btsocket_l2cap_process_l2ca_con_ind(msg, rt);
1637 		break;
1638 
1639 	case NGM_L2CAP_L2CA_CFG: /* L2CA_Config response */
1640 		ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(msg, rt);
1641 		break;
1642 
1643 	case NGM_L2CAP_L2CA_CFG_RSP: /* L2CA_ConfigRsp response */
1644 		ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(msg, rt);
1645 		break;
1646 
1647 	case NGM_L2CAP_L2CA_CFG_IND: /* L2CA_Config indicator */
1648 		ng_btsocket_l2cap_process_l2ca_cfg_ind(msg, rt);
1649 		break;
1650 
1651 	case NGM_L2CAP_L2CA_DISCON: /* L2CA_Disconnect response */
1652 		ng_btsocket_l2cap_process_l2ca_discon_rsp(msg, rt);
1653 		break;
1654 
1655 	case NGM_L2CAP_L2CA_DISCON_IND: /* L2CA_Disconnect indicator */
1656 		ng_btsocket_l2cap_process_l2ca_discon_ind(msg, rt);
1657 		break;
1658 
1659 	case NGM_L2CAP_L2CA_WRITE: /* L2CA_Write response */
1660 		ng_btsocket_l2cap_process_l2ca_write_rsp(msg, rt);
1661 		break;
1662 
1663 	/* XXX FIXME add other L2CA messages */
1664 
1665 	default:
1666 		NG_BTSOCKET_L2CAP_WARN(
1667 "%s: Unknown L2CA message, cmd=%d\n", __func__, msg->header.cmd);
1668 		break;
1669 	}
1670 drop:
1671 	NG_FREE_MSG(msg);
1672 } /* ng_btsocket_l2cap_l2ca_msg_input */
1673 
1674 /*
1675  * L2CAP sockets input routine
1676  */
1677 
1678 static void
1679 ng_btsocket_l2cap_input(void *context, int pending)
1680 {
1681 	item_p	item = NULL;
1682 	hook_p	hook = NULL;
1683 
1684 	for (;;) {
1685 		mtx_lock(&ng_btsocket_l2cap_queue_mtx);
1686 		NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_queue, item);
1687 		mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
1688 
1689 		if (item == NULL)
1690 			break;
1691 
1692 		NGI_GET_HOOK(item, hook);
1693 		if (hook != NULL && NG_HOOK_NOT_VALID(hook))
1694 			goto drop;
1695 
1696 		switch(item->el_flags & NGQF_TYPE) {
1697 		case NGQF_DATA: {
1698 			struct mbuf     *m = NULL;
1699 
1700 			NGI_GET_M(item, m);
1701 			ng_btsocket_l2cap_data_input(m, hook);
1702 			} break;
1703 
1704 		case NGQF_MESG: {
1705 			struct ng_mesg  *msg = NULL;
1706 
1707 			NGI_GET_MSG(item, msg);
1708 
1709 			switch (msg->header.cmd) {
1710 			case NGM_L2CAP_L2CA_CON:
1711 			case NGM_L2CAP_L2CA_CON_RSP:
1712 			case NGM_L2CAP_L2CA_CON_IND:
1713 			case NGM_L2CAP_L2CA_CFG:
1714 			case NGM_L2CAP_L2CA_CFG_RSP:
1715 			case NGM_L2CAP_L2CA_CFG_IND:
1716 			case NGM_L2CAP_L2CA_DISCON:
1717 			case NGM_L2CAP_L2CA_DISCON_IND:
1718 			case NGM_L2CAP_L2CA_WRITE:
1719 			/* XXX FIXME add other L2CA messages */
1720 				ng_btsocket_l2cap_l2ca_msg_input(msg, hook);
1721 				break;
1722 
1723 			default:
1724 				ng_btsocket_l2cap_default_msg_input(msg, hook);
1725 				break;
1726 			}
1727 			} break;
1728 
1729 		default:
1730 			KASSERT(0,
1731 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
1732 			break;
1733 		}
1734 drop:
1735 		if (hook != NULL)
1736 			NG_HOOK_UNREF(hook);
1737 
1738 		NG_FREE_ITEM(item);
1739 	}
1740 } /* ng_btsocket_l2cap_input */
1741 
1742 /*
1743  * Route cleanup task. Gets scheduled when hook is disconnected. Here we
1744  * will find all sockets that use "invalid" hook and disconnect them.
1745  */
1746 
1747 static void
1748 ng_btsocket_l2cap_rtclean(void *context, int pending)
1749 {
1750 	ng_btsocket_l2cap_pcb_p		pcb = NULL, pcb_next = NULL;
1751 	ng_btsocket_l2cap_rtentry_p	rt = NULL;
1752 
1753 	mtx_lock(&ng_btsocket_l2cap_rt_mtx);
1754 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1755 
1756 	/*
1757 	 * First disconnect all sockets that use "invalid" hook
1758 	 */
1759 
1760 	for (pcb = LIST_FIRST(&ng_btsocket_l2cap_sockets); pcb != NULL; ) {
1761 		mtx_lock(&pcb->pcb_mtx);
1762 		pcb_next = LIST_NEXT(pcb, next);
1763 
1764 		if (pcb->rt != NULL &&
1765 		    pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) {
1766 			if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
1767 				ng_btsocket_l2cap_untimeout(pcb);
1768 
1769 			pcb->so->so_error = ENETDOWN;
1770 			pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1771 			soisdisconnected(pcb->so);
1772 
1773 			pcb->token = 0;
1774 			pcb->cid = 0;
1775 			pcb->rt = NULL;
1776 		}
1777 
1778 		mtx_unlock(&pcb->pcb_mtx);
1779 		pcb = pcb_next;
1780 	}
1781 
1782 	/*
1783 	 * Now cleanup routing table
1784 	 */
1785 
1786 	for (rt = LIST_FIRST(&ng_btsocket_l2cap_rt); rt != NULL; ) {
1787 		ng_btsocket_l2cap_rtentry_p	rt_next = LIST_NEXT(rt, next);
1788 
1789 		if (rt->hook != NULL && NG_HOOK_NOT_VALID(rt->hook)) {
1790 			LIST_REMOVE(rt, next);
1791 
1792 			NG_HOOK_SET_PRIVATE(rt->hook, NULL);
1793 			NG_HOOK_UNREF(rt->hook); /* Remove extra reference */
1794 
1795 			bzero(rt, sizeof(*rt));
1796 			free(rt, M_NETGRAPH_BTSOCKET_L2CAP);
1797 		}
1798 
1799 		rt = rt_next;
1800 	}
1801 
1802 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1803 	mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
1804 } /* ng_btsocket_l2cap_rtclean */
1805 
1806 /*
1807  * Initialize everything
1808  */
1809 
1810 void
1811 ng_btsocket_l2cap_init(void)
1812 {
1813 	int	error = 0;
1814 
1815 	/* Skip initialization of globals for non-default instances. */
1816 	if (!IS_DEFAULT_VNET(curvnet))
1817 		return;
1818 
1819 	ng_btsocket_l2cap_node = NULL;
1820 	ng_btsocket_l2cap_debug_level = NG_BTSOCKET_WARN_LEVEL;
1821 
1822 	/* Register Netgraph node type */
1823 	error = ng_newtype(&typestruct);
1824 	if (error != 0) {
1825 		NG_BTSOCKET_L2CAP_ALERT(
1826 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
1827 
1828                 return;
1829 	}
1830 
1831 	/* Create Netgrapg node */
1832 	error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_node);
1833 	if (error != 0) {
1834 		NG_BTSOCKET_L2CAP_ALERT(
1835 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
1836 
1837 		ng_btsocket_l2cap_node = NULL;
1838 
1839 		return;
1840 	}
1841 
1842 	error = ng_name_node(ng_btsocket_l2cap_node,
1843 				NG_BTSOCKET_L2CAP_NODE_TYPE);
1844 	if (error != 0) {
1845 		NG_BTSOCKET_L2CAP_ALERT(
1846 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
1847 
1848 		NG_NODE_UNREF(ng_btsocket_l2cap_node);
1849 		ng_btsocket_l2cap_node = NULL;
1850 
1851 		return;
1852 	}
1853 
1854 	/* Create input queue */
1855 	NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_queue, ifqmaxlen);
1856 	mtx_init(&ng_btsocket_l2cap_queue_mtx,
1857 		"btsocks_l2cap_queue_mtx", NULL, MTX_DEF);
1858 	TASK_INIT(&ng_btsocket_l2cap_queue_task, 0,
1859 		ng_btsocket_l2cap_input, NULL);
1860 
1861 	/* Create list of sockets */
1862 	LIST_INIT(&ng_btsocket_l2cap_sockets);
1863 	mtx_init(&ng_btsocket_l2cap_sockets_mtx,
1864 		"btsocks_l2cap_sockets_mtx", NULL, MTX_DEF);
1865 
1866 	/* Routing table */
1867 	LIST_INIT(&ng_btsocket_l2cap_rt);
1868 	mtx_init(&ng_btsocket_l2cap_rt_mtx,
1869 		"btsocks_l2cap_rt_mtx", NULL, MTX_DEF);
1870 	TASK_INIT(&ng_btsocket_l2cap_rt_task, 0,
1871 		ng_btsocket_l2cap_rtclean, NULL);
1872 } /* ng_btsocket_l2cap_init */
1873 
1874 /*
1875  * Abort connection on socket
1876  */
1877 
1878 void
1879 ng_btsocket_l2cap_abort(struct socket *so)
1880 {
1881 	so->so_error = ECONNABORTED;
1882 
1883 	(void)ng_btsocket_l2cap_disconnect(so);
1884 } /* ng_btsocket_l2cap_abort */
1885 
1886 void
1887 ng_btsocket_l2cap_close(struct socket *so)
1888 {
1889 
1890 	(void)ng_btsocket_l2cap_disconnect(so);
1891 } /* ng_btsocket_l2cap_close */
1892 
1893 /*
1894  * Accept connection on socket. Nothing to do here, socket must be connected
1895  * and ready, so just return peer address and be done with it.
1896  */
1897 
1898 int
1899 ng_btsocket_l2cap_accept(struct socket *so, struct sockaddr **nam)
1900 {
1901 	if (ng_btsocket_l2cap_node == NULL)
1902 		return (EINVAL);
1903 
1904 	return (ng_btsocket_l2cap_peeraddr(so, nam));
1905 } /* ng_btsocket_l2cap_accept */
1906 
1907 /*
1908  * Create and attach new socket
1909  */
1910 
1911 int
1912 ng_btsocket_l2cap_attach(struct socket *so, int proto, struct thread *td)
1913 {
1914 	static u_int32_t	token = 0;
1915 	ng_btsocket_l2cap_pcb_p	pcb = so2l2cap_pcb(so);
1916 	int			error;
1917 
1918 	/* Check socket and protocol */
1919 	if (ng_btsocket_l2cap_node == NULL)
1920 		return (EPROTONOSUPPORT);
1921 	if (so->so_type != SOCK_SEQPACKET)
1922 		return (ESOCKTNOSUPPORT);
1923 
1924 #if 0 /* XXX sonewconn() calls "pru_attach" with proto == 0 */
1925 	if (proto != 0)
1926 		if (proto != BLUETOOTH_PROTO_L2CAP)
1927 			return (EPROTONOSUPPORT);
1928 #endif /* XXX */
1929 
1930 	if (pcb != NULL)
1931 		return (EISCONN);
1932 
1933 	/* Reserve send and receive space if it is not reserved yet */
1934 	if ((so->so_snd.sb_hiwat == 0) || (so->so_rcv.sb_hiwat == 0)) {
1935 		error = soreserve(so, NG_BTSOCKET_L2CAP_SENDSPACE,
1936 					NG_BTSOCKET_L2CAP_RECVSPACE);
1937 		if (error != 0)
1938 			return (error);
1939 	}
1940 
1941 	/* Allocate the PCB */
1942         pcb = malloc(sizeof(*pcb),
1943 		M_NETGRAPH_BTSOCKET_L2CAP, M_NOWAIT | M_ZERO);
1944         if (pcb == NULL)
1945                 return (ENOMEM);
1946 
1947 	/* Link the PCB and the socket */
1948 	so->so_pcb = (caddr_t) pcb;
1949 	pcb->so = so;
1950 	pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1951 
1952 	/* Initialize PCB */
1953 	pcb->imtu = pcb->omtu = NG_L2CAP_MTU_DEFAULT;
1954 
1955 	/* Default flow */
1956 	pcb->iflow.flags = 0x0;
1957 	pcb->iflow.service_type = NG_HCI_SERVICE_TYPE_BEST_EFFORT;
1958 	pcb->iflow.token_rate = 0xffffffff; /* maximum */
1959 	pcb->iflow.token_bucket_size = 0xffffffff; /* maximum */
1960 	pcb->iflow.peak_bandwidth = 0x00000000; /* maximum */
1961 	pcb->iflow.latency = 0xffffffff; /* don't care */
1962 	pcb->iflow.delay_variation = 0xffffffff; /* don't care */
1963 
1964 	bcopy(&pcb->iflow, &pcb->oflow, sizeof(pcb->oflow));
1965 
1966 	pcb->flush_timo = NG_L2CAP_FLUSH_TIMO_DEFAULT;
1967 	pcb->link_timo = NG_L2CAP_LINK_TIMO_DEFAULT;
1968 
1969 	/*
1970 	 * XXX Mark PCB mutex as DUPOK to prevent "duplicated lock of
1971 	 * the same type" message. When accepting new L2CAP connection
1972 	 * ng_btsocket_l2cap_process_l2ca_con_ind() holds both PCB mutexes
1973 	 * for "old" (accepting) PCB and "new" (created) PCB.
1974 	 */
1975 
1976 	mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_pcb_mtx", NULL,
1977 		MTX_DEF|MTX_DUPOK);
1978 	callout_init_mtx(&pcb->timo, &pcb->pcb_mtx, 0);
1979 
1980         /*
1981 	 * Add the PCB to the list
1982 	 *
1983 	 * XXX FIXME VERY IMPORTANT!
1984 	 *
1985 	 * This is totally FUBAR. We could get here in two cases:
1986 	 *
1987 	 * 1) When user calls socket()
1988 	 * 2) When we need to accept new incomming connection and call
1989 	 *    sonewconn()
1990 	 *
1991 	 * In the first case we must acquire ng_btsocket_l2cap_sockets_mtx.
1992 	 * In the second case we hold ng_btsocket_l2cap_sockets_mtx already.
1993 	 * So we now need to distinguish between these cases. From reading
1994 	 * /sys/kern/uipc_socket.c we can find out that sonewconn() calls
1995 	 * pru_attach with proto == 0 and td == NULL. For now use this fact
1996 	 * to figure out if we were called from socket() or from sonewconn().
1997 	 */
1998 
1999 	if (td != NULL)
2000 		mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
2001 	else
2002 		mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
2003 
2004 	/* Set PCB token. Use ng_btsocket_l2cap_sockets_mtx for protection */
2005 	if (++ token == 0)
2006 		token ++;
2007 
2008 	pcb->token = token;
2009 
2010 	LIST_INSERT_HEAD(&ng_btsocket_l2cap_sockets, pcb, next);
2011 
2012 	if (td != NULL)
2013 		mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
2014 
2015         return (0);
2016 } /* ng_btsocket_l2cap_attach */
2017 
2018 /*
2019  * Bind socket
2020  */
2021 
2022 int
2023 ng_btsocket_l2cap_bind(struct socket *so, struct sockaddr *nam,
2024 		struct thread *td)
2025 {
2026 	ng_btsocket_l2cap_pcb_t	*pcb = NULL;
2027 	struct sockaddr_l2cap	*sa = (struct sockaddr_l2cap *) nam;
2028 	int			 psm, error = 0;
2029 
2030 	if (ng_btsocket_l2cap_node == NULL)
2031 		return (EINVAL);
2032 
2033 	/* Verify address */
2034 	if (sa == NULL)
2035 		return (EINVAL);
2036 	if (sa->l2cap_family != AF_BLUETOOTH)
2037 		return (EAFNOSUPPORT);
2038 	if (sa->l2cap_len != sizeof(*sa))
2039 		return (EINVAL);
2040 
2041 	psm = le16toh(sa->l2cap_psm);
2042 
2043 	/*
2044 	 * Check if other socket has this address already (look for exact
2045 	 * match PSM and bdaddr) and assign socket address if it's available.
2046 	 *
2047 	 * Note: socket can be bound to ANY PSM (zero) thus allowing several
2048 	 * channels with the same PSM between the same pair of BD_ADDR'es.
2049 	 */
2050 
2051 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
2052 
2053 	LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next)
2054 		if (psm != 0 && psm == pcb->psm &&
2055 		    bcmp(&pcb->src, &sa->l2cap_bdaddr, sizeof(bdaddr_t)) == 0)
2056 			break;
2057 
2058 	if (pcb == NULL) {
2059 		/* Set socket address */
2060 		pcb = so2l2cap_pcb(so);
2061 		if (pcb != NULL) {
2062 			bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src));
2063 			pcb->psm = psm;
2064 		} else
2065 			error = EINVAL;
2066 	} else
2067 		error = EADDRINUSE;
2068 
2069 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
2070 
2071 	return (error);
2072 } /* ng_btsocket_l2cap_bind */
2073 
2074 /*
2075  * Connect socket
2076  */
2077 
2078 int
2079 ng_btsocket_l2cap_connect(struct socket *so, struct sockaddr *nam,
2080 		struct thread *td)
2081 {
2082 	ng_btsocket_l2cap_pcb_t		*pcb = so2l2cap_pcb(so);
2083 	struct sockaddr_l2cap		*sa = (struct sockaddr_l2cap *) nam;
2084 	ng_btsocket_l2cap_rtentry_t	*rt = NULL;
2085 	int				 have_src, error = 0;
2086 
2087 	/* Check socket */
2088 	if (pcb == NULL)
2089 		return (EINVAL);
2090 	if (ng_btsocket_l2cap_node == NULL)
2091 		return (EINVAL);
2092 	if (pcb->state == NG_BTSOCKET_L2CAP_CONNECTING)
2093 		return (EINPROGRESS);
2094 
2095 	/* Verify address */
2096 	if (sa == NULL)
2097 		return (EINVAL);
2098 	if (sa->l2cap_family != AF_BLUETOOTH)
2099 		return (EAFNOSUPPORT);
2100 	if (sa->l2cap_len != sizeof(*sa))
2101 		return (EINVAL);
2102 	if (sa->l2cap_psm == 0 ||
2103 	    bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
2104 		return (EDESTADDRREQ);
2105 	if (pcb->psm != 0 && pcb->psm != le16toh(sa->l2cap_psm))
2106 		return (EINVAL);
2107 
2108 	/*
2109 	 * Routing. Socket should be bound to some source address. The source
2110 	 * address can be ANY. Destination address must be set and it must not
2111 	 * be ANY. If source address is ANY then find first rtentry that has
2112 	 * src != dst.
2113 	 */
2114 
2115 	mtx_lock(&ng_btsocket_l2cap_rt_mtx);
2116 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
2117 	mtx_lock(&pcb->pcb_mtx);
2118 
2119 	/* Send destination address and PSM */
2120 	bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst));
2121 	pcb->psm = le16toh(sa->l2cap_psm);
2122 
2123 	pcb->rt = NULL;
2124 	have_src = bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src));
2125 
2126 	LIST_FOREACH(rt, &ng_btsocket_l2cap_rt, next) {
2127 		if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
2128 			continue;
2129 
2130 		/* Match src and dst */
2131 		if (have_src) {
2132 			if (bcmp(&pcb->src, &rt->src, sizeof(rt->src)) == 0)
2133 				break;
2134 		} else {
2135 			if (bcmp(&pcb->dst, &rt->src, sizeof(rt->src)) != 0)
2136 				break;
2137 		}
2138 	}
2139 
2140 	if (rt != NULL) {
2141 		pcb->rt = rt;
2142 
2143 		if (!have_src)
2144 			bcopy(&rt->src, &pcb->src, sizeof(pcb->src));
2145 	} else
2146 		error = EHOSTUNREACH;
2147 
2148 	/*
2149 	 * Send L2CA_Connect request
2150 	 */
2151 
2152 	if (error == 0) {
2153 		error = ng_btsocket_l2cap_send_l2ca_con_req(pcb);
2154 		if (error == 0) {
2155 			pcb->flags |= NG_BTSOCKET_L2CAP_CLIENT;
2156 			pcb->state = NG_BTSOCKET_L2CAP_CONNECTING;
2157 			soisconnecting(pcb->so);
2158 
2159 			ng_btsocket_l2cap_timeout(pcb);
2160 		}
2161 	}
2162 
2163 	mtx_unlock(&pcb->pcb_mtx);
2164 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
2165 	mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
2166 
2167 	return (error);
2168 } /* ng_btsocket_l2cap_connect */
2169 
2170 /*
2171  * Process ioctl's calls on socket
2172  */
2173 
2174 int
2175 ng_btsocket_l2cap_control(struct socket *so, u_long cmd, caddr_t data,
2176 		struct ifnet *ifp, struct thread *td)
2177 {
2178 	return (EINVAL);
2179 } /* ng_btsocket_l2cap_control */
2180 
2181 /*
2182  * Process getsockopt/setsockopt system calls
2183  */
2184 
2185 int
2186 ng_btsocket_l2cap_ctloutput(struct socket *so, struct sockopt *sopt)
2187 {
2188 	ng_btsocket_l2cap_pcb_p	pcb = so2l2cap_pcb(so);
2189 	int			error = 0;
2190 	ng_l2cap_cfg_opt_val_t	v;
2191 
2192 	if (pcb == NULL)
2193 		return (EINVAL);
2194 	if (ng_btsocket_l2cap_node == NULL)
2195 		return (EINVAL);
2196 
2197 	if (sopt->sopt_level != SOL_L2CAP)
2198 		return (0);
2199 
2200 	mtx_lock(&pcb->pcb_mtx);
2201 
2202 	switch (sopt->sopt_dir) {
2203 	case SOPT_GET:
2204 		switch (sopt->sopt_name) {
2205 		case SO_L2CAP_IMTU: /* get incoming MTU */
2206 			error = sooptcopyout(sopt, &pcb->imtu,
2207 						sizeof(pcb->imtu));
2208 			break;
2209 
2210 		case SO_L2CAP_OMTU: /* get outgoing (peer incoming) MTU */
2211 			error = sooptcopyout(sopt, &pcb->omtu,
2212 						sizeof(pcb->omtu));
2213 			break;
2214 
2215 		case SO_L2CAP_IFLOW: /* get incoming flow spec. */
2216 			error = sooptcopyout(sopt, &pcb->iflow,
2217 						sizeof(pcb->iflow));
2218 			break;
2219 
2220 		case SO_L2CAP_OFLOW: /* get outgoing flow spec. */
2221 			error = sooptcopyout(sopt, &pcb->oflow,
2222 						sizeof(pcb->oflow));
2223 			break;
2224 
2225 		case SO_L2CAP_FLUSH: /* get flush timeout */
2226 			error = sooptcopyout(sopt, &pcb->flush_timo,
2227 						sizeof(pcb->flush_timo));
2228 			break;
2229 
2230 		default:
2231 			error = ENOPROTOOPT;
2232 			break;
2233 		}
2234 		break;
2235 
2236 	case SOPT_SET:
2237 		/*
2238 		 * XXX
2239 		 * We do not allow to change these parameters while socket is
2240 		 * connected or we are in the process of creating a connection.
2241 		 * May be this should indicate re-configuration of the open
2242 		 * channel?
2243 		 */
2244 
2245 		if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
2246 			error = EACCES;
2247 			break;
2248 		}
2249 
2250 		switch (sopt->sopt_name) {
2251 		case SO_L2CAP_IMTU: /* set incoming MTU */
2252 			error = sooptcopyin(sopt, &v, sizeof(v), sizeof(v.mtu));
2253 			if (error == 0)
2254 				pcb->imtu = v.mtu;
2255 			break;
2256 
2257 		case SO_L2CAP_OFLOW: /* set outgoing flow spec. */
2258 			error = sooptcopyin(sopt, &v, sizeof(v),sizeof(v.flow));
2259 			if (error == 0)
2260 				bcopy(&v.flow, &pcb->oflow, sizeof(pcb->oflow));
2261 			break;
2262 
2263 		case SO_L2CAP_FLUSH: /* set flush timeout */
2264 			error = sooptcopyin(sopt, &v, sizeof(v),
2265 						sizeof(v.flush_timo));
2266 			if (error == 0)
2267 				pcb->flush_timo = v.flush_timo;
2268 			break;
2269 
2270 		default:
2271 			error = ENOPROTOOPT;
2272 			break;
2273 		}
2274 		break;
2275 
2276 	default:
2277 		error = EINVAL;
2278 		break;
2279 	}
2280 
2281 	mtx_unlock(&pcb->pcb_mtx);
2282 
2283 	return (error);
2284 } /* ng_btsocket_l2cap_ctloutput */
2285 
2286 /*
2287  * Detach and destroy socket
2288  */
2289 
2290 void
2291 ng_btsocket_l2cap_detach(struct socket *so)
2292 {
2293 	ng_btsocket_l2cap_pcb_p	pcb = so2l2cap_pcb(so);
2294 
2295 	KASSERT(pcb != NULL, ("ng_btsocket_l2cap_detach: pcb == NULL"));
2296 
2297 	if (ng_btsocket_l2cap_node == NULL)
2298 		return;
2299 
2300 	mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
2301 	mtx_lock(&pcb->pcb_mtx);
2302 
2303 	/* XXX what to do with pending request? */
2304 	if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
2305 		ng_btsocket_l2cap_untimeout(pcb);
2306 
2307 	if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED &&
2308 	    pcb->state != NG_BTSOCKET_L2CAP_DISCONNECTING)
2309 		/* Send disconnect request with "zero" token */
2310 		ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
2311 
2312 	pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
2313 
2314 	LIST_REMOVE(pcb, next);
2315 
2316 	mtx_unlock(&pcb->pcb_mtx);
2317 	mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
2318 
2319 	mtx_destroy(&pcb->pcb_mtx);
2320 	bzero(pcb, sizeof(*pcb));
2321 	free(pcb, M_NETGRAPH_BTSOCKET_L2CAP);
2322 
2323 	soisdisconnected(so);
2324 	so->so_pcb = NULL;
2325 } /* ng_btsocket_l2cap_detach */
2326 
2327 /*
2328  * Disconnect socket
2329  */
2330 
2331 int
2332 ng_btsocket_l2cap_disconnect(struct socket *so)
2333 {
2334 	ng_btsocket_l2cap_pcb_p	pcb = so2l2cap_pcb(so);
2335 	int			error = 0;
2336 
2337 	if (pcb == NULL)
2338 		return (EINVAL);
2339 	if (ng_btsocket_l2cap_node == NULL)
2340 		return (EINVAL);
2341 
2342 	mtx_lock(&pcb->pcb_mtx);
2343 
2344 	if (pcb->state == NG_BTSOCKET_L2CAP_DISCONNECTING) {
2345 		mtx_unlock(&pcb->pcb_mtx);
2346 		return (EINPROGRESS);
2347 	}
2348 
2349 	if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
2350 		/* XXX FIXME what to do with pending request? */
2351 		if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
2352 			ng_btsocket_l2cap_untimeout(pcb);
2353 
2354 		error = ng_btsocket_l2cap_send_l2ca_discon_req(pcb->token, pcb);
2355 		if (error == 0) {
2356 			pcb->state = NG_BTSOCKET_L2CAP_DISCONNECTING;
2357 			soisdisconnecting(so);
2358 
2359 			ng_btsocket_l2cap_timeout(pcb);
2360 		}
2361 
2362 		/* XXX FIXME what to do if error != 0 */
2363 	}
2364 
2365 	mtx_unlock(&pcb->pcb_mtx);
2366 
2367 	return (error);
2368 } /* ng_btsocket_l2cap_disconnect */
2369 
2370 /*
2371  * Listen on socket
2372  */
2373 
2374 int
2375 ng_btsocket_l2cap_listen(struct socket *so, int backlog, struct thread *td)
2376 {
2377 	ng_btsocket_l2cap_pcb_p	pcb = so2l2cap_pcb(so);
2378 	int error;
2379 
2380 	SOCK_LOCK(so);
2381 	error = solisten_proto_check(so);
2382 	if (error != 0)
2383 		goto out;
2384 	if (pcb == NULL) {
2385 		error = EINVAL;
2386 		goto out;
2387 	}
2388 	if (ng_btsocket_l2cap_node == NULL) {
2389 		error = EINVAL;
2390 		goto out;
2391 	}
2392 	if (pcb->psm == 0) {
2393 		error = EADDRNOTAVAIL;
2394 		goto out;
2395 	}
2396 	solisten_proto(so, backlog);
2397 out:
2398 	SOCK_UNLOCK(so);
2399 	return (error);
2400 } /* ng_btsocket_listen */
2401 
2402 /*
2403  * Get peer address
2404  */
2405 
2406 int
2407 ng_btsocket_l2cap_peeraddr(struct socket *so, struct sockaddr **nam)
2408 {
2409 	ng_btsocket_l2cap_pcb_p	pcb = so2l2cap_pcb(so);
2410 	struct sockaddr_l2cap	sa;
2411 
2412 	if (pcb == NULL)
2413 		return (EINVAL);
2414 	if (ng_btsocket_l2cap_node == NULL)
2415 		return (EINVAL);
2416 
2417 	bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
2418 	sa.l2cap_psm = htole16(pcb->psm);
2419 	sa.l2cap_len = sizeof(sa);
2420 	sa.l2cap_family = AF_BLUETOOTH;
2421 
2422 	*nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
2423 
2424 	return ((*nam == NULL)? ENOMEM : 0);
2425 } /* ng_btsocket_l2cap_peeraddr */
2426 
2427 /*
2428  * Send data to socket
2429  */
2430 
2431 int
2432 ng_btsocket_l2cap_send(struct socket *so, int flags, struct mbuf *m,
2433 		struct sockaddr *nam, struct mbuf *control, struct thread *td)
2434 {
2435 	ng_btsocket_l2cap_pcb_t	*pcb = so2l2cap_pcb(so);
2436 	int			 error = 0;
2437 
2438 	if (ng_btsocket_l2cap_node == NULL) {
2439 		error = ENETDOWN;
2440 		goto drop;
2441 	}
2442 
2443 	/* Check socket and input */
2444 	if (pcb == NULL || m == NULL || control != NULL) {
2445 		error = EINVAL;
2446 		goto drop;
2447 	}
2448 
2449 	mtx_lock(&pcb->pcb_mtx);
2450 
2451 	/* Make sure socket is connected */
2452 	if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
2453 		mtx_unlock(&pcb->pcb_mtx);
2454 		error = ENOTCONN;
2455 		goto drop;
2456 	}
2457 
2458 	/* Check route */
2459 	if (pcb->rt == NULL ||
2460 	    pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook)) {
2461 		mtx_unlock(&pcb->pcb_mtx);
2462 		error = ENETDOWN;
2463 		goto drop;
2464 	}
2465 
2466 	/* Check packet size agains outgoing (peer's incoming) MTU) */
2467 	if (m->m_pkthdr.len > pcb->omtu) {
2468 		NG_BTSOCKET_L2CAP_ERR(
2469 "%s: Packet too big, len=%d, omtu=%d\n", __func__, m->m_pkthdr.len, pcb->omtu);
2470 
2471 		mtx_unlock(&pcb->pcb_mtx);
2472 		error = EMSGSIZE;
2473 		goto drop;
2474 	}
2475 
2476 	/*
2477 	 * First put packet on socket send queue. Then check if we have
2478 	 * pending timeout. If we do not have timeout then we must send
2479 	 * packet and schedule timeout. Otherwise do nothing and wait for
2480 	 * L2CA_WRITE_RSP.
2481 	 */
2482 
2483 	sbappendrecord(&pcb->so->so_snd, m);
2484 	m = NULL;
2485 
2486 	if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) {
2487 		error = ng_btsocket_l2cap_send2(pcb);
2488 		if (error == 0)
2489 			ng_btsocket_l2cap_timeout(pcb);
2490 		else
2491 			sbdroprecord(&pcb->so->so_snd); /* XXX */
2492 	}
2493 
2494 	mtx_unlock(&pcb->pcb_mtx);
2495 drop:
2496 	NG_FREE_M(m); /* checks for != NULL */
2497 	NG_FREE_M(control);
2498 
2499 	return (error);
2500 } /* ng_btsocket_l2cap_send */
2501 
2502 /*
2503  * Send first packet in the socket queue to the L2CAP layer
2504  */
2505 
2506 static int
2507 ng_btsocket_l2cap_send2(ng_btsocket_l2cap_pcb_p pcb)
2508 {
2509 	struct	mbuf		*m = NULL;
2510 	ng_l2cap_l2ca_hdr_t	*hdr = NULL;
2511 	int			 error = 0;
2512 
2513 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
2514 
2515 	if (sbavail(&pcb->so->so_snd) == 0)
2516 		return (EINVAL); /* XXX */
2517 
2518 	m = m_dup(pcb->so->so_snd.sb_mb, M_NOWAIT);
2519 	if (m == NULL)
2520 		return (ENOBUFS);
2521 
2522 	/* Create L2CA packet header */
2523 	M_PREPEND(m, sizeof(*hdr), M_NOWAIT);
2524 	if (m != NULL)
2525 		if (m->m_len < sizeof(*hdr))
2526 			m = m_pullup(m, sizeof(*hdr));
2527 
2528 	if (m == NULL) {
2529 		NG_BTSOCKET_L2CAP_ERR(
2530 "%s: Failed to create L2CA packet header\n", __func__);
2531 
2532 		return (ENOBUFS);
2533 	}
2534 
2535 	hdr = mtod(m, ng_l2cap_l2ca_hdr_t *);
2536 	hdr->token = pcb->token;
2537 	hdr->length = m->m_pkthdr.len - sizeof(*hdr);
2538 	hdr->lcid = pcb->cid;
2539 
2540 	NG_BTSOCKET_L2CAP_INFO(
2541 "%s: Sending packet: len=%d, length=%d, lcid=%d, token=%d, state=%d\n",
2542 		__func__, m->m_pkthdr.len, hdr->length, hdr->lcid,
2543 		hdr->token, pcb->state);
2544 
2545 	/*
2546 	 * If we got here than we have successfuly creates new L2CAP
2547 	 * data packet and now we can send it to the L2CAP layer
2548 	 */
2549 
2550 	NG_SEND_DATA_ONLY(error, pcb->rt->hook, m);
2551 
2552 	return (error);
2553 } /* ng_btsocket_l2cap_send2 */
2554 
2555 /*
2556  * Get socket address
2557  */
2558 
2559 int
2560 ng_btsocket_l2cap_sockaddr(struct socket *so, struct sockaddr **nam)
2561 {
2562 	ng_btsocket_l2cap_pcb_p	pcb = so2l2cap_pcb(so);
2563 	struct sockaddr_l2cap	sa;
2564 
2565 	if (pcb == NULL)
2566 		return (EINVAL);
2567 	if (ng_btsocket_l2cap_node == NULL)
2568 		return (EINVAL);
2569 
2570 	bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
2571 	sa.l2cap_psm = htole16(pcb->psm);
2572 	sa.l2cap_len = sizeof(sa);
2573 	sa.l2cap_family = AF_BLUETOOTH;
2574 
2575 	*nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
2576 
2577 	return ((*nam == NULL)? ENOMEM : 0);
2578 } /* ng_btsocket_l2cap_sockaddr */
2579 
2580 /*****************************************************************************
2581  *****************************************************************************
2582  **                              Misc. functions
2583  *****************************************************************************
2584  *****************************************************************************/
2585 
2586 /*
2587  * Look for the socket that listens on given PSM and bdaddr. Returns exact or
2588  * close match (if any). Caller must hold ng_btsocket_l2cap_sockets_mtx.
2589  */
2590 
2591 static ng_btsocket_l2cap_pcb_p
2592 ng_btsocket_l2cap_pcb_by_addr(bdaddr_p bdaddr, int psm)
2593 {
2594 	ng_btsocket_l2cap_pcb_p	p = NULL, p1 = NULL;
2595 
2596 	mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
2597 
2598 	LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next) {
2599 		if (p->so == NULL || !(p->so->so_options & SO_ACCEPTCONN) ||
2600 		    p->psm != psm)
2601 			continue;
2602 
2603 		if (bcmp(&p->src, bdaddr, sizeof(p->src)) == 0)
2604 			break;
2605 
2606 		if (bcmp(&p->src, NG_HCI_BDADDR_ANY, sizeof(p->src)) == 0)
2607 			p1 = p;
2608 	}
2609 
2610 	return ((p != NULL)? p : p1);
2611 } /* ng_btsocket_l2cap_pcb_by_addr */
2612 
2613 /*
2614  * Look for the socket that has given token.
2615  * Caller must hold ng_btsocket_l2cap_sockets_mtx.
2616  */
2617 
2618 static ng_btsocket_l2cap_pcb_p
2619 ng_btsocket_l2cap_pcb_by_token(u_int32_t token)
2620 {
2621 	ng_btsocket_l2cap_pcb_p	p = NULL;
2622 
2623 	if (token == 0)
2624 		return (NULL);
2625 
2626 	mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
2627 
2628 	LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next)
2629 		if (p->token == token)
2630 			break;
2631 
2632 	return (p);
2633 } /* ng_btsocket_l2cap_pcb_by_token */
2634 
2635 /*
2636  * Look for the socket that assigned to given source address and channel ID.
2637  * Caller must hold ng_btsocket_l2cap_sockets_mtx
2638  */
2639 
2640 static ng_btsocket_l2cap_pcb_p
2641 ng_btsocket_l2cap_pcb_by_cid(bdaddr_p src, int cid)
2642 {
2643 	ng_btsocket_l2cap_pcb_p	p = NULL;
2644 
2645 	mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
2646 
2647 	LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next)
2648 		if (p->cid == cid && bcmp(src, &p->src, sizeof(p->src)) == 0)
2649 			break;
2650 
2651 	return (p);
2652 } /* ng_btsocket_l2cap_pcb_by_cid */
2653 
2654 /*
2655  * Set timeout on socket
2656  */
2657 
2658 static void
2659 ng_btsocket_l2cap_timeout(ng_btsocket_l2cap_pcb_p pcb)
2660 {
2661 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
2662 
2663 	if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) {
2664 		pcb->flags |= NG_BTSOCKET_L2CAP_TIMO;
2665 		callout_reset(&pcb->timo, bluetooth_l2cap_ertx_timeout(),
2666 		    ng_btsocket_l2cap_process_timeout, pcb);
2667 	} else
2668 		KASSERT(0,
2669 ("%s: Duplicated socket timeout?!\n", __func__));
2670 } /* ng_btsocket_l2cap_timeout */
2671 
2672 /*
2673  * Unset timeout on socket
2674  */
2675 
2676 static void
2677 ng_btsocket_l2cap_untimeout(ng_btsocket_l2cap_pcb_p pcb)
2678 {
2679 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
2680 
2681 	if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO) {
2682 		callout_stop(&pcb->timo);
2683 		pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO;
2684 	} else
2685 		KASSERT(0,
2686 ("%s: No socket timeout?!\n", __func__));
2687 } /* ng_btsocket_l2cap_untimeout */
2688 
2689 /*
2690  * Process timeout on socket
2691  */
2692 
2693 static void
2694 ng_btsocket_l2cap_process_timeout(void *xpcb)
2695 {
2696 	ng_btsocket_l2cap_pcb_p	pcb = (ng_btsocket_l2cap_pcb_p) xpcb;
2697 
2698 	mtx_assert(&pcb->pcb_mtx, MA_OWNED);
2699 
2700 	pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO;
2701 	pcb->so->so_error = ETIMEDOUT;
2702 
2703 	switch (pcb->state) {
2704 	case NG_BTSOCKET_L2CAP_CONNECTING:
2705 	case NG_BTSOCKET_L2CAP_CONFIGURING:
2706 		/* Send disconnect request with "zero" token */
2707 		if (pcb->cid != 0)
2708 			ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
2709 
2710 		/* ... and close the socket */
2711 		pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
2712 		soisdisconnected(pcb->so);
2713 		break;
2714 
2715 	case NG_BTSOCKET_L2CAP_OPEN:
2716 		/* Send timeout - drop packet and wakeup sender */
2717 		sbdroprecord(&pcb->so->so_snd);
2718 		sowwakeup(pcb->so);
2719 		break;
2720 
2721 	case NG_BTSOCKET_L2CAP_DISCONNECTING:
2722 		/* Disconnect timeout - disconnect the socket anyway */
2723 		pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
2724 		soisdisconnected(pcb->so);
2725 		break;
2726 
2727 	default:
2728 		NG_BTSOCKET_L2CAP_ERR(
2729 "%s: Invalid socket state=%d\n", __func__, pcb->state);
2730 		break;
2731 	}
2732 } /* ng_btsocket_l2cap_process_timeout */
2733 
2734 /*
2735  * Translate HCI/L2CAP error code into "errno" code
2736  * XXX Note: Some L2CAP and HCI error codes have the same value, but
2737  *     different meaning
2738  */
2739 
2740 static int
2741 ng_btsocket_l2cap_result2errno(int result)
2742 {
2743 	switch (result) {
2744 	case 0x00: /* No error */
2745 		return (0);
2746 
2747 	case 0x01: /* Unknown HCI command */
2748 		return (ENODEV);
2749 
2750 	case 0x02: /* No connection */
2751 		return (ENOTCONN);
2752 
2753 	case 0x03: /* Hardware failure */
2754 		return (EIO);
2755 
2756 	case 0x04: /* Page timeout */
2757 		return (EHOSTDOWN);
2758 
2759 	case 0x05: /* Authentication failure */
2760 	case 0x06: /* Key missing */
2761 	case 0x18: /* Pairing not allowed */
2762 	case 0x21: /* Role change not allowed */
2763 	case 0x24: /* LMP PSU not allowed */
2764 	case 0x25: /* Encryption mode not acceptable */
2765 	case 0x26: /* Unit key used */
2766 		return (EACCES);
2767 
2768 	case 0x07: /* Memory full */
2769 		return (ENOMEM);
2770 
2771 	case 0x08:   /* Connection timeout */
2772 	case 0x10:   /* Host timeout */
2773 	case 0x22:   /* LMP response timeout */
2774 	case 0xee:   /* HCI timeout */
2775 	case 0xeeee: /* L2CAP timeout */
2776 		return (ETIMEDOUT);
2777 
2778 	case 0x09: /* Max number of connections */
2779 	case 0x0a: /* Max number of SCO connections to a unit */
2780 		return (EMLINK);
2781 
2782 	case 0x0b: /* ACL connection already exists */
2783 		return (EEXIST);
2784 
2785 	case 0x0c: /* Command disallowed */
2786 		return (EBUSY);
2787 
2788 	case 0x0d: /* Host rejected due to limited resources */
2789 	case 0x0e: /* Host rejected due to securiity reasons */
2790 	case 0x0f: /* Host rejected due to remote unit is a personal unit */
2791 	case 0x1b: /* SCO offset rejected */
2792 	case 0x1c: /* SCO interval rejected */
2793 	case 0x1d: /* SCO air mode rejected */
2794 		return (ECONNREFUSED);
2795 
2796 	case 0x11: /* Unsupported feature or parameter value */
2797 	case 0x19: /* Unknown LMP PDU */
2798 	case 0x1a: /* Unsupported remote feature */
2799 	case 0x20: /* Unsupported LMP parameter value */
2800 	case 0x27: /* QoS is not supported */
2801 	case 0x29: /* Paring with unit key not supported */
2802 		return (EOPNOTSUPP);
2803 
2804 	case 0x12: /* Invalid HCI command parameter */
2805 	case 0x1e: /* Invalid LMP parameters */
2806 		return (EINVAL);
2807 
2808 	case 0x13: /* Other end terminated connection: User ended connection */
2809 	case 0x14: /* Other end terminated connection: Low resources */
2810 	case 0x15: /* Other end terminated connection: About to power off */
2811 		return (ECONNRESET);
2812 
2813 	case 0x16: /* Connection terminated by local host */
2814 		return (ECONNABORTED);
2815 
2816 #if 0 /* XXX not yet */
2817 	case 0x17: /* Repeated attempts */
2818 	case 0x1f: /* Unspecified error */
2819 	case 0x23: /* LMP error transaction collision */
2820 	case 0x28: /* Instant passed */
2821 #endif
2822 	}
2823 
2824 	return (ENOSYS);
2825 } /* ng_btsocket_l2cap_result2errno */
2826 
2827