Unbreak VKERNEL64 for clang compatibility.
[dragonfly.git] / sys / netbt / rfcomm_socket.c
blob9261284b5563e9aa8380ea4ad63d789b566e8c9d
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 $ */
4 /*-
5 * Copyright (c) 2006 Itronix Inc.
6 * All rights reserved.
8 * Written by Iain Hibbert for Itronix Inc.
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.
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.
35 /* load symbolic names */
36 #ifdef BLUETOOTH_DEBUG
37 #define PRUREQUESTS
38 #define PRCOREQUESTS
39 #endif
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>
51 #include <sys/msgport2.h>
53 #include <vm/vm_zone.h>
55 #include <netbt/bluetooth.h>
56 #include <netbt/hci.h> /* XXX for EPASSTHROUGH */
57 #include <netbt/rfcomm.h>
59 /****************************************************************************
61 * RFCOMM SOCK_STREAM Sockets - serial line emulation
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 *);
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,
83 /* sysctl variables */
84 int rfcomm_sendspace = 4096;
85 int rfcomm_recvspace = 4096;
88 * rfcomm_ctloutput(request, socket, level, optname, opt)
91 void
92 rfcomm_ctloutput(netmsg_t msg)
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;
100 #ifdef notyet /* XXX */
101 DPRINTFN(2, "%s\n", prcorequests[sopt->sopt_dir]);
102 #endif
104 if (pcb == NULL) {
105 error = EINVAL;
106 goto out;
109 if (sopt->sopt_level != BTPROTO_RFCOMM) {
110 error = ENOPROTOOPT;
111 goto out;
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;
125 soopt_from_kbuf(sopt, mtod(m, void *), m->m_len);
126 break;
128 case PRCO_SETOPT:
129 error = rfcomm_setopt2(pcb, sopt->sopt_name, so, sopt);
131 break;
133 default:
134 error = ENOPROTOOPT;
135 break;
137 out:
138 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
141 /**********************************************************************
143 * RFCOMM callbacks
146 static void
147 rfcomm_connecting(void *arg)
149 /* struct socket *so = arg; */
151 KKASSERT(arg != NULL);
152 DPRINTF("Connecting\n");
155 static void
156 rfcomm_connected(void *arg)
158 struct socket *so = arg;
160 KKASSERT(so != NULL);
161 DPRINTF("Connected\n");
162 soisconnected(so);
165 static void
166 rfcomm_disconnected(void *arg, int err)
168 struct socket *so = arg;
170 KKASSERT(so != NULL);
171 DPRINTF("Disconnected\n");
173 so->so_error = err;
174 soisdisconnected(so);
177 static void *
178 rfcomm_newconn(void *arg, struct sockaddr_bt *laddr,
179 struct sockaddr_bt *raddr)
181 struct socket *so = arg;
183 DPRINTF("New Connection\n");
184 so = sonewconn(so, 0);
185 if (so == NULL)
186 return NULL;
188 soisconnecting(so);
190 return so->so_pcb;
194 * rfcomm_complete(rfcomm_dlc, length)
196 * length bytes are sent and may be removed from socket buffer
198 static void
199 rfcomm_complete(void *arg, int length)
201 struct socket *so = arg;
203 sbdrop(&so->so_snd.sb, length);
204 sowwakeup(so);
208 * rfcomm_linkmode(rfcomm_dlc, new)
210 * link mode change notification.
212 static void
213 rfcomm_linkmode(void *arg, int new)
215 struct socket *so = arg;
216 int mode;
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"));
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);
231 * rfcomm_input(rfcomm_dlc, mbuf)
233 static void
234 rfcomm_input(void *arg, struct mbuf *m)
236 struct socket *so = arg;
238 KKASSERT(so != NULL);
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;
247 DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
249 sbappendstream(&so->so_rcv.sb, m);
250 sorwakeup(so);
254 * Implementation of usrreqs.
256 static void
257 rfcomm_sdetach(netmsg_t msg)
259 struct socket *so = msg->detach.base.nm_so;
260 int error;
262 error = rfcomm_detach((struct rfcomm_dlc **)&so->so_pcb);
263 lwkt_replymsg(&msg->detach.base.lmsg, error);
267 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
268 * will sofree() it when we return.
270 static void
271 rfcomm_sabort(netmsg_t msg)
273 struct socket *so = msg->abort.base.nm_so;
274 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
276 rfcomm_disconnect(pcb, 0);
277 soisdisconnected(so);
278 rfcomm_sdetach(msg);
279 /* msg invalid now */
282 static void
283 rfcomm_sdisconnect(netmsg_t msg)
285 struct socket *so = msg->abort.base.nm_so;
286 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
287 int error;
289 soisdisconnecting(so);
290 error = rfcomm_disconnect(pcb, so->so_linger);
291 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
294 static void
295 rfcomm_scontrol(netmsg_t msg)
297 lwkt_replymsg(&msg->control.base.lmsg, EPASSTHROUGH);
300 static void
301 rfcomm_sattach(netmsg_t msg)
303 struct socket *so = msg->attach.base.nm_so;
304 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
305 int error;
307 if (pcb != NULL) {
308 error = EINVAL;
309 goto out;
313 * Since we have nothing to add, we attach the DLC
314 * structure directly to our PCB pointer.
316 error = soreserve(so, rfcomm_sendspace, rfcomm_recvspace, NULL);
317 if (error)
318 goto out;
320 error = rfcomm_attach((struct rfcomm_dlc **)&so->so_pcb,
321 &rfcomm_proto, so);
322 if (error)
323 goto out;
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);
332 static void
333 rfcomm_sbind(netmsg_t msg)
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;
341 KKASSERT(nam != NULL);
342 sa = (struct sockaddr_bt *)nam;
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);
351 lwkt_replymsg(&msg->bind.base.lmsg, error);
354 static void
355 rfcomm_sconnect(netmsg_t msg)
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;
363 KKASSERT(nam != NULL);
364 sa = (struct sockaddr_bt *)nam;
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);
374 lwkt_replymsg(&msg->connect.base.lmsg, error);
377 static void
378 rfcomm_speeraddr(netmsg_t msg)
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;
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);
393 lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
396 static void
397 rfcomm_ssockaddr(netmsg_t msg)
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;
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);
412 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
415 static void
416 rfcomm_sshutdown(netmsg_t msg)
418 struct socket *so = msg->shutdown.base.nm_so;
420 socantsendmore(so);
421 lwkt_replymsg(&msg->shutdown.base.lmsg, 0);
424 static void
425 rfcomm_ssend(netmsg_t msg)
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;
434 KKASSERT(m != NULL);
436 /* no use for that */
437 if (control) {
438 m_freem(control);
439 control = NULL;
442 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
443 if (m0) {
444 sbappendstream(&so->so_snd.sb, m);
445 error = rfcomm_send(pcb, m0);
446 } else {
447 error = ENOMEM;
449 lwkt_replymsg(&msg->send.base.lmsg, error);
452 static void
453 rfcomm_saccept(netmsg_t msg)
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;
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);
468 lwkt_replymsg(&msg->accept.base.lmsg, error);
471 static void
472 rfcomm_slisten(netmsg_t msg)
474 struct socket *so = msg->listen.base.nm_so;
475 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *)so->so_pcb;
476 int error;
478 error = rfcomm_listen(pcb);
479 lwkt_replymsg(&msg->listen.base.lmsg, error);
482 static void
483 rfcomm_srcvd(netmsg_t msg)
485 struct socket *so = msg->rcvd.base.nm_so;
486 struct rfcomm_dlc *pcb = (struct rfcomm_dlc *) so->so_pcb;
487 int error;
489 error = rfcomm_rcvd(pcb, sbspace(&so->so_rcv));
490 lwkt_replymsg(&msg->rcvd.base.lmsg, error);
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