1 /*	$NetBSD: netbsd32_socket.c,v 1.7 2002/10/23 13:16:45 scw 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.7 2002/10/23 13:16:45 scw 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 
53 #include <compat/netbsd32/netbsd32.h>
54 #include <compat/netbsd32/netbsd32_syscallargs.h>
55 #include <compat/netbsd32/netbsd32_conv.h>
56 
57 /* note that the netbsd32_msghdr's iov really points to a struct iovec, not a netbsd32_iovec. */
58 static int recvit32 __P((struct proc *, int, struct netbsd32_msghdr *, struct iovec *, caddr_t,
59 			 register_t *));
60 
61 int
62 netbsd32_recvmsg(p, v, retval)
63 	struct proc *p;
64 	void *v;
65 	register_t *retval;
66 {
67 	struct netbsd32_recvmsg_args /* {
68 		syscallarg(int) s;
69 		syscallarg(netbsd32_msghdrp_t) msg;
70 		syscallarg(int) flags;
71 	} */ *uap = v;
72 	struct netbsd32_msghdr msg;
73 	struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
74 	int error;
75 
76 	error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, msg)), (caddr_t)&msg,
77 	    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 		MALLOC(iov, struct iovec *,
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(p, SCARG(uap, s), &msg, iov, (caddr_t)0,
98 	    retval)) == 0) {
99 		error = copyout((caddr_t)&msg,
100 		    (caddr_t)NETBSD32PTR64(SCARG(uap, msg)), sizeof(msg));
101 	}
102 done:
103 	if (iov != aiov)
104 		FREE(iov, M_IOV);
105 	return (error);
106 }
107 
108 int
109 recvit32(p, s, mp, iov, namelenp, retsize)
110 	struct proc *p;
111 	int s;
112 	struct netbsd32_msghdr *mp;
113 	struct iovec *iov;
114 	caddr_t namelenp;
115 	register_t *retsize;
116 {
117 	struct file *fp;
118 	struct uio auio;
119 	int i;
120 	int len, error;
121 	struct mbuf *from = 0, *control = 0;
122 	struct socket *so;
123 #ifdef KTRACE
124 	struct iovec *ktriov = NULL;
125 #endif
126 
127 	/* getsock() will use the descriptor for us */
128 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
129 		return (error);
130 	auio.uio_iov = iov;
131 	auio.uio_iovcnt = mp->msg_iovlen;
132 	auio.uio_segflg = UIO_USERSPACE;
133 	auio.uio_rw = UIO_READ;
134 	auio.uio_procp = p;
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 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
161 		memcpy((caddr_t)ktriov, (caddr_t)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 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
168 	if (error) {
169 		if (auio.uio_resid != len && (error == ERESTART ||
170 		    error == EINTR || error == EWOULDBLOCK))
171 			error = 0;
172 	}
173 #ifdef KTRACE
174 	if (ktriov != NULL) {
175 		if (error == 0)
176 			ktrgenio(p, s, UIO_READ, ktriov,
177 			    len - auio.uio_resid, error);
178 		FREE(ktriov, M_TEMP);
179 	}
180 #endif
181 	if (error)
182 		goto out;
183 	*retsize = len - auio.uio_resid;
184 	if (mp->msg_name) {
185 		len = mp->msg_namelen;
186 		if (len <= 0 || from == 0)
187 			len = 0;
188 		else {
189 			if (len > from->m_len)
190 				len = from->m_len;
191 			/* else if len < from->m_len ??? */
192 			error = copyout(mtod(from, caddr_t),
193 			    (caddr_t)NETBSD32PTR64(mp->msg_name),
194 			    (unsigned)len);
195 			if (error)
196 				goto out;
197 		}
198 		mp->msg_namelen = len;
199 		if (namelenp &&
200 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int))))
201 			goto out;
202 	}
203 	if (mp->msg_control) {
204 		len = mp->msg_controllen;
205 		if (len <= 0 || control == 0)
206 			len = 0;
207 		else {
208 			struct mbuf *m = control;
209 			caddr_t p = (caddr_t)NETBSD32PTR64(mp->msg_control);
210 
211 			do {
212 				i = m->m_len;
213 				if (len < i) {
214 					mp->msg_flags |= MSG_CTRUNC;
215 					i = len;
216 				}
217 				error = copyout(mtod(m, caddr_t), p,
218 				    (unsigned)i);
219 				if (m->m_next)
220 					i = ALIGN(i);
221 				p += i;
222 				len -= i;
223 				if (error != 0 || len <= 0)
224 					break;
225 			} while ((m = m->m_next) != NULL);
226 			len = p - (caddr_t)NETBSD32PTR64(mp->msg_control);
227 		}
228 		mp->msg_controllen = len;
229 	}
230  out:
231 	if (from)
232 		m_freem(from);
233 	if (control)
234 		m_freem(control);
235  out1:
236 	FILE_UNUSE(fp, p);
237 	return (error);
238 }
239 
240 int
241 netbsd32_sendmsg(p, v, retval)
242 	struct proc *p;
243 	void *v;
244 	register_t *retval;
245 {
246 	struct netbsd32_sendmsg_args /* {
247 		syscallarg(int) s;
248 		syscallarg(const netbsd32_msghdrp_t) msg;
249 		syscallarg(int) flags;
250 	} */ *uap = v;
251 	struct msghdr msg;
252 	struct netbsd32_msghdr msg32;
253 	struct iovec aiov[UIO_SMALLIOV], *iov;
254 	int error;
255 
256 	error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, msg)), (caddr_t)&msg32,
257 	    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 		MALLOC(iov, struct iovec *,
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(p, 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(p, v, retval)
286 	struct proc *p;
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(uap, fromlenaddr)) {
303 		error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, fromlenaddr)),
304 		    (caddr_t)&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 	msg.msg_iov = NULL; /* ignored in recvit32(), uses iov */
311 	msg.msg_iovlen = 1;
312 	aiov.iov_base = (caddr_t)NETBSD32PTR64(SCARG(uap, buf));
313 	aiov.iov_len = (u_long)SCARG(uap, len);
314 	msg.msg_control = 0;
315 	msg.msg_flags = SCARG(uap, flags);
316 	return (recvit32(p, SCARG(uap, s), &msg, &aiov,
317 	    (caddr_t)NETBSD32PTR64(SCARG(uap, fromlenaddr)), retval));
318 }
319 
320 int
321 netbsd32_sendto(p, v, retval)
322 	struct proc *p;
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 = (caddr_t)NETBSD32PTR64(SCARG(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 = (char *)NETBSD32PTR64(SCARG(uap, buf));	/* XXX kills const */
343 	aiov.iov_len = SCARG(uap, len);
344 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
345 }
346