Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_socket.c revision 1.1.4.5
      1 /*	$NetBSD: netbsd32_socket.c,v 1.1.4.5 2002/08/23 02:37:11 petrov Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_socket.c,v 1.1.4.5 2002/08/23 02:37:11 petrov Exp $");
     33 
     34 #if defined(_KERNEL_OPT)
     35 #include "opt_ktrace.h"
     36 #endif
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #define msg __msg /* Don't ask me! */
     41 #include <sys/malloc.h>
     42 #include <sys/mount.h>
     43 #include <sys/socket.h>
     44 #include <sys/sockio.h>
     45 #include <sys/socketvar.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/ktrace.h>
     48 #include <sys/file.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/sa.h>
     51 #include <sys/syscallargs.h>
     52 #include <sys/proc.h>
     53 
     54 #include <compat/netbsd32/netbsd32.h>
     55 #include <compat/netbsd32/netbsd32_syscallargs.h>
     56 #include <compat/netbsd32/netbsd32_conv.h>
     57 
     58 /* note that the netbsd32_msghdr's iov really points to a struct iovec, not a netbsd32_iovec. */
     59 static int recvit32 __P((struct proc *, int, struct netbsd32_msghdr *, struct iovec *, caddr_t,
     60 			 register_t *));
     61 
     62 int
     63 netbsd32_recvmsg(l, v, retval)
     64 	struct lwp *l;
     65 	void *v;
     66 	register_t *retval;
     67 {
     68 	struct netbsd32_recvmsg_args /* {
     69 		syscallarg(int) s;
     70 		syscallarg(netbsd32_msghdrp_t) msg;
     71 		syscallarg(int) flags;
     72 	} */ *uap = v;
     73 	struct netbsd32_msghdr msg;
     74 	struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
     75 	int error;
     76 
     77 	error = copyin((caddr_t)(u_long)SCARG(uap, msg), (caddr_t)&msg,
     78 		       sizeof(msg));
     79 		/* netbsd32_msghdr needs the iov pre-allocated */
     80 	if (error)
     81 		return (error);
     82 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
     83 		if ((u_int)msg.msg_iovlen > IOV_MAX)
     84 			return (EMSGSIZE);
     85 		MALLOC(iov, struct iovec *,
     86 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
     87 		       M_WAITOK);
     88 	} else if ((u_int)msg.msg_iovlen > 0)
     89 		iov = aiov;
     90 	else
     91 		return (EMSGSIZE);
     92 	msg.msg_flags = SCARG(uap, flags);
     93 	uiov = (struct iovec *)(u_long)msg.msg_iov;
     94 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)uiov,
     95 				   iov, msg.msg_iovlen);
     96 	if (error)
     97 		goto done;
     98 	if ((error = recvit32(l->l_proc, SCARG(uap, s), &msg, iov, (caddr_t)0, retval)) == 0) {
     99 		error = copyout((caddr_t)&msg, (caddr_t)(u_long)SCARG(uap, msg),
    100 		    sizeof(msg));
    101 	}
    102 done:
    103 	if (iov != aiov)
    104 		FREE(iov, M_IOV);
    105 	return (error);
    106 }
    107 
    108 int
    109 recvit32(p, s, mp, iov, namelenp, retsize)
    110 	struct proc *p;
    111 	int s;
    112 	struct netbsd32_msghdr *mp;
    113 	struct iovec *iov;
    114 	caddr_t namelenp;
    115 	register_t *retsize;
    116 {
    117 	struct file *fp;
    118 	struct uio auio;
    119 	int i;
    120 	int len, error;
    121 	struct mbuf *from = 0, *control = 0;
    122 	struct socket *so;
    123 #ifdef KTRACE
    124 	struct iovec *ktriov = NULL;
    125 #endif
    126 
    127 	/* getsock() will use the descriptor for us */
    128 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    129 		return (error);
    130 	auio.uio_iov = iov;
    131 	auio.uio_iovcnt = mp->msg_iovlen;
    132 	auio.uio_segflg = UIO_USERSPACE;
    133 	auio.uio_rw = UIO_READ;
    134 	auio.uio_procp = p;
    135 	auio.uio_offset = 0;			/* XXX */
    136 	auio.uio_resid = 0;
    137 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    138 #if 0
    139 		/* cannot happen iov_len is unsigned */
    140 		if (iov->iov_len < 0) {
    141 			error = EINVAL;
    142 			goto out1;
    143 		}
    144 #endif
    145 		/*
    146 		 * Reads return ssize_t because -1 is returned on error.
    147 		 * Therefore we must restrict the length to SSIZE_MAX to
    148 		 * avoid garbage return values.
    149 		 */
    150 		auio.uio_resid += iov->iov_len;
    151 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    152 			error = EINVAL;
    153 			goto out1;
    154 		}
    155 	}
    156 #ifdef KTRACE
    157 	if (KTRPOINT(p, KTR_GENIO)) {
    158 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    159 
    160 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
    161 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
    162 	}
    163 #endif
    164 	len = auio.uio_resid;
    165 	so = (struct socket *)fp->f_data;
    166 	error = (*so->so_receive)(so, &from, &auio, NULL,
    167 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
    168 	if (error) {
    169 		if (auio.uio_resid != len && (error == ERESTART ||
    170 		    error == EINTR || error == EWOULDBLOCK))
    171 			error = 0;
    172 	}
    173 #ifdef KTRACE
    174 	if (ktriov != NULL) {
    175 		if (error == 0)
    176 			ktrgenio(p, s, UIO_READ, ktriov,
    177 			    len - auio.uio_resid, error);
    178 		FREE(ktriov, M_TEMP);
    179 	}
    180 #endif
    181 	if (error)
    182 		goto out;
    183 	*retsize = len - auio.uio_resid;
    184 	if (mp->msg_name) {
    185 		len = mp->msg_namelen;
    186 		if (len <= 0 || from == 0)
    187 			len = 0;
    188 		else {
    189 			if (len > from->m_len)
    190 				len = from->m_len;
    191 			/* else if len < from->m_len ??? */
    192 			error = copyout(mtod(from, caddr_t),
    193 					(caddr_t)(u_long)mp->msg_name, (unsigned)len);
    194 			if (error)
    195 				goto out;
    196 		}
    197 		mp->msg_namelen = len;
    198 		if (namelenp &&
    199 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int))))
    200 			goto out;
    201 	}
    202 	if (mp->msg_control) {
    203 		len = mp->msg_controllen;
    204 		if (len <= 0 || control == 0)
    205 			len = 0;
    206 		else {
    207 			struct mbuf *m = control;
    208 			caddr_t p = (caddr_t)(u_long)mp->msg_control;
    209 
    210 			do {
    211 				i = m->m_len;
    212 				if (len < i) {
    213 					mp->msg_flags |= MSG_CTRUNC;
    214 					i = len;
    215 				}
    216 				error = copyout(mtod(m, caddr_t), p,
    217 				    (unsigned)i);
    218 				if (m->m_next)
    219 					i = ALIGN(i);
    220 				p += i;
    221 				len -= i;
    222 				if (error != 0 || len <= 0)
    223 					break;
    224 			} while ((m = m->m_next) != NULL);
    225 			len = p - (caddr_t)(u_long)mp->msg_control;
    226 		}
    227 		mp->msg_controllen = len;
    228 	}
    229  out:
    230 	if (from)
    231 		m_freem(from);
    232 	if (control)
    233 		m_freem(control);
    234  out1:
    235 	FILE_UNUSE(fp, p);
    236 	return (error);
    237 }
    238 
    239 int
    240 netbsd32_sendmsg(l, v, retval)
    241 	struct lwp *l;
    242 	void *v;
    243 	register_t *retval;
    244 {
    245 	struct netbsd32_sendmsg_args /* {
    246 		syscallarg(int) s;
    247 		syscallarg(const netbsd32_msghdrp_t) msg;
    248 		syscallarg(int) flags;
    249 	} */ *uap = v;
    250 	struct msghdr msg;
    251 	struct netbsd32_msghdr msg32;
    252 	struct iovec aiov[UIO_SMALLIOV], *iov;
    253 	int error;
    254 
    255 	error = copyin((caddr_t)(u_long)SCARG(uap, msg),
    256 		       (caddr_t)&msg32, sizeof(msg32));
    257 	if (error)
    258 		return (error);
    259 	netbsd32_to_msghdr(&msg32, &msg);
    260 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
    261 		if ((u_int)msg.msg_iovlen > IOV_MAX)
    262 			return (EMSGSIZE);
    263 		MALLOC(iov, struct iovec *,
    264 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
    265 		       M_WAITOK);
    266 	} else if ((u_int)msg.msg_iovlen > 0)
    267 		iov = aiov;
    268 	else
    269 		return (EMSGSIZE);
    270 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)msg.msg_iov,
    271 				   iov, msg.msg_iovlen);
    272 	if (error)
    273 		goto done;
    274 	msg.msg_iov = iov;
    275 	/* Luckily we can use this directly */
    276 	error = sendit(l->l_proc, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    277 done:
    278 	if (iov != aiov)
    279 		FREE(iov, M_IOV);
    280 	return (error);
    281 }
    282 
    283 int
    284 netbsd32_recvfrom(l, v, retval)
    285 	struct lwp *l;
    286 	void *v;
    287 	register_t *retval;
    288 {
    289 	struct netbsd32_recvfrom_args /* {
    290 		syscallarg(int) s;
    291 		syscallarg(netbsd32_voidp) buf;
    292 		syscallarg(netbsd32_size_t) len;
    293 		syscallarg(int) flags;
    294 		syscallarg(netbsd32_sockaddrp_t) from;
    295 		syscallarg(netbsd32_intp) fromlenaddr;
    296 	} */ *uap = v;
    297 	struct netbsd32_msghdr msg;
    298 	struct iovec aiov;
    299 	int error;
    300 
    301 	if (SCARG(uap, fromlenaddr)) {
    302 		error = copyin((caddr_t)(u_long)SCARG(uap, fromlenaddr),
    303 			       (caddr_t)&msg.msg_namelen,
    304 			       sizeof(msg.msg_namelen));
    305 		if (error)
    306 			return (error);
    307 	} else
    308 		msg.msg_namelen = 0;
    309 	msg.msg_name = SCARG(uap, from);
    310 	msg.msg_iov = NULL; /* ignored in recvit32(), uses iov */
    311 	msg.msg_iovlen = 1;
    312 	aiov.iov_base = (caddr_t)(u_long)SCARG(uap, buf);
    313 	aiov.iov_len = (u_long)SCARG(uap, len);
    314 	msg.msg_control = 0;
    315 	msg.msg_flags = SCARG(uap, flags);
    316 	return (recvit32(l->l_proc, SCARG(uap, s), &msg, &aiov,
    317 		       (caddr_t)(u_long)SCARG(uap, fromlenaddr), retval));
    318 }
    319 
    320 int
    321 netbsd32_sendto(l, v, retval)
    322 	struct lwp *l;
    323 	void *v;
    324 	register_t *retval;
    325 {
    326 	struct netbsd32_sendto_args /* {
    327 		syscallarg(int) s;
    328 		syscallarg(const netbsd32_voidp) buf;
    329 		syscallarg(netbsd32_size_t) len;
    330 		syscallarg(int) flags;
    331 		syscallarg(const netbsd32_sockaddrp_t) to;
    332 		syscallarg(int) tolen;
    333 	} */ *uap = v;
    334 	struct msghdr msg;
    335 	struct iovec aiov;
    336 
    337 	msg.msg_name = (caddr_t)(u_long)SCARG(uap, to);		/* XXX kills const */
    338 	msg.msg_namelen = SCARG(uap, tolen);
    339 	msg.msg_iov = &aiov;
    340 	msg.msg_iovlen = 1;
    341 	msg.msg_control = 0;
    342 	aiov.iov_base = (char *)(u_long)SCARG(uap, buf);	/* XXX kills const */
    343 	aiov.iov_len = SCARG(uap, len);
    344 	return (sendit(l->l_proc, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
    345 }
    346