l2cap_socket.c revision 1.22 1 1.22 rtr /* $NetBSD: l2cap_socket.c,v 1.22 2014/07/09 04:54:03 rtr Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2005 Iain Hibbert.
5 1.1 gdamore * Copyright (c) 2006 Itronix Inc.
6 1.1 gdamore * All rights reserved.
7 1.1 gdamore *
8 1.1 gdamore * Redistribution and use in source and binary forms, with or without
9 1.1 gdamore * modification, are permitted provided that the following conditions
10 1.1 gdamore * are met:
11 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
12 1.1 gdamore * notice, this list of conditions and the following disclaimer.
13 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
15 1.1 gdamore * documentation and/or other materials provided with the distribution.
16 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse
17 1.1 gdamore * or promote products derived from this software without specific
18 1.1 gdamore * prior written permission.
19 1.1 gdamore *
20 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE.
31 1.1 gdamore */
32 1.1 gdamore
33 1.1 gdamore #include <sys/cdefs.h>
34 1.22 rtr __KERNEL_RCSID(0, "$NetBSD: l2cap_socket.c,v 1.22 2014/07/09 04:54:03 rtr Exp $");
35 1.6 plunky
36 1.6 plunky /* load symbolic names */
37 1.6 plunky #ifdef BLUETOOTH_DEBUG
38 1.6 plunky #define PRUREQUESTS
39 1.6 plunky #define PRCOREQUESTS
40 1.6 plunky #endif
41 1.1 gdamore
42 1.1 gdamore #include <sys/param.h>
43 1.1 gdamore #include <sys/domain.h>
44 1.1 gdamore #include <sys/kernel.h>
45 1.1 gdamore #include <sys/mbuf.h>
46 1.1 gdamore #include <sys/proc.h>
47 1.1 gdamore #include <sys/protosw.h>
48 1.1 gdamore #include <sys/socket.h>
49 1.1 gdamore #include <sys/socketvar.h>
50 1.1 gdamore #include <sys/systm.h>
51 1.1 gdamore
52 1.1 gdamore #include <netbt/bluetooth.h>
53 1.1 gdamore #include <netbt/l2cap.h>
54 1.1 gdamore
55 1.1 gdamore /*
56 1.1 gdamore * L2CAP Sockets
57 1.1 gdamore *
58 1.1 gdamore * SOCK_SEQPACKET - normal L2CAP connection
59 1.1 gdamore *
60 1.1 gdamore * SOCK_DGRAM - connectionless L2CAP - XXX not yet
61 1.1 gdamore */
62 1.1 gdamore
63 1.1 gdamore static void l2cap_connecting(void *);
64 1.1 gdamore static void l2cap_connected(void *);
65 1.1 gdamore static void l2cap_disconnected(void *, int);
66 1.1 gdamore static void *l2cap_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
67 1.1 gdamore static void l2cap_complete(void *, int);
68 1.7 plunky static void l2cap_linkmode(void *, int);
69 1.1 gdamore static void l2cap_input(void *, struct mbuf *);
70 1.1 gdamore
71 1.1 gdamore static const struct btproto l2cap_proto = {
72 1.1 gdamore l2cap_connecting,
73 1.1 gdamore l2cap_connected,
74 1.1 gdamore l2cap_disconnected,
75 1.1 gdamore l2cap_newconn,
76 1.1 gdamore l2cap_complete,
77 1.7 plunky l2cap_linkmode,
78 1.7 plunky l2cap_input,
79 1.1 gdamore };
80 1.1 gdamore
81 1.1 gdamore /* sysctl variables */
82 1.1 gdamore int l2cap_sendspace = 4096;
83 1.1 gdamore int l2cap_recvspace = 4096;
84 1.1 gdamore
85 1.12 rmind static int
86 1.14 rmind l2cap_attach(struct socket *so, int proto)
87 1.12 rmind {
88 1.12 rmind int error;
89 1.12 rmind
90 1.12 rmind KASSERT(so->so_pcb == NULL);
91 1.12 rmind
92 1.12 rmind if (so->so_lock == NULL) {
93 1.12 rmind mutex_obj_hold(bt_lock);
94 1.12 rmind so->so_lock = bt_lock;
95 1.12 rmind solock(so);
96 1.12 rmind }
97 1.12 rmind KASSERT(solocked(so));
98 1.12 rmind
99 1.12 rmind /*
100 1.12 rmind * For L2CAP socket PCB we just use an l2cap_channel structure
101 1.12 rmind * since we have nothing to add..
102 1.12 rmind */
103 1.12 rmind error = soreserve(so, l2cap_sendspace, l2cap_recvspace);
104 1.12 rmind if (error)
105 1.12 rmind return error;
106 1.12 rmind
107 1.14 rmind return l2cap_attach_pcb((struct l2cap_channel **)&so->so_pcb,
108 1.12 rmind &l2cap_proto, so);
109 1.12 rmind }
110 1.12 rmind
111 1.12 rmind static void
112 1.14 rmind l2cap_detach(struct socket *so)
113 1.12 rmind {
114 1.13 rmind KASSERT(so->so_pcb != NULL);
115 1.14 rmind l2cap_detach_pcb((struct l2cap_channel **)&so->so_pcb);
116 1.12 rmind KASSERT(so->so_pcb == NULL);
117 1.12 rmind }
118 1.12 rmind
119 1.16 rtr static int
120 1.21 rtr l2cap_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
121 1.16 rtr {
122 1.16 rtr return EPASSTHROUGH;
123 1.16 rtr }
124 1.16 rtr
125 1.18 rtr static int
126 1.18 rtr l2cap_stat(struct socket *so, struct stat *ub)
127 1.18 rtr {
128 1.21 rtr KASSERT(solocked(so));
129 1.21 rtr
130 1.20 rtr return 0;
131 1.18 rtr }
132 1.18 rtr
133 1.22 rtr static int
134 1.22 rtr l2cap_peeraddr(struct socket *so, struct mbuf *nam)
135 1.22 rtr {
136 1.22 rtr struct l2cap_channel *pcb = so->so_pcb;
137 1.22 rtr struct sockaddr_bt *sa;
138 1.22 rtr
139 1.22 rtr KASSERT(solocked(so));
140 1.22 rtr KASSERT(pcb != NULL);
141 1.22 rtr KASSERT(nam != NULL);
142 1.22 rtr
143 1.22 rtr sa = mtod(nam, struct sockaddr_bt *);
144 1.22 rtr nam->m_len = sizeof(struct sockaddr_bt);
145 1.22 rtr return l2cap_peeraddr_pcb(pcb, sa);
146 1.22 rtr }
147 1.22 rtr
148 1.22 rtr static int
149 1.22 rtr l2cap_sockaddr(struct socket *so, struct mbuf *nam)
150 1.22 rtr {
151 1.22 rtr struct l2cap_channel *pcb = so->so_pcb;
152 1.22 rtr struct sockaddr_bt *sa;
153 1.22 rtr
154 1.22 rtr KASSERT(solocked(so));
155 1.22 rtr KASSERT(pcb != NULL);
156 1.22 rtr KASSERT(nam != NULL);
157 1.22 rtr
158 1.22 rtr sa = mtod(nam, struct sockaddr_bt *);
159 1.22 rtr nam->m_len = sizeof(struct sockaddr_bt);
160 1.22 rtr return l2cap_sockaddr_pcb(pcb, sa);
161 1.22 rtr }
162 1.22 rtr
163 1.1 gdamore /*
164 1.1 gdamore * User Request.
165 1.1 gdamore * up is socket
166 1.16 rtr * m is optional mbuf chain containing message
167 1.1 gdamore * nam is either
168 1.1 gdamore * optional mbuf chain containing an address
169 1.1 gdamore * message flags (PRU_RCVD)
170 1.1 gdamore * ctl is either
171 1.1 gdamore * optional mbuf chain containing socket options
172 1.16 rtr * optional interface pointer PRU_PURGEIF
173 1.1 gdamore * l is pointer to process requesting action (if any)
174 1.1 gdamore *
175 1.1 gdamore * we are responsible for disposing of m and ctl if
176 1.1 gdamore * they are mbuf chains
177 1.1 gdamore */
178 1.11 rmind static int
179 1.1 gdamore l2cap_usrreq(struct socket *up, int req, struct mbuf *m,
180 1.3 christos struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
181 1.1 gdamore {
182 1.1 gdamore struct l2cap_channel *pcb = up->so_pcb;
183 1.1 gdamore struct sockaddr_bt *sa;
184 1.1 gdamore struct mbuf *m0;
185 1.1 gdamore int err = 0;
186 1.1 gdamore
187 1.1 gdamore DPRINTFN(2, "%s\n", prurequests[req]);
188 1.12 rmind KASSERT(req != PRU_ATTACH);
189 1.12 rmind KASSERT(req != PRU_DETACH);
190 1.16 rtr KASSERT(req != PRU_CONTROL);
191 1.18 rtr KASSERT(req != PRU_SENSE);
192 1.22 rtr KASSERT(req != PRU_PEERADDR);
193 1.22 rtr KASSERT(req != PRU_SOCKADDR);
194 1.1 gdamore
195 1.1 gdamore switch (req) {
196 1.1 gdamore case PRU_PURGEIF:
197 1.1 gdamore return EOPNOTSUPP;
198 1.1 gdamore }
199 1.1 gdamore
200 1.1 gdamore if (pcb == NULL) {
201 1.1 gdamore err = EINVAL;
202 1.1 gdamore goto release;
203 1.1 gdamore }
204 1.1 gdamore
205 1.1 gdamore switch(req) {
206 1.1 gdamore case PRU_DISCONNECT:
207 1.1 gdamore soisdisconnecting(up);
208 1.1 gdamore return l2cap_disconnect(pcb, up->so_linger);
209 1.1 gdamore
210 1.1 gdamore case PRU_ABORT:
211 1.1 gdamore l2cap_disconnect(pcb, 0);
212 1.1 gdamore soisdisconnected(up);
213 1.14 rmind l2cap_detach(up);
214 1.12 rmind return 0;
215 1.1 gdamore
216 1.1 gdamore case PRU_BIND:
217 1.5 plunky KASSERT(nam != NULL);
218 1.1 gdamore sa = mtod(nam, struct sockaddr_bt *);
219 1.1 gdamore
220 1.1 gdamore if (sa->bt_len != sizeof(struct sockaddr_bt))
221 1.1 gdamore return EINVAL;
222 1.1 gdamore
223 1.1 gdamore if (sa->bt_family != AF_BLUETOOTH)
224 1.1 gdamore return EAFNOSUPPORT;
225 1.1 gdamore
226 1.1 gdamore return l2cap_bind(pcb, sa);
227 1.1 gdamore
228 1.1 gdamore case PRU_CONNECT:
229 1.5 plunky KASSERT(nam != NULL);
230 1.1 gdamore sa = mtod(nam, struct sockaddr_bt *);
231 1.1 gdamore
232 1.1 gdamore if (sa->bt_len != sizeof(struct sockaddr_bt))
233 1.1 gdamore return EINVAL;
234 1.1 gdamore
235 1.1 gdamore if (sa->bt_family != AF_BLUETOOTH)
236 1.1 gdamore return EAFNOSUPPORT;
237 1.1 gdamore
238 1.1 gdamore soisconnecting(up);
239 1.1 gdamore return l2cap_connect(pcb, sa);
240 1.1 gdamore
241 1.1 gdamore case PRU_SHUTDOWN:
242 1.1 gdamore socantsendmore(up);
243 1.1 gdamore break;
244 1.1 gdamore
245 1.1 gdamore case PRU_SEND:
246 1.5 plunky KASSERT(m != NULL);
247 1.1 gdamore if (m->m_pkthdr.len == 0)
248 1.1 gdamore break;
249 1.1 gdamore
250 1.1 gdamore if (m->m_pkthdr.len > pcb->lc_omtu) {
251 1.1 gdamore err = EMSGSIZE;
252 1.1 gdamore break;
253 1.1 gdamore }
254 1.1 gdamore
255 1.1 gdamore m0 = m_copypacket(m, M_DONTWAIT);
256 1.1 gdamore if (m0 == NULL) {
257 1.1 gdamore err = ENOMEM;
258 1.1 gdamore break;
259 1.1 gdamore }
260 1.1 gdamore
261 1.1 gdamore if (ctl) /* no use for that */
262 1.1 gdamore m_freem(ctl);
263 1.1 gdamore
264 1.1 gdamore sbappendrecord(&up->so_snd, m);
265 1.1 gdamore return l2cap_send(pcb, m0);
266 1.1 gdamore
267 1.1 gdamore case PRU_RCVD:
268 1.1 gdamore case PRU_RCVOOB:
269 1.1 gdamore return EOPNOTSUPP; /* (no release) */
270 1.1 gdamore
271 1.1 gdamore case PRU_LISTEN:
272 1.1 gdamore return l2cap_listen(pcb);
273 1.1 gdamore
274 1.1 gdamore case PRU_ACCEPT:
275 1.5 plunky KASSERT(nam != NULL);
276 1.1 gdamore sa = mtod(nam, struct sockaddr_bt *);
277 1.1 gdamore nam->m_len = sizeof(struct sockaddr_bt);
278 1.22 rtr return l2cap_peeraddr_pcb(pcb, sa);
279 1.1 gdamore
280 1.1 gdamore case PRU_CONNECT2:
281 1.1 gdamore case PRU_SENDOOB:
282 1.1 gdamore case PRU_FASTTIMO:
283 1.1 gdamore case PRU_SLOWTIMO:
284 1.1 gdamore case PRU_PROTORCV:
285 1.1 gdamore case PRU_PROTOSEND:
286 1.1 gdamore err = EOPNOTSUPP;
287 1.1 gdamore break;
288 1.1 gdamore
289 1.1 gdamore default:
290 1.1 gdamore UNKNOWN(req);
291 1.1 gdamore err = EOPNOTSUPP;
292 1.1 gdamore break;
293 1.1 gdamore }
294 1.1 gdamore
295 1.1 gdamore release:
296 1.1 gdamore if (m) m_freem(m);
297 1.1 gdamore if (ctl) m_freem(ctl);
298 1.1 gdamore return err;
299 1.1 gdamore }
300 1.1 gdamore
301 1.1 gdamore /*
302 1.9 plunky * l2cap_ctloutput(req, socket, sockopt)
303 1.1 gdamore *
304 1.1 gdamore * Apply configuration commands to channel. This corresponds to
305 1.1 gdamore * "Reconfigure Channel Request" in the L2CAP specification.
306 1.1 gdamore */
307 1.1 gdamore int
308 1.9 plunky l2cap_ctloutput(int req, struct socket *so, struct sockopt *sopt)
309 1.1 gdamore {
310 1.1 gdamore struct l2cap_channel *pcb = so->so_pcb;
311 1.1 gdamore int err = 0;
312 1.1 gdamore
313 1.1 gdamore DPRINTFN(2, "%s\n", prcorequests[req]);
314 1.1 gdamore
315 1.4 plunky if (pcb == NULL)
316 1.4 plunky return EINVAL;
317 1.4 plunky
318 1.9 plunky if (sopt->sopt_level != BTPROTO_L2CAP)
319 1.4 plunky return ENOPROTOOPT;
320 1.1 gdamore
321 1.1 gdamore switch(req) {
322 1.1 gdamore case PRCO_GETOPT:
323 1.9 plunky err = l2cap_getopt(pcb, sopt);
324 1.1 gdamore break;
325 1.1 gdamore
326 1.1 gdamore case PRCO_SETOPT:
327 1.9 plunky err = l2cap_setopt(pcb, sopt);
328 1.1 gdamore break;
329 1.1 gdamore
330 1.1 gdamore default:
331 1.4 plunky err = ENOPROTOOPT;
332 1.1 gdamore break;
333 1.1 gdamore }
334 1.1 gdamore
335 1.1 gdamore return err;
336 1.1 gdamore }
337 1.1 gdamore
338 1.1 gdamore /**********************************************************************
339 1.1 gdamore *
340 1.1 gdamore * L2CAP Protocol socket callbacks
341 1.1 gdamore *
342 1.1 gdamore */
343 1.1 gdamore
344 1.1 gdamore static void
345 1.1 gdamore l2cap_connecting(void *arg)
346 1.1 gdamore {
347 1.1 gdamore struct socket *so = arg;
348 1.1 gdamore
349 1.1 gdamore DPRINTF("Connecting\n");
350 1.1 gdamore soisconnecting(so);
351 1.1 gdamore }
352 1.1 gdamore
353 1.1 gdamore static void
354 1.1 gdamore l2cap_connected(void *arg)
355 1.1 gdamore {
356 1.1 gdamore struct socket *so = arg;
357 1.1 gdamore
358 1.1 gdamore DPRINTF("Connected\n");
359 1.1 gdamore soisconnected(so);
360 1.1 gdamore }
361 1.1 gdamore
362 1.1 gdamore static void
363 1.1 gdamore l2cap_disconnected(void *arg, int err)
364 1.1 gdamore {
365 1.1 gdamore struct socket *so = arg;
366 1.1 gdamore
367 1.1 gdamore DPRINTF("Disconnected (%d)\n", err);
368 1.1 gdamore
369 1.1 gdamore so->so_error = err;
370 1.1 gdamore soisdisconnected(so);
371 1.1 gdamore }
372 1.1 gdamore
373 1.1 gdamore static void *
374 1.3 christos l2cap_newconn(void *arg, struct sockaddr_bt *laddr,
375 1.3 christos struct sockaddr_bt *raddr)
376 1.1 gdamore {
377 1.1 gdamore struct socket *so = arg;
378 1.1 gdamore
379 1.1 gdamore DPRINTF("New Connection\n");
380 1.10 rmind so = sonewconn(so, false);
381 1.1 gdamore if (so == NULL)
382 1.1 gdamore return NULL;
383 1.1 gdamore
384 1.1 gdamore soisconnecting(so);
385 1.1 gdamore
386 1.1 gdamore return so->so_pcb;
387 1.1 gdamore }
388 1.1 gdamore
389 1.1 gdamore static void
390 1.1 gdamore l2cap_complete(void *arg, int count)
391 1.1 gdamore {
392 1.1 gdamore struct socket *so = arg;
393 1.1 gdamore
394 1.1 gdamore while (count-- > 0)
395 1.1 gdamore sbdroprecord(&so->so_snd);
396 1.1 gdamore
397 1.1 gdamore sowwakeup(so);
398 1.1 gdamore }
399 1.1 gdamore
400 1.1 gdamore static void
401 1.7 plunky l2cap_linkmode(void *arg, int new)
402 1.7 plunky {
403 1.7 plunky struct socket *so = arg;
404 1.9 plunky struct sockopt sopt;
405 1.7 plunky int mode;
406 1.7 plunky
407 1.7 plunky DPRINTF("auth %s, encrypt %s, secure %s\n",
408 1.7 plunky (new & L2CAP_LM_AUTH ? "on" : "off"),
409 1.7 plunky (new & L2CAP_LM_ENCRYPT ? "on" : "off"),
410 1.7 plunky (new & L2CAP_LM_SECURE ? "on" : "off"));
411 1.7 plunky
412 1.9 plunky sockopt_init(&sopt, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
413 1.9 plunky (void)l2cap_getopt(so->so_pcb, &sopt);
414 1.9 plunky (void)sockopt_getint(&sopt, &mode);
415 1.9 plunky sockopt_destroy(&sopt);
416 1.9 plunky
417 1.7 plunky if (((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
418 1.7 plunky || ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
419 1.7 plunky || ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE)))
420 1.7 plunky l2cap_disconnect(so->so_pcb, 0);
421 1.7 plunky }
422 1.7 plunky
423 1.7 plunky static void
424 1.1 gdamore l2cap_input(void *arg, struct mbuf *m)
425 1.1 gdamore {
426 1.1 gdamore struct socket *so = arg;
427 1.1 gdamore
428 1.1 gdamore if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
429 1.1 gdamore printf("%s: packet (%d bytes) dropped (socket buffer full)\n",
430 1.1 gdamore __func__, m->m_pkthdr.len);
431 1.1 gdamore m_freem(m);
432 1.1 gdamore return;
433 1.1 gdamore }
434 1.1 gdamore
435 1.1 gdamore DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
436 1.1 gdamore
437 1.1 gdamore sbappendrecord(&so->so_rcv, m);
438 1.1 gdamore sorwakeup(so);
439 1.1 gdamore }
440 1.11 rmind
441 1.15 rmind PR_WRAP_USRREQS(l2cap)
442 1.11 rmind
443 1.15 rmind #define l2cap_attach l2cap_attach_wrapper
444 1.15 rmind #define l2cap_detach l2cap_detach_wrapper
445 1.16 rtr #define l2cap_ioctl l2cap_ioctl_wrapper
446 1.18 rtr #define l2cap_stat l2cap_stat_wrapper
447 1.22 rtr #define l2cap_peeraddr l2cap_peeraddr_wrapper
448 1.22 rtr #define l2cap_sockaddr l2cap_sockaddr_wrapper
449 1.11 rmind #define l2cap_usrreq l2cap_usrreq_wrapper
450 1.11 rmind
451 1.11 rmind const struct pr_usrreqs l2cap_usrreqs = {
452 1.14 rmind .pr_attach = l2cap_attach,
453 1.14 rmind .pr_detach = l2cap_detach,
454 1.16 rtr .pr_ioctl = l2cap_ioctl,
455 1.18 rtr .pr_stat = l2cap_stat,
456 1.22 rtr .pr_peeraddr = l2cap_peeraddr,
457 1.22 rtr .pr_sockaddr = l2cap_sockaddr,
458 1.11 rmind .pr_generic = l2cap_usrreq,
459 1.11 rmind };
460