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