1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * sendfile(2) and related extensions: 6 * Copyright (c) 1998, David Greenman. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "opt_capsicum.h" 39 #include "opt_inet.h" 40 #include "opt_inet6.h" 41 #include "opt_sctp.h" 42 #include "opt_compat.h" 43 #include "opt_ktrace.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/capability.h> 48 #include <sys/kernel.h> 49 #include <sys/lock.h> 50 #include <sys/mutex.h> 51 #include <sys/sysproto.h> 52 #include <sys/malloc.h> 53 #include <sys/filedesc.h> 54 #include <sys/event.h> 55 #include <sys/proc.h> 56 #include <sys/fcntl.h> 57 #include <sys/file.h> 58 #include <sys/filio.h> 59 #include <sys/jail.h> 60 #include <sys/mount.h> 61 #include <sys/mbuf.h> 62 #include <sys/protosw.h> 63 #include <sys/sf_buf.h> 64 #include <sys/sysent.h> 65 #include <sys/socket.h> 66 #include <sys/socketvar.h> 67 #include <sys/signalvar.h> 68 #include <sys/syscallsubr.h> 69 #include <sys/sysctl.h> 70 #include <sys/uio.h> 71 #include <sys/vnode.h> 72 #ifdef KTRACE 73 #include <sys/ktrace.h> 74 #endif 75 #ifdef COMPAT_FREEBSD32 76 #include <compat/freebsd32/freebsd32_util.h> 77 #endif 78 79 #include <net/vnet.h> 80 81 #include <security/audit/audit.h> 82 #include <security/mac/mac_framework.h> 83 84 #include <vm/vm.h> 85 #include <vm/vm_param.h> 86 #include <vm/vm_object.h> 87 #include <vm/vm_page.h> 88 #include <vm/vm_pageout.h> 89 #include <vm/vm_kern.h> 90 #include <vm/vm_extern.h> 91 92 #if defined(INET) || defined(INET6) 93 #ifdef SCTP 94 #include <netinet/sctp.h> 95 #include <netinet/sctp_peeloff.h> 96 #endif /* SCTP */ 97 #endif /* INET || INET6 */ 98 99 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags); 100 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp); 101 102 static int accept1(struct thread *td, struct accept_args *uap, int compat); 103 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat); 104 static int getsockname1(struct thread *td, struct getsockname_args *uap, 105 int compat); 106 static int getpeername1(struct thread *td, struct getpeername_args *uap, 107 int compat); 108 109 /* 110 * NSFBUFS-related variables and associated sysctls 111 */ 112 int nsfbufs; 113 int nsfbufspeak; 114 int nsfbufsused; 115 116 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0, 117 "Maximum number of sendfile(2) sf_bufs available"); 118 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0, 119 "Number of sendfile(2) sf_bufs at peak usage"); 120 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0, 121 "Number of sendfile(2) sf_bufs in use"); 122 123 /* 124 * Convert a user file descriptor to a kernel file entry and check that, if 125 * it is a capability, the right rights are present. A reference on the file 126 * entry is held upon returning. 127 */ 128 static int 129 getsock_cap(struct filedesc *fdp, int fd, cap_rights_t rights, 130 struct file **fpp, u_int *fflagp) 131 { 132 struct file *fp; 133 #ifdef CAPABILITIES 134 struct file *fp_fromcap; 135 int error; 136 #endif 137 138 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL) 139 return (EBADF); 140 #ifdef CAPABILITIES 141 /* 142 * If the file descriptor is for a capability, test rights and use 143 * the file descriptor referenced by the capability. 144 */ 145 error = cap_funwrap(fp, rights, &fp_fromcap); 146 if (error) { 147 fdrop(fp, curthread); 148 return (error); 149 } 150 if (fp != fp_fromcap) { 151 fhold(fp_fromcap); 152 fdrop(fp, curthread); 153 fp = fp_fromcap; 154 } 155 #endif /* CAPABILITIES */ 156 if (fp->f_type != DTYPE_SOCKET) { 157 fdrop(fp, curthread); 158 return (ENOTSOCK); 159 } 160 if (fflagp != NULL) 161 *fflagp = fp->f_flag; 162 *fpp = fp; 163 return (0); 164 } 165 166 /* 167 * System call interface to the socket abstraction. 168 */ 169 #if defined(COMPAT_43) 170 #define COMPAT_OLDSOCK 171 #endif 172 173 int 174 sys_socket(td, uap) 175 struct thread *td; 176 struct socket_args /* { 177 int domain; 178 int type; 179 int protocol; 180 } */ *uap; 181 { 182 struct socket *so; 183 struct file *fp; 184 int fd, error; 185 186 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol); 187 #ifdef MAC 188 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type, 189 uap->protocol); 190 if (error) 191 return (error); 192 #endif 193 error = falloc(td, &fp, &fd, 0); 194 if (error) 195 return (error); 196 /* An extra reference on `fp' has been held for us by falloc(). */ 197 error = socreate(uap->domain, &so, uap->type, uap->protocol, 198 td->td_ucred, td); 199 if (error) { 200 fdclose(td->td_proc->p_fd, fp, fd, td); 201 } else { 202 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops); 203 td->td_retval[0] = fd; 204 } 205 fdrop(fp, td); 206 return (error); 207 } 208 209 /* ARGSUSED */ 210 int 211 sys_bind(td, uap) 212 struct thread *td; 213 struct bind_args /* { 214 int s; 215 caddr_t name; 216 int namelen; 217 } */ *uap; 218 { 219 struct sockaddr *sa; 220 int error; 221 222 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0) 223 return (error); 224 225 error = kern_bind(td, uap->s, sa); 226 free(sa, M_SONAME); 227 return (error); 228 } 229 230 int 231 kern_bind(td, fd, sa) 232 struct thread *td; 233 int fd; 234 struct sockaddr *sa; 235 { 236 struct socket *so; 237 struct file *fp; 238 int error; 239 240 AUDIT_ARG_FD(fd); 241 AUDIT_ARG_SOCKADDR(td, sa); 242 error = getsock_cap(td->td_proc->p_fd, fd, CAP_BIND, &fp, NULL); 243 if (error) 244 return (error); 245 so = fp->f_data; 246 #ifdef KTRACE 247 if (KTRPOINT(td, KTR_STRUCT)) 248 ktrsockaddr(sa); 249 #endif 250 #ifdef MAC 251 error = mac_socket_check_bind(td->td_ucred, so, sa); 252 if (error == 0) 253 #endif 254 error = sobind(so, sa, td); 255 fdrop(fp, td); 256 return (error); 257 } 258 259 /* ARGSUSED */ 260 int 261 sys_listen(td, uap) 262 struct thread *td; 263 struct listen_args /* { 264 int s; 265 int backlog; 266 } */ *uap; 267 { 268 struct socket *so; 269 struct file *fp; 270 int error; 271 272 AUDIT_ARG_FD(uap->s); 273 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_LISTEN, &fp, NULL); 274 if (error == 0) { 275 so = fp->f_data; 276 #ifdef MAC 277 error = mac_socket_check_listen(td->td_ucred, so); 278 if (error == 0) 279 #endif 280 error = solisten(so, uap->backlog, td); 281 fdrop(fp, td); 282 } 283 return(error); 284 } 285 286 /* 287 * accept1() 288 */ 289 static int 290 accept1(td, uap, compat) 291 struct thread *td; 292 struct accept_args /* { 293 int s; 294 struct sockaddr * __restrict name; 295 socklen_t * __restrict anamelen; 296 } */ *uap; 297 int compat; 298 { 299 struct sockaddr *name; 300 socklen_t namelen; 301 struct file *fp; 302 int error; 303 304 if (uap->name == NULL) 305 return (kern_accept(td, uap->s, NULL, NULL, NULL)); 306 307 error = copyin(uap->anamelen, &namelen, sizeof (namelen)); 308 if (error) 309 return (error); 310 311 error = kern_accept(td, uap->s, &name, &namelen, &fp); 312 313 /* 314 * return a namelen of zero for older code which might 315 * ignore the return value from accept. 316 */ 317 if (error) { 318 (void) copyout(&namelen, 319 uap->anamelen, sizeof(*uap->anamelen)); 320 return (error); 321 } 322 323 if (error == 0 && name != NULL) { 324 #ifdef COMPAT_OLDSOCK 325 if (compat) 326 ((struct osockaddr *)name)->sa_family = 327 name->sa_family; 328 #endif 329 error = copyout(name, uap->name, namelen); 330 } 331 if (error == 0) 332 error = copyout(&namelen, uap->anamelen, 333 sizeof(namelen)); 334 if (error) 335 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td); 336 fdrop(fp, td); 337 free(name, M_SONAME); 338 return (error); 339 } 340 341 int 342 kern_accept(struct thread *td, int s, struct sockaddr **name, 343 socklen_t *namelen, struct file **fp) 344 { 345 struct filedesc *fdp; 346 struct file *headfp, *nfp = NULL; 347 struct sockaddr *sa = NULL; 348 int error; 349 struct socket *head, *so; 350 int fd; 351 u_int fflag; 352 pid_t pgid; 353 int tmp; 354 355 if (name) { 356 *name = NULL; 357 if (*namelen < 0) 358 return (EINVAL); 359 } 360 361 AUDIT_ARG_FD(s); 362 fdp = td->td_proc->p_fd; 363 error = getsock_cap(fdp, s, CAP_ACCEPT, &headfp, &fflag); 364 if (error) 365 return (error); 366 head = headfp->f_data; 367 if ((head->so_options & SO_ACCEPTCONN) == 0) { 368 error = EINVAL; 369 goto done; 370 } 371 #ifdef MAC 372 error = mac_socket_check_accept(td->td_ucred, head); 373 if (error != 0) 374 goto done; 375 #endif 376 error = falloc(td, &nfp, &fd, 0); 377 if (error) 378 goto done; 379 ACCEPT_LOCK(); 380 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) { 381 ACCEPT_UNLOCK(); 382 error = EWOULDBLOCK; 383 goto noconnection; 384 } 385 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) { 386 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) { 387 head->so_error = ECONNABORTED; 388 break; 389 } 390 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH, 391 "accept", 0); 392 if (error) { 393 ACCEPT_UNLOCK(); 394 goto noconnection; 395 } 396 } 397 if (head->so_error) { 398 error = head->so_error; 399 head->so_error = 0; 400 ACCEPT_UNLOCK(); 401 goto noconnection; 402 } 403 so = TAILQ_FIRST(&head->so_comp); 404 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP")); 405 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP")); 406 407 /* 408 * Before changing the flags on the socket, we have to bump the 409 * reference count. Otherwise, if the protocol calls sofree(), 410 * the socket will be released due to a zero refcount. 411 */ 412 SOCK_LOCK(so); /* soref() and so_state update */ 413 soref(so); /* file descriptor reference */ 414 415 TAILQ_REMOVE(&head->so_comp, so, so_list); 416 head->so_qlen--; 417 so->so_state |= (head->so_state & SS_NBIO); 418 so->so_qstate &= ~SQ_COMP; 419 so->so_head = NULL; 420 421 SOCK_UNLOCK(so); 422 ACCEPT_UNLOCK(); 423 424 /* An extra reference on `nfp' has been held for us by falloc(). */ 425 td->td_retval[0] = fd; 426 427 /* connection has been removed from the listen queue */ 428 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0); 429 430 pgid = fgetown(&head->so_sigio); 431 if (pgid != 0) 432 fsetown(pgid, &so->so_sigio); 433 434 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops); 435 /* Sync socket nonblocking/async state with file flags */ 436 tmp = fflag & FNONBLOCK; 437 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td); 438 tmp = fflag & FASYNC; 439 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td); 440 sa = 0; 441 error = soaccept(so, &sa); 442 if (error) { 443 /* 444 * return a namelen of zero for older code which might 445 * ignore the return value from accept. 446 */ 447 if (name) 448 *namelen = 0; 449 goto noconnection; 450 } 451 if (sa == NULL) { 452 if (name) 453 *namelen = 0; 454 goto done; 455 } 456 AUDIT_ARG_SOCKADDR(td, sa); 457 if (name) { 458 /* check sa_len before it is destroyed */ 459 if (*namelen > sa->sa_len) 460 *namelen = sa->sa_len; 461 #ifdef KTRACE 462 if (KTRPOINT(td, KTR_STRUCT)) 463 ktrsockaddr(sa); 464 #endif 465 *name = sa; 466 sa = NULL; 467 } 468 noconnection: 469 if (sa) 470 free(sa, M_SONAME); 471 472 /* 473 * close the new descriptor, assuming someone hasn't ripped it 474 * out from under us. 475 */ 476 if (error) 477 fdclose(fdp, nfp, fd, td); 478 479 /* 480 * Release explicitly held references before returning. We return 481 * a reference on nfp to the caller on success if they request it. 482 */ 483 done: 484 if (fp != NULL) { 485 if (error == 0) { 486 *fp = nfp; 487 nfp = NULL; 488 } else 489 *fp = NULL; 490 } 491 if (nfp != NULL) 492 fdrop(nfp, td); 493 fdrop(headfp, td); 494 return (error); 495 } 496 497 int 498 sys_accept(td, uap) 499 struct thread *td; 500 struct accept_args *uap; 501 { 502 503 return (accept1(td, uap, 0)); 504 } 505 506 #ifdef COMPAT_OLDSOCK 507 int 508 oaccept(td, uap) 509 struct thread *td; 510 struct accept_args *uap; 511 { 512 513 return (accept1(td, uap, 1)); 514 } 515 #endif /* COMPAT_OLDSOCK */ 516 517 /* ARGSUSED */ 518 int 519 sys_connect(td, uap) 520 struct thread *td; 521 struct connect_args /* { 522 int s; 523 caddr_t name; 524 int namelen; 525 } */ *uap; 526 { 527 struct sockaddr *sa; 528 int error; 529 530 error = getsockaddr(&sa, uap->name, uap->namelen); 531 if (error) 532 return (error); 533 534 error = kern_connect(td, uap->s, sa); 535 free(sa, M_SONAME); 536 return (error); 537 } 538 539 540 int 541 kern_connect(td, fd, sa) 542 struct thread *td; 543 int fd; 544 struct sockaddr *sa; 545 { 546 struct socket *so; 547 struct file *fp; 548 int error; 549 int interrupted = 0; 550 551 AUDIT_ARG_FD(fd); 552 AUDIT_ARG_SOCKADDR(td, sa); 553 error = getsock_cap(td->td_proc->p_fd, fd, CAP_CONNECT, &fp, NULL); 554 if (error) 555 return (error); 556 so = fp->f_data; 557 if (so->so_state & SS_ISCONNECTING) { 558 error = EALREADY; 559 goto done1; 560 } 561 #ifdef KTRACE 562 if (KTRPOINT(td, KTR_STRUCT)) 563 ktrsockaddr(sa); 564 #endif 565 #ifdef MAC 566 error = mac_socket_check_connect(td->td_ucred, so, sa); 567 if (error) 568 goto bad; 569 #endif 570 error = soconnect(so, sa, td); 571 if (error) 572 goto bad; 573 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) { 574 error = EINPROGRESS; 575 goto done1; 576 } 577 SOCK_LOCK(so); 578 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 579 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH, 580 "connec", 0); 581 if (error) { 582 if (error == EINTR || error == ERESTART) 583 interrupted = 1; 584 break; 585 } 586 } 587 if (error == 0) { 588 error = so->so_error; 589 so->so_error = 0; 590 } 591 SOCK_UNLOCK(so); 592 bad: 593 if (!interrupted) 594 so->so_state &= ~SS_ISCONNECTING; 595 if (error == ERESTART) 596 error = EINTR; 597 done1: 598 fdrop(fp, td); 599 return (error); 600 } 601 602 int 603 kern_socketpair(struct thread *td, int domain, int type, int protocol, 604 int *rsv) 605 { 606 struct filedesc *fdp = td->td_proc->p_fd; 607 struct file *fp1, *fp2; 608 struct socket *so1, *so2; 609 int fd, error; 610 611 AUDIT_ARG_SOCKET(domain, type, protocol); 612 #ifdef MAC 613 /* We might want to have a separate check for socket pairs. */ 614 error = mac_socket_check_create(td->td_ucred, domain, type, 615 protocol); 616 if (error) 617 return (error); 618 #endif 619 error = socreate(domain, &so1, type, protocol, td->td_ucred, td); 620 if (error) 621 return (error); 622 error = socreate(domain, &so2, type, protocol, td->td_ucred, td); 623 if (error) 624 goto free1; 625 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */ 626 error = falloc(td, &fp1, &fd, 0); 627 if (error) 628 goto free2; 629 rsv[0] = fd; 630 fp1->f_data = so1; /* so1 already has ref count */ 631 error = falloc(td, &fp2, &fd, 0); 632 if (error) 633 goto free3; 634 fp2->f_data = so2; /* so2 already has ref count */ 635 rsv[1] = fd; 636 error = soconnect2(so1, so2); 637 if (error) 638 goto free4; 639 if (type == SOCK_DGRAM) { 640 /* 641 * Datagram socket connection is asymmetric. 642 */ 643 error = soconnect2(so2, so1); 644 if (error) 645 goto free4; 646 } 647 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops); 648 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops); 649 fdrop(fp1, td); 650 fdrop(fp2, td); 651 return (0); 652 free4: 653 fdclose(fdp, fp2, rsv[1], td); 654 fdrop(fp2, td); 655 free3: 656 fdclose(fdp, fp1, rsv[0], td); 657 fdrop(fp1, td); 658 free2: 659 if (so2 != NULL) 660 (void)soclose(so2); 661 free1: 662 if (so1 != NULL) 663 (void)soclose(so1); 664 return (error); 665 } 666 667 int 668 sys_socketpair(struct thread *td, struct socketpair_args *uap) 669 { 670 int error, sv[2]; 671 672 error = kern_socketpair(td, uap->domain, uap->type, 673 uap->protocol, sv); 674 if (error) 675 return (error); 676 error = copyout(sv, uap->rsv, 2 * sizeof(int)); 677 if (error) { 678 (void)kern_close(td, sv[0]); 679 (void)kern_close(td, sv[1]); 680 } 681 return (error); 682 } 683 684 static int 685 sendit(td, s, mp, flags) 686 struct thread *td; 687 int s; 688 struct msghdr *mp; 689 int flags; 690 { 691 struct mbuf *control; 692 struct sockaddr *to; 693 int error; 694 695 #ifdef CAPABILITY_MODE 696 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL)) 697 return (ECAPMODE); 698 #endif 699 700 if (mp->msg_name != NULL) { 701 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen); 702 if (error) { 703 to = NULL; 704 goto bad; 705 } 706 mp->msg_name = to; 707 } else { 708 to = NULL; 709 } 710 711 if (mp->msg_control) { 712 if (mp->msg_controllen < sizeof(struct cmsghdr) 713 #ifdef COMPAT_OLDSOCK 714 && mp->msg_flags != MSG_COMPAT 715 #endif 716 ) { 717 error = EINVAL; 718 goto bad; 719 } 720 error = sockargs(&control, mp->msg_control, 721 mp->msg_controllen, MT_CONTROL); 722 if (error) 723 goto bad; 724 #ifdef COMPAT_OLDSOCK 725 if (mp->msg_flags == MSG_COMPAT) { 726 struct cmsghdr *cm; 727 728 M_PREPEND(control, sizeof(*cm), M_WAITOK); 729 cm = mtod(control, struct cmsghdr *); 730 cm->cmsg_len = control->m_len; 731 cm->cmsg_level = SOL_SOCKET; 732 cm->cmsg_type = SCM_RIGHTS; 733 } 734 #endif 735 } else { 736 control = NULL; 737 } 738 739 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE); 740 741 bad: 742 if (to) 743 free(to, M_SONAME); 744 return (error); 745 } 746 747 int 748 kern_sendit(td, s, mp, flags, control, segflg) 749 struct thread *td; 750 int s; 751 struct msghdr *mp; 752 int flags; 753 struct mbuf *control; 754 enum uio_seg segflg; 755 { 756 struct file *fp; 757 struct uio auio; 758 struct iovec *iov; 759 struct socket *so; 760 int i, error; 761 ssize_t len; 762 cap_rights_t rights; 763 #ifdef KTRACE 764 struct uio *ktruio = NULL; 765 #endif 766 767 AUDIT_ARG_FD(s); 768 rights = CAP_WRITE; 769 if (mp->msg_name != NULL) { 770 AUDIT_ARG_SOCKADDR(td, mp->msg_name); 771 rights |= CAP_CONNECT; 772 } 773 error = getsock_cap(td->td_proc->p_fd, s, rights, &fp, NULL); 774 if (error) 775 return (error); 776 so = (struct socket *)fp->f_data; 777 778 #ifdef KTRACE 779 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT)) 780 ktrsockaddr(mp->msg_name); 781 #endif 782 #ifdef MAC 783 if (mp->msg_name != NULL) { 784 error = mac_socket_check_connect(td->td_ucred, so, 785 mp->msg_name); 786 if (error) 787 goto bad; 788 } 789 error = mac_socket_check_send(td->td_ucred, so); 790 if (error) 791 goto bad; 792 #endif 793 794 auio.uio_iov = mp->msg_iov; 795 auio.uio_iovcnt = mp->msg_iovlen; 796 auio.uio_segflg = segflg; 797 auio.uio_rw = UIO_WRITE; 798 auio.uio_td = td; 799 auio.uio_offset = 0; /* XXX */ 800 auio.uio_resid = 0; 801 iov = mp->msg_iov; 802 for (i = 0; i < mp->msg_iovlen; i++, iov++) { 803 if ((auio.uio_resid += iov->iov_len) < 0) { 804 error = EINVAL; 805 goto bad; 806 } 807 } 808 #ifdef KTRACE 809 if (KTRPOINT(td, KTR_GENIO)) 810 ktruio = cloneuio(&auio); 811 #endif 812 len = auio.uio_resid; 813 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td); 814 if (error) { 815 if (auio.uio_resid != len && (error == ERESTART || 816 error == EINTR || error == EWOULDBLOCK)) 817 error = 0; 818 /* Generation of SIGPIPE can be controlled per socket */ 819 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) && 820 !(flags & MSG_NOSIGNAL)) { 821 PROC_LOCK(td->td_proc); 822 tdsignal(td, SIGPIPE); 823 PROC_UNLOCK(td->td_proc); 824 } 825 } 826 if (error == 0) 827 td->td_retval[0] = len - auio.uio_resid; 828 #ifdef KTRACE 829 if (ktruio != NULL) { 830 ktruio->uio_resid = td->td_retval[0]; 831 ktrgenio(s, UIO_WRITE, ktruio, error); 832 } 833 #endif 834 bad: 835 fdrop(fp, td); 836 return (error); 837 } 838 839 int 840 sys_sendto(td, uap) 841 struct thread *td; 842 struct sendto_args /* { 843 int s; 844 caddr_t buf; 845 size_t len; 846 int flags; 847 caddr_t to; 848 int tolen; 849 } */ *uap; 850 { 851 struct msghdr msg; 852 struct iovec aiov; 853 int error; 854 855 msg.msg_name = uap->to; 856 msg.msg_namelen = uap->tolen; 857 msg.msg_iov = &aiov; 858 msg.msg_iovlen = 1; 859 msg.msg_control = 0; 860 #ifdef COMPAT_OLDSOCK 861 msg.msg_flags = 0; 862 #endif 863 aiov.iov_base = uap->buf; 864 aiov.iov_len = uap->len; 865 error = sendit(td, uap->s, &msg, uap->flags); 866 return (error); 867 } 868 869 #ifdef COMPAT_OLDSOCK 870 int 871 osend(td, uap) 872 struct thread *td; 873 struct osend_args /* { 874 int s; 875 caddr_t buf; 876 int len; 877 int flags; 878 } */ *uap; 879 { 880 struct msghdr msg; 881 struct iovec aiov; 882 int error; 883 884 msg.msg_name = 0; 885 msg.msg_namelen = 0; 886 msg.msg_iov = &aiov; 887 msg.msg_iovlen = 1; 888 aiov.iov_base = uap->buf; 889 aiov.iov_len = uap->len; 890 msg.msg_control = 0; 891 msg.msg_flags = 0; 892 error = sendit(td, uap->s, &msg, uap->flags); 893 return (error); 894 } 895 896 int 897 osendmsg(td, uap) 898 struct thread *td; 899 struct osendmsg_args /* { 900 int s; 901 caddr_t msg; 902 int flags; 903 } */ *uap; 904 { 905 struct msghdr msg; 906 struct iovec *iov; 907 int error; 908 909 error = copyin(uap->msg, &msg, sizeof (struct omsghdr)); 910 if (error) 911 return (error); 912 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 913 if (error) 914 return (error); 915 msg.msg_iov = iov; 916 msg.msg_flags = MSG_COMPAT; 917 error = sendit(td, uap->s, &msg, uap->flags); 918 free(iov, M_IOV); 919 return (error); 920 } 921 #endif 922 923 int 924 sys_sendmsg(td, uap) 925 struct thread *td; 926 struct sendmsg_args /* { 927 int s; 928 caddr_t msg; 929 int flags; 930 } */ *uap; 931 { 932 struct msghdr msg; 933 struct iovec *iov; 934 int error; 935 936 error = copyin(uap->msg, &msg, sizeof (msg)); 937 if (error) 938 return (error); 939 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 940 if (error) 941 return (error); 942 msg.msg_iov = iov; 943 #ifdef COMPAT_OLDSOCK 944 msg.msg_flags = 0; 945 #endif 946 error = sendit(td, uap->s, &msg, uap->flags); 947 free(iov, M_IOV); 948 return (error); 949 } 950 951 int 952 kern_recvit(td, s, mp, fromseg, controlp) 953 struct thread *td; 954 int s; 955 struct msghdr *mp; 956 enum uio_seg fromseg; 957 struct mbuf **controlp; 958 { 959 struct uio auio; 960 struct iovec *iov; 961 int i; 962 ssize_t len; 963 int error; 964 struct mbuf *m, *control = NULL; 965 caddr_t ctlbuf; 966 struct file *fp; 967 struct socket *so; 968 struct sockaddr *fromsa = NULL; 969 #ifdef KTRACE 970 struct uio *ktruio = NULL; 971 #endif 972 973 if (controlp != NULL) 974 *controlp = NULL; 975 976 AUDIT_ARG_FD(s); 977 error = getsock_cap(td->td_proc->p_fd, s, CAP_READ, &fp, NULL); 978 if (error) 979 return (error); 980 so = fp->f_data; 981 982 #ifdef MAC 983 error = mac_socket_check_receive(td->td_ucred, so); 984 if (error) { 985 fdrop(fp, td); 986 return (error); 987 } 988 #endif 989 990 auio.uio_iov = mp->msg_iov; 991 auio.uio_iovcnt = mp->msg_iovlen; 992 auio.uio_segflg = UIO_USERSPACE; 993 auio.uio_rw = UIO_READ; 994 auio.uio_td = td; 995 auio.uio_offset = 0; /* XXX */ 996 auio.uio_resid = 0; 997 iov = mp->msg_iov; 998 for (i = 0; i < mp->msg_iovlen; i++, iov++) { 999 if ((auio.uio_resid += iov->iov_len) < 0) { 1000 fdrop(fp, td); 1001 return (EINVAL); 1002 } 1003 } 1004 #ifdef KTRACE 1005 if (KTRPOINT(td, KTR_GENIO)) 1006 ktruio = cloneuio(&auio); 1007 #endif 1008 len = auio.uio_resid; 1009 error = soreceive(so, &fromsa, &auio, NULL, 1010 (mp->msg_control || controlp) ? &control : NULL, 1011 &mp->msg_flags); 1012 if (error) { 1013 if (auio.uio_resid != len && (error == ERESTART || 1014 error == EINTR || error == EWOULDBLOCK)) 1015 error = 0; 1016 } 1017 if (fromsa != NULL) 1018 AUDIT_ARG_SOCKADDR(td, fromsa); 1019 #ifdef KTRACE 1020 if (ktruio != NULL) { 1021 ktruio->uio_resid = len - auio.uio_resid; 1022 ktrgenio(s, UIO_READ, ktruio, error); 1023 } 1024 #endif 1025 if (error) 1026 goto out; 1027 td->td_retval[0] = len - auio.uio_resid; 1028 if (mp->msg_name) { 1029 len = mp->msg_namelen; 1030 if (len <= 0 || fromsa == NULL) 1031 len = 0; 1032 else { 1033 /* save sa_len before it is destroyed by MSG_COMPAT */ 1034 len = MIN(len, fromsa->sa_len); 1035 #ifdef COMPAT_OLDSOCK 1036 if (mp->msg_flags & MSG_COMPAT) 1037 ((struct osockaddr *)fromsa)->sa_family = 1038 fromsa->sa_family; 1039 #endif 1040 if (fromseg == UIO_USERSPACE) { 1041 error = copyout(fromsa, mp->msg_name, 1042 (unsigned)len); 1043 if (error) 1044 goto out; 1045 } else 1046 bcopy(fromsa, mp->msg_name, len); 1047 } 1048 mp->msg_namelen = len; 1049 } 1050 if (mp->msg_control && controlp == NULL) { 1051 #ifdef COMPAT_OLDSOCK 1052 /* 1053 * We assume that old recvmsg calls won't receive access 1054 * rights and other control info, esp. as control info 1055 * is always optional and those options didn't exist in 4.3. 1056 * If we receive rights, trim the cmsghdr; anything else 1057 * is tossed. 1058 */ 1059 if (control && mp->msg_flags & MSG_COMPAT) { 1060 if (mtod(control, struct cmsghdr *)->cmsg_level != 1061 SOL_SOCKET || 1062 mtod(control, struct cmsghdr *)->cmsg_type != 1063 SCM_RIGHTS) { 1064 mp->msg_controllen = 0; 1065 goto out; 1066 } 1067 control->m_len -= sizeof (struct cmsghdr); 1068 control->m_data += sizeof (struct cmsghdr); 1069 } 1070 #endif 1071 len = mp->msg_controllen; 1072 m = control; 1073 mp->msg_controllen = 0; 1074 ctlbuf = mp->msg_control; 1075 1076 while (m && len > 0) { 1077 unsigned int tocopy; 1078 1079 if (len >= m->m_len) 1080 tocopy = m->m_len; 1081 else { 1082 mp->msg_flags |= MSG_CTRUNC; 1083 tocopy = len; 1084 } 1085 1086 if ((error = copyout(mtod(m, caddr_t), 1087 ctlbuf, tocopy)) != 0) 1088 goto out; 1089 1090 ctlbuf += tocopy; 1091 len -= tocopy; 1092 m = m->m_next; 1093 } 1094 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control; 1095 } 1096 out: 1097 fdrop(fp, td); 1098 #ifdef KTRACE 1099 if (fromsa && KTRPOINT(td, KTR_STRUCT)) 1100 ktrsockaddr(fromsa); 1101 #endif 1102 if (fromsa) 1103 free(fromsa, M_SONAME); 1104 1105 if (error == 0 && controlp != NULL) 1106 *controlp = control; 1107 else if (control) 1108 m_freem(control); 1109 1110 return (error); 1111 } 1112 1113 static int 1114 recvit(td, s, mp, namelenp) 1115 struct thread *td; 1116 int s; 1117 struct msghdr *mp; 1118 void *namelenp; 1119 { 1120 int error; 1121 1122 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL); 1123 if (error) 1124 return (error); 1125 if (namelenp) { 1126 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t)); 1127 #ifdef COMPAT_OLDSOCK 1128 if (mp->msg_flags & MSG_COMPAT) 1129 error = 0; /* old recvfrom didn't check */ 1130 #endif 1131 } 1132 return (error); 1133 } 1134 1135 int 1136 sys_recvfrom(td, uap) 1137 struct thread *td; 1138 struct recvfrom_args /* { 1139 int s; 1140 caddr_t buf; 1141 size_t len; 1142 int flags; 1143 struct sockaddr * __restrict from; 1144 socklen_t * __restrict fromlenaddr; 1145 } */ *uap; 1146 { 1147 struct msghdr msg; 1148 struct iovec aiov; 1149 int error; 1150 1151 if (uap->fromlenaddr) { 1152 error = copyin(uap->fromlenaddr, 1153 &msg.msg_namelen, sizeof (msg.msg_namelen)); 1154 if (error) 1155 goto done2; 1156 } else { 1157 msg.msg_namelen = 0; 1158 } 1159 msg.msg_name = uap->from; 1160 msg.msg_iov = &aiov; 1161 msg.msg_iovlen = 1; 1162 aiov.iov_base = uap->buf; 1163 aiov.iov_len = uap->len; 1164 msg.msg_control = 0; 1165 msg.msg_flags = uap->flags; 1166 error = recvit(td, uap->s, &msg, uap->fromlenaddr); 1167 done2: 1168 return(error); 1169 } 1170 1171 #ifdef COMPAT_OLDSOCK 1172 int 1173 orecvfrom(td, uap) 1174 struct thread *td; 1175 struct recvfrom_args *uap; 1176 { 1177 1178 uap->flags |= MSG_COMPAT; 1179 return (sys_recvfrom(td, uap)); 1180 } 1181 #endif 1182 1183 #ifdef COMPAT_OLDSOCK 1184 int 1185 orecv(td, uap) 1186 struct thread *td; 1187 struct orecv_args /* { 1188 int s; 1189 caddr_t buf; 1190 int len; 1191 int flags; 1192 } */ *uap; 1193 { 1194 struct msghdr msg; 1195 struct iovec aiov; 1196 int error; 1197 1198 msg.msg_name = 0; 1199 msg.msg_namelen = 0; 1200 msg.msg_iov = &aiov; 1201 msg.msg_iovlen = 1; 1202 aiov.iov_base = uap->buf; 1203 aiov.iov_len = uap->len; 1204 msg.msg_control = 0; 1205 msg.msg_flags = uap->flags; 1206 error = recvit(td, uap->s, &msg, NULL); 1207 return (error); 1208 } 1209 1210 /* 1211 * Old recvmsg. This code takes advantage of the fact that the old msghdr 1212 * overlays the new one, missing only the flags, and with the (old) access 1213 * rights where the control fields are now. 1214 */ 1215 int 1216 orecvmsg(td, uap) 1217 struct thread *td; 1218 struct orecvmsg_args /* { 1219 int s; 1220 struct omsghdr *msg; 1221 int flags; 1222 } */ *uap; 1223 { 1224 struct msghdr msg; 1225 struct iovec *iov; 1226 int error; 1227 1228 error = copyin(uap->msg, &msg, sizeof (struct omsghdr)); 1229 if (error) 1230 return (error); 1231 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 1232 if (error) 1233 return (error); 1234 msg.msg_flags = uap->flags | MSG_COMPAT; 1235 msg.msg_iov = iov; 1236 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen); 1237 if (msg.msg_controllen && error == 0) 1238 error = copyout(&msg.msg_controllen, 1239 &uap->msg->msg_accrightslen, sizeof (int)); 1240 free(iov, M_IOV); 1241 return (error); 1242 } 1243 #endif 1244 1245 int 1246 sys_recvmsg(td, uap) 1247 struct thread *td; 1248 struct recvmsg_args /* { 1249 int s; 1250 struct msghdr *msg; 1251 int flags; 1252 } */ *uap; 1253 { 1254 struct msghdr msg; 1255 struct iovec *uiov, *iov; 1256 int error; 1257 1258 error = copyin(uap->msg, &msg, sizeof (msg)); 1259 if (error) 1260 return (error); 1261 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 1262 if (error) 1263 return (error); 1264 msg.msg_flags = uap->flags; 1265 #ifdef COMPAT_OLDSOCK 1266 msg.msg_flags &= ~MSG_COMPAT; 1267 #endif 1268 uiov = msg.msg_iov; 1269 msg.msg_iov = iov; 1270 error = recvit(td, uap->s, &msg, NULL); 1271 if (error == 0) { 1272 msg.msg_iov = uiov; 1273 error = copyout(&msg, uap->msg, sizeof(msg)); 1274 } 1275 free(iov, M_IOV); 1276 return (error); 1277 } 1278 1279 /* ARGSUSED */ 1280 int 1281 sys_shutdown(td, uap) 1282 struct thread *td; 1283 struct shutdown_args /* { 1284 int s; 1285 int how; 1286 } */ *uap; 1287 { 1288 struct socket *so; 1289 struct file *fp; 1290 int error; 1291 1292 AUDIT_ARG_FD(uap->s); 1293 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SHUTDOWN, &fp, 1294 NULL); 1295 if (error == 0) { 1296 so = fp->f_data; 1297 error = soshutdown(so, uap->how); 1298 fdrop(fp, td); 1299 } 1300 return (error); 1301 } 1302 1303 /* ARGSUSED */ 1304 int 1305 sys_setsockopt(td, uap) 1306 struct thread *td; 1307 struct setsockopt_args /* { 1308 int s; 1309 int level; 1310 int name; 1311 caddr_t val; 1312 int valsize; 1313 } */ *uap; 1314 { 1315 1316 return (kern_setsockopt(td, uap->s, uap->level, uap->name, 1317 uap->val, UIO_USERSPACE, uap->valsize)); 1318 } 1319 1320 int 1321 kern_setsockopt(td, s, level, name, val, valseg, valsize) 1322 struct thread *td; 1323 int s; 1324 int level; 1325 int name; 1326 void *val; 1327 enum uio_seg valseg; 1328 socklen_t valsize; 1329 { 1330 int error; 1331 struct socket *so; 1332 struct file *fp; 1333 struct sockopt sopt; 1334 1335 if (val == NULL && valsize != 0) 1336 return (EFAULT); 1337 if ((int)valsize < 0) 1338 return (EINVAL); 1339 1340 sopt.sopt_dir = SOPT_SET; 1341 sopt.sopt_level = level; 1342 sopt.sopt_name = name; 1343 sopt.sopt_val = val; 1344 sopt.sopt_valsize = valsize; 1345 switch (valseg) { 1346 case UIO_USERSPACE: 1347 sopt.sopt_td = td; 1348 break; 1349 case UIO_SYSSPACE: 1350 sopt.sopt_td = NULL; 1351 break; 1352 default: 1353 panic("kern_setsockopt called with bad valseg"); 1354 } 1355 1356 AUDIT_ARG_FD(s); 1357 error = getsock_cap(td->td_proc->p_fd, s, CAP_SETSOCKOPT, &fp, NULL); 1358 if (error == 0) { 1359 so = fp->f_data; 1360 error = sosetopt(so, &sopt); 1361 fdrop(fp, td); 1362 } 1363 return(error); 1364 } 1365 1366 /* ARGSUSED */ 1367 int 1368 sys_getsockopt(td, uap) 1369 struct thread *td; 1370 struct getsockopt_args /* { 1371 int s; 1372 int level; 1373 int name; 1374 void * __restrict val; 1375 socklen_t * __restrict avalsize; 1376 } */ *uap; 1377 { 1378 socklen_t valsize; 1379 int error; 1380 1381 if (uap->val) { 1382 error = copyin(uap->avalsize, &valsize, sizeof (valsize)); 1383 if (error) 1384 return (error); 1385 } 1386 1387 error = kern_getsockopt(td, uap->s, uap->level, uap->name, 1388 uap->val, UIO_USERSPACE, &valsize); 1389 1390 if (error == 0) 1391 error = copyout(&valsize, uap->avalsize, sizeof (valsize)); 1392 return (error); 1393 } 1394 1395 /* 1396 * Kernel version of getsockopt. 1397 * optval can be a userland or userspace. optlen is always a kernel pointer. 1398 */ 1399 int 1400 kern_getsockopt(td, s, level, name, val, valseg, valsize) 1401 struct thread *td; 1402 int s; 1403 int level; 1404 int name; 1405 void *val; 1406 enum uio_seg valseg; 1407 socklen_t *valsize; 1408 { 1409 int error; 1410 struct socket *so; 1411 struct file *fp; 1412 struct sockopt sopt; 1413 1414 if (val == NULL) 1415 *valsize = 0; 1416 if ((int)*valsize < 0) 1417 return (EINVAL); 1418 1419 sopt.sopt_dir = SOPT_GET; 1420 sopt.sopt_level = level; 1421 sopt.sopt_name = name; 1422 sopt.sopt_val = val; 1423 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */ 1424 switch (valseg) { 1425 case UIO_USERSPACE: 1426 sopt.sopt_td = td; 1427 break; 1428 case UIO_SYSSPACE: 1429 sopt.sopt_td = NULL; 1430 break; 1431 default: 1432 panic("kern_getsockopt called with bad valseg"); 1433 } 1434 1435 AUDIT_ARG_FD(s); 1436 error = getsock_cap(td->td_proc->p_fd, s, CAP_GETSOCKOPT, &fp, NULL); 1437 if (error == 0) { 1438 so = fp->f_data; 1439 error = sogetopt(so, &sopt); 1440 *valsize = sopt.sopt_valsize; 1441 fdrop(fp, td); 1442 } 1443 return (error); 1444 } 1445 1446 /* 1447 * getsockname1() - Get socket name. 1448 */ 1449 /* ARGSUSED */ 1450 static int 1451 getsockname1(td, uap, compat) 1452 struct thread *td; 1453 struct getsockname_args /* { 1454 int fdes; 1455 struct sockaddr * __restrict asa; 1456 socklen_t * __restrict alen; 1457 } */ *uap; 1458 int compat; 1459 { 1460 struct sockaddr *sa; 1461 socklen_t len; 1462 int error; 1463 1464 error = copyin(uap->alen, &len, sizeof(len)); 1465 if (error) 1466 return (error); 1467 1468 error = kern_getsockname(td, uap->fdes, &sa, &len); 1469 if (error) 1470 return (error); 1471 1472 if (len != 0) { 1473 #ifdef COMPAT_OLDSOCK 1474 if (compat) 1475 ((struct osockaddr *)sa)->sa_family = sa->sa_family; 1476 #endif 1477 error = copyout(sa, uap->asa, (u_int)len); 1478 } 1479 free(sa, M_SONAME); 1480 if (error == 0) 1481 error = copyout(&len, uap->alen, sizeof(len)); 1482 return (error); 1483 } 1484 1485 int 1486 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa, 1487 socklen_t *alen) 1488 { 1489 struct socket *so; 1490 struct file *fp; 1491 socklen_t len; 1492 int error; 1493 1494 if (*alen < 0) 1495 return (EINVAL); 1496 1497 AUDIT_ARG_FD(fd); 1498 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETSOCKNAME, &fp, NULL); 1499 if (error) 1500 return (error); 1501 so = fp->f_data; 1502 *sa = NULL; 1503 CURVNET_SET(so->so_vnet); 1504 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa); 1505 CURVNET_RESTORE(); 1506 if (error) 1507 goto bad; 1508 if (*sa == NULL) 1509 len = 0; 1510 else 1511 len = MIN(*alen, (*sa)->sa_len); 1512 *alen = len; 1513 #ifdef KTRACE 1514 if (KTRPOINT(td, KTR_STRUCT)) 1515 ktrsockaddr(*sa); 1516 #endif 1517 bad: 1518 fdrop(fp, td); 1519 if (error && *sa) { 1520 free(*sa, M_SONAME); 1521 *sa = NULL; 1522 } 1523 return (error); 1524 } 1525 1526 int 1527 sys_getsockname(td, uap) 1528 struct thread *td; 1529 struct getsockname_args *uap; 1530 { 1531 1532 return (getsockname1(td, uap, 0)); 1533 } 1534 1535 #ifdef COMPAT_OLDSOCK 1536 int 1537 ogetsockname(td, uap) 1538 struct thread *td; 1539 struct getsockname_args *uap; 1540 { 1541 1542 return (getsockname1(td, uap, 1)); 1543 } 1544 #endif /* COMPAT_OLDSOCK */ 1545 1546 /* 1547 * getpeername1() - Get name of peer for connected socket. 1548 */ 1549 /* ARGSUSED */ 1550 static int 1551 getpeername1(td, uap, compat) 1552 struct thread *td; 1553 struct getpeername_args /* { 1554 int fdes; 1555 struct sockaddr * __restrict asa; 1556 socklen_t * __restrict alen; 1557 } */ *uap; 1558 int compat; 1559 { 1560 struct sockaddr *sa; 1561 socklen_t len; 1562 int error; 1563 1564 error = copyin(uap->alen, &len, sizeof (len)); 1565 if (error) 1566 return (error); 1567 1568 error = kern_getpeername(td, uap->fdes, &sa, &len); 1569 if (error) 1570 return (error); 1571 1572 if (len != 0) { 1573 #ifdef COMPAT_OLDSOCK 1574 if (compat) 1575 ((struct osockaddr *)sa)->sa_family = sa->sa_family; 1576 #endif 1577 error = copyout(sa, uap->asa, (u_int)len); 1578 } 1579 free(sa, M_SONAME); 1580 if (error == 0) 1581 error = copyout(&len, uap->alen, sizeof(len)); 1582 return (error); 1583 } 1584 1585 int 1586 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa, 1587 socklen_t *alen) 1588 { 1589 struct socket *so; 1590 struct file *fp; 1591 socklen_t len; 1592 int error; 1593 1594 if (*alen < 0) 1595 return (EINVAL); 1596 1597 AUDIT_ARG_FD(fd); 1598 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETPEERNAME, &fp, NULL); 1599 if (error) 1600 return (error); 1601 so = fp->f_data; 1602 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) { 1603 error = ENOTCONN; 1604 goto done; 1605 } 1606 *sa = NULL; 1607 CURVNET_SET(so->so_vnet); 1608 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa); 1609 CURVNET_RESTORE(); 1610 if (error) 1611 goto bad; 1612 if (*sa == NULL) 1613 len = 0; 1614 else 1615 len = MIN(*alen, (*sa)->sa_len); 1616 *alen = len; 1617 #ifdef KTRACE 1618 if (KTRPOINT(td, KTR_STRUCT)) 1619 ktrsockaddr(*sa); 1620 #endif 1621 bad: 1622 if (error && *sa) { 1623 free(*sa, M_SONAME); 1624 *sa = NULL; 1625 } 1626 done: 1627 fdrop(fp, td); 1628 return (error); 1629 } 1630 1631 int 1632 sys_getpeername(td, uap) 1633 struct thread *td; 1634 struct getpeername_args *uap; 1635 { 1636 1637 return (getpeername1(td, uap, 0)); 1638 } 1639 1640 #ifdef COMPAT_OLDSOCK 1641 int 1642 ogetpeername(td, uap) 1643 struct thread *td; 1644 struct ogetpeername_args *uap; 1645 { 1646 1647 /* XXX uap should have type `getpeername_args *' to begin with. */ 1648 return (getpeername1(td, (struct getpeername_args *)uap, 1)); 1649 } 1650 #endif /* COMPAT_OLDSOCK */ 1651 1652 int 1653 sockargs(mp, buf, buflen, type) 1654 struct mbuf **mp; 1655 caddr_t buf; 1656 int buflen, type; 1657 { 1658 struct sockaddr *sa; 1659 struct mbuf *m; 1660 int error; 1661 1662 if ((u_int)buflen > MLEN) { 1663 #ifdef COMPAT_OLDSOCK 1664 if (type == MT_SONAME && (u_int)buflen <= 112) 1665 buflen = MLEN; /* unix domain compat. hack */ 1666 else 1667 #endif 1668 if ((u_int)buflen > MCLBYTES) 1669 return (EINVAL); 1670 } 1671 m = m_get(M_WAITOK, type); 1672 if ((u_int)buflen > MLEN) 1673 MCLGET(m, M_WAITOK); 1674 m->m_len = buflen; 1675 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen); 1676 if (error) 1677 (void) m_free(m); 1678 else { 1679 *mp = m; 1680 if (type == MT_SONAME) { 1681 sa = mtod(m, struct sockaddr *); 1682 1683 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN 1684 if (sa->sa_family == 0 && sa->sa_len < AF_MAX) 1685 sa->sa_family = sa->sa_len; 1686 #endif 1687 sa->sa_len = buflen; 1688 } 1689 } 1690 return (error); 1691 } 1692 1693 int 1694 getsockaddr(namp, uaddr, len) 1695 struct sockaddr **namp; 1696 caddr_t uaddr; 1697 size_t len; 1698 { 1699 struct sockaddr *sa; 1700 int error; 1701 1702 if (len > SOCK_MAXADDRLEN) 1703 return (ENAMETOOLONG); 1704 if (len < offsetof(struct sockaddr, sa_data[0])) 1705 return (EINVAL); 1706 sa = malloc(len, M_SONAME, M_WAITOK); 1707 error = copyin(uaddr, sa, len); 1708 if (error) { 1709 free(sa, M_SONAME); 1710 } else { 1711 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN 1712 if (sa->sa_family == 0 && sa->sa_len < AF_MAX) 1713 sa->sa_family = sa->sa_len; 1714 #endif 1715 sa->sa_len = len; 1716 *namp = sa; 1717 } 1718 return (error); 1719 } 1720 1721 #include <sys/condvar.h> 1722 1723 struct sendfile_sync { 1724 struct mtx mtx; 1725 struct cv cv; 1726 unsigned count; 1727 }; 1728 1729 /* 1730 * Detach mapped page and release resources back to the system. 1731 */ 1732 void 1733 sf_buf_mext(void *addr, void *args) 1734 { 1735 vm_page_t m; 1736 struct sendfile_sync *sfs; 1737 1738 m = sf_buf_page(args); 1739 sf_buf_free(args); 1740 vm_page_lock(m); 1741 vm_page_unwire(m, 0); 1742 /* 1743 * Check for the object going away on us. This can 1744 * happen since we don't hold a reference to it. 1745 * If so, we're responsible for freeing the page. 1746 */ 1747 if (m->wire_count == 0 && m->object == NULL) 1748 vm_page_free(m); 1749 vm_page_unlock(m); 1750 if (addr == NULL) 1751 return; 1752 sfs = addr; 1753 mtx_lock(&sfs->mtx); 1754 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0")); 1755 if (--sfs->count == 0) 1756 cv_signal(&sfs->cv); 1757 mtx_unlock(&sfs->mtx); 1758 } 1759 1760 /* 1761 * sendfile(2) 1762 * 1763 * int sendfile(int fd, int s, off_t offset, size_t nbytes, 1764 * struct sf_hdtr *hdtr, off_t *sbytes, int flags) 1765 * 1766 * Send a file specified by 'fd' and starting at 'offset' to a socket 1767 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes == 1768 * 0. Optionally add a header and/or trailer to the socket output. If 1769 * specified, write the total number of bytes sent into *sbytes. 1770 */ 1771 int 1772 sys_sendfile(struct thread *td, struct sendfile_args *uap) 1773 { 1774 1775 return (do_sendfile(td, uap, 0)); 1776 } 1777 1778 static int 1779 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat) 1780 { 1781 struct sf_hdtr hdtr; 1782 struct uio *hdr_uio, *trl_uio; 1783 int error; 1784 1785 hdr_uio = trl_uio = NULL; 1786 1787 if (uap->hdtr != NULL) { 1788 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr)); 1789 if (error) 1790 goto out; 1791 if (hdtr.headers != NULL) { 1792 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio); 1793 if (error) 1794 goto out; 1795 } 1796 if (hdtr.trailers != NULL) { 1797 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio); 1798 if (error) 1799 goto out; 1800 1801 } 1802 } 1803 1804 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat); 1805 out: 1806 if (hdr_uio) 1807 free(hdr_uio, M_IOV); 1808 if (trl_uio) 1809 free(trl_uio, M_IOV); 1810 return (error); 1811 } 1812 1813 #ifdef COMPAT_FREEBSD4 1814 int 1815 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap) 1816 { 1817 struct sendfile_args args; 1818 1819 args.fd = uap->fd; 1820 args.s = uap->s; 1821 args.offset = uap->offset; 1822 args.nbytes = uap->nbytes; 1823 args.hdtr = uap->hdtr; 1824 args.sbytes = uap->sbytes; 1825 args.flags = uap->flags; 1826 1827 return (do_sendfile(td, &args, 1)); 1828 } 1829 #endif /* COMPAT_FREEBSD4 */ 1830 1831 int 1832 kern_sendfile(struct thread *td, struct sendfile_args *uap, 1833 struct uio *hdr_uio, struct uio *trl_uio, int compat) 1834 { 1835 struct file *sock_fp; 1836 struct vnode *vp; 1837 struct vm_object *obj = NULL; 1838 struct socket *so = NULL; 1839 struct mbuf *m = NULL; 1840 struct sf_buf *sf; 1841 struct vm_page *pg; 1842 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0; 1843 int error, hdrlen = 0, mnw = 0; 1844 struct sendfile_sync *sfs = NULL; 1845 1846 /* 1847 * The file descriptor must be a regular file and have a 1848 * backing VM object. 1849 * File offset must be positive. If it goes beyond EOF 1850 * we send only the header/trailer and no payload data. 1851 */ 1852 AUDIT_ARG_FD(uap->fd); 1853 if ((error = fgetvp_read(td, uap->fd, CAP_READ, &vp)) != 0) 1854 goto out; 1855 vn_lock(vp, LK_SHARED | LK_RETRY); 1856 if (vp->v_type == VREG) { 1857 obj = vp->v_object; 1858 if (obj != NULL) { 1859 /* 1860 * Temporarily increase the backing VM 1861 * object's reference count so that a forced 1862 * reclamation of its vnode does not 1863 * immediately destroy it. 1864 */ 1865 VM_OBJECT_LOCK(obj); 1866 if ((obj->flags & OBJ_DEAD) == 0) { 1867 vm_object_reference_locked(obj); 1868 VM_OBJECT_UNLOCK(obj); 1869 } else { 1870 VM_OBJECT_UNLOCK(obj); 1871 obj = NULL; 1872 } 1873 } 1874 } 1875 VOP_UNLOCK(vp, 0); 1876 if (obj == NULL) { 1877 error = EINVAL; 1878 goto out; 1879 } 1880 if (uap->offset < 0) { 1881 error = EINVAL; 1882 goto out; 1883 } 1884 1885 /* 1886 * The socket must be a stream socket and connected. 1887 * Remember if it a blocking or non-blocking socket. 1888 */ 1889 if ((error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_WRITE, 1890 &sock_fp, NULL)) != 0) 1891 goto out; 1892 so = sock_fp->f_data; 1893 if (so->so_type != SOCK_STREAM) { 1894 error = EINVAL; 1895 goto out; 1896 } 1897 if ((so->so_state & SS_ISCONNECTED) == 0) { 1898 error = ENOTCONN; 1899 goto out; 1900 } 1901 /* 1902 * Do not wait on memory allocations but return ENOMEM for 1903 * caller to retry later. 1904 * XXX: Experimental. 1905 */ 1906 if (uap->flags & SF_MNOWAIT) 1907 mnw = 1; 1908 1909 if (uap->flags & SF_SYNC) { 1910 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO); 1911 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF); 1912 cv_init(&sfs->cv, "sendfile"); 1913 } 1914 1915 #ifdef MAC 1916 error = mac_socket_check_send(td->td_ucred, so); 1917 if (error) 1918 goto out; 1919 #endif 1920 1921 /* If headers are specified copy them into mbufs. */ 1922 if (hdr_uio != NULL) { 1923 hdr_uio->uio_td = td; 1924 hdr_uio->uio_rw = UIO_WRITE; 1925 if (hdr_uio->uio_resid > 0) { 1926 /* 1927 * In FBSD < 5.0 the nbytes to send also included 1928 * the header. If compat is specified subtract the 1929 * header size from nbytes. 1930 */ 1931 if (compat) { 1932 if (uap->nbytes > hdr_uio->uio_resid) 1933 uap->nbytes -= hdr_uio->uio_resid; 1934 else 1935 uap->nbytes = 0; 1936 } 1937 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK), 1938 0, 0, 0); 1939 if (m == NULL) { 1940 error = mnw ? EAGAIN : ENOBUFS; 1941 goto out; 1942 } 1943 hdrlen = m_length(m, NULL); 1944 } 1945 } 1946 1947 /* 1948 * Protect against multiple writers to the socket. 1949 * 1950 * XXXRW: Historically this has assumed non-interruptibility, so now 1951 * we implement that, but possibly shouldn't. 1952 */ 1953 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR); 1954 1955 /* 1956 * Loop through the pages of the file, starting with the requested 1957 * offset. Get a file page (do I/O if necessary), map the file page 1958 * into an sf_buf, attach an mbuf header to the sf_buf, and queue 1959 * it on the socket. 1960 * This is done in two loops. The inner loop turns as many pages 1961 * as it can, up to available socket buffer space, without blocking 1962 * into mbufs to have it bulk delivered into the socket send buffer. 1963 * The outer loop checks the state and available space of the socket 1964 * and takes care of the overall progress. 1965 */ 1966 for (off = uap->offset, rem = uap->nbytes; ; ) { 1967 struct mbuf *mtail = NULL; 1968 int loopbytes = 0; 1969 int space = 0; 1970 int done = 0; 1971 1972 /* 1973 * Check the socket state for ongoing connection, 1974 * no errors and space in socket buffer. 1975 * If space is low allow for the remainder of the 1976 * file to be processed if it fits the socket buffer. 1977 * Otherwise block in waiting for sufficient space 1978 * to proceed, or if the socket is nonblocking, return 1979 * to userland with EAGAIN while reporting how far 1980 * we've come. 1981 * We wait until the socket buffer has significant free 1982 * space to do bulk sends. This makes good use of file 1983 * system read ahead and allows packet segmentation 1984 * offloading hardware to take over lots of work. If 1985 * we were not careful here we would send off only one 1986 * sfbuf at a time. 1987 */ 1988 SOCKBUF_LOCK(&so->so_snd); 1989 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2) 1990 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2; 1991 retry_space: 1992 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1993 error = EPIPE; 1994 SOCKBUF_UNLOCK(&so->so_snd); 1995 goto done; 1996 } else if (so->so_error) { 1997 error = so->so_error; 1998 so->so_error = 0; 1999 SOCKBUF_UNLOCK(&so->so_snd); 2000 goto done; 2001 } 2002 space = sbspace(&so->so_snd); 2003 if (space < rem && 2004 (space <= 0 || 2005 space < so->so_snd.sb_lowat)) { 2006 if (so->so_state & SS_NBIO) { 2007 SOCKBUF_UNLOCK(&so->so_snd); 2008 error = EAGAIN; 2009 goto done; 2010 } 2011 /* 2012 * sbwait drops the lock while sleeping. 2013 * When we loop back to retry_space the 2014 * state may have changed and we retest 2015 * for it. 2016 */ 2017 error = sbwait(&so->so_snd); 2018 /* 2019 * An error from sbwait usually indicates that we've 2020 * been interrupted by a signal. If we've sent anything 2021 * then return bytes sent, otherwise return the error. 2022 */ 2023 if (error) { 2024 SOCKBUF_UNLOCK(&so->so_snd); 2025 goto done; 2026 } 2027 goto retry_space; 2028 } 2029 SOCKBUF_UNLOCK(&so->so_snd); 2030 2031 /* 2032 * Reduce space in the socket buffer by the size of 2033 * the header mbuf chain. 2034 * hdrlen is set to 0 after the first loop. 2035 */ 2036 space -= hdrlen; 2037 2038 /* 2039 * Loop and construct maximum sized mbuf chain to be bulk 2040 * dumped into socket buffer. 2041 */ 2042 while (space > loopbytes) { 2043 vm_pindex_t pindex; 2044 vm_offset_t pgoff; 2045 struct mbuf *m0; 2046 2047 VM_OBJECT_LOCK(obj); 2048 /* 2049 * Calculate the amount to transfer. 2050 * Not to exceed a page, the EOF, 2051 * or the passed in nbytes. 2052 */ 2053 pgoff = (vm_offset_t)(off & PAGE_MASK); 2054 xfsize = omin(PAGE_SIZE - pgoff, 2055 obj->un_pager.vnp.vnp_size - uap->offset - 2056 fsbytes - loopbytes); 2057 if (uap->nbytes) 2058 rem = (uap->nbytes - fsbytes - loopbytes); 2059 else 2060 rem = obj->un_pager.vnp.vnp_size - 2061 uap->offset - fsbytes - loopbytes; 2062 xfsize = omin(rem, xfsize); 2063 xfsize = omin(space - loopbytes, xfsize); 2064 if (xfsize <= 0) { 2065 VM_OBJECT_UNLOCK(obj); 2066 done = 1; /* all data sent */ 2067 break; 2068 } 2069 2070 /* 2071 * Attempt to look up the page. Allocate 2072 * if not found or wait and loop if busy. 2073 */ 2074 pindex = OFF_TO_IDX(off); 2075 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY | 2076 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY); 2077 2078 /* 2079 * Check if page is valid for what we need, 2080 * otherwise initiate I/O. 2081 * If we already turned some pages into mbufs, 2082 * send them off before we come here again and 2083 * block. 2084 */ 2085 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize)) 2086 VM_OBJECT_UNLOCK(obj); 2087 else if (m != NULL) 2088 error = EAGAIN; /* send what we already got */ 2089 else if (uap->flags & SF_NODISKIO) 2090 error = EBUSY; 2091 else { 2092 int bsize; 2093 ssize_t resid; 2094 2095 /* 2096 * Ensure that our page is still around 2097 * when the I/O completes. 2098 */ 2099 vm_page_io_start(pg); 2100 VM_OBJECT_UNLOCK(obj); 2101 2102 /* 2103 * Get the page from backing store. 2104 */ 2105 error = vn_lock(vp, LK_SHARED); 2106 if (error != 0) 2107 goto after_read; 2108 bsize = vp->v_mount->mnt_stat.f_iosize; 2109 2110 /* 2111 * XXXMAC: Because we don't have fp->f_cred 2112 * here, we pass in NOCRED. This is probably 2113 * wrong, but is consistent with our original 2114 * implementation. 2115 */ 2116 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE, 2117 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED | 2118 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT), 2119 td->td_ucred, NOCRED, &resid, td); 2120 VOP_UNLOCK(vp, 0); 2121 after_read: 2122 VM_OBJECT_LOCK(obj); 2123 vm_page_io_finish(pg); 2124 if (!error) 2125 VM_OBJECT_UNLOCK(obj); 2126 mbstat.sf_iocnt++; 2127 } 2128 if (error) { 2129 vm_page_lock(pg); 2130 vm_page_unwire(pg, 0); 2131 /* 2132 * See if anyone else might know about 2133 * this page. If not and it is not valid, 2134 * then free it. 2135 */ 2136 if (pg->wire_count == 0 && pg->valid == 0 && 2137 pg->busy == 0 && !(pg->oflags & VPO_BUSY)) 2138 vm_page_free(pg); 2139 vm_page_unlock(pg); 2140 VM_OBJECT_UNLOCK(obj); 2141 if (error == EAGAIN) 2142 error = 0; /* not a real error */ 2143 break; 2144 } 2145 2146 /* 2147 * Get a sendfile buf. When allocating the 2148 * first buffer for mbuf chain, we usually 2149 * wait as long as necessary, but this wait 2150 * can be interrupted. For consequent 2151 * buffers, do not sleep, since several 2152 * threads might exhaust the buffers and then 2153 * deadlock. 2154 */ 2155 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT : 2156 SFB_CATCH); 2157 if (sf == NULL) { 2158 mbstat.sf_allocfail++; 2159 vm_page_lock(pg); 2160 vm_page_unwire(pg, 0); 2161 KASSERT(pg->object != NULL, 2162 ("kern_sendfile: object disappeared")); 2163 vm_page_unlock(pg); 2164 if (m == NULL) 2165 error = (mnw ? EAGAIN : EINTR); 2166 break; 2167 } 2168 2169 /* 2170 * Get an mbuf and set it up as having 2171 * external storage. 2172 */ 2173 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA); 2174 if (m0 == NULL) { 2175 error = (mnw ? EAGAIN : ENOBUFS); 2176 sf_buf_mext((void *)sf_buf_kva(sf), sf); 2177 break; 2178 } 2179 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext, 2180 sfs, sf, M_RDONLY, EXT_SFBUF); 2181 m0->m_data = (char *)sf_buf_kva(sf) + pgoff; 2182 m0->m_len = xfsize; 2183 2184 /* Append to mbuf chain. */ 2185 if (mtail != NULL) 2186 mtail->m_next = m0; 2187 else if (m != NULL) 2188 m_last(m)->m_next = m0; 2189 else 2190 m = m0; 2191 mtail = m0; 2192 2193 /* Keep track of bits processed. */ 2194 loopbytes += xfsize; 2195 off += xfsize; 2196 2197 if (sfs != NULL) { 2198 mtx_lock(&sfs->mtx); 2199 sfs->count++; 2200 mtx_unlock(&sfs->mtx); 2201 } 2202 } 2203 2204 /* Add the buffer chain to the socket buffer. */ 2205 if (m != NULL) { 2206 int mlen, err; 2207 2208 mlen = m_length(m, NULL); 2209 SOCKBUF_LOCK(&so->so_snd); 2210 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 2211 error = EPIPE; 2212 SOCKBUF_UNLOCK(&so->so_snd); 2213 goto done; 2214 } 2215 SOCKBUF_UNLOCK(&so->so_snd); 2216 CURVNET_SET(so->so_vnet); 2217 /* Avoid error aliasing. */ 2218 err = (*so->so_proto->pr_usrreqs->pru_send) 2219 (so, 0, m, NULL, NULL, td); 2220 CURVNET_RESTORE(); 2221 if (err == 0) { 2222 /* 2223 * We need two counters to get the 2224 * file offset and nbytes to send 2225 * right: 2226 * - sbytes contains the total amount 2227 * of bytes sent, including headers. 2228 * - fsbytes contains the total amount 2229 * of bytes sent from the file. 2230 */ 2231 sbytes += mlen; 2232 fsbytes += mlen; 2233 if (hdrlen) { 2234 fsbytes -= hdrlen; 2235 hdrlen = 0; 2236 } 2237 } else if (error == 0) 2238 error = err; 2239 m = NULL; /* pru_send always consumes */ 2240 } 2241 2242 /* Quit outer loop on error or when we're done. */ 2243 if (done) 2244 break; 2245 if (error) 2246 goto done; 2247 } 2248 2249 /* 2250 * Send trailers. Wimp out and use writev(2). 2251 */ 2252 if (trl_uio != NULL) { 2253 sbunlock(&so->so_snd); 2254 error = kern_writev(td, uap->s, trl_uio); 2255 if (error == 0) 2256 sbytes += td->td_retval[0]; 2257 goto out; 2258 } 2259 2260 done: 2261 sbunlock(&so->so_snd); 2262 out: 2263 /* 2264 * If there was no error we have to clear td->td_retval[0] 2265 * because it may have been set by writev. 2266 */ 2267 if (error == 0) { 2268 td->td_retval[0] = 0; 2269 } 2270 if (uap->sbytes != NULL) { 2271 copyout(&sbytes, uap->sbytes, sizeof(off_t)); 2272 } 2273 if (obj != NULL) 2274 vm_object_deallocate(obj); 2275 if (vp != NULL) 2276 vrele(vp); 2277 if (so) 2278 fdrop(sock_fp, td); 2279 if (m) 2280 m_freem(m); 2281 2282 if (sfs != NULL) { 2283 mtx_lock(&sfs->mtx); 2284 if (sfs->count != 0) 2285 cv_wait(&sfs->cv, &sfs->mtx); 2286 KASSERT(sfs->count == 0, ("sendfile sync still busy")); 2287 cv_destroy(&sfs->cv); 2288 mtx_destroy(&sfs->mtx); 2289 free(sfs, M_TEMP); 2290 } 2291 2292 if (error == ERESTART) 2293 error = EINTR; 2294 2295 return (error); 2296 } 2297 2298 /* 2299 * SCTP syscalls. 2300 * Functionality only compiled in if SCTP is defined in the kernel Makefile, 2301 * otherwise all return EOPNOTSUPP. 2302 * XXX: We should make this loadable one day. 2303 */ 2304 int 2305 sys_sctp_peeloff(td, uap) 2306 struct thread *td; 2307 struct sctp_peeloff_args /* { 2308 int sd; 2309 caddr_t name; 2310 } */ *uap; 2311 { 2312 #if (defined(INET) || defined(INET6)) && defined(SCTP) 2313 struct file *nfp = NULL; 2314 int error; 2315 struct socket *head, *so; 2316 int fd; 2317 u_int fflag; 2318 2319 AUDIT_ARG_FD(uap->sd); 2320 error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag); 2321 if (error) 2322 goto done2; 2323 if (head->so_proto->pr_protocol != IPPROTO_SCTP) { 2324 error = EOPNOTSUPP; 2325 goto done; 2326 } 2327 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name); 2328 if (error) 2329 goto done; 2330 /* 2331 * At this point we know we do have a assoc to pull 2332 * we proceed to get the fd setup. This may block 2333 * but that is ok. 2334 */ 2335 2336 error = falloc(td, &nfp, &fd, 0); 2337 if (error) 2338 goto done; 2339 td->td_retval[0] = fd; 2340 2341 CURVNET_SET(head->so_vnet); 2342 so = sonewconn(head, SS_ISCONNECTED); 2343 if (so == NULL) 2344 goto noconnection; 2345 /* 2346 * Before changing the flags on the socket, we have to bump the 2347 * reference count. Otherwise, if the protocol calls sofree(), 2348 * the socket will be released due to a zero refcount. 2349 */ 2350 SOCK_LOCK(so); 2351 soref(so); /* file descriptor reference */ 2352 SOCK_UNLOCK(so); 2353 2354 ACCEPT_LOCK(); 2355 2356 TAILQ_REMOVE(&head->so_comp, so, so_list); 2357 head->so_qlen--; 2358 so->so_state |= (head->so_state & SS_NBIO); 2359 so->so_state &= ~SS_NOFDREF; 2360 so->so_qstate &= ~SQ_COMP; 2361 so->so_head = NULL; 2362 ACCEPT_UNLOCK(); 2363 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops); 2364 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name); 2365 if (error) 2366 goto noconnection; 2367 if (head->so_sigio != NULL) 2368 fsetown(fgetown(&head->so_sigio), &so->so_sigio); 2369 2370 noconnection: 2371 /* 2372 * close the new descriptor, assuming someone hasn't ripped it 2373 * out from under us. 2374 */ 2375 if (error) 2376 fdclose(td->td_proc->p_fd, nfp, fd, td); 2377 2378 /* 2379 * Release explicitly held references before returning. 2380 */ 2381 CURVNET_RESTORE(); 2382 done: 2383 if (nfp != NULL) 2384 fdrop(nfp, td); 2385 fputsock(head); 2386 done2: 2387 return (error); 2388 #else /* SCTP */ 2389 return (EOPNOTSUPP); 2390 #endif /* SCTP */ 2391 } 2392 2393 int 2394 sys_sctp_generic_sendmsg (td, uap) 2395 struct thread *td; 2396 struct sctp_generic_sendmsg_args /* { 2397 int sd, 2398 caddr_t msg, 2399 int mlen, 2400 caddr_t to, 2401 __socklen_t tolen, 2402 struct sctp_sndrcvinfo *sinfo, 2403 int flags 2404 } */ *uap; 2405 { 2406 #if (defined(INET) || defined(INET6)) && defined(SCTP) 2407 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL; 2408 struct socket *so; 2409 struct file *fp = NULL; 2410 int error = 0, len; 2411 struct sockaddr *to = NULL; 2412 #ifdef KTRACE 2413 struct uio *ktruio = NULL; 2414 #endif 2415 struct uio auio; 2416 struct iovec iov[1]; 2417 cap_rights_t rights; 2418 2419 if (uap->sinfo) { 2420 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo)); 2421 if (error) 2422 return (error); 2423 u_sinfo = &sinfo; 2424 } 2425 2426 rights = CAP_WRITE; 2427 if (uap->tolen) { 2428 error = getsockaddr(&to, uap->to, uap->tolen); 2429 if (error) { 2430 to = NULL; 2431 goto sctp_bad2; 2432 } 2433 rights |= CAP_CONNECT; 2434 } 2435 2436 AUDIT_ARG_FD(uap->sd); 2437 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL); 2438 if (error) 2439 goto sctp_bad; 2440 #ifdef KTRACE 2441 if (to && (KTRPOINT(td, KTR_STRUCT))) 2442 ktrsockaddr(to); 2443 #endif 2444 2445 iov[0].iov_base = uap->msg; 2446 iov[0].iov_len = uap->mlen; 2447 2448 so = (struct socket *)fp->f_data; 2449 if (so->so_proto->pr_protocol != IPPROTO_SCTP) { 2450 error = EOPNOTSUPP; 2451 goto sctp_bad; 2452 } 2453 #ifdef MAC 2454 error = mac_socket_check_send(td->td_ucred, so); 2455 if (error) 2456 goto sctp_bad; 2457 #endif /* MAC */ 2458 2459 auio.uio_iov = iov; 2460 auio.uio_iovcnt = 1; 2461 auio.uio_segflg = UIO_USERSPACE; 2462 auio.uio_rw = UIO_WRITE; 2463 auio.uio_td = td; 2464 auio.uio_offset = 0; /* XXX */ 2465 auio.uio_resid = 0; 2466 len = auio.uio_resid = uap->mlen; 2467 CURVNET_SET(so->so_vnet); 2468 error = sctp_lower_sosend(so, to, &auio, 2469 (struct mbuf *)NULL, (struct mbuf *)NULL, 2470 uap->flags, u_sinfo, td); 2471 CURVNET_RESTORE(); 2472 if (error) { 2473 if (auio.uio_resid != len && (error == ERESTART || 2474 error == EINTR || error == EWOULDBLOCK)) 2475 error = 0; 2476 /* Generation of SIGPIPE can be controlled per socket. */ 2477 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) && 2478 !(uap->flags & MSG_NOSIGNAL)) { 2479 PROC_LOCK(td->td_proc); 2480 tdsignal(td, SIGPIPE); 2481 PROC_UNLOCK(td->td_proc); 2482 } 2483 } 2484 if (error == 0) 2485 td->td_retval[0] = len - auio.uio_resid; 2486 #ifdef KTRACE 2487 if (ktruio != NULL) { 2488 ktruio->uio_resid = td->td_retval[0]; 2489 ktrgenio(uap->sd, UIO_WRITE, ktruio, error); 2490 } 2491 #endif /* KTRACE */ 2492 sctp_bad: 2493 if (fp) 2494 fdrop(fp, td); 2495 sctp_bad2: 2496 if (to) 2497 free(to, M_SONAME); 2498 return (error); 2499 #else /* SCTP */ 2500 return (EOPNOTSUPP); 2501 #endif /* SCTP */ 2502 } 2503 2504 int 2505 sys_sctp_generic_sendmsg_iov(td, uap) 2506 struct thread *td; 2507 struct sctp_generic_sendmsg_iov_args /* { 2508 int sd, 2509 struct iovec *iov, 2510 int iovlen, 2511 caddr_t to, 2512 __socklen_t tolen, 2513 struct sctp_sndrcvinfo *sinfo, 2514 int flags 2515 } */ *uap; 2516 { 2517 #if (defined(INET) || defined(INET6)) && defined(SCTP) 2518 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL; 2519 struct socket *so; 2520 struct file *fp = NULL; 2521 int error=0, i; 2522 ssize_t len; 2523 struct sockaddr *to = NULL; 2524 #ifdef KTRACE 2525 struct uio *ktruio = NULL; 2526 #endif 2527 struct uio auio; 2528 struct iovec *iov, *tiov; 2529 cap_rights_t rights; 2530 2531 if (uap->sinfo) { 2532 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo)); 2533 if (error) 2534 return (error); 2535 u_sinfo = &sinfo; 2536 } 2537 rights = CAP_WRITE; 2538 if (uap->tolen) { 2539 error = getsockaddr(&to, uap->to, uap->tolen); 2540 if (error) { 2541 to = NULL; 2542 goto sctp_bad2; 2543 } 2544 rights |= CAP_CONNECT; 2545 } 2546 2547 AUDIT_ARG_FD(uap->sd); 2548 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL); 2549 if (error) 2550 goto sctp_bad1; 2551 2552 #ifdef COMPAT_FREEBSD32 2553 if (SV_CURPROC_FLAG(SV_ILP32)) 2554 error = freebsd32_copyiniov((struct iovec32 *)uap->iov, 2555 uap->iovlen, &iov, EMSGSIZE); 2556 else 2557 #endif 2558 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE); 2559 if (error) 2560 goto sctp_bad1; 2561 #ifdef KTRACE 2562 if (to && (KTRPOINT(td, KTR_STRUCT))) 2563 ktrsockaddr(to); 2564 #endif 2565 2566 so = (struct socket *)fp->f_data; 2567 if (so->so_proto->pr_protocol != IPPROTO_SCTP) { 2568 error = EOPNOTSUPP; 2569 goto sctp_bad; 2570 } 2571 #ifdef MAC 2572 error = mac_socket_check_send(td->td_ucred, so); 2573 if (error) 2574 goto sctp_bad; 2575 #endif /* MAC */ 2576 2577 auio.uio_iov = iov; 2578 auio.uio_iovcnt = uap->iovlen; 2579 auio.uio_segflg = UIO_USERSPACE; 2580 auio.uio_rw = UIO_WRITE; 2581 auio.uio_td = td; 2582 auio.uio_offset = 0; /* XXX */ 2583 auio.uio_resid = 0; 2584 tiov = iov; 2585 for (i = 0; i <uap->iovlen; i++, tiov++) { 2586 if ((auio.uio_resid += tiov->iov_len) < 0) { 2587 error = EINVAL; 2588 goto sctp_bad; 2589 } 2590 } 2591 len = auio.uio_resid; 2592 CURVNET_SET(so->so_vnet); 2593 error = sctp_lower_sosend(so, to, &auio, 2594 (struct mbuf *)NULL, (struct mbuf *)NULL, 2595 uap->flags, u_sinfo, td); 2596 CURVNET_RESTORE(); 2597 if (error) { 2598 if (auio.uio_resid != len && (error == ERESTART || 2599 error == EINTR || error == EWOULDBLOCK)) 2600 error = 0; 2601 /* Generation of SIGPIPE can be controlled per socket */ 2602 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) && 2603 !(uap->flags & MSG_NOSIGNAL)) { 2604 PROC_LOCK(td->td_proc); 2605 tdsignal(td, SIGPIPE); 2606 PROC_UNLOCK(td->td_proc); 2607 } 2608 } 2609 if (error == 0) 2610 td->td_retval[0] = len - auio.uio_resid; 2611 #ifdef KTRACE 2612 if (ktruio != NULL) { 2613 ktruio->uio_resid = td->td_retval[0]; 2614 ktrgenio(uap->sd, UIO_WRITE, ktruio, error); 2615 } 2616 #endif /* KTRACE */ 2617 sctp_bad: 2618 free(iov, M_IOV); 2619 sctp_bad1: 2620 if (fp) 2621 fdrop(fp, td); 2622 sctp_bad2: 2623 if (to) 2624 free(to, M_SONAME); 2625 return (error); 2626 #else /* SCTP */ 2627 return (EOPNOTSUPP); 2628 #endif /* SCTP */ 2629 } 2630 2631 int 2632 sys_sctp_generic_recvmsg(td, uap) 2633 struct thread *td; 2634 struct sctp_generic_recvmsg_args /* { 2635 int sd, 2636 struct iovec *iov, 2637 int iovlen, 2638 struct sockaddr *from, 2639 __socklen_t *fromlenaddr, 2640 struct sctp_sndrcvinfo *sinfo, 2641 int *msg_flags 2642 } */ *uap; 2643 { 2644 #if (defined(INET) || defined(INET6)) && defined(SCTP) 2645 uint8_t sockbufstore[256]; 2646 struct uio auio; 2647 struct iovec *iov, *tiov; 2648 struct sctp_sndrcvinfo sinfo; 2649 struct socket *so; 2650 struct file *fp = NULL; 2651 struct sockaddr *fromsa; 2652 int fromlen; 2653 ssize_t len; 2654 int i, msg_flags; 2655 int error = 0; 2656 #ifdef KTRACE 2657 struct uio *ktruio = NULL; 2658 #endif 2659 2660 AUDIT_ARG_FD(uap->sd); 2661 error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_READ, &fp, NULL); 2662 if (error) { 2663 return (error); 2664 } 2665 #ifdef COMPAT_FREEBSD32 2666 if (SV_CURPROC_FLAG(SV_ILP32)) 2667 error = freebsd32_copyiniov((struct iovec32 *)uap->iov, 2668 uap->iovlen, &iov, EMSGSIZE); 2669 else 2670 #endif 2671 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE); 2672 if (error) 2673 goto out1; 2674 2675 so = fp->f_data; 2676 if (so->so_proto->pr_protocol != IPPROTO_SCTP) { 2677 error = EOPNOTSUPP; 2678 goto out; 2679 } 2680 #ifdef MAC 2681 error = mac_socket_check_receive(td->td_ucred, so); 2682 if (error) { 2683 goto out; 2684 } 2685 #endif /* MAC */ 2686 2687 if (uap->fromlenaddr) { 2688 error = copyin(uap->fromlenaddr, 2689 &fromlen, sizeof (fromlen)); 2690 if (error) { 2691 goto out; 2692 } 2693 } else { 2694 fromlen = 0; 2695 } 2696 if (uap->msg_flags) { 2697 error = copyin(uap->msg_flags, &msg_flags, sizeof (int)); 2698 if (error) { 2699 goto out; 2700 } 2701 } else { 2702 msg_flags = 0; 2703 } 2704 auio.uio_iov = iov; 2705 auio.uio_iovcnt = uap->iovlen; 2706 auio.uio_segflg = UIO_USERSPACE; 2707 auio.uio_rw = UIO_READ; 2708 auio.uio_td = td; 2709 auio.uio_offset = 0; /* XXX */ 2710 auio.uio_resid = 0; 2711 tiov = iov; 2712 for (i = 0; i <uap->iovlen; i++, tiov++) { 2713 if ((auio.uio_resid += tiov->iov_len) < 0) { 2714 error = EINVAL; 2715 goto out; 2716 } 2717 } 2718 len = auio.uio_resid; 2719 fromsa = (struct sockaddr *)sockbufstore; 2720 2721 #ifdef KTRACE 2722 if (KTRPOINT(td, KTR_GENIO)) 2723 ktruio = cloneuio(&auio); 2724 #endif /* KTRACE */ 2725 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 2726 CURVNET_SET(so->so_vnet); 2727 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL, 2728 fromsa, fromlen, &msg_flags, 2729 (struct sctp_sndrcvinfo *)&sinfo, 1); 2730 CURVNET_RESTORE(); 2731 if (error) { 2732 if (auio.uio_resid != len && (error == ERESTART || 2733 error == EINTR || error == EWOULDBLOCK)) 2734 error = 0; 2735 } else { 2736 if (uap->sinfo) 2737 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo)); 2738 } 2739 #ifdef KTRACE 2740 if (ktruio != NULL) { 2741 ktruio->uio_resid = len - auio.uio_resid; 2742 ktrgenio(uap->sd, UIO_READ, ktruio, error); 2743 } 2744 #endif /* KTRACE */ 2745 if (error) 2746 goto out; 2747 td->td_retval[0] = len - auio.uio_resid; 2748 2749 if (fromlen && uap->from) { 2750 len = fromlen; 2751 if (len <= 0 || fromsa == 0) 2752 len = 0; 2753 else { 2754 len = MIN(len, fromsa->sa_len); 2755 error = copyout(fromsa, uap->from, (size_t)len); 2756 if (error) 2757 goto out; 2758 } 2759 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t)); 2760 if (error) { 2761 goto out; 2762 } 2763 } 2764 #ifdef KTRACE 2765 if (KTRPOINT(td, KTR_STRUCT)) 2766 ktrsockaddr(fromsa); 2767 #endif 2768 if (uap->msg_flags) { 2769 error = copyout(&msg_flags, uap->msg_flags, sizeof (int)); 2770 if (error) { 2771 goto out; 2772 } 2773 } 2774 out: 2775 free(iov, M_IOV); 2776 out1: 2777 if (fp) 2778 fdrop(fp, td); 2779 2780 return (error); 2781 #else /* SCTP */ 2782 return (EOPNOTSUPP); 2783 #endif /* SCTP */ 2784 } 2785