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