1 1.10 rtr /* $NetBSD: l2cap_lower.c,v 1.10 2014/08/05 07:55:31 rtr Exp $ */ 2 1.1 gdamore 3 1.1 gdamore /*- 4 1.1 gdamore * Copyright (c) 2005 Iain Hibbert. 5 1.1 gdamore * Copyright (c) 2006 Itronix Inc. 6 1.1 gdamore * All rights reserved. 7 1.1 gdamore * 8 1.1 gdamore * Redistribution and use in source and binary forms, with or without 9 1.1 gdamore * modification, are permitted provided that the following conditions 10 1.1 gdamore * are met: 11 1.1 gdamore * 1. Redistributions of source code must retain the above copyright 12 1.1 gdamore * notice, this list of conditions and the following disclaimer. 13 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 gdamore * notice, this list of conditions and the following disclaimer in the 15 1.1 gdamore * documentation and/or other materials provided with the distribution. 16 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse 17 1.1 gdamore * or promote products derived from this software without specific 18 1.1 gdamore * prior written permission. 19 1.1 gdamore * 20 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 21 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 24 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE. 31 1.1 gdamore */ 32 1.1 gdamore 33 1.1 gdamore #include <sys/cdefs.h> 34 1.10 rtr __KERNEL_RCSID(0, "$NetBSD: l2cap_lower.c,v 1.10 2014/08/05 07:55:31 rtr Exp $"); 35 1.1 gdamore 36 1.1 gdamore #include <sys/param.h> 37 1.1 gdamore #include <sys/kernel.h> 38 1.1 gdamore #include <sys/malloc.h> 39 1.1 gdamore #include <sys/mbuf.h> 40 1.1 gdamore #include <sys/proc.h> 41 1.1 gdamore #include <sys/queue.h> 42 1.1 gdamore #include <sys/systm.h> 43 1.1 gdamore 44 1.1 gdamore #include <netbt/bluetooth.h> 45 1.1 gdamore #include <netbt/hci.h> 46 1.1 gdamore #include <netbt/l2cap.h> 47 1.1 gdamore 48 1.1 gdamore /**************************************************************************** 49 1.1 gdamore * 50 1.1 gdamore * L2CAP Channel Lower Layer interface 51 1.1 gdamore */ 52 1.1 gdamore 53 1.1 gdamore /* 54 1.1 gdamore * L2CAP channel is disconnected, could be: 55 1.1 gdamore * 56 1.1 gdamore * HCI layer received "Disconnect Complete" event for ACL link 57 1.1 gdamore * some Request timed out 58 1.1 gdamore * Config failed 59 1.1 gdamore * Other end reported invalid CID 60 1.1 gdamore * Normal disconnection 61 1.6 plunky * Change link mode failed 62 1.1 gdamore */ 63 1.1 gdamore void 64 1.1 gdamore l2cap_close(struct l2cap_channel *chan, int err) 65 1.1 gdamore { 66 1.1 gdamore struct l2cap_pdu *pdu; 67 1.1 gdamore struct l2cap_req *req, *n; 68 1.1 gdamore 69 1.1 gdamore if (chan->lc_state == L2CAP_CLOSED) 70 1.1 gdamore return; 71 1.1 gdamore 72 1.1 gdamore /* 73 1.1 gdamore * Since any potential PDU could be half sent we just let it go, 74 1.1 gdamore * but disassociate ourselves from it as links deal with ownerless 75 1.1 gdamore * PDU's in any case. We could try harder to flush unsent packets 76 1.1 gdamore * but maybe its better to leave them in the queue? 77 1.1 gdamore */ 78 1.1 gdamore TAILQ_FOREACH(pdu, &chan->lc_link->hl_txq, lp_next) { 79 1.1 gdamore if (pdu->lp_chan == chan) 80 1.1 gdamore pdu->lp_chan = NULL; 81 1.1 gdamore } 82 1.1 gdamore 83 1.1 gdamore /* 84 1.1 gdamore * and clear any outstanding requests.. 85 1.1 gdamore */ 86 1.1 gdamore req = TAILQ_FIRST(&chan->lc_link->hl_reqs); 87 1.1 gdamore while (req != NULL) { 88 1.1 gdamore n = TAILQ_NEXT(req, lr_next); 89 1.1 gdamore if (req->lr_chan == chan) 90 1.1 gdamore l2cap_request_free(req); 91 1.1 gdamore 92 1.1 gdamore req = n; 93 1.1 gdamore } 94 1.1 gdamore 95 1.1 gdamore chan->lc_pending = 0; 96 1.1 gdamore chan->lc_state = L2CAP_CLOSED; 97 1.1 gdamore hci_acl_close(chan->lc_link, err); 98 1.1 gdamore chan->lc_link = NULL; 99 1.1 gdamore 100 1.1 gdamore (*chan->lc_proto->disconnected)(chan->lc_upper, err); 101 1.1 gdamore } 102 1.1 gdamore 103 1.1 gdamore /* 104 1.1 gdamore * Process incoming L2CAP frame from ACL link. We take off the B-Frame 105 1.1 gdamore * header (which is present in all packets), verify the data length 106 1.1 gdamore * and distribute the rest of the frame to the relevant channel 107 1.1 gdamore * handler. 108 1.1 gdamore */ 109 1.1 gdamore void 110 1.1 gdamore l2cap_recv_frame(struct mbuf *m, struct hci_link *link) 111 1.1 gdamore { 112 1.1 gdamore struct l2cap_channel *chan; 113 1.1 gdamore l2cap_hdr_t hdr; 114 1.1 gdamore 115 1.1 gdamore m_copydata(m, 0, sizeof(hdr), &hdr); 116 1.1 gdamore m_adj(m, sizeof(hdr)); 117 1.1 gdamore 118 1.1 gdamore hdr.length = le16toh(hdr.length); 119 1.1 gdamore hdr.dcid = le16toh(hdr.dcid); 120 1.1 gdamore 121 1.1 gdamore DPRINTFN(5, "(%s) received packet (%d bytes)\n", 122 1.7 plunky device_xname(link->hl_unit->hci_dev), hdr.length); 123 1.1 gdamore 124 1.1 gdamore if (hdr.length != m->m_pkthdr.len) 125 1.1 gdamore goto failed; 126 1.1 gdamore 127 1.1 gdamore if (hdr.dcid == L2CAP_SIGNAL_CID) { 128 1.1 gdamore l2cap_recv_signal(m, link); 129 1.1 gdamore return; 130 1.1 gdamore } 131 1.1 gdamore 132 1.1 gdamore if (hdr.dcid == L2CAP_CLT_CID) { 133 1.3 plunky m_freem(m); /* TODO */ 134 1.1 gdamore return; 135 1.1 gdamore } 136 1.1 gdamore 137 1.1 gdamore chan = l2cap_cid_lookup(hdr.dcid); 138 1.4 plunky if (chan != NULL && chan->lc_link == link 139 1.8 plunky && chan->lc_imtu >= hdr.length 140 1.4 plunky && chan->lc_state == L2CAP_OPEN) { 141 1.1 gdamore (*chan->lc_proto->input)(chan->lc_upper, m); 142 1.1 gdamore return; 143 1.1 gdamore } 144 1.1 gdamore 145 1.9 plunky DPRINTF("(%s) invalid L2CAP packet dropped, CID #%d, length %d\n", 146 1.9 plunky device_xname(link->hl_unit->hci_dev), hdr.dcid, hdr.length); 147 1.1 gdamore 148 1.1 gdamore failed: 149 1.1 gdamore m_freem(m); 150 1.1 gdamore } 151 1.1 gdamore 152 1.1 gdamore /* 153 1.10 rtr * Start another L2CAP packet on its way. This is called from l2cap_send_pcb 154 1.1 gdamore * (when no PDU is pending) and hci_acl_start (when PDU has been placed on 155 1.1 gdamore * device queue). Thus we can have more than one PDU waiting at the device 156 1.1 gdamore * if space is available but no single channel will hog the link. 157 1.1 gdamore */ 158 1.1 gdamore int 159 1.1 gdamore l2cap_start(struct l2cap_channel *chan) 160 1.1 gdamore { 161 1.1 gdamore struct mbuf *m; 162 1.1 gdamore int err = 0; 163 1.1 gdamore 164 1.1 gdamore if (chan->lc_state != L2CAP_OPEN) 165 1.1 gdamore return 0; 166 1.1 gdamore 167 1.1 gdamore if (MBUFQ_FIRST(&chan->lc_txq) == NULL) { 168 1.1 gdamore DPRINTFN(5, "no data, pending = %d\n", chan->lc_pending); 169 1.1 gdamore /* 170 1.1 gdamore * If we are just waiting for the queue to flush 171 1.1 gdamore * and it has, we may disconnect.. 172 1.1 gdamore */ 173 1.1 gdamore if (chan->lc_flags & L2CAP_SHUTDOWN 174 1.1 gdamore && chan->lc_pending == 0) { 175 1.1 gdamore chan->lc_state = L2CAP_WAIT_DISCONNECT; 176 1.1 gdamore err = l2cap_send_disconnect_req(chan); 177 1.1 gdamore if (err) 178 1.1 gdamore l2cap_close(chan, err); 179 1.1 gdamore } 180 1.1 gdamore 181 1.1 gdamore return err; 182 1.1 gdamore } 183 1.1 gdamore 184 1.1 gdamore /* 185 1.1 gdamore * We could check QoS/RFC mode here and optionally not send 186 1.1 gdamore * the packet if we are not ready for any reason 187 1.1 gdamore * 188 1.1 gdamore * Also to support flush timeout then we might want to start 189 1.1 gdamore * the timer going? (would need to keep some kind of record 190 1.1 gdamore * of packets sent, possibly change it so that we allocate 191 1.1 gdamore * the l2cap_pdu and fragment the packet, then hand it down 192 1.1 gdamore * and get it back when its completed). Hm. 193 1.1 gdamore */ 194 1.1 gdamore 195 1.1 gdamore MBUFQ_DEQUEUE(&chan->lc_txq, m); 196 1.1 gdamore 197 1.5 plunky KASSERT(chan->lc_link != NULL); 198 1.5 plunky KASSERT(m != NULL); 199 1.1 gdamore 200 1.1 gdamore DPRINTFN(5, "CID #%d sending packet (%d bytes)\n", 201 1.1 gdamore chan->lc_lcid, m->m_pkthdr.len); 202 1.1 gdamore 203 1.1 gdamore chan->lc_pending++; 204 1.1 gdamore return hci_acl_send(m, chan->lc_link, chan); 205 1.1 gdamore } 206