aboutsummaryrefslogtreecommitdiff
path: root/sys/netgraph/bluetooth/l2cap/ng_l2cap_main.c
blob: b438cf22337450c1587c92eba5b814278b218423 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
 * ng_l2cap_main.c
 */

/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: ng_l2cap_main.c,v 1.2 2003/04/28 21:44:59 max Exp $
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/queue.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
#include <netgraph/bluetooth/include/ng_bluetooth.h>
#include <netgraph/bluetooth/include/ng_hci.h>
#include <netgraph/bluetooth/include/ng_l2cap.h>
#include <netgraph/bluetooth/l2cap/ng_l2cap_var.h>
#include <netgraph/bluetooth/l2cap/ng_l2cap_cmds.h>
#include <netgraph/bluetooth/l2cap/ng_l2cap_evnt.h>
#include <netgraph/bluetooth/l2cap/ng_l2cap_llpi.h>
#include <netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h>
#include <netgraph/bluetooth/l2cap/ng_l2cap_misc.h>
#include <netgraph/bluetooth/l2cap/ng_l2cap_prse.h>

/******************************************************************************
 ******************************************************************************
 **  This node implements Link Layer Control and Adaptation Protocol (L2CAP)
 ******************************************************************************
 ******************************************************************************/

/* MALLOC define */
#ifdef NG_SEPARATE_MALLOC
MALLOC_DEFINE(M_NETGRAPH_L2CAP, "netgraph_l2cap", 
	"Netgraph Bluetooth L2CAP node");
#else
#define M_NETGRAPH_L2CAP M_NETGRAPH
#endif /* NG_SEPARATE_MALLOC */

/* Netgraph node methods */
static	ng_constructor_t	ng_l2cap_constructor;
static	ng_shutdown_t		ng_l2cap_shutdown;
static	ng_newhook_t		ng_l2cap_newhook;
static	ng_connect_t		ng_l2cap_connect;
static	ng_disconnect_t		ng_l2cap_disconnect;
static	ng_rcvmsg_t		ng_l2cap_lower_rcvmsg;
static	ng_rcvmsg_t		ng_l2cap_upper_rcvmsg;
static	ng_rcvmsg_t		ng_l2cap_default_rcvmsg;
static	ng_rcvdata_t		ng_l2cap_rcvdata;

/* Netgraph node type descriptor */
static	struct ng_type typestruct = {
	.version =	NG_ABI_VERSION,
	.name =		NG_L2CAP_NODE_TYPE,
	.constructor =	ng_l2cap_constructor,
	.rcvmsg =	ng_l2cap_default_rcvmsg,
	.shutdown =	ng_l2cap_shutdown,
	.newhook =	ng_l2cap_newhook,
	.connect =	ng_l2cap_connect,
	.rcvdata =	ng_l2cap_rcvdata,
	.disconnect =	ng_l2cap_disconnect,
	.cmdlist =	ng_l2cap_cmdlist,
};
NETGRAPH_INIT(l2cap, &typestruct);
MODULE_VERSION(ng_l2cap, NG_BLUETOOTH_VERSION);
MODULE_DEPEND(ng_l2cap, ng_bluetooth, NG_BLUETOOTH_VERSION,
        NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);

/*****************************************************************************
 *****************************************************************************
 **                   Netgraph methods implementation
 *****************************************************************************
 *****************************************************************************/

static void ng_l2cap_cleanup          (ng_l2cap_p);
static void ng_l2cap_destroy_channels (ng_l2cap_p);

/*
 * Create new instance of L2CAP node
 */

static int
ng_l2cap_constructor(node_p node)
{
	ng_l2cap_p	l2cap = NULL;

	/* Create new L2CAP node */
	l2cap = malloc(sizeof(*l2cap), M_NETGRAPH_L2CAP, M_WAITOK | M_ZERO);

	l2cap->node = node;
	l2cap->debug = NG_L2CAP_WARN_LEVEL;
	l2cap->discon_timo = 5; /* sec */

	LIST_INIT(&l2cap->con_list);
	LIST_INIT(&l2cap->chan_list);

	NG_NODE_SET_PRIVATE(node, l2cap);
	NG_NODE_FORCE_WRITER(node);

	return (0);
} /* ng_l2cap_constructor */

/*
 * Shutdown L2CAP node
 */

static int
ng_l2cap_shutdown(node_p node)
{
	ng_l2cap_p	l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);

	NG_NODE_SET_PRIVATE(node, NULL);
	NG_NODE_UNREF(node);

	/* Clean up L2CAP node. Delete all connection, channels and commands */
	l2cap->node = NULL;
	ng_l2cap_cleanup(l2cap);

	bzero(l2cap, sizeof(*l2cap));
	free(l2cap, M_NETGRAPH_L2CAP);

	return (0);
} /* ng_l2cap_shutdown */

/*
 * Give our OK for a hook to be added. HCI layer is connected to the HCI 
 * (NG_L2CAP_HOOK_HCI) hook. As per specification L2CAP layer MUST provide
 * Procol/Service Multiplexing, so the L2CAP node provides separate hooks 
 * for SDP (NG_L2CAP_HOOK_SDP), RFCOMM (NG_L2CAP_HOOK_RFCOMM) and TCP 
 * (NG_L2CAP_HOOK_TCP) protcols. Unknown PSM will be forwarded to 
 * NG_L2CAP_HOOK_ORPHAN hook. Control node/application is connected to 
 * control (NG_L2CAP_HOOK_CTL) hook. 
 */

static int
ng_l2cap_newhook(node_p node, hook_p hook, char const *name)
{
	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
	hook_p		*h = NULL;

	if (strcmp(name, NG_L2CAP_HOOK_HCI) == 0)
		h = &l2cap->hci;
	else if (strcmp(name, NG_L2CAP_HOOK_L2C) == 0)
		h = &l2cap->l2c;
	else if (strcmp(name, NG_L2CAP_HOOK_CTL) == 0)
		h = &l2cap->ctl;
	else
		return (EINVAL);

	if (*h != NULL)
		return (EISCONN);

	*h = hook;

	return (0);
} /* ng_l2cap_newhook */

/*
 * Give our final OK to connect hook. Nothing to do here.
 */

static int
ng_l2cap_connect(hook_p hook)
{
	ng_l2cap_p	l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
	int		error = 0;

	if (hook == l2cap->hci)
		NG_HOOK_SET_RCVMSG(hook, ng_l2cap_lower_rcvmsg);
	else
	if (hook == l2cap->l2c || hook == l2cap->ctl) {
		NG_HOOK_SET_RCVMSG(hook, ng_l2cap_upper_rcvmsg);

		/* Send delayed notification to the upper layer */
		error = ng_send_fn(l2cap->node, hook, ng_l2cap_send_hook_info,
				NULL, 0);
	} else
		error = EINVAL;

	return (error);
} /* ng_l2cap_connect */

/*
 * Disconnect the hook. For downstream hook we must notify upper layers.
 * 
 * XXX For upstream hooks this is really ugly :( Hook was disconnected and it 
 * XXX is now too late to do anything. For now we just clean up our own mess
 * XXX and remove all channels that use disconnected upstream hook. If we don't 
 * XXX do that then L2CAP node can get out of sync with upper layers.
 * XXX No notification will be sent to remote peer. 
 */

static int
ng_l2cap_disconnect(hook_p hook)
{
	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
	hook_p		*h = NULL;

	if (hook == l2cap->hci) {
		ng_l2cap_cleanup(l2cap);
		h = &l2cap->hci;
	} else
	if (hook == l2cap->l2c) {
		ng_l2cap_destroy_channels(l2cap);
		h = &l2cap->l2c;
	} else
	if (hook == l2cap->ctl)
		h = &l2cap->ctl;
	else
		return (EINVAL);

	*h = NULL;

	/* Shutdown when all hooks are disconnected */
	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
	    NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
		ng_rmnode_self(NG_HOOK_NODE(hook));

	return (0);
} /* ng_l2cap_disconnect */

/*
 * Process control message from lower layer
 */

static int
ng_l2cap_lower_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
	struct ng_mesg	*msg = NGI_MSG(item); /* item still has message */
	int		 error = 0;

	switch (msg->header.typecookie) {
	case NGM_HCI_COOKIE:
		switch (msg->header.cmd) {
		/* HCI node is ready */
		case NGM_HCI_NODE_UP: {
			ng_hci_node_up_ep	*ep = NULL;

			if (msg->header.arglen != sizeof(*ep))
				error = EMSGSIZE;
			else {
				ep = (ng_hci_node_up_ep *)(msg->data);

				NG_L2CAP_INFO(
"%s: %s - HCI node is up, bdaddr: %x:%x:%x:%x:%x:%x, " \
"pkt_size=%d bytes, num_pkts=%d\n",	__func__, NG_NODE_NAME(l2cap->node),
					ep->bdaddr.b[5], ep->bdaddr.b[4],
					ep->bdaddr.b[3], ep->bdaddr.b[2],
					ep->bdaddr.b[1], ep->bdaddr.b[0],
					ep->pkt_size, ep->num_pkts);

				bcopy(&ep->bdaddr, &l2cap->bdaddr,
					sizeof(l2cap->bdaddr));
				l2cap->pkt_size = ep->pkt_size;
				l2cap->num_pkts = ep->num_pkts;

				/* Notify upper layers */
				ng_l2cap_send_hook_info(l2cap->node,
					l2cap->l2c, NULL, 0);
				ng_l2cap_send_hook_info(l2cap->node,
					l2cap->ctl, NULL, 0);
			}
			} break;

		case NGM_HCI_SYNC_CON_QUEUE: {
			ng_hci_sync_con_queue_ep	*ep = NULL;
			ng_l2cap_con_p			 con = NULL;

			if (msg->header.arglen != sizeof(*ep))
				error = EMSGSIZE;
			else {
				ep = (ng_hci_sync_con_queue_ep *)(msg->data);
				con = ng_l2cap_con_by_handle(l2cap,
							ep->con_handle);
				if (con == NULL)
					break;

				NG_L2CAP_INFO(
"%s: %s - sync HCI connection queue, con_handle=%d, pending=%d, completed=%d\n",
					 __func__, NG_NODE_NAME(l2cap->node),
					ep->con_handle, con->pending,
					ep->completed);

				con->pending -= ep->completed;
				if (con->pending < 0) {
					NG_L2CAP_WARN(
"%s: %s - pending packet counter is out of sync! " \
"con_handle=%d, pending=%d, completed=%d\n",	__func__, 
						NG_NODE_NAME(l2cap->node),
						con->con_handle, con->pending, 
						ep->completed);

					con->pending = 0;
				}

				ng_l2cap_lp_deliver(con);
			}
			} break;

		/* LP_ConnectCfm[Neg] */
		case NGM_HCI_LP_CON_CFM:
			error = ng_l2cap_lp_con_cfm(l2cap, msg);
			break;

		/* LP_ConnectInd */
		case NGM_HCI_LP_CON_IND:
			error = ng_l2cap_lp_con_ind(l2cap, msg);
			break;

		/* LP_DisconnectInd */
		case NGM_HCI_LP_DISCON_IND:
			error = ng_l2cap_lp_discon_ind(l2cap, msg);
			break;

		/* LP_QoSSetupCfm[Neg] */
		case NGM_HCI_LP_QOS_CFM:
			error = ng_l2cap_lp_qos_cfm(l2cap, msg);
			break;

		/* LP_OoSViolationInd */
		case NGM_HCI_LP_QOS_IND:
			error = ng_l2cap_lp_qos_ind(l2cap, msg);
			break;
		case NGM_HCI_LP_ENC_CHG:
			error = ng_l2cap_lp_enc_change(l2cap, msg);
			break;
		default:
			error = EINVAL;
			break;
		}
		break;

	default:
		return (ng_l2cap_default_rcvmsg(node, item, lasthook));
		/* NOT REACHED */
	}

	NG_FREE_ITEM(item);

	return (error);
} /* ng_l2cap_lower_rcvmsg */

/*
 * Process control message from upper layer
 */

static int
ng_l2cap_upper_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
	struct ng_mesg	*msg = NGI_MSG(item); /* item still has message */
	int		 error = 0;

	switch (msg->header.typecookie) {
	case NGM_L2CAP_COOKIE:
		switch (msg->header.cmd) {
		/* L2CA_Connect */
		case NGM_L2CAP_L2CA_CON:
			error = ng_l2cap_l2ca_con_req(l2cap, msg);
			break;

		/* L2CA_ConnectRsp */
		case NGM_L2CAP_L2CA_CON_RSP:
			error = ng_l2cap_l2ca_con_rsp_req(l2cap, msg);
			break;

		/* L2CA_Config */
		case NGM_L2CAP_L2CA_CFG:
			error = ng_l2cap_l2ca_cfg_req(l2cap, msg);
			break;

		/* L2CA_ConfigRsp */
		case NGM_L2CAP_L2CA_CFG_RSP:
			error = ng_l2cap_l2ca_cfg_rsp_req(l2cap, msg);
			break;

		/* L2CA_Disconnect */
		case NGM_L2CAP_L2CA_DISCON:
			error = ng_l2cap_l2ca_discon_req(l2cap, msg);
			break;

		/* L2CA_GroupCreate */
		case NGM_L2CAP_L2CA_GRP_CREATE:
			error = ng_l2cap_l2ca_grp_create(l2cap, msg);
			break;

		/* L2CA_GroupClose */
		case NGM_L2CAP_L2CA_GRP_CLOSE:
			error = ng_l2cap_l2ca_grp_close(l2cap, msg);
			break;

		/* L2CA_GroupAddMember */
		case NGM_L2CAP_L2CA_GRP_ADD_MEMBER:
			error = ng_l2cap_l2ca_grp_add_member_req(l2cap, msg);
			break;

		/* L2CA_GroupDeleteMember */
		case NGM_L2CAP_L2CA_GRP_REM_MEMBER:
			error = ng_l2cap_l2ca_grp_rem_member(l2cap, msg);
			break;

		/* L2CA_GroupMembership */
		case NGM_L2CAP_L2CA_GRP_MEMBERSHIP:
			error = ng_l2cap_l2ca_grp_get_members(l2cap, msg);
			break;

		/* L2CA_Ping */
		case NGM_L2CAP_L2CA_PING:
			error = ng_l2cap_l2ca_ping_req(l2cap, msg);
			break;

		/* L2CA_GetInfo */
		case NGM_L2CAP_L2CA_GET_INFO:
			error = ng_l2cap_l2ca_get_info_req(l2cap, msg);
			break;

		/* L2CA_EnableCLT */
		case NGM_L2CAP_L2CA_ENABLE_CLT:
			error = ng_l2cap_l2ca_enable_clt(l2cap, msg);
			break;

		default:
			return (ng_l2cap_default_rcvmsg(node, item, lasthook));
			/* NOT REACHED */
		}
		break;

	default:
		return (ng_l2cap_default_rcvmsg(node, item, lasthook));
		/* NOT REACHED */
	}

	NG_FREE_ITEM(item);

	return (error);
} /* ng_l2cap_upper_rcvmsg */

/*
 * Default control message processing routine
 */

static int
ng_l2cap_default_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
	struct ng_mesg	*msg = NULL, *rsp = NULL;
	int		 error = 0;

	/* Detach and process message */
	NGI_GET_MSG(item, msg);

	switch (msg->header.typecookie) {
	case NGM_GENERIC_COOKIE:
		switch (msg->header.cmd) {
		case NGM_TEXT_STATUS:
			NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT);
			if (rsp == NULL)
				error = ENOMEM;
			else
				snprintf(rsp->data, NG_TEXTRESPONSE,
					"bdaddr %x:%x:%x:%x:%x:%x, " \
					"pkt_size %d\n" \
					"Hooks %s %s %s\n" \
					"Flags %#x\n",
					l2cap->bdaddr.b[5], l2cap->bdaddr.b[4],
					l2cap->bdaddr.b[3], l2cap->bdaddr.b[2],
					l2cap->bdaddr.b[1], l2cap->bdaddr.b[0],
					l2cap->pkt_size,
					(l2cap->hci != NULL)?
						NG_L2CAP_HOOK_HCI : "",
					(l2cap->l2c != NULL)?
						NG_L2CAP_HOOK_L2C : "", 
					(l2cap->ctl != NULL)?
						NG_L2CAP_HOOK_CTL : "", 
					l2cap->flags);
			break;

		default:
			error = EINVAL;
			break;
		}
		break;

	/* Messages from the upper layer or directed to the local node */
	case NGM_L2CAP_COOKIE:
		switch (msg->header.cmd) {
		/* Get node flags */
		case NGM_L2CAP_NODE_GET_FLAGS:
			NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_flags_ep),
				M_NOWAIT);
			if (rsp == NULL)
				error = ENOMEM;
			else
				*((ng_l2cap_node_flags_ep *)(rsp->data)) =
					l2cap->flags;
			break;

		/* Get node debug */
		case NGM_L2CAP_NODE_GET_DEBUG:
			NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_debug_ep),
				M_NOWAIT);
			if (rsp == NULL)
				error = ENOMEM;
			else
				*((ng_l2cap_node_debug_ep *)(rsp->data)) =
					l2cap->debug;
			break;

		/* Set node debug */
		case NGM_L2CAP_NODE_SET_DEBUG:
			if (msg->header.arglen !=
					sizeof(ng_l2cap_node_debug_ep))
				error = EMSGSIZE;
			else
				l2cap->debug =
					*((ng_l2cap_node_debug_ep *)(msg->data));
			break;

		/* Get connection list */
		case NGM_L2CAP_NODE_GET_CON_LIST: {
			ng_l2cap_con_p			 con = NULL;
			ng_l2cap_node_con_list_ep	*e1 = NULL;
			ng_l2cap_node_con_ep		*e2 = NULL;
			int				 n = 0;

			/* Count number of connections */
			LIST_FOREACH(con, &l2cap->con_list, next)
				n++;
			if (n > NG_L2CAP_MAX_CON_NUM)
				n = NG_L2CAP_MAX_CON_NUM;

			/* Prepare response */
			NG_MKRESPONSE(rsp, msg,
				sizeof(*e1) + n * sizeof(*e2), M_NOWAIT);
			if (rsp == NULL) {
				error = ENOMEM;
				break;
			}

			e1 = (ng_l2cap_node_con_list_ep *)(rsp->data);
			e2 = (ng_l2cap_node_con_ep *)(e1 + 1);

			e1->num_connections = n;

			LIST_FOREACH(con, &l2cap->con_list, next) {
				e2->state = con->state;

				e2->flags = con->flags;
				if (con->tx_pkt != NULL)
					e2->flags |= NG_L2CAP_CON_TX;
				if (con->rx_pkt != NULL)
					e2->flags |= NG_L2CAP_CON_RX;

				e2->pending = con->pending;

				e2->con_handle = con->con_handle;
				bcopy(&con->remote, &e2->remote,
					sizeof(e2->remote));

				e2 ++;
				if (--n <= 0)
					break;
			}
			} break;

		/* Get channel list */
		case NGM_L2CAP_NODE_GET_CHAN_LIST: {
			ng_l2cap_chan_p			 ch = NULL;
			ng_l2cap_node_chan_list_ep	*e1 = NULL;
			ng_l2cap_node_chan_ep		*e2 = NULL;
			int				 n = 0;

			/* Count number of channels */
			LIST_FOREACH(ch, &l2cap->chan_list, next)
				n ++;
			if (n > NG_L2CAP_MAX_CHAN_NUM)
				n = NG_L2CAP_MAX_CHAN_NUM;

			/* Prepare response */
			NG_MKRESPONSE(rsp, msg,
				sizeof(ng_l2cap_node_chan_list_ep) +
				n * sizeof(ng_l2cap_node_chan_ep), M_NOWAIT);
			if (rsp == NULL) {
				error = ENOMEM;
				break;
			}

			e1 = (ng_l2cap_node_chan_list_ep *)(rsp->data);
			e2 = (ng_l2cap_node_chan_ep *)(e1 + 1);

			e1->num_channels = n;

			LIST_FOREACH(ch, &l2cap->chan_list, next) {
				e2->state = ch->state;

				e2->scid = ch->scid;
				e2->dcid = ch->dcid;

				e2->imtu = ch->imtu;
				e2->omtu = ch->omtu;

				e2->psm = ch->psm;
				bcopy(&ch->con->remote, &e2->remote,
					sizeof(e2->remote));

				e2 ++;
				if (--n <= 0)
					break;
			}
			} break;

		case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
			NG_MKRESPONSE(rsp, msg,
				sizeof(ng_l2cap_node_auto_discon_ep), M_NOWAIT);
			if (rsp == NULL)
				error = ENOMEM;
			else
				*((ng_l2cap_node_auto_discon_ep *)(rsp->data)) =
					l2cap->discon_timo;
			break;

		case NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO:
			if (msg->header.arglen !=
					sizeof(ng_l2cap_node_auto_discon_ep))
				error = EMSGSIZE;
			else
				l2cap->discon_timo =
					*((ng_l2cap_node_auto_discon_ep *)
							(msg->data));
			break;

		default:
			error = EINVAL;
			break;
		}
		break;

	default:
		error = EINVAL;
		break;
	}

	NG_RESPOND_MSG(error, node, item, rsp);
	NG_FREE_MSG(msg);

	return (error);
} /* ng_l2cap_rcvmsg */

/*
 * Process data packet from one of our hooks.
 *
 * From the HCI hook we expect to receive ACL data packets. ACL data packets 
 * gets re-assembled into one L2CAP packet (according to length) and then gets 
 * processed.
 *
 * NOTE: We expect to receive L2CAP packet header in the first fragment. 
 *       Otherwise we WILL NOT be able to get length of the L2CAP packet.
 * 
 * Signaling L2CAP packets (destination channel ID == 0x1) are processed within
 * the node. Connectionless data packets (destination channel ID == 0x2) will 
 * be forwarded to appropriate upstream hook unless it is not connected or 
 * connectionless traffic for the specified PSM was disabled.
 *
 * From the upstream hooks we expect to receive data packets. These data 
 * packets will be converted into L2CAP data packets. The length of each
 * L2CAP packet must not exceed channel's omtu (our peer's imtu). Then
 * these L2CAP packets will be converted to ACL data packets (according to 
 * HCI layer MTU) and sent to lower layer.
 *
 * No data is expected from the control hook.
 */

static int
ng_l2cap_rcvdata(hook_p hook, item_p item)
{
	ng_l2cap_p	 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
	struct mbuf	*m = NULL;
	int		 error = 0;

	/* Detach mbuf, discard item and process data */
	NGI_GET_M(item, m);
	NG_FREE_ITEM(item);

	if (hook == l2cap->hci)
		error = ng_l2cap_lp_receive(l2cap, m);
	else if (hook == l2cap->l2c)
		error = ng_l2cap_l2ca_write_req(l2cap, m);
	else {
		NG_FREE_M(m);
		error = EINVAL;
	}

	return (error);
} /* ng_l2cap_rcvdata */

/*
 * Clean all connections, channels and commands for the L2CAP node
 */

static void
ng_l2cap_cleanup(ng_l2cap_p l2cap)
{
	ng_l2cap_con_p	con = NULL;

	/* Clean up connection and channels */
	while (!LIST_EMPTY(&l2cap->con_list)) {
		con = LIST_FIRST(&l2cap->con_list);

		if (con->flags & NG_L2CAP_CON_LP_TIMO)
			ng_l2cap_lp_untimeout(con);
		else if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)
			ng_l2cap_discon_untimeout(con);

		/* Connection terminated by local host */
		ng_l2cap_con_fail(con, 0x16);
	}
} /* ng_l2cap_cleanup */

/*
 * Destroy all channels that use specified upstream hook
 */

static void
ng_l2cap_destroy_channels(ng_l2cap_p l2cap)
{
	while (!LIST_EMPTY(&l2cap->chan_list))
		ng_l2cap_free_chan(LIST_FIRST(&l2cap->chan_list));
} /* ng_l2cap_destroy_channels_by_hook */