uipc_syscalls.c revision 1.154.2.1 1 /* $NetBSD: uipc_syscalls.c,v 1.154.2.1 2012/07/20 23:10:06 riz 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.2.1 2012/07/20 23:10:06 riz 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) && iovsz > 0) {
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 (ktrpoint(KTR_GENIO)) {
644 ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
645 if (ktriov != NULL)
646 kmem_free(ktriov, iovsz);
647 }
648
649 if (iov != aiov)
650 kmem_free(iov, iovsz);
651 if (to)
652 m_freem(to);
653 if (control)
654 m_freem(control);
655
656 return (error);
657 }
658
659 int
660 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap, register_t *retval)
661 {
662 /* {
663 syscallarg(int) s;
664 syscallarg(void *) buf;
665 syscallarg(size_t) len;
666 syscallarg(int) flags;
667 syscallarg(struct sockaddr *) from;
668 syscallarg(unsigned int *) fromlenaddr;
669 } */
670 struct msghdr msg;
671 struct iovec aiov;
672 int error;
673 struct mbuf *from;
674
675 msg.msg_name = NULL;
676 msg.msg_iov = &aiov;
677 msg.msg_iovlen = 1;
678 aiov.iov_base = SCARG(uap, buf);
679 aiov.iov_len = SCARG(uap, len);
680 msg.msg_control = NULL;
681 msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
682
683 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
684 if (error != 0)
685 return error;
686
687 error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
688 MSG_LENUSRSPACE, from);
689 if (from != NULL)
690 m_free(from);
691 return error;
692 }
693
694 int
695 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap, register_t *retval)
696 {
697 /* {
698 syscallarg(int) s;
699 syscallarg(struct msghdr *) msg;
700 syscallarg(int) flags;
701 } */
702 struct msghdr msg;
703 int error;
704 struct mbuf *from, *control;
705
706 error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
707 if (error)
708 return (error);
709
710 msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
711
712 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
713 msg.msg_control != NULL ? &control : NULL, retval);
714 if (error != 0)
715 return error;
716
717 if (msg.msg_control != NULL)
718 error = copyout_msg_control(l, &msg, control);
719
720 if (error == 0)
721 error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
722 from);
723 if (from != NULL)
724 m_free(from);
725 if (error == 0) {
726 ktrkuser("msghdr", &msg, sizeof msg);
727 error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
728 }
729
730 return (error);
731 }
732
733 /*
734 * Adjust for a truncated SCM_RIGHTS control message.
735 * This means closing any file descriptors that aren't present
736 * in the returned buffer.
737 * m is the mbuf holding the (already externalized) SCM_RIGHTS message.
738 */
739 static void
740 free_rights(struct mbuf *m)
741 {
742 int nfd;
743 int i;
744 int *fdv;
745
746 nfd = m->m_len < CMSG_SPACE(sizeof(int)) ? 0
747 : (m->m_len - CMSG_SPACE(sizeof(int))) / sizeof(int) + 1;
748 fdv = (int *) CMSG_DATA(mtod(m,struct cmsghdr *));
749 for (i = 0; i < nfd; i++) {
750 if (fd_getfile(fdv[i]) != NULL)
751 (void)fd_close(fdv[i]);
752 }
753 }
754
755 void
756 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
757 {
758 struct mbuf *next;
759 struct cmsghdr *cmsg;
760 bool do_free_rights = false;
761
762 while (control != NULL) {
763 cmsg = mtod(control, struct cmsghdr *);
764 if (control == uncopied)
765 do_free_rights = true;
766 if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
767 && cmsg->cmsg_type == SCM_RIGHTS)
768 free_rights(control);
769 next = control->m_next;
770 m_free(control);
771 control = next;
772 }
773 }
774
775 /* Copy socket control/CMSG data to user buffer, frees the mbuf */
776 int
777 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
778 {
779 int i, len, error = 0;
780 struct cmsghdr *cmsg;
781 struct mbuf *m;
782 char *q;
783
784 len = mp->msg_controllen;
785 if (len <= 0 || control == 0) {
786 mp->msg_controllen = 0;
787 free_control_mbuf(l, control, control);
788 return 0;
789 }
790
791 q = (char *)mp->msg_control;
792
793 for (m = control; m != NULL; ) {
794 cmsg = mtod(m, struct cmsghdr *);
795 i = m->m_len;
796 if (len < i) {
797 mp->msg_flags |= MSG_CTRUNC;
798 if (cmsg->cmsg_level == SOL_SOCKET
799 && cmsg->cmsg_type == SCM_RIGHTS)
800 /* Do not truncate me ... */
801 break;
802 i = len;
803 }
804 error = copyout(mtod(m, void *), q, i);
805 ktrkuser("msgcontrol", mtod(m, void *), i);
806 if (error != 0) {
807 /* We must free all the SCM_RIGHTS */
808 m = control;
809 break;
810 }
811 m = m->m_next;
812 if (m)
813 i = ALIGN(i);
814 q += i;
815 len -= i;
816 if (len <= 0)
817 break;
818 }
819
820 free_control_mbuf(l, control, m);
821
822 mp->msg_controllen = q - (char *)mp->msg_control;
823 return error;
824 }
825
826 int
827 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from,
828 struct mbuf **control, register_t *retsize)
829 {
830 struct iovec aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
831 struct socket *so;
832 struct uio auio;
833 size_t len, iovsz;
834 int i, error;
835
836 ktrkuser("msghdr", mp, sizeof *mp);
837
838 *from = NULL;
839 if (control != NULL)
840 *control = NULL;
841
842 if ((error = fd_getsock(s, &so)) != 0)
843 return (error);
844
845 iovsz = mp->msg_iovlen * sizeof(struct iovec);
846
847 if (mp->msg_flags & MSG_IOVUSRSPACE) {
848 if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
849 if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
850 error = EMSGSIZE;
851 goto out;
852 }
853 iov = kmem_alloc(iovsz, KM_SLEEP);
854 }
855 if (mp->msg_iovlen != 0) {
856 error = copyin(mp->msg_iov, iov, iovsz);
857 if (error)
858 goto out;
859 }
860 auio.uio_iov = iov;
861 } else
862 auio.uio_iov = mp->msg_iov;
863 auio.uio_iovcnt = mp->msg_iovlen;
864 auio.uio_rw = UIO_READ;
865 auio.uio_offset = 0; /* XXX */
866 auio.uio_resid = 0;
867 KASSERT(l == curlwp);
868 auio.uio_vmspace = l->l_proc->p_vmspace;
869
870 tiov = auio.uio_iov;
871 for (i = 0; i < mp->msg_iovlen; i++, tiov++) {
872 /*
873 * Reads return ssize_t because -1 is returned on error.
874 * Therefore we must restrict the length to SSIZE_MAX to
875 * avoid garbage return values.
876 */
877 auio.uio_resid += tiov->iov_len;
878 if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
879 error = EINVAL;
880 goto out;
881 }
882 }
883
884 if (ktrpoint(KTR_GENIO) && iovsz > 0) {
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 (ktrpoint(KTR_GENIO)) {
901 ktrgeniov(s, UIO_READ, ktriov, len, error);
902 if (ktriov != NULL)
903 kmem_free(ktriov, iovsz);
904 }
905
906 if (error != 0) {
907 m_freem(*from);
908 *from = NULL;
909 if (control != NULL) {
910 free_control_mbuf(l, *control, *control);
911 *control = NULL;
912 }
913 }
914 out:
915 if (iov != aiov)
916 kmem_free(iov, iovsz);
917 fd_putfile(s);
918 return (error);
919 }
920
921
922 /* ARGSUSED */
923 int
924 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap, register_t *retval)
925 {
926 /* {
927 syscallarg(int) s;
928 syscallarg(int) how;
929 } */
930 struct socket *so;
931 int error;
932
933 if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
934 return (error);
935 solock(so);
936 error = soshutdown(so, SCARG(uap, how));
937 sounlock(so);
938 fd_putfile(SCARG(uap, s));
939 return (error);
940 }
941
942 /* ARGSUSED */
943 int
944 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap, register_t *retval)
945 {
946 /* {
947 syscallarg(int) s;
948 syscallarg(int) level;
949 syscallarg(int) name;
950 syscallarg(const void *) val;
951 syscallarg(unsigned int) valsize;
952 } */
953 struct sockopt sopt;
954 struct socket *so;
955 file_t *fp;
956 int error;
957 unsigned int len;
958
959 len = SCARG(uap, valsize);
960 if (len > 0 && SCARG(uap, val) == NULL)
961 return (EINVAL);
962
963 if (len > MCLBYTES)
964 return (EINVAL);
965
966 if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
967 return (error);
968
969 sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len);
970
971 if (len > 0) {
972 error = copyin(SCARG(uap, val), sopt.sopt_data, len);
973 if (error)
974 goto out;
975 }
976
977 error = sosetopt(so, &sopt);
978 if (so->so_options & SO_NOSIGPIPE)
979 atomic_or_uint(&fp->f_flag, FNOSIGPIPE);
980 else
981 atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE);
982
983 out:
984 sockopt_destroy(&sopt);
985 fd_putfile(SCARG(uap, s));
986 return (error);
987 }
988
989 /* ARGSUSED */
990 int
991 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap, register_t *retval)
992 {
993 /* {
994 syscallarg(int) s;
995 syscallarg(int) level;
996 syscallarg(int) name;
997 syscallarg(void *) val;
998 syscallarg(unsigned int *) avalsize;
999 } */
1000 struct sockopt sopt;
1001 struct socket *so;
1002 file_t *fp;
1003 unsigned int valsize, len;
1004 int error;
1005
1006 if (SCARG(uap, val) != NULL) {
1007 error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize));
1008 if (error)
1009 return (error);
1010 } else
1011 valsize = 0;
1012
1013 if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
1014 return (error);
1015
1016 sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), 0);
1017
1018 if (fp->f_flag & FNOSIGPIPE)
1019 so->so_options |= SO_NOSIGPIPE;
1020 else
1021 so->so_options &= ~SO_NOSIGPIPE;
1022 error = sogetopt(so, &sopt);
1023 if (error)
1024 goto out;
1025
1026 if (valsize > 0) {
1027 len = min(valsize, sopt.sopt_size);
1028 error = copyout(sopt.sopt_data, SCARG(uap, val), len);
1029 if (error)
1030 goto out;
1031
1032 error = copyout(&len, SCARG(uap, avalsize), sizeof(len));
1033 if (error)
1034 goto out;
1035 }
1036
1037 out:
1038 sockopt_destroy(&sopt);
1039 fd_putfile(SCARG(uap, s));
1040 return (error);
1041 }
1042
1043 #ifdef PIPE_SOCKETPAIR
1044 /* ARGSUSED */
1045 int
1046 pipe1(struct lwp *l, register_t *retval, int flags)
1047 {
1048 file_t *rf, *wf;
1049 struct socket *rso, *wso;
1050 int fd, error;
1051 proc_t *p;
1052
1053 if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
1054 return EINVAL;
1055 p = curproc;
1056 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
1057 return (error);
1058 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
1059 goto free1;
1060 /* remember this socket pair implements a pipe */
1061 wso->so_state |= SS_ISAPIPE;
1062 rso->so_state |= SS_ISAPIPE;
1063 if ((error = fd_allocfile(&rf, &fd)) != 0)
1064 goto free2;
1065 retval[0] = fd;
1066 rf->f_flag = FREAD | flags;
1067 rf->f_type = DTYPE_SOCKET;
1068 rf->f_ops = &socketops;
1069 rf->f_data = rso;
1070 if ((error = fd_allocfile(&wf, &fd)) != 0)
1071 goto free3;
1072 wf->f_flag = FWRITE | flags;
1073 wf->f_type = DTYPE_SOCKET;
1074 wf->f_ops = &socketops;
1075 wf->f_data = wso;
1076 retval[1] = fd;
1077 solock(wso);
1078 error = unp_connect2(wso, rso, PRU_CONNECT2);
1079 sounlock(wso);
1080 if (error != 0)
1081 goto free4;
1082 fd_affix(p, wf, (int)retval[1]);
1083 fd_affix(p, rf, (int)retval[0]);
1084 return (0);
1085 free4:
1086 fd_abort(p, wf, (int)retval[1]);
1087 free3:
1088 fd_abort(p, rf, (int)retval[0]);
1089 free2:
1090 (void)soclose(wso);
1091 free1:
1092 (void)soclose(rso);
1093 return (error);
1094 }
1095 #endif /* PIPE_SOCKETPAIR */
1096
1097 /*
1098 * Get socket name.
1099 */
1100 /* ARGSUSED */
1101 int
1102 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam)
1103 {
1104 struct socket *so;
1105 struct mbuf *m;
1106 int error;
1107
1108 if ((error = fd_getsock(fd, &so)) != 0)
1109 return error;
1110
1111 m = m_getclr(M_WAIT, MT_SONAME);
1112 MCLAIM(m, so->so_mowner);
1113
1114 solock(so);
1115 if (which == PRU_PEERADDR
1116 && (so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) == 0) {
1117 error = ENOTCONN;
1118 } else {
1119 *nam = m;
1120 error = (*so->so_proto->pr_usrreq)(so, which, NULL, m, NULL,
1121 NULL);
1122 }
1123 sounlock(so);
1124 if (error != 0)
1125 m_free(m);
1126 fd_putfile(fd);
1127 return error;
1128 }
1129
1130 int
1131 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
1132 struct mbuf *addr)
1133 {
1134 int len;
1135 int error;
1136
1137 if (asa == NULL)
1138 /* Assume application not interested */
1139 return 0;
1140
1141 if (flags & MSG_LENUSRSPACE) {
1142 error = copyin(alen, &len, sizeof(len));
1143 if (error)
1144 return error;
1145 } else
1146 len = *alen;
1147 if (len < 0)
1148 return EINVAL;
1149
1150 if (addr == NULL) {
1151 len = 0;
1152 error = 0;
1153 } else {
1154 if (len > addr->m_len)
1155 len = addr->m_len;
1156 /* Maybe this ought to copy a chain ? */
1157 ktrkuser("sockname", mtod(addr, void *), len);
1158 error = copyout(mtod(addr, void *), asa, len);
1159 }
1160
1161 if (error == 0) {
1162 if (flags & MSG_LENUSRSPACE)
1163 error = copyout(&len, alen, sizeof(len));
1164 else
1165 *alen = len;
1166 }
1167
1168 return error;
1169 }
1170
1171 /*
1172 * Get socket name.
1173 */
1174 /* ARGSUSED */
1175 int
1176 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap, register_t *retval)
1177 {
1178 /* {
1179 syscallarg(int) fdes;
1180 syscallarg(struct sockaddr *) asa;
1181 syscallarg(unsigned int *) alen;
1182 } */
1183 struct mbuf *m;
1184 int error;
1185
1186 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
1187 if (error != 0)
1188 return error;
1189
1190 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1191 MSG_LENUSRSPACE, m);
1192 if (m != NULL)
1193 m_free(m);
1194 return error;
1195 }
1196
1197 /*
1198 * Get name of peer for connected socket.
1199 */
1200 /* ARGSUSED */
1201 int
1202 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap, register_t *retval)
1203 {
1204 /* {
1205 syscallarg(int) fdes;
1206 syscallarg(struct sockaddr *) asa;
1207 syscallarg(unsigned int *) alen;
1208 } */
1209 struct mbuf *m;
1210 int error;
1211
1212 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
1213 if (error != 0)
1214 return error;
1215
1216 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1217 MSG_LENUSRSPACE, m);
1218 if (m != NULL)
1219 m_free(m);
1220 return error;
1221 }
1222
1223 /*
1224 * XXX In a perfect world, we wouldn't pass around socket control
1225 * XXX arguments in mbufs, and this could go away.
1226 */
1227 int
1228 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
1229 {
1230 struct sockaddr *sa;
1231 struct mbuf *m;
1232 int error;
1233
1234 /*
1235 * We can't allow socket names > UCHAR_MAX in length, since that
1236 * will overflow sa_len. Control data more than a page size in
1237 * length is just too much.
1238 */
1239 if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
1240 return (EINVAL);
1241
1242 /* Allocate an mbuf to hold the arguments. */
1243 m = m_get(M_WAIT, type);
1244 /* can't claim. don't who to assign it to. */
1245 if (buflen > MLEN) {
1246 /*
1247 * Won't fit into a regular mbuf, so we allocate just
1248 * enough external storage to hold the argument.
1249 */
1250 MEXTMALLOC(m, buflen, M_WAITOK);
1251 }
1252 m->m_len = buflen;
1253 error = copyin(bf, mtod(m, void *), buflen);
1254 if (error) {
1255 (void) m_free(m);
1256 return (error);
1257 }
1258 ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
1259 *mp = m;
1260 if (type == MT_SONAME) {
1261 sa = mtod(m, struct sockaddr *);
1262 #if BYTE_ORDER != BIG_ENDIAN
1263 /*
1264 * 4.3BSD compat thing - need to stay, since bind(2),
1265 * connect(2), sendto(2) were not versioned for COMPAT_43.
1266 */
1267 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1268 sa->sa_family = sa->sa_len;
1269 #endif
1270 sa->sa_len = buflen;
1271 }
1272 return (0);
1273 }
1274