l2cap_socket.c revision 1.34 1 /* $NetBSD: l2cap_socket.c,v 1.34 2015/04/26 21:40:49 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.34 2015/04/26 21:40:49 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 sockaddr *nam)
121 {
122 struct l2cap_channel *pcb = so->so_pcb;
123
124 KASSERT(solocked(so));
125 KASSERT(nam != NULL);
126
127 if (pcb == NULL)
128 return EINVAL;
129
130 return l2cap_peeraddr_pcb(pcb, (struct sockaddr_bt *)nam);
131 }
132
133 static int
134 l2cap_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
135 {
136 struct l2cap_channel *pcb = so->so_pcb;
137 struct sockaddr_bt *sa = (struct sockaddr_bt *)nam;
138
139 KASSERT(solocked(so));
140 KASSERT(nam != NULL);
141
142 if (pcb == NULL)
143 return EINVAL;
144
145 if (sa->bt_len != sizeof(struct sockaddr_bt))
146 return EINVAL;
147
148 if (sa->bt_family != AF_BLUETOOTH)
149 return EAFNOSUPPORT;
150
151 return l2cap_bind_pcb(pcb, sa);
152 }
153
154 static int
155 l2cap_listen(struct socket *so, struct lwp *l)
156 {
157 struct l2cap_channel *pcb = so->so_pcb;
158
159 KASSERT(solocked(so));
160
161 if (pcb == NULL)
162 return EINVAL;
163
164 return l2cap_listen_pcb(pcb);
165 }
166
167 static int
168 l2cap_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
169 {
170 struct l2cap_channel *pcb = so->so_pcb;
171 struct sockaddr_bt *sa;
172
173 KASSERT(solocked(so));
174 KASSERT(nam != NULL);
175
176 if (pcb == NULL)
177 return EINVAL;
178
179 sa = mtod(nam, struct sockaddr_bt *);
180 if (sa->bt_len != sizeof(struct sockaddr_bt))
181 return EINVAL;
182
183 if (sa->bt_family != AF_BLUETOOTH)
184 return EAFNOSUPPORT;
185
186 soisconnecting(so);
187 return l2cap_connect_pcb(pcb, sa);
188 }
189
190 static int
191 l2cap_connect2(struct socket *so, struct socket *so2)
192 {
193 KASSERT(solocked(so));
194
195 if (so->so_pcb == NULL)
196 return EINVAL;
197
198 return EOPNOTSUPP;
199 }
200
201 static int
202 l2cap_disconnect(struct socket *so)
203 {
204 struct l2cap_channel *pcb = so->so_pcb;
205
206 KASSERT(solocked(so));
207
208 if (pcb == NULL)
209 return EINVAL;
210
211 soisdisconnecting(so);
212 return l2cap_disconnect_pcb(pcb, so->so_linger);
213 }
214
215 static int
216 l2cap_shutdown(struct socket *so)
217 {
218 KASSERT(solocked(so));
219
220 socantsendmore(so);
221 return 0;
222 }
223
224 static int
225 l2cap_abort(struct socket *so)
226 {
227 struct l2cap_channel *pcb = so->so_pcb;
228
229 KASSERT(solocked(so));
230
231 if (pcb == NULL)
232 return EINVAL;
233
234 l2cap_disconnect_pcb(pcb, 0);
235 soisdisconnected(so);
236 l2cap_detach(so);
237 return 0;
238 }
239
240 static int
241 l2cap_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
242 {
243 return EPASSTHROUGH;
244 }
245
246 static int
247 l2cap_stat(struct socket *so, struct stat *ub)
248 {
249 KASSERT(solocked(so));
250
251 return 0;
252 }
253
254 static int
255 l2cap_peeraddr(struct socket *so, struct sockaddr *nam)
256 {
257 struct l2cap_channel *pcb = so->so_pcb;
258
259 KASSERT(solocked(so));
260 KASSERT(pcb != NULL);
261 KASSERT(nam != NULL);
262
263 return l2cap_peeraddr_pcb(pcb, (struct sockaddr_bt *)nam);
264 }
265
266 static int
267 l2cap_sockaddr(struct socket *so, struct sockaddr *nam)
268 {
269 struct l2cap_channel *pcb = so->so_pcb;
270
271 KASSERT(solocked(so));
272 KASSERT(pcb != NULL);
273 KASSERT(nam != NULL);
274
275 return l2cap_sockaddr_pcb(pcb, (struct sockaddr_bt *)nam);
276 }
277
278 static int
279 l2cap_rcvd(struct socket *so, int flags, struct lwp *l)
280 {
281 KASSERT(solocked(so));
282
283 return EOPNOTSUPP;
284 }
285
286 static int
287 l2cap_recvoob(struct socket *so, struct mbuf *m, int flags)
288 {
289 KASSERT(solocked(so));
290
291 return EOPNOTSUPP;
292 }
293
294 static int
295 l2cap_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
296 struct mbuf *control, struct lwp *l)
297 {
298 struct l2cap_channel *pcb = so->so_pcb;
299 struct mbuf *m0;
300 int error = 0;
301
302 KASSERT(solocked(so));
303 KASSERT(m != NULL);
304
305 if (control)
306 m_freem(control);
307
308 if (pcb == NULL) {
309 error = EINVAL;
310 goto release;
311 }
312
313 if (m->m_pkthdr.len == 0)
314 goto release;
315
316 if (m->m_pkthdr.len > pcb->lc_omtu) {
317 error = EMSGSIZE;
318 goto release;
319 }
320
321 m0 = m_copypacket(m, M_DONTWAIT);
322 if (m0 == NULL) {
323 error = ENOMEM;
324 goto release;
325 }
326
327 sbappendrecord(&so->so_snd, m);
328 return l2cap_send_pcb(pcb, m0);
329
330 release:
331 if (m)
332 m_freem(m);
333
334 return error;
335 }
336
337 static int
338 l2cap_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
339 {
340 KASSERT(solocked(so));
341
342 if (m)
343 m_freem(m);
344 if (control)
345 m_freem(control);
346
347 return EOPNOTSUPP;
348 }
349
350 static int
351 l2cap_purgeif(struct socket *so, struct ifnet *ifp)
352 {
353
354 return EOPNOTSUPP;
355 }
356
357 /*
358 * l2cap_ctloutput(req, socket, sockopt)
359 *
360 * Apply configuration commands to channel. This corresponds to
361 * "Reconfigure Channel Request" in the L2CAP specification.
362 */
363 int
364 l2cap_ctloutput(int req, struct socket *so, struct sockopt *sopt)
365 {
366 struct l2cap_channel *pcb = so->so_pcb;
367 int err = 0;
368
369 DPRINTFN(2, "%s\n", prcorequests[req]);
370
371 if (pcb == NULL)
372 return EINVAL;
373
374 if (sopt->sopt_level != BTPROTO_L2CAP)
375 return ENOPROTOOPT;
376
377 switch(req) {
378 case PRCO_GETOPT:
379 err = l2cap_getopt(pcb, sopt);
380 break;
381
382 case PRCO_SETOPT:
383 err = l2cap_setopt(pcb, sopt);
384 break;
385
386 default:
387 err = ENOPROTOOPT;
388 break;
389 }
390
391 return err;
392 }
393
394 /**********************************************************************
395 *
396 * L2CAP Protocol socket callbacks
397 *
398 */
399
400 static void
401 l2cap_connecting(void *arg)
402 {
403 struct socket *so = arg;
404
405 DPRINTF("Connecting\n");
406 soisconnecting(so);
407 }
408
409 static void
410 l2cap_connected(void *arg)
411 {
412 struct socket *so = arg;
413
414 DPRINTF("Connected\n");
415 soisconnected(so);
416 }
417
418 static void
419 l2cap_disconnected(void *arg, int err)
420 {
421 struct socket *so = arg;
422
423 DPRINTF("Disconnected (%d)\n", err);
424
425 so->so_error = err;
426 soisdisconnected(so);
427 }
428
429 static void *
430 l2cap_newconn(void *arg, struct sockaddr_bt *laddr,
431 struct sockaddr_bt *raddr)
432 {
433 struct socket *so = arg;
434
435 DPRINTF("New Connection\n");
436 so = sonewconn(so, false);
437 if (so == NULL)
438 return NULL;
439
440 soisconnecting(so);
441
442 return so->so_pcb;
443 }
444
445 static void
446 l2cap_complete(void *arg, int count)
447 {
448 struct socket *so = arg;
449
450 while (count-- > 0)
451 sbdroprecord(&so->so_snd);
452
453 sowwakeup(so);
454 }
455
456 static void
457 l2cap_linkmode(void *arg, int new)
458 {
459 struct socket *so = arg;
460 struct sockopt sopt;
461 int mode;
462
463 DPRINTF("auth %s, encrypt %s, secure %s\n",
464 (new & L2CAP_LM_AUTH ? "on" : "off"),
465 (new & L2CAP_LM_ENCRYPT ? "on" : "off"),
466 (new & L2CAP_LM_SECURE ? "on" : "off"));
467
468 sockopt_init(&sopt, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
469 (void)l2cap_getopt(so->so_pcb, &sopt);
470 (void)sockopt_getint(&sopt, &mode);
471 sockopt_destroy(&sopt);
472
473 if (((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
474 || ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
475 || ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE)))
476 l2cap_disconnect_pcb(so->so_pcb, 0);
477 }
478
479 static void
480 l2cap_input(void *arg, struct mbuf *m)
481 {
482 struct socket *so = arg;
483
484 if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
485 printf("%s: packet (%d bytes) dropped (socket buffer full)\n",
486 __func__, m->m_pkthdr.len);
487 m_freem(m);
488 return;
489 }
490
491 DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
492
493 sbappendrecord(&so->so_rcv, m);
494 sorwakeup(so);
495 }
496
497 PR_WRAP_USRREQS(l2cap)
498
499 #define l2cap_attach l2cap_attach_wrapper
500 #define l2cap_detach l2cap_detach_wrapper
501 #define l2cap_accept l2cap_accept_wrapper
502 #define l2cap_bind l2cap_bind_wrapper
503 #define l2cap_listen l2cap_listen_wrapper
504 #define l2cap_connect l2cap_connect_wrapper
505 #define l2cap_connect2 l2cap_connect2_wrapper
506 #define l2cap_disconnect l2cap_disconnect_wrapper
507 #define l2cap_shutdown l2cap_shutdown_wrapper
508 #define l2cap_abort l2cap_abort_wrapper
509 #define l2cap_ioctl l2cap_ioctl_wrapper
510 #define l2cap_stat l2cap_stat_wrapper
511 #define l2cap_peeraddr l2cap_peeraddr_wrapper
512 #define l2cap_sockaddr l2cap_sockaddr_wrapper
513 #define l2cap_rcvd l2cap_rcvd_wrapper
514 #define l2cap_recvoob l2cap_recvoob_wrapper
515 #define l2cap_send l2cap_send_wrapper
516 #define l2cap_sendoob l2cap_sendoob_wrapper
517 #define l2cap_purgeif l2cap_purgeif_wrapper
518
519 const struct pr_usrreqs l2cap_usrreqs = {
520 .pr_attach = l2cap_attach,
521 .pr_detach = l2cap_detach,
522 .pr_accept = l2cap_accept,
523 .pr_bind = l2cap_bind,
524 .pr_listen = l2cap_listen,
525 .pr_connect = l2cap_connect,
526 .pr_connect2 = l2cap_connect2,
527 .pr_disconnect = l2cap_disconnect,
528 .pr_shutdown = l2cap_shutdown,
529 .pr_abort = l2cap_abort,
530 .pr_ioctl = l2cap_ioctl,
531 .pr_stat = l2cap_stat,
532 .pr_peeraddr = l2cap_peeraddr,
533 .pr_sockaddr = l2cap_sockaddr,
534 .pr_rcvd = l2cap_rcvd,
535 .pr_recvoob = l2cap_recvoob,
536 .pr_send = l2cap_send,
537 .pr_sendoob = l2cap_sendoob,
538 .pr_purgeif = l2cap_purgeif,
539 };
540