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