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