sockin.c revision 1.51 1 /* $NetBSD: sockin.c,v 1.51 2014/07/24 15:12:03 rtr Exp $ */
2
3 /*
4 * Copyright (c) 2008, 2009 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: sockin.c,v 1.51 2014/07/24 15:12:03 rtr Exp $");
30
31 #include <sys/param.h>
32 #include <sys/condvar.h>
33 #include <sys/domain.h>
34 #include <sys/kmem.h>
35 #include <sys/kthread.h>
36 #include <sys/mbuf.h>
37 #include <sys/mutex.h>
38 #include <sys/once.h>
39 #include <sys/poll.h>
40 #include <sys/protosw.h>
41 #include <sys/queue.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/time.h>
45
46 #include <net/bpf.h>
47 #include <net/if.h>
48 #include <net/radix.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53
54 #include <rump/rumpuser.h>
55
56 #include "rump_private.h"
57 #include "sockin_user.h"
58
59 /*
60 * An inet communication domain which uses the socket interface.
61 * Supports IPv4 & IPv6 UDP/TCP.
62 */
63
64 DOMAIN_DEFINE(sockindomain);
65 DOMAIN_DEFINE(sockin6domain);
66
67 static int sockin_do_init(void);
68 static void sockin_init(void);
69 static int sockin_attach(struct socket *, int);
70 static void sockin_detach(struct socket *);
71 static int sockin_accept(struct socket *, struct mbuf *);
72 static int sockin_bind(struct socket *, struct mbuf *);
73 static int sockin_listen(struct socket *);
74 static int sockin_ioctl(struct socket *, u_long, void *, struct ifnet *);
75 static int sockin_stat(struct socket *, struct stat *);
76 static int sockin_peeraddr(struct socket *, struct mbuf *);
77 static int sockin_sockaddr(struct socket *, struct mbuf *);
78 static int sockin_recvoob(struct socket *, struct mbuf *, int);
79 static int sockin_sendoob(struct socket *, struct mbuf *, struct mbuf *);
80 static int sockin_usrreq(struct socket *, int, struct mbuf *,
81 struct mbuf *, struct mbuf *, struct lwp *);
82 static int sockin_ctloutput(int op, struct socket *, struct sockopt *);
83
84 static const struct pr_usrreqs sockin_usrreqs = {
85 .pr_attach = sockin_attach,
86 .pr_detach = sockin_detach,
87 .pr_accept = sockin_accept,
88 .pr_bind = sockin_bind,
89 .pr_listen = sockin_listen,
90 .pr_ioctl = sockin_ioctl,
91 .pr_stat = sockin_stat,
92 .pr_peeraddr = sockin_peeraddr,
93 .pr_sockaddr = sockin_sockaddr,
94 .pr_recvoob = sockin_recvoob,
95 .pr_sendoob = sockin_sendoob,
96 .pr_generic = sockin_usrreq,
97 };
98
99 const struct protosw sockinsw[] = {
100 {
101 .pr_type = SOCK_DGRAM,
102 .pr_domain = &sockindomain,
103 .pr_protocol = IPPROTO_UDP,
104 .pr_flags = PR_ATOMIC|PR_ADDR,
105 .pr_usrreqs = &sockin_usrreqs,
106 .pr_ctloutput = sockin_ctloutput,
107 },
108 {
109 .pr_type = SOCK_STREAM,
110 .pr_domain = &sockindomain,
111 .pr_protocol = IPPROTO_TCP,
112 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS,
113 .pr_usrreqs = &sockin_usrreqs,
114 .pr_ctloutput = sockin_ctloutput,
115 }};
116 const struct protosw sockin6sw[] = {
117 {
118 .pr_type = SOCK_DGRAM,
119 .pr_domain = &sockin6domain,
120 .pr_protocol = IPPROTO_UDP,
121 .pr_flags = PR_ATOMIC|PR_ADDR,
122 .pr_usrreqs = &sockin_usrreqs,
123 .pr_ctloutput = sockin_ctloutput,
124 },
125 {
126 .pr_type = SOCK_STREAM,
127 .pr_domain = &sockin6domain,
128 .pr_protocol = IPPROTO_TCP,
129 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS,
130 .pr_usrreqs = &sockin_usrreqs,
131 .pr_ctloutput = sockin_ctloutput,
132 }};
133
134 struct domain sockindomain = {
135 .dom_family = PF_INET,
136 .dom_name = "socket_inet",
137 .dom_init = sockin_init,
138 .dom_externalize = NULL,
139 .dom_dispose = NULL,
140 .dom_protosw = sockinsw,
141 .dom_protoswNPROTOSW = &sockinsw[__arraycount(sockinsw)],
142 .dom_rtattach = rt_inithead,
143 .dom_rtoffset = 32,
144 .dom_maxrtkey = sizeof(struct sockaddr_in),
145 .dom_ifattach = NULL,
146 .dom_ifdetach = NULL,
147 .dom_ifqueues = { NULL },
148 .dom_link = { NULL },
149 .dom_mowner = MOWNER_INIT("",""),
150 .dom_rtcache = { NULL },
151 .dom_sockaddr_cmp = NULL
152 };
153 struct domain sockin6domain = {
154 .dom_family = PF_INET6,
155 .dom_name = "socket_inet6",
156 .dom_init = sockin_init,
157 .dom_externalize = NULL,
158 .dom_dispose = NULL,
159 .dom_protosw = sockin6sw,
160 .dom_protoswNPROTOSW = &sockin6sw[__arraycount(sockin6sw)],
161 .dom_rtattach = rt_inithead,
162 .dom_rtoffset = 32,
163 .dom_maxrtkey = sizeof(struct sockaddr_in6),
164 .dom_ifattach = NULL,
165 .dom_ifdetach = NULL,
166 .dom_ifqueues = { NULL },
167 .dom_link = { NULL },
168 .dom_mowner = MOWNER_INIT("",""),
169 .dom_rtcache = { NULL },
170 .dom_sockaddr_cmp = NULL
171 };
172
173 #define SO2S(so) ((intptr_t)(so->so_internal))
174 #define SOCKIN_SBSIZE 65536
175
176 struct sockin_unit {
177 struct socket *su_so;
178
179 LIST_ENTRY(sockin_unit) su_entries;
180 };
181 static LIST_HEAD(, sockin_unit) su_ent = LIST_HEAD_INITIALIZER(su_ent);
182 static kmutex_t su_mtx;
183 static bool rebuild;
184 static int nsock;
185
186 /* XXX: for the bpf hack */
187 static struct ifnet sockin_if;
188 int ifpromisc(struct ifnet *ifp, int pswitch) { return 0; }
189
190 static int
191 registersock(struct socket *so, int news)
192 {
193 struct sockin_unit *su;
194
195 su = kmem_alloc(sizeof(*su), KM_NOSLEEP);
196 if (!su)
197 return ENOMEM;
198
199 so->so_internal = (void *)(intptr_t)news;
200 su->su_so = so;
201
202 mutex_enter(&su_mtx);
203 LIST_INSERT_HEAD(&su_ent, su, su_entries);
204 nsock++;
205 rebuild = true;
206 mutex_exit(&su_mtx);
207
208 return 0;
209 }
210
211 static void
212 removesock(struct socket *so)
213 {
214 struct sockin_unit *su_iter;
215
216 mutex_enter(&su_mtx);
217 LIST_FOREACH(su_iter, &su_ent, su_entries) {
218 if (su_iter->su_so == so)
219 break;
220 }
221 if (!su_iter)
222 panic("no such socket");
223
224 LIST_REMOVE(su_iter, su_entries);
225 nsock--;
226 rebuild = true;
227 mutex_exit(&su_mtx);
228
229 rumpuser_close(SO2S(su_iter->su_so));
230 kmem_free(su_iter, sizeof(*su_iter));
231 }
232
233 static void
234 sockin_process(struct socket *so)
235 {
236 struct sockaddr_in6 from;
237 struct iovec io;
238 struct msghdr rmsg;
239 struct mbuf *m;
240 size_t n, plen;
241 int error;
242
243 m = m_gethdr(M_WAIT, MT_DATA);
244 if (so->so_proto->pr_type == SOCK_DGRAM) {
245 plen = IP_MAXPACKET;
246 MEXTMALLOC(m, plen, M_DONTWAIT);
247 } else {
248 plen = MCLBYTES;
249 MCLGET(m, M_DONTWAIT);
250 }
251 if ((m->m_flags & M_EXT) == 0) {
252 m_freem(m);
253 return;
254 }
255
256 memset(&rmsg, 0, sizeof(rmsg));
257 io.iov_base = mtod(m, void *);
258 io.iov_len = plen;
259 rmsg.msg_iov = &io;
260 rmsg.msg_iovlen = 1;
261 rmsg.msg_name = (struct sockaddr *)&from;
262 rmsg.msg_namelen = sizeof(from);
263
264 error = rumpcomp_sockin_recvmsg(SO2S(so), &rmsg, 0, &n);
265 if (error || n == 0) {
266 m_freem(m);
267
268 /* Treat a TCP socket a goner */
269 if (error != EAGAIN && so->so_proto->pr_type == SOCK_STREAM) {
270 mutex_enter(softnet_lock);
271 soisdisconnected(so);
272 mutex_exit(softnet_lock);
273 removesock(so);
274 }
275 return;
276 }
277 m->m_len = m->m_pkthdr.len = n;
278
279 bpf_mtap_af(&sockin_if, AF_UNSPEC, m);
280
281 mutex_enter(softnet_lock);
282 if (so->so_proto->pr_type == SOCK_DGRAM) {
283 if (!sbappendaddr(&so->so_rcv, rmsg.msg_name, m, NULL)) {
284 m_freem(m);
285 }
286 } else {
287 sbappendstream(&so->so_rcv, m);
288 }
289
290 sorwakeup(so);
291 mutex_exit(softnet_lock);
292 }
293
294 static void
295 sockin_waccept(struct socket *so)
296 {
297 struct socket *nso;
298 struct sockaddr_in6 sin;
299 int news, error, slen;
300
301 slen = sizeof(sin);
302 error = rumpcomp_sockin_accept(SO2S(so), (struct sockaddr *)&sin,
303 &slen, &news);
304 if (error)
305 return;
306
307 mutex_enter(softnet_lock);
308 nso = sonewconn(so, true);
309 if (nso == NULL)
310 goto errout;
311 if (registersock(nso, news) != 0)
312 goto errout;
313 mutex_exit(softnet_lock);
314 return;
315
316 errout:
317 rumpuser_close(news);
318 if (nso)
319 soclose(nso);
320 mutex_exit(softnet_lock);
321 }
322
323 #define POLLTIMEOUT 100 /* check for new entries every 100ms */
324
325 /* XXX: doesn't handle socket (kernel) locking properly? */
326 static void
327 sockinworker(void *arg)
328 {
329 struct pollfd *pfds = NULL, *npfds;
330 struct sockin_unit *su_iter;
331 struct socket *so;
332 int cursock = 0, i, rv, error;
333
334 /*
335 * Loop reading requests. Check for new sockets periodically
336 * (could be smarter, but I'm lazy).
337 */
338 for (;;) {
339 if (rebuild) {
340 npfds = NULL;
341 mutex_enter(&su_mtx);
342 if (nsock)
343 npfds = kmem_alloc(nsock * sizeof(*npfds),
344 KM_NOSLEEP);
345 if (npfds || nsock == 0) {
346 if (pfds)
347 kmem_free(pfds, cursock*sizeof(*pfds));
348 pfds = npfds;
349 cursock = nsock;
350 rebuild = false;
351
352 i = 0;
353 LIST_FOREACH(su_iter, &su_ent, su_entries) {
354 pfds[i].fd = SO2S(su_iter->su_so);
355 pfds[i].events = POLLIN;
356 pfds[i].revents = 0;
357 i++;
358 }
359 KASSERT(i == nsock);
360 }
361 mutex_exit(&su_mtx);
362 }
363
364 /* find affected sockets & process */
365 error = rumpcomp_sockin_poll(pfds, cursock, POLLTIMEOUT, &rv);
366 for (i = 0; i < cursock && rv > 0 && error == 0; i++) {
367 if (pfds[i].revents & POLLIN) {
368 mutex_enter(&su_mtx);
369 LIST_FOREACH(su_iter, &su_ent, su_entries) {
370 if (SO2S(su_iter->su_so)==pfds[i].fd) {
371 so = su_iter->su_so;
372 mutex_exit(&su_mtx);
373 if(so->so_options&SO_ACCEPTCONN)
374 sockin_waccept(so);
375 else
376 sockin_process(so);
377 mutex_enter(&su_mtx);
378 break;
379 }
380 }
381 /* if we can't find it, just wing it */
382 KASSERT(rebuild || su_iter);
383 mutex_exit(&su_mtx);
384 pfds[i].revents = 0;
385 rv--;
386 i = -1;
387 continue;
388 }
389
390 /* something else? ignore */
391 if (pfds[i].revents) {
392 pfds[i].revents = 0;
393 rv--;
394 }
395 }
396 KASSERT(rv <= 0);
397 }
398
399 }
400
401 static int
402 sockin_do_init(void)
403 {
404 int rv;
405
406 if (rump_threads) {
407 if ((rv = kthread_create(PRI_NONE, 0, NULL, sockinworker,
408 NULL, NULL, "sockwork")) != 0)
409 panic("sockin_init: could not create worker thread\n");
410 } else {
411 printf("sockin_init: no threads => no worker thread\n");
412 }
413 mutex_init(&su_mtx, MUTEX_DEFAULT, IPL_NONE);
414 strlcpy(sockin_if.if_xname, "sockin0", sizeof(sockin_if.if_xname));
415 bpf_attach(&sockin_if, DLT_NULL, 0);
416 return 0;
417 }
418
419 static void
420 sockin_init(void)
421 {
422 static ONCE_DECL(init);
423
424 RUN_ONCE(&init, sockin_do_init);
425 }
426
427 static int
428 sockin_attach(struct socket *so, int proto)
429 {
430 const int type = so->so_proto->pr_type;
431 int error, news, family;
432
433 sosetlock(so);
434 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
435 error = soreserve(so, SOCKIN_SBSIZE, SOCKIN_SBSIZE);
436 if (error)
437 return error;
438 }
439
440 family = so->so_proto->pr_domain->dom_family;
441 KASSERT(family == PF_INET || family == PF_INET6);
442 error = rumpcomp_sockin_socket(family, type, 0, &news);
443 if (error)
444 return error;
445
446 /* For UDP sockets, make sure we can send/recv maximum. */
447 if (type == SOCK_DGRAM) {
448 int sbsize = SOCKIN_SBSIZE;
449 error = rumpcomp_sockin_setsockopt(news,
450 SOL_SOCKET, SO_SNDBUF,
451 &sbsize, sizeof(sbsize));
452 sbsize = SOCKIN_SBSIZE;
453 error = rumpcomp_sockin_setsockopt(news,
454 SOL_SOCKET, SO_RCVBUF,
455 &sbsize, sizeof(sbsize));
456 }
457
458 if ((error = registersock(so, news)) != 0)
459 rumpuser_close(news);
460
461 return error;
462 }
463
464 static void
465 sockin_detach(struct socket *so)
466 {
467 panic("sockin_detach: IMPLEMENT ME\n");
468 }
469
470 static int
471 sockin_accept(struct socket *so, struct mbuf *nam)
472 {
473 KASSERT(solocked(so));
474
475 /* we do all the work in the worker thread */
476 return 0;
477 }
478
479 static int
480 sockin_bind(struct socket *so, struct mbuf *nam)
481 {
482 KASSERT(solocked(so));
483 KASSERT(nam != NULL);
484
485 return rumpcomp_sockin_bind(SO2S(so),
486 mtod(nam, const struct sockaddr *),
487 nam->m_len);
488 }
489
490 static int
491 sockin_listen(struct socket *so)
492 {
493 KASSERT(solocked(so));
494
495 return rumpcomp_sockin_listen(SO2S(so), so->so_qlimit);
496 }
497
498 static int
499 sockin_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
500 {
501 return ENOTTY;
502 }
503
504 static int
505 sockin_stat(struct socket *so, struct stat *ub)
506 {
507 KASSERT(solocked(so));
508
509 return 0;
510 }
511
512 static int
513 sockin_peeraddr(struct socket *so, struct mbuf *nam)
514 {
515 KASSERT(solocked(so));
516
517 int error = 0;
518 int slen = nam->m_len;
519
520 error = rumpcomp_sockin_getname(SO2S(so),
521 mtod(nam, struct sockaddr *), &slen, RUMPCOMP_SOCKIN_PEERNAME);
522 if (error == 0)
523 nam->m_len = slen;
524 return error;
525 }
526
527 static int
528 sockin_sockaddr(struct socket *so, struct mbuf *nam)
529 {
530 KASSERT(solocked(so));
531
532 int error = 0;
533 int slen = nam->m_len;
534
535 error = rumpcomp_sockin_getname(SO2S(so),
536 mtod(nam, struct sockaddr *), &slen, RUMPCOMP_SOCKIN_SOCKNAME);
537 if (error == 0)
538 nam->m_len = slen;
539 return error;
540 }
541
542 static int
543 sockin_recvoob(struct socket *so, struct mbuf *m, int flags)
544 {
545 panic("sockin_recvoob: IMPLEMENT ME, recvoob not supported");
546 }
547
548 static int
549 sockin_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
550 {
551 panic("sockin_sendoob: IMPLEMENT ME, sendoob not supported");
552 }
553
554 static int
555 sockin_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
556 struct mbuf *control, struct lwp *l)
557 {
558 int error = 0;
559
560 KASSERT(req != PRU_ACCEPT);
561 KASSERT(req != PRU_CONTROL);
562 KASSERT(req != PRU_SENSE);
563 KASSERT(req != PRU_PEERADDR);
564 KASSERT(req != PRU_SOCKADDR);
565 KASSERT(req != PRU_RCVOOB);
566 KASSERT(req != PRU_SENDOOB);
567
568 switch (req) {
569 case PRU_CONNECT:
570 error = rumpcomp_sockin_connect(SO2S(so),
571 mtod(nam, struct sockaddr *), nam->m_len);
572 if (error == 0)
573 soisconnected(so);
574 break;
575
576 case PRU_SEND:
577 {
578 struct sockaddr *saddr;
579 struct msghdr mhdr;
580 size_t iov_max, i;
581 struct iovec iov_buf[32], *iov;
582 struct mbuf *m2;
583 size_t tot, n;
584 int s;
585
586 bpf_mtap_af(&sockin_if, AF_UNSPEC, m);
587
588 memset(&mhdr, 0, sizeof(mhdr));
589
590 iov_max = 0;
591 for (m2 = m; m2 != NULL; m2 = m2->m_next) {
592 iov_max++;
593 }
594
595 if (iov_max <= __arraycount(iov_buf)) {
596 iov = iov_buf;
597 } else {
598 iov = kmem_alloc(sizeof(struct iovec) * iov_max,
599 KM_SLEEP);
600 }
601
602 tot = 0;
603 for (i = 0, m2 = m; m2 != NULL; m2 = m2->m_next, i++) {
604 iov[i].iov_base = m2->m_data;
605 iov[i].iov_len = m2->m_len;
606 tot += m2->m_len;
607 }
608 mhdr.msg_iov = iov;
609 mhdr.msg_iovlen = i;
610 s = SO2S(so);
611
612 if (nam != NULL) {
613 saddr = mtod(nam, struct sockaddr *);
614 mhdr.msg_name = saddr;
615 mhdr.msg_namelen = saddr->sa_len;
616 }
617
618 rumpcomp_sockin_sendmsg(s, &mhdr, 0, &n);
619
620 if (iov != iov_buf)
621 kmem_free(iov, sizeof(struct iovec) * iov_max);
622
623 m_freem(m);
624 m_freem(control);
625
626 /* this assumes too many things to list.. buthey, testing */
627 if (!rump_threads)
628 sockin_process(so);
629 }
630 break;
631
632 case PRU_SHUTDOWN:
633 removesock(so);
634 break;
635
636 default:
637 panic("sockin_usrreq: IMPLEMENT ME, req %d not supported", req);
638 }
639
640 return error;
641 }
642
643 static int
644 sockin_ctloutput(int op, struct socket *so, struct sockopt *sopt)
645 {
646
647 return rumpcomp_sockin_setsockopt(SO2S(so), sopt->sopt_level,
648 sopt->sopt_name, sopt->sopt_data, sopt->sopt_size);
649 }
650
651 int sockin_unavailable(void);
652 int
653 sockin_unavailable(void)
654 {
655
656 panic("interface not available in with sockin");
657 }
658 __strong_alias(rtrequest,sockin_unavailable);
659 __strong_alias(ifunit,sockin_unavailable);
660 __strong_alias(ifreq_setaddr,sockin_unavailable);
661