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