xref: /dflybsd-src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_main.c (revision e85b99abf6da4a83a7dc495b0ef37ce19864149f)
1b06ebda0SMatthew Dillon /*
2b06ebda0SMatthew Dillon  * ng_l2cap_main.c
3b06ebda0SMatthew Dillon  */
4b06ebda0SMatthew Dillon 
5b06ebda0SMatthew Dillon /*-
6b06ebda0SMatthew Dillon  * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
7b06ebda0SMatthew Dillon  * All rights reserved.
8b06ebda0SMatthew Dillon  *
9b06ebda0SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
10b06ebda0SMatthew Dillon  * modification, are permitted provided that the following conditions
11b06ebda0SMatthew Dillon  * are met:
12b06ebda0SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
13b06ebda0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
14b06ebda0SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
15b06ebda0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
16b06ebda0SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
17b06ebda0SMatthew Dillon  *
18b06ebda0SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19b06ebda0SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b06ebda0SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b06ebda0SMatthew Dillon  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22b06ebda0SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b06ebda0SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24b06ebda0SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25b06ebda0SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26b06ebda0SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27b06ebda0SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28b06ebda0SMatthew Dillon  * SUCH DAMAGE.
29b06ebda0SMatthew Dillon  *
30b06ebda0SMatthew Dillon  * $Id: ng_l2cap_main.c,v 1.2 2003/04/28 21:44:59 max Exp $
31b06ebda0SMatthew Dillon  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_main.c,v 1.5 2005/01/07 01:45:43 imp Exp $
32b06ebda0SMatthew Dillon  */
33b06ebda0SMatthew Dillon 
34b06ebda0SMatthew Dillon #include <sys/param.h>
35b06ebda0SMatthew Dillon #include <sys/systm.h>
36b06ebda0SMatthew Dillon #include <sys/kernel.h>
37b06ebda0SMatthew Dillon #include <sys/malloc.h>
38b06ebda0SMatthew Dillon #include <sys/mbuf.h>
39b06ebda0SMatthew Dillon #include <sys/queue.h>
40*e85b99abSSascha Wildner #include <netgraph7/ng_message.h>
41*e85b99abSSascha Wildner #include <netgraph7/netgraph.h>
42*e85b99abSSascha Wildner #include <netgraph7/ng_parse.h>
43*e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_bluetooth.h>
44*e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_hci.h>
45*e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_l2cap.h>
46*e85b99abSSascha Wildner #include <netgraph7/bluetooth/l2cap/ng_l2cap_var.h>
47*e85b99abSSascha Wildner #include <netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h>
48*e85b99abSSascha Wildner #include <netgraph7/bluetooth/l2cap/ng_l2cap_evnt.h>
49*e85b99abSSascha Wildner #include <netgraph7/bluetooth/l2cap/ng_l2cap_llpi.h>
50*e85b99abSSascha Wildner #include <netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.h>
51*e85b99abSSascha Wildner #include <netgraph7/bluetooth/l2cap/ng_l2cap_misc.h>
52*e85b99abSSascha Wildner #include <netgraph7/bluetooth/l2cap/ng_l2cap_prse.h>
53b06ebda0SMatthew Dillon 
54b06ebda0SMatthew Dillon /******************************************************************************
55b06ebda0SMatthew Dillon  ******************************************************************************
56b06ebda0SMatthew Dillon  **  This node implements Link Layer Control and Adaptation Protocol (L2CAP)
57b06ebda0SMatthew Dillon  ******************************************************************************
58b06ebda0SMatthew Dillon  ******************************************************************************/
59b06ebda0SMatthew Dillon 
60b06ebda0SMatthew Dillon /* MALLOC define */
61b06ebda0SMatthew Dillon #ifdef NG_SEPARATE_MALLOC
62b06ebda0SMatthew Dillon MALLOC_DEFINE(M_NETGRAPH_L2CAP, "netgraph_l2cap",
63b06ebda0SMatthew Dillon 	"Netgraph Bluetooth L2CAP node");
64b06ebda0SMatthew Dillon #else
65b06ebda0SMatthew Dillon #define M_NETGRAPH_L2CAP M_NETGRAPH
66b06ebda0SMatthew Dillon #endif /* NG_SEPARATE_MALLOC */
67b06ebda0SMatthew Dillon 
68b06ebda0SMatthew Dillon /* Netgraph node methods */
69b06ebda0SMatthew Dillon static	ng_constructor_t	ng_l2cap_constructor;
70b06ebda0SMatthew Dillon static	ng_shutdown_t		ng_l2cap_shutdown;
71b06ebda0SMatthew Dillon static	ng_newhook_t		ng_l2cap_newhook;
72b06ebda0SMatthew Dillon static	ng_connect_t		ng_l2cap_connect;
73b06ebda0SMatthew Dillon static	ng_disconnect_t		ng_l2cap_disconnect;
74b06ebda0SMatthew Dillon static	ng_rcvmsg_t		ng_l2cap_lower_rcvmsg;
75b06ebda0SMatthew Dillon static	ng_rcvmsg_t		ng_l2cap_upper_rcvmsg;
76b06ebda0SMatthew Dillon static	ng_rcvmsg_t		ng_l2cap_default_rcvmsg;
77b06ebda0SMatthew Dillon static	ng_rcvdata_t		ng_l2cap_rcvdata;
78b06ebda0SMatthew Dillon 
79b06ebda0SMatthew Dillon /* Netgraph node type descriptor */
80b06ebda0SMatthew Dillon static	struct ng_type typestruct = {
81b06ebda0SMatthew Dillon 	.version =	NG_ABI_VERSION,
82b06ebda0SMatthew Dillon 	.name =		NG_L2CAP_NODE_TYPE,
83b06ebda0SMatthew Dillon 	.constructor =	ng_l2cap_constructor,
84b06ebda0SMatthew Dillon 	.rcvmsg =	ng_l2cap_default_rcvmsg,
85b06ebda0SMatthew Dillon 	.shutdown =	ng_l2cap_shutdown,
86b06ebda0SMatthew Dillon 	.newhook =	ng_l2cap_newhook,
87b06ebda0SMatthew Dillon 	.connect =	ng_l2cap_connect,
88b06ebda0SMatthew Dillon 	.rcvdata =	ng_l2cap_rcvdata,
89b06ebda0SMatthew Dillon 	.disconnect =	ng_l2cap_disconnect,
90b06ebda0SMatthew Dillon 	.cmdlist =	ng_l2cap_cmdlist,
91b06ebda0SMatthew Dillon };
92b06ebda0SMatthew Dillon NETGRAPH_INIT(l2cap, &typestruct);
93b06ebda0SMatthew Dillon MODULE_VERSION(ng_l2cap, NG_BLUETOOTH_VERSION);
94b06ebda0SMatthew Dillon MODULE_DEPEND(ng_l2cap, ng_bluetooth, NG_BLUETOOTH_VERSION,
95b06ebda0SMatthew Dillon         NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
96b06ebda0SMatthew Dillon 
97b06ebda0SMatthew Dillon /*****************************************************************************
98b06ebda0SMatthew Dillon  *****************************************************************************
99b06ebda0SMatthew Dillon  **                   Netgraph methods implementation
100b06ebda0SMatthew Dillon  *****************************************************************************
101b06ebda0SMatthew Dillon  *****************************************************************************/
102b06ebda0SMatthew Dillon 
103b06ebda0SMatthew Dillon static void ng_l2cap_cleanup          (ng_l2cap_p);
104b06ebda0SMatthew Dillon static void ng_l2cap_destroy_channels (ng_l2cap_p);
105b06ebda0SMatthew Dillon 
106b06ebda0SMatthew Dillon /*
107b06ebda0SMatthew Dillon  * Create new instance of L2CAP node
108b06ebda0SMatthew Dillon  */
109b06ebda0SMatthew Dillon 
110b06ebda0SMatthew Dillon static int
ng_l2cap_constructor(node_p node)111b06ebda0SMatthew Dillon ng_l2cap_constructor(node_p node)
112b06ebda0SMatthew Dillon {
113b06ebda0SMatthew Dillon 	ng_l2cap_p	l2cap = NULL;
114b06ebda0SMatthew Dillon 
115b06ebda0SMatthew Dillon 	/* Create new L2CAP node */
116fc025606SSascha Wildner 	l2cap = kmalloc(sizeof(*l2cap), M_NETGRAPH_L2CAP,
117fc025606SSascha Wildner 			M_WAITOK | M_NULLOK | M_ZERO);
118b06ebda0SMatthew Dillon 	if (l2cap == NULL)
119b06ebda0SMatthew Dillon 		return (ENOMEM);
120b06ebda0SMatthew Dillon 
121b06ebda0SMatthew Dillon 	l2cap->node = node;
122b06ebda0SMatthew Dillon 	l2cap->debug = NG_L2CAP_WARN_LEVEL;
123b06ebda0SMatthew Dillon 	l2cap->discon_timo = 5; /* sec */
124b06ebda0SMatthew Dillon 
125b06ebda0SMatthew Dillon 	LIST_INIT(&l2cap->con_list);
126b06ebda0SMatthew Dillon 	LIST_INIT(&l2cap->chan_list);
127b06ebda0SMatthew Dillon 
128b06ebda0SMatthew Dillon 	NG_NODE_SET_PRIVATE(node, l2cap);
129b06ebda0SMatthew Dillon 	NG_NODE_FORCE_WRITER(node);
130b06ebda0SMatthew Dillon 
131b06ebda0SMatthew Dillon 	return (0);
132b06ebda0SMatthew Dillon } /* ng_l2cap_constructor */
133b06ebda0SMatthew Dillon 
134b06ebda0SMatthew Dillon /*
135b06ebda0SMatthew Dillon  * Shutdown L2CAP node
136b06ebda0SMatthew Dillon  */
137b06ebda0SMatthew Dillon 
138b06ebda0SMatthew Dillon static int
ng_l2cap_shutdown(node_p node)139b06ebda0SMatthew Dillon ng_l2cap_shutdown(node_p node)
140b06ebda0SMatthew Dillon {
141b06ebda0SMatthew Dillon 	ng_l2cap_p	l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
142b06ebda0SMatthew Dillon 
143b06ebda0SMatthew Dillon 	NG_NODE_SET_PRIVATE(node, NULL);
144b06ebda0SMatthew Dillon 	NG_NODE_UNREF(node);
145b06ebda0SMatthew Dillon 
146b06ebda0SMatthew Dillon 	/* Clean up L2CAP node. Delete all connection, channels and commands */
147b06ebda0SMatthew Dillon 	l2cap->node = NULL;
148b06ebda0SMatthew Dillon 	ng_l2cap_cleanup(l2cap);
149b06ebda0SMatthew Dillon 
150b06ebda0SMatthew Dillon 	bzero(l2cap, sizeof(*l2cap));
151fc025606SSascha Wildner 	kfree(l2cap, M_NETGRAPH_L2CAP);
152b06ebda0SMatthew Dillon 
153b06ebda0SMatthew Dillon 	return (0);
154b06ebda0SMatthew Dillon } /* ng_l2cap_shutdown */
155b06ebda0SMatthew Dillon 
156b06ebda0SMatthew Dillon /*
157b06ebda0SMatthew Dillon  * Give our OK for a hook to be added. HCI layer is connected to the HCI
158b06ebda0SMatthew Dillon  * (NG_L2CAP_HOOK_HCI) hook. As per specification L2CAP layer MUST provide
159b06ebda0SMatthew Dillon  * Procol/Service Multiplexing, so the L2CAP node provides separate hooks
160b06ebda0SMatthew Dillon  * for SDP (NG_L2CAP_HOOK_SDP), RFCOMM (NG_L2CAP_HOOK_RFCOMM) and TCP
161b06ebda0SMatthew Dillon  * (NG_L2CAP_HOOK_TCP) protcols. Unknown PSM will be forwarded to
162b06ebda0SMatthew Dillon  * NG_L2CAP_HOOK_ORPHAN hook. Control node/application is connected to
163b06ebda0SMatthew Dillon  * control (NG_L2CAP_HOOK_CTL) hook.
164b06ebda0SMatthew Dillon  */
165b06ebda0SMatthew Dillon 
166b06ebda0SMatthew Dillon static int
ng_l2cap_newhook(node_p node,hook_p hook,char const * name)167b06ebda0SMatthew Dillon ng_l2cap_newhook(node_p node, hook_p hook, char const *name)
168b06ebda0SMatthew Dillon {
169b06ebda0SMatthew Dillon 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
170b06ebda0SMatthew Dillon 	hook_p		*h = NULL;
171b06ebda0SMatthew Dillon 
172b06ebda0SMatthew Dillon 	if (strcmp(name, NG_L2CAP_HOOK_HCI) == 0)
173b06ebda0SMatthew Dillon 		h = &l2cap->hci;
174b06ebda0SMatthew Dillon 	else if (strcmp(name, NG_L2CAP_HOOK_L2C) == 0)
175b06ebda0SMatthew Dillon 		h = &l2cap->l2c;
176b06ebda0SMatthew Dillon 	else if (strcmp(name, NG_L2CAP_HOOK_CTL) == 0)
177b06ebda0SMatthew Dillon 		h = &l2cap->ctl;
178b06ebda0SMatthew Dillon 	else
179b06ebda0SMatthew Dillon 		return (EINVAL);
180b06ebda0SMatthew Dillon 
181b06ebda0SMatthew Dillon 	if (*h != NULL)
182b06ebda0SMatthew Dillon 		return (EISCONN);
183b06ebda0SMatthew Dillon 
184b06ebda0SMatthew Dillon 	*h = hook;
185b06ebda0SMatthew Dillon 
186b06ebda0SMatthew Dillon 	return (0);
187b06ebda0SMatthew Dillon } /* ng_l2cap_newhook */
188b06ebda0SMatthew Dillon 
189b06ebda0SMatthew Dillon /*
190b06ebda0SMatthew Dillon  * Give our final OK to connect hook. Nothing to do here.
191b06ebda0SMatthew Dillon  */
192b06ebda0SMatthew Dillon 
193b06ebda0SMatthew Dillon static int
ng_l2cap_connect(hook_p hook)194b06ebda0SMatthew Dillon ng_l2cap_connect(hook_p hook)
195b06ebda0SMatthew Dillon {
196b06ebda0SMatthew Dillon 	ng_l2cap_p	l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
197b06ebda0SMatthew Dillon 	int		error = 0;
198b06ebda0SMatthew Dillon 
199b06ebda0SMatthew Dillon 	if (hook == l2cap->hci)
200b06ebda0SMatthew Dillon 		NG_HOOK_SET_RCVMSG(hook, ng_l2cap_lower_rcvmsg);
201b06ebda0SMatthew Dillon 	else
202b06ebda0SMatthew Dillon 	if (hook == l2cap->l2c || hook == l2cap->ctl) {
203b06ebda0SMatthew Dillon 		NG_HOOK_SET_RCVMSG(hook, ng_l2cap_upper_rcvmsg);
204b06ebda0SMatthew Dillon 
205b06ebda0SMatthew Dillon 		/* Send delayed notification to the upper layer */
206b06ebda0SMatthew Dillon 		error = ng_send_fn(l2cap->node, hook, ng_l2cap_send_hook_info,
207b06ebda0SMatthew Dillon 				NULL, 0);
208b06ebda0SMatthew Dillon 	} else
209b06ebda0SMatthew Dillon 		error = EINVAL;
210b06ebda0SMatthew Dillon 
211b06ebda0SMatthew Dillon 	return (error);
212b06ebda0SMatthew Dillon } /* ng_l2cap_connect */
213b06ebda0SMatthew Dillon 
214b06ebda0SMatthew Dillon /*
215b06ebda0SMatthew Dillon  * Disconnect the hook. For downstream hook we must notify upper layers.
216b06ebda0SMatthew Dillon  *
217b06ebda0SMatthew Dillon  * XXX For upstream hooks this is really ugly :( Hook was disconnected and it
218b06ebda0SMatthew Dillon  * XXX is now too late to do anything. For now we just clean up our own mess
219b06ebda0SMatthew Dillon  * XXX and remove all channels that use disconnected upstream hook. If we don't
220b06ebda0SMatthew Dillon  * XXX do that then L2CAP node can get out of sync with upper layers.
221b06ebda0SMatthew Dillon  * XXX No notification will be sent to remote peer.
222b06ebda0SMatthew Dillon  */
223b06ebda0SMatthew Dillon 
224b06ebda0SMatthew Dillon static int
ng_l2cap_disconnect(hook_p hook)225b06ebda0SMatthew Dillon ng_l2cap_disconnect(hook_p hook)
226b06ebda0SMatthew Dillon {
227b06ebda0SMatthew Dillon 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
228b06ebda0SMatthew Dillon 	hook_p		*h = NULL;
229b06ebda0SMatthew Dillon 
230b06ebda0SMatthew Dillon 	if (hook == l2cap->hci) {
231b06ebda0SMatthew Dillon 		ng_l2cap_cleanup(l2cap);
232b06ebda0SMatthew Dillon 		h = &l2cap->hci;
233b06ebda0SMatthew Dillon 	} else
234b06ebda0SMatthew Dillon 	if (hook == l2cap->l2c) {
235b06ebda0SMatthew Dillon 		ng_l2cap_destroy_channels(l2cap);
236b06ebda0SMatthew Dillon 		h = &l2cap->l2c;
237b06ebda0SMatthew Dillon 	} else
238b06ebda0SMatthew Dillon 	if (hook == l2cap->ctl)
239b06ebda0SMatthew Dillon 		h = &l2cap->ctl;
240b06ebda0SMatthew Dillon 	else
241b06ebda0SMatthew Dillon 		return (EINVAL);
242b06ebda0SMatthew Dillon 
243b06ebda0SMatthew Dillon 	*h = NULL;
244b06ebda0SMatthew Dillon 
245b06ebda0SMatthew Dillon 	/* Shutdown when all hooks are disconnected */
246b06ebda0SMatthew Dillon 	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
247b06ebda0SMatthew Dillon 	    NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
248b06ebda0SMatthew Dillon 		ng_rmnode_self(NG_HOOK_NODE(hook));
249b06ebda0SMatthew Dillon 
250b06ebda0SMatthew Dillon 	return (0);
251b06ebda0SMatthew Dillon } /* ng_l2cap_disconnect */
252b06ebda0SMatthew Dillon 
253b06ebda0SMatthew Dillon /*
254b06ebda0SMatthew Dillon  * Process control message from lower layer
255b06ebda0SMatthew Dillon  */
256b06ebda0SMatthew Dillon 
257b06ebda0SMatthew Dillon static int
ng_l2cap_lower_rcvmsg(node_p node,item_p item,hook_p lasthook)258b06ebda0SMatthew Dillon ng_l2cap_lower_rcvmsg(node_p node, item_p item, hook_p lasthook)
259b06ebda0SMatthew Dillon {
260b06ebda0SMatthew Dillon 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
261b06ebda0SMatthew Dillon 	struct ng_mesg	*msg = NGI_MSG(item); /* item still has message */
262b06ebda0SMatthew Dillon 	int		 error = 0;
263b06ebda0SMatthew Dillon 
264b06ebda0SMatthew Dillon 	switch (msg->header.typecookie) {
265b06ebda0SMatthew Dillon 	case NGM_HCI_COOKIE:
266b06ebda0SMatthew Dillon 		switch (msg->header.cmd) {
267b06ebda0SMatthew Dillon 		/* HCI node is ready */
268b06ebda0SMatthew Dillon 		case NGM_HCI_NODE_UP: {
269b06ebda0SMatthew Dillon 			ng_hci_node_up_ep	*ep = NULL;
270b06ebda0SMatthew Dillon 
271b06ebda0SMatthew Dillon 			if (msg->header.arglen != sizeof(*ep))
272b06ebda0SMatthew Dillon 				error = EMSGSIZE;
273b06ebda0SMatthew Dillon 			else {
274b06ebda0SMatthew Dillon 				ep = (ng_hci_node_up_ep *)(msg->data);
275b06ebda0SMatthew Dillon 
276b06ebda0SMatthew Dillon 				NG_L2CAP_INFO(
277b06ebda0SMatthew Dillon "%s: %s - HCI node is up, bdaddr: %x:%x:%x:%x:%x:%x, " \
278b06ebda0SMatthew Dillon "pkt_size=%d bytes, num_pkts=%d\n",	__func__, NG_NODE_NAME(l2cap->node),
279b06ebda0SMatthew Dillon 					ep->bdaddr.b[5], ep->bdaddr.b[4],
280b06ebda0SMatthew Dillon 					ep->bdaddr.b[3], ep->bdaddr.b[2],
281b06ebda0SMatthew Dillon 					ep->bdaddr.b[1], ep->bdaddr.b[0],
282b06ebda0SMatthew Dillon 					ep->pkt_size, ep->num_pkts);
283b06ebda0SMatthew Dillon 
284b06ebda0SMatthew Dillon 				bcopy(&ep->bdaddr, &l2cap->bdaddr,
285b06ebda0SMatthew Dillon 					sizeof(l2cap->bdaddr));
286b06ebda0SMatthew Dillon 				l2cap->pkt_size = ep->pkt_size;
287b06ebda0SMatthew Dillon 				l2cap->num_pkts = ep->num_pkts;
288b06ebda0SMatthew Dillon 
289b06ebda0SMatthew Dillon 				/* Notify upper layers */
290b06ebda0SMatthew Dillon 				ng_l2cap_send_hook_info(l2cap->node,
291b06ebda0SMatthew Dillon 					l2cap->l2c, NULL, 0);
292b06ebda0SMatthew Dillon 				ng_l2cap_send_hook_info(l2cap->node,
293b06ebda0SMatthew Dillon 					l2cap->ctl, NULL, 0);
294b06ebda0SMatthew Dillon 			}
295b06ebda0SMatthew Dillon 			} break;
296b06ebda0SMatthew Dillon 
297b06ebda0SMatthew Dillon 		case NGM_HCI_SYNC_CON_QUEUE: {
298b06ebda0SMatthew Dillon 			ng_hci_sync_con_queue_ep	*ep = NULL;
299b06ebda0SMatthew Dillon 			ng_l2cap_con_p			 con = NULL;
300b06ebda0SMatthew Dillon 
301b06ebda0SMatthew Dillon 			if (msg->header.arglen != sizeof(*ep))
302b06ebda0SMatthew Dillon 				error = EMSGSIZE;
303b06ebda0SMatthew Dillon 			else {
304b06ebda0SMatthew Dillon 				ep = (ng_hci_sync_con_queue_ep *)(msg->data);
305b06ebda0SMatthew Dillon 				con = ng_l2cap_con_by_handle(l2cap,
306b06ebda0SMatthew Dillon 							ep->con_handle);
307b06ebda0SMatthew Dillon 				if (con == NULL)
308b06ebda0SMatthew Dillon 					break;
309b06ebda0SMatthew Dillon 
310b06ebda0SMatthew Dillon 				NG_L2CAP_INFO(
311b06ebda0SMatthew Dillon "%s: %s - sync HCI connection queue, con_handle=%d, pending=%d, completed=%d\n",
312b06ebda0SMatthew Dillon 					 __func__, NG_NODE_NAME(l2cap->node),
313b06ebda0SMatthew Dillon 					ep->con_handle, con->pending,
314b06ebda0SMatthew Dillon 					ep->completed);
315b06ebda0SMatthew Dillon 
316b06ebda0SMatthew Dillon 				con->pending -= ep->completed;
317b06ebda0SMatthew Dillon 				if (con->pending < 0) {
318b06ebda0SMatthew Dillon 					NG_L2CAP_WARN(
319b06ebda0SMatthew Dillon "%s: %s - pending packet counter is out of sync! " \
320b06ebda0SMatthew Dillon "con_handle=%d, pending=%d, completed=%d\n",	__func__,
321b06ebda0SMatthew Dillon 						NG_NODE_NAME(l2cap->node),
322b06ebda0SMatthew Dillon 						con->con_handle, con->pending,
323b06ebda0SMatthew Dillon 						ep->completed);
324b06ebda0SMatthew Dillon 
325b06ebda0SMatthew Dillon 					con->pending = 0;
326b06ebda0SMatthew Dillon 				}
327b06ebda0SMatthew Dillon 
328b06ebda0SMatthew Dillon 				ng_l2cap_lp_deliver(con);
329b06ebda0SMatthew Dillon 			}
330b06ebda0SMatthew Dillon 			} break;
331b06ebda0SMatthew Dillon 
332b06ebda0SMatthew Dillon 		/* LP_ConnectCfm[Neg] */
333b06ebda0SMatthew Dillon 		case NGM_HCI_LP_CON_CFM:
334b06ebda0SMatthew Dillon 			error = ng_l2cap_lp_con_cfm(l2cap, msg);
335b06ebda0SMatthew Dillon 			break;
336b06ebda0SMatthew Dillon 
337b06ebda0SMatthew Dillon 		/* LP_ConnectInd */
338b06ebda0SMatthew Dillon 		case NGM_HCI_LP_CON_IND:
339b06ebda0SMatthew Dillon 			error = ng_l2cap_lp_con_ind(l2cap, msg);
340b06ebda0SMatthew Dillon 			break;
341b06ebda0SMatthew Dillon 
342b06ebda0SMatthew Dillon 		/* LP_DisconnectInd */
343b06ebda0SMatthew Dillon 		case NGM_HCI_LP_DISCON_IND:
344b06ebda0SMatthew Dillon 			error = ng_l2cap_lp_discon_ind(l2cap, msg);
345b06ebda0SMatthew Dillon 			break;
346b06ebda0SMatthew Dillon 
347b06ebda0SMatthew Dillon 		/* LP_QoSSetupCfm[Neg] */
348b06ebda0SMatthew Dillon 		case NGM_HCI_LP_QOS_CFM:
349b06ebda0SMatthew Dillon 			error = ng_l2cap_lp_qos_cfm(l2cap, msg);
350b06ebda0SMatthew Dillon 			break;
351b06ebda0SMatthew Dillon 
352b06ebda0SMatthew Dillon 		/* LP_OoSViolationInd */
353b06ebda0SMatthew Dillon 		case NGM_HCI_LP_QOS_IND:
354b06ebda0SMatthew Dillon 			error = ng_l2cap_lp_qos_ind(l2cap, msg);
355b06ebda0SMatthew Dillon 			break;
356b06ebda0SMatthew Dillon 
357b06ebda0SMatthew Dillon 		default:
358b06ebda0SMatthew Dillon 			error = EINVAL;
359b06ebda0SMatthew Dillon 			break;
360b06ebda0SMatthew Dillon 		}
361b06ebda0SMatthew Dillon 		break;
362b06ebda0SMatthew Dillon 
363b06ebda0SMatthew Dillon 	default:
364b06ebda0SMatthew Dillon 		return (ng_l2cap_default_rcvmsg(node, item, lasthook));
365b06ebda0SMatthew Dillon 		/* NOT REACHED */
366b06ebda0SMatthew Dillon 	}
367b06ebda0SMatthew Dillon 
368b06ebda0SMatthew Dillon 	NG_FREE_ITEM(item);
369b06ebda0SMatthew Dillon 
370b06ebda0SMatthew Dillon 	return (error);
371b06ebda0SMatthew Dillon } /* ng_l2cap_lower_rcvmsg */
372b06ebda0SMatthew Dillon 
373b06ebda0SMatthew Dillon /*
374b06ebda0SMatthew Dillon  * Process control message from upper layer
375b06ebda0SMatthew Dillon  */
376b06ebda0SMatthew Dillon 
377b06ebda0SMatthew Dillon static int
ng_l2cap_upper_rcvmsg(node_p node,item_p item,hook_p lasthook)378b06ebda0SMatthew Dillon ng_l2cap_upper_rcvmsg(node_p node, item_p item, hook_p lasthook)
379b06ebda0SMatthew Dillon {
380b06ebda0SMatthew Dillon 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
381b06ebda0SMatthew Dillon 	struct ng_mesg	*msg = NGI_MSG(item); /* item still has message */
382b06ebda0SMatthew Dillon 	int		 error = 0;
383b06ebda0SMatthew Dillon 
384b06ebda0SMatthew Dillon 	switch (msg->header.typecookie) {
385b06ebda0SMatthew Dillon 	case NGM_L2CAP_COOKIE:
386b06ebda0SMatthew Dillon 		switch (msg->header.cmd) {
387b06ebda0SMatthew Dillon 		/* L2CA_Connect */
388b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_CON:
389b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_con_req(l2cap, msg);
390b06ebda0SMatthew Dillon 			break;
391b06ebda0SMatthew Dillon 
392b06ebda0SMatthew Dillon 		/* L2CA_ConnectRsp */
393b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_CON_RSP:
394b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_con_rsp_req(l2cap, msg);
395b06ebda0SMatthew Dillon 			break;
396b06ebda0SMatthew Dillon 
397b06ebda0SMatthew Dillon 		/* L2CA_Config */
398b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_CFG:
399b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_cfg_req(l2cap, msg);
400b06ebda0SMatthew Dillon 			break;
401b06ebda0SMatthew Dillon 
402b06ebda0SMatthew Dillon 		/* L2CA_ConfigRsp */
403b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_CFG_RSP:
404b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_cfg_rsp_req(l2cap, msg);
405b06ebda0SMatthew Dillon 			break;
406b06ebda0SMatthew Dillon 
407b06ebda0SMatthew Dillon 		/* L2CA_Disconnect */
408b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_DISCON:
409b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_discon_req(l2cap, msg);
410b06ebda0SMatthew Dillon 			break;
411b06ebda0SMatthew Dillon 
412b06ebda0SMatthew Dillon 		/* L2CA_GroupCreate */
413b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_GRP_CREATE:
414b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_grp_create(l2cap, msg);
415b06ebda0SMatthew Dillon 			break;
416b06ebda0SMatthew Dillon 
417b06ebda0SMatthew Dillon 		/* L2CA_GroupClose */
418b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_GRP_CLOSE:
419b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_grp_close(l2cap, msg);
420b06ebda0SMatthew Dillon 			break;
421b06ebda0SMatthew Dillon 
422b06ebda0SMatthew Dillon 		/* L2CA_GroupAddMember */
423b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_GRP_ADD_MEMBER:
424b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_grp_add_member_req(l2cap, msg);
425b06ebda0SMatthew Dillon 			break;
426b06ebda0SMatthew Dillon 
427b06ebda0SMatthew Dillon 		/* L2CA_GroupDeleteMember */
428b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_GRP_REM_MEMBER:
429b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_grp_rem_member(l2cap, msg);
430b06ebda0SMatthew Dillon 			break;
431b06ebda0SMatthew Dillon 
432b06ebda0SMatthew Dillon 		/* L2CA_GroupMembership */
433b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_GRP_MEMBERSHIP:
434b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_grp_get_members(l2cap, msg);
435b06ebda0SMatthew Dillon 			break;
436b06ebda0SMatthew Dillon 
437b06ebda0SMatthew Dillon 		/* L2CA_Ping */
438b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_PING:
439b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_ping_req(l2cap, msg);
440b06ebda0SMatthew Dillon 			break;
441b06ebda0SMatthew Dillon 
442b06ebda0SMatthew Dillon 		/* L2CA_GetInfo */
443b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_GET_INFO:
444b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_get_info_req(l2cap, msg);
445b06ebda0SMatthew Dillon 			break;
446b06ebda0SMatthew Dillon 
447b06ebda0SMatthew Dillon 		/* L2CA_EnableCLT */
448b06ebda0SMatthew Dillon 		case NGM_L2CAP_L2CA_ENABLE_CLT:
449b06ebda0SMatthew Dillon 			error = ng_l2cap_l2ca_enable_clt(l2cap, msg);
450b06ebda0SMatthew Dillon 			break;
451b06ebda0SMatthew Dillon 
452b06ebda0SMatthew Dillon 		default:
453b06ebda0SMatthew Dillon 			return (ng_l2cap_default_rcvmsg(node, item, lasthook));
454b06ebda0SMatthew Dillon 			/* NOT REACHED */
455b06ebda0SMatthew Dillon 		}
456b06ebda0SMatthew Dillon 		break;
457b06ebda0SMatthew Dillon 
458b06ebda0SMatthew Dillon 	default:
459b06ebda0SMatthew Dillon 		return (ng_l2cap_default_rcvmsg(node, item, lasthook));
460b06ebda0SMatthew Dillon 		/* NOT REACHED */
461b06ebda0SMatthew Dillon 	}
462b06ebda0SMatthew Dillon 
463b06ebda0SMatthew Dillon 	NG_FREE_ITEM(item);
464b06ebda0SMatthew Dillon 
465b06ebda0SMatthew Dillon 	return (error);
466b06ebda0SMatthew Dillon } /* ng_l2cap_upper_rcvmsg */
467b06ebda0SMatthew Dillon 
468b06ebda0SMatthew Dillon /*
469b06ebda0SMatthew Dillon  * Default control message processing routine
470b06ebda0SMatthew Dillon  */
471b06ebda0SMatthew Dillon 
472b06ebda0SMatthew Dillon static int
ng_l2cap_default_rcvmsg(node_p node,item_p item,hook_p lasthook)473b06ebda0SMatthew Dillon ng_l2cap_default_rcvmsg(node_p node, item_p item, hook_p lasthook)
474b06ebda0SMatthew Dillon {
475b06ebda0SMatthew Dillon 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
476b06ebda0SMatthew Dillon 	struct ng_mesg	*msg = NULL, *rsp = NULL;
477b06ebda0SMatthew Dillon 	int		 error = 0;
478b06ebda0SMatthew Dillon 
479b06ebda0SMatthew Dillon 	/* Detach and process message */
480b06ebda0SMatthew Dillon 	NGI_GET_MSG(item, msg);
481b06ebda0SMatthew Dillon 
482b06ebda0SMatthew Dillon 	switch (msg->header.typecookie) {
483b06ebda0SMatthew Dillon 	case NGM_GENERIC_COOKIE:
484b06ebda0SMatthew Dillon 		switch (msg->header.cmd) {
485b06ebda0SMatthew Dillon 		case NGM_TEXT_STATUS:
4865a975a3dSMatthew Dillon 			NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK);
487b06ebda0SMatthew Dillon 			if (rsp == NULL)
488b06ebda0SMatthew Dillon 				error = ENOMEM;
489b06ebda0SMatthew Dillon 			else
490a62226e4SSascha Wildner 				ksnprintf(rsp->data, NG_TEXTRESPONSE,
491b06ebda0SMatthew Dillon 					"bdaddr %x:%x:%x:%x:%x:%x, " \
492b06ebda0SMatthew Dillon 					"pkt_size %d\n" \
493b06ebda0SMatthew Dillon 					"Hooks %s %s %s\n" \
494b06ebda0SMatthew Dillon 					"Flags %#x\n",
495b06ebda0SMatthew Dillon 					l2cap->bdaddr.b[5], l2cap->bdaddr.b[4],
496b06ebda0SMatthew Dillon 					l2cap->bdaddr.b[3], l2cap->bdaddr.b[2],
497b06ebda0SMatthew Dillon 					l2cap->bdaddr.b[1], l2cap->bdaddr.b[0],
498b06ebda0SMatthew Dillon 					l2cap->pkt_size,
499b06ebda0SMatthew Dillon 					(l2cap->hci != NULL)?
500b06ebda0SMatthew Dillon 						NG_L2CAP_HOOK_HCI : "",
501b06ebda0SMatthew Dillon 					(l2cap->l2c != NULL)?
502b06ebda0SMatthew Dillon 						NG_L2CAP_HOOK_L2C : "",
503b06ebda0SMatthew Dillon 					(l2cap->ctl != NULL)?
504b06ebda0SMatthew Dillon 						NG_L2CAP_HOOK_CTL : "",
505b06ebda0SMatthew Dillon 					l2cap->flags);
506b06ebda0SMatthew Dillon 			break;
507b06ebda0SMatthew Dillon 
508b06ebda0SMatthew Dillon 		default:
509b06ebda0SMatthew Dillon 			error = EINVAL;
510b06ebda0SMatthew Dillon 			break;
511b06ebda0SMatthew Dillon 		}
512b06ebda0SMatthew Dillon 		break;
513b06ebda0SMatthew Dillon 
514b06ebda0SMatthew Dillon 	/* Messages from the upper layer or directed to the local node */
515b06ebda0SMatthew Dillon 	case NGM_L2CAP_COOKIE:
516b06ebda0SMatthew Dillon 		switch (msg->header.cmd) {
517b06ebda0SMatthew Dillon 		/* Get node flags */
518b06ebda0SMatthew Dillon 		case NGM_L2CAP_NODE_GET_FLAGS:
519b06ebda0SMatthew Dillon 			NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_flags_ep),
5205a975a3dSMatthew Dillon 				M_WAITOK | M_NULLOK);
521b06ebda0SMatthew Dillon 			if (rsp == NULL)
522b06ebda0SMatthew Dillon 				error = ENOMEM;
523b06ebda0SMatthew Dillon 			else
524b06ebda0SMatthew Dillon 				*((ng_l2cap_node_flags_ep *)(rsp->data)) =
525b06ebda0SMatthew Dillon 					l2cap->flags;
526b06ebda0SMatthew Dillon 			break;
527b06ebda0SMatthew Dillon 
528b06ebda0SMatthew Dillon 		/* Get node debug */
529b06ebda0SMatthew Dillon 		case NGM_L2CAP_NODE_GET_DEBUG:
530b06ebda0SMatthew Dillon 			NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_debug_ep),
5315a975a3dSMatthew Dillon 				M_WAITOK | M_NULLOK);
532b06ebda0SMatthew Dillon 			if (rsp == NULL)
533b06ebda0SMatthew Dillon 				error = ENOMEM;
534b06ebda0SMatthew Dillon 			else
535b06ebda0SMatthew Dillon 				*((ng_l2cap_node_debug_ep *)(rsp->data)) =
536b06ebda0SMatthew Dillon 					l2cap->debug;
537b06ebda0SMatthew Dillon 			break;
538b06ebda0SMatthew Dillon 
539b06ebda0SMatthew Dillon 		/* Set node debug */
540b06ebda0SMatthew Dillon 		case NGM_L2CAP_NODE_SET_DEBUG:
541b06ebda0SMatthew Dillon 			if (msg->header.arglen !=
542b06ebda0SMatthew Dillon 					sizeof(ng_l2cap_node_debug_ep))
543b06ebda0SMatthew Dillon 				error = EMSGSIZE;
544b06ebda0SMatthew Dillon 			else
545b06ebda0SMatthew Dillon 				l2cap->debug =
546b06ebda0SMatthew Dillon 					*((ng_l2cap_node_debug_ep *)(msg->data));
547b06ebda0SMatthew Dillon 			break;
548b06ebda0SMatthew Dillon 
549b06ebda0SMatthew Dillon 		/* Get connection list */
550b06ebda0SMatthew Dillon 		case NGM_L2CAP_NODE_GET_CON_LIST: {
551b06ebda0SMatthew Dillon 			ng_l2cap_con_p			 con = NULL;
552b06ebda0SMatthew Dillon 			ng_l2cap_node_con_list_ep	*e1 = NULL;
553b06ebda0SMatthew Dillon 			ng_l2cap_node_con_ep		*e2 = NULL;
554b06ebda0SMatthew Dillon 			int				 n = 0;
555b06ebda0SMatthew Dillon 
556b06ebda0SMatthew Dillon 			/* Count number of connections */
557b06ebda0SMatthew Dillon 			LIST_FOREACH(con, &l2cap->con_list, next)
558b06ebda0SMatthew Dillon 				n++;
559b06ebda0SMatthew Dillon 			if (n > NG_L2CAP_MAX_CON_NUM)
560b06ebda0SMatthew Dillon 				n = NG_L2CAP_MAX_CON_NUM;
561b06ebda0SMatthew Dillon 
562b06ebda0SMatthew Dillon 			/* Prepare response */
563b06ebda0SMatthew Dillon 			NG_MKRESPONSE(rsp, msg,
5645a975a3dSMatthew Dillon 				sizeof(*e1) + n * sizeof(*e2), M_WAITOK | M_NULLOK);
565b06ebda0SMatthew Dillon 			if (rsp == NULL) {
566b06ebda0SMatthew Dillon 				error = ENOMEM;
567b06ebda0SMatthew Dillon 				break;
568b06ebda0SMatthew Dillon 			}
569b06ebda0SMatthew Dillon 
570b06ebda0SMatthew Dillon 			e1 = (ng_l2cap_node_con_list_ep *)(rsp->data);
571b06ebda0SMatthew Dillon 			e2 = (ng_l2cap_node_con_ep *)(e1 + 1);
572b06ebda0SMatthew Dillon 
573b06ebda0SMatthew Dillon 			e1->num_connections = n;
574b06ebda0SMatthew Dillon 
575b06ebda0SMatthew Dillon 			LIST_FOREACH(con, &l2cap->con_list, next) {
576b06ebda0SMatthew Dillon 				e2->state = con->state;
577b06ebda0SMatthew Dillon 
578b06ebda0SMatthew Dillon 				e2->flags = con->flags;
579b06ebda0SMatthew Dillon 				if (con->tx_pkt != NULL)
580b06ebda0SMatthew Dillon 					e2->flags |= NG_L2CAP_CON_TX;
581b06ebda0SMatthew Dillon 				if (con->rx_pkt != NULL)
582b06ebda0SMatthew Dillon 					e2->flags |= NG_L2CAP_CON_RX;
583b06ebda0SMatthew Dillon 
584b06ebda0SMatthew Dillon 				e2->pending = con->pending;
585b06ebda0SMatthew Dillon 
586b06ebda0SMatthew Dillon 				e2->con_handle = con->con_handle;
587b06ebda0SMatthew Dillon 				bcopy(&con->remote, &e2->remote,
588b06ebda0SMatthew Dillon 					sizeof(e2->remote));
589b06ebda0SMatthew Dillon 
590b06ebda0SMatthew Dillon 				e2 ++;
591b06ebda0SMatthew Dillon 				if (--n <= 0)
592b06ebda0SMatthew Dillon 					break;
593b06ebda0SMatthew Dillon 			}
594b06ebda0SMatthew Dillon 			} break;
595b06ebda0SMatthew Dillon 
596b06ebda0SMatthew Dillon 		/* Get channel list */
597b06ebda0SMatthew Dillon 		case NGM_L2CAP_NODE_GET_CHAN_LIST: {
598b06ebda0SMatthew Dillon 			ng_l2cap_chan_p			 ch = NULL;
599b06ebda0SMatthew Dillon 			ng_l2cap_node_chan_list_ep	*e1 = NULL;
600b06ebda0SMatthew Dillon 			ng_l2cap_node_chan_ep		*e2 = NULL;
601b06ebda0SMatthew Dillon 			int				 n = 0;
602b06ebda0SMatthew Dillon 
603b06ebda0SMatthew Dillon 			/* Count number of channels */
604b06ebda0SMatthew Dillon 			LIST_FOREACH(ch, &l2cap->chan_list, next)
605b06ebda0SMatthew Dillon 				n ++;
606b06ebda0SMatthew Dillon 			if (n > NG_L2CAP_MAX_CHAN_NUM)
607b06ebda0SMatthew Dillon 				n = NG_L2CAP_MAX_CHAN_NUM;
608b06ebda0SMatthew Dillon 
609b06ebda0SMatthew Dillon 			/* Prepare response */
610b06ebda0SMatthew Dillon 			NG_MKRESPONSE(rsp, msg,
611b06ebda0SMatthew Dillon 				sizeof(ng_l2cap_node_chan_list_ep) +
6125a975a3dSMatthew Dillon 				n * sizeof(ng_l2cap_node_chan_ep), M_WAITOK | M_NULLOK);
613b06ebda0SMatthew Dillon 			if (rsp == NULL) {
614b06ebda0SMatthew Dillon 				error = ENOMEM;
615b06ebda0SMatthew Dillon 				break;
616b06ebda0SMatthew Dillon 			}
617b06ebda0SMatthew Dillon 
618b06ebda0SMatthew Dillon 			e1 = (ng_l2cap_node_chan_list_ep *)(rsp->data);
619b06ebda0SMatthew Dillon 			e2 = (ng_l2cap_node_chan_ep *)(e1 + 1);
620b06ebda0SMatthew Dillon 
621b06ebda0SMatthew Dillon 			e1->num_channels = n;
622b06ebda0SMatthew Dillon 
623b06ebda0SMatthew Dillon 			LIST_FOREACH(ch, &l2cap->chan_list, next) {
624b06ebda0SMatthew Dillon 				e2->state = ch->state;
625b06ebda0SMatthew Dillon 
626b06ebda0SMatthew Dillon 				e2->scid = ch->scid;
627b06ebda0SMatthew Dillon 				e2->dcid = ch->dcid;
628b06ebda0SMatthew Dillon 
629b06ebda0SMatthew Dillon 				e2->imtu = ch->imtu;
630b06ebda0SMatthew Dillon 				e2->omtu = ch->omtu;
631b06ebda0SMatthew Dillon 
632b06ebda0SMatthew Dillon 				e2->psm = ch->psm;
633b06ebda0SMatthew Dillon 				bcopy(&ch->con->remote, &e2->remote,
634b06ebda0SMatthew Dillon 					sizeof(e2->remote));
635b06ebda0SMatthew Dillon 
636b06ebda0SMatthew Dillon 				e2 ++;
637b06ebda0SMatthew Dillon 				if (--n <= 0)
638b06ebda0SMatthew Dillon 					break;
639b06ebda0SMatthew Dillon 			}
640b06ebda0SMatthew Dillon 			} break;
641b06ebda0SMatthew Dillon 
642b06ebda0SMatthew Dillon 		case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
643b06ebda0SMatthew Dillon 			NG_MKRESPONSE(rsp, msg,
6445a975a3dSMatthew Dillon 				sizeof(ng_l2cap_node_auto_discon_ep), M_WAITOK | M_NULLOK);
645b06ebda0SMatthew Dillon 			if (rsp == NULL)
646b06ebda0SMatthew Dillon 				error = ENOMEM;
647b06ebda0SMatthew Dillon 			else
648b06ebda0SMatthew Dillon 				*((ng_l2cap_node_auto_discon_ep *)(rsp->data)) =
649b06ebda0SMatthew Dillon 					l2cap->discon_timo;
650b06ebda0SMatthew Dillon 			break;
651b06ebda0SMatthew Dillon 
652b06ebda0SMatthew Dillon 		case NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO:
653b06ebda0SMatthew Dillon 			if (msg->header.arglen !=
654b06ebda0SMatthew Dillon 					sizeof(ng_l2cap_node_auto_discon_ep))
655b06ebda0SMatthew Dillon 				error = EMSGSIZE;
656b06ebda0SMatthew Dillon 			else
657b06ebda0SMatthew Dillon 				l2cap->discon_timo =
658b06ebda0SMatthew Dillon 					*((ng_l2cap_node_auto_discon_ep *)
659b06ebda0SMatthew Dillon 							(msg->data));
660b06ebda0SMatthew Dillon 			break;
661b06ebda0SMatthew Dillon 
662b06ebda0SMatthew Dillon 		default:
663b06ebda0SMatthew Dillon 			error = EINVAL;
664b06ebda0SMatthew Dillon 			break;
665b06ebda0SMatthew Dillon 		}
666b06ebda0SMatthew Dillon 		break;
667b06ebda0SMatthew Dillon 
668b06ebda0SMatthew Dillon 	default:
669b06ebda0SMatthew Dillon 		error = EINVAL;
670b06ebda0SMatthew Dillon 		break;
671b06ebda0SMatthew Dillon 	}
672b06ebda0SMatthew Dillon 
673b06ebda0SMatthew Dillon 	NG_RESPOND_MSG(error, node, item, rsp);
674b06ebda0SMatthew Dillon 	NG_FREE_MSG(msg);
675b06ebda0SMatthew Dillon 
676b06ebda0SMatthew Dillon 	return (error);
677b06ebda0SMatthew Dillon } /* ng_l2cap_rcvmsg */
678b06ebda0SMatthew Dillon 
679b06ebda0SMatthew Dillon /*
680b06ebda0SMatthew Dillon  * Process data packet from one of our hooks.
681b06ebda0SMatthew Dillon  *
682b06ebda0SMatthew Dillon  * From the HCI hook we expect to receive ACL data packets. ACL data packets
683b06ebda0SMatthew Dillon  * gets re-assembled into one L2CAP packet (according to length) and then gets
684b06ebda0SMatthew Dillon  * processed.
685b06ebda0SMatthew Dillon  *
686b06ebda0SMatthew Dillon  * NOTE: We expect to receive L2CAP packet header in the first fragment.
687b06ebda0SMatthew Dillon  *       Otherwise we WILL NOT be able to get length of the L2CAP packet.
688b06ebda0SMatthew Dillon  *
689b06ebda0SMatthew Dillon  * Signaling L2CAP packets (destination channel ID == 0x1) are processed within
690b06ebda0SMatthew Dillon  * the node. Connectionless data packets (destination channel ID == 0x2) will
691b06ebda0SMatthew Dillon  * be forwarded to appropriate upstream hook unless it is not connected or
692b06ebda0SMatthew Dillon  * connectionless traffic for the specified PSM was disabled.
693b06ebda0SMatthew Dillon  *
694b06ebda0SMatthew Dillon  * From the upstream hooks we expect to receive data packets. These data
695b06ebda0SMatthew Dillon  * packets will be converted into L2CAP data packets. The length of each
696b06ebda0SMatthew Dillon  * L2CAP packet must not exceed channel's omtu (our peer's imtu). Then
697b06ebda0SMatthew Dillon  * these L2CAP packets will be converted to ACL data packets (according to
698b06ebda0SMatthew Dillon  * HCI layer MTU) and sent to lower layer.
699b06ebda0SMatthew Dillon  *
700b06ebda0SMatthew Dillon  * No data is expected from the control hook.
701b06ebda0SMatthew Dillon  */
702b06ebda0SMatthew Dillon 
703b06ebda0SMatthew Dillon static int
ng_l2cap_rcvdata(hook_p hook,item_p item)704b06ebda0SMatthew Dillon ng_l2cap_rcvdata(hook_p hook, item_p item)
705b06ebda0SMatthew Dillon {
706b06ebda0SMatthew Dillon 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
707b06ebda0SMatthew Dillon 	struct mbuf	*m = NULL;
708b06ebda0SMatthew Dillon 	int		 error = 0;
709b06ebda0SMatthew Dillon 
710b06ebda0SMatthew Dillon 	/* Detach mbuf, discard item and process data */
711b06ebda0SMatthew Dillon 	NGI_GET_M(item, m);
712b06ebda0SMatthew Dillon 	NG_FREE_ITEM(item);
713b06ebda0SMatthew Dillon 
714b06ebda0SMatthew Dillon 	if (hook == l2cap->hci)
715b06ebda0SMatthew Dillon 		error = ng_l2cap_lp_receive(l2cap, m);
716b06ebda0SMatthew Dillon 	else if (hook == l2cap->l2c)
717b06ebda0SMatthew Dillon 		error = ng_l2cap_l2ca_write_req(l2cap, m);
718b06ebda0SMatthew Dillon 	else {
719b06ebda0SMatthew Dillon 		NG_FREE_M(m);
720b06ebda0SMatthew Dillon 		error = EINVAL;
721b06ebda0SMatthew Dillon 	}
722b06ebda0SMatthew Dillon 
723b06ebda0SMatthew Dillon 	return (error);
724b06ebda0SMatthew Dillon } /* ng_l2cap_rcvdata */
725b06ebda0SMatthew Dillon 
726b06ebda0SMatthew Dillon /*
727b06ebda0SMatthew Dillon  * Clean all connections, channels and commands for the L2CAP node
728b06ebda0SMatthew Dillon  */
729b06ebda0SMatthew Dillon 
730b06ebda0SMatthew Dillon static void
ng_l2cap_cleanup(ng_l2cap_p l2cap)731b06ebda0SMatthew Dillon ng_l2cap_cleanup(ng_l2cap_p l2cap)
732b06ebda0SMatthew Dillon {
733b06ebda0SMatthew Dillon 	ng_l2cap_con_p	con = NULL;
734b06ebda0SMatthew Dillon 
735b06ebda0SMatthew Dillon 	/* Clean up connection and channels */
736b06ebda0SMatthew Dillon 	while (!LIST_EMPTY(&l2cap->con_list)) {
737b06ebda0SMatthew Dillon 		con = LIST_FIRST(&l2cap->con_list);
738b06ebda0SMatthew Dillon 
739b06ebda0SMatthew Dillon 		if (con->flags & NG_L2CAP_CON_LP_TIMO)
740b06ebda0SMatthew Dillon 			ng_l2cap_lp_untimeout(con);
741b06ebda0SMatthew Dillon 		else if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)
742b06ebda0SMatthew Dillon 			ng_l2cap_discon_untimeout(con);
743b06ebda0SMatthew Dillon 
744b06ebda0SMatthew Dillon 		/* Connection terminated by local host */
745b06ebda0SMatthew Dillon 		ng_l2cap_con_fail(con, 0x16);
746b06ebda0SMatthew Dillon 	}
747b06ebda0SMatthew Dillon } /* ng_l2cap_cleanup */
748b06ebda0SMatthew Dillon 
749b06ebda0SMatthew Dillon /*
750b06ebda0SMatthew Dillon  * Destroy all channels that use specified upstream hook
751b06ebda0SMatthew Dillon  */
752b06ebda0SMatthew Dillon 
753b06ebda0SMatthew Dillon static void
ng_l2cap_destroy_channels(ng_l2cap_p l2cap)754b06ebda0SMatthew Dillon ng_l2cap_destroy_channels(ng_l2cap_p l2cap)
755b06ebda0SMatthew Dillon {
756b06ebda0SMatthew Dillon 	while (!LIST_EMPTY(&l2cap->chan_list))
757b06ebda0SMatthew Dillon 		ng_l2cap_free_chan(LIST_FIRST(&l2cap->chan_list));
758b06ebda0SMatthew Dillon } /* ng_l2cap_destroy_channels_by_hook */
759b06ebda0SMatthew Dillon 
760