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