Print this page
    
OS-4517 lxbrand convert bind to IKE (remove unused function)
OS-4517 lxbrand convert bind to IKE
OS-4505 lxbrand java fails to bind on ipv6 address
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
OS-3752 Increase IOV_MAX to at least 1024
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/sockfs/socksubr.c
          +++ new/usr/src/uts/common/fs/sockfs/socksubr.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  
    | 
      ↓ open down ↓ | 
    13 lines elided | 
    
      ↑ open up ↑ | 
  
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
       24 + * Copyright 2015, Joyent, Inc. All rights reserved.
  24   25   */
  25   26  
  26   27  #include <sys/types.h>
  27   28  #include <sys/t_lock.h>
  28   29  #include <sys/param.h>
  29   30  #include <sys/systm.h>
  30   31  #include <sys/buf.h>
  31   32  #include <sys/conf.h>
  32   33  #include <sys/cred.h>
  33   34  #include <sys/kmem.h>
  34   35  #include <sys/sysmacros.h>
  35   36  #include <sys/vfs.h>
  36   37  #include <sys/vfs_opreg.h>
  37   38  #include <sys/vnode.h>
  38   39  #include <sys/debug.h>
  39   40  #include <sys/errno.h>
  40   41  #include <sys/time.h>
  41   42  #include <sys/file.h>
  42   43  #include <sys/open.h>
  43   44  #include <sys/user.h>
  44   45  #include <sys/termios.h>
  45   46  #include <sys/stream.h>
  46   47  #include <sys/strsubr.h>
  47   48  #include <sys/strsun.h>
  48   49  #include <sys/esunddi.h>
  49   50  #include <sys/flock.h>
  50   51  #include <sys/modctl.h>
  51   52  #include <sys/cmn_err.h>
  52   53  #include <sys/mkdev.h>
  53   54  #include <sys/pathname.h>
  54   55  #include <sys/ddi.h>
  55   56  #include <sys/stat.h>
  56   57  #include <sys/fs/snode.h>
  57   58  #include <sys/fs/dv_node.h>
  58   59  #include <sys/zone.h>
  59   60  
  60   61  #include <sys/socket.h>
  61   62  #include <sys/socketvar.h>
  62   63  #include <netinet/in.h>
  63   64  #include <sys/un.h>
  64   65  #include <sys/ucred.h>
  65   66  
  66   67  #include <sys/tiuser.h>
  67   68  #define _SUN_TPI_VERSION        2
  68   69  #include <sys/tihdr.h>
  69   70  
  70   71  #include <c2/audit.h>
  71   72  
  72   73  #include <fs/sockfs/nl7c.h>
  73   74  #include <fs/sockfs/sockcommon.h>
  74   75  #include <fs/sockfs/sockfilter_impl.h>
  75   76  #include <fs/sockfs/socktpi.h>
  76   77  #include <fs/sockfs/socktpi_impl.h>
  77   78  #include <fs/sockfs/sodirect.h>
  78   79  
  79   80  /*
  80   81   * Macros that operate on struct cmsghdr.
  81   82   * The CMSG_VALID macro does not assume that the last option buffer is padded.
  82   83   */
  83   84  #define CMSG_CONTENT(cmsg)      (&((cmsg)[1]))
  84   85  #define CMSG_CONTENTLEN(cmsg)   ((cmsg)->cmsg_len - sizeof (struct cmsghdr))
  85   86  #define CMSG_VALID(cmsg, start, end)                                    \
  86   87          (ISALIGNED_cmsghdr(cmsg) &&                                     \
  87   88          ((uintptr_t)(cmsg) >= (uintptr_t)(start)) &&                    \
  88   89          ((uintptr_t)(cmsg) < (uintptr_t)(end)) &&                       \
  89   90          ((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) &&       \
  90   91          ((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)))
  91   92  #define SO_LOCK_WAKEUP_TIME     3000    /* Wakeup time in milliseconds */
  92   93  
  93   94  dev_t sockdev;  /* For fsid in getattr */
  94   95  int sockfs_defer_nl7c_init = 0;
  95   96  
  96   97  struct socklist socklist;
  97   98  
  98   99  struct kmem_cache *socket_cache;
  99  100  
 100  101  /*
 101  102   * sockconf_lock protects the socket configuration (socket types and
 102  103   * socket filters) which is changed via the sockconfig system call.
 103  104   */
 104  105  krwlock_t sockconf_lock;
 105  106  
 106  107  static int sockfs_update(kstat_t *, int);
 107  108  static int sockfs_snapshot(kstat_t *, void *, int);
 108  109  extern smod_info_t *sotpi_smod_create(void);
 109  110  
 110  111  extern void sendfile_init();
 111  112  
 112  113  extern void nl7c_init(void);
 113  114  
 114  115  extern int modrootloaded;
 115  116  
 116  117  #define ADRSTRLEN (2 * sizeof (void *) + 1)
 117  118  /*
 118  119   * kernel structure for passing the sockinfo data back up to the user.
 119  120   * the strings array allows us to convert AF_UNIX addresses into strings
 120  121   * with a common method regardless of which n-bit kernel we're running.
 121  122   */
 122  123  struct k_sockinfo {
 123  124          struct sockinfo ks_si;
 124  125          char            ks_straddr[3][ADRSTRLEN];
 125  126  };
 126  127  
 127  128  /*
 128  129   * Translate from a device pathname (e.g. "/dev/tcp") to a vnode.
 129  130   * Returns with the vnode held.
 130  131   */
 131  132  int
 132  133  sogetvp(char *devpath, vnode_t **vpp, int uioflag)
 133  134  {
 134  135          struct snode *csp;
 135  136          vnode_t *vp, *dvp;
 136  137          major_t maj;
 137  138          int error;
 138  139  
 139  140          ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE);
 140  141  
 141  142          /*
 142  143           * Lookup the underlying filesystem vnode.
 143  144           */
 144  145          error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp);
 145  146          if (error)
 146  147                  return (error);
 147  148  
 148  149          /* Check that it is the correct vnode */
 149  150          if (vp->v_type != VCHR) {
 150  151                  VN_RELE(vp);
 151  152                  return (ENOTSOCK);
 152  153          }
 153  154  
 154  155          /*
 155  156           * If devpath went through devfs, the device should already
 156  157           * be configured. If devpath is a mknod file, however, we
 157  158           * need to make sure the device is properly configured.
 158  159           * To do this, we do something similar to spec_open()
 159  160           * except that we resolve to the minor/leaf level since
 160  161           * we need to return a vnode.
 161  162           */
 162  163          csp = VTOS(VTOS(vp)->s_commonvp);
 163  164          if (!(csp->s_flag & SDIPSET)) {
 164  165                  char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 165  166                  error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname);
 166  167                  if (error == 0)
 167  168                          error = devfs_lookupname(pathname, NULLVPP, &dvp);
 168  169                  VN_RELE(vp);
 169  170                  kmem_free(pathname, MAXPATHLEN);
 170  171                  if (error != 0)
 171  172                          return (ENXIO);
 172  173                  vp = dvp;       /* use the devfs vp */
 173  174          }
 174  175  
 175  176          /* device is configured at this point */
 176  177          maj = getmajor(vp->v_rdev);
 177  178          if (!STREAMSTAB(maj)) {
 178  179                  VN_RELE(vp);
 179  180                  return (ENOSTR);
 180  181          }
 181  182  
 182  183          *vpp = vp;
 183  184          return (0);
 184  185  }
 185  186  
 186  187  /*
 187  188   * Update the accessed, updated, or changed times in an sonode
 188  189   * with the current time.
 189  190   *
 190  191   * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable
 191  192   * attributes in a fstat call. (They return the current time and 0 for
 192  193   * all timestamps, respectively.) We maintain the current timestamps
 193  194   * here primarily so that should sockmod be popped the resulting
 194  195   * file descriptor will behave like a stream w.r.t. the timestamps.
 195  196   */
 196  197  void
 197  198  so_update_attrs(struct sonode *so, int flag)
 198  199  {
 199  200          time_t now = gethrestime_sec();
 200  201  
 201  202          if (SOCK_IS_NONSTR(so))
 202  203                  return;
 203  204  
 204  205          mutex_enter(&so->so_lock);
 205  206          so->so_flag |= flag;
 206  207          if (flag & SOACC)
 207  208                  SOTOTPI(so)->sti_atime = now;
 208  209          if (flag & SOMOD)
 209  210                  SOTOTPI(so)->sti_mtime = now;
 210  211          mutex_exit(&so->so_lock);
 211  212  }
 212  213  
 213  214  extern so_create_func_t sock_comm_create_function;
 214  215  extern so_destroy_func_t sock_comm_destroy_function;
 215  216  /*
 216  217   * Init function called when sockfs is loaded.
 217  218   */
 218  219  int
 219  220  sockinit(int fstype, char *name)
 220  221  {
 221  222          static const fs_operation_def_t sock_vfsops_template[] = {
 222  223                  NULL, NULL
 223  224          };
 224  225          int error;
 225  226          major_t dev;
 226  227          char *err_str;
 227  228  
 228  229          error = vfs_setfsops(fstype, sock_vfsops_template, NULL);
 229  230          if (error != 0) {
 230  231                  zcmn_err(GLOBAL_ZONEID, CE_WARN,
 231  232                      "sockinit: bad vfs ops template");
 232  233                  return (error);
 233  234          }
 234  235  
 235  236          error = vn_make_ops(name, socket_vnodeops_template,
 236  237              &socket_vnodeops);
 237  238          if (error != 0) {
 238  239                  err_str = "sockinit: bad socket vnode ops template";
 239  240                  /* vn_make_ops() does not reset socktpi_vnodeops on failure. */
 240  241                  socket_vnodeops = NULL;
 241  242                  goto failure;
 242  243          }
 243  244  
 244  245          socket_cache = kmem_cache_create("socket_cache",
 245  246              sizeof (struct sonode), 0, sonode_constructor,
 246  247              sonode_destructor, NULL, NULL, NULL, 0);
 247  248  
 248  249          rw_init(&sockconf_lock, NULL, RW_DEFAULT, NULL);
 249  250  
 250  251          error = socktpi_init();
 251  252          if (error != 0) {
 252  253                  err_str = NULL;
 253  254                  goto failure;
 254  255          }
 255  256  
 256  257          error = sod_init();
 257  258          if (error != 0) {
 258  259                  err_str = NULL;
 259  260                  goto failure;
 260  261          }
 261  262  
 262  263          /*
 263  264           * Set up the default create and destroy functions
 264  265           */
 265  266          sock_comm_create_function = socket_sonode_create;
 266  267          sock_comm_destroy_function = socket_sonode_destroy;
 267  268  
 268  269          /*
 269  270           * Build initial list mapping socket parameters to vnode.
 270  271           */
 271  272          smod_init();
 272  273          smod_add(sotpi_smod_create());
 273  274  
 274  275          sockparams_init();
 275  276  
 276  277          /*
 277  278           * If sockets are needed before init runs /sbin/soconfig
 278  279           * it is possible to preload the sockparams list here using
 279  280           * calls like:
 280  281           *      sockconfig(1,2,3, "/dev/tcp", 0);
 281  282           */
 282  283  
 283  284          /*
 284  285           * Create a unique dev_t for use in so_fsid.
 285  286           */
 286  287  
 287  288          if ((dev = getudev()) == (major_t)-1)
 288  289                  dev = 0;
 289  290          sockdev = makedevice(dev, 0);
 290  291  
 291  292          mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL);
 292  293          sendfile_init();
 293  294          if (!modrootloaded) {
 294  295                  sockfs_defer_nl7c_init = 1;
 295  296          } else {
 296  297                  nl7c_init();
 297  298          }
 298  299  
 299  300          /* Initialize socket filters */
 300  301          sof_init();
 301  302  
 302  303          return (0);
 303  304  
 304  305  failure:
 305  306          (void) vfs_freevfsops_by_type(fstype);
 306  307          if (socket_vnodeops != NULL)
 307  308                  vn_freevnodeops(socket_vnodeops);
 308  309          if (err_str != NULL)
 309  310                  zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str);
 310  311          return (error);
 311  312  }
 312  313  
 313  314  /*
 314  315   * Caller must hold the mutex. Used to set SOLOCKED.
 315  316   */
 316  317  void
 317  318  so_lock_single(struct sonode *so)
 318  319  {
 319  320          ASSERT(MUTEX_HELD(&so->so_lock));
 320  321  
 321  322          while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) {
 322  323                  cv_wait_stop(&so->so_single_cv, &so->so_lock,
 323  324                      SO_LOCK_WAKEUP_TIME);
 324  325          }
 325  326          so->so_flag |= SOLOCKED;
 326  327  }
 327  328  
 328  329  /*
 329  330   * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND.
 330  331   * Used to clear SOLOCKED or SOASYNC_UNBIND.
 331  332   */
 332  333  void
 333  334  so_unlock_single(struct sonode *so, int flag)
 334  335  {
 335  336          ASSERT(MUTEX_HELD(&so->so_lock));
 336  337          ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND));
 337  338          ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0);
 338  339          ASSERT(so->so_flag & flag);
 339  340          /*
 340  341           * Process the T_DISCON_IND on sti_discon_ind_mp.
 341  342           *
 342  343           * Call to so_drain_discon_ind will result in so_lock
 343  344           * being dropped and re-acquired later.
 344  345           */
 345  346          if (!SOCK_IS_NONSTR(so)) {
 346  347                  sotpi_info_t *sti = SOTOTPI(so);
 347  348  
 348  349                  if (sti->sti_discon_ind_mp != NULL)
 349  350                          so_drain_discon_ind(so);
 350  351          }
 351  352  
 352  353          cv_signal(&so->so_single_cv);
 353  354          so->so_flag &= ~flag;
 354  355  }
 355  356  
 356  357  /*
 357  358   * Caller must hold the mutex. Used to set SOREADLOCKED.
 358  359   * If the caller wants nonblocking behavior it should set fmode.
 359  360   */
 360  361  int
 361  362  so_lock_read(struct sonode *so, int fmode)
 362  363  {
 363  364          ASSERT(MUTEX_HELD(&so->so_lock));
 364  365  
 365  366          while (so->so_flag & SOREADLOCKED) {
 366  367                  if (fmode & (FNDELAY|FNONBLOCK))
 367  368                          return (EWOULDBLOCK);
 368  369                  cv_wait_stop(&so->so_read_cv, &so->so_lock,
 369  370                      SO_LOCK_WAKEUP_TIME);
 370  371          }
 371  372          so->so_flag |= SOREADLOCKED;
 372  373          return (0);
 373  374  }
 374  375  
 375  376  /*
 376  377   * Like so_lock_read above but allows signals.
 377  378   */
 378  379  int
 379  380  so_lock_read_intr(struct sonode *so, int fmode)
 380  381  {
 381  382          ASSERT(MUTEX_HELD(&so->so_lock));
 382  383  
 383  384          while (so->so_flag & SOREADLOCKED) {
 384  385                  if (fmode & (FNDELAY|FNONBLOCK))
 385  386                          return (EWOULDBLOCK);
 386  387                  if (!cv_wait_sig(&so->so_read_cv, &so->so_lock))
 387  388                          return (EINTR);
 388  389          }
 389  390          so->so_flag |= SOREADLOCKED;
 390  391          return (0);
 391  392  }
 392  393  
 393  394  /*
 394  395   * Caller must hold the mutex. Used to clear SOREADLOCKED,
 395  396   * set in so_lock_read() or so_lock_read_intr().
 396  397   */
 397  398  void
 398  399  so_unlock_read(struct sonode *so)
 399  400  {
 400  401          ASSERT(MUTEX_HELD(&so->so_lock));
 401  402          ASSERT(so->so_flag & SOREADLOCKED);
 402  403  
 403  404          cv_signal(&so->so_read_cv);
 404  405          so->so_flag &= ~SOREADLOCKED;
 405  406  }
 406  407  
 407  408  /*
 408  409   * Verify that the specified offset falls within the mblk and
 409  410   * that the resulting pointer is aligned.
 410  411   * Returns NULL if not.
 411  412   */
 412  413  void *
 413  414  sogetoff(mblk_t *mp, t_uscalar_t offset,
 414  415      t_uscalar_t length, uint_t align_size)
 415  416  {
 416  417          uintptr_t ptr1, ptr2;
 417  418  
 418  419          ASSERT(mp && mp->b_wptr >= mp->b_rptr);
 419  420          ptr1 = (uintptr_t)mp->b_rptr + offset;
 420  421          ptr2 = (uintptr_t)ptr1 + length;
 421  422          if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) {
 422  423                  eprintline(0);
 423  424                  return (NULL);
 424  425          }
 425  426          if ((ptr1 & (align_size - 1)) != 0) {
 426  427                  eprintline(0);
 427  428                  return (NULL);
 428  429          }
  
    | 
      ↓ open down ↓ | 
    395 lines elided | 
    
      ↑ open up ↑ | 
  
 429  430          return ((void *)ptr1);
 430  431  }
 431  432  
 432  433  /*
 433  434   * Return the AF_UNIX underlying filesystem vnode matching a given name.
 434  435   * Makes sure the sending and the destination sonodes are compatible.
 435  436   * The vnode is returned held.
 436  437   *
 437  438   * The underlying filesystem VSOCK vnode has a v_stream pointer that
 438  439   * references the actual stream head (hence indirectly the actual sonode).
      440 + *
      441 + * This function is non-static so it can be used by brand emulation.
 439  442   */
 440      -static int
      443 +int
 441  444  so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess,
 442      -                vnode_t **vpp)
      445 +    vnode_t **vpp)
 443  446  {
 444  447          vnode_t         *vp;    /* Underlying filesystem vnode */
 445  448          vnode_t         *rvp;   /* real vnode */
 446  449          vnode_t         *svp;   /* sockfs vnode */
 447  450          struct sonode   *so2;
 448  451          int             error;
 449  452  
 450  453          dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n", (void *)so,
 451  454              soun->sun_path));
 452  455  
 453  456          error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
 454  457          if (error) {
 455  458                  eprintsoline(so, error);
 456  459                  return (error);
 457  460          }
 458  461  
 459  462          /*
 460  463           * Traverse lofs mounts get the real vnode
 461  464           */
 462  465          if (VOP_REALVP(vp, &rvp, NULL) == 0) {
 463  466                  VN_HOLD(rvp);           /* hold the real vnode */
 464  467                  VN_RELE(vp);            /* release hold from lookup */
 465  468                  vp = rvp;
 466  469          }
 467  470  
 468  471          if (vp->v_type != VSOCK) {
 469  472                  error = ENOTSOCK;
 470  473                  eprintsoline(so, error);
 471  474                  goto done2;
 472  475          }
 473  476  
 474  477          if (checkaccess) {
 475  478                  /*
 476  479                   * Check that we have permissions to access the destination
 477  480                   * vnode. This check is not done in BSD but it is required
 478  481                   * by X/Open.
 479  482                   */
 480  483                  if (error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL)) {
 481  484                          eprintsoline(so, error);
 482  485                          goto done2;
 483  486                  }
 484  487          }
 485  488  
 486  489          /*
 487  490           * Check if the remote socket has been closed.
 488  491           *
 489  492           * Synchronize with vn_rele_stream by holding v_lock while traversing
 490  493           * v_stream->sd_vnode.
 491  494           */
 492  495          mutex_enter(&vp->v_lock);
 493  496          if (vp->v_stream == NULL) {
 494  497                  mutex_exit(&vp->v_lock);
 495  498                  if (so->so_type == SOCK_DGRAM)
 496  499                          error = EDESTADDRREQ;
 497  500                  else
 498  501                          error = ECONNREFUSED;
 499  502  
 500  503                  eprintsoline(so, error);
 501  504                  goto done2;
 502  505          }
 503  506          ASSERT(vp->v_stream->sd_vnode);
 504  507          svp = vp->v_stream->sd_vnode;
 505  508          /*
 506  509           * holding v_lock on underlying filesystem vnode and acquiring
 507  510           * it on sockfs vnode. Assumes that no code ever attempts to
 508  511           * acquire these locks in the reverse order.
 509  512           */
 510  513          VN_HOLD(svp);
 511  514          mutex_exit(&vp->v_lock);
 512  515  
 513  516          if (svp->v_type != VSOCK) {
 514  517                  error = ENOTSOCK;
 515  518                  eprintsoline(so, error);
 516  519                  goto done;
 517  520          }
 518  521  
 519  522          so2 = VTOSO(svp);
 520  523  
 521  524          if (so->so_type != so2->so_type) {
 522  525                  error = EPROTOTYPE;
 523  526                  eprintsoline(so, error);
 524  527                  goto done;
 525  528          }
 526  529  
 527  530          VN_RELE(svp);
 528  531          *vpp = vp;
 529  532          return (0);
 530  533  
 531  534  done:
 532  535          VN_RELE(svp);
 533  536  done2:
 534  537          VN_RELE(vp);
 535  538          return (error);
 536  539  }
 537  540  
 538  541  /*
 539  542   * Verify peer address for connect and sendto/sendmsg.
 540  543   * Since sendto/sendmsg would not get synchronous errors from the transport
 541  544   * provider we have to do these ugly checks in the socket layer to
 542  545   * preserve compatibility with SunOS 4.X.
 543  546   */
 544  547  int
 545  548  so_addr_verify(struct sonode *so, const struct sockaddr *name,
 546  549      socklen_t namelen)
 547  550  {
 548  551          int             family;
 549  552  
 550  553          dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n",
 551  554              (void *)so, (void *)name, namelen));
 552  555  
 553  556          ASSERT(name != NULL);
 554  557  
 555  558          family = so->so_family;
 556  559          switch (family) {
 557  560          case AF_INET:
 558  561                  if (name->sa_family != family) {
 559  562                          eprintsoline(so, EAFNOSUPPORT);
 560  563                          return (EAFNOSUPPORT);
 561  564                  }
 562  565                  if (namelen != (socklen_t)sizeof (struct sockaddr_in)) {
 563  566                          eprintsoline(so, EINVAL);
 564  567                          return (EINVAL);
 565  568                  }
 566  569                  break;
 567  570          case AF_INET6: {
 568  571  #ifdef DEBUG
 569  572                  struct sockaddr_in6 *sin6;
 570  573  #endif /* DEBUG */
 571  574  
 572  575                  if (name->sa_family != family) {
 573  576                          eprintsoline(so, EAFNOSUPPORT);
 574  577                          return (EAFNOSUPPORT);
 575  578                  }
 576  579                  if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) {
 577  580                          eprintsoline(so, EINVAL);
 578  581                          return (EINVAL);
 579  582                  }
 580  583  #ifdef DEBUG
 581  584                  /* Verify that apps don't forget to clear sin6_scope_id etc */
 582  585                  sin6 = (struct sockaddr_in6 *)name;
 583  586                  if (sin6->sin6_scope_id != 0 &&
 584  587                      !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
 585  588                          zcmn_err(getzoneid(), CE_WARN,
 586  589                              "connect/send* with uninitialized sin6_scope_id "
 587  590                              "(%d) on socket. Pid = %d\n",
 588  591                              (int)sin6->sin6_scope_id, (int)curproc->p_pid);
 589  592                  }
 590  593  #endif /* DEBUG */
 591  594                  break;
 592  595          }
 593  596          case AF_UNIX:
 594  597                  if (SOTOTPI(so)->sti_faddr_noxlate) {
 595  598                          return (0);
 596  599                  }
 597  600                  if (namelen < (socklen_t)sizeof (short)) {
 598  601                          eprintsoline(so, ENOENT);
 599  602                          return (ENOENT);
 600  603                  }
 601  604                  if (name->sa_family != family) {
 602  605                          eprintsoline(so, EAFNOSUPPORT);
 603  606                          return (EAFNOSUPPORT);
 604  607                  }
 605  608                  /* MAXPATHLEN + soun_family + nul termination */
 606  609                  if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
 607  610                          eprintsoline(so, ENAMETOOLONG);
 608  611                          return (ENAMETOOLONG);
 609  612                  }
 610  613  
 611  614                  break;
 612  615  
 613  616          default:
 614  617                  /*
 615  618                   * Default is don't do any length or sa_family check
 616  619                   * to allow non-sockaddr style addresses.
 617  620                   */
 618  621                  break;
 619  622          }
 620  623  
 621  624          return (0);
 622  625  }
 623  626  
 624  627  
 625  628  /*
 626  629   * Translate an AF_UNIX sockaddr_un to the transport internal name.
 627  630   * Assumes caller has called so_addr_verify first.
 628  631   */
 629  632  /*ARGSUSED*/
 630  633  int
 631  634  so_ux_addr_xlate(struct sonode *so, struct sockaddr *name,
 632  635      socklen_t namelen, int checkaccess,
 633  636      void **addrp, socklen_t *addrlenp)
 634  637  {
 635  638          int                     error;
 636  639          struct sockaddr_un      *soun;
 637  640          vnode_t                 *vp;
 638  641          void                    *addr;
 639  642          socklen_t               addrlen;
 640  643          sotpi_info_t            *sti = SOTOTPI(so);
 641  644  
 642  645          dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n",
 643  646              (void *)so, (void *)name, namelen, checkaccess));
 644  647  
 645  648          ASSERT(name != NULL);
 646  649          ASSERT(so->so_family == AF_UNIX);
 647  650          ASSERT(!sti->sti_faddr_noxlate);
 648  651          ASSERT(namelen >= (socklen_t)sizeof (short));
 649  652          ASSERT(name->sa_family == AF_UNIX);
 650  653          soun = (struct sockaddr_un *)name;
 651  654          /*
 652  655           * Lookup vnode for the specified path name and verify that
 653  656           * it is a socket.
 654  657           */
 655  658          error = so_ux_lookup(so, soun, checkaccess, &vp);
 656  659          if (error) {
 657  660                  eprintsoline(so, error);
 658  661                  return (error);
 659  662          }
 660  663          /*
 661  664           * Use the address of the peer vnode as the address to send
 662  665           * to. We release the peer vnode here. In case it has been
 663  666           * closed by the time the T_CONN_REQ or T_UNITDATA_REQ reaches the
 664  667           * transport the message will get an error or be dropped.
 665  668           */
 666  669          sti->sti_ux_faddr.soua_vp = vp;
 667  670          sti->sti_ux_faddr.soua_magic = SOU_MAGIC_EXPLICIT;
 668  671          addr = &sti->sti_ux_faddr;
 669  672          addrlen = (socklen_t)sizeof (sti->sti_ux_faddr);
 670  673          dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n",
 671  674              addrlen, (void *)vp));
 672  675          VN_RELE(vp);
 673  676          *addrp = addr;
 674  677          *addrlenp = (socklen_t)addrlen;
 675  678          return (0);
 676  679  }
 677  680  
 678  681  /*
 679  682   * Esballoc free function for messages that contain SO_FILEP option.
 680  683   * Decrement the reference count on the file pointers using closef.
 681  684   */
 682  685  void
 683  686  fdbuf_free(struct fdbuf *fdbuf)
 684  687  {
 685  688          int     i;
 686  689          struct file *fp;
 687  690  
 688  691          dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd));
 689  692          for (i = 0; i < fdbuf->fd_numfd; i++) {
 690  693                  /*
 691  694                   * We need pointer size alignment for fd_fds. On a LP64
 692  695                   * kernel, the required alignment is 8 bytes while
 693  696                   * the option headers and values are only 4 bytes
 694  697                   * aligned. So its safer to do a bcopy compared to
 695  698                   * assigning fdbuf->fd_fds[i] to fp.
 696  699                   */
 697  700                  bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
 698  701                  dprint(1, ("fdbuf_free: [%d] = %p\n", i, (void *)fp));
 699  702                  (void) closef(fp);
 700  703          }
 701  704          if (fdbuf->fd_ebuf != NULL)
 702  705                  kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen);
 703  706          kmem_free(fdbuf, fdbuf->fd_size);
 704  707  }
 705  708  
 706  709  /*
 707  710   * Allocate an esballoc'ed message for AF_UNIX file descriptor passing.
 708  711   * Waits if memory is not available.
 709  712   */
 710  713  mblk_t *
 711  714  fdbuf_allocmsg(int size, struct fdbuf *fdbuf)
 712  715  {
 713  716          uchar_t *buf;
 714  717          mblk_t  *mp;
 715  718  
 716  719          dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd));
 717  720          buf = kmem_alloc(size, KM_SLEEP);
 718  721          fdbuf->fd_ebuf = (caddr_t)buf;
 719  722          fdbuf->fd_ebuflen = size;
 720  723          fdbuf->fd_frtn.free_func = fdbuf_free;
 721  724          fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf;
 722  725  
 723  726          mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn);
 724  727          mp->b_datap->db_type = M_PROTO;
 725  728          return (mp);
 726  729  }
 727  730  
 728  731  /*
 729  732   * Extract file descriptors from a fdbuf.
 730  733   * Return list in rights/rightslen.
 731  734   */
 732  735  /*ARGSUSED*/
 733  736  static int
 734  737  fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen)
 735  738  {
 736  739          int     i, fd;
 737  740          int     *rp;
 738  741          struct file *fp;
 739  742          int     numfd;
 740  743  
 741  744          dprint(1, ("fdbuf_extract: %d fds, len %d\n",
 742  745              fdbuf->fd_numfd, rightslen));
 743  746  
 744  747          numfd = fdbuf->fd_numfd;
 745  748          ASSERT(rightslen == numfd * (int)sizeof (int));
 746  749  
 747  750          /*
 748  751           * Allocate a file descriptor and increment the f_count.
 749  752           * The latter is needed since we always call fdbuf_free
 750  753           * which performs a closef.
 751  754           */
 752  755          rp = (int *)rights;
 753  756          for (i = 0; i < numfd; i++) {
 754  757                  if ((fd = ufalloc(0)) == -1)
 755  758                          goto cleanup;
 756  759                  /*
 757  760                   * We need pointer size alignment for fd_fds. On a LP64
 758  761                   * kernel, the required alignment is 8 bytes while
 759  762                   * the option headers and values are only 4 bytes
 760  763                   * aligned. So its safer to do a bcopy compared to
 761  764                   * assigning fdbuf->fd_fds[i] to fp.
 762  765                   */
 763  766                  bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
 764  767                  mutex_enter(&fp->f_tlock);
 765  768                  fp->f_count++;
 766  769                  mutex_exit(&fp->f_tlock);
 767  770                  setf(fd, fp);
 768  771                  *rp++ = fd;
 769  772                  if (AU_AUDITING())
 770  773                          audit_fdrecv(fd, fp);
 771  774                  dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n",
 772  775                      i, fd, (void *)fp, fp->f_count));
 773  776          }
 774  777          return (0);
 775  778  
 776  779  cleanup:
 777  780          /*
 778  781           * Undo whatever partial work the loop above has done.
 779  782           */
 780  783          {
 781  784                  int j;
 782  785  
 783  786                  rp = (int *)rights;
 784  787                  for (j = 0; j < i; j++) {
 785  788                          dprint(0,
 786  789                              ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp));
 787  790                          (void) closeandsetf(*rp++, NULL);
 788  791                  }
 789  792          }
 790  793  
 791  794          return (EMFILE);
 792  795  }
 793  796  
 794  797  /*
 795  798   * Insert file descriptors into an fdbuf.
 796  799   * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed
 797  800   * by calling fdbuf_free().
 798  801   */
 799  802  int
 800  803  fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp)
 801  804  {
 802  805          int             numfd, i;
 803  806          int             *fds;
 804  807          struct file     *fp;
 805  808          struct fdbuf    *fdbuf;
 806  809          int             fdbufsize;
 807  810  
 808  811          dprint(1, ("fdbuf_create: len %d\n", rightslen));
 809  812  
 810  813          numfd = rightslen / (int)sizeof (int);
 811  814  
 812  815          fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *));
 813  816          fdbuf = kmem_alloc(fdbufsize, KM_SLEEP);
 814  817          fdbuf->fd_size = fdbufsize;
 815  818          fdbuf->fd_numfd = 0;
 816  819          fdbuf->fd_ebuf = NULL;
 817  820          fdbuf->fd_ebuflen = 0;
 818  821          fds = (int *)rights;
 819  822          for (i = 0; i < numfd; i++) {
 820  823                  if ((fp = getf(fds[i])) == NULL) {
 821  824                          fdbuf_free(fdbuf);
 822  825                          return (EBADF);
 823  826                  }
 824  827                  dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n",
 825  828                      i, fds[i], (void *)fp, fp->f_count));
 826  829                  mutex_enter(&fp->f_tlock);
 827  830                  fp->f_count++;
 828  831                  mutex_exit(&fp->f_tlock);
 829  832                  /*
 830  833                   * The maximum alignment for fdbuf (or any option header
 831  834                   * and its value) it 4 bytes. On a LP64 kernel, the alignment
 832  835                   * is not sufficient for pointers (fd_fds in this case). Since
 833  836                   * we just did a kmem_alloc (we get a double word alignment),
 834  837                   * we don't need to do anything on the send side (we loose
 835  838                   * the double word alignment because fdbuf goes after an
 836  839                   * option header (eg T_unitdata_req) which is only 4 byte
 837  840                   * aligned). We take care of this when we extract the file
 838  841                   * descriptor in fdbuf_extract or fdbuf_free.
 839  842                   */
 840  843                  fdbuf->fd_fds[i] = fp;
 841  844                  fdbuf->fd_numfd++;
 842  845                  releasef(fds[i]);
 843  846                  if (AU_AUDITING())
 844  847                          audit_fdsend(fds[i], fp, 0);
 845  848          }
 846  849          *fdbufp = fdbuf;
 847  850          return (0);
 848  851  }
 849  852  
 850  853  static int
 851  854  fdbuf_optlen(int rightslen)
 852  855  {
 853  856          int numfd;
 854  857  
 855  858          numfd = rightslen / (int)sizeof (int);
 856  859  
 857  860          return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)));
 858  861  }
 859  862  
 860  863  static t_uscalar_t
 861  864  fdbuf_cmsglen(int fdbuflen)
 862  865  {
 863  866          return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) /
 864  867              (int)sizeof (struct file *) * (int)sizeof (int));
 865  868  }
 866  869  
 867  870  
 868  871  /*
 869  872   * Return non-zero if the mblk and fdbuf are consistent.
 870  873   */
 871  874  static int
 872  875  fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen)
 873  876  {
 874  877          if (fdbuflen >= FDBUF_HDRSIZE &&
 875  878              fdbuflen == fdbuf->fd_size) {
 876  879                  frtn_t *frp = mp->b_datap->db_frtnp;
 877  880                  /*
 878  881                   * Check that the SO_FILEP portion of the
 879  882                   * message has not been modified by
 880  883                   * the loopback transport. The sending sockfs generates
 881  884                   * a message that is esballoc'ed with the free function
 882  885                   * being fdbuf_free() and where free_arg contains the
 883  886                   * identical information as the SO_FILEP content.
 884  887                   *
 885  888                   * If any of these constraints are not satisfied we
 886  889                   * silently ignore the option.
 887  890                   */
 888  891                  ASSERT(mp);
 889  892                  if (frp != NULL &&
 890  893                      frp->free_func == fdbuf_free &&
 891  894                      frp->free_arg != NULL &&
 892  895                      bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) {
 893  896                          dprint(1, ("fdbuf_verify: fdbuf %p len %d\n",
 894  897                              (void *)fdbuf, fdbuflen));
 895  898                          return (1);
 896  899                  } else {
 897  900                          zcmn_err(getzoneid(), CE_WARN,
 898  901                              "sockfs: mismatched fdbuf content (%p)",
 899  902                              (void *)mp);
 900  903                          return (0);
 901  904                  }
 902  905          } else {
 903  906                  zcmn_err(getzoneid(), CE_WARN,
 904  907                      "sockfs: mismatched fdbuf len %d, %d\n",
 905  908                      fdbuflen, fdbuf->fd_size);
 906  909                  return (0);
 907  910          }
 908  911  }
 909  912  
 910  913  /*
 911  914   * When the file descriptors returned by sorecvmsg can not be passed
 912  915   * to the application this routine will cleanup the references on
 913  916   * the files. Start at startoff bytes into the buffer.
 914  917   */
 915  918  static void
 916  919  close_fds(void *fdbuf, int fdbuflen, int startoff)
 917  920  {
 918  921          int *fds = (int *)fdbuf;
 919  922          int numfd = fdbuflen / (int)sizeof (int);
 920  923          int i;
 921  924  
 922  925          dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff));
 923  926  
 924  927          for (i = 0; i < numfd; i++) {
 925  928                  if (startoff < 0)
 926  929                          startoff = 0;
 927  930                  if (startoff < (int)sizeof (int)) {
 928  931                          /*
 929  932                           * This file descriptor is partially or fully after
 930  933                           * the offset
 931  934                           */
 932  935                          dprint(0,
 933  936                              ("close_fds: cleanup[%d] = %d\n", i, fds[i]));
 934  937                          (void) closeandsetf(fds[i], NULL);
 935  938                  }
 936  939                  startoff -= (int)sizeof (int);
 937  940          }
 938  941  }
 939  942  
 940  943  /*
 941  944   * Close all file descriptors contained in the control part starting at
 942  945   * the startoffset.
 943  946   */
 944  947  void
 945  948  so_closefds(void *control, t_uscalar_t controllen, int oldflg,
 946  949      int startoff)
 947  950  {
 948  951          struct cmsghdr *cmsg;
 949  952  
 950  953          if (control == NULL)
 951  954                  return;
 952  955  
 953  956          if (oldflg) {
 954  957                  close_fds(control, controllen, startoff);
 955  958                  return;
 956  959          }
 957  960          /* Scan control part for file descriptors. */
 958  961          for (cmsg = (struct cmsghdr *)control;
 959  962              CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
 960  963              cmsg = CMSG_NEXT(cmsg)) {
 961  964                  if (cmsg->cmsg_level == SOL_SOCKET &&
 962  965                      cmsg->cmsg_type == SCM_RIGHTS) {
 963  966                          close_fds(CMSG_CONTENT(cmsg),
 964  967                              (int)CMSG_CONTENTLEN(cmsg),
 965  968                              startoff - (int)sizeof (struct cmsghdr));
 966  969                  }
 967  970                  startoff -= cmsg->cmsg_len;
 968  971          }
 969  972  }
 970  973  
 971  974  /*
 972  975   * Returns a pointer/length for the file descriptors contained
 973  976   * in the control buffer. Returns with *fdlenp == -1 if there are no
 974  977   * file descriptor options present. This is different than there being
 975  978   * a zero-length file descriptor option.
 976  979   * Fail if there are multiple SCM_RIGHT cmsgs.
 977  980   */
 978  981  int
 979  982  so_getfdopt(void *control, t_uscalar_t controllen, int oldflg,
 980  983      void **fdsp, int *fdlenp)
 981  984  {
 982  985          struct cmsghdr *cmsg;
 983  986          void *fds;
 984  987          int fdlen;
 985  988  
 986  989          if (control == NULL) {
 987  990                  *fdsp = NULL;
 988  991                  *fdlenp = -1;
 989  992                  return (0);
 990  993          }
 991  994  
 992  995          if (oldflg) {
 993  996                  *fdsp = control;
 994  997                  if (controllen == 0)
 995  998                          *fdlenp = -1;
 996  999                  else
 997 1000                          *fdlenp = controllen;
 998 1001                  dprint(1, ("so_getfdopt: old %d\n", *fdlenp));
 999 1002                  return (0);
1000 1003          }
1001 1004  
1002 1005          fds = NULL;
1003 1006          fdlen = 0;
1004 1007  
1005 1008          for (cmsg = (struct cmsghdr *)control;
1006 1009              CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1007 1010              cmsg = CMSG_NEXT(cmsg)) {
1008 1011                  if (cmsg->cmsg_level == SOL_SOCKET &&
1009 1012                      cmsg->cmsg_type == SCM_RIGHTS) {
1010 1013                          if (fds != NULL)
1011 1014                                  return (EINVAL);
1012 1015                          fds = CMSG_CONTENT(cmsg);
1013 1016                          fdlen = (int)CMSG_CONTENTLEN(cmsg);
1014 1017                          dprint(1, ("so_getfdopt: new %lu\n",
1015 1018                              (size_t)CMSG_CONTENTLEN(cmsg)));
1016 1019                  }
1017 1020          }
1018 1021          if (fds == NULL) {
1019 1022                  dprint(1, ("so_getfdopt: NONE\n"));
1020 1023                  *fdlenp = -1;
1021 1024          } else
1022 1025                  *fdlenp = fdlen;
1023 1026          *fdsp = fds;
1024 1027          return (0);
1025 1028  }
1026 1029  
1027 1030  /*
1028 1031   * Return the length of the options including any file descriptor options.
1029 1032   */
1030 1033  t_uscalar_t
1031 1034  so_optlen(void *control, t_uscalar_t controllen, int oldflg)
1032 1035  {
1033 1036          struct cmsghdr *cmsg;
1034 1037          t_uscalar_t optlen = 0;
1035 1038          t_uscalar_t len;
1036 1039  
1037 1040          if (control == NULL)
1038 1041                  return (0);
1039 1042  
1040 1043          if (oldflg)
1041 1044                  return ((t_uscalar_t)(sizeof (struct T_opthdr) +
1042 1045                      fdbuf_optlen(controllen)));
1043 1046  
1044 1047          for (cmsg = (struct cmsghdr *)control;
1045 1048              CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1046 1049              cmsg = CMSG_NEXT(cmsg)) {
1047 1050                  if (cmsg->cmsg_level == SOL_SOCKET &&
1048 1051                      cmsg->cmsg_type == SCM_RIGHTS) {
1049 1052                          len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg));
1050 1053                  } else {
1051 1054                          len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
1052 1055                  }
1053 1056                  optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) +
1054 1057                      sizeof (struct T_opthdr));
1055 1058          }
1056 1059          dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n",
1057 1060              controllen, oldflg, optlen));
1058 1061          return (optlen);
1059 1062  }
1060 1063  
1061 1064  /*
1062 1065   * Copy options from control to the mblk. Skip any file descriptor options.
1063 1066   */
1064 1067  void
1065 1068  so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp)
1066 1069  {
1067 1070          struct T_opthdr toh;
1068 1071          struct cmsghdr *cmsg;
1069 1072  
1070 1073          if (control == NULL)
1071 1074                  return;
1072 1075  
1073 1076          if (oldflg) {
1074 1077                  /* No real options - caller has handled file descriptors */
1075 1078                  return;
1076 1079          }
1077 1080          for (cmsg = (struct cmsghdr *)control;
1078 1081              CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1079 1082              cmsg = CMSG_NEXT(cmsg)) {
1080 1083                  /*
1081 1084                   * Note: The caller handles file descriptors prior
1082 1085                   * to calling this function.
1083 1086                   */
1084 1087                  t_uscalar_t len;
1085 1088  
1086 1089                  if (cmsg->cmsg_level == SOL_SOCKET &&
1087 1090                      cmsg->cmsg_type == SCM_RIGHTS)
1088 1091                          continue;
1089 1092  
1090 1093                  len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
1091 1094                  toh.level = cmsg->cmsg_level;
1092 1095                  toh.name = cmsg->cmsg_type;
1093 1096                  toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr);
1094 1097                  toh.status = 0;
1095 1098  
1096 1099                  soappendmsg(mp, &toh, sizeof (toh));
1097 1100                  soappendmsg(mp, CMSG_CONTENT(cmsg), len);
1098 1101                  mp->b_wptr += _TPI_ALIGN_TOPT(len) - len;
1099 1102                  ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
1100 1103          }
1101 1104  }
1102 1105  
1103 1106  /*
1104 1107   * Return the length of the control message derived from the options.
1105 1108   * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP.
1106 1109   * When oldflg is set only include SO_FILEP.
1107 1110   * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
1108 1111   * allocates the space that so_opt2cmsg fills. If one changes, the other should
1109 1112   * also be checked for any possible impacts.
1110 1113   */
1111 1114  t_uscalar_t
1112 1115  so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg)
1113 1116  {
1114 1117          t_uscalar_t cmsglen = 0;
1115 1118          struct T_opthdr *tohp;
1116 1119          t_uscalar_t len;
1117 1120          t_uscalar_t last_roundup = 0;
1118 1121  
1119 1122          ASSERT(__TPI_TOPT_ISALIGNED(opt));
1120 1123  
1121 1124          for (tohp = (struct T_opthdr *)opt;
1122 1125              tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1123 1126              tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1124 1127                  dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n",
1125 1128                      tohp->level, tohp->name, tohp->len));
1126 1129                  if (tohp->level == SOL_SOCKET &&
1127 1130                      (tohp->name == SO_SRCADDR ||
1128 1131                      tohp->name == SO_UNIX_CLOSE)) {
1129 1132                          continue;
1130 1133                  }
1131 1134                  if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
1132 1135                          struct fdbuf *fdbuf;
1133 1136                          int fdbuflen;
1134 1137  
1135 1138                          fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
1136 1139                          fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
1137 1140  
1138 1141                          if (!fdbuf_verify(mp, fdbuf, fdbuflen))
1139 1142                                  continue;
1140 1143                          if (oldflg) {
1141 1144                                  cmsglen += fdbuf_cmsglen(fdbuflen);
1142 1145                                  continue;
1143 1146                          }
1144 1147                          len = fdbuf_cmsglen(fdbuflen);
1145 1148                  } else if (tohp->level == SOL_SOCKET &&
1146 1149                      tohp->name == SCM_TIMESTAMP) {
1147 1150                          if (oldflg)
1148 1151                                  continue;
1149 1152  
1150 1153                          if (get_udatamodel() == DATAMODEL_NATIVE) {
1151 1154                                  len = sizeof (struct timeval);
1152 1155                          } else {
1153 1156                                  len = sizeof (struct timeval32);
1154 1157                          }
1155 1158                  } else {
1156 1159                          if (oldflg)
1157 1160                                  continue;
1158 1161                          len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
1159 1162                  }
1160 1163                  /*
1161 1164                   * Exclude roundup for last option to not set
1162 1165                   * MSG_CTRUNC when the cmsg fits but the padding doesn't fit.
1163 1166                   */
1164 1167                  last_roundup = (t_uscalar_t)
1165 1168                      (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) -
1166 1169                      (len + (int)sizeof (struct cmsghdr)));
1167 1170                  cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) +
1168 1171                      last_roundup;
1169 1172          }
1170 1173          cmsglen -= last_roundup;
1171 1174          dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n",
1172 1175              optlen, oldflg, cmsglen));
1173 1176          return (cmsglen);
1174 1177  }
1175 1178  
1176 1179  /*
1177 1180   * Copy options from options to the control. Convert SO_FILEP to
1178 1181   * file descriptors.
1179 1182   * Returns errno or zero.
1180 1183   * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
1181 1184   * allocates the space that so_opt2cmsg fills. If one changes, the other should
1182 1185   * also be checked for any possible impacts.
1183 1186   */
1184 1187  int
1185 1188  so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg,
1186 1189      void *control, t_uscalar_t controllen)
1187 1190  {
1188 1191          struct T_opthdr *tohp;
1189 1192          struct cmsghdr *cmsg;
1190 1193          struct fdbuf *fdbuf;
1191 1194          int fdbuflen;
1192 1195          int error;
1193 1196  #if defined(DEBUG) || defined(__lint)
1194 1197          struct cmsghdr *cend = (struct cmsghdr *)
1195 1198              (((uint8_t *)control) + ROUNDUP_cmsglen(controllen));
1196 1199  #endif
1197 1200          cmsg = (struct cmsghdr *)control;
1198 1201  
1199 1202          ASSERT(__TPI_TOPT_ISALIGNED(opt));
1200 1203  
1201 1204          for (tohp = (struct T_opthdr *)opt;
1202 1205              tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1203 1206              tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1204 1207                  dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n",
1205 1208                      tohp->level, tohp->name, tohp->len));
1206 1209  
1207 1210                  if (tohp->level == SOL_SOCKET &&
1208 1211                      (tohp->name == SO_SRCADDR ||
1209 1212                      tohp->name == SO_UNIX_CLOSE)) {
1210 1213                          continue;
1211 1214                  }
1212 1215                  ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen);
1213 1216                  if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
1214 1217                          fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
1215 1218                          fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
1216 1219  
1217 1220                          if (!fdbuf_verify(mp, fdbuf, fdbuflen))
1218 1221                                  return (EPROTO);
1219 1222                          if (oldflg) {
1220 1223                                  error = fdbuf_extract(fdbuf, control,
1221 1224                                      (int)controllen);
1222 1225                                  if (error != 0)
1223 1226                                          return (error);
1224 1227                                  continue;
1225 1228                          } else {
1226 1229                                  int fdlen;
1227 1230  
1228 1231                                  fdlen = (int)fdbuf_cmsglen(
1229 1232                                      (int)_TPI_TOPT_DATALEN(tohp));
1230 1233  
1231 1234                                  cmsg->cmsg_level = tohp->level;
1232 1235                                  cmsg->cmsg_type = SCM_RIGHTS;
1233 1236                                  cmsg->cmsg_len = (socklen_t)(fdlen +
1234 1237                                      sizeof (struct cmsghdr));
1235 1238  
1236 1239                                  error = fdbuf_extract(fdbuf,
1237 1240                                      CMSG_CONTENT(cmsg), fdlen);
1238 1241                                  if (error != 0)
1239 1242                                          return (error);
1240 1243                          }
1241 1244                  } else if (tohp->level == SOL_SOCKET &&
1242 1245                      tohp->name == SCM_TIMESTAMP) {
1243 1246                          timestruc_t *timestamp;
1244 1247  
1245 1248                          if (oldflg)
1246 1249                                  continue;
1247 1250  
1248 1251                          cmsg->cmsg_level = tohp->level;
1249 1252                          cmsg->cmsg_type = tohp->name;
1250 1253  
1251 1254                          timestamp =
1252 1255                              (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1],
1253 1256                              sizeof (intptr_t));
1254 1257  
1255 1258                          if (get_udatamodel() == DATAMODEL_NATIVE) {
1256 1259                                  struct timeval tv;
1257 1260  
1258 1261                                  cmsg->cmsg_len = sizeof (struct timeval) +
1259 1262                                      sizeof (struct cmsghdr);
1260 1263                                  tv.tv_sec = timestamp->tv_sec;
1261 1264                                  tv.tv_usec = timestamp->tv_nsec /
1262 1265                                      (NANOSEC / MICROSEC);
1263 1266                                  /*
1264 1267                                   * on LP64 systems, the struct timeval in
1265 1268                                   * the destination will not be 8-byte aligned,
1266 1269                                   * so use bcopy to avoid alignment trouble
1267 1270                                   */
1268 1271                                  bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv));
1269 1272                          } else {
1270 1273                                  struct timeval32 *time32;
1271 1274  
1272 1275                                  cmsg->cmsg_len = sizeof (struct timeval32) +
1273 1276                                      sizeof (struct cmsghdr);
1274 1277                                  time32 = (struct timeval32 *)CMSG_CONTENT(cmsg);
1275 1278                                  time32->tv_sec = (time32_t)timestamp->tv_sec;
1276 1279                                  time32->tv_usec =
1277 1280                                      (int32_t)(timestamp->tv_nsec /
1278 1281                                      (NANOSEC / MICROSEC));
1279 1282                          }
1280 1283  
1281 1284                  } else {
1282 1285                          if (oldflg)
1283 1286                                  continue;
1284 1287  
1285 1288                          cmsg->cmsg_level = tohp->level;
1286 1289                          cmsg->cmsg_type = tohp->name;
1287 1290                          cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) +
1288 1291                              sizeof (struct cmsghdr));
1289 1292  
1290 1293                          /* copy content to control data part */
1291 1294                          bcopy(&tohp[1], CMSG_CONTENT(cmsg),
1292 1295                              CMSG_CONTENTLEN(cmsg));
1293 1296                  }
1294 1297                  /* move to next CMSG structure! */
1295 1298                  cmsg = CMSG_NEXT(cmsg);
1296 1299          }
1297 1300          dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n",
1298 1301              control, controllen, (void *)cend, (void *)cmsg));
1299 1302          ASSERT(cmsg <= cend);
1300 1303          return (0);
1301 1304  }
1302 1305  
1303 1306  /*
1304 1307   * Extract the SO_SRCADDR option value if present.
1305 1308   */
1306 1309  void
1307 1310  so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp,
1308 1311      t_uscalar_t *srclenp)
1309 1312  {
1310 1313          struct T_opthdr         *tohp;
1311 1314  
1312 1315          ASSERT(__TPI_TOPT_ISALIGNED(opt));
1313 1316  
1314 1317          ASSERT(srcp != NULL && srclenp != NULL);
1315 1318          *srcp = NULL;
1316 1319          *srclenp = 0;
1317 1320  
1318 1321          for (tohp = (struct T_opthdr *)opt;
1319 1322              tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1320 1323              tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1321 1324                  dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n",
1322 1325                      tohp->level, tohp->name, tohp->len));
1323 1326                  if (tohp->level == SOL_SOCKET &&
1324 1327                      tohp->name == SO_SRCADDR) {
1325 1328                          *srcp = _TPI_TOPT_DATA(tohp);
1326 1329                          *srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
1327 1330                  }
1328 1331          }
1329 1332  }
1330 1333  
1331 1334  /*
1332 1335   * Verify if the SO_UNIX_CLOSE option is present.
1333 1336   */
1334 1337  int
1335 1338  so_getopt_unix_close(void *opt, t_uscalar_t optlen)
1336 1339  {
1337 1340          struct T_opthdr         *tohp;
1338 1341  
1339 1342          ASSERT(__TPI_TOPT_ISALIGNED(opt));
1340 1343  
1341 1344          for (tohp = (struct T_opthdr *)opt;
1342 1345              tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1343 1346              tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1344 1347                  dprint(1,
1345 1348                      ("so_getopt_unix_close: level 0x%x, name %d, len %d\n",
1346 1349                      tohp->level, tohp->name, tohp->len));
1347 1350                  if (tohp->level == SOL_SOCKET &&
1348 1351                      tohp->name == SO_UNIX_CLOSE)
1349 1352                          return (1);
1350 1353          }
1351 1354          return (0);
1352 1355  }
1353 1356  
1354 1357  /*
1355 1358   * Allocate an M_PROTO message.
1356 1359   *
1357 1360   * If allocation fails the behavior depends on sleepflg:
1358 1361   *      _ALLOC_NOSLEEP  fail immediately
1359 1362   *      _ALLOC_INTR     sleep for memory until a signal is caught
1360 1363   *      _ALLOC_SLEEP    sleep forever. Don't return NULL.
1361 1364   */
1362 1365  mblk_t *
1363 1366  soallocproto(size_t size, int sleepflg, cred_t *cr)
1364 1367  {
1365 1368          mblk_t  *mp;
1366 1369  
1367 1370          /* Round up size for reuse */
1368 1371          size = MAX(size, 64);
1369 1372          if (cr != NULL)
1370 1373                  mp = allocb_cred(size, cr, curproc->p_pid);
1371 1374          else
1372 1375                  mp = allocb(size, BPRI_MED);
1373 1376  
1374 1377          if (mp == NULL) {
1375 1378                  int error;      /* Dummy - error not returned to caller */
1376 1379  
1377 1380                  switch (sleepflg) {
1378 1381                  case _ALLOC_SLEEP:
1379 1382                          if (cr != NULL) {
1380 1383                                  mp = allocb_cred_wait(size, STR_NOSIG, &error,
1381 1384                                      cr, curproc->p_pid);
1382 1385                          } else {
1383 1386                                  mp = allocb_wait(size, BPRI_MED, STR_NOSIG,
1384 1387                                      &error);
1385 1388                          }
1386 1389                          ASSERT(mp);
1387 1390                          break;
1388 1391                  case _ALLOC_INTR:
1389 1392                          if (cr != NULL) {
1390 1393                                  mp = allocb_cred_wait(size, 0, &error, cr,
1391 1394                                      curproc->p_pid);
1392 1395                          } else {
1393 1396                                  mp = allocb_wait(size, BPRI_MED, 0, &error);
1394 1397                          }
1395 1398                          if (mp == NULL) {
1396 1399                                  /* Caught signal while sleeping for memory */
1397 1400                                  eprintline(ENOBUFS);
1398 1401                                  return (NULL);
1399 1402                          }
1400 1403                          break;
1401 1404                  case _ALLOC_NOSLEEP:
1402 1405                  default:
1403 1406                          eprintline(ENOBUFS);
1404 1407                          return (NULL);
1405 1408                  }
1406 1409          }
1407 1410          DB_TYPE(mp) = M_PROTO;
1408 1411          return (mp);
1409 1412  }
1410 1413  
1411 1414  /*
1412 1415   * Allocate an M_PROTO message with a single component.
1413 1416   * len is the length of buf. size is the amount to allocate.
1414 1417   *
1415 1418   * buf can be NULL with a non-zero len.
1416 1419   * This results in a bzero'ed chunk being placed the message.
1417 1420   */
1418 1421  mblk_t *
1419 1422  soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg,
1420 1423      cred_t *cr)
1421 1424  {
1422 1425          mblk_t  *mp;
1423 1426  
1424 1427          if (size == 0)
1425 1428                  size = len;
1426 1429  
1427 1430          ASSERT(size >= len);
1428 1431          /* Round up size for reuse */
1429 1432          size = MAX(size, 64);
1430 1433          mp = soallocproto(size, sleepflg, cr);
1431 1434          if (mp == NULL)
1432 1435                  return (NULL);
1433 1436          mp->b_datap->db_type = M_PROTO;
1434 1437          if (len != 0) {
1435 1438                  if (buf != NULL)
1436 1439                          bcopy(buf, mp->b_wptr, len);
1437 1440                  else
1438 1441                          bzero(mp->b_wptr, len);
1439 1442                  mp->b_wptr += len;
1440 1443          }
1441 1444          return (mp);
1442 1445  }
1443 1446  
1444 1447  /*
1445 1448   * Append buf/len to mp.
1446 1449   * The caller has to ensure that there is enough room in the mblk.
1447 1450   *
1448 1451   * buf can be NULL with a non-zero len.
1449 1452   * This results in a bzero'ed chunk being placed the message.
1450 1453   */
1451 1454  void
1452 1455  soappendmsg(mblk_t *mp, const void *buf, ssize_t len)
1453 1456  {
1454 1457          ASSERT(mp);
1455 1458  
1456 1459          if (len != 0) {
1457 1460                  /* Assert for room left */
1458 1461                  ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len);
1459 1462                  if (buf != NULL)
1460 1463                          bcopy(buf, mp->b_wptr, len);
1461 1464                  else
1462 1465                          bzero(mp->b_wptr, len);
1463 1466          }
1464 1467          mp->b_wptr += len;
1465 1468  }
1466 1469  
1467 1470  /*
1468 1471   * Create a message using two kernel buffers.
1469 1472   * If size is set that will determine the allocation size (e.g. for future
1470 1473   * soappendmsg calls). If size is zero it is derived from the buffer
1471 1474   * lengths.
1472 1475   */
1473 1476  mblk_t *
1474 1477  soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1475 1478      ssize_t size, int sleepflg, cred_t *cr)
1476 1479  {
1477 1480          mblk_t *mp;
1478 1481  
1479 1482          if (size == 0)
1480 1483                  size = len1 + len2;
1481 1484          ASSERT(size >= len1 + len2);
1482 1485  
1483 1486          mp = soallocproto1(buf1, len1, size, sleepflg, cr);
1484 1487          if (mp)
1485 1488                  soappendmsg(mp, buf2, len2);
1486 1489          return (mp);
1487 1490  }
1488 1491  
1489 1492  /*
1490 1493   * Create a message using three kernel buffers.
1491 1494   * If size is set that will determine the allocation size (for future
1492 1495   * soappendmsg calls). If size is zero it is derived from the buffer
1493 1496   * lengths.
1494 1497   */
1495 1498  mblk_t *
1496 1499  soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1497 1500      const void *buf3, ssize_t len3, ssize_t size, int sleepflg, cred_t *cr)
1498 1501  {
1499 1502          mblk_t *mp;
1500 1503  
1501 1504          if (size == 0)
1502 1505                  size = len1 + len2 +len3;
1503 1506          ASSERT(size >= len1 + len2 + len3);
1504 1507  
1505 1508          mp = soallocproto1(buf1, len1, size, sleepflg, cr);
1506 1509          if (mp != NULL) {
1507 1510                  soappendmsg(mp, buf2, len2);
1508 1511                  soappendmsg(mp, buf3, len3);
1509 1512          }
1510 1513          return (mp);
1511 1514  }
1512 1515  
1513 1516  #ifdef DEBUG
1514 1517  char *
1515 1518  pr_state(uint_t state, uint_t mode)
1516 1519  {
1517 1520          static char buf[1024];
1518 1521  
1519 1522          buf[0] = 0;
1520 1523          if (state & SS_ISCONNECTED)
1521 1524                  (void) strcat(buf, "ISCONNECTED ");
1522 1525          if (state & SS_ISCONNECTING)
1523 1526                  (void) strcat(buf, "ISCONNECTING ");
1524 1527          if (state & SS_ISDISCONNECTING)
1525 1528                  (void) strcat(buf, "ISDISCONNECTING ");
1526 1529          if (state & SS_CANTSENDMORE)
1527 1530                  (void) strcat(buf, "CANTSENDMORE ");
1528 1531  
1529 1532          if (state & SS_CANTRCVMORE)
1530 1533                  (void) strcat(buf, "CANTRCVMORE ");
1531 1534          if (state & SS_ISBOUND)
1532 1535                  (void) strcat(buf, "ISBOUND ");
1533 1536          if (state & SS_NDELAY)
1534 1537                  (void) strcat(buf, "NDELAY ");
1535 1538          if (state & SS_NONBLOCK)
1536 1539                  (void) strcat(buf, "NONBLOCK ");
1537 1540  
1538 1541          if (state & SS_ASYNC)
1539 1542                  (void) strcat(buf, "ASYNC ");
1540 1543          if (state & SS_ACCEPTCONN)
1541 1544                  (void) strcat(buf, "ACCEPTCONN ");
1542 1545          if (state & SS_SAVEDEOR)
1543 1546                  (void) strcat(buf, "SAVEDEOR ");
1544 1547  
1545 1548          if (state & SS_RCVATMARK)
1546 1549                  (void) strcat(buf, "RCVATMARK ");
1547 1550          if (state & SS_OOBPEND)
1548 1551                  (void) strcat(buf, "OOBPEND ");
1549 1552          if (state & SS_HAVEOOBDATA)
1550 1553                  (void) strcat(buf, "HAVEOOBDATA ");
1551 1554          if (state & SS_HADOOBDATA)
1552 1555                  (void) strcat(buf, "HADOOBDATA ");
1553 1556  
1554 1557          if (mode & SM_PRIV)
1555 1558                  (void) strcat(buf, "PRIV ");
1556 1559          if (mode & SM_ATOMIC)
1557 1560                  (void) strcat(buf, "ATOMIC ");
1558 1561          if (mode & SM_ADDR)
1559 1562                  (void) strcat(buf, "ADDR ");
1560 1563          if (mode & SM_CONNREQUIRED)
1561 1564                  (void) strcat(buf, "CONNREQUIRED ");
1562 1565  
1563 1566          if (mode & SM_FDPASSING)
1564 1567                  (void) strcat(buf, "FDPASSING ");
1565 1568          if (mode & SM_EXDATA)
1566 1569                  (void) strcat(buf, "EXDATA ");
1567 1570          if (mode & SM_OPTDATA)
1568 1571                  (void) strcat(buf, "OPTDATA ");
1569 1572          if (mode & SM_BYTESTREAM)
1570 1573                  (void) strcat(buf, "BYTESTREAM ");
1571 1574          return (buf);
1572 1575  }
1573 1576  
1574 1577  char *
1575 1578  pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen)
1576 1579  {
1577 1580          static char buf[1024];
1578 1581  
1579 1582          if (addr == NULL || addrlen == 0) {
1580 1583                  (void) sprintf(buf, "(len %d) %p", addrlen, (void *)addr);
1581 1584                  return (buf);
1582 1585          }
1583 1586          switch (family) {
1584 1587          case AF_INET: {
1585 1588                  struct sockaddr_in sin;
1586 1589  
1587 1590                  bcopy(addr, &sin, sizeof (sin));
1588 1591  
1589 1592                  (void) sprintf(buf, "(len %d) %x/%d",
1590 1593                      addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
1591 1594                  break;
1592 1595          }
1593 1596          case AF_INET6: {
1594 1597                  struct sockaddr_in6 sin6;
1595 1598                  uint16_t *piece = (uint16_t *)&sin6.sin6_addr;
1596 1599  
1597 1600                  bcopy((char *)addr, (char *)&sin6, sizeof (sin6));
1598 1601                  (void) sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d",
1599 1602                      addrlen,
1600 1603                      ntohs(piece[0]), ntohs(piece[1]),
1601 1604                      ntohs(piece[2]), ntohs(piece[3]),
1602 1605                      ntohs(piece[4]), ntohs(piece[5]),
1603 1606                      ntohs(piece[6]), ntohs(piece[7]),
1604 1607                      ntohs(sin6.sin6_port));
1605 1608                  break;
1606 1609          }
1607 1610          case AF_UNIX: {
1608 1611                  struct sockaddr_un *soun = (struct sockaddr_un *)addr;
1609 1612  
1610 1613                  (void) sprintf(buf, "(len %d) %s", addrlen,
1611 1614                      (soun == NULL) ? "(none)" : soun->sun_path);
1612 1615                  break;
1613 1616          }
1614 1617          default:
1615 1618                  (void) sprintf(buf, "(unknown af %d)", family);
1616 1619                  break;
1617 1620          }
1618 1621          return (buf);
1619 1622  }
1620 1623  
1621 1624  /* The logical equivalence operator (a if-and-only-if b) */
1622 1625  #define EQUIVALENT(a, b)        (((a) && (b)) || (!(a) && (!(b))))
1623 1626  
1624 1627  /*
1625 1628   * Verify limitations and invariants on oob state.
1626 1629   * Return 1 if OK, otherwise 0 so that it can be used as
1627 1630   *      ASSERT(verify_oobstate(so));
1628 1631   */
1629 1632  int
1630 1633  so_verify_oobstate(struct sonode *so)
1631 1634  {
1632 1635          boolean_t havemark;
1633 1636  
1634 1637          ASSERT(MUTEX_HELD(&so->so_lock));
1635 1638  
1636 1639          /*
1637 1640           * The possible state combinations are:
1638 1641           *      0
1639 1642           *      SS_OOBPEND
1640 1643           *      SS_OOBPEND|SS_HAVEOOBDATA
1641 1644           *      SS_OOBPEND|SS_HADOOBDATA
1642 1645           *      SS_HADOOBDATA
1643 1646           */
1644 1647          switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) {
1645 1648          case 0:
1646 1649          case SS_OOBPEND:
1647 1650          case SS_OOBPEND|SS_HAVEOOBDATA:
1648 1651          case SS_OOBPEND|SS_HADOOBDATA:
1649 1652          case SS_HADOOBDATA:
1650 1653                  break;
1651 1654          default:
1652 1655                  printf("Bad oob state 1 (%p): state %s\n",
1653 1656                      (void *)so, pr_state(so->so_state, so->so_mode));
1654 1657                  return (0);
1655 1658          }
1656 1659  
1657 1660          /* SS_RCVATMARK should only be set when SS_OOBPEND is set */
1658 1661          if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) {
1659 1662                  printf("Bad oob state 2 (%p): state %s\n",
1660 1663                      (void *)so, pr_state(so->so_state, so->so_mode));
1661 1664                  return (0);
1662 1665          }
1663 1666  
1664 1667          /*
1665 1668           * (havemark != 0 or SS_RCVATMARK) iff SS_OOBPEND
1666 1669           * For TPI, the presence of a "mark" is indicated by sti_oobsigcnt.
1667 1670           */
1668 1671          havemark = (SOCK_IS_NONSTR(so)) ? so->so_oobmark > 0 :
1669 1672              SOTOTPI(so)->sti_oobsigcnt > 0;
1670 1673  
1671 1674          if (!EQUIVALENT(havemark || (so->so_state & SS_RCVATMARK),
1672 1675              so->so_state & SS_OOBPEND)) {
1673 1676                  printf("Bad oob state 3 (%p): state %s\n",
1674 1677                      (void *)so, pr_state(so->so_state, so->so_mode));
1675 1678                  return (0);
1676 1679          }
1677 1680  
1678 1681          /*
1679 1682           * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA
1680 1683           */
1681 1684          if (!(so->so_options & SO_OOBINLINE) &&
1682 1685              !EQUIVALENT(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) {
1683 1686                  printf("Bad oob state 4 (%p): state %s\n",
1684 1687                      (void *)so, pr_state(so->so_state, so->so_mode));
1685 1688                  return (0);
1686 1689          }
1687 1690  
1688 1691          if (!SOCK_IS_NONSTR(so) &&
1689 1692              SOTOTPI(so)->sti_oobsigcnt < SOTOTPI(so)->sti_oobcnt) {
1690 1693                  printf("Bad oob state 5 (%p): counts %d/%d state %s\n",
1691 1694                      (void *)so, SOTOTPI(so)->sti_oobsigcnt,
1692 1695                      SOTOTPI(so)->sti_oobcnt,
1693 1696                      pr_state(so->so_state, so->so_mode));
1694 1697                  return (0);
1695 1698          }
1696 1699  
1697 1700          return (1);
1698 1701  }
1699 1702  #undef  EQUIVALENT
1700 1703  #endif /* DEBUG */
1701 1704  
1702 1705  /* initialize sockfs zone specific kstat related items                  */
1703 1706  void *
1704 1707  sock_kstat_init(zoneid_t zoneid)
1705 1708  {
1706 1709          kstat_t *ksp;
1707 1710  
1708 1711          ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc",
1709 1712              KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid);
1710 1713  
1711 1714          if (ksp != NULL) {
1712 1715                  ksp->ks_update = sockfs_update;
1713 1716                  ksp->ks_snapshot = sockfs_snapshot;
1714 1717                  ksp->ks_lock = &socklist.sl_lock;
1715 1718                  ksp->ks_private = (void *)(uintptr_t)zoneid;
1716 1719                  kstat_install(ksp);
1717 1720          }
1718 1721  
1719 1722          return (ksp);
1720 1723  }
1721 1724  
1722 1725  /* tear down sockfs zone specific kstat related items                   */
1723 1726  /*ARGSUSED*/
1724 1727  void
1725 1728  sock_kstat_fini(zoneid_t zoneid, void *arg)
1726 1729  {
1727 1730          kstat_t *ksp = (kstat_t *)arg;
1728 1731  
1729 1732          if (ksp != NULL) {
1730 1733                  ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private);
1731 1734                  kstat_delete(ksp);
1732 1735          }
1733 1736  }
1734 1737  
1735 1738  /*
1736 1739   * Zones:
1737 1740   * Note that nactive is going to be different for each zone.
1738 1741   * This means we require kstat to call sockfs_update and then sockfs_snapshot
1739 1742   * for the same zone, or sockfs_snapshot will be taken into the wrong size
1740 1743   * buffer. This is safe, but if the buffer is too small, user will not be
1741 1744   * given details of all sockets. However, as this kstat has a ks_lock, kstat
1742 1745   * driver will keep it locked between the update and the snapshot, so no
1743 1746   * other process (zone) can currently get inbetween resulting in a wrong size
1744 1747   * buffer allocation.
1745 1748   */
1746 1749  static int
1747 1750  sockfs_update(kstat_t *ksp, int rw)
1748 1751  {
1749 1752          uint_t  nactive = 0;            /* # of active AF_UNIX sockets  */
1750 1753          struct sonode   *so;            /* current sonode on socklist   */
1751 1754          zoneid_t        myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
1752 1755  
1753 1756          ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
1754 1757  
1755 1758          if (rw == KSTAT_WRITE) {        /* bounce all writes            */
1756 1759                  return (EACCES);
1757 1760          }
1758 1761  
1759 1762          for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
1760 1763                  if (so->so_count != 0 && so->so_zoneid == myzoneid) {
1761 1764                          nactive++;
1762 1765                  }
1763 1766          }
1764 1767          ksp->ks_ndata = nactive;
1765 1768          ksp->ks_data_size = nactive * sizeof (struct k_sockinfo);
1766 1769  
1767 1770          return (0);
1768 1771  }
1769 1772  
1770 1773  static int
1771 1774  sockfs_snapshot(kstat_t *ksp, void *buf, int rw)
1772 1775  {
1773 1776          int                     ns;     /* # of sonodes we've copied    */
1774 1777          struct sonode           *so;    /* current sonode on socklist   */
1775 1778          struct k_sockinfo       *pksi;  /* where we put sockinfo data   */
1776 1779          t_uscalar_t             sn_len; /* soa_len                      */
1777 1780          zoneid_t                myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
1778 1781          sotpi_info_t            *sti;
1779 1782  
1780 1783          ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
1781 1784  
1782 1785          ksp->ks_snaptime = gethrtime();
1783 1786  
1784 1787          if (rw == KSTAT_WRITE) {        /* bounce all writes            */
1785 1788                  return (EACCES);
1786 1789          }
1787 1790  
1788 1791          /*
1789 1792           * for each sonode on the socklist, we massage the important
1790 1793           * info into buf, in k_sockinfo format.
1791 1794           */
1792 1795          pksi = (struct k_sockinfo *)buf;
1793 1796          ns = 0;
1794 1797          for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
1795 1798                  /* only stuff active sonodes and the same zone:         */
1796 1799                  if (so->so_count == 0 || so->so_zoneid != myzoneid) {
1797 1800                          continue;
1798 1801                  }
1799 1802  
1800 1803                  /*
1801 1804                   * If the sonode was activated between the update and the
1802 1805                   * snapshot, we're done - as this is only a snapshot.
1803 1806                   */
1804 1807                  if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) {
1805 1808                          break;
1806 1809                  }
1807 1810  
1808 1811                  sti = SOTOTPI(so);
1809 1812                  /* copy important info into buf:                        */
1810 1813                  pksi->ks_si.si_size = sizeof (struct k_sockinfo);
1811 1814                  pksi->ks_si.si_family = so->so_family;
1812 1815                  pksi->ks_si.si_type = so->so_type;
1813 1816                  pksi->ks_si.si_flag = so->so_flag;
1814 1817                  pksi->ks_si.si_state = so->so_state;
1815 1818                  pksi->ks_si.si_serv_type = sti->sti_serv_type;
1816 1819                  pksi->ks_si.si_ux_laddr_sou_magic =
1817 1820                      sti->sti_ux_laddr.soua_magic;
1818 1821                  pksi->ks_si.si_ux_faddr_sou_magic =
1819 1822                      sti->sti_ux_faddr.soua_magic;
1820 1823                  pksi->ks_si.si_laddr_soa_len = sti->sti_laddr.soa_len;
1821 1824                  pksi->ks_si.si_faddr_soa_len = sti->sti_faddr.soa_len;
1822 1825                  pksi->ks_si.si_szoneid = so->so_zoneid;
1823 1826                  pksi->ks_si.si_faddr_noxlate = sti->sti_faddr_noxlate;
1824 1827  
1825 1828                  mutex_enter(&so->so_lock);
1826 1829  
1827 1830                  if (sti->sti_laddr_sa != NULL) {
1828 1831                          ASSERT(sti->sti_laddr_sa->sa_data != NULL);
1829 1832                          sn_len = sti->sti_laddr_len;
1830 1833                          ASSERT(sn_len <= sizeof (short) +
1831 1834                              sizeof (pksi->ks_si.si_laddr_sun_path));
1832 1835  
1833 1836                          pksi->ks_si.si_laddr_family =
1834 1837                              sti->sti_laddr_sa->sa_family;
1835 1838                          if (sn_len != 0) {
1836 1839                                  /* AF_UNIX socket names are NULL terminated */
1837 1840                                  (void) strncpy(pksi->ks_si.si_laddr_sun_path,
1838 1841                                      sti->sti_laddr_sa->sa_data,
1839 1842                                      sizeof (pksi->ks_si.si_laddr_sun_path));
1840 1843                                  sn_len = strlen(pksi->ks_si.si_laddr_sun_path);
1841 1844                          }
1842 1845                          pksi->ks_si.si_laddr_sun_path[sn_len] = 0;
1843 1846                  }
1844 1847  
1845 1848                  if (sti->sti_faddr_sa != NULL) {
1846 1849                          ASSERT(sti->sti_faddr_sa->sa_data != NULL);
1847 1850                          sn_len = sti->sti_faddr_len;
1848 1851                          ASSERT(sn_len <= sizeof (short) +
1849 1852                              sizeof (pksi->ks_si.si_faddr_sun_path));
1850 1853  
1851 1854                          pksi->ks_si.si_faddr_family =
1852 1855                              sti->sti_faddr_sa->sa_family;
1853 1856                          if (sn_len != 0) {
1854 1857                                  (void) strncpy(pksi->ks_si.si_faddr_sun_path,
1855 1858                                      sti->sti_faddr_sa->sa_data,
1856 1859                                      sizeof (pksi->ks_si.si_faddr_sun_path));
1857 1860                                  sn_len = strlen(pksi->ks_si.si_faddr_sun_path);
1858 1861                          }
1859 1862                          pksi->ks_si.si_faddr_sun_path[sn_len] = 0;
1860 1863                  }
1861 1864  
1862 1865                  mutex_exit(&so->so_lock);
1863 1866  
1864 1867                  (void) sprintf(pksi->ks_straddr[0], "%p", (void *)so);
1865 1868                  (void) sprintf(pksi->ks_straddr[1], "%p",
1866 1869                      (void *)sti->sti_ux_laddr.soua_vp);
1867 1870                  (void) sprintf(pksi->ks_straddr[2], "%p",
1868 1871                      (void *)sti->sti_ux_faddr.soua_vp);
1869 1872  
1870 1873                  ns++;
1871 1874                  pksi++;
  
    | 
      ↓ open down ↓ | 
    1419 lines elided | 
    
      ↑ open up ↑ | 
  
1872 1875          }
1873 1876  
1874 1877          ksp->ks_ndata = ns;
1875 1878          return (0);
1876 1879  }
1877 1880  
1878 1881  ssize_t
1879 1882  soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size)
1880 1883  {
1881 1884          struct uio auio;
1882      -        struct iovec aiov[MSG_MAXIOVLEN];
     1885 +        struct iovec aiov[1];
1883 1886          register vnode_t *vp;
1884 1887          int ioflag, rwflag;
1885 1888          ssize_t cnt;
1886 1889          int error = 0;
1887 1890          int iovcnt = 0;
1888 1891          short fflag;
1889 1892  
1890 1893          vp = fp->f_vnode;
1891 1894          fflag = fp->f_flag;
1892 1895  
1893 1896          rwflag = 0;
1894 1897          aiov[0].iov_base = (caddr_t)buf;
1895 1898          aiov[0].iov_len = size;
1896 1899          iovcnt = 1;
1897 1900          cnt = (ssize_t)size;
1898 1901          (void) VOP_RWLOCK(vp, rwflag, NULL);
1899 1902  
1900 1903          auio.uio_loffset = fileoff;
1901 1904          auio.uio_iov = aiov;
1902 1905          auio.uio_iovcnt = iovcnt;
1903 1906          auio.uio_resid = cnt;
1904 1907          auio.uio_segflg = UIO_SYSSPACE;
1905 1908          auio.uio_llimit = MAXOFFSET_T;
1906 1909          auio.uio_fmode = fflag;
1907 1910          auio.uio_extflg = UIO_COPY_CACHED;
1908 1911  
1909 1912          ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
1910 1913  
1911 1914          /* If read sync is not asked for, filter sync flags */
1912 1915          if ((ioflag & FRSYNC) == 0)
1913 1916                  ioflag &= ~(FSYNC|FDSYNC);
1914 1917          error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
1915 1918          cnt -= auio.uio_resid;
1916 1919  
1917 1920          VOP_RWUNLOCK(vp, rwflag, NULL);
1918 1921  
1919 1922          if (error == EINTR && cnt != 0)
1920 1923                  error = 0;
1921 1924  out:
1922 1925          if (error != 0) {
1923 1926                  *err = error;
1924 1927                  return (0);
1925 1928          } else {
1926 1929                  *err = 0;
1927 1930                  return (cnt);
1928 1931          }
1929 1932  }
1930 1933  
1931 1934  int
1932 1935  so_copyin(const void *from, void *to, size_t size, int fromkernel)
1933 1936  {
1934 1937          if (fromkernel) {
1935 1938                  bcopy(from, to, size);
1936 1939                  return (0);
1937 1940          }
1938 1941          return (xcopyin(from, to, size));
1939 1942  }
1940 1943  
1941 1944  int
1942 1945  so_copyout(const void *from, void *to, size_t size, int tokernel)
1943 1946  {
1944 1947          if (tokernel) {
1945 1948                  bcopy(from, to, size);
1946 1949                  return (0);
1947 1950          }
1948 1951          return (xcopyout(from, to, size));
1949 1952  }
  
    | 
      ↓ open down ↓ | 
    57 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX