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