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