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