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