rfcomm_socket.c revision 1.32 1 /* $NetBSD: rfcomm_socket.c,v 1.32 2014/08/08 03:05:45 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.32 2014/08/08 03:05:45 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_pcb(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, struct lwp *l)
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, struct lwp *l)
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, struct lwp *l)
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_rcvd(struct socket *so, int flags, struct lwp *l)
287 {
288 struct rfcomm_dlc *pcb = so->so_pcb;
289
290 KASSERT(solocked(so));
291
292 if (pcb == NULL)
293 return EINVAL;
294
295 return rfcomm_rcvd_pcb(pcb, sbspace(&so->so_rcv));
296 }
297
298 static int
299 rfcomm_recvoob(struct socket *so, struct mbuf *m, int flags)
300 {
301 KASSERT(solocked(so));
302
303 return EOPNOTSUPP;
304 }
305
306 static int
307 rfcomm_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
308 struct mbuf *control, struct lwp *l)
309 {
310 struct rfcomm_dlc *pcb = so->so_pcb;
311 int err = 0;
312 struct mbuf *m0;
313
314 KASSERT(solocked(so));
315 KASSERT(m != NULL);
316
317 if (control) /* no use for that */
318 m_freem(control);
319
320 if (pcb == NULL) {
321 err = EINVAL;
322 goto release;
323 }
324
325 m0 = m_copypacket(m, M_DONTWAIT);
326 if (m0 == NULL) {
327 err = ENOMEM;
328 goto release;
329 }
330
331 sbappendstream(&so->so_snd, m);
332 return rfcomm_send_pcb(pcb, m0);
333
334 release:
335 m_freem(m);
336 return err;
337 }
338
339 static int
340 rfcomm_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
341 {
342 KASSERT(solocked(so));
343
344 if (m)
345 m_freem(m);
346 if (control)
347 m_freem(control);
348
349 return EOPNOTSUPP;
350 }
351
352 /*
353 * User Request.
354 * up is socket
355 * m is optional mbuf chain containing message
356 * ctl is either
357 * optional mbuf chain containing socket options
358 * optional interface pointer PRU_PURGEIF
359 * l is pointer to process requesting action (if any)
360 *
361 * we are responsible for disposing of m and ctl if
362 * they are mbuf chains
363 */
364 static int
365 rfcomm_usrreq(struct socket *up, int req, struct mbuf *m,
366 struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
367 {
368 struct rfcomm_dlc *pcb = up->so_pcb;
369 int err = 0;
370
371 DPRINTFN(2, "%s\n", prurequests[req]);
372 KASSERT(req != PRU_ATTACH);
373 KASSERT(req != PRU_DETACH);
374 KASSERT(req != PRU_ACCEPT);
375 KASSERT(req != PRU_BIND);
376 KASSERT(req != PRU_LISTEN);
377 KASSERT(req != PRU_CONNECT);
378 KASSERT(req != PRU_DISCONNECT);
379 KASSERT(req != PRU_SHUTDOWN);
380 KASSERT(req != PRU_ABORT);
381 KASSERT(req != PRU_CONTROL);
382 KASSERT(req != PRU_SENSE);
383 KASSERT(req != PRU_PEERADDR);
384 KASSERT(req != PRU_SOCKADDR);
385 KASSERT(req != PRU_RCVD);
386 KASSERT(req != PRU_RCVOOB);
387 KASSERT(req != PRU_SEND);
388 KASSERT(req != PRU_SENDOOB);
389
390 switch (req) {
391 case PRU_PURGEIF:
392 return EOPNOTSUPP;
393 }
394 if (pcb == NULL) {
395 err = EINVAL;
396 goto release;
397 }
398
399 switch(req) {
400 case PRU_CONNECT2:
401 case PRU_FASTTIMO:
402 case PRU_SLOWTIMO:
403 case PRU_PROTORCV:
404 case PRU_PROTOSEND:
405 err = EOPNOTSUPP;
406 break;
407
408 default:
409 UNKNOWN(req);
410 err = EOPNOTSUPP;
411 break;
412 }
413
414 release:
415 if (m) m_freem(m);
416 if (ctl) m_freem(ctl);
417 return err;
418 }
419
420 /*
421 * rfcomm_ctloutput(req, socket, sockopt)
422 *
423 */
424 int
425 rfcomm_ctloutput(int req, struct socket *so, struct sockopt *sopt)
426 {
427 struct rfcomm_dlc *pcb = so->so_pcb;
428 int err = 0;
429
430 DPRINTFN(2, "%s\n", prcorequests[req]);
431
432 if (pcb == NULL)
433 return EINVAL;
434
435 if (sopt->sopt_level != BTPROTO_RFCOMM)
436 return ENOPROTOOPT;
437
438 switch(req) {
439 case PRCO_GETOPT:
440 err = rfcomm_getopt(pcb, sopt);
441 break;
442
443 case PRCO_SETOPT:
444 err = rfcomm_setopt(pcb, sopt);
445 break;
446
447 default:
448 err = ENOPROTOOPT;
449 break;
450 }
451
452 return err;
453 }
454
455 /**********************************************************************
456 *
457 * RFCOMM callbacks
458 */
459
460 static void
461 rfcomm_connecting(void *arg)
462 {
463 /* struct socket *so = arg; */
464
465 KASSERT(arg != NULL);
466 DPRINTF("Connecting\n");
467 }
468
469 static void
470 rfcomm_connected(void *arg)
471 {
472 struct socket *so = arg;
473
474 KASSERT(so != NULL);
475 DPRINTF("Connected\n");
476 soisconnected(so);
477 }
478
479 static void
480 rfcomm_disconnected(void *arg, int err)
481 {
482 struct socket *so = arg;
483
484 KASSERT(so != NULL);
485 DPRINTF("Disconnected\n");
486
487 so->so_error = err;
488 soisdisconnected(so);
489 }
490
491 static void *
492 rfcomm_newconn(void *arg, struct sockaddr_bt *laddr,
493 struct sockaddr_bt *raddr)
494 {
495 struct socket *so = arg;
496
497 DPRINTF("New Connection\n");
498 so = sonewconn(so, false);
499 if (so == NULL)
500 return NULL;
501
502 soisconnecting(so);
503
504 return so->so_pcb;
505 }
506
507 /*
508 * rfcomm_complete(rfcomm_dlc, length)
509 *
510 * length bytes are sent and may be removed from socket buffer
511 */
512 static void
513 rfcomm_complete(void *arg, int length)
514 {
515 struct socket *so = arg;
516
517 sbdrop(&so->so_snd, length);
518 sowwakeup(so);
519 }
520
521 /*
522 * rfcomm_linkmode(rfcomm_dlc, new)
523 *
524 * link mode change notification.
525 */
526 static void
527 rfcomm_linkmode(void *arg, int new)
528 {
529 struct socket *so = arg;
530 struct sockopt sopt;
531 int mode;
532
533 DPRINTF("auth %s, encrypt %s, secure %s\n",
534 (new & RFCOMM_LM_AUTH ? "on" : "off"),
535 (new & RFCOMM_LM_ENCRYPT ? "on" : "off"),
536 (new & RFCOMM_LM_SECURE ? "on" : "off"));
537
538 sockopt_init(&sopt, BTPROTO_RFCOMM, SO_RFCOMM_LM, 0);
539 (void)rfcomm_getopt(so->so_pcb, &sopt);
540 (void)sockopt_getint(&sopt, &mode);
541 sockopt_destroy(&sopt);
542
543 if (((mode & RFCOMM_LM_AUTH) && !(new & RFCOMM_LM_AUTH))
544 || ((mode & RFCOMM_LM_ENCRYPT) && !(new & RFCOMM_LM_ENCRYPT))
545 || ((mode & RFCOMM_LM_SECURE) && !(new & RFCOMM_LM_SECURE)))
546 rfcomm_disconnect_pcb(so->so_pcb, 0);
547 }
548
549 /*
550 * rfcomm_input(rfcomm_dlc, mbuf)
551 */
552 static void
553 rfcomm_input(void *arg, struct mbuf *m)
554 {
555 struct socket *so = arg;
556
557 KASSERT(so != NULL);
558
559 if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
560 printf("%s: %d bytes dropped (socket buffer full)\n",
561 __func__, m->m_pkthdr.len);
562 m_freem(m);
563 return;
564 }
565
566 DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
567
568 sbappendstream(&so->so_rcv, m);
569 sorwakeup(so);
570 }
571
572 PR_WRAP_USRREQS(rfcomm)
573
574 #define rfcomm_attach rfcomm_attach_wrapper
575 #define rfcomm_detach rfcomm_detach_wrapper
576 #define rfcomm_accept rfcomm_accept_wrapper
577 #define rfcomm_bind rfcomm_bind_wrapper
578 #define rfcomm_listen rfcomm_listen_wrapper
579 #define rfcomm_connect rfcomm_connect_wrapper
580 #define rfcomm_disconnect rfcomm_disconnect_wrapper
581 #define rfcomm_shutdown rfcomm_shutdown_wrapper
582 #define rfcomm_abort rfcomm_abort_wrapper
583 #define rfcomm_ioctl rfcomm_ioctl_wrapper
584 #define rfcomm_stat rfcomm_stat_wrapper
585 #define rfcomm_peeraddr rfcomm_peeraddr_wrapper
586 #define rfcomm_sockaddr rfcomm_sockaddr_wrapper
587 #define rfcomm_rcvd rfcomm_rcvd_wrapper
588 #define rfcomm_recvoob rfcomm_recvoob_wrapper
589 #define rfcomm_send rfcomm_send_wrapper
590 #define rfcomm_sendoob rfcomm_sendoob_wrapper
591 #define rfcomm_usrreq rfcomm_usrreq_wrapper
592
593 const struct pr_usrreqs rfcomm_usrreqs = {
594 .pr_attach = rfcomm_attach,
595 .pr_detach = rfcomm_detach,
596 .pr_accept = rfcomm_accept,
597 .pr_bind = rfcomm_bind,
598 .pr_listen = rfcomm_listen,
599 .pr_connect = rfcomm_connect,
600 .pr_disconnect = rfcomm_disconnect,
601 .pr_shutdown = rfcomm_shutdown,
602 .pr_abort = rfcomm_abort,
603 .pr_ioctl = rfcomm_ioctl,
604 .pr_stat = rfcomm_stat,
605 .pr_peeraddr = rfcomm_peeraddr,
606 .pr_sockaddr = rfcomm_sockaddr,
607 .pr_rcvd = rfcomm_rcvd,
608 .pr_recvoob = rfcomm_recvoob,
609 .pr_send = rfcomm_send,
610 .pr_sendoob = rfcomm_sendoob,
611 .pr_generic = rfcomm_usrreq,
612 };
613