1 /*	$NetBSD: netbsd32_socket.c,v 1.6 2001/11/13 02:09:07 lukem 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.6 2001/11/13 02:09:07 lukem 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)(u_long)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 *)(u_long)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, retval)) == 0) {
98 		error = copyout((caddr_t)&msg, (caddr_t)(u_long)SCARG(uap, msg),
99 		    sizeof(msg));
100 	}
101 done:
102 	if (iov != aiov)
103 		FREE(iov, M_IOV);
104 	return (error);
105 }
106 
107 int
108 recvit32(p, s, mp, iov, namelenp, retsize)
109 	struct proc *p;
110 	int s;
111 	struct netbsd32_msghdr *mp;
112 	struct iovec *iov;
113 	caddr_t namelenp;
114 	register_t *retsize;
115 {
116 	struct file *fp;
117 	struct uio auio;
118 	int i;
119 	int len, error;
120 	struct mbuf *from = 0, *control = 0;
121 	struct socket *so;
122 #ifdef KTRACE
123 	struct iovec *ktriov = NULL;
124 #endif
125 
126 	/* getsock() will use the descriptor for us */
127 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
128 		return (error);
129 	auio.uio_iov = iov;
130 	auio.uio_iovcnt = mp->msg_iovlen;
131 	auio.uio_segflg = UIO_USERSPACE;
132 	auio.uio_rw = UIO_READ;
133 	auio.uio_procp = p;
134 	auio.uio_offset = 0;			/* XXX */
135 	auio.uio_resid = 0;
136 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
137 #if 0
138 		/* cannot happen iov_len is unsigned */
139 		if (iov->iov_len < 0) {
140 			error = EINVAL;
141 			goto out1;
142 		}
143 #endif
144 		/*
145 		 * Reads return ssize_t because -1 is returned on error.
146 		 * Therefore we must restrict the length to SSIZE_MAX to
147 		 * avoid garbage return values.
148 		 */
149 		auio.uio_resid += iov->iov_len;
150 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
151 			error = EINVAL;
152 			goto out1;
153 		}
154 	}
155 #ifdef KTRACE
156 	if (KTRPOINT(p, KTR_GENIO)) {
157 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
158 
159 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
160 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
161 	}
162 #endif
163 	len = auio.uio_resid;
164 	so = (struct socket *)fp->f_data;
165 	error = (*so->so_receive)(so, &from, &auio, NULL,
166 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
167 	if (error) {
168 		if (auio.uio_resid != len && (error == ERESTART ||
169 		    error == EINTR || error == EWOULDBLOCK))
170 			error = 0;
171 	}
172 #ifdef KTRACE
173 	if (ktriov != NULL) {
174 		if (error == 0)
175 			ktrgenio(p, s, UIO_READ, ktriov,
176 			    len - auio.uio_resid, error);
177 		FREE(ktriov, M_TEMP);
178 	}
179 #endif
180 	if (error)
181 		goto out;
182 	*retsize = len - auio.uio_resid;
183 	if (mp->msg_name) {
184 		len = mp->msg_namelen;
185 		if (len <= 0 || from == 0)
186 			len = 0;
187 		else {
188 			if (len > from->m_len)
189 				len = from->m_len;
190 			/* else if len < from->m_len ??? */
191 			error = copyout(mtod(from, caddr_t),
192 					(caddr_t)(u_long)mp->msg_name, (unsigned)len);
193 			if (error)
194 				goto out;
195 		}
196 		mp->msg_namelen = len;
197 		if (namelenp &&
198 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int))))
199 			goto out;
200 	}
201 	if (mp->msg_control) {
202 		len = mp->msg_controllen;
203 		if (len <= 0 || control == 0)
204 			len = 0;
205 		else {
206 			struct mbuf *m = control;
207 			caddr_t p = (caddr_t)(u_long)mp->msg_control;
208 
209 			do {
210 				i = m->m_len;
211 				if (len < i) {
212 					mp->msg_flags |= MSG_CTRUNC;
213 					i = len;
214 				}
215 				error = copyout(mtod(m, caddr_t), p,
216 				    (unsigned)i);
217 				if (m->m_next)
218 					i = ALIGN(i);
219 				p += i;
220 				len -= i;
221 				if (error != 0 || len <= 0)
222 					break;
223 			} while ((m = m->m_next) != NULL);
224 			len = p - (caddr_t)(u_long)mp->msg_control;
225 		}
226 		mp->msg_controllen = len;
227 	}
228  out:
229 	if (from)
230 		m_freem(from);
231 	if (control)
232 		m_freem(control);
233  out1:
234 	FILE_UNUSE(fp, p);
235 	return (error);
236 }
237 
238 int
239 netbsd32_sendmsg(p, v, retval)
240 	struct proc *p;
241 	void *v;
242 	register_t *retval;
243 {
244 	struct netbsd32_sendmsg_args /* {
245 		syscallarg(int) s;
246 		syscallarg(const netbsd32_msghdrp_t) msg;
247 		syscallarg(int) flags;
248 	} */ *uap = v;
249 	struct msghdr msg;
250 	struct netbsd32_msghdr msg32;
251 	struct iovec aiov[UIO_SMALLIOV], *iov;
252 	int error;
253 
254 	error = copyin((caddr_t)(u_long)SCARG(uap, msg),
255 		       (caddr_t)&msg32, sizeof(msg32));
256 	if (error)
257 		return (error);
258 	netbsd32_to_msghdr(&msg32, &msg);
259 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
260 		if ((u_int)msg.msg_iovlen > IOV_MAX)
261 			return (EMSGSIZE);
262 		MALLOC(iov, struct iovec *,
263 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
264 		       M_WAITOK);
265 	} else if ((u_int)msg.msg_iovlen > 0)
266 		iov = aiov;
267 	else
268 		return (EMSGSIZE);
269 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)msg.msg_iov,
270 				   iov, msg.msg_iovlen);
271 	if (error)
272 		goto done;
273 	msg.msg_iov = iov;
274 	/* Luckily we can use this directly */
275 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
276 done:
277 	if (iov != aiov)
278 		FREE(iov, M_IOV);
279 	return (error);
280 }
281 
282 int
283 netbsd32_recvfrom(p, v, retval)
284 	struct proc *p;
285 	void *v;
286 	register_t *retval;
287 {
288 	struct netbsd32_recvfrom_args /* {
289 		syscallarg(int) s;
290 		syscallarg(netbsd32_voidp) buf;
291 		syscallarg(netbsd32_size_t) len;
292 		syscallarg(int) flags;
293 		syscallarg(netbsd32_sockaddrp_t) from;
294 		syscallarg(netbsd32_intp) fromlenaddr;
295 	} */ *uap = v;
296 	struct netbsd32_msghdr msg;
297 	struct iovec aiov;
298 	int error;
299 
300 	if (SCARG(uap, fromlenaddr)) {
301 		error = copyin((caddr_t)(u_long)SCARG(uap, fromlenaddr),
302 			       (caddr_t)&msg.msg_namelen,
303 			       sizeof(msg.msg_namelen));
304 		if (error)
305 			return (error);
306 	} else
307 		msg.msg_namelen = 0;
308 	msg.msg_name = SCARG(uap, from);
309 	msg.msg_iov = NULL; /* ignored in recvit32(), uses iov */
310 	msg.msg_iovlen = 1;
311 	aiov.iov_base = (caddr_t)(u_long)SCARG(uap, buf);
312 	aiov.iov_len = (u_long)SCARG(uap, len);
313 	msg.msg_control = 0;
314 	msg.msg_flags = SCARG(uap, flags);
315 	return (recvit32(p, SCARG(uap, s), &msg, &aiov,
316 		       (caddr_t)(u_long)SCARG(uap, fromlenaddr), retval));
317 }
318 
319 int
320 netbsd32_sendto(p, v, retval)
321 	struct proc *p;
322 	void *v;
323 	register_t *retval;
324 {
325 	struct netbsd32_sendto_args /* {
326 		syscallarg(int) s;
327 		syscallarg(const netbsd32_voidp) buf;
328 		syscallarg(netbsd32_size_t) len;
329 		syscallarg(int) flags;
330 		syscallarg(const netbsd32_sockaddrp_t) to;
331 		syscallarg(int) tolen;
332 	} */ *uap = v;
333 	struct msghdr msg;
334 	struct iovec aiov;
335 
336 	msg.msg_name = (caddr_t)(u_long)SCARG(uap, to);		/* XXX kills const */
337 	msg.msg_namelen = SCARG(uap, tolen);
338 	msg.msg_iov = &aiov;
339 	msg.msg_iovlen = 1;
340 	msg.msg_control = 0;
341 	aiov.iov_base = (char *)(u_long)SCARG(uap, buf);	/* XXX kills const */
342 	aiov.iov_len = SCARG(uap, len);
343 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
344 }
345