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