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 $ */
5 * Copyright (c) 2005 Iain Hibbert.
6 * Copyright (c) 2006 Itronix Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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>
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
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
);
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
));
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
));
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
));
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)
137 * proto->connecting(upper)
140 l2cap_connect(struct l2cap_channel
*chan
, struct sockaddr_bt
*dest
)
142 struct hci_unit
*unit
;
145 memcpy(&chan
->lc_raddr
, dest
, sizeof(struct sockaddr_bt
));
147 if (L2CAP_PSM_INVALID(chan
->lc_raddr
.bt_psm
))
150 if (bdaddr_any(&chan
->lc_raddr
.bt_bdaddr
))
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
);
161 unit
= hci_unit_lookup(&chan
->lc_laddr
.bt_bdaddr
);
165 /* attach to active list */
166 err
= l2cap_cid_alloc(chan
);
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
)
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
);
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
);
198 chan
->lc_state
= L2CAP_CLOSED
;
199 hci_acl_close(chan
->lc_link
, err
);
200 chan
->lc_link
= NULL
;
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
));
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
230 l2cap_disconnect(struct l2cap_channel
*chan
, int linger
)
234 if (chan
->lc_state
== L2CAP_CLOSED
235 || chan
->lc_state
== L2CAP_WAIT_DISCONNECT
)
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)
246 chan
->lc_state
= L2CAP_WAIT_DISCONNECT
;
247 err
= l2cap_send_disconnect_req(chan
);
249 l2cap_close(chan
, 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
;
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
);
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
)
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
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
)
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
))
340 LIST_INSERT_HEAD(&l2cap_listen_list
, chan
, lc_ncid
);
342 LIST_INSERT_AFTER(prev
, chan
, lc_ncid
);
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
355 * proto->complete(upper, n)
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
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
)
375 if (chan
->lc_state
== L2CAP_CLOSED
) {
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
);
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
);
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,
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
)
424 case SO_L2CAP_IMTU
: /* set Incoming MTU */
425 mtu
= *(uint16_t *)addr
;
426 if (mtu
< L2CAP_MTU_MINIMUM
)
428 else if (chan
->lc_state
== L2CAP_CLOSED
)
435 case SO_L2CAP_LM
: /* set link mode */
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
);
452 case SO_L2CAP_OQOS
: /* set Outgoing QoS flow spec */
453 case SO_L2CAP_FLUSH
: /* set Outgoing Flush Timeout */
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
)
474 case SO_L2CAP_IMTU
: /* set Incoming MTU */
475 err
= soopt_to_kbuf(sopt
, &mtu
, sizeof(uint16_t),
480 if (mtu
< L2CAP_MTU_MINIMUM
)
482 else if (chan
->lc_state
== L2CAP_CLOSED
)
489 case SO_L2CAP_LM
: /* set link mode */
490 err
= soopt_to_kbuf(sopt
, &mode
, sizeof(int), sizeof(int));
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
);
507 case SO_L2CAP_OQOS
: /* set Outgoing QoS flow spec */
508 case SO_L2CAP_FLUSH
: /* set Outgoing Flush Timeout */
517 * l2cap_getopt(l2cap_channel, opt, addr)
519 * Return configuration parameters.
522 l2cap_getopt(struct l2cap_channel
*chan
, int opt
, void *addr
)
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
;