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