xref: /netbsd-src/sys/kern/uipc_syscalls.c (revision 2980e352a13e8f0b545a366830c411e7a542ada8)
1 /*	$NetBSD: uipc_syscalls.c,v 1.133 2008/06/24 11:21:46 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Copyright (c) 1982, 1986, 1989, 1990, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. Neither the name of the University nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  *	@(#)uipc_syscalls.c	8.6 (Berkeley) 2/14/95
58  */
59 
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.133 2008/06/24 11:21:46 ad Exp $");
62 
63 #include "opt_pipe.h"
64 
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/filedesc.h>
68 #include <sys/proc.h>
69 #include <sys/file.h>
70 #include <sys/buf.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/signalvar.h>
77 #include <sys/un.h>
78 #include <sys/ktrace.h>
79 #include <sys/event.h>
80 
81 #include <sys/mount.h>
82 #include <sys/syscallargs.h>
83 
84 #include <uvm/uvm_extern.h>
85 
86 /*
87  * System call interface to the socket abstraction.
88  */
89 extern const struct fileops socketops;
90 
91 int
92 sys___socket30(struct lwp *l, const struct sys___socket30_args *uap, register_t *retval)
93 {
94 	/* {
95 		syscallarg(int)	domain;
96 		syscallarg(int)	type;
97 		syscallarg(int)	protocol;
98 	} */
99 	int		fd, error;
100 
101 	error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type),
102 			 SCARG(uap, protocol), l, &fd);
103 	if (error == 0)
104 		*retval = fd;
105 	return error;
106 }
107 
108 /* ARGSUSED */
109 int
110 sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval)
111 {
112 	/* {
113 		syscallarg(int)				s;
114 		syscallarg(const struct sockaddr *)	name;
115 		syscallarg(unsigned int)		namelen;
116 	} */
117 	struct mbuf	*nam;
118 	int		error;
119 
120 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
121 	    MT_SONAME);
122 	if (error)
123 		return error;
124 
125 	return do_sys_bind(l, SCARG(uap, s), nam);
126 }
127 
128 int
129 do_sys_bind(struct lwp *l, int fd, struct mbuf *nam)
130 {
131 	struct socket	*so;
132 	int		error;
133 
134 	if ((error = fd_getsock(fd, &so)) != 0) {
135 		m_freem(nam);
136 		return (error);
137 	}
138 	MCLAIM(nam, so->so_mowner);
139 	error = sobind(so, nam, l);
140 	m_freem(nam);
141 	fd_putfile(fd);
142 	return error;
143 }
144 
145 /* ARGSUSED */
146 int
147 sys_listen(struct lwp *l, const struct sys_listen_args *uap, register_t *retval)
148 {
149 	/* {
150 		syscallarg(int)	s;
151 		syscallarg(int)	backlog;
152 	} */
153 	struct socket	*so;
154 	int		error;
155 
156 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
157 		return (error);
158 	error = solisten(so, SCARG(uap, backlog), l);
159 	fd_putfile(SCARG(uap, s));
160 	return error;
161 }
162 
163 int
164 do_sys_accept(struct lwp *l, int sock, struct mbuf **name, register_t *new_sock)
165 {
166 	file_t		*fp, *fp2;
167 	struct mbuf	*nam;
168 	int		error, fd;
169 	struct socket	*so, *so2;
170 
171 	if ((fp = fd_getfile(sock)) == NULL)
172 		return (EBADF);
173 	if (fp->f_type != DTYPE_SOCKET) {
174 		fd_putfile(sock);
175 		return (ENOTSOCK);
176 	}
177 	if ((error = fd_allocfile(&fp2, &fd)) != 0) {
178 		fd_putfile(sock);
179 		return (error);
180 	}
181 	nam = m_get(M_WAIT, MT_SONAME);
182 	*new_sock = fd;
183 	so = fp->f_data;
184 	solock(so);
185 	if (!(so->so_proto->pr_flags & PR_LISTEN)) {
186 		error = EOPNOTSUPP;
187 		goto bad;
188 	}
189 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
190 		error = EINVAL;
191 		goto bad;
192 	}
193 	if (so->so_nbio && so->so_qlen == 0) {
194 		error = EWOULDBLOCK;
195 		goto bad;
196 	}
197 	while (so->so_qlen == 0 && so->so_error == 0) {
198 		if (so->so_state & SS_CANTRCVMORE) {
199 			so->so_error = ECONNABORTED;
200 			break;
201 		}
202 		error = sowait(so, 0);
203 		if (error) {
204 			goto bad;
205 		}
206 	}
207 	if (so->so_error) {
208 		error = so->so_error;
209 		so->so_error = 0;
210 		goto bad;
211 	}
212 	/* connection has been removed from the listen queue */
213 	KNOTE(&so->so_rcv.sb_sel.sel_klist, NOTE_SUBMIT);
214 	so2 = TAILQ_FIRST(&so->so_q);
215 	if (soqremque(so2, 1) == 0)
216 		panic("accept");
217 	fp2->f_type = DTYPE_SOCKET;
218 	fp2->f_flag = fp->f_flag;
219 	fp2->f_ops = &socketops;
220 	fp2->f_data = so2;
221 	error = soaccept(so2, nam);
222 	sounlock(so);
223 	if (error) {
224 		/* an error occurred, free the file descriptor and mbuf */
225 		m_freem(nam);
226 		mutex_enter(&fp2->f_lock);
227 		fp2->f_count++;
228 		mutex_exit(&fp2->f_lock);
229 		closef(fp2);
230 		fd_abort(curproc, NULL, fd);
231 	} else {
232 		fd_affix(curproc, fp2, fd);
233 		*name = nam;
234 	}
235 	fd_putfile(sock);
236 	return (error);
237  bad:
238  	sounlock(so);
239  	m_freem(nam);
240 	fd_putfile(sock);
241  	fd_abort(curproc, fp2, fd);
242  	return (error);
243 }
244 
245 int
246 sys_accept(struct lwp *l, const struct sys_accept_args *uap, register_t *retval)
247 {
248 	/* {
249 		syscallarg(int)			s;
250 		syscallarg(struct sockaddr *)	name;
251 		syscallarg(unsigned int *)	anamelen;
252 	} */
253 	int error, fd;
254 	struct mbuf *name;
255 
256 	error = do_sys_accept(l, SCARG(uap, s), &name, retval);
257 	if (error != 0)
258 		return error;
259 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
260 	    MSG_LENUSRSPACE, name);
261 	if (name != NULL)
262 		m_free(name);
263 	if (error != 0) {
264 		fd = (int)*retval;
265 		if (fd_getfile(fd) != NULL)
266 			(void)fd_close(fd);
267 	}
268 	return error;
269 }
270 
271 /* ARGSUSED */
272 int
273 sys_connect(struct lwp *l, const struct sys_connect_args *uap, register_t *retval)
274 {
275 	/* {
276 		syscallarg(int)				s;
277 		syscallarg(const struct sockaddr *)	name;
278 		syscallarg(unsigned int)		namelen;
279 	} */
280 	int		error;
281 	struct mbuf	*nam;
282 
283 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
284 	    MT_SONAME);
285 	if (error)
286 		return error;
287 	return do_sys_connect(l,  SCARG(uap, s), nam);
288 }
289 
290 int
291 do_sys_connect(struct lwp *l, int fd, struct mbuf *nam)
292 {
293 	struct socket	*so;
294 	int		error;
295 	int		interrupted = 0;
296 
297 	if ((error = fd_getsock(fd, &so)) != 0) {
298 		m_freem(nam);
299 		return (error);
300 	}
301 	solock(so);
302 	MCLAIM(nam, so->so_mowner);
303 	if (so->so_state & SS_ISCONNECTING) {
304 		error = EALREADY;
305 		goto out;
306 	}
307 
308 	error = soconnect(so, nam, l);
309 	if (error)
310 		goto bad;
311 	if (so->so_nbio && (so->so_state & SS_ISCONNECTING)) {
312 		error = EINPROGRESS;
313 		goto out;
314 	}
315 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
316 		error = sowait(so, 0);
317 		if (error) {
318 			if (error == EINTR || error == ERESTART)
319 				interrupted = 1;
320 			break;
321 		}
322 	}
323 	if (error == 0) {
324 		error = so->so_error;
325 		so->so_error = 0;
326 	}
327  bad:
328 	if (!interrupted)
329 		so->so_state &= ~SS_ISCONNECTING;
330 	if (error == ERESTART)
331 		error = EINTR;
332  out:
333  	sounlock(so);
334  	fd_putfile(fd);
335 	m_freem(nam);
336 	return (error);
337 }
338 
339 int
340 sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap, register_t *retval)
341 {
342 	/* {
343 		syscallarg(int)		domain;
344 		syscallarg(int)		type;
345 		syscallarg(int)		protocol;
346 		syscallarg(int *)	rsv;
347 	} */
348 	file_t		*fp1, *fp2;
349 	struct socket	*so1, *so2;
350 	int		fd, error, sv[2];
351 	proc_t		*p;
352 
353 	p = curproc;
354 	error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
355 	    SCARG(uap, protocol), l, NULL);
356 	if (error)
357 		return (error);
358 	error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
359 	    SCARG(uap, protocol), l, so1);
360 	if (error)
361 		goto free1;
362 	if ((error = fd_allocfile(&fp1, &fd)) != 0)
363 		goto free2;
364 	sv[0] = fd;
365 	fp1->f_flag = FREAD|FWRITE;
366 	fp1->f_type = DTYPE_SOCKET;
367 	fp1->f_ops = &socketops;
368 	fp1->f_data = so1;
369 	if ((error = fd_allocfile(&fp2, &fd)) != 0)
370 		goto free3;
371 	fp2->f_flag = FREAD|FWRITE;
372 	fp2->f_type = DTYPE_SOCKET;
373 	fp2->f_ops = &socketops;
374 	fp2->f_data = so2;
375 	sv[1] = fd;
376 	solock(so1);
377 	error = soconnect2(so1, so2);
378 	if (error == 0 && SCARG(uap, type) == SOCK_DGRAM) {
379 		/*
380 		 * Datagram socket connection is asymmetric.
381 		 */
382 		error = soconnect2(so2, so1);
383 	}
384 	sounlock(so1);
385 	if (error == 0)
386 		error = copyout(sv, SCARG(uap, rsv), 2 * sizeof(int));
387 	if (error == 0) {
388 		fd_affix(p, fp2, sv[1]);
389 		fd_affix(p, fp1, sv[0]);
390 		return (0);
391 	}
392 	fd_abort(p, fp2, sv[1]);
393  free3:
394 	fd_abort(p, fp1, sv[0]);
395  free2:
396 	(void)soclose(so2);
397  free1:
398 	(void)soclose(so1);
399 	return (error);
400 }
401 
402 int
403 sys_sendto(struct lwp *l, const struct sys_sendto_args *uap, register_t *retval)
404 {
405 	/* {
406 		syscallarg(int)				s;
407 		syscallarg(const void *)		buf;
408 		syscallarg(size_t)			len;
409 		syscallarg(int)				flags;
410 		syscallarg(const struct sockaddr *)	to;
411 		syscallarg(unsigned int)		tolen;
412 	} */
413 	struct msghdr	msg;
414 	struct iovec	aiov;
415 
416 	msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */
417 	msg.msg_namelen = SCARG(uap, tolen);
418 	msg.msg_iov = &aiov;
419 	msg.msg_iovlen = 1;
420 	msg.msg_control = NULL;
421 	msg.msg_flags = 0;
422 	aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
423 	aiov.iov_len = SCARG(uap, len);
424 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
425 }
426 
427 int
428 sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap, register_t *retval)
429 {
430 	/* {
431 		syscallarg(int)				s;
432 		syscallarg(const struct msghdr *)	msg;
433 		syscallarg(int)				flags;
434 	} */
435 	struct msghdr	msg;
436 	int		error;
437 
438 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
439 	if (error)
440 		return (error);
441 
442 	msg.msg_flags = MSG_IOVUSRSPACE;
443 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
444 }
445 
446 int
447 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
448 		register_t *retsize)
449 {
450 	struct uio	auio;
451 	int		i, len, error, iovlen;
452 	struct mbuf	*to, *control;
453 	struct socket	*so;
454 	struct iovec	*tiov;
455 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov;
456 	struct iovec	*ktriov = NULL;
457 
458 	ktrkuser("msghdr", mp, sizeof *mp);
459 
460 	/* If the caller passed us stuff in mbufs, we must free them */
461 	if (mp->msg_flags & MSG_NAMEMBUF)
462 		to = mp->msg_name;
463 	else
464 		to = NULL;
465 
466 	if (mp->msg_flags & MSG_CONTROLMBUF)
467 		control = mp->msg_control;
468 	else
469 		control = NULL;
470 
471 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
472 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
473 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
474 				error = EMSGSIZE;
475 				goto bad;
476 			}
477 			iov = malloc(sizeof(struct iovec) * mp->msg_iovlen,
478 			    M_IOV, M_WAITOK);
479 		}
480 		if (mp->msg_iovlen != 0) {
481 			error = copyin(mp->msg_iov, iov,
482 			    (size_t)(mp->msg_iovlen * sizeof(struct iovec)));
483 			if (error)
484 				goto bad;
485 		}
486 		mp->msg_iov = iov;
487 	}
488 
489 	auio.uio_iov = mp->msg_iov;
490 	auio.uio_iovcnt = mp->msg_iovlen;
491 	auio.uio_rw = UIO_WRITE;
492 	auio.uio_offset = 0;			/* XXX */
493 	auio.uio_resid = 0;
494 	KASSERT(l == curlwp);
495 	auio.uio_vmspace = l->l_proc->p_vmspace;
496 
497 	for (i = 0, tiov = mp->msg_iov; i < mp->msg_iovlen; i++, tiov++) {
498 #if 0
499 		/* cannot happen; iov_len is unsigned */
500 		if (tiov->iov_len < 0) {
501 			error = EINVAL;
502 			goto bad;
503 		}
504 #endif
505 		/*
506 		 * Writes return ssize_t because -1 is returned on error.
507 		 * Therefore, we must restrict the length to SSIZE_MAX to
508 		 * avoid garbage return values.
509 		 */
510 		auio.uio_resid += tiov->iov_len;
511 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
512 			error = EINVAL;
513 			goto bad;
514 		}
515 	}
516 
517 	if (mp->msg_name && to == NULL) {
518 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
519 		    MT_SONAME);
520 		if (error)
521 			goto bad;
522 	}
523 
524 	if (mp->msg_control) {
525 		if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
526 			error = EINVAL;
527 			goto bad;
528 		}
529 		if (control == NULL) {
530 			error = sockargs(&control, mp->msg_control,
531 			    mp->msg_controllen, MT_CONTROL);
532 			if (error)
533 				goto bad;
534 		}
535 	}
536 
537 	if (ktrpoint(KTR_GENIO)) {
538 		iovlen = auio.uio_iovcnt * sizeof(struct iovec);
539 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
540 		memcpy(ktriov, auio.uio_iov, iovlen);
541 	}
542 
543 	if ((error = fd_getsock(s, &so)) != 0)
544 		goto bad;
545 
546 	if (mp->msg_name)
547 		MCLAIM(to, so->so_mowner);
548 	if (mp->msg_control)
549 		MCLAIM(control, so->so_mowner);
550 
551 	len = auio.uio_resid;
552 	error = (*so->so_send)(so, to, &auio, NULL, control, flags, l);
553 	/* Protocol is responsible for freeing 'control' */
554 	control = NULL;
555 
556 	fd_putfile(s);
557 
558 	if (error) {
559 		if (auio.uio_resid != len && (error == ERESTART ||
560 		    error == EINTR || error == EWOULDBLOCK))
561 			error = 0;
562 		if (error == EPIPE && (flags & MSG_NOSIGNAL) == 0) {
563 			mutex_enter(proc_lock);
564 			psignal(l->l_proc, SIGPIPE);
565 			mutex_exit(proc_lock);
566 		}
567 	}
568 	if (error == 0)
569 		*retsize = len - auio.uio_resid;
570 
571 bad:
572 	if (ktriov != NULL) {
573 		ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
574 		free(ktriov, M_TEMP);
575 	}
576 
577  	if (iov != aiov)
578 		free(iov, M_IOV);
579 	if (to)
580 		m_freem(to);
581 	if (control)
582 		m_freem(control);
583 
584 	return (error);
585 }
586 
587 int
588 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap, register_t *retval)
589 {
590 	/* {
591 		syscallarg(int)			s;
592 		syscallarg(void *)		buf;
593 		syscallarg(size_t)		len;
594 		syscallarg(int)			flags;
595 		syscallarg(struct sockaddr *)	from;
596 		syscallarg(unsigned int *)	fromlenaddr;
597 	} */
598 	struct msghdr	msg;
599 	struct iovec	aiov;
600 	int		error;
601 	struct mbuf	*from;
602 
603 	msg.msg_name = NULL;
604 	msg.msg_iov = &aiov;
605 	msg.msg_iovlen = 1;
606 	aiov.iov_base = SCARG(uap, buf);
607 	aiov.iov_len = SCARG(uap, len);
608 	msg.msg_control = NULL;
609 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
610 
611 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
612 	if (error != 0)
613 		return error;
614 
615 	error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
616 	    MSG_LENUSRSPACE, from);
617 	if (from != NULL)
618 		m_free(from);
619 	return error;
620 }
621 
622 int
623 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap, register_t *retval)
624 {
625 	/* {
626 		syscallarg(int)			s;
627 		syscallarg(struct msghdr *)	msg;
628 		syscallarg(int)			flags;
629 	} */
630 	struct msghdr	msg;
631 	int		error;
632 	struct mbuf	*from, *control;
633 
634 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
635 	if (error)
636 		return (error);
637 
638 	msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
639 
640 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
641 	    msg.msg_control != NULL ? &control : NULL, retval);
642 	if (error != 0)
643 		return error;
644 
645 	if (msg.msg_control != NULL)
646 		error = copyout_msg_control(l, &msg, control);
647 
648 	if (error == 0)
649 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
650 			from);
651 	if (from != NULL)
652 		m_free(from);
653 	if (error == 0) {
654 		ktrkuser("msghdr", &msg, sizeof msg);
655 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
656 	}
657 
658 	return (error);
659 }
660 
661 /*
662  * Adjust for a truncated SCM_RIGHTS control message.
663  *  This means closing any file descriptors that aren't present
664  *  in the returned buffer.
665  *  m is the mbuf holding the (already externalized) SCM_RIGHTS message.
666  */
667 static void
668 free_rights(struct mbuf *m)
669 {
670 	int nfd;
671 	int i;
672 	int *fdv;
673 
674 	nfd = m->m_len < CMSG_SPACE(sizeof(int)) ? 0
675 	    : (m->m_len - CMSG_SPACE(sizeof(int))) / sizeof(int) + 1;
676 	fdv = (int *) CMSG_DATA(mtod(m,struct cmsghdr *));
677 	for (i = 0; i < nfd; i++) {
678 		if (fd_getfile(fdv[i]) != NULL)
679 			(void)fd_close(fdv[i]);
680 	}
681 }
682 
683 void
684 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
685 {
686 	struct mbuf *next;
687 	struct cmsghdr *cmsg;
688 	bool do_free_rights = false;
689 
690 	while (control != NULL) {
691 		cmsg = mtod(control, struct cmsghdr *);
692 		if (control == uncopied)
693 			do_free_rights = true;
694 		if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
695 		    && cmsg->cmsg_type == SCM_RIGHTS)
696 			free_rights(control);
697 		next = control->m_next;
698 		m_free(control);
699 		control = next;
700 	}
701 }
702 
703 /* Copy socket control/CMSG data to user buffer, frees the mbuf */
704 int
705 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
706 {
707 	int i, len, error = 0;
708 	struct cmsghdr *cmsg;
709 	struct mbuf *m;
710 	char *q;
711 
712 	len = mp->msg_controllen;
713 	if (len <= 0 || control == 0) {
714 		mp->msg_controllen = 0;
715 		free_control_mbuf(l, control, control);
716 		return 0;
717 	}
718 
719 	q = (char *)mp->msg_control;
720 
721 	for (m = control; m != NULL; ) {
722 		cmsg = mtod(m, struct cmsghdr *);
723 		i = m->m_len;
724 		if (len < i) {
725 			mp->msg_flags |= MSG_CTRUNC;
726 			if (cmsg->cmsg_level == SOL_SOCKET
727 			    && cmsg->cmsg_type == SCM_RIGHTS)
728 				/* Do not truncate me ... */
729 				break;
730 			i = len;
731 		}
732 		error = copyout(mtod(m, void *), q, i);
733 		ktrkuser("msgcontrol", mtod(m, void *), i);
734 		if (error != 0) {
735 			/* We must free all the SCM_RIGHTS */
736 			m = control;
737 			break;
738 		}
739 		m = m->m_next;
740 		if (m)
741 			i = ALIGN(i);
742 		q += i;
743 		len -= i;
744 		if (len <= 0)
745 			break;
746 	}
747 
748 	free_control_mbuf(l, control, m);
749 
750 	mp->msg_controllen = q - (char *)mp->msg_control;
751 	return error;
752 }
753 
754 int
755 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from,
756     struct mbuf **control, register_t *retsize)
757 {
758 	struct uio	auio;
759 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov;
760 	struct iovec	*tiov;
761 	int		i, len, error, iovlen;
762 	struct socket	*so;
763 	struct iovec	*ktriov;
764 
765 	ktrkuser("msghdr", mp, sizeof *mp);
766 
767 	*from = NULL;
768 	if (control != NULL)
769 		*control = NULL;
770 
771 	if ((error = fd_getsock(s, &so)) != 0)
772 		return (error);
773 
774 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
775 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
776 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
777 				error = EMSGSIZE;
778 				goto out;
779 			}
780 			iov = malloc(sizeof(struct iovec) * mp->msg_iovlen,
781 			    M_IOV, M_WAITOK);
782 		}
783 		if (mp->msg_iovlen != 0) {
784 			error = copyin(mp->msg_iov, iov,
785 			    (size_t)(mp->msg_iovlen * sizeof(struct iovec)));
786 			if (error)
787 				goto out;
788 		}
789 		auio.uio_iov = iov;
790 	} else
791 		auio.uio_iov = mp->msg_iov;
792 	auio.uio_iovcnt = mp->msg_iovlen;
793 	auio.uio_rw = UIO_READ;
794 	auio.uio_offset = 0;			/* XXX */
795 	auio.uio_resid = 0;
796 	KASSERT(l == curlwp);
797 	auio.uio_vmspace = l->l_proc->p_vmspace;
798 
799 	tiov = auio.uio_iov;
800 	for (i = 0; i < mp->msg_iovlen; i++, tiov++) {
801 #if 0
802 		/* cannot happen iov_len is unsigned */
803 		if (tiov->iov_len < 0) {
804 			error = EINVAL;
805 			goto out;
806 		}
807 #endif
808 		/*
809 		 * Reads return ssize_t because -1 is returned on error.
810 		 * Therefore we must restrict the length to SSIZE_MAX to
811 		 * avoid garbage return values.
812 		 */
813 		auio.uio_resid += tiov->iov_len;
814 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
815 			error = EINVAL;
816 			goto out;
817 		}
818 	}
819 
820 	ktriov = NULL;
821 	if (ktrpoint(KTR_GENIO)) {
822 		iovlen = auio.uio_iovcnt * sizeof(struct iovec);
823 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
824 		memcpy(ktriov, auio.uio_iov, iovlen);
825 	}
826 
827 	len = auio.uio_resid;
828 	mp->msg_flags &= MSG_USERFLAGS;
829 	error = (*so->so_receive)(so, from, &auio, NULL, control,
830 	    &mp->msg_flags);
831 	len -= auio.uio_resid;
832 	*retsize = len;
833 	if (error != 0 && len != 0
834 	    && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
835 		/* Some data transferred */
836 		error = 0;
837 
838 	if (ktriov != NULL) {
839 		ktrgeniov(s, UIO_READ, ktriov, len, error);
840 		free(ktriov, M_TEMP);
841 	}
842 
843 	if (error != 0) {
844 		m_freem(*from);
845 		*from = NULL;
846 		if (control != NULL) {
847 			free_control_mbuf(l, *control, *control);
848 			*control = NULL;
849 		}
850 	}
851  out:
852 	if (iov != aiov)
853 		free(iov, M_TEMP);
854 	fd_putfile(s);
855 	return (error);
856 }
857 
858 
859 /* ARGSUSED */
860 int
861 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap, register_t *retval)
862 {
863 	/* {
864 		syscallarg(int)	s;
865 		syscallarg(int)	how;
866 	} */
867 	struct socket	*so;
868 	int		error;
869 
870 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
871 		return (error);
872 	solock(so);
873 	error = soshutdown(so, SCARG(uap, how));
874 	sounlock(so);
875 	fd_putfile(SCARG(uap, s));
876 	return (error);
877 }
878 
879 /* ARGSUSED */
880 int
881 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap, register_t *retval)
882 {
883 	/* {
884 		syscallarg(int)			s;
885 		syscallarg(int)			level;
886 		syscallarg(int)			name;
887 		syscallarg(const void *)	val;
888 		syscallarg(unsigned int)	valsize;
889 	} */
890 	struct proc	*p;
891 	struct mbuf	*m;
892 	struct socket	*so;
893 	int		error;
894 	unsigned int	len;
895 
896 	p = l->l_proc;
897 	m = NULL;
898 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
899 		return (error);
900 	len = SCARG(uap, valsize);
901 	if (len > MCLBYTES) {
902 		error = EINVAL;
903 		goto out;
904 	}
905 	if (SCARG(uap, val)) {
906 		m = getsombuf(so, MT_SOOPTS);
907 		if (len > MLEN)
908 			m_clget(m, M_WAIT);
909 		error = copyin(SCARG(uap, val), mtod(m, void *), len);
910 		if (error) {
911 			(void) m_free(m);
912 			goto out;
913 		}
914 		m->m_len = SCARG(uap, valsize);
915 	}
916 	error = sosetopt(so, SCARG(uap, level), SCARG(uap, name), m);
917  out:
918  	fd_putfile(SCARG(uap, s));
919 	return (error);
920 }
921 
922 /* ARGSUSED */
923 int
924 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap, register_t *retval)
925 {
926 	/* {
927 		syscallarg(int)			s;
928 		syscallarg(int)			level;
929 		syscallarg(int)			name;
930 		syscallarg(void *)		val;
931 		syscallarg(unsigned int *)	avalsize;
932 	} */
933 	struct socket	*so;
934 	struct mbuf	*m;
935 	unsigned int	op, i, valsize;
936 	int		error;
937 	char *val = SCARG(uap, val);
938 
939 	m = NULL;
940 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
941 		return (error);
942 	if (val != NULL) {
943 		error = copyin(SCARG(uap, avalsize),
944 			       &valsize, sizeof(valsize));
945 		if (error)
946 			goto out;
947 	} else
948 		valsize = 0;
949 	error = sogetopt(so, SCARG(uap, level), SCARG(uap, name), &m);
950 	if (error == 0 && val != NULL && valsize && m != NULL) {
951 		op = 0;
952 		while (m && !error && op < valsize) {
953 			i = min(m->m_len, (valsize - op));
954 			error = copyout(mtod(m, void *), val, i);
955 			op += i;
956 			val += i;
957 			m = m_free(m);
958 		}
959 		valsize = op;
960 		if (error == 0)
961 			error = copyout(&valsize,
962 					SCARG(uap, avalsize), sizeof(valsize));
963 	}
964 	if (m != NULL)
965 		(void) m_freem(m);
966  out:
967  	fd_putfile(SCARG(uap, s));
968 	return (error);
969 }
970 
971 #ifdef PIPE_SOCKETPAIR
972 /* ARGSUSED */
973 int
974 sys_pipe(struct lwp *l, const void *v, register_t *retval)
975 {
976 	file_t		*rf, *wf;
977 	struct socket	*rso, *wso;
978 	int		fd, error;
979 	proc_t		*p;
980 
981 	p = curproc;
982 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
983 		return (error);
984 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
985 		goto free1;
986 	/* remember this socket pair implements a pipe */
987 	wso->so_state |= SS_ISAPIPE;
988 	rso->so_state |= SS_ISAPIPE;
989 	if ((error = fd_allocfile(&rf, &fd)) != 0)
990 		goto free2;
991 	retval[0] = fd;
992 	rf->f_flag = FREAD;
993 	rf->f_type = DTYPE_SOCKET;
994 	rf->f_ops = &socketops;
995 	rf->f_data = rso;
996 	if ((error = fd_allocfile(&wf, &fd)) != 0)
997 		goto free3;
998 	wf->f_flag = FWRITE;
999 	wf->f_type = DTYPE_SOCKET;
1000 	wf->f_ops = &socketops;
1001 	wf->f_data = wso;
1002 	retval[1] = fd;
1003 	solock(wso);
1004 	error = unp_connect2(wso, rso, PRU_CONNECT2);
1005 	sounlock(wso);
1006 	if (error != 0)
1007 		goto free4;
1008 	fd_affix(p, wf, (int)retval[1]);
1009 	fd_affix(p, rf, (int)retval[0]);
1010 	return (0);
1011  free4:
1012 	fd_abort(p, wf, (int)retval[1]);
1013  free3:
1014 	fd_abort(p, rf, (int)retval[0]);
1015  free2:
1016 	(void)soclose(wso);
1017  free1:
1018 	(void)soclose(rso);
1019 	return (error);
1020 }
1021 #endif /* PIPE_SOCKETPAIR */
1022 
1023 /*
1024  * Get socket name.
1025  */
1026 /* ARGSUSED */
1027 int
1028 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam)
1029 {
1030 	struct socket	*so;
1031 	struct mbuf	*m;
1032 	int		error;
1033 
1034 	if ((error = fd_getsock(fd, &so)) != 0)
1035 		return error;
1036 
1037 	m = m_getclr(M_WAIT, MT_SONAME);
1038 	MCLAIM(m, so->so_mowner);
1039 
1040 	solock(so);
1041 	if (which == PRU_PEERADDR
1042 	    && (so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) == 0) {
1043 		error = ENOTCONN;
1044 	} else {
1045 		*nam = m;
1046 		error = (*so->so_proto->pr_usrreq)(so, which, NULL, m, NULL,
1047 		    NULL);
1048 	}
1049  	sounlock(so);
1050 	if (error != 0)
1051 		m_free(m);
1052  	fd_putfile(fd);
1053 	return error;
1054 }
1055 
1056 int
1057 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
1058     struct mbuf *addr)
1059 {
1060 	int len;
1061 	int error;
1062 
1063 	if (asa == NULL)
1064 		/* Assume application not interested */
1065 		return 0;
1066 
1067 	if (flags & MSG_LENUSRSPACE) {
1068 		error = copyin(alen, &len, sizeof(len));
1069 		if (error)
1070 			return error;
1071 	} else
1072 		len = *alen;
1073 	if (len < 0)
1074 		return EINVAL;
1075 
1076 	if (addr == NULL) {
1077 		len = 0;
1078 		error = 0;
1079 	} else {
1080 		if (len > addr->m_len)
1081 			len = addr->m_len;
1082 		/* Maybe this ought to copy a chain ? */
1083 		ktrkuser("sockname", mtod(addr, void *), len);
1084 		error = copyout(mtod(addr, void *), asa, len);
1085 	}
1086 
1087 	if (error == 0) {
1088 		if (flags & MSG_LENUSRSPACE)
1089 			error = copyout(&len, alen, sizeof(len));
1090 		else
1091 			*alen = len;
1092 	}
1093 
1094 	return error;
1095 }
1096 
1097 /*
1098  * Get socket name.
1099  */
1100 /* ARGSUSED */
1101 int
1102 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap, register_t *retval)
1103 {
1104 	/* {
1105 		syscallarg(int)			fdes;
1106 		syscallarg(struct sockaddr *)	asa;
1107 		syscallarg(unsigned int *)	alen;
1108 	} */
1109 	struct mbuf	*m;
1110 	int		error;
1111 
1112 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
1113 	if (error != 0)
1114 		return error;
1115 
1116 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1117 	    MSG_LENUSRSPACE, m);
1118 	if (m != NULL)
1119 		m_free(m);
1120 	return error;
1121 }
1122 
1123 /*
1124  * Get name of peer for connected socket.
1125  */
1126 /* ARGSUSED */
1127 int
1128 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap, register_t *retval)
1129 {
1130 	/* {
1131 		syscallarg(int)			fdes;
1132 		syscallarg(struct sockaddr *)	asa;
1133 		syscallarg(unsigned int *)	alen;
1134 	} */
1135 	struct mbuf	*m;
1136 	int		error;
1137 
1138 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
1139 	if (error != 0)
1140 		return error;
1141 
1142 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1143 	    MSG_LENUSRSPACE, m);
1144 	if (m != NULL)
1145 		m_free(m);
1146 	return error;
1147 }
1148 
1149 /*
1150  * XXX In a perfect world, we wouldn't pass around socket control
1151  * XXX arguments in mbufs, and this could go away.
1152  */
1153 int
1154 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
1155 {
1156 	struct sockaddr	*sa;
1157 	struct mbuf	*m;
1158 	int		error;
1159 
1160 	/*
1161 	 * We can't allow socket names > UCHAR_MAX in length, since that
1162 	 * will overflow sa_len.  Control data more than a page size in
1163 	 * length is just too much.
1164 	 */
1165 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
1166 		return (EINVAL);
1167 
1168 	/* Allocate an mbuf to hold the arguments. */
1169 	m = m_get(M_WAIT, type);
1170 	/* can't claim.  don't who to assign it to. */
1171 	if (buflen > MLEN) {
1172 		/*
1173 		 * Won't fit into a regular mbuf, so we allocate just
1174 		 * enough external storage to hold the argument.
1175 		 */
1176 		MEXTMALLOC(m, buflen, M_WAITOK);
1177 	}
1178 	m->m_len = buflen;
1179 	error = copyin(bf, mtod(m, void *), buflen);
1180 	if (error) {
1181 		(void) m_free(m);
1182 		return (error);
1183 	}
1184 	ktrkuser("sockargs", mtod(m, void *), buflen);
1185 	*mp = m;
1186 	if (type == MT_SONAME) {
1187 		sa = mtod(m, struct sockaddr *);
1188 #if BYTE_ORDER != BIG_ENDIAN
1189 		/*
1190 		 * 4.3BSD compat thing - need to stay, since bind(2),
1191 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
1192 		 */
1193 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1194 			sa->sa_family = sa->sa_len;
1195 #endif
1196 		sa->sa_len = buflen;
1197 	}
1198 	return (0);
1199 }
1200