netbsd32_socket.c revision 1.20.2.2 1 /* $NetBSD: netbsd32_socket.c,v 1.20.2.2 2007/03/24 14:55:15 yamt 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.20.2.2 2007/03/24 14:55:15 yamt 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 if ((u_int)msg.msg_iovlen > 0)
88 iov = aiov;
89 else
90 return (EMSGSIZE);
91 msg.msg_flags = SCARG(uap, flags);
92 uiov = (struct iovec *)NETBSD32PTR64(msg.msg_iov);
93 error = netbsd32_to_iovecin((struct netbsd32_iovec *)uiov,
94 iov, msg.msg_iovlen);
95 if (error)
96 goto done;
97 if ((error = recvit32(l, SCARG(uap, s), &msg, iov, (void *)0,
98 retval)) == 0) {
99 error = copyout(&msg, SCARG_P32(uap, msg), sizeof(msg));
100 }
101 done:
102 if (iov != aiov)
103 FREE(iov, M_IOV);
104 return (error);
105 }
106
107 int
108 recvit32(l, s, mp, iov, namelenp, retsize)
109 struct lwp *l;
110 int s;
111 struct netbsd32_msghdr *mp;
112 struct iovec *iov;
113 void *namelenp;
114 register_t *retsize;
115 {
116 struct file *fp;
117 struct uio auio;
118 int i;
119 int len, error;
120 struct mbuf *from = 0, *control = 0;
121 struct socket *so;
122 struct proc *p;
123 #ifdef KTRACE
124 struct iovec *ktriov = NULL;
125 #endif
126 p = l->l_proc;
127
128 /* getsock() will use the descriptor for us */
129 if ((error = getsock(p->p_fd, s, &fp)) != 0)
130 return (error);
131 auio.uio_iov = iov;
132 auio.uio_iovcnt = mp->msg_iovlen;
133 auio.uio_rw = UIO_READ;
134 auio.uio_vmspace = l->l_proc->p_vmspace;
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 ktriov = (struct iovec *)malloc(iovlen, M_TEMP, M_WAITOK);
161 memcpy((void *)ktriov, (void *)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 NETBSD32PTR64(mp->msg_control) ? &control : NULL,
168 &mp->msg_flags);
169 if (error) {
170 if (auio.uio_resid != len && (error == ERESTART ||
171 error == EINTR || error == EWOULDBLOCK))
172 error = 0;
173 }
174 #ifdef KTRACE
175 if (ktriov != NULL) {
176 if (error == 0)
177 ktrgenio(l, s, UIO_READ, ktriov,
178 len - auio.uio_resid, error);
179 FREE(ktriov, M_TEMP);
180 }
181 #endif
182 if (error)
183 goto out;
184 *retsize = len - auio.uio_resid;
185 if (NETBSD32PTR64(mp->msg_name)) {
186 len = mp->msg_namelen;
187 if (len <= 0 || from == 0)
188 len = 0;
189 else {
190 if (len > from->m_len)
191 len = from->m_len;
192 /* else if len < from->m_len ??? */
193 error = copyout(mtod(from, void *),
194 (void *)NETBSD32PTR64(mp->msg_name),
195 (unsigned)len);
196 if (error)
197 goto out;
198 }
199 mp->msg_namelen = len;
200 if (namelenp &&
201 (error = copyout((void *)&len, namelenp, sizeof(int))))
202 goto out;
203 }
204 if (NETBSD32PTR64(mp->msg_control)) {
205 len = mp->msg_controllen;
206 if (len <= 0 || control == 0)
207 len = 0;
208 else {
209 struct mbuf *m = control;
210 void *cp = (void *)NETBSD32PTR64(mp->msg_control);
211
212 do {
213 i = m->m_len;
214 if (len < i) {
215 mp->msg_flags |= MSG_CTRUNC;
216 i = len;
217 }
218 error = copyout(mtod(m, void *), cp,
219 (unsigned)i);
220 if (m->m_next)
221 i = ALIGN(i);
222 cp = (char *)cp + i;
223 len -= i;
224 if (error != 0 || len <= 0)
225 break;
226 } while ((m = m->m_next) != NULL);
227 len = (char *)cp - (char *)NETBSD32PTR64(mp->msg_control);
228 }
229 mp->msg_controllen = len;
230 }
231 out:
232 if (from)
233 m_freem(from);
234 if (control)
235 m_freem(control);
236 out1:
237 FILE_UNUSE(fp, l);
238 return (error);
239 }
240
241 int
242 netbsd32_sendmsg(l, v, retval)
243 struct lwp *l;
244 void *v;
245 register_t *retval;
246 {
247 struct netbsd32_sendmsg_args /* {
248 syscallarg(int) s;
249 syscallarg(const netbsd32_msghdrp_t) msg;
250 syscallarg(int) flags;
251 } */ *uap = v;
252 struct msghdr msg;
253 struct netbsd32_msghdr msg32;
254 struct iovec aiov[UIO_SMALLIOV], *iov;
255 int error;
256
257 error = copyin(SCARG_P32(uap, msg), &msg32, sizeof(msg32));
258 if (error)
259 return (error);
260 netbsd32_to_msghdr(&msg32, &msg);
261 if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
262 if ((u_int)msg.msg_iovlen > IOV_MAX)
263 return (EMSGSIZE);
264 iov = (struct iovec *)malloc(
265 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
266 M_WAITOK);
267 } else if ((u_int)msg.msg_iovlen > 0)
268 iov = aiov;
269 else
270 return (EMSGSIZE);
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 /* Luckily we can use this directly */
277 error = sendit(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
278 done:
279 if (iov != aiov)
280 FREE(iov, M_IOV);
281 return (error);
282 }
283
284 int
285 netbsd32_recvfrom(l, v, retval)
286 struct lwp *l;
287 void *v;
288 register_t *retval;
289 {
290 struct netbsd32_recvfrom_args /* {
291 syscallarg(int) s;
292 syscallarg(netbsd32_voidp) buf;
293 syscallarg(netbsd32_size_t) len;
294 syscallarg(int) flags;
295 syscallarg(netbsd32_sockaddrp_t) from;
296 syscallarg(netbsd32_intp) fromlenaddr;
297 } */ *uap = v;
298 struct netbsd32_msghdr msg;
299 struct iovec aiov;
300 int error;
301
302 if (SCARG_P32(uap, fromlenaddr)) {
303 error = copyin(SCARG_P32(uap, fromlenaddr),
304 &msg.msg_namelen, 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 NETBSD32PTR32(msg.msg_iov, 0); /* ignored in recvit32(), uses iov */
311 msg.msg_iovlen = 1;
312 aiov.iov_base = SCARG_P32(uap, buf);
313 aiov.iov_len = (u_long)SCARG(uap, len);
314 NETBSD32PTR32(msg.msg_control, 0);
315 msg.msg_flags = SCARG(uap, flags);
316 return (recvit32(l, SCARG(uap, s), &msg, &aiov,
317 SCARG_P32(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 = SCARG_P32(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 = SCARG_P32(uap, buf); /* XXX kills const */
343 aiov.iov_len = SCARG(uap, len);
344 return (sendit(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
345 }
346