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