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