l2cap_socket.c revision 1.31 1 /* $NetBSD: l2cap_socket.c,v 1.31 2014/08/09 05:33:01 rtr Exp $ */
2
3 /*-
4 * Copyright (c) 2005 Iain Hibbert.
5 * Copyright (c) 2006 Itronix Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of Itronix Inc. may not be used to endorse
17 * or promote products derived from this software without specific
18 * prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: l2cap_socket.c,v 1.31 2014/08/09 05:33:01 rtr Exp $");
35
36 /* load symbolic names */
37 #ifdef BLUETOOTH_DEBUG
38 #define PRUREQUESTS
39 #define PRCOREQUESTS
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/domain.h>
44 #include <sys/kernel.h>
45 #include <sys/mbuf.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/systm.h>
51
52 #include <netbt/bluetooth.h>
53 #include <netbt/l2cap.h>
54
55 /*
56 * L2CAP Sockets
57 *
58 * SOCK_SEQPACKET - normal L2CAP connection
59 *
60 * SOCK_DGRAM - connectionless L2CAP - XXX not yet
61 */
62
63 static void l2cap_connecting(void *);
64 static void l2cap_connected(void *);
65 static void l2cap_disconnected(void *, int);
66 static void *l2cap_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
67 static void l2cap_complete(void *, int);
68 static void l2cap_linkmode(void *, int);
69 static void l2cap_input(void *, struct mbuf *);
70
71 static const struct btproto l2cap_proto = {
72 l2cap_connecting,
73 l2cap_connected,
74 l2cap_disconnected,
75 l2cap_newconn,
76 l2cap_complete,
77 l2cap_linkmode,
78 l2cap_input,
79 };
80
81 /* sysctl variables */
82 int l2cap_sendspace = 4096;
83 int l2cap_recvspace = 4096;
84
85 static int
86 l2cap_attach(struct socket *so, int proto)
87 {
88 int error;
89
90 KASSERT(so->so_pcb == NULL);
91
92 if (so->so_lock == NULL) {
93 mutex_obj_hold(bt_lock);
94 so->so_lock = bt_lock;
95 solock(so);
96 }
97 KASSERT(solocked(so));
98
99 /*
100 * For L2CAP socket PCB we just use an l2cap_channel structure
101 * since we have nothing to add..
102 */
103 error = soreserve(so, l2cap_sendspace, l2cap_recvspace);
104 if (error)
105 return error;
106
107 return l2cap_attach_pcb((struct l2cap_channel **)&so->so_pcb,
108 &l2cap_proto, so);
109 }
110
111 static void
112 l2cap_detach(struct socket *so)
113 {
114 KASSERT(so->so_pcb != NULL);
115 l2cap_detach_pcb((struct l2cap_channel **)&so->so_pcb);
116 KASSERT(so->so_pcb == NULL);
117 }
118
119 static int
120 l2cap_accept(struct socket *so, struct mbuf *nam)
121 {
122 struct l2cap_channel *pcb = so->so_pcb;
123 struct sockaddr_bt *sa;
124
125 KASSERT(solocked(so));
126 KASSERT(nam != NULL);
127
128 if (pcb == NULL)
129 return EINVAL;
130
131 sa = mtod(nam, struct sockaddr_bt *);
132 nam->m_len = sizeof(struct sockaddr_bt);
133 return l2cap_peeraddr_pcb(pcb, sa);
134 }
135
136 static int
137 l2cap_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
138 {
139 struct l2cap_channel *pcb = so->so_pcb;
140 struct sockaddr_bt *sa;
141
142 KASSERT(solocked(so));
143 KASSERT(nam != NULL);
144
145 if (pcb == NULL)
146 return EINVAL;
147
148 sa = mtod(nam, struct sockaddr_bt *);
149 if (sa->bt_len != sizeof(struct sockaddr_bt))
150 return EINVAL;
151
152 if (sa->bt_family != AF_BLUETOOTH)
153 return EAFNOSUPPORT;
154
155 return l2cap_bind_pcb(pcb, sa);
156 }
157
158 static int
159 l2cap_listen(struct socket *so, struct lwp *l)
160 {
161 struct l2cap_channel *pcb = so->so_pcb;
162
163 KASSERT(solocked(so));
164
165 if (pcb == NULL)
166 return EINVAL;
167
168 return l2cap_listen_pcb(pcb);
169 }
170
171 static int
172 l2cap_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
173 {
174 struct l2cap_channel *pcb = so->so_pcb;
175 struct sockaddr_bt *sa;
176
177 KASSERT(solocked(so));
178 KASSERT(nam != NULL);
179
180 if (pcb == NULL)
181 return EINVAL;
182
183 sa = mtod(nam, struct sockaddr_bt *);
184 if (sa->bt_len != sizeof(struct sockaddr_bt))
185 return EINVAL;
186
187 if (sa->bt_family != AF_BLUETOOTH)
188 return EAFNOSUPPORT;
189
190 soisconnecting(so);
191 return l2cap_connect_pcb(pcb, sa);
192 }
193
194 static int
195 l2cap_connect2(struct socket *so, struct socket *so2)
196 {
197 KASSERT(solocked(so));
198
199 if (so->so_pcb == NULL)
200 return EINVAL;
201
202 return EOPNOTSUPP;
203 }
204
205 static int
206 l2cap_disconnect(struct socket *so)
207 {
208 struct l2cap_channel *pcb = so->so_pcb;
209
210 KASSERT(solocked(so));
211
212 if (pcb == NULL)
213 return EINVAL;
214
215 soisdisconnecting(so);
216 return l2cap_disconnect_pcb(pcb, so->so_linger);
217 }
218
219 static int
220 l2cap_shutdown(struct socket *so)
221 {
222 KASSERT(solocked(so));
223
224 socantsendmore(so);
225 return 0;
226 }
227
228 static int
229 l2cap_abort(struct socket *so)
230 {
231 struct l2cap_channel *pcb = so->so_pcb;
232
233 KASSERT(solocked(so));
234
235 if (pcb == NULL)
236 return EINVAL;
237
238 l2cap_disconnect_pcb(pcb, 0);
239 soisdisconnected(so);
240 l2cap_detach(so);
241 return 0;
242 }
243
244 static int
245 l2cap_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
246 {
247 return EPASSTHROUGH;
248 }
249
250 static int
251 l2cap_stat(struct socket *so, struct stat *ub)
252 {
253 KASSERT(solocked(so));
254
255 return 0;
256 }
257
258 static int
259 l2cap_peeraddr(struct socket *so, struct mbuf *nam)
260 {
261 struct l2cap_channel *pcb = so->so_pcb;
262 struct sockaddr_bt *sa;
263
264 KASSERT(solocked(so));
265 KASSERT(pcb != NULL);
266 KASSERT(nam != NULL);
267
268 sa = mtod(nam, struct sockaddr_bt *);
269 nam->m_len = sizeof(struct sockaddr_bt);
270 return l2cap_peeraddr_pcb(pcb, sa);
271 }
272
273 static int
274 l2cap_sockaddr(struct socket *so, struct mbuf *nam)
275 {
276 struct l2cap_channel *pcb = so->so_pcb;
277 struct sockaddr_bt *sa;
278
279 KASSERT(solocked(so));
280 KASSERT(pcb != NULL);
281 KASSERT(nam != NULL);
282
283 sa = mtod(nam, struct sockaddr_bt *);
284 nam->m_len = sizeof(struct sockaddr_bt);
285 return l2cap_sockaddr_pcb(pcb, sa);
286 }
287
288 static int
289 l2cap_rcvd(struct socket *so, int flags, struct lwp *l)
290 {
291 KASSERT(solocked(so));
292
293 return EOPNOTSUPP;
294 }
295
296 static int
297 l2cap_recvoob(struct socket *so, struct mbuf *m, int flags)
298 {
299 KASSERT(solocked(so));
300
301 return EOPNOTSUPP;
302 }
303
304 static int
305 l2cap_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
306 struct mbuf *control, struct lwp *l)
307 {
308 struct l2cap_channel *pcb = so->so_pcb;
309 struct mbuf *m0;
310 int error = 0;
311
312 KASSERT(solocked(so));
313 KASSERT(m != NULL);
314
315 if (control)
316 m_freem(control);
317
318 if (pcb == NULL) {
319 error = EINVAL;
320 goto release;
321 }
322
323 if (m->m_pkthdr.len == 0)
324 goto release;
325
326 if (m->m_pkthdr.len > pcb->lc_omtu) {
327 error = EMSGSIZE;
328 goto release;
329 }
330
331 m0 = m_copypacket(m, M_DONTWAIT);
332 if (m0 == NULL) {
333 error = ENOMEM;
334 goto release;
335 }
336
337 sbappendrecord(&so->so_snd, m);
338 return l2cap_send_pcb(pcb, m0);
339
340 release:
341 if (m)
342 m_freem(m);
343
344 return error;
345 }
346
347 static int
348 l2cap_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
349 {
350 KASSERT(solocked(so));
351
352 if (m)
353 m_freem(m);
354 if (control)
355 m_freem(control);
356
357 return EOPNOTSUPP;
358 }
359
360 static int
361 l2cap_purgeif(struct socket *so, struct ifnet *ifp)
362 {
363
364 return EOPNOTSUPP;
365 }
366
367 /*
368 * User Request.
369 * up is socket
370 * m is optional mbuf chain containing message
371 * ctl is either
372 * optional mbuf chain containing socket options
373 * l is pointer to process requesting action (if any)
374 *
375 * we are responsible for disposing of m and ctl if
376 * they are mbuf chains
377 */
378 static int
379 l2cap_usrreq(struct socket *up, int req, struct mbuf *m,
380 struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
381 {
382 struct l2cap_channel *pcb = up->so_pcb;
383 int err = 0;
384
385 DPRINTFN(2, "%s\n", prurequests[req]);
386 KASSERT(req != PRU_ATTACH);
387 KASSERT(req != PRU_DETACH);
388 KASSERT(req != PRU_ACCEPT);
389 KASSERT(req != PRU_BIND);
390 KASSERT(req != PRU_LISTEN);
391 KASSERT(req != PRU_CONNECT);
392 KASSERT(req != PRU_CONNECT2);
393 KASSERT(req != PRU_DISCONNECT);
394 KASSERT(req != PRU_SHUTDOWN);
395 KASSERT(req != PRU_ABORT);
396 KASSERT(req != PRU_CONTROL);
397 KASSERT(req != PRU_SENSE);
398 KASSERT(req != PRU_PEERADDR);
399 KASSERT(req != PRU_SOCKADDR);
400 KASSERT(req != PRU_RCVD);
401 KASSERT(req != PRU_RCVOOB);
402 KASSERT(req != PRU_SEND);
403 KASSERT(req != PRU_SENDOOB);
404 KASSERT(req != PRU_PURGEIF);
405
406 if (pcb == NULL) {
407 err = EINVAL;
408 goto release;
409 }
410
411 switch(req) {
412 case PRU_FASTTIMO:
413 case PRU_SLOWTIMO:
414 case PRU_PROTORCV:
415 case PRU_PROTOSEND:
416 err = EOPNOTSUPP;
417 break;
418
419 default:
420 UNKNOWN(req);
421 err = EOPNOTSUPP;
422 break;
423 }
424
425 release:
426 if (m) m_freem(m);
427 if (ctl) m_freem(ctl);
428 return err;
429 }
430
431 /*
432 * l2cap_ctloutput(req, socket, sockopt)
433 *
434 * Apply configuration commands to channel. This corresponds to
435 * "Reconfigure Channel Request" in the L2CAP specification.
436 */
437 int
438 l2cap_ctloutput(int req, struct socket *so, struct sockopt *sopt)
439 {
440 struct l2cap_channel *pcb = so->so_pcb;
441 int err = 0;
442
443 DPRINTFN(2, "%s\n", prcorequests[req]);
444
445 if (pcb == NULL)
446 return EINVAL;
447
448 if (sopt->sopt_level != BTPROTO_L2CAP)
449 return ENOPROTOOPT;
450
451 switch(req) {
452 case PRCO_GETOPT:
453 err = l2cap_getopt(pcb, sopt);
454 break;
455
456 case PRCO_SETOPT:
457 err = l2cap_setopt(pcb, sopt);
458 break;
459
460 default:
461 err = ENOPROTOOPT;
462 break;
463 }
464
465 return err;
466 }
467
468 /**********************************************************************
469 *
470 * L2CAP Protocol socket callbacks
471 *
472 */
473
474 static void
475 l2cap_connecting(void *arg)
476 {
477 struct socket *so = arg;
478
479 DPRINTF("Connecting\n");
480 soisconnecting(so);
481 }
482
483 static void
484 l2cap_connected(void *arg)
485 {
486 struct socket *so = arg;
487
488 DPRINTF("Connected\n");
489 soisconnected(so);
490 }
491
492 static void
493 l2cap_disconnected(void *arg, int err)
494 {
495 struct socket *so = arg;
496
497 DPRINTF("Disconnected (%d)\n", err);
498
499 so->so_error = err;
500 soisdisconnected(so);
501 }
502
503 static void *
504 l2cap_newconn(void *arg, struct sockaddr_bt *laddr,
505 struct sockaddr_bt *raddr)
506 {
507 struct socket *so = arg;
508
509 DPRINTF("New Connection\n");
510 so = sonewconn(so, false);
511 if (so == NULL)
512 return NULL;
513
514 soisconnecting(so);
515
516 return so->so_pcb;
517 }
518
519 static void
520 l2cap_complete(void *arg, int count)
521 {
522 struct socket *so = arg;
523
524 while (count-- > 0)
525 sbdroprecord(&so->so_snd);
526
527 sowwakeup(so);
528 }
529
530 static void
531 l2cap_linkmode(void *arg, int new)
532 {
533 struct socket *so = arg;
534 struct sockopt sopt;
535 int mode;
536
537 DPRINTF("auth %s, encrypt %s, secure %s\n",
538 (new & L2CAP_LM_AUTH ? "on" : "off"),
539 (new & L2CAP_LM_ENCRYPT ? "on" : "off"),
540 (new & L2CAP_LM_SECURE ? "on" : "off"));
541
542 sockopt_init(&sopt, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
543 (void)l2cap_getopt(so->so_pcb, &sopt);
544 (void)sockopt_getint(&sopt, &mode);
545 sockopt_destroy(&sopt);
546
547 if (((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
548 || ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
549 || ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE)))
550 l2cap_disconnect_pcb(so->so_pcb, 0);
551 }
552
553 static void
554 l2cap_input(void *arg, struct mbuf *m)
555 {
556 struct socket *so = arg;
557
558 if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
559 printf("%s: packet (%d bytes) dropped (socket buffer full)\n",
560 __func__, m->m_pkthdr.len);
561 m_freem(m);
562 return;
563 }
564
565 DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
566
567 sbappendrecord(&so->so_rcv, m);
568 sorwakeup(so);
569 }
570
571 PR_WRAP_USRREQS(l2cap)
572
573 #define l2cap_attach l2cap_attach_wrapper
574 #define l2cap_detach l2cap_detach_wrapper
575 #define l2cap_accept l2cap_accept_wrapper
576 #define l2cap_bind l2cap_bind_wrapper
577 #define l2cap_listen l2cap_listen_wrapper
578 #define l2cap_connect l2cap_connect_wrapper
579 #define l2cap_connect2 l2cap_connect2_wrapper
580 #define l2cap_disconnect l2cap_disconnect_wrapper
581 #define l2cap_shutdown l2cap_shutdown_wrapper
582 #define l2cap_abort l2cap_abort_wrapper
583 #define l2cap_ioctl l2cap_ioctl_wrapper
584 #define l2cap_stat l2cap_stat_wrapper
585 #define l2cap_peeraddr l2cap_peeraddr_wrapper
586 #define l2cap_sockaddr l2cap_sockaddr_wrapper
587 #define l2cap_rcvd l2cap_rcvd_wrapper
588 #define l2cap_recvoob l2cap_recvoob_wrapper
589 #define l2cap_send l2cap_send_wrapper
590 #define l2cap_sendoob l2cap_sendoob_wrapper
591 #define l2cap_purgeif l2cap_purgeif_wrapper
592 #define l2cap_usrreq l2cap_usrreq_wrapper
593
594 const struct pr_usrreqs l2cap_usrreqs = {
595 .pr_attach = l2cap_attach,
596 .pr_detach = l2cap_detach,
597 .pr_accept = l2cap_accept,
598 .pr_bind = l2cap_bind,
599 .pr_listen = l2cap_listen,
600 .pr_connect = l2cap_connect,
601 .pr_connect2 = l2cap_connect2,
602 .pr_disconnect = l2cap_disconnect,
603 .pr_shutdown = l2cap_shutdown,
604 .pr_abort = l2cap_abort,
605 .pr_ioctl = l2cap_ioctl,
606 .pr_stat = l2cap_stat,
607 .pr_peeraddr = l2cap_peeraddr,
608 .pr_sockaddr = l2cap_sockaddr,
609 .pr_rcvd = l2cap_rcvd,
610 .pr_recvoob = l2cap_recvoob,
611 .pr_send = l2cap_send,
612 .pr_sendoob = l2cap_sendoob,
613 .pr_purgeif = l2cap_purgeif,
614 .pr_generic = l2cap_usrreq,
615 };
616