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