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