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