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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94 37 * $FreeBSD: src/sys/kern/uipc_syscalls.c,v 1.65.2.17 2003/04/04 17:11:16 tegge Exp $ 38 * $DragonFly: src/sys/kern/uipc_syscalls.c,v 1.92 2008/11/26 13:10:56 sephe Exp $ 39 */ 40 41 #include "opt_ktrace.h" 42 #include "opt_sctp.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/sysproto.h> 48 #include <sys/malloc.h> 49 #include <sys/filedesc.h> 50 #include <sys/event.h> 51 #include <sys/proc.h> 52 #include <sys/fcntl.h> 53 #include <sys/file.h> 54 #include <sys/filio.h> 55 #include <sys/kern_syscall.h> 56 #include <sys/mbuf.h> 57 #include <sys/protosw.h> 58 #include <sys/sfbuf.h> 59 #include <sys/socket.h> 60 #include <sys/socketvar.h> 61 #include <sys/socketops.h> 62 #include <sys/uio.h> 63 #include <sys/vnode.h> 64 #include <sys/lock.h> 65 #include <sys/mount.h> 66 #ifdef KTRACE 67 #include <sys/ktrace.h> 68 #endif 69 #include <vm/vm.h> 70 #include <vm/vm_object.h> 71 #include <vm/vm_page.h> 72 #include <vm/vm_pageout.h> 73 #include <vm/vm_kern.h> 74 #include <vm/vm_extern.h> 75 #include <sys/file2.h> 76 #include <sys/signalvar.h> 77 #include <sys/serialize.h> 78 79 #include <sys/thread2.h> 80 #include <sys/msgport2.h> 81 #include <sys/socketvar2.h> 82 #include <sys/mplock2.h> 83 #include <net/netmsg2.h> 84 85 #ifdef SCTP 86 #include <netinet/sctp_peeloff.h> 87 #endif /* SCTP */ 88 89 /* 90 * System call interface to the socket abstraction. 91 */ 92 93 extern struct fileops socketops; 94 95 /* 96 * socket_args(int domain, int type, int protocol) 97 */ 98 int 99 kern_socket(int domain, int type, int protocol, int *res) 100 { 101 struct thread *td = curthread; 102 struct filedesc *fdp = td->td_proc->p_fd; 103 struct socket *so; 104 struct file *fp; 105 int fd, error; 106 107 KKASSERT(td->td_lwp); 108 109 error = falloc(td->td_lwp, &fp, &fd); 110 if (error) 111 return (error); 112 error = socreate(domain, &so, type, protocol, td); 113 if (error) { 114 fsetfd(fdp, NULL, fd); 115 } else { 116 fp->f_type = DTYPE_SOCKET; 117 fp->f_flag = FREAD | FWRITE; 118 fp->f_ops = &socketops; 119 fp->f_data = so; 120 *res = fd; 121 fsetfd(fdp, fp, fd); 122 } 123 fdrop(fp); 124 return (error); 125 } 126 127 /* 128 * MPALMOSTSAFE 129 */ 130 int 131 sys_socket(struct socket_args *uap) 132 { 133 int error; 134 135 get_mplock(); 136 error = kern_socket(uap->domain, uap->type, uap->protocol, 137 &uap->sysmsg_iresult); 138 rel_mplock(); 139 140 return (error); 141 } 142 143 int 144 kern_bind(int s, struct sockaddr *sa) 145 { 146 struct thread *td = curthread; 147 struct proc *p = td->td_proc; 148 struct file *fp; 149 int error; 150 151 KKASSERT(p); 152 error = holdsock(p->p_fd, s, &fp); 153 if (error) 154 return (error); 155 error = sobind((struct socket *)fp->f_data, sa, td); 156 fdrop(fp); 157 return (error); 158 } 159 160 /* 161 * bind_args(int s, caddr_t name, int namelen) 162 * 163 * MPALMOSTSAFE 164 */ 165 int 166 sys_bind(struct bind_args *uap) 167 { 168 struct sockaddr *sa; 169 int error; 170 171 error = getsockaddr(&sa, uap->name, uap->namelen); 172 if (error) 173 return (error); 174 get_mplock(); 175 error = kern_bind(uap->s, sa); 176 rel_mplock(); 177 FREE(sa, M_SONAME); 178 179 return (error); 180 } 181 182 int 183 kern_listen(int s, int backlog) 184 { 185 struct thread *td = curthread; 186 struct proc *p = td->td_proc; 187 struct file *fp; 188 int error; 189 190 KKASSERT(p); 191 error = holdsock(p->p_fd, s, &fp); 192 if (error) 193 return (error); 194 error = solisten((struct socket *)fp->f_data, backlog, td); 195 fdrop(fp); 196 return(error); 197 } 198 199 /* 200 * listen_args(int s, int backlog) 201 * 202 * MPALMOSTSAFE 203 */ 204 int 205 sys_listen(struct listen_args *uap) 206 { 207 int error; 208 209 get_mplock(); 210 error = kern_listen(uap->s, uap->backlog); 211 rel_mplock(); 212 return (error); 213 } 214 215 /* 216 * Returns the accepted socket as well. 217 */ 218 static boolean_t 219 soaccept_predicate(struct netmsg *msg0) 220 { 221 struct netmsg_so_notify *msg = (struct netmsg_so_notify *)msg0; 222 struct socket *head = msg->nm_so; 223 224 if (head->so_error != 0) { 225 msg->nm_netmsg.nm_lmsg.ms_error = head->so_error; 226 return (TRUE); 227 } 228 lwkt_gettoken(&head->so_rcv.ssb_token); 229 if (!TAILQ_EMPTY(&head->so_comp)) { 230 /* Abuse nm_so field as copy in/copy out parameter. XXX JH */ 231 msg->nm_so = TAILQ_FIRST(&head->so_comp); 232 TAILQ_REMOVE(&head->so_comp, msg->nm_so, so_list); 233 head->so_qlen--; 234 235 msg->nm_netmsg.nm_lmsg.ms_error = 0; 236 lwkt_reltoken(&head->so_rcv.ssb_token); 237 return (TRUE); 238 } 239 lwkt_reltoken(&head->so_rcv.ssb_token); 240 if (head->so_state & SS_CANTRCVMORE) { 241 msg->nm_netmsg.nm_lmsg.ms_error = ECONNABORTED; 242 return (TRUE); 243 } 244 if (msg->nm_fflags & FNONBLOCK) { 245 msg->nm_netmsg.nm_lmsg.ms_error = EWOULDBLOCK; 246 return (TRUE); 247 } 248 249 return (FALSE); 250 } 251 252 /* 253 * The second argument to kern_accept() is a handle to a struct sockaddr. 254 * This allows kern_accept() to return a pointer to an allocated struct 255 * sockaddr which must be freed later with FREE(). The caller must 256 * initialize *name to NULL. 257 */ 258 int 259 kern_accept(int s, int fflags, struct sockaddr **name, int *namelen, int *res) 260 { 261 struct thread *td = curthread; 262 struct filedesc *fdp = td->td_proc->p_fd; 263 struct file *lfp = NULL; 264 struct file *nfp = NULL; 265 struct sockaddr *sa; 266 struct socket *head, *so; 267 struct netmsg_so_notify msg; 268 int fd; 269 u_int fflag; /* type must match fp->f_flag */ 270 int error, tmp; 271 272 *res = -1; 273 if (name && namelen && *namelen < 0) 274 return (EINVAL); 275 276 error = holdsock(td->td_proc->p_fd, s, &lfp); 277 if (error) 278 return (error); 279 280 error = falloc(td->td_lwp, &nfp, &fd); 281 if (error) { /* Probably ran out of file descriptors. */ 282 fdrop(lfp); 283 return (error); 284 } 285 head = (struct socket *)lfp->f_data; 286 if ((head->so_options & SO_ACCEPTCONN) == 0) { 287 error = EINVAL; 288 goto done; 289 } 290 291 if (fflags & O_FBLOCKING) 292 fflags |= lfp->f_flag & ~FNONBLOCK; 293 else if (fflags & O_FNONBLOCKING) 294 fflags |= lfp->f_flag | FNONBLOCK; 295 else 296 fflags = lfp->f_flag; 297 298 /* optimize for uniprocessor case later XXX JH */ 299 netmsg_init_abortable(&msg.nm_netmsg, head, &curthread->td_msgport, 300 0, netmsg_so_notify, netmsg_so_notify_doabort); 301 msg.nm_predicate = soaccept_predicate; 302 msg.nm_fflags = fflags; 303 msg.nm_so = head; 304 msg.nm_etype = NM_REVENT; 305 error = lwkt_domsg(head->so_port, &msg.nm_netmsg.nm_lmsg, PCATCH); 306 if (error) 307 goto done; 308 309 /* 310 * At this point we have the connection that's ready to be accepted. 311 */ 312 so = msg.nm_so; 313 314 fflag = lfp->f_flag; 315 316 /* connection has been removed from the listen queue */ 317 KNOTE(&head->so_rcv.ssb_kq.ki_note, 0); 318 319 soclrstate(so, SS_COMP); 320 so->so_head = NULL; 321 if (head->so_sigio != NULL) 322 fsetown(fgetown(head->so_sigio), &so->so_sigio); 323 324 nfp->f_type = DTYPE_SOCKET; 325 nfp->f_flag = fflag; 326 nfp->f_ops = &socketops; 327 nfp->f_data = so; 328 /* Sync socket nonblocking/async state with file flags */ 329 tmp = fflag & FNONBLOCK; 330 fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td->td_ucred, NULL); 331 tmp = fflag & FASYNC; 332 fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td->td_ucred, NULL); 333 334 sa = NULL; 335 error = soaccept(so, &sa); 336 337 /* 338 * Set the returned name and namelen as applicable. Set the returned 339 * namelen to 0 for older code which might ignore the return value 340 * from accept. 341 */ 342 if (error == 0) { 343 if (sa && name && namelen) { 344 if (*namelen > sa->sa_len) 345 *namelen = sa->sa_len; 346 *name = sa; 347 } else { 348 if (sa) 349 FREE(sa, M_SONAME); 350 } 351 } 352 353 done: 354 /* 355 * If an error occured clear the reserved descriptor, else associate 356 * nfp with it. 357 * 358 * Note that *res is normally ignored if an error is returned but 359 * a syscall message will still have access to the result code. 360 */ 361 if (error) { 362 fsetfd(fdp, NULL, fd); 363 } else { 364 *res = fd; 365 fsetfd(fdp, nfp, fd); 366 } 367 fdrop(nfp); 368 fdrop(lfp); 369 return (error); 370 } 371 372 /* 373 * accept(int s, caddr_t name, int *anamelen) 374 * 375 * MPALMOSTSAFE 376 */ 377 int 378 sys_accept(struct accept_args *uap) 379 { 380 struct sockaddr *sa = NULL; 381 int sa_len; 382 int error; 383 384 if (uap->name) { 385 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len)); 386 if (error) 387 return (error); 388 389 get_mplock(); 390 error = kern_accept(uap->s, 0, &sa, &sa_len, 391 &uap->sysmsg_iresult); 392 rel_mplock(); 393 394 if (error == 0) 395 error = copyout(sa, uap->name, sa_len); 396 if (error == 0) { 397 error = copyout(&sa_len, uap->anamelen, 398 sizeof(*uap->anamelen)); 399 } 400 if (sa) 401 FREE(sa, M_SONAME); 402 } else { 403 get_mplock(); 404 error = kern_accept(uap->s, 0, NULL, 0, 405 &uap->sysmsg_iresult); 406 rel_mplock(); 407 } 408 return (error); 409 } 410 411 /* 412 * extaccept(int s, int fflags, caddr_t name, int *anamelen) 413 * 414 * MPALMOSTSAFE 415 */ 416 int 417 sys_extaccept(struct extaccept_args *uap) 418 { 419 struct sockaddr *sa = NULL; 420 int sa_len; 421 int error; 422 int fflags = uap->flags & O_FMASK; 423 424 if (uap->name) { 425 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len)); 426 if (error) 427 return (error); 428 429 get_mplock(); 430 error = kern_accept(uap->s, fflags, &sa, &sa_len, 431 &uap->sysmsg_iresult); 432 rel_mplock(); 433 434 if (error == 0) 435 error = copyout(sa, uap->name, sa_len); 436 if (error == 0) { 437 error = copyout(&sa_len, uap->anamelen, 438 sizeof(*uap->anamelen)); 439 } 440 if (sa) 441 FREE(sa, M_SONAME); 442 } else { 443 get_mplock(); 444 error = kern_accept(uap->s, fflags, NULL, 0, 445 &uap->sysmsg_iresult); 446 rel_mplock(); 447 } 448 return (error); 449 } 450 451 452 /* 453 * Returns TRUE if predicate satisfied. 454 */ 455 static boolean_t 456 soconnected_predicate(struct netmsg *msg0) 457 { 458 struct netmsg_so_notify *msg = (struct netmsg_so_notify *)msg0; 459 struct socket *so = msg->nm_so; 460 461 /* check predicate */ 462 if (!(so->so_state & SS_ISCONNECTING) || so->so_error != 0) { 463 msg->nm_netmsg.nm_lmsg.ms_error = so->so_error; 464 return (TRUE); 465 } 466 467 return (FALSE); 468 } 469 470 int 471 kern_connect(int s, int fflags, struct sockaddr *sa) 472 { 473 struct thread *td = curthread; 474 struct proc *p = td->td_proc; 475 struct file *fp; 476 struct socket *so; 477 int error, interrupted = 0; 478 479 error = holdsock(p->p_fd, s, &fp); 480 if (error) 481 return (error); 482 so = (struct socket *)fp->f_data; 483 484 if (fflags & O_FBLOCKING) 485 /* fflags &= ~FNONBLOCK; */; 486 else if (fflags & O_FNONBLOCKING) 487 fflags |= FNONBLOCK; 488 else 489 fflags = fp->f_flag; 490 491 if (so->so_state & SS_ISCONNECTING) { 492 error = EALREADY; 493 goto done; 494 } 495 error = soconnect(so, sa, td); 496 if (error) 497 goto bad; 498 if ((fflags & FNONBLOCK) && (so->so_state & SS_ISCONNECTING)) { 499 error = EINPROGRESS; 500 goto done; 501 } 502 if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 503 struct netmsg_so_notify msg; 504 505 netmsg_init_abortable(&msg.nm_netmsg, so, 506 &curthread->td_msgport, 507 0, 508 netmsg_so_notify, 509 netmsg_so_notify_doabort); 510 msg.nm_predicate = soconnected_predicate; 511 msg.nm_so = so; 512 msg.nm_etype = NM_REVENT; 513 error = lwkt_domsg(so->so_port, &msg.nm_netmsg.nm_lmsg, PCATCH); 514 if (error == EINTR || error == ERESTART) 515 interrupted = 1; 516 } 517 if (error == 0) { 518 error = so->so_error; 519 so->so_error = 0; 520 } 521 bad: 522 if (!interrupted) 523 soclrstate(so, SS_ISCONNECTING); 524 if (error == ERESTART) 525 error = EINTR; 526 done: 527 fdrop(fp); 528 return (error); 529 } 530 531 /* 532 * connect_args(int s, caddr_t name, int namelen) 533 * 534 * MPALMOSTSAFE 535 */ 536 int 537 sys_connect(struct connect_args *uap) 538 { 539 struct sockaddr *sa; 540 int error; 541 542 error = getsockaddr(&sa, uap->name, uap->namelen); 543 if (error) 544 return (error); 545 get_mplock(); 546 error = kern_connect(uap->s, 0, sa); 547 rel_mplock(); 548 FREE(sa, M_SONAME); 549 550 return (error); 551 } 552 553 /* 554 * connect_args(int s, int fflags, caddr_t name, int namelen) 555 * 556 * MPALMOSTSAFE 557 */ 558 int 559 sys_extconnect(struct extconnect_args *uap) 560 { 561 struct sockaddr *sa; 562 int error; 563 int fflags = uap->flags & O_FMASK; 564 565 error = getsockaddr(&sa, uap->name, uap->namelen); 566 if (error) 567 return (error); 568 get_mplock(); 569 error = kern_connect(uap->s, fflags, sa); 570 rel_mplock(); 571 FREE(sa, M_SONAME); 572 573 return (error); 574 } 575 576 int 577 kern_socketpair(int domain, int type, int protocol, int *sv) 578 { 579 struct thread *td = curthread; 580 struct filedesc *fdp; 581 struct file *fp1, *fp2; 582 struct socket *so1, *so2; 583 int fd1, fd2, error; 584 585 fdp = td->td_proc->p_fd; 586 error = socreate(domain, &so1, type, protocol, td); 587 if (error) 588 return (error); 589 error = socreate(domain, &so2, type, protocol, td); 590 if (error) 591 goto free1; 592 error = falloc(td->td_lwp, &fp1, &fd1); 593 if (error) 594 goto free2; 595 sv[0] = fd1; 596 fp1->f_data = so1; 597 error = falloc(td->td_lwp, &fp2, &fd2); 598 if (error) 599 goto free3; 600 fp2->f_data = so2; 601 sv[1] = fd2; 602 error = soconnect2(so1, so2); 603 if (error) 604 goto free4; 605 if (type == SOCK_DGRAM) { 606 /* 607 * Datagram socket connection is asymmetric. 608 */ 609 error = soconnect2(so2, so1); 610 if (error) 611 goto free4; 612 } 613 fp1->f_type = fp2->f_type = DTYPE_SOCKET; 614 fp1->f_flag = fp2->f_flag = FREAD|FWRITE; 615 fp1->f_ops = fp2->f_ops = &socketops; 616 fsetfd(fdp, fp1, fd1); 617 fsetfd(fdp, fp2, fd2); 618 fdrop(fp1); 619 fdrop(fp2); 620 return (error); 621 free4: 622 fsetfd(fdp, NULL, fd2); 623 fdrop(fp2); 624 free3: 625 fsetfd(fdp, NULL, fd1); 626 fdrop(fp1); 627 free2: 628 (void)soclose(so2, 0); 629 free1: 630 (void)soclose(so1, 0); 631 return (error); 632 } 633 634 /* 635 * socketpair(int domain, int type, int protocol, int *rsv) 636 * 637 * MPALMOSTSAFE 638 */ 639 int 640 sys_socketpair(struct socketpair_args *uap) 641 { 642 int error, sockv[2]; 643 644 get_mplock(); 645 error = kern_socketpair(uap->domain, uap->type, uap->protocol, sockv); 646 rel_mplock(); 647 648 if (error == 0) 649 error = copyout(sockv, uap->rsv, sizeof(sockv)); 650 return (error); 651 } 652 653 int 654 kern_sendmsg(int s, struct sockaddr *sa, struct uio *auio, 655 struct mbuf *control, int flags, size_t *res) 656 { 657 struct thread *td = curthread; 658 struct lwp *lp = td->td_lwp; 659 struct proc *p = td->td_proc; 660 struct file *fp; 661 size_t len; 662 int error; 663 struct socket *so; 664 #ifdef KTRACE 665 struct iovec *ktriov = NULL; 666 struct uio ktruio; 667 #endif 668 669 error = holdsock(p->p_fd, s, &fp); 670 if (error) 671 return (error); 672 #ifdef KTRACE 673 if (KTRPOINT(td, KTR_GENIO)) { 674 int iovlen = auio->uio_iovcnt * sizeof (struct iovec); 675 676 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 677 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen); 678 ktruio = *auio; 679 } 680 #endif 681 len = auio->uio_resid; 682 so = (struct socket *)fp->f_data; 683 if ((flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) { 684 if (fp->f_flag & FNONBLOCK) 685 flags |= MSG_FNONBLOCKING; 686 } 687 error = so_pru_sosend(so, sa, auio, NULL, control, flags, td); 688 if (error) { 689 if (auio->uio_resid != len && (error == ERESTART || 690 error == EINTR || error == EWOULDBLOCK)) 691 error = 0; 692 if (error == EPIPE) 693 lwpsignal(p, lp, SIGPIPE); 694 } 695 #ifdef KTRACE 696 if (ktriov != NULL) { 697 if (error == 0) { 698 ktruio.uio_iov = ktriov; 699 ktruio.uio_resid = len - auio->uio_resid; 700 ktrgenio(lp, s, UIO_WRITE, &ktruio, error); 701 } 702 FREE(ktriov, M_TEMP); 703 } 704 #endif 705 if (error == 0) 706 *res = len - auio->uio_resid; 707 fdrop(fp); 708 return (error); 709 } 710 711 /* 712 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen) 713 * 714 * MPALMOSTSAFE 715 */ 716 int 717 sys_sendto(struct sendto_args *uap) 718 { 719 struct thread *td = curthread; 720 struct uio auio; 721 struct iovec aiov; 722 struct sockaddr *sa = NULL; 723 int error; 724 725 if (uap->to) { 726 error = getsockaddr(&sa, uap->to, uap->tolen); 727 if (error) 728 return (error); 729 } 730 aiov.iov_base = uap->buf; 731 aiov.iov_len = uap->len; 732 auio.uio_iov = &aiov; 733 auio.uio_iovcnt = 1; 734 auio.uio_offset = 0; 735 auio.uio_resid = uap->len; 736 auio.uio_segflg = UIO_USERSPACE; 737 auio.uio_rw = UIO_WRITE; 738 auio.uio_td = td; 739 740 get_mplock(); 741 error = kern_sendmsg(uap->s, sa, &auio, NULL, uap->flags, 742 &uap->sysmsg_szresult); 743 rel_mplock(); 744 745 if (sa) 746 FREE(sa, M_SONAME); 747 return (error); 748 } 749 750 /* 751 * sendmsg_args(int s, caddr_t msg, int flags) 752 * 753 * MPALMOSTSAFE 754 */ 755 int 756 sys_sendmsg(struct sendmsg_args *uap) 757 { 758 struct thread *td = curthread; 759 struct msghdr msg; 760 struct uio auio; 761 struct iovec aiov[UIO_SMALLIOV], *iov = NULL; 762 struct sockaddr *sa = NULL; 763 struct mbuf *control = NULL; 764 int error; 765 766 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg)); 767 if (error) 768 return (error); 769 770 /* 771 * Conditionally copyin msg.msg_name. 772 */ 773 if (msg.msg_name) { 774 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen); 775 if (error) 776 return (error); 777 } 778 779 /* 780 * Populate auio. 781 */ 782 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen, 783 &auio.uio_resid); 784 if (error) 785 goto cleanup2; 786 auio.uio_iov = iov; 787 auio.uio_iovcnt = msg.msg_iovlen; 788 auio.uio_offset = 0; 789 auio.uio_segflg = UIO_USERSPACE; 790 auio.uio_rw = UIO_WRITE; 791 auio.uio_td = td; 792 793 /* 794 * Conditionally copyin msg.msg_control. 795 */ 796 if (msg.msg_control) { 797 if (msg.msg_controllen < sizeof(struct cmsghdr) || 798 msg.msg_controllen > MLEN) { 799 error = EINVAL; 800 goto cleanup; 801 } 802 control = m_get(MB_WAIT, MT_CONTROL); 803 if (control == NULL) { 804 error = ENOBUFS; 805 goto cleanup; 806 } 807 control->m_len = msg.msg_controllen; 808 error = copyin(msg.msg_control, mtod(control, caddr_t), 809 msg.msg_controllen); 810 if (error) { 811 m_free(control); 812 goto cleanup; 813 } 814 } 815 816 get_mplock(); 817 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags, 818 &uap->sysmsg_szresult); 819 rel_mplock(); 820 821 cleanup: 822 iovec_free(&iov, aiov); 823 cleanup2: 824 if (sa) 825 FREE(sa, M_SONAME); 826 return (error); 827 } 828 829 /* 830 * kern_recvmsg() takes a handle to sa and control. If the handle is non- 831 * null, it returns a dynamically allocated struct sockaddr and an mbuf. 832 * Don't forget to FREE() and m_free() these if they are returned. 833 */ 834 int 835 kern_recvmsg(int s, struct sockaddr **sa, struct uio *auio, 836 struct mbuf **control, int *flags, size_t *res) 837 { 838 struct thread *td = curthread; 839 struct proc *p = td->td_proc; 840 struct file *fp; 841 size_t len; 842 int error; 843 int lflags; 844 struct socket *so; 845 #ifdef KTRACE 846 struct iovec *ktriov = NULL; 847 struct uio ktruio; 848 #endif 849 850 error = holdsock(p->p_fd, s, &fp); 851 if (error) 852 return (error); 853 #ifdef KTRACE 854 if (KTRPOINT(td, KTR_GENIO)) { 855 int iovlen = auio->uio_iovcnt * sizeof (struct iovec); 856 857 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 858 bcopy(auio->uio_iov, ktriov, iovlen); 859 ktruio = *auio; 860 } 861 #endif 862 len = auio->uio_resid; 863 so = (struct socket *)fp->f_data; 864 865 if (flags == NULL || (*flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) { 866 if (fp->f_flag & FNONBLOCK) { 867 if (flags) { 868 *flags |= MSG_FNONBLOCKING; 869 } else { 870 lflags = MSG_FNONBLOCKING; 871 flags = &lflags; 872 } 873 } 874 } 875 876 error = so_pru_soreceive(so, sa, auio, NULL, control, flags); 877 if (error) { 878 if (auio->uio_resid != len && (error == ERESTART || 879 error == EINTR || error == EWOULDBLOCK)) 880 error = 0; 881 } 882 #ifdef KTRACE 883 if (ktriov != NULL) { 884 if (error == 0) { 885 ktruio.uio_iov = ktriov; 886 ktruio.uio_resid = len - auio->uio_resid; 887 ktrgenio(td->td_lwp, s, UIO_READ, &ktruio, error); 888 } 889 FREE(ktriov, M_TEMP); 890 } 891 #endif 892 if (error == 0) 893 *res = len - auio->uio_resid; 894 fdrop(fp); 895 return (error); 896 } 897 898 /* 899 * recvfrom_args(int s, caddr_t buf, size_t len, int flags, 900 * caddr_t from, int *fromlenaddr) 901 * 902 * MPALMOSTSAFE 903 */ 904 int 905 sys_recvfrom(struct recvfrom_args *uap) 906 { 907 struct thread *td = curthread; 908 struct uio auio; 909 struct iovec aiov; 910 struct sockaddr *sa = NULL; 911 int error, fromlen; 912 913 if (uap->from && uap->fromlenaddr) { 914 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen)); 915 if (error) 916 return (error); 917 if (fromlen < 0) 918 return (EINVAL); 919 } else { 920 fromlen = 0; 921 } 922 aiov.iov_base = uap->buf; 923 aiov.iov_len = uap->len; 924 auio.uio_iov = &aiov; 925 auio.uio_iovcnt = 1; 926 auio.uio_offset = 0; 927 auio.uio_resid = uap->len; 928 auio.uio_segflg = UIO_USERSPACE; 929 auio.uio_rw = UIO_READ; 930 auio.uio_td = td; 931 932 get_mplock(); 933 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL, 934 &uap->flags, &uap->sysmsg_szresult); 935 rel_mplock(); 936 937 if (error == 0 && uap->from) { 938 /* note: sa may still be NULL */ 939 if (sa) { 940 fromlen = MIN(fromlen, sa->sa_len); 941 error = copyout(sa, uap->from, fromlen); 942 } else { 943 fromlen = 0; 944 } 945 if (error == 0) { 946 error = copyout(&fromlen, uap->fromlenaddr, 947 sizeof(fromlen)); 948 } 949 } 950 if (sa) 951 FREE(sa, M_SONAME); 952 953 return (error); 954 } 955 956 /* 957 * recvmsg_args(int s, struct msghdr *msg, int flags) 958 * 959 * MPALMOSTSAFE 960 */ 961 int 962 sys_recvmsg(struct recvmsg_args *uap) 963 { 964 struct thread *td = curthread; 965 struct msghdr msg; 966 struct uio auio; 967 struct iovec aiov[UIO_SMALLIOV], *iov = NULL; 968 struct mbuf *m, *control = NULL; 969 struct sockaddr *sa = NULL; 970 caddr_t ctlbuf; 971 socklen_t *ufromlenp, *ucontrollenp; 972 int error, fromlen, controllen, len, flags, *uflagsp; 973 974 /* 975 * This copyin handles everything except the iovec. 976 */ 977 error = copyin(uap->msg, &msg, sizeof(msg)); 978 if (error) 979 return (error); 980 981 if (msg.msg_name && msg.msg_namelen < 0) 982 return (EINVAL); 983 if (msg.msg_control && msg.msg_controllen < 0) 984 return (EINVAL); 985 986 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr, 987 msg_namelen)); 988 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr, 989 msg_controllen)); 990 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr, 991 msg_flags)); 992 993 /* 994 * Populate auio. 995 */ 996 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen, 997 &auio.uio_resid); 998 if (error) 999 return (error); 1000 auio.uio_iov = iov; 1001 auio.uio_iovcnt = msg.msg_iovlen; 1002 auio.uio_offset = 0; 1003 auio.uio_segflg = UIO_USERSPACE; 1004 auio.uio_rw = UIO_READ; 1005 auio.uio_td = td; 1006 1007 flags = uap->flags; 1008 1009 get_mplock(); 1010 error = kern_recvmsg(uap->s, 1011 (msg.msg_name ? &sa : NULL), &auio, 1012 (msg.msg_control ? &control : NULL), &flags, 1013 &uap->sysmsg_szresult); 1014 rel_mplock(); 1015 1016 /* 1017 * Conditionally copyout the name and populate the namelen field. 1018 */ 1019 if (error == 0 && msg.msg_name) { 1020 /* note: sa may still be NULL */ 1021 if (sa != NULL) { 1022 fromlen = MIN(msg.msg_namelen, sa->sa_len); 1023 error = copyout(sa, msg.msg_name, fromlen); 1024 } else { 1025 fromlen = 0; 1026 } 1027 if (error == 0) 1028 error = copyout(&fromlen, ufromlenp, 1029 sizeof(*ufromlenp)); 1030 } 1031 1032 /* 1033 * Copyout msg.msg_control and msg.msg_controllen. 1034 */ 1035 if (error == 0 && msg.msg_control) { 1036 len = msg.msg_controllen; 1037 m = control; 1038 ctlbuf = (caddr_t)msg.msg_control; 1039 1040 while(m && len > 0) { 1041 unsigned int tocopy; 1042 1043 if (len >= m->m_len) { 1044 tocopy = m->m_len; 1045 } else { 1046 msg.msg_flags |= MSG_CTRUNC; 1047 tocopy = len; 1048 } 1049 1050 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy); 1051 if (error) 1052 goto cleanup; 1053 1054 ctlbuf += tocopy; 1055 len -= tocopy; 1056 m = m->m_next; 1057 } 1058 controllen = ctlbuf - (caddr_t)msg.msg_control; 1059 error = copyout(&controllen, ucontrollenp, 1060 sizeof(*ucontrollenp)); 1061 } 1062 1063 if (error == 0) 1064 error = copyout(&flags, uflagsp, sizeof(*uflagsp)); 1065 1066 cleanup: 1067 if (sa) 1068 FREE(sa, M_SONAME); 1069 iovec_free(&iov, aiov); 1070 if (control) 1071 m_freem(control); 1072 return (error); 1073 } 1074 1075 /* 1076 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an 1077 * in kernel pointer instead of a userland pointer. This allows us 1078 * to manipulate socket options in the emulation code. 1079 */ 1080 int 1081 kern_setsockopt(int s, struct sockopt *sopt) 1082 { 1083 struct thread *td = curthread; 1084 struct proc *p = td->td_proc; 1085 struct file *fp; 1086 int error; 1087 1088 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0) 1089 return (EFAULT); 1090 if (sopt->sopt_val != NULL && sopt->sopt_valsize == 0) 1091 return (EINVAL); 1092 if (sopt->sopt_valsize < 0) 1093 return (EINVAL); 1094 1095 error = holdsock(p->p_fd, s, &fp); 1096 if (error) 1097 return (error); 1098 1099 error = sosetopt((struct socket *)fp->f_data, sopt); 1100 fdrop(fp); 1101 return (error); 1102 } 1103 1104 /* 1105 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize) 1106 * 1107 * MPALMOSTSAFE 1108 */ 1109 int 1110 sys_setsockopt(struct setsockopt_args *uap) 1111 { 1112 struct thread *td = curthread; 1113 struct sockopt sopt; 1114 int error; 1115 1116 sopt.sopt_level = uap->level; 1117 sopt.sopt_name = uap->name; 1118 sopt.sopt_valsize = uap->valsize; 1119 sopt.sopt_td = td; 1120 sopt.sopt_val = NULL; 1121 1122 if (sopt.sopt_valsize < 0 || sopt.sopt_valsize > SOMAXOPT_SIZE) 1123 return (EINVAL); 1124 if (uap->val) { 1125 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK); 1126 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize); 1127 if (error) 1128 goto out; 1129 } 1130 1131 get_mplock(); 1132 error = kern_setsockopt(uap->s, &sopt); 1133 rel_mplock(); 1134 out: 1135 if (uap->val) 1136 kfree(sopt.sopt_val, M_TEMP); 1137 return(error); 1138 } 1139 1140 /* 1141 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an 1142 * in kernel pointer instead of a userland pointer. This allows us 1143 * to manipulate socket options in the emulation code. 1144 */ 1145 int 1146 kern_getsockopt(int s, struct sockopt *sopt) 1147 { 1148 struct thread *td = curthread; 1149 struct proc *p = td->td_proc; 1150 struct file *fp; 1151 int error; 1152 1153 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0) 1154 return (EFAULT); 1155 if (sopt->sopt_val != NULL && sopt->sopt_valsize == 0) 1156 return (EINVAL); 1157 if (sopt->sopt_valsize < 0 || sopt->sopt_valsize > SOMAXOPT_SIZE) 1158 return (EINVAL); 1159 1160 error = holdsock(p->p_fd, s, &fp); 1161 if (error) 1162 return (error); 1163 1164 error = sogetopt((struct socket *)fp->f_data, sopt); 1165 fdrop(fp); 1166 return (error); 1167 } 1168 1169 /* 1170 * getsockopt_args(int s, int level, int name, caddr_t val, int *avalsize) 1171 * 1172 * MPALMOSTSAFE 1173 */ 1174 int 1175 sys_getsockopt(struct getsockopt_args *uap) 1176 { 1177 struct thread *td = curthread; 1178 struct sockopt sopt; 1179 int error, valsize; 1180 1181 if (uap->val) { 1182 error = copyin(uap->avalsize, &valsize, sizeof(valsize)); 1183 if (error) 1184 return (error); 1185 } else { 1186 valsize = 0; 1187 } 1188 1189 sopt.sopt_level = uap->level; 1190 sopt.sopt_name = uap->name; 1191 sopt.sopt_valsize = valsize; 1192 sopt.sopt_td = td; 1193 sopt.sopt_val = NULL; 1194 1195 if (sopt.sopt_valsize < 0 || sopt.sopt_valsize > SOMAXOPT_SIZE) 1196 return (EINVAL); 1197 if (uap->val) { 1198 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK); 1199 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize); 1200 if (error) 1201 goto out; 1202 } 1203 1204 get_mplock(); 1205 error = kern_getsockopt(uap->s, &sopt); 1206 rel_mplock(); 1207 if (error) 1208 goto out; 1209 valsize = sopt.sopt_valsize; 1210 error = copyout(&valsize, uap->avalsize, sizeof(valsize)); 1211 if (error) 1212 goto out; 1213 if (uap->val) 1214 error = copyout(sopt.sopt_val, uap->val, sopt.sopt_valsize); 1215 out: 1216 if (uap->val) 1217 kfree(sopt.sopt_val, M_TEMP); 1218 return (error); 1219 } 1220 1221 /* 1222 * The second argument to kern_getsockname() is a handle to a struct sockaddr. 1223 * This allows kern_getsockname() to return a pointer to an allocated struct 1224 * sockaddr which must be freed later with FREE(). The caller must 1225 * initialize *name to NULL. 1226 */ 1227 int 1228 kern_getsockname(int s, struct sockaddr **name, int *namelen) 1229 { 1230 struct thread *td = curthread; 1231 struct proc *p = td->td_proc; 1232 struct file *fp; 1233 struct socket *so; 1234 struct sockaddr *sa = NULL; 1235 int error; 1236 1237 error = holdsock(p->p_fd, s, &fp); 1238 if (error) 1239 return (error); 1240 if (*namelen < 0) { 1241 fdrop(fp); 1242 return (EINVAL); 1243 } 1244 so = (struct socket *)fp->f_data; 1245 error = so_pru_sockaddr(so, &sa); 1246 if (error == 0) { 1247 if (sa == NULL) { 1248 *namelen = 0; 1249 } else { 1250 *namelen = MIN(*namelen, sa->sa_len); 1251 *name = sa; 1252 } 1253 } 1254 1255 fdrop(fp); 1256 return (error); 1257 } 1258 1259 /* 1260 * getsockname_args(int fdes, caddr_t asa, int *alen) 1261 * 1262 * Get socket name. 1263 * 1264 * MPALMOSTSAFE 1265 */ 1266 int 1267 sys_getsockname(struct getsockname_args *uap) 1268 { 1269 struct sockaddr *sa = NULL; 1270 int error, sa_len; 1271 1272 error = copyin(uap->alen, &sa_len, sizeof(sa_len)); 1273 if (error) 1274 return (error); 1275 1276 get_mplock(); 1277 error = kern_getsockname(uap->fdes, &sa, &sa_len); 1278 rel_mplock(); 1279 1280 if (error == 0) 1281 error = copyout(sa, uap->asa, sa_len); 1282 if (error == 0) 1283 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen)); 1284 if (sa) 1285 FREE(sa, M_SONAME); 1286 return (error); 1287 } 1288 1289 /* 1290 * The second argument to kern_getpeername() is a handle to a struct sockaddr. 1291 * This allows kern_getpeername() to return a pointer to an allocated struct 1292 * sockaddr which must be freed later with FREE(). The caller must 1293 * initialize *name to NULL. 1294 */ 1295 int 1296 kern_getpeername(int s, struct sockaddr **name, int *namelen) 1297 { 1298 struct thread *td = curthread; 1299 struct proc *p = td->td_proc; 1300 struct file *fp; 1301 struct socket *so; 1302 struct sockaddr *sa = NULL; 1303 int error; 1304 1305 error = holdsock(p->p_fd, s, &fp); 1306 if (error) 1307 return (error); 1308 if (*namelen < 0) { 1309 fdrop(fp); 1310 return (EINVAL); 1311 } 1312 so = (struct socket *)fp->f_data; 1313 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) { 1314 fdrop(fp); 1315 return (ENOTCONN); 1316 } 1317 error = so_pru_peeraddr(so, &sa); 1318 if (error == 0) { 1319 if (sa == NULL) { 1320 *namelen = 0; 1321 } else { 1322 *namelen = MIN(*namelen, sa->sa_len); 1323 *name = sa; 1324 } 1325 } 1326 1327 fdrop(fp); 1328 return (error); 1329 } 1330 1331 /* 1332 * getpeername_args(int fdes, caddr_t asa, int *alen) 1333 * 1334 * Get name of peer for connected socket. 1335 * 1336 * MPALMOSTSAFE 1337 */ 1338 int 1339 sys_getpeername(struct getpeername_args *uap) 1340 { 1341 struct sockaddr *sa = NULL; 1342 int error, sa_len; 1343 1344 error = copyin(uap->alen, &sa_len, sizeof(sa_len)); 1345 if (error) 1346 return (error); 1347 1348 get_mplock(); 1349 error = kern_getpeername(uap->fdes, &sa, &sa_len); 1350 rel_mplock(); 1351 1352 if (error == 0) 1353 error = copyout(sa, uap->asa, sa_len); 1354 if (error == 0) 1355 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen)); 1356 if (sa) 1357 FREE(sa, M_SONAME); 1358 return (error); 1359 } 1360 1361 int 1362 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len) 1363 { 1364 struct sockaddr *sa; 1365 int error; 1366 1367 *namp = NULL; 1368 if (len > SOCK_MAXADDRLEN) 1369 return ENAMETOOLONG; 1370 if (len < offsetof(struct sockaddr, sa_data[0])) 1371 return EDOM; 1372 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK); 1373 error = copyin(uaddr, sa, len); 1374 if (error) { 1375 FREE(sa, M_SONAME); 1376 } else { 1377 #if BYTE_ORDER != BIG_ENDIAN 1378 /* 1379 * The bind(), connect(), and sendto() syscalls were not 1380 * versioned for COMPAT_43. Thus, this check must stay. 1381 */ 1382 if (sa->sa_family == 0 && sa->sa_len < AF_MAX) 1383 sa->sa_family = sa->sa_len; 1384 #endif 1385 sa->sa_len = len; 1386 *namp = sa; 1387 } 1388 return error; 1389 } 1390 1391 /* 1392 * Detach a mapped page and release resources back to the system. 1393 * We must release our wiring and if the object is ripped out 1394 * from under the vm_page we become responsible for freeing the 1395 * page. These routines must be MPSAFE. 1396 * 1397 * XXX HACK XXX TEMPORARY UNTIL WE IMPLEMENT EXT MBUF REFERENCE COUNTING 1398 * 1399 * XXX vm_page_*() routines are not MPSAFE yet, the MP lock is required. 1400 */ 1401 static void 1402 sf_buf_mfree(void *arg) 1403 { 1404 struct sf_buf *sf = arg; 1405 vm_page_t m; 1406 1407 /* 1408 * XXX vm_page_*() and SFBUF routines not MPSAFE yet. 1409 */ 1410 get_mplock(); 1411 crit_enter(); 1412 m = sf_buf_page(sf); 1413 if (sf_buf_free(sf) == 0) { 1414 vm_page_unwire(m, 0); 1415 if (m->wire_count == 0 && m->object == NULL) 1416 vm_page_try_to_free(m); 1417 } 1418 crit_exit(); 1419 rel_mplock(); 1420 } 1421 1422 /* 1423 * sendfile(2). 1424 * int sendfile(int fd, int s, off_t offset, size_t nbytes, 1425 * struct sf_hdtr *hdtr, off_t *sbytes, int flags) 1426 * 1427 * Send a file specified by 'fd' and starting at 'offset' to a socket 1428 * specified by 's'. Send only 'nbytes' of the file or until EOF if 1429 * nbytes == 0. Optionally add a header and/or trailer to the socket 1430 * output. If specified, write the total number of bytes sent into *sbytes. 1431 * 1432 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused 1433 * the headers to count against the remaining bytes to be sent from 1434 * the file descriptor. We may wish to implement a compatibility syscall 1435 * in the future. 1436 * 1437 * MPALMOSTSAFE 1438 */ 1439 int 1440 sys_sendfile(struct sendfile_args *uap) 1441 { 1442 struct thread *td = curthread; 1443 struct proc *p = td->td_proc; 1444 struct file *fp; 1445 struct vnode *vp = NULL; 1446 struct sf_hdtr hdtr; 1447 struct iovec aiov[UIO_SMALLIOV], *iov = NULL; 1448 struct uio auio; 1449 struct mbuf *mheader = NULL; 1450 size_t hbytes = 0; 1451 size_t tbytes; 1452 off_t hdtr_size = 0; 1453 off_t sbytes; 1454 int error; 1455 1456 KKASSERT(p); 1457 1458 /* 1459 * Do argument checking. Must be a regular file in, stream 1460 * type and connected socket out, positive offset. 1461 */ 1462 fp = holdfp(p->p_fd, uap->fd, FREAD); 1463 if (fp == NULL) { 1464 return (EBADF); 1465 } 1466 if (fp->f_type != DTYPE_VNODE) { 1467 fdrop(fp); 1468 return (EINVAL); 1469 } 1470 get_mplock(); 1471 vp = (struct vnode *)fp->f_data; 1472 vref(vp); 1473 fdrop(fp); 1474 1475 /* 1476 * If specified, get the pointer to the sf_hdtr struct for 1477 * any headers/trailers. 1478 */ 1479 if (uap->hdtr) { 1480 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr)); 1481 if (error) 1482 goto done; 1483 /* 1484 * Send any headers. 1485 */ 1486 if (hdtr.headers) { 1487 error = iovec_copyin(hdtr.headers, &iov, aiov, 1488 hdtr.hdr_cnt, &hbytes); 1489 if (error) 1490 goto done; 1491 auio.uio_iov = iov; 1492 auio.uio_iovcnt = hdtr.hdr_cnt; 1493 auio.uio_offset = 0; 1494 auio.uio_segflg = UIO_USERSPACE; 1495 auio.uio_rw = UIO_WRITE; 1496 auio.uio_td = td; 1497 auio.uio_resid = hbytes; 1498 1499 mheader = m_uiomove(&auio); 1500 1501 iovec_free(&iov, aiov); 1502 if (mheader == NULL) 1503 goto done; 1504 } 1505 } 1506 1507 error = kern_sendfile(vp, uap->s, uap->offset, uap->nbytes, mheader, 1508 &sbytes, uap->flags); 1509 if (error) 1510 goto done; 1511 1512 /* 1513 * Send trailers. Wimp out and use writev(2). 1514 */ 1515 if (uap->hdtr != NULL && hdtr.trailers != NULL) { 1516 error = iovec_copyin(hdtr.trailers, &iov, aiov, 1517 hdtr.trl_cnt, &auio.uio_resid); 1518 if (error) 1519 goto done; 1520 auio.uio_iov = iov; 1521 auio.uio_iovcnt = hdtr.trl_cnt; 1522 auio.uio_offset = 0; 1523 auio.uio_segflg = UIO_USERSPACE; 1524 auio.uio_rw = UIO_WRITE; 1525 auio.uio_td = td; 1526 1527 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0, &tbytes); 1528 1529 iovec_free(&iov, aiov); 1530 if (error) 1531 goto done; 1532 hdtr_size += tbytes; /* trailer bytes successfully sent */ 1533 } 1534 1535 done: 1536 if (vp) 1537 vrele(vp); 1538 rel_mplock(); 1539 if (uap->sbytes != NULL) { 1540 sbytes += hdtr_size; 1541 copyout(&sbytes, uap->sbytes, sizeof(off_t)); 1542 } 1543 return (error); 1544 } 1545 1546 int 1547 kern_sendfile(struct vnode *vp, int sfd, off_t offset, size_t nbytes, 1548 struct mbuf *mheader, off_t *sbytes, int flags) 1549 { 1550 struct thread *td = curthread; 1551 struct proc *p = td->td_proc; 1552 struct vm_object *obj; 1553 struct socket *so; 1554 struct file *fp; 1555 struct mbuf *m; 1556 struct sf_buf *sf; 1557 struct vm_page *pg; 1558 off_t off, xfsize; 1559 off_t hbytes = 0; 1560 int error = 0; 1561 1562 if (vp->v_type != VREG) { 1563 error = EINVAL; 1564 goto done0; 1565 } 1566 if ((obj = vp->v_object) == NULL) { 1567 error = EINVAL; 1568 goto done0; 1569 } 1570 error = holdsock(p->p_fd, sfd, &fp); 1571 if (error) 1572 goto done0; 1573 so = (struct socket *)fp->f_data; 1574 if (so->so_type != SOCK_STREAM) { 1575 error = EINVAL; 1576 goto done; 1577 } 1578 if ((so->so_state & SS_ISCONNECTED) == 0) { 1579 error = ENOTCONN; 1580 goto done; 1581 } 1582 if (offset < 0) { 1583 error = EINVAL; 1584 goto done; 1585 } 1586 1587 *sbytes = 0; 1588 /* 1589 * Protect against multiple writers to the socket. 1590 */ 1591 ssb_lock(&so->so_snd, M_WAITOK); 1592 1593 /* 1594 * Loop through the pages in the file, starting with the requested 1595 * offset. Get a file page (do I/O if necessary), map the file page 1596 * into an sf_buf, attach an mbuf header to the sf_buf, and queue 1597 * it on the socket. 1598 */ 1599 for (off = offset; ; off += xfsize, *sbytes += xfsize + hbytes) { 1600 vm_pindex_t pindex; 1601 vm_offset_t pgoff; 1602 1603 pindex = OFF_TO_IDX(off); 1604 retry_lookup: 1605 /* 1606 * Calculate the amount to transfer. Not to exceed a page, 1607 * the EOF, or the passed in nbytes. 1608 */ 1609 xfsize = vp->v_filesize - off; 1610 if (xfsize > PAGE_SIZE) 1611 xfsize = PAGE_SIZE; 1612 pgoff = (vm_offset_t)(off & PAGE_MASK); 1613 if (PAGE_SIZE - pgoff < xfsize) 1614 xfsize = PAGE_SIZE - pgoff; 1615 if (nbytes && xfsize > (nbytes - *sbytes)) 1616 xfsize = nbytes - *sbytes; 1617 if (xfsize <= 0) 1618 break; 1619 /* 1620 * Optimize the non-blocking case by looking at the socket space 1621 * before going to the extra work of constituting the sf_buf. 1622 */ 1623 if ((fp->f_flag & FNONBLOCK) && ssb_space(&so->so_snd) <= 0) { 1624 if (so->so_state & SS_CANTSENDMORE) 1625 error = EPIPE; 1626 else 1627 error = EAGAIN; 1628 ssb_unlock(&so->so_snd); 1629 goto done; 1630 } 1631 /* 1632 * Attempt to look up the page. 1633 * 1634 * Allocate if not found, wait and loop if busy, then 1635 * wire the page. critical section protection is 1636 * required to maintain the object association (an 1637 * interrupt can free the page) through to the 1638 * vm_page_wire() call. 1639 */ 1640 crit_enter(); 1641 lwkt_gettoken(&vm_token); 1642 pg = vm_page_lookup(obj, pindex); 1643 if (pg == NULL) { 1644 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL); 1645 if (pg == NULL) { 1646 vm_wait(0); 1647 lwkt_reltoken(&vm_token); 1648 crit_exit(); 1649 goto retry_lookup; 1650 } 1651 vm_page_wakeup(pg); 1652 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) { 1653 lwkt_reltoken(&vm_token); 1654 crit_exit(); 1655 goto retry_lookup; 1656 } 1657 vm_page_wire(pg); 1658 lwkt_reltoken(&vm_token); 1659 crit_exit(); 1660 1661 /* 1662 * If page is not valid for what we need, initiate I/O 1663 */ 1664 1665 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) { 1666 struct uio auio; 1667 struct iovec aiov; 1668 int bsize; 1669 1670 /* 1671 * Ensure that our page is still around when the I/O 1672 * completes. 1673 */ 1674 vm_page_io_start(pg); 1675 1676 /* 1677 * Get the page from backing store. 1678 */ 1679 bsize = vp->v_mount->mnt_stat.f_iosize; 1680 auio.uio_iov = &aiov; 1681 auio.uio_iovcnt = 1; 1682 aiov.iov_base = 0; 1683 aiov.iov_len = MAXBSIZE; 1684 auio.uio_resid = MAXBSIZE; 1685 auio.uio_offset = trunc_page(off); 1686 auio.uio_segflg = UIO_NOCOPY; 1687 auio.uio_rw = UIO_READ; 1688 auio.uio_td = td; 1689 vn_lock(vp, LK_SHARED | LK_RETRY); 1690 error = VOP_READ(vp, &auio, 1691 IO_VMIO | ((MAXBSIZE / bsize) << 16), 1692 td->td_ucred); 1693 vn_unlock(vp); 1694 vm_page_flag_clear(pg, PG_ZERO); 1695 vm_page_io_finish(pg); 1696 if (error) { 1697 crit_enter(); 1698 vm_page_unwire(pg, 0); 1699 vm_page_try_to_free(pg); 1700 crit_exit(); 1701 ssb_unlock(&so->so_snd); 1702 goto done; 1703 } 1704 } 1705 1706 1707 /* 1708 * Get a sendfile buf. We usually wait as long as necessary, 1709 * but this wait can be interrupted. 1710 */ 1711 if ((sf = sf_buf_alloc(pg)) == NULL) { 1712 crit_enter(); 1713 vm_page_unwire(pg, 0); 1714 vm_page_try_to_free(pg); 1715 crit_exit(); 1716 ssb_unlock(&so->so_snd); 1717 error = EINTR; 1718 goto done; 1719 } 1720 1721 /* 1722 * Get an mbuf header and set it up as having external storage. 1723 */ 1724 MGETHDR(m, MB_WAIT, MT_DATA); 1725 if (m == NULL) { 1726 error = ENOBUFS; 1727 sf_buf_free(sf); 1728 ssb_unlock(&so->so_snd); 1729 goto done; 1730 } 1731 1732 m->m_ext.ext_free = sf_buf_mfree; 1733 m->m_ext.ext_ref = sf_buf_ref; 1734 m->m_ext.ext_arg = sf; 1735 m->m_ext.ext_buf = (void *)sf_buf_kva(sf); 1736 m->m_ext.ext_size = PAGE_SIZE; 1737 m->m_data = (char *)sf_buf_kva(sf) + pgoff; 1738 m->m_flags |= M_EXT; 1739 m->m_pkthdr.len = m->m_len = xfsize; 1740 KKASSERT((m->m_flags & (M_EXT_CLUSTER)) == 0); 1741 1742 if (mheader != NULL) { 1743 hbytes = mheader->m_pkthdr.len; 1744 mheader->m_pkthdr.len += m->m_pkthdr.len; 1745 m_cat(mheader, m); 1746 m = mheader; 1747 mheader = NULL; 1748 } else 1749 hbytes = 0; 1750 1751 /* 1752 * Add the buffer to the socket buffer chain. 1753 */ 1754 crit_enter(); 1755 retry_space: 1756 /* 1757 * Make sure that the socket is still able to take more data. 1758 * CANTSENDMORE being true usually means that the connection 1759 * was closed. so_error is true when an error was sensed after 1760 * a previous send. 1761 * The state is checked after the page mapping and buffer 1762 * allocation above since those operations may block and make 1763 * any socket checks stale. From this point forward, nothing 1764 * blocks before the pru_send (or more accurately, any blocking 1765 * results in a loop back to here to re-check). 1766 */ 1767 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) { 1768 if (so->so_state & SS_CANTSENDMORE) { 1769 error = EPIPE; 1770 } else { 1771 error = so->so_error; 1772 so->so_error = 0; 1773 } 1774 m_freem(m); 1775 ssb_unlock(&so->so_snd); 1776 crit_exit(); 1777 goto done; 1778 } 1779 /* 1780 * Wait for socket space to become available. We do this just 1781 * after checking the connection state above in order to avoid 1782 * a race condition with ssb_wait(). 1783 */ 1784 if (ssb_space(&so->so_snd) < so->so_snd.ssb_lowat) { 1785 if (fp->f_flag & FNONBLOCK) { 1786 m_freem(m); 1787 ssb_unlock(&so->so_snd); 1788 crit_exit(); 1789 error = EAGAIN; 1790 goto done; 1791 } 1792 error = ssb_wait(&so->so_snd); 1793 /* 1794 * An error from ssb_wait usually indicates that we've 1795 * been interrupted by a signal. If we've sent anything 1796 * then return bytes sent, otherwise return the error. 1797 */ 1798 if (error) { 1799 m_freem(m); 1800 ssb_unlock(&so->so_snd); 1801 crit_exit(); 1802 goto done; 1803 } 1804 goto retry_space; 1805 } 1806 error = so_pru_send(so, 0, m, NULL, NULL, td); 1807 crit_exit(); 1808 if (error) { 1809 ssb_unlock(&so->so_snd); 1810 goto done; 1811 } 1812 } 1813 if (mheader != NULL) { 1814 *sbytes += mheader->m_pkthdr.len; 1815 error = so_pru_send(so, 0, mheader, NULL, NULL, td); 1816 mheader = NULL; 1817 } 1818 ssb_unlock(&so->so_snd); 1819 1820 done: 1821 fdrop(fp); 1822 done0: 1823 if (mheader != NULL) 1824 m_freem(mheader); 1825 return (error); 1826 } 1827 1828 /* 1829 * MPALMOSTSAFE 1830 */ 1831 int 1832 sys_sctp_peeloff(struct sctp_peeloff_args *uap) 1833 { 1834 #ifdef SCTP 1835 struct thread *td = curthread; 1836 struct filedesc *fdp = td->td_proc->p_fd; 1837 struct file *lfp = NULL; 1838 struct file *nfp = NULL; 1839 int error; 1840 struct socket *head, *so; 1841 caddr_t assoc_id; 1842 int fd; 1843 short fflag; /* type must match fp->f_flag */ 1844 1845 assoc_id = uap->name; 1846 error = holdsock(td->td_proc->p_fd, uap->sd, &lfp); 1847 if (error) 1848 return (error); 1849 1850 get_mplock(); 1851 crit_enter(); 1852 head = (struct socket *)lfp->f_data; 1853 error = sctp_can_peel_off(head, assoc_id); 1854 if (error) { 1855 crit_exit(); 1856 goto done; 1857 } 1858 /* 1859 * At this point we know we do have a assoc to pull 1860 * we proceed to get the fd setup. This may block 1861 * but that is ok. 1862 */ 1863 1864 fflag = lfp->f_flag; 1865 error = falloc(td->td_lwp, &nfp, &fd); 1866 if (error) { 1867 /* 1868 * Probably ran out of file descriptors. Put the 1869 * unaccepted connection back onto the queue and 1870 * do another wakeup so some other process might 1871 * have a chance at it. 1872 */ 1873 crit_exit(); 1874 goto done; 1875 } 1876 uap->sysmsg_iresult = fd; 1877 1878 so = sctp_get_peeloff(head, assoc_id, &error); 1879 if (so == NULL) { 1880 /* 1881 * Either someone else peeled it off OR 1882 * we can't get a socket. 1883 */ 1884 goto noconnection; 1885 } 1886 soreference(so); /* reference needed */ 1887 soclrstate(so, SS_NOFDREF | SS_COMP); /* when clearing NOFDREF */ 1888 so->so_head = NULL; 1889 if (head->so_sigio != NULL) 1890 fsetown(fgetown(head->so_sigio), &so->so_sigio); 1891 1892 nfp->f_type = DTYPE_SOCKET; 1893 nfp->f_flag = fflag; 1894 nfp->f_ops = &socketops; 1895 nfp->f_data = so; 1896 1897 noconnection: 1898 /* 1899 * Assign the file pointer to the reserved descriptor, or clear 1900 * the reserved descriptor if an error occured. 1901 */ 1902 if (error) 1903 fsetfd(fdp, NULL, fd); 1904 else 1905 fsetfd(fdp, nfp, fd); 1906 crit_exit(); 1907 /* 1908 * Release explicitly held references before returning. 1909 */ 1910 done: 1911 rel_mplock(); 1912 if (nfp != NULL) 1913 fdrop(nfp); 1914 fdrop(lfp); 1915 return (error); 1916 #else /* SCTP */ 1917 return(EOPNOTSUPP); 1918 #endif /* SCTP */ 1919 } 1920