Home | History | Annotate | Line # | Download | only in libsockin
sockin.c revision 1.4.2.3
      1  1.4.2.3    skrll /*	$NetBSD: sockin.c,v 1.4.2.3 2009/04/28 07:37:52 skrll Exp $	*/
      2      1.1    pooka 
      3      1.1    pooka /*
      4  1.4.2.2    skrll  * 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.4.2.1    skrll #include <sys/cdefs.h>
     29  1.4.2.3    skrll __KERNEL_RCSID(0, "$NetBSD: sockin.c,v 1.4.2.3 2009/04/28 07:37:52 skrll Exp $");
     30  1.4.2.1    skrll 
     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.1    pooka #include <sys/poll.h>
     39      1.1    pooka #include <sys/protosw.h>
     40      1.1    pooka #include <sys/queue.h>
     41      1.1    pooka #include <sys/socket.h>
     42      1.1    pooka #include <sys/socketvar.h>
     43      1.1    pooka #include <sys/time.h>
     44      1.1    pooka 
     45  1.4.2.1    skrll #include <net/radix.h>
     46  1.4.2.1    skrll 
     47      1.1    pooka #include <netinet/in.h>
     48      1.1    pooka #include <netinet/in_systm.h>
     49      1.1    pooka #include <netinet/ip.h>
     50      1.1    pooka 
     51      1.1    pooka #include <rump/rumpuser.h>
     52      1.1    pooka 
     53  1.4.2.2    skrll #include "rump_private.h"
     54  1.4.2.2    skrll 
     55      1.1    pooka /*
     56      1.1    pooka  * An inet communication domain which uses the socket interface.
     57      1.1    pooka  * Currently supports only IPv4 UDP, but could easily be extended to
     58      1.1    pooka  * support IPv6 and TCP by adding more stuff to the protosw.
     59      1.1    pooka  */
     60      1.1    pooka 
     61      1.1    pooka DOMAIN_DEFINE(sockindomain);
     62      1.1    pooka 
     63      1.1    pooka static void	sockin_init(void);
     64      1.1    pooka static int	sockin_usrreq(struct socket *, int, struct mbuf *,
     65      1.1    pooka 			      struct mbuf *, struct mbuf *, struct lwp *);
     66  1.4.2.1    skrll static int	sockin_ctloutput(int op, struct socket *, struct sockopt *);
     67      1.1    pooka 
     68      1.1    pooka const struct protosw sockinsw[] = {
     69      1.2    pooka {
     70      1.2    pooka 	.pr_type = SOCK_DGRAM,
     71      1.1    pooka 	.pr_domain = &sockindomain,
     72      1.1    pooka 	.pr_protocol = IPPROTO_UDP,
     73      1.1    pooka 	.pr_flags = PR_ATOMIC|PR_ADDR,
     74      1.1    pooka 	.pr_usrreq = sockin_usrreq,
     75  1.4.2.1    skrll 	.pr_ctloutput = sockin_ctloutput,
     76      1.2    pooka },
     77      1.2    pooka {
     78      1.2    pooka 	.pr_type = SOCK_STREAM,
     79      1.2    pooka 	.pr_domain = &sockindomain,
     80      1.2    pooka 	.pr_protocol = IPPROTO_TCP,
     81      1.2    pooka 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS,
     82      1.2    pooka 	.pr_usrreq = sockin_usrreq,
     83  1.4.2.1    skrll 	.pr_ctloutput = sockin_ctloutput,
     84      1.1    pooka }};
     85      1.1    pooka 
     86      1.1    pooka struct domain sockindomain = {
     87      1.1    pooka 	.dom_family = PF_INET,
     88      1.1    pooka 	.dom_name = "socket_inet",
     89      1.2    pooka 	.dom_init = sockin_init,
     90      1.1    pooka 	.dom_externalize = NULL,
     91      1.1    pooka 	.dom_dispose = NULL,
     92      1.1    pooka 	.dom_protosw = sockinsw,
     93      1.4  minskim 	.dom_protoswNPROTOSW = &sockinsw[__arraycount(sockinsw)],
     94  1.4.2.1    skrll 	.dom_rtattach = rn_inithead,
     95  1.4.2.1    skrll 	.dom_rtoffset = 32,
     96  1.4.2.1    skrll 	.dom_maxrtkey = sizeof(struct sockaddr_in),
     97      1.1    pooka 	.dom_ifattach = NULL,
     98      1.1    pooka 	.dom_ifdetach = NULL,
     99      1.1    pooka 	.dom_ifqueues = { NULL },
    100      1.1    pooka 	.dom_link = { NULL },
    101      1.1    pooka 	.dom_mowner = MOWNER_INIT("",""),
    102      1.1    pooka 	.dom_rtcache = { NULL },
    103      1.1    pooka 	.dom_sockaddr_cmp = NULL
    104      1.1    pooka };
    105      1.1    pooka 
    106      1.3    pooka #define SO2S(so) ((intptr_t)(so->so_internal))
    107      1.2    pooka #define SOCKIN_SBSIZE 65536
    108      1.1    pooka 
    109  1.4.2.1    skrll struct sockin_unit {
    110  1.4.2.1    skrll 	struct socket *su_so;
    111  1.4.2.1    skrll 
    112  1.4.2.1    skrll 	LIST_ENTRY(sockin_unit) su_entries;
    113  1.4.2.1    skrll };
    114  1.4.2.1    skrll static LIST_HEAD(, sockin_unit) su_ent = LIST_HEAD_INITIALIZER(su_ent);
    115  1.4.2.1    skrll static kmutex_t su_mtx;
    116  1.4.2.1    skrll static bool rebuild;
    117  1.4.2.1    skrll static int nsock;
    118  1.4.2.1    skrll 
    119  1.4.2.1    skrll static int
    120  1.4.2.1    skrll registersock(struct socket *so, int news)
    121  1.4.2.1    skrll {
    122  1.4.2.1    skrll 	struct sockin_unit *su;
    123  1.4.2.1    skrll 
    124  1.4.2.1    skrll 	su = kmem_alloc(sizeof(*su), KM_NOSLEEP);
    125  1.4.2.1    skrll 	if (!su)
    126  1.4.2.1    skrll 		return ENOMEM;
    127  1.4.2.1    skrll 
    128  1.4.2.1    skrll 	so->so_internal = (void *)(intptr_t)news;
    129  1.4.2.1    skrll 	su->su_so = so;
    130  1.4.2.1    skrll 
    131  1.4.2.1    skrll 	mutex_enter(&su_mtx);
    132  1.4.2.1    skrll 	LIST_INSERT_HEAD(&su_ent, su, su_entries);
    133  1.4.2.1    skrll 	nsock++;
    134  1.4.2.1    skrll 	rebuild = true;
    135  1.4.2.1    skrll 	mutex_exit(&su_mtx);
    136  1.4.2.1    skrll 
    137  1.4.2.1    skrll 	return 0;
    138  1.4.2.1    skrll }
    139  1.4.2.1    skrll 
    140      1.1    pooka static void
    141  1.4.2.2    skrll removesock(struct socket *so)
    142  1.4.2.2    skrll {
    143  1.4.2.2    skrll 	struct sockin_unit *su_iter;
    144  1.4.2.2    skrll 	int error;
    145  1.4.2.2    skrll 
    146  1.4.2.2    skrll 	mutex_enter(&su_mtx);
    147  1.4.2.2    skrll 	LIST_FOREACH(su_iter, &su_ent, su_entries) {
    148  1.4.2.2    skrll 		if (su_iter->su_so == so)
    149  1.4.2.2    skrll 			break;
    150  1.4.2.2    skrll 	}
    151  1.4.2.2    skrll 	if (!su_iter)
    152  1.4.2.2    skrll 		panic("no such socket");
    153  1.4.2.2    skrll 
    154  1.4.2.2    skrll 	LIST_REMOVE(su_iter, su_entries);
    155  1.4.2.2    skrll 	nsock--;
    156  1.4.2.2    skrll 	rebuild = true;
    157  1.4.2.2    skrll 	mutex_exit(&su_mtx);
    158  1.4.2.2    skrll 
    159  1.4.2.2    skrll 	rumpuser_close(SO2S(su_iter->su_so), &error);
    160  1.4.2.2    skrll 	kmem_free(su_iter, sizeof(*su_iter));
    161  1.4.2.2    skrll }
    162  1.4.2.2    skrll 
    163  1.4.2.2    skrll static void
    164      1.1    pooka sockin_process(struct socket *so)
    165      1.1    pooka {
    166      1.1    pooka 	struct sockaddr_in from;
    167      1.1    pooka 	struct iovec io;
    168      1.1    pooka 	struct msghdr rmsg;
    169      1.1    pooka 	struct mbuf *m;
    170      1.1    pooka 	ssize_t n;
    171      1.1    pooka 	size_t plen;
    172      1.1    pooka 	int error;
    173      1.1    pooka 
    174      1.1    pooka 	m = m_gethdr(M_WAIT, MT_DATA);
    175  1.4.2.2    skrll 	if (so->so_proto->pr_type == SOCK_DGRAM) {
    176  1.4.2.2    skrll 		plen = IP_MAXPACKET;
    177  1.4.2.2    skrll 		MEXTMALLOC(m, plen, M_DONTWAIT);
    178  1.4.2.2    skrll 	} else {
    179  1.4.2.2    skrll 		plen = MCLBYTES;
    180  1.4.2.2    skrll 		MCLGET(m, M_DONTWAIT);
    181  1.4.2.2    skrll 	}
    182  1.4.2.2    skrll 	if ((m->m_flags & M_EXT) == 0) {
    183  1.4.2.2    skrll 		m_freem(m);
    184  1.4.2.2    skrll 		return;
    185  1.4.2.2    skrll 	}
    186      1.1    pooka 
    187      1.1    pooka 	memset(&rmsg, 0, sizeof(rmsg));
    188      1.1    pooka 	io.iov_base = mtod(m, void *);
    189      1.1    pooka 	io.iov_len = plen;
    190      1.1    pooka 	rmsg.msg_iov = &io;
    191      1.1    pooka 	rmsg.msg_iovlen = 1;
    192      1.1    pooka 	rmsg.msg_name = (struct sockaddr *)&from;
    193      1.1    pooka 	rmsg.msg_namelen = sizeof(from);
    194      1.1    pooka 
    195      1.1    pooka 	n = rumpuser_net_recvmsg(SO2S(so), &rmsg, 0, &error);
    196      1.1    pooka 	if (n <= 0) {
    197      1.1    pooka 		m_freem(m);
    198  1.4.2.2    skrll 
    199  1.4.2.2    skrll 		/* Treat a TCP socket a goner */
    200  1.4.2.2    skrll 		if (so->so_proto->pr_type == SOCK_STREAM && error != EAGAIN) {
    201  1.4.2.2    skrll 			mutex_enter(softnet_lock);
    202  1.4.2.2    skrll 			soisdisconnected(so);
    203  1.4.2.2    skrll 			mutex_exit(softnet_lock);
    204  1.4.2.2    skrll 			removesock(so);
    205  1.4.2.2    skrll 		}
    206      1.1    pooka 		return;
    207      1.1    pooka 	}
    208      1.1    pooka 	m->m_len = m->m_pkthdr.len = n;
    209      1.1    pooka 
    210  1.4.2.2    skrll 	mutex_enter(softnet_lock);
    211      1.2    pooka 	if (so->so_proto->pr_type == SOCK_DGRAM) {
    212      1.2    pooka 		if (!sbappendaddr(&so->so_rcv, rmsg.msg_name, m, NULL)) {
    213      1.2    pooka 			m_freem(m);
    214      1.2    pooka 		}
    215      1.2    pooka 	} else {
    216      1.2    pooka 		sbappendstream(&so->so_rcv, m);
    217      1.1    pooka 	}
    218      1.2    pooka 
    219      1.1    pooka 	sorwakeup(so);
    220  1.4.2.2    skrll 	mutex_exit(softnet_lock);
    221      1.1    pooka }
    222      1.1    pooka 
    223  1.4.2.1    skrll static void
    224  1.4.2.1    skrll sockin_accept(struct socket *so)
    225  1.4.2.1    skrll {
    226  1.4.2.1    skrll 	struct socket *nso;
    227  1.4.2.1    skrll 	struct sockaddr_in sin;
    228  1.4.2.1    skrll 	int news, error, slen;
    229  1.4.2.1    skrll 
    230  1.4.2.1    skrll 	slen = sizeof(sin);
    231  1.4.2.1    skrll 	news = rumpuser_net_accept(SO2S(so), (struct sockaddr *)&sin,
    232  1.4.2.1    skrll 	    &slen, &error);
    233  1.4.2.1    skrll 	if (news == -1)
    234  1.4.2.1    skrll 		return;
    235      1.1    pooka 
    236  1.4.2.2    skrll 	mutex_enter(softnet_lock);
    237  1.4.2.2    skrll 	nso = sonewconn(so, SS_ISCONNECTED);
    238  1.4.2.2    skrll 	if (nso == NULL)
    239  1.4.2.1    skrll 		goto errout;
    240  1.4.2.1    skrll 	if (registersock(nso, news) != 0)
    241  1.4.2.1    skrll 		goto errout;
    242  1.4.2.2    skrll 	mutex_exit(softnet_lock);
    243  1.4.2.1    skrll 	return;
    244  1.4.2.1    skrll 
    245  1.4.2.1    skrll  errout:
    246  1.4.2.1    skrll 	rumpuser_close(news, &error);
    247  1.4.2.1    skrll 	if (nso)
    248  1.4.2.1    skrll 		soclose(nso);
    249  1.4.2.2    skrll 	mutex_exit(softnet_lock);
    250  1.4.2.1    skrll }
    251      1.1    pooka 
    252      1.1    pooka #define POLLTIMEOUT 100	/* check for new entries every 100ms */
    253      1.1    pooka 
    254      1.1    pooka /* XXX: doesn't handle socket (kernel) locking properly? */
    255      1.1    pooka static void
    256      1.1    pooka sockinworker(void *arg)
    257      1.1    pooka {
    258      1.1    pooka 	struct pollfd *pfds = NULL, *npfds;
    259      1.1    pooka 	struct sockin_unit *su_iter;
    260  1.4.2.1    skrll 	struct socket *so;
    261      1.1    pooka 	int cursock = 0, i, rv, error;
    262      1.1    pooka 
    263      1.1    pooka 	/*
    264      1.1    pooka 	 * Loop reading requests.  Check for new sockets periodically
    265      1.1    pooka 	 * (could be smarter, but I'm lazy).
    266      1.1    pooka 	 */
    267      1.1    pooka 	for (;;) {
    268      1.1    pooka 		if (rebuild) {
    269      1.1    pooka 			npfds = NULL;
    270      1.1    pooka 			mutex_enter(&su_mtx);
    271      1.1    pooka 			if (nsock)
    272      1.1    pooka 				npfds = kmem_alloc(nsock * sizeof(*npfds),
    273      1.1    pooka 				    KM_NOSLEEP);
    274      1.1    pooka 			if (npfds || nsock == 0) {
    275      1.1    pooka 				if (pfds)
    276      1.1    pooka 					kmem_free(pfds, cursock*sizeof(*pfds));
    277      1.1    pooka 				pfds = npfds;
    278      1.1    pooka 				cursock = nsock;
    279      1.1    pooka 				rebuild = false;
    280      1.1    pooka 
    281      1.1    pooka 				i = 0;
    282      1.1    pooka 				LIST_FOREACH(su_iter, &su_ent, su_entries) {
    283      1.1    pooka 					pfds[i].fd = SO2S(su_iter->su_so);
    284      1.1    pooka 					pfds[i].events = POLLIN;
    285      1.1    pooka 					pfds[i].revents = 0;
    286      1.1    pooka 					i++;
    287      1.1    pooka 				}
    288      1.1    pooka 				KASSERT(i == nsock);
    289      1.1    pooka 			}
    290      1.1    pooka 			mutex_exit(&su_mtx);
    291      1.1    pooka 		}
    292      1.1    pooka 
    293      1.1    pooka 		/* find affected sockets & process */
    294      1.1    pooka 		rv = rumpuser_poll(pfds, cursock, POLLTIMEOUT, &error);
    295      1.1    pooka 		for (i = 0; i < cursock && rv > 0; i++) {
    296      1.1    pooka 			if (pfds[i].revents & POLLIN) {
    297      1.1    pooka 				mutex_enter(&su_mtx);
    298      1.1    pooka 				LIST_FOREACH(su_iter, &su_ent, su_entries) {
    299      1.1    pooka 					if (SO2S(su_iter->su_so)==pfds[i].fd) {
    300  1.4.2.1    skrll 						so = su_iter->su_so;
    301  1.4.2.1    skrll 						mutex_exit(&su_mtx);
    302  1.4.2.1    skrll 						if(so->so_options&SO_ACCEPTCONN)
    303  1.4.2.1    skrll 							sockin_accept(so);
    304  1.4.2.1    skrll 						else
    305  1.4.2.1    skrll 							sockin_process(so);
    306  1.4.2.1    skrll 						mutex_enter(&su_mtx);
    307      1.1    pooka 						break;
    308      1.1    pooka 					}
    309      1.1    pooka 				}
    310      1.1    pooka 				/* if we can't find it, just wing it */
    311      1.1    pooka 				KASSERT(rebuild || su_iter);
    312      1.1    pooka 				mutex_exit(&su_mtx);
    313      1.1    pooka 				pfds[i].revents = 0;
    314      1.1    pooka 				rv--;
    315      1.1    pooka 				i = -1;
    316      1.1    pooka 				continue;
    317      1.1    pooka 			}
    318      1.1    pooka 
    319      1.1    pooka 			/* something else?  ignore */
    320      1.1    pooka 			if (pfds[i].revents) {
    321      1.1    pooka 				pfds[i].revents = 0;
    322      1.1    pooka 				rv--;
    323      1.1    pooka 			}
    324      1.1    pooka 		}
    325      1.1    pooka 		KASSERT(rv <= 0);
    326      1.1    pooka 	}
    327      1.1    pooka 
    328      1.1    pooka }
    329      1.1    pooka 
    330      1.1    pooka static void
    331  1.4.2.3    skrll sockin_init(void)
    332      1.1    pooka {
    333      1.1    pooka 	int rv;
    334      1.1    pooka 
    335  1.4.2.2    skrll 	if (rump_threads) {
    336  1.4.2.2    skrll 		if ((rv = kthread_create(PRI_NONE, 0, NULL, sockinworker,
    337  1.4.2.2    skrll 		    NULL, NULL, "sockwork")) != 0)
    338  1.4.2.2    skrll 			panic("sockin_init: could not create worker thread\n");
    339  1.4.2.2    skrll 	} else {
    340  1.4.2.2    skrll 		printf("sockin_init: no threads => no worker thread\n");
    341  1.4.2.2    skrll 	}
    342      1.1    pooka 	mutex_init(&su_mtx, MUTEX_DEFAULT, IPL_NONE);
    343      1.1    pooka }
    344      1.1    pooka 
    345      1.1    pooka static int
    346      1.1    pooka sockin_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    347      1.1    pooka 	struct mbuf *control, struct lwp *l)
    348      1.1    pooka {
    349      1.2    pooka 	int error = 0, rv;
    350      1.1    pooka 
    351      1.1    pooka 	switch (req) {
    352      1.1    pooka 	case PRU_ATTACH:
    353      1.1    pooka 	{
    354  1.4.2.2    skrll 		int news, dummy;
    355      1.1    pooka 
    356      1.1    pooka 		sosetlock(so);
    357      1.2    pooka 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    358      1.2    pooka 			error = soreserve(so, SOCKIN_SBSIZE, SOCKIN_SBSIZE);
    359      1.2    pooka 			if (error)
    360      1.2    pooka 				break;
    361      1.2    pooka 		}
    362      1.1    pooka 
    363      1.2    pooka 		news = rumpuser_net_socket(PF_INET, so->so_proto->pr_type,
    364      1.2    pooka 		    0, &error);
    365  1.4.2.1    skrll 		if (news == -1)
    366      1.1    pooka 			break;
    367      1.1    pooka 
    368  1.4.2.1    skrll 		if ((error = registersock(so, news)) != 0)
    369  1.4.2.2    skrll 			rumpuser_close(news, &dummy);
    370  1.4.2.1    skrll 
    371      1.1    pooka 		break;
    372      1.1    pooka 	}
    373      1.1    pooka 
    374  1.4.2.1    skrll 	case PRU_ACCEPT:
    375  1.4.2.1    skrll 		/* we do all the work in the worker thread */
    376  1.4.2.1    skrll 		break;
    377  1.4.2.1    skrll 
    378  1.4.2.1    skrll 	case PRU_BIND:
    379  1.4.2.1    skrll 		rumpuser_net_bind(SO2S(so), mtod(nam, const struct sockaddr *),
    380  1.4.2.1    skrll 		    sizeof(struct sockaddr_in), &error);
    381  1.4.2.1    skrll 		break;
    382  1.4.2.1    skrll 
    383      1.1    pooka 	case PRU_CONNECT:
    384      1.3    pooka 		rv = rumpuser_net_connect(SO2S(so),
    385      1.2    pooka 		    mtod(nam, struct sockaddr *), sizeof(struct sockaddr_in),
    386      1.2    pooka 		    &error);
    387      1.2    pooka 		if (rv == 0)
    388  1.4.2.2    skrll 			soisconnected(so);
    389      1.1    pooka 		break;
    390      1.1    pooka 
    391  1.4.2.1    skrll 	case PRU_LISTEN:
    392  1.4.2.1    skrll 		rumpuser_net_listen(SO2S(so), so->so_qlimit, &error);
    393  1.4.2.1    skrll 		break;
    394  1.4.2.1    skrll 
    395      1.1    pooka 	case PRU_SEND:
    396      1.1    pooka 	{
    397      1.1    pooka 		struct sockaddr *saddr;
    398      1.1    pooka 		struct msghdr mhdr;
    399      1.1    pooka 		struct iovec iov[16];
    400      1.1    pooka 		struct mbuf *m2;
    401      1.1    pooka 		size_t tot;
    402      1.1    pooka 		int i, s;
    403      1.1    pooka 
    404      1.1    pooka 		memset(&mhdr, 0, sizeof(mhdr));
    405      1.1    pooka 
    406      1.1    pooka 		tot = 0;
    407      1.1    pooka 		for (i = 0, m2 = m; m2; m2 = m2->m_next, i++) {
    408      1.1    pooka 			if (i > 16)
    409      1.1    pooka 				panic("lazy bum");
    410      1.1    pooka 			iov[i].iov_base = m2->m_data;
    411      1.1    pooka 			iov[i].iov_len = m2->m_len;
    412      1.1    pooka 			tot += m2->m_len;
    413      1.1    pooka 
    414      1.1    pooka 		}
    415      1.1    pooka 		mhdr.msg_iov = iov;
    416      1.1    pooka 		mhdr.msg_iovlen = i;
    417      1.3    pooka 		s = SO2S(so);
    418      1.2    pooka 
    419  1.4.2.2    skrll 		if (nam) {
    420      1.2    pooka 			saddr = mtod(nam, struct sockaddr *);
    421      1.2    pooka 			mhdr.msg_name = saddr;
    422      1.2    pooka 			mhdr.msg_namelen = saddr->sa_len;
    423      1.2    pooka 		}
    424      1.1    pooka 
    425      1.1    pooka 		rumpuser_net_sendmsg(s, &mhdr, 0, &error);
    426      1.2    pooka 
    427      1.1    pooka 		m_freem(m);
    428      1.1    pooka 		m_freem(control);
    429  1.4.2.2    skrll 
    430      1.1    pooka 		/* this assumes too many things to list.. buthey, testing */
    431  1.4.2.2    skrll 		if (!rump_threads)
    432  1.4.2.2    skrll 			sockin_process(so);
    433      1.1    pooka 	}
    434      1.1    pooka 		break;
    435      1.1    pooka 
    436      1.1    pooka 	case PRU_SHUTDOWN:
    437  1.4.2.2    skrll 		removesock(so);
    438  1.4.2.2    skrll 		break;
    439      1.1    pooka 
    440  1.4.2.2    skrll 	case PRU_SOCKADDR:
    441  1.4.2.2    skrll 	case PRU_PEERADDR:
    442  1.4.2.2    skrll 	{
    443  1.4.2.2    skrll 		int slen = nam->m_len;
    444  1.4.2.2    skrll 		enum rumpuser_getnametype which;
    445      1.1    pooka 
    446  1.4.2.2    skrll 		if (req == PRU_SOCKADDR)
    447  1.4.2.2    skrll 			which = RUMPUSER_SOCKNAME;
    448  1.4.2.2    skrll 		else
    449  1.4.2.2    skrll 			which = RUMPUSER_PEERNAME;
    450  1.4.2.2    skrll 		rumpuser_net_getname(SO2S(so),
    451  1.4.2.2    skrll 		    mtod(nam, struct sockaddr *), &slen, which, &error);
    452  1.4.2.2    skrll 		if (error == 0)
    453  1.4.2.2    skrll 			nam->m_len = slen;
    454      1.1    pooka 		break;
    455  1.4.2.2    skrll 	}
    456      1.1    pooka 
    457      1.1    pooka 	default:
    458      1.1    pooka 		panic("sockin_usrreq: IMPLEMENT ME, req %d not supported", req);
    459      1.1    pooka 	}
    460      1.1    pooka 
    461      1.1    pooka 	return error;
    462      1.1    pooka }
    463  1.4.2.1    skrll 
    464  1.4.2.1    skrll static int
    465  1.4.2.1    skrll sockin_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    466  1.4.2.1    skrll {
    467  1.4.2.1    skrll 
    468  1.4.2.1    skrll 	/* XXX: we should also do something here */
    469  1.4.2.1    skrll 	return 0;
    470  1.4.2.1    skrll }
    471