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 $ */
5 * Copyright (c) 2006 Itronix Inc.
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
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
41 #include <sys/param.h>
42 #include <sys/domain.h>
43 #include <sys/kernel.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
= {
83 /* sysctl variables */
84 int rfcomm_sendspace
= 4096;
85 int rfcomm_recvspace
= 4096;
88 * rfcomm_ctloutput(request, socket, level, optname, opt)
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
;
100 #ifdef notyet /* XXX */
101 DPRINTFN(2, "%s\n", prcorequests
[sopt
->sopt_dir
]);
109 if (sopt
->sopt_level
!= BTPROTO_RFCOMM
) {
114 switch(sopt
->sopt_dir
) {
116 m
= m_get(M_WAITOK
, MT_DATA
);
118 m
->m_len
= rfcomm_getopt(pcb
, sopt
->sopt_name
, mtod(m
, void *));
125 soopt_from_kbuf(sopt
, mtod(m
, void *), m
->m_len
);
129 error
= rfcomm_setopt2(pcb
, sopt
->sopt_name
, so
, sopt
);
138 lwkt_replymsg(&msg
->ctloutput
.base
.lmsg
, error
);
141 /**********************************************************************
147 rfcomm_connecting(void *arg
)
149 /* struct socket *so = arg; */
151 KKASSERT(arg
!= NULL
);
152 DPRINTF("Connecting\n");
156 rfcomm_connected(void *arg
)
158 struct socket
*so
= arg
;
160 KKASSERT(so
!= NULL
);
161 DPRINTF("Connected\n");
166 rfcomm_disconnected(void *arg
, int err
)
168 struct socket
*so
= arg
;
170 KKASSERT(so
!= NULL
);
171 DPRINTF("Disconnected\n");
174 soisdisconnected(so
);
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);
194 * rfcomm_complete(rfcomm_dlc, length)
196 * length bytes are sent and may be removed from socket buffer
199 rfcomm_complete(void *arg
, int length
)
201 struct socket
*so
= arg
;
203 sbdrop(&so
->so_snd
.sb
, length
);
208 * rfcomm_linkmode(rfcomm_dlc, new)
210 * link mode change notification.
213 rfcomm_linkmode(void *arg
, int new)
215 struct socket
*so
= arg
;
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)
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
);
247 DPRINTFN(10, "received %d bytes\n", m
->m_pkthdr
.len
);
249 sbappendstream(&so
->so_rcv
.sb
, m
);
254 * Implementation of usrreqs.
257 rfcomm_sdetach(netmsg_t msg
)
259 struct socket
*so
= msg
->detach
.base
.nm_so
;
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.
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
);
279 /* msg invalid now */
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
;
289 soisdisconnecting(so
);
290 error
= rfcomm_disconnect(pcb
, so
->so_linger
);
291 lwkt_replymsg(&msg
->disconnect
.base
.lmsg
, error
);
295 rfcomm_scontrol(netmsg_t msg
)
297 lwkt_replymsg(&msg
->control
.base
.lmsg
, EPASSTHROUGH
);
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
;
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
);
320 error
= rfcomm_attach((struct rfcomm_dlc
**)&so
->so_pcb
,
325 error
= rfcomm_rcvd(so
->so_pcb
, sbspace(&so
->so_rcv
));
327 rfcomm_detach((struct rfcomm_dlc
**)&so
->so_pcb
);
329 lwkt_replymsg(&msg
->attach
.base
.lmsg
, error
);
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
;
341 KKASSERT(nam
!= NULL
);
342 sa
= (struct sockaddr_bt
*)nam
;
344 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
)) {
346 } else if (sa
->bt_family
!= AF_BLUETOOTH
) {
347 error
= EAFNOSUPPORT
;
349 error
= rfcomm_bind(pcb
, sa
);
351 lwkt_replymsg(&msg
->bind
.base
.lmsg
, error
);
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
;
363 KKASSERT(nam
!= NULL
);
364 sa
= (struct sockaddr_bt
*)nam
;
366 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
)) {
368 } else if (sa
->bt_family
!= AF_BLUETOOTH
) {
369 error
= EAFNOSUPPORT
;
372 error
= rfcomm_connect(pcb
, sa
);
374 lwkt_replymsg(&msg
->connect
.base
.lmsg
, error
);
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
;
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
);
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
;
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
);
416 rfcomm_sshutdown(netmsg_t msg
)
418 struct socket
*so
= msg
->shutdown
.base
.nm_so
;
421 lwkt_replymsg(&msg
->shutdown
.base
.lmsg
, 0);
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
;
436 /* no use for that */
442 m0
= m_copym(m
, 0, M_COPYALL
, M_NOWAIT
);
444 sbappendstream(&so
->so_snd
.sb
, m
);
445 error
= rfcomm_send(pcb
, m0
);
449 lwkt_replymsg(&msg
->send
.base
.lmsg
, error
);
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
;
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
);
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
;
478 error
= rfcomm_listen(pcb
);
479 lwkt_replymsg(&msg
->listen
.base
.lmsg
, error
);
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
;
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