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