netbsd32_socket.c revision 1.11.2.2 1 /* $NetBSD: netbsd32_socket.c,v 1.11.2.2 2004/08/03 10:44:21 skrll 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.11.2.2 2004/08/03 10:44:21 skrll 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)NETBSD32PTR64(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 *)NETBSD32PTR64(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,
99 retval)) == 0) {
100 error = copyout((caddr_t)&msg,
101 (caddr_t)NETBSD32PTR64(SCARG(uap, msg)), sizeof(msg));
102 }
103 done:
104 if (iov != aiov)
105 FREE(iov, M_IOV);
106 return (error);
107 }
108
109 int
110 recvit32(p, s, mp, iov, namelenp, retsize)
111 struct proc *p;
112 int s;
113 struct netbsd32_msghdr *mp;
114 struct iovec *iov;
115 caddr_t namelenp;
116 register_t *retsize;
117 {
118 struct file *fp;
119 struct uio auio;
120 int i;
121 int len, error;
122 struct mbuf *from = 0, *control = 0;
123 struct socket *so;
124 #ifdef KTRACE
125 struct iovec *ktriov = NULL;
126 #endif
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_segflg = UIO_USERSPACE;
134 auio.uio_rw = UIO_READ;
135 auio.uio_lwp = l;
136 auio.uio_offset = 0; /* XXX */
137 auio.uio_resid = 0;
138 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
139 #if 0
140 /* cannot happen iov_len is unsigned */
141 if (iov->iov_len < 0) {
142 error = EINVAL;
143 goto out1;
144 }
145 #endif
146 /*
147 * Reads return ssize_t because -1 is returned on error.
148 * Therefore we must restrict the length to SSIZE_MAX to
149 * avoid garbage return values.
150 */
151 auio.uio_resid += iov->iov_len;
152 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
153 error = EINVAL;
154 goto out1;
155 }
156 }
157 #ifdef KTRACE
158 if (KTRPOINT(p, KTR_GENIO)) {
159 int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
160
161 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
162 memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
163 }
164 #endif
165 len = auio.uio_resid;
166 so = (struct socket *)fp->f_data;
167 error = (*so->so_receive)(so, &from, &auio, NULL,
168 mp->msg_control ? &control : NULL, &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(p, 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 (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, caddr_t),
194 (caddr_t)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((caddr_t)&len, namelenp, sizeof(int))))
202 goto out;
203 }
204 if (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 caddr_t p = (caddr_t)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, caddr_t), p,
219 (unsigned)i);
220 if (m->m_next)
221 i = ALIGN(i);
222 p += i;
223 len -= i;
224 if (error != 0 || len <= 0)
225 break;
226 } while ((m = m->m_next) != NULL);
227 len = p - (caddr_t)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, p);
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((caddr_t)NETBSD32PTR64(SCARG(uap, msg)), (caddr_t)&msg32,
258 sizeof(msg32));
259 if (error)
260 return (error);
261 netbsd32_to_msghdr(&msg32, &msg);
262 if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
263 if ((u_int)msg.msg_iovlen > IOV_MAX)
264 return (EMSGSIZE);
265 MALLOC(iov, struct iovec *,
266 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
267 M_WAITOK);
268 } else if ((u_int)msg.msg_iovlen > 0)
269 iov = aiov;
270 else
271 return (EMSGSIZE);
272 error = netbsd32_to_iovecin((struct netbsd32_iovec *)msg.msg_iov,
273 iov, msg.msg_iovlen);
274 if (error)
275 goto done;
276 msg.msg_iov = iov;
277 /* Luckily we can use this directly */
278 error = sendit(l->l_proc, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
279 done:
280 if (iov != aiov)
281 FREE(iov, M_IOV);
282 return (error);
283 }
284
285 int
286 netbsd32_recvfrom(l, v, retval)
287 struct lwp *l;
288 void *v;
289 register_t *retval;
290 {
291 struct netbsd32_recvfrom_args /* {
292 syscallarg(int) s;
293 syscallarg(netbsd32_voidp) buf;
294 syscallarg(netbsd32_size_t) len;
295 syscallarg(int) flags;
296 syscallarg(netbsd32_sockaddrp_t) from;
297 syscallarg(netbsd32_intp) fromlenaddr;
298 } */ *uap = v;
299 struct netbsd32_msghdr msg;
300 struct iovec aiov;
301 int error;
302
303 if (SCARG(uap, fromlenaddr)) {
304 error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, fromlenaddr)),
305 (caddr_t)&msg.msg_namelen, sizeof(msg.msg_namelen));
306 if (error)
307 return (error);
308 } else
309 msg.msg_namelen = 0;
310 msg.msg_name = SCARG(uap, from);
311 msg.msg_iov = 0; /* ignored in recvit32(), uses iov */
312 msg.msg_iovlen = 1;
313 aiov.iov_base = (caddr_t)NETBSD32PTR64(SCARG(uap, buf));
314 aiov.iov_len = (u_long)SCARG(uap, len);
315 msg.msg_control = 0;
316 msg.msg_flags = SCARG(uap, flags);
317 return (recvit32(l->l_proc, SCARG(uap, s), &msg, &aiov,
318 (caddr_t)NETBSD32PTR64(SCARG(uap, fromlenaddr)), retval));
319 }
320
321 int
322 netbsd32_sendto(l, v, retval)
323 struct lwp *l;
324 void *v;
325 register_t *retval;
326 {
327 struct netbsd32_sendto_args /* {
328 syscallarg(int) s;
329 syscallarg(const netbsd32_voidp) buf;
330 syscallarg(netbsd32_size_t) len;
331 syscallarg(int) flags;
332 syscallarg(const netbsd32_sockaddrp_t) to;
333 syscallarg(int) tolen;
334 } */ *uap = v;
335 struct msghdr msg;
336 struct iovec aiov;
337
338 msg.msg_name = (caddr_t)NETBSD32PTR64(SCARG(uap, to)); /* XXX kills const */
339 msg.msg_namelen = SCARG(uap, tolen);
340 msg.msg_iov = &aiov;
341 msg.msg_iovlen = 1;
342 msg.msg_control = 0;
343 aiov.iov_base = (char *)NETBSD32PTR64(SCARG(uap, buf)); /* XXX kills const */
344 aiov.iov_len = SCARG(uap, len);
345 return (sendit(l->l_proc, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
346 }
347