dhcpcd: update README.DRAGONFLY
[dragonfly.git] / sys / netbt / l2cap_upper.c
blob2b27b726ba1b9d38aa1ac2a25574ce6dbe1665f9
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 $ */
4 /*-
5 * Copyright (c) 2005 Iain Hibbert.
6 * Copyright (c) 2006 Itronix Inc.
7 * All rights reserved.
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.
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.
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>
45 #include <netbt/bluetooth.h>
46 #include <netbt/hci.h>
47 #include <netbt/l2cap.h>
49 /*******************************************************************************
51 * L2CAP Channel - Upper Protocol API
55 * l2cap_attach(handle, btproto, upper)
57 * attach new l2cap_channel to handle, populate
58 * with reasonable defaults
60 int
61 l2cap_attach(struct l2cap_channel **handle,
62 const struct btproto *proto, void *upper)
64 struct l2cap_channel *chan;
66 KKASSERT(handle != NULL);
67 KKASSERT(proto != NULL);
68 KKASSERT(upper != NULL);
70 chan = kmalloc(sizeof(*chan), M_BLUETOOTH, M_NOWAIT | M_ZERO);
71 if (chan == NULL)
72 return ENOMEM;
74 chan->lc_proto = proto;
75 chan->lc_upper = upper;
77 chan->lc_state = L2CAP_CLOSED;
79 chan->lc_lcid = L2CAP_NULL_CID;
80 chan->lc_rcid = L2CAP_NULL_CID;
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;
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;
90 chan->lc_imtu = L2CAP_MTU_DEFAULT;
91 chan->lc_omtu = L2CAP_MTU_DEFAULT;
92 chan->lc_flush = L2CAP_FLUSH_TIMO_DEFAULT;
94 memcpy(&chan->lc_iqos, &l2cap_default_qos, sizeof(l2cap_qos_t));
95 memcpy(&chan->lc_oqos, &l2cap_default_qos, sizeof(l2cap_qos_t));
97 *handle = chan;
98 return 0;
102 * l2cap_bind(l2cap_channel, sockaddr)
104 * set local address of channel
107 l2cap_bind(struct l2cap_channel *chan, struct sockaddr_bt *addr)
110 memcpy(&chan->lc_laddr, addr, sizeof(struct sockaddr_bt));
111 return 0;
115 * l2cap_sockaddr(l2cap_channel, sockaddr)
117 * get local address of channel
120 l2cap_sockaddr(struct l2cap_channel *chan, struct sockaddr_bt *addr)
122 memcpy(addr, &chan->lc_laddr, sizeof(struct sockaddr_bt));
123 return 0;
127 * l2cap_connect(l2cap_channel, sockaddr)
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:
133 * proto->connected(upper)
134 * proto->disconnected(upper, error)
136 * and, optionally
137 * proto->connecting(upper)
140 l2cap_connect(struct l2cap_channel *chan, struct sockaddr_bt *dest)
142 struct hci_unit *unit;
143 int err;
145 memcpy(&chan->lc_raddr, dest, sizeof(struct sockaddr_bt));
147 if (L2CAP_PSM_INVALID(chan->lc_raddr.bt_psm))
148 return EINVAL;
150 if (bdaddr_any(&chan->lc_raddr.bt_bdaddr))
151 return EDESTADDRREQ;
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;
161 unit = hci_unit_lookup(&chan->lc_laddr.bt_bdaddr);
162 if (unit == NULL)
163 return EHOSTUNREACH;
165 /* attach to active list */
166 err = l2cap_cid_alloc(chan);
167 if (err)
168 return err;
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;
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;
182 if (err)
183 goto fail;
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.
190 chan->lc_state = L2CAP_WAIT_RECV_CONNECT_RSP;
191 err = l2cap_send_connect_req(chan);
192 if (err)
193 goto fail;
195 return 0;
197 fail:
198 chan->lc_state = L2CAP_CLOSED;
199 hci_acl_close(chan->lc_link, err);
200 chan->lc_link = NULL;
201 return err;
205 * l2cap_peeraddr(l2cap_channel, sockaddr)
207 * get remote address of channel
210 l2cap_peeraddr(struct l2cap_channel *chan, struct sockaddr_bt *addr)
212 memcpy(addr, &chan->lc_raddr, sizeof(struct sockaddr_bt));
213 return 0;
217 * l2cap_disconnect(l2cap_channel, linger)
219 * Initiate L2CAP disconnection. This corresponds to
220 * "Close Channel Request" in the L2CAP specification
221 * and will result in a call to
223 * proto->disconnected(upper, error)
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.
230 l2cap_disconnect(struct l2cap_channel *chan, int linger)
232 int err = 0;
234 if (chan->lc_state == L2CAP_CLOSED
235 || chan->lc_state == L2CAP_WAIT_DISCONNECT)
236 return EINVAL;
238 chan->lc_flags |= L2CAP_SHUTDOWN;
241 * no need to do anything unless the queue is empty or
242 * we are not lingering..
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);
251 return err;
255 * l2cap_detach(handle)
257 * Detach l2cap channel from handle & close it down
260 l2cap_detach(struct l2cap_channel **handle)
262 struct l2cap_channel *chan;
264 chan = *handle;
265 *handle = NULL;
267 if (chan->lc_state != L2CAP_CLOSED)
268 l2cap_close(chan, 0);
270 if (chan->lc_lcid != L2CAP_NULL_CID) {
271 LIST_REMOVE(chan, lc_ncid);
272 chan->lc_lcid = L2CAP_NULL_CID;
275 IF_DRAIN(&chan->lc_txq);
278 * Could implement some kind of delayed expunge to make sure that the
279 * CID is really dead before it becomes available for reuse?
282 kfree(chan, M_BLUETOOTH);
283 return 0;
287 * l2cap_listen(l2cap_channel)
289 * Use this channel as a listening post (until detached). This will
290 * result in calls to:
292 * proto->newconn(upper, laddr, raddr)
294 * for incoming connections matching the psm and local address of the
295 * channel (NULL psm/address are permitted and match any protocol/device).
297 * The upper layer should create and return a new channel.
299 * You cannot use this channel for anything else subsequent to this call
302 l2cap_listen(struct l2cap_channel *chan)
304 struct l2cap_channel *used, *prev = NULL;
306 if (chan->lc_lcid != L2CAP_NULL_CID)
307 return EINVAL;
309 if (chan->lc_laddr.bt_psm != L2CAP_PSM_ANY
310 && L2CAP_PSM_INVALID(chan->lc_laddr.bt_psm))
311 return EADDRNOTAVAIL;
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.
319 chan->lc_lcid = L2CAP_SIGNAL_CID;
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.
327 LIST_FOREACH(used, &l2cap_listen_list, lc_ncid) {
328 if (used->lc_laddr.bt_psm < chan->lc_laddr.bt_psm)
329 break;
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;
336 prev = used;
339 if (prev == NULL)
340 LIST_INSERT_HEAD(&l2cap_listen_list, chan, lc_ncid);
341 else
342 LIST_INSERT_AFTER(prev, chan, lc_ncid);
344 return 0;
348 * l2cap_send(l2cap_channel, mbuf)
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:
355 * proto->complete(upper, n)
357 * (currently n == 1)
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.
366 * We only support Basic mode so far, so encapsulate with a
367 * B-Frame header and start sending if we are not already
370 l2cap_send(struct l2cap_channel *chan, struct mbuf *m)
372 l2cap_hdr_t *hdr;
373 int plen;
375 if (chan->lc_state == L2CAP_CLOSED) {
376 m_freem(m);
377 return ENOTCONN;
380 plen = m->m_pkthdr.len;
382 DPRINTFN(5, "send %d bytes on CID #%d (pending = %d)\n",
383 plen, chan->lc_lcid, chan->lc_pending);
385 /* Encapsulate with B-Frame */
386 M_PREPEND(m, sizeof(l2cap_hdr_t), M_NOWAIT);
387 if (m == NULL)
388 return ENOMEM;
390 hdr = mtod(m, l2cap_hdr_t *);
391 hdr->length = htole16(plen);
392 hdr->dcid = htole16(chan->lc_rcid);
394 /* Queue it on our list */
395 IF_ENQUEUE(&chan->lc_txq, m);
397 /* If we are not sending, then start doing so */
398 if (chan->lc_pending == 0)
399 return l2cap_start(chan);
401 return 0;
405 * l2cap_setopt(l2cap_channel, opt, addr)
407 * Apply configuration options to channel. This corresponds to
408 * "Configure Channel Request" in the L2CAP specification.
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)
415 * will be made when the change is complete.
418 l2cap_setopt(struct l2cap_channel *chan, int opt, void *addr)
420 int mode, err = 0;
421 uint16_t mtu;
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;
433 break;
435 case SO_L2CAP_LM: /* set link mode */
436 mode = *(int *)addr;
437 mode &= (L2CAP_LM_SECURE | L2CAP_LM_ENCRYPT | L2CAP_LM_AUTH);
439 if (mode & L2CAP_LM_SECURE)
440 mode |= L2CAP_LM_ENCRYPT;
442 if (mode & L2CAP_LM_ENCRYPT)
443 mode |= L2CAP_LM_AUTH;
445 chan->lc_mode = mode;
447 if (chan->lc_state == L2CAP_OPEN)
448 err = l2cap_setmode(chan);
450 break;
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;
459 return err;
464 * Used in l2cap_socket for set options, coming from socket.
467 l2cap_setopt2(struct l2cap_channel *chan, int opt, struct socket *so,
468 struct sockopt *sopt)
470 int mode, err = 0;
471 uint16_t mtu;
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;
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;
487 break;
489 case SO_L2CAP_LM: /* set link mode */
490 err = soopt_to_kbuf(sopt, &mode, sizeof(int), sizeof(int));
491 if (err)
492 break;
494 mode &= (L2CAP_LM_SECURE | L2CAP_LM_ENCRYPT | L2CAP_LM_AUTH);
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;
502 if (chan->lc_state == L2CAP_OPEN)
503 err = l2cap_setmode(chan);
505 break;
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;
513 return err;
517 * l2cap_getopt(l2cap_channel, opt, addr)
519 * Return configuration parameters.
522 l2cap_getopt(struct l2cap_channel *chan, int opt, void *addr)
525 switch (opt) {
526 case SO_L2CAP_IMTU: /* get Incoming MTU */
527 *(uint16_t *)addr = chan->lc_imtu;
528 return sizeof(uint16_t);
530 case SO_L2CAP_OMTU: /* get Outgoing MTU */
531 *(uint16_t *)addr = chan->lc_omtu;
532 return sizeof(uint16_t);
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);
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);
542 case SO_L2CAP_FLUSH: /* get Flush Timeout */
543 *(uint16_t *)addr = chan->lc_flush;
544 return sizeof(uint16_t);
546 case SO_L2CAP_LM: /* get link mode */
547 *(int *)addr = chan->lc_mode;
548 return sizeof(int);
550 default:
551 break;
554 return 0;