netbsd32_socket.c revision 1.26 1 /* $NetBSD: netbsd32_socket.c,v 1.26 2007/06/01 22:53:52 dsl 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.26 2007/06/01 22:53:52 dsl 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/syscallargs.h>
51 #include <sys/proc.h>
52 #include <sys/dirent.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 lwp *, int, struct netbsd32_msghdr *, struct iovec *, void *,
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(SCARG_P32(uap, msg), &msg, sizeof(msg));
78 /* netbsd32_msghdr needs the iov pre-allocated */
79 if (error)
80 return (error);
81 if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
82 if ((u_int)msg.msg_iovlen > IOV_MAX)
83 return (EMSGSIZE);
84 iov = (struct iovec *)malloc(
85 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
86 M_WAITOK);
87 } else
88 iov = aiov;
89 msg.msg_flags = SCARG(uap, flags);
90 uiov = (struct iovec *)NETBSD32PTR64(msg.msg_iov);
91 error = netbsd32_to_iovecin((struct netbsd32_iovec *)uiov,
92 iov, msg.msg_iovlen);
93 if (error)
94 goto done;
95 if ((error = recvit32(l, SCARG(uap, s), &msg, iov, (void *)0,
96 retval)) == 0) {
97 error = copyout(&msg, SCARG_P32(uap, msg), sizeof(msg));
98 }
99 done:
100 if (iov != aiov)
101 FREE(iov, M_IOV);
102 return (error);
103 }
104
105 int
106 recvit32(l, s, mp, iov, namelenp, retsize)
107 struct lwp *l;
108 int s;
109 struct netbsd32_msghdr *mp;
110 struct iovec *iov;
111 void *namelenp;
112 register_t *retsize;
113 {
114 struct file *fp;
115 struct uio auio;
116 int i;
117 int len, error;
118 struct mbuf *from = 0, *control = 0;
119 struct socket *so;
120 struct proc *p;
121 #ifdef KTRACE
122 struct iovec *ktriov = NULL;
123 #endif
124 p = l->l_proc;
125
126 /* getsock() will use the descriptor for us */
127 if ((error = getsock(p->p_fd, s, &fp)) != 0)
128 return (error);
129 auio.uio_iov = iov;
130 auio.uio_iovcnt = mp->msg_iovlen;
131 auio.uio_rw = UIO_READ;
132 auio.uio_vmspace = l->l_proc->p_vmspace;
133 auio.uio_offset = 0; /* XXX */
134 auio.uio_resid = 0;
135 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
136 #if 0
137 /* cannot happen iov_len is unsigned */
138 if (iov->iov_len < 0) {
139 error = EINVAL;
140 goto out1;
141 }
142 #endif
143 /*
144 * Reads return ssize_t because -1 is returned on error.
145 * Therefore we must restrict the length to SSIZE_MAX to
146 * avoid garbage return values.
147 */
148 auio.uio_resid += iov->iov_len;
149 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
150 error = EINVAL;
151 goto out1;
152 }
153 }
154 #ifdef KTRACE
155 if (KTRPOINT(p, KTR_GENIO)) {
156 int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
157
158 ktriov = (struct iovec *)malloc(iovlen, M_TEMP, M_WAITOK);
159 memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
160 }
161 #endif
162 len = auio.uio_resid;
163 so = (struct socket *)fp->f_data;
164 error = (*so->so_receive)(so, &from, &auio, NULL,
165 NETBSD32PTR64(mp->msg_control) ? &control : NULL,
166 &mp->msg_flags);
167 if (error) {
168 if (auio.uio_resid != len && (error == ERESTART ||
169 error == EINTR || error == EWOULDBLOCK))
170 error = 0;
171 }
172 #ifdef KTRACE
173 if (ktriov != NULL) {
174 if (error == 0)
175 ktrgenio(l, s, UIO_READ, ktriov,
176 len - auio.uio_resid, error);
177 FREE(ktriov, M_TEMP);
178 }
179 #endif
180 if (error)
181 goto out;
182 *retsize = len - auio.uio_resid;
183 if (NETBSD32PTR64(mp->msg_name)) {
184 len = mp->msg_namelen;
185 if (len <= 0 || from == 0)
186 len = 0;
187 else {
188 if (len > from->m_len)
189 len = from->m_len;
190 /* else if len < from->m_len ??? */
191 error = copyout(mtod(from, void *),
192 (void *)NETBSD32PTR64(mp->msg_name),
193 (unsigned)len);
194 if (error)
195 goto out;
196 }
197 mp->msg_namelen = len;
198 if (namelenp &&
199 (error = copyout((void *)&len, namelenp, sizeof(int))))
200 goto out;
201 }
202 if (NETBSD32PTR64(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 void *cp = (void *)NETBSD32PTR64(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, void *), cp,
217 (unsigned)i);
218 if (m->m_next)
219 i = ALIGN(i);
220 cp = (char *)cp + i;
221 len -= i;
222 if (error != 0 || len <= 0)
223 break;
224 } while ((m = m->m_next) != NULL);
225 len = (char *)cp - (char *)NETBSD32PTR64(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, l);
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(SCARG_P32(uap, msg), &msg32, sizeof(msg32));
256 if (error)
257 return (error);
258 netbsd32_to_msghdr(&msg32, &msg);
259
260 if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
261 if ((u_int)msg.msg_iovlen > IOV_MAX)
262 return (EMSGSIZE);
263 iov = (struct iovec *)malloc(
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
271 error = netbsd32_to_iovecin((struct netbsd32_iovec *)msg.msg_iov,
272 iov, msg.msg_iovlen);
273 if (error)
274 goto done;
275 msg.msg_iov = iov;
276 msg.msg_flags = 0;
277
278 /* Luckily we can use this directly */
279 /* XXX: dsl (June'07) The cmsg alignment rules differ ! */
280 error = do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
281 done:
282 if (iov != aiov)
283 FREE(iov, M_IOV);
284 return (error);
285 }
286
287 int
288 netbsd32_recvfrom(l, v, retval)
289 struct lwp *l;
290 void *v;
291 register_t *retval;
292 {
293 struct netbsd32_recvfrom_args /* {
294 syscallarg(int) s;
295 syscallarg(netbsd32_voidp) buf;
296 syscallarg(netbsd32_size_t) len;
297 syscallarg(int) flags;
298 syscallarg(netbsd32_sockaddrp_t) from;
299 syscallarg(netbsd32_intp) fromlenaddr;
300 } */ *uap = v;
301 struct netbsd32_msghdr msg;
302 struct iovec aiov;
303 int error;
304
305 if (SCARG_P32(uap, fromlenaddr)) {
306 error = copyin(SCARG_P32(uap, fromlenaddr),
307 &msg.msg_namelen, sizeof(msg.msg_namelen));
308 if (error)
309 return (error);
310 } else
311 msg.msg_namelen = 0;
312 msg.msg_name = SCARG(uap, from);
313 NETBSD32PTR32(msg.msg_iov, 0); /* ignored in recvit32(), uses iov */
314 msg.msg_iovlen = 1;
315 aiov.iov_base = SCARG_P32(uap, buf);
316 aiov.iov_len = (u_long)SCARG(uap, len);
317 NETBSD32PTR32(msg.msg_control, 0);
318 msg.msg_flags = SCARG(uap, flags);
319 return (recvit32(l, SCARG(uap, s), &msg, &aiov,
320 SCARG_P32(uap, fromlenaddr), retval));
321 }
322
323 int
324 netbsd32_sendto(l, v, retval)
325 struct lwp *l;
326 void *v;
327 register_t *retval;
328 {
329 struct netbsd32_sendto_args /* {
330 syscallarg(int) s;
331 syscallarg(const netbsd32_voidp) buf;
332 syscallarg(netbsd32_size_t) len;
333 syscallarg(int) flags;
334 syscallarg(const netbsd32_sockaddrp_t) to;
335 syscallarg(int) tolen;
336 } */ *uap = v;
337 struct msghdr msg;
338 struct iovec aiov;
339
340 msg.msg_name = SCARG_P32(uap, to); /* XXX kills const */
341 msg.msg_namelen = SCARG(uap, tolen);
342 msg.msg_iov = &aiov;
343 msg.msg_iovlen = 1;
344 msg.msg_control = 0;
345 aiov.iov_base = SCARG_P32(uap, buf); /* XXX kills const */
346 aiov.iov_len = SCARG(uap, len);
347 msg.msg_flags = 0;
348 return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
349 }
350