1 /* $OpenBSD: src/sys/netbt/rfcomm_dlc.c,v 1.2 2008/02/24 21:34:48 uwe Exp $ */
2 /* $NetBSD: rfcomm_dlc.c,v 1.4 2007/11/03 17:20:17 plunky Exp $ */
5 * Copyright (c) 2006 Itronix Inc.
8 * Written by Iain Hibbert for 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/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 #include <sys/endian.h>
43 #include <netbt/bluetooth.h>
44 #include <netbt/hci.h>
45 #include <netbt/l2cap.h>
46 #include <netbt/rfcomm.h>
49 * rfcomm_dlc_lookup(rfcomm_session, dlci)
51 * Find DLC on session with matching dlci
54 rfcomm_dlc_lookup(struct rfcomm_session
*rs
, int dlci
)
56 struct rfcomm_dlc
*dlc
;
58 LIST_FOREACH(dlc
, &rs
->rs_dlcs
, rd_next
) {
59 if (dlc
->rd_dlci
== dlci
)
67 * rfcomm_dlc_newconn(rfcomm_session, dlci)
69 * handle a new dlc request (since its called from a couple of places)
72 rfcomm_dlc_newconn(struct rfcomm_session
*rs
, int dlci
)
74 struct rfcomm_session
*ls
;
75 struct rfcomm_dlc
*new, *dlc
, *any
, *best
;
76 struct sockaddr_bt laddr
, raddr
, addr
;
80 * Search amongst the listening DLC community for the best match for
81 * address & channel. We keep listening DLC's hanging on listening
82 * sessions in a last first order, so scan the entire bunch and keep
83 * a note of the best address and BDADDR_ANY matches in order to find
84 * the oldest and most specific match.
86 l2cap_sockaddr(rs
->rs_l2cap
, &laddr
);
87 l2cap_peeraddr(rs
->rs_l2cap
, &raddr
);
88 chan
= RFCOMM_CHANNEL(dlci
);
92 LIST_FOREACH(ls
, &rfcomm_session_listen
, rs_next
) {
93 l2cap_sockaddr(ls
->rs_l2cap
, &addr
);
95 if (addr
.bt_psm
!= laddr
.bt_psm
)
98 if (bdaddr_same(&laddr
.bt_bdaddr
, &addr
.bt_bdaddr
)) {
99 LIST_FOREACH(dlc
, &ls
->rs_dlcs
, rd_next
) {
100 if (dlc
->rd_laddr
.bt_channel
== chan
)
105 if (bdaddr_any(&addr
.bt_bdaddr
)) {
106 LIST_FOREACH(dlc
, &ls
->rs_dlcs
, rd_next
) {
107 if (dlc
->rd_laddr
.bt_channel
== chan
)
113 dlc
= best
? best
: any
;
116 * Note that if this fails, we could have missed a chance to open
117 * a connection - really need to rewrite the strategy for storing
118 * listening DLC's so all can be checked in turn..
121 new = (*dlc
->rd_proto
->newconn
)(dlc
->rd_upper
, &laddr
, &raddr
);
124 rfcomm_session_send_frame(rs
, RFCOMM_FRAME_DM
, dlci
);
129 new->rd_mtu
= rfcomm_mtu_default
;
130 new->rd_mode
= dlc
->rd_mode
;
132 memcpy(&new->rd_laddr
, &laddr
, sizeof(struct sockaddr_bt
));
133 new->rd_laddr
.bt_channel
= chan
;
135 memcpy(&new->rd_raddr
, &raddr
, sizeof(struct sockaddr_bt
));
136 new->rd_raddr
.bt_channel
= chan
;
138 new->rd_session
= rs
;
139 new->rd_state
= RFCOMM_DLC_WAIT_CONNECT
;
140 LIST_INSERT_HEAD(&rs
->rs_dlcs
, new, rd_next
);
146 * rfcomm_dlc_close(dlc, error)
148 * detach DLC from session and clean up
151 rfcomm_dlc_close(struct rfcomm_dlc
*dlc
, int err
)
153 struct rfcomm_session
*rs
;
154 struct rfcomm_credit
*credit
;
156 KKASSERT(dlc
->rd_state
!= RFCOMM_DLC_CLOSED
);
158 /* Clear credit history */
159 rs
= dlc
->rd_session
;
160 STAILQ_FOREACH(credit
, &rs
->rs_credits
, rc_next
)
161 if (credit
->rc_dlc
== dlc
)
162 credit
->rc_dlc
= NULL
;
164 callout_stop(&dlc
->rd_timeout
);
166 LIST_REMOVE(dlc
, rd_next
);
167 dlc
->rd_session
= NULL
;
168 dlc
->rd_state
= RFCOMM_DLC_CLOSED
;
170 (*dlc
->rd_proto
->disconnected
)(dlc
->rd_upper
, err
);
173 * It is the responsibility of the party who sends the last
174 * DISC(dlci) to disconnect the session, but we will schedule
175 * an expiry just in case that doesnt happen..
177 if (LIST_EMPTY(&rs
->rs_dlcs
)) {
178 if (rs
->rs_state
== RFCOMM_SESSION_LISTEN
)
179 rfcomm_session_free(rs
);
181 callout_reset(&rs
->rs_timeout
, rfcomm_ack_timeout
* hz
,
182 rfcomm_session_timeout
, rs
);
187 * rfcomm_dlc_timeout(dlc)
189 * DLC timeout function is schedUled when we sent any of SABM,
190 * DISC, MCC_MSC, or MCC_PN and should be cancelled when we get
191 * the relevant response. There is nothing to do but shut this
195 rfcomm_dlc_timeout(void *arg
)
197 struct rfcomm_dlc
*dlc
= arg
;
201 if (dlc
->rd_state
!= RFCOMM_DLC_CLOSED
)
202 rfcomm_dlc_close(dlc
, ETIMEDOUT
);
203 else if (dlc
->rd_flags
& RFCOMM_DLC_DETACH
)
204 kfree(dlc
, M_BLUETOOTH
);
210 * rfcomm_dlc_setmode(rfcomm_dlc)
212 * Set link mode for DLC. This is only called when the session is
213 * already open, so we don't need to worry about any previous mode
217 rfcomm_dlc_setmode(struct rfcomm_dlc
*dlc
)
221 KKASSERT(dlc
->rd_session
!= NULL
);
222 KKASSERT(dlc
->rd_session
->rs_state
== RFCOMM_SESSION_OPEN
);
224 DPRINTF("dlci %d, auth %s, encrypt %s, secure %s\n", dlc
->rd_dlci
,
225 (dlc
->rd_mode
& RFCOMM_LM_AUTH
? "yes" : "no"),
226 (dlc
->rd_mode
& RFCOMM_LM_ENCRYPT
? "yes" : "no"),
227 (dlc
->rd_mode
& RFCOMM_LM_SECURE
? "yes" : "no"));
229 if (dlc
->rd_mode
& RFCOMM_LM_AUTH
)
230 mode
|= L2CAP_LM_AUTH
;
232 if (dlc
->rd_mode
& RFCOMM_LM_ENCRYPT
)
233 mode
|= L2CAP_LM_ENCRYPT
;
235 if (dlc
->rd_mode
& RFCOMM_LM_SECURE
)
236 mode
|= L2CAP_LM_SECURE
;
238 return l2cap_setopt(dlc
->rd_session
->rs_l2cap
, SO_L2CAP_LM
, &mode
);
242 * rfcomm_dlc_connect(rfcomm_dlc)
244 * initiate DLC connection (session is already connected)
247 rfcomm_dlc_connect(struct rfcomm_dlc
*dlc
)
249 struct rfcomm_mcc_pn pn
;
252 KKASSERT(dlc
->rd_session
!= NULL
);
253 KKASSERT(dlc
->rd_session
->rs_state
== RFCOMM_SESSION_OPEN
);
254 KKASSERT(dlc
->rd_state
== RFCOMM_DLC_WAIT_SESSION
);
257 * If we have not already sent a PN on the session, we must send
258 * a PN to negotiate Credit Flow Control, and this setting will
259 * apply to all future connections for this session. We ask for
260 * this every time, in order to establish initial credits.
262 memset(&pn
, 0, sizeof(pn
));
263 pn
.dlci
= dlc
->rd_dlci
;
264 pn
.priority
= dlc
->rd_dlci
| 0x07;
265 pn
.mtu
= htole16(dlc
->rd_mtu
);
267 pn
.flow_control
= 0xf0;
268 dlc
->rd_rxcred
= (dlc
->rd_rxsize
/ dlc
->rd_mtu
);
269 dlc
->rd_rxcred
= min(dlc
->rd_rxcred
, RFCOMM_CREDITS_DEFAULT
);
270 pn
.credits
= dlc
->rd_rxcred
;
272 err
= rfcomm_session_send_mcc(dlc
->rd_session
, 1,
273 RFCOMM_MCC_PN
, &pn
, sizeof(pn
));
277 dlc
->rd_state
= RFCOMM_DLC_WAIT_CONNECT
;
278 callout_reset(&dlc
->rd_timeout
, rfcomm_mcc_timeout
* hz
,
279 rfcomm_dlc_timeout
, dlc
);
284 * rfcomm_dlc_open(rfcomm_dlc)
286 * send "Modem Status Command" and mark DLC as open.
289 rfcomm_dlc_open(struct rfcomm_dlc
*dlc
)
291 struct rfcomm_mcc_msc msc
;
294 KKASSERT(dlc
->rd_session
!= NULL
);
295 KKASSERT(dlc
->rd_session
->rs_state
== RFCOMM_SESSION_OPEN
);
297 memset(&msc
, 0, sizeof(msc
));
298 msc
.address
= RFCOMM_MKADDRESS(1, dlc
->rd_dlci
);
299 msc
.modem
= dlc
->rd_lmodem
& 0xfe; /* EA = 0 */
300 msc
.brk
= 0x00 | 0x01; /* EA = 1 */
302 err
= rfcomm_session_send_mcc(dlc
->rd_session
, 1,
303 RFCOMM_MCC_MSC
, &msc
, sizeof(msc
));
307 callout_reset(&dlc
->rd_timeout
, rfcomm_mcc_timeout
* hz
,
308 rfcomm_dlc_timeout
, dlc
);
310 dlc
->rd_state
= RFCOMM_DLC_OPEN
;
311 (*dlc
->rd_proto
->connected
)(dlc
->rd_upper
);
317 * rfcomm_dlc_start(rfcomm_dlc)
319 * Start sending data (and/or credits) for DLC. Our strategy is to
320 * send anything we can down to the l2cap layer. When credits run
321 * out, data will naturally bunch up. When not using credit flow
322 * control, we limit the number of packets we have pending to reduce
324 * We should deal with channel priority somehow.
327 rfcomm_dlc_start(struct rfcomm_dlc
*dlc
)
329 struct rfcomm_session
*rs
= dlc
->rd_session
;
333 KKASSERT(rs
!= NULL
);
334 KKASSERT(rs
->rs_state
== RFCOMM_SESSION_OPEN
);
335 KKASSERT(dlc
->rd_state
== RFCOMM_DLC_OPEN
);
340 if (rs
->rs_flags
& RFCOMM_SESSION_CFC
) {
341 credits
= (dlc
->rd_rxsize
/ dlc
->rd_mtu
);
342 credits
-= dlc
->rd_rxcred
;
343 credits
= min(credits
, RFCOMM_CREDITS_MAX
);
348 if (dlc
->rd_txcred
== 0)
351 if (rs
->rs_flags
& RFCOMM_SESSION_RFC
)
354 if (dlc
->rd_rmodem
& RFCOMM_MSC_FC
)
357 if (dlc
->rd_pending
> RFCOMM_CREDITS_DEFAULT
)
361 if (dlc
->rd_txbuf
== NULL
)
369 * No need to send small numbers of credits on their
370 * own unless the other end hasn't many left.
372 if (credits
< RFCOMM_CREDITS_DEFAULT
373 && dlc
->rd_rxcred
> RFCOMM_CREDITS_DEFAULT
)
379 * take what data we can from (front of) txbuf
382 if (len
< m
->m_pkthdr
.len
) {
383 dlc
->rd_txbuf
= m_split(m
, len
, M_NOWAIT
);
384 if (dlc
->rd_txbuf
== NULL
) {
389 dlc
->rd_txbuf
= NULL
;
390 len
= m
->m_pkthdr
.len
;
394 DPRINTFN(10, "dlci %d send %d bytes, %d credits, rxcred = %d\n",
395 dlc
->rd_dlci
, len
, credits
, dlc
->rd_rxcred
);
397 if (rfcomm_session_send_uih(rs
, dlc
, credits
, m
)) {
398 kprintf("%s: lost %d bytes on DLCI %d\n",
399 __func__
, len
, dlc
->rd_dlci
);
406 if (rs
->rs_flags
& RFCOMM_SESSION_CFC
) {
411 dlc
->rd_rxcred
+= credits
;