6 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: ng_l2cap_main.c,v 1.2 2003/04/28 21:44:59 max Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/queue.h>
40 #include <netgraph/ng_message.h>
41 #include <netgraph/netgraph.h>
42 #include <netgraph/ng_parse.h>
43 #include <netgraph/bluetooth/include/ng_bluetooth.h>
44 #include <netgraph/bluetooth/include/ng_hci.h>
45 #include <netgraph/bluetooth/include/ng_l2cap.h>
46 #include <netgraph/bluetooth/l2cap/ng_l2cap_var.h>
47 #include <netgraph/bluetooth/l2cap/ng_l2cap_cmds.h>
48 #include <netgraph/bluetooth/l2cap/ng_l2cap_evnt.h>
49 #include <netgraph/bluetooth/l2cap/ng_l2cap_llpi.h>
50 #include <netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h>
51 #include <netgraph/bluetooth/l2cap/ng_l2cap_misc.h>
52 #include <netgraph/bluetooth/l2cap/ng_l2cap_prse.h>
54 /******************************************************************************
55 ******************************************************************************
56 ** This node implements Link Layer Control and Adaptation Protocol (L2CAP)
57 ******************************************************************************
58 ******************************************************************************/
61 #ifdef NG_SEPARATE_MALLOC
62 MALLOC_DEFINE(M_NETGRAPH_L2CAP, "netgraph_l2cap",
63 "Netgraph Bluetooth L2CAP node");
65 #define M_NETGRAPH_L2CAP M_NETGRAPH
66 #endif /* NG_SEPARATE_MALLOC */
68 /* Netgraph node methods */
69 static ng_constructor_t ng_l2cap_constructor;
70 static ng_shutdown_t ng_l2cap_shutdown;
71 static ng_newhook_t ng_l2cap_newhook;
72 static ng_connect_t ng_l2cap_connect;
73 static ng_disconnect_t ng_l2cap_disconnect;
74 static ng_rcvmsg_t ng_l2cap_lower_rcvmsg;
75 static ng_rcvmsg_t ng_l2cap_upper_rcvmsg;
76 static ng_rcvmsg_t ng_l2cap_default_rcvmsg;
77 static ng_rcvdata_t ng_l2cap_rcvdata;
79 /* Netgraph node type descriptor */
80 static struct ng_type typestruct = {
81 .version = NG_ABI_VERSION,
82 .name = NG_L2CAP_NODE_TYPE,
83 .constructor = ng_l2cap_constructor,
84 .rcvmsg = ng_l2cap_default_rcvmsg,
85 .shutdown = ng_l2cap_shutdown,
86 .newhook = ng_l2cap_newhook,
87 .connect = ng_l2cap_connect,
88 .rcvdata = ng_l2cap_rcvdata,
89 .disconnect = ng_l2cap_disconnect,
90 .cmdlist = ng_l2cap_cmdlist,
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);
97 /*****************************************************************************
98 *****************************************************************************
99 ** Netgraph methods implementation
100 *****************************************************************************
101 *****************************************************************************/
103 static void ng_l2cap_cleanup (ng_l2cap_p);
104 static void ng_l2cap_destroy_channels (ng_l2cap_p);
107 * Create new instance of L2CAP node
111 ng_l2cap_constructor(node_p node)
113 ng_l2cap_p l2cap = NULL;
115 /* Create new L2CAP node */
116 l2cap = malloc(sizeof(*l2cap),
117 M_NETGRAPH_L2CAP, M_NOWAIT|M_ZERO);
122 l2cap->debug = NG_L2CAP_WARN_LEVEL;
123 l2cap->discon_timo = 5; /* sec */
125 LIST_INIT(&l2cap->con_list);
126 LIST_INIT(&l2cap->chan_list);
128 NG_NODE_SET_PRIVATE(node, l2cap);
129 NG_NODE_FORCE_WRITER(node);
132 } /* ng_l2cap_constructor */
135 * Shutdown L2CAP node
139 ng_l2cap_shutdown(node_p node)
141 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
143 NG_NODE_SET_PRIVATE(node, NULL);
146 /* Clean up L2CAP node. Delete all connection, channels and commands */
148 ng_l2cap_cleanup(l2cap);
150 bzero(l2cap, sizeof(*l2cap));
151 free(l2cap, M_NETGRAPH_L2CAP);
154 } /* ng_l2cap_shutdown */
157 * Give our OK for a hook to be added. HCI layer is connected to the HCI
158 * (NG_L2CAP_HOOK_HCI) hook. As per specification L2CAP layer MUST provide
159 * Procol/Service Multiplexing, so the L2CAP node provides separate hooks
160 * for SDP (NG_L2CAP_HOOK_SDP), RFCOMM (NG_L2CAP_HOOK_RFCOMM) and TCP
161 * (NG_L2CAP_HOOK_TCP) protcols. Unknown PSM will be forwarded to
162 * NG_L2CAP_HOOK_ORPHAN hook. Control node/application is connected to
163 * control (NG_L2CAP_HOOK_CTL) hook.
167 ng_l2cap_newhook(node_p node, hook_p hook, char const *name)
169 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
172 if (strcmp(name, NG_L2CAP_HOOK_HCI) == 0)
174 else if (strcmp(name, NG_L2CAP_HOOK_L2C) == 0)
176 else if (strcmp(name, NG_L2CAP_HOOK_CTL) == 0)
187 } /* ng_l2cap_newhook */
190 * Give our final OK to connect hook. Nothing to do here.
194 ng_l2cap_connect(hook_p hook)
196 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
199 if (hook == l2cap->hci)
200 NG_HOOK_SET_RCVMSG(hook, ng_l2cap_lower_rcvmsg);
202 if (hook == l2cap->l2c || hook == l2cap->ctl) {
203 NG_HOOK_SET_RCVMSG(hook, ng_l2cap_upper_rcvmsg);
205 /* Send delayed notification to the upper layer */
206 error = ng_send_fn(l2cap->node, hook, ng_l2cap_send_hook_info,
212 } /* ng_l2cap_connect */
215 * Disconnect the hook. For downstream hook we must notify upper layers.
217 * XXX For upstream hooks this is really ugly :( Hook was disconnected and it
218 * XXX is now too late to do anything. For now we just clean up our own mess
219 * XXX and remove all channels that use disconnected upstream hook. If we don't
220 * XXX do that then L2CAP node can get out of sync with upper layers.
221 * XXX No notification will be sent to remote peer.
225 ng_l2cap_disconnect(hook_p hook)
227 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
230 if (hook == l2cap->hci) {
231 ng_l2cap_cleanup(l2cap);
234 if (hook == l2cap->l2c) {
235 ng_l2cap_destroy_channels(l2cap);
238 if (hook == l2cap->ctl)
245 /* Shutdown when all hooks are disconnected */
246 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
247 NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
248 ng_rmnode_self(NG_HOOK_NODE(hook));
251 } /* ng_l2cap_disconnect */
254 * Process control message from lower layer
258 ng_l2cap_lower_rcvmsg(node_p node, item_p item, hook_p lasthook)
260 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
261 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
264 switch (msg->header.typecookie) {
266 switch (msg->header.cmd) {
267 /* HCI node is ready */
268 case NGM_HCI_NODE_UP: {
269 ng_hci_node_up_ep *ep = NULL;
271 if (msg->header.arglen != sizeof(*ep))
274 ep = (ng_hci_node_up_ep *)(msg->data);
277 "%s: %s - HCI node is up, bdaddr: %x:%x:%x:%x:%x:%x, " \
278 "pkt_size=%d bytes, num_pkts=%d\n", __func__, NG_NODE_NAME(l2cap->node),
279 ep->bdaddr.b[5], ep->bdaddr.b[4],
280 ep->bdaddr.b[3], ep->bdaddr.b[2],
281 ep->bdaddr.b[1], ep->bdaddr.b[0],
282 ep->pkt_size, ep->num_pkts);
284 bcopy(&ep->bdaddr, &l2cap->bdaddr,
285 sizeof(l2cap->bdaddr));
286 l2cap->pkt_size = ep->pkt_size;
287 l2cap->num_pkts = ep->num_pkts;
289 /* Notify upper layers */
290 ng_l2cap_send_hook_info(l2cap->node,
291 l2cap->l2c, NULL, 0);
292 ng_l2cap_send_hook_info(l2cap->node,
293 l2cap->ctl, NULL, 0);
297 case NGM_HCI_SYNC_CON_QUEUE: {
298 ng_hci_sync_con_queue_ep *ep = NULL;
299 ng_l2cap_con_p con = NULL;
301 if (msg->header.arglen != sizeof(*ep))
304 ep = (ng_hci_sync_con_queue_ep *)(msg->data);
305 con = ng_l2cap_con_by_handle(l2cap,
311 "%s: %s - sync HCI connection queue, con_handle=%d, pending=%d, completed=%d\n",
312 __func__, NG_NODE_NAME(l2cap->node),
313 ep->con_handle, con->pending,
316 con->pending -= ep->completed;
317 if (con->pending < 0) {
319 "%s: %s - pending packet counter is out of sync! " \
320 "con_handle=%d, pending=%d, completed=%d\n", __func__,
321 NG_NODE_NAME(l2cap->node),
322 con->con_handle, con->pending,
328 ng_l2cap_lp_deliver(con);
332 /* LP_ConnectCfm[Neg] */
333 case NGM_HCI_LP_CON_CFM:
334 error = ng_l2cap_lp_con_cfm(l2cap, msg);
338 case NGM_HCI_LP_CON_IND:
339 error = ng_l2cap_lp_con_ind(l2cap, msg);
342 /* LP_DisconnectInd */
343 case NGM_HCI_LP_DISCON_IND:
344 error = ng_l2cap_lp_discon_ind(l2cap, msg);
347 /* LP_QoSSetupCfm[Neg] */
348 case NGM_HCI_LP_QOS_CFM:
349 error = ng_l2cap_lp_qos_cfm(l2cap, msg);
352 /* LP_OoSViolationInd */
353 case NGM_HCI_LP_QOS_IND:
354 error = ng_l2cap_lp_qos_ind(l2cap, msg);
364 return (ng_l2cap_default_rcvmsg(node, item, lasthook));
371 } /* ng_l2cap_lower_rcvmsg */
374 * Process control message from upper layer
378 ng_l2cap_upper_rcvmsg(node_p node, item_p item, hook_p lasthook)
380 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
381 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
384 switch (msg->header.typecookie) {
385 case NGM_L2CAP_COOKIE:
386 switch (msg->header.cmd) {
388 case NGM_L2CAP_L2CA_CON:
389 error = ng_l2cap_l2ca_con_req(l2cap, msg);
392 /* L2CA_ConnectRsp */
393 case NGM_L2CAP_L2CA_CON_RSP:
394 error = ng_l2cap_l2ca_con_rsp_req(l2cap, msg);
398 case NGM_L2CAP_L2CA_CFG:
399 error = ng_l2cap_l2ca_cfg_req(l2cap, msg);
403 case NGM_L2CAP_L2CA_CFG_RSP:
404 error = ng_l2cap_l2ca_cfg_rsp_req(l2cap, msg);
407 /* L2CA_Disconnect */
408 case NGM_L2CAP_L2CA_DISCON:
409 error = ng_l2cap_l2ca_discon_req(l2cap, msg);
412 /* L2CA_GroupCreate */
413 case NGM_L2CAP_L2CA_GRP_CREATE:
414 error = ng_l2cap_l2ca_grp_create(l2cap, msg);
417 /* L2CA_GroupClose */
418 case NGM_L2CAP_L2CA_GRP_CLOSE:
419 error = ng_l2cap_l2ca_grp_close(l2cap, msg);
422 /* L2CA_GroupAddMember */
423 case NGM_L2CAP_L2CA_GRP_ADD_MEMBER:
424 error = ng_l2cap_l2ca_grp_add_member_req(l2cap, msg);
427 /* L2CA_GroupDeleteMember */
428 case NGM_L2CAP_L2CA_GRP_REM_MEMBER:
429 error = ng_l2cap_l2ca_grp_rem_member(l2cap, msg);
432 /* L2CA_GroupMembership */
433 case NGM_L2CAP_L2CA_GRP_MEMBERSHIP:
434 error = ng_l2cap_l2ca_grp_get_members(l2cap, msg);
438 case NGM_L2CAP_L2CA_PING:
439 error = ng_l2cap_l2ca_ping_req(l2cap, msg);
443 case NGM_L2CAP_L2CA_GET_INFO:
444 error = ng_l2cap_l2ca_get_info_req(l2cap, msg);
448 case NGM_L2CAP_L2CA_ENABLE_CLT:
449 error = ng_l2cap_l2ca_enable_clt(l2cap, msg);
453 return (ng_l2cap_default_rcvmsg(node, item, lasthook));
459 return (ng_l2cap_default_rcvmsg(node, item, lasthook));
466 } /* ng_l2cap_upper_rcvmsg */
469 * Default control message processing routine
473 ng_l2cap_default_rcvmsg(node_p node, item_p item, hook_p lasthook)
475 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
476 struct ng_mesg *msg = NULL, *rsp = NULL;
479 /* Detach and process message */
480 NGI_GET_MSG(item, msg);
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);
490 snprintf(rsp->data, NG_TEXTRESPONSE,
491 "bdaddr %x:%x:%x:%x:%x:%x, " \
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],
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 : "",
514 /* Messages from the upper layer or directed to the local node */
515 case NGM_L2CAP_COOKIE:
516 switch (msg->header.cmd) {
518 case NGM_L2CAP_NODE_GET_FLAGS:
519 NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_flags_ep),
524 *((ng_l2cap_node_flags_ep *)(rsp->data)) =
529 case NGM_L2CAP_NODE_GET_DEBUG:
530 NG_MKRESPONSE(rsp, msg, sizeof(ng_l2cap_node_debug_ep),
535 *((ng_l2cap_node_debug_ep *)(rsp->data)) =
540 case NGM_L2CAP_NODE_SET_DEBUG:
541 if (msg->header.arglen !=
542 sizeof(ng_l2cap_node_debug_ep))
546 *((ng_l2cap_node_debug_ep *)(msg->data));
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;
556 /* Count number of connections */
557 LIST_FOREACH(con, &l2cap->con_list, next)
559 if (n > NG_L2CAP_MAX_CON_NUM)
560 n = NG_L2CAP_MAX_CON_NUM;
562 /* Prepare response */
563 NG_MKRESPONSE(rsp, msg,
564 sizeof(*e1) + n * sizeof(*e2), M_NOWAIT);
570 e1 = (ng_l2cap_node_con_list_ep *)(rsp->data);
571 e2 = (ng_l2cap_node_con_ep *)(e1 + 1);
573 e1->num_connections = n;
575 LIST_FOREACH(con, &l2cap->con_list, next) {
576 e2->state = con->state;
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;
584 e2->pending = con->pending;
586 e2->con_handle = con->con_handle;
587 bcopy(&con->remote, &e2->remote,
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;
603 /* Count number of channels */
604 LIST_FOREACH(ch, &l2cap->chan_list, next)
606 if (n > NG_L2CAP_MAX_CHAN_NUM)
607 n = NG_L2CAP_MAX_CHAN_NUM;
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);
618 e1 = (ng_l2cap_node_chan_list_ep *)(rsp->data);
619 e2 = (ng_l2cap_node_chan_ep *)(e1 + 1);
621 e1->num_channels = n;
623 LIST_FOREACH(ch, &l2cap->chan_list, next) {
624 e2->state = ch->state;
633 bcopy(&ch->con->remote, &e2->remote,
642 case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
643 NG_MKRESPONSE(rsp, msg,
644 sizeof(ng_l2cap_node_auto_discon_ep), M_NOWAIT);
648 *((ng_l2cap_node_auto_discon_ep *)(rsp->data)) =
652 case NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO:
653 if (msg->header.arglen !=
654 sizeof(ng_l2cap_node_auto_discon_ep))
658 *((ng_l2cap_node_auto_discon_ep *)
673 NG_RESPOND_MSG(error, node, item, rsp);
677 } /* ng_l2cap_rcvmsg */
680 * Process data packet from one of our hooks.
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
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.
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.
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.
700 * No data is expected from the control hook.
704 ng_l2cap_rcvdata(hook_p hook, item_p item)
706 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
707 struct mbuf *m = NULL;
710 /* Detach mbuf, discard item and process data */
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);
724 } /* ng_l2cap_rcvdata */
727 * Clean all connections, channels and commands for the L2CAP node
731 ng_l2cap_cleanup(ng_l2cap_p l2cap)
733 ng_l2cap_con_p con = NULL;
735 /* Clean up connection and channels */
736 while (!LIST_EMPTY(&l2cap->con_list)) {
737 con = LIST_FIRST(&l2cap->con_list);
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);
744 /* Connection terminated by local host */
745 ng_l2cap_con_fail(con, 0x16);
747 } /* ng_l2cap_cleanup */
750 * Destroy all channels that use specified upstream hook
754 ng_l2cap_destroy_channels(ng_l2cap_p l2cap)
756 while (!LIST_EMPTY(&l2cap->chan_list))
757 ng_l2cap_free_chan(LIST_FIRST(&l2cap->chan_list));
758 } /* ng_l2cap_destroy_channels_by_hook */