1 /* $DragonFly: src/sys/netbt/l2cap_upper.c,v 1.3 2008/06/20 20:52:29 aggelos Exp $ */
2 /* $OpenBSD: l2cap_upper.c,v 1.2 2007/10/01 16:39:30 krw Exp $ */
3 /* $NetBSD: l2cap_upper.c,v 1.8 2007/04/29 20:23:36 msaitoh Exp $ */
6 * Copyright (c) 2005 Iain Hibbert.
7 * Copyright (c) 2006 Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
35 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/kernel.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>
47 #include <netbt/bluetooth.h>
48 #include <netbt/hci.h>
49 #include <netbt/l2cap.h>
51 /*******************************************************************************
53 * L2CAP Channel - Upper Protocol API
57 * l2cap_attach(handle, btproto, upper)
59 * attach new l2cap_channel to handle, populate
60 * with reasonable defaults
63 l2cap_attach(struct l2cap_channel
**handle
,
64 const struct btproto
*proto
, void *upper
)
66 struct l2cap_channel
*chan
;
68 KKASSERT(handle
!= NULL
);
69 KKASSERT(proto
!= NULL
);
70 KKASSERT(upper
!= NULL
);
72 chan
= kmalloc(sizeof(*chan
), M_BLUETOOTH
, M_NOWAIT
| M_ZERO
);
76 chan
->lc_proto
= proto
;
77 chan
->lc_upper
= upper
;
79 chan
->lc_state
= L2CAP_CLOSED
;
81 chan
->lc_lcid
= L2CAP_NULL_CID
;
82 chan
->lc_rcid
= L2CAP_NULL_CID
;
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
;
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
;
92 chan
->lc_imtu
= L2CAP_MTU_DEFAULT
;
93 chan
->lc_omtu
= L2CAP_MTU_DEFAULT
;
94 chan
->lc_flush
= L2CAP_FLUSH_TIMO_DEFAULT
;
96 memcpy(&chan
->lc_iqos
, &l2cap_default_qos
, sizeof(l2cap_qos_t
));
97 memcpy(&chan
->lc_oqos
, &l2cap_default_qos
, sizeof(l2cap_qos_t
));
104 * l2cap_bind(l2cap_channel, sockaddr)
106 * set local address of channel
109 l2cap_bind(struct l2cap_channel
*chan
, struct sockaddr_bt
*addr
)
112 memcpy(&chan
->lc_laddr
, addr
, sizeof(struct sockaddr_bt
));
117 * l2cap_sockaddr(l2cap_channel, sockaddr)
119 * get local address of channel
122 l2cap_sockaddr(struct l2cap_channel
*chan
, struct sockaddr_bt
*addr
)
124 memcpy(addr
, &chan
->lc_laddr
, sizeof(struct sockaddr_bt
));
129 * l2cap_connect(l2cap_channel, sockaddr)
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:
135 * proto->connected(upper)
136 * proto->disconnected(upper, error)
139 * proto->connecting(upper)
142 l2cap_connect(struct l2cap_channel
*chan
, struct sockaddr_bt
*dest
)
144 struct hci_unit
*unit
;
147 memcpy(&chan
->lc_raddr
, dest
, sizeof(struct sockaddr_bt
));
149 if (L2CAP_PSM_INVALID(chan
->lc_raddr
.bt_psm
))
152 if (bdaddr_any(&chan
->lc_raddr
.bt_bdaddr
))
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
);
163 unit
= hci_unit_lookup(&chan
->lc_laddr
.bt_bdaddr
);
167 /* attach to active list */
168 err
= l2cap_cid_alloc(chan
);
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
)
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
);
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.
192 chan
->lc_state
= L2CAP_WAIT_RECV_CONNECT_RSP
;
193 err
= l2cap_send_connect_req(chan
);
200 chan
->lc_state
= L2CAP_CLOSED
;
201 hci_acl_close(chan
->lc_link
, err
);
202 chan
->lc_link
= NULL
;
207 * l2cap_peeraddr(l2cap_channel, sockaddr)
209 * get remote address of channel
212 l2cap_peeraddr(struct l2cap_channel
*chan
, struct sockaddr_bt
*addr
)
214 memcpy(addr
, &chan
->lc_raddr
, sizeof(struct sockaddr_bt
));
219 * l2cap_disconnect(l2cap_channel, linger)
221 * Initiate L2CAP disconnection. This corresponds to
222 * "Close Channel Request" in the L2CAP specification
223 * and will result in a call to
225 * proto->disconnected(upper, error)
227 * when the disconnection is complete. If linger is set,
228 * the call will not be made until data has flushed from
232 l2cap_disconnect(struct l2cap_channel
*chan
, int linger
)
236 if (chan
->lc_state
== L2CAP_CLOSED
237 || chan
->lc_state
== L2CAP_WAIT_DISCONNECT
)
240 chan
->lc_flags
|= L2CAP_SHUTDOWN
;
243 * no need to do anything unless the queue is empty or
244 * we are not lingering..
246 if ((IF_QEMPTY(&chan
->lc_txq
) && chan
->lc_pending
== 0)
248 chan
->lc_state
= L2CAP_WAIT_DISCONNECT
;
249 err
= l2cap_send_disconnect_req(chan
);
251 l2cap_close(chan
, err
);
257 * l2cap_detach(handle)
259 * Detach l2cap channel from handle & close it down
262 l2cap_detach(struct l2cap_channel
**handle
)
264 struct l2cap_channel
*chan
;
269 if (chan
->lc_state
!= L2CAP_CLOSED
)
270 l2cap_close(chan
, 0);
272 if (chan
->lc_lcid
!= L2CAP_NULL_CID
) {
273 LIST_REMOVE(chan
, lc_ncid
);
274 chan
->lc_lcid
= L2CAP_NULL_CID
;
277 IF_DRAIN(&chan
->lc_txq
);
280 * Could implement some kind of delayed expunge to make sure that the
281 * CID is really dead before it becomes available for reuse?
284 kfree(chan
, M_BLUETOOTH
);
289 * l2cap_listen(l2cap_channel)
291 * Use this channel as a listening post (until detached). This will
292 * result in calls to:
294 * proto->newconn(upper, laddr, raddr)
296 * for incoming connections matching the psm and local address of the
297 * channel (NULL psm/address are permitted and match any protocol/device).
299 * The upper layer should create and return a new channel.
301 * You cannot use this channel for anything else subsequent to this call
304 l2cap_listen(struct l2cap_channel
*chan
)
306 struct l2cap_channel
*used
, *prev
= NULL
;
308 if (chan
->lc_lcid
!= L2CAP_NULL_CID
)
311 if (chan
->lc_laddr
.bt_psm
!= L2CAP_PSM_ANY
312 && L2CAP_PSM_INVALID(chan
->lc_laddr
.bt_psm
))
313 return EADDRNOTAVAIL
;
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
321 chan
->lc_lcid
= L2CAP_SIGNAL_CID
;
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.
329 LIST_FOREACH(used
, &l2cap_listen_list
, lc_ncid
) {
330 if (used
->lc_laddr
.bt_psm
< chan
->lc_laddr
.bt_psm
)
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
))
342 LIST_INSERT_HEAD(&l2cap_listen_list
, chan
, lc_ncid
);
344 LIST_INSERT_AFTER(prev
, chan
, lc_ncid
);
350 * l2cap_send(l2cap_channel, mbuf)
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
357 * proto->complete(upper, n)
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
368 * We only support Basic mode so far, so encapsulate with a
369 * B-Frame header and start sending if we are not already
372 l2cap_send(struct l2cap_channel
*chan
, struct mbuf
*m
)
377 if (chan
->lc_state
== L2CAP_CLOSED
) {
382 plen
= m
->m_pkthdr
.len
;
384 DPRINTFN(5, "send %d bytes on CID #%d (pending = %d)\n",
385 plen
, chan
->lc_lcid
, chan
->lc_pending
);
387 /* Encapsulate with B-Frame */
388 M_PREPEND(m
, sizeof(l2cap_hdr_t
), MB_DONTWAIT
);
392 hdr
= mtod(m
, l2cap_hdr_t
*);
393 hdr
->length
= htole16(plen
);
394 hdr
->dcid
= htole16(chan
->lc_rcid
);
396 /* Queue it on our list */
397 IF_ENQUEUE(&chan
->lc_txq
, m
);
399 /* If we are not sending, then start doing so */
400 if (chan
->lc_pending
== 0)
401 return l2cap_start(chan
);
407 * l2cap_setopt(l2cap_channel, opt, addr)
409 * Apply configuration options to channel. This corresponds to
410 * "Configure Channel Request" in the L2CAP specification.
412 * for SO_L2CAP_LM, the settings will take effect when the
413 * channel is established. If the channel is already open,
415 * proto->linkmode(upper, new)
417 * will be made when the change is complete.
420 l2cap_setopt(struct l2cap_channel
*chan
, int opt
, void *addr
)
426 case SO_L2CAP_IMTU
: /* set Incoming MTU */
427 mtu
= *(uint16_t *)addr
;
428 if (mtu
< L2CAP_MTU_MINIMUM
)
430 else if (chan
->lc_state
== L2CAP_CLOSED
)
437 case SO_L2CAP_LM
: /* set link mode */
439 mode
&= (L2CAP_LM_SECURE
| L2CAP_LM_ENCRYPT
| L2CAP_LM_AUTH
);
441 if (mode
& L2CAP_LM_SECURE
)
442 mode
|= L2CAP_LM_ENCRYPT
;
444 if (mode
& L2CAP_LM_ENCRYPT
)
445 mode
|= L2CAP_LM_AUTH
;
447 chan
->lc_mode
= mode
;
449 if (chan
->lc_state
== L2CAP_OPEN
)
450 err
= l2cap_setmode(chan
);
454 case SO_L2CAP_OQOS
: /* set Outgoing QoS flow spec */
455 case SO_L2CAP_FLUSH
: /* set Outgoing Flush Timeout */
466 * Used in l2cap_socket for set options, coming from socket.
469 l2cap_setopt2(struct l2cap_channel
*chan
, int opt
, struct socket
*so
,
470 struct sockopt
*sopt
)
476 case SO_L2CAP_IMTU
: /* set Incoming MTU */
477 err
= soopt_to_kbuf(sopt
, &mtu
, sizeof(uint16_t),
482 if (mtu
< L2CAP_MTU_MINIMUM
)
484 else if (chan
->lc_state
== L2CAP_CLOSED
)
491 case SO_L2CAP_LM
: /* set link mode */
492 err
= soopt_to_kbuf(sopt
, &mode
, sizeof(int), sizeof(int));
496 mode
&= (L2CAP_LM_SECURE
| L2CAP_LM_ENCRYPT
| L2CAP_LM_AUTH
);
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
;
504 if (chan
->lc_state
== L2CAP_OPEN
)
505 err
= l2cap_setmode(chan
);
509 case SO_L2CAP_OQOS
: /* set Outgoing QoS flow spec */
510 case SO_L2CAP_FLUSH
: /* set Outgoing Flush Timeout */
519 * l2cap_getopt(l2cap_channel, opt, addr)
521 * Return configuration parameters.
524 l2cap_getopt(struct l2cap_channel
*chan
, int opt
, void *addr
)
528 case SO_L2CAP_IMTU
: /* get Incoming MTU */
529 *(uint16_t *)addr
= chan
->lc_imtu
;
530 return sizeof(uint16_t);
532 case SO_L2CAP_OMTU
: /* get Outgoing MTU */
533 *(uint16_t *)addr
= chan
->lc_omtu
;
534 return sizeof(uint16_t);
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
);
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
);
544 case SO_L2CAP_FLUSH
: /* get Flush Timeout */
545 *(uint16_t *)addr
= chan
->lc_flush
;
546 return sizeof(uint16_t);
548 case SO_L2CAP_LM
: /* get link mode */
549 *(int *)addr
= chan
->lc_mode
;