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