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_llpi.c,v 1.5 2003/09/08 19:11:45 max Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/endian.h>
40 #include <sys/malloc.h>
42 #include <sys/queue.h>
43 #include <netgraph/ng_message.h>
44 #include <netgraph/netgraph.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>
55 /******************************************************************************
56 ******************************************************************************
57 ** Lower Layer Protocol (HCI) Interface module
58 ******************************************************************************
59 ******************************************************************************/
62 * Send LP_ConnectReq event to the lower layer protocol. Create new connection
63 * descriptor and initialize it. Create LP_ConnectReq event and send it to the
64 * lower layer, then adjust connection state and start timer. The function WILL
65 * FAIL if connection to the remote unit already exists.
69 ng_l2cap_lp_con_req(ng_l2cap_p l2cap, bdaddr_p bdaddr, int type)
71 struct ng_mesg *msg = NULL;
72 ng_hci_lp_con_req_ep *ep = NULL;
73 ng_l2cap_con_p con = NULL;
76 /* Verify that we DO NOT have connection to the remote unit */
77 con = ng_l2cap_con_by_addr(l2cap, bdaddr, type);
80 "%s: %s - unexpected LP_ConnectReq event. " \
81 "Connection already exists, state=%d, con_handle=%d\n",
82 __func__, NG_NODE_NAME(l2cap->node), con->state,
88 /* Check if lower layer protocol is still connected */
89 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
91 "%s: %s - hook \"%s\" is not connected or valid\n",
92 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
97 /* Create and intialize new connection descriptor */
98 con = ng_l2cap_new_con(l2cap, bdaddr, type);
102 /* Create and send LP_ConnectReq event */
103 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_LP_CON_REQ,
104 sizeof(*ep), M_NOWAIT);
106 ng_l2cap_free_con(con);
111 ep = (ng_hci_lp_con_req_ep *) (msg->data);
112 bcopy(bdaddr, &ep->bdaddr, sizeof(ep->bdaddr));
113 ep->link_type = type;
115 con->flags |= NG_L2CAP_CON_OUTGOING;
116 con->state = NG_L2CAP_W4_LP_CON_CFM;
117 ng_l2cap_lp_timeout(con);
119 NG_SEND_MSG_HOOK(error, l2cap->node, msg, l2cap->hci, 0);
121 if (ng_l2cap_lp_untimeout(con) == 0)
122 ng_l2cap_free_con(con);
125 * Do not free connection if ng_l2cap_lp_untimeout() failed
126 * let timeout handler deal with it. Always return error to
132 } /* ng_l2cap_lp_con_req */
135 * Process LP_ConnectCfm event from the lower layer protocol. It could be
136 * positive or negative. Verify remote unit address then stop the timer and
141 ng_l2cap_lp_con_cfm(ng_l2cap_p l2cap, struct ng_mesg *msg)
143 ng_hci_lp_con_cfm_ep *ep = NULL;
144 ng_l2cap_con_p con = NULL;
148 if (msg->header.arglen != sizeof(*ep)) {
150 "%s: %s - invalid LP_ConnectCfm[Neg] message size\n",
151 __func__, NG_NODE_NAME(l2cap->node));
156 ep = (ng_hci_lp_con_cfm_ep *) (msg->data);
157 /* Check if we have requested/accepted this connection */
158 con = ng_l2cap_con_by_addr(l2cap, &ep->bdaddr, ep->link_type);
161 "%s: %s - unexpected LP_ConnectCfm event. Connection does not exist\n",
162 __func__, NG_NODE_NAME(l2cap->node));
167 /* Check connection state */
168 if (con->state != NG_L2CAP_W4_LP_CON_CFM) {
170 "%s: %s - unexpected LP_ConnectCfm event. " \
171 "Invalid connection state, state=%d, con_handle=%d\n",
172 __func__, NG_NODE_NAME(l2cap->node), con->state,
179 * Looks like it is our confirmation. It is safe now to cancel
180 * connection timer and notify upper layer. If timeout already
181 * happened then ignore connection confirmation and let timeout
185 if ((error = ng_l2cap_lp_untimeout(con)) != 0)
188 if (ep->status == 0) {
189 con->state = NG_L2CAP_CON_OPEN;
190 con->con_handle = ep->con_handle;
191 ng_l2cap_lp_deliver(con);
192 } else /* Negative confirmation - remove connection descriptor */
193 ng_l2cap_con_fail(con, ep->status);
196 } /* ng_l2cap_lp_con_cfm */
199 * Process LP_ConnectInd event from the lower layer protocol. This is a good
200 * place to put some extra check on remote unit address and/or class. We could
201 * even forward this information to control hook (or check against internal
202 * black list) and thus implement some kind of firewall. But for now be simple
203 * and create new connection descriptor, start timer and send LP_ConnectRsp
204 * event (i.e. accept connection).
208 ng_l2cap_lp_con_ind(ng_l2cap_p l2cap, struct ng_mesg *msg)
210 ng_hci_lp_con_ind_ep *ep = NULL;
211 ng_hci_lp_con_rsp_ep *rp = NULL;
212 struct ng_mesg *rsp = NULL;
213 ng_l2cap_con_p con = NULL;
217 if (msg->header.arglen != sizeof(*ep)) {
219 "%s: %s - invalid LP_ConnectInd message size\n",
220 __func__, NG_NODE_NAME(l2cap->node));
225 ep = (ng_hci_lp_con_ind_ep *) (msg->data);
227 /* Make sure we have only one connection to the remote unit */
228 con = ng_l2cap_con_by_addr(l2cap, &ep->bdaddr, ep->link_type);
231 "%s: %s - unexpected LP_ConnectInd event. " \
232 "Connection already exists, state=%d, con_handle=%d\n",
233 __func__, NG_NODE_NAME(l2cap->node), con->state,
239 /* Check if lower layer protocol is still connected */
240 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
242 "%s: %s - hook \"%s\" is not connected or valid",
243 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
248 /* Create and intialize new connection descriptor */
249 con = ng_l2cap_new_con(l2cap, &ep->bdaddr, ep->link_type);
253 /* Create and send LP_ConnectRsp event */
254 NG_MKMESSAGE(rsp, NGM_HCI_COOKIE, NGM_HCI_LP_CON_RSP,
255 sizeof(*rp), M_NOWAIT);
257 ng_l2cap_free_con(con);
262 rp = (ng_hci_lp_con_rsp_ep *)(rsp->data);
263 rp->status = 0x00; /* accept connection */
264 rp->link_type = NG_HCI_LINK_ACL;
265 bcopy(&ep->bdaddr, &rp->bdaddr, sizeof(rp->bdaddr));
267 con->state = NG_L2CAP_W4_LP_CON_CFM;
268 ng_l2cap_lp_timeout(con);
270 NG_SEND_MSG_HOOK(error, l2cap->node, rsp, l2cap->hci, 0);
272 if (ng_l2cap_lp_untimeout(con) == 0)
273 ng_l2cap_free_con(con);
276 * Do not free connection if ng_l2cap_lp_untimeout() failed
277 * let timeout handler deal with it. Always return error to
283 } /* ng_l2cap_lp_con_ind */
286 * Process LP_DisconnectInd event from the lower layer protocol. We have been
287 * disconnected from the remote unit. So notify the upper layer protocol.
291 ng_l2cap_lp_discon_ind(ng_l2cap_p l2cap, struct ng_mesg *msg)
293 ng_hci_lp_discon_ind_ep *ep = NULL;
294 ng_l2cap_con_p con = NULL;
298 if (msg->header.arglen != sizeof(*ep)) {
300 "%s: %s - invalid LP_DisconnectInd message size\n",
301 __func__, NG_NODE_NAME(l2cap->node));
306 ep = (ng_hci_lp_discon_ind_ep *) (msg->data);
308 /* Check if we have this connection */
309 con = ng_l2cap_con_by_handle(l2cap, ep->con_handle);
312 "%s: %s - unexpected LP_DisconnectInd event. " \
313 "Connection does not exist, con_handle=%d\n",
314 __func__, NG_NODE_NAME(l2cap->node), ep->con_handle);
319 /* XXX Verify connection state -- do we need to check this? */
320 if (con->state != NG_L2CAP_CON_OPEN) {
322 "%s: %s - unexpected LP_DisconnectInd event. " \
323 "Invalid connection state, state=%d, con_handle=%d\n",
324 __func__, NG_NODE_NAME(l2cap->node), con->state,
331 * Notify upper layer and remove connection
332 * Note: The connection could have auto disconnect timeout set. Try
333 * to remove it. If auto disconnect timeout happened then ignore
334 * disconnect indication and let timeout handle that.
337 if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)
338 if ((error = ng_l2cap_discon_untimeout(con)) != 0)
341 ng_l2cap_con_fail(con, ep->reason);
344 } /* ng_l2cap_lp_discon_ind */
347 * Send LP_QoSSetupReq event to the lower layer protocol
351 ng_l2cap_lp_qos_req(ng_l2cap_p l2cap, u_int16_t con_handle,
352 ng_l2cap_flow_p flow)
354 struct ng_mesg *msg = NULL;
355 ng_hci_lp_qos_req_ep *ep = NULL;
356 ng_l2cap_con_p con = NULL;
359 /* Verify that we have this connection */
360 con = ng_l2cap_con_by_handle(l2cap, con_handle);
363 "%s: %s - unexpected LP_QoSSetupReq event. " \
364 "Connection does not exist, con_handle=%d\n",
365 __func__, NG_NODE_NAME(l2cap->node), con_handle);
370 /* Verify connection state */
371 if (con->state != NG_L2CAP_CON_OPEN) {
373 "%s: %s - unexpected LP_QoSSetupReq event. " \
374 "Invalid connection state, state=%d, con_handle=%d\n",
375 __func__, NG_NODE_NAME(l2cap->node), con->state,
381 /* Check if lower layer protocol is still connected */
382 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
384 "%s: %s - hook \"%s\" is not connected or valid",
385 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
390 /* Create and send LP_QoSSetupReq event */
391 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_LP_QOS_REQ,
392 sizeof(*ep), M_NOWAIT);
396 ep = (ng_hci_lp_qos_req_ep *) (msg->data);
397 ep->con_handle = con_handle;
398 ep->flags = flow->flags;
399 ep->service_type = flow->service_type;
400 ep->token_rate = flow->token_rate;
401 ep->peak_bandwidth = flow->peak_bandwidth;
402 ep->latency = flow->latency;
403 ep->delay_variation = flow->delay_variation;
405 NG_SEND_MSG_HOOK(error, l2cap->node, msg, l2cap->hci, 0);
408 } /* ng_l2cap_lp_con_req */
411 * Process LP_QoSSetupCfm from the lower layer protocol
415 ng_l2cap_lp_qos_cfm(ng_l2cap_p l2cap, struct ng_mesg *msg)
417 ng_hci_lp_qos_cfm_ep *ep = NULL;
421 if (msg->header.arglen != sizeof(*ep)) {
423 "%s: %s - invalid LP_QoSSetupCfm[Neg] message size\n",
424 __func__, NG_NODE_NAME(l2cap->node));
429 ep = (ng_hci_lp_qos_cfm_ep *) (msg->data);
430 /* XXX FIXME do something */
433 } /* ng_l2cap_lp_qos_cfm */
436 * Process LP_QoSViolationInd event from the lower layer protocol. Lower
437 * layer protocol has detected QoS Violation, so we MUST notify the
442 ng_l2cap_lp_qos_ind(ng_l2cap_p l2cap, struct ng_mesg *msg)
444 ng_hci_lp_qos_ind_ep *ep = NULL;
445 ng_l2cap_con_p con = NULL;
449 if (msg->header.arglen != sizeof(*ep)) {
451 "%s: %s - invalid LP_QoSViolation message size\n",
452 __func__, NG_NODE_NAME(l2cap->node));
457 ep = (ng_hci_lp_qos_ind_ep *) (msg->data);
459 /* Check if we have this connection */
460 con = ng_l2cap_con_by_handle(l2cap, ep->con_handle);
463 "%s: %s - unexpected LP_QoSViolationInd event. " \
464 "Connection does not exist, con_handle=%d\n",
465 __func__, NG_NODE_NAME(l2cap->node), ep->con_handle);
470 /* Verify connection state */
471 if (con->state != NG_L2CAP_CON_OPEN) {
473 "%s: %s - unexpected LP_QoSViolationInd event. " \
474 "Invalid connection state, state=%d, con_handle=%d\n",
475 __func__, NG_NODE_NAME(l2cap->node), con->state,
481 /* XXX FIXME Notify upper layer and terminate channels if required */
484 } /* ng_l2cap_qos_ind */
487 ng_l2cap_lp_enc_change(ng_l2cap_p l2cap, struct ng_mesg *msg)
489 ng_hci_lp_enc_change_ep *ep = NULL;
490 ng_l2cap_con_p con = NULL;
492 ng_l2cap_chan_p ch = NULL;
494 if (msg->header.arglen != sizeof(*ep)) {
496 "%s: %s - invalid LP_ENCChange message size\n",
497 __func__, NG_NODE_NAME(l2cap->node));
502 ep = (ng_hci_lp_enc_change_ep *) (msg->data);
504 /* Check if we have this connection */
505 con = ng_l2cap_con_by_handle(l2cap, ep->con_handle);
508 "%s: %s - unexpected LP_Enc Change Event. " \
509 "Connection does not exist, con_handle=%d\n",
510 __func__, NG_NODE_NAME(l2cap->node), ep->con_handle);
515 /* Verify connection state */
516 if (con->state != NG_L2CAP_CON_OPEN) {
518 "%s: %s - unexpected ENC_CHANGE event. " \
519 "Invalid connection state, state=%d, con_handle=%d\n",
520 __func__, NG_NODE_NAME(l2cap->node), con->state,
526 con->encryption = ep->status;
528 LIST_FOREACH(ch, &l2cap->chan_list, next){
529 if((ch->con->con_handle == ep->con_handle) &&
530 (ch->con->linktype == ep->link_type))
531 ng_l2cap_l2ca_encryption_change(ch, ep->status);
536 } /* ng_l2cap_enc_change */
539 * Prepare L2CAP packet. Prepend packet with L2CAP packet header and then
540 * segment it according to HCI MTU.
544 ng_l2cap_lp_send(ng_l2cap_con_p con, u_int16_t dcid, struct mbuf *m0)
546 ng_l2cap_p l2cap = con->l2cap;
547 ng_l2cap_hdr_t *l2cap_hdr = NULL;
548 ng_hci_acldata_pkt_t *acl_hdr = NULL;
549 struct mbuf *m_last = NULL, *m = NULL;
550 int len, flag = (con->linktype == NG_HCI_LINK_ACL) ? NG_HCI_PACKET_START : NG_HCI_LE_PACKET_START;
552 KASSERT((con->tx_pkt == NULL),
553 ("%s: %s - another packet pending?!\n", __func__, NG_NODE_NAME(l2cap->node)));
554 KASSERT((l2cap->pkt_size > 0),
555 ("%s: %s - invalid l2cap->pkt_size?!\n", __func__, NG_NODE_NAME(l2cap->node)));
557 /* Prepend mbuf with L2CAP header */
558 m0 = ng_l2cap_prepend(m0, sizeof(*l2cap_hdr));
561 "%s: %s - ng_l2cap_prepend(%zd) failed\n",
562 __func__, NG_NODE_NAME(l2cap->node),
568 l2cap_hdr = mtod(m0, ng_l2cap_hdr_t *);
569 l2cap_hdr->length = htole16(m0->m_pkthdr.len - sizeof(*l2cap_hdr));
570 l2cap_hdr->dcid = htole16(dcid);
573 * Segment single L2CAP packet according to the HCI layer MTU. Convert
574 * each segment into ACL data packet and prepend it with ACL data packet
575 * header. Link all segments together via m_nextpkt link.
577 * XXX BC (Broadcast flag) will always be 0 (zero).
581 /* Check length of the packet against HCI MTU */
582 len = m0->m_pkthdr.len;
583 if (len > l2cap->pkt_size) {
584 m = m_split(m0, l2cap->pkt_size, M_NOWAIT);
587 "%s: %s - m_split(%d) failed\n", __func__, NG_NODE_NAME(l2cap->node),
592 len = l2cap->pkt_size;
595 /* Convert packet fragment into ACL data packet */
596 m0 = ng_l2cap_prepend(m0, sizeof(*acl_hdr));
599 "%s: %s - ng_l2cap_prepend(%zd) failed\n",
600 __func__, NG_NODE_NAME(l2cap->node),
605 acl_hdr = mtod(m0, ng_hci_acldata_pkt_t *);
606 acl_hdr->type = NG_HCI_ACL_DATA_PKT;
607 acl_hdr->length = htole16(len);
608 acl_hdr->con_handle = htole16(NG_HCI_MK_CON_HANDLE(
609 con->con_handle, flag, 0));
611 /* Add fragment to the chain */
612 m0->m_nextpkt = NULL;
614 if (con->tx_pkt == NULL)
615 con->tx_pkt = m_last = m0;
617 m_last->m_nextpkt = m0;
622 "%s: %s - attaching ACL packet, con_handle=%d, PB=%#x, length=%d\n",
623 __func__, NG_NODE_NAME(l2cap->node), con->con_handle,
628 flag = NG_HCI_PACKET_FRAGMENT;
636 while (con->tx_pkt != NULL) {
637 m = con->tx_pkt->m_nextpkt;
638 m_freem(con->tx_pkt);
643 } /* ng_l2cap_lp_send */
646 * Receive ACL data packet from the HCI layer. First strip ACL packet header
647 * and get connection handle, PB (Packet Boundary) flag and payload length.
648 * Then find connection descriptor and verify its state. Then process ACL
651 * 1) If we got first segment (pb == NG_HCI_PACKET_START) then extract L2CAP
652 * header and get total length of the L2CAP packet. Then start new L2CAP
655 * 2) If we got other (then first :) segment (pb == NG_HCI_PACKET_FRAGMENT)
656 * then add segment to the packet.
660 ng_l2cap_lp_receive(ng_l2cap_p l2cap, struct mbuf *m)
662 ng_hci_acldata_pkt_t *acl_hdr = NULL;
663 ng_l2cap_hdr_t *l2cap_hdr = NULL;
664 ng_l2cap_con_p con = NULL;
665 u_int16_t con_handle, length, pb;
668 /* Check ACL data packet */
669 if (m->m_pkthdr.len < sizeof(*acl_hdr)) {
671 "%s: %s - invalid ACL data packet. Packet too small, length=%d\n",
672 __func__, NG_NODE_NAME(l2cap->node), m->m_pkthdr.len);
677 /* Strip ACL data packet header */
678 NG_L2CAP_M_PULLUP(m, sizeof(*acl_hdr));
682 acl_hdr = mtod(m, ng_hci_acldata_pkt_t *);
683 m_adj(m, sizeof(*acl_hdr));
685 /* Get ACL connection handle, PB flag and payload length */
686 acl_hdr->con_handle = le16toh(acl_hdr->con_handle);
687 con_handle = NG_HCI_CON_HANDLE(acl_hdr->con_handle);
688 pb = NG_HCI_PB_FLAG(acl_hdr->con_handle);
689 length = le16toh(acl_hdr->length);
692 "%s: %s - got ACL data packet, con_handle=%d, PB=%#x, length=%d\n",
693 __func__, NG_NODE_NAME(l2cap->node), con_handle, pb, length);
695 /* Get connection descriptor */
696 con = ng_l2cap_con_by_handle(l2cap, con_handle);
699 "%s: %s - unexpected ACL data packet. " \
700 "Connection does not exist, con_handle=%d\n",
701 __func__, NG_NODE_NAME(l2cap->node), con_handle);
706 /* Verify connection state */
707 if (con->state != NG_L2CAP_CON_OPEN) {
709 "%s: %s - unexpected ACL data packet. Invalid connection state=%d\n",
710 __func__, NG_NODE_NAME(l2cap->node), con->state);
716 if ((pb == NG_HCI_PACKET_START) || (pb == NG_HCI_LE_PACKET_START))
718 if (con->rx_pkt != NULL) {
720 "%s: %s - dropping incomplete L2CAP packet, got %d bytes, want %d bytes\n",
721 __func__, NG_NODE_NAME(l2cap->node),
722 con->rx_pkt->m_pkthdr.len, con->rx_pkt_len);
723 NG_FREE_M(con->rx_pkt);
727 /* Get L2CAP header */
728 if (m->m_pkthdr.len < sizeof(*l2cap_hdr)) {
730 "%s: %s - invalid L2CAP packet start fragment. Packet too small, length=%d\n",
731 __func__, NG_NODE_NAME(l2cap->node),
737 NG_L2CAP_M_PULLUP(m, sizeof(*l2cap_hdr));
741 l2cap_hdr = mtod(m, ng_l2cap_hdr_t *);
744 "%s: %s - staring new L2CAP packet, con_handle=%d, length=%d\n",
745 __func__, NG_NODE_NAME(l2cap->node), con_handle,
746 le16toh(l2cap_hdr->length));
748 /* Start new L2CAP packet */
750 con->rx_pkt_len = le16toh(l2cap_hdr->length)+sizeof(*l2cap_hdr);
751 } else if (pb == NG_HCI_PACKET_FRAGMENT) {
752 if (con->rx_pkt == NULL) {
754 "%s: %s - unexpected ACL data packet fragment, con_handle=%d\n",
755 __func__, NG_NODE_NAME(l2cap->node),
760 /* Add fragment to the L2CAP packet */
761 m_cat(con->rx_pkt, m);
762 con->rx_pkt->m_pkthdr.len += length;
765 "%s: %s - invalid ACL data packet. Invalid PB flag=%#x\n",
766 __func__, NG_NODE_NAME(l2cap->node), pb);
771 con->rx_pkt_len -= length;
772 if (con->rx_pkt_len < 0) {
774 "%s: %s - packet length mismatch. Got %d bytes, offset %d bytes\n",
775 __func__, NG_NODE_NAME(l2cap->node),
776 con->rx_pkt->m_pkthdr.len, con->rx_pkt_len);
777 NG_FREE_M(con->rx_pkt);
779 } else if (con->rx_pkt_len == 0) {
780 /* OK, we have got complete L2CAP packet, so process it */
781 error = ng_l2cap_receive(con);
792 } /* ng_l2cap_lp_receive */
795 * Send queued ACL packets to the HCI layer
799 ng_l2cap_lp_deliver(ng_l2cap_con_p con)
801 ng_l2cap_p l2cap = con->l2cap;
802 struct mbuf *m = NULL;
805 /* Check connection */
806 if (con->state != NG_L2CAP_CON_OPEN)
809 if (con->tx_pkt == NULL)
810 ng_l2cap_con_wakeup(con);
812 if (con->tx_pkt == NULL)
815 /* Check if lower layer protocol is still connected */
816 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
818 "%s: %s - hook \"%s\" is not connected or valid",
819 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
821 goto drop; /* XXX what to do with "pending"? */
824 /* Send ACL data packets */
825 while (con->pending < con->l2cap->num_pkts && con->tx_pkt != NULL) {
827 con->tx_pkt = con->tx_pkt->m_nextpkt;
830 if(m->m_flags &M_PROTO2){
831 ng_l2cap_lp_receive(con->l2cap, m);
835 "%s: %s - sending ACL packet, con_handle=%d, len=%d\n",
836 __func__, NG_NODE_NAME(l2cap->node), con->con_handle,
839 NG_SEND_DATA_ONLY(error, l2cap->hci, m);
842 "%s: %s - could not send ACL data packet, con_handle=%d, error=%d\n",
843 __func__, NG_NODE_NAME(l2cap->node),
844 con->con_handle, error);
846 goto drop; /* XXX what to do with "pending"? */
853 "%s: %s - %d ACL packets have been sent, con_handle=%d\n",
854 __func__, NG_NODE_NAME(l2cap->node), con->pending,
860 while (con->tx_pkt != NULL) {
861 m = con->tx_pkt->m_nextpkt;
862 m_freem(con->tx_pkt);
865 } /* ng_l2cap_lp_deliver */
868 * Process connection timeout. Remove connection from the list. If there
869 * are any channels that wait for the connection then notify them. Free
870 * connection descriptor.
874 ng_l2cap_process_lp_timeout(node_p node, hook_p hook, void *arg1, int con_handle)
876 ng_l2cap_p l2cap = NULL;
877 ng_l2cap_con_p con = NULL;
879 if (NG_NODE_NOT_VALID(node)) {
880 printf("%s: Netgraph node is not valid\n", __func__);
884 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
885 con = ng_l2cap_con_by_handle(l2cap, con_handle);
889 "%s: %s - could not find connection, con_handle=%d\n",
890 __func__, NG_NODE_NAME(node), con_handle);
894 if (!(con->flags & NG_L2CAP_CON_LP_TIMO)) {
896 "%s: %s - no pending LP timeout, con_handle=%d, state=%d, flags=%#x\n",
897 __func__, NG_NODE_NAME(node), con_handle, con->state,
903 * Notify channels that connection has timed out. This will remove
904 * connection, channels and pending commands.
907 con->flags &= ~NG_L2CAP_CON_LP_TIMO;
908 ng_l2cap_con_fail(con, NG_L2CAP_TIMEOUT);
909 } /* ng_l2cap_process_lp_timeout */
912 * Process auto disconnect timeout and send LP_DisconReq event to the
913 * lower layer protocol
917 ng_l2cap_process_discon_timeout(node_p node, hook_p hook, void *arg1, int con_handle)
919 ng_l2cap_p l2cap = NULL;
920 ng_l2cap_con_p con = NULL;
921 struct ng_mesg *msg = NULL;
922 ng_hci_lp_discon_req_ep *ep = NULL;
925 if (NG_NODE_NOT_VALID(node)) {
926 printf("%s: Netgraph node is not valid\n", __func__);
930 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
931 con = ng_l2cap_con_by_handle(l2cap, con_handle);
935 "%s: %s - could not find connection, con_handle=%d\n",
936 __func__, NG_NODE_NAME(node), con_handle);
940 if (!(con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)) {
942 "%s: %s - no pending disconnect timeout, con_handle=%d, state=%d, flags=%#x\n",
943 __func__, NG_NODE_NAME(node), con_handle, con->state,
948 con->flags &= ~NG_L2CAP_CON_AUTO_DISCON_TIMO;
950 /* Check if lower layer protocol is still connected */
951 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
953 "%s: %s - hook \"%s\" is not connected or valid\n",
954 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
958 /* Create and send LP_DisconReq event */
959 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_LP_DISCON_REQ,
960 sizeof(*ep), M_NOWAIT);
964 ep = (ng_hci_lp_discon_req_ep *) (msg->data);
965 ep->con_handle = con->con_handle;
966 ep->reason = 0x13; /* User Ended Connection */
968 NG_SEND_MSG_HOOK(error, l2cap->node, msg, l2cap->hci, 0);
969 } /* ng_l2cap_process_discon_timeout */