xref: /dragonfly/sys/netbt/rfcomm_dlc.c (revision 6b5c5d0d)
1 /* $OpenBSD: rfcomm_dlc.c,v 1.1 2007/06/01 02:46:12 uwe Exp $ */
2 /* $NetBSD: rfcomm_dlc.c,v 1.3 2007/04/21 06:15:23 plunky Exp $ */
3 /* $DragonFly: src/sys/netbt/rfcomm_dlc.c,v 1.1 2007/12/30 20:02:56 hasso Exp $ */
4 
5 /*-
6  * Copyright (c) 2006 Itronix Inc.
7  * All rights reserved.
8  *
9  * Written by Iain Hibbert for Itronix Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of Itronix Inc. may not be used to endorse
20  *    or promote products derived from this software without specific
21  *    prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/mbuf.h>
41 #include <sys/proc.h>
42 #include <sys/systm.h>
43 #include <sys/endian.h>
44 
45 #include <netbt/bluetooth.h>
46 #include <netbt/hci.h>
47 #include <netbt/l2cap.h>
48 #include <netbt/rfcomm.h>
49 
50 /*
51  * rfcomm_dlc_lookup(rfcomm_session, dlci)
52  *
53  * Find DLC on session with matching dlci
54  */
55 struct rfcomm_dlc *
56 rfcomm_dlc_lookup(struct rfcomm_session *rs, int dlci)
57 {
58 	struct rfcomm_dlc *dlc;
59 
60 	LIST_FOREACH(dlc, &rs->rs_dlcs, rd_next) {
61 		if (dlc->rd_dlci == dlci)
62 			break;
63 	}
64 
65 	return dlc;
66 }
67 
68 /*
69  * rfcomm_dlc_newconn(rfcomm_session, dlci)
70  *
71  * handle a new dlc request (since its called from a couple of places)
72  */
73 struct rfcomm_dlc *
74 rfcomm_dlc_newconn(struct rfcomm_session *rs, int dlci)
75 {
76 	struct rfcomm_session *ls;
77 	struct rfcomm_dlc *new, *dlc, *any, *best;
78 	struct sockaddr_bt laddr, raddr, addr;
79 	int chan;
80 
81 	/*
82 	 * Search amongst the listening DLC community for the best match for
83 	 * address & channel. We keep listening DLC's hanging on listening
84 	 * sessions in a last first order, so scan the entire bunch and keep
85 	 * a note of the best address and BDADDR_ANY matches in order to find
86 	 * the oldest and most specific match.
87 	 */
88 	l2cap_sockaddr(rs->rs_l2cap, &laddr);
89 	l2cap_peeraddr(rs->rs_l2cap, &raddr);
90 	chan = RFCOMM_CHANNEL(dlci);
91 	new = NULL;
92 
93 	any = best = NULL;
94 	LIST_FOREACH(ls, &rfcomm_session_listen, rs_next) {
95 		l2cap_sockaddr(ls->rs_l2cap, &addr);
96 
97 		if (addr.bt_psm != laddr.bt_psm)
98 			continue;
99 
100 		if (bdaddr_same(&laddr.bt_bdaddr, &addr.bt_bdaddr)) {
101 			LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) {
102 				if (dlc->rd_laddr.bt_channel == chan)
103 					best = dlc;
104 			}
105 		}
106 
107 		if (bdaddr_any(&addr.bt_bdaddr)) {
108 			LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) {
109 				if (dlc->rd_laddr.bt_channel == chan)
110 					any = dlc;
111 			}
112 		}
113 	}
114 
115 	dlc = best ? best : any;
116 
117 	/* XXX
118 	 * Note that if this fails, we could have missed a chance to open
119 	 * a connection - really need to rewrite the strategy for storing
120 	 * listening DLC's so all can be checked in turn..
121 	 */
122 	if (dlc != NULL)
123 		new = (*dlc->rd_proto->newconn)(dlc->rd_upper, &laddr, &raddr);
124 
125 	if (new == NULL) {
126 		rfcomm_session_send_frame(rs, RFCOMM_FRAME_DM, dlci);
127 		return NULL;
128 	}
129 
130 	new->rd_dlci = dlci;
131 	new->rd_mtu = rfcomm_mtu_default;
132 	new->rd_mode = dlc->rd_mode;
133 
134 	memcpy(&new->rd_laddr, &laddr, sizeof(struct sockaddr_bt));
135 	new->rd_laddr.bt_channel = chan;
136 
137 	memcpy(&new->rd_raddr, &raddr, sizeof(struct sockaddr_bt));
138 	new->rd_raddr.bt_channel = chan;
139 
140 	new->rd_session = rs;
141 	new->rd_state = RFCOMM_DLC_WAIT_CONNECT;
142 	LIST_INSERT_HEAD(&rs->rs_dlcs, new, rd_next);
143 
144 	return new;
145 }
146 
147 /*
148  * rfcomm_dlc_close(dlc, error)
149  *
150  * detach DLC from session and clean up
151  */
152 void
153 rfcomm_dlc_close(struct rfcomm_dlc *dlc, int err)
154 {
155 	struct rfcomm_session *rs;
156 	struct rfcomm_credit *credit;
157 
158 	KKASSERT(dlc->rd_state != RFCOMM_DLC_CLOSED);
159 
160 	/* Clear credit history */
161 	rs = dlc->rd_session;
162 	STAILQ_FOREACH(credit, &rs->rs_credits, rc_next)
163 		if (credit->rc_dlc == dlc)
164 			credit->rc_dlc = NULL;
165 
166 	callout_stop(&dlc->rd_timeout);
167 
168 	LIST_REMOVE(dlc, rd_next);
169 	dlc->rd_session = NULL;
170 	dlc->rd_state = RFCOMM_DLC_CLOSED;
171 
172 	(*dlc->rd_proto->disconnected)(dlc->rd_upper, err);
173 
174 	/*
175 	 * It is the responsibility of the party who sends the last
176 	 * DISC(dlci) to disconnect the session, but we will schedule
177 	 * an expiry just in case that doesnt happen..
178 	 */
179 	if (LIST_EMPTY(&rs->rs_dlcs)) {
180 		if (rs->rs_state == RFCOMM_SESSION_LISTEN)
181 			rfcomm_session_free(rs);
182 		else
183 			callout_reset(&rs->rs_timeout, rfcomm_ack_timeout * hz,
184 			    rfcomm_session_timeout, rs);
185 	}
186 }
187 
188 /*
189  * rfcomm_dlc_timeout(dlc)
190  *
191  * DLC timeout function is schedUled when we sent any of SABM,
192  * DISC, MCC_MSC, or MCC_PN and should be cancelled when we get
193  * the relevant response. There is nothing to do but shut this
194  * DLC down.
195  */
196 void
197 rfcomm_dlc_timeout(void *arg)
198 {
199 	struct rfcomm_dlc *dlc = arg;
200 
201 	crit_enter();
202 
203 	if (dlc->rd_state != RFCOMM_DLC_CLOSED)
204 		rfcomm_dlc_close(dlc, ETIMEDOUT);
205 	else if (dlc->rd_flags & RFCOMM_DLC_DETACH)
206 		kfree(dlc, M_BLUETOOTH);
207 
208 	crit_exit();
209 }
210 
211 /*
212  * rfcomm_dlc_setmode(rfcomm_dlc)
213  *
214  * Set link mode for DLC.  This is only called when the session is
215  * already open, so we don't need to worry about any previous mode
216  * settings.
217  */
218 int
219 rfcomm_dlc_setmode(struct rfcomm_dlc *dlc)
220 {
221 	int mode = 0;
222 
223 	KKASSERT(dlc->rd_session != NULL);
224 	KKASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
225 
226 	DPRINTF("dlci %d, auth %s, encrypt %s, secure %s\n", dlc->rd_dlci,
227 		(dlc->rd_mode & RFCOMM_LM_AUTH ? "yes" : "no"),
228 		(dlc->rd_mode & RFCOMM_LM_ENCRYPT ? "yes" : "no"),
229 		(dlc->rd_mode & RFCOMM_LM_SECURE ? "yes" : "no"));
230 
231 	if (dlc->rd_mode & RFCOMM_LM_AUTH)
232 		mode |= L2CAP_LM_AUTH;
233 
234 	if (dlc->rd_mode & RFCOMM_LM_ENCRYPT)
235 		mode |= L2CAP_LM_ENCRYPT;
236 
237 	if (dlc->rd_mode & RFCOMM_LM_SECURE)
238 		mode |= L2CAP_LM_SECURE;
239 
240 	return l2cap_setopt(dlc->rd_session->rs_l2cap, SO_L2CAP_LM, &mode);
241 }
242 
243 /*
244  * rfcomm_dlc_connect(rfcomm_dlc)
245  *
246  * initiate DLC connection (session is already connected)
247  */
248 int
249 rfcomm_dlc_connect(struct rfcomm_dlc *dlc)
250 {
251 	struct rfcomm_mcc_pn pn;
252 	int err = 0;
253 
254 	KKASSERT(dlc->rd_session != NULL);
255 	KKASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
256 	KKASSERT(dlc->rd_state == RFCOMM_DLC_WAIT_SESSION);
257 
258 	/*
259 	 * If we have not already sent a PN on the session, we must send
260 	 * a PN to negotiate Credit Flow Control, and this setting will
261 	 * apply to all future connections for this session. We ask for
262 	 * this every time, in order to establish initial credits.
263 	 */
264 	memset(&pn, 0, sizeof(pn));
265 	pn.dlci = dlc->rd_dlci;
266 	pn.priority = dlc->rd_dlci | 0x07;
267 	pn.mtu = htole16(dlc->rd_mtu);
268 
269 	pn.flow_control = 0xf0;
270 	dlc->rd_rxcred = (dlc->rd_rxsize / dlc->rd_mtu);
271 	dlc->rd_rxcred = min(dlc->rd_rxcred, RFCOMM_CREDITS_DEFAULT);
272 	pn.credits = dlc->rd_rxcred;
273 
274 	err = rfcomm_session_send_mcc(dlc->rd_session, 1,
275 					RFCOMM_MCC_PN, &pn, sizeof(pn));
276 	if (err)
277 		return err;
278 
279 	dlc->rd_state = RFCOMM_DLC_WAIT_CONNECT;
280 	callout_reset(&dlc->rd_timeout, rfcomm_mcc_timeout * hz,
281 	    rfcomm_dlc_timeout, dlc);
282 	return 0;
283 }
284 
285 /*
286  * rfcomm_dlc_open(rfcomm_dlc)
287  *
288  * send "Modem Status Command" and mark DLC as open.
289  */
290 int
291 rfcomm_dlc_open(struct rfcomm_dlc *dlc)
292 {
293 	struct rfcomm_mcc_msc msc;
294 	int err;
295 
296 	KKASSERT(dlc->rd_session != NULL);
297 	KKASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
298 
299 	memset(&msc, 0, sizeof(msc));
300 	msc.address = RFCOMM_MKADDRESS(1, dlc->rd_dlci);
301 	msc.modem = dlc->rd_lmodem & 0xfe;	/* EA = 0 */
302 	msc.brk =	0x00	   | 0x01;	/* EA = 1 */
303 
304 	err = rfcomm_session_send_mcc(dlc->rd_session, 1,
305 				RFCOMM_MCC_MSC, &msc, sizeof(msc));
306 	if (err)
307 		return err;
308 
309 	callout_reset(&dlc->rd_timeout, rfcomm_mcc_timeout * hz,
310 	    rfcomm_dlc_timeout, dlc);
311 
312 	dlc->rd_state = RFCOMM_DLC_OPEN;
313 	(*dlc->rd_proto->connected)(dlc->rd_upper);
314 
315 	return 0;
316 }
317 
318 /*
319  * rfcomm_dlc_start(rfcomm_dlc)
320  *
321  * Start sending data (and/or credits) for DLC. Our strategy is to
322  * send anything we can down to the l2cap layer. When credits run
323  * out, data will naturally bunch up. When not using credit flow
324  * control, we limit the number of packets we have pending to reduce
325  * flow control lag.
326  * We should deal with channel priority somehow.
327  */
328 void
329 rfcomm_dlc_start(struct rfcomm_dlc *dlc)
330 {
331 	struct rfcomm_session *rs = dlc->rd_session;
332 	struct mbuf *m;
333 	int len, credits;
334 
335 	KKASSERT(rs != NULL);
336 	KKASSERT(rs->rs_state == RFCOMM_SESSION_OPEN);
337 	KKASSERT(dlc->rd_state == RFCOMM_DLC_OPEN);
338 
339 	for (;;) {
340 		credits = 0;
341 		len = dlc->rd_mtu;
342 		if (rs->rs_flags & RFCOMM_SESSION_CFC) {
343 			credits = (dlc->rd_rxsize / dlc->rd_mtu);
344 			credits -= dlc->rd_rxcred;
345 			credits = min(credits, RFCOMM_CREDITS_MAX);
346 
347 			if (credits > 0)
348 				len--;
349 
350 			if (dlc->rd_txcred == 0)
351 				len = 0;
352 		} else {
353 			if (rs->rs_flags & RFCOMM_SESSION_RFC)
354 				break;
355 
356 			if (dlc->rd_rmodem & RFCOMM_MSC_FC)
357 				break;
358 
359 			if (dlc->rd_pending > RFCOMM_CREDITS_DEFAULT)
360 				break;
361 		}
362 
363 		if (dlc->rd_txbuf == NULL)
364 			len = 0;
365 
366 		if (len == 0) {
367 			if (credits == 0)
368 				break;
369 
370 			/*
371 			 * No need to send small numbers of credits on their
372 			 * own unless the other end hasn't many left.
373 			 */
374 			if (credits < RFCOMM_CREDITS_DEFAULT
375 			    && dlc->rd_rxcred > RFCOMM_CREDITS_DEFAULT)
376 				break;
377 
378 			m = NULL;
379 		} else {
380 			/*
381 			 * take what data we can from (front of) txbuf
382 			 */
383 			m = dlc->rd_txbuf;
384 			if (len < m->m_pkthdr.len) {
385 				dlc->rd_txbuf = m_split(m, len, MB_DONTWAIT);
386 				if (dlc->rd_txbuf == NULL) {
387 					dlc->rd_txbuf = m;
388 					break;
389 				}
390 			} else {
391 				dlc->rd_txbuf = NULL;
392 				len = m->m_pkthdr.len;
393 			}
394 		}
395 
396 		DPRINTFN(10, "dlci %d send %d bytes, %d credits, rxcred = %d\n",
397 			dlc->rd_dlci, len, credits, dlc->rd_rxcred);
398 
399 		if (rfcomm_session_send_uih(rs, dlc, credits, m)) {
400 			kprintf("%s: lost %d bytes on DLCI %d\n",
401 				__func__, len, dlc->rd_dlci);
402 
403 			break;
404 		}
405 
406 		dlc->rd_pending++;
407 
408 		if (rs->rs_flags & RFCOMM_SESSION_CFC) {
409 			if (len > 0)
410 				dlc->rd_txcred--;
411 
412 			if (credits > 0)
413 				dlc->rd_rxcred += credits;
414 		}
415 	}
416 }
417