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