rfcomm_socket.c revision 1.7 1 /* $NetBSD: rfcomm_socket.c,v 1.7 2007/04/21 06:15:23 plunky Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Written by Iain Hibbert for Itronix Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
20 *
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.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: rfcomm_socket.c,v 1.7 2007/04/21 06:15:23 plunky Exp $");
36
37 /* load symbolic names */
38 #ifdef BLUETOOTH_DEBUG
39 #define PRUREQUESTS
40 #define PRCOREQUESTS
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/domain.h>
45 #include <sys/kernel.h>
46 #include <sys/mbuf.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/systm.h>
52
53 #include <netbt/bluetooth.h>
54 #include <netbt/rfcomm.h>
55
56 /****************************************************************************
57 *
58 * RFCOMM SOCK_STREAM Sockets - serial line emulation
59 *
60 */
61
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 *);
69
70 static const struct btproto rfcomm_proto = {
71 rfcomm_connecting,
72 rfcomm_connected,
73 rfcomm_disconnected,
74 rfcomm_newconn,
75 rfcomm_complete,
76 rfcomm_linkmode,
77 rfcomm_input,
78 };
79
80 /* sysctl variables */
81 int rfcomm_sendspace = 4096;
82 int rfcomm_recvspace = 4096;
83
84 /*
85 * User Request.
86 * up is socket
87 * m is either
88 * optional mbuf chain containing message
89 * ioctl command (PRU_CONTROL)
90 * nam is either
91 * optional mbuf chain containing an address
92 * ioctl data (PRU_CONTROL)
93 * optionally protocol number (PRU_ATTACH)
94 * message flags (PRU_RCVD)
95 * ctl is either
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)
99 *
100 * we are responsible for disposing of m and ctl if
101 * they are mbuf chains
102 */
103 int
104 rfcomm_usrreq(struct socket *up, int req, struct mbuf *m,
105 struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
106 {
107 struct rfcomm_dlc *pcb = up->so_pcb;
108 struct sockaddr_bt *sa;
109 struct mbuf *m0;
110 int err = 0;
111
112 DPRINTFN(2, "%s\n", prurequests[req]);
113
114 switch (req) {
115 case PRU_CONTROL:
116 return EPASSTHROUGH;
117
118 case PRU_PURGEIF:
119 return EOPNOTSUPP;
120
121 case PRU_ATTACH:
122 if (pcb != NULL)
123 return EINVAL;
124
125 /*
126 * Since we have nothing to add, we attach the DLC
127 * structure directly to our PCB pointer.
128 */
129 err = rfcomm_attach((struct rfcomm_dlc **)&up->so_pcb,
130 &rfcomm_proto, up);
131 if (err)
132 return err;
133
134 err = soreserve(up, rfcomm_sendspace, rfcomm_recvspace);
135 if (err)
136 return err;
137
138 err = rfcomm_rcvd(up->so_pcb, sbspace(&up->so_rcv));
139 if (err)
140 return err;
141
142 return 0;
143 }
144
145 if (pcb == NULL) {
146 err = EINVAL;
147 goto release;
148 }
149
150 switch(req) {
151 case PRU_DISCONNECT:
152 soisdisconnecting(up);
153 return rfcomm_disconnect(pcb, up->so_linger);
154
155 case PRU_ABORT:
156 rfcomm_disconnect(pcb, 0);
157 soisdisconnected(up);
158 /* fall through to */
159 case PRU_DETACH:
160 return rfcomm_detach((struct rfcomm_dlc **)&up->so_pcb);
161
162 case PRU_BIND:
163 KASSERT(nam != NULL);
164 sa = mtod(nam, struct sockaddr_bt *);
165
166 if (sa->bt_len != sizeof(struct sockaddr_bt))
167 return EINVAL;
168
169 if (sa->bt_family != AF_BLUETOOTH)
170 return EAFNOSUPPORT;
171
172 return rfcomm_bind(pcb, sa);
173
174 case PRU_CONNECT:
175 KASSERT(nam != NULL);
176 sa = mtod(nam, struct sockaddr_bt *);
177
178 if (sa->bt_len != sizeof(struct sockaddr_bt))
179 return EINVAL;
180
181 if (sa->bt_family != AF_BLUETOOTH)
182 return EAFNOSUPPORT;
183
184 soisconnecting(up);
185 return rfcomm_connect(pcb, sa);
186
187 case PRU_PEERADDR:
188 KASSERT(nam != NULL);
189 sa = mtod(nam, struct sockaddr_bt *);
190 nam->m_len = sizeof(struct sockaddr_bt);
191 return rfcomm_peeraddr(pcb, sa);
192
193 case PRU_SOCKADDR:
194 KASSERT(nam != NULL);
195 sa = mtod(nam, struct sockaddr_bt *);
196 nam->m_len = sizeof(struct sockaddr_bt);
197 return rfcomm_sockaddr(pcb, sa);
198
199 case PRU_SHUTDOWN:
200 socantsendmore(up);
201 break;
202
203 case PRU_SEND:
204 KASSERT(m != NULL);
205
206 if (ctl) /* no use for that */
207 m_freem(ctl);
208
209 m0 = m_copypacket(m, M_DONTWAIT);
210 if (m0 == NULL)
211 return ENOMEM;
212
213 sbappendstream(&up->so_snd, m);
214
215 return rfcomm_send(pcb, m0);
216
217 case PRU_SENSE:
218 return 0; /* (no release) */
219
220 case PRU_RCVD:
221 return rfcomm_rcvd(pcb, sbspace(&up->so_rcv));
222
223 case PRU_RCVOOB:
224 return EOPNOTSUPP; /* (no release) */
225
226 case PRU_LISTEN:
227 return rfcomm_listen(pcb);
228
229 case PRU_ACCEPT:
230 KASSERT(nam != NULL);
231 sa = mtod(nam, struct sockaddr_bt *);
232 nam->m_len = sizeof(struct sockaddr_bt);
233 return rfcomm_peeraddr(pcb, sa);
234
235 case PRU_CONNECT2:
236 case PRU_SENDOOB:
237 case PRU_FASTTIMO:
238 case PRU_SLOWTIMO:
239 case PRU_PROTORCV:
240 case PRU_PROTOSEND:
241 err = EOPNOTSUPP;
242 break;
243
244 default:
245 UNKNOWN(req);
246 err = EOPNOTSUPP;
247 break;
248 }
249
250 release:
251 if (m) m_freem(m);
252 if (ctl) m_freem(ctl);
253 return err;
254 }
255
256 /*
257 * rfcomm_ctloutput(request, socket, level, optname, opt)
258 *
259 */
260 int
261 rfcomm_ctloutput(int req, struct socket *so, int level,
262 int optname, struct mbuf **opt)
263 {
264 struct rfcomm_dlc *pcb = so->so_pcb;
265 struct mbuf *m;
266 int err = 0;
267
268 DPRINTFN(2, "%s\n", prcorequests[req]);
269
270 if (pcb == NULL)
271 return EINVAL;
272
273 if (level != BTPROTO_RFCOMM)
274 return ENOPROTOOPT;
275
276 switch(req) {
277 case PRCO_GETOPT:
278 m = m_get(M_WAIT, MT_SOOPTS);
279 m->m_len = rfcomm_getopt(pcb, optname, mtod(m, void *));
280 if (m->m_len == 0) {
281 m_freem(m);
282 m = NULL;
283 err = ENOPROTOOPT;
284 }
285 *opt = m;
286 break;
287
288 case PRCO_SETOPT:
289 m = *opt;
290 KASSERT(m != NULL);
291 err = rfcomm_setopt(pcb, optname, mtod(m, void *));
292 m_freem(m);
293 break;
294
295 default:
296 err = ENOPROTOOPT;
297 break;
298 }
299
300 return err;
301 }
302
303 /**********************************************************************
304 *
305 * RFCOMM callbacks
306 */
307
308 static void
309 rfcomm_connecting(void *arg)
310 {
311 /* struct socket *so = arg; */
312
313 KASSERT(arg != NULL);
314 DPRINTF("Connecting\n");
315 }
316
317 static void
318 rfcomm_connected(void *arg)
319 {
320 struct socket *so = arg;
321
322 KASSERT(so != NULL);
323 DPRINTF("Connected\n");
324 soisconnected(so);
325 }
326
327 static void
328 rfcomm_disconnected(void *arg, int err)
329 {
330 struct socket *so = arg;
331
332 KASSERT(so != NULL);
333 DPRINTF("Disconnected\n");
334
335 so->so_error = err;
336 soisdisconnected(so);
337 }
338
339 static void *
340 rfcomm_newconn(void *arg, struct sockaddr_bt *laddr,
341 struct sockaddr_bt *raddr)
342 {
343 struct socket *so = arg;
344
345 DPRINTF("New Connection\n");
346 so = sonewconn(so, 0);
347 if (so == NULL)
348 return NULL;
349
350 soisconnecting(so);
351
352 return so->so_pcb;
353 }
354
355 /*
356 * rfcomm_complete(rfcomm_dlc, length)
357 *
358 * length bytes are sent and may be removed from socket buffer
359 */
360 static void
361 rfcomm_complete(void *arg, int length)
362 {
363 struct socket *so = arg;
364
365 sbdrop(&so->so_snd, length);
366 sowwakeup(so);
367 }
368
369 /*
370 * rfcomm_linkmode(rfcomm_dlc, new)
371 *
372 * link mode change notification.
373 */
374 static void
375 rfcomm_linkmode(void *arg, int new)
376 {
377 struct socket *so = arg;
378 int mode;
379
380 DPRINTF("auth %s, encrypt %s, secure %s\n",
381 (new & RFCOMM_LM_AUTH ? "on" : "off"),
382 (new & RFCOMM_LM_ENCRYPT ? "on" : "off"),
383 (new & RFCOMM_LM_SECURE ? "on" : "off"));
384
385 (void)rfcomm_getopt(so->so_pcb, SO_RFCOMM_LM, &mode);
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);
390 }
391
392 /*
393 * rfcomm_input(rfcomm_dlc, mbuf)
394 */
395 static void
396 rfcomm_input(void *arg, struct mbuf *m)
397 {
398 struct socket *so = arg;
399
400 KASSERT(so != NULL);
401
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);
405 m_freem(m);
406 return;
407 }
408
409 DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
410
411 sbappendstream(&so->so_rcv, m);
412 sorwakeup(so);
413 }
414