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