18bf2d53dc2ed9f5410bd4a33fd19af5f4398f28
[dragonfly.git] / sys / netbt / rfcomm_socket.c
1 /* $DragonFly: src/sys/netbt/rfcomm_socket.c,v 1.3 2008/06/20 20:52:29 aggelos Exp $ */
2 /* $OpenBSD: src/sys/netbt/rfcomm_socket.c,v 1.2 2008/02/24 21:34:48 uwe Exp $ */
3 /* $NetBSD: rfcomm_socket.c,v 1.8 2007/10/15 18:04:34 plunky 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 /* load symbolic names */
37 #ifdef BLUETOOTH_DEBUG
38 #define PRUREQUESTS
39 #define PRCOREQUESTS
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/domain.h>
44 #include <sys/kernel.h>
45 #include <sys/mbuf.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/systm.h>
51
52 #include <sys/msgport2.h>
53
54 #include <vm/vm_zone.h>
55
56 #include <netbt/bluetooth.h>
57 #include <netbt/hci.h>          /* XXX for EPASSTHROUGH */
58 #include <netbt/rfcomm.h>
59
60 /****************************************************************************
61  *
62  *      RFCOMM SOCK_STREAM Sockets - serial line emulation
63  *
64  */
65
66 static void rfcomm_connecting(void *);
67 static void rfcomm_connected(void *);
68 static void rfcomm_disconnected(void *, int);
69 static void *rfcomm_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
70 static void rfcomm_complete(void *, int);
71 static void rfcomm_linkmode(void *, int);
72 static void rfcomm_input(void *, struct mbuf *);
73
74 static const struct btproto rfcomm_proto = {
75         rfcomm_connecting,
76         rfcomm_connected,
77         rfcomm_disconnected,
78         rfcomm_newconn,
79         rfcomm_complete,
80         rfcomm_linkmode,
81         rfcomm_input,
82 };
83
84 /* sysctl variables */
85 int rfcomm_sendspace = 4096;
86 int rfcomm_recvspace = 4096;
87
88 /*
89  * rfcomm_ctloutput(request, socket, level, optname, opt)
90  *
91  */
92 void
93 rfcomm_ctloutput(netmsg_t msg)
94 {
95         struct socket *so = msg->ctloutput.base.nm_so;
96         struct sockopt *sopt = msg->ctloutput.nm_sopt;
97         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
98         struct mbuf *m;
99         int error = 0;
100
101 #ifdef notyet                   /* XXX */
102         DPRINTFN(2, "%s\n", prcorequests[sopt->sopt_dir]);
103 #endif
104
105         if (pcb == NULL) {
106                 error = EINVAL;
107                 goto out;
108         }
109
110         if (sopt->sopt_level != BTPROTO_RFCOMM) {
111                 error = ENOPROTOOPT;
112                 goto out;
113         }
114
115         switch(sopt->sopt_dir) {
116         case PRCO_GETOPT:
117                 m = m_get(M_WAITOK, MT_DATA);
118                 crit_enter();
119                 m->m_len = rfcomm_getopt(pcb, sopt->sopt_name, mtod(m, void *));
120                 crit_exit();            
121                 if (m->m_len == 0) {
122                         m_freem(m);
123                         m = NULL;
124                         error = ENOPROTOOPT;
125                 }
126                 soopt_from_kbuf(sopt, mtod(m, void *), m->m_len);
127                 break;
128
129         case PRCO_SETOPT:
130                 error = rfcomm_setopt2(pcb, sopt->sopt_name, so, sopt);
131
132                 break;
133
134         default:
135                 error = ENOPROTOOPT;
136                 break;
137         }
138 out:
139         lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
140 }
141
142 /**********************************************************************
143  *
144  * RFCOMM callbacks
145  */
146
147 static void
148 rfcomm_connecting(void *arg)
149 {
150         /* struct socket *so = arg; */
151
152         KKASSERT(arg != NULL);
153         DPRINTF("Connecting\n");
154 }
155
156 static void
157 rfcomm_connected(void *arg)
158 {
159         struct socket *so = arg;
160
161         KKASSERT(so != NULL);
162         DPRINTF("Connected\n");
163         soisconnected(so);
164 }
165
166 static void
167 rfcomm_disconnected(void *arg, int err)
168 {
169         struct socket *so = arg;
170
171         KKASSERT(so != NULL);
172         DPRINTF("Disconnected\n");
173
174         so->so_error = err;
175         soisdisconnected(so);
176 }
177
178 static void *
179 rfcomm_newconn(void *arg, struct sockaddr_bt *laddr,
180     struct sockaddr_bt *raddr)
181 {
182         struct socket *so = arg;
183
184         DPRINTF("New Connection\n");
185         so = sonewconn(so, 0);
186         if (so == NULL)
187                 return NULL;
188
189         soisconnecting(so);
190
191         return so->so_pcb;
192 }
193
194 /*
195  * rfcomm_complete(rfcomm_dlc, length)
196  *
197  * length bytes are sent and may be removed from socket buffer
198  */
199 static void
200 rfcomm_complete(void *arg, int length)
201 {
202         struct socket *so = arg;
203
204         sbdrop(&so->so_snd.sb, length);
205         sowwakeup(so);
206 }
207
208 /*
209  * rfcomm_linkmode(rfcomm_dlc, new)
210  *
211  * link mode change notification.
212  */
213 static void
214 rfcomm_linkmode(void *arg, int new)
215 {
216         struct socket *so = arg;
217         int mode;
218
219         DPRINTF("auth %s, encrypt %s, secure %s\n",
220                 (new & RFCOMM_LM_AUTH ? "on" : "off"),
221                 (new & RFCOMM_LM_ENCRYPT ? "on" : "off"),
222                 (new & RFCOMM_LM_SECURE ? "on" : "off"));
223
224         (void)rfcomm_getopt(so->so_pcb, SO_RFCOMM_LM, &mode);
225         if (((mode & RFCOMM_LM_AUTH) && !(new & RFCOMM_LM_AUTH))
226             || ((mode & RFCOMM_LM_ENCRYPT) && !(new & RFCOMM_LM_ENCRYPT))
227             || ((mode & RFCOMM_LM_SECURE) && !(new & RFCOMM_LM_SECURE)))
228                 rfcomm_disconnect(so->so_pcb, 0);
229 }
230
231 /*
232  * rfcomm_input(rfcomm_dlc, mbuf)
233  */
234 static void
235 rfcomm_input(void *arg, struct mbuf *m)
236 {
237         struct socket *so = arg;
238
239         KKASSERT(so != NULL);
240
241         if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
242                 kprintf("%s: %d bytes dropped (socket buffer full)\n",
243                         __func__, m->m_pkthdr.len);
244                 m_freem(m);
245                 return;
246         }
247
248         DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
249
250         sbappendstream(&so->so_rcv.sb, m);
251         sorwakeup(so);
252 }
253
254 /*
255  * Implementation of usrreqs.
256  */
257 static void
258 rfcomm_sdetach(netmsg_t msg)
259 {
260         struct socket *so = msg->detach.base.nm_so;
261         int error;
262
263         error = rfcomm_detach((struct rfcomm_dlc **)&so->so_pcb);
264         lwkt_replymsg(&msg->detach.base.lmsg, error);
265 }
266
267 /*
268  * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
269  *       will sofree() it when we return.
270  */
271 static void
272 rfcomm_sabort(netmsg_t msg)
273 {
274         struct socket *so = msg->abort.base.nm_so;
275         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
276
277         rfcomm_disconnect(pcb, 0);
278         soisdisconnected(so);
279         rfcomm_sdetach(msg);
280         /* msg invalid now */
281 }
282
283 static void
284 rfcomm_sdisconnect(netmsg_t msg)
285 {
286         struct socket *so = msg->abort.base.nm_so;
287         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
288         int error;
289
290         soisdisconnecting(so);
291         error = rfcomm_disconnect(pcb, so->so_linger);
292         lwkt_replymsg(&msg->disconnect.base.lmsg, error);
293 }
294
295 static void
296 rfcomm_scontrol(netmsg_t msg)
297 {
298         lwkt_replymsg(&msg->control.base.lmsg, EPASSTHROUGH);
299 }
300
301 static void
302 rfcomm_sattach(netmsg_t msg)
303 {
304         struct socket *so = msg->attach.base.nm_so;
305         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
306         int error;
307
308         if (pcb != NULL) {
309                 error = EINVAL;
310                 goto out;
311         }
312
313         /*
314          * Since we have nothing to add, we attach the DLC
315          * structure directly to our PCB pointer.
316          */
317         error = soreserve(so, rfcomm_sendspace, rfcomm_recvspace, NULL);
318         if (error)
319                 goto out;
320
321         error = rfcomm_attach((struct rfcomm_dlc **)&so->so_pcb,
322                               &rfcomm_proto, so);
323         if (error)
324                 goto out;
325
326         error = rfcomm_rcvd(so->so_pcb, sbspace(&so->so_rcv));
327         if (error)
328                 rfcomm_detach((struct rfcomm_dlc **)&so->so_pcb);
329 out:
330         lwkt_replymsg(&msg->attach.base.lmsg, error);
331 }
332
333 static void
334 rfcomm_sbind(netmsg_t msg)
335 {
336         struct socket *so = msg->bind.base.nm_so;
337         struct sockaddr *nam = msg->bind.nm_nam;
338         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
339         struct sockaddr_bt *sa;
340         int error;
341
342         KKASSERT(nam != NULL);
343         sa = (struct sockaddr_bt *)nam;
344
345         if (sa->bt_len != sizeof(struct sockaddr_bt)) {
346                 error = EINVAL;
347         } else if (sa->bt_family != AF_BLUETOOTH) {
348                 error = EAFNOSUPPORT;
349         } else {
350                 error = rfcomm_bind(pcb, sa);
351         }
352         lwkt_replymsg(&msg->bind.base.lmsg, error);
353 }
354
355 static void
356 rfcomm_sconnect(netmsg_t msg)
357 {
358         struct socket *so = msg->connect.base.nm_so;
359         struct sockaddr *nam = msg->connect.nm_nam;
360         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
361         struct sockaddr_bt *sa;
362         int error;
363
364         KKASSERT(nam != NULL);
365         sa = (struct sockaddr_bt *)nam;
366
367         if (sa->bt_len != sizeof(struct sockaddr_bt)) {
368                 error = EINVAL;
369         } else if (sa->bt_family != AF_BLUETOOTH) {
370                 error = EAFNOSUPPORT;
371         } else {
372                 soisconnecting(so);
373                 error = rfcomm_connect(pcb, sa);
374         }
375         lwkt_replymsg(&msg->connect.base.lmsg, error);
376 }
377
378 static void
379 rfcomm_speeraddr(netmsg_t msg)
380 {
381         struct socket *so = msg->peeraddr.base.nm_so;
382         struct sockaddr **nam = msg->peeraddr.nm_nam;
383         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
384         struct sockaddr_bt *sa, ssa;
385         int error;
386
387         sa = &ssa;
388         bzero(sa, sizeof *sa);
389         sa->bt_len = sizeof(struct sockaddr_bt);
390         sa->bt_family = AF_BLUETOOTH;
391         error = rfcomm_peeraddr(pcb, sa);;
392         *nam = dup_sockaddr((struct sockaddr *)sa);
393
394         lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
395 }
396
397 static void
398 rfcomm_ssockaddr(netmsg_t msg)
399 {
400         struct socket *so = msg->sockaddr.base.nm_so;
401         struct sockaddr **nam = msg->sockaddr.nm_nam;
402         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
403         struct sockaddr_bt *sa, ssa;
404         int error;
405
406         sa = &ssa;
407         bzero(sa, sizeof *sa);
408         sa->bt_len = sizeof(struct sockaddr_bt);
409         sa->bt_family = AF_BLUETOOTH;
410         error = rfcomm_sockaddr(pcb, sa);;
411         *nam = dup_sockaddr((struct sockaddr *)sa);
412
413         lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
414 }
415
416 static void
417 rfcomm_sshutdown(netmsg_t msg)
418 {
419         struct socket *so = msg->shutdown.base.nm_so;
420
421         socantsendmore(so);
422         lwkt_replymsg(&msg->shutdown.base.lmsg, 0);
423 }
424
425 static void
426 rfcomm_ssend(netmsg_t msg)
427 {
428         struct socket *so = msg->send.base.nm_so;
429         struct mbuf *m = msg->send.nm_m;
430         struct mbuf *control = msg->send.nm_control;
431         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
432         struct mbuf *m0;
433         int error;
434
435         KKASSERT(m != NULL);
436
437         /* no use for that */
438         if (control) {
439                 m_freem(control);
440                 control = NULL;
441         }
442
443         m0 = m_copym(m, 0, M_COPYALL, MB_DONTWAIT);
444         if (m0) {
445                 sbappendstream(&so->so_snd.sb, m);
446                 error = rfcomm_send(pcb, m0);
447         } else {
448                 error = ENOMEM;
449         }
450         lwkt_replymsg(&msg->send.base.lmsg, error);
451 }
452
453 static void
454 rfcomm_saccept(netmsg_t msg)
455 {
456         struct socket *so = msg->accept.base.nm_so;
457         struct sockaddr **nam = msg->accept.nm_nam;
458         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
459         struct sockaddr_bt *sa, ssa;
460         int error;
461
462         sa = &ssa;
463         bzero(sa, sizeof *sa);
464         sa->bt_len = sizeof(struct sockaddr_bt);
465         sa->bt_family = AF_BLUETOOTH;
466         error = rfcomm_peeraddr(pcb, sa);;
467         *nam = dup_sockaddr((struct sockaddr *)sa);
468
469         lwkt_replymsg(&msg->accept.base.lmsg, error);
470 }
471
472 static void
473 rfcomm_slisten(netmsg_t msg)
474 {
475         struct socket *so = msg->listen.base.nm_so;
476         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *)so->so_pcb;
477         int error;
478
479         error = rfcomm_listen(pcb);
480         lwkt_replymsg(&msg->listen.base.lmsg, error);
481 }
482
483 static void
484 rfcomm_srcvd(netmsg_t msg)
485 {
486         struct socket *so = msg->rcvd.base.nm_so;
487         struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb; 
488         int error;
489
490         error = rfcomm_rcvd(pcb, sbspace(&so->so_rcv));
491         lwkt_replymsg(&msg->rcvd.base.lmsg, error);
492 }
493
494 struct pr_usrreqs rfcomm_usrreqs = {
495         .pru_abort = rfcomm_sabort,
496         .pru_accept = rfcomm_saccept,
497         .pru_attach = rfcomm_sattach,
498         .pru_bind = rfcomm_sbind,
499         .pru_connect = rfcomm_sconnect,
500         .pru_connect2 = pr_generic_notsupp,
501         .pru_control = rfcomm_scontrol,
502         .pru_detach = rfcomm_sdetach,
503         .pru_disconnect = rfcomm_sdisconnect,
504         .pru_listen = rfcomm_slisten,
505         .pru_peeraddr = rfcomm_speeraddr,
506         .pru_rcvd = rfcomm_srcvd,
507         .pru_rcvoob = pr_generic_notsupp,
508         .pru_send = rfcomm_ssend,
509         .pru_sense = pru_sense_null,
510         .pru_shutdown = rfcomm_sshutdown,
511         .pru_sockaddr = rfcomm_ssockaddr,
512         .pru_sosend = sosend,
513         .pru_soreceive = soreceive
514 };