1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/t_lock.h> 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/buf.h> 32 #include <sys/conf.h> 33 #include <sys/cred.h> 34 #include <sys/kmem.h> 35 #include <sys/sysmacros.h> 36 #include <sys/vfs.h> 37 #include <sys/vnode.h> 38 #include <sys/debug.h> 39 #include <sys/errno.h> 40 #include <sys/time.h> 41 #include <sys/file.h> 42 #include <sys/user.h> 43 #include <sys/stream.h> 44 #include <sys/strsubr.h> 45 #include <sys/strsun.h> 46 #include <sys/sunddi.h> 47 #include <sys/esunddi.h> 48 #include <sys/flock.h> 49 #include <sys/modctl.h> 50 #include <sys/cmn_err.h> 51 #include <sys/vmsystm.h> 52 #include <sys/policy.h> 53 54 #include <sys/socket.h> 55 #include <sys/socketvar.h> 56 57 #include <sys/isa_defs.h> 58 #include <sys/inttypes.h> 59 #include <sys/systm.h> 60 #include <sys/cpuvar.h> 61 #include <sys/filio.h> 62 #include <sys/sendfile.h> 63 #include <sys/ddi.h> 64 #include <vm/seg.h> 65 #include <vm/seg_map.h> 66 #include <vm/seg_kpm.h> 67 68 #include <fs/sockfs/nl7c.h> 69 #include <fs/sockfs/sockcommon.h> 70 #include <fs/sockfs/socktpi.h> 71 72 #ifdef SOCK_TEST 73 int do_useracc = 1; /* Controlled by setting SO_DEBUG to 4 */ 74 #else 75 #define do_useracc 1 76 #endif /* SOCK_TEST */ 77 78 extern int xnet_truncate_print; 79 80 /* 81 * Note: DEF_IOV_MAX is defined and used as it is in "fs/vncalls.c" 82 * as there isn't a formal definition of IOV_MAX ??? 83 */ 84 #define MSG_MAXIOVLEN 16 85 86 /* 87 * Kernel component of socket creation. 88 * 89 * The socket library determines which version number to use. 90 * First the library calls this with a NULL devpath. If this fails 91 * to find a transport (using solookup) the library will look in /etc/netconfig 92 * for the appropriate transport. If one is found it will pass in the 93 * devpath for the kernel to use. 94 */ 95 int 96 so_socket(int family, int type, int protocol, char *devpath, int version) 97 { 98 struct sonode *so; 99 vnode_t *vp; 100 struct file *fp; 101 int fd; 102 int error; 103 104 if (devpath != NULL) { 105 char *buf; 106 size_t kdevpathlen = 0; 107 108 buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 109 if ((error = copyinstr(devpath, buf, 110 MAXPATHLEN, &kdevpathlen)) != 0) { 111 kmem_free(buf, MAXPATHLEN); 112 return (set_errno(error)); 113 } 114 so = socket_create(family, type, protocol, buf, NULL, 115 SOCKET_SLEEP, version, CRED(), &error); 116 kmem_free(buf, MAXPATHLEN); 117 } else { 118 so = socket_create(family, type, protocol, NULL, NULL, 119 SOCKET_SLEEP, version, CRED(), &error); 120 } 121 if (so == NULL) 122 return (set_errno(error)); 123 124 /* Allocate a file descriptor for the socket */ 125 vp = SOTOV(so); 126 if (error = falloc(vp, FWRITE|FREAD, &fp, &fd)) { 127 (void) socket_close(so, 0, CRED()); 128 socket_destroy(so); 129 return (set_errno(error)); 130 } 131 132 /* 133 * Now fill in the entries that falloc reserved 134 */ 135 mutex_exit(&fp->f_tlock); 136 setf(fd, fp); 137 138 return (fd); 139 } 140 141 /* 142 * Map from a file descriptor to a socket node. 143 * Returns with the file descriptor held i.e. the caller has to 144 * use releasef when done with the file descriptor. 145 */ 146 struct sonode * 147 getsonode(int sock, int *errorp, file_t **fpp) 148 { 149 file_t *fp; 150 vnode_t *vp; 151 struct sonode *so; 152 153 if ((fp = getf(sock)) == NULL) { 154 *errorp = EBADF; 155 eprintline(*errorp); 156 return (NULL); 157 } 158 vp = fp->f_vnode; 159 /* Check if it is a socket */ 160 if (vp->v_type != VSOCK) { 161 releasef(sock); 162 *errorp = ENOTSOCK; 163 eprintline(*errorp); 164 return (NULL); 165 } 166 /* 167 * Use the stream head to find the real socket vnode. 168 * This is needed when namefs sits above sockfs. 169 */ 170 if (vp->v_stream) { 171 ASSERT(vp->v_stream->sd_vnode); 172 vp = vp->v_stream->sd_vnode; 173 174 so = VTOSO(vp); 175 if (so->so_version == SOV_STREAM) { 176 releasef(sock); 177 *errorp = ENOTSOCK; 178 eprintsoline(so, *errorp); 179 return (NULL); 180 } 181 } else { 182 so = VTOSO(vp); 183 } 184 if (fpp) 185 *fpp = fp; 186 return (so); 187 } 188 189 /* 190 * Allocate and copyin a sockaddr. 191 * Ensures NULL termination for AF_UNIX addresses by extending them 192 * with one NULL byte if need be. Verifies that the length is not 193 * excessive to prevent an application from consuming all of kernel 194 * memory. Returns NULL when an error occurred. 195 */ 196 static struct sockaddr * 197 copyin_name(struct sonode *so, struct sockaddr *name, socklen_t *namelenp, 198 int *errorp) 199 { 200 char *faddr; 201 size_t namelen = (size_t)*namelenp; 202 203 ASSERT(namelen != 0); 204 if (namelen > SO_MAXARGSIZE) { 205 *errorp = EINVAL; 206 eprintsoline(so, *errorp); 207 return (NULL); 208 } 209 210 faddr = (char *)kmem_alloc(namelen, KM_SLEEP); 211 if (copyin(name, faddr, namelen)) { 212 kmem_free(faddr, namelen); 213 *errorp = EFAULT; 214 eprintsoline(so, *errorp); 215 return (NULL); 216 } 217 218 /* 219 * Add space for NULL termination if needed. 220 * Do a quick check if the last byte is NUL. 221 */ 222 if (so->so_family == AF_UNIX && faddr[namelen - 1] != '\0') { 223 /* Check if there is any NULL termination */ 224 size_t i; 225 int foundnull = 0; 226 227 for (i = sizeof (name->sa_family); i < namelen; i++) { 228 if (faddr[i] == '\0') { 229 foundnull = 1; 230 break; 231 } 232 } 233 if (!foundnull) { 234 /* Add extra byte for NUL padding */ 235 char *nfaddr; 236 237 nfaddr = (char *)kmem_alloc(namelen + 1, KM_SLEEP); 238 bcopy(faddr, nfaddr, namelen); 239 kmem_free(faddr, namelen); 240 241 /* NUL terminate */ 242 nfaddr[namelen] = '\0'; 243 namelen++; 244 ASSERT((socklen_t)namelen == namelen); 245 *namelenp = (socklen_t)namelen; 246 faddr = nfaddr; 247 } 248 } 249 return ((struct sockaddr *)faddr); 250 } 251 252 /* 253 * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL. 254 */ 255 static int 256 copyout_arg(void *uaddr, socklen_t ulen, void *ulenp, 257 void *kaddr, socklen_t klen) 258 { 259 if (uaddr != NULL) { 260 if (ulen > klen) 261 ulen = klen; 262 263 if (ulen != 0) { 264 if (copyout(kaddr, uaddr, ulen)) 265 return (EFAULT); 266 } 267 } else 268 ulen = 0; 269 270 if (ulenp != NULL) { 271 if (copyout(&ulen, ulenp, sizeof (ulen))) 272 return (EFAULT); 273 } 274 return (0); 275 } 276 277 /* 278 * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL. 279 * If klen is greater than ulen it still uses the non-truncated 280 * klen to update ulenp. 281 */ 282 static int 283 copyout_name(void *uaddr, socklen_t ulen, void *ulenp, 284 void *kaddr, socklen_t klen) 285 { 286 if (uaddr != NULL) { 287 if (ulen >= klen) 288 ulen = klen; 289 else if (ulen != 0 && xnet_truncate_print) { 290 printf("sockfs: truncating copyout of address using " 291 "XNET semantics for pid = %d. Lengths %d, %d\n", 292 curproc->p_pid, klen, ulen); 293 } 294 295 if (ulen != 0) { 296 if (copyout(kaddr, uaddr, ulen)) 297 return (EFAULT); 298 } else 299 klen = 0; 300 } else 301 klen = 0; 302 303 if (ulenp != NULL) { 304 if (copyout(&klen, ulenp, sizeof (klen))) 305 return (EFAULT); 306 } 307 return (0); 308 } 309 310 /* 311 * The socketpair() code in libsocket creates two sockets (using 312 * the /etc/netconfig fallback if needed) before calling this routine 313 * to connect the two sockets together. 314 * 315 * For a SOCK_STREAM socketpair a listener is needed - in that case this 316 * routine will create a new file descriptor as part of accepting the 317 * connection. The library socketpair() will check if svs[2] has changed 318 * in which case it will close the changed fd. 319 * 320 * Note that this code could use the TPI feature of accepting the connection 321 * on the listening endpoint. However, that would require significant changes 322 * to soaccept. 323 */ 324 int 325 so_socketpair(int sv[2]) 326 { 327 int svs[2]; 328 struct sonode *so1, *so2; 329 int error; 330 struct sockaddr_ux *name; 331 size_t namelen; 332 sotpi_info_t *sti1; 333 sotpi_info_t *sti2; 334 335 dprint(1, ("so_socketpair(%p)\n", (void *)sv)); 336 337 error = useracc(sv, sizeof (svs), B_WRITE); 338 if (error && do_useracc) 339 return (set_errno(EFAULT)); 340 341 if (copyin(sv, svs, sizeof (svs))) 342 return (set_errno(EFAULT)); 343 344 if ((so1 = getsonode(svs[0], &error, NULL)) == NULL) 345 return (set_errno(error)); 346 347 if ((so2 = getsonode(svs[1], &error, NULL)) == NULL) { 348 releasef(svs[0]); 349 return (set_errno(error)); 350 } 351 352 if (so1->so_family != AF_UNIX || so2->so_family != AF_UNIX) { 353 error = EOPNOTSUPP; 354 goto done; 355 } 356 357 sti1 = SOTOTPI(so1); 358 sti2 = SOTOTPI(so2); 359 360 /* 361 * The code below makes assumptions about the "sockfs" implementation. 362 * So make sure that the correct implementation is really used. 363 */ 364 ASSERT(so1->so_ops == &sotpi_sonodeops); 365 ASSERT(so2->so_ops == &sotpi_sonodeops); 366 367 if (so1->so_type == SOCK_DGRAM) { 368 /* 369 * Bind both sockets and connect them with each other. 370 * Need to allocate name/namelen for soconnect. 371 */ 372 error = socket_bind(so1, NULL, 0, _SOBIND_UNSPEC, CRED()); 373 if (error) { 374 eprintsoline(so1, error); 375 goto done; 376 } 377 error = socket_bind(so2, NULL, 0, _SOBIND_UNSPEC, CRED()); 378 if (error) { 379 eprintsoline(so2, error); 380 goto done; 381 } 382 namelen = sizeof (struct sockaddr_ux); 383 name = kmem_alloc(namelen, KM_SLEEP); 384 name->sou_family = AF_UNIX; 385 name->sou_addr = sti2->sti_ux_laddr; 386 error = socket_connect(so1, 387 (struct sockaddr *)name, 388 (socklen_t)namelen, 389 0, _SOCONNECT_NOXLATE, CRED()); 390 if (error) { 391 kmem_free(name, namelen); 392 eprintsoline(so1, error); 393 goto done; 394 } 395 name->sou_addr = sti1->sti_ux_laddr; 396 error = socket_connect(so2, 397 (struct sockaddr *)name, 398 (socklen_t)namelen, 399 0, _SOCONNECT_NOXLATE, CRED()); 400 kmem_free(name, namelen); 401 if (error) { 402 eprintsoline(so2, error); 403 goto done; 404 } 405 releasef(svs[0]); 406 releasef(svs[1]); 407 } else { 408 /* 409 * Bind both sockets, with so1 being a listener. 410 * Connect so2 to so1 - nonblocking to avoid waiting for 411 * soaccept to complete. 412 * Accept a connection on so1. Pass out the new fd as sv[0]. 413 * The library will detect the changed fd and close 414 * the original one. 415 */ 416 struct sonode *nso; 417 struct vnode *nvp; 418 struct file *nfp; 419 int nfd; 420 421 /* 422 * We could simply call socket_listen() here (which would do the 423 * binding automatically) if the code didn't rely on passing 424 * _SOBIND_NOXLATE to the TPI implementation of socket_bind(). 425 */ 426 error = socket_bind(so1, NULL, 0, _SOBIND_UNSPEC| 427 _SOBIND_NOXLATE|_SOBIND_LISTEN|_SOBIND_SOCKETPAIR, 428 CRED()); 429 if (error) { 430 eprintsoline(so1, error); 431 goto done; 432 } 433 error = socket_bind(so2, NULL, 0, _SOBIND_UNSPEC, CRED()); 434 if (error) { 435 eprintsoline(so2, error); 436 goto done; 437 } 438 439 namelen = sizeof (struct sockaddr_ux); 440 name = kmem_alloc(namelen, KM_SLEEP); 441 name->sou_family = AF_UNIX; 442 name->sou_addr = sti1->sti_ux_laddr; 443 error = socket_connect(so2, 444 (struct sockaddr *)name, 445 (socklen_t)namelen, 446 FNONBLOCK, _SOCONNECT_NOXLATE, CRED()); 447 kmem_free(name, namelen); 448 if (error) { 449 if (error != EINPROGRESS) { 450 eprintsoline(so2, error); goto done; 451 } 452 } 453 454 error = socket_accept(so1, 0, CRED(), &nso); 455 if (error) { 456 eprintsoline(so1, error); 457 goto done; 458 } 459 460 /* wait for so2 being SS_CONNECTED ignoring signals */ 461 mutex_enter(&so2->so_lock); 462 error = sowaitconnected(so2, 0, 1); 463 mutex_exit(&so2->so_lock); 464 if (error != 0) { 465 (void) socket_close(nso, 0, CRED()); 466 socket_destroy(nso); 467 eprintsoline(so2, error); 468 goto done; 469 } 470 471 nvp = SOTOV(nso); 472 if (error = falloc(nvp, FWRITE|FREAD, &nfp, &nfd)) { 473 (void) socket_close(nso, 0, CRED()); 474 socket_destroy(nso); 475 eprintsoline(nso, error); 476 goto done; 477 } 478 /* 479 * fill in the entries that falloc reserved 480 */ 481 mutex_exit(&nfp->f_tlock); 482 setf(nfd, nfp); 483 484 releasef(svs[0]); 485 releasef(svs[1]); 486 svs[0] = nfd; 487 488 /* 489 * The socketpair library routine will close the original 490 * svs[0] when this code passes out a different file 491 * descriptor. 492 */ 493 if (copyout(svs, sv, sizeof (svs))) { 494 (void) closeandsetf(nfd, NULL); 495 eprintline(EFAULT); 496 return (set_errno(EFAULT)); 497 } 498 } 499 return (0); 500 501 done: 502 releasef(svs[0]); 503 releasef(svs[1]); 504 return (set_errno(error)); 505 } 506 507 int 508 bind(int sock, struct sockaddr *name, socklen_t namelen, int version) 509 { 510 struct sonode *so; 511 int error; 512 513 dprint(1, ("bind(%d, %p, %d)\n", 514 sock, (void *)name, namelen)); 515 516 if ((so = getsonode(sock, &error, NULL)) == NULL) 517 return (set_errno(error)); 518 519 /* Allocate and copyin name */ 520 /* 521 * X/Open test does not expect EFAULT with NULL name and non-zero 522 * namelen. 523 */ 524 if (name != NULL && namelen != 0) { 525 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 526 name = copyin_name(so, name, &namelen, &error); 527 if (name == NULL) { 528 releasef(sock); 529 return (set_errno(error)); 530 } 531 } else { 532 name = NULL; 533 namelen = 0; 534 } 535 536 switch (version) { 537 default: 538 error = socket_bind(so, name, namelen, 0, CRED()); 539 break; 540 case SOV_XPG4_2: 541 error = socket_bind(so, name, namelen, _SOBIND_XPG4_2, CRED()); 542 break; 543 case SOV_SOCKBSD: 544 error = socket_bind(so, name, namelen, _SOBIND_SOCKBSD, CRED()); 545 break; 546 } 547 done: 548 releasef(sock); 549 if (name != NULL) 550 kmem_free(name, (size_t)namelen); 551 552 if (error) 553 return (set_errno(error)); 554 return (0); 555 } 556 557 /* ARGSUSED2 */ 558 int 559 listen(int sock, int backlog, int version) 560 { 561 struct sonode *so; 562 int error; 563 564 dprint(1, ("listen(%d, %d)\n", 565 sock, backlog)); 566 567 if ((so = getsonode(sock, &error, NULL)) == NULL) 568 return (set_errno(error)); 569 570 error = socket_listen(so, backlog, CRED()); 571 572 releasef(sock); 573 if (error) 574 return (set_errno(error)); 575 return (0); 576 } 577 578 /*ARGSUSED3*/ 579 int 580 accept(int sock, struct sockaddr *name, socklen_t *namelenp, int version) 581 { 582 struct sonode *so; 583 file_t *fp; 584 int error; 585 socklen_t namelen; 586 struct sonode *nso; 587 struct vnode *nvp; 588 struct file *nfp; 589 int nfd; 590 struct sockaddr *addrp; 591 socklen_t addrlen; 592 593 dprint(1, ("accept(%d, %p, %p)\n", 594 sock, (void *)name, (void *)namelenp)); 595 596 if ((so = getsonode(sock, &error, &fp)) == NULL) 597 return (set_errno(error)); 598 599 if (name != NULL) { 600 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 601 if (copyin(namelenp, &namelen, sizeof (namelen))) { 602 releasef(sock); 603 return (set_errno(EFAULT)); 604 } 605 if (namelen != 0) { 606 error = useracc(name, (size_t)namelen, B_WRITE); 607 if (error && do_useracc) { 608 releasef(sock); 609 return (set_errno(EFAULT)); 610 } 611 } else 612 name = NULL; 613 } else { 614 namelen = 0; 615 } 616 617 /* 618 * Allocate the user fd before socket_accept() in order to 619 * catch EMFILE errors before calling socket_accept(). 620 */ 621 if ((nfd = ufalloc(0)) == -1) { 622 eprintsoline(so, EMFILE); 623 releasef(sock); 624 return (set_errno(EMFILE)); 625 } 626 error = socket_accept(so, fp->f_flag, CRED(), &nso); 627 releasef(sock); 628 if (error) { 629 setf(nfd, NULL); 630 return (set_errno(error)); 631 } 632 633 nvp = SOTOV(nso); 634 635 ASSERT(MUTEX_NOT_HELD(&nso->so_lock)); 636 if (namelen != 0) { 637 addrlen = so->so_max_addr_len; 638 addrp = (struct sockaddr *)kmem_alloc(addrlen, KM_SLEEP); 639 640 if ((error = socket_getpeername(nso, (struct sockaddr *)addrp, 641 &addrlen, B_TRUE, CRED())) == 0) { 642 error = copyout_name(name, namelen, namelenp, 643 addrp, addrlen); 644 } else { 645 ASSERT(error == EINVAL || error == ENOTCONN); 646 error = ECONNABORTED; 647 } 648 kmem_free(addrp, so->so_max_addr_len); 649 } 650 651 if (error) { 652 setf(nfd, NULL); 653 (void) socket_close(nso, 0, CRED()); 654 socket_destroy(nso); 655 return (set_errno(error)); 656 } 657 if (error = falloc(NULL, FWRITE|FREAD, &nfp, NULL)) { 658 setf(nfd, NULL); 659 (void) socket_close(nso, 0, CRED()); 660 socket_destroy(nso); 661 eprintsoline(so, error); 662 return (set_errno(error)); 663 } 664 /* 665 * fill in the entries that falloc reserved 666 */ 667 nfp->f_vnode = nvp; 668 mutex_exit(&nfp->f_tlock); 669 setf(nfd, nfp); 670 671 /* 672 * Copy FNDELAY and FNONBLOCK from listener to acceptor 673 */ 674 if (so->so_state & (SS_NDELAY|SS_NONBLOCK)) { 675 uint_t oflag = nfp->f_flag; 676 int arg = 0; 677 678 if (so->so_state & SS_NONBLOCK) 679 arg |= FNONBLOCK; 680 else if (so->so_state & SS_NDELAY) 681 arg |= FNDELAY; 682 683 /* 684 * This code is a simplification of the F_SETFL code in fcntl() 685 * Ignore any errors from VOP_SETFL. 686 */ 687 if ((error = VOP_SETFL(nvp, oflag, arg, nfp->f_cred, NULL)) 688 != 0) { 689 eprintsoline(so, error); 690 error = 0; 691 } else { 692 mutex_enter(&nfp->f_tlock); 693 nfp->f_flag &= ~FMASK | (FREAD|FWRITE); 694 nfp->f_flag |= arg; 695 mutex_exit(&nfp->f_tlock); 696 } 697 } 698 return (nfd); 699 } 700 701 int 702 connect(int sock, struct sockaddr *name, socklen_t namelen, int version) 703 { 704 struct sonode *so; 705 file_t *fp; 706 int error; 707 708 dprint(1, ("connect(%d, %p, %d)\n", 709 sock, (void *)name, namelen)); 710 711 if ((so = getsonode(sock, &error, &fp)) == NULL) 712 return (set_errno(error)); 713 714 /* Allocate and copyin name */ 715 if (namelen != 0) { 716 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 717 name = copyin_name(so, name, &namelen, &error); 718 if (name == NULL) { 719 releasef(sock); 720 return (set_errno(error)); 721 } 722 } else 723 name = NULL; 724 725 error = socket_connect(so, name, namelen, fp->f_flag, 726 (version != SOV_XPG4_2) ? 0 : _SOCONNECT_XPG4_2, CRED()); 727 releasef(sock); 728 if (name) 729 kmem_free(name, (size_t)namelen); 730 if (error) 731 return (set_errno(error)); 732 return (0); 733 } 734 735 /*ARGSUSED2*/ 736 int 737 shutdown(int sock, int how, int version) 738 { 739 struct sonode *so; 740 int error; 741 742 dprint(1, ("shutdown(%d, %d)\n", 743 sock, how)); 744 745 if ((so = getsonode(sock, &error, NULL)) == NULL) 746 return (set_errno(error)); 747 748 error = socket_shutdown(so, how, CRED()); 749 750 releasef(sock); 751 if (error) 752 return (set_errno(error)); 753 return (0); 754 } 755 756 /* 757 * Common receive routine. 758 */ 759 static ssize_t 760 recvit(int sock, 761 struct nmsghdr *msg, 762 struct uio *uiop, 763 int flags, 764 socklen_t *namelenp, 765 socklen_t *controllenp, 766 int *flagsp) 767 { 768 struct sonode *so; 769 file_t *fp; 770 void *name; 771 socklen_t namelen; 772 void *control; 773 socklen_t controllen; 774 ssize_t len; 775 int error; 776 777 if ((so = getsonode(sock, &error, &fp)) == NULL) 778 return (set_errno(error)); 779 780 len = uiop->uio_resid; 781 uiop->uio_fmode = fp->f_flag; 782 uiop->uio_extflg = UIO_COPY_CACHED; 783 784 name = msg->msg_name; 785 namelen = msg->msg_namelen; 786 control = msg->msg_control; 787 controllen = msg->msg_controllen; 788 789 msg->msg_flags = flags & (MSG_OOB | MSG_PEEK | MSG_WAITALL | 790 MSG_DONTWAIT | MSG_XPG4_2); 791 792 error = socket_recvmsg(so, msg, uiop, CRED()); 793 if (error) { 794 releasef(sock); 795 return (set_errno(error)); 796 } 797 lwp_stat_update(LWP_STAT_MSGRCV, 1); 798 releasef(sock); 799 800 error = copyout_name(name, namelen, namelenp, 801 msg->msg_name, msg->msg_namelen); 802 if (error) 803 goto err; 804 805 if (flagsp != NULL) { 806 /* 807 * Clear internal flag. 808 */ 809 msg->msg_flags &= ~MSG_XPG4_2; 810 811 /* 812 * Determine MSG_CTRUNC. sorecvmsg sets MSG_CTRUNC only 813 * when controllen is zero and there is control data to 814 * copy out. 815 */ 816 if (controllen != 0 && 817 (msg->msg_controllen > controllen || control == NULL)) { 818 dprint(1, ("recvit: CTRUNC %d %d %p\n", 819 msg->msg_controllen, controllen, control)); 820 821 msg->msg_flags |= MSG_CTRUNC; 822 } 823 if (copyout(&msg->msg_flags, flagsp, 824 sizeof (msg->msg_flags))) { 825 error = EFAULT; 826 goto err; 827 } 828 } 829 /* 830 * Note: This MUST be done last. There can be no "goto err" after this 831 * point since it could make so_closefds run twice on some part 832 * of the file descriptor array. 833 */ 834 if (controllen != 0) { 835 if (!(flags & MSG_XPG4_2)) { 836 /* 837 * Good old msg_accrights can only return a multiple 838 * of 4 bytes. 839 */ 840 controllen &= ~((int)sizeof (uint32_t) - 1); 841 } 842 error = copyout_arg(control, controllen, controllenp, 843 msg->msg_control, msg->msg_controllen); 844 if (error) 845 goto err; 846 847 if (msg->msg_controllen > controllen || control == NULL) { 848 if (control == NULL) 849 controllen = 0; 850 so_closefds(msg->msg_control, msg->msg_controllen, 851 !(flags & MSG_XPG4_2), controllen); 852 } 853 } 854 if (msg->msg_namelen != 0) 855 kmem_free(msg->msg_name, (size_t)msg->msg_namelen); 856 if (msg->msg_controllen != 0) 857 kmem_free(msg->msg_control, (size_t)msg->msg_controllen); 858 return (len - uiop->uio_resid); 859 860 err: 861 /* 862 * If we fail and the control part contains file descriptors 863 * we have to close the fd's. 864 */ 865 if (msg->msg_controllen != 0) 866 so_closefds(msg->msg_control, msg->msg_controllen, 867 !(flags & MSG_XPG4_2), 0); 868 if (msg->msg_namelen != 0) 869 kmem_free(msg->msg_name, (size_t)msg->msg_namelen); 870 if (msg->msg_controllen != 0) 871 kmem_free(msg->msg_control, (size_t)msg->msg_controllen); 872 return (set_errno(error)); 873 } 874 875 /* 876 * Native system call 877 */ 878 ssize_t 879 recv(int sock, void *buffer, size_t len, int flags) 880 { 881 struct nmsghdr lmsg; 882 struct uio auio; 883 struct iovec aiov[1]; 884 885 dprint(1, ("recv(%d, %p, %ld, %d)\n", 886 sock, buffer, len, flags)); 887 888 if ((ssize_t)len < 0) { 889 return (set_errno(EINVAL)); 890 } 891 892 aiov[0].iov_base = buffer; 893 aiov[0].iov_len = len; 894 auio.uio_loffset = 0; 895 auio.uio_iov = aiov; 896 auio.uio_iovcnt = 1; 897 auio.uio_resid = len; 898 auio.uio_segflg = UIO_USERSPACE; 899 auio.uio_limit = 0; 900 901 lmsg.msg_namelen = 0; 902 lmsg.msg_controllen = 0; 903 lmsg.msg_flags = 0; 904 return (recvit(sock, &lmsg, &auio, flags, NULL, NULL, NULL)); 905 } 906 907 ssize_t 908 recvfrom(int sock, void *buffer, size_t len, int flags, 909 struct sockaddr *name, socklen_t *namelenp) 910 { 911 struct nmsghdr lmsg; 912 struct uio auio; 913 struct iovec aiov[1]; 914 915 dprint(1, ("recvfrom(%d, %p, %ld, %d, %p, %p)\n", 916 sock, buffer, len, flags, (void *)name, (void *)namelenp)); 917 918 if ((ssize_t)len < 0) { 919 return (set_errno(EINVAL)); 920 } 921 922 aiov[0].iov_base = buffer; 923 aiov[0].iov_len = len; 924 auio.uio_loffset = 0; 925 auio.uio_iov = aiov; 926 auio.uio_iovcnt = 1; 927 auio.uio_resid = len; 928 auio.uio_segflg = UIO_USERSPACE; 929 auio.uio_limit = 0; 930 931 lmsg.msg_name = (char *)name; 932 if (namelenp != NULL) { 933 if (copyin(namelenp, &lmsg.msg_namelen, 934 sizeof (lmsg.msg_namelen))) 935 return (set_errno(EFAULT)); 936 } else { 937 lmsg.msg_namelen = 0; 938 } 939 lmsg.msg_controllen = 0; 940 lmsg.msg_flags = 0; 941 942 return (recvit(sock, &lmsg, &auio, flags, namelenp, NULL, NULL)); 943 } 944 945 /* 946 * Uses the MSG_XPG4_2 flag to determine if the caller is using 947 * struct omsghdr or struct nmsghdr. 948 */ 949 ssize_t 950 recvmsg(int sock, struct nmsghdr *msg, int flags) 951 { 952 STRUCT_DECL(nmsghdr, u_lmsg); 953 STRUCT_HANDLE(nmsghdr, umsgptr); 954 struct nmsghdr lmsg; 955 struct uio auio; 956 struct iovec aiov[MSG_MAXIOVLEN]; 957 int iovcnt; 958 ssize_t len; 959 int i; 960 int *flagsp; 961 model_t model; 962 963 dprint(1, ("recvmsg(%d, %p, %d)\n", 964 sock, (void *)msg, flags)); 965 966 model = get_udatamodel(); 967 STRUCT_INIT(u_lmsg, model); 968 STRUCT_SET_HANDLE(umsgptr, model, msg); 969 970 if (flags & MSG_XPG4_2) { 971 if (copyin(msg, STRUCT_BUF(u_lmsg), STRUCT_SIZE(u_lmsg))) 972 return (set_errno(EFAULT)); 973 flagsp = STRUCT_FADDR(umsgptr, msg_flags); 974 } else { 975 /* 976 * Assumes that nmsghdr and omsghdr are identically shaped 977 * except for the added msg_flags field. 978 */ 979 if (copyin(msg, STRUCT_BUF(u_lmsg), 980 SIZEOF_STRUCT(omsghdr, model))) 981 return (set_errno(EFAULT)); 982 STRUCT_FSET(u_lmsg, msg_flags, 0); 983 flagsp = NULL; 984 } 985 986 /* 987 * Code below us will kmem_alloc memory and hang it 988 * off msg_control and msg_name fields. This forces 989 * us to copy the structure to its native form. 990 */ 991 lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name); 992 lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen); 993 lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov); 994 lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen); 995 lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control); 996 lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen); 997 lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags); 998 999 iovcnt = lmsg.msg_iovlen; 1000 1001 if (iovcnt <= 0 || iovcnt > MSG_MAXIOVLEN) { 1002 return (set_errno(EMSGSIZE)); 1003 } 1004 1005 #ifdef _SYSCALL32_IMPL 1006 /* 1007 * 32-bit callers need to have their iovec expanded, while ensuring 1008 * that they can't move more than 2Gbytes of data in a single call. 1009 */ 1010 if (model == DATAMODEL_ILP32) { 1011 struct iovec32 aiov32[MSG_MAXIOVLEN]; 1012 ssize32_t count32; 1013 1014 if (copyin((struct iovec32 *)lmsg.msg_iov, aiov32, 1015 iovcnt * sizeof (struct iovec32))) 1016 return (set_errno(EFAULT)); 1017 1018 count32 = 0; 1019 for (i = 0; i < iovcnt; i++) { 1020 ssize32_t iovlen32; 1021 1022 iovlen32 = aiov32[i].iov_len; 1023 count32 += iovlen32; 1024 if (iovlen32 < 0 || count32 < 0) 1025 return (set_errno(EINVAL)); 1026 aiov[i].iov_len = iovlen32; 1027 aiov[i].iov_base = 1028 (caddr_t)(uintptr_t)aiov32[i].iov_base; 1029 } 1030 } else 1031 #endif /* _SYSCALL32_IMPL */ 1032 if (copyin(lmsg.msg_iov, aiov, iovcnt * sizeof (struct iovec))) { 1033 return (set_errno(EFAULT)); 1034 } 1035 len = 0; 1036 for (i = 0; i < iovcnt; i++) { 1037 ssize_t iovlen = aiov[i].iov_len; 1038 len += iovlen; 1039 if (iovlen < 0 || len < 0) { 1040 return (set_errno(EINVAL)); 1041 } 1042 } 1043 auio.uio_loffset = 0; 1044 auio.uio_iov = aiov; 1045 auio.uio_iovcnt = iovcnt; 1046 auio.uio_resid = len; 1047 auio.uio_segflg = UIO_USERSPACE; 1048 auio.uio_limit = 0; 1049 1050 if (lmsg.msg_control != NULL && 1051 (do_useracc == 0 || 1052 useracc(lmsg.msg_control, lmsg.msg_controllen, 1053 B_WRITE) != 0)) { 1054 return (set_errno(EFAULT)); 1055 } 1056 1057 return (recvit(sock, &lmsg, &auio, flags, 1058 STRUCT_FADDR(umsgptr, msg_namelen), 1059 STRUCT_FADDR(umsgptr, msg_controllen), flagsp)); 1060 } 1061 1062 /* 1063 * Common send function. 1064 */ 1065 static ssize_t 1066 sendit(int sock, struct nmsghdr *msg, struct uio *uiop, int flags) 1067 { 1068 struct sonode *so; 1069 file_t *fp; 1070 void *name; 1071 socklen_t namelen; 1072 void *control; 1073 socklen_t controllen; 1074 ssize_t len; 1075 int error; 1076 1077 if ((so = getsonode(sock, &error, &fp)) == NULL) 1078 return (set_errno(error)); 1079 1080 uiop->uio_fmode = fp->f_flag; 1081 1082 if (so->so_family == AF_UNIX) 1083 uiop->uio_extflg = UIO_COPY_CACHED; 1084 else 1085 uiop->uio_extflg = UIO_COPY_DEFAULT; 1086 1087 /* Allocate and copyin name and control */ 1088 name = msg->msg_name; 1089 namelen = msg->msg_namelen; 1090 if (name != NULL && namelen != 0) { 1091 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 1092 name = copyin_name(so, 1093 (struct sockaddr *)name, 1094 &namelen, &error); 1095 if (name == NULL) 1096 goto done3; 1097 /* copyin_name null terminates addresses for AF_UNIX */ 1098 msg->msg_namelen = namelen; 1099 msg->msg_name = name; 1100 } else { 1101 msg->msg_name = name = NULL; 1102 msg->msg_namelen = namelen = 0; 1103 } 1104 1105 control = msg->msg_control; 1106 controllen = msg->msg_controllen; 1107 if ((control != NULL) && (controllen != 0)) { 1108 /* 1109 * Verify that the length is not excessive to prevent 1110 * an application from consuming all of kernel memory. 1111 */ 1112 if (controllen > SO_MAXARGSIZE) { 1113 error = EINVAL; 1114 goto done2; 1115 } 1116 control = kmem_alloc(controllen, KM_SLEEP); 1117 1118 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 1119 if (copyin(msg->msg_control, control, controllen)) { 1120 error = EFAULT; 1121 goto done1; 1122 } 1123 msg->msg_control = control; 1124 } else { 1125 msg->msg_control = control = NULL; 1126 msg->msg_controllen = controllen = 0; 1127 } 1128 1129 len = uiop->uio_resid; 1130 msg->msg_flags = flags; 1131 1132 error = socket_sendmsg(so, msg, uiop, CRED()); 1133 done1: 1134 if (control != NULL) 1135 kmem_free(control, controllen); 1136 done2: 1137 if (name != NULL) 1138 kmem_free(name, namelen); 1139 done3: 1140 if (error != 0) { 1141 releasef(sock); 1142 return (set_errno(error)); 1143 } 1144 lwp_stat_update(LWP_STAT_MSGSND, 1); 1145 releasef(sock); 1146 return (len - uiop->uio_resid); 1147 } 1148 1149 /* 1150 * Native system call 1151 */ 1152 ssize_t 1153 send(int sock, void *buffer, size_t len, int flags) 1154 { 1155 struct nmsghdr lmsg; 1156 struct uio auio; 1157 struct iovec aiov[1]; 1158 1159 dprint(1, ("send(%d, %p, %ld, %d)\n", 1160 sock, buffer, len, flags)); 1161 1162 if ((ssize_t)len < 0) { 1163 return (set_errno(EINVAL)); 1164 } 1165 1166 aiov[0].iov_base = buffer; 1167 aiov[0].iov_len = len; 1168 auio.uio_loffset = 0; 1169 auio.uio_iov = aiov; 1170 auio.uio_iovcnt = 1; 1171 auio.uio_resid = len; 1172 auio.uio_segflg = UIO_USERSPACE; 1173 auio.uio_limit = 0; 1174 1175 lmsg.msg_name = NULL; 1176 lmsg.msg_control = NULL; 1177 if (!(flags & MSG_XPG4_2)) { 1178 /* 1179 * In order to be compatible with the libsocket/sockmod 1180 * implementation we set EOR for all send* calls. 1181 */ 1182 flags |= MSG_EOR; 1183 } 1184 return (sendit(sock, &lmsg, &auio, flags)); 1185 } 1186 1187 /* 1188 * Uses the MSG_XPG4_2 flag to determine if the caller is using 1189 * struct omsghdr or struct nmsghdr. 1190 */ 1191 ssize_t 1192 sendmsg(int sock, struct nmsghdr *msg, int flags) 1193 { 1194 struct nmsghdr lmsg; 1195 STRUCT_DECL(nmsghdr, u_lmsg); 1196 struct uio auio; 1197 struct iovec aiov[MSG_MAXIOVLEN]; 1198 int iovcnt; 1199 ssize_t len; 1200 int i; 1201 model_t model; 1202 1203 dprint(1, ("sendmsg(%d, %p, %d)\n", sock, (void *)msg, flags)); 1204 1205 model = get_udatamodel(); 1206 STRUCT_INIT(u_lmsg, model); 1207 1208 if (flags & MSG_XPG4_2) { 1209 if (copyin(msg, (char *)STRUCT_BUF(u_lmsg), 1210 STRUCT_SIZE(u_lmsg))) 1211 return (set_errno(EFAULT)); 1212 } else { 1213 /* 1214 * Assumes that nmsghdr and omsghdr are identically shaped 1215 * except for the added msg_flags field. 1216 */ 1217 if (copyin(msg, (char *)STRUCT_BUF(u_lmsg), 1218 SIZEOF_STRUCT(omsghdr, model))) 1219 return (set_errno(EFAULT)); 1220 /* 1221 * In order to be compatible with the libsocket/sockmod 1222 * implementation we set EOR for all send* calls. 1223 */ 1224 flags |= MSG_EOR; 1225 } 1226 1227 /* 1228 * Code below us will kmem_alloc memory and hang it 1229 * off msg_control and msg_name fields. This forces 1230 * us to copy the structure to its native form. 1231 */ 1232 lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name); 1233 lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen); 1234 lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov); 1235 lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen); 1236 lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control); 1237 lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen); 1238 lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags); 1239 1240 iovcnt = lmsg.msg_iovlen; 1241 1242 if (iovcnt <= 0 || iovcnt > MSG_MAXIOVLEN) { 1243 /* 1244 * Unless this is XPG 4.2 we allow iovcnt == 0 to 1245 * be compatible with SunOS 4.X and 4.4BSD. 1246 */ 1247 if (iovcnt != 0 || (flags & MSG_XPG4_2)) 1248 return (set_errno(EMSGSIZE)); 1249 } 1250 1251 #ifdef _SYSCALL32_IMPL 1252 /* 1253 * 32-bit callers need to have their iovec expanded, while ensuring 1254 * that they can't move more than 2Gbytes of data in a single call. 1255 */ 1256 if (model == DATAMODEL_ILP32) { 1257 struct iovec32 aiov32[MSG_MAXIOVLEN]; 1258 ssize32_t count32; 1259 1260 if (iovcnt != 0 && 1261 copyin((struct iovec32 *)lmsg.msg_iov, aiov32, 1262 iovcnt * sizeof (struct iovec32))) 1263 return (set_errno(EFAULT)); 1264 1265 count32 = 0; 1266 for (i = 0; i < iovcnt; i++) { 1267 ssize32_t iovlen32; 1268 1269 iovlen32 = aiov32[i].iov_len; 1270 count32 += iovlen32; 1271 if (iovlen32 < 0 || count32 < 0) 1272 return (set_errno(EINVAL)); 1273 aiov[i].iov_len = iovlen32; 1274 aiov[i].iov_base = 1275 (caddr_t)(uintptr_t)aiov32[i].iov_base; 1276 } 1277 } else 1278 #endif /* _SYSCALL32_IMPL */ 1279 if (iovcnt != 0 && 1280 copyin(lmsg.msg_iov, aiov, 1281 (unsigned)iovcnt * sizeof (struct iovec))) { 1282 return (set_errno(EFAULT)); 1283 } 1284 len = 0; 1285 for (i = 0; i < iovcnt; i++) { 1286 ssize_t iovlen = aiov[i].iov_len; 1287 len += iovlen; 1288 if (iovlen < 0 || len < 0) { 1289 return (set_errno(EINVAL)); 1290 } 1291 } 1292 auio.uio_loffset = 0; 1293 auio.uio_iov = aiov; 1294 auio.uio_iovcnt = iovcnt; 1295 auio.uio_resid = len; 1296 auio.uio_segflg = UIO_USERSPACE; 1297 auio.uio_limit = 0; 1298 1299 return (sendit(sock, &lmsg, &auio, flags)); 1300 } 1301 1302 ssize_t 1303 sendto(int sock, void *buffer, size_t len, int flags, 1304 struct sockaddr *name, socklen_t namelen) 1305 { 1306 struct nmsghdr lmsg; 1307 struct uio auio; 1308 struct iovec aiov[1]; 1309 1310 dprint(1, ("sendto(%d, %p, %ld, %d, %p, %d)\n", 1311 sock, buffer, len, flags, (void *)name, namelen)); 1312 1313 if ((ssize_t)len < 0) { 1314 return (set_errno(EINVAL)); 1315 } 1316 1317 aiov[0].iov_base = buffer; 1318 aiov[0].iov_len = len; 1319 auio.uio_loffset = 0; 1320 auio.uio_iov = aiov; 1321 auio.uio_iovcnt = 1; 1322 auio.uio_resid = len; 1323 auio.uio_segflg = UIO_USERSPACE; 1324 auio.uio_limit = 0; 1325 1326 lmsg.msg_name = (char *)name; 1327 lmsg.msg_namelen = namelen; 1328 lmsg.msg_control = NULL; 1329 if (!(flags & MSG_XPG4_2)) { 1330 /* 1331 * In order to be compatible with the libsocket/sockmod 1332 * implementation we set EOR for all send* calls. 1333 */ 1334 flags |= MSG_EOR; 1335 } 1336 return (sendit(sock, &lmsg, &auio, flags)); 1337 } 1338 1339 /*ARGSUSED3*/ 1340 int 1341 getpeername(int sock, struct sockaddr *name, socklen_t *namelenp, int version) 1342 { 1343 struct sonode *so; 1344 int error; 1345 socklen_t namelen; 1346 socklen_t sock_addrlen; 1347 struct sockaddr *sock_addrp; 1348 1349 dprint(1, ("getpeername(%d, %p, %p)\n", 1350 sock, (void *)name, (void *)namelenp)); 1351 1352 if ((so = getsonode(sock, &error, NULL)) == NULL) 1353 goto bad; 1354 1355 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 1356 if (copyin(namelenp, &namelen, sizeof (namelen)) || 1357 (name == NULL && namelen != 0)) { 1358 error = EFAULT; 1359 goto rel_out; 1360 } 1361 sock_addrlen = so->so_max_addr_len; 1362 sock_addrp = (struct sockaddr *)kmem_alloc(sock_addrlen, KM_SLEEP); 1363 1364 if ((error = socket_getpeername(so, sock_addrp, &sock_addrlen, 1365 B_FALSE, CRED())) == 0) { 1366 ASSERT(sock_addrlen <= so->so_max_addr_len); 1367 error = copyout_name(name, namelen, namelenp, 1368 (void *)sock_addrp, sock_addrlen); 1369 } 1370 kmem_free(sock_addrp, so->so_max_addr_len); 1371 rel_out: 1372 releasef(sock); 1373 bad: return (error != 0 ? set_errno(error) : 0); 1374 } 1375 1376 /*ARGSUSED3*/ 1377 int 1378 getsockname(int sock, struct sockaddr *name, 1379 socklen_t *namelenp, int version) 1380 { 1381 struct sonode *so; 1382 int error; 1383 socklen_t namelen, sock_addrlen; 1384 struct sockaddr *sock_addrp; 1385 1386 dprint(1, ("getsockname(%d, %p, %p)\n", 1387 sock, (void *)name, (void *)namelenp)); 1388 1389 if ((so = getsonode(sock, &error, NULL)) == NULL) 1390 goto bad; 1391 1392 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 1393 if (copyin(namelenp, &namelen, sizeof (namelen)) || 1394 (name == NULL && namelen != 0)) { 1395 error = EFAULT; 1396 goto rel_out; 1397 } 1398 1399 sock_addrlen = so->so_max_addr_len; 1400 sock_addrp = (struct sockaddr *)kmem_alloc(sock_addrlen, KM_SLEEP); 1401 if ((error = socket_getsockname(so, sock_addrp, &sock_addrlen, 1402 CRED())) == 0) { 1403 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 1404 ASSERT(sock_addrlen <= so->so_max_addr_len); 1405 error = copyout_name(name, namelen, namelenp, 1406 (void *)sock_addrp, sock_addrlen); 1407 } 1408 kmem_free(sock_addrp, so->so_max_addr_len); 1409 rel_out: 1410 releasef(sock); 1411 bad: return (error != 0 ? set_errno(error) : 0); 1412 } 1413 1414 /*ARGSUSED5*/ 1415 int 1416 getsockopt(int sock, 1417 int level, 1418 int option_name, 1419 void *option_value, 1420 socklen_t *option_lenp, 1421 int version) 1422 { 1423 struct sonode *so; 1424 socklen_t optlen, optlen_res; 1425 void *optval; 1426 int error; 1427 1428 dprint(1, ("getsockopt(%d, %d, %d, %p, %p)\n", 1429 sock, level, option_name, option_value, (void *)option_lenp)); 1430 1431 if ((so = getsonode(sock, &error, NULL)) == NULL) 1432 return (set_errno(error)); 1433 1434 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 1435 if (copyin(option_lenp, &optlen, sizeof (optlen))) { 1436 releasef(sock); 1437 return (set_errno(EFAULT)); 1438 } 1439 /* 1440 * Verify that the length is not excessive to prevent 1441 * an application from consuming all of kernel memory. 1442 */ 1443 if (optlen > SO_MAXARGSIZE) { 1444 error = EINVAL; 1445 releasef(sock); 1446 return (set_errno(error)); 1447 } 1448 optval = kmem_alloc(optlen, KM_SLEEP); 1449 optlen_res = optlen; 1450 error = socket_getsockopt(so, level, option_name, optval, 1451 &optlen_res, (version != SOV_XPG4_2) ? 0 : _SOGETSOCKOPT_XPG4_2, 1452 CRED()); 1453 releasef(sock); 1454 if (error) { 1455 kmem_free(optval, optlen); 1456 return (set_errno(error)); 1457 } 1458 error = copyout_arg(option_value, optlen, option_lenp, 1459 optval, optlen_res); 1460 kmem_free(optval, optlen); 1461 if (error) 1462 return (set_errno(error)); 1463 return (0); 1464 } 1465 1466 /*ARGSUSED5*/ 1467 int 1468 setsockopt(int sock, 1469 int level, 1470 int option_name, 1471 void *option_value, 1472 socklen_t option_len, 1473 int version) 1474 { 1475 struct sonode *so; 1476 intptr_t buffer[2]; 1477 void *optval = NULL; 1478 int error; 1479 1480 dprint(1, ("setsockopt(%d, %d, %d, %p, %d)\n", 1481 sock, level, option_name, option_value, option_len)); 1482 1483 if ((so = getsonode(sock, &error, NULL)) == NULL) 1484 return (set_errno(error)); 1485 1486 if (option_value != NULL) { 1487 if (option_len != 0) { 1488 /* 1489 * Verify that the length is not excessive to prevent 1490 * an application from consuming all of kernel memory. 1491 */ 1492 if (option_len > SO_MAXARGSIZE) { 1493 error = EINVAL; 1494 goto done2; 1495 } 1496 optval = option_len <= sizeof (buffer) ? 1497 &buffer : kmem_alloc((size_t)option_len, KM_SLEEP); 1498 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 1499 if (copyin(option_value, optval, (size_t)option_len)) { 1500 error = EFAULT; 1501 goto done1; 1502 } 1503 } 1504 } else 1505 option_len = 0; 1506 1507 error = socket_setsockopt(so, level, option_name, optval, 1508 (t_uscalar_t)option_len, CRED()); 1509 done1: 1510 if (optval != buffer) 1511 kmem_free(optval, (size_t)option_len); 1512 done2: 1513 releasef(sock); 1514 if (error) 1515 return (set_errno(error)); 1516 return (0); 1517 } 1518 1519 /* 1520 * Add config info when name is non-NULL; delete info when name is NULL. 1521 * name could be a device name or a module name and are user address. 1522 */ 1523 int 1524 sockconfig(int family, int type, int protocol, char *name) 1525 { 1526 char *kdevpath = NULL; /* Copied in devpath string */ 1527 char *kmodule = NULL; 1528 size_t pathlen = 0; 1529 int error = 0; 1530 1531 dprint(1, ("sockconfig(%d, %d, %d, %p)\n", 1532 family, type, protocol, (void *)name)); 1533 1534 if (secpolicy_net_config(CRED(), B_FALSE) != 0) 1535 return (set_errno(EPERM)); 1536 1537 /* 1538 * By default set the kdevpath and kmodule to NULL to delete an entry. 1539 * Otherwise when name is not NULL, set the kdevpath or kmodule 1540 * value to add an entry. 1541 */ 1542 if (name != NULL) { 1543 /* 1544 * Adding an entry. 1545 * Copyin the name. 1546 * This also makes it possible to check for too long pathnames. 1547 * Compress the space needed for the name before passing it 1548 * to soconfig - soconfig will store the string until 1549 * the configuration is removed. 1550 */ 1551 char *buf; 1552 buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1553 if ((error = copyinstr(name, buf, MAXPATHLEN, &pathlen)) != 0) { 1554 kmem_free(buf, MAXPATHLEN); 1555 goto done; 1556 } 1557 if (strncmp(buf, "/dev", strlen("/dev")) == 0) { 1558 /* For device */ 1559 1560 /* 1561 * Special handling for NCA: 1562 * 1563 * DEV_NCA is never opened even if an application 1564 * requests for AF_NCA. The device opened is instead a 1565 * predefined AF_INET transport (NCA_INET_DEV). 1566 * 1567 * Prior to Volo (PSARC/2007/587) NCA would determine 1568 * the device using a lookup, which worked then because 1569 * all protocols were based on TPI. Since TPI is no 1570 * longer the default, we have to explicitly state 1571 * which device to use. 1572 */ 1573 if (strcmp(buf, NCA_DEV) == 0) { 1574 /* only support entry <28, 2, 0> */ 1575 if (family != AF_NCA || type != SOCK_STREAM || 1576 protocol != 0) { 1577 kmem_free(buf, MAXPATHLEN); 1578 error = EINVAL; 1579 goto done; 1580 } 1581 1582 pathlen = strlen(NCA_INET_DEV) + 1; 1583 kdevpath = kmem_alloc(pathlen, KM_SLEEP); 1584 bcopy(NCA_INET_DEV, kdevpath, pathlen); 1585 kdevpath[pathlen - 1] = '\0'; 1586 } else { 1587 kdevpath = kmem_alloc(pathlen, KM_SLEEP); 1588 bcopy(buf, kdevpath, pathlen); 1589 kdevpath[pathlen - 1] = '\0'; 1590 } 1591 } else { 1592 /* For socket module */ 1593 kmodule = kmem_alloc(pathlen, KM_SLEEP); 1594 bcopy(buf, kmodule, pathlen); 1595 kmodule[pathlen - 1] = '\0'; 1596 1597 pathlen = 0; 1598 if (strcmp(kmodule, "tcp") == 0) { 1599 /* Get the tcp device name for fallback */ 1600 if (family == 2) { 1601 pathlen = strlen("/dev/tcp") + 1; 1602 kdevpath = kmem_alloc(pathlen, 1603 KM_SLEEP); 1604 bcopy("/dev/tcp", kdevpath, 1605 pathlen); 1606 kdevpath[pathlen - 1] = '\0'; 1607 } else { 1608 ASSERT(family == 26); 1609 pathlen = strlen("/dev/tcp6") + 1; 1610 kdevpath = kmem_alloc(pathlen, 1611 KM_SLEEP); 1612 bcopy("/dev/tcp6", kdevpath, pathlen); 1613 kdevpath[pathlen - 1] = '\0'; 1614 } 1615 } else if (strcmp(kmodule, "udp") == 0) { 1616 /* Get the udp device name for fallback */ 1617 if (family == 2) { 1618 pathlen = strlen("/dev/udp") + 1; 1619 kdevpath = kmem_alloc(pathlen, 1620 KM_SLEEP); 1621 bcopy("/dev/udp", kdevpath, pathlen); 1622 kdevpath[pathlen - 1] = '\0'; 1623 } else { 1624 ASSERT(family == 26); 1625 pathlen = strlen("/dev/udp6") + 1; 1626 kdevpath = kmem_alloc(pathlen, 1627 KM_SLEEP); 1628 bcopy("/dev/udp6", kdevpath, pathlen); 1629 kdevpath[pathlen - 1] = '\0'; 1630 } 1631 } else if (strcmp(kmodule, "icmp") == 0) { 1632 /* Get the icmp device name for fallback */ 1633 if (family == 2) { 1634 pathlen = strlen("/dev/rawip") + 1; 1635 kdevpath = kmem_alloc(pathlen, 1636 KM_SLEEP); 1637 bcopy("/dev/rawip", kdevpath, pathlen); 1638 kdevpath[pathlen - 1] = '\0'; 1639 } else { 1640 ASSERT(family == 26); 1641 pathlen = strlen("/dev/rawip6") + 1; 1642 kdevpath = kmem_alloc(pathlen, 1643 KM_SLEEP); 1644 bcopy("/dev/rawip6", kdevpath, pathlen); 1645 kdevpath[pathlen - 1] = '\0'; 1646 } 1647 } 1648 } 1649 1650 kmem_free(buf, MAXPATHLEN); 1651 } 1652 error = soconfig(family, type, protocol, kdevpath, (int)pathlen, 1653 kmodule); 1654 done: 1655 if (error) { 1656 eprintline(error); 1657 return (set_errno(error)); 1658 } 1659 return (0); 1660 } 1661 1662 1663 /* 1664 * Sendfile is implemented through two schemes, direct I/O or by 1665 * caching in the filesystem page cache. We cache the input file by 1666 * default and use direct I/O only if sendfile_max_size is set 1667 * appropriately as explained below. Note that this logic is consistent 1668 * with other filesystems where caching is turned on by default 1669 * unless explicitly turned off by using the DIRECTIO ioctl. 1670 * 1671 * We choose a slightly different scheme here. One can turn off 1672 * caching by setting sendfile_max_size to 0. One can also enable 1673 * caching of files <= sendfile_max_size by setting sendfile_max_size 1674 * to an appropriate value. By default sendfile_max_size is set to the 1675 * maximum value so that all files are cached. In future, we may provide 1676 * better interfaces for caching the file. 1677 * 1678 * Sendfile through Direct I/O (Zero copy) 1679 * -------------------------------------- 1680 * 1681 * As disks are normally slower than the network, we can't have a 1682 * single thread that reads the disk and writes to the network. We 1683 * need to have parallelism. This is done by having the sendfile 1684 * thread create another thread that reads from the filesystem 1685 * and queues it for network processing. In this scheme, the data 1686 * is never copied anywhere i.e it is zero copy unlike the other 1687 * scheme. 1688 * 1689 * We have a sendfile queue (snfq) where each sendfile 1690 * request (snf_req_t) is queued for processing by a thread. Number 1691 * of threads is dynamically allocated and they exit if they are idling 1692 * beyond a specified amount of time. When each request (snf_req_t) is 1693 * processed by a thread, it produces a number of mblk_t structures to 1694 * be consumed by the sendfile thread. snf_deque and snf_enque are 1695 * used for consuming and producing mblks. Size of the filesystem 1696 * read is determined by the tunable (sendfile_read_size). A single 1697 * mblk holds sendfile_read_size worth of data (except the last 1698 * read of the file) which is sent down as a whole to the network. 1699 * sendfile_read_size is set to 1 MB as this seems to be the optimal 1700 * value for the UFS filesystem backed by a striped storage array. 1701 * 1702 * Synchronisation between read (producer) and write (consumer) threads. 1703 * -------------------------------------------------------------------- 1704 * 1705 * sr_lock protects sr_ib_head and sr_ib_tail. The lock is held while 1706 * adding and deleting items in this list. Error can happen anytime 1707 * during read or write. There could be unprocessed mblks in the 1708 * sr_ib_XXX list when a read or write error occurs. Whenever error 1709 * is encountered, we need two things to happen : 1710 * 1711 * a) One of the threads need to clean the mblks. 1712 * b) When one thread encounters an error, the other should stop. 1713 * 1714 * For (a), we don't want to penalize the reader thread as it could do 1715 * some useful work processing other requests. For (b), the error can 1716 * be detected by examining sr_read_error or sr_write_error. 1717 * sr_lock protects sr_read_error and sr_write_error. If both reader and 1718 * writer encounters error, we need to report the write error back to 1719 * the application as that's what would have happened if the operations 1720 * were done sequentially. With this in mind, following should work : 1721 * 1722 * - Check for errors before read or write. 1723 * - If the reader encounters error, set the error in sr_read_error. 1724 * Check sr_write_error, if it is set, send cv_signal as it is 1725 * waiting for reader to complete. If it is not set, the writer 1726 * is either running sinking data to the network or blocked 1727 * because of flow control. For handling the latter case, we 1728 * always send a signal. In any case, it will examine sr_read_error 1729 * and return. sr_read_error is marked with SR_READ_DONE to tell 1730 * the writer that the reader is done in all the cases. 1731 * - If the writer encounters error, set the error in sr_write_error. 1732 * The reader thread is either blocked because of flow control or 1733 * running reading data from the disk. For the former, we need to 1734 * wakeup the thread. Again to keep it simple, we always wake up 1735 * the reader thread. Then, wait for the read thread to complete 1736 * if it is not done yet. Cleanup and return. 1737 * 1738 * High and low water marks for the read thread. 1739 * -------------------------------------------- 1740 * 1741 * If sendfile() is used to send data over a slow network, we need to 1742 * make sure that the read thread does not produce data at a faster 1743 * rate than the network. This can happen if the disk is faster than 1744 * the network. In such a case, we don't want to build a very large queue. 1745 * But we would still like to get all of the network throughput possible. 1746 * This implies that network should never block waiting for data. 1747 * As there are lot of disk throughput/network throughput combinations 1748 * possible, it is difficult to come up with an accurate number. 1749 * A typical 10K RPM disk has a max seek latency 17ms and rotational 1750 * latency of 3ms for reading a disk block. Thus, the total latency to 1751 * initiate a new read, transfer data from the disk and queue for 1752 * transmission would take about a max of 25ms. Todays max transfer rate 1753 * for network is 100MB/sec. If the thread is blocked because of flow 1754 * control, it would take 25ms to get new data ready for transmission. 1755 * We have to make sure that network is not idling, while we are initiating 1756 * new transfers. So, at 100MB/sec, to keep network busy we would need 1757 * 2.5MB of data. Rounding off, we keep the low water mark to be 3MB of data. 1758 * We need to pick a high water mark so that the woken up thread would 1759 * do considerable work before blocking again to prevent thrashing. Currently, 1760 * we pick this to be 10 times that of the low water mark. 1761 * 1762 * Sendfile with segmap caching (One copy from page cache to mblks). 1763 * ---------------------------------------------------------------- 1764 * 1765 * We use the segmap cache for caching the file, if the size of file 1766 * is <= sendfile_max_size. In this case we don't use threads as VM 1767 * is reasonably fast enough to keep up with the network. If the underlying 1768 * transport allows, we call segmap_getmapflt() to map MAXBSIZE (8K) worth 1769 * of data into segmap space, and use the virtual address from segmap 1770 * directly through desballoc() to avoid copy. Once the transport is done 1771 * with the data, the mapping will be released through segmap_release() 1772 * called by the call-back routine. 1773 * 1774 * If zero-copy is not allowed by the transport, we simply call VOP_READ() 1775 * to copy the data from the filesystem into our temporary network buffer. 1776 * 1777 * To disable caching, set sendfile_max_size to 0. 1778 */ 1779 1780 uint_t sendfile_read_size = 1024 * 1024; 1781 #define SENDFILE_REQ_LOWAT 3 * 1024 * 1024 1782 uint_t sendfile_req_lowat = SENDFILE_REQ_LOWAT; 1783 uint_t sendfile_req_hiwat = 10 * SENDFILE_REQ_LOWAT; 1784 struct sendfile_stats sf_stats; 1785 struct sendfile_queue *snfq; 1786 clock_t snfq_timeout; 1787 off64_t sendfile_max_size; 1788 1789 static void snf_enque(snf_req_t *, mblk_t *); 1790 static mblk_t *snf_deque(snf_req_t *); 1791 1792 void 1793 sendfile_init(void) 1794 { 1795 snfq = kmem_zalloc(sizeof (struct sendfile_queue), KM_SLEEP); 1796 1797 mutex_init(&snfq->snfq_lock, NULL, MUTEX_DEFAULT, NULL); 1798 cv_init(&snfq->snfq_cv, NULL, CV_DEFAULT, NULL); 1799 snfq->snfq_max_threads = max_ncpus; 1800 snfq_timeout = SNFQ_TIMEOUT; 1801 /* Cache all files by default. */ 1802 sendfile_max_size = MAXOFFSET_T; 1803 } 1804 1805 /* 1806 * Queues a mblk_t for network processing. 1807 */ 1808 static void 1809 snf_enque(snf_req_t *sr, mblk_t *mp) 1810 { 1811 mp->b_next = NULL; 1812 mutex_enter(&sr->sr_lock); 1813 if (sr->sr_mp_head == NULL) { 1814 sr->sr_mp_head = sr->sr_mp_tail = mp; 1815 cv_signal(&sr->sr_cv); 1816 } else { 1817 sr->sr_mp_tail->b_next = mp; 1818 sr->sr_mp_tail = mp; 1819 } 1820 sr->sr_qlen += MBLKL(mp); 1821 while ((sr->sr_qlen > sr->sr_hiwat) && 1822 (sr->sr_write_error == 0)) { 1823 sf_stats.ss_full_waits++; 1824 cv_wait(&sr->sr_cv, &sr->sr_lock); 1825 } 1826 mutex_exit(&sr->sr_lock); 1827 } 1828 1829 /* 1830 * De-queues a mblk_t for network processing. 1831 */ 1832 static mblk_t * 1833 snf_deque(snf_req_t *sr) 1834 { 1835 mblk_t *mp; 1836 1837 mutex_enter(&sr->sr_lock); 1838 /* 1839 * If we have encountered an error on read or read is 1840 * completed and no more mblks, return NULL. 1841 * We need to check for NULL sr_mp_head also as 1842 * the reads could have completed and there is 1843 * nothing more to come. 1844 */ 1845 if (((sr->sr_read_error & ~SR_READ_DONE) != 0) || 1846 ((sr->sr_read_error & SR_READ_DONE) && 1847 sr->sr_mp_head == NULL)) { 1848 mutex_exit(&sr->sr_lock); 1849 return (NULL); 1850 } 1851 /* 1852 * To start with neither SR_READ_DONE is marked nor 1853 * the error is set. When we wake up from cv_wait, 1854 * following are the possibilities : 1855 * 1856 * a) sr_read_error is zero and mblks are queued. 1857 * b) sr_read_error is set to SR_READ_DONE 1858 * and mblks are queued. 1859 * c) sr_read_error is set to SR_READ_DONE 1860 * and no mblks. 1861 * d) sr_read_error is set to some error other 1862 * than SR_READ_DONE. 1863 */ 1864 1865 while ((sr->sr_read_error == 0) && (sr->sr_mp_head == NULL)) { 1866 sf_stats.ss_empty_waits++; 1867 cv_wait(&sr->sr_cv, &sr->sr_lock); 1868 } 1869 /* Handle (a) and (b) first - the normal case. */ 1870 if (((sr->sr_read_error & ~SR_READ_DONE) == 0) && 1871 (sr->sr_mp_head != NULL)) { 1872 mp = sr->sr_mp_head; 1873 sr->sr_mp_head = mp->b_next; 1874 sr->sr_qlen -= MBLKL(mp); 1875 if (sr->sr_qlen < sr->sr_lowat) 1876 cv_signal(&sr->sr_cv); 1877 mutex_exit(&sr->sr_lock); 1878 mp->b_next = NULL; 1879 return (mp); 1880 } 1881 /* Handle (c) and (d). */ 1882 mutex_exit(&sr->sr_lock); 1883 return (NULL); 1884 } 1885 1886 /* 1887 * Reads data from the filesystem and queues it for network processing. 1888 */ 1889 void 1890 snf_async_read(snf_req_t *sr) 1891 { 1892 size_t iosize; 1893 u_offset_t fileoff; 1894 u_offset_t size; 1895 int ret_size; 1896 int error; 1897 file_t *fp; 1898 mblk_t *mp; 1899 struct vnode *vp; 1900 int extra = 0; 1901 int maxblk = 0; 1902 int wroff = 0; 1903 struct sonode *so; 1904 1905 fp = sr->sr_fp; 1906 size = sr->sr_file_size; 1907 fileoff = sr->sr_file_off; 1908 1909 /* 1910 * Ignore the error for filesystems that doesn't support DIRECTIO. 1911 */ 1912 (void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_ON, 0, 1913 kcred, NULL, NULL); 1914 1915 vp = sr->sr_vp; 1916 if (vp->v_type == VSOCK) { 1917 stdata_t *stp; 1918 1919 /* 1920 * Get the extra space to insert a header and a trailer. 1921 */ 1922 so = VTOSO(vp); 1923 stp = vp->v_stream; 1924 if (stp == NULL) { 1925 wroff = so->so_proto_props.sopp_wroff; 1926 maxblk = so->so_proto_props.sopp_maxblk; 1927 extra = wroff + so->so_proto_props.sopp_tail; 1928 } else { 1929 wroff = (int)(stp->sd_wroff); 1930 maxblk = (int)(stp->sd_maxblk); 1931 extra = wroff + (int)(stp->sd_tail); 1932 } 1933 } 1934 1935 while ((size != 0) && (sr->sr_write_error == 0)) { 1936 1937 iosize = (int)MIN(sr->sr_maxpsz, size); 1938 1939 /* 1940 * For sockets acting as an SSL proxy, we 1941 * need to adjust the size to the maximum 1942 * SSL record size set in the stream head. 1943 */ 1944 if (vp->v_type == VSOCK && !SOCK_IS_NONSTR(so) && 1945 SOTOTPI(so)->sti_kssl_ctx != NULL) 1946 iosize = (int)MIN(iosize, maxblk); 1947 1948 if (is_system_labeled()) { 1949 mp = allocb_cred(iosize + extra, CRED(), 1950 curproc->p_pid); 1951 } else { 1952 mp = allocb(iosize + extra, BPRI_MED); 1953 } 1954 if (mp == NULL) { 1955 error = EAGAIN; 1956 break; 1957 } 1958 1959 mp->b_rptr += wroff; 1960 1961 ret_size = soreadfile(fp, mp->b_rptr, fileoff, &error, iosize); 1962 1963 /* Error or Reached EOF ? */ 1964 if ((error != 0) || (ret_size == 0)) { 1965 freeb(mp); 1966 break; 1967 } 1968 mp->b_wptr = mp->b_rptr + ret_size; 1969 1970 snf_enque(sr, mp); 1971 size -= ret_size; 1972 fileoff += ret_size; 1973 } 1974 (void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_OFF, 0, 1975 kcred, NULL, NULL); 1976 mutex_enter(&sr->sr_lock); 1977 sr->sr_read_error = error; 1978 sr->sr_read_error |= SR_READ_DONE; 1979 cv_signal(&sr->sr_cv); 1980 mutex_exit(&sr->sr_lock); 1981 } 1982 1983 void 1984 snf_async_thread(void) 1985 { 1986 snf_req_t *sr; 1987 callb_cpr_t cprinfo; 1988 clock_t time_left = 1; 1989 clock_t now; 1990 1991 CALLB_CPR_INIT(&cprinfo, &snfq->snfq_lock, callb_generic_cpr, "snfq"); 1992 1993 mutex_enter(&snfq->snfq_lock); 1994 for (;;) { 1995 /* 1996 * If we didn't find a entry, then block until woken up 1997 * again and then look through the queues again. 1998 */ 1999 while ((sr = snfq->snfq_req_head) == NULL) { 2000 CALLB_CPR_SAFE_BEGIN(&cprinfo); 2001 if (time_left <= 0) { 2002 snfq->snfq_svc_threads--; 2003 CALLB_CPR_EXIT(&cprinfo); 2004 thread_exit(); 2005 /* NOTREACHED */ 2006 } 2007 snfq->snfq_idle_cnt++; 2008 2009 time_to_wait(&now, snfq_timeout); 2010 time_left = cv_timedwait(&snfq->snfq_cv, 2011 &snfq->snfq_lock, now); 2012 snfq->snfq_idle_cnt--; 2013 2014 CALLB_CPR_SAFE_END(&cprinfo, &snfq->snfq_lock); 2015 } 2016 snfq->snfq_req_head = sr->sr_next; 2017 snfq->snfq_req_cnt--; 2018 mutex_exit(&snfq->snfq_lock); 2019 snf_async_read(sr); 2020 mutex_enter(&snfq->snfq_lock); 2021 } 2022 } 2023 2024 2025 snf_req_t * 2026 create_thread(int operation, struct vnode *vp, file_t *fp, 2027 u_offset_t fileoff, u_offset_t size) 2028 { 2029 snf_req_t *sr; 2030 stdata_t *stp; 2031 2032 sr = (snf_req_t *)kmem_zalloc(sizeof (snf_req_t), KM_SLEEP); 2033 2034 sr->sr_vp = vp; 2035 sr->sr_fp = fp; 2036 stp = vp->v_stream; 2037 2038 /* 2039 * store sd_qn_maxpsz into sr_maxpsz while we have stream head. 2040 * stream might be closed before thread returns from snf_async_read. 2041 */ 2042 if (stp != NULL && stp->sd_qn_maxpsz > 0) { 2043 sr->sr_maxpsz = MIN(MAXBSIZE, stp->sd_qn_maxpsz); 2044 } else { 2045 sr->sr_maxpsz = MAXBSIZE; 2046 } 2047 2048 sr->sr_operation = operation; 2049 sr->sr_file_off = fileoff; 2050 sr->sr_file_size = size; 2051 sr->sr_hiwat = sendfile_req_hiwat; 2052 sr->sr_lowat = sendfile_req_lowat; 2053 mutex_init(&sr->sr_lock, NULL, MUTEX_DEFAULT, NULL); 2054 cv_init(&sr->sr_cv, NULL, CV_DEFAULT, NULL); 2055 /* 2056 * See whether we need another thread for servicing this 2057 * request. If there are already enough requests queued 2058 * for the threads, create one if not exceeding 2059 * snfq_max_threads. 2060 */ 2061 mutex_enter(&snfq->snfq_lock); 2062 if (snfq->snfq_req_cnt >= snfq->snfq_idle_cnt && 2063 snfq->snfq_svc_threads < snfq->snfq_max_threads) { 2064 (void) thread_create(NULL, 0, &snf_async_thread, 0, 0, &p0, 2065 TS_RUN, minclsyspri); 2066 snfq->snfq_svc_threads++; 2067 } 2068 if (snfq->snfq_req_head == NULL) { 2069 snfq->snfq_req_head = snfq->snfq_req_tail = sr; 2070 cv_signal(&snfq->snfq_cv); 2071 } else { 2072 snfq->snfq_req_tail->sr_next = sr; 2073 snfq->snfq_req_tail = sr; 2074 } 2075 snfq->snfq_req_cnt++; 2076 mutex_exit(&snfq->snfq_lock); 2077 return (sr); 2078 } 2079 2080 int 2081 snf_direct_io(file_t *fp, file_t *rfp, u_offset_t fileoff, u_offset_t size, 2082 ssize_t *count) 2083 { 2084 snf_req_t *sr; 2085 mblk_t *mp; 2086 int iosize; 2087 int error = 0; 2088 short fflag; 2089 struct vnode *vp; 2090 int ksize; 2091 struct nmsghdr msg; 2092 2093 ksize = 0; 2094 *count = 0; 2095 bzero(&msg, sizeof (msg)); 2096 2097 vp = fp->f_vnode; 2098 fflag = fp->f_flag; 2099 if ((sr = create_thread(READ_OP, vp, rfp, fileoff, size)) == NULL) 2100 return (EAGAIN); 2101 2102 /* 2103 * We check for read error in snf_deque. It has to check 2104 * for successful READ_DONE and return NULL, and we might 2105 * as well make an additional check there. 2106 */ 2107 while ((mp = snf_deque(sr)) != NULL) { 2108 2109 if (ISSIG(curthread, JUSTLOOKING)) { 2110 freeb(mp); 2111 error = EINTR; 2112 break; 2113 } 2114 iosize = MBLKL(mp); 2115 2116 error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &mp); 2117 2118 if (error != 0) { 2119 if (mp != NULL) 2120 freeb(mp); 2121 break; 2122 } 2123 ksize += iosize; 2124 } 2125 *count = ksize; 2126 2127 mutex_enter(&sr->sr_lock); 2128 sr->sr_write_error = error; 2129 /* Look at the big comments on why we cv_signal here. */ 2130 cv_signal(&sr->sr_cv); 2131 2132 /* Wait for the reader to complete always. */ 2133 while (!(sr->sr_read_error & SR_READ_DONE)) { 2134 cv_wait(&sr->sr_cv, &sr->sr_lock); 2135 } 2136 /* If there is no write error, check for read error. */ 2137 if (error == 0) 2138 error = (sr->sr_read_error & ~SR_READ_DONE); 2139 2140 if (error != 0) { 2141 mblk_t *next_mp; 2142 2143 mp = sr->sr_mp_head; 2144 while (mp != NULL) { 2145 next_mp = mp->b_next; 2146 mp->b_next = NULL; 2147 freeb(mp); 2148 mp = next_mp; 2149 } 2150 } 2151 mutex_exit(&sr->sr_lock); 2152 kmem_free(sr, sizeof (snf_req_t)); 2153 return (error); 2154 } 2155 2156 #define SNF_VPMMAXPGS (VPMMAXPGS/2) 2157 #define SNF_MAXVMAPS (SNF_VPMMAXPGS + 1) 2158 2159 typedef struct { 2160 volatile unsigned int ref; 2161 frtn_t snfv_frtn; 2162 vnode_t *snfv_vp; 2163 struct vmap vml[SNF_MAXVMAPS]; 2164 } snf_vmap_desbinfo; 2165 2166 typedef struct { 2167 frtn_t snfi_frtn; 2168 caddr_t snfi_base; 2169 uint_t snfi_mapoff; 2170 size_t snfi_len; 2171 vnode_t *snfi_vp; 2172 } snf_smap_desbinfo; 2173 2174 void 2175 snf_vmap_desbfree(snf_vmap_desbinfo *snfv) 2176 { 2177 if (atomic_add_32_nv(&snfv->ref, -1) < 1) { 2178 if (snfv->vml) 2179 vpm_unmap_pages(snfv->vml, S_READ); 2180 VN_RELE(snfv->snfv_vp); 2181 kmem_free(snfv, sizeof (snf_vmap_desbinfo)); 2182 } 2183 } 2184 2185 /* 2186 * The callback function when the last ref of the mblk is dropped, 2187 * normally occurs when TCP receives the ack. But it can be the driver 2188 * too due to lazy reclaim. 2189 */ 2190 void 2191 snf_smap_desbfree(snf_smap_desbinfo *snfi) 2192 { 2193 if (! IS_KPM_ADDR(snfi->snfi_base)) { 2194 /* 2195 * We don't need to call segmap_fault(F_SOFTUNLOCK) for 2196 * segmap_kpm as long as the latter never falls back to 2197 * "use_segmap_range". (See segmap_getmapflt().) 2198 * 2199 * Using S_OTHER saves an redundant hat_setref() in 2200 * segmap_unlock() 2201 */ 2202 (void) segmap_fault(kas.a_hat, segkmap, 2203 (caddr_t)(uintptr_t)(((uintptr_t)snfi->snfi_base + 2204 snfi->snfi_mapoff) & PAGEMASK), snfi->snfi_len, 2205 F_SOFTUNLOCK, S_OTHER); 2206 } 2207 (void) segmap_release(segkmap, snfi->snfi_base, SM_DONTNEED); 2208 VN_RELE(snfi->snfi_vp); 2209 kmem_free(snfi, sizeof (*snfi)); 2210 } 2211 2212 /* 2213 * Use segmap instead of bcopy to send down a desballoca'ed, mblk. The mblk 2214 * contains a segmap slot of no more than MAXBSIZE. 2215 * 2216 * At the end of the whole sendfile() operation, we wait till the data from 2217 * the last mblk is ack'ed by the transport before returning so that the 2218 * caller of sendfile() can safely modify the file content. 2219 */ 2220 int 2221 snf_segmap(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t size, 2222 ssize_t *count, boolean_t nowait) 2223 { 2224 caddr_t base; 2225 int mapoff; 2226 vnode_t *vp; 2227 mblk_t *mp; 2228 mblk_t *mp1; 2229 int maxsize; 2230 int iosize; 2231 int iosz; 2232 int i; 2233 int error; 2234 short fflag; 2235 int ksize; 2236 snf_smap_desbinfo *snfi; 2237 snf_vmap_desbinfo *snfv; 2238 struct vmap *vml; 2239 struct vattr va; 2240 boolean_t dowait = B_FALSE; 2241 struct nmsghdr msg; 2242 2243 vp = fp->f_vnode; 2244 fflag = fp->f_flag; 2245 ksize = 0; 2246 bzero(&msg, sizeof (msg)); 2247 2248 for (;;) { 2249 if (ISSIG(curthread, JUSTLOOKING)) { 2250 error = EINTR; 2251 break; 2252 } 2253 2254 if (vpm_enable) { /* Use vpm page mappings */ 2255 mapoff = fileoff & PAGEOFFSET; 2256 maxsize = MIN((SNF_VPMMAXPGS * PAGESIZE), size); 2257 2258 snfv = kmem_alloc(sizeof (snf_vmap_desbinfo), KM_SLEEP); 2259 vml = snfv->vml; 2260 if (vpm_map_pages(fvp, fileoff, (size_t)maxsize, 2261 (VPM_FETCHPAGE), vml, SNF_MAXVMAPS, 2262 NULL, S_READ) != 0) { 2263 kmem_free(snfv, sizeof (snf_vmap_desbinfo)); 2264 error = EIO; 2265 goto out; 2266 } 2267 snfv->snfv_frtn.free_func = snf_vmap_desbfree; 2268 snfv->snfv_frtn.free_arg = (caddr_t)snfv; 2269 iosz = MIN(vml[0].vs_len - mapoff, size); 2270 mp = esballoca((uchar_t *)vml[0].vs_addr + mapoff, 2271 iosz, BPRI_HI, &snfv->snfv_frtn); 2272 2273 if (mp == NULL) { 2274 vpm_unmap_pages(vml, S_READ); 2275 kmem_free(snfv, sizeof (snf_vmap_desbinfo)); 2276 error = EAGAIN; 2277 goto out; 2278 } 2279 /* Mark this dblk with the zero-copy flag */ 2280 mp->b_datap->db_struioflag |= STRUIO_ZC; 2281 mp->b_wptr += iosz; 2282 iosize = iosz; 2283 fileoff += iosz; 2284 size -= iosz; 2285 snfv->ref = 1; 2286 2287 for (i = 1; (vml[i].vs_data != NULL) && 2288 (iosize < maxsize) && (size > 0); i++) { 2289 iosz = MIN(vml[i].vs_len, size); 2290 mp1 = esballoca((uchar_t *)vml[i].vs_addr, 2291 iosz, BPRI_HI, &snfv->snfv_frtn); 2292 2293 if (mp1 == NULL) { 2294 error = EAGAIN; 2295 goto part; 2296 } 2297 mp1->b_datap->db_struioflag |= STRUIO_ZC; 2298 mp1->b_wptr += iosz; 2299 iosize += iosz; 2300 fileoff += iosz; 2301 size -= iosz; 2302 snfv->ref++; 2303 linkb(mp, mp1); 2304 } 2305 part: 2306 VN_HOLD(fvp); 2307 snfv->snfv_vp = fvp; 2308 } else { 2309 2310 /* vpm not supported. fallback to segmap */ 2311 mapoff = fileoff & MAXBOFFSET; 2312 iosize = MAXBSIZE - mapoff; 2313 if (iosize > size) 2314 iosize = size; 2315 /* 2316 * we don't forcefault because we'll call 2317 * segmap_fault(F_SOFTLOCK) next. 2318 * 2319 * S_READ will get the ref bit set (by either 2320 * segmap_getmapflt() or segmap_fault()) and page 2321 * shared locked. 2322 */ 2323 base = segmap_getmapflt(segkmap, fvp, fileoff, iosize, 2324 segmap_kpm ? SM_FAULT : 0, S_READ); 2325 2326 snfi = kmem_alloc(sizeof (*snfi), KM_SLEEP); 2327 snfi->snfi_len = (size_t)roundup(mapoff+iosize, 2328 PAGESIZE)- (mapoff & PAGEMASK); 2329 /* 2330 * We must call segmap_fault() even for segmap_kpm 2331 * because that's how error gets returned. 2332 * (segmap_getmapflt() never fails but segmap_fault() 2333 * does.) 2334 */ 2335 if (segmap_fault(kas.a_hat, segkmap, 2336 (caddr_t)(uintptr_t)(((uintptr_t)base + mapoff) 2337 & PAGEMASK), snfi->snfi_len, 2338 F_SOFTLOCK, S_READ) != 0) { 2339 (void) segmap_release(segkmap, base, 0); 2340 kmem_free(snfi, sizeof (*snfi)); 2341 error = EIO; 2342 goto out; 2343 } 2344 snfi->snfi_frtn.free_func = snf_smap_desbfree; 2345 snfi->snfi_frtn.free_arg = (caddr_t)snfi; 2346 snfi->snfi_base = base; 2347 snfi->snfi_mapoff = mapoff; 2348 mp = esballoca((uchar_t *)base + mapoff, iosize, 2349 BPRI_HI, &snfi->snfi_frtn); 2350 2351 if (mp == NULL) { 2352 (void) segmap_fault(kas.a_hat, segkmap, 2353 (caddr_t)(uintptr_t)(((uintptr_t)base 2354 + mapoff) & PAGEMASK), snfi->snfi_len, 2355 F_SOFTUNLOCK, S_OTHER); 2356 (void) segmap_release(segkmap, base, 0); 2357 kmem_free(snfi, sizeof (*snfi)); 2358 freemsg(mp); 2359 error = EAGAIN; 2360 goto out; 2361 } 2362 VN_HOLD(fvp); 2363 snfi->snfi_vp = fvp; 2364 mp->b_wptr += iosize; 2365 2366 /* Mark this dblk with the zero-copy flag */ 2367 mp->b_datap->db_struioflag |= STRUIO_ZC; 2368 fileoff += iosize; 2369 size -= iosize; 2370 } 2371 2372 if (size == 0 && !nowait) { 2373 ASSERT(!dowait); 2374 dowait = B_TRUE; 2375 mp->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 2376 } 2377 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL); 2378 error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &mp); 2379 if (error != 0) { 2380 if (error == EAGAIN) { 2381 iosz = 0; 2382 mp1 = mp; 2383 while (mp1 != NULL) { 2384 iosz += MBLKL(mp1); 2385 mp1 = mp1->b_cont; 2386 } 2387 *count = ksize + (iosize - iosz); 2388 } else { 2389 *count = ksize; 2390 } 2391 if (mp != NULL) 2392 freemsg(mp); 2393 return (error); 2394 } 2395 ksize += iosize; 2396 if (size == 0) 2397 goto done; 2398 2399 (void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL); 2400 va.va_mask = AT_SIZE; 2401 error = VOP_GETATTR(fvp, &va, 0, kcred, NULL); 2402 if (error) 2403 break; 2404 /* Read as much as possible. */ 2405 if (fileoff >= va.va_size) 2406 break; 2407 if (size + fileoff > va.va_size) 2408 size = va.va_size - fileoff; 2409 } 2410 out: 2411 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL); 2412 done: 2413 *count = ksize; 2414 if (dowait) { 2415 stdata_t *stp; 2416 2417 stp = vp->v_stream; 2418 if (stp == NULL) { 2419 struct sonode *so; 2420 so = VTOSO(vp); 2421 error = so_zcopy_wait(so); 2422 } else { 2423 mutex_enter(&stp->sd_lock); 2424 while (!(stp->sd_flag & STZCNOTIFY)) { 2425 if (cv_wait_sig(&stp->sd_zcopy_wait, 2426 &stp->sd_lock) == 0) { 2427 error = EINTR; 2428 break; 2429 } 2430 } 2431 stp->sd_flag &= ~STZCNOTIFY; 2432 mutex_exit(&stp->sd_lock); 2433 } 2434 } 2435 return (error); 2436 } 2437 2438 int 2439 snf_cache(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t size, 2440 uint_t maxpsz, ssize_t *count) 2441 { 2442 struct vnode *vp; 2443 mblk_t *mp; 2444 int iosize; 2445 int extra = 0; 2446 int error; 2447 short fflag; 2448 int ksize; 2449 int ioflag; 2450 struct uio auio; 2451 struct iovec aiov; 2452 struct vattr va; 2453 int maxblk = 0; 2454 int wroff = 0; 2455 struct sonode *so; 2456 struct nmsghdr msg; 2457 2458 vp = fp->f_vnode; 2459 if (vp->v_type == VSOCK) { 2460 stdata_t *stp; 2461 2462 /* 2463 * Get the extra space to insert a header and a trailer. 2464 */ 2465 so = VTOSO(vp); 2466 stp = vp->v_stream; 2467 if (stp == NULL) { 2468 wroff = so->so_proto_props.sopp_wroff; 2469 maxblk = so->so_proto_props.sopp_maxblk; 2470 extra = wroff + so->so_proto_props.sopp_tail; 2471 } else { 2472 wroff = (int)(stp->sd_wroff); 2473 maxblk = (int)(stp->sd_maxblk); 2474 extra = wroff + (int)(stp->sd_tail); 2475 } 2476 } 2477 bzero(&msg, sizeof (msg)); 2478 fflag = fp->f_flag; 2479 ksize = 0; 2480 auio.uio_iov = &aiov; 2481 auio.uio_iovcnt = 1; 2482 auio.uio_segflg = UIO_SYSSPACE; 2483 auio.uio_llimit = MAXOFFSET_T; 2484 auio.uio_fmode = fflag; 2485 auio.uio_extflg = UIO_COPY_CACHED; 2486 ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC); 2487 /* If read sync is not asked for, filter sync flags */ 2488 if ((ioflag & FRSYNC) == 0) 2489 ioflag &= ~(FSYNC|FDSYNC); 2490 for (;;) { 2491 if (ISSIG(curthread, JUSTLOOKING)) { 2492 error = EINTR; 2493 break; 2494 } 2495 iosize = (int)MIN(maxpsz, size); 2496 2497 /* 2498 * For sockets acting as an SSL proxy, we 2499 * need to adjust the size to the maximum 2500 * SSL record size set in the stream head. 2501 */ 2502 if (vp->v_type == VSOCK && !SOCK_IS_NONSTR(so) && 2503 SOTOTPI(so)->sti_kssl_ctx != NULL) 2504 iosize = (int)MIN(iosize, maxblk); 2505 2506 if (is_system_labeled()) { 2507 mp = allocb_cred(iosize + extra, CRED(), 2508 curproc->p_pid); 2509 } else { 2510 mp = allocb(iosize + extra, BPRI_MED); 2511 } 2512 if (mp == NULL) { 2513 error = EAGAIN; 2514 break; 2515 } 2516 2517 mp->b_rptr += wroff; 2518 2519 aiov.iov_base = (caddr_t)mp->b_rptr; 2520 aiov.iov_len = iosize; 2521 auio.uio_loffset = fileoff; 2522 auio.uio_resid = iosize; 2523 2524 error = VOP_READ(fvp, &auio, ioflag, fp->f_cred, NULL); 2525 iosize -= auio.uio_resid; 2526 2527 if (error == EINTR && iosize != 0) 2528 error = 0; 2529 2530 if (error != 0 || iosize == 0) { 2531 freeb(mp); 2532 break; 2533 } 2534 mp->b_wptr = mp->b_rptr + iosize; 2535 2536 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL); 2537 2538 error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &mp); 2539 2540 if (error != 0) { 2541 *count = ksize; 2542 if (mp != NULL) 2543 freeb(mp); 2544 return (error); 2545 } 2546 ksize += iosize; 2547 size -= iosize; 2548 if (size == 0) 2549 goto done; 2550 2551 fileoff += iosize; 2552 (void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL); 2553 va.va_mask = AT_SIZE; 2554 error = VOP_GETATTR(fvp, &va, 0, kcred, NULL); 2555 if (error) 2556 break; 2557 /* Read as much as possible. */ 2558 if (fileoff >= va.va_size) 2559 size = 0; 2560 else if (size + fileoff > va.va_size) 2561 size = va.va_size - fileoff; 2562 } 2563 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL); 2564 done: 2565 *count = ksize; 2566 return (error); 2567 } 2568 2569 #if defined(_SYSCALL32_IMPL) || defined(_ILP32) 2570 /* 2571 * Largefile support for 32 bit applications only. 2572 */ 2573 int 2574 sosendfile64(file_t *fp, file_t *rfp, const struct ksendfilevec64 *sfv, 2575 ssize32_t *count32) 2576 { 2577 ssize32_t sfv_len; 2578 u_offset_t sfv_off, va_size; 2579 struct vnode *vp, *fvp, *realvp; 2580 struct vattr va; 2581 stdata_t *stp; 2582 ssize_t count = 0; 2583 int error = 0; 2584 boolean_t dozcopy = B_FALSE; 2585 uint_t maxpsz; 2586 2587 sfv_len = (ssize32_t)sfv->sfv_len; 2588 if (sfv_len < 0) { 2589 error = EINVAL; 2590 goto out; 2591 } 2592 2593 if (sfv_len == 0) goto out; 2594 2595 sfv_off = (u_offset_t)sfv->sfv_off; 2596 2597 /* Same checks as in pread */ 2598 if (sfv_off > MAXOFFSET_T) { 2599 error = EINVAL; 2600 goto out; 2601 } 2602 if (sfv_off + sfv_len > MAXOFFSET_T) 2603 sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off); 2604 2605 /* 2606 * There are no more checks on sfv_len. So, we cast it to 2607 * u_offset_t and share the snf_direct_io/snf_cache code between 2608 * 32 bit and 64 bit. 2609 * 2610 * TODO: should do nbl_need_check() like read()? 2611 */ 2612 if (sfv_len > sendfile_max_size) { 2613 sf_stats.ss_file_not_cached++; 2614 error = snf_direct_io(fp, rfp, sfv_off, (u_offset_t)sfv_len, 2615 &count); 2616 goto out; 2617 } 2618 fvp = rfp->f_vnode; 2619 if (VOP_REALVP(fvp, &realvp, NULL) == 0) 2620 fvp = realvp; 2621 /* 2622 * Grab the lock as a reader to prevent the file size 2623 * from changing underneath. 2624 */ 2625 (void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL); 2626 va.va_mask = AT_SIZE; 2627 error = VOP_GETATTR(fvp, &va, 0, kcred, NULL); 2628 va_size = va.va_size; 2629 if ((error != 0) || (va_size == 0) || (sfv_off >= va_size)) { 2630 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL); 2631 goto out; 2632 } 2633 /* Read as much as possible. */ 2634 if (sfv_off + sfv_len > va_size) 2635 sfv_len = va_size - sfv_off; 2636 2637 vp = fp->f_vnode; 2638 stp = vp->v_stream; 2639 /* 2640 * When the NOWAIT flag is not set, we enable zero-copy only if the 2641 * transfer size is large enough. This prevents performance loss 2642 * when the caller sends the file piece by piece. 2643 */ 2644 if (sfv_len >= MAXBSIZE && (sfv_len >= (va_size >> 1) || 2645 (sfv->sfv_flag & SFV_NOWAIT) || sfv_len >= 0x1000000) && 2646 !vn_has_flocks(fvp) && !(fvp->v_flag & VNOMAP)) { 2647 uint_t copyflag; 2648 copyflag = stp != NULL ? stp->sd_copyflag : 2649 VTOSO(vp)->so_proto_props.sopp_zcopyflag; 2650 if ((copyflag & (STZCVMSAFE|STZCVMUNSAFE)) == 0) { 2651 int on = 1; 2652 2653 if (socket_setsockopt(VTOSO(vp), SOL_SOCKET, 2654 SO_SND_COPYAVOID, &on, sizeof (on), CRED()) == 0) 2655 dozcopy = B_TRUE; 2656 } else { 2657 dozcopy = copyflag & STZCVMSAFE; 2658 } 2659 } 2660 if (dozcopy) { 2661 sf_stats.ss_file_segmap++; 2662 error = snf_segmap(fp, fvp, sfv_off, (u_offset_t)sfv_len, 2663 &count, ((sfv->sfv_flag & SFV_NOWAIT) != 0)); 2664 } else { 2665 if (vp->v_type == VSOCK && stp == NULL) { 2666 sonode_t *so = VTOSO(vp); 2667 maxpsz = so->so_proto_props.sopp_maxpsz; 2668 } else if (stp != NULL) { 2669 maxpsz = stp->sd_qn_maxpsz; 2670 } else { 2671 maxpsz = maxphys; 2672 } 2673 2674 if (maxpsz == INFPSZ) 2675 maxpsz = maxphys; 2676 else 2677 maxpsz = roundup(maxpsz, MAXBSIZE); 2678 sf_stats.ss_file_cached++; 2679 error = snf_cache(fp, fvp, sfv_off, (u_offset_t)sfv_len, 2680 maxpsz, &count); 2681 } 2682 out: 2683 releasef(sfv->sfv_fd); 2684 *count32 = (ssize32_t)count; 2685 return (error); 2686 } 2687 #endif 2688 2689 #ifdef _SYSCALL32_IMPL 2690 /* 2691 * recv32(), recvfrom32(), send32(), sendto32(): intentionally return a 2692 * ssize_t rather than ssize32_t; see the comments above read32 for details. 2693 */ 2694 2695 ssize_t 2696 recv32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags) 2697 { 2698 return (recv(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags)); 2699 } 2700 2701 ssize_t 2702 recvfrom32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags, 2703 caddr32_t name, caddr32_t namelenp) 2704 { 2705 return (recvfrom(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags, 2706 (void *)(uintptr_t)name, (void *)(uintptr_t)namelenp)); 2707 } 2708 2709 ssize_t 2710 send32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags) 2711 { 2712 return (send(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags)); 2713 } 2714 2715 ssize_t 2716 sendto32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags, 2717 caddr32_t name, socklen_t namelen) 2718 { 2719 return (sendto(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags, 2720 (void *)(uintptr_t)name, namelen)); 2721 } 2722 #endif /* _SYSCALL32_IMPL */ 2723 2724 /* 2725 * Function wrappers (mostly around the sonode switch) for 2726 * backward compatibility. 2727 */ 2728 2729 int 2730 soaccept(struct sonode *so, int fflag, struct sonode **nsop) 2731 { 2732 return (socket_accept(so, fflag, CRED(), nsop)); 2733 } 2734 2735 int 2736 sobind(struct sonode *so, struct sockaddr *name, socklen_t namelen, 2737 int backlog, int flags) 2738 { 2739 int error; 2740 2741 error = socket_bind(so, name, namelen, flags, CRED()); 2742 if (error == 0 && backlog != 0) 2743 return (socket_listen(so, backlog, CRED())); 2744 2745 return (error); 2746 } 2747 2748 int 2749 solisten(struct sonode *so, int backlog) 2750 { 2751 return (socket_listen(so, backlog, CRED())); 2752 } 2753 2754 int 2755 soconnect(struct sonode *so, const struct sockaddr *name, socklen_t namelen, 2756 int fflag, int flags) 2757 { 2758 return (socket_connect(so, name, namelen, fflag, flags, CRED())); 2759 } 2760 2761 int 2762 sorecvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop) 2763 { 2764 return (socket_recvmsg(so, msg, uiop, CRED())); 2765 } 2766 2767 int 2768 sosendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop) 2769 { 2770 return (socket_sendmsg(so, msg, uiop, CRED())); 2771 } 2772 2773 int 2774 soshutdown(struct sonode *so, int how) 2775 { 2776 return (socket_shutdown(so, how, CRED())); 2777 } 2778 2779 int 2780 sogetsockopt(struct sonode *so, int level, int option_name, void *optval, 2781 socklen_t *optlenp, int flags) 2782 { 2783 return (socket_getsockopt(so, level, option_name, optval, optlenp, 2784 flags, CRED())); 2785 } 2786 2787 int 2788 sosetsockopt(struct sonode *so, int level, int option_name, const void *optval, 2789 t_uscalar_t optlen) 2790 { 2791 return (socket_setsockopt(so, level, option_name, optval, optlen, 2792 CRED())); 2793 } 2794 2795 /* 2796 * Because this is backward compatibility interface it only needs to be 2797 * able to handle the creation of TPI sockfs sockets. 2798 */ 2799 struct sonode * 2800 socreate(struct sockparams *sp, int family, int type, int protocol, int version, 2801 int *errorp) 2802 { 2803 struct sonode *so; 2804 2805 ASSERT(sp != NULL); 2806 2807 so = sp->sp_smod_info->smod_sock_create_func(sp, family, type, protocol, 2808 version, SOCKET_SLEEP, errorp, CRED()); 2809 if (so == NULL) { 2810 SOCKPARAMS_DEC_REF(sp); 2811 } else { 2812 if ((*errorp = SOP_INIT(so, NULL, CRED(), SOCKET_SLEEP)) == 0) { 2813 /* Cannot fail, only bumps so_count */ 2814 (void) VOP_OPEN(&SOTOV(so), FREAD|FWRITE, CRED(), NULL); 2815 } else { 2816 socket_destroy(so); 2817 so = NULL; 2818 } 2819 } 2820 return (so); 2821 } 2822