No empty .Rs/.Re
[netbsd-mini2440.git] / sys / compat / netbsd32 / netbsd32_socket.c
blobc8c4f4ae05a8bdf400987e1cde67ea5075e80d83
1 /* $NetBSD: netbsd32_socket.c,v 1.35 2009/06/15 22:59:53 njoly Exp $ */
3 /*
4 * Copyright (c) 1998, 2001 Matthew R. Green
5 * All rights reserved.
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.
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.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_socket.c,v 1.35 2009/06/15 22:59:53 njoly Exp $");
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>
48 #include <compat/netbsd32/netbsd32.h>
49 #include <compat/netbsd32/netbsd32_syscallargs.h>
50 #include <compat/netbsd32/netbsd32_conv.h>
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 *);
56 int
57 netbsd32_recvmsg(struct lwp *l, const struct netbsd32_recvmsg_args *uap, register_t *retval)
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], *iov;
66 struct netbsd32_iovec *iov32;
67 int error;
69 error = copyin(SCARG_P32(uap, msg), &msg, sizeof(msg));
70 /* netbsd32_msghdr needs the iov pre-allocated */
71 if (error)
72 return (error);
73 if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
74 if ((u_int)msg.msg_iovlen > IOV_MAX)
75 return (EMSGSIZE);
76 iov = (struct iovec *)malloc(
77 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
78 M_WAITOK);
79 } else
80 iov = aiov;
81 msg.msg_flags = SCARG(uap, flags);
82 iov32 = NETBSD32PTR64(msg.msg_iov);
83 error = netbsd32_to_iovecin(iov32, iov, msg.msg_iovlen);
84 if (error)
85 goto done;
86 if ((error = recvit32(l, SCARG(uap, s), &msg, iov, (void *)0,
87 retval)) == 0) {
88 error = copyout(&msg, SCARG_P32(uap, msg), sizeof(msg));
90 done:
91 if (iov != aiov)
92 free(iov, M_IOV);
93 return (error);
96 int
97 recvit32(struct lwp *l, int s, struct netbsd32_msghdr *mp, struct iovec *iov, void *namelenp, register_t *retsize)
99 struct uio auio;
100 int i, len, error, iovlen;
101 struct mbuf *from = 0, *control = 0;
102 struct socket *so;
103 struct proc *p;
104 struct iovec *ktriov = NULL;
105 p = l->l_proc;
107 /* fd_getsock() will use the descriptor for us */
108 if ((error = fd_getsock(s, &so)) != 0)
109 return (error);
110 auio.uio_iov = iov;
111 auio.uio_iovcnt = mp->msg_iovlen;
112 auio.uio_rw = UIO_READ;
113 auio.uio_vmspace = l->l_proc->p_vmspace;
114 auio.uio_offset = 0; /* XXX */
115 auio.uio_resid = 0;
116 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
117 #if 0
118 /* cannot happen iov_len is unsigned */
119 if (iov->iov_len < 0) {
120 error = EINVAL;
121 goto out1;
123 #endif
125 * Reads return ssize_t because -1 is returned on error.
126 * Therefore we must restrict the length to SSIZE_MAX to
127 * avoid garbage return values.
129 auio.uio_resid += iov->iov_len;
130 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
131 error = EINVAL;
132 goto out1;
136 if (ktrpoint(KTR_GENIO)) {
137 iovlen = auio.uio_iovcnt * sizeof(struct iovec);
138 ktriov = (struct iovec *)malloc(iovlen, M_TEMP, M_WAITOK);
139 memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
142 len = auio.uio_resid;
143 error = (*so->so_receive)(so, &from, &auio, NULL,
144 NETBSD32PTR64(mp->msg_control) ? &control : NULL,
145 &mp->msg_flags);
146 if (error) {
147 if (auio.uio_resid != len && (error == ERESTART ||
148 error == EINTR || error == EWOULDBLOCK))
149 error = 0;
152 if (ktriov != NULL) {
153 ktrgeniov(s, UIO_READ, ktriov, len - auio.uio_resid, error);
154 free(ktriov, M_TEMP);
157 if (error)
158 goto out;
159 *retsize = len - auio.uio_resid;
160 if (NETBSD32PTR64(mp->msg_name)) {
161 len = mp->msg_namelen;
162 if (len <= 0 || from == 0)
163 len = 0;
164 else {
165 if (len > from->m_len)
166 len = from->m_len;
167 /* else if len < from->m_len ??? */
168 error = copyout(mtod(from, void *),
169 (void *)NETBSD32PTR64(mp->msg_name),
170 (unsigned)len);
171 if (error)
172 goto out;
174 mp->msg_namelen = len;
175 if (namelenp &&
176 (error = copyout((void *)&len, namelenp, sizeof(int))))
177 goto out;
179 if (NETBSD32PTR64(mp->msg_control)) {
180 len = mp->msg_controllen;
181 if (len <= 0 || control == 0)
182 len = 0;
183 else {
184 struct mbuf *m = control;
185 void *cp = (void *)NETBSD32PTR64(mp->msg_control);
187 do {
188 i = m->m_len;
189 if (len < i) {
190 mp->msg_flags |= MSG_CTRUNC;
191 i = len;
193 error = copyout(mtod(m, void *), cp,
194 (unsigned)i);
195 if (m->m_next)
196 i = ALIGN(i);
197 cp = (char *)cp + i;
198 len -= i;
199 if (error != 0 || len <= 0)
200 break;
201 } while ((m = m->m_next) != NULL);
202 len = (char *)cp - (char *)NETBSD32PTR64(mp->msg_control);
204 mp->msg_controllen = len;
206 out:
207 if (from)
208 m_freem(from);
209 if (control)
210 m_freem(control);
211 out1:
212 fd_putfile(s);
213 return (error);
217 netbsd32_sendmsg(struct lwp *l, const struct netbsd32_sendmsg_args *uap, register_t *retval)
219 /* {
220 syscallarg(int) s;
221 syscallarg(const netbsd32_msghdrp_t) msg;
222 syscallarg(int) flags;
223 } */
224 struct msghdr msg;
225 struct netbsd32_msghdr msg32;
226 struct iovec aiov[UIO_SMALLIOV], *iov;
227 struct netbsd32_iovec *iov32;
228 int error;
230 error = copyin(SCARG_P32(uap, msg), &msg32, sizeof(msg32));
231 if (error)
232 return (error);
233 netbsd32_to_msghdr(&msg32, &msg);
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;
244 iov32 = NETBSD32PTR64(msg32.msg_iov);
245 error = netbsd32_to_iovecin(iov32, iov, msg.msg_iovlen);
246 if (error)
247 goto done;
248 msg.msg_iov = iov;
249 msg.msg_flags = 0;
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);
261 netbsd32_recvfrom(struct lwp *l, const struct netbsd32_recvfrom_args *uap, register_t *retval)
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;
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));
294 netbsd32_sendto(struct lwp *l, const struct netbsd32_sendto_args *uap, register_t *retval)
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;
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);