kern_sendfile.c revision 73929
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: head/sys/kern/uipc_syscalls.c 73929 2001-03-07 03:37:06Z jhb $ 38 */ 39 40#include "opt_compat.h" 41#include "opt_ktrace.h" 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/kernel.h> 46#include <sys/sysproto.h> 47#include <sys/malloc.h> 48#include <sys/filedesc.h> 49#include <sys/event.h> 50#include <sys/proc.h> 51#include <sys/fcntl.h> 52#include <sys/file.h> 53#include <sys/mbuf.h> 54#include <sys/protosw.h> 55#include <sys/socket.h> 56#include <sys/socketvar.h> 57#include <sys/signalvar.h> 58#include <sys/uio.h> 59#include <sys/vnode.h> 60#include <sys/lock.h> 61#include <sys/mount.h> 62#ifdef KTRACE 63#include <sys/ktrace.h> 64#endif 65#include <vm/vm.h> 66#include <vm/vm_object.h> 67#include <vm/vm_page.h> 68#include <vm/vm_pageout.h> 69#include <vm/vm_kern.h> 70#include <vm/vm_extern.h> 71 72static void sf_buf_init(void *arg); 73SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL) 74static struct sf_buf *sf_buf_alloc(void); 75static void sf_buf_free(caddr_t addr, void *args); 76 77static int sendit __P((struct proc *p, int s, struct msghdr *mp, int flags)); 78static int recvit __P((struct proc *p, int s, struct msghdr *mp, 79 caddr_t namelenp)); 80 81static int accept1 __P((struct proc *p, struct accept_args *uap, int compat)); 82static int getsockname1 __P((struct proc *p, struct getsockname_args *uap, 83 int compat)); 84static int getpeername1 __P((struct proc *p, struct getpeername_args *uap, 85 int compat)); 86 87/* 88 * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the 89 * sf_freelist head with the sf_lock mutex. 90 */ 91static struct { 92 SLIST_HEAD(, sf_buf) sf_head; 93 struct mtx sf_lock; 94} sf_freelist; 95 96static vm_offset_t sf_base; 97static struct sf_buf *sf_bufs; 98static u_int sf_buf_alloc_want; 99 100/* 101 * System call interface to the socket abstraction. 102 */ 103#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 104#define COMPAT_OLDSOCK 105#endif 106 107extern struct fileops socketops; 108 109int 110socket(p, uap) 111 struct proc *p; 112 register struct socket_args /* { 113 int domain; 114 int type; 115 int protocol; 116 } */ *uap; 117{ 118 struct filedesc *fdp = p->p_fd; 119 struct socket *so; 120 struct file *fp; 121 int fd, error; 122 123 error = falloc(p, &fp, &fd); 124 if (error) 125 return (error); 126 fhold(fp); 127 error = socreate(uap->domain, &so, uap->type, uap->protocol, p); 128 if (error) { 129 if (fdp->fd_ofiles[fd] == fp) { 130 fdp->fd_ofiles[fd] = NULL; 131 fdrop(fp, p); 132 } 133 } else { 134 fp->f_data = (caddr_t)so; 135 fp->f_flag = FREAD|FWRITE; 136 fp->f_ops = &socketops; 137 fp->f_type = DTYPE_SOCKET; 138 p->p_retval[0] = fd; 139 } 140 fdrop(fp, p); 141 return (error); 142} 143 144/* ARGSUSED */ 145int 146bind(p, uap) 147 struct proc *p; 148 register struct bind_args /* { 149 int s; 150 caddr_t name; 151 int namelen; 152 } */ *uap; 153{ 154 struct file *fp; 155 struct sockaddr *sa; 156 int error; 157 158 error = holdsock(p->p_fd, uap->s, &fp); 159 if (error) 160 return (error); 161 error = getsockaddr(&sa, uap->name, uap->namelen); 162 if (error) { 163 fdrop(fp, p); 164 return (error); 165 } 166 error = sobind((struct socket *)fp->f_data, sa, p); 167 FREE(sa, M_SONAME); 168 fdrop(fp, p); 169 return (error); 170} 171 172/* ARGSUSED */ 173int 174listen(p, uap) 175 struct proc *p; 176 register struct listen_args /* { 177 int s; 178 int backlog; 179 } */ *uap; 180{ 181 struct file *fp; 182 int error; 183 184 error = holdsock(p->p_fd, uap->s, &fp); 185 if (error) 186 return (error); 187 error = solisten((struct socket *)fp->f_data, uap->backlog, p); 188 fdrop(fp, p); 189 return(error); 190} 191 192static int 193accept1(p, uap, compat) 194 struct proc *p; 195 register struct accept_args /* { 196 int s; 197 caddr_t name; 198 int *anamelen; 199 } */ *uap; 200 int compat; 201{ 202 struct filedesc *fdp = p->p_fd; 203 struct file *lfp = NULL; 204 struct file *nfp = NULL; 205 struct sockaddr *sa; 206 int namelen, error, s; 207 struct socket *head, *so; 208 int fd; 209 short fflag; /* type must match fp->f_flag */ 210 211 if (uap->name) { 212 error = copyin((caddr_t)uap->anamelen, (caddr_t)&namelen, 213 sizeof (namelen)); 214 if(error) 215 return (error); 216 } 217 error = holdsock(fdp, uap->s, &lfp); 218 if (error) 219 return (error); 220 s = splnet(); 221 head = (struct socket *)lfp->f_data; 222 if ((head->so_options & SO_ACCEPTCONN) == 0) { 223 splx(s); 224 error = EINVAL; 225 goto done; 226 } 227 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) { 228 splx(s); 229 error = EWOULDBLOCK; 230 goto done; 231 } 232 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) { 233 if (head->so_state & SS_CANTRCVMORE) { 234 head->so_error = ECONNABORTED; 235 break; 236 } 237 error = tsleep((caddr_t)&head->so_timeo, PSOCK | PCATCH, 238 "accept", 0); 239 if (error) { 240 splx(s); 241 goto done; 242 } 243 } 244 if (head->so_error) { 245 error = head->so_error; 246 head->so_error = 0; 247 splx(s); 248 goto done; 249 } 250 251 /* 252 * At this point we know that there is at least one connection 253 * ready to be accepted. Remove it from the queue prior to 254 * allocating the file descriptor for it since falloc() may 255 * block allowing another process to accept the connection 256 * instead. 257 */ 258 so = TAILQ_FIRST(&head->so_comp); 259 TAILQ_REMOVE(&head->so_comp, so, so_list); 260 head->so_qlen--; 261 262 fflag = lfp->f_flag; 263 error = falloc(p, &nfp, &fd); 264 if (error) { 265 /* 266 * Probably ran out of file descriptors. Put the 267 * unaccepted connection back onto the queue and 268 * do another wakeup so some other process might 269 * have a chance at it. 270 */ 271 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list); 272 head->so_qlen++; 273 wakeup_one(&head->so_timeo); 274 splx(s); 275 goto done; 276 } 277 fhold(nfp); 278 p->p_retval[0] = fd; 279 280 /* connection has been removed from the listen queue */ 281 KNOTE(&head->so_rcv.sb_sel.si_note, 0); 282 283 so->so_state &= ~SS_COMP; 284 so->so_head = NULL; 285 if (head->so_sigio != NULL) 286 fsetown(fgetown(head->so_sigio), &so->so_sigio); 287 288 nfp->f_data = (caddr_t)so; 289 nfp->f_flag = fflag; 290 nfp->f_ops = &socketops; 291 nfp->f_type = DTYPE_SOCKET; 292 sa = 0; 293 error = soaccept(so, &sa); 294 if (error) { 295 /* 296 * return a namelen of zero for older code which might 297 * ignore the return value from accept. 298 */ 299 if (uap->name != NULL) { 300 namelen = 0; 301 (void) copyout((caddr_t)&namelen, 302 (caddr_t)uap->anamelen, sizeof(*uap->anamelen)); 303 } 304 goto noconnection; 305 } 306 if (sa == NULL) { 307 namelen = 0; 308 if (uap->name) 309 goto gotnoname; 310 splx(s); 311 error = 0; 312 goto done; 313 } 314 if (uap->name) { 315 /* check sa_len before it is destroyed */ 316 if (namelen > sa->sa_len) 317 namelen = sa->sa_len; 318#ifdef COMPAT_OLDSOCK 319 if (compat) 320 ((struct osockaddr *)sa)->sa_family = 321 sa->sa_family; 322#endif 323 error = copyout(sa, (caddr_t)uap->name, (u_int)namelen); 324 if (!error) 325gotnoname: 326 error = copyout((caddr_t)&namelen, 327 (caddr_t)uap->anamelen, sizeof (*uap->anamelen)); 328 } 329noconnection: 330 if (sa) 331 FREE(sa, M_SONAME); 332 333 /* 334 * close the new descriptor, assuming someone hasn't ripped it 335 * out from under us. 336 */ 337 if (error) { 338 if (fdp->fd_ofiles[fd] == nfp) { 339 fdp->fd_ofiles[fd] = NULL; 340 fdrop(nfp, p); 341 } 342 } 343 splx(s); 344 345 /* 346 * Release explicitly held references before returning. 347 */ 348done: 349 if (nfp != NULL) 350 fdrop(nfp, p); 351 fdrop(lfp, p); 352 return (error); 353} 354 355int 356accept(p, uap) 357 struct proc *p; 358 struct accept_args *uap; 359{ 360 361 return (accept1(p, uap, 0)); 362} 363 364#ifdef COMPAT_OLDSOCK 365int 366oaccept(p, uap) 367 struct proc *p; 368 struct accept_args *uap; 369{ 370 371 return (accept1(p, uap, 1)); 372} 373#endif /* COMPAT_OLDSOCK */ 374 375/* ARGSUSED */ 376int 377connect(p, uap) 378 struct proc *p; 379 register struct connect_args /* { 380 int s; 381 caddr_t name; 382 int namelen; 383 } */ *uap; 384{ 385 struct file *fp; 386 register struct socket *so; 387 struct sockaddr *sa; 388 int error, s; 389 390 error = holdsock(p->p_fd, uap->s, &fp); 391 if (error) 392 return (error); 393 so = (struct socket *)fp->f_data; 394 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) { 395 error = EALREADY; 396 goto done; 397 } 398 error = getsockaddr(&sa, uap->name, uap->namelen); 399 if (error) 400 goto done; 401 error = soconnect(so, sa, p); 402 if (error) 403 goto bad; 404 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) { 405 FREE(sa, M_SONAME); 406 error = EINPROGRESS; 407 goto done; 408 } 409 s = splnet(); 410 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 411 error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH, 412 "connec", 0); 413 if (error) 414 break; 415 } 416 if (error == 0) { 417 error = so->so_error; 418 so->so_error = 0; 419 } 420 splx(s); 421bad: 422 so->so_state &= ~SS_ISCONNECTING; 423 FREE(sa, M_SONAME); 424 if (error == ERESTART) 425 error = EINTR; 426done: 427 fdrop(fp, p); 428 return (error); 429} 430 431int 432socketpair(p, uap) 433 struct proc *p; 434 register struct socketpair_args /* { 435 int domain; 436 int type; 437 int protocol; 438 int *rsv; 439 } */ *uap; 440{ 441 register struct filedesc *fdp = p->p_fd; 442 struct file *fp1, *fp2; 443 struct socket *so1, *so2; 444 int fd, error, sv[2]; 445 446 error = socreate(uap->domain, &so1, uap->type, uap->protocol, p); 447 if (error) 448 return (error); 449 error = socreate(uap->domain, &so2, uap->type, uap->protocol, p); 450 if (error) 451 goto free1; 452 error = falloc(p, &fp1, &fd); 453 if (error) 454 goto free2; 455 fhold(fp1); 456 sv[0] = fd; 457 fp1->f_data = (caddr_t)so1; 458 error = falloc(p, &fp2, &fd); 459 if (error) 460 goto free3; 461 fhold(fp2); 462 fp2->f_data = (caddr_t)so2; 463 sv[1] = fd; 464 error = soconnect2(so1, so2); 465 if (error) 466 goto free4; 467 if (uap->type == SOCK_DGRAM) { 468 /* 469 * Datagram socket connection is asymmetric. 470 */ 471 error = soconnect2(so2, so1); 472 if (error) 473 goto free4; 474 } 475 fp1->f_flag = fp2->f_flag = FREAD|FWRITE; 476 fp1->f_ops = fp2->f_ops = &socketops; 477 fp1->f_type = fp2->f_type = DTYPE_SOCKET; 478 error = copyout((caddr_t)sv, (caddr_t)uap->rsv, 2 * sizeof (int)); 479 fdrop(fp1, p); 480 fdrop(fp2, p); 481 return (error); 482free4: 483 if (fdp->fd_ofiles[sv[1]] == fp2) { 484 fdp->fd_ofiles[sv[1]] = NULL; 485 fdrop(fp2, p); 486 } 487 fdrop(fp2, p); 488free3: 489 if (fdp->fd_ofiles[sv[0]] == fp1) { 490 fdp->fd_ofiles[sv[0]] = NULL; 491 fdrop(fp1, p); 492 } 493 fdrop(fp1, p); 494free2: 495 (void)soclose(so2); 496free1: 497 (void)soclose(so1); 498 return (error); 499} 500 501static int 502sendit(p, s, mp, flags) 503 register struct proc *p; 504 int s; 505 register struct msghdr *mp; 506 int flags; 507{ 508 struct file *fp; 509 struct uio auio; 510 register struct iovec *iov; 511 register int i; 512 struct mbuf *control; 513 struct sockaddr *to; 514 int len, error; 515 struct socket *so; 516#ifdef KTRACE 517 struct iovec *ktriov = NULL; 518 struct uio ktruio; 519#endif 520 521 error = holdsock(p->p_fd, s, &fp); 522 if (error) 523 return (error); 524 auio.uio_iov = mp->msg_iov; 525 auio.uio_iovcnt = mp->msg_iovlen; 526 auio.uio_segflg = UIO_USERSPACE; 527 auio.uio_rw = UIO_WRITE; 528 auio.uio_procp = p; 529 auio.uio_offset = 0; /* XXX */ 530 auio.uio_resid = 0; 531 iov = mp->msg_iov; 532 for (i = 0; i < mp->msg_iovlen; i++, iov++) { 533 if ((auio.uio_resid += iov->iov_len) < 0) { 534 fdrop(fp, p); 535 return (EINVAL); 536 } 537 } 538 if (mp->msg_name) { 539 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen); 540 if (error) { 541 fdrop(fp, p); 542 return (error); 543 } 544 } else { 545 to = 0; 546 } 547 if (mp->msg_control) { 548 if (mp->msg_controllen < sizeof(struct cmsghdr) 549#ifdef COMPAT_OLDSOCK 550 && mp->msg_flags != MSG_COMPAT 551#endif 552 ) { 553 error = EINVAL; 554 goto bad; 555 } 556 error = sockargs(&control, mp->msg_control, 557 mp->msg_controllen, MT_CONTROL); 558 if (error) 559 goto bad; 560#ifdef COMPAT_OLDSOCK 561 if (mp->msg_flags == MSG_COMPAT) { 562 register struct cmsghdr *cm; 563 564 M_PREPEND(control, sizeof(*cm), M_TRYWAIT); 565 if (control == 0) { 566 error = ENOBUFS; 567 goto bad; 568 } else { 569 cm = mtod(control, struct cmsghdr *); 570 cm->cmsg_len = control->m_len; 571 cm->cmsg_level = SOL_SOCKET; 572 cm->cmsg_type = SCM_RIGHTS; 573 } 574 } 575#endif 576 } else { 577 control = 0; 578 } 579#ifdef KTRACE 580 if (KTRPOINT(p, KTR_GENIO)) { 581 int iovlen = auio.uio_iovcnt * sizeof (struct iovec); 582 583 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 584 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 585 ktruio = auio; 586 } 587#endif 588 len = auio.uio_resid; 589 so = (struct socket *)fp->f_data; 590 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, &auio, 0, control, 591 flags, p); 592 if (error) { 593 if (auio.uio_resid != len && (error == ERESTART || 594 error == EINTR || error == EWOULDBLOCK)) 595 error = 0; 596 if (error == EPIPE) { 597 PROC_LOCK(p); 598 psignal(p, SIGPIPE); 599 PROC_UNLOCK(p); 600 } 601 } 602 if (error == 0) 603 p->p_retval[0] = len - auio.uio_resid; 604#ifdef KTRACE 605 if (ktriov != NULL) { 606 if (error == 0) { 607 ktruio.uio_iov = ktriov; 608 ktruio.uio_resid = p->p_retval[0]; 609 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error); 610 } 611 FREE(ktriov, M_TEMP); 612 } 613#endif 614bad: 615 fdrop(fp, p); 616 if (to) 617 FREE(to, M_SONAME); 618 return (error); 619} 620 621int 622sendto(p, uap) 623 struct proc *p; 624 register struct sendto_args /* { 625 int s; 626 caddr_t buf; 627 size_t len; 628 int flags; 629 caddr_t to; 630 int tolen; 631 } */ *uap; 632{ 633 struct msghdr msg; 634 struct iovec aiov; 635 636 msg.msg_name = uap->to; 637 msg.msg_namelen = uap->tolen; 638 msg.msg_iov = &aiov; 639 msg.msg_iovlen = 1; 640 msg.msg_control = 0; 641#ifdef COMPAT_OLDSOCK 642 msg.msg_flags = 0; 643#endif 644 aiov.iov_base = uap->buf; 645 aiov.iov_len = uap->len; 646 return (sendit(p, uap->s, &msg, uap->flags)); 647} 648 649#ifdef COMPAT_OLDSOCK 650int 651osend(p, uap) 652 struct proc *p; 653 register struct osend_args /* { 654 int s; 655 caddr_t buf; 656 int len; 657 int flags; 658 } */ *uap; 659{ 660 struct msghdr msg; 661 struct iovec aiov; 662 663 msg.msg_name = 0; 664 msg.msg_namelen = 0; 665 msg.msg_iov = &aiov; 666 msg.msg_iovlen = 1; 667 aiov.iov_base = uap->buf; 668 aiov.iov_len = uap->len; 669 msg.msg_control = 0; 670 msg.msg_flags = 0; 671 return (sendit(p, uap->s, &msg, uap->flags)); 672} 673 674int 675osendmsg(p, uap) 676 struct proc *p; 677 register struct osendmsg_args /* { 678 int s; 679 caddr_t msg; 680 int flags; 681 } */ *uap; 682{ 683 struct msghdr msg; 684 struct iovec aiov[UIO_SMALLIOV], *iov; 685 int error; 686 687 error = copyin(uap->msg, (caddr_t)&msg, sizeof (struct omsghdr)); 688 if (error) 689 return (error); 690 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) { 691 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) 692 return (EMSGSIZE); 693 MALLOC(iov, struct iovec *, 694 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV, 695 M_WAITOK); 696 } else 697 iov = aiov; 698 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov, 699 (unsigned)(msg.msg_iovlen * sizeof (struct iovec))); 700 if (error) 701 goto done; 702 msg.msg_flags = MSG_COMPAT; 703 msg.msg_iov = iov; 704 error = sendit(p, uap->s, &msg, uap->flags); 705done: 706 if (iov != aiov) 707 FREE(iov, M_IOV); 708 return (error); 709} 710#endif 711 712int 713sendmsg(p, uap) 714 struct proc *p; 715 register struct sendmsg_args /* { 716 int s; 717 caddr_t msg; 718 int flags; 719 } */ *uap; 720{ 721 struct msghdr msg; 722 struct iovec aiov[UIO_SMALLIOV], *iov; 723 int error; 724 725 error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg)); 726 if (error) 727 return (error); 728 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) { 729 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) 730 return (EMSGSIZE); 731 MALLOC(iov, struct iovec *, 732 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV, 733 M_WAITOK); 734 } else 735 iov = aiov; 736 if (msg.msg_iovlen && 737 (error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov, 738 (unsigned)(msg.msg_iovlen * sizeof (struct iovec))))) 739 goto done; 740 msg.msg_iov = iov; 741#ifdef COMPAT_OLDSOCK 742 msg.msg_flags = 0; 743#endif 744 error = sendit(p, uap->s, &msg, uap->flags); 745done: 746 if (iov != aiov) 747 FREE(iov, M_IOV); 748 return (error); 749} 750 751static int 752recvit(p, s, mp, namelenp) 753 register struct proc *p; 754 int s; 755 register struct msghdr *mp; 756 caddr_t namelenp; 757{ 758 struct file *fp; 759 struct uio auio; 760 register struct iovec *iov; 761 register int i; 762 int len, error; 763 struct mbuf *m, *control = 0; 764 caddr_t ctlbuf; 765 struct socket *so; 766 struct sockaddr *fromsa = 0; 767#ifdef KTRACE 768 struct iovec *ktriov = NULL; 769 struct uio ktruio; 770#endif 771 772 error = holdsock(p->p_fd, s, &fp); 773 if (error) 774 return (error); 775 auio.uio_iov = mp->msg_iov; 776 auio.uio_iovcnt = mp->msg_iovlen; 777 auio.uio_segflg = UIO_USERSPACE; 778 auio.uio_rw = UIO_READ; 779 auio.uio_procp = p; 780 auio.uio_offset = 0; /* XXX */ 781 auio.uio_resid = 0; 782 iov = mp->msg_iov; 783 for (i = 0; i < mp->msg_iovlen; i++, iov++) { 784 if ((auio.uio_resid += iov->iov_len) < 0) { 785 fdrop(fp, p); 786 return (EINVAL); 787 } 788 } 789#ifdef KTRACE 790 if (KTRPOINT(p, KTR_GENIO)) { 791 int iovlen = auio.uio_iovcnt * sizeof (struct iovec); 792 793 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 794 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 795 ktruio = auio; 796 } 797#endif 798 len = auio.uio_resid; 799 so = (struct socket *)fp->f_data; 800 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio, 801 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0, 802 &mp->msg_flags); 803 if (error) { 804 if (auio.uio_resid != len && (error == ERESTART || 805 error == EINTR || error == EWOULDBLOCK)) 806 error = 0; 807 } 808#ifdef KTRACE 809 if (ktriov != NULL) { 810 if (error == 0) { 811 ktruio.uio_iov = ktriov; 812 ktruio.uio_resid = len - auio.uio_resid; 813 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error); 814 } 815 FREE(ktriov, M_TEMP); 816 } 817#endif 818 if (error) 819 goto out; 820 p->p_retval[0] = len - auio.uio_resid; 821 if (mp->msg_name) { 822 len = mp->msg_namelen; 823 if (len <= 0 || fromsa == 0) 824 len = 0; 825 else { 826#ifndef MIN 827#define MIN(a,b) ((a)>(b)?(b):(a)) 828#endif 829 /* save sa_len before it is destroyed by MSG_COMPAT */ 830 len = MIN(len, fromsa->sa_len); 831#ifdef COMPAT_OLDSOCK 832 if (mp->msg_flags & MSG_COMPAT) 833 ((struct osockaddr *)fromsa)->sa_family = 834 fromsa->sa_family; 835#endif 836 error = copyout(fromsa, 837 (caddr_t)mp->msg_name, (unsigned)len); 838 if (error) 839 goto out; 840 } 841 mp->msg_namelen = len; 842 if (namelenp && 843 (error = copyout((caddr_t)&len, namelenp, sizeof (int)))) { 844#ifdef COMPAT_OLDSOCK 845 if (mp->msg_flags & MSG_COMPAT) 846 error = 0; /* old recvfrom didn't check */ 847 else 848#endif 849 goto out; 850 } 851 } 852 if (mp->msg_control) { 853#ifdef COMPAT_OLDSOCK 854 /* 855 * We assume that old recvmsg calls won't receive access 856 * rights and other control info, esp. as control info 857 * is always optional and those options didn't exist in 4.3. 858 * If we receive rights, trim the cmsghdr; anything else 859 * is tossed. 860 */ 861 if (control && mp->msg_flags & MSG_COMPAT) { 862 if (mtod(control, struct cmsghdr *)->cmsg_level != 863 SOL_SOCKET || 864 mtod(control, struct cmsghdr *)->cmsg_type != 865 SCM_RIGHTS) { 866 mp->msg_controllen = 0; 867 goto out; 868 } 869 control->m_len -= sizeof (struct cmsghdr); 870 control->m_data += sizeof (struct cmsghdr); 871 } 872#endif 873 len = mp->msg_controllen; 874 m = control; 875 mp->msg_controllen = 0; 876 ctlbuf = (caddr_t) mp->msg_control; 877 878 while (m && len > 0) { 879 unsigned int tocopy; 880 881 if (len >= m->m_len) 882 tocopy = m->m_len; 883 else { 884 mp->msg_flags |= MSG_CTRUNC; 885 tocopy = len; 886 } 887 888 if ((error = copyout((caddr_t)mtod(m, caddr_t), 889 ctlbuf, tocopy)) != 0) 890 goto out; 891 892 ctlbuf += tocopy; 893 len -= tocopy; 894 m = m->m_next; 895 } 896 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control; 897 } 898out: 899 fdrop(fp, p); 900 if (fromsa) 901 FREE(fromsa, M_SONAME); 902 if (control) 903 m_freem(control); 904 return (error); 905} 906 907int 908recvfrom(p, uap) 909 struct proc *p; 910 register struct recvfrom_args /* { 911 int s; 912 caddr_t buf; 913 size_t len; 914 int flags; 915 caddr_t from; 916 int *fromlenaddr; 917 } */ *uap; 918{ 919 struct msghdr msg; 920 struct iovec aiov; 921 int error; 922 923 if (uap->fromlenaddr) { 924 error = copyin((caddr_t)uap->fromlenaddr, 925 (caddr_t)&msg.msg_namelen, sizeof (msg.msg_namelen)); 926 if (error) 927 return (error); 928 } else 929 msg.msg_namelen = 0; 930 msg.msg_name = uap->from; 931 msg.msg_iov = &aiov; 932 msg.msg_iovlen = 1; 933 aiov.iov_base = uap->buf; 934 aiov.iov_len = uap->len; 935 msg.msg_control = 0; 936 msg.msg_flags = uap->flags; 937 return (recvit(p, uap->s, &msg, (caddr_t)uap->fromlenaddr)); 938} 939 940#ifdef COMPAT_OLDSOCK 941int 942orecvfrom(p, uap) 943 struct proc *p; 944 struct recvfrom_args *uap; 945{ 946 947 uap->flags |= MSG_COMPAT; 948 return (recvfrom(p, uap)); 949} 950#endif 951 952 953#ifdef COMPAT_OLDSOCK 954int 955orecv(p, uap) 956 struct proc *p; 957 register struct orecv_args /* { 958 int s; 959 caddr_t buf; 960 int len; 961 int flags; 962 } */ *uap; 963{ 964 struct msghdr msg; 965 struct iovec aiov; 966 967 msg.msg_name = 0; 968 msg.msg_namelen = 0; 969 msg.msg_iov = &aiov; 970 msg.msg_iovlen = 1; 971 aiov.iov_base = uap->buf; 972 aiov.iov_len = uap->len; 973 msg.msg_control = 0; 974 msg.msg_flags = uap->flags; 975 return (recvit(p, uap->s, &msg, (caddr_t)0)); 976} 977 978/* 979 * Old recvmsg. This code takes advantage of the fact that the old msghdr 980 * overlays the new one, missing only the flags, and with the (old) access 981 * rights where the control fields are now. 982 */ 983int 984orecvmsg(p, uap) 985 struct proc *p; 986 register struct orecvmsg_args /* { 987 int s; 988 struct omsghdr *msg; 989 int flags; 990 } */ *uap; 991{ 992 struct msghdr msg; 993 struct iovec aiov[UIO_SMALLIOV], *iov; 994 int error; 995 996 error = copyin((caddr_t)uap->msg, (caddr_t)&msg, 997 sizeof (struct omsghdr)); 998 if (error) 999 return (error); 1000 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) { 1001 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) 1002 return (EMSGSIZE); 1003 MALLOC(iov, struct iovec *, 1004 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV, 1005 M_WAITOK); 1006 } else 1007 iov = aiov; 1008 msg.msg_flags = uap->flags | MSG_COMPAT; 1009 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov, 1010 (unsigned)(msg.msg_iovlen * sizeof (struct iovec))); 1011 if (error) 1012 goto done; 1013 msg.msg_iov = iov; 1014 error = recvit(p, uap->s, &msg, (caddr_t)&uap->msg->msg_namelen); 1015 1016 if (msg.msg_controllen && error == 0) 1017 error = copyout((caddr_t)&msg.msg_controllen, 1018 (caddr_t)&uap->msg->msg_accrightslen, sizeof (int)); 1019done: 1020 if (iov != aiov) 1021 FREE(iov, M_IOV); 1022 return (error); 1023} 1024#endif 1025 1026int 1027recvmsg(p, uap) 1028 struct proc *p; 1029 register struct recvmsg_args /* { 1030 int s; 1031 struct msghdr *msg; 1032 int flags; 1033 } */ *uap; 1034{ 1035 struct msghdr msg; 1036 struct iovec aiov[UIO_SMALLIOV], *uiov, *iov; 1037 register int error; 1038 1039 error = copyin((caddr_t)uap->msg, (caddr_t)&msg, sizeof (msg)); 1040 if (error) 1041 return (error); 1042 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) { 1043 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) 1044 return (EMSGSIZE); 1045 MALLOC(iov, struct iovec *, 1046 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV, 1047 M_WAITOK); 1048 } else 1049 iov = aiov; 1050#ifdef COMPAT_OLDSOCK 1051 msg.msg_flags = uap->flags &~ MSG_COMPAT; 1052#else 1053 msg.msg_flags = uap->flags; 1054#endif 1055 uiov = msg.msg_iov; 1056 msg.msg_iov = iov; 1057 error = copyin((caddr_t)uiov, (caddr_t)iov, 1058 (unsigned)(msg.msg_iovlen * sizeof (struct iovec))); 1059 if (error) 1060 goto done; 1061 error = recvit(p, uap->s, &msg, (caddr_t)0); 1062 if (!error) { 1063 msg.msg_iov = uiov; 1064 error = copyout((caddr_t)&msg, (caddr_t)uap->msg, sizeof(msg)); 1065 } 1066done: 1067 if (iov != aiov) 1068 FREE(iov, M_IOV); 1069 return (error); 1070} 1071 1072/* ARGSUSED */ 1073int 1074shutdown(p, uap) 1075 struct proc *p; 1076 register struct shutdown_args /* { 1077 int s; 1078 int how; 1079 } */ *uap; 1080{ 1081 struct file *fp; 1082 int error; 1083 1084 error = holdsock(p->p_fd, uap->s, &fp); 1085 if (error) 1086 return (error); 1087 error = soshutdown((struct socket *)fp->f_data, uap->how); 1088 fdrop(fp, p); 1089 return(error); 1090} 1091 1092/* ARGSUSED */ 1093int 1094setsockopt(p, uap) 1095 struct proc *p; 1096 register struct setsockopt_args /* { 1097 int s; 1098 int level; 1099 int name; 1100 caddr_t val; 1101 int valsize; 1102 } */ *uap; 1103{ 1104 struct file *fp; 1105 struct sockopt sopt; 1106 int error; 1107 1108 if (uap->val == 0 && uap->valsize != 0) 1109 return (EFAULT); 1110 if (uap->valsize < 0) 1111 return (EINVAL); 1112 1113 error = holdsock(p->p_fd, uap->s, &fp); 1114 if (error) 1115 return (error); 1116 1117 sopt.sopt_dir = SOPT_SET; 1118 sopt.sopt_level = uap->level; 1119 sopt.sopt_name = uap->name; 1120 sopt.sopt_val = uap->val; 1121 sopt.sopt_valsize = uap->valsize; 1122 sopt.sopt_p = p; 1123 error = sosetopt((struct socket *)fp->f_data, &sopt); 1124 fdrop(fp, p); 1125 return(error); 1126} 1127 1128/* ARGSUSED */ 1129int 1130getsockopt(p, uap) 1131 struct proc *p; 1132 register struct getsockopt_args /* { 1133 int s; 1134 int level; 1135 int name; 1136 caddr_t val; 1137 int *avalsize; 1138 } */ *uap; 1139{ 1140 int valsize, error; 1141 struct file *fp; 1142 struct sockopt sopt; 1143 1144 error = holdsock(p->p_fd, uap->s, &fp); 1145 if (error) 1146 return (error); 1147 if (uap->val) { 1148 error = copyin((caddr_t)uap->avalsize, (caddr_t)&valsize, 1149 sizeof (valsize)); 1150 if (error) { 1151 fdrop(fp, p); 1152 return (error); 1153 } 1154 if (valsize < 0) { 1155 fdrop(fp, p); 1156 return (EINVAL); 1157 } 1158 } else { 1159 valsize = 0; 1160 } 1161 1162 sopt.sopt_dir = SOPT_GET; 1163 sopt.sopt_level = uap->level; 1164 sopt.sopt_name = uap->name; 1165 sopt.sopt_val = uap->val; 1166 sopt.sopt_valsize = (size_t)valsize; /* checked non-negative above */ 1167 sopt.sopt_p = p; 1168 1169 error = sogetopt((struct socket *)fp->f_data, &sopt); 1170 if (error == 0) { 1171 valsize = sopt.sopt_valsize; 1172 error = copyout((caddr_t)&valsize, 1173 (caddr_t)uap->avalsize, sizeof (valsize)); 1174 } 1175 fdrop(fp, p); 1176 return (error); 1177} 1178 1179/* 1180 * Get socket name. 1181 */ 1182/* ARGSUSED */ 1183static int 1184getsockname1(p, uap, compat) 1185 struct proc *p; 1186 register struct getsockname_args /* { 1187 int fdes; 1188 caddr_t asa; 1189 int *alen; 1190 } */ *uap; 1191 int compat; 1192{ 1193 struct file *fp; 1194 register struct socket *so; 1195 struct sockaddr *sa; 1196 int len, error; 1197 1198 error = holdsock(p->p_fd, uap->fdes, &fp); 1199 if (error) 1200 return (error); 1201 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len)); 1202 if (error) { 1203 fdrop(fp, p); 1204 return (error); 1205 } 1206 so = (struct socket *)fp->f_data; 1207 sa = 0; 1208 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa); 1209 if (error) 1210 goto bad; 1211 if (sa == 0) { 1212 len = 0; 1213 goto gotnothing; 1214 } 1215 1216 len = MIN(len, sa->sa_len); 1217#ifdef COMPAT_OLDSOCK 1218 if (compat) 1219 ((struct osockaddr *)sa)->sa_family = sa->sa_family; 1220#endif 1221 error = copyout(sa, (caddr_t)uap->asa, (u_int)len); 1222 if (error == 0) 1223gotnothing: 1224 error = copyout((caddr_t)&len, (caddr_t)uap->alen, 1225 sizeof (len)); 1226bad: 1227 if (sa) 1228 FREE(sa, M_SONAME); 1229 fdrop(fp, p); 1230 return (error); 1231} 1232 1233int 1234getsockname(p, uap) 1235 struct proc *p; 1236 struct getsockname_args *uap; 1237{ 1238 1239 return (getsockname1(p, uap, 0)); 1240} 1241 1242#ifdef COMPAT_OLDSOCK 1243int 1244ogetsockname(p, uap) 1245 struct proc *p; 1246 struct getsockname_args *uap; 1247{ 1248 1249 return (getsockname1(p, uap, 1)); 1250} 1251#endif /* COMPAT_OLDSOCK */ 1252 1253/* 1254 * Get name of peer for connected socket. 1255 */ 1256/* ARGSUSED */ 1257static int 1258getpeername1(p, uap, compat) 1259 struct proc *p; 1260 register struct getpeername_args /* { 1261 int fdes; 1262 caddr_t asa; 1263 int *alen; 1264 } */ *uap; 1265 int compat; 1266{ 1267 struct file *fp; 1268 register struct socket *so; 1269 struct sockaddr *sa; 1270 int len, error; 1271 1272 error = holdsock(p->p_fd, uap->fdes, &fp); 1273 if (error) 1274 return (error); 1275 so = (struct socket *)fp->f_data; 1276 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) { 1277 fdrop(fp, p); 1278 return (ENOTCONN); 1279 } 1280 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len)); 1281 if (error) { 1282 fdrop(fp, p); 1283 return (error); 1284 } 1285 sa = 0; 1286 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa); 1287 if (error) 1288 goto bad; 1289 if (sa == 0) { 1290 len = 0; 1291 goto gotnothing; 1292 } 1293 len = MIN(len, sa->sa_len); 1294#ifdef COMPAT_OLDSOCK 1295 if (compat) 1296 ((struct osockaddr *)sa)->sa_family = 1297 sa->sa_family; 1298#endif 1299 error = copyout(sa, (caddr_t)uap->asa, (u_int)len); 1300 if (error) 1301 goto bad; 1302gotnothing: 1303 error = copyout((caddr_t)&len, (caddr_t)uap->alen, sizeof (len)); 1304bad: 1305 if (sa) 1306 FREE(sa, M_SONAME); 1307 fdrop(fp, p); 1308 return (error); 1309} 1310 1311int 1312getpeername(p, uap) 1313 struct proc *p; 1314 struct getpeername_args *uap; 1315{ 1316 1317 return (getpeername1(p, uap, 0)); 1318} 1319 1320#ifdef COMPAT_OLDSOCK 1321int 1322ogetpeername(p, uap) 1323 struct proc *p; 1324 struct ogetpeername_args *uap; 1325{ 1326 1327 /* XXX uap should have type `getpeername_args *' to begin with. */ 1328 return (getpeername1(p, (struct getpeername_args *)uap, 1)); 1329} 1330#endif /* COMPAT_OLDSOCK */ 1331 1332int 1333sockargs(mp, buf, buflen, type) 1334 struct mbuf **mp; 1335 caddr_t buf; 1336 int buflen, type; 1337{ 1338 register struct sockaddr *sa; 1339 register struct mbuf *m; 1340 int error; 1341 1342 if ((u_int)buflen > MLEN) { 1343#ifdef COMPAT_OLDSOCK 1344 if (type == MT_SONAME && (u_int)buflen <= 112) 1345 buflen = MLEN; /* unix domain compat. hack */ 1346 else 1347#endif 1348 return (EINVAL); 1349 } 1350 m = m_get(M_TRYWAIT, type); 1351 if (m == NULL) 1352 return (ENOBUFS); 1353 m->m_len = buflen; 1354 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen); 1355 if (error) 1356 (void) m_free(m); 1357 else { 1358 *mp = m; 1359 if (type == MT_SONAME) { 1360 sa = mtod(m, struct sockaddr *); 1361 1362#if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN 1363 if (sa->sa_family == 0 && sa->sa_len < AF_MAX) 1364 sa->sa_family = sa->sa_len; 1365#endif 1366 sa->sa_len = buflen; 1367 } 1368 } 1369 return (error); 1370} 1371 1372int 1373getsockaddr(namp, uaddr, len) 1374 struct sockaddr **namp; 1375 caddr_t uaddr; 1376 size_t len; 1377{ 1378 struct sockaddr *sa; 1379 int error; 1380 1381 if (len > SOCK_MAXADDRLEN) 1382 return ENAMETOOLONG; 1383 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK); 1384 error = copyin(uaddr, sa, len); 1385 if (error) { 1386 FREE(sa, M_SONAME); 1387 } else { 1388#if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN 1389 if (sa->sa_family == 0 && sa->sa_len < AF_MAX) 1390 sa->sa_family = sa->sa_len; 1391#endif 1392 sa->sa_len = len; 1393 *namp = sa; 1394 } 1395 return error; 1396} 1397 1398/* 1399 * holdsock() - load the struct file pointer associated 1400 * with a socket into *fpp. If an error occurs, non-zero 1401 * will be returned and *fpp will be set to NULL. 1402 */ 1403int 1404holdsock(fdp, fdes, fpp) 1405 struct filedesc *fdp; 1406 int fdes; 1407 struct file **fpp; 1408{ 1409 register struct file *fp = NULL; 1410 int error = 0; 1411 1412 if ((unsigned)fdes >= fdp->fd_nfiles || 1413 (fp = fdp->fd_ofiles[fdes]) == NULL) { 1414 error = EBADF; 1415 } else if (fp->f_type != DTYPE_SOCKET) { 1416 error = ENOTSOCK; 1417 fp = NULL; 1418 } else { 1419 fhold(fp); 1420 } 1421 *fpp = fp; 1422 return(error); 1423} 1424 1425/* 1426 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-)) 1427 * XXX - The sf_buf functions are currently private to sendfile(2), so have 1428 * been made static, but may be useful in the future for doing zero-copy in 1429 * other parts of the networking code. 1430 */ 1431static void 1432sf_buf_init(void *arg) 1433{ 1434 int i; 1435 1436 mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", MTX_DEF); 1437 mtx_lock(&sf_freelist.sf_lock); 1438 SLIST_INIT(&sf_freelist.sf_head); 1439 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE); 1440 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, 1441 M_NOWAIT | M_ZERO); 1442 for (i = 0; i < nsfbufs; i++) { 1443 sf_bufs[i].kva = sf_base + i * PAGE_SIZE; 1444 SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list); 1445 } 1446 sf_buf_alloc_want = 0; 1447 mtx_unlock(&sf_freelist.sf_lock); 1448} 1449 1450/* 1451 * Get an sf_buf from the freelist. Will block if none are available. 1452 */ 1453static struct sf_buf * 1454sf_buf_alloc() 1455{ 1456 struct sf_buf *sf; 1457 1458 mtx_lock(&sf_freelist.sf_lock); 1459 while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) { 1460 sf_buf_alloc_want++; 1461 msleep(&sf_freelist, &sf_freelist.sf_lock, PVM, "sfbufa", 0); 1462 } 1463 SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list); 1464 mtx_unlock(&sf_freelist.sf_lock); 1465 return (sf); 1466} 1467 1468#define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT]) 1469 1470/* 1471 * Detatch mapped page and release resources back to the system. 1472 */ 1473static void 1474sf_buf_free(caddr_t addr, void *args) 1475{ 1476 struct sf_buf *sf; 1477 struct vm_page *m; 1478 int s; 1479 1480 sf = dtosf(addr); 1481 pmap_qremove((vm_offset_t)addr, 1); 1482 m = sf->m; 1483 s = splvm(); 1484 vm_page_unwire(m, 0); 1485 /* 1486 * Check for the object going away on us. This can 1487 * happen since we don't hold a reference to it. 1488 * If so, we're responsible for freeing the page. 1489 */ 1490 if (m->wire_count == 0 && m->object == NULL) 1491 vm_page_free(m); 1492 splx(s); 1493 sf->m = NULL; 1494 mtx_lock(&sf_freelist.sf_lock); 1495 SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list); 1496 if (sf_buf_alloc_want) { 1497 sf_buf_alloc_want--; 1498 wakeup_one(&sf_freelist); 1499 } 1500 mtx_unlock(&sf_freelist.sf_lock); 1501} 1502 1503/* 1504 * sendfile(2) 1505 * int sendfile(int fd, int s, off_t offset, size_t nbytes, 1506 * struct sf_hdtr *hdtr, off_t *sbytes, int flags) 1507 * 1508 * Send a file specified by 'fd' and starting at 'offset' to a socket 1509 * specified by 's'. Send only 'nbytes' of the file or until EOF if 1510 * nbytes == 0. Optionally add a header and/or trailer to the socket 1511 * output. If specified, write the total number of bytes sent into *sbytes. 1512 */ 1513int 1514sendfile(struct proc *p, struct sendfile_args *uap) 1515{ 1516 struct file *fp; 1517 struct filedesc *fdp = p->p_fd; 1518 struct vnode *vp; 1519 struct vm_object *obj; 1520 struct socket *so; 1521 struct mbuf *m; 1522 struct sf_buf *sf; 1523 struct vm_page *pg; 1524 struct writev_args nuap; 1525 struct sf_hdtr hdtr; 1526 off_t off, xfsize, sbytes = 0; 1527 int error = 0, s; 1528 1529 vp = NULL; 1530 /* 1531 * Do argument checking. Must be a regular file in, stream 1532 * type and connected socket out, positive offset. 1533 */ 1534 fp = holdfp(fdp, uap->fd, FREAD); 1535 if (fp == NULL) { 1536 error = EBADF; 1537 goto done; 1538 } 1539 if (fp->f_type != DTYPE_VNODE) { 1540 error = EINVAL; 1541 goto done; 1542 } 1543 vp = (struct vnode *)fp->f_data; 1544 vref(vp); 1545 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) { 1546 error = EINVAL; 1547 goto done; 1548 } 1549 fdrop(fp, p); 1550 error = holdsock(p->p_fd, uap->s, &fp); 1551 if (error) 1552 goto done; 1553 so = (struct socket *)fp->f_data; 1554 if (so->so_type != SOCK_STREAM) { 1555 error = EINVAL; 1556 goto done; 1557 } 1558 if ((so->so_state & SS_ISCONNECTED) == 0) { 1559 error = ENOTCONN; 1560 goto done; 1561 } 1562 if (uap->offset < 0) { 1563 error = EINVAL; 1564 goto done; 1565 } 1566 1567 /* 1568 * If specified, get the pointer to the sf_hdtr struct for 1569 * any headers/trailers. 1570 */ 1571 if (uap->hdtr != NULL) { 1572 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr)); 1573 if (error) 1574 goto done; 1575 /* 1576 * Send any headers. Wimp out and use writev(2). 1577 */ 1578 if (hdtr.headers != NULL) { 1579 nuap.fd = uap->s; 1580 nuap.iovp = hdtr.headers; 1581 nuap.iovcnt = hdtr.hdr_cnt; 1582 error = writev(p, &nuap); 1583 if (error) 1584 goto done; 1585 sbytes += p->p_retval[0]; 1586 } 1587 } 1588 1589 /* 1590 * Protect against multiple writers to the socket. 1591 */ 1592 (void) sblock(&so->so_snd, M_WAITOK); 1593 1594 /* 1595 * Loop through the pages in the file, starting with the requested 1596 * offset. Get a file page (do I/O if necessary), map the file page 1597 * into an sf_buf, attach an mbuf header to the sf_buf, and queue 1598 * it on the socket. 1599 */ 1600 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) { 1601 vm_pindex_t pindex; 1602 vm_offset_t pgoff; 1603 1604 pindex = OFF_TO_IDX(off); 1605retry_lookup: 1606 /* 1607 * Calculate the amount to transfer. Not to exceed a page, 1608 * the EOF, or the passed in nbytes. 1609 */ 1610 xfsize = obj->un_pager.vnp.vnp_size - off; 1611 if (xfsize > PAGE_SIZE) 1612 xfsize = PAGE_SIZE; 1613 pgoff = (vm_offset_t)(off & PAGE_MASK); 1614 if (PAGE_SIZE - pgoff < xfsize) 1615 xfsize = PAGE_SIZE - pgoff; 1616 if (uap->nbytes && xfsize > (uap->nbytes - sbytes)) 1617 xfsize = uap->nbytes - sbytes; 1618 if (xfsize <= 0) 1619 break; 1620 /* 1621 * Optimize the non-blocking case by looking at the socket space 1622 * before going to the extra work of constituting the sf_buf. 1623 */ 1624 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) { 1625 if (so->so_state & SS_CANTSENDMORE) 1626 error = EPIPE; 1627 else 1628 error = EAGAIN; 1629 sbunlock(&so->so_snd); 1630 goto done; 1631 } 1632 /* 1633 * Attempt to look up the page. 1634 * 1635 * Allocate if not found 1636 * 1637 * Wait and loop if busy. 1638 */ 1639 pg = vm_page_lookup(obj, pindex); 1640 1641 if (pg == NULL) { 1642 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL); 1643 if (pg == NULL) { 1644 VM_WAIT; 1645 goto retry_lookup; 1646 } 1647 vm_page_wakeup(pg); 1648 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) { 1649 goto retry_lookup; 1650 } 1651 1652 /* 1653 * Wire the page so it does not get ripped out from under 1654 * us. 1655 */ 1656 1657 vm_page_wire(pg); 1658 1659 /* 1660 * If page is not valid for what we need, initiate I/O 1661 */ 1662 1663 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) { 1664 struct uio auio; 1665 struct iovec aiov; 1666 int bsize; 1667 1668 /* 1669 * Ensure that our page is still around when the I/O 1670 * completes. 1671 */ 1672 vm_page_io_start(pg); 1673 1674 /* 1675 * Get the page from backing store. 1676 */ 1677 bsize = vp->v_mount->mnt_stat.f_iosize; 1678 auio.uio_iov = &aiov; 1679 auio.uio_iovcnt = 1; 1680 aiov.iov_base = 0; 1681 aiov.iov_len = MAXBSIZE; 1682 auio.uio_resid = MAXBSIZE; 1683 auio.uio_offset = trunc_page(off); 1684 auio.uio_segflg = UIO_NOCOPY; 1685 auio.uio_rw = UIO_READ; 1686 auio.uio_procp = p; 1687 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p); 1688 error = VOP_READ(vp, &auio, IO_VMIO | ((MAXBSIZE / bsize) << 16), 1689 p->p_ucred); 1690 VOP_UNLOCK(vp, 0, p); 1691 vm_page_flag_clear(pg, PG_ZERO); 1692 vm_page_io_finish(pg); 1693 if (error) { 1694 vm_page_unwire(pg, 0); 1695 /* 1696 * See if anyone else might know about this page. 1697 * If not and it is not valid, then free it. 1698 */ 1699 if (pg->wire_count == 0 && pg->valid == 0 && 1700 pg->busy == 0 && !(pg->flags & PG_BUSY) && 1701 pg->hold_count == 0) { 1702 vm_page_busy(pg); 1703 vm_page_free(pg); 1704 } 1705 sbunlock(&so->so_snd); 1706 goto done; 1707 } 1708 } 1709 1710 /* 1711 * Allocate a kernel virtual page and insert the physical page 1712 * into it. 1713 */ 1714 1715 sf = sf_buf_alloc(); 1716 sf->m = pg; 1717 pmap_qenter(sf->kva, &pg, 1); 1718 /* 1719 * Get an mbuf header and set it up as having external storage. 1720 */ 1721 MGETHDR(m, M_TRYWAIT, MT_DATA); 1722 if (m == NULL) { 1723 error = ENOBUFS; 1724 sf_buf_free((void *)sf->kva, NULL); 1725 goto done; 1726 } 1727 /* 1728 * Setup external storage for mbuf. 1729 */ 1730 MEXTADD(m, sf->kva, PAGE_SIZE, sf_buf_free, NULL, M_RDONLY, 1731 EXT_SFBUF); 1732 m->m_data = (char *) sf->kva + pgoff; 1733 m->m_pkthdr.len = m->m_len = xfsize; 1734 /* 1735 * Add the buffer to the socket buffer chain. 1736 */ 1737 s = splnet(); 1738retry_space: 1739 /* 1740 * Make sure that the socket is still able to take more data. 1741 * CANTSENDMORE being true usually means that the connection 1742 * was closed. so_error is true when an error was sensed after 1743 * a previous send. 1744 * The state is checked after the page mapping and buffer 1745 * allocation above since those operations may block and make 1746 * any socket checks stale. From this point forward, nothing 1747 * blocks before the pru_send (or more accurately, any blocking 1748 * results in a loop back to here to re-check). 1749 */ 1750 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) { 1751 if (so->so_state & SS_CANTSENDMORE) { 1752 error = EPIPE; 1753 } else { 1754 error = so->so_error; 1755 so->so_error = 0; 1756 } 1757 m_freem(m); 1758 sbunlock(&so->so_snd); 1759 splx(s); 1760 goto done; 1761 } 1762 /* 1763 * Wait for socket space to become available. We do this just 1764 * after checking the connection state above in order to avoid 1765 * a race condition with sbwait(). 1766 */ 1767 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) { 1768 if (so->so_state & SS_NBIO) { 1769 m_freem(m); 1770 sbunlock(&so->so_snd); 1771 splx(s); 1772 error = EAGAIN; 1773 goto done; 1774 } 1775 error = sbwait(&so->so_snd); 1776 /* 1777 * An error from sbwait usually indicates that we've 1778 * been interrupted by a signal. If we've sent anything 1779 * then return bytes sent, otherwise return the error. 1780 */ 1781 if (error) { 1782 m_freem(m); 1783 sbunlock(&so->so_snd); 1784 splx(s); 1785 goto done; 1786 } 1787 goto retry_space; 1788 } 1789 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, p); 1790 splx(s); 1791 if (error) { 1792 sbunlock(&so->so_snd); 1793 goto done; 1794 } 1795 } 1796 sbunlock(&so->so_snd); 1797 1798 /* 1799 * Send trailers. Wimp out and use writev(2). 1800 */ 1801 if (uap->hdtr != NULL && hdtr.trailers != NULL) { 1802 nuap.fd = uap->s; 1803 nuap.iovp = hdtr.trailers; 1804 nuap.iovcnt = hdtr.trl_cnt; 1805 error = writev(p, &nuap); 1806 if (error) 1807 goto done; 1808 sbytes += p->p_retval[0]; 1809 } 1810 1811done: 1812 if (uap->sbytes != NULL) { 1813 copyout(&sbytes, uap->sbytes, sizeof(off_t)); 1814 } 1815 if (vp) 1816 vrele(vp); 1817 if (fp) 1818 fdrop(fp, p); 1819 return (error); 1820} 1821