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