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