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