rfcomm_socket.c revision 1.28 1 /* $NetBSD: rfcomm_socket.c,v 1.28 2014/07/31 03:39:35 rtr 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.28 2014/07/31 03:39:35 rtr 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 static int
85 rfcomm_attach(struct socket *so, int proto)
86 {
87 int error;
88
89 KASSERT(so->so_pcb == NULL);
90
91 if (so->so_lock == NULL) {
92 mutex_obj_hold(bt_lock);
93 so->so_lock = bt_lock;
94 solock(so);
95 }
96 KASSERT(solocked(so));
97
98 /*
99 * Since we have nothing to add, we attach the DLC
100 * structure directly to our PCB pointer.
101 */
102 error = soreserve(so, rfcomm_sendspace, rfcomm_recvspace);
103 if (error)
104 return error;
105
106 error = rfcomm_attach_pcb((struct rfcomm_dlc **)&so->so_pcb,
107 &rfcomm_proto, so);
108 if (error)
109 return error;
110
111 error = rfcomm_rcvd(so->so_pcb, sbspace(&so->so_rcv));
112 if (error) {
113 rfcomm_detach_pcb((struct rfcomm_dlc **)&so->so_pcb);
114 return error;
115 }
116 return 0;
117 }
118
119 static void
120 rfcomm_detach(struct socket *so)
121 {
122 KASSERT(so->so_pcb != NULL);
123 rfcomm_detach_pcb((struct rfcomm_dlc **)&so->so_pcb);
124 KASSERT(so->so_pcb == NULL);
125 }
126
127 static int
128 rfcomm_accept(struct socket *so, struct mbuf *nam)
129 {
130 struct rfcomm_dlc *pcb = so->so_pcb;
131 struct sockaddr_bt *sa;
132
133 KASSERT(solocked(so));
134 KASSERT(nam != NULL);
135
136 if (pcb == NULL)
137 return EINVAL;
138
139 sa = mtod(nam, struct sockaddr_bt *);
140 nam->m_len = sizeof(struct sockaddr_bt);
141 return rfcomm_peeraddr_pcb(pcb, sa);
142 }
143
144 static int
145 rfcomm_bind(struct socket *so, struct mbuf *nam)
146 {
147 struct rfcomm_dlc *pcb = so->so_pcb;
148 struct sockaddr_bt *sa;
149
150 KASSERT(solocked(so));
151 KASSERT(nam != NULL);
152
153 if (pcb == NULL)
154 return EINVAL;
155
156 sa = mtod(nam, struct sockaddr_bt *);
157 if (sa->bt_len != sizeof(struct sockaddr_bt))
158 return EINVAL;
159
160 if (sa->bt_family != AF_BLUETOOTH)
161 return EAFNOSUPPORT;
162
163 return rfcomm_bind_pcb(pcb, sa);
164 }
165
166 static int
167 rfcomm_listen(struct socket *so)
168 {
169 struct rfcomm_dlc *pcb = so->so_pcb;
170
171 KASSERT(solocked(so));
172
173 if (pcb == NULL)
174 return EINVAL;
175
176 return rfcomm_listen_pcb(pcb);
177 }
178
179 static int
180 rfcomm_connect(struct socket *so, struct mbuf *nam)
181 {
182 struct rfcomm_dlc *pcb = so->so_pcb;
183 struct sockaddr_bt *sa;
184
185 KASSERT(solocked(so));
186 KASSERT(nam != NULL);
187
188 if (pcb == NULL)
189 return EINVAL;
190
191 sa = mtod(nam, struct sockaddr_bt *);
192 if (sa->bt_len != sizeof(struct sockaddr_bt))
193 return EINVAL;
194
195 if (sa->bt_family != AF_BLUETOOTH)
196 return EAFNOSUPPORT;
197
198 soisconnecting(so);
199 return rfcomm_connect_pcb(pcb, sa);
200 }
201
202 static int
203 rfcomm_disconnect(struct socket *so)
204 {
205 struct rfcomm_dlc *pcb = so->so_pcb;
206
207 KASSERT(solocked(so));
208
209 if (pcb == NULL)
210 return EINVAL;
211
212 soisdisconnecting(so);
213 return rfcomm_disconnect_pcb(pcb, so->so_linger);
214 }
215
216 static int
217 rfcomm_shutdown(struct socket *so)
218 {
219 KASSERT(solocked(so));
220
221 socantsendmore(so);
222 return 0;
223 }
224
225 static int
226 rfcomm_abort(struct socket *so)
227 {
228 struct rfcomm_dlc *pcb = so->so_pcb;
229
230 KASSERT(solocked(so));
231
232 if (pcb == NULL)
233 return EINVAL;
234
235 rfcomm_disconnect_pcb(pcb, 0);
236 soisdisconnected(so);
237 rfcomm_detach(so);
238 return 0;
239 }
240
241 static int
242 rfcomm_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
243 {
244 return EPASSTHROUGH;
245 }
246
247 static int
248 rfcomm_stat(struct socket *so, struct stat *ub)
249 {
250 KASSERT(solocked(so));
251
252 return 0;
253 }
254
255 static int
256 rfcomm_peeraddr(struct socket *so, struct mbuf *nam)
257 {
258 struct rfcomm_dlc *pcb = so->so_pcb;
259 struct sockaddr_bt *sa;
260
261 KASSERT(solocked(so));
262 KASSERT(pcb != NULL);
263 KASSERT(nam != NULL);
264
265 sa = mtod(nam, struct sockaddr_bt *);
266 nam->m_len = sizeof(struct sockaddr_bt);
267 return rfcomm_peeraddr_pcb(pcb, sa);
268 }
269
270 static int
271 rfcomm_sockaddr(struct socket *so, struct mbuf *nam)
272 {
273 struct rfcomm_dlc *pcb = so->so_pcb;
274 struct sockaddr_bt *sa;
275
276 KASSERT(solocked(so));
277 KASSERT(pcb != NULL);
278 KASSERT(nam != NULL);
279
280 sa = mtod(nam, struct sockaddr_bt *);
281 nam->m_len = sizeof(struct sockaddr_bt);
282 return rfcomm_sockaddr_pcb(pcb, sa);
283 }
284
285 static int
286 rfcomm_recvoob(struct socket *so, struct mbuf *m, int flags)
287 {
288 KASSERT(solocked(so));
289
290 return EOPNOTSUPP;
291 }
292
293 static int
294 rfcomm_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
295 {
296 KASSERT(solocked(so));
297
298 if (m)
299 m_freem(m);
300 if (control)
301 m_freem(control);
302
303 return EOPNOTSUPP;
304 }
305
306 /*
307 * User Request.
308 * up is socket
309 * m is optional mbuf chain containing message
310 * nam is either
311 * optional mbuf chain containing an address
312 * message flags (PRU_RCVD)
313 * ctl is either
314 * optional mbuf chain containing socket options
315 * optional interface pointer PRU_PURGEIF
316 * l is pointer to process requesting action (if any)
317 *
318 * we are responsible for disposing of m and ctl if
319 * they are mbuf chains
320 */
321 static int
322 rfcomm_usrreq(struct socket *up, int req, struct mbuf *m,
323 struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
324 {
325 struct rfcomm_dlc *pcb = up->so_pcb;
326 struct mbuf *m0;
327 int err = 0;
328
329 DPRINTFN(2, "%s\n", prurequests[req]);
330 KASSERT(req != PRU_ATTACH);
331 KASSERT(req != PRU_DETACH);
332 KASSERT(req != PRU_ACCEPT);
333 KASSERT(req != PRU_BIND);
334 KASSERT(req != PRU_LISTEN);
335 KASSERT(req != PRU_CONNECT);
336 KASSERT(req != PRU_DISCONNECT);
337 KASSERT(req != PRU_SHUTDOWN);
338 KASSERT(req != PRU_ABORT);
339 KASSERT(req != PRU_CONTROL);
340 KASSERT(req != PRU_SENSE);
341 KASSERT(req != PRU_PEERADDR);
342 KASSERT(req != PRU_SOCKADDR);
343 KASSERT(req != PRU_RCVOOB);
344 KASSERT(req != PRU_SENDOOB);
345
346 switch (req) {
347 case PRU_PURGEIF:
348 return EOPNOTSUPP;
349 }
350 if (pcb == NULL) {
351 err = EINVAL;
352 goto release;
353 }
354
355 switch(req) {
356 case PRU_SEND:
357 KASSERT(m != NULL);
358
359 if (ctl) /* no use for that */
360 m_freem(ctl);
361
362 m0 = m_copypacket(m, M_DONTWAIT);
363 if (m0 == NULL)
364 return ENOMEM;
365
366 sbappendstream(&up->so_snd, m);
367
368 return rfcomm_send(pcb, m0);
369
370 case PRU_RCVD:
371 return rfcomm_rcvd(pcb, sbspace(&up->so_rcv));
372
373 case PRU_CONNECT2:
374 case PRU_FASTTIMO:
375 case PRU_SLOWTIMO:
376 case PRU_PROTORCV:
377 case PRU_PROTOSEND:
378 err = EOPNOTSUPP;
379 break;
380
381 default:
382 UNKNOWN(req);
383 err = EOPNOTSUPP;
384 break;
385 }
386
387 release:
388 if (m) m_freem(m);
389 if (ctl) m_freem(ctl);
390 return err;
391 }
392
393 /*
394 * rfcomm_ctloutput(req, socket, sockopt)
395 *
396 */
397 int
398 rfcomm_ctloutput(int req, struct socket *so, struct sockopt *sopt)
399 {
400 struct rfcomm_dlc *pcb = so->so_pcb;
401 int err = 0;
402
403 DPRINTFN(2, "%s\n", prcorequests[req]);
404
405 if (pcb == NULL)
406 return EINVAL;
407
408 if (sopt->sopt_level != BTPROTO_RFCOMM)
409 return ENOPROTOOPT;
410
411 switch(req) {
412 case PRCO_GETOPT:
413 err = rfcomm_getopt(pcb, sopt);
414 break;
415
416 case PRCO_SETOPT:
417 err = rfcomm_setopt(pcb, sopt);
418 break;
419
420 default:
421 err = ENOPROTOOPT;
422 break;
423 }
424
425 return err;
426 }
427
428 /**********************************************************************
429 *
430 * RFCOMM callbacks
431 */
432
433 static void
434 rfcomm_connecting(void *arg)
435 {
436 /* struct socket *so = arg; */
437
438 KASSERT(arg != NULL);
439 DPRINTF("Connecting\n");
440 }
441
442 static void
443 rfcomm_connected(void *arg)
444 {
445 struct socket *so = arg;
446
447 KASSERT(so != NULL);
448 DPRINTF("Connected\n");
449 soisconnected(so);
450 }
451
452 static void
453 rfcomm_disconnected(void *arg, int err)
454 {
455 struct socket *so = arg;
456
457 KASSERT(so != NULL);
458 DPRINTF("Disconnected\n");
459
460 so->so_error = err;
461 soisdisconnected(so);
462 }
463
464 static void *
465 rfcomm_newconn(void *arg, struct sockaddr_bt *laddr,
466 struct sockaddr_bt *raddr)
467 {
468 struct socket *so = arg;
469
470 DPRINTF("New Connection\n");
471 so = sonewconn(so, false);
472 if (so == NULL)
473 return NULL;
474
475 soisconnecting(so);
476
477 return so->so_pcb;
478 }
479
480 /*
481 * rfcomm_complete(rfcomm_dlc, length)
482 *
483 * length bytes are sent and may be removed from socket buffer
484 */
485 static void
486 rfcomm_complete(void *arg, int length)
487 {
488 struct socket *so = arg;
489
490 sbdrop(&so->so_snd, length);
491 sowwakeup(so);
492 }
493
494 /*
495 * rfcomm_linkmode(rfcomm_dlc, new)
496 *
497 * link mode change notification.
498 */
499 static void
500 rfcomm_linkmode(void *arg, int new)
501 {
502 struct socket *so = arg;
503 struct sockopt sopt;
504 int mode;
505
506 DPRINTF("auth %s, encrypt %s, secure %s\n",
507 (new & RFCOMM_LM_AUTH ? "on" : "off"),
508 (new & RFCOMM_LM_ENCRYPT ? "on" : "off"),
509 (new & RFCOMM_LM_SECURE ? "on" : "off"));
510
511 sockopt_init(&sopt, BTPROTO_RFCOMM, SO_RFCOMM_LM, 0);
512 (void)rfcomm_getopt(so->so_pcb, &sopt);
513 (void)sockopt_getint(&sopt, &mode);
514 sockopt_destroy(&sopt);
515
516 if (((mode & RFCOMM_LM_AUTH) && !(new & RFCOMM_LM_AUTH))
517 || ((mode & RFCOMM_LM_ENCRYPT) && !(new & RFCOMM_LM_ENCRYPT))
518 || ((mode & RFCOMM_LM_SECURE) && !(new & RFCOMM_LM_SECURE)))
519 rfcomm_disconnect_pcb(so->so_pcb, 0);
520 }
521
522 /*
523 * rfcomm_input(rfcomm_dlc, mbuf)
524 */
525 static void
526 rfcomm_input(void *arg, struct mbuf *m)
527 {
528 struct socket *so = arg;
529
530 KASSERT(so != NULL);
531
532 if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
533 printf("%s: %d bytes dropped (socket buffer full)\n",
534 __func__, m->m_pkthdr.len);
535 m_freem(m);
536 return;
537 }
538
539 DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
540
541 sbappendstream(&so->so_rcv, m);
542 sorwakeup(so);
543 }
544
545 PR_WRAP_USRREQS(rfcomm)
546
547 #define rfcomm_attach rfcomm_attach_wrapper
548 #define rfcomm_detach rfcomm_detach_wrapper
549 #define rfcomm_accept rfcomm_accept_wrapper
550 #define rfcomm_bind rfcomm_bind_wrapper
551 #define rfcomm_listen rfcomm_listen_wrapper
552 #define rfcomm_connect rfcomm_connect_wrapper
553 #define rfcomm_disconnect rfcomm_disconnect_wrapper
554 #define rfcomm_shutdown rfcomm_shutdown_wrapper
555 #define rfcomm_abort rfcomm_abort_wrapper
556 #define rfcomm_ioctl rfcomm_ioctl_wrapper
557 #define rfcomm_stat rfcomm_stat_wrapper
558 #define rfcomm_peeraddr rfcomm_peeraddr_wrapper
559 #define rfcomm_sockaddr rfcomm_sockaddr_wrapper
560 #define rfcomm_recvoob rfcomm_recvoob_wrapper
561 #define rfcomm_sendoob rfcomm_sendoob_wrapper
562 #define rfcomm_usrreq rfcomm_usrreq_wrapper
563
564 const struct pr_usrreqs rfcomm_usrreqs = {
565 .pr_attach = rfcomm_attach,
566 .pr_detach = rfcomm_detach,
567 .pr_accept = rfcomm_accept,
568 .pr_bind = rfcomm_bind,
569 .pr_listen = rfcomm_listen,
570 .pr_connect = rfcomm_connect,
571 .pr_disconnect = rfcomm_disconnect,
572 .pr_shutdown = rfcomm_shutdown,
573 .pr_abort = rfcomm_abort,
574 .pr_ioctl = rfcomm_ioctl,
575 .pr_stat = rfcomm_stat,
576 .pr_peeraddr = rfcomm_peeraddr,
577 .pr_sockaddr = rfcomm_sockaddr,
578 .pr_recvoob = rfcomm_recvoob,
579 .pr_sendoob = rfcomm_sendoob,
580 .pr_generic = rfcomm_usrreq,
581 };
582