1 /*
2 * ng_l2cap_main.c
3 */
4
5 /*-
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $Id: ng_l2cap_main.c,v 1.2 2003/04/28 21:44:59 max Exp $
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/queue.h>
41 #include <netgraph/ng_message.h>
42 #include <netgraph/netgraph.h>
43 #include <netgraph/ng_parse.h>
44 #include <netgraph/bluetooth/include/ng_bluetooth.h>
45 #include <netgraph/bluetooth/include/ng_hci.h>
46 #include <netgraph/bluetooth/include/ng_l2cap.h>
47 #include <netgraph/bluetooth/l2cap/ng_l2cap_var.h>
48 #include <netgraph/bluetooth/l2cap/ng_l2cap_cmds.h>
49 #include <netgraph/bluetooth/l2cap/ng_l2cap_evnt.h>
50 #include <netgraph/bluetooth/l2cap/ng_l2cap_llpi.h>
51 #include <netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h>
52 #include <netgraph/bluetooth/l2cap/ng_l2cap_misc.h>
53 #include <netgraph/bluetooth/l2cap/ng_l2cap_prse.h>
54
55 /******************************************************************************
56 ******************************************************************************
57 ** This node implements Link Layer Control and Adaptation Protocol (L2CAP)
58 ******************************************************************************
59 ******************************************************************************/
60
61 /* MALLOC define */
62 #ifdef NG_SEPARATE_MALLOC
63 MALLOC_DEFINE(M_NETGRAPH_L2CAP, "netgraph_l2cap",
64 "Netgraph Bluetooth L2CAP node");
65 #else
66 #define M_NETGRAPH_L2CAP M_NETGRAPH
67 #endif /* NG_SEPARATE_MALLOC */
68
69 /* Netgraph node methods */
70 static ng_constructor_t ng_l2cap_constructor;
71 static ng_shutdown_t ng_l2cap_shutdown;
72 static ng_newhook_t ng_l2cap_newhook;
73 static ng_connect_t ng_l2cap_connect;
74 static ng_disconnect_t ng_l2cap_disconnect;
75 static ng_rcvmsg_t ng_l2cap_lower_rcvmsg;
76 static ng_rcvmsg_t ng_l2cap_upper_rcvmsg;
77 static ng_rcvmsg_t ng_l2cap_default_rcvmsg;
78 static ng_rcvdata_t ng_l2cap_rcvdata;
79
80 /* Netgraph node type descriptor */
81 static struct ng_type typestruct = {
82 .version = NG_ABI_VERSION,
83 .name = NG_L2CAP_NODE_TYPE,
84 .constructor = ng_l2cap_constructor,
85 .rcvmsg = ng_l2cap_default_rcvmsg,
86 .shutdown = ng_l2cap_shutdown,
87 .newhook = ng_l2cap_newhook,
88 .connect = ng_l2cap_connect,
89 .rcvdata = ng_l2cap_rcvdata,
90 .disconnect = ng_l2cap_disconnect,
91 .cmdlist = ng_l2cap_cmdlist,
92 };
93 NETGRAPH_INIT(l2cap, &typestruct);
94 MODULE_VERSION(ng_l2cap, NG_BLUETOOTH_VERSION);
95 MODULE_DEPEND(ng_l2cap, ng_bluetooth, NG_BLUETOOTH_VERSION,
96 NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
97
98 /*****************************************************************************
99 *****************************************************************************
100 ** Netgraph methods implementation
101 *****************************************************************************
102 *****************************************************************************/
103
104 static void ng_l2cap_cleanup (ng_l2cap_p);
105 static void ng_l2cap_destroy_channels (ng_l2cap_p);
106
107 /*
108 * Create new instance of L2CAP node
109 */
110
111 static int
ng_l2cap_constructor(node_p node)112 ng_l2cap_constructor(node_p node)
113 {
114 ng_l2cap_p l2cap = NULL;
115
116 /* Create new L2CAP node */
117 l2cap = malloc(sizeof(*l2cap), M_NETGRAPH_L2CAP, M_WAITOK | M_ZERO);
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
ng_l2cap_shutdown(node_p node)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
ng_l2cap_newhook(node_p node,hook_p hook,char const * name)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
ng_l2cap_connect(hook_p hook)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
ng_l2cap_disconnect(hook_p hook)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
ng_l2cap_lower_rcvmsg(node_p node,item_p item,hook_p lasthook)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 case NGM_HCI_LP_ENC_CHG:
355 error = ng_l2cap_lp_enc_change(l2cap, msg);
356 break;
357 default:
358 error = EINVAL;
359 break;
360 }
361 break;
362
363 default:
364 return (ng_l2cap_default_rcvmsg(node, item, lasthook));
365 /* NOT REACHED */
366 }
367
368 NG_FREE_ITEM(item);
369
370 return (error);
371 } /* ng_l2cap_lower_rcvmsg */
372
373 /*
374 * Process control message from upper layer
375 */
376
377 static int
ng_l2cap_upper_rcvmsg(node_p node,item_p item,hook_p lasthook)378 ng_l2cap_upper_rcvmsg(node_p node, item_p item, hook_p lasthook)
379 {
380 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
381 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
382 int error = 0;
383
384 switch (msg->header.typecookie) {
385 case NGM_L2CAP_COOKIE:
386 switch (msg->header.cmd) {
387 /* L2CA_Connect */
388 case NGM_L2CAP_L2CA_CON:
389 error = ng_l2cap_l2ca_con_req(l2cap, msg);
390 break;
391
392 /* L2CA_ConnectRsp */
393 case NGM_L2CAP_L2CA_CON_RSP:
394 error = ng_l2cap_l2ca_con_rsp_req(l2cap, msg);
395 break;
396
397 /* L2CA_Config */
398 case NGM_L2CAP_L2CA_CFG:
399 error = ng_l2cap_l2ca_cfg_req(l2cap, msg);
400 break;
401
402 /* L2CA_ConfigRsp */
403 case NGM_L2CAP_L2CA_CFG_RSP:
404 error = ng_l2cap_l2ca_cfg_rsp_req(l2cap, msg);
405 break;
406
407 /* L2CA_Disconnect */
408 case NGM_L2CAP_L2CA_DISCON:
409 error = ng_l2cap_l2ca_discon_req(l2cap, msg);
410 break;
411
412 /* L2CA_GroupCreate */
413 case NGM_L2CAP_L2CA_GRP_CREATE:
414 error = ng_l2cap_l2ca_grp_create(l2cap, msg);
415 break;
416
417 /* L2CA_GroupClose */
418 case NGM_L2CAP_L2CA_GRP_CLOSE:
419 error = ng_l2cap_l2ca_grp_close(l2cap, msg);
420 break;
421
422 /* L2CA_GroupAddMember */
423 case NGM_L2CAP_L2CA_GRP_ADD_MEMBER:
424 error = ng_l2cap_l2ca_grp_add_member_req(l2cap, msg);
425 break;
426
427 /* L2CA_GroupDeleteMember */
428 case NGM_L2CAP_L2CA_GRP_REM_MEMBER:
429 error = ng_l2cap_l2ca_grp_rem_member(l2cap, msg);
430 break;
431
432 /* L2CA_GroupMembership */
433 case NGM_L2CAP_L2CA_GRP_MEMBERSHIP:
434 error = ng_l2cap_l2ca_grp_get_members(l2cap, msg);
435 break;
436
437 /* L2CA_Ping */
438 case NGM_L2CAP_L2CA_PING:
439 error = ng_l2cap_l2ca_ping_req(l2cap, msg);
440 break;
441
442 /* L2CA_GetInfo */
443 case NGM_L2CAP_L2CA_GET_INFO:
444 error = ng_l2cap_l2ca_get_info_req(l2cap, msg);
445 break;
446
447 /* L2CA_EnableCLT */
448 case NGM_L2CAP_L2CA_ENABLE_CLT:
449 error = ng_l2cap_l2ca_enable_clt(l2cap, msg);
450 break;
451
452 default:
453 return (ng_l2cap_default_rcvmsg(node, item, lasthook));
454 /* NOT REACHED */
455 }
456 break;
457
458 default:
459 return (ng_l2cap_default_rcvmsg(node, item, lasthook));
460 /* NOT REACHED */
461 }
462
463 NG_FREE_ITEM(item);
464
465 return (error);
466 } /* ng_l2cap_upper_rcvmsg */
467
468 /*
469 * Default control message processing routine
470 */
471
472 static int
ng_l2cap_default_rcvmsg(node_p node,item_p item,hook_p lasthook)473 ng_l2cap_default_rcvmsg(node_p node, item_p item, hook_p lasthook)
474 {
475 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
476 struct ng_mesg *msg = NULL, *rsp = NULL;
477 int error = 0;
478
479 /* Detach and process message */
480 NGI_GET_MSG(item, msg);
481
482 switch (msg->header.typecookie) {
483 case NGM_GENERIC_COOKIE:
484 switch (msg->header.cmd) {
485 case NGM_TEXT_STATUS:
486 NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT);
487 if (rsp == NULL)
488 error = ENOMEM;
489 else
490 snprintf(rsp->data, NG_TEXTRESPONSE,
491 "bdaddr %x:%x:%x:%x:%x:%x, " \
492 "pkt_size %d\n" \
493 "Hooks %s %s %s\n" \
494 "Flags %#x\n",
495 l2cap->bdaddr.b[5], l2cap->bdaddr.b[4],
496 l2cap->bdaddr.b[3], l2cap->bdaddr.b[2],
497 l2cap->bdaddr.b[1], l2cap->bdaddr.b[0],
498 l2cap->pkt_size,
499 (l2cap->hci != NULL)?
500 NG_L2CAP_HOOK_HCI : "",
501 (l2cap->l2c != NULL)?
502 NG_L2CAP_HOOK_L2C : "",
503 (l2cap->ctl != NULL)?
504 NG_L2CAP_HOOK_CTL : "",
505 l2cap->flags);
506 break;
507
508 default:
509 error = EINVAL;
510 break;
511 }
512 break;
513
514 /* Messages from the upper layer or directed to the local node */
515 case NGM_L2CAP_COOKIE:
516 switch (msg->header.cmd) {
517 /* Get node flags */
518 case NGM_L2CAP_NODE_GET_FLAGS:
519 NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_flags_ep),
520 M_NOWAIT);
521 if (rsp == NULL)
522 error = ENOMEM;
523 else
524 *((ng_l2cap_node_flags_ep *)(rsp->data)) =
525 l2cap->flags;
526 break;
527
528 /* Get node debug */
529 case NGM_L2CAP_NODE_GET_DEBUG:
530 NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_debug_ep),
531 M_NOWAIT);
532 if (rsp == NULL)
533 error = ENOMEM;
534 else
535 *((ng_l2cap_node_debug_ep *)(rsp->data)) =
536 l2cap->debug;
537 break;
538
539 /* Set node debug */
540 case NGM_L2CAP_NODE_SET_DEBUG:
541 if (msg->header.arglen !=
542 sizeof(ng_l2cap_node_debug_ep))
543 error = EMSGSIZE;
544 else
545 l2cap->debug =
546 *((ng_l2cap_node_debug_ep *)(msg->data));
547 break;
548
549 /* Get connection list */
550 case NGM_L2CAP_NODE_GET_CON_LIST: {
551 ng_l2cap_con_p con = NULL;
552 ng_l2cap_node_con_list_ep *e1 = NULL;
553 ng_l2cap_node_con_ep *e2 = NULL;
554 int n = 0;
555
556 /* Count number of connections */
557 LIST_FOREACH(con, &l2cap->con_list, next)
558 n++;
559 if (n > NG_L2CAP_MAX_CON_NUM)
560 n = NG_L2CAP_MAX_CON_NUM;
561
562 /* Prepare response */
563 NG_MKRESPONSE(rsp, msg,
564 sizeof(*e1) + n * sizeof(*e2), M_NOWAIT);
565 if (rsp == NULL) {
566 error = ENOMEM;
567 break;
568 }
569
570 e1 = (ng_l2cap_node_con_list_ep *)(rsp->data);
571 e2 = (ng_l2cap_node_con_ep *)(e1 + 1);
572
573 e1->num_connections = n;
574
575 LIST_FOREACH(con, &l2cap->con_list, next) {
576 e2->state = con->state;
577
578 e2->flags = con->flags;
579 if (con->tx_pkt != NULL)
580 e2->flags |= NG_L2CAP_CON_TX;
581 if (con->rx_pkt != NULL)
582 e2->flags |= NG_L2CAP_CON_RX;
583
584 e2->pending = con->pending;
585
586 e2->con_handle = con->con_handle;
587 bcopy(&con->remote, &e2->remote,
588 sizeof(e2->remote));
589
590 e2 ++;
591 if (--n <= 0)
592 break;
593 }
594 } break;
595
596 /* Get channel list */
597 case NGM_L2CAP_NODE_GET_CHAN_LIST: {
598 ng_l2cap_chan_p ch = NULL;
599 ng_l2cap_node_chan_list_ep *e1 = NULL;
600 ng_l2cap_node_chan_ep *e2 = NULL;
601 int n = 0;
602
603 /* Count number of channels */
604 LIST_FOREACH(ch, &l2cap->chan_list, next)
605 n ++;
606 if (n > NG_L2CAP_MAX_CHAN_NUM)
607 n = NG_L2CAP_MAX_CHAN_NUM;
608
609 /* Prepare response */
610 NG_MKRESPONSE(rsp, msg,
611 sizeof(ng_l2cap_node_chan_list_ep) +
612 n * sizeof(ng_l2cap_node_chan_ep), M_NOWAIT);
613 if (rsp == NULL) {
614 error = ENOMEM;
615 break;
616 }
617
618 e1 = (ng_l2cap_node_chan_list_ep *)(rsp->data);
619 e2 = (ng_l2cap_node_chan_ep *)(e1 + 1);
620
621 e1->num_channels = n;
622
623 LIST_FOREACH(ch, &l2cap->chan_list, next) {
624 e2->state = ch->state;
625
626 e2->scid = ch->scid;
627 e2->dcid = ch->dcid;
628
629 e2->imtu = ch->imtu;
630 e2->omtu = ch->omtu;
631
632 e2->psm = ch->psm;
633 bcopy(&ch->con->remote, &e2->remote,
634 sizeof(e2->remote));
635
636 e2 ++;
637 if (--n <= 0)
638 break;
639 }
640 } break;
641
642 case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
643 NG_MKRESPONSE(rsp, msg,
644 sizeof(ng_l2cap_node_auto_discon_ep), M_NOWAIT);
645 if (rsp == NULL)
646 error = ENOMEM;
647 else
648 *((ng_l2cap_node_auto_discon_ep *)(rsp->data)) =
649 l2cap->discon_timo;
650 break;
651
652 case NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO:
653 if (msg->header.arglen !=
654 sizeof(ng_l2cap_node_auto_discon_ep))
655 error = EMSGSIZE;
656 else
657 l2cap->discon_timo =
658 *((ng_l2cap_node_auto_discon_ep *)
659 (msg->data));
660 break;
661
662 default:
663 error = EINVAL;
664 break;
665 }
666 break;
667
668 default:
669 error = EINVAL;
670 break;
671 }
672
673 NG_RESPOND_MSG(error, node, item, rsp);
674 NG_FREE_MSG(msg);
675
676 return (error);
677 } /* ng_l2cap_rcvmsg */
678
679 /*
680 * Process data packet from one of our hooks.
681 *
682 * From the HCI hook we expect to receive ACL data packets. ACL data packets
683 * gets re-assembled into one L2CAP packet (according to length) and then gets
684 * processed.
685 *
686 * NOTE: We expect to receive L2CAP packet header in the first fragment.
687 * Otherwise we WILL NOT be able to get length of the L2CAP packet.
688 *
689 * Signaling L2CAP packets (destination channel ID == 0x1) are processed within
690 * the node. Connectionless data packets (destination channel ID == 0x2) will
691 * be forwarded to appropriate upstream hook unless it is not connected or
692 * connectionless traffic for the specified PSM was disabled.
693 *
694 * From the upstream hooks we expect to receive data packets. These data
695 * packets will be converted into L2CAP data packets. The length of each
696 * L2CAP packet must not exceed channel's omtu (our peer's imtu). Then
697 * these L2CAP packets will be converted to ACL data packets (according to
698 * HCI layer MTU) and sent to lower layer.
699 *
700 * No data is expected from the control hook.
701 */
702
703 static int
ng_l2cap_rcvdata(hook_p hook,item_p item)704 ng_l2cap_rcvdata(hook_p hook, item_p item)
705 {
706 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
707 struct mbuf *m = NULL;
708 int error = 0;
709
710 /* Detach mbuf, discard item and process data */
711 NGI_GET_M(item, m);
712 NG_FREE_ITEM(item);
713
714 if (hook == l2cap->hci)
715 error = ng_l2cap_lp_receive(l2cap, m);
716 else if (hook == l2cap->l2c)
717 error = ng_l2cap_l2ca_write_req(l2cap, m);
718 else {
719 NG_FREE_M(m);
720 error = EINVAL;
721 }
722
723 return (error);
724 } /* ng_l2cap_rcvdata */
725
726 /*
727 * Clean all connections, channels and commands for the L2CAP node
728 */
729
730 static void
ng_l2cap_cleanup(ng_l2cap_p l2cap)731 ng_l2cap_cleanup(ng_l2cap_p l2cap)
732 {
733 ng_l2cap_con_p con = NULL;
734
735 /* Clean up connection and channels */
736 while (!LIST_EMPTY(&l2cap->con_list)) {
737 con = LIST_FIRST(&l2cap->con_list);
738
739 if (con->flags & NG_L2CAP_CON_LP_TIMO)
740 ng_l2cap_lp_untimeout(con);
741 else if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)
742 ng_l2cap_discon_untimeout(con);
743
744 /* Connection terminated by local host */
745 ng_l2cap_con_fail(con, 0x16);
746 }
747 } /* ng_l2cap_cleanup */
748
749 /*
750 * Destroy all channels that use specified upstream hook
751 */
752
753 static void
ng_l2cap_destroy_channels(ng_l2cap_p l2cap)754 ng_l2cap_destroy_channels(ng_l2cap_p l2cap)
755 {
756 while (!LIST_EMPTY(&l2cap->chan_list))
757 ng_l2cap_free_chan(LIST_FIRST(&l2cap->chan_list));
758 } /* ng_l2cap_destroy_channels_by_hook */
759