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 $
31 * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_main.c,v 1.5 2005/01/07 01:45:43 imp Exp $
32 * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_main.c,v 1.2 2008/06/26 23:05:40 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include "ng_message.h"
44 #include "bluetooth/include/ng_bluetooth.h"
45 #include "bluetooth/include/ng_hci.h"
46 #include "bluetooth/include/ng_l2cap.h"
47 #include "bluetooth/l2cap/ng_l2cap_var.h"
48 #include "bluetooth/l2cap/ng_l2cap_cmds.h"
49 #include "bluetooth/l2cap/ng_l2cap_evnt.h"
50 #include "bluetooth/l2cap/ng_l2cap_llpi.h"
51 #include "bluetooth/l2cap/ng_l2cap_ulpi.h"
52 #include "bluetooth/l2cap/ng_l2cap_misc.h"
53 #include "bluetooth/l2cap/ng_l2cap_prse.h"
55 /******************************************************************************
56 ******************************************************************************
57 ** This node implements Link Layer Control and Adaptation Protocol (L2CAP)
58 ******************************************************************************
59 ******************************************************************************/
62 #ifdef NG_SEPARATE_MALLOC
63 MALLOC_DEFINE(M_NETGRAPH_L2CAP, "netgraph_l2cap",
64 "Netgraph Bluetooth L2CAP node");
66 #define M_NETGRAPH_L2CAP M_NETGRAPH
67 #endif /* NG_SEPARATE_MALLOC */
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;
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,
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);
98 /*****************************************************************************
99 *****************************************************************************
100 ** Netgraph methods implementation
101 *****************************************************************************
102 *****************************************************************************/
104 static void ng_l2cap_cleanup (ng_l2cap_p);
105 static void ng_l2cap_destroy_channels (ng_l2cap_p);
108 * Create new instance of L2CAP node
112 ng_l2cap_constructor(node_p node)
114 ng_l2cap_p l2cap = NULL;
116 /* Create new L2CAP node */
117 l2cap = kmalloc(sizeof(*l2cap), M_NETGRAPH_L2CAP,
118 M_WAITOK | M_NULLOK | M_ZERO);
123 l2cap->debug = NG_L2CAP_WARN_LEVEL;
124 l2cap->discon_timo = 5; /* sec */
126 LIST_INIT(&l2cap->con_list);
127 LIST_INIT(&l2cap->chan_list);
129 NG_NODE_SET_PRIVATE(node, l2cap);
130 NG_NODE_FORCE_WRITER(node);
133 } /* ng_l2cap_constructor */
136 * Shutdown L2CAP node
140 ng_l2cap_shutdown(node_p node)
142 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
144 NG_NODE_SET_PRIVATE(node, NULL);
147 /* Clean up L2CAP node. Delete all connection, channels and commands */
149 ng_l2cap_cleanup(l2cap);
151 bzero(l2cap, sizeof(*l2cap));
152 kfree(l2cap, M_NETGRAPH_L2CAP);
155 } /* ng_l2cap_shutdown */
158 * Give our OK for a hook to be added. HCI layer is connected to the HCI
159 * (NG_L2CAP_HOOK_HCI) hook. As per specification L2CAP layer MUST provide
160 * Procol/Service Multiplexing, so the L2CAP node provides separate hooks
161 * for SDP (NG_L2CAP_HOOK_SDP), RFCOMM (NG_L2CAP_HOOK_RFCOMM) and TCP
162 * (NG_L2CAP_HOOK_TCP) protcols. Unknown PSM will be forwarded to
163 * NG_L2CAP_HOOK_ORPHAN hook. Control node/application is connected to
164 * control (NG_L2CAP_HOOK_CTL) hook.
168 ng_l2cap_newhook(node_p node, hook_p hook, char const *name)
170 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
173 if (strcmp(name, NG_L2CAP_HOOK_HCI) == 0)
175 else if (strcmp(name, NG_L2CAP_HOOK_L2C) == 0)
177 else if (strcmp(name, NG_L2CAP_HOOK_CTL) == 0)
188 } /* ng_l2cap_newhook */
191 * Give our final OK to connect hook. Nothing to do here.
195 ng_l2cap_connect(hook_p hook)
197 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
200 if (hook == l2cap->hci)
201 NG_HOOK_SET_RCVMSG(hook, ng_l2cap_lower_rcvmsg);
203 if (hook == l2cap->l2c || hook == l2cap->ctl) {
204 NG_HOOK_SET_RCVMSG(hook, ng_l2cap_upper_rcvmsg);
206 /* Send delayed notification to the upper layer */
207 error = ng_send_fn(l2cap->node, hook, ng_l2cap_send_hook_info,
213 } /* ng_l2cap_connect */
216 * Disconnect the hook. For downstream hook we must notify upper layers.
218 * XXX For upstream hooks this is really ugly :( Hook was disconnected and it
219 * XXX is now too late to do anything. For now we just clean up our own mess
220 * XXX and remove all channels that use disconnected upstream hook. If we don't
221 * XXX do that then L2CAP node can get out of sync with upper layers.
222 * XXX No notification will be sent to remote peer.
226 ng_l2cap_disconnect(hook_p hook)
228 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
231 if (hook == l2cap->hci) {
232 ng_l2cap_cleanup(l2cap);
235 if (hook == l2cap->l2c) {
236 ng_l2cap_destroy_channels(l2cap);
239 if (hook == l2cap->ctl)
246 /* Shutdown when all hooks are disconnected */
247 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
248 NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
249 ng_rmnode_self(NG_HOOK_NODE(hook));
252 } /* ng_l2cap_disconnect */
255 * Process control message from lower layer
259 ng_l2cap_lower_rcvmsg(node_p node, item_p item, hook_p lasthook)
261 ng_l2cap_p l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
262 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
265 switch (msg->header.typecookie) {
267 switch (msg->header.cmd) {
268 /* HCI node is ready */
269 case NGM_HCI_NODE_UP: {
270 ng_hci_node_up_ep *ep = NULL;
272 if (msg->header.arglen != sizeof(*ep))
275 ep = (ng_hci_node_up_ep *)(msg->data);
278 "%s: %s - HCI node is up, bdaddr: %x:%x:%x:%x:%x:%x, " \
279 "pkt_size=%d bytes, num_pkts=%d\n", __func__, NG_NODE_NAME(l2cap->node),
280 ep->bdaddr.b[5], ep->bdaddr.b[4],
281 ep->bdaddr.b[3], ep->bdaddr.b[2],
282 ep->bdaddr.b[1], ep->bdaddr.b[0],
283 ep->pkt_size, ep->num_pkts);
285 bcopy(&ep->bdaddr, &l2cap->bdaddr,
286 sizeof(l2cap->bdaddr));
287 l2cap->pkt_size = ep->pkt_size;
288 l2cap->num_pkts = ep->num_pkts;
290 /* Notify upper layers */
291 ng_l2cap_send_hook_info(l2cap->node,
292 l2cap->l2c, NULL, 0);
293 ng_l2cap_send_hook_info(l2cap->node,
294 l2cap->ctl, NULL, 0);
298 case NGM_HCI_SYNC_CON_QUEUE: {
299 ng_hci_sync_con_queue_ep *ep = NULL;
300 ng_l2cap_con_p con = NULL;
302 if (msg->header.arglen != sizeof(*ep))
305 ep = (ng_hci_sync_con_queue_ep *)(msg->data);
306 con = ng_l2cap_con_by_handle(l2cap,
312 "%s: %s - sync HCI connection queue, con_handle=%d, pending=%d, completed=%d\n",
313 __func__, NG_NODE_NAME(l2cap->node),
314 ep->con_handle, con->pending,
317 con->pending -= ep->completed;
318 if (con->pending < 0) {
320 "%s: %s - pending packet counter is out of sync! " \
321 "con_handle=%d, pending=%d, completed=%d\n", __func__,
322 NG_NODE_NAME(l2cap->node),
323 con->con_handle, con->pending,
329 ng_l2cap_lp_deliver(con);
333 /* LP_ConnectCfm[Neg] */
334 case NGM_HCI_LP_CON_CFM:
335 error = ng_l2cap_lp_con_cfm(l2cap, msg);
339 case NGM_HCI_LP_CON_IND:
340 error = ng_l2cap_lp_con_ind(l2cap, msg);
343 /* LP_DisconnectInd */
344 case NGM_HCI_LP_DISCON_IND:
345 error = ng_l2cap_lp_discon_ind(l2cap, msg);
348 /* LP_QoSSetupCfm[Neg] */
349 case NGM_HCI_LP_QOS_CFM:
350 error = ng_l2cap_lp_qos_cfm(l2cap, msg);
353 /* LP_OoSViolationInd */
354 case NGM_HCI_LP_QOS_IND:
355 error = ng_l2cap_lp_qos_ind(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_WAITOK | M_NULLOK);
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),
521 M_WAITOK | M_NULLOK);
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),
532 M_WAITOK | M_NULLOK);
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_WAITOK | M_NULLOK);
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_WAITOK | M_NULLOK);
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_WAITOK | M_NULLOK);
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 */