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