6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
32 * $Id: ng_l2cap_main.c,v 1.2 2003/04/28 21:44:59 max Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
41 #include <sys/queue.h>
42 #include <netgraph/ng_message.h>
43 #include <netgraph/netgraph.h>
44 #include <netgraph/ng_parse.h>
45 #include <netgraph/bluetooth/include/ng_bluetooth.h>
46 #include <netgraph/bluetooth/include/ng_hci.h>
47 #include <netgraph/bluetooth/include/ng_l2cap.h>
48 #include <netgraph/bluetooth/l2cap/ng_l2cap_var.h>
49 #include <netgraph/bluetooth/l2cap/ng_l2cap_cmds.h>
50 #include <netgraph/bluetooth/l2cap/ng_l2cap_evnt.h>
51 #include <netgraph/bluetooth/l2cap/ng_l2cap_llpi.h>
52 #include <netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h>
53 #include <netgraph/bluetooth/l2cap/ng_l2cap_misc.h>
54 #include <netgraph/bluetooth/l2cap/ng_l2cap_prse.h>
56 /******************************************************************************
57 ******************************************************************************
58 ** This node implements Link Layer Control and Adaptation Protocol (L2CAP)
59 ******************************************************************************
60 ******************************************************************************/
63 #ifdef NG_SEPARATE_MALLOC
64 MALLOC_DEFINE(M_NETGRAPH_L2CAP, "netgraph_l2cap",
65 "Netgraph Bluetooth L2CAP node");
67 #define M_NETGRAPH_L2CAP M_NETGRAPH
68 #endif /* NG_SEPARATE_MALLOC */
70 /* Netgraph node methods */
71 static ng_constructor_t ng_l2cap_constructor;
72 static ng_shutdown_t ng_l2cap_shutdown;
73 static ng_newhook_t ng_l2cap_newhook;
74 static ng_connect_t ng_l2cap_connect;
75 static ng_disconnect_t ng_l2cap_disconnect;
76 static ng_rcvmsg_t ng_l2cap_lower_rcvmsg;
77 static ng_rcvmsg_t ng_l2cap_upper_rcvmsg;
78 static ng_rcvmsg_t ng_l2cap_default_rcvmsg;
79 static ng_rcvdata_t ng_l2cap_rcvdata;
81 /* Netgraph node type descriptor */
82 static struct ng_type typestruct = {
83 .version = NG_ABI_VERSION,
84 .name = NG_L2CAP_NODE_TYPE,
85 .constructor = ng_l2cap_constructor,
86 .rcvmsg = ng_l2cap_default_rcvmsg,
87 .shutdown = ng_l2cap_shutdown,
88 .newhook = ng_l2cap_newhook,
89 .connect = ng_l2cap_connect,
90 .rcvdata = ng_l2cap_rcvdata,
91 .disconnect = ng_l2cap_disconnect,
92 .cmdlist = ng_l2cap_cmdlist,
94 NETGRAPH_INIT(l2cap, &typestruct);
95 MODULE_VERSION(ng_l2cap, NG_BLUETOOTH_VERSION);
96 MODULE_DEPEND(ng_l2cap, ng_bluetooth, NG_BLUETOOTH_VERSION,
97 NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
99 /*****************************************************************************
100 *****************************************************************************
101 ** Netgraph methods implementation
102 *****************************************************************************
103 *****************************************************************************/
105 static void ng_l2cap_cleanup (ng_l2cap_p);
106 static void ng_l2cap_destroy_channels (ng_l2cap_p);
109 * Create new instance of L2CAP node
113 ng_l2cap_constructor(node_p node)
115 ng_l2cap_p l2cap = NULL;
117 /* Create new L2CAP node */
118 l2cap = malloc(sizeof(*l2cap), M_NETGRAPH_L2CAP, M_WAITOK | M_ZERO);
121 l2cap->debug = NG_L2CAP_WARN_LEVEL;
122 l2cap->discon_timo = 5; /* sec */
124 LIST_INIT(&l2cap->con_list);
125 LIST_INIT(&l2cap->chan_list);
127 NG_NODE_SET_PRIVATE(node, l2cap);
128 NG_NODE_FORCE_WRITER(node);
131 } /* ng_l2cap_constructor */
134 * Shutdown L2CAP node
138 ng_l2cap_shutdown(node_p node)
140 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
142 NG_NODE_SET_PRIVATE(node, NULL);
145 /* Clean up L2CAP node. Delete all connection, channels and commands */
147 ng_l2cap_cleanup(l2cap);
149 bzero(l2cap, sizeof(*l2cap));
150 free(l2cap, M_NETGRAPH_L2CAP);
153 } /* ng_l2cap_shutdown */
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.
166 ng_l2cap_newhook(node_p node, hook_p hook, char const *name)
168 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
171 if (strcmp(name, NG_L2CAP_HOOK_HCI) == 0)
173 else if (strcmp(name, NG_L2CAP_HOOK_L2C) == 0)
175 else if (strcmp(name, NG_L2CAP_HOOK_CTL) == 0)
186 } /* ng_l2cap_newhook */
189 * Give our final OK to connect hook. Nothing to do here.
193 ng_l2cap_connect(hook_p hook)
195 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
198 if (hook == l2cap->hci)
199 NG_HOOK_SET_RCVMSG(hook, ng_l2cap_lower_rcvmsg);
201 if (hook == l2cap->l2c || hook == l2cap->ctl) {
202 NG_HOOK_SET_RCVMSG(hook, ng_l2cap_upper_rcvmsg);
204 /* Send delayed notification to the upper layer */
205 error = ng_send_fn(l2cap->node, hook, ng_l2cap_send_hook_info,
211 } /* ng_l2cap_connect */
214 * Disconnect the hook. For downstream hook we must notify upper layers.
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.
224 ng_l2cap_disconnect(hook_p hook)
226 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
229 if (hook == l2cap->hci) {
230 ng_l2cap_cleanup(l2cap);
233 if (hook == l2cap->l2c) {
234 ng_l2cap_destroy_channels(l2cap);
237 if (hook == l2cap->ctl)
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));
250 } /* ng_l2cap_disconnect */
253 * Process control message from lower layer
257 ng_l2cap_lower_rcvmsg(node_p node, item_p item, hook_p lasthook)
259 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
260 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
263 switch (msg->header.typecookie) {
265 switch (msg->header.cmd) {
266 /* HCI node is ready */
267 case NGM_HCI_NODE_UP: {
268 ng_hci_node_up_ep *ep = NULL;
270 if (msg->header.arglen != sizeof(*ep))
273 ep = (ng_hci_node_up_ep *)(msg->data);
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);
283 bcopy(&ep->bdaddr, &l2cap->bdaddr,
284 sizeof(l2cap->bdaddr));
285 l2cap->pkt_size = ep->pkt_size;
286 l2cap->num_pkts = ep->num_pkts;
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);
296 case NGM_HCI_SYNC_CON_QUEUE: {
297 ng_hci_sync_con_queue_ep *ep = NULL;
298 ng_l2cap_con_p con = NULL;
300 if (msg->header.arglen != sizeof(*ep))
303 ep = (ng_hci_sync_con_queue_ep *)(msg->data);
304 con = ng_l2cap_con_by_handle(l2cap,
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,
315 con->pending -= ep->completed;
316 if (con->pending < 0) {
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,
327 ng_l2cap_lp_deliver(con);
331 /* LP_ConnectCfm[Neg] */
332 case NGM_HCI_LP_CON_CFM:
333 error = ng_l2cap_lp_con_cfm(l2cap, msg);
337 case NGM_HCI_LP_CON_IND:
338 error = ng_l2cap_lp_con_ind(l2cap, msg);
341 /* LP_DisconnectInd */
342 case NGM_HCI_LP_DISCON_IND:
343 error = ng_l2cap_lp_discon_ind(l2cap, msg);
346 /* LP_QoSSetupCfm[Neg] */
347 case NGM_HCI_LP_QOS_CFM:
348 error = ng_l2cap_lp_qos_cfm(l2cap, msg);
351 /* LP_OoSViolationInd */
352 case NGM_HCI_LP_QOS_IND:
353 error = ng_l2cap_lp_qos_ind(l2cap, msg);
355 case NGM_HCI_LP_ENC_CHG:
356 error = ng_l2cap_lp_enc_change(l2cap, msg);
365 return (ng_l2cap_default_rcvmsg(node, item, lasthook));
372 } /* ng_l2cap_lower_rcvmsg */
375 * Process control message from upper layer
379 ng_l2cap_upper_rcvmsg(node_p node, item_p item, hook_p lasthook)
381 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
382 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
385 switch (msg->header.typecookie) {
386 case NGM_L2CAP_COOKIE:
387 switch (msg->header.cmd) {
389 case NGM_L2CAP_L2CA_CON:
390 error = ng_l2cap_l2ca_con_req(l2cap, msg);
393 /* L2CA_ConnectRsp */
394 case NGM_L2CAP_L2CA_CON_RSP:
395 error = ng_l2cap_l2ca_con_rsp_req(l2cap, msg);
399 case NGM_L2CAP_L2CA_CFG:
400 error = ng_l2cap_l2ca_cfg_req(l2cap, msg);
404 case NGM_L2CAP_L2CA_CFG_RSP:
405 error = ng_l2cap_l2ca_cfg_rsp_req(l2cap, msg);
408 /* L2CA_Disconnect */
409 case NGM_L2CAP_L2CA_DISCON:
410 error = ng_l2cap_l2ca_discon_req(l2cap, msg);
413 /* L2CA_GroupCreate */
414 case NGM_L2CAP_L2CA_GRP_CREATE:
415 error = ng_l2cap_l2ca_grp_create(l2cap, msg);
418 /* L2CA_GroupClose */
419 case NGM_L2CAP_L2CA_GRP_CLOSE:
420 error = ng_l2cap_l2ca_grp_close(l2cap, msg);
423 /* L2CA_GroupAddMember */
424 case NGM_L2CAP_L2CA_GRP_ADD_MEMBER:
425 error = ng_l2cap_l2ca_grp_add_member_req(l2cap, msg);
428 /* L2CA_GroupDeleteMember */
429 case NGM_L2CAP_L2CA_GRP_REM_MEMBER:
430 error = ng_l2cap_l2ca_grp_rem_member(l2cap, msg);
433 /* L2CA_GroupMembership */
434 case NGM_L2CAP_L2CA_GRP_MEMBERSHIP:
435 error = ng_l2cap_l2ca_grp_get_members(l2cap, msg);
439 case NGM_L2CAP_L2CA_PING:
440 error = ng_l2cap_l2ca_ping_req(l2cap, msg);
444 case NGM_L2CAP_L2CA_GET_INFO:
445 error = ng_l2cap_l2ca_get_info_req(l2cap, msg);
449 case NGM_L2CAP_L2CA_ENABLE_CLT:
450 error = ng_l2cap_l2ca_enable_clt(l2cap, msg);
454 return (ng_l2cap_default_rcvmsg(node, item, lasthook));
460 return (ng_l2cap_default_rcvmsg(node, item, lasthook));
467 } /* ng_l2cap_upper_rcvmsg */
470 * Default control message processing routine
474 ng_l2cap_default_rcvmsg(node_p node, item_p item, hook_p lasthook)
476 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
477 struct ng_mesg *msg = NULL, *rsp = NULL;
480 /* Detach and process message */
481 NGI_GET_MSG(item, msg);
483 switch (msg->header.typecookie) {
484 case NGM_GENERIC_COOKIE:
485 switch (msg->header.cmd) {
486 case NGM_TEXT_STATUS:
487 NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT);
491 snprintf(rsp->data, NG_TEXTRESPONSE,
492 "bdaddr %x:%x:%x:%x:%x:%x, " \
496 l2cap->bdaddr.b[5], l2cap->bdaddr.b[4],
497 l2cap->bdaddr.b[3], l2cap->bdaddr.b[2],
498 l2cap->bdaddr.b[1], l2cap->bdaddr.b[0],
500 (l2cap->hci != NULL)?
501 NG_L2CAP_HOOK_HCI : "",
502 (l2cap->l2c != NULL)?
503 NG_L2CAP_HOOK_L2C : "",
504 (l2cap->ctl != NULL)?
505 NG_L2CAP_HOOK_CTL : "",
515 /* Messages from the upper layer or directed to the local node */
516 case NGM_L2CAP_COOKIE:
517 switch (msg->header.cmd) {
519 case NGM_L2CAP_NODE_GET_FLAGS:
520 NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_flags_ep),
525 *((ng_l2cap_node_flags_ep *)(rsp->data)) =
530 case NGM_L2CAP_NODE_GET_DEBUG:
531 NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_debug_ep),
536 *((ng_l2cap_node_debug_ep *)(rsp->data)) =
541 case NGM_L2CAP_NODE_SET_DEBUG:
542 if (msg->header.arglen !=
543 sizeof(ng_l2cap_node_debug_ep))
547 *((ng_l2cap_node_debug_ep *)(msg->data));
550 /* Get connection list */
551 case NGM_L2CAP_NODE_GET_CON_LIST: {
552 ng_l2cap_con_p con = NULL;
553 ng_l2cap_node_con_list_ep *e1 = NULL;
554 ng_l2cap_node_con_ep *e2 = NULL;
557 /* Count number of connections */
558 LIST_FOREACH(con, &l2cap->con_list, next)
560 if (n > NG_L2CAP_MAX_CON_NUM)
561 n = NG_L2CAP_MAX_CON_NUM;
563 /* Prepare response */
564 NG_MKRESPONSE(rsp, msg,
565 sizeof(*e1) + n * sizeof(*e2), M_NOWAIT);
571 e1 = (ng_l2cap_node_con_list_ep *)(rsp->data);
572 e2 = (ng_l2cap_node_con_ep *)(e1 + 1);
574 e1->num_connections = n;
576 LIST_FOREACH(con, &l2cap->con_list, next) {
577 e2->state = con->state;
579 e2->flags = con->flags;
580 if (con->tx_pkt != NULL)
581 e2->flags |= NG_L2CAP_CON_TX;
582 if (con->rx_pkt != NULL)
583 e2->flags |= NG_L2CAP_CON_RX;
585 e2->pending = con->pending;
587 e2->con_handle = con->con_handle;
588 bcopy(&con->remote, &e2->remote,
597 /* Get channel list */
598 case NGM_L2CAP_NODE_GET_CHAN_LIST: {
599 ng_l2cap_chan_p ch = NULL;
600 ng_l2cap_node_chan_list_ep *e1 = NULL;
601 ng_l2cap_node_chan_ep *e2 = NULL;
604 /* Count number of channels */
605 LIST_FOREACH(ch, &l2cap->chan_list, next)
607 if (n > NG_L2CAP_MAX_CHAN_NUM)
608 n = NG_L2CAP_MAX_CHAN_NUM;
610 /* Prepare response */
611 NG_MKRESPONSE(rsp, msg,
612 sizeof(ng_l2cap_node_chan_list_ep) +
613 n * sizeof(ng_l2cap_node_chan_ep), M_NOWAIT);
619 e1 = (ng_l2cap_node_chan_list_ep *)(rsp->data);
620 e2 = (ng_l2cap_node_chan_ep *)(e1 + 1);
622 e1->num_channels = n;
624 LIST_FOREACH(ch, &l2cap->chan_list, next) {
625 e2->state = ch->state;
634 bcopy(&ch->con->remote, &e2->remote,
643 case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
644 NG_MKRESPONSE(rsp, msg,
645 sizeof(ng_l2cap_node_auto_discon_ep), M_NOWAIT);
649 *((ng_l2cap_node_auto_discon_ep *)(rsp->data)) =
653 case NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO:
654 if (msg->header.arglen !=
655 sizeof(ng_l2cap_node_auto_discon_ep))
659 *((ng_l2cap_node_auto_discon_ep *)
674 NG_RESPOND_MSG(error, node, item, rsp);
678 } /* ng_l2cap_rcvmsg */
681 * Process data packet from one of our hooks.
683 * From the HCI hook we expect to receive ACL data packets. ACL data packets
684 * gets re-assembled into one L2CAP packet (according to length) and then gets
687 * NOTE: We expect to receive L2CAP packet header in the first fragment.
688 * Otherwise we WILL NOT be able to get length of the L2CAP packet.
690 * Signaling L2CAP packets (destination channel ID == 0x1) are processed within
691 * the node. Connectionless data packets (destination channel ID == 0x2) will
692 * be forwarded to appropriate upstream hook unless it is not connected or
693 * connectionless traffic for the specified PSM was disabled.
695 * From the upstream hooks we expect to receive data packets. These data
696 * packets will be converted into L2CAP data packets. The length of each
697 * L2CAP packet must not exceed channel's omtu (our peer's imtu). Then
698 * these L2CAP packets will be converted to ACL data packets (according to
699 * HCI layer MTU) and sent to lower layer.
701 * No data is expected from the control hook.
705 ng_l2cap_rcvdata(hook_p hook, item_p item)
707 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
708 struct mbuf *m = NULL;
711 /* Detach mbuf, discard item and process data */
715 if (hook == l2cap->hci)
716 error = ng_l2cap_lp_receive(l2cap, m);
717 else if (hook == l2cap->l2c)
718 error = ng_l2cap_l2ca_write_req(l2cap, m);
725 } /* ng_l2cap_rcvdata */
728 * Clean all connections, channels and commands for the L2CAP node
732 ng_l2cap_cleanup(ng_l2cap_p l2cap)
734 ng_l2cap_con_p con = NULL;
736 /* Clean up connection and channels */
737 while (!LIST_EMPTY(&l2cap->con_list)) {
738 con = LIST_FIRST(&l2cap->con_list);
740 if (con->flags & NG_L2CAP_CON_LP_TIMO)
741 ng_l2cap_lp_untimeout(con);
742 else if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)
743 ng_l2cap_discon_untimeout(con);
745 /* Connection terminated by local host */
746 ng_l2cap_con_fail(con, 0x16);
748 } /* ng_l2cap_cleanup */
751 * Destroy all channels that use specified upstream hook
755 ng_l2cap_destroy_channels(ng_l2cap_p l2cap)
757 while (!LIST_EMPTY(&l2cap->chan_list))
758 ng_l2cap_free_chan(LIST_FIRST(&l2cap->chan_list));
759 } /* ng_l2cap_destroy_channels_by_hook */