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