xref: /freebsd/sys/netgraph/bluetooth/l2cap/ng_l2cap_main.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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.2 2003/04/28 21:44:59 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 <netgraph/bluetooth/include/ng_bluetooth.h>
42 #include <netgraph/bluetooth/include/ng_hci.h>
43 #include <netgraph/bluetooth/include/ng_l2cap.h>
44 #include <netgraph/bluetooth/l2cap/ng_l2cap_var.h>
45 #include <netgraph/bluetooth/l2cap/ng_l2cap_cmds.h>
46 #include <netgraph/bluetooth/l2cap/ng_l2cap_evnt.h>
47 #include <netgraph/bluetooth/l2cap/ng_l2cap_llpi.h>
48 #include <netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h>
49 #include <netgraph/bluetooth/l2cap/ng_l2cap_misc.h>
50 #include <netgraph/bluetooth/l2cap/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 	.version =	NG_ABI_VERSION,
80 	.name =		NG_L2CAP_NODE_TYPE,
81 	.constructor =	ng_l2cap_constructor,
82 	.rcvmsg =	ng_l2cap_default_rcvmsg,
83 	.shutdown =	ng_l2cap_shutdown,
84 	.newhook =	ng_l2cap_newhook,
85 	.connect =	ng_l2cap_connect,
86 	.rcvdata =	ng_l2cap_rcvdata,
87 	.disconnect =	ng_l2cap_disconnect,
88 	.cmdlist =	ng_l2cap_cmdlist,
89 };
90 NETGRAPH_INIT(l2cap, &typestruct);
91 MODULE_VERSION(ng_l2cap, NG_BLUETOOTH_VERSION);
92 MODULE_DEPEND(ng_l2cap, ng_bluetooth, NG_BLUETOOTH_VERSION,
93         NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
94 
95 /*****************************************************************************
96  *****************************************************************************
97  **                   Netgraph methods implementation
98  *****************************************************************************
99  *****************************************************************************/
100 
101 static void ng_l2cap_cleanup          (ng_l2cap_p);
102 static void ng_l2cap_destroy_channels (ng_l2cap_p);
103 
104 /*
105  * Create new instance of L2CAP node
106  */
107 
108 static int
109 ng_l2cap_constructor(node_p node)
110 {
111 	ng_l2cap_p	l2cap = NULL;
112 
113 	/* Create new L2CAP node */
114 	MALLOC(l2cap, ng_l2cap_p, sizeof(*l2cap),
115 		M_NETGRAPH_L2CAP, M_NOWAIT|M_ZERO);
116 	if (l2cap == NULL)
117 		return (ENOMEM);
118 
119 	l2cap->node = node;
120 	l2cap->debug = NG_L2CAP_WARN_LEVEL;
121 	l2cap->discon_timo = 5; /* sec */
122 
123 	LIST_INIT(&l2cap->con_list);
124 	LIST_INIT(&l2cap->chan_list);
125 
126 	NG_NODE_SET_PRIVATE(node, l2cap);
127 	NG_NODE_FORCE_WRITER(node);
128 
129 	return (0);
130 } /* ng_l2cap_constructor */
131 
132 /*
133  * Shutdown L2CAP node
134  */
135 
136 static int
137 ng_l2cap_shutdown(node_p node)
138 {
139 	ng_l2cap_p	l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
140 
141 	NG_NODE_SET_PRIVATE(node, NULL);
142 	NG_NODE_UNREF(node);
143 
144 	/* Clean up L2CAP node. Delete all connection, channels and commands */
145 	l2cap->node = NULL;
146 	ng_l2cap_cleanup(l2cap);
147 
148 	bzero(l2cap, sizeof(*l2cap));
149 	FREE(l2cap, M_NETGRAPH_L2CAP);
150 
151 	return (0);
152 } /* ng_l2cap_shutdown */
153 
154 /*
155  * Give our OK for a hook to be added. HCI layer is connected to the HCI
156  * (NG_L2CAP_HOOK_HCI) hook. As per specification L2CAP layer MUST provide
157  * Procol/Service Multiplexing, so the L2CAP node provides separate hooks
158  * for SDP (NG_L2CAP_HOOK_SDP), RFCOMM (NG_L2CAP_HOOK_RFCOMM) and TCP
159  * (NG_L2CAP_HOOK_TCP) protcols. Unknown PSM will be forwarded to
160  * NG_L2CAP_HOOK_ORPHAN hook. Control node/application is connected to
161  * control (NG_L2CAP_HOOK_CTL) hook.
162  */
163 
164 static int
165 ng_l2cap_newhook(node_p node, hook_p hook, char const *name)
166 {
167 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
168 	hook_p		*h = NULL;
169 
170 	if (strcmp(name, NG_L2CAP_HOOK_HCI) == 0)
171 		h = &l2cap->hci;
172 	else if (strcmp(name, NG_L2CAP_HOOK_L2C) == 0)
173 		h = &l2cap->l2c;
174 	else if (strcmp(name, NG_L2CAP_HOOK_CTL) == 0)
175 		h = &l2cap->ctl;
176 	else
177 		return (EINVAL);
178 
179 	if (*h != NULL)
180 		return (EISCONN);
181 
182 	*h = hook;
183 
184 	return (0);
185 } /* ng_l2cap_newhook */
186 
187 /*
188  * Give our final OK to connect hook. Nothing to do here.
189  */
190 
191 static int
192 ng_l2cap_connect(hook_p hook)
193 {
194 	ng_l2cap_p	l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
195 	int		error = 0;
196 
197 	if (hook == l2cap->hci)
198 		NG_HOOK_SET_RCVMSG(hook, ng_l2cap_lower_rcvmsg);
199 	else
200 	if (hook == l2cap->l2c || hook == l2cap->ctl) {
201 		NG_HOOK_SET_RCVMSG(hook, ng_l2cap_upper_rcvmsg);
202 
203 		/* Send delayed notification to the upper layer */
204 		error = ng_send_fn(l2cap->node, hook, ng_l2cap_send_hook_info,
205 				NULL, 0);
206 	} else
207 		error = EINVAL;
208 
209 	return (error);
210 } /* ng_l2cap_connect */
211 
212 /*
213  * Disconnect the hook. For downstream hook we must notify upper layers.
214  *
215  * XXX For upstream hooks this is really ugly :( Hook was disconnected and it
216  * XXX is now too late to do anything. For now we just clean up our own mess
217  * XXX and remove all channels that use disconnected upstream hook. If we don't
218  * XXX do that then L2CAP node can get out of sync with upper layers.
219  * XXX No notification will be sent to remote peer.
220  */
221 
222 static int
223 ng_l2cap_disconnect(hook_p hook)
224 {
225 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
226 	hook_p		*h = NULL;
227 
228 	if (hook == l2cap->hci) {
229 		ng_l2cap_cleanup(l2cap);
230 		h = &l2cap->hci;
231 	} else
232 	if (hook == l2cap->l2c) {
233 		ng_l2cap_destroy_channels(l2cap);
234 		h = &l2cap->l2c;
235 	} else
236 	if (hook == l2cap->ctl)
237 		h = &l2cap->ctl;
238 	else
239 		return (EINVAL);
240 
241 	*h = NULL;
242 
243 	/* Shutdown when all hooks are disconnected */
244 	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
245 	    NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
246 		ng_rmnode_self(NG_HOOK_NODE(hook));
247 
248 	return (0);
249 } /* ng_l2cap_disconnect */
250 
251 /*
252  * Process control message from lower layer
253  */
254 
255 static int
256 ng_l2cap_lower_rcvmsg(node_p node, item_p item, hook_p lasthook)
257 {
258 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
259 	struct ng_mesg	*msg = NGI_MSG(item); /* item still has message */
260 	int		 error = 0;
261 
262 	switch (msg->header.typecookie) {
263 	case NGM_HCI_COOKIE:
264 		switch (msg->header.cmd) {
265 		/* HCI node is ready */
266 		case NGM_HCI_NODE_UP: {
267 			ng_hci_node_up_ep	*ep = NULL;
268 
269 			if (msg->header.arglen != sizeof(*ep))
270 				error = EMSGSIZE;
271 			else {
272 				ep = (ng_hci_node_up_ep *)(msg->data);
273 
274 				NG_L2CAP_INFO(
275 "%s: %s - HCI node is up, bdaddr: %x:%x:%x:%x:%x:%x, " \
276 "pkt_size=%d bytes, num_pkts=%d\n",	__func__, NG_NODE_NAME(l2cap->node),
277 					ep->bdaddr.b[5], ep->bdaddr.b[4],
278 					ep->bdaddr.b[3], ep->bdaddr.b[2],
279 					ep->bdaddr.b[1], ep->bdaddr.b[0],
280 					ep->pkt_size, ep->num_pkts);
281 
282 				bcopy(&ep->bdaddr, &l2cap->bdaddr,
283 					sizeof(l2cap->bdaddr));
284 				l2cap->pkt_size = ep->pkt_size;
285 				l2cap->num_pkts = ep->num_pkts;
286 
287 				/* Notify upper layers */
288 				ng_l2cap_send_hook_info(l2cap->node,
289 					l2cap->l2c, NULL, 0);
290 				ng_l2cap_send_hook_info(l2cap->node,
291 					l2cap->ctl, NULL, 0);
292 			}
293 			} break;
294 
295 		case NGM_HCI_SYNC_CON_QUEUE: {
296 			ng_hci_sync_con_queue_ep	*ep = NULL;
297 			ng_l2cap_con_p			 con = NULL;
298 
299 			if (msg->header.arglen != sizeof(*ep))
300 				error = EMSGSIZE;
301 			else {
302 				ep = (ng_hci_sync_con_queue_ep *)(msg->data);
303 				con = ng_l2cap_con_by_handle(l2cap,
304 							ep->con_handle);
305 				if (con == NULL)
306 					break;
307 
308 				NG_L2CAP_INFO(
309 "%s: %s - sync HCI connection queue, con_handle=%d, pending=%d, completed=%d\n",
310 					 __func__, NG_NODE_NAME(l2cap->node),
311 					ep->con_handle, con->pending,
312 					ep->completed);
313 
314 				con->pending -= ep->completed;
315 				if (con->pending < 0) {
316 					NG_L2CAP_WARN(
317 "%s: %s - pending packet counter is out of sync! " \
318 "con_handle=%d, pending=%d, completed=%d\n",	__func__,
319 						NG_NODE_NAME(l2cap->node),
320 						con->con_handle, con->pending,
321 						ep->completed);
322 
323 					con->pending = 0;
324 				}
325 
326 				ng_l2cap_lp_deliver(con);
327 			}
328 			} break;
329 
330 		/* LP_ConnectCfm[Neg] */
331 		case NGM_HCI_LP_CON_CFM:
332 			error = ng_l2cap_lp_con_cfm(l2cap, msg);
333 			break;
334 
335 		/* LP_ConnectInd */
336 		case NGM_HCI_LP_CON_IND:
337 			error = ng_l2cap_lp_con_ind(l2cap, msg);
338 			break;
339 
340 		/* LP_DisconnectInd */
341 		case NGM_HCI_LP_DISCON_IND:
342 			error = ng_l2cap_lp_discon_ind(l2cap, msg);
343 			break;
344 
345 		/* LP_QoSSetupCfm[Neg] */
346 		case NGM_HCI_LP_QOS_CFM:
347 			error = ng_l2cap_lp_qos_cfm(l2cap, msg);
348 			break;
349 
350 		/* LP_OoSViolationInd */
351 		case NGM_HCI_LP_QOS_IND:
352 			error = ng_l2cap_lp_qos_ind(l2cap, msg);
353 			break;
354 
355 		default:
356 			error = EINVAL;
357 			break;
358 		}
359 		break;
360 
361 	default:
362 		return (ng_l2cap_default_rcvmsg(node, item, lasthook));
363 		/* NOT REACHED */
364 	}
365 
366 	NG_FREE_ITEM(item);
367 
368 	return (error);
369 } /* ng_l2cap_lower_rcvmsg */
370 
371 /*
372  * Process control message from upper layer
373  */
374 
375 static int
376 ng_l2cap_upper_rcvmsg(node_p node, item_p item, hook_p lasthook)
377 {
378 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
379 	struct ng_mesg	*msg = NGI_MSG(item); /* item still has message */
380 	int		 error = 0;
381 
382 	switch (msg->header.typecookie) {
383 	case NGM_L2CAP_COOKIE:
384 		switch (msg->header.cmd) {
385 		/* L2CA_Connect */
386 		case NGM_L2CAP_L2CA_CON:
387 			error = ng_l2cap_l2ca_con_req(l2cap, msg);
388 			break;
389 
390 		/* L2CA_ConnectRsp */
391 		case NGM_L2CAP_L2CA_CON_RSP:
392 			error = ng_l2cap_l2ca_con_rsp_req(l2cap, msg);
393 			break;
394 
395 		/* L2CA_Config */
396 		case NGM_L2CAP_L2CA_CFG:
397 			error = ng_l2cap_l2ca_cfg_req(l2cap, msg);
398 			break;
399 
400 		/* L2CA_ConfigRsp */
401 		case NGM_L2CAP_L2CA_CFG_RSP:
402 			error = ng_l2cap_l2ca_cfg_rsp_req(l2cap, msg);
403 			break;
404 
405 		/* L2CA_Disconnect */
406 		case NGM_L2CAP_L2CA_DISCON:
407 			error = ng_l2cap_l2ca_discon_req(l2cap, msg);
408 			break;
409 
410 		/* L2CA_GroupCreate */
411 		case NGM_L2CAP_L2CA_GRP_CREATE:
412 			error = ng_l2cap_l2ca_grp_create(l2cap, msg);
413 			break;
414 
415 		/* L2CA_GroupClose */
416 		case NGM_L2CAP_L2CA_GRP_CLOSE:
417 			error = ng_l2cap_l2ca_grp_close(l2cap, msg);
418 			break;
419 
420 		/* L2CA_GroupAddMember */
421 		case NGM_L2CAP_L2CA_GRP_ADD_MEMBER:
422 			error = ng_l2cap_l2ca_grp_add_member_req(l2cap, msg);
423 			break;
424 
425 		/* L2CA_GroupDeleteMember */
426 		case NGM_L2CAP_L2CA_GRP_REM_MEMBER:
427 			error = ng_l2cap_l2ca_grp_rem_member(l2cap, msg);
428 			break;
429 
430 		/* L2CA_GroupMembership */
431 		case NGM_L2CAP_L2CA_GRP_MEMBERSHIP:
432 			error = ng_l2cap_l2ca_grp_get_members(l2cap, msg);
433 			break;
434 
435 		/* L2CA_Ping */
436 		case NGM_L2CAP_L2CA_PING:
437 			error = ng_l2cap_l2ca_ping_req(l2cap, msg);
438 			break;
439 
440 		/* L2CA_GetInfo */
441 		case NGM_L2CAP_L2CA_GET_INFO:
442 			error = ng_l2cap_l2ca_get_info_req(l2cap, msg);
443 			break;
444 
445 		/* L2CA_EnableCLT */
446 		case NGM_L2CAP_L2CA_ENABLE_CLT:
447 			error = ng_l2cap_l2ca_enable_clt(l2cap, msg);
448 			break;
449 
450 		default:
451 			return (ng_l2cap_default_rcvmsg(node, item, lasthook));
452 			/* NOT REACHED */
453 		}
454 		break;
455 
456 	default:
457 		return (ng_l2cap_default_rcvmsg(node, item, lasthook));
458 		/* NOT REACHED */
459 	}
460 
461 	NG_FREE_ITEM(item);
462 
463 	return (error);
464 } /* ng_l2cap_upper_rcvmsg */
465 
466 /*
467  * Default control message processing routine
468  */
469 
470 static int
471 ng_l2cap_default_rcvmsg(node_p node, item_p item, hook_p lasthook)
472 {
473 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
474 	struct ng_mesg	*msg = NULL, *rsp = NULL;
475 	int		 error = 0;
476 
477 	/* Detach and process message */
478 	NGI_GET_MSG(item, msg);
479 
480 	switch (msg->header.typecookie) {
481 	case NGM_GENERIC_COOKIE:
482 		switch (msg->header.cmd) {
483 		case NGM_TEXT_STATUS:
484 			NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT);
485 			if (rsp == NULL)
486 				error = ENOMEM;
487 			else
488 				snprintf(rsp->data, NG_TEXTRESPONSE,
489 					"bdaddr %x:%x:%x:%x:%x:%x, " \
490 					"pkt_size %d\n" \
491 					"Hooks %s %s %s\n" \
492 					"Flags %#x\n",
493 					l2cap->bdaddr.b[5], l2cap->bdaddr.b[4],
494 					l2cap->bdaddr.b[3], l2cap->bdaddr.b[2],
495 					l2cap->bdaddr.b[1], l2cap->bdaddr.b[0],
496 					l2cap->pkt_size,
497 					(l2cap->hci != NULL)?
498 						NG_L2CAP_HOOK_HCI : "",
499 					(l2cap->l2c != NULL)?
500 						NG_L2CAP_HOOK_L2C : "",
501 					(l2cap->ctl != NULL)?
502 						NG_L2CAP_HOOK_CTL : "",
503 					l2cap->flags);
504 			break;
505 
506 		default:
507 			error = EINVAL;
508 			break;
509 		}
510 		break;
511 
512 	/* Messages from the upper layer or directed to the local node */
513 	case NGM_L2CAP_COOKIE:
514 		switch (msg->header.cmd) {
515 		/* Get node flags */
516 		case NGM_L2CAP_NODE_GET_FLAGS:
517 			NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_flags_ep),
518 				M_NOWAIT);
519 			if (rsp == NULL)
520 				error = ENOMEM;
521 			else
522 				*((ng_l2cap_node_flags_ep *)(rsp->data)) =
523 					l2cap->flags;
524 			break;
525 
526 		/* Get node debug */
527 		case NGM_L2CAP_NODE_GET_DEBUG:
528 			NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_debug_ep),
529 				M_NOWAIT);
530 			if (rsp == NULL)
531 				error = ENOMEM;
532 			else
533 				*((ng_l2cap_node_debug_ep *)(rsp->data)) =
534 					l2cap->debug;
535 			break;
536 
537 		/* Set node debug */
538 		case NGM_L2CAP_NODE_SET_DEBUG:
539 			if (msg->header.arglen !=
540 					sizeof(ng_l2cap_node_debug_ep))
541 				error = EMSGSIZE;
542 			else
543 				l2cap->debug =
544 					*((ng_l2cap_node_debug_ep *)(msg->data));
545 			break;
546 
547 		/* Get connection list */
548 		case NGM_L2CAP_NODE_GET_CON_LIST: {
549 			ng_l2cap_con_p			 con = NULL;
550 			ng_l2cap_node_con_list_ep	*e1 = NULL;
551 			ng_l2cap_node_con_ep		*e2 = NULL;
552 			int				 n = 0;
553 
554 			/* Count number of connections */
555 			LIST_FOREACH(con, &l2cap->con_list, next)
556 				n++;
557 			if (n > NG_L2CAP_MAX_CON_NUM)
558 				n = NG_L2CAP_MAX_CON_NUM;
559 
560 			/* Prepare response */
561 			NG_MKRESPONSE(rsp, msg,
562 				sizeof(*e1) + n * sizeof(*e2), M_NOWAIT);
563 			if (rsp == NULL) {
564 				error = ENOMEM;
565 				break;
566 			}
567 
568 			e1 = (ng_l2cap_node_con_list_ep *)(rsp->data);
569 			e2 = (ng_l2cap_node_con_ep *)(e1 + 1);
570 
571 			e1->num_connections = n;
572 
573 			LIST_FOREACH(con, &l2cap->con_list, next) {
574 				e2->state = con->state;
575 
576 				e2->flags = con->flags;
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 		case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
641 			NG_MKRESPONSE(rsp, msg,
642 				sizeof(ng_l2cap_node_auto_discon_ep), M_NOWAIT);
643 			if (rsp == NULL)
644 				error = ENOMEM;
645 			else
646 				*((ng_l2cap_node_auto_discon_ep *)(rsp->data)) =
647 					l2cap->discon_timo;
648 			break;
649 
650 		case NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO:
651 			if (msg->header.arglen !=
652 					sizeof(ng_l2cap_node_auto_discon_ep))
653 				error = EMSGSIZE;
654 			else
655 				l2cap->discon_timo =
656 					*((ng_l2cap_node_auto_discon_ep *)
657 							(msg->data));
658 			break;
659 
660 		default:
661 			error = EINVAL;
662 			break;
663 		}
664 		break;
665 
666 	default:
667 		error = EINVAL;
668 		break;
669 	}
670 
671 	NG_RESPOND_MSG(error, node, item, rsp);
672 	NG_FREE_MSG(msg);
673 
674 	return (error);
675 } /* ng_l2cap_rcvmsg */
676 
677 /*
678  * Process data packet from one of our hooks.
679  *
680  * From the HCI hook we expect to receive ACL data packets. ACL data packets
681  * gets re-assembled into one L2CAP packet (according to length) and then gets
682  * processed.
683  *
684  * NOTE: We expect to receive L2CAP packet header in the first fragment.
685  *       Otherwise we WILL NOT be able to get length of the L2CAP packet.
686  *
687  * Signaling L2CAP packets (destination channel ID == 0x1) are processed within
688  * the node. Connectionless data packets (destination channel ID == 0x2) will
689  * be forwarded to appropriate upstream hook unless it is not connected or
690  * connectionless traffic for the specified PSM was disabled.
691  *
692  * From the upstream hooks we expect to receive data packets. These data
693  * packets will be converted into L2CAP data packets. The length of each
694  * L2CAP packet must not exceed channel's omtu (our peer's imtu). Then
695  * these L2CAP packets will be converted to ACL data packets (according to
696  * HCI layer MTU) and sent to lower layer.
697  *
698  * No data is expected from the control hook.
699  */
700 
701 static int
702 ng_l2cap_rcvdata(hook_p hook, item_p item)
703 {
704 	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
705 	struct mbuf	*m = NULL;
706 	int		 error = 0;
707 
708 	/* Detach mbuf, discard item and process data */
709 	NGI_GET_M(item, m);
710 	NG_FREE_ITEM(item);
711 
712 	if (hook == l2cap->hci)
713 		error = ng_l2cap_lp_receive(l2cap, m);
714 	else if (hook == l2cap->l2c)
715 		error = ng_l2cap_l2ca_write_req(l2cap, m);
716 	else {
717 		NG_FREE_M(m);
718 		error = EINVAL;
719 	}
720 
721 	return (error);
722 } /* ng_l2cap_rcvdata */
723 
724 /*
725  * Clean all connections, channels and commands for the L2CAP node
726  */
727 
728 static void
729 ng_l2cap_cleanup(ng_l2cap_p l2cap)
730 {
731 	ng_l2cap_con_p	con = NULL;
732 
733 	/* Clean up connection and channels */
734 	while (!LIST_EMPTY(&l2cap->con_list)) {
735 		con = LIST_FIRST(&l2cap->con_list);
736 
737 		if (con->flags & NG_L2CAP_CON_LP_TIMO)
738 			ng_l2cap_lp_untimeout(con);
739 		else if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)
740 			ng_l2cap_discon_untimeout(con);
741 
742 		/* Connection terminated by local host */
743 		ng_l2cap_con_fail(con, 0x16);
744 	}
745 } /* ng_l2cap_cleanup */
746 
747 /*
748  * Destroy all channels that use specified upstream hook
749  */
750 
751 static void
752 ng_l2cap_destroy_channels(ng_l2cap_p l2cap)
753 {
754 	while (!LIST_EMPTY(&l2cap->chan_list))
755 		ng_l2cap_free_chan(LIST_FIRST(&l2cap->chan_list));
756 } /* ng_l2cap_destroy_channels_by_hook */
757 
758