Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.184.2.1
      1  1.184.2.1    bouyer /*	$NetBSD: uipc_syscalls.c,v 1.184.2.1 2017/04/21 16:54:02 bouyer 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.184.2.1    bouyer __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.184.2.1 2017/04/21 16:54:02 bouyer Exp $");
     65       1.31   thorpej 
     66      1.180     pooka #ifdef _KERNEL_OPT
     67       1.68  jdolecek #include "opt_pipe.h"
     68      1.180     pooka #endif
     69       1.55  jdolecek 
     70        1.6   mycroft #include <sys/param.h>
     71        1.9       cgd #include <sys/systm.h>
     72        1.6   mycroft #include <sys/filedesc.h>
     73        1.6   mycroft #include <sys/proc.h>
     74        1.6   mycroft #include <sys/file.h>
     75        1.6   mycroft #include <sys/buf.h>
     76      1.147  christos #define MBUFTYPES
     77        1.6   mycroft #include <sys/mbuf.h>
     78        1.6   mycroft #include <sys/protosw.h>
     79        1.6   mycroft #include <sys/socket.h>
     80        1.6   mycroft #include <sys/socketvar.h>
     81       1.18  christos #include <sys/signalvar.h>
     82       1.18  christos #include <sys/un.h>
     83        1.6   mycroft #include <sys/ktrace.h>
     84       1.71  jdolecek #include <sys/event.h>
     85      1.153  christos #include <sys/atomic.h>
     86      1.139      elad #include <sys/kauth.h>
     87        1.1       cgd 
     88        1.9       cgd #include <sys/mount.h>
     89        1.9       cgd #include <sys/syscallargs.h>
     90        1.9       cgd 
     91        1.1       cgd /*
     92        1.1       cgd  * System call interface to the socket abstraction.
     93        1.1       cgd  */
     94       1.89  christos extern const struct fileops socketops;
     95        1.1       cgd 
     96      1.175       rtr static int	sockargs_sb(struct sockaddr_big *, const void *, socklen_t);
     97      1.175       rtr 
     98        1.7   mycroft int
     99      1.167     rmind sys___socket30(struct lwp *l, const struct sys___socket30_args *uap,
    100      1.167     rmind     register_t *retval)
    101       1.15   thorpej {
    102      1.125       dsl 	/* {
    103       1.57     lukem 		syscallarg(int)	domain;
    104       1.57     lukem 		syscallarg(int)	type;
    105       1.57     lukem 		syscallarg(int)	protocol;
    106      1.125       dsl 	} */
    107      1.167     rmind 	int fd, error;
    108        1.1       cgd 
    109      1.120    dyoung 	error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type),
    110      1.169     rmind 	    SCARG(uap, protocol), &fd);
    111      1.167     rmind 	if (error == 0) {
    112        1.1       cgd 		*retval = fd;
    113      1.167     rmind 	}
    114      1.120    dyoung 	return error;
    115        1.1       cgd }
    116        1.1       cgd 
    117        1.7   mycroft int
    118      1.125       dsl sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval)
    119       1.15   thorpej {
    120      1.125       dsl 	/* {
    121       1.57     lukem 		syscallarg(int)				s;
    122       1.57     lukem 		syscallarg(const struct sockaddr *)	name;
    123       1.57     lukem 		syscallarg(unsigned int)		namelen;
    124      1.125       dsl 	} */
    125      1.111       dsl 	int		error;
    126      1.175       rtr 	struct sockaddr_big sb;
    127      1.111       dsl 
    128      1.175       rtr 	error = sockargs_sb(&sb, SCARG(uap, name), SCARG(uap, namelen));
    129      1.111       dsl 	if (error)
    130      1.111       dsl 		return error;
    131      1.111       dsl 
    132      1.175       rtr 	return do_sys_bind(l, SCARG(uap, s), (struct sockaddr *)&sb);
    133      1.111       dsl }
    134      1.111       dsl 
    135      1.111       dsl int
    136      1.175       rtr do_sys_bind(struct lwp *l, int fd, struct sockaddr *nam)
    137      1.111       dsl {
    138      1.128        ad 	struct socket	*so;
    139       1.57     lukem 	int		error;
    140        1.1       cgd 
    141      1.175       rtr 	if ((error = fd_getsock(fd, &so)) != 0)
    142      1.175       rtr 		return error;
    143      1.128        ad 	error = sobind(so, nam, l);
    144      1.128        ad 	fd_putfile(fd);
    145      1.111       dsl 	return error;
    146        1.1       cgd }
    147        1.1       cgd 
    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.176       rtr do_sys_accept(struct lwp *l, int sock, struct sockaddr *name,
    167      1.167     rmind     register_t *new_sock, const sigset_t *mask, int flags, int clrflags)
    168       1.15   thorpej {
    169      1.128        ad 	file_t		*fp, *fp2;
    170      1.129        ad 	int		error, fd;
    171      1.128        ad 	struct socket	*so, *so2;
    172      1.138       dsl 	short		wakeup_state = 0;
    173        1.1       cgd 
    174      1.128        ad 	if ((fp = fd_getfile(sock)) == NULL)
    175      1.167     rmind 		return EBADF;
    176      1.132     rmind 	if (fp->f_type != DTYPE_SOCKET) {
    177      1.132     rmind 		fd_putfile(sock);
    178      1.167     rmind 		return ENOTSOCK;
    179      1.132     rmind 	}
    180      1.132     rmind 	if ((error = fd_allocfile(&fp2, &fd)) != 0) {
    181      1.132     rmind 		fd_putfile(sock);
    182      1.167     rmind 		return error;
    183      1.132     rmind 	}
    184      1.128        ad 	*new_sock = fd;
    185      1.173      matt 	so = fp->f_socket;
    186      1.129        ad 	solock(so);
    187      1.144  christos 
    188      1.144  christos 	if (__predict_false(mask))
    189      1.144  christos 		sigsuspendsetup(l, mask);
    190      1.144  christos 
    191       1.44   darrenr 	if (!(so->so_proto->pr_flags & PR_LISTEN)) {
    192      1.128        ad 		error = EOPNOTSUPP;
    193      1.128        ad 		goto bad;
    194       1.44   darrenr 	}
    195        1.1       cgd 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    196      1.128        ad 		error = EINVAL;
    197      1.128        ad 		goto bad;
    198        1.1       cgd 	}
    199      1.149  christos 	if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
    200      1.128        ad 		error = EWOULDBLOCK;
    201      1.128        ad 		goto bad;
    202        1.1       cgd 	}
    203        1.1       cgd 	while (so->so_qlen == 0 && so->so_error == 0) {
    204        1.1       cgd 		if (so->so_state & SS_CANTRCVMORE) {
    205        1.1       cgd 			so->so_error = ECONNABORTED;
    206        1.1       cgd 			break;
    207        1.1       cgd 		}
    208      1.138       dsl 		if (wakeup_state & SS_RESTARTSYS) {
    209      1.138       dsl 			error = ERESTART;
    210      1.138       dsl 			goto bad;
    211      1.138       dsl 		}
    212      1.135      yamt 		error = sowait(so, true, 0);
    213       1.18  christos 		if (error) {
    214      1.128        ad 			goto bad;
    215        1.1       cgd 		}
    216      1.138       dsl 		wakeup_state = so->so_state;
    217        1.1       cgd 	}
    218        1.1       cgd 	if (so->so_error) {
    219        1.1       cgd 		error = so->so_error;
    220        1.1       cgd 		so->so_error = 0;
    221      1.128        ad 		goto bad;
    222        1.1       cgd 	}
    223       1.71  jdolecek 	/* connection has been removed from the listen queue */
    224      1.129        ad 	KNOTE(&so->so_rcv.sb_sel.sel_klist, NOTE_SUBMIT);
    225      1.128        ad 	so2 = TAILQ_FIRST(&so->so_q);
    226      1.128        ad 	if (soqremque(so2, 1) == 0)
    227        1.1       cgd 		panic("accept");
    228      1.128        ad 	fp2->f_type = DTYPE_SOCKET;
    229      1.144  christos 	fp2->f_flag = (fp->f_flag & ~clrflags) |
    230      1.151  christos 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
    231      1.151  christos 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
    232      1.128        ad 	fp2->f_ops = &socketops;
    233      1.173      matt 	fp2->f_socket = so2;
    234      1.164  christos 	if (fp2->f_flag & FNONBLOCK)
    235      1.160  christos 		so2->so_state |= SS_NBIO;
    236      1.164  christos 	else
    237      1.164  christos 		so2->so_state &= ~SS_NBIO;
    238      1.176       rtr 	error = soaccept(so2, name);
    239      1.139      elad 	so2->so_cred = kauth_cred_dup(so->so_cred);
    240      1.129        ad 	sounlock(so);
    241       1.49   mycroft 	if (error) {
    242      1.113       dsl 		/* an error occurred, free the file descriptor and mbuf */
    243      1.128        ad 		mutex_enter(&fp2->f_lock);
    244      1.128        ad 		fp2->f_count++;
    245      1.128        ad 		mutex_exit(&fp2->f_lock);
    246      1.128        ad 		closef(fp2);
    247      1.128        ad 		fd_abort(curproc, NULL, fd);
    248       1.99  christos 	} else {
    249      1.144  christos 		fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
    250      1.128        ad 		fd_affix(curproc, fp2, fd);
    251       1.49   mycroft 	}
    252      1.129        ad 	fd_putfile(sock);
    253      1.144  christos 	if (__predict_false(mask))
    254      1.144  christos 		sigsuspendteardown(l);
    255      1.167     rmind 	return error;
    256      1.128        ad  bad:
    257      1.167     rmind 	sounlock(so);
    258      1.129        ad 	fd_putfile(sock);
    259      1.167     rmind 	fd_abort(curproc, fp2, fd);
    260      1.144  christos 	if (__predict_false(mask))
    261      1.144  christos 		sigsuspendteardown(l);
    262      1.167     rmind 	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.182   msaitoh 	struct sockaddr_big name;
    275      1.113       dsl 
    276      1.176       rtr 	name.sb_len = UCHAR_MAX;
    277      1.176       rtr 	error = do_sys_accept(l, SCARG(uap, s), (struct sockaddr *)&name,
    278      1.176       rtr 	    retval, NULL, 0, 0);
    279      1.144  christos 	if (error != 0)
    280      1.144  christos 		return error;
    281      1.176       rtr 	error = copyout_sockname_sb(SCARG(uap, name), SCARG(uap, anamelen),
    282      1.176       rtr 	    MSG_LENUSRSPACE, &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.176       rtr 	struct sockaddr_big 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.176       rtr 	name.sb_len = UCHAR_MAX;
    315      1.176       rtr 	error = do_sys_accept(l, SCARG(uap, s), (struct sockaddr *)&name,
    316      1.176       rtr 	    retval, mask, SCARG(uap, flags), FNONBLOCK);
    317      1.113       dsl 	if (error != 0)
    318      1.113       dsl 		return error;
    319      1.176       rtr 	error = copyout_sockname_sb(SCARG(uap, name), SCARG(uap, anamelen),
    320      1.176       rtr 	    MSG_LENUSRSPACE, &name);
    321      1.128        ad 	if (error != 0) {
    322      1.128        ad 		fd = (int)*retval;
    323      1.128        ad 		if (fd_getfile(fd) != NULL)
    324      1.128        ad 			(void)fd_close(fd);
    325      1.128        ad 	}
    326      1.113       dsl 	return error;
    327      1.113       dsl }
    328      1.113       dsl 
    329        1.7   mycroft int
    330      1.167     rmind sys_connect(struct lwp *l, const struct sys_connect_args *uap,
    331      1.167     rmind     register_t *retval)
    332       1.15   thorpej {
    333      1.125       dsl 	/* {
    334       1.57     lukem 		syscallarg(int)				s;
    335       1.57     lukem 		syscallarg(const struct sockaddr *)	name;
    336       1.57     lukem 		syscallarg(unsigned int)		namelen;
    337      1.125       dsl 	} */
    338      1.111       dsl 	int		error;
    339      1.177       rtr 	struct sockaddr_big sbig;
    340      1.111       dsl 
    341      1.177       rtr 	error = sockargs_sb(&sbig, SCARG(uap, name), SCARG(uap, namelen));
    342      1.111       dsl 	if (error)
    343      1.111       dsl 		return error;
    344      1.177       rtr 	return do_sys_connect(l, SCARG(uap, s), (struct sockaddr *)&sbig);
    345      1.111       dsl }
    346      1.111       dsl 
    347      1.111       dsl int
    348      1.177       rtr do_sys_connect(struct lwp *l, int fd, struct sockaddr *nam)
    349      1.111       dsl {
    350       1.57     lukem 	struct socket	*so;
    351      1.111       dsl 	int		error;
    352       1.87     ragge 	int		interrupted = 0;
    353        1.1       cgd 
    354      1.128        ad 	if ((error = fd_getsock(fd, &so)) != 0) {
    355        1.1       cgd 		return (error);
    356      1.111       dsl 	}
    357      1.129        ad 	solock(so);
    358      1.136        ad 	if ((so->so_state & SS_ISCONNECTING) != 0) {
    359       1.62  jdolecek 		error = EALREADY;
    360       1.62  jdolecek 		goto out;
    361       1.62  jdolecek 	}
    362      1.111       dsl 
    363       1.95  christos 	error = soconnect(so, nam, l);
    364        1.1       cgd 	if (error)
    365        1.1       cgd 		goto bad;
    366      1.150  christos 	if ((so->so_state & (SS_NBIO|SS_ISCONNECTING)) ==
    367      1.150  christos 	    (SS_NBIO|SS_ISCONNECTING)) {
    368       1.62  jdolecek 		error = EINPROGRESS;
    369       1.62  jdolecek 		goto out;
    370        1.1       cgd 	}
    371      1.136        ad 	while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) {
    372      1.135      yamt 		error = sowait(so, true, 0);
    373      1.137       dsl 		if (__predict_false((so->so_state & SS_ISABORTING) != 0)) {
    374      1.136        ad 			error = EPIPE;
    375      1.136        ad 			interrupted = 1;
    376      1.136        ad 			break;
    377      1.136        ad 		}
    378       1.87     ragge 		if (error) {
    379       1.87     ragge 			if (error == EINTR || error == ERESTART)
    380       1.87     ragge 				interrupted = 1;
    381        1.1       cgd 			break;
    382       1.87     ragge 		}
    383       1.18  christos 	}
    384        1.1       cgd 	if (error == 0) {
    385        1.1       cgd 		error = so->so_error;
    386        1.1       cgd 		so->so_error = 0;
    387        1.1       cgd 	}
    388       1.57     lukem  bad:
    389       1.87     ragge 	if (!interrupted)
    390       1.87     ragge 		so->so_state &= ~SS_ISCONNECTING;
    391        1.1       cgd 	if (error == ERESTART)
    392        1.1       cgd 		error = EINTR;
    393       1.62  jdolecek  out:
    394      1.167     rmind 	sounlock(so);
    395      1.167     rmind 	fd_putfile(fd);
    396      1.167     rmind 	return error;
    397        1.1       cgd }
    398        1.1       cgd 
    399      1.148  christos static int
    400      1.148  christos makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type,
    401      1.148  christos     int domain, int proto, struct socket *soo)
    402      1.148  christos {
    403      1.167     rmind 	struct socket *so;
    404      1.148  christos 	int error;
    405      1.148  christos 
    406      1.167     rmind 	if ((error = socreate(domain, &so, type, proto, l, soo)) != 0) {
    407      1.148  christos 		return error;
    408      1.167     rmind 	}
    409      1.168     rmind 	if (flags & SOCK_NONBLOCK) {
    410      1.168     rmind 		so->so_state |= SS_NBIO;
    411      1.168     rmind 	}
    412      1.148  christos 
    413      1.148  christos 	if ((error = fd_allocfile(fp, fd)) != 0) {
    414      1.148  christos 		soclose(so);
    415      1.148  christos 		return error;
    416      1.148  christos 	}
    417      1.148  christos 	fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0);
    418      1.151  christos 	(*fp)->f_flag = FREAD|FWRITE|
    419      1.151  christos 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
    420      1.151  christos 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
    421      1.148  christos 	(*fp)->f_type = DTYPE_SOCKET;
    422      1.148  christos 	(*fp)->f_ops = &socketops;
    423      1.173      matt 	(*fp)->f_socket = so;
    424      1.148  christos 	return 0;
    425      1.148  christos }
    426      1.148  christos 
    427        1.7   mycroft int
    428      1.148  christos sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap,
    429      1.148  christos     register_t *retval)
    430       1.15   thorpej {
    431      1.125       dsl 	/* {
    432       1.57     lukem 		syscallarg(int)		domain;
    433       1.57     lukem 		syscallarg(int)		type;
    434       1.57     lukem 		syscallarg(int)		protocol;
    435       1.57     lukem 		syscallarg(int *)	rsv;
    436      1.125       dsl 	} */
    437      1.128        ad 	file_t		*fp1, *fp2;
    438       1.57     lukem 	struct socket	*so1, *so2;
    439       1.57     lukem 	int		fd, error, sv[2];
    440      1.167     rmind 	proc_t		*p = curproc;
    441      1.144  christos 	int		flags = SCARG(uap, type) & SOCK_FLAGS_MASK;
    442      1.144  christos 	int		type = SCARG(uap, type) & ~SOCK_FLAGS_MASK;
    443      1.148  christos 	int		domain = SCARG(uap, domain);
    444      1.148  christos 	int		proto = SCARG(uap, protocol);
    445        1.1       cgd 
    446      1.148  christos 	error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL);
    447       1.18  christos 	if (error)
    448      1.148  christos 		return error;
    449      1.173      matt 	so1 = fp1->f_socket;
    450      1.148  christos 	sv[0] = fd;
    451      1.148  christos 
    452      1.148  christos 	error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1);
    453       1.18  christos 	if (error)
    454      1.148  christos 		goto out;
    455      1.173      matt 	so2 = fp2->f_socket;
    456        1.1       cgd 	sv[1] = fd;
    457      1.148  christos 
    458      1.129        ad 	solock(so1);
    459      1.129        ad 	error = soconnect2(so1, so2);
    460      1.148  christos 	if (error == 0 && type == SOCK_DGRAM) {
    461        1.1       cgd 		/*
    462        1.1       cgd 		 * Datagram socket connection is asymmetric.
    463        1.1       cgd 		 */
    464      1.129        ad 		error = soconnect2(so2, so1);
    465      1.129        ad 	}
    466      1.129        ad 	sounlock(so1);
    467      1.148  christos 
    468      1.129        ad 	if (error == 0)
    469      1.148  christos 		error = copyout(sv, SCARG(uap, rsv), sizeof(sv));
    470      1.129        ad 	if (error == 0) {
    471      1.129        ad 		fd_affix(p, fp2, sv[1]);
    472      1.129        ad 		fd_affix(p, fp1, sv[0]);
    473      1.148  christos 		return 0;
    474        1.1       cgd 	}
    475      1.128        ad 	fd_abort(p, fp2, sv[1]);
    476      1.148  christos 	(void)soclose(so2);
    477      1.148  christos out:
    478      1.128        ad 	fd_abort(p, fp1, sv[0]);
    479        1.1       cgd 	(void)soclose(so1);
    480      1.148  christos 	return error;
    481        1.1       cgd }
    482        1.1       cgd 
    483        1.7   mycroft int
    484      1.167     rmind sys_sendto(struct lwp *l, const struct sys_sendto_args *uap,
    485      1.167     rmind     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.183    martin 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
    507      1.183    martin 	    NULL, 0, retval);
    508        1.1       cgd }
    509        1.1       cgd 
    510        1.7   mycroft int
    511      1.167     rmind sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap,
    512      1.167     rmind     register_t *retval)
    513       1.15   thorpej {
    514      1.125       dsl 	/* {
    515       1.57     lukem 		syscallarg(int)				s;
    516       1.57     lukem 		syscallarg(const struct msghdr *)	msg;
    517       1.57     lukem 		syscallarg(int)				flags;
    518      1.125       dsl 	} */
    519       1.57     lukem 	struct msghdr	msg;
    520       1.57     lukem 	int		error;
    521        1.1       cgd 
    522      1.110       dsl 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    523       1.18  christos 	if (error)
    524        1.1       cgd 		return (error);
    525      1.111       dsl 
    526      1.111       dsl 	msg.msg_flags = MSG_IOVUSRSPACE;
    527      1.183    martin 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
    528      1.183    martin 	    NULL, 0, retval);
    529        1.1       cgd }
    530        1.1       cgd 
    531  1.184.2.1    bouyer int
    532      1.155  christos do_sys_sendmsg_so(struct lwp *l, int s, struct socket *so, file_t *fp,
    533      1.183    martin     struct msghdr *mp, int flags, const void *kthdr, size_t ktsize,
    534      1.183    martin     register_t *retsize)
    535        1.1       cgd {
    536      1.155  christos 
    537      1.141     rmind 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
    538      1.178       rtr 	struct sockaddr *sa = NULL;
    539       1.57     lukem 	struct mbuf	*to, *control;
    540      1.141     rmind 	struct uio	auio;
    541      1.141     rmind 	size_t		len, iovsz;
    542      1.141     rmind 	int		i, error;
    543       1.90     perry 
    544      1.183    martin 	if (__predict_false(kthdr == NULL && ktsize == 0)) {
    545      1.183    martin 		kthdr = mp;
    546      1.183    martin 		ktsize = sizeof(*mp);
    547      1.183    martin 	}
    548      1.183    martin 	if (__predict_true(kthdr != NULL))
    549      1.183    martin 		ktrkuser("msghdr", kthdr, ktsize);
    550      1.117       dsl 
    551      1.141     rmind 	/* If the caller passed us stuff in mbufs, we must free them. */
    552      1.141     rmind 	to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL;
    553      1.141     rmind 	control = (mp->msg_flags & MSG_CONTROLMBUF) ? mp->msg_control : NULL;
    554      1.141     rmind 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    555      1.111       dsl 
    556      1.111       dsl 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    557      1.111       dsl 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    558      1.111       dsl 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    559      1.111       dsl 				error = EMSGSIZE;
    560      1.111       dsl 				goto bad;
    561      1.111       dsl 			}
    562      1.141     rmind 			iov = kmem_alloc(iovsz, KM_SLEEP);
    563      1.111       dsl 		}
    564      1.111       dsl 		if (mp->msg_iovlen != 0) {
    565      1.141     rmind 			error = copyin(mp->msg_iov, iov, iovsz);
    566      1.111       dsl 			if (error)
    567      1.111       dsl 				goto bad;
    568      1.111       dsl 		}
    569      1.181  christos 		auio.uio_iov = iov;
    570      1.181  christos 	} else
    571      1.181  christos 		auio.uio_iov = mp->msg_iov;
    572      1.111       dsl 
    573        1.1       cgd 	auio.uio_iovcnt = mp->msg_iovlen;
    574        1.1       cgd 	auio.uio_rw = UIO_WRITE;
    575        1.1       cgd 	auio.uio_offset = 0;			/* XXX */
    576        1.1       cgd 	auio.uio_resid = 0;
    577       1.97      yamt 	KASSERT(l == curlwp);
    578       1.97      yamt 	auio.uio_vmspace = l->l_proc->p_vmspace;
    579      1.111       dsl 
    580      1.181  christos 	tiov = auio.uio_iov;
    581      1.181  christos 	for (i = 0; i < auio.uio_iovcnt; i++, tiov++) {
    582       1.33   thorpej 		/*
    583       1.33   thorpej 		 * Writes return ssize_t because -1 is returned on error.
    584       1.33   thorpej 		 * Therefore, we must restrict the length to SSIZE_MAX to
    585       1.33   thorpej 		 * avoid garbage return values.
    586       1.33   thorpej 		 */
    587      1.111       dsl 		auio.uio_resid += tiov->iov_len;
    588      1.111       dsl 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    589       1.43   thorpej 			error = EINVAL;
    590      1.111       dsl 			goto bad;
    591       1.43   thorpej 		}
    592        1.1       cgd 	}
    593      1.111       dsl 
    594      1.112     enami 	if (mp->msg_name && to == NULL) {
    595       1.63  jdolecek 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    596      1.112     enami 		    MT_SONAME);
    597       1.63  jdolecek 		if (error)
    598      1.111       dsl 			goto bad;
    599      1.111       dsl 	}
    600      1.111       dsl 
    601        1.1       cgd 	if (mp->msg_control) {
    602      1.104      elad 		if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
    603        1.1       cgd 			error = EINVAL;
    604        1.1       cgd 			goto bad;
    605        1.1       cgd 		}
    606      1.112     enami 		if (control == NULL) {
    607      1.111       dsl 			error = sockargs(&control, mp->msg_control,
    608      1.112     enami 			    mp->msg_controllen, MT_CONTROL);
    609      1.111       dsl 			if (error)
    610      1.111       dsl 				goto bad;
    611      1.111       dsl 		}
    612      1.111       dsl 	}
    613      1.111       dsl 
    614      1.158   mlelstv 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
    615      1.141     rmind 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    616      1.141     rmind 		memcpy(ktriov, auio.uio_iov, iovsz);
    617        1.1       cgd 	}
    618      1.111       dsl 
    619      1.111       dsl 	if (mp->msg_name)
    620      1.111       dsl 		MCLAIM(to, so->so_mowner);
    621      1.111       dsl 	if (mp->msg_control)
    622      1.111       dsl 		MCLAIM(control, so->so_mowner);
    623      1.111       dsl 
    624      1.178       rtr 	if (to) {
    625      1.178       rtr 		sa = mtod(to, struct sockaddr *);
    626      1.178       rtr 	}
    627      1.178       rtr 
    628        1.1       cgd 	len = auio.uio_resid;
    629      1.178       rtr 	error = (*so->so_send)(so, sa, &auio, NULL, control, flags, l);
    630      1.111       dsl 	/* Protocol is responsible for freeing 'control' */
    631      1.111       dsl 	control = NULL;
    632      1.111       dsl 
    633       1.18  christos 	if (error) {
    634        1.1       cgd 		if (auio.uio_resid != len && (error == ERESTART ||
    635        1.1       cgd 		    error == EINTR || error == EWOULDBLOCK))
    636        1.1       cgd 			error = 0;
    637      1.151  christos 		if (error == EPIPE && (fp->f_flag & FNOSIGPIPE) == 0 &&
    638      1.151  christos 		    (flags & MSG_NOSIGNAL) == 0) {
    639      1.130        ad 			mutex_enter(proc_lock);
    640      1.111       dsl 			psignal(l->l_proc, SIGPIPE);
    641      1.130        ad 			mutex_exit(proc_lock);
    642      1.106        ad 		}
    643        1.1       cgd 	}
    644        1.1       cgd 	if (error == 0)
    645        1.1       cgd 		*retsize = len - auio.uio_resid;
    646      1.111       dsl 
    647      1.119     rmind bad:
    648      1.157   mlelstv 	if (ktriov != NULL) {
    649      1.116        ad 		ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
    650      1.157   mlelstv 		kmem_free(ktriov, iovsz);
    651        1.1       cgd 	}
    652      1.111       dsl 
    653      1.167     rmind 	if (iov != aiov)
    654      1.167     rmind 		kmem_free(iov, iovsz);
    655        1.1       cgd 	if (to)
    656        1.1       cgd 		m_freem(to);
    657      1.121  christos 	if (control)
    658      1.111       dsl 		m_freem(control);
    659      1.111       dsl 
    660      1.167     rmind 	return error;
    661        1.1       cgd }
    662        1.1       cgd 
    663        1.7   mycroft int
    664      1.155  christos do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
    665      1.183    martin     const void *kthdr, size_t ktsize, register_t *retsize)
    666      1.155  christos {
    667      1.155  christos 	int		error;
    668      1.155  christos 	struct socket	*so;
    669      1.155  christos 	file_t		*fp;
    670      1.155  christos 
    671      1.179      maxv 	if ((error = fd_getsock1(s, &so, &fp)) != 0) {
    672      1.179      maxv 		/* We have to free msg_name and msg_control ourselves */
    673      1.179      maxv 		if (mp->msg_flags & MSG_NAMEMBUF)
    674      1.179      maxv 			m_freem(mp->msg_name);
    675      1.179      maxv 		if (mp->msg_flags & MSG_CONTROLMBUF)
    676      1.179      maxv 			m_freem(mp->msg_control);
    677      1.155  christos 		return error;
    678      1.179      maxv 	}
    679      1.183    martin 	error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, kthdr, ktsize,
    680      1.183    martin 	    retsize);
    681      1.179      maxv 	/* msg_name and msg_control freed */
    682      1.155  christos 	fd_putfile(s);
    683      1.155  christos 	return error;
    684      1.155  christos }
    685      1.155  christos 
    686      1.155  christos int
    687      1.167     rmind sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap,
    688      1.167     rmind     register_t *retval)
    689       1.15   thorpej {
    690      1.125       dsl 	/* {
    691       1.57     lukem 		syscallarg(int)			s;
    692       1.57     lukem 		syscallarg(void *)		buf;
    693       1.57     lukem 		syscallarg(size_t)		len;
    694       1.57     lukem 		syscallarg(int)			flags;
    695       1.57     lukem 		syscallarg(struct sockaddr *)	from;
    696       1.57     lukem 		syscallarg(unsigned int *)	fromlenaddr;
    697      1.125       dsl 	} */
    698       1.57     lukem 	struct msghdr	msg;
    699       1.57     lukem 	struct iovec	aiov;
    700       1.57     lukem 	int		error;
    701      1.113       dsl 	struct mbuf	*from;
    702        1.1       cgd 
    703      1.121  christos 	msg.msg_name = NULL;
    704        1.1       cgd 	msg.msg_iov = &aiov;
    705        1.1       cgd 	msg.msg_iovlen = 1;
    706        1.9       cgd 	aiov.iov_base = SCARG(uap, buf);
    707        1.9       cgd 	aiov.iov_len = SCARG(uap, len);
    708      1.113       dsl 	msg.msg_control = NULL;
    709      1.113       dsl 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
    710      1.113       dsl 
    711      1.183    martin 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, NULL, 0, &from,
    712      1.183    martin 	    NULL, retval);
    713      1.113       dsl 	if (error != 0)
    714      1.113       dsl 		return error;
    715      1.113       dsl 
    716      1.113       dsl 	error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
    717      1.113       dsl 	    MSG_LENUSRSPACE, from);
    718      1.113       dsl 	if (from != NULL)
    719      1.113       dsl 		m_free(from);
    720      1.113       dsl 	return error;
    721        1.1       cgd }
    722        1.1       cgd 
    723        1.7   mycroft int
    724      1.167     rmind sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap,
    725      1.167     rmind     register_t *retval)
    726       1.15   thorpej {
    727      1.125       dsl 	/* {
    728       1.57     lukem 		syscallarg(int)			s;
    729       1.57     lukem 		syscallarg(struct msghdr *)	msg;
    730       1.57     lukem 		syscallarg(int)			flags;
    731      1.125       dsl 	} */
    732       1.81      fvdl 	struct msghdr	msg;
    733       1.57     lukem 	int		error;
    734      1.113       dsl 	struct mbuf	*from, *control;
    735        1.1       cgd 
    736      1.110       dsl 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    737       1.18  christos 	if (error)
    738      1.167     rmind 		return error;
    739      1.113       dsl 
    740      1.113       dsl 	msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    741      1.113       dsl 
    742      1.183    martin 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, NULL, 0, &from,
    743      1.113       dsl 	    msg.msg_control != NULL ? &control : NULL, retval);
    744      1.113       dsl 	if (error != 0)
    745      1.113       dsl 		return error;
    746      1.113       dsl 
    747      1.113       dsl 	if (msg.msg_control != NULL)
    748      1.113       dsl 		error = copyout_msg_control(l, &msg, control);
    749      1.113       dsl 
    750      1.113       dsl 	if (error == 0)
    751      1.113       dsl 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
    752      1.113       dsl 			from);
    753      1.113       dsl 	if (from != NULL)
    754      1.113       dsl 		m_free(from);
    755      1.117       dsl 	if (error == 0) {
    756      1.117       dsl 		ktrkuser("msghdr", &msg, sizeof msg);
    757      1.110       dsl 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
    758      1.117       dsl 	}
    759      1.113       dsl 
    760      1.167     rmind 	return error;
    761        1.1       cgd }
    762        1.1       cgd 
    763      1.155  christos int
    764      1.155  christos sys_sendmmsg(struct lwp *l, const struct sys_sendmmsg_args *uap,
    765      1.155  christos     register_t *retval)
    766      1.155  christos {
    767      1.155  christos 	/* {
    768      1.155  christos 		syscallarg(int)			s;
    769      1.155  christos 		syscallarg(struct mmsghdr *)	mmsg;
    770      1.155  christos 		syscallarg(unsigned int)	vlen;
    771      1.155  christos 		syscallarg(unsigned int)	flags;
    772      1.155  christos 	} */
    773      1.155  christos 	struct mmsghdr mmsg;
    774      1.155  christos 	struct socket *so;
    775      1.155  christos 	file_t *fp;
    776      1.155  christos 	struct msghdr *msg = &mmsg.msg_hdr;
    777      1.155  christos 	int error, s;
    778      1.155  christos 	unsigned int vlen, flags, dg;
    779      1.155  christos 
    780      1.155  christos 	s = SCARG(uap, s);
    781      1.155  christos 	if ((error = fd_getsock1(s, &so, &fp)) != 0)
    782      1.155  christos 		return error;
    783      1.155  christos 
    784      1.155  christos 	vlen = SCARG(uap, vlen);
    785      1.155  christos 	if (vlen > 1024)
    786      1.155  christos 		vlen = 1024;
    787      1.155  christos 
    788      1.155  christos 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    789      1.155  christos 
    790      1.155  christos 	for (dg = 0; dg < vlen;) {
    791      1.155  christos 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
    792      1.155  christos 		if (error)
    793      1.155  christos 			break;
    794      1.155  christos 
    795      1.155  christos 		msg->msg_flags = flags;
    796      1.155  christos 
    797      1.183    martin 		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags,
    798      1.183    martin 		    &msg, sizeof(msg), retval);
    799      1.155  christos 		if (error)
    800      1.155  christos 			break;
    801      1.155  christos 
    802      1.155  christos 		ktrkuser("msghdr", msg, sizeof *msg);
    803      1.155  christos 		mmsg.msg_len = *retval;
    804      1.155  christos 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
    805      1.155  christos 		if (error)
    806      1.155  christos 			break;
    807      1.155  christos 		dg++;
    808      1.155  christos 
    809      1.155  christos 	}
    810      1.155  christos 
    811      1.155  christos 	*retval = dg;
    812      1.155  christos 	if (error)
    813      1.155  christos 		so->so_error = error;
    814      1.155  christos 
    815      1.155  christos 	fd_putfile(s);
    816      1.155  christos 
    817      1.155  christos 	/*
    818      1.155  christos 	 * If we succeeded at least once, return 0, hopefully so->so_error
    819      1.155  christos 	 * will catch it next time.
    820      1.155  christos 	 */
    821      1.155  christos 	if (dg)
    822      1.155  christos 		return 0;
    823      1.155  christos 	return error;
    824      1.155  christos }
    825      1.155  christos 
    826       1.92    martin /*
    827      1.113       dsl  * Adjust for a truncated SCM_RIGHTS control message.
    828      1.113       dsl  *  This means closing any file descriptors that aren't present
    829      1.113       dsl  *  in the returned buffer.
    830      1.113       dsl  *  m is the mbuf holding the (already externalized) SCM_RIGHTS message.
    831       1.92    martin  */
    832       1.93    martin static void
    833      1.128        ad free_rights(struct mbuf *m)
    834       1.92    martin {
    835      1.159  riastrad 	struct cmsghdr *cm;
    836       1.92    martin 	int *fdv;
    837      1.159  riastrad 	unsigned int nfds, i;
    838       1.92    martin 
    839      1.159  riastrad 	KASSERT(sizeof(*cm) <= m->m_len);
    840      1.159  riastrad 	cm = mtod(m, struct cmsghdr *);
    841      1.159  riastrad 
    842      1.159  riastrad 	KASSERT(CMSG_ALIGN(sizeof(*cm)) <= cm->cmsg_len);
    843      1.159  riastrad 	KASSERT(cm->cmsg_len <= m->m_len);
    844      1.159  riastrad 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
    845      1.159  riastrad 	fdv = (int *)CMSG_DATA(cm);
    846      1.159  riastrad 
    847      1.159  riastrad 	for (i = 0; i < nfds; i++)
    848      1.128        ad 		if (fd_getfile(fdv[i]) != NULL)
    849      1.128        ad 			(void)fd_close(fdv[i]);
    850      1.113       dsl }
    851      1.113       dsl 
    852      1.113       dsl void
    853      1.113       dsl free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
    854      1.113       dsl {
    855      1.113       dsl 	struct mbuf *next;
    856      1.114       dsl 	struct cmsghdr *cmsg;
    857      1.113       dsl 	bool do_free_rights = false;
    858      1.113       dsl 
    859      1.113       dsl 	while (control != NULL) {
    860      1.114       dsl 		cmsg = mtod(control, struct cmsghdr *);
    861      1.113       dsl 		if (control == uncopied)
    862      1.113       dsl 			do_free_rights = true;
    863      1.114       dsl 		if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
    864      1.114       dsl 		    && cmsg->cmsg_type == SCM_RIGHTS)
    865      1.128        ad 			free_rights(control);
    866      1.113       dsl 		next = control->m_next;
    867      1.113       dsl 		m_free(control);
    868      1.113       dsl 		control = next;
    869      1.113       dsl 	}
    870      1.113       dsl }
    871      1.113       dsl 
    872      1.113       dsl /* Copy socket control/CMSG data to user buffer, frees the mbuf */
    873      1.113       dsl int
    874      1.113       dsl copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
    875      1.113       dsl {
    876      1.113       dsl 	int i, len, error = 0;
    877      1.114       dsl 	struct cmsghdr *cmsg;
    878      1.113       dsl 	struct mbuf *m;
    879      1.113       dsl 	char *q;
    880      1.113       dsl 
    881      1.113       dsl 	len = mp->msg_controllen;
    882      1.113       dsl 	if (len <= 0 || control == 0) {
    883      1.113       dsl 		mp->msg_controllen = 0;
    884      1.113       dsl 		free_control_mbuf(l, control, control);
    885      1.113       dsl 		return 0;
    886      1.113       dsl 	}
    887      1.113       dsl 
    888      1.113       dsl 	q = (char *)mp->msg_control;
    889      1.113       dsl 
    890      1.113       dsl 	for (m = control; m != NULL; ) {
    891      1.114       dsl 		cmsg = mtod(m, struct cmsghdr *);
    892      1.113       dsl 		i = m->m_len;
    893      1.113       dsl 		if (len < i) {
    894      1.113       dsl 			mp->msg_flags |= MSG_CTRUNC;
    895      1.114       dsl 			if (cmsg->cmsg_level == SOL_SOCKET
    896      1.114       dsl 			    && cmsg->cmsg_type == SCM_RIGHTS)
    897      1.113       dsl 				/* Do not truncate me ... */
    898      1.113       dsl 				break;
    899      1.113       dsl 			i = len;
    900      1.113       dsl 		}
    901      1.113       dsl 		error = copyout(mtod(m, void *), q, i);
    902      1.117       dsl 		ktrkuser("msgcontrol", mtod(m, void *), i);
    903      1.113       dsl 		if (error != 0) {
    904      1.113       dsl 			/* We must free all the SCM_RIGHTS */
    905      1.113       dsl 			m = control;
    906      1.113       dsl 			break;
    907      1.113       dsl 		}
    908      1.113       dsl 		m = m->m_next;
    909      1.113       dsl 		if (m)
    910      1.113       dsl 			i = ALIGN(i);
    911      1.113       dsl 		q += i;
    912      1.113       dsl 		len -= i;
    913      1.113       dsl 		if (len <= 0)
    914      1.113       dsl 			break;
    915      1.113       dsl 	}
    916      1.113       dsl 
    917      1.113       dsl 	free_control_mbuf(l, control, m);
    918      1.113       dsl 
    919      1.113       dsl 	mp->msg_controllen = q - (char *)mp->msg_control;
    920      1.113       dsl 	return error;
    921       1.92    martin }
    922       1.92    martin 
    923  1.184.2.1    bouyer int
    924      1.155  christos do_sys_recvmsg_so(struct lwp *l, int s, struct socket *so, struct msghdr *mp,
    925      1.183    martin     const void *ktrhdr, size_t ktsize, struct mbuf **from,
    926      1.183    martin     struct mbuf **control, register_t *retsize)
    927        1.1       cgd {
    928      1.156     njoly 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
    929       1.57     lukem 	struct uio	auio;
    930      1.141     rmind 	size_t		len, iovsz;
    931      1.141     rmind 	int		i, error;
    932       1.57     lukem 
    933      1.183    martin 	if (__predict_false(ktrhdr == NULL && ktsize == 0)) {
    934      1.183    martin 		ktrhdr = mp;
    935      1.183    martin 		ktsize = sizeof *mp;
    936      1.183    martin 	}
    937      1.183    martin 	if (__predict_true(ktrhdr != NULL))
    938      1.183    martin 		ktrkuser("msghdr", ktrhdr, ktsize);
    939      1.117       dsl 
    940      1.113       dsl 	*from = NULL;
    941      1.113       dsl 	if (control != NULL)
    942      1.113       dsl 		*control = NULL;
    943       1.90     perry 
    944      1.141     rmind 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    945      1.141     rmind 
    946      1.113       dsl 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    947      1.113       dsl 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    948      1.113       dsl 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    949      1.113       dsl 				error = EMSGSIZE;
    950      1.113       dsl 				goto out;
    951      1.113       dsl 			}
    952      1.141     rmind 			iov = kmem_alloc(iovsz, KM_SLEEP);
    953      1.113       dsl 		}
    954      1.113       dsl 		if (mp->msg_iovlen != 0) {
    955      1.141     rmind 			error = copyin(mp->msg_iov, iov, iovsz);
    956      1.113       dsl 			if (error)
    957      1.113       dsl 				goto out;
    958      1.113       dsl 		}
    959      1.113       dsl 		auio.uio_iov = iov;
    960      1.113       dsl 	} else
    961      1.113       dsl 		auio.uio_iov = mp->msg_iov;
    962        1.1       cgd 	auio.uio_iovcnt = mp->msg_iovlen;
    963        1.1       cgd 	auio.uio_rw = UIO_READ;
    964        1.1       cgd 	auio.uio_offset = 0;			/* XXX */
    965        1.1       cgd 	auio.uio_resid = 0;
    966       1.97      yamt 	KASSERT(l == curlwp);
    967       1.97      yamt 	auio.uio_vmspace = l->l_proc->p_vmspace;
    968      1.113       dsl 
    969      1.113       dsl 	tiov = auio.uio_iov;
    970      1.181  christos 	for (i = 0; i < auio.uio_iovcnt; i++, tiov++) {
    971       1.33   thorpej 		/*
    972       1.33   thorpej 		 * Reads return ssize_t because -1 is returned on error.
    973       1.33   thorpej 		 * Therefore we must restrict the length to SSIZE_MAX to
    974       1.33   thorpej 		 * avoid garbage return values.
    975       1.33   thorpej 		 */
    976      1.113       dsl 		auio.uio_resid += tiov->iov_len;
    977      1.113       dsl 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    978       1.43   thorpej 			error = EINVAL;
    979      1.113       dsl 			goto out;
    980       1.43   thorpej 		}
    981        1.1       cgd 	}
    982        1.1       cgd 
    983      1.158   mlelstv 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
    984      1.141     rmind 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    985      1.141     rmind 		memcpy(ktriov, auio.uio_iov, iovsz);
    986        1.1       cgd 	}
    987      1.113       dsl 
    988        1.1       cgd 	len = auio.uio_resid;
    989      1.115       dsl 	mp->msg_flags &= MSG_USERFLAGS;
    990      1.113       dsl 	error = (*so->so_receive)(so, from, &auio, NULL, control,
    991      1.129        ad 	    &mp->msg_flags);
    992      1.113       dsl 	len -= auio.uio_resid;
    993      1.113       dsl 	*retsize = len;
    994      1.113       dsl 	if (error != 0 && len != 0
    995      1.113       dsl 	    && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
    996      1.113       dsl 		/* Some data transferred */
    997      1.113       dsl 		error = 0;
    998      1.116        ad 
    999      1.157   mlelstv 	if (ktriov != NULL) {
   1000      1.116        ad 		ktrgeniov(s, UIO_READ, ktriov, len, error);
   1001      1.157   mlelstv 		kmem_free(ktriov, iovsz);
   1002        1.1       cgd 	}
   1003      1.116        ad 
   1004      1.113       dsl 	if (error != 0) {
   1005      1.113       dsl 		m_freem(*from);
   1006      1.113       dsl 		*from = NULL;
   1007      1.113       dsl 		if (control != NULL) {
   1008      1.113       dsl 			free_control_mbuf(l, *control, *control);
   1009      1.113       dsl 			*control = NULL;
   1010        1.1       cgd 		}
   1011        1.1       cgd 	}
   1012       1.43   thorpej  out:
   1013      1.113       dsl 	if (iov != aiov)
   1014      1.141     rmind 		kmem_free(iov, iovsz);
   1015      1.167     rmind 	return error;
   1016      1.155  christos }
   1017      1.155  christos 
   1018      1.155  christos 
   1019      1.155  christos int
   1020      1.183    martin do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp,
   1021      1.183    martin     const void *ktrhdr, size_t ktrsize,
   1022      1.183    martin     struct mbuf **from, struct mbuf **control, register_t *retsize)
   1023      1.155  christos {
   1024      1.155  christos 	int error;
   1025      1.155  christos 	struct socket *so;
   1026      1.155  christos 
   1027      1.155  christos 	if ((error = fd_getsock(s, &so)) != 0)
   1028      1.155  christos 		return error;
   1029      1.183    martin 	error = do_sys_recvmsg_so(l, s, so, mp, ktrhdr, ktrsize, from,
   1030      1.183    martin 	    control, retsize);
   1031      1.128        ad 	fd_putfile(s);
   1032      1.155  christos 	return error;
   1033        1.1       cgd }
   1034        1.1       cgd 
   1035      1.155  christos int
   1036      1.155  christos sys_recvmmsg(struct lwp *l, const struct sys_recvmmsg_args *uap,
   1037      1.155  christos     register_t *retval)
   1038      1.155  christos {
   1039      1.155  christos 	/* {
   1040      1.155  christos 		syscallarg(int)			s;
   1041      1.155  christos 		syscallarg(struct mmsghdr *)	mmsg;
   1042      1.155  christos 		syscallarg(unsigned int)	vlen;
   1043      1.155  christos 		syscallarg(unsigned int)	flags;
   1044      1.155  christos 		syscallarg(struct timespec *)	timeout;
   1045      1.155  christos 	} */
   1046      1.155  christos 	struct mmsghdr mmsg;
   1047      1.155  christos 	struct socket *so;
   1048      1.155  christos 	struct msghdr *msg = &mmsg.msg_hdr;
   1049      1.155  christos 	int error, s;
   1050      1.155  christos 	struct mbuf *from, *control;
   1051      1.155  christos 	struct timespec ts, now;
   1052      1.155  christos 	unsigned int vlen, flags, dg;
   1053      1.155  christos 
   1054      1.155  christos 	if (SCARG(uap, timeout)) {
   1055      1.155  christos 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
   1056      1.155  christos 			return error;
   1057      1.155  christos 		getnanotime(&now);
   1058      1.155  christos 		timespecadd(&now, &ts, &ts);
   1059      1.155  christos 	}
   1060      1.155  christos 
   1061      1.155  christos 	s = SCARG(uap, s);
   1062      1.155  christos 	if ((error = fd_getsock(s, &so)) != 0)
   1063      1.155  christos 		return error;
   1064      1.155  christos 
   1065      1.155  christos 	vlen = SCARG(uap, vlen);
   1066      1.155  christos 	if (vlen > 1024)
   1067      1.155  christos 		vlen = 1024;
   1068      1.155  christos 
   1069      1.155  christos 	from = NULL;
   1070      1.155  christos 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
   1071      1.155  christos 
   1072      1.155  christos 	for (dg = 0; dg < vlen;) {
   1073      1.155  christos 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
   1074      1.155  christos 		if (error)
   1075      1.155  christos 			break;
   1076      1.155  christos 
   1077      1.155  christos 		msg->msg_flags = flags & ~MSG_WAITFORONE;
   1078      1.155  christos 
   1079      1.155  christos 		if (from != NULL) {
   1080      1.155  christos 			m_free(from);
   1081      1.155  christos 			from = NULL;
   1082      1.155  christos 		}
   1083      1.155  christos 
   1084      1.183    martin 		error = do_sys_recvmsg_so(l, s, so, msg, NULL, 0, &from,
   1085      1.155  christos 		    msg->msg_control != NULL ? &control : NULL, retval);
   1086      1.166     seanb 		if (error) {
   1087      1.166     seanb 			if (error == EAGAIN && dg > 0)
   1088      1.166     seanb 				error = 0;
   1089      1.155  christos 			break;
   1090      1.166     seanb 		}
   1091      1.155  christos 
   1092      1.155  christos 		if (msg->msg_control != NULL)
   1093      1.155  christos 			error = copyout_msg_control(l, msg, control);
   1094      1.155  christos 		if (error)
   1095      1.155  christos 			break;
   1096      1.155  christos 
   1097      1.155  christos 		error = copyout_sockname(msg->msg_name, &msg->msg_namelen, 0,
   1098      1.155  christos 		    from);
   1099      1.155  christos 		if (error)
   1100      1.155  christos 			break;
   1101      1.155  christos 
   1102      1.155  christos 		ktrkuser("msghdr", msg, sizeof *msg);
   1103      1.155  christos 		mmsg.msg_len = *retval;
   1104      1.155  christos 
   1105      1.155  christos 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
   1106      1.155  christos 		if (error)
   1107      1.155  christos 			break;
   1108      1.155  christos 
   1109      1.155  christos 		dg++;
   1110      1.155  christos 		if (msg->msg_flags & MSG_OOB)
   1111      1.155  christos 			break;
   1112      1.155  christos 
   1113      1.155  christos 		if (SCARG(uap, timeout)) {
   1114      1.155  christos 			getnanotime(&now);
   1115      1.155  christos 			timespecsub(&now, &ts, &now);
   1116      1.155  christos 			if (now.tv_sec > 0)
   1117      1.155  christos 				break;
   1118      1.155  christos 		}
   1119      1.155  christos 
   1120      1.155  christos 		if (flags & MSG_WAITFORONE)
   1121      1.155  christos 			flags |= MSG_DONTWAIT;
   1122      1.155  christos 
   1123      1.155  christos 	}
   1124      1.155  christos 
   1125      1.155  christos 	if (from != NULL)
   1126      1.155  christos 		m_free(from);
   1127      1.155  christos 
   1128      1.155  christos 	*retval = dg;
   1129      1.155  christos 	if (error)
   1130      1.155  christos 		so->so_error = error;
   1131      1.155  christos 
   1132      1.155  christos 	fd_putfile(s);
   1133      1.155  christos 
   1134      1.155  christos 	/*
   1135      1.155  christos 	 * If we succeeded at least once, return 0, hopefully so->so_error
   1136      1.155  christos 	 * will catch it next time.
   1137      1.155  christos 	 */
   1138      1.155  christos 	if (dg)
   1139      1.155  christos 		return 0;
   1140      1.155  christos 
   1141      1.155  christos 	return error;
   1142      1.155  christos }
   1143      1.113       dsl 
   1144        1.7   mycroft int
   1145      1.167     rmind sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap,
   1146      1.167     rmind     register_t *retval)
   1147       1.15   thorpej {
   1148      1.125       dsl 	/* {
   1149       1.57     lukem 		syscallarg(int)	s;
   1150       1.57     lukem 		syscallarg(int)	how;
   1151      1.125       dsl 	} */
   1152      1.128        ad 	struct socket	*so;
   1153       1.57     lukem 	int		error;
   1154        1.1       cgd 
   1155      1.128        ad 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
   1156      1.167     rmind 		return error;
   1157      1.129        ad 	solock(so);
   1158      1.128        ad 	error = soshutdown(so, SCARG(uap, how));
   1159      1.129        ad 	sounlock(so);
   1160      1.128        ad 	fd_putfile(SCARG(uap, s));
   1161      1.167     rmind 	return error;
   1162        1.1       cgd }
   1163        1.1       cgd 
   1164        1.7   mycroft int
   1165      1.167     rmind sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap,
   1166      1.167     rmind     register_t *retval)
   1167       1.15   thorpej {
   1168      1.125       dsl 	/* {
   1169       1.57     lukem 		syscallarg(int)			s;
   1170       1.57     lukem 		syscallarg(int)			level;
   1171       1.57     lukem 		syscallarg(int)			name;
   1172       1.57     lukem 		syscallarg(const void *)	val;
   1173       1.57     lukem 		syscallarg(unsigned int)	valsize;
   1174      1.125       dsl 	} */
   1175      1.134    plunky 	struct sockopt	sopt;
   1176       1.76      matt 	struct socket	*so;
   1177      1.151  christos 	file_t		*fp;
   1178       1.57     lukem 	int		error;
   1179       1.75   thorpej 	unsigned int	len;
   1180        1.1       cgd 
   1181      1.134    plunky 	len = SCARG(uap, valsize);
   1182      1.134    plunky 	if (len > 0 && SCARG(uap, val) == NULL)
   1183      1.167     rmind 		return EINVAL;
   1184      1.134    plunky 
   1185      1.134    plunky 	if (len > MCLBYTES)
   1186      1.167     rmind 		return EINVAL;
   1187      1.134    plunky 
   1188      1.151  christos 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
   1189        1.1       cgd 		return (error);
   1190      1.134    plunky 
   1191      1.134    plunky 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len);
   1192      1.134    plunky 
   1193      1.134    plunky 	if (len > 0) {
   1194      1.134    plunky 		error = copyin(SCARG(uap, val), sopt.sopt_data, len);
   1195      1.134    plunky 		if (error)
   1196       1.43   thorpej 			goto out;
   1197        1.1       cgd 	}
   1198      1.134    plunky 
   1199      1.134    plunky 	error = sosetopt(so, &sopt);
   1200      1.151  christos 	if (so->so_options & SO_NOSIGPIPE)
   1201      1.152  christos 		atomic_or_uint(&fp->f_flag, FNOSIGPIPE);
   1202      1.151  christos 	else
   1203      1.152  christos 		atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE);
   1204      1.134    plunky 
   1205       1.43   thorpej  out:
   1206      1.134    plunky 	sockopt_destroy(&sopt);
   1207      1.167     rmind 	fd_putfile(SCARG(uap, s));
   1208      1.167     rmind 	return error;
   1209        1.1       cgd }
   1210        1.1       cgd 
   1211        1.7   mycroft int
   1212      1.167     rmind sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap,
   1213      1.167     rmind     register_t *retval)
   1214       1.15   thorpej {
   1215      1.125       dsl 	/* {
   1216       1.57     lukem 		syscallarg(int)			s;
   1217       1.57     lukem 		syscallarg(int)			level;
   1218       1.57     lukem 		syscallarg(int)			name;
   1219       1.57     lukem 		syscallarg(void *)		val;
   1220       1.57     lukem 		syscallarg(unsigned int *)	avalsize;
   1221      1.125       dsl 	} */
   1222      1.134    plunky 	struct sockopt	sopt;
   1223      1.128        ad 	struct socket	*so;
   1224      1.151  christos 	file_t		*fp;
   1225      1.134    plunky 	unsigned int	valsize, len;
   1226       1.57     lukem 	int		error;
   1227        1.1       cgd 
   1228      1.134    plunky 	if (SCARG(uap, val) != NULL) {
   1229      1.134    plunky 		error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize));
   1230      1.134    plunky 		if (error)
   1231      1.167     rmind 			return error;
   1232      1.134    plunky 	} else
   1233      1.134    plunky 		valsize = 0;
   1234      1.134    plunky 
   1235      1.151  christos 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
   1236        1.1       cgd 		return (error);
   1237      1.134    plunky 
   1238      1.134    plunky 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), 0);
   1239      1.134    plunky 
   1240      1.151  christos 	if (fp->f_flag & FNOSIGPIPE)
   1241      1.154  christos 		so->so_options |= SO_NOSIGPIPE;
   1242      1.151  christos 	else
   1243      1.154  christos 		so->so_options &= ~SO_NOSIGPIPE;
   1244      1.134    plunky 	error = sogetopt(so, &sopt);
   1245      1.134    plunky 	if (error)
   1246      1.134    plunky 		goto out;
   1247      1.134    plunky 
   1248      1.134    plunky 	if (valsize > 0) {
   1249      1.134    plunky 		len = min(valsize, sopt.sopt_size);
   1250      1.134    plunky 		error = copyout(sopt.sopt_data, SCARG(uap, val), len);
   1251      1.134    plunky 		if (error)
   1252      1.134    plunky 			goto out;
   1253      1.134    plunky 
   1254      1.134    plunky 		error = copyout(&len, SCARG(uap, avalsize), sizeof(len));
   1255       1.18  christos 		if (error)
   1256       1.43   thorpej 			goto out;
   1257        1.1       cgd 	}
   1258      1.134    plunky 
   1259       1.43   thorpej  out:
   1260      1.134    plunky 	sockopt_destroy(&sopt);
   1261      1.167     rmind 	fd_putfile(SCARG(uap, s));
   1262      1.167     rmind 	return error;
   1263        1.1       cgd }
   1264        1.1       cgd 
   1265       1.68  jdolecek #ifdef PIPE_SOCKETPAIR
   1266      1.167     rmind 
   1267        1.7   mycroft int
   1268      1.142  christos pipe1(struct lwp *l, register_t *retval, int flags)
   1269        1.1       cgd {
   1270      1.128        ad 	file_t		*rf, *wf;
   1271       1.57     lukem 	struct socket	*rso, *wso;
   1272       1.57     lukem 	int		fd, error;
   1273      1.128        ad 	proc_t		*p;
   1274        1.1       cgd 
   1275      1.151  christos 	if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
   1276      1.145  christos 		return EINVAL;
   1277      1.128        ad 	p = curproc;
   1278      1.129        ad 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
   1279      1.167     rmind 		return error;
   1280      1.129        ad 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
   1281        1.1       cgd 		goto free1;
   1282       1.58      manu 	/* remember this socket pair implements a pipe */
   1283       1.58      manu 	wso->so_state |= SS_ISAPIPE;
   1284       1.58      manu 	rso->so_state |= SS_ISAPIPE;
   1285      1.128        ad 	if ((error = fd_allocfile(&rf, &fd)) != 0)
   1286        1.1       cgd 		goto free2;
   1287        1.1       cgd 	retval[0] = fd;
   1288      1.142  christos 	rf->f_flag = FREAD | flags;
   1289        1.1       cgd 	rf->f_type = DTYPE_SOCKET;
   1290        1.1       cgd 	rf->f_ops = &socketops;
   1291      1.173      matt 	rf->f_socket = rso;
   1292      1.128        ad 	if ((error = fd_allocfile(&wf, &fd)) != 0)
   1293        1.1       cgd 		goto free3;
   1294      1.142  christos 	wf->f_flag = FWRITE | flags;
   1295        1.1       cgd 	wf->f_type = DTYPE_SOCKET;
   1296        1.1       cgd 	wf->f_ops = &socketops;
   1297      1.173      matt 	wf->f_socket = wso;
   1298        1.1       cgd 	retval[1] = fd;
   1299      1.129        ad 	solock(wso);
   1300      1.172       rtr 	error = unp_connect2(wso, rso);
   1301      1.129        ad 	sounlock(wso);
   1302      1.129        ad 	if (error != 0)
   1303        1.1       cgd 		goto free4;
   1304      1.128        ad 	fd_affix(p, wf, (int)retval[1]);
   1305      1.128        ad 	fd_affix(p, rf, (int)retval[0]);
   1306        1.1       cgd 	return (0);
   1307       1.57     lukem  free4:
   1308      1.128        ad 	fd_abort(p, wf, (int)retval[1]);
   1309       1.57     lukem  free3:
   1310      1.128        ad 	fd_abort(p, rf, (int)retval[0]);
   1311       1.57     lukem  free2:
   1312        1.1       cgd 	(void)soclose(wso);
   1313       1.57     lukem  free1:
   1314        1.1       cgd 	(void)soclose(rso);
   1315      1.167     rmind 	return error;
   1316        1.1       cgd }
   1317       1.68  jdolecek #endif /* PIPE_SOCKETPAIR */
   1318        1.1       cgd 
   1319        1.1       cgd /*
   1320      1.171       rtr  * Get peer socket name.
   1321        1.1       cgd  */
   1322        1.7   mycroft int
   1323      1.176       rtr do_sys_getpeername(int fd, struct sockaddr *nam)
   1324       1.15   thorpej {
   1325       1.57     lukem 	struct socket	*so;
   1326       1.57     lukem 	int		error;
   1327        1.1       cgd 
   1328      1.128        ad 	if ((error = fd_getsock(fd, &so)) != 0)
   1329      1.113       dsl 		return error;
   1330      1.113       dsl 
   1331      1.129        ad 	solock(so);
   1332      1.171       rtr 	if ((so->so_state & SS_ISCONNECTED) == 0)
   1333      1.113       dsl 		error = ENOTCONN;
   1334      1.171       rtr 	else {
   1335      1.176       rtr 		error = (*so->so_proto->pr_usrreqs->pr_peeraddr)(so, nam);
   1336      1.113       dsl 	}
   1337      1.167     rmind 	sounlock(so);
   1338      1.167     rmind 	fd_putfile(fd);
   1339      1.113       dsl 	return error;
   1340      1.113       dsl }
   1341      1.113       dsl 
   1342      1.171       rtr /*
   1343      1.171       rtr  * Get local socket name.
   1344      1.171       rtr  */
   1345      1.171       rtr int
   1346      1.176       rtr do_sys_getsockname(int fd, struct sockaddr *nam)
   1347      1.171       rtr {
   1348      1.171       rtr 	struct socket	*so;
   1349      1.171       rtr 	int		error;
   1350      1.171       rtr 
   1351      1.171       rtr 	if ((error = fd_getsock(fd, &so)) != 0)
   1352      1.171       rtr 		return error;
   1353      1.171       rtr 
   1354      1.171       rtr 	solock(so);
   1355      1.176       rtr 	error = (*so->so_proto->pr_usrreqs->pr_sockaddr)(so, nam);
   1356      1.171       rtr 	sounlock(so);
   1357      1.171       rtr 	fd_putfile(fd);
   1358      1.171       rtr 	return error;
   1359      1.171       rtr }
   1360      1.171       rtr 
   1361      1.113       dsl int
   1362      1.176       rtr copyout_sockname_sb(struct sockaddr *asa, unsigned int *alen, int flags,
   1363      1.176       rtr     struct sockaddr_big *addr)
   1364      1.176       rtr {
   1365      1.176       rtr 	int len;
   1366      1.176       rtr 	int error;
   1367      1.176       rtr 
   1368      1.176       rtr 	if (asa == NULL)
   1369      1.176       rtr 		/* Assume application not interested */
   1370      1.176       rtr 		return 0;
   1371      1.176       rtr 
   1372      1.176       rtr 	if (flags & MSG_LENUSRSPACE) {
   1373      1.176       rtr 		error = copyin(alen, &len, sizeof(len));
   1374      1.176       rtr 		if (error)
   1375      1.176       rtr 			return error;
   1376      1.176       rtr 	} else
   1377      1.176       rtr 		len = *alen;
   1378      1.176       rtr 	if (len < 0)
   1379      1.176       rtr 		return EINVAL;
   1380      1.176       rtr 
   1381      1.176       rtr 	if (addr == NULL) {
   1382      1.176       rtr 		len = 0;
   1383      1.176       rtr 		error = 0;
   1384      1.176       rtr 	} else {
   1385      1.176       rtr 		if (len > addr->sb_len)
   1386      1.176       rtr 			len = addr->sb_len;
   1387      1.176       rtr 		/* XXX addr isn't an mbuf... */
   1388      1.176       rtr 		ktrkuser(mbuftypes[MT_SONAME], addr, len);
   1389      1.176       rtr 		error = copyout(addr, asa, len);
   1390      1.176       rtr 	}
   1391      1.176       rtr 
   1392      1.176       rtr 	if (error == 0) {
   1393      1.176       rtr 		if (flags & MSG_LENUSRSPACE)
   1394      1.176       rtr 			error = copyout(&len, alen, sizeof(len));
   1395      1.176       rtr 		else
   1396      1.176       rtr 			*alen = len;
   1397      1.176       rtr 	}
   1398      1.176       rtr 
   1399      1.176       rtr 	return error;
   1400      1.176       rtr }
   1401      1.176       rtr 
   1402      1.176       rtr int
   1403      1.113       dsl copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
   1404      1.113       dsl     struct mbuf *addr)
   1405      1.113       dsl {
   1406      1.113       dsl 	int len;
   1407      1.113       dsl 	int error;
   1408      1.113       dsl 
   1409      1.113       dsl 	if (asa == NULL)
   1410      1.113       dsl 		/* Assume application not interested */
   1411      1.113       dsl 		return 0;
   1412      1.113       dsl 
   1413      1.113       dsl 	if (flags & MSG_LENUSRSPACE) {
   1414      1.113       dsl 		error = copyin(alen, &len, sizeof(len));
   1415      1.113       dsl 		if (error)
   1416      1.113       dsl 			return error;
   1417      1.113       dsl 	} else
   1418      1.113       dsl 		len = *alen;
   1419      1.118       dsl 	if (len < 0)
   1420      1.113       dsl 		return EINVAL;
   1421      1.113       dsl 
   1422      1.113       dsl 	if (addr == NULL) {
   1423      1.113       dsl 		len = 0;
   1424      1.113       dsl 		error = 0;
   1425      1.113       dsl 	} else {
   1426      1.113       dsl 		if (len > addr->m_len)
   1427      1.113       dsl 			len = addr->m_len;
   1428      1.113       dsl 		/* Maybe this ought to copy a chain ? */
   1429      1.161  christos 		ktrkuser(mbuftypes[MT_SONAME], mtod(addr, void *), len);
   1430      1.113       dsl 		error = copyout(mtod(addr, void *), asa, len);
   1431      1.113       dsl 	}
   1432      1.113       dsl 
   1433      1.113       dsl 	if (error == 0) {
   1434      1.113       dsl 		if (flags & MSG_LENUSRSPACE)
   1435      1.113       dsl 			error = copyout(&len, alen, sizeof(len));
   1436      1.113       dsl 		else
   1437      1.113       dsl 			*alen = len;
   1438      1.113       dsl 	}
   1439      1.113       dsl 
   1440      1.113       dsl 	return error;
   1441      1.113       dsl }
   1442      1.113       dsl 
   1443      1.113       dsl /*
   1444      1.113       dsl  * Get socket name.
   1445      1.113       dsl  */
   1446      1.113       dsl int
   1447      1.167     rmind sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap,
   1448      1.167     rmind     register_t *retval)
   1449      1.113       dsl {
   1450      1.125       dsl 	/* {
   1451      1.113       dsl 		syscallarg(int)			fdes;
   1452      1.113       dsl 		syscallarg(struct sockaddr *)	asa;
   1453      1.113       dsl 		syscallarg(unsigned int *)	alen;
   1454      1.125       dsl 	} */
   1455      1.176       rtr 	struct sockaddr_big sbig;
   1456      1.176       rtr 	int		    error;
   1457      1.113       dsl 
   1458      1.176       rtr 	sbig.sb_len = UCHAR_MAX;
   1459      1.176       rtr 	error = do_sys_getsockname(SCARG(uap, fdes), (struct sockaddr *)&sbig);
   1460      1.113       dsl 	if (error != 0)
   1461      1.113       dsl 		return error;
   1462      1.113       dsl 
   1463      1.176       rtr 	error = copyout_sockname_sb(SCARG(uap, asa), SCARG(uap, alen),
   1464      1.176       rtr 	    MSG_LENUSRSPACE, &sbig);
   1465      1.113       dsl 	return error;
   1466        1.1       cgd }
   1467        1.1       cgd 
   1468        1.1       cgd /*
   1469        1.1       cgd  * Get name of peer for connected socket.
   1470        1.1       cgd  */
   1471        1.7   mycroft int
   1472      1.167     rmind sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap,
   1473      1.167     rmind     register_t *retval)
   1474       1.15   thorpej {
   1475      1.125       dsl 	/* {
   1476       1.57     lukem 		syscallarg(int)			fdes;
   1477       1.57     lukem 		syscallarg(struct sockaddr *)	asa;
   1478       1.57     lukem 		syscallarg(unsigned int *)	alen;
   1479      1.125       dsl 	} */
   1480      1.176       rtr 	struct sockaddr_big sbig;
   1481      1.176       rtr 	int		    error;
   1482        1.1       cgd 
   1483      1.176       rtr 	sbig.sb_len = UCHAR_MAX;
   1484      1.176       rtr 	error = do_sys_getpeername(SCARG(uap, fdes), (struct sockaddr *)&sbig);
   1485      1.113       dsl 	if (error != 0)
   1486      1.113       dsl 		return error;
   1487      1.113       dsl 
   1488      1.176       rtr 	error = copyout_sockname_sb(SCARG(uap, asa), SCARG(uap, alen),
   1489      1.176       rtr 	    MSG_LENUSRSPACE, &sbig);
   1490      1.113       dsl 	return error;
   1491        1.1       cgd }
   1492        1.1       cgd 
   1493      1.175       rtr static int
   1494      1.175       rtr sockargs_sb(struct sockaddr_big *sb, const void *name, socklen_t buflen)
   1495      1.175       rtr {
   1496      1.175       rtr 	int error;
   1497      1.175       rtr 
   1498      1.175       rtr 	/*
   1499      1.175       rtr 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1500      1.175       rtr 	 * will overflow sb_len. Further no reasonable buflen is <=
   1501      1.175       rtr 	 * offsetof(sockaddr_big, sb_data) since it shall be at least
   1502      1.175       rtr 	 * the size of the preamble sb_len and sb_family members.
   1503      1.175       rtr 	 */
   1504      1.175       rtr 	if (buflen > UCHAR_MAX ||
   1505      1.175       rtr 	    buflen <= offsetof(struct sockaddr_big, sb_data))
   1506      1.175       rtr 		return EINVAL;
   1507      1.175       rtr 
   1508      1.175       rtr 	error = copyin(name, (void *)sb, buflen);
   1509      1.175       rtr 	if (error)
   1510      1.175       rtr 		return error;
   1511      1.175       rtr 
   1512      1.184  christos 	ktrkuser(mbuftypes[MT_SONAME], sb, buflen);
   1513      1.175       rtr #if BYTE_ORDER != BIG_ENDIAN
   1514      1.175       rtr 	/*
   1515      1.175       rtr 	 * 4.3BSD compat thing - need to stay, since bind(2),
   1516      1.175       rtr 	 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1517      1.175       rtr 	 */
   1518      1.175       rtr 	if (sb->sb_family == 0 && sb->sb_len < AF_MAX)
   1519      1.175       rtr 		sb->sb_family = sb->sb_len;
   1520      1.175       rtr #endif
   1521      1.175       rtr 	sb->sb_len = buflen;
   1522      1.175       rtr 	return 0;
   1523      1.175       rtr }
   1524      1.175       rtr 
   1525       1.24   thorpej /*
   1526       1.24   thorpej  * XXX In a perfect world, we wouldn't pass around socket control
   1527       1.24   thorpej  * XXX arguments in mbufs, and this could go away.
   1528       1.24   thorpej  */
   1529        1.7   mycroft int
   1530       1.91  christos sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
   1531        1.1       cgd {
   1532       1.57     lukem 	struct sockaddr	*sa;
   1533       1.57     lukem 	struct mbuf	*m;
   1534       1.57     lukem 	int		error;
   1535        1.1       cgd 
   1536       1.24   thorpej 	/*
   1537       1.25   thorpej 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1538       1.64      matt 	 * will overflow sa_len.  Control data more than a page size in
   1539       1.64      matt 	 * length is just too much.
   1540       1.24   thorpej 	 */
   1541       1.64      matt 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
   1542      1.167     rmind 		return EINVAL;
   1543       1.24   thorpej 
   1544      1.174       rtr 	/*
   1545      1.174       rtr 	 * length must greater than sizeof(sa_family) + sizeof(sa_len)
   1546      1.174       rtr 	 */
   1547      1.174       rtr 	if (type == MT_SONAME && buflen <= 2)
   1548      1.174       rtr 		return EINVAL;
   1549      1.174       rtr 
   1550       1.24   thorpej 	/* Allocate an mbuf to hold the arguments. */
   1551       1.24   thorpej 	m = m_get(M_WAIT, type);
   1552       1.76      matt 	/* can't claim.  don't who to assign it to. */
   1553       1.64      matt 	if (buflen > MLEN) {
   1554       1.24   thorpej 		/*
   1555       1.24   thorpej 		 * Won't fit into a regular mbuf, so we allocate just
   1556       1.24   thorpej 		 * enough external storage to hold the argument.
   1557       1.24   thorpej 		 */
   1558       1.24   thorpej 		MEXTMALLOC(m, buflen, M_WAITOK);
   1559        1.1       cgd 	}
   1560        1.1       cgd 	m->m_len = buflen;
   1561      1.107  christos 	error = copyin(bf, mtod(m, void *), buflen);
   1562        1.1       cgd 	if (error) {
   1563      1.167     rmind 		(void)m_free(m);
   1564      1.167     rmind 		return error;
   1565        1.1       cgd 	}
   1566      1.147  christos 	ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
   1567        1.1       cgd 	*mp = m;
   1568        1.1       cgd 	if (type == MT_SONAME) {
   1569        1.7   mycroft 		sa = mtod(m, struct sockaddr *);
   1570       1.65  jdolecek #if BYTE_ORDER != BIG_ENDIAN
   1571       1.65  jdolecek 		/*
   1572       1.65  jdolecek 		 * 4.3BSD compat thing - need to stay, since bind(2),
   1573       1.65  jdolecek 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1574       1.65  jdolecek 		 */
   1575        1.1       cgd 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1576        1.1       cgd 			sa->sa_family = sa->sa_len;
   1577        1.1       cgd #endif
   1578        1.1       cgd 		sa->sa_len = buflen;
   1579        1.1       cgd 	}
   1580      1.167     rmind 	return 0;
   1581        1.1       cgd }
   1582