1 /* $NetBSD: rfcomm_socket.c,v 1.9 2008/04/24 11:38:37 ad Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
7 * Written by Iain Hibbert for Itronix Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: rfcomm_socket.c,v 1.9 2008/04/24 11:38:37 ad Exp $");
37 /* load symbolic names */
38 #ifdef BLUETOOTH_DEBUG
43 #include <sys/param.h>
44 #include <sys/domain.h>
45 #include <sys/kernel.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/systm.h>
53 #include <netbt/bluetooth.h>
54 #include <netbt/rfcomm.h>
56 /****************************************************************************
58 * RFCOMM SOCK_STREAM Sockets - serial line emulation
62 static void rfcomm_connecting(void *);
63 static void rfcomm_connected(void *);
64 static void rfcomm_disconnected(void *, int);
65 static void *rfcomm_newconn(void *, struct sockaddr_bt
*, struct sockaddr_bt
*);
66 static void rfcomm_complete(void *, int);
67 static void rfcomm_linkmode(void *, int);
68 static void rfcomm_input(void *, struct mbuf
*);
70 static const struct btproto rfcomm_proto
= {
80 /* sysctl variables */
81 int rfcomm_sendspace
= 4096;
82 int rfcomm_recvspace
= 4096;
88 * optional mbuf chain containing message
89 * ioctl command (PRU_CONTROL)
91 * optional mbuf chain containing an address
92 * ioctl data (PRU_CONTROL)
93 * optionally protocol number (PRU_ATTACH)
94 * message flags (PRU_RCVD)
96 * optional mbuf chain containing socket options
97 * optional interface pointer (PRU_CONTROL, PRU_PURGEIF)
98 * l is pointer to process requesting action (if any)
100 * we are responsible for disposing of m and ctl if
101 * they are mbuf chains
104 rfcomm_usrreq(struct socket
*up
, int req
, struct mbuf
*m
,
105 struct mbuf
*nam
, struct mbuf
*ctl
, struct lwp
*l
)
107 struct rfcomm_dlc
*pcb
= up
->so_pcb
;
108 struct sockaddr_bt
*sa
;
112 DPRINTFN(2, "%s\n", prurequests
[req
]);
122 if (up
->so_lock
== NULL
) {
123 mutex_obj_hold(bt_lock
);
124 up
->so_lock
= bt_lock
;
127 KASSERT(solocked(up
));
131 * Since we have nothing to add, we attach the DLC
132 * structure directly to our PCB pointer.
134 err
= soreserve(up
, rfcomm_sendspace
, rfcomm_recvspace
);
138 err
= rfcomm_attach((struct rfcomm_dlc
**)&up
->so_pcb
,
143 err
= rfcomm_rcvd(up
->so_pcb
, sbspace(&up
->so_rcv
));
145 rfcomm_detach((struct rfcomm_dlc
**)&up
->so_pcb
);
159 soisdisconnecting(up
);
160 return rfcomm_disconnect(pcb
, up
->so_linger
);
163 rfcomm_disconnect(pcb
, 0);
164 soisdisconnected(up
);
165 /* fall through to */
167 return rfcomm_detach((struct rfcomm_dlc
**)&up
->so_pcb
);
170 KASSERT(nam
!= NULL
);
171 sa
= mtod(nam
, struct sockaddr_bt
*);
173 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
))
176 if (sa
->bt_family
!= AF_BLUETOOTH
)
179 return rfcomm_bind(pcb
, sa
);
182 KASSERT(nam
!= NULL
);
183 sa
= mtod(nam
, struct sockaddr_bt
*);
185 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
))
188 if (sa
->bt_family
!= AF_BLUETOOTH
)
192 return rfcomm_connect(pcb
, sa
);
195 KASSERT(nam
!= NULL
);
196 sa
= mtod(nam
, struct sockaddr_bt
*);
197 nam
->m_len
= sizeof(struct sockaddr_bt
);
198 return rfcomm_peeraddr(pcb
, sa
);
201 KASSERT(nam
!= NULL
);
202 sa
= mtod(nam
, struct sockaddr_bt
*);
203 nam
->m_len
= sizeof(struct sockaddr_bt
);
204 return rfcomm_sockaddr(pcb
, sa
);
213 if (ctl
) /* no use for that */
216 m0
= m_copypacket(m
, M_DONTWAIT
);
220 sbappendstream(&up
->so_snd
, m
);
222 return rfcomm_send(pcb
, m0
);
225 return 0; /* (no release) */
228 return rfcomm_rcvd(pcb
, sbspace(&up
->so_rcv
));
231 return EOPNOTSUPP
; /* (no release) */
234 return rfcomm_listen(pcb
);
237 KASSERT(nam
!= NULL
);
238 sa
= mtod(nam
, struct sockaddr_bt
*);
239 nam
->m_len
= sizeof(struct sockaddr_bt
);
240 return rfcomm_peeraddr(pcb
, sa
);
259 if (ctl
) m_freem(ctl
);
264 * rfcomm_ctloutput(req, socket, sockopt)
268 rfcomm_ctloutput(int req
, struct socket
*so
, struct sockopt
*sopt
)
270 struct rfcomm_dlc
*pcb
= so
->so_pcb
;
273 DPRINTFN(2, "%s\n", prcorequests
[req
]);
278 if (sopt
->sopt_level
!= BTPROTO_RFCOMM
)
283 err
= rfcomm_getopt(pcb
, sopt
);
287 err
= rfcomm_setopt(pcb
, sopt
);
298 /**********************************************************************
304 rfcomm_connecting(void *arg
)
306 /* struct socket *so = arg; */
308 KASSERT(arg
!= NULL
);
309 DPRINTF("Connecting\n");
313 rfcomm_connected(void *arg
)
315 struct socket
*so
= arg
;
318 DPRINTF("Connected\n");
323 rfcomm_disconnected(void *arg
, int err
)
325 struct socket
*so
= arg
;
328 DPRINTF("Disconnected\n");
331 soisdisconnected(so
);
335 rfcomm_newconn(void *arg
, struct sockaddr_bt
*laddr
,
336 struct sockaddr_bt
*raddr
)
338 struct socket
*so
= arg
;
340 DPRINTF("New Connection\n");
341 so
= sonewconn(so
, 0);
351 * rfcomm_complete(rfcomm_dlc, length)
353 * length bytes are sent and may be removed from socket buffer
356 rfcomm_complete(void *arg
, int length
)
358 struct socket
*so
= arg
;
360 sbdrop(&so
->so_snd
, length
);
365 * rfcomm_linkmode(rfcomm_dlc, new)
367 * link mode change notification.
370 rfcomm_linkmode(void *arg
, int new)
372 struct socket
*so
= arg
;
376 DPRINTF("auth %s, encrypt %s, secure %s\n",
377 (new & RFCOMM_LM_AUTH
? "on" : "off"),
378 (new & RFCOMM_LM_ENCRYPT
? "on" : "off"),
379 (new & RFCOMM_LM_SECURE
? "on" : "off"));
381 sockopt_init(&sopt
, BTPROTO_RFCOMM
, SO_RFCOMM_LM
, 0);
382 (void)rfcomm_getopt(so
->so_pcb
, &sopt
);
383 (void)sockopt_getint(&sopt
, &mode
);
384 sockopt_destroy(&sopt
);
386 if (((mode
& RFCOMM_LM_AUTH
) && !(new & RFCOMM_LM_AUTH
))
387 || ((mode
& RFCOMM_LM_ENCRYPT
) && !(new & RFCOMM_LM_ENCRYPT
))
388 || ((mode
& RFCOMM_LM_SECURE
) && !(new & RFCOMM_LM_SECURE
)))
389 rfcomm_disconnect(so
->so_pcb
, 0);
393 * rfcomm_input(rfcomm_dlc, mbuf)
396 rfcomm_input(void *arg
, struct mbuf
*m
)
398 struct socket
*so
= arg
;
402 if (m
->m_pkthdr
.len
> sbspace(&so
->so_rcv
)) {
403 printf("%s: %d bytes dropped (socket buffer full)\n",
404 __func__
, m
->m_pkthdr
.len
);
409 DPRINTFN(10, "received %d bytes\n", m
->m_pkthdr
.len
);
411 sbappendstream(&so
->so_rcv
, m
);