xref: /dragonfly/sys/netbt/l2cap_upper.c (revision 805c8e8e4093ceca2e27510ad3a66d4de8060a55)
1 /* $OpenBSD: l2cap_upper.c,v 1.2 2007/10/01 16:39:30 krw Exp $ */
2 /* $NetBSD: l2cap_upper.c,v 1.8 2007/04/29 20:23:36 msaitoh Exp $ */
3 
4 /*-
5  * Copyright (c) 2005 Iain Hibbert.
6  * Copyright (c) 2006 Itronix Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  * 3. The name of Itronix Inc. may not be used to endorse
18  *    or promote products derived from this software without specific
19  *    prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/proc.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/systm.h>
43 #include <sys/endian.h>
44 
45 #include <netbt/bluetooth.h>
46 #include <netbt/hci.h>
47 #include <netbt/l2cap.h>
48 
49 /*******************************************************************************
50  *
51  *        L2CAP Channel - Upper Protocol API
52  */
53 
54 /*
55  * l2cap_attach(handle, btproto, upper)
56  *
57  *        attach new l2cap_channel to handle, populate
58  *        with reasonable defaults
59  */
60 int
l2cap_attach(struct l2cap_channel ** handle,const struct btproto * proto,void * upper)61 l2cap_attach(struct l2cap_channel **handle,
62                     const struct btproto *proto, void *upper)
63 {
64           struct l2cap_channel *chan;
65 
66           KKASSERT(handle != NULL);
67           KKASSERT(proto != NULL);
68           KKASSERT(upper != NULL);
69 
70           chan = kmalloc(sizeof(*chan), M_BLUETOOTH, M_NOWAIT | M_ZERO);
71           if (chan == NULL)
72                     return ENOMEM;
73 
74           chan->lc_proto = proto;
75           chan->lc_upper = upper;
76 
77           chan->lc_state = L2CAP_CLOSED;
78 
79           chan->lc_lcid = L2CAP_NULL_CID;
80           chan->lc_rcid = L2CAP_NULL_CID;
81 
82           chan->lc_laddr.bt_len = sizeof(struct sockaddr_bt);
83           chan->lc_laddr.bt_family = AF_BLUETOOTH;
84           chan->lc_laddr.bt_psm = L2CAP_PSM_ANY;
85 
86           chan->lc_raddr.bt_len = sizeof(struct sockaddr_bt);
87           chan->lc_raddr.bt_family = AF_BLUETOOTH;
88           chan->lc_raddr.bt_psm = L2CAP_PSM_ANY;
89 
90           chan->lc_imtu = L2CAP_MTU_DEFAULT;
91           chan->lc_omtu = L2CAP_MTU_DEFAULT;
92           chan->lc_flush = L2CAP_FLUSH_TIMO_DEFAULT;
93 
94           memcpy(&chan->lc_iqos, &l2cap_default_qos, sizeof(l2cap_qos_t));
95           memcpy(&chan->lc_oqos, &l2cap_default_qos, sizeof(l2cap_qos_t));
96 
97           *handle = chan;
98           return 0;
99 }
100 
101 /*
102  * l2cap_bind(l2cap_channel, sockaddr)
103  *
104  *        set local address of channel
105  */
106 int
l2cap_bind(struct l2cap_channel * chan,struct sockaddr_bt * addr)107 l2cap_bind(struct l2cap_channel *chan, struct sockaddr_bt *addr)
108 {
109 
110           memcpy(&chan->lc_laddr, addr, sizeof(struct sockaddr_bt));
111           return 0;
112 }
113 
114 /*
115  * l2cap_sockaddr(l2cap_channel, sockaddr)
116  *
117  *        get local address of channel
118  */
119 int
l2cap_sockaddr(struct l2cap_channel * chan,struct sockaddr_bt * addr)120 l2cap_sockaddr(struct l2cap_channel *chan, struct sockaddr_bt *addr)
121 {
122           memcpy(addr, &chan->lc_laddr, sizeof(struct sockaddr_bt));
123           return 0;
124 }
125 
126 /*
127  * l2cap_connect(l2cap_channel, sockaddr)
128  *
129  *        Initiate a connection to destination. This corresponds to
130  *        "Open Channel Request" in the L2CAP specification and will
131  *        result in one of the following:
132  *
133  *                  proto->connected(upper)
134  *                  proto->disconnected(upper, error)
135  *
136  *        and, optionally
137  *                  proto->connecting(upper)
138  */
139 int
l2cap_connect(struct l2cap_channel * chan,struct sockaddr_bt * dest)140 l2cap_connect(struct l2cap_channel *chan, struct sockaddr_bt *dest)
141 {
142           struct hci_unit *unit;
143           int err;
144 
145           memcpy(&chan->lc_raddr, dest, sizeof(struct sockaddr_bt));
146 
147           if (L2CAP_PSM_INVALID(chan->lc_raddr.bt_psm))
148                     return EINVAL;
149 
150           if (bdaddr_any(&chan->lc_raddr.bt_bdaddr))
151                     return EDESTADDRREQ;
152 
153           /* set local address if it needs setting */
154           if (bdaddr_any(&chan->lc_laddr.bt_bdaddr)) {
155                     err = hci_route_lookup(&chan->lc_laddr.bt_bdaddr,
156                                                   &chan->lc_raddr.bt_bdaddr);
157                     if (err)
158                               return err;
159           }
160 
161           unit = hci_unit_lookup(&chan->lc_laddr.bt_bdaddr);
162           if (unit == NULL)
163                     return EHOSTUNREACH;
164 
165           /* attach to active list */
166           err = l2cap_cid_alloc(chan);
167           if (err)
168                     return err;
169 
170           /* open link to remote device */
171           chan->lc_link = hci_acl_open(unit, &chan->lc_raddr.bt_bdaddr);
172           if (chan->lc_link == NULL)
173                     return EHOSTUNREACH;
174 
175           /* set the link mode */
176           err = l2cap_setmode(chan);
177           if (err == EINPROGRESS) {
178                     chan->lc_state = L2CAP_WAIT_SEND_CONNECT_REQ;
179                     (*chan->lc_proto->connecting)(chan->lc_upper);
180                     return 0;
181           }
182           if (err)
183                     goto fail;
184 
185           /*
186            * We can queue a connect request now even though the link may
187            * not yet be open; Our mode setting is assured, and the queue
188            * will be started automatically at the right time.
189            */
190           chan->lc_state = L2CAP_WAIT_RECV_CONNECT_RSP;
191           err = l2cap_send_connect_req(chan);
192           if (err)
193                     goto fail;
194 
195           return 0;
196 
197 fail:
198           chan->lc_state = L2CAP_CLOSED;
199           hci_acl_close(chan->lc_link, err);
200           chan->lc_link = NULL;
201           return err;
202 }
203 
204 /*
205  * l2cap_peeraddr(l2cap_channel, sockaddr)
206  *
207  *        get remote address of channel
208  */
209 int
l2cap_peeraddr(struct l2cap_channel * chan,struct sockaddr_bt * addr)210 l2cap_peeraddr(struct l2cap_channel *chan, struct sockaddr_bt *addr)
211 {
212           memcpy(addr, &chan->lc_raddr, sizeof(struct sockaddr_bt));
213           return 0;
214 }
215 
216 /*
217  * l2cap_disconnect(l2cap_channel, linger)
218  *
219  *        Initiate L2CAP disconnection. This corresponds to
220  *        "Close Channel Request" in the L2CAP specification
221  *        and will result in a call to
222  *
223  *                  proto->disconnected(upper, error)
224  *
225  *        when the disconnection is complete. If linger is set,
226  *        the call will not be made until data has flushed from
227  *        the queue.
228  */
229 int
l2cap_disconnect(struct l2cap_channel * chan,int linger)230 l2cap_disconnect(struct l2cap_channel *chan, int linger)
231 {
232           int err = 0;
233 
234           if (chan->lc_state == L2CAP_CLOSED
235               || chan->lc_state == L2CAP_WAIT_DISCONNECT)
236                     return EINVAL;
237 
238           chan->lc_flags |= L2CAP_SHUTDOWN;
239 
240           /*
241            * no need to do anything unless the queue is empty or
242            * we are not lingering..
243            */
244           if ((IF_QEMPTY(&chan->lc_txq) && chan->lc_pending == 0)
245               || linger == 0) {
246                     chan->lc_state = L2CAP_WAIT_DISCONNECT;
247                     err = l2cap_send_disconnect_req(chan);
248                     if (err)
249                               l2cap_close(chan, err);
250           }
251           return err;
252 }
253 
254 /*
255  * l2cap_detach(handle)
256  *
257  *        Detach l2cap channel from handle & close it down
258  */
259 int
l2cap_detach(struct l2cap_channel ** handle)260 l2cap_detach(struct l2cap_channel **handle)
261 {
262           struct l2cap_channel *chan;
263 
264           chan = *handle;
265           *handle = NULL;
266 
267           if (chan->lc_state != L2CAP_CLOSED)
268                     l2cap_close(chan, 0);
269 
270           if (chan->lc_lcid != L2CAP_NULL_CID) {
271                     LIST_REMOVE(chan, lc_ncid);
272                     chan->lc_lcid = L2CAP_NULL_CID;
273           }
274 
275           IF_DRAIN(&chan->lc_txq);
276 
277           /*
278            * Could implement some kind of delayed expunge to make sure that the
279            * CID is really dead before it becomes available for reuse?
280            */
281 
282           kfree(chan, M_BLUETOOTH);
283           return 0;
284 }
285 
286 /*
287  * l2cap_listen(l2cap_channel)
288  *
289  *        Use this channel as a listening post (until detached). This will
290  *        result in calls to:
291  *
292  *                  proto->newconn(upper, laddr, raddr)
293  *
294  *        for incoming connections matching the psm and local address of the
295  *        channel (NULL psm/address are permitted and match any protocol/device).
296  *
297  *        The upper layer should create and return a new channel.
298  *
299  *        You cannot use this channel for anything else subsequent to this call
300  */
301 int
l2cap_listen(struct l2cap_channel * chan)302 l2cap_listen(struct l2cap_channel *chan)
303 {
304           struct l2cap_channel *used, *prev = NULL;
305 
306           if (chan->lc_lcid != L2CAP_NULL_CID)
307                     return EINVAL;
308 
309           if (chan->lc_laddr.bt_psm != L2CAP_PSM_ANY
310               && L2CAP_PSM_INVALID(chan->lc_laddr.bt_psm))
311                     return EADDRNOTAVAIL;
312 
313           /*
314            * This CID is irrelevant, as the channel is not stored on the active
315            * list and the socket code does not allow operations on listening
316            * sockets, but we set it so the detach code knows to LIST_REMOVE the
317            * channel.
318            */
319           chan->lc_lcid = L2CAP_SIGNAL_CID;
320 
321           /*
322            * The list of listening channels is stored in an order such that new
323            * listeners dont usurp current listeners, but that specific listening
324            * takes precedence over promiscuous, and the connect request code can
325            * easily use the first matching entry.
326            */
327           LIST_FOREACH(used, &l2cap_listen_list, lc_ncid) {
328                     if (used->lc_laddr.bt_psm < chan->lc_laddr.bt_psm)
329                               break;
330 
331                     if (used->lc_laddr.bt_psm == chan->lc_laddr.bt_psm
332                               && bdaddr_any(&used->lc_laddr.bt_bdaddr)
333                               && !bdaddr_any(&chan->lc_laddr.bt_bdaddr))
334                               break;
335 
336                     prev = used;
337           }
338 
339           if (prev == NULL)
340                     LIST_INSERT_HEAD(&l2cap_listen_list, chan, lc_ncid);
341           else
342                     LIST_INSERT_AFTER(prev, chan, lc_ncid);
343 
344           return 0;
345 }
346 
347 /*
348  * l2cap_send(l2cap_channel, mbuf)
349  *
350  *        Output SDU on channel described by channel. This corresponds
351  *        to "Send Data Request" in the L2CAP specification. The upper
352  *        layer will be notified when SDU's have completed sending by a
353  *        call to:
354  *
355  *                  proto->complete(upper, n)
356  *
357  *        (currently n == 1)
358  *
359  *        Note: I'm not sure how this will work out, but I think that
360  *        if outgoing Retransmission Mode or Flow Control Mode is
361  *        negotiated then this call will not be made until the SDU has
362  *        been acknowleged by the peer L2CAP entity. For 'Best Effort'
363  *        it will be made when the packet has cleared the controller
364  *        buffers.
365  *
366  *        We only support Basic mode so far, so encapsulate with a
367  *        B-Frame header and start sending if we are not already
368  */
369 int
l2cap_send(struct l2cap_channel * chan,struct mbuf * m)370 l2cap_send(struct l2cap_channel *chan, struct mbuf *m)
371 {
372           l2cap_hdr_t *hdr;
373           int plen;
374 
375           if (chan->lc_state == L2CAP_CLOSED) {
376                     m_freem(m);
377                     return ENOTCONN;
378           }
379 
380           plen = m->m_pkthdr.len;
381 
382           DPRINTFN(5, "send %d bytes on CID #%d (pending = %d)\n",
383                     plen, chan->lc_lcid, chan->lc_pending);
384 
385           /* Encapsulate with B-Frame */
386           M_PREPEND(m, sizeof(l2cap_hdr_t), M_NOWAIT);
387           if (m == NULL)
388                     return ENOMEM;
389 
390           hdr = mtod(m, l2cap_hdr_t *);
391           hdr->length = htole16(plen);
392           hdr->dcid = htole16(chan->lc_rcid);
393 
394           /* Queue it on our list */
395           IF_ENQUEUE(&chan->lc_txq, m);
396 
397           /* If we are not sending, then start doing so */
398           if (chan->lc_pending == 0)
399                     return l2cap_start(chan);
400 
401           return 0;
402 }
403 
404 /*
405  * l2cap_setopt(l2cap_channel, opt, addr)
406  *
407  *        Apply configuration options to channel. This corresponds to
408  *        "Configure Channel Request" in the L2CAP specification.
409  *
410  *        for SO_L2CAP_LM, the settings will take effect when the
411  *        channel is established. If the channel is already open,
412  *        a call to
413  *                  proto->linkmode(upper, new)
414  *
415  *        will be made when the change is complete.
416  */
417 int
l2cap_setopt(struct l2cap_channel * chan,int opt,void * addr)418 l2cap_setopt(struct l2cap_channel *chan, int opt, void *addr)
419 {
420           int mode, err = 0;
421           uint16_t mtu;
422 
423           switch (opt) {
424           case SO_L2CAP_IMTU: /* set Incoming MTU */
425                     mtu = *(uint16_t *)addr;
426                     if (mtu < L2CAP_MTU_MINIMUM)
427                               err = EINVAL;
428                     else if (chan->lc_state == L2CAP_CLOSED)
429                               chan->lc_imtu = mtu;
430                     else
431                               err = EBUSY;
432 
433                     break;
434 
435           case SO_L2CAP_LM:   /* set link mode */
436                     mode = *(int *)addr;
437                     mode &= (L2CAP_LM_SECURE | L2CAP_LM_ENCRYPT | L2CAP_LM_AUTH);
438 
439                     if (mode & L2CAP_LM_SECURE)
440                               mode |= L2CAP_LM_ENCRYPT;
441 
442                     if (mode & L2CAP_LM_ENCRYPT)
443                               mode |= L2CAP_LM_AUTH;
444 
445                     chan->lc_mode = mode;
446 
447                     if (chan->lc_state == L2CAP_OPEN)
448                               err = l2cap_setmode(chan);
449 
450                     break;
451 
452           case SO_L2CAP_OQOS: /* set Outgoing QoS flow spec */
453           case SO_L2CAP_FLUSH:          /* set Outgoing Flush Timeout */
454           default:
455                     err = ENOPROTOOPT;
456                     break;
457           }
458 
459           return err;
460 }
461 
462 
463 /*
464  * Used in l2cap_socket for set options, coming from socket.
465  */
466 int
l2cap_setopt2(struct l2cap_channel * chan,int opt,struct socket * so,struct sockopt * sopt)467 l2cap_setopt2(struct l2cap_channel *chan, int opt, struct socket *so,
468     struct sockopt *sopt)
469 {
470           int mode, err = 0;
471           uint16_t mtu;
472 
473           switch (opt) {
474           case SO_L2CAP_IMTU: /* set Incoming MTU */
475                     err = soopt_to_kbuf(sopt, &mtu, sizeof(uint16_t),
476                         sizeof(uint16_t));
477                     if (err)
478                               break;
479 
480                     if (mtu < L2CAP_MTU_MINIMUM)
481                               err = EINVAL;
482                     else if (chan->lc_state == L2CAP_CLOSED)
483                               chan->lc_imtu = mtu;
484                     else
485                               err = EBUSY;
486 
487                     break;
488 
489           case SO_L2CAP_LM:   /* set link mode */
490                     err = soopt_to_kbuf(sopt, &mode, sizeof(int), sizeof(int));
491                     if (err)
492                               break;
493 
494                     mode &= (L2CAP_LM_SECURE | L2CAP_LM_ENCRYPT | L2CAP_LM_AUTH);
495 
496                     if (mode & L2CAP_LM_SECURE)
497                               mode |= L2CAP_LM_ENCRYPT;
498                     if (mode & L2CAP_LM_ENCRYPT)
499                               mode |= L2CAP_LM_AUTH;
500                     chan->lc_mode = mode;
501 
502                     if (chan->lc_state == L2CAP_OPEN)
503                               err = l2cap_setmode(chan);
504 
505                     break;
506 
507           case SO_L2CAP_OQOS: /* set Outgoing QoS flow spec */
508           case SO_L2CAP_FLUSH:          /* set Outgoing Flush Timeout */
509           default:
510                     err = ENOPROTOOPT;
511                     break;
512           }
513           return err;
514 }
515 
516 /*
517  * l2cap_getopt(l2cap_channel, opt, addr)
518  *
519  *        Return configuration parameters.
520  */
521 int
l2cap_getopt(struct l2cap_channel * chan,int opt,void * addr)522 l2cap_getopt(struct l2cap_channel *chan, int opt, void *addr)
523 {
524 
525           switch (opt) {
526           case SO_L2CAP_IMTU: /* get Incoming MTU */
527                     *(uint16_t *)addr = chan->lc_imtu;
528                     return sizeof(uint16_t);
529 
530           case SO_L2CAP_OMTU: /* get Outgoing MTU */
531                     *(uint16_t *)addr = chan->lc_omtu;
532                     return sizeof(uint16_t);
533 
534           case SO_L2CAP_IQOS: /* get Incoming QoS flow spec */
535                     memcpy(addr, &chan->lc_iqos, sizeof(l2cap_qos_t));
536                     return sizeof(l2cap_qos_t);
537 
538           case SO_L2CAP_OQOS: /* get Outgoing QoS flow spec */
539                     memcpy(addr, &chan->lc_oqos, sizeof(l2cap_qos_t));
540                     return sizeof(l2cap_qos_t);
541 
542           case SO_L2CAP_FLUSH:          /* get Flush Timeout */
543                     *(uint16_t *)addr = chan->lc_flush;
544                     return sizeof(uint16_t);
545 
546           case SO_L2CAP_LM:   /* get link mode */
547                     *(int *)addr = chan->lc_mode;
548                     return sizeof(int);
549 
550           default:
551                     break;
552           }
553 
554           return 0;
555 }
556