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