xref: /netbsd-src/sys/kern/uipc_syscalls.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: uipc_syscalls.c,v 1.165 2013/10/09 20:15:39 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.165 2013/10/09 20:15:39 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 	if (fp2->f_flag & FNONBLOCK)
238 		so2->so_state |= SS_NBIO;
239 	else
240 		so2->so_state &= ~SS_NBIO;
241 	error = soaccept(so2, nam);
242 	so2->so_cred = kauth_cred_dup(so->so_cred);
243 	sounlock(so);
244 	if (error) {
245 		/* an error occurred, free the file descriptor and mbuf */
246 		m_freem(nam);
247 		mutex_enter(&fp2->f_lock);
248 		fp2->f_count++;
249 		mutex_exit(&fp2->f_lock);
250 		closef(fp2);
251 		fd_abort(curproc, NULL, fd);
252 	} else {
253 		fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
254 		fd_affix(curproc, fp2, fd);
255 		*name = nam;
256 	}
257 	fd_putfile(sock);
258 	if (__predict_false(mask))
259 		sigsuspendteardown(l);
260 	return (error);
261  bad:
262  	sounlock(so);
263  	m_freem(nam);
264 	fd_putfile(sock);
265  	fd_abort(curproc, fp2, fd);
266 	if (__predict_false(mask))
267 		sigsuspendteardown(l);
268  	return (error);
269 }
270 
271 int
272 sys_accept(struct lwp *l, const struct sys_accept_args *uap, register_t *retval)
273 {
274 	/* {
275 		syscallarg(int)			s;
276 		syscallarg(struct sockaddr *)	name;
277 		syscallarg(unsigned int *)	anamelen;
278 	} */
279 	int error, fd;
280 	struct mbuf *name;
281 
282 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, NULL, 0, 0);
283 	if (error != 0)
284 		return error;
285 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
286 	    MSG_LENUSRSPACE, name);
287 	if (name != NULL)
288 		m_free(name);
289 	if (error != 0) {
290 		fd = (int)*retval;
291 		if (fd_getfile(fd) != NULL)
292 			(void)fd_close(fd);
293 	}
294 	return error;
295 }
296 
297 int
298 sys_paccept(struct lwp *l, const struct sys_paccept_args *uap,
299     register_t *retval)
300 {
301 	/* {
302 		syscallarg(int)			s;
303 		syscallarg(struct sockaddr *)	name;
304 		syscallarg(unsigned int *)	anamelen;
305 		syscallarg(const sigset_t *)	mask;
306 		syscallarg(int)			flags;
307 	} */
308 	int error, fd;
309 	struct mbuf *name;
310 	sigset_t *mask, amask;
311 
312 	if (SCARG(uap, mask) != NULL) {
313 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
314 		if (error)
315 			return error;
316 		mask = &amask;
317 	} else
318 		mask = NULL;
319 
320 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, mask,
321 	    SCARG(uap, flags), FNONBLOCK);
322 	if (error != 0)
323 		return error;
324 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
325 	    MSG_LENUSRSPACE, name);
326 	if (name != NULL)
327 		m_free(name);
328 	if (error != 0) {
329 		fd = (int)*retval;
330 		if (fd_getfile(fd) != NULL)
331 			(void)fd_close(fd);
332 	}
333 	return error;
334 }
335 
336 /* ARGSUSED */
337 int
338 sys_connect(struct lwp *l, const struct sys_connect_args *uap, register_t *retval)
339 {
340 	/* {
341 		syscallarg(int)				s;
342 		syscallarg(const struct sockaddr *)	name;
343 		syscallarg(unsigned int)		namelen;
344 	} */
345 	int		error;
346 	struct mbuf	*nam;
347 
348 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
349 	    MT_SONAME);
350 	if (error)
351 		return error;
352 	return do_sys_connect(l,  SCARG(uap, s), nam);
353 }
354 
355 int
356 do_sys_connect(struct lwp *l, int fd, struct mbuf *nam)
357 {
358 	struct socket	*so;
359 	int		error;
360 	int		interrupted = 0;
361 
362 	if ((error = fd_getsock(fd, &so)) != 0) {
363 		m_freem(nam);
364 		return (error);
365 	}
366 	solock(so);
367 	MCLAIM(nam, so->so_mowner);
368 	if ((so->so_state & SS_ISCONNECTING) != 0) {
369 		error = EALREADY;
370 		goto out;
371 	}
372 
373 	error = soconnect(so, nam, l);
374 	if (error)
375 		goto bad;
376 	if ((so->so_state & (SS_NBIO|SS_ISCONNECTING)) ==
377 	    (SS_NBIO|SS_ISCONNECTING)) {
378 		error = EINPROGRESS;
379 		goto out;
380 	}
381 	while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) {
382 		error = sowait(so, true, 0);
383 		if (__predict_false((so->so_state & SS_ISABORTING) != 0)) {
384 			error = EPIPE;
385 			interrupted = 1;
386 			break;
387 		}
388 		if (error) {
389 			if (error == EINTR || error == ERESTART)
390 				interrupted = 1;
391 			break;
392 		}
393 	}
394 	if (error == 0) {
395 		error = so->so_error;
396 		so->so_error = 0;
397 	}
398  bad:
399 	if (!interrupted)
400 		so->so_state &= ~SS_ISCONNECTING;
401 	if (error == ERESTART)
402 		error = EINTR;
403  out:
404  	sounlock(so);
405  	fd_putfile(fd);
406 	m_freem(nam);
407 	return (error);
408 }
409 
410 static int
411 makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type,
412     int domain, int proto, struct socket *soo)
413 {
414 	int error;
415 	struct socket *so;
416 
417 	if ((error = socreate(domain, &so, type, proto, l, soo)) != 0)
418 		return error;
419 
420 	if ((error = fd_allocfile(fp, fd)) != 0) {
421 		soclose(so);
422 		return error;
423 	}
424 	fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0);
425 	(*fp)->f_flag = FREAD|FWRITE|
426 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
427 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
428 	(*fp)->f_type = DTYPE_SOCKET;
429 	(*fp)->f_ops = &socketops;
430 	(*fp)->f_data = so;
431 	if (flags & SOCK_NONBLOCK)
432 		so->so_state |= SS_NBIO;
433 	return 0;
434 }
435 
436 int
437 sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap,
438     register_t *retval)
439 {
440 	/* {
441 		syscallarg(int)		domain;
442 		syscallarg(int)		type;
443 		syscallarg(int)		protocol;
444 		syscallarg(int *)	rsv;
445 	} */
446 	file_t		*fp1, *fp2;
447 	struct socket	*so1, *so2;
448 	int		fd, error, sv[2];
449 	proc_t		*p;
450 	int		flags = SCARG(uap, type) & SOCK_FLAGS_MASK;
451 	int		type = SCARG(uap, type) & ~SOCK_FLAGS_MASK;
452 	int		domain = SCARG(uap, domain);
453 	int		proto = SCARG(uap, protocol);
454 
455 	p = curproc;
456 
457 	error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL);
458 	if (error)
459 		return error;
460 	so1 = fp1->f_data;
461 	sv[0] = fd;
462 
463 	error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1);
464 	if (error)
465 		goto out;
466 	so2 = fp2->f_data;
467 	sv[1] = fd;
468 
469 	solock(so1);
470 	error = soconnect2(so1, so2);
471 	if (error == 0 && type == SOCK_DGRAM) {
472 		/*
473 		 * Datagram socket connection is asymmetric.
474 		 */
475 		error = soconnect2(so2, so1);
476 	}
477 	sounlock(so1);
478 
479 	if (error == 0)
480 		error = copyout(sv, SCARG(uap, rsv), sizeof(sv));
481 	if (error == 0) {
482 		fd_affix(p, fp2, sv[1]);
483 		fd_affix(p, fp1, sv[0]);
484 		return 0;
485 	}
486 	fd_abort(p, fp2, sv[1]);
487 	(void)soclose(so2);
488 out:
489 	fd_abort(p, fp1, sv[0]);
490 	(void)soclose(so1);
491 	return error;
492 }
493 
494 int
495 sys_sendto(struct lwp *l, const struct sys_sendto_args *uap, register_t *retval)
496 {
497 	/* {
498 		syscallarg(int)				s;
499 		syscallarg(const void *)		buf;
500 		syscallarg(size_t)			len;
501 		syscallarg(int)				flags;
502 		syscallarg(const struct sockaddr *)	to;
503 		syscallarg(unsigned int)		tolen;
504 	} */
505 	struct msghdr	msg;
506 	struct iovec	aiov;
507 
508 	msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */
509 	msg.msg_namelen = SCARG(uap, tolen);
510 	msg.msg_iov = &aiov;
511 	msg.msg_iovlen = 1;
512 	msg.msg_control = NULL;
513 	msg.msg_flags = 0;
514 	aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
515 	aiov.iov_len = SCARG(uap, len);
516 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
517 }
518 
519 int
520 sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap, register_t *retval)
521 {
522 	/* {
523 		syscallarg(int)				s;
524 		syscallarg(const struct msghdr *)	msg;
525 		syscallarg(int)				flags;
526 	} */
527 	struct msghdr	msg;
528 	int		error;
529 
530 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
531 	if (error)
532 		return (error);
533 
534 	msg.msg_flags = MSG_IOVUSRSPACE;
535 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
536 }
537 
538 static int
539 do_sys_sendmsg_so(struct lwp *l, int s, struct socket *so, file_t *fp,
540     struct msghdr *mp, int flags, register_t *retsize)
541 {
542 
543 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
544 	struct mbuf	*to, *control;
545 	struct uio	auio;
546 	size_t		len, iovsz;
547 	int		i, error;
548 
549 	ktrkuser("msghdr", mp, sizeof *mp);
550 
551 	/* If the caller passed us stuff in mbufs, we must free them. */
552 	to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL;
553 	control = (mp->msg_flags & MSG_CONTROLMBUF) ? mp->msg_control : NULL;
554 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
555 
556 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
557 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
558 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
559 				error = EMSGSIZE;
560 				goto bad;
561 			}
562 			iov = kmem_alloc(iovsz, KM_SLEEP);
563 		}
564 		if (mp->msg_iovlen != 0) {
565 			error = copyin(mp->msg_iov, iov, iovsz);
566 			if (error)
567 				goto bad;
568 		}
569 		mp->msg_iov = iov;
570 	}
571 
572 	auio.uio_iov = mp->msg_iov;
573 	auio.uio_iovcnt = mp->msg_iovlen;
574 	auio.uio_rw = UIO_WRITE;
575 	auio.uio_offset = 0;			/* XXX */
576 	auio.uio_resid = 0;
577 	KASSERT(l == curlwp);
578 	auio.uio_vmspace = l->l_proc->p_vmspace;
579 
580 	for (i = 0, tiov = mp->msg_iov; i < mp->msg_iovlen; i++, tiov++) {
581 		/*
582 		 * Writes return ssize_t because -1 is returned on error.
583 		 * Therefore, we must restrict the length to SSIZE_MAX to
584 		 * avoid garbage return values.
585 		 */
586 		auio.uio_resid += tiov->iov_len;
587 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
588 			error = EINVAL;
589 			goto bad;
590 		}
591 	}
592 
593 	if (mp->msg_name && to == NULL) {
594 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
595 		    MT_SONAME);
596 		if (error)
597 			goto bad;
598 	}
599 
600 	if (mp->msg_control) {
601 		if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
602 			error = EINVAL;
603 			goto bad;
604 		}
605 		if (control == NULL) {
606 			error = sockargs(&control, mp->msg_control,
607 			    mp->msg_controllen, MT_CONTROL);
608 			if (error)
609 				goto bad;
610 		}
611 	}
612 
613 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
614 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
615 		memcpy(ktriov, auio.uio_iov, iovsz);
616 	}
617 
618 	if (mp->msg_name)
619 		MCLAIM(to, so->so_mowner);
620 	if (mp->msg_control)
621 		MCLAIM(control, so->so_mowner);
622 
623 	len = auio.uio_resid;
624 	error = (*so->so_send)(so, to, &auio, NULL, control, flags, l);
625 	/* Protocol is responsible for freeing 'control' */
626 	control = NULL;
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 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
660     register_t *retsize)
661 {
662 	int		error;
663 	struct socket	*so;
664 	file_t		*fp;
665 
666 	if ((error = fd_getsock1(s, &so, &fp)) != 0)
667 		return error;
668 	error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, retsize);
669 	fd_putfile(s);
670 	return error;
671 }
672 
673 int
674 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap, register_t *retval)
675 {
676 	/* {
677 		syscallarg(int)			s;
678 		syscallarg(void *)		buf;
679 		syscallarg(size_t)		len;
680 		syscallarg(int)			flags;
681 		syscallarg(struct sockaddr *)	from;
682 		syscallarg(unsigned int *)	fromlenaddr;
683 	} */
684 	struct msghdr	msg;
685 	struct iovec	aiov;
686 	int		error;
687 	struct mbuf	*from;
688 
689 	msg.msg_name = NULL;
690 	msg.msg_iov = &aiov;
691 	msg.msg_iovlen = 1;
692 	aiov.iov_base = SCARG(uap, buf);
693 	aiov.iov_len = SCARG(uap, len);
694 	msg.msg_control = NULL;
695 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
696 
697 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
698 	if (error != 0)
699 		return error;
700 
701 	error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
702 	    MSG_LENUSRSPACE, from);
703 	if (from != NULL)
704 		m_free(from);
705 	return error;
706 }
707 
708 int
709 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap, register_t *retval)
710 {
711 	/* {
712 		syscallarg(int)			s;
713 		syscallarg(struct msghdr *)	msg;
714 		syscallarg(int)			flags;
715 	} */
716 	struct msghdr	msg;
717 	int		error;
718 	struct mbuf	*from, *control;
719 
720 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
721 	if (error)
722 		return (error);
723 
724 	msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
725 
726 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
727 	    msg.msg_control != NULL ? &control : NULL, retval);
728 	if (error != 0)
729 		return error;
730 
731 	if (msg.msg_control != NULL)
732 		error = copyout_msg_control(l, &msg, control);
733 
734 	if (error == 0)
735 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
736 			from);
737 	if (from != NULL)
738 		m_free(from);
739 	if (error == 0) {
740 		ktrkuser("msghdr", &msg, sizeof msg);
741 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
742 	}
743 
744 	return (error);
745 }
746 
747 int
748 sys_sendmmsg(struct lwp *l, const struct sys_sendmmsg_args *uap,
749     register_t *retval)
750 {
751 	/* {
752 		syscallarg(int)			s;
753 		syscallarg(struct mmsghdr *)	mmsg;
754 		syscallarg(unsigned int)	vlen;
755 		syscallarg(unsigned int)	flags;
756 	} */
757 	struct mmsghdr mmsg;
758 	struct socket *so;
759 	file_t *fp;
760 	struct msghdr *msg = &mmsg.msg_hdr;
761 	int error, s;
762 	unsigned int vlen, flags, dg;
763 
764 	s = SCARG(uap, s);
765 	if ((error = fd_getsock1(s, &so, &fp)) != 0)
766 		return error;
767 
768 	vlen = SCARG(uap, vlen);
769 	if (vlen > 1024)
770 		vlen = 1024;
771 
772 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
773 
774 	for (dg = 0; dg < vlen;) {
775 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
776 		if (error)
777 			break;
778 
779 		msg->msg_flags = flags;
780 
781 		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval);
782 		if (error)
783 			break;
784 
785 		ktrkuser("msghdr", msg, sizeof *msg);
786 		mmsg.msg_len = *retval;
787 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
788 		if (error)
789 			break;
790 		dg++;
791 
792 	}
793 
794 	*retval = dg;
795 	if (error)
796 		so->so_error = error;
797 
798 	fd_putfile(s);
799 
800 	/*
801 	 * If we succeeded at least once, return 0, hopefully so->so_error
802 	 * will catch it next time.
803 	 */
804 	if (dg)
805 		return 0;
806 	return error;
807 }
808 
809 /*
810  * Adjust for a truncated SCM_RIGHTS control message.
811  *  This means closing any file descriptors that aren't present
812  *  in the returned buffer.
813  *  m is the mbuf holding the (already externalized) SCM_RIGHTS message.
814  */
815 static void
816 free_rights(struct mbuf *m)
817 {
818 	struct cmsghdr *cm;
819 	int *fdv;
820 	unsigned int nfds, i;
821 
822 	KASSERT(sizeof(*cm) <= m->m_len);
823 	cm = mtod(m, struct cmsghdr *);
824 
825 	KASSERT(CMSG_ALIGN(sizeof(*cm)) <= cm->cmsg_len);
826 	KASSERT(cm->cmsg_len <= m->m_len);
827 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
828 	fdv = (int *)CMSG_DATA(cm);
829 
830 	for (i = 0; i < nfds; i++)
831 		if (fd_getfile(fdv[i]) != NULL)
832 			(void)fd_close(fdv[i]);
833 }
834 
835 void
836 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
837 {
838 	struct mbuf *next;
839 	struct cmsghdr *cmsg;
840 	bool do_free_rights = false;
841 
842 	while (control != NULL) {
843 		cmsg = mtod(control, struct cmsghdr *);
844 		if (control == uncopied)
845 			do_free_rights = true;
846 		if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
847 		    && cmsg->cmsg_type == SCM_RIGHTS)
848 			free_rights(control);
849 		next = control->m_next;
850 		m_free(control);
851 		control = next;
852 	}
853 }
854 
855 /* Copy socket control/CMSG data to user buffer, frees the mbuf */
856 int
857 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
858 {
859 	int i, len, error = 0;
860 	struct cmsghdr *cmsg;
861 	struct mbuf *m;
862 	char *q;
863 
864 	len = mp->msg_controllen;
865 	if (len <= 0 || control == 0) {
866 		mp->msg_controllen = 0;
867 		free_control_mbuf(l, control, control);
868 		return 0;
869 	}
870 
871 	q = (char *)mp->msg_control;
872 
873 	for (m = control; m != NULL; ) {
874 		cmsg = mtod(m, struct cmsghdr *);
875 		i = m->m_len;
876 		if (len < i) {
877 			mp->msg_flags |= MSG_CTRUNC;
878 			if (cmsg->cmsg_level == SOL_SOCKET
879 			    && cmsg->cmsg_type == SCM_RIGHTS)
880 				/* Do not truncate me ... */
881 				break;
882 			i = len;
883 		}
884 		error = copyout(mtod(m, void *), q, i);
885 		ktrkuser("msgcontrol", mtod(m, void *), i);
886 		if (error != 0) {
887 			/* We must free all the SCM_RIGHTS */
888 			m = control;
889 			break;
890 		}
891 		m = m->m_next;
892 		if (m)
893 			i = ALIGN(i);
894 		q += i;
895 		len -= i;
896 		if (len <= 0)
897 			break;
898 	}
899 
900 	free_control_mbuf(l, control, m);
901 
902 	mp->msg_controllen = q - (char *)mp->msg_control;
903 	return error;
904 }
905 
906 static int
907 do_sys_recvmsg_so(struct lwp *l, int s, struct socket *so, struct msghdr *mp,
908     struct mbuf **from, struct mbuf **control, register_t *retsize)
909 {
910 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
911 	struct uio	auio;
912 	size_t		len, iovsz;
913 	int		i, error;
914 
915 	ktrkuser("msghdr", mp, sizeof *mp);
916 
917 	*from = NULL;
918 	if (control != NULL)
919 		*control = NULL;
920 
921 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
922 
923 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
924 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
925 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
926 				error = EMSGSIZE;
927 				goto out;
928 			}
929 			iov = kmem_alloc(iovsz, KM_SLEEP);
930 		}
931 		if (mp->msg_iovlen != 0) {
932 			error = copyin(mp->msg_iov, iov, iovsz);
933 			if (error)
934 				goto out;
935 		}
936 		auio.uio_iov = iov;
937 	} else
938 		auio.uio_iov = mp->msg_iov;
939 	auio.uio_iovcnt = mp->msg_iovlen;
940 	auio.uio_rw = UIO_READ;
941 	auio.uio_offset = 0;			/* XXX */
942 	auio.uio_resid = 0;
943 	KASSERT(l == curlwp);
944 	auio.uio_vmspace = l->l_proc->p_vmspace;
945 
946 	tiov = auio.uio_iov;
947 	for (i = 0; i < mp->msg_iovlen; i++, tiov++) {
948 		/*
949 		 * Reads return ssize_t because -1 is returned on error.
950 		 * Therefore we must restrict the length to SSIZE_MAX to
951 		 * avoid garbage return values.
952 		 */
953 		auio.uio_resid += tiov->iov_len;
954 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
955 			error = EINVAL;
956 			goto out;
957 		}
958 	}
959 
960 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
961 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
962 		memcpy(ktriov, auio.uio_iov, iovsz);
963 	}
964 
965 	len = auio.uio_resid;
966 	mp->msg_flags &= MSG_USERFLAGS;
967 	error = (*so->so_receive)(so, from, &auio, NULL, control,
968 	    &mp->msg_flags);
969 	len -= auio.uio_resid;
970 	*retsize = len;
971 	if (error != 0 && len != 0
972 	    && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
973 		/* Some data transferred */
974 		error = 0;
975 
976 	if (ktriov != NULL) {
977 		ktrgeniov(s, UIO_READ, ktriov, len, error);
978 		kmem_free(ktriov, iovsz);
979 	}
980 
981 	if (error != 0) {
982 		m_freem(*from);
983 		*from = NULL;
984 		if (control != NULL) {
985 			free_control_mbuf(l, *control, *control);
986 			*control = NULL;
987 		}
988 	}
989  out:
990 	if (iov != aiov)
991 		kmem_free(iov, iovsz);
992 	return (error);
993 }
994 
995 
996 int
997 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from,
998     struct mbuf **control, register_t *retsize)
999 {
1000 	int error;
1001 	struct socket *so;
1002 
1003 	if ((error = fd_getsock(s, &so)) != 0)
1004 		return error;
1005 	error = do_sys_recvmsg_so(l, s, so, mp, from, control, retsize);
1006 	fd_putfile(s);
1007 	return error;
1008 }
1009 
1010 int
1011 sys_recvmmsg(struct lwp *l, const struct sys_recvmmsg_args *uap,
1012     register_t *retval)
1013 {
1014 	/* {
1015 		syscallarg(int)			s;
1016 		syscallarg(struct mmsghdr *)	mmsg;
1017 		syscallarg(unsigned int)	vlen;
1018 		syscallarg(unsigned int)	flags;
1019 		syscallarg(struct timespec *)	timeout;
1020 	} */
1021 	struct mmsghdr mmsg;
1022 	struct socket *so;
1023 	struct msghdr *msg = &mmsg.msg_hdr;
1024 	int error, s;
1025 	struct mbuf *from, *control;
1026 	struct timespec ts, now;
1027 	unsigned int vlen, flags, dg;
1028 
1029 	if (SCARG(uap, timeout)) {
1030 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
1031 			return error;
1032 		getnanotime(&now);
1033 		timespecadd(&now, &ts, &ts);
1034 	}
1035 
1036 	s = SCARG(uap, s);
1037 	if ((error = fd_getsock(s, &so)) != 0)
1038 		return error;
1039 
1040 	vlen = SCARG(uap, vlen);
1041 	if (vlen > 1024)
1042 		vlen = 1024;
1043 
1044 	from = NULL;
1045 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
1046 
1047 	for (dg = 0; dg < vlen;) {
1048 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
1049 		if (error)
1050 			break;
1051 
1052 		msg->msg_flags = flags & ~MSG_WAITFORONE;
1053 
1054 		if (from != NULL) {
1055 			m_free(from);
1056 			from = NULL;
1057 		}
1058 
1059 		error = do_sys_recvmsg_so(l, s, so, msg, &from,
1060 		    msg->msg_control != NULL ? &control : NULL, retval);
1061 		if (error)
1062 			break;
1063 
1064 		if (msg->msg_control != NULL)
1065 			error = copyout_msg_control(l, msg, control);
1066 		if (error)
1067 			break;
1068 
1069 		error = copyout_sockname(msg->msg_name, &msg->msg_namelen, 0,
1070 		    from);
1071 		if (error)
1072 			break;
1073 
1074 		ktrkuser("msghdr", msg, sizeof *msg);
1075 		mmsg.msg_len = *retval;
1076 
1077 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
1078 		if (error)
1079 			break;
1080 
1081 		dg++;
1082 		if (msg->msg_flags & MSG_OOB)
1083 			break;
1084 
1085 		if (SCARG(uap, timeout)) {
1086 			getnanotime(&now);
1087 			timespecsub(&now, &ts, &now);
1088 			if (now.tv_sec > 0)
1089 				break;
1090 		}
1091 
1092 		if (flags & MSG_WAITFORONE)
1093 			flags |= MSG_DONTWAIT;
1094 
1095 	}
1096 
1097 	if (from != NULL)
1098 		m_free(from);
1099 
1100 	*retval = dg;
1101 	if (error)
1102 		so->so_error = error;
1103 
1104 	fd_putfile(s);
1105 
1106 	/*
1107 	 * If we succeeded at least once, return 0, hopefully so->so_error
1108 	 * will catch it next time.
1109 	 */
1110 	if (dg)
1111 		return 0;
1112 
1113 	return error;
1114 }
1115 
1116 /* ARGSUSED */
1117 int
1118 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap, register_t *retval)
1119 {
1120 	/* {
1121 		syscallarg(int)	s;
1122 		syscallarg(int)	how;
1123 	} */
1124 	struct socket	*so;
1125 	int		error;
1126 
1127 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
1128 		return (error);
1129 	solock(so);
1130 	error = soshutdown(so, SCARG(uap, how));
1131 	sounlock(so);
1132 	fd_putfile(SCARG(uap, s));
1133 	return (error);
1134 }
1135 
1136 /* ARGSUSED */
1137 int
1138 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap, register_t *retval)
1139 {
1140 	/* {
1141 		syscallarg(int)			s;
1142 		syscallarg(int)			level;
1143 		syscallarg(int)			name;
1144 		syscallarg(const void *)	val;
1145 		syscallarg(unsigned int)	valsize;
1146 	} */
1147 	struct sockopt	sopt;
1148 	struct socket	*so;
1149 	file_t		*fp;
1150 	int		error;
1151 	unsigned int	len;
1152 
1153 	len = SCARG(uap, valsize);
1154 	if (len > 0 && SCARG(uap, val) == NULL)
1155 		return (EINVAL);
1156 
1157 	if (len > MCLBYTES)
1158 		return (EINVAL);
1159 
1160 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
1161 		return (error);
1162 
1163 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len);
1164 
1165 	if (len > 0) {
1166 		error = copyin(SCARG(uap, val), sopt.sopt_data, len);
1167 		if (error)
1168 			goto out;
1169 	}
1170 
1171 	error = sosetopt(so, &sopt);
1172 	if (so->so_options & SO_NOSIGPIPE)
1173 		atomic_or_uint(&fp->f_flag, FNOSIGPIPE);
1174 	else
1175 		atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE);
1176 
1177  out:
1178 	sockopt_destroy(&sopt);
1179  	fd_putfile(SCARG(uap, s));
1180 	return (error);
1181 }
1182 
1183 /* ARGSUSED */
1184 int
1185 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap, register_t *retval)
1186 {
1187 	/* {
1188 		syscallarg(int)			s;
1189 		syscallarg(int)			level;
1190 		syscallarg(int)			name;
1191 		syscallarg(void *)		val;
1192 		syscallarg(unsigned int *)	avalsize;
1193 	} */
1194 	struct sockopt	sopt;
1195 	struct socket	*so;
1196 	file_t		*fp;
1197 	unsigned int	valsize, len;
1198 	int		error;
1199 
1200 	if (SCARG(uap, val) != NULL) {
1201 		error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize));
1202 		if (error)
1203 			return (error);
1204 	} else
1205 		valsize = 0;
1206 
1207 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
1208 		return (error);
1209 
1210 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), 0);
1211 
1212 	if (fp->f_flag & FNOSIGPIPE)
1213 		so->so_options |= SO_NOSIGPIPE;
1214 	else
1215 		so->so_options &= ~SO_NOSIGPIPE;
1216 	error = sogetopt(so, &sopt);
1217 	if (error)
1218 		goto out;
1219 
1220 	if (valsize > 0) {
1221 		len = min(valsize, sopt.sopt_size);
1222 		error = copyout(sopt.sopt_data, SCARG(uap, val), len);
1223 		if (error)
1224 			goto out;
1225 
1226 		error = copyout(&len, SCARG(uap, avalsize), sizeof(len));
1227 		if (error)
1228 			goto out;
1229 	}
1230 
1231  out:
1232 	sockopt_destroy(&sopt);
1233  	fd_putfile(SCARG(uap, s));
1234 	return (error);
1235 }
1236 
1237 #ifdef PIPE_SOCKETPAIR
1238 /* ARGSUSED */
1239 int
1240 pipe1(struct lwp *l, register_t *retval, int flags)
1241 {
1242 	file_t		*rf, *wf;
1243 	struct socket	*rso, *wso;
1244 	int		fd, error;
1245 	proc_t		*p;
1246 
1247 	if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
1248 		return EINVAL;
1249 	p = curproc;
1250 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
1251 		return (error);
1252 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
1253 		goto free1;
1254 	/* remember this socket pair implements a pipe */
1255 	wso->so_state |= SS_ISAPIPE;
1256 	rso->so_state |= SS_ISAPIPE;
1257 	if ((error = fd_allocfile(&rf, &fd)) != 0)
1258 		goto free2;
1259 	retval[0] = fd;
1260 	rf->f_flag = FREAD | flags;
1261 	rf->f_type = DTYPE_SOCKET;
1262 	rf->f_ops = &socketops;
1263 	rf->f_data = rso;
1264 	if ((error = fd_allocfile(&wf, &fd)) != 0)
1265 		goto free3;
1266 	wf->f_flag = FWRITE | flags;
1267 	wf->f_type = DTYPE_SOCKET;
1268 	wf->f_ops = &socketops;
1269 	wf->f_data = wso;
1270 	retval[1] = fd;
1271 	solock(wso);
1272 	error = unp_connect2(wso, rso, PRU_CONNECT2);
1273 	sounlock(wso);
1274 	if (error != 0)
1275 		goto free4;
1276 	fd_affix(p, wf, (int)retval[1]);
1277 	fd_affix(p, rf, (int)retval[0]);
1278 	return (0);
1279  free4:
1280 	fd_abort(p, wf, (int)retval[1]);
1281  free3:
1282 	fd_abort(p, rf, (int)retval[0]);
1283  free2:
1284 	(void)soclose(wso);
1285  free1:
1286 	(void)soclose(rso);
1287 	return (error);
1288 }
1289 #endif /* PIPE_SOCKETPAIR */
1290 
1291 /*
1292  * Get socket name.
1293  */
1294 int
1295 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam)
1296 {
1297 	struct socket	*so;
1298 	struct mbuf	*m;
1299 	int		error;
1300 
1301 	if ((error = fd_getsock(fd, &so)) != 0)
1302 		return error;
1303 
1304 	m = m_getclr(M_WAIT, MT_SONAME);
1305 	MCLAIM(m, so->so_mowner);
1306 
1307 	solock(so);
1308 	if (which == PRU_PEERADDR && (so->so_state & SS_ISCONNECTED) == 0) {
1309 		error = ENOTCONN;
1310 	} else {
1311 		*nam = m;
1312 		error = (*so->so_proto->pr_usrreq)(so, which, NULL, m, NULL,
1313 		    NULL);
1314 	}
1315  	sounlock(so);
1316 	if (error != 0)
1317 		m_free(m);
1318  	fd_putfile(fd);
1319 	return error;
1320 }
1321 
1322 int
1323 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
1324     struct mbuf *addr)
1325 {
1326 	int len;
1327 	int error;
1328 
1329 	if (asa == NULL)
1330 		/* Assume application not interested */
1331 		return 0;
1332 
1333 	if (flags & MSG_LENUSRSPACE) {
1334 		error = copyin(alen, &len, sizeof(len));
1335 		if (error)
1336 			return error;
1337 	} else
1338 		len = *alen;
1339 	if (len < 0)
1340 		return EINVAL;
1341 
1342 	if (addr == NULL) {
1343 		len = 0;
1344 		error = 0;
1345 	} else {
1346 		if (len > addr->m_len)
1347 			len = addr->m_len;
1348 		/* Maybe this ought to copy a chain ? */
1349 		ktrkuser(mbuftypes[MT_SONAME], mtod(addr, void *), len);
1350 		error = copyout(mtod(addr, void *), asa, len);
1351 	}
1352 
1353 	if (error == 0) {
1354 		if (flags & MSG_LENUSRSPACE)
1355 			error = copyout(&len, alen, sizeof(len));
1356 		else
1357 			*alen = len;
1358 	}
1359 
1360 	return error;
1361 }
1362 
1363 /*
1364  * Get socket name.
1365  */
1366 /* ARGSUSED */
1367 int
1368 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap, register_t *retval)
1369 {
1370 	/* {
1371 		syscallarg(int)			fdes;
1372 		syscallarg(struct sockaddr *)	asa;
1373 		syscallarg(unsigned int *)	alen;
1374 	} */
1375 	struct mbuf	*m;
1376 	int		error;
1377 
1378 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
1379 	if (error != 0)
1380 		return error;
1381 
1382 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1383 	    MSG_LENUSRSPACE, m);
1384 	if (m != NULL)
1385 		m_free(m);
1386 	return error;
1387 }
1388 
1389 /*
1390  * Get name of peer for connected socket.
1391  */
1392 /* ARGSUSED */
1393 int
1394 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap, register_t *retval)
1395 {
1396 	/* {
1397 		syscallarg(int)			fdes;
1398 		syscallarg(struct sockaddr *)	asa;
1399 		syscallarg(unsigned int *)	alen;
1400 	} */
1401 	struct mbuf	*m;
1402 	int		error;
1403 
1404 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
1405 	if (error != 0)
1406 		return error;
1407 
1408 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1409 	    MSG_LENUSRSPACE, m);
1410 	if (m != NULL)
1411 		m_free(m);
1412 	return error;
1413 }
1414 
1415 /*
1416  * XXX In a perfect world, we wouldn't pass around socket control
1417  * XXX arguments in mbufs, and this could go away.
1418  */
1419 int
1420 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
1421 {
1422 	struct sockaddr	*sa;
1423 	struct mbuf	*m;
1424 	int		error;
1425 
1426 	/*
1427 	 * We can't allow socket names > UCHAR_MAX in length, since that
1428 	 * will overflow sa_len.  Control data more than a page size in
1429 	 * length is just too much.
1430 	 */
1431 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
1432 		return (EINVAL);
1433 
1434 	/* Allocate an mbuf to hold the arguments. */
1435 	m = m_get(M_WAIT, type);
1436 	/* can't claim.  don't who to assign it to. */
1437 	if (buflen > MLEN) {
1438 		/*
1439 		 * Won't fit into a regular mbuf, so we allocate just
1440 		 * enough external storage to hold the argument.
1441 		 */
1442 		MEXTMALLOC(m, buflen, M_WAITOK);
1443 	}
1444 	m->m_len = buflen;
1445 	error = copyin(bf, mtod(m, void *), buflen);
1446 	if (error) {
1447 		(void) m_free(m);
1448 		return (error);
1449 	}
1450 	ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
1451 	*mp = m;
1452 	if (type == MT_SONAME) {
1453 		sa = mtod(m, struct sockaddr *);
1454 #if BYTE_ORDER != BIG_ENDIAN
1455 		/*
1456 		 * 4.3BSD compat thing - need to stay, since bind(2),
1457 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
1458 		 */
1459 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1460 			sa->sa_family = sa->sa_len;
1461 #endif
1462 		sa->sa_len = buflen;
1463 	}
1464 	return (0);
1465 }
1466