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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "@(#)sockcommon_sops.c 1.1 07/06/14 SMI" 28 29 #include <sys/types.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/sysmacros.h> 33 #include <sys/debug.h> 34 #include <sys/cmn_err.h> 35 36 #include <sys/stropts.h> 37 #include <sys/socket.h> 38 #include <sys/socketvar.h> 39 40 #define _SUN_TPI_VERSION 2 41 #include <sys/tihdr.h> 42 #include <sys/sockio.h> 43 #include <sys/sodirect.h> 44 #include <sys/kmem_impl.h> 45 46 #include <sys/strsubr.h> 47 #include <sys/strsun.h> 48 #include <sys/ddi.h> 49 #include <netinet/in.h> 50 #include <inet/ip.h> 51 52 #include <fs/sockfs/sockcommon.h> 53 54 #include <sys/socket_proto.h> 55 56 #include <fs/sockfs/socktpi_impl.h> 57 #include <sys/tihdr.h> 58 #include <fs/sockfs/nl7c.h> 59 #include <inet/kssl/ksslapi.h> 60 61 62 extern int xnet_skip_checks; 63 extern int xnet_check_print; 64 65 static void so_queue_oob(sock_upper_handle_t, mblk_t *, size_t); 66 67 68 /*ARGSUSED*/ 69 int 70 so_accept_notsupp(struct sonode *lso, int fflag, 71 struct cred *cr, struct sonode **nsop) 72 { 73 return (EOPNOTSUPP); 74 } 75 76 /*ARGSUSED*/ 77 int 78 so_listen_notsupp(struct sonode *so, int backlog, struct cred *cr) 79 { 80 return (EOPNOTSUPP); 81 } 82 83 /*ARGSUSED*/ 84 int 85 so_getsockname_notsupp(struct sonode *so, struct sockaddr *sa, 86 socklen_t *len, struct cred *cr) 87 { 88 return (EOPNOTSUPP); 89 } 90 91 /*ARGSUSED*/ 92 int 93 so_getpeername_notsupp(struct sonode *so, struct sockaddr *addr, 94 socklen_t *addrlen, boolean_t accept, struct cred *cr) 95 { 96 return (EOPNOTSUPP); 97 } 98 99 /*ARGSUSED*/ 100 int 101 so_shutdown_notsupp(struct sonode *so, int how, struct cred *cr) 102 { 103 return (EOPNOTSUPP); 104 } 105 106 /*ARGSUSED*/ 107 int 108 so_sendmblk_notsupp(struct sonode *so, struct msghdr *msg, int fflag, 109 struct cred *cr, mblk_t **mpp) 110 { 111 return (EOPNOTSUPP); 112 } 113 114 /* 115 * Generic Socket Ops 116 */ 117 118 /* ARGSUSED */ 119 int 120 so_init(struct sonode *so, struct sonode *pso, struct cred *cr, int flags) 121 { 122 return (socket_init_common(so, pso, flags, cr)); 123 } 124 125 int 126 so_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen, 127 int flags, struct cred *cr) 128 { 129 int error; 130 131 SO_BLOCK_FALLBACK(so, SOP_BIND(so, name, namelen, flags, cr)); 132 133 ASSERT(flags == _SOBIND_XPG4_2 || flags == _SOBIND_SOCKBSD); 134 135 /* X/Open requires this check */ 136 if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 137 if (xnet_check_print) { 138 printf("sockfs: X/Open bind state check " 139 "caused EINVAL\n"); 140 } 141 error = EINVAL; 142 goto done; 143 } 144 145 /* 146 * a bind to a NULL address is interpreted as unbind. So just 147 * do the downcall. 148 */ 149 if (name == NULL) 150 goto dobind; 151 152 switch (so->so_family) { 153 case AF_INET: 154 if ((size_t)namelen != sizeof (sin_t)) { 155 error = name->sa_family != so->so_family ? 156 EAFNOSUPPORT : EINVAL; 157 eprintsoline(so, error); 158 goto done; 159 } 160 161 if ((flags & _SOBIND_XPG4_2) && 162 (name->sa_family != so->so_family)) { 163 /* 164 * This check has to be made for X/Open 165 * sockets however application failures have 166 * been observed when it is applied to 167 * all sockets. 168 */ 169 error = EAFNOSUPPORT; 170 eprintsoline(so, error); 171 goto done; 172 } 173 /* 174 * Force a zero sa_family to match so_family. 175 * 176 * Some programs like inetd(1M) don't set the 177 * family field. Other programs leave 178 * sin_family set to garbage - SunOS 4.X does 179 * not check the family field on a bind. 180 * We use the family field that 181 * was passed in to the socket() call. 182 */ 183 name->sa_family = so->so_family; 184 break; 185 186 case AF_INET6: { 187 #ifdef DEBUG 188 sin6_t *sin6 = (sin6_t *)name; 189 #endif 190 if ((size_t)namelen != sizeof (sin6_t)) { 191 error = name->sa_family != so->so_family ? 192 EAFNOSUPPORT : EINVAL; 193 eprintsoline(so, error); 194 goto done; 195 } 196 197 if (name->sa_family != so->so_family) { 198 /* 199 * With IPv6 we require the family to match 200 * unlike in IPv4. 201 */ 202 error = EAFNOSUPPORT; 203 eprintsoline(so, error); 204 goto done; 205 } 206 #ifdef DEBUG 207 /* 208 * Verify that apps don't forget to clear 209 * sin6_scope_id etc 210 */ 211 if (sin6->sin6_scope_id != 0 && 212 !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 213 zcmn_err(getzoneid(), CE_WARN, 214 "bind with uninitialized sin6_scope_id " 215 "(%d) on socket. Pid = %d\n", 216 (int)sin6->sin6_scope_id, 217 (int)curproc->p_pid); 218 } 219 if (sin6->__sin6_src_id != 0) { 220 zcmn_err(getzoneid(), CE_WARN, 221 "bind with uninitialized __sin6_src_id " 222 "(%d) on socket. Pid = %d\n", 223 (int)sin6->__sin6_src_id, 224 (int)curproc->p_pid); 225 } 226 #endif /* DEBUG */ 227 228 break; 229 } 230 default: 231 /* Just pass the request to the protocol */ 232 goto dobind; 233 } 234 235 /* 236 * First we check if either NCA or KSSL has been enabled for 237 * the requested address, and if so, we fall back to TPI. 238 * If neither of those two services are enabled, then we just 239 * pass the request to the protocol. 240 * 241 * Note that KSSL can only be enabled on a socket if NCA is NOT 242 * enabled for that socket, hence the else-statement below. 243 */ 244 if (nl7c_enabled && ((so->so_family == AF_INET || 245 so->so_family == AF_INET6) && 246 nl7c_lookup_addr(name, namelen) != NULL)) { 247 /* 248 * NL7C is not supported in non-global zones, 249 * we enforce this restriction here. 250 */ 251 if (so->so_zoneid == GLOBAL_ZONEID) { 252 /* NCA should be used, so fall back to TPI */ 253 error = so_tpi_fallback(so, cr); 254 SO_UNBLOCK_FALLBACK(so); 255 if (error) 256 return (error); 257 else 258 return (SOP_BIND(so, name, namelen, flags, cr)); 259 } 260 } else if (so->so_type == SOCK_STREAM) { 261 /* Check if KSSL has been configured for this address */ 262 kssl_ent_t ent; 263 kssl_endpt_type_t type; 264 struct T_bind_req bind_req; 265 mblk_t *mp; 266 267 /* 268 * TODO: Check with KSSL team if we could add a function call 269 * that only queries whether KSSL is enabled for the given 270 * address. 271 */ 272 bind_req.PRIM_type = T_BIND_REQ; 273 bind_req.ADDR_length = namelen; 274 bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req); 275 mp = soallocproto2(&bind_req, sizeof (bind_req), 276 name, namelen, 0, _ALLOC_SLEEP); 277 278 type = kssl_check_proxy(mp, so, &ent); 279 freemsg(mp); 280 281 if (type != KSSL_NO_PROXY) { 282 /* 283 * KSSL has been configured for this address, so 284 * we must fall back to TPI. 285 */ 286 kssl_release_ent(ent, so, type); 287 error = so_tpi_fallback(so, cr); 288 SO_UNBLOCK_FALLBACK(so); 289 if (error) 290 return (error); 291 else 292 return (SOP_BIND(so, name, namelen, flags, cr)); 293 } 294 } 295 296 dobind: 297 error = (*so->so_downcalls->sd_bind) 298 (so->so_proto_handle, name, namelen, cr); 299 done: 300 SO_UNBLOCK_FALLBACK(so); 301 302 return (error); 303 } 304 305 int 306 so_listen(struct sonode *so, int backlog, struct cred *cr) 307 { 308 int error = 0; 309 310 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 311 SO_BLOCK_FALLBACK(so, SOP_LISTEN(so, backlog, cr)); 312 313 error = (*so->so_downcalls->sd_listen)(so->so_proto_handle, backlog, 314 cr); 315 316 SO_UNBLOCK_FALLBACK(so); 317 318 return (error); 319 } 320 321 322 int 323 so_connect(struct sonode *so, const struct sockaddr *name, 324 socklen_t namelen, int fflag, int flags, struct cred *cr) 325 { 326 int error = 0; 327 sock_connid_t id; 328 329 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 330 SO_BLOCK_FALLBACK(so, SOP_CONNECT(so, name, namelen, fflag, flags, cr)); 331 332 /* 333 * If there is a pending error, return error 334 * This can happen if a non blocking operation caused an error. 335 */ 336 337 if (so->so_error != 0) { 338 mutex_enter(&so->so_lock); 339 error = sogeterr(so, B_TRUE); 340 mutex_exit(&so->so_lock); 341 if (error != 0) 342 goto done; 343 } 344 345 error = (*so->so_downcalls->sd_connect)(so->so_proto_handle, 346 name, namelen, &id, cr); 347 348 if (error == EINPROGRESS) 349 error = so_wait_connected(so, fflag & (FNONBLOCK|FNDELAY), id); 350 351 done: 352 SO_UNBLOCK_FALLBACK(so); 353 return (error); 354 } 355 356 /*ARGSUSED*/ 357 int 358 so_accept(struct sonode *so, int fflag, struct cred *cr, struct sonode **nsop) 359 { 360 int error = 0; 361 struct sonode *nso; 362 363 *nsop = NULL; 364 365 SO_BLOCK_FALLBACK(so, SOP_ACCEPT(so, fflag, cr, nsop)); 366 if ((so->so_state & SS_ACCEPTCONN) == 0) { 367 SO_UNBLOCK_FALLBACK(so); 368 return ((so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW) ? 369 EOPNOTSUPP : EINVAL); 370 } 371 372 if ((error = so_acceptq_dequeue(so, (fflag & (FNONBLOCK|FNDELAY)), 373 &nso)) == 0) { 374 ASSERT(nso != NULL); 375 376 /* finish the accept */ 377 error = (*so->so_downcalls->sd_accept)(so->so_proto_handle, 378 nso->so_proto_handle, (sock_upper_handle_t)nso, cr); 379 if (error != 0) { 380 (void) socket_close(nso, 0, cr); 381 socket_destroy(nso); 382 } else { 383 *nsop = nso; 384 } 385 } 386 387 SO_UNBLOCK_FALLBACK(so); 388 return (error); 389 } 390 391 int 392 so_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, 393 struct cred *cr) 394 { 395 int error, flags; 396 boolean_t dontblock; 397 ssize_t orig_resid; 398 mblk_t *mp; 399 400 SO_BLOCK_FALLBACK(so, SOP_SENDMSG(so, msg, uiop, cr)); 401 402 flags = msg->msg_flags; 403 error = 0; 404 dontblock = (flags & MSG_DONTWAIT) || 405 (uiop->uio_fmode & (FNONBLOCK|FNDELAY)); 406 407 if (!(flags & MSG_XPG4_2) && msg->msg_controllen != 0) { 408 /* 409 * Old way of passing fd's is not supported 410 */ 411 SO_UNBLOCK_FALLBACK(so); 412 return (EOPNOTSUPP); 413 } 414 415 if ((so->so_mode & SM_ATOMIC) && 416 uiop->uio_resid > so->so_proto_props.sopp_maxpsz && 417 so->so_proto_props.sopp_maxpsz != -1) { 418 SO_UNBLOCK_FALLBACK(so); 419 return (EMSGSIZE); 420 } 421 422 /* 423 * For atomic sends we will only do one iteration. 424 */ 425 do { 426 if (so->so_state & SS_CANTSENDMORE) { 427 error = EPIPE; 428 break; 429 } 430 431 if (so->so_error != 0) { 432 mutex_enter(&so->so_lock); 433 error = sogeterr(so, B_TRUE); 434 mutex_exit(&so->so_lock); 435 if (error != 0) 436 break; 437 } 438 439 /* 440 * Send down OOB messages even if the send path is being 441 * flow controlled (assuming the protocol supports OOB data). 442 */ 443 if (flags & MSG_OOB) { 444 if ((so->so_mode & SM_EXDATA) == 0) { 445 error = EOPNOTSUPP; 446 break; 447 } 448 } else if (so->so_snd_qfull) { 449 /* 450 * Need to wait until the protocol is ready to receive 451 * more data for transmission. 452 */ 453 if ((error = so_snd_wait_qnotfull(so, dontblock)) != 0) 454 break; 455 } 456 457 /* 458 * Time to send data to the protocol. We either copy the 459 * data into mblks or pass the uio directly to the protocol. 460 * We decide what to do based on the available down calls. 461 */ 462 if (so->so_downcalls->sd_send_uio != NULL) { 463 error = (*so->so_downcalls->sd_send_uio) 464 (so->so_proto_handle, uiop, msg, cr); 465 if (error != 0) 466 break; 467 } else { 468 /* save the resid in case of failure */ 469 orig_resid = uiop->uio_resid; 470 471 if ((mp = socopyinuio(uiop, 472 so->so_proto_props.sopp_maxpsz, 473 so->so_proto_props.sopp_wroff, 474 so->so_proto_props.sopp_maxblk, 475 so->so_proto_props.sopp_tail, &error)) == NULL) { 476 break; 477 } 478 ASSERT(uiop->uio_resid >= 0); 479 480 error = (*so->so_downcalls->sd_send) 481 (so->so_proto_handle, mp, msg, cr); 482 if (error != 0) { 483 /* 484 * The send failed. We do not have to free the 485 * mblks, because that is the protocol's 486 * responsibility. However, uio_resid must 487 * remain accurate, so adjust that here. 488 */ 489 uiop->uio_resid = orig_resid; 490 break; 491 } 492 } 493 } while (uiop->uio_resid > 0); 494 495 SO_UNBLOCK_FALLBACK(so); 496 497 return (error); 498 } 499 500 int 501 so_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag, 502 struct cred *cr, mblk_t **mpp) 503 { 504 int error; 505 boolean_t dontblock; 506 size_t size; 507 mblk_t *mp = *mpp; 508 509 SO_BLOCK_FALLBACK(so, SOP_SENDMBLK(so, msg, fflag, cr, mpp)); 510 511 error = 0; 512 dontblock = (msg->msg_flags & MSG_DONTWAIT) || 513 (fflag & (FNONBLOCK|FNDELAY)); 514 size = msgdsize(mp); 515 516 if (so->so_downcalls->sd_send == NULL) { 517 SO_UNBLOCK_FALLBACK(so); 518 return (EOPNOTSUPP); 519 } 520 521 if ((so->so_mode & SM_ATOMIC) && 522 size > so->so_proto_props.sopp_maxpsz && 523 so->so_proto_props.sopp_maxpsz != -1) { 524 SO_UNBLOCK_FALLBACK(so); 525 return (EMSGSIZE); 526 } 527 528 while (mp != NULL) { 529 mblk_t *nmp, *last_mblk; 530 size_t mlen; 531 532 if (so->so_state & SS_CANTSENDMORE) { 533 error = EPIPE; 534 break; 535 } 536 if (so->so_error != 0) { 537 mutex_enter(&so->so_lock); 538 error = sogeterr(so, B_TRUE); 539 mutex_exit(&so->so_lock); 540 if (error != 0) 541 break; 542 } 543 if (so->so_snd_qfull) { 544 /* 545 * Need to wait until the protocol is ready to receive 546 * more data for transmission. 547 */ 548 if ((error = so_snd_wait_qnotfull(so, dontblock)) != 0) 549 break; 550 } 551 552 /* 553 * We only allow so_maxpsz of data to be sent down to 554 * the protocol at time. 555 */ 556 mlen = MBLKL(mp); 557 nmp = mp->b_cont; 558 last_mblk = mp; 559 while (nmp != NULL) { 560 mlen += MBLKL(nmp); 561 if (mlen > so->so_proto_props.sopp_maxpsz) { 562 last_mblk->b_cont = NULL; 563 break; 564 } 565 last_mblk = nmp; 566 nmp = nmp->b_cont; 567 } 568 569 error = (*so->so_downcalls->sd_send) 570 (so->so_proto_handle, mp, msg, cr); 571 if (error != 0) { 572 /* 573 * The send failed. The protocol will free the mblks 574 * that were sent down. Let the caller deal with the 575 * rest. 576 */ 577 *mpp = nmp; 578 break; 579 } 580 581 *mpp = mp = nmp; 582 } 583 584 SO_UNBLOCK_FALLBACK(so); 585 586 return (error); 587 } 588 589 int 590 so_shutdown(struct sonode *so, int how, struct cred *cr) 591 { 592 int error; 593 594 SO_BLOCK_FALLBACK(so, SOP_SHUTDOWN(so, how, cr)); 595 596 /* 597 * SunOS 4.X has no check for datagram sockets. 598 * 5.X checks that it is connected (ENOTCONN) 599 * X/Open requires that we check the connected state. 600 */ 601 if (!(so->so_state & SS_ISCONNECTED)) { 602 if (!xnet_skip_checks) { 603 error = ENOTCONN; 604 if (xnet_check_print) { 605 printf("sockfs: X/Open shutdown check " 606 "caused ENOTCONN\n"); 607 } 608 } 609 goto done; 610 } 611 612 error = ((*so->so_downcalls->sd_shutdown)(so->so_proto_handle, 613 how, cr)); 614 615 /* 616 * Protocol agreed to shutdown. We need to flush the 617 * receive buffer if the receive side is being shutdown. 618 */ 619 if (error == 0 && how != SHUT_WR) { 620 mutex_enter(&so->so_lock); 621 /* wait for active reader to finish */ 622 (void) so_lock_read(so, 0); 623 624 so_rcv_flush(so); 625 626 so_unlock_read(so); 627 mutex_exit(&so->so_lock); 628 } 629 630 done: 631 SO_UNBLOCK_FALLBACK(so); 632 return (error); 633 } 634 635 int 636 so_getsockname(struct sonode *so, struct sockaddr *addr, 637 socklen_t *addrlen, struct cred *cr) 638 { 639 int error; 640 641 SO_BLOCK_FALLBACK(so, SOP_GETSOCKNAME(so, addr, addrlen, cr)); 642 643 error = (*so->so_downcalls->sd_getsockname) 644 (so->so_proto_handle, addr, addrlen, cr); 645 646 SO_UNBLOCK_FALLBACK(so); 647 return (error); 648 } 649 650 int 651 so_getpeername(struct sonode *so, struct sockaddr *addr, 652 socklen_t *addrlen, boolean_t accept, struct cred *cr) 653 { 654 int error; 655 656 SO_BLOCK_FALLBACK(so, SOP_GETPEERNAME(so, addr, addrlen, accept, cr)); 657 658 if (accept) { 659 error = (*so->so_downcalls->sd_getpeername) 660 (so->so_proto_handle, addr, addrlen, cr); 661 } else if (!(so->so_state & SS_ISCONNECTED)) { 662 error = ENOTCONN; 663 } else if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 664 /* Added this check for X/Open */ 665 error = EINVAL; 666 if (xnet_check_print) { 667 printf("sockfs: X/Open getpeername check => EINVAL\n"); 668 } 669 } else { 670 error = (*so->so_downcalls->sd_getpeername) 671 (so->so_proto_handle, addr, addrlen, cr); 672 } 673 674 SO_UNBLOCK_FALLBACK(so); 675 return (error); 676 } 677 678 int 679 so_getsockopt(struct sonode *so, int level, int option_name, 680 void *optval, socklen_t *optlenp, int flags, struct cred *cr) 681 { 682 int error = 0; 683 684 ASSERT(MUTEX_NOT_HELD(&so->so_lock)); 685 SO_BLOCK_FALLBACK(so, 686 SOP_GETSOCKOPT(so, level, option_name, optval, optlenp, flags, cr)); 687 688 error = socket_getopt_common(so, level, option_name, optval, 689 optlenp); 690 if (error < 0) { 691 error = (*so->so_downcalls->sd_getsockopt) 692 (so->so_proto_handle, level, option_name, optval, optlenp, 693 cr); 694 if (error == ENOPROTOOPT) { 695 if (level == SOL_SOCKET) { 696 /* 697 * If a protocol does not support a particular 698 * socket option, set can fail (not allowed) 699 * but get can not fail. This is the previous 700 * sockfs bahvior. 701 */ 702 switch (option_name) { 703 case SO_LINGER: 704 if (*optlenp < (t_uscalar_t) 705 sizeof (struct linger)) { 706 error = EINVAL; 707 break; 708 } 709 error = 0; 710 bzero(optval, sizeof (struct linger)); 711 *optlenp = sizeof (struct linger); 712 break; 713 case SO_RCVTIMEO: 714 case SO_SNDTIMEO: 715 if (*optlenp < (t_uscalar_t) 716 sizeof (struct timeval)) { 717 error = EINVAL; 718 break; 719 } 720 error = 0; 721 bzero(optval, sizeof (struct timeval)); 722 *optlenp = sizeof (struct timeval); 723 break; 724 case SO_SND_BUFINFO: 725 if (*optlenp < (t_uscalar_t) 726 sizeof (struct so_snd_bufinfo)) { 727 error = EINVAL; 728 break; 729 } 730 error = 0; 731 bzero(optval, 732 sizeof (struct so_snd_bufinfo)); 733 *optlenp = 734 sizeof (struct so_snd_bufinfo); 735 break; 736 case SO_DEBUG: 737 case SO_REUSEADDR: 738 case SO_KEEPALIVE: 739 case SO_DONTROUTE: 740 case SO_BROADCAST: 741 case SO_USELOOPBACK: 742 case SO_OOBINLINE: 743 case SO_DGRAM_ERRIND: 744 case SO_SNDBUF: 745 case SO_RCVBUF: 746 error = 0; 747 *((int32_t *)optval) = 0; 748 *optlenp = sizeof (int32_t); 749 break; 750 default: 751 break; 752 } 753 } 754 } 755 } 756 757 SO_UNBLOCK_FALLBACK(so); 758 return (error); 759 } 760 761 int 762 so_setsockopt(struct sonode *so, int level, int option_name, 763 const void *optval, socklen_t optlen, struct cred *cr) 764 { 765 int error = 0; 766 767 SO_BLOCK_FALLBACK(so, 768 SOP_SETSOCKOPT(so, level, option_name, optval, optlen, cr)); 769 770 /* X/Open requires this check */ 771 if (so->so_state & SS_CANTSENDMORE && !xnet_skip_checks) { 772 SO_UNBLOCK_FALLBACK(so); 773 if (xnet_check_print) 774 printf("sockfs: X/Open setsockopt check => EINVAL\n"); 775 return (EINVAL); 776 } 777 778 if (level == SOL_SOCKET && 779 ((option_name == SO_RCVTIMEO) || (option_name == SO_SNDTIMEO))) { 780 struct timeval *tl = (struct timeval *)optval; 781 clock_t t_usec; 782 783 if (optlen != (t_uscalar_t)sizeof (struct timeval)) { 784 SO_UNBLOCK_FALLBACK(so); 785 return (EINVAL); 786 } 787 t_usec = tl->tv_sec * 1000 * 1000 + tl->tv_usec; 788 mutex_enter(&so->so_lock); 789 if (option_name == SO_RCVTIMEO) 790 so->so_rcvtimeo = drv_usectohz(t_usec); 791 else 792 so->so_sndtimeo = drv_usectohz(t_usec); 793 mutex_exit(&so->so_lock); 794 SO_UNBLOCK_FALLBACK(so); 795 return (0); 796 } 797 error = (*so->so_downcalls->sd_setsockopt) 798 (so->so_proto_handle, level, option_name, optval, optlen, cr); 799 800 SO_UNBLOCK_FALLBACK(so); 801 return (error); 802 } 803 804 int 805 so_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode, 806 struct cred *cr, int32_t *rvalp) 807 { 808 int error = 0; 809 810 SO_BLOCK_FALLBACK(so, SOP_IOCTL(so, cmd, arg, mode, cr, rvalp)); 811 812 /* 813 * If there is a pending error, return error 814 * This can happen if a non blocking operation caused an error. 815 */ 816 if (so->so_error != 0) { 817 mutex_enter(&so->so_lock); 818 error = sogeterr(so, B_TRUE); 819 mutex_exit(&so->so_lock); 820 if (error != 0) 821 goto done; 822 } 823 824 /* 825 * calling strioc can result in the socket falling back to TPI, 826 * if that is supported. 827 */ 828 if ((error = socket_ioctl_common(so, cmd, arg, mode, cr, rvalp)) < 0 && 829 (error = socket_strioc_common(so, cmd, arg, mode, cr, rvalp)) < 0) { 830 error = (*so->so_downcalls->sd_ioctl)(so->so_proto_handle, 831 cmd, arg, mode, rvalp, cr); 832 } 833 834 done: 835 SO_UNBLOCK_FALLBACK(so); 836 837 return (error); 838 } 839 840 int 841 so_poll(struct sonode *so, short events, int anyyet, short *reventsp, 842 struct pollhead **phpp) 843 { 844 int state = so->so_state; 845 *reventsp = 0; 846 847 if (so->so_error != 0 && 848 ((POLLIN|POLLRDNORM|POLLOUT) & events) != 0) { 849 *reventsp = (POLLIN|POLLRDNORM|POLLOUT) & events; 850 return (0); 851 } 852 853 /* 854 * As long as there is buffer to send data, and the socket is 855 * in a state where it can send data (i.e., connected for 856 * connection oriented protocols), then turn on POLLOUT events 857 */ 858 if (!so->so_snd_qfull && ((so->so_mode & SM_CONNREQUIRED) == 0 || 859 state & SS_ISCONNECTED)) { 860 *reventsp |= POLLOUT & events; 861 } 862 863 /* 864 * Turn on POLLIN whenever there is data on the receive queue, 865 * or the socket is in a state where no more data will be received. 866 * Also, if the socket is accepting connections, flip the bit if 867 * there is something on the queue. 868 */ 869 870 /* Pending connections */ 871 if (so->so_acceptq_len > 0) 872 *reventsp |= (POLLIN|POLLRDNORM) & events; 873 874 /* Data */ 875 /* so_downcalls is null for sctp */ 876 if (so->so_downcalls != NULL && so->so_downcalls->sd_poll != NULL) { 877 *reventsp |= (*so->so_downcalls->sd_poll) 878 (so->so_proto_handle, events & SO_PROTO_POLLEV, anyyet, 879 CRED()) & events; 880 ASSERT((*reventsp & ~events) == 0); 881 /* do not recheck events */ 882 events &= ~SO_PROTO_POLLEV; 883 } else { 884 if (SO_HAVE_DATA(so)) 885 *reventsp |= (POLLIN|POLLRDNORM) & events; 886 887 /* Urgent data */ 888 if ((state & SS_OOBPEND) != 0) 889 *reventsp |= (POLLRDBAND) & events; 890 } 891 892 if (!*reventsp && !anyyet) { 893 /* Check for read events again, but this time under lock */ 894 if (events & (POLLIN|POLLRDNORM)) { 895 mutex_enter(&so->so_lock); 896 if (SO_HAVE_DATA(so) || so->so_acceptq_len > 0) { 897 mutex_exit(&so->so_lock); 898 *reventsp |= (POLLIN|POLLRDNORM) & events; 899 return (0); 900 } else { 901 so->so_pollev |= SO_POLLEV_IN; 902 mutex_exit(&so->so_lock); 903 } 904 } 905 *phpp = &so->so_poll_list; 906 } 907 return (0); 908 } 909 910 /* 911 * Generic Upcalls 912 */ 913 void 914 so_connected(sock_upper_handle_t sock_handle, sock_connid_t id, 915 cred_t *peer_cred, pid_t peer_cpid) 916 { 917 struct sonode *so = (struct sonode *)sock_handle; 918 919 mutex_enter(&so->so_lock); 920 ASSERT(so->so_proto_handle != NULL); 921 922 if (peer_cred != NULL) { 923 if (so->so_peercred != NULL) 924 crfree(so->so_peercred); 925 crhold(peer_cred); 926 so->so_peercred = peer_cred; 927 so->so_cpid = peer_cpid; 928 } 929 930 so->so_proto_connid = id; 931 soisconnected(so); 932 /* 933 * Wake ones who're waiting for conn to become established. 934 */ 935 so_notify_connected(so); 936 } 937 938 int 939 so_disconnected(sock_upper_handle_t sock_handle, sock_connid_t id, int error) 940 { 941 struct sonode *so = (struct sonode *)sock_handle; 942 943 mutex_enter(&so->so_lock); 944 945 so->so_proto_connid = id; 946 soisdisconnected(so, error); 947 so_notify_disconnected(so, error); 948 949 return (0); 950 } 951 952 void 953 so_opctl(sock_upper_handle_t sock_handle, sock_opctl_action_t action, 954 uintptr_t arg) 955 { 956 struct sonode *so = (struct sonode *)sock_handle; 957 958 switch (action) { 959 case SOCK_OPCTL_SHUT_SEND: 960 mutex_enter(&so->so_lock); 961 socantsendmore(so); 962 so_notify_disconnecting(so); 963 break; 964 case SOCK_OPCTL_SHUT_RECV: { 965 mutex_enter(&so->so_lock); 966 socantrcvmore(so); 967 so_notify_eof(so); 968 break; 969 } 970 case SOCK_OPCTL_ENAB_ACCEPT: 971 mutex_enter(&so->so_lock); 972 so->so_state |= SS_ACCEPTCONN; 973 so->so_backlog = (unsigned int)arg; 974 mutex_exit(&so->so_lock); 975 break; 976 default: 977 ASSERT(0); 978 break; 979 } 980 } 981 982 void 983 so_txq_full(sock_upper_handle_t sock_handle, boolean_t qfull) 984 { 985 struct sonode *so = (struct sonode *)sock_handle; 986 987 if (qfull) { 988 so_snd_qfull(so); 989 } else { 990 so_snd_qnotfull(so); 991 mutex_enter(&so->so_lock); 992 so_notify_writable(so); 993 } 994 } 995 996 sock_upper_handle_t 997 so_newconn(sock_upper_handle_t parenthandle, 998 sock_lower_handle_t proto_handle, sock_downcalls_t *sock_downcalls, 999 struct cred *peer_cred, pid_t peer_cpid, sock_upcalls_t **sock_upcallsp) 1000 { 1001 struct sonode *so = (struct sonode *)parenthandle; 1002 struct sonode *nso; 1003 int error; 1004 1005 ASSERT(proto_handle != NULL); 1006 1007 if ((so->so_state & SS_ACCEPTCONN) == 0 || 1008 so->so_acceptq_len >= so->so_backlog) 1009 return (NULL); 1010 1011 nso = socket_newconn(so, proto_handle, sock_downcalls, SOCKET_NOSLEEP, 1012 &error); 1013 if (nso == NULL) 1014 return (NULL); 1015 1016 if (peer_cred != NULL) { 1017 crhold(peer_cred); 1018 nso->so_peercred = peer_cred; 1019 nso->so_cpid = peer_cpid; 1020 } 1021 1022 (void) so_acceptq_enqueue(so, nso); 1023 mutex_enter(&so->so_lock); 1024 so_notify_newconn(so); 1025 1026 *sock_upcallsp = &so_upcalls; 1027 1028 return ((sock_upper_handle_t)nso); 1029 } 1030 1031 void 1032 so_set_prop(sock_upper_handle_t sock_handle, struct sock_proto_props *soppp) 1033 { 1034 struct sonode *so; 1035 1036 so = (struct sonode *)sock_handle; 1037 1038 mutex_enter(&so->so_lock); 1039 1040 if (soppp->sopp_flags & SOCKOPT_MAXBLK) 1041 so->so_proto_props.sopp_maxblk = soppp->sopp_maxblk; 1042 if (soppp->sopp_flags & SOCKOPT_WROFF) 1043 so->so_proto_props.sopp_wroff = soppp->sopp_wroff; 1044 if (soppp->sopp_flags & SOCKOPT_TAIL) 1045 so->so_proto_props.sopp_tail = soppp->sopp_tail; 1046 if (soppp->sopp_flags & SOCKOPT_RCVHIWAT) 1047 so->so_proto_props.sopp_rxhiwat = soppp->sopp_rxhiwat; 1048 if (soppp->sopp_flags & SOCKOPT_RCVLOWAT) 1049 so->so_proto_props.sopp_rxlowat = soppp->sopp_rxlowat; 1050 if (soppp->sopp_flags & SOCKOPT_MAXPSZ) 1051 so->so_proto_props.sopp_maxpsz = soppp->sopp_maxpsz; 1052 if (soppp->sopp_flags & SOCKOPT_MINPSZ) 1053 so->so_proto_props.sopp_minpsz = soppp->sopp_minpsz; 1054 if (soppp->sopp_flags & SOCKOPT_ZCOPY) { 1055 if (soppp->sopp_zcopyflag & ZCVMSAFE) { 1056 so->so_proto_props.sopp_zcopyflag |= STZCVMSAFE; 1057 so->so_proto_props.sopp_zcopyflag &= ~STZCVMUNSAFE; 1058 } else if (soppp->sopp_zcopyflag & ZCVMUNSAFE) { 1059 so->so_proto_props.sopp_zcopyflag |= STZCVMUNSAFE; 1060 so->so_proto_props.sopp_zcopyflag &= ~STZCVMSAFE; 1061 } 1062 1063 if (soppp->sopp_zcopyflag & COPYCACHED) { 1064 so->so_proto_props.sopp_zcopyflag |= STRCOPYCACHED; 1065 } 1066 } 1067 if (soppp->sopp_flags & SOCKOPT_OOBINLINE) 1068 so->so_proto_props.sopp_oobinline = soppp->sopp_oobinline; 1069 if (soppp->sopp_flags & SOCKOPT_RCVTIMER) 1070 so->so_proto_props.sopp_rcvtimer = soppp->sopp_rcvtimer; 1071 if (soppp->sopp_flags & SOCKOPT_RCVTHRESH) 1072 so->so_proto_props.sopp_rcvthresh = soppp->sopp_rcvthresh; 1073 if (soppp->sopp_flags & SOCKOPT_MAXADDRLEN) 1074 so->so_proto_props.sopp_maxaddrlen = soppp->sopp_maxaddrlen; 1075 1076 mutex_exit(&so->so_lock); 1077 1078 #ifdef DEBUG 1079 soppp->sopp_flags &= ~(SOCKOPT_MAXBLK | SOCKOPT_WROFF | SOCKOPT_TAIL | 1080 SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | SOCKOPT_MAXPSZ | 1081 SOCKOPT_ZCOPY | SOCKOPT_OOBINLINE | SOCKOPT_RCVTIMER | 1082 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ); 1083 ASSERT(soppp->sopp_flags == 0); 1084 #endif 1085 } 1086 1087 /* ARGSUSED */ 1088 ssize_t 1089 so_queue_msg(sock_upper_handle_t sock_handle, mblk_t *mp, 1090 size_t msg_size, int flags, int *errorp, boolean_t *force_pushp) 1091 { 1092 struct sonode *so = (struct sonode *)sock_handle; 1093 boolean_t force_push = B_TRUE; 1094 int space_left; 1095 sodirect_t *sodp = so->so_direct; 1096 1097 ASSERT(errorp != NULL); 1098 *errorp = 0; 1099 if (mp == NULL) { 1100 if (msg_size > 0) { 1101 ASSERT(so->so_downcalls->sd_recv_uio != NULL); 1102 mutex_enter(&so->so_lock); 1103 /* the notify functions will drop the lock */ 1104 if (flags & MSG_OOB) 1105 so_notify_oobdata(so, IS_SO_OOB_INLINE(so)); 1106 else 1107 so_notify_data(so, msg_size); 1108 return (0); 1109 } 1110 /* 1111 * recv space check 1112 */ 1113 mutex_enter(&so->so_lock); 1114 space_left = so->so_rcvbuf - so->so_rcv_queued; 1115 if (space_left <= 0) { 1116 so->so_flowctrld = B_TRUE; 1117 *errorp = ENOSPC; 1118 space_left = -1; 1119 } 1120 goto done_unlock; 1121 } 1122 1123 ASSERT(mp->b_next == NULL); 1124 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_PROTO); 1125 ASSERT(msg_size == msgdsize(mp)); 1126 1127 if (flags & MSG_OOB) { 1128 so_queue_oob(sock_handle, mp, msg_size); 1129 return (0); 1130 } 1131 1132 if (force_pushp != NULL) 1133 force_push = *force_pushp; 1134 1135 if (DB_TYPE(mp) == M_PROTO && !__TPI_PRIM_ISALIGNED(mp->b_rptr)) { 1136 /* The read pointer is not aligned correctly for TPI */ 1137 zcmn_err(getzoneid(), CE_WARN, 1138 "sockfs: Unaligned TPI message received. rptr = %p\n", 1139 (void *)mp->b_rptr); 1140 freemsg(mp); 1141 mutex_enter(sodp->sod_lockp); 1142 SOD_UIOAFINI(sodp); 1143 mutex_exit(sodp->sod_lockp); 1144 1145 return (so->so_rcvbuf - so->so_rcv_queued); 1146 } 1147 1148 mutex_enter(&so->so_lock); 1149 if (so->so_state & (SS_FALLBACK_PENDING | SS_FALLBACK_COMP)) { 1150 SOD_DISABLE(sodp); 1151 mutex_exit(&so->so_lock); 1152 *errorp = EOPNOTSUPP; 1153 return (-1); 1154 } 1155 if (so->so_state & SS_CANTRCVMORE) { 1156 freemsg(mp); 1157 SOD_DISABLE(sodp); 1158 mutex_exit(&so->so_lock); 1159 return (0); 1160 } 1161 1162 /* process the mblk via I/OAT if capable */ 1163 if (sodp != NULL && (sodp->sod_state & SOD_ENABLED)) { 1164 if (DB_TYPE(mp) == M_DATA) { 1165 (void) sod_uioa_mblk_init(sodp, mp, msg_size); 1166 } else { 1167 SOD_UIOAFINI(sodp); 1168 } 1169 } 1170 1171 if (mp->b_next == NULL) { 1172 so_enqueue_msg(so, mp, msg_size); 1173 } else { 1174 do { 1175 mblk_t *nmp; 1176 1177 if ((nmp = mp->b_next) != NULL) { 1178 mp->b_next = NULL; 1179 } 1180 so_enqueue_msg(so, mp, msgdsize(mp)); 1181 mp = nmp; 1182 } while (mp != NULL); 1183 } 1184 1185 space_left = so->so_rcvbuf - so->so_rcv_queued; 1186 if (space_left <= 0) { 1187 so->so_flowctrld = B_TRUE; 1188 *errorp = ENOSPC; 1189 space_left = -1; 1190 } 1191 1192 if (force_push || so->so_rcv_queued >= so->so_rcv_thresh || 1193 so->so_rcv_queued >= so->so_rcv_wanted || 1194 (sodp != NULL && so->so_rcv_queued >= sodp->sod_want)) { 1195 SOCKET_TIMER_CANCEL(so); 1196 /* 1197 * so_notify_data will release the lock 1198 */ 1199 so_notify_data(so, so->so_rcv_queued); 1200 1201 if (force_pushp != NULL) 1202 *force_pushp = B_TRUE; 1203 goto done; 1204 } else if (so->so_rcv_timer_tid == 0) { 1205 /* Make sure the recv push timer is running */ 1206 SOCKET_TIMER_START(so); 1207 } 1208 1209 done_unlock: 1210 mutex_exit(&so->so_lock); 1211 done: 1212 return (space_left); 1213 } 1214 1215 /* 1216 * Set the offset of where the oob data is relative to the bytes in 1217 * queued. Also generate SIGURG 1218 */ 1219 void 1220 so_signal_oob(sock_upper_handle_t sock_handle, ssize_t offset) 1221 { 1222 struct sonode *so; 1223 1224 ASSERT(offset >= 0); 1225 so = (struct sonode *)sock_handle; 1226 mutex_enter(&so->so_lock); 1227 SOD_UIOAFINI(so->so_direct); 1228 1229 /* 1230 * New urgent data on the way so forget about any old 1231 * urgent data. 1232 */ 1233 so->so_state &= ~(SS_HAVEOOBDATA|SS_HADOOBDATA); 1234 1235 /* 1236 * Record that urgent data is pending. 1237 */ 1238 so->so_state |= SS_OOBPEND; 1239 1240 if (so->so_oobmsg != NULL) { 1241 dprintso(so, 1, ("sock: discarding old oob\n")); 1242 freemsg(so->so_oobmsg); 1243 so->so_oobmsg = NULL; 1244 } 1245 1246 /* 1247 * set the offset where the urgent byte is 1248 */ 1249 so->so_oobmark = so->so_rcv_queued + offset; 1250 if (so->so_oobmark == 0) 1251 so->so_state |= SS_RCVATMARK; 1252 else 1253 so->so_state &= ~SS_RCVATMARK; 1254 1255 so_notify_oobsig(so); 1256 } 1257 1258 /* 1259 * Queue the OOB byte 1260 */ 1261 static void 1262 so_queue_oob(sock_upper_handle_t sock_handle, mblk_t *mp, size_t len) 1263 { 1264 struct sonode *so; 1265 1266 so = (struct sonode *)sock_handle; 1267 mutex_enter(&so->so_lock); 1268 SOD_UIOAFINI(so->so_direct); 1269 1270 ASSERT(mp != NULL); 1271 if (!IS_SO_OOB_INLINE(so)) { 1272 so->so_oobmsg = mp; 1273 so->so_state |= SS_HAVEOOBDATA; 1274 } else { 1275 so_enqueue_msg(so, mp, len); 1276 } 1277 1278 so_notify_oobdata(so, IS_SO_OOB_INLINE(so)); 1279 } 1280 1281 int 1282 so_close(struct sonode *so, int flag, struct cred *cr) 1283 { 1284 int error; 1285 1286 error = (*so->so_downcalls->sd_close)(so->so_proto_handle, flag, cr); 1287 1288 /* 1289 * At this point there will be no more upcalls from the protocol 1290 */ 1291 mutex_enter(&so->so_lock); 1292 so_rcv_flush(so); 1293 mutex_exit(&so->so_lock); 1294 1295 return (error); 1296 } 1297 1298 void 1299 so_zcopy_notify(sock_upper_handle_t sock_handle) 1300 { 1301 struct sonode *so = (struct sonode *)sock_handle; 1302 1303 mutex_enter(&so->so_lock); 1304 so->so_copyflag |= STZCNOTIFY; 1305 cv_broadcast(&so->so_copy_cv); 1306 mutex_exit(&so->so_lock); 1307 } 1308 1309 void 1310 so_set_error(sock_upper_handle_t sock_handle, int error) 1311 { 1312 struct sonode *so = (struct sonode *)sock_handle; 1313 1314 mutex_enter(&so->so_lock); 1315 1316 soseterror(so, error); 1317 1318 so_notify_error(so); 1319 } 1320 1321 /* 1322 * so_recvmsg - read data from the socket 1323 * 1324 * There are two ways of obtaining data; either we ask the protocol to 1325 * copy directly into the supplied buffer, or we copy data from the 1326 * sonode's receive queue. The decision which one to use depends on 1327 * whether the protocol has a sd_recv_uio down call. 1328 */ 1329 int 1330 so_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, 1331 struct cred *cr) 1332 { 1333 rval_t rval; 1334 int flags = 0; 1335 t_uscalar_t controllen, namelen; 1336 int error = 0; 1337 int ret; 1338 mblk_t *mctlp = NULL; 1339 union T_primitives *tpr; 1340 void *control; 1341 ssize_t saved_resid; 1342 struct uio *suiop; 1343 1344 SO_BLOCK_FALLBACK(so, SOP_RECVMSG(so, msg, uiop, cr)); 1345 1346 if ((so->so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 && 1347 (so->so_mode & SM_CONNREQUIRED)) { 1348 SO_UNBLOCK_FALLBACK(so); 1349 return (ENOTCONN); 1350 } 1351 1352 if (msg->msg_flags & MSG_PEEK) 1353 msg->msg_flags &= ~MSG_WAITALL; 1354 1355 if (so->so_mode & SM_ATOMIC) 1356 msg->msg_flags |= MSG_TRUNC; 1357 1358 if (msg->msg_flags & MSG_OOB) { 1359 if ((so->so_mode & SM_EXDATA) == 0) { 1360 error = EOPNOTSUPP; 1361 } else if (so->so_downcalls->sd_recv_uio != NULL) { 1362 error = (*so->so_downcalls->sd_recv_uio) 1363 (so->so_proto_handle, uiop, msg, cr); 1364 } else { 1365 error = sorecvoob(so, msg, uiop, msg->msg_flags, 1366 IS_SO_OOB_INLINE(so)); 1367 } 1368 SO_UNBLOCK_FALLBACK(so); 1369 return (error); 1370 } 1371 1372 /* 1373 * If the protocol has the recv down call, then pass the request 1374 * down. 1375 */ 1376 if (so->so_downcalls->sd_recv_uio != NULL) { 1377 error = (*so->so_downcalls->sd_recv_uio) 1378 (so->so_proto_handle, uiop, msg, cr); 1379 SO_UNBLOCK_FALLBACK(so); 1380 return (error); 1381 } 1382 1383 /* 1384 * Reading data from the socket buffer 1385 */ 1386 flags = msg->msg_flags; 1387 msg->msg_flags = 0; 1388 1389 /* 1390 * Set msg_controllen and msg_namelen to zero here to make it 1391 * simpler in the cases that no control or name is returned. 1392 */ 1393 controllen = msg->msg_controllen; 1394 namelen = msg->msg_namelen; 1395 msg->msg_controllen = 0; 1396 msg->msg_namelen = 0; 1397 1398 mutex_enter(&so->so_lock); 1399 /* Set SOREADLOCKED */ 1400 error = so_lock_read_intr(so, 1401 uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0)); 1402 mutex_exit(&so->so_lock); 1403 if (error) { 1404 SO_UNBLOCK_FALLBACK(so); 1405 return (error); 1406 } 1407 1408 suiop = sod_rcv_init(so, flags, &uiop); 1409 retry: 1410 saved_resid = uiop->uio_resid; 1411 error = so_dequeue_msg(so, &mctlp, uiop, &rval, flags); 1412 if (error != 0) { 1413 goto out; 1414 } 1415 /* 1416 * For datagrams the MOREDATA flag is used to set MSG_TRUNC. 1417 * For non-datagrams MOREDATA is used to set MSG_EOR. 1418 */ 1419 ASSERT(!(rval.r_val1 & MORECTL)); 1420 if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC)) 1421 msg->msg_flags |= MSG_TRUNC; 1422 if (mctlp == NULL) { 1423 dprintso(so, 1, ("so_recvmsg: got M_DATA\n")); 1424 1425 mutex_enter(&so->so_lock); 1426 /* Set MSG_EOR based on MOREDATA */ 1427 if (!(rval.r_val1 & MOREDATA)) { 1428 if (so->so_state & SS_SAVEDEOR) { 1429 msg->msg_flags |= MSG_EOR; 1430 so->so_state &= ~SS_SAVEDEOR; 1431 } 1432 } 1433 /* 1434 * If some data was received (i.e. not EOF) and the 1435 * read/recv* has not been satisfied wait for some more. 1436 */ 1437 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 1438 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 1439 mutex_exit(&so->so_lock); 1440 goto retry; 1441 } 1442 1443 goto out_locked; 1444 } 1445 /* strsock_proto has already verified length and alignment */ 1446 tpr = (union T_primitives *)mctlp->b_rptr; 1447 dprintso(so, 1, ("so_recvmsg: type %d\n", tpr->type)); 1448 switch (tpr->type) { 1449 case T_DATA_IND: { 1450 /* 1451 * Set msg_flags to MSG_EOR based on 1452 * MORE_flag and MOREDATA. 1453 */ 1454 mutex_enter(&so->so_lock); 1455 so->so_state &= ~SS_SAVEDEOR; 1456 if (!(tpr->data_ind.MORE_flag & 1)) { 1457 if (!(rval.r_val1 & MOREDATA)) 1458 msg->msg_flags |= MSG_EOR; 1459 else 1460 so->so_state |= SS_SAVEDEOR; 1461 } 1462 freemsg(mctlp); 1463 /* 1464 * If some data was received (i.e. not EOF) and the 1465 * read/recv* has not been satisfied wait for some more. 1466 */ 1467 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 1468 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 1469 mutex_exit(&so->so_lock); 1470 goto retry; 1471 } 1472 goto out_locked; 1473 } 1474 case T_UNITDATA_IND: { 1475 void *addr; 1476 t_uscalar_t addrlen; 1477 void *abuf; 1478 t_uscalar_t optlen; 1479 void *opt; 1480 1481 if (namelen != 0) { 1482 /* Caller wants source address */ 1483 addrlen = tpr->unitdata_ind.SRC_length; 1484 addr = sogetoff(mctlp, tpr->unitdata_ind.SRC_offset, 1485 addrlen, 1); 1486 if (addr == NULL) { 1487 freemsg(mctlp); 1488 error = EPROTO; 1489 eprintsoline(so, error); 1490 goto out; 1491 } 1492 ASSERT(so->so_family != AF_UNIX); 1493 } 1494 optlen = tpr->unitdata_ind.OPT_length; 1495 if (optlen != 0) { 1496 t_uscalar_t ncontrollen; 1497 1498 /* 1499 * Extract any source address option. 1500 * Determine how large cmsg buffer is needed. 1501 */ 1502 opt = sogetoff(mctlp, tpr->unitdata_ind.OPT_offset, 1503 optlen, __TPI_ALIGN_SIZE); 1504 1505 if (opt == NULL) { 1506 freemsg(mctlp); 1507 error = EPROTO; 1508 eprintsoline(so, error); 1509 goto out; 1510 } 1511 if (so->so_family == AF_UNIX) 1512 so_getopt_srcaddr(opt, optlen, &addr, &addrlen); 1513 ncontrollen = so_cmsglen(mctlp, opt, optlen, 1514 !(flags & MSG_XPG4_2)); 1515 if (controllen != 0) 1516 controllen = ncontrollen; 1517 else if (ncontrollen != 0) 1518 msg->msg_flags |= MSG_CTRUNC; 1519 } else { 1520 controllen = 0; 1521 } 1522 1523 if (namelen != 0) { 1524 /* 1525 * Return address to caller. 1526 * Caller handles truncation if length 1527 * exceeds msg_namelen. 1528 * NOTE: AF_UNIX NUL termination is ensured by 1529 * the sender's copyin_name(). 1530 */ 1531 abuf = kmem_alloc(addrlen, KM_SLEEP); 1532 1533 bcopy(addr, abuf, addrlen); 1534 msg->msg_name = abuf; 1535 msg->msg_namelen = addrlen; 1536 } 1537 1538 if (controllen != 0) { 1539 /* 1540 * Return control msg to caller. 1541 * Caller handles truncation if length 1542 * exceeds msg_controllen. 1543 */ 1544 control = kmem_zalloc(controllen, KM_SLEEP); 1545 1546 error = so_opt2cmsg(mctlp, opt, optlen, 1547 !(flags & MSG_XPG4_2), control, controllen); 1548 if (error) { 1549 freemsg(mctlp); 1550 if (msg->msg_namelen != 0) 1551 kmem_free(msg->msg_name, 1552 msg->msg_namelen); 1553 kmem_free(control, controllen); 1554 eprintsoline(so, error); 1555 goto out; 1556 } 1557 msg->msg_control = control; 1558 msg->msg_controllen = controllen; 1559 } 1560 1561 freemsg(mctlp); 1562 goto out; 1563 } 1564 case T_OPTDATA_IND: { 1565 struct T_optdata_req *tdr; 1566 void *opt; 1567 t_uscalar_t optlen; 1568 1569 tdr = (struct T_optdata_req *)mctlp->b_rptr; 1570 optlen = tdr->OPT_length; 1571 if (optlen != 0) { 1572 t_uscalar_t ncontrollen; 1573 /* 1574 * Determine how large cmsg buffer is needed. 1575 */ 1576 opt = sogetoff(mctlp, 1577 tpr->optdata_ind.OPT_offset, optlen, 1578 __TPI_ALIGN_SIZE); 1579 1580 if (opt == NULL) { 1581 freemsg(mctlp); 1582 error = EPROTO; 1583 eprintsoline(so, error); 1584 goto out; 1585 } 1586 1587 ncontrollen = so_cmsglen(mctlp, opt, optlen, 1588 !(flags & MSG_XPG4_2)); 1589 if (controllen != 0) 1590 controllen = ncontrollen; 1591 else if (ncontrollen != 0) 1592 msg->msg_flags |= MSG_CTRUNC; 1593 } else { 1594 controllen = 0; 1595 } 1596 1597 if (controllen != 0) { 1598 /* 1599 * Return control msg to caller. 1600 * Caller handles truncation if length 1601 * exceeds msg_controllen. 1602 */ 1603 control = kmem_zalloc(controllen, KM_SLEEP); 1604 1605 error = so_opt2cmsg(mctlp, opt, optlen, 1606 !(flags & MSG_XPG4_2), control, controllen); 1607 if (error) { 1608 freemsg(mctlp); 1609 kmem_free(control, controllen); 1610 eprintsoline(so, error); 1611 goto out; 1612 } 1613 msg->msg_control = control; 1614 msg->msg_controllen = controllen; 1615 } 1616 1617 /* 1618 * Set msg_flags to MSG_EOR based on 1619 * DATA_flag and MOREDATA. 1620 */ 1621 mutex_enter(&so->so_lock); 1622 so->so_state &= ~SS_SAVEDEOR; 1623 if (!(tpr->data_ind.MORE_flag & 1)) { 1624 if (!(rval.r_val1 & MOREDATA)) 1625 msg->msg_flags |= MSG_EOR; 1626 else 1627 so->so_state |= SS_SAVEDEOR; 1628 } 1629 freemsg(mctlp); 1630 /* 1631 * If some data was received (i.e. not EOF) and the 1632 * read/recv* has not been satisfied wait for some more. 1633 * Not possible to wait if control info was received. 1634 */ 1635 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 1636 controllen == 0 && 1637 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 1638 mutex_exit(&so->so_lock); 1639 goto retry; 1640 } 1641 goto out_locked; 1642 } 1643 default: 1644 cmn_err(CE_CONT, "so_recvmsg bad type %x \n", 1645 tpr->type); 1646 freemsg(mctlp); 1647 error = EPROTO; 1648 ASSERT(0); 1649 } 1650 out: 1651 mutex_enter(&so->so_lock); 1652 out_locked: 1653 /* The sod_lockp pointers to the sonode so_lock */ 1654 ret = sod_rcv_done(so, suiop, uiop); 1655 if (ret != 0 && error == 0) 1656 error = ret; 1657 1658 so_unlock_read(so); /* Clear SOREADLOCKED */ 1659 mutex_exit(&so->so_lock); 1660 1661 SO_UNBLOCK_FALLBACK(so); 1662 1663 return (error); 1664 } 1665 1666 sonodeops_t so_sonodeops = { 1667 so_init, /* sop_init */ 1668 so_accept, /* sop_accept */ 1669 so_bind, /* sop_bind */ 1670 so_listen, /* sop_listen */ 1671 so_connect, /* sop_connect */ 1672 so_recvmsg, /* sop_recvmsg */ 1673 so_sendmsg, /* sop_sendmsg */ 1674 so_sendmblk, /* sop_sendmblk */ 1675 so_getpeername, /* sop_getpeername */ 1676 so_getsockname, /* sop_getsockname */ 1677 so_shutdown, /* sop_shutdown */ 1678 so_getsockopt, /* sop_getsockopt */ 1679 so_setsockopt, /* sop_setsockopt */ 1680 so_ioctl, /* sop_ioctl */ 1681 so_poll, /* sop_poll */ 1682 so_close, /* sop_close */ 1683 }; 1684 1685 sock_upcalls_t so_upcalls = { 1686 so_newconn, 1687 so_connected, 1688 so_disconnected, 1689 so_opctl, 1690 so_queue_msg, 1691 so_set_prop, 1692 so_txq_full, 1693 so_signal_oob, 1694 so_zcopy_notify, 1695 so_set_error 1696 }; 1697