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