Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.115.6.2
      1  1.115.6.2  jmcneill /*	$NetBSD: uipc_syscalls.c,v 1.115.6.2 2007/09/03 16:48:51 jmcneill Exp $	*/
      2        1.8       cgd 
      3        1.1       cgd /*
      4        1.7   mycroft  * Copyright (c) 1982, 1986, 1989, 1990, 1993
      5        1.7   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.82       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  *
     31       1.29      fvdl  *	@(#)uipc_syscalls.c	8.6 (Berkeley) 2/14/95
     32        1.1       cgd  */
     33       1.67     lukem 
     34       1.67     lukem #include <sys/cdefs.h>
     35  1.115.6.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.115.6.2 2007/09/03 16:48:51 jmcneill Exp $");
     36       1.31   thorpej 
     37       1.68  jdolecek #include "opt_pipe.h"
     38       1.55  jdolecek 
     39        1.6   mycroft #include <sys/param.h>
     40        1.9       cgd #include <sys/systm.h>
     41        1.6   mycroft #include <sys/filedesc.h>
     42        1.6   mycroft #include <sys/proc.h>
     43        1.6   mycroft #include <sys/file.h>
     44        1.6   mycroft #include <sys/buf.h>
     45        1.6   mycroft #include <sys/malloc.h>
     46        1.6   mycroft #include <sys/mbuf.h>
     47        1.6   mycroft #include <sys/protosw.h>
     48        1.6   mycroft #include <sys/socket.h>
     49        1.6   mycroft #include <sys/socketvar.h>
     50       1.18  christos #include <sys/signalvar.h>
     51       1.18  christos #include <sys/un.h>
     52        1.6   mycroft #include <sys/ktrace.h>
     53       1.71  jdolecek #include <sys/event.h>
     54        1.1       cgd 
     55        1.9       cgd #include <sys/mount.h>
     56        1.9       cgd #include <sys/syscallargs.h>
     57        1.9       cgd 
     58       1.44   darrenr #include <uvm/uvm_extern.h>
     59       1.44   darrenr 
     60        1.1       cgd /*
     61        1.1       cgd  * System call interface to the socket abstraction.
     62        1.1       cgd  */
     63       1.89  christos extern const struct fileops socketops;
     64        1.1       cgd 
     65        1.7   mycroft int
     66      1.100       mrg sys___socket30(struct lwp *l, void *v, register_t *retval)
     67       1.15   thorpej {
     68      1.100       mrg 	struct sys___socket30_args /* {
     69       1.57     lukem 		syscallarg(int)	domain;
     70       1.57     lukem 		syscallarg(int)	type;
     71       1.57     lukem 		syscallarg(int)	protocol;
     72       1.15   thorpej 	} */ *uap = v;
     73       1.75   thorpej 
     74       1.57     lukem 	struct filedesc	*fdp;
     75       1.57     lukem 	struct socket	*so;
     76       1.57     lukem 	struct file	*fp;
     77       1.57     lukem 	int		fd, error;
     78        1.1       cgd 
     79      1.101        ad 	fdp = l->l_proc->p_fd;
     80       1.43   thorpej 	/* falloc() will use the desciptor for us */
     81      1.101        ad 	if ((error = falloc(l, &fp, &fd)) != 0)
     82        1.1       cgd 		return (error);
     83        1.1       cgd 	fp->f_flag = FREAD|FWRITE;
     84        1.1       cgd 	fp->f_type = DTYPE_SOCKET;
     85        1.1       cgd 	fp->f_ops = &socketops;
     86       1.18  christos 	error = socreate(SCARG(uap, domain), &so, SCARG(uap, type),
     87       1.95  christos 			 SCARG(uap, protocol), l);
     88       1.18  christos 	if (error) {
     89       1.95  christos 		FILE_UNUSE(fp, l);
     90       1.50   thorpej 		fdremove(fdp, fd);
     91        1.1       cgd 		ffree(fp);
     92        1.1       cgd 	} else {
     93      1.110       dsl 		fp->f_data = so;
     94       1.59   thorpej 		FILE_SET_MATURE(fp);
     95       1.95  christos 		FILE_UNUSE(fp, l);
     96        1.1       cgd 		*retval = fd;
     97        1.1       cgd 	}
     98        1.1       cgd 	return (error);
     99        1.1       cgd }
    100        1.1       cgd 
    101        1.1       cgd /* ARGSUSED */
    102        1.7   mycroft int
    103      1.105      yamt sys_bind(struct lwp *l, void *v, register_t *retval)
    104       1.15   thorpej {
    105       1.51  augustss 	struct sys_bind_args /* {
    106       1.57     lukem 		syscallarg(int)				s;
    107       1.57     lukem 		syscallarg(const struct sockaddr *)	name;
    108       1.57     lukem 		syscallarg(unsigned int)		namelen;
    109       1.15   thorpej 	} */ *uap = v;
    110      1.111       dsl 	struct mbuf	*nam;
    111      1.111       dsl 	int		error;
    112      1.111       dsl 
    113      1.112     enami 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    114      1.112     enami 	    MT_SONAME);
    115      1.111       dsl 	if (error)
    116      1.111       dsl 		return error;
    117      1.111       dsl 
    118      1.111       dsl 	return do_sys_bind(l, SCARG(uap, s), nam);
    119      1.111       dsl }
    120      1.111       dsl 
    121      1.111       dsl int
    122      1.111       dsl do_sys_bind(struct lwp *l, int s, struct mbuf *nam)
    123      1.111       dsl {
    124       1.57     lukem 	struct file	*fp;
    125       1.57     lukem 	int		error;
    126        1.1       cgd 
    127       1.43   thorpej 	/* getsock() will use the descriptor for us */
    128      1.111       dsl 	if ((error = getsock(l->l_proc->p_fd, s, &fp)) != 0) {
    129      1.111       dsl 		m_freem(nam);
    130        1.1       cgd 		return (error);
    131       1.43   thorpej 	}
    132       1.76      matt 	MCLAIM(nam, ((struct socket *)fp->f_data)->so_mowner);
    133      1.111       dsl 	error = sobind(fp->f_data, nam, l);
    134        1.1       cgd 	m_freem(nam);
    135       1.95  christos 	FILE_UNUSE(fp, l);
    136      1.111       dsl 	return error;
    137        1.1       cgd }
    138        1.1       cgd 
    139        1.1       cgd /* ARGSUSED */
    140        1.7   mycroft int
    141      1.105      yamt sys_listen(struct lwp *l, void *v, register_t *retval)
    142       1.15   thorpej {
    143       1.51  augustss 	struct sys_listen_args /* {
    144       1.57     lukem 		syscallarg(int)	s;
    145       1.57     lukem 		syscallarg(int)	backlog;
    146       1.15   thorpej 	} */ *uap = v;
    147       1.57     lukem 	struct file	*fp;
    148       1.57     lukem 	int		error;
    149        1.1       cgd 
    150       1.43   thorpej 	/* getsock() will use the descriptor for us */
    151      1.111       dsl 	if ((error = getsock(l->l_proc->p_fd, SCARG(uap, s), &fp)) != 0)
    152        1.1       cgd 		return (error);
    153      1.111       dsl 	error = solisten(fp->f_data, SCARG(uap, backlog));
    154       1.95  christos 	FILE_UNUSE(fp, l);
    155      1.111       dsl 	return error;
    156        1.1       cgd }
    157        1.1       cgd 
    158        1.7   mycroft int
    159      1.113       dsl do_sys_accept(struct lwp *l, int sock, struct mbuf **name, register_t *new_sock)
    160       1.15   thorpej {
    161       1.57     lukem 	struct filedesc	*fdp;
    162       1.57     lukem 	struct file	*fp;
    163       1.57     lukem 	struct mbuf	*nam;
    164       1.57     lukem 	int		error, s, fd;
    165       1.57     lukem 	struct socket	*so;
    166       1.79  christos 	int		fflag;
    167        1.1       cgd 
    168      1.101        ad 	fdp = l->l_proc->p_fd;
    169       1.44   darrenr 
    170       1.43   thorpej 	/* getsock() will use the descriptor for us */
    171      1.113       dsl 	if ((error = getsock(fdp, sock, &fp)) != 0)
    172        1.1       cgd 		return (error);
    173       1.14   mycroft 	s = splsoftnet();
    174        1.1       cgd 	so = (struct socket *)fp->f_data;
    175       1.95  christos 	FILE_UNUSE(fp, l);
    176       1.44   darrenr 	if (!(so->so_proto->pr_flags & PR_LISTEN)) {
    177       1.44   darrenr 		splx(s);
    178       1.44   darrenr 		return (EOPNOTSUPP);
    179       1.44   darrenr 	}
    180        1.1       cgd 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    181        1.1       cgd 		splx(s);
    182        1.1       cgd 		return (EINVAL);
    183        1.1       cgd 	}
    184        1.1       cgd 	if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
    185        1.1       cgd 		splx(s);
    186        1.1       cgd 		return (EWOULDBLOCK);
    187        1.1       cgd 	}
    188        1.1       cgd 	while (so->so_qlen == 0 && so->so_error == 0) {
    189        1.1       cgd 		if (so->so_state & SS_CANTRCVMORE) {
    190        1.1       cgd 			so->so_error = ECONNABORTED;
    191        1.1       cgd 			break;
    192        1.1       cgd 		}
    193      1.110       dsl 		error = tsleep(&so->so_timeo, PSOCK | PCATCH,
    194      1.101        ad 		    netcon, 0);
    195       1.18  christos 		if (error) {
    196        1.1       cgd 			splx(s);
    197        1.1       cgd 			return (error);
    198        1.1       cgd 		}
    199        1.1       cgd 	}
    200        1.1       cgd 	if (so->so_error) {
    201        1.1       cgd 		error = so->so_error;
    202        1.1       cgd 		so->so_error = 0;
    203        1.1       cgd 		splx(s);
    204        1.1       cgd 		return (error);
    205        1.1       cgd 	}
    206       1.79  christos 	fflag = fp->f_flag;
    207       1.43   thorpej 	/* falloc() will use the descriptor for us */
    208      1.101        ad 	if ((error = falloc(l, &fp, &fd)) != 0) {
    209        1.1       cgd 		splx(s);
    210        1.1       cgd 		return (error);
    211        1.1       cgd 	}
    212      1.113       dsl 	*new_sock = fd;
    213       1.71  jdolecek 
    214       1.71  jdolecek 	/* connection has been removed from the listen queue */
    215       1.74  christos 	KNOTE(&so->so_rcv.sb_sel.sel_klist, 0);
    216       1.71  jdolecek 
    217       1.70      matt 	{ struct socket *aso = TAILQ_FIRST(&so->so_q);
    218        1.1       cgd 	  if (soqremque(aso, 1) == 0)
    219        1.1       cgd 		panic("accept");
    220        1.1       cgd 	  so = aso;
    221        1.1       cgd 	}
    222        1.1       cgd 	fp->f_type = DTYPE_SOCKET;
    223       1.79  christos 	fp->f_flag = fflag;
    224        1.1       cgd 	fp->f_ops = &socketops;
    225      1.110       dsl 	fp->f_data = so;
    226        1.1       cgd 	nam = m_get(M_WAIT, MT_SONAME);
    227      1.113       dsl 	error = soaccept(so, nam);
    228      1.113       dsl 
    229       1.49   mycroft 	if (error) {
    230      1.113       dsl 		/* an error occurred, free the file descriptor and mbuf */
    231      1.113       dsl 		m_freem(nam);
    232       1.50   thorpej 		fdremove(fdp, fd);
    233      1.108      yamt 		closef(fp, l);
    234       1.99  christos 	} else {
    235       1.99  christos 		FILE_SET_MATURE(fp);
    236      1.109      yamt 		FILE_UNUSE(fp, l);
    237      1.113       dsl 		*name = nam;
    238       1.49   mycroft 	}
    239       1.46   darrenr 	splx(s);
    240       1.46   darrenr 	return (error);
    241        1.1       cgd }
    242        1.1       cgd 
    243      1.113       dsl int
    244      1.113       dsl sys_accept(struct lwp *l, void *v, register_t *retval)
    245      1.113       dsl {
    246      1.113       dsl 	struct sys_accept_args /* {
    247      1.113       dsl 		syscallarg(int)			s;
    248      1.113       dsl 		syscallarg(struct sockaddr *)	name;
    249      1.113       dsl 		syscallarg(unsigned int *)	anamelen;
    250      1.113       dsl 	} */ *uap = v;
    251      1.113       dsl 	int error;
    252      1.113       dsl 	struct mbuf *name;
    253      1.113       dsl 
    254      1.113       dsl 	error = do_sys_accept(l, SCARG(uap, s), &name, retval);
    255      1.113       dsl 	if (error != 0)
    256      1.113       dsl 		return error;
    257      1.113       dsl 
    258      1.113       dsl 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
    259      1.113       dsl 	    MSG_LENUSRSPACE, name);
    260      1.113       dsl 	if (name != NULL)
    261      1.113       dsl 		m_free(name);
    262      1.113       dsl 	if (error != 0)
    263      1.113       dsl 		fdrelease(l, *retval);
    264      1.113       dsl 	return error;
    265      1.113       dsl }
    266      1.113       dsl 
    267        1.1       cgd /* ARGSUSED */
    268        1.7   mycroft int
    269      1.105      yamt sys_connect(struct lwp *l, void *v, register_t *retval)
    270       1.15   thorpej {
    271       1.51  augustss 	struct sys_connect_args /* {
    272       1.57     lukem 		syscallarg(int)				s;
    273       1.57     lukem 		syscallarg(const struct sockaddr *)	name;
    274       1.57     lukem 		syscallarg(unsigned int)		namelen;
    275       1.15   thorpej 	} */ *uap = v;
    276      1.111       dsl 	int		error;
    277      1.111       dsl 	struct mbuf	*nam;
    278      1.111       dsl 
    279      1.112     enami 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    280      1.112     enami 	    MT_SONAME);
    281      1.111       dsl 	if (error)
    282      1.111       dsl 		return error;
    283      1.111       dsl 	return do_sys_connect(l,  SCARG(uap, s), nam);
    284      1.111       dsl }
    285      1.111       dsl 
    286      1.111       dsl int
    287      1.111       dsl do_sys_connect(struct lwp *l, int s, struct mbuf *nam)
    288      1.111       dsl {
    289       1.57     lukem 	struct file	*fp;
    290       1.57     lukem 	struct socket	*so;
    291      1.111       dsl 	int		error;
    292       1.87     ragge 	int		interrupted = 0;
    293        1.1       cgd 
    294       1.43   thorpej 	/* getsock() will use the descriptor for us */
    295      1.111       dsl 	if ((error = getsock(l->l_proc->p_fd, s, &fp)) != 0) {
    296      1.111       dsl 		m_freem(nam);
    297        1.1       cgd 		return (error);
    298      1.111       dsl 	}
    299      1.111       dsl 	so = fp->f_data;
    300      1.111       dsl 	MCLAIM(nam, so->so_mowner);
    301       1.87     ragge 	if (so->so_state & SS_ISCONNECTING) {
    302       1.62  jdolecek 		error = EALREADY;
    303       1.62  jdolecek 		goto out;
    304       1.62  jdolecek 	}
    305      1.111       dsl 
    306       1.95  christos 	error = soconnect(so, nam, l);
    307        1.1       cgd 	if (error)
    308        1.1       cgd 		goto bad;
    309        1.1       cgd 	if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
    310       1.62  jdolecek 		error = EINPROGRESS;
    311       1.62  jdolecek 		goto out;
    312        1.1       cgd 	}
    313       1.14   mycroft 	s = splsoftnet();
    314       1.18  christos 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
    315      1.110       dsl 		error = tsleep(&so->so_timeo, PSOCK | PCATCH,
    316       1.18  christos 			       netcon, 0);
    317       1.87     ragge 		if (error) {
    318       1.87     ragge 			if (error == EINTR || error == ERESTART)
    319       1.87     ragge 				interrupted = 1;
    320        1.1       cgd 			break;
    321       1.87     ragge 		}
    322       1.18  christos 	}
    323        1.1       cgd 	if (error == 0) {
    324        1.1       cgd 		error = so->so_error;
    325        1.1       cgd 		so->so_error = 0;
    326        1.1       cgd 	}
    327        1.1       cgd 	splx(s);
    328       1.57     lukem  bad:
    329       1.87     ragge 	if (!interrupted)
    330       1.87     ragge 		so->so_state &= ~SS_ISCONNECTING;
    331        1.1       cgd 	if (error == ERESTART)
    332        1.1       cgd 		error = EINTR;
    333       1.62  jdolecek  out:
    334       1.95  christos 	FILE_UNUSE(fp, l);
    335      1.111       dsl 	m_freem(nam);
    336        1.1       cgd 	return (error);
    337        1.1       cgd }
    338        1.1       cgd 
    339        1.7   mycroft int
    340      1.105      yamt sys_socketpair(struct lwp *l, void *v, register_t *retval)
    341       1.15   thorpej {
    342       1.51  augustss 	struct sys_socketpair_args /* {
    343       1.57     lukem 		syscallarg(int)		domain;
    344       1.57     lukem 		syscallarg(int)		type;
    345       1.57     lukem 		syscallarg(int)		protocol;
    346       1.57     lukem 		syscallarg(int *)	rsv;
    347       1.15   thorpej 	} */ *uap = v;
    348       1.57     lukem 	struct filedesc	*fdp;
    349       1.57     lukem 	struct file	*fp1, *fp2;
    350       1.57     lukem 	struct socket	*so1, *so2;
    351       1.57     lukem 	int		fd, error, sv[2];
    352        1.1       cgd 
    353      1.101        ad 	fdp = l->l_proc->p_fd;
    354       1.18  christos 	error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
    355      1.101        ad 	    SCARG(uap, protocol), l);
    356       1.18  christos 	if (error)
    357        1.1       cgd 		return (error);
    358       1.18  christos 	error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
    359      1.101        ad 	    SCARG(uap, protocol), l);
    360       1.18  christos 	if (error)
    361        1.1       cgd 		goto free1;
    362       1.43   thorpej 	/* falloc() will use the descriptor for us */
    363      1.101        ad 	if ((error = falloc(l, &fp1, &fd)) != 0)
    364        1.1       cgd 		goto free2;
    365        1.1       cgd 	sv[0] = fd;
    366        1.1       cgd 	fp1->f_flag = FREAD|FWRITE;
    367        1.1       cgd 	fp1->f_type = DTYPE_SOCKET;
    368        1.1       cgd 	fp1->f_ops = &socketops;
    369      1.110       dsl 	fp1->f_data = so1;
    370      1.101        ad 	if ((error = falloc(l, &fp2, &fd)) != 0)
    371        1.1       cgd 		goto free3;
    372        1.1       cgd 	fp2->f_flag = FREAD|FWRITE;
    373        1.1       cgd 	fp2->f_type = DTYPE_SOCKET;
    374        1.1       cgd 	fp2->f_ops = &socketops;
    375      1.110       dsl 	fp2->f_data = so2;
    376        1.1       cgd 	sv[1] = fd;
    377       1.18  christos 	if ((error = soconnect2(so1, so2)) != 0)
    378        1.1       cgd 		goto free4;
    379        1.9       cgd 	if (SCARG(uap, type) == SOCK_DGRAM) {
    380        1.1       cgd 		/*
    381        1.1       cgd 		 * Datagram socket connection is asymmetric.
    382        1.1       cgd 		 */
    383       1.18  christos 		 if ((error = soconnect2(so2, so1)) != 0)
    384        1.1       cgd 			goto free4;
    385        1.1       cgd 	}
    386      1.110       dsl 	error = copyout(sv, SCARG(uap, rsv), 2 * sizeof(int));
    387       1.59   thorpej 	FILE_SET_MATURE(fp1);
    388       1.59   thorpej 	FILE_SET_MATURE(fp2);
    389       1.95  christos 	FILE_UNUSE(fp1, l);
    390       1.95  christos 	FILE_UNUSE(fp2, l);
    391        1.1       cgd 	return (error);
    392       1.57     lukem  free4:
    393       1.95  christos 	FILE_UNUSE(fp2, l);
    394        1.1       cgd 	ffree(fp2);
    395       1.50   thorpej 	fdremove(fdp, sv[1]);
    396       1.57     lukem  free3:
    397       1.95  christos 	FILE_UNUSE(fp1, l);
    398        1.1       cgd 	ffree(fp1);
    399       1.50   thorpej 	fdremove(fdp, sv[0]);
    400       1.57     lukem  free2:
    401        1.1       cgd 	(void)soclose(so2);
    402       1.57     lukem  free1:
    403        1.1       cgd 	(void)soclose(so1);
    404        1.1       cgd 	return (error);
    405        1.1       cgd }
    406        1.1       cgd 
    407        1.7   mycroft int
    408       1.75   thorpej sys_sendto(struct lwp *l, void *v, register_t *retval)
    409       1.15   thorpej {
    410       1.51  augustss 	struct sys_sendto_args /* {
    411       1.57     lukem 		syscallarg(int)				s;
    412       1.57     lukem 		syscallarg(const void *)		buf;
    413       1.57     lukem 		syscallarg(size_t)			len;
    414       1.57     lukem 		syscallarg(int)				flags;
    415       1.57     lukem 		syscallarg(const struct sockaddr *)	to;
    416       1.57     lukem 		syscallarg(unsigned int)		tolen;
    417       1.15   thorpej 	} */ *uap = v;
    418       1.57     lukem 	struct msghdr	msg;
    419       1.57     lukem 	struct iovec	aiov;
    420        1.1       cgd 
    421       1.91  christos 	msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */
    422        1.9       cgd 	msg.msg_namelen = SCARG(uap, tolen);
    423        1.1       cgd 	msg.msg_iov = &aiov;
    424        1.1       cgd 	msg.msg_iovlen = 1;
    425        1.1       cgd 	msg.msg_control = 0;
    426        1.1       cgd 	msg.msg_flags = 0;
    427       1.91  christos 	aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
    428        1.9       cgd 	aiov.iov_len = SCARG(uap, len);
    429      1.111       dsl 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    430        1.1       cgd }
    431        1.1       cgd 
    432        1.7   mycroft int
    433       1.75   thorpej sys_sendmsg(struct lwp *l, void *v, register_t *retval)
    434       1.15   thorpej {
    435       1.51  augustss 	struct sys_sendmsg_args /* {
    436       1.57     lukem 		syscallarg(int)				s;
    437       1.57     lukem 		syscallarg(const struct msghdr *)	msg;
    438       1.57     lukem 		syscallarg(int)				flags;
    439       1.15   thorpej 	} */ *uap = v;
    440       1.57     lukem 	struct msghdr	msg;
    441       1.57     lukem 	int		error;
    442        1.1       cgd 
    443      1.110       dsl 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    444       1.18  christos 	if (error)
    445        1.1       cgd 		return (error);
    446      1.111       dsl 
    447      1.111       dsl 	msg.msg_flags = MSG_IOVUSRSPACE;
    448      1.111       dsl 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    449        1.1       cgd }
    450        1.1       cgd 
    451        1.7   mycroft int
    452      1.111       dsl do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
    453      1.111       dsl 		register_t *retsize)
    454        1.1       cgd {
    455       1.57     lukem 	struct file	*fp;
    456       1.57     lukem 	struct uio	auio;
    457  1.115.6.1  jmcneill 	int		i, len, error, iovlen;
    458       1.57     lukem 	struct mbuf	*to, *control;
    459       1.57     lukem 	struct socket	*so;
    460      1.111       dsl 	struct iovec	*tiov;
    461      1.111       dsl 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov;
    462       1.57     lukem 	struct iovec	*ktriov;
    463       1.90     perry 
    464  1.115.6.2  jmcneill 	ktrkuser("msghdr", mp, sizeof *mp);
    465  1.115.6.2  jmcneill 
    466      1.111       dsl 	/* If the caller passed us stuff in mbufs, we must free them */
    467      1.111       dsl 	if (mp->msg_flags & MSG_NAMEMBUF)
    468      1.111       dsl 		to = mp->msg_name;
    469      1.111       dsl 	else
    470      1.111       dsl 		to = NULL;
    471      1.111       dsl 
    472      1.111       dsl 	if (mp->msg_flags & MSG_CONTROLMBUF)
    473      1.112     enami 		control = mp->msg_control;
    474      1.111       dsl 	else
    475      1.111       dsl 		control = NULL;
    476      1.111       dsl 
    477      1.111       dsl 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    478      1.111       dsl 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    479      1.111       dsl 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    480      1.111       dsl 				error = EMSGSIZE;
    481      1.111       dsl 				goto bad;
    482      1.111       dsl 			}
    483      1.111       dsl 			iov = malloc(sizeof(struct iovec) * mp->msg_iovlen,
    484      1.111       dsl 			    M_IOV, M_WAITOK);
    485      1.111       dsl 		}
    486      1.111       dsl 		if (mp->msg_iovlen != 0) {
    487      1.111       dsl 			error = copyin(mp->msg_iov, iov,
    488      1.111       dsl 			    (size_t)(mp->msg_iovlen * sizeof(struct iovec)));
    489      1.111       dsl 			if (error)
    490      1.111       dsl 				goto bad;
    491      1.111       dsl 		}
    492      1.111       dsl 		mp->msg_iov = iov;
    493      1.111       dsl 	}
    494      1.111       dsl 
    495        1.1       cgd 	auio.uio_iov = mp->msg_iov;
    496        1.1       cgd 	auio.uio_iovcnt = mp->msg_iovlen;
    497        1.1       cgd 	auio.uio_rw = UIO_WRITE;
    498        1.1       cgd 	auio.uio_offset = 0;			/* XXX */
    499        1.1       cgd 	auio.uio_resid = 0;
    500       1.97      yamt 	KASSERT(l == curlwp);
    501       1.97      yamt 	auio.uio_vmspace = l->l_proc->p_vmspace;
    502      1.111       dsl 
    503      1.111       dsl 	for (i = 0, tiov = mp->msg_iov; i < mp->msg_iovlen; i++, tiov++) {
    504       1.18  christos #if 0
    505       1.18  christos 		/* cannot happen; iov_len is unsigned */
    506      1.111       dsl 		if (tiov->iov_len < 0) {
    507       1.43   thorpej 			error = EINVAL;
    508      1.111       dsl 			goto bad;
    509       1.43   thorpej 		}
    510       1.18  christos #endif
    511       1.33   thorpej 		/*
    512       1.33   thorpej 		 * Writes return ssize_t because -1 is returned on error.
    513       1.33   thorpej 		 * Therefore, we must restrict the length to SSIZE_MAX to
    514       1.33   thorpej 		 * avoid garbage return values.
    515       1.33   thorpej 		 */
    516      1.111       dsl 		auio.uio_resid += tiov->iov_len;
    517      1.111       dsl 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    518       1.43   thorpej 			error = EINVAL;
    519      1.111       dsl 			goto bad;
    520       1.43   thorpej 		}
    521        1.1       cgd 	}
    522      1.111       dsl 
    523      1.112     enami 	if (mp->msg_name && to == NULL) {
    524       1.63  jdolecek 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    525      1.112     enami 		    MT_SONAME);
    526       1.63  jdolecek 		if (error)
    527      1.111       dsl 			goto bad;
    528      1.111       dsl 	}
    529      1.111       dsl 
    530        1.1       cgd 	if (mp->msg_control) {
    531      1.104      elad 		if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
    532        1.1       cgd 			error = EINVAL;
    533        1.1       cgd 			goto bad;
    534        1.1       cgd 		}
    535      1.112     enami 		if (control == NULL) {
    536      1.111       dsl 			error = sockargs(&control, mp->msg_control,
    537      1.112     enami 			    mp->msg_controllen, MT_CONTROL);
    538      1.111       dsl 			if (error)
    539      1.111       dsl 				goto bad;
    540      1.111       dsl 		}
    541      1.111       dsl 	}
    542      1.111       dsl 
    543  1.115.6.1  jmcneill 	ktriov = NULL;
    544  1.115.6.1  jmcneill 	if (ktrpoint(KTR_GENIO)) {
    545  1.115.6.1  jmcneill 		iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    546       1.54   thorpej 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    547      1.110       dsl 		memcpy(ktriov, auio.uio_iov, iovlen);
    548        1.1       cgd 	}
    549      1.111       dsl 
    550      1.111       dsl 	/* getsock() will use the descriptor for us */
    551      1.111       dsl 	if ((error = getsock(l->l_proc->p_fd, s, &fp)) != 0)
    552      1.111       dsl 		goto bad;
    553      1.111       dsl 	so = (struct socket *)fp->f_data;
    554      1.111       dsl 
    555      1.111       dsl 	if (mp->msg_name)
    556      1.111       dsl 		MCLAIM(to, so->so_mowner);
    557      1.111       dsl 	if (mp->msg_control)
    558      1.111       dsl 		MCLAIM(control, so->so_mowner);
    559      1.111       dsl 
    560        1.1       cgd 	len = auio.uio_resid;
    561       1.95  christos 	error = (*so->so_send)(so, to, &auio, NULL, control, flags, l);
    562      1.111       dsl 	/* Protocol is responsible for freeing 'control' */
    563      1.111       dsl 	control = NULL;
    564      1.111       dsl 
    565      1.111       dsl 	FILE_UNUSE(fp, l);
    566      1.111       dsl 
    567       1.18  christos 	if (error) {
    568        1.1       cgd 		if (auio.uio_resid != len && (error == ERESTART ||
    569        1.1       cgd 		    error == EINTR || error == EWOULDBLOCK))
    570        1.1       cgd 			error = 0;
    571      1.106        ad 		if (error == EPIPE && (flags & MSG_NOSIGNAL) == 0) {
    572      1.106        ad 			mutex_enter(&proclist_mutex);
    573      1.111       dsl 			psignal(l->l_proc, SIGPIPE);
    574      1.106        ad 			mutex_exit(&proclist_mutex);
    575      1.106        ad 		}
    576        1.1       cgd 	}
    577        1.1       cgd 	if (error == 0)
    578        1.1       cgd 		*retsize = len - auio.uio_resid;
    579      1.111       dsl 
    580        1.1       cgd 	if (ktriov != NULL) {
    581  1.115.6.1  jmcneill 		ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
    582       1.54   thorpej 		free(ktriov, M_TEMP);
    583        1.1       cgd 	}
    584      1.111       dsl 
    585       1.43   thorpej  bad:
    586      1.111       dsl 	if (iov != aiov)
    587      1.111       dsl 		free(iov, M_IOV);
    588        1.1       cgd 	if (to)
    589        1.1       cgd 		m_freem(to);
    590      1.111       dsl 	if (control != NULL)
    591      1.111       dsl 		m_freem(control);
    592      1.111       dsl 
    593        1.1       cgd 	return (error);
    594        1.1       cgd }
    595        1.1       cgd 
    596        1.7   mycroft int
    597       1.75   thorpej sys_recvfrom(struct lwp *l, void *v, register_t *retval)
    598       1.15   thorpej {
    599       1.51  augustss 	struct sys_recvfrom_args /* {
    600       1.57     lukem 		syscallarg(int)			s;
    601       1.57     lukem 		syscallarg(void *)		buf;
    602       1.57     lukem 		syscallarg(size_t)		len;
    603       1.57     lukem 		syscallarg(int)			flags;
    604       1.57     lukem 		syscallarg(struct sockaddr *)	from;
    605       1.57     lukem 		syscallarg(unsigned int *)	fromlenaddr;
    606       1.15   thorpej 	} */ *uap = v;
    607       1.57     lukem 	struct msghdr	msg;
    608       1.57     lukem 	struct iovec	aiov;
    609       1.57     lukem 	int		error;
    610      1.113       dsl 	struct mbuf	*from;
    611        1.1       cgd 
    612      1.113       dsl 	msg.msg_name = NULL;;
    613        1.1       cgd 	msg.msg_iov = &aiov;
    614        1.1       cgd 	msg.msg_iovlen = 1;
    615        1.9       cgd 	aiov.iov_base = SCARG(uap, buf);
    616        1.9       cgd 	aiov.iov_len = SCARG(uap, len);
    617      1.113       dsl 	msg.msg_control = NULL;
    618      1.113       dsl 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
    619      1.113       dsl 
    620      1.113       dsl 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
    621      1.113       dsl 	if (error != 0)
    622      1.113       dsl 		return error;
    623      1.113       dsl 
    624      1.113       dsl 	error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
    625      1.113       dsl 	    MSG_LENUSRSPACE, from);
    626      1.113       dsl 	if (from != NULL)
    627      1.113       dsl 		m_free(from);
    628      1.113       dsl 	return error;
    629        1.1       cgd }
    630        1.1       cgd 
    631        1.7   mycroft int
    632       1.75   thorpej sys_recvmsg(struct lwp *l, void *v, register_t *retval)
    633       1.15   thorpej {
    634       1.51  augustss 	struct sys_recvmsg_args /* {
    635       1.57     lukem 		syscallarg(int)			s;
    636       1.57     lukem 		syscallarg(struct msghdr *)	msg;
    637       1.57     lukem 		syscallarg(int)			flags;
    638       1.15   thorpej 	} */ *uap = v;
    639       1.81      fvdl 	struct msghdr	msg;
    640       1.57     lukem 	int		error;
    641      1.113       dsl 	struct mbuf	*from, *control;
    642        1.1       cgd 
    643      1.110       dsl 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    644       1.18  christos 	if (error)
    645        1.1       cgd 		return (error);
    646      1.113       dsl 
    647      1.113       dsl 	msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    648      1.113       dsl 
    649      1.113       dsl 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
    650      1.113       dsl 	    msg.msg_control != NULL ? &control : NULL, retval);
    651      1.113       dsl 	if (error != 0)
    652      1.113       dsl 		return error;
    653      1.113       dsl 
    654      1.113       dsl 	if (msg.msg_control != NULL)
    655      1.113       dsl 		error = copyout_msg_control(l, &msg, control);
    656      1.113       dsl 
    657      1.113       dsl 	if (error == 0)
    658      1.113       dsl 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
    659      1.113       dsl 			from);
    660      1.113       dsl 	if (from != NULL)
    661      1.113       dsl 		m_free(from);
    662  1.115.6.2  jmcneill 	if (error == 0) {
    663  1.115.6.2  jmcneill 		ktrkuser("msghdr", &msg, sizeof msg);
    664      1.110       dsl 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
    665  1.115.6.2  jmcneill 	}
    666      1.113       dsl 
    667        1.1       cgd 	return (error);
    668        1.1       cgd }
    669        1.1       cgd 
    670       1.92    martin /*
    671      1.113       dsl  * Adjust for a truncated SCM_RIGHTS control message.
    672      1.113       dsl  *  This means closing any file descriptors that aren't present
    673      1.113       dsl  *  in the returned buffer.
    674      1.113       dsl  *  m is the mbuf holding the (already externalized) SCM_RIGHTS message.
    675       1.92    martin  */
    676       1.93    martin static void
    677      1.113       dsl free_rights(struct mbuf *m, struct lwp *l)
    678       1.92    martin {
    679       1.92    martin 	int nfd;
    680       1.92    martin 	int i;
    681       1.92    martin 	int *fdv;
    682       1.92    martin 
    683       1.94    martin 	nfd = m->m_len < CMSG_SPACE(sizeof(int)) ? 0
    684       1.94    martin 	    : (m->m_len - CMSG_SPACE(sizeof(int))) / sizeof(int) + 1;
    685       1.92    martin 	fdv = (int *) CMSG_DATA(mtod(m,struct cmsghdr *));
    686      1.113       dsl 	for (i = 0; i < nfd; i++)
    687      1.113       dsl 		fdrelease(l, fdv[i]);
    688      1.113       dsl }
    689      1.113       dsl 
    690      1.113       dsl void
    691      1.113       dsl free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
    692      1.113       dsl {
    693      1.113       dsl 	struct mbuf *next;
    694      1.114       dsl 	struct cmsghdr *cmsg;
    695      1.113       dsl 	bool do_free_rights = false;
    696      1.113       dsl 
    697      1.113       dsl 	while (control != NULL) {
    698      1.114       dsl 		cmsg = mtod(control, struct cmsghdr *);
    699      1.113       dsl 		if (control == uncopied)
    700      1.113       dsl 			do_free_rights = true;
    701      1.114       dsl 		if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
    702      1.114       dsl 		    && cmsg->cmsg_type == SCM_RIGHTS)
    703      1.113       dsl 			free_rights(control, l);
    704      1.113       dsl 		next = control->m_next;
    705      1.113       dsl 		m_free(control);
    706      1.113       dsl 		control = next;
    707      1.113       dsl 	}
    708      1.113       dsl }
    709      1.113       dsl 
    710      1.113       dsl /* Copy socket control/CMSG data to user buffer, frees the mbuf */
    711      1.113       dsl int
    712      1.113       dsl copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
    713      1.113       dsl {
    714      1.113       dsl 	int i, len, error = 0;
    715      1.114       dsl 	struct cmsghdr *cmsg;
    716      1.113       dsl 	struct mbuf *m;
    717      1.113       dsl 	char *q;
    718      1.113       dsl 
    719      1.113       dsl 	len = mp->msg_controllen;
    720      1.113       dsl 	if (len <= 0 || control == 0) {
    721      1.113       dsl 		mp->msg_controllen = 0;
    722      1.113       dsl 		free_control_mbuf(l, control, control);
    723      1.113       dsl 		return 0;
    724      1.113       dsl 	}
    725      1.113       dsl 
    726      1.113       dsl 	q = (char *)mp->msg_control;
    727      1.113       dsl 
    728      1.113       dsl 	for (m = control; m != NULL; ) {
    729      1.114       dsl 		cmsg = mtod(m, struct cmsghdr *);
    730      1.113       dsl 		i = m->m_len;
    731      1.113       dsl 		if (len < i) {
    732      1.113       dsl 			mp->msg_flags |= MSG_CTRUNC;
    733      1.114       dsl 			if (cmsg->cmsg_level == SOL_SOCKET
    734      1.114       dsl 			    && cmsg->cmsg_type == SCM_RIGHTS)
    735      1.113       dsl 				/* Do not truncate me ... */
    736      1.113       dsl 				break;
    737      1.113       dsl 			i = len;
    738      1.113       dsl 		}
    739      1.113       dsl 		error = copyout(mtod(m, void *), q, i);
    740  1.115.6.2  jmcneill 		ktrkuser("msgcontrol", mtod(m, void *), i);
    741      1.113       dsl 		if (error != 0) {
    742      1.113       dsl 			/* We must free all the SCM_RIGHTS */
    743      1.113       dsl 			m = control;
    744      1.113       dsl 			break;
    745      1.113       dsl 		}
    746      1.113       dsl 		m = m->m_next;
    747      1.113       dsl 		if (m)
    748      1.113       dsl 			i = ALIGN(i);
    749      1.113       dsl 		q += i;
    750      1.113       dsl 		len -= i;
    751      1.113       dsl 		if (len <= 0)
    752      1.113       dsl 			break;
    753      1.113       dsl 	}
    754      1.113       dsl 
    755      1.113       dsl 	free_control_mbuf(l, control, m);
    756      1.113       dsl 
    757      1.113       dsl 	mp->msg_controllen = q - (char *)mp->msg_control;
    758      1.113       dsl 	return error;
    759       1.92    martin }
    760       1.92    martin 
    761        1.7   mycroft int
    762      1.113       dsl do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from,
    763      1.113       dsl     struct mbuf **control, register_t *retsize)
    764        1.1       cgd {
    765       1.57     lukem 	struct file	*fp;
    766       1.57     lukem 	struct uio	auio;
    767      1.113       dsl 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov;
    768      1.113       dsl 	struct iovec	*tiov;
    769  1.115.6.1  jmcneill 	int		i, len, error, iovlen;
    770       1.57     lukem 	struct socket	*so;
    771       1.57     lukem 	struct iovec	*ktriov;
    772       1.57     lukem 
    773  1.115.6.2  jmcneill 	ktrkuser("msghdr", mp, sizeof *mp);
    774  1.115.6.2  jmcneill 
    775      1.113       dsl 	*from = NULL;
    776      1.113       dsl 	if (control != NULL)
    777      1.113       dsl 		*control = NULL;
    778       1.90     perry 
    779       1.43   thorpej 	/* getsock() will use the descriptor for us */
    780      1.113       dsl 	if ((error = getsock(l->l_proc->p_fd, s, &fp)) != 0)
    781        1.1       cgd 		return (error);
    782       1.76      matt 	so = (struct socket *)fp->f_data;
    783      1.113       dsl 
    784      1.113       dsl 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    785      1.113       dsl 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    786      1.113       dsl 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    787      1.113       dsl 				error = EMSGSIZE;
    788      1.113       dsl 				goto out;
    789      1.113       dsl 			}
    790      1.113       dsl 			iov = malloc(sizeof(struct iovec) * mp->msg_iovlen,
    791      1.113       dsl 			    M_IOV, M_WAITOK);
    792      1.113       dsl 		}
    793      1.113       dsl 		if (mp->msg_iovlen != 0) {
    794      1.113       dsl 			error = copyin(mp->msg_iov, iov,
    795      1.113       dsl 			    (size_t)(mp->msg_iovlen * sizeof(struct iovec)));
    796      1.113       dsl 			if (error)
    797      1.113       dsl 				goto out;
    798      1.113       dsl 		}
    799      1.113       dsl 		auio.uio_iov = iov;
    800      1.113       dsl 	} else
    801      1.113       dsl 		auio.uio_iov = mp->msg_iov;
    802        1.1       cgd 	auio.uio_iovcnt = mp->msg_iovlen;
    803        1.1       cgd 	auio.uio_rw = UIO_READ;
    804        1.1       cgd 	auio.uio_offset = 0;			/* XXX */
    805        1.1       cgd 	auio.uio_resid = 0;
    806       1.97      yamt 	KASSERT(l == curlwp);
    807       1.97      yamt 	auio.uio_vmspace = l->l_proc->p_vmspace;
    808      1.113       dsl 
    809      1.113       dsl 	tiov = auio.uio_iov;
    810      1.113       dsl 	for (i = 0; i < mp->msg_iovlen; i++, tiov++) {
    811       1.18  christos #if 0
    812       1.18  christos 		/* cannot happen iov_len is unsigned */
    813      1.113       dsl 		if (tiov->iov_len < 0) {
    814       1.43   thorpej 			error = EINVAL;
    815      1.113       dsl 			goto out;
    816       1.43   thorpej 		}
    817       1.18  christos #endif
    818       1.33   thorpej 		/*
    819       1.33   thorpej 		 * Reads return ssize_t because -1 is returned on error.
    820       1.33   thorpej 		 * Therefore we must restrict the length to SSIZE_MAX to
    821       1.33   thorpej 		 * avoid garbage return values.
    822       1.33   thorpej 		 */
    823      1.113       dsl 		auio.uio_resid += tiov->iov_len;
    824      1.113       dsl 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    825       1.43   thorpej 			error = EINVAL;
    826      1.113       dsl 			goto out;
    827       1.43   thorpej 		}
    828        1.1       cgd 	}
    829        1.1       cgd 
    830  1.115.6.1  jmcneill 	ktriov = NULL;
    831  1.115.6.1  jmcneill 	if (ktrpoint(KTR_GENIO)) {
    832  1.115.6.1  jmcneill 		iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    833       1.54   thorpej 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    834      1.110       dsl 		memcpy(ktriov, auio.uio_iov, iovlen);
    835        1.1       cgd 	}
    836      1.113       dsl 
    837        1.1       cgd 	len = auio.uio_resid;
    838      1.115       dsl 	mp->msg_flags &= MSG_USERFLAGS;
    839      1.113       dsl 	error = (*so->so_receive)(so, from, &auio, NULL, control,
    840      1.113       dsl 			  &mp->msg_flags);
    841      1.113       dsl 	len -= auio.uio_resid;
    842      1.113       dsl 	*retsize = len;
    843      1.113       dsl 	if (error != 0 && len != 0
    844      1.113       dsl 	    && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
    845      1.113       dsl 		/* Some data transferred */
    846      1.113       dsl 		error = 0;
    847  1.115.6.1  jmcneill 
    848        1.1       cgd 	if (ktriov != NULL) {
    849  1.115.6.1  jmcneill 		ktrgeniov(s, UIO_READ, ktriov, len, error);
    850       1.54   thorpej 		free(ktriov, M_TEMP);
    851        1.1       cgd 	}
    852  1.115.6.1  jmcneill 
    853      1.113       dsl 	if (error != 0) {
    854      1.113       dsl 		m_freem(*from);
    855      1.113       dsl 		*from = NULL;
    856      1.113       dsl 		if (control != NULL) {
    857      1.113       dsl 			free_control_mbuf(l, *control, *control);
    858      1.113       dsl 			*control = NULL;
    859        1.1       cgd 		}
    860        1.1       cgd 	}
    861       1.43   thorpej  out:
    862      1.113       dsl 	if (iov != aiov)
    863      1.113       dsl 		free(iov, M_TEMP);
    864       1.95  christos 	FILE_UNUSE(fp, l);
    865        1.1       cgd 	return (error);
    866        1.1       cgd }
    867        1.1       cgd 
    868      1.113       dsl 
    869        1.1       cgd /* ARGSUSED */
    870        1.7   mycroft int
    871      1.105      yamt sys_shutdown(struct lwp *l, void *v, register_t *retval)
    872       1.15   thorpej {
    873       1.51  augustss 	struct sys_shutdown_args /* {
    874       1.57     lukem 		syscallarg(int)	s;
    875       1.57     lukem 		syscallarg(int)	how;
    876       1.15   thorpej 	} */ *uap = v;
    877       1.75   thorpej 	struct proc	*p;
    878       1.57     lukem 	struct file	*fp;
    879       1.57     lukem 	int		error;
    880        1.1       cgd 
    881       1.75   thorpej 	p = l->l_proc;
    882       1.43   thorpej 	/* getsock() will use the descriptor for us */
    883       1.18  christos 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    884        1.1       cgd 		return (error);
    885       1.43   thorpej 	error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
    886       1.95  christos 	FILE_UNUSE(fp, l);
    887       1.43   thorpej 	return (error);
    888        1.1       cgd }
    889        1.1       cgd 
    890        1.1       cgd /* ARGSUSED */
    891        1.7   mycroft int
    892      1.105      yamt sys_setsockopt(struct lwp *l, void *v, register_t *retval)
    893       1.15   thorpej {
    894       1.51  augustss 	struct sys_setsockopt_args /* {
    895       1.57     lukem 		syscallarg(int)			s;
    896       1.57     lukem 		syscallarg(int)			level;
    897       1.57     lukem 		syscallarg(int)			name;
    898       1.57     lukem 		syscallarg(const void *)	val;
    899       1.57     lukem 		syscallarg(unsigned int)	valsize;
    900       1.15   thorpej 	} */ *uap = v;
    901       1.75   thorpej 	struct proc	*p;
    902       1.57     lukem 	struct file	*fp;
    903       1.57     lukem 	struct mbuf	*m;
    904       1.76      matt 	struct socket	*so;
    905       1.57     lukem 	int		error;
    906       1.75   thorpej 	unsigned int	len;
    907        1.1       cgd 
    908       1.75   thorpej 	p = l->l_proc;
    909       1.57     lukem 	m = NULL;
    910       1.43   thorpej 	/* getsock() will use the descriptor for us */
    911       1.18  christos 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    912        1.1       cgd 		return (error);
    913       1.76      matt 	so = (struct socket *)fp->f_data;
    914       1.75   thorpej 	len = SCARG(uap, valsize);
    915       1.75   thorpej 	if (len > MCLBYTES) {
    916       1.43   thorpej 		error = EINVAL;
    917       1.43   thorpej 		goto out;
    918       1.43   thorpej 	}
    919        1.9       cgd 	if (SCARG(uap, val)) {
    920        1.1       cgd 		m = m_get(M_WAIT, MT_SOOPTS);
    921       1.76      matt 		MCLAIM(m, so->so_mowner);
    922       1.75   thorpej 		if (len > MLEN)
    923       1.76      matt 			m_clget(m, M_WAIT);
    924      1.107  christos 		error = copyin(SCARG(uap, val), mtod(m, void *), len);
    925       1.18  christos 		if (error) {
    926        1.1       cgd 			(void) m_free(m);
    927       1.43   thorpej 			goto out;
    928        1.1       cgd 		}
    929        1.9       cgd 		m->m_len = SCARG(uap, valsize);
    930        1.1       cgd 	}
    931       1.76      matt 	error = sosetopt(so, SCARG(uap, level), SCARG(uap, name), m);
    932       1.43   thorpej  out:
    933       1.95  christos 	FILE_UNUSE(fp, l);
    934       1.43   thorpej 	return (error);
    935        1.1       cgd }
    936        1.1       cgd 
    937        1.1       cgd /* ARGSUSED */
    938        1.7   mycroft int
    939      1.105      yamt sys_getsockopt(struct lwp *l, void *v, register_t *retval)
    940       1.15   thorpej {
    941       1.51  augustss 	struct sys_getsockopt_args /* {
    942       1.57     lukem 		syscallarg(int)			s;
    943       1.57     lukem 		syscallarg(int)			level;
    944       1.57     lukem 		syscallarg(int)			name;
    945       1.57     lukem 		syscallarg(void *)		val;
    946       1.57     lukem 		syscallarg(unsigned int *)	avalsize;
    947       1.15   thorpej 	} */ *uap = v;
    948       1.57     lukem 	struct file	*fp;
    949       1.76      matt 	struct mbuf	*m;
    950       1.57     lukem 	unsigned int	op, i, valsize;
    951       1.57     lukem 	int		error;
    952        1.1       cgd 
    953       1.57     lukem 	m = NULL;
    954       1.43   thorpej 	/* getsock() will use the descriptor for us */
    955      1.101        ad 	if ((error = getsock(l->l_proc->p_fd, SCARG(uap, s), &fp)) != 0)
    956        1.1       cgd 		return (error);
    957        1.9       cgd 	if (SCARG(uap, val)) {
    958      1.110       dsl 		error = copyin(SCARG(uap, avalsize),
    959      1.110       dsl 			       &valsize, sizeof(valsize));
    960       1.18  christos 		if (error)
    961       1.43   thorpej 			goto out;
    962        1.1       cgd 	} else
    963        1.1       cgd 		valsize = 0;
    964        1.9       cgd 	if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
    965        1.9       cgd 	    SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
    966        1.9       cgd 	    m != NULL) {
    967       1.45    itojun 		op = 0;
    968       1.45    itojun 		while (m && !error && op < valsize) {
    969       1.45    itojun 			i = min(m->m_len, (valsize - op));
    970      1.107  christos 			error = copyout(mtod(m, void *), SCARG(uap, val), i);
    971       1.45    itojun 			op += i;
    972       1.96     perry 			SCARG(uap, val) = ((uint8_t *)SCARG(uap, val)) + i;
    973       1.76      matt 			m = m_free(m);
    974       1.45    itojun 		}
    975       1.45    itojun 		valsize = op;
    976        1.1       cgd 		if (error == 0)
    977       1.45    itojun 			error = copyout(&valsize,
    978       1.45    itojun 					SCARG(uap, avalsize), sizeof(valsize));
    979        1.1       cgd 	}
    980        1.1       cgd 	if (m != NULL)
    981       1.76      matt 		(void) m_freem(m);
    982       1.43   thorpej  out:
    983       1.95  christos 	FILE_UNUSE(fp, l);
    984        1.1       cgd 	return (error);
    985        1.1       cgd }
    986        1.1       cgd 
    987       1.68  jdolecek #ifdef PIPE_SOCKETPAIR
    988        1.1       cgd /* ARGSUSED */
    989        1.7   mycroft int
    990      1.105      yamt sys_pipe(struct lwp *l, void *v, register_t *retval)
    991        1.1       cgd {
    992       1.57     lukem 	struct filedesc	*fdp;
    993       1.57     lukem 	struct file	*rf, *wf;
    994       1.57     lukem 	struct socket	*rso, *wso;
    995       1.57     lukem 	int		fd, error;
    996        1.1       cgd 
    997      1.101        ad 	fdp = l->l_proc->p_fd;
    998       1.95  christos 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l)) != 0)
    999        1.1       cgd 		return (error);
   1000       1.95  christos 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l)) != 0)
   1001        1.1       cgd 		goto free1;
   1002       1.58      manu 	/* remember this socket pair implements a pipe */
   1003       1.58      manu 	wso->so_state |= SS_ISAPIPE;
   1004       1.58      manu 	rso->so_state |= SS_ISAPIPE;
   1005       1.43   thorpej 	/* falloc() will use the descriptor for us */
   1006      1.101        ad 	if ((error = falloc(l, &rf, &fd)) != 0)
   1007        1.1       cgd 		goto free2;
   1008        1.1       cgd 	retval[0] = fd;
   1009        1.1       cgd 	rf->f_flag = FREAD;
   1010        1.1       cgd 	rf->f_type = DTYPE_SOCKET;
   1011        1.1       cgd 	rf->f_ops = &socketops;
   1012      1.110       dsl 	rf->f_data = rso;
   1013      1.101        ad 	if ((error = falloc(l, &wf, &fd)) != 0)
   1014        1.1       cgd 		goto free3;
   1015        1.1       cgd 	wf->f_flag = FWRITE;
   1016        1.1       cgd 	wf->f_type = DTYPE_SOCKET;
   1017        1.1       cgd 	wf->f_ops = &socketops;
   1018      1.110       dsl 	wf->f_data = wso;
   1019        1.1       cgd 	retval[1] = fd;
   1020       1.86      matt 	if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0)
   1021        1.1       cgd 		goto free4;
   1022       1.59   thorpej 	FILE_SET_MATURE(rf);
   1023       1.59   thorpej 	FILE_SET_MATURE(wf);
   1024       1.95  christos 	FILE_UNUSE(rf, l);
   1025       1.95  christos 	FILE_UNUSE(wf, l);
   1026        1.1       cgd 	return (0);
   1027       1.57     lukem  free4:
   1028       1.95  christos 	FILE_UNUSE(wf, l);
   1029        1.1       cgd 	ffree(wf);
   1030       1.50   thorpej 	fdremove(fdp, retval[1]);
   1031       1.57     lukem  free3:
   1032       1.95  christos 	FILE_UNUSE(rf, l);
   1033        1.1       cgd 	ffree(rf);
   1034       1.50   thorpej 	fdremove(fdp, retval[0]);
   1035       1.57     lukem  free2:
   1036        1.1       cgd 	(void)soclose(wso);
   1037       1.57     lukem  free1:
   1038        1.1       cgd 	(void)soclose(rso);
   1039        1.1       cgd 	return (error);
   1040        1.1       cgd }
   1041       1.68  jdolecek #endif /* PIPE_SOCKETPAIR */
   1042        1.1       cgd 
   1043        1.1       cgd /*
   1044        1.1       cgd  * Get socket name.
   1045        1.1       cgd  */
   1046       1.13  christos /* ARGSUSED */
   1047        1.7   mycroft int
   1048      1.113       dsl do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam)
   1049       1.15   thorpej {
   1050       1.57     lukem 	struct file	*fp;
   1051       1.57     lukem 	struct socket	*so;
   1052       1.57     lukem 	struct mbuf	*m;
   1053       1.57     lukem 	int		error;
   1054        1.1       cgd 
   1055       1.43   thorpej 	/* getsock() will use the descriptor for us */
   1056      1.113       dsl 	if ((error = getsock(l->l_proc->p_fd, fd, &fp)) != 0)
   1057      1.113       dsl 		return error;
   1058        1.1       cgd 	so = (struct socket *)fp->f_data;
   1059      1.113       dsl 
   1060      1.113       dsl 	if (which == PRU_PEERADDR
   1061      1.113       dsl 	    && (so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) == 0) {
   1062      1.113       dsl 		error = ENOTCONN;
   1063      1.113       dsl 		goto bad;
   1064      1.113       dsl 	}
   1065      1.113       dsl 
   1066        1.1       cgd 	m = m_getclr(M_WAIT, MT_SONAME);
   1067      1.113       dsl 	*nam = m;
   1068       1.76      matt 	MCLAIM(m, so->so_mowner);
   1069      1.113       dsl 	error = (*so->so_proto->pr_usrreq)(so, which, (struct mbuf *)0,
   1070       1.95  christos 	    m, (struct mbuf *)0, (struct lwp *)0);
   1071      1.113       dsl 	if (error != 0)
   1072      1.113       dsl 		m_free(m);
   1073      1.113       dsl     bad:
   1074       1.95  christos 	FILE_UNUSE(fp, l);
   1075      1.113       dsl 	return error;
   1076      1.113       dsl }
   1077      1.113       dsl 
   1078      1.113       dsl int
   1079      1.113       dsl copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
   1080      1.113       dsl     struct mbuf *addr)
   1081      1.113       dsl {
   1082      1.113       dsl 	int len;
   1083      1.113       dsl 	int error;
   1084      1.113       dsl 
   1085      1.113       dsl 	if (asa == NULL)
   1086      1.113       dsl 		/* Assume application not interested */
   1087      1.113       dsl 		return 0;
   1088      1.113       dsl 
   1089      1.113       dsl 	if (flags & MSG_LENUSRSPACE) {
   1090      1.113       dsl 		error = copyin(alen, &len, sizeof(len));
   1091      1.113       dsl 		if (error)
   1092      1.113       dsl 			return error;
   1093      1.113       dsl 	} else
   1094      1.113       dsl 		len = *alen;
   1095  1.115.6.2  jmcneill 	if (len < 0)
   1096      1.113       dsl 		return EINVAL;
   1097      1.113       dsl 
   1098      1.113       dsl 	if (addr == NULL) {
   1099      1.113       dsl 		len = 0;
   1100      1.113       dsl 		error = 0;
   1101      1.113       dsl 	} else {
   1102      1.113       dsl 		if (len > addr->m_len)
   1103      1.113       dsl 			len = addr->m_len;
   1104      1.113       dsl 		/* Maybe this ought to copy a chain ? */
   1105  1.115.6.2  jmcneill 		ktrkuser("sockname", mtod(addr, void *), len);
   1106      1.113       dsl 		error = copyout(mtod(addr, void *), asa, len);
   1107      1.113       dsl 	}
   1108      1.113       dsl 
   1109      1.113       dsl 	if (error == 0) {
   1110      1.113       dsl 		if (flags & MSG_LENUSRSPACE)
   1111      1.113       dsl 			error = copyout(&len, alen, sizeof(len));
   1112      1.113       dsl 		else
   1113      1.113       dsl 			*alen = len;
   1114      1.113       dsl 	}
   1115      1.113       dsl 
   1116      1.113       dsl 	return error;
   1117      1.113       dsl }
   1118      1.113       dsl 
   1119      1.113       dsl /*
   1120      1.113       dsl  * Get socket name.
   1121      1.113       dsl  */
   1122      1.113       dsl /* ARGSUSED */
   1123      1.113       dsl int
   1124      1.113       dsl sys_getsockname(struct lwp *l, void *v, register_t *retval)
   1125      1.113       dsl {
   1126      1.113       dsl 	struct sys_getsockname_args /* {
   1127      1.113       dsl 		syscallarg(int)			fdes;
   1128      1.113       dsl 		syscallarg(struct sockaddr *)	asa;
   1129      1.113       dsl 		syscallarg(unsigned int *)	alen;
   1130      1.113       dsl 	} */ *uap = v;
   1131      1.113       dsl 	struct mbuf	*m;
   1132      1.113       dsl 	int		error;
   1133      1.113       dsl 
   1134      1.113       dsl 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
   1135      1.113       dsl 	if (error != 0)
   1136      1.113       dsl 		return error;
   1137      1.113       dsl 
   1138      1.113       dsl 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1139      1.113       dsl 	    MSG_LENUSRSPACE, m);
   1140      1.113       dsl 	if (m != NULL)
   1141      1.113       dsl 		m_free(m);
   1142      1.113       dsl 	return error;
   1143        1.1       cgd }
   1144        1.1       cgd 
   1145        1.1       cgd /*
   1146        1.1       cgd  * Get name of peer for connected socket.
   1147        1.1       cgd  */
   1148       1.13  christos /* ARGSUSED */
   1149        1.7   mycroft int
   1150      1.105      yamt sys_getpeername(struct lwp *l, void *v, register_t *retval)
   1151       1.15   thorpej {
   1152       1.51  augustss 	struct sys_getpeername_args /* {
   1153       1.57     lukem 		syscallarg(int)			fdes;
   1154       1.57     lukem 		syscallarg(struct sockaddr *)	asa;
   1155       1.57     lukem 		syscallarg(unsigned int *)	alen;
   1156       1.15   thorpej 	} */ *uap = v;
   1157       1.57     lukem 	struct mbuf	*m;
   1158       1.57     lukem 	int		error;
   1159        1.1       cgd 
   1160      1.113       dsl 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
   1161      1.113       dsl 	if (error != 0)
   1162      1.113       dsl 		return error;
   1163      1.113       dsl 
   1164      1.113       dsl 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1165      1.113       dsl 	    MSG_LENUSRSPACE, m);
   1166      1.113       dsl 	if (m != NULL)
   1167      1.113       dsl 		m_free(m);
   1168      1.113       dsl 	return error;
   1169        1.1       cgd }
   1170        1.1       cgd 
   1171       1.24   thorpej /*
   1172       1.24   thorpej  * XXX In a perfect world, we wouldn't pass around socket control
   1173       1.24   thorpej  * XXX arguments in mbufs, and this could go away.
   1174       1.24   thorpej  */
   1175        1.7   mycroft int
   1176       1.91  christos sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
   1177        1.1       cgd {
   1178       1.57     lukem 	struct sockaddr	*sa;
   1179       1.57     lukem 	struct mbuf	*m;
   1180       1.57     lukem 	int		error;
   1181        1.1       cgd 
   1182       1.24   thorpej 	/*
   1183       1.25   thorpej 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1184       1.64      matt 	 * will overflow sa_len.  Control data more than a page size in
   1185       1.64      matt 	 * length is just too much.
   1186       1.24   thorpej 	 */
   1187       1.64      matt 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
   1188       1.24   thorpej 		return (EINVAL);
   1189       1.24   thorpej 
   1190       1.24   thorpej 	/* Allocate an mbuf to hold the arguments. */
   1191       1.24   thorpej 	m = m_get(M_WAIT, type);
   1192       1.76      matt 	/* can't claim.  don't who to assign it to. */
   1193       1.64      matt 	if (buflen > MLEN) {
   1194       1.24   thorpej 		/*
   1195       1.24   thorpej 		 * Won't fit into a regular mbuf, so we allocate just
   1196       1.24   thorpej 		 * enough external storage to hold the argument.
   1197       1.24   thorpej 		 */
   1198       1.24   thorpej 		MEXTMALLOC(m, buflen, M_WAITOK);
   1199        1.1       cgd 	}
   1200        1.1       cgd 	m->m_len = buflen;
   1201      1.107  christos 	error = copyin(bf, mtod(m, void *), buflen);
   1202        1.1       cgd 	if (error) {
   1203        1.1       cgd 		(void) m_free(m);
   1204        1.7   mycroft 		return (error);
   1205        1.1       cgd 	}
   1206  1.115.6.2  jmcneill 	ktrkuser("sockargs", mtod(m, void *), buflen);
   1207        1.1       cgd 	*mp = m;
   1208        1.1       cgd 	if (type == MT_SONAME) {
   1209        1.7   mycroft 		sa = mtod(m, struct sockaddr *);
   1210       1.65  jdolecek #if BYTE_ORDER != BIG_ENDIAN
   1211       1.65  jdolecek 		/*
   1212       1.65  jdolecek 		 * 4.3BSD compat thing - need to stay, since bind(2),
   1213       1.65  jdolecek 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1214       1.65  jdolecek 		 */
   1215        1.1       cgd 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1216        1.1       cgd 			sa->sa_family = sa->sa_len;
   1217        1.1       cgd #endif
   1218        1.1       cgd 		sa->sa_len = buflen;
   1219        1.1       cgd 	}
   1220        1.1       cgd 	return (0);
   1221        1.1       cgd }
   1222        1.1       cgd 
   1223        1.7   mycroft int
   1224       1.57     lukem getsock(struct filedesc *fdp, int fdes, struct file **fpp)
   1225        1.1       cgd {
   1226       1.57     lukem 	struct file	*fp;
   1227        1.1       cgd 
   1228       1.59   thorpej 	if ((fp = fd_getfile(fdp, fdes)) == NULL)
   1229        1.1       cgd 		return (EBADF);
   1230       1.43   thorpej 
   1231       1.43   thorpej 	FILE_USE(fp);
   1232       1.43   thorpej 
   1233       1.43   thorpej 	if (fp->f_type != DTYPE_SOCKET) {
   1234       1.43   thorpej 		FILE_UNUSE(fp, NULL);
   1235        1.1       cgd 		return (ENOTSOCK);
   1236       1.43   thorpej 	}
   1237        1.1       cgd 	*fpp = fp;
   1238        1.1       cgd 	return (0);
   1239        1.1       cgd }
   1240