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