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