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