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