Remove some duplicated semicolons (mostly in the kernel).
[dragonfly.git] / sys / netbt / rfcomm_socket.c
CommitLineData
83aacede
HT
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 $ */
0a9108eb
HT
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
0a9108eb
HT
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>
002c1265
MD
50
51#include <sys/msgport2.h>
52
0a9108eb
HT
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
65static void rfcomm_connecting(void *);
66static void rfcomm_connected(void *);
67static void rfcomm_disconnected(void *, int);
68static void *rfcomm_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
69static void rfcomm_complete(void *, int);
70static void rfcomm_linkmode(void *, int);
71static void rfcomm_input(void *, struct mbuf *);
72
73static 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 */
84int rfcomm_sendspace = 4096;
85int rfcomm_recvspace = 4096;
86
87/*
88 * rfcomm_ctloutput(request, socket, level, optname, opt)
89 *
90 */
002c1265
MD
91void
92rfcomm_ctloutput(netmsg_t msg)
0a9108eb 93{
002c1265
MD
94 struct socket *so = msg->ctloutput.base.nm_so;
95 struct sockopt *sopt = msg->ctloutput.nm_sopt;
0a9108eb
HT
96 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
97 struct mbuf *m;
002c1265 98 int error = 0;
0a9108eb
HT
99
100#ifdef notyet /* XXX */
101 DPRINTFN(2, "%s\n", prcorequests[sopt->sopt_dir]);
102#endif
103
002c1265
MD
104 if (pcb == NULL) {
105 error = EINVAL;
106 goto out;
107 }
0a9108eb 108
002c1265
MD
109 if (sopt->sopt_level != BTPROTO_RFCOMM) {
110 error = ENOPROTOOPT;
111 goto out;
112 }
0a9108eb
HT
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;
002c1265 123 error = ENOPROTOOPT;
0a9108eb 124 }
d82b71f8 125 soopt_from_kbuf(sopt, mtod(m, void *), m->m_len);
0a9108eb
HT
126 break;
127
128 case PRCO_SETOPT:
002c1265 129 error = rfcomm_setopt2(pcb, sopt->sopt_name, so, sopt);
0a9108eb
HT
130
131 break;
132
133 default:
002c1265 134 error = ENOPROTOOPT;
0a9108eb
HT
135 break;
136 }
002c1265
MD
137out:
138 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
0a9108eb
HT
139}
140
141/**********************************************************************
142 *
143 * RFCOMM callbacks
144 */
145
146static void
147rfcomm_connecting(void *arg)
148{
149 /* struct socket *so = arg; */
150
151 KKASSERT(arg != NULL);
152 DPRINTF("Connecting\n");
153}
154
155static void
156rfcomm_connected(void *arg)
157{
158 struct socket *so = arg;
159
160 KKASSERT(so != NULL);
161 DPRINTF("Connected\n");
162 soisconnected(so);
163}
164
165static void
166rfcomm_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
177static void *
178rfcomm_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 */
198static void
199rfcomm_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 */
212static void
213rfcomm_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 */
233static void
234rfcomm_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 */
002c1265
MD
256static void
257rfcomm_sdetach(netmsg_t msg)
0a9108eb 258{
002c1265
MD
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);
0a9108eb
HT
264}
265
6cef7136
MD
266/*
267 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
268 * will sofree() it when we return.
269 */
002c1265
MD
270static void
271rfcomm_sabort(netmsg_t msg)
0a9108eb 272{
002c1265 273 struct socket *so = msg->abort.base.nm_so;
0a9108eb
HT
274 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
275
276 rfcomm_disconnect(pcb, 0);
277 soisdisconnected(so);
002c1265
MD
278 rfcomm_sdetach(msg);
279 /* msg invalid now */
0a9108eb
HT
280}
281
002c1265
MD
282static void
283rfcomm_sdisconnect(netmsg_t msg)
0a9108eb 284{
002c1265 285 struct socket *so = msg->abort.base.nm_so;
0a9108eb 286 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
002c1265 287 int error;
0a9108eb
HT
288
289 soisdisconnecting(so);
002c1265
MD
290 error = rfcomm_disconnect(pcb, so->so_linger);
291 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
0a9108eb
HT
292}
293
002c1265
MD
294static void
295rfcomm_scontrol(netmsg_t msg)
0a9108eb 296{
002c1265 297 lwkt_replymsg(&msg->control.base.lmsg, EPASSTHROUGH);
0a9108eb
HT
298}
299
002c1265
MD
300static void
301rfcomm_sattach(netmsg_t msg)
0a9108eb 302{
002c1265 303 struct socket *so = msg->attach.base.nm_so;
0a9108eb 304 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
002c1265 305 int error;
0a9108eb 306
002c1265
MD
307 if (pcb != NULL) {
308 error = EINVAL;
309 goto out;
310 }
0a9108eb
HT
311
312 /*
313 * Since we have nothing to add, we attach the DLC
314 * structure directly to our PCB pointer.
315 */
002c1265
MD
316 error = soreserve(so, rfcomm_sendspace, rfcomm_recvspace, NULL);
317 if (error)
318 goto out;
0a9108eb 319
002c1265
MD
320 error = rfcomm_attach((struct rfcomm_dlc **)&so->so_pcb,
321 &rfcomm_proto, so);
322 if (error)
323 goto out;
0a9108eb 324
002c1265
MD
325 error = rfcomm_rcvd(so->so_pcb, sbspace(&so->so_rcv));
326 if (error)
83aacede 327 rfcomm_detach((struct rfcomm_dlc **)&so->so_pcb);
002c1265
MD
328out:
329 lwkt_replymsg(&msg->attach.base.lmsg, error);
0a9108eb
HT
330}
331
002c1265
MD
332static void
333rfcomm_sbind(netmsg_t msg)
0a9108eb 334{
002c1265
MD
335 struct socket *so = msg->bind.base.nm_so;
336 struct sockaddr *nam = msg->bind.nm_nam;
0a9108eb
HT
337 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
338 struct sockaddr_bt *sa;
002c1265 339 int error;
0a9108eb
HT
340
341 KKASSERT(nam != NULL);
342 sa = (struct sockaddr_bt *)nam;
343
002c1265
MD
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);
0a9108eb
HT
352}
353
002c1265
MD
354static void
355rfcomm_sconnect(netmsg_t msg)
0a9108eb 356{
002c1265
MD
357 struct socket *so = msg->connect.base.nm_so;
358 struct sockaddr *nam = msg->connect.nm_nam;
0a9108eb
HT
359 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
360 struct sockaddr_bt *sa;
002c1265 361 int error;
0a9108eb
HT
362
363 KKASSERT(nam != NULL);
364 sa = (struct sockaddr_bt *)nam;
365
002c1265
MD
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);
0a9108eb
HT
375}
376
002c1265
MD
377static void
378rfcomm_speeraddr(netmsg_t msg)
0a9108eb 379{
002c1265
MD
380 struct socket *so = msg->peeraddr.base.nm_so;
381 struct sockaddr **nam = msg->peeraddr.nm_nam;
0a9108eb
HT
382 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
383 struct sockaddr_bt *sa, ssa;
002c1265 384 int error;
0a9108eb
HT
385
386 sa = &ssa;
387 bzero(sa, sizeof *sa);
388 sa->bt_len = sizeof(struct sockaddr_bt);
389 sa->bt_family = AF_BLUETOOTH;
3598cc14 390 error = rfcomm_peeraddr(pcb, sa);
0a9108eb 391 *nam = dup_sockaddr((struct sockaddr *)sa);
002c1265
MD
392
393 lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
0a9108eb
HT
394}
395
002c1265
MD
396static void
397rfcomm_ssockaddr(netmsg_t msg)
0a9108eb 398{
002c1265
MD
399 struct socket *so = msg->sockaddr.base.nm_so;
400 struct sockaddr **nam = msg->sockaddr.nm_nam;
0a9108eb
HT
401 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
402 struct sockaddr_bt *sa, ssa;
002c1265 403 int error;
0a9108eb
HT
404
405 sa = &ssa;
406 bzero(sa, sizeof *sa);
407 sa->bt_len = sizeof(struct sockaddr_bt);
408 sa->bt_family = AF_BLUETOOTH;
3598cc14 409 error = rfcomm_sockaddr(pcb, sa);
0a9108eb 410 *nam = dup_sockaddr((struct sockaddr *)sa);
002c1265
MD
411
412 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
0a9108eb
HT
413}
414
002c1265
MD
415static void
416rfcomm_sshutdown(netmsg_t msg)
0a9108eb 417{
002c1265
MD
418 struct socket *so = msg->shutdown.base.nm_so;
419
0a9108eb 420 socantsendmore(so);
002c1265 421 lwkt_replymsg(&msg->shutdown.base.lmsg, 0);
0a9108eb
HT
422}
423
002c1265
MD
424static void
425rfcomm_ssend(netmsg_t msg)
0a9108eb 426{
002c1265
MD
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;
0a9108eb
HT
430 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
431 struct mbuf *m0;
002c1265 432 int error;
0a9108eb
HT
433
434 KKASSERT(m != NULL);
435
002c1265
MD
436 /* no use for that */
437 if (control) {
0a9108eb 438 m_freem(control);
002c1265
MD
439 control = NULL;
440 }
0a9108eb
HT
441
442 m0 = m_copym(m, 0, M_COPYALL, MB_DONTWAIT);
002c1265
MD
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);
0a9108eb
HT
450}
451
002c1265
MD
452static void
453rfcomm_saccept(netmsg_t msg)
0a9108eb 454{
002c1265
MD
455 struct socket *so = msg->accept.base.nm_so;
456 struct sockaddr **nam = msg->accept.nm_nam;
0a9108eb
HT
457 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
458 struct sockaddr_bt *sa, ssa;
002c1265 459 int error;
0a9108eb
HT
460
461 sa = &ssa;
462 bzero(sa, sizeof *sa);
463 sa->bt_len = sizeof(struct sockaddr_bt);
464 sa->bt_family = AF_BLUETOOTH;
3598cc14 465 error = rfcomm_peeraddr(pcb, sa);
0a9108eb 466 *nam = dup_sockaddr((struct sockaddr *)sa);
002c1265
MD
467
468 lwkt_replymsg(&msg->accept.base.lmsg, error);
0a9108eb
HT
469}
470
002c1265
MD
471static void
472rfcomm_slisten(netmsg_t msg)
0a9108eb 473{
002c1265
MD
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);
0a9108eb
HT
480}
481
002c1265
MD
482static void
483rfcomm_srcvd(netmsg_t msg)
0a9108eb 484{
002c1265 485 struct socket *so = msg->rcvd.base.nm_so;
0a9108eb 486 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
002c1265
MD
487 int error;
488
489 error = rfcomm_rcvd(pcb, sbspace(&so->so_rcv));
490 lwkt_replymsg(&msg->rcvd.base.lmsg, error);
0a9108eb
HT
491}
492
493struct 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,
002c1265 499 .pru_connect2 = pr_generic_notsupp,
0a9108eb
HT
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,
002c1265 506 .pru_rcvoob = pr_generic_notsupp,
0a9108eb
HT
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,
8b5c39bb 512 .pru_soreceive = soreceive
0a9108eb 513};