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) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2015 by Delphix. All rights reserved.
  25  * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
  26  */
  27 
  28 #include <sys/param.h>
  29 #include <sys/errno.h>
  30 #include <sys/vfs.h>
  31 #include <sys/vnode.h>
  32 #include <sys/cred.h>
  33 #include <sys/cmn_err.h>
  34 #include <sys/systm.h>
  35 #include <sys/kmem.h>
  36 #include <sys/pathname.h>
  37 #include <sys/utsname.h>
  38 #include <sys/debug.h>
  39 #include <sys/door.h>
  40 #include <sys/sdt.h>
  41 #include <sys/thread.h>
  42 #include <sys/avl.h>
  43 
  44 #include <rpc/types.h>
  45 #include <rpc/auth.h>
  46 #include <rpc/clnt.h>
  47 
  48 #include <nfs/nfs.h>
  49 #include <nfs/export.h>
  50 #include <nfs/nfs_clnt.h>
  51 #include <nfs/auth.h>
  52 
  53 static struct kmem_cache *exi_cache_handle;
  54 static void exi_cache_reclaim(void *);
  55 static void exi_cache_trim(struct exportinfo *exi);
  56 static void *nfsauth_zone_init(zoneid_t);
  57 static void nfsauth_zone_shutdown(zoneid_t zoneid, void *data);
  58 static void nfsauth_zone_fini(zoneid_t, void *);
  59 
  60 extern pri_t minclsyspri;
  61 
  62 /* NFS auth cache statistics */
  63 volatile uint_t nfsauth_cache_hit;
  64 volatile uint_t nfsauth_cache_miss;
  65 volatile uint_t nfsauth_cache_refresh;
  66 volatile uint_t nfsauth_cache_reclaim;
  67 volatile uint_t exi_cache_auth_reclaim_failed;
  68 volatile uint_t exi_cache_clnt_reclaim_failed;
  69 
  70 /*
  71  * The lifetime of an auth cache entry:
  72  * ------------------------------------
  73  *
  74  * An auth cache entry is created with both the auth_time
  75  * and auth_freshness times set to the current time.
  76  *
  77  * Upon every client access which results in a hit, the
  78  * auth_time will be updated.
  79  *
  80  * If a client access determines that the auth_freshness
  81  * indicates that the entry is STALE, then it will be
  82  * refreshed. Note that this will explicitly reset
  83  * auth_time.
  84  *
  85  * When the REFRESH successfully occurs, then the
  86  * auth_freshness is updated.
  87  *
  88  * There are two ways for an entry to leave the cache:
  89  *
  90  * 1) Purged by an action on the export (remove or changed)
  91  * 2) Memory backpressure from the kernel (check against NFSAUTH_CACHE_TRIM)
  92  *
  93  * For 2) we check the timeout value against auth_time.
  94  */
  95 
  96 /*
  97  * Number of seconds until we mark for refresh an auth cache entry.
  98  */
  99 #define NFSAUTH_CACHE_REFRESH 600
 100 
 101 /*
 102  * Number of idle seconds until we yield to backpressure
 103  * to trim a cache entry.
 104  */
 105 #define NFSAUTH_CACHE_TRIM 3600
 106 
 107 /*
 108  * While we could encapuslate the exi_list inside the
 109  * exi structure, we can't do that for the auth_list.
 110  * So, to keep things looking clean, we keep them both
 111  * in these external lists.
 112  */
 113 typedef struct refreshq_exi_node {
 114         struct exportinfo       *ren_exi;
 115         list_t                  ren_authlist;
 116         list_node_t             ren_node;
 117 } refreshq_exi_node_t;
 118 
 119 typedef struct refreshq_auth_node {
 120         struct auth_cache       *ran_auth;
 121         char                    *ran_netid;
 122         list_node_t             ran_node;
 123 } refreshq_auth_node_t;
 124 
 125 /*
 126  * Used to manipulate things on the refreshq_queue.  Note that the refresh
 127  * thread will effectively pop a node off of the queue, at which point it
 128  * will no longer need to hold the mutex.
 129  */
 130 static kmutex_t refreshq_lock;
 131 static list_t refreshq_queue;
 132 static kcondvar_t refreshq_cv;
 133 
 134 /*
 135  * If there is ever a problem with loading the module, then nfsauth_fini()
 136  * needs to be called to remove state.  In that event, since the refreshq
 137  * thread has been started, they need to work together to get rid of state.
 138  */
 139 typedef enum nfsauth_refreshq_thread_state {
 140         REFRESHQ_THREAD_RUNNING,
 141         REFRESHQ_THREAD_FINI_REQ,
 142         REFRESHQ_THREAD_HALTED,
 143         REFRESHQ_THREAD_NEED_CREATE
 144 } nfsauth_refreshq_thread_state_t;
 145 
 146 typedef struct nfsauth_globals {
 147         kmutex_t        mountd_lock;
 148         door_handle_t   mountd_dh;
 149 
 150         /*
 151          * Used to manipulate things on the refreshq_queue.  Note that the
 152          * refresh thread will effectively pop a node off of the queue,
 153          * at which point it will no longer need to hold the mutex.
 154          */
 155         kmutex_t        refreshq_lock;
 156         list_t          refreshq_queue;
 157         kcondvar_t      refreshq_cv;
 158 
 159         /*
 160          * A list_t would be overkill.  These are auth_cache entries which are
 161          * no longer linked to an exi.  It should be the case that all of their
 162          * states are NFS_AUTH_INVALID, i.e., the only way to be put on this
 163          * list is iff their state indicated that they had been placed on the
 164          * refreshq_queue.
 165          *
 166          * Note that while there is no link from the exi or back to the exi,
 167          * the exi can not go away until these entries are harvested.
 168          */
 169         struct auth_cache               *refreshq_dead_entries;
 170         nfsauth_refreshq_thread_state_t refreshq_thread_state;
 171 
 172 } nfsauth_globals_t;
 173 
 174 static void nfsauth_free_node(struct auth_cache *);
 175 static void nfsauth_refresh_thread(nfsauth_globals_t *);
 176 
 177 static int nfsauth_cache_compar(const void *, const void *);
 178 
 179 static zone_key_t       nfsauth_zone_key;
 180 
 181 void
 182 mountd_args(uint_t did)
 183 {
 184         nfsauth_globals_t *nag;
 185 
 186         nag = zone_getspecific(nfsauth_zone_key, curzone);
 187         mutex_enter(&nag->mountd_lock);
 188         if (nag->mountd_dh != NULL)
 189                 door_ki_rele(nag->mountd_dh);
 190         nag->mountd_dh = door_ki_lookup(did);
 191         mutex_exit(&nag->mountd_lock);
 192 }
 193 
 194 void
 195 nfsauth_init(void)
 196 {
 197         zone_key_create(&nfsauth_zone_key, nfsauth_zone_init,
 198             nfsauth_zone_shutdown, nfsauth_zone_fini);
 199 
 200         exi_cache_handle = kmem_cache_create("exi_cache_handle",
 201             sizeof (struct auth_cache), 0, NULL, NULL,
 202             exi_cache_reclaim, NULL, NULL, 0);
 203 }
 204 
 205 void
 206 nfsauth_fini(void)
 207 {
 208         kmem_cache_destroy(exi_cache_handle);
 209 }
 210 
 211 /*ARGSUSED*/
 212 static void *
 213 nfsauth_zone_init(zoneid_t zoneid)
 214 {
 215         nfsauth_globals_t *nag;
 216 
 217         nag = kmem_zalloc(sizeof (*nag), KM_SLEEP);
 218 
 219         /*
 220          * mountd can be restarted by smf(5).  We need to make sure
 221          * the updated door handle will safely make it to mountd_dh.
 222          */
 223         mutex_init(&nag->mountd_lock, NULL, MUTEX_DEFAULT, NULL);
 224         mutex_init(&nag->refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
 225         list_create(&nag->refreshq_queue, sizeof (refreshq_exi_node_t),
 226             offsetof(refreshq_exi_node_t, ren_node));
 227         cv_init(&nag->refreshq_cv, NULL, CV_DEFAULT, NULL);
 228         nag->refreshq_thread_state = REFRESHQ_THREAD_NEED_CREATE;
 229 
 230         return (nag);
 231 }
 232 
 233 /*ARGSUSED*/
 234 static void
 235 nfsauth_zone_shutdown(zoneid_t zoneid, void *data)
 236 {
 237         refreshq_exi_node_t     *ren;
 238         nfsauth_globals_t       *nag = data;
 239 
 240         /* Prevent the nfsauth_refresh_thread from getting new work */
 241         mutex_enter(&nag->refreshq_lock);
 242         if (nag->refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
 243                 nag->refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
 244                 cv_broadcast(&nag->refreshq_cv);
 245 
 246                 /* Wait for nfsauth_refresh_thread() to exit */
 247                 while (nag->refreshq_thread_state != REFRESHQ_THREAD_HALTED)
 248                         cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
 249         }
 250         mutex_exit(&nag->refreshq_lock);
 251 
 252         /*
 253          * Walk the exi_list and in turn, walk the auth_lists and free all
 254          * lists.  In addition, free INVALID auth_cache entries.
 255          */
 256         while ((ren = list_remove_head(&nag->refreshq_queue))) {
 257                 refreshq_auth_node_t *ran;
 258 
 259                 while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
 260                         struct auth_cache *p = ran->ran_auth;
 261                         if (p->auth_state == NFS_AUTH_INVALID)
 262                                 nfsauth_free_node(p);
 263                         strfree(ran->ran_netid);
 264                         kmem_free(ran, sizeof (*ran));
 265                 }
 266 
 267                 list_destroy(&ren->ren_authlist);
 268                 exi_rele(ren->ren_exi);
 269                 kmem_free(ren, sizeof (*ren));
 270         }
 271 }
 272 
 273 /*ARGSUSED*/
 274 static void
 275 nfsauth_zone_fini(zoneid_t zoneid, void *data)
 276 {
 277         nfsauth_globals_t *nag = data;
 278 
 279         list_destroy(&nag->refreshq_queue);
 280         cv_destroy(&nag->refreshq_cv);
 281         mutex_destroy(&nag->refreshq_lock);
 282         mutex_destroy(&nag->mountd_lock);
 283         /* Extra cleanup. */
 284         if (nag->mountd_dh != NULL)
 285                 door_ki_rele(nag->mountd_dh);
 286         kmem_free(nag, sizeof (*nag));
 287 }
 288 
 289 /*
 290  * Convert the address in a netbuf to
 291  * a hash index for the auth_cache table.
 292  */
 293 static int
 294 hash(struct netbuf *a)
 295 {
 296         int i, h = 0;
 297 
 298         for (i = 0; i < a->len; i++)
 299                 h ^= a->buf[i];
 300 
 301         return (h & (AUTH_TABLESIZE - 1));
 302 }
 303 
 304 /*
 305  * Mask out the components of an
 306  * address that do not identify
 307  * a host. For socket addresses the
 308  * masking gets rid of the port number.
 309  */
 310 static void
 311 addrmask(struct netbuf *addr, struct netbuf *mask)
 312 {
 313         int i;
 314 
 315         for (i = 0; i < addr->len; i++)
 316                 addr->buf[i] &= mask->buf[i];
 317 }
 318 
 319 /*
 320  * nfsauth4_access is used for NFS V4 auth checking. Besides doing
 321  * the common nfsauth_access(), it will check if the client can
 322  * have a limited access to this vnode even if the security flavor
 323  * used does not meet the policy.
 324  */
 325 int
 326 nfsauth4_access(struct exportinfo *exi, vnode_t *vp, struct svc_req *req,
 327     cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
 328 {
 329         int access;
 330 
 331         access = nfsauth_access(exi, req, cr, uid, gid, ngids, gids);
 332 
 333         /*
 334          * There are cases that the server needs to allow the client
 335          * to have a limited view.
 336          *
 337          * e.g.
 338          * /export is shared as "sec=sys,rw=dfs-test-4,sec=krb5,rw"
 339          * /export/home is shared as "sec=sys,rw"
 340          *
 341          * When the client mounts /export with sec=sys, the client
 342          * would get a limited view with RO access on /export to see
 343          * "home" only because the client is allowed to access
 344          * /export/home with auth_sys.
 345          */
 346         if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
 347                 /*
 348                  * Allow ro permission with LIMITED view if there is a
 349                  * sub-dir exported under vp.
 350                  */
 351                 if (has_visible(exi, vp))
 352                         return (NFSAUTH_LIMITED);
 353         }
 354 
 355         return (access);
 356 }
 357 
 358 static void
 359 sys_log(const char *msg)
 360 {
 361         static time_t   tstamp = 0;
 362         time_t          now;
 363 
 364         /*
 365          * msg is shown (at most) once per minute
 366          */
 367         now = gethrestime_sec();
 368         if ((tstamp + 60) < now) {
 369                 tstamp = now;
 370                 cmn_err(CE_WARN, msg);
 371         }
 372 }
 373 
 374 /*
 375  * Callup to the mountd to get access information in the kernel.
 376  */
 377 static bool_t
 378 nfsauth_retrieve(nfsauth_globals_t *nag, struct exportinfo *exi,
 379     char *req_netid, int flavor, struct netbuf *addr, int *access,
 380     cred_t *clnt_cred, uid_t *srv_uid, gid_t *srv_gid, uint_t *srv_gids_cnt,
 381     gid_t **srv_gids)
 382 {
 383         varg_t                    varg = {0};
 384         nfsauth_res_t             res = {0};
 385         XDR                       xdrs;
 386         size_t                    absz;
 387         caddr_t                   abuf;
 388         int                       last = 0;
 389         door_arg_t                da;
 390         door_info_t               di;
 391         door_handle_t             dh;
 392         uint_t                    ntries = 0;
 393 
 394         /*
 395          * No entry in the cache for this client/flavor
 396          * so we need to call the nfsauth service in the
 397          * mount daemon.
 398          */
 399 
 400         varg.vers = V_PROTO;
 401         varg.arg_u.arg.cmd = NFSAUTH_ACCESS;
 402         varg.arg_u.arg.areq.req_client.n_len = addr->len;
 403         varg.arg_u.arg.areq.req_client.n_bytes = addr->buf;
 404         varg.arg_u.arg.areq.req_netid = req_netid;
 405         varg.arg_u.arg.areq.req_path = exi->exi_export.ex_path;
 406         varg.arg_u.arg.areq.req_flavor = flavor;
 407         varg.arg_u.arg.areq.req_clnt_uid = crgetuid(clnt_cred);
 408         varg.arg_u.arg.areq.req_clnt_gid = crgetgid(clnt_cred);
 409         varg.arg_u.arg.areq.req_clnt_gids.len = crgetngroups(clnt_cred);
 410         varg.arg_u.arg.areq.req_clnt_gids.val = (gid_t *)crgetgroups(clnt_cred);
 411 
 412         DTRACE_PROBE1(nfsserv__func__nfsauth__varg, varg_t *, &varg);
 413 
 414         /*
 415          * Setup the XDR stream for encoding the arguments. Notice that
 416          * in addition to the args having variable fields (req_netid and
 417          * req_path), the argument data structure is itself versioned,
 418          * so we need to make sure we can size the arguments buffer
 419          * appropriately to encode all the args. If we can't get sizing
 420          * info _or_ properly encode the arguments, there's really no
 421          * point in continuting, so we fail the request.
 422          */
 423         if ((absz = xdr_sizeof(xdr_varg, &varg)) == 0) {
 424                 *access = NFSAUTH_DENIED;
 425                 return (FALSE);
 426         }
 427 
 428         abuf = (caddr_t)kmem_alloc(absz, KM_SLEEP);
 429         xdrmem_create(&xdrs, abuf, absz, XDR_ENCODE);
 430         if (!xdr_varg(&xdrs, &varg)) {
 431                 XDR_DESTROY(&xdrs);
 432                 goto fail;
 433         }
 434         XDR_DESTROY(&xdrs);
 435 
 436         /*
 437          * Prepare the door arguments
 438          *
 439          * We don't know the size of the message the daemon
 440          * will pass back to us.  By setting rbuf to NULL,
 441          * we force the door code to allocate a buf of the
 442          * appropriate size.  We must set rsize > 0, however,
 443          * else the door code acts as if no response was
 444          * expected and doesn't pass the data to us.
 445          */
 446         da.data_ptr = (char *)abuf;
 447         da.data_size = absz;
 448         da.desc_ptr = NULL;
 449         da.desc_num = 0;
 450         da.rbuf = NULL;
 451         da.rsize = 1;
 452 
 453 retry:
 454         mutex_enter(&nag->mountd_lock);
 455         dh = nag->mountd_dh;
 456         if (dh != NULL)
 457                 door_ki_hold(dh);
 458         mutex_exit(&nag->mountd_lock);
 459 
 460         if (dh == NULL) {
 461                 /*
 462                  * The rendezvous point has not been established yet!
 463                  * This could mean that either mountd(1m) has not yet
 464                  * been started or that _this_ routine nuked the door
 465                  * handle after receiving an EINTR for a REVOKED door.
 466                  *
 467                  * Returning NFSAUTH_DROP will cause the NFS client
 468                  * to retransmit the request, so let's try to be more
 469                  * rescillient and attempt for ntries before we bail.
 470                  */
 471                 if (++ntries % NFSAUTH_DR_TRYCNT) {
 472                         delay(hz);
 473                         goto retry;
 474                 }
 475 
 476                 kmem_free(abuf, absz);
 477 
 478                 sys_log("nfsauth: mountd has not established door");
 479                 *access = NFSAUTH_DROP;
 480                 return (FALSE);
 481         }
 482 
 483         ntries = 0;
 484 
 485         /*
 486          * Now that we've got what we need, place the call.
 487          */
 488         switch (door_ki_upcall_limited(dh, &da, NULL, SIZE_MAX, 0)) {
 489         case 0:                         /* Success */
 490                 door_ki_rele(dh);
 491 
 492                 if (da.data_ptr == NULL && da.data_size == 0) {
 493                         /*
 494                          * The door_return that contained the data
 495                          * failed! We're here because of the 2nd
 496                          * door_return (w/o data) such that we can
 497                          * get control of the thread (and exit
 498                          * gracefully).
 499                          */
 500                         DTRACE_PROBE1(nfsserv__func__nfsauth__door__nil,
 501                             door_arg_t *, &da);
 502                         goto fail;
 503                 }
 504 
 505                 break;
 506 
 507         case EAGAIN:
 508                 /*
 509                  * Server out of resources; back off for a bit
 510                  */
 511                 door_ki_rele(dh);
 512                 delay(hz);
 513                 goto retry;
 514                 /* NOTREACHED */
 515 
 516         case EINTR:
 517                 if (!door_ki_info(dh, &di)) {
 518                         door_ki_rele(dh);
 519 
 520                         if (di.di_attributes & DOOR_REVOKED) {
 521                                 /*
 522                                  * The server barfed and revoked
 523                                  * the (existing) door on us; we
 524                                  * want to wait to give smf(5) a
 525                                  * chance to restart mountd(1m)
 526                                  * and establish a new door handle.
 527                                  */
 528                                 mutex_enter(&nag->mountd_lock);
 529                                 if (dh == nag->mountd_dh) {
 530                                         door_ki_rele(nag->mountd_dh);
 531                                         nag->mountd_dh = NULL;
 532                                 }
 533                                 mutex_exit(&nag->mountd_lock);
 534                                 delay(hz);
 535                                 goto retry;
 536                         }
 537                         /*
 538                          * If the door was _not_ revoked on us,
 539                          * then more than likely we took an INTR,
 540                          * so we need to fail the operation.
 541                          */
 542                         goto fail;
 543                 }
 544                 /*
 545                  * The only failure that can occur from getting
 546                  * the door info is EINVAL, so we let the code
 547                  * below handle it.
 548                  */
 549                 /* FALLTHROUGH */
 550 
 551         case EBADF:
 552         case EINVAL:
 553         default:
 554                 /*
 555                  * If we have a stale door handle, give smf a last
 556                  * chance to start it by sleeping for a little bit.
 557                  * If we're still hosed, we'll fail the call.
 558                  *
 559                  * Since we're going to reacquire the door handle
 560                  * upon the retry, we opt to sleep for a bit and
 561                  * _not_ to clear mountd_dh. If mountd restarted
 562                  * and was able to set mountd_dh, we should see
 563                  * the new instance; if not, we won't get caught
 564                  * up in the retry/DELAY loop.
 565                  */
 566                 door_ki_rele(dh);
 567                 if (!last) {
 568                         delay(hz);
 569                         last++;
 570                         goto retry;
 571                 }
 572                 sys_log("nfsauth: stale mountd door handle");
 573                 goto fail;
 574         }
 575 
 576         ASSERT(da.rbuf != NULL);
 577 
 578         /*
 579          * No door errors encountered; setup the XDR stream for decoding
 580          * the results. If we fail to decode the results, we've got no
 581          * other recourse than to fail the request.
 582          */
 583         xdrmem_create(&xdrs, da.rbuf, da.rsize, XDR_DECODE);
 584         if (!xdr_nfsauth_res(&xdrs, &res)) {
 585                 xdr_free(xdr_nfsauth_res, (char *)&res);
 586                 XDR_DESTROY(&xdrs);
 587                 kmem_free(da.rbuf, da.rsize);
 588                 goto fail;
 589         }
 590         XDR_DESTROY(&xdrs);
 591         kmem_free(da.rbuf, da.rsize);
 592 
 593         DTRACE_PROBE1(nfsserv__func__nfsauth__results, nfsauth_res_t *, &res);
 594         switch (res.stat) {
 595                 case NFSAUTH_DR_OKAY:
 596                         *access = res.ares.auth_perm;
 597                         *srv_uid = res.ares.auth_srv_uid;
 598                         *srv_gid = res.ares.auth_srv_gid;
 599                         *srv_gids_cnt = res.ares.auth_srv_gids.len;
 600                         *srv_gids = kmem_alloc(*srv_gids_cnt * sizeof (gid_t),
 601                             KM_SLEEP);
 602                         bcopy(res.ares.auth_srv_gids.val, *srv_gids,
 603                             *srv_gids_cnt * sizeof (gid_t));
 604                         break;
 605 
 606                 case NFSAUTH_DR_EFAIL:
 607                 case NFSAUTH_DR_DECERR:
 608                 case NFSAUTH_DR_BADCMD:
 609                 default:
 610                         xdr_free(xdr_nfsauth_res, (char *)&res);
 611 fail:
 612                         *access = NFSAUTH_DENIED;
 613                         kmem_free(abuf, absz);
 614                         return (FALSE);
 615                         /* NOTREACHED */
 616         }
 617 
 618         xdr_free(xdr_nfsauth_res, (char *)&res);
 619         kmem_free(abuf, absz);
 620 
 621         return (TRUE);
 622 }
 623 
 624 static void
 625 nfsauth_refresh_thread(nfsauth_globals_t *nag)
 626 {
 627         refreshq_exi_node_t     *ren;
 628         refreshq_auth_node_t    *ran;
 629 
 630         struct exportinfo       *exi;
 631 
 632         int                     access;
 633         bool_t                  retrieval;
 634 
 635         callb_cpr_t             cprinfo;
 636 
 637         CALLB_CPR_INIT(&cprinfo, &nag->refreshq_lock, callb_generic_cpr,
 638             "nfsauth_refresh");
 639 
 640         for (;;) {
 641                 mutex_enter(&nag->refreshq_lock);
 642                 if (nag->refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
 643                         /* Keep the hold on the lock! */
 644                         break;
 645                 }
 646 
 647                 ren = list_remove_head(&nag->refreshq_queue);
 648                 if (ren == NULL) {
 649                         CALLB_CPR_SAFE_BEGIN(&cprinfo);
 650                         cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
 651                         CALLB_CPR_SAFE_END(&cprinfo, &nag->refreshq_lock);
 652                         mutex_exit(&nag->refreshq_lock);
 653                         continue;
 654                 }
 655                 mutex_exit(&nag->refreshq_lock);
 656 
 657                 exi = ren->ren_exi;
 658                 ASSERT(exi != NULL);
 659 
 660                 /*
 661                  * Since the ren was removed from the refreshq_queue above,
 662                  * this is the only thread aware about the ren existence, so we
 663                  * have the exclusive ownership of it and we do not need to
 664                  * protect it by any lock.
 665                  */
 666                 while ((ran = list_remove_head(&ren->ren_authlist))) {
 667                         uid_t uid;
 668                         gid_t gid;
 669                         uint_t ngids;
 670                         gid_t *gids;
 671                         struct auth_cache *p = ran->ran_auth;
 672                         char *netid = ran->ran_netid;
 673 
 674                         ASSERT(p != NULL);
 675                         ASSERT(netid != NULL);
 676 
 677                         kmem_free(ran, sizeof (refreshq_auth_node_t));
 678 
 679                         mutex_enter(&p->auth_lock);
 680 
 681                         /*
 682                          * Once the entry goes INVALID, it can not change
 683                          * state.
 684                          *
 685                          * No need to refresh entries also in a case we are
 686                          * just shutting down.
 687                          *
 688                          * In general, there is no need to hold the
 689                          * refreshq_lock to test the refreshq_thread_state.  We
 690                          * do hold it at other places because there is some
 691                          * related thread synchronization (or some other tasks)
 692                          * close to the refreshq_thread_state check.
 693                          *
 694                          * The check for the refreshq_thread_state value here
 695                          * is purely advisory to allow the faster
 696                          * nfsauth_refresh_thread() shutdown.  In a case we
 697                          * will miss such advisory, nothing catastrophic
 698                          * happens: we will just spin longer here before the
 699                          * shutdown.
 700                          */
 701                         if (p->auth_state == NFS_AUTH_INVALID ||
 702                             nag->refreshq_thread_state !=
 703                             REFRESHQ_THREAD_RUNNING) {
 704                                 mutex_exit(&p->auth_lock);
 705 
 706                                 if (p->auth_state == NFS_AUTH_INVALID)
 707                                         nfsauth_free_node(p);
 708 
 709                                 strfree(netid);
 710 
 711                                 continue;
 712                         }
 713 
 714                         /*
 715                          * Make sure the state is valid.  Note that once we
 716                          * change the state to NFS_AUTH_REFRESHING, no other
 717                          * thread will be able to work on this entry.
 718                          */
 719                         ASSERT(p->auth_state == NFS_AUTH_STALE);
 720 
 721                         p->auth_state = NFS_AUTH_REFRESHING;
 722                         mutex_exit(&p->auth_lock);
 723 
 724                         DTRACE_PROBE2(nfsauth__debug__cache__refresh,
 725                             struct exportinfo *, exi,
 726                             struct auth_cache *, p);
 727 
 728                         /*
 729                          * The first caching of the access rights
 730                          * is done with the netid pulled out of the
 731                          * request from the client. All subsequent
 732                          * users of the cache may or may not have
 733                          * the same netid. It doesn't matter. So
 734                          * when we refresh, we simply use the netid
 735                          * of the request which triggered the
 736                          * refresh attempt.
 737                          */
 738                         retrieval = nfsauth_retrieve(nag, exi, netid,
 739                             p->auth_flavor, &p->auth_clnt->authc_addr, &access,
 740                             p->auth_clnt_cred, &uid, &gid, &ngids, &gids);
 741 
 742                         /*
 743                          * This can only be set in one other place
 744                          * and the state has to be NFS_AUTH_FRESH.
 745                          */
 746                         strfree(netid);
 747 
 748                         mutex_enter(&p->auth_lock);
 749                         if (p->auth_state == NFS_AUTH_INVALID) {
 750                                 mutex_exit(&p->auth_lock);
 751                                 nfsauth_free_node(p);
 752                                 if (retrieval == TRUE)
 753                                         kmem_free(gids, ngids * sizeof (gid_t));
 754                         } else {
 755                                 /*
 756                                  * If we got an error, do not reset the
 757                                  * time. This will cause the next access
 758                                  * check for the client to reschedule this
 759                                  * node.
 760                                  */
 761                                 if (retrieval == TRUE) {
 762                                         p->auth_access = access;
 763 
 764                                         p->auth_srv_uid = uid;
 765                                         p->auth_srv_gid = gid;
 766                                         kmem_free(p->auth_srv_gids,
 767                                             p->auth_srv_ngids * sizeof (gid_t));
 768                                         p->auth_srv_ngids = ngids;
 769                                         p->auth_srv_gids = gids;
 770 
 771                                         p->auth_freshness = gethrestime_sec();
 772                                 }
 773                                 p->auth_state = NFS_AUTH_FRESH;
 774 
 775                                 cv_broadcast(&p->auth_cv);
 776                                 mutex_exit(&p->auth_lock);
 777                         }
 778                 }
 779 
 780                 list_destroy(&ren->ren_authlist);
 781                 exi_rele(ren->ren_exi);
 782                 kmem_free(ren, sizeof (refreshq_exi_node_t));
 783         }
 784 
 785         nag->refreshq_thread_state = REFRESHQ_THREAD_HALTED;
 786         cv_broadcast(&nag->refreshq_cv);
 787         CALLB_CPR_EXIT(&cprinfo);
 788         DTRACE_PROBE(nfsauth__nfsauth__refresh__thread__exit);
 789         zthread_exit();
 790 }
 791 
 792 int
 793 nfsauth_cache_clnt_compar(const void *v1, const void *v2)
 794 {
 795         int c;
 796 
 797         const struct auth_cache_clnt *a1 = (const struct auth_cache_clnt *)v1;
 798         const struct auth_cache_clnt *a2 = (const struct auth_cache_clnt *)v2;
 799 
 800         if (a1->authc_addr.len < a2->authc_addr.len)
 801                 return (-1);
 802         if (a1->authc_addr.len > a2->authc_addr.len)
 803                 return (1);
 804 
 805         c = memcmp(a1->authc_addr.buf, a2->authc_addr.buf, a1->authc_addr.len);
 806         if (c < 0)
 807                 return (-1);
 808         if (c > 0)
 809                 return (1);
 810 
 811         return (0);
 812 }
 813 
 814 static int
 815 nfsauth_cache_compar(const void *v1, const void *v2)
 816 {
 817         int c;
 818 
 819         const struct auth_cache *a1 = (const struct auth_cache *)v1;
 820         const struct auth_cache *a2 = (const struct auth_cache *)v2;
 821 
 822         if (a1->auth_flavor < a2->auth_flavor)
 823                 return (-1);
 824         if (a1->auth_flavor > a2->auth_flavor)
 825                 return (1);
 826 
 827         if (crgetuid(a1->auth_clnt_cred) < crgetuid(a2->auth_clnt_cred))
 828                 return (-1);
 829         if (crgetuid(a1->auth_clnt_cred) > crgetuid(a2->auth_clnt_cred))
 830                 return (1);
 831 
 832         if (crgetgid(a1->auth_clnt_cred) < crgetgid(a2->auth_clnt_cred))
 833                 return (-1);
 834         if (crgetgid(a1->auth_clnt_cred) > crgetgid(a2->auth_clnt_cred))
 835                 return (1);
 836 
 837         if (crgetngroups(a1->auth_clnt_cred) < crgetngroups(a2->auth_clnt_cred))
 838                 return (-1);
 839         if (crgetngroups(a1->auth_clnt_cred) > crgetngroups(a2->auth_clnt_cred))
 840                 return (1);
 841 
 842         c = memcmp(crgetgroups(a1->auth_clnt_cred),
 843             crgetgroups(a2->auth_clnt_cred), crgetngroups(a1->auth_clnt_cred));
 844         if (c < 0)
 845                 return (-1);
 846         if (c > 0)
 847                 return (1);
 848 
 849         return (0);
 850 }
 851 
 852 /*
 853  * Get the access information from the cache or callup to the mountd
 854  * to get and cache the access information in the kernel.
 855  */
 856 static int
 857 nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor,
 858     cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
 859 {
 860         nfsauth_globals_t       *nag;
 861         struct netbuf           *taddrmask;
 862         struct netbuf           addr;   /* temporary copy of client's address */
 863         const struct netbuf     *claddr;
 864         avl_tree_t              *tree;
 865         struct auth_cache       ac;     /* used as a template for avl_find() */
 866         struct auth_cache_clnt  *c;
 867         struct auth_cache_clnt  acc;    /* used as a template for avl_find() */
 868         struct auth_cache       *p = NULL;
 869         int                     access;
 870 
 871         uid_t                   tmpuid;
 872         gid_t                   tmpgid;
 873         uint_t                  tmpngids;
 874         gid_t                   *tmpgids;
 875 
 876         avl_index_t             where;  /* used for avl_find()/avl_insert() */
 877 
 878         ASSERT(cr != NULL);
 879 
 880         ASSERT3P(curzone, ==, exi->exi_zone);
 881         nag = zone_getspecific(nfsauth_zone_key, curzone);
 882 
 883         /*
 884          * Now check whether this client already
 885          * has an entry for this flavor in the cache
 886          * for this export.
 887          * Get the caller's address, mask off the
 888          * parts of the address that do not identify
 889          * the host (port number, etc), and then hash
 890          * it to find the chain of cache entries.
 891          */
 892 
 893         claddr = svc_getrpccaller(req->rq_xprt);
 894         addr = *claddr;
 895         addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
 896         bcopy(claddr->buf, addr.buf, claddr->len);
 897 
 898         SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
 899         ASSERT(taddrmask != NULL);
 900         addrmask(&addr, taddrmask);
 901 
 902         ac.auth_flavor = flavor;
 903         ac.auth_clnt_cred = crdup(cr);
 904 
 905         acc.authc_addr = addr;
 906 
 907         tree = exi->exi_cache[hash(&addr)];
 908 
 909         rw_enter(&exi->exi_cache_lock, RW_READER);
 910         c = (struct auth_cache_clnt *)avl_find(tree, &acc, NULL);
 911 
 912         if (c == NULL) {
 913                 struct auth_cache_clnt *nc;
 914 
 915                 rw_exit(&exi->exi_cache_lock);
 916 
 917                 nc = kmem_alloc(sizeof (*nc), KM_NOSLEEP | KM_NORMALPRI);
 918                 if (nc == NULL)
 919                         goto retrieve;
 920 
 921                 /*
 922                  * Initialize the new auth_cache_clnt
 923                  */
 924                 nc->authc_addr = addr;
 925                 nc->authc_addr.buf = kmem_alloc(addr.maxlen,
 926                     KM_NOSLEEP | KM_NORMALPRI);
 927                 if (addr.maxlen != 0 && nc->authc_addr.buf == NULL) {
 928                         kmem_free(nc, sizeof (*nc));
 929                         goto retrieve;
 930                 }
 931                 bcopy(addr.buf, nc->authc_addr.buf, addr.len);
 932                 rw_init(&nc->authc_lock, NULL, RW_DEFAULT, NULL);
 933                 avl_create(&nc->authc_tree, nfsauth_cache_compar,
 934                     sizeof (struct auth_cache),
 935                     offsetof(struct auth_cache, auth_link));
 936 
 937                 rw_enter(&exi->exi_cache_lock, RW_WRITER);
 938                 c = (struct auth_cache_clnt *)avl_find(tree, &acc, &where);
 939                 if (c == NULL) {
 940                         avl_insert(tree, nc, where);
 941                         rw_downgrade(&exi->exi_cache_lock);
 942                         c = nc;
 943                 } else {
 944                         rw_downgrade(&exi->exi_cache_lock);
 945 
 946                         avl_destroy(&nc->authc_tree);
 947                         rw_destroy(&nc->authc_lock);
 948                         kmem_free(nc->authc_addr.buf, nc->authc_addr.maxlen);
 949                         kmem_free(nc, sizeof (*nc));
 950                 }
 951         }
 952 
 953         ASSERT(c != NULL);
 954 
 955         rw_enter(&c->authc_lock, RW_READER);
 956         p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, NULL);
 957 
 958         if (p == NULL) {
 959                 struct auth_cache *np;
 960 
 961                 rw_exit(&c->authc_lock);
 962 
 963                 np = kmem_cache_alloc(exi_cache_handle,
 964                     KM_NOSLEEP | KM_NORMALPRI);
 965                 if (np == NULL) {
 966                         rw_exit(&exi->exi_cache_lock);
 967                         goto retrieve;
 968                 }
 969 
 970                 /*
 971                  * Initialize the new auth_cache
 972                  */
 973                 np->auth_clnt = c;
 974                 np->auth_flavor = flavor;
 975                 np->auth_clnt_cred = ac.auth_clnt_cred;
 976                 np->auth_srv_ngids = 0;
 977                 np->auth_srv_gids = NULL;
 978                 np->auth_time = np->auth_freshness = gethrestime_sec();
 979                 np->auth_state = NFS_AUTH_NEW;
 980                 mutex_init(&np->auth_lock, NULL, MUTEX_DEFAULT, NULL);
 981                 cv_init(&np->auth_cv, NULL, CV_DEFAULT, NULL);
 982 
 983                 rw_enter(&c->authc_lock, RW_WRITER);
 984                 rw_exit(&exi->exi_cache_lock);
 985 
 986                 p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, &where);
 987                 if (p == NULL) {
 988                         avl_insert(&c->authc_tree, np, where);
 989                         rw_downgrade(&c->authc_lock);
 990                         p = np;
 991                 } else {
 992                         rw_downgrade(&c->authc_lock);
 993 
 994                         cv_destroy(&np->auth_cv);
 995                         mutex_destroy(&np->auth_lock);
 996                         crfree(ac.auth_clnt_cred);
 997                         kmem_cache_free(exi_cache_handle, np);
 998                 }
 999         } else {
1000                 rw_exit(&exi->exi_cache_lock);
1001                 crfree(ac.auth_clnt_cred);
1002         }
1003 
1004         mutex_enter(&p->auth_lock);
1005         rw_exit(&c->authc_lock);
1006 
1007         /*
1008          * If the entry is in the WAITING state then some other thread is just
1009          * retrieving the required info.  The entry was either NEW, or the list
1010          * of client's supplemental groups is going to be changed (either by
1011          * this thread, or by some other thread).  We need to wait until the
1012          * nfsauth_retrieve() is done.
1013          */
1014         while (p->auth_state == NFS_AUTH_WAITING)
1015                 cv_wait(&p->auth_cv, &p->auth_lock);
1016 
1017         /*
1018          * Here the entry cannot be in WAITING or INVALID state.
1019          */
1020         ASSERT(p->auth_state != NFS_AUTH_WAITING);
1021         ASSERT(p->auth_state != NFS_AUTH_INVALID);
1022 
1023         /*
1024          * If the cache entry is not valid yet, we need to retrieve the
1025          * info ourselves.
1026          */
1027         if (p->auth_state == NFS_AUTH_NEW) {
1028                 bool_t res;
1029                 /*
1030                  * NFS_AUTH_NEW is the default output auth_state value in a
1031                  * case we failed somewhere below.
1032                  */
1033                 auth_state_t state = NFS_AUTH_NEW;
1034 
1035                 p->auth_state = NFS_AUTH_WAITING;
1036                 mutex_exit(&p->auth_lock);
1037                 kmem_free(addr.buf, addr.maxlen);
1038                 addr = p->auth_clnt->authc_addr;
1039 
1040                 atomic_inc_uint(&nfsauth_cache_miss);
1041 
1042                 res = nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt),
1043                     flavor, &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids,
1044                     &tmpgids);
1045 
1046                 p->auth_access = access;
1047                 p->auth_time = p->auth_freshness = gethrestime_sec();
1048 
1049                 if (res == TRUE) {
1050                         if (uid != NULL)
1051                                 *uid = tmpuid;
1052                         if (gid != NULL)
1053                                 *gid = tmpgid;
1054                         if (ngids != NULL && gids != NULL) {
1055                                 *ngids = tmpngids;
1056                                 *gids = tmpgids;
1057 
1058                                 /*
1059                                  * We need a copy of gids for the
1060                                  * auth_cache entry
1061                                  */
1062                                 tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
1063                                     KM_NOSLEEP | KM_NORMALPRI);
1064                                 if (tmpgids != NULL)
1065                                         bcopy(*gids, tmpgids,
1066                                             tmpngids * sizeof (gid_t));
1067                         }
1068 
1069                         if (tmpgids != NULL || tmpngids == 0) {
1070                                 p->auth_srv_uid = tmpuid;
1071                                 p->auth_srv_gid = tmpgid;
1072                                 p->auth_srv_ngids = tmpngids;
1073                                 p->auth_srv_gids = tmpgids;
1074 
1075                                 state = NFS_AUTH_FRESH;
1076                         }
1077                 }
1078 
1079                 /*
1080                  * Set the auth_state and notify waiters.
1081                  */
1082                 mutex_enter(&p->auth_lock);
1083                 p->auth_state = state;
1084                 cv_broadcast(&p->auth_cv);
1085                 mutex_exit(&p->auth_lock);
1086         } else {
1087                 uint_t nach;
1088                 time_t refresh;
1089 
1090                 refresh = gethrestime_sec() - p->auth_freshness;
1091 
1092                 p->auth_time = gethrestime_sec();
1093 
1094                 if (uid != NULL)
1095                         *uid = p->auth_srv_uid;
1096                 if (gid != NULL)
1097                         *gid = p->auth_srv_gid;
1098                 if (ngids != NULL && gids != NULL) {
1099                         *ngids = p->auth_srv_ngids;
1100                         *gids = kmem_alloc(*ngids * sizeof (gid_t), KM_SLEEP);
1101                         bcopy(p->auth_srv_gids, *gids, *ngids * sizeof (gid_t));
1102                 }
1103 
1104                 access = p->auth_access;
1105 
1106                 if ((refresh > NFSAUTH_CACHE_REFRESH) &&
1107                     p->auth_state == NFS_AUTH_FRESH) {
1108                         refreshq_auth_node_t *ran;
1109                         uint_t nacr;
1110 
1111                         p->auth_state = NFS_AUTH_STALE;
1112                         mutex_exit(&p->auth_lock);
1113 
1114                         nacr = atomic_inc_uint_nv(&nfsauth_cache_refresh);
1115                         DTRACE_PROBE3(nfsauth__debug__cache__stale,
1116                             struct exportinfo *, exi,
1117                             struct auth_cache *, p,
1118                             uint_t, nacr);
1119 
1120                         ran = kmem_alloc(sizeof (refreshq_auth_node_t),
1121                             KM_SLEEP);
1122                         ran->ran_auth = p;
1123                         ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
1124 
1125                         mutex_enter(&nag->refreshq_lock);
1126 
1127                         if (nag->refreshq_thread_state ==
1128                             REFRESHQ_THREAD_NEED_CREATE) {
1129                                 /* Launch nfsauth refresh thread */
1130                                 nag->refreshq_thread_state =
1131                                     REFRESHQ_THREAD_RUNNING;
1132                                 (void) zthread_create(NULL, 0,
1133                                     nfsauth_refresh_thread, nag, 0,
1134                                     minclsyspri);
1135                         }
1136 
1137                         /*
1138                          * We should not add a work queue item if the thread
1139                          * is not accepting them.
1140                          */
1141                         if (nag->refreshq_thread_state ==
1142                             REFRESHQ_THREAD_RUNNING) {
1143                                 refreshq_exi_node_t *ren;
1144 
1145                                 /*
1146                                  * Is there an existing exi_list?
1147                                  */
1148                                 for (ren = list_head(&nag->refreshq_queue);
1149                                     ren != NULL;
1150                                     ren = list_next(&nag->refreshq_queue,
1151                                     ren)) {
1152                                         if (ren->ren_exi == exi) {
1153                                                 list_insert_tail(
1154                                                     &ren->ren_authlist, ran);
1155                                                 break;
1156                                         }
1157                                 }
1158 
1159                                 if (ren == NULL) {
1160                                         ren = kmem_alloc(
1161                                             sizeof (refreshq_exi_node_t),
1162                                             KM_SLEEP);
1163 
1164                                         exi_hold(exi);
1165                                         ren->ren_exi = exi;
1166 
1167                                         list_create(&ren->ren_authlist,
1168                                             sizeof (refreshq_auth_node_t),
1169                                             offsetof(refreshq_auth_node_t,
1170                                             ran_node));
1171 
1172                                         list_insert_tail(&ren->ren_authlist,
1173                                             ran);
1174                                         list_insert_tail(&nag->refreshq_queue,
1175                                             ren);
1176                                 }
1177 
1178                                 cv_broadcast(&nag->refreshq_cv);
1179                         } else {
1180                                 strfree(ran->ran_netid);
1181                                 kmem_free(ran, sizeof (refreshq_auth_node_t));
1182                         }
1183 
1184                         mutex_exit(&nag->refreshq_lock);
1185                 } else {
1186                         mutex_exit(&p->auth_lock);
1187                 }
1188 
1189                 nach = atomic_inc_uint_nv(&nfsauth_cache_hit);
1190                 DTRACE_PROBE2(nfsauth__debug__cache__hit,
1191                     uint_t, nach,
1192                     time_t, refresh);
1193 
1194                 kmem_free(addr.buf, addr.maxlen);
1195         }
1196 
1197         return (access);
1198 
1199 retrieve:
1200         crfree(ac.auth_clnt_cred);
1201 
1202         /*
1203          * Retrieve the required data without caching.
1204          */
1205 
1206         ASSERT(p == NULL);
1207 
1208         atomic_inc_uint(&nfsauth_cache_miss);
1209 
1210         if (nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt), flavor,
1211             &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
1212                 if (uid != NULL)
1213                         *uid = tmpuid;
1214                 if (gid != NULL)
1215                         *gid = tmpgid;
1216                 if (ngids != NULL && gids != NULL) {
1217                         *ngids = tmpngids;
1218                         *gids = tmpgids;
1219                 } else {
1220                         kmem_free(tmpgids, tmpngids * sizeof (gid_t));
1221                 }
1222         }
1223 
1224         kmem_free(addr.buf, addr.maxlen);
1225 
1226         return (access);
1227 }
1228 
1229 /*
1230  * Check if the requesting client has access to the filesystem with
1231  * a given nfs flavor number which is an explicitly shared flavor.
1232  */
1233 int
1234 nfsauth4_secinfo_access(struct exportinfo *exi, struct svc_req *req,
1235     int flavor, int perm, cred_t *cr)
1236 {
1237         int access;
1238 
1239         if (! (perm & M_4SEC_EXPORTED)) {
1240                 return (NFSAUTH_DENIED);
1241         }
1242 
1243         /*
1244          * Optimize if there are no lists
1245          */
1246         if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0) {
1247                 perm &= ~M_4SEC_EXPORTED;
1248                 if (perm == M_RO)
1249                         return (NFSAUTH_RO);
1250                 if (perm == M_RW)
1251                         return (NFSAUTH_RW);
1252         }
1253 
1254         access = nfsauth_cache_get(exi, req, flavor, cr, NULL, NULL, NULL,
1255             NULL);
1256 
1257         return (access);
1258 }
1259 
1260 int
1261 nfsauth_access(struct exportinfo *exi, struct svc_req *req, cred_t *cr,
1262     uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
1263 {
1264         int access, mapaccess;
1265         struct secinfo *sp;
1266         int i, flavor, perm;
1267         int authnone_entry = -1;
1268 
1269         /*
1270          * By default root is mapped to anonymous user.
1271          * This might get overriden later in nfsauth_cache_get().
1272          */
1273         if (crgetuid(cr) == 0) {
1274                 if (uid != NULL)
1275                         *uid = exi->exi_export.ex_anon;
1276                 if (gid != NULL)
1277                         *gid = exi->exi_export.ex_anon;
1278         } else {
1279                 if (uid != NULL)
1280                         *uid = crgetuid(cr);
1281                 if (gid != NULL)
1282                         *gid = crgetgid(cr);
1283         }
1284 
1285         if (ngids != NULL)
1286                 *ngids = 0;
1287         if (gids != NULL)
1288                 *gids = NULL;
1289 
1290         /*
1291          *  Get the nfs flavor number from xprt.
1292          */
1293         flavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
1294 
1295         /*
1296          * First check the access restrictions on the filesystem.  If
1297          * there are no lists associated with this flavor then there's no
1298          * need to make an expensive call to the nfsauth service or to
1299          * cache anything.
1300          */
1301 
1302         sp = exi->exi_export.ex_secinfo;
1303         for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
1304                 if (flavor != sp[i].s_secinfo.sc_nfsnum) {
1305                         if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
1306                                 authnone_entry = i;
1307                         continue;
1308                 }
1309                 break;
1310         }
1311 
1312         mapaccess = 0;
1313 
1314         if (i >= exi->exi_export.ex_seccnt) {
1315                 /*
1316                  * Flavor not found, but use AUTH_NONE if it exists
1317                  */
1318                 if (authnone_entry == -1)
1319                         return (NFSAUTH_DENIED);
1320                 flavor = AUTH_NONE;
1321                 mapaccess = NFSAUTH_MAPNONE;
1322                 i = authnone_entry;
1323         }
1324 
1325         /*
1326          * If the flavor is in the ex_secinfo list, but not an explicitly
1327          * shared flavor by the user, it is a result of the nfsv4 server
1328          * namespace setup. We will grant an RO permission similar for
1329          * a pseudo node except that this node is a shared one.
1330          *
1331          * e.g. flavor in (flavor) indicates that it is not explictly
1332          *      shared by the user:
1333          *
1334          *              /       (sys, krb5)
1335          *              |
1336          *              export  #share -o sec=sys (krb5)
1337          *              |
1338          *              secure  #share -o sec=krb5
1339          *
1340          *      In this case, when a krb5 request coming in to access
1341          *      /export, RO permission is granted.
1342          */
1343         if (!(sp[i].s_flags & M_4SEC_EXPORTED))
1344                 return (mapaccess | NFSAUTH_RO);
1345 
1346         /*
1347          * Optimize if there are no lists.
1348          * We cannot optimize for AUTH_SYS with NGRPS (16) supplemental groups.
1349          */
1350         perm = sp[i].s_flags;
1351         if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0 && (ngroups_max <= NGRPS ||
1352             flavor != AUTH_SYS || crgetngroups(cr) < NGRPS)) {
1353                 perm &= ~M_4SEC_EXPORTED;
1354                 if (perm == M_RO)
1355                         return (mapaccess | NFSAUTH_RO);
1356                 if (perm == M_RW)
1357                         return (mapaccess | NFSAUTH_RW);
1358         }
1359 
1360         access = nfsauth_cache_get(exi, req, flavor, cr, uid, gid, ngids, gids);
1361 
1362         /*
1363          * For both NFSAUTH_DENIED and NFSAUTH_WRONGSEC we do not care about
1364          * the supplemental groups.
1365          */
1366         if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
1367                 if (ngids != NULL && gids != NULL) {
1368                         kmem_free(*gids, *ngids * sizeof (gid_t));
1369                         *ngids = 0;
1370                         *gids = NULL;
1371                 }
1372         }
1373 
1374         /*
1375          * Client's security flavor doesn't match with "ro" or
1376          * "rw" list. Try again using AUTH_NONE if present.
1377          */
1378         if ((access & NFSAUTH_WRONGSEC) && (flavor != AUTH_NONE)) {
1379                 /*
1380                  * Have we already encountered AUTH_NONE ?
1381                  */
1382                 if (authnone_entry != -1) {
1383                         mapaccess = NFSAUTH_MAPNONE;
1384                         access = nfsauth_cache_get(exi, req, AUTH_NONE, cr,
1385                             NULL, NULL, NULL, NULL);
1386                 } else {
1387                         /*
1388                          * Check for AUTH_NONE presence.
1389                          */
1390                         for (; i < exi->exi_export.ex_seccnt; i++) {
1391                                 if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
1392                                         mapaccess = NFSAUTH_MAPNONE;
1393                                         access = nfsauth_cache_get(exi, req,
1394                                             AUTH_NONE, cr, NULL, NULL, NULL,
1395                                             NULL);
1396                                         break;
1397                                 }
1398                         }
1399                 }
1400         }
1401 
1402         if (access & NFSAUTH_DENIED)
1403                 access = NFSAUTH_DENIED;
1404 
1405         return (access | mapaccess);
1406 }
1407 
1408 static void
1409 nfsauth_free_clnt_node(struct auth_cache_clnt *p)
1410 {
1411         void *cookie = NULL;
1412         struct auth_cache *node;
1413 
1414         while ((node = avl_destroy_nodes(&p->authc_tree, &cookie)) != NULL)
1415                 nfsauth_free_node(node);
1416         avl_destroy(&p->authc_tree);
1417 
1418         kmem_free(p->authc_addr.buf, p->authc_addr.maxlen);
1419         rw_destroy(&p->authc_lock);
1420 
1421         kmem_free(p, sizeof (*p));
1422 }
1423 
1424 static void
1425 nfsauth_free_node(struct auth_cache *p)
1426 {
1427         crfree(p->auth_clnt_cred);
1428         kmem_free(p->auth_srv_gids, p->auth_srv_ngids * sizeof (gid_t));
1429         mutex_destroy(&p->auth_lock);
1430         cv_destroy(&p->auth_cv);
1431         kmem_cache_free(exi_cache_handle, p);
1432 }
1433 
1434 /*
1435  * Free the nfsauth cache for a given export
1436  */
1437 void
1438 nfsauth_cache_free(struct exportinfo *exi)
1439 {
1440         int i;
1441 
1442         /*
1443          * The only way we got here was with an exi_rele, which means that no
1444          * auth cache entry is being refreshed.
1445          */
1446 
1447         for (i = 0; i < AUTH_TABLESIZE; i++) {
1448                 avl_tree_t *tree = exi->exi_cache[i];
1449                 void *cookie = NULL;
1450                 struct auth_cache_clnt *node;
1451 
1452                 while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
1453                         nfsauth_free_clnt_node(node);
1454         }
1455 }
1456 
1457 /*
1458  * Called by the kernel memory allocator when
1459  * memory is low. Free unused cache entries.
1460  * If that's not enough, the VM system will
1461  * call again for some more.
1462  */
1463 /*ARGSUSED*/
1464 void
1465 exi_cache_reclaim(void *cdrarg)
1466 {
1467         int i;
1468         struct exportinfo *exi;
1469         nfs_export_t *ne = nfs_get_export();
1470 
1471         rw_enter(&ne->exported_lock, RW_READER);
1472 
1473         for (i = 0; i < EXPTABLESIZE; i++) {
1474                 for (exi = ne->exptable[i]; exi; exi = exi->fid_hash.next)
1475                         exi_cache_trim(exi);
1476         }
1477 
1478         rw_exit(&ne->exported_lock);
1479 
1480         atomic_inc_uint(&nfsauth_cache_reclaim);
1481 }
1482 
1483 void
1484 exi_cache_trim(struct exportinfo *exi)
1485 {
1486         struct auth_cache_clnt *c;
1487         struct auth_cache_clnt *nextc;
1488         struct auth_cache *p;
1489         struct auth_cache *next;
1490         int i;
1491         time_t stale_time;
1492         avl_tree_t *tree;
1493 
1494         for (i = 0; i < AUTH_TABLESIZE; i++) {
1495                 tree = exi->exi_cache[i];
1496                 stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
1497                 rw_enter(&exi->exi_cache_lock, RW_READER);
1498 
1499                 /*
1500                  * Free entries that have not been
1501                  * used for NFSAUTH_CACHE_TRIM seconds.
1502                  */
1503                 for (c = avl_first(tree); c != NULL; c = AVL_NEXT(tree, c)) {
1504                         /*
1505                          * We are being called by the kmem subsystem to reclaim
1506                          * memory so don't block if we can't get the lock.
1507                          */
1508                         if (rw_tryenter(&c->authc_lock, RW_WRITER) == 0) {
1509                                 exi_cache_auth_reclaim_failed++;
1510                                 rw_exit(&exi->exi_cache_lock);
1511                                 return;
1512                         }
1513 
1514                         for (p = avl_first(&c->authc_tree); p != NULL;
1515                             p = next) {
1516                                 next = AVL_NEXT(&c->authc_tree, p);
1517 
1518                                 ASSERT(p->auth_state != NFS_AUTH_INVALID);
1519 
1520                                 mutex_enter(&p->auth_lock);
1521 
1522                                 /*
1523                                  * We won't trim recently used and/or WAITING
1524                                  * entries.
1525                                  */
1526                                 if (p->auth_time > stale_time ||
1527                                     p->auth_state == NFS_AUTH_WAITING) {
1528                                         mutex_exit(&p->auth_lock);
1529                                         continue;
1530                                 }
1531 
1532                                 DTRACE_PROBE1(nfsauth__debug__trim__state,
1533                                     auth_state_t, p->auth_state);
1534 
1535                                 /*
1536                                  * STALE and REFRESHING entries needs to be
1537                                  * marked INVALID only because they are
1538                                  * referenced by some other structures or
1539                                  * threads.  They will be freed later.
1540                                  */
1541                                 if (p->auth_state == NFS_AUTH_STALE ||
1542                                     p->auth_state == NFS_AUTH_REFRESHING) {
1543                                         p->auth_state = NFS_AUTH_INVALID;
1544                                         mutex_exit(&p->auth_lock);
1545 
1546                                         avl_remove(&c->authc_tree, p);
1547                                 } else {
1548                                         mutex_exit(&p->auth_lock);
1549 
1550                                         avl_remove(&c->authc_tree, p);
1551                                         nfsauth_free_node(p);
1552                                 }
1553                         }
1554                         rw_exit(&c->authc_lock);
1555                 }
1556 
1557                 if (rw_tryupgrade(&exi->exi_cache_lock) == 0) {
1558                         rw_exit(&exi->exi_cache_lock);
1559                         exi_cache_clnt_reclaim_failed++;
1560                         continue;
1561                 }
1562 
1563                 for (c = avl_first(tree); c != NULL; c = nextc) {
1564                         nextc = AVL_NEXT(tree, c);
1565 
1566                         if (avl_is_empty(&c->authc_tree) == B_FALSE)
1567                                 continue;
1568 
1569                         avl_remove(tree, c);
1570 
1571                         nfsauth_free_clnt_node(c);
1572                 }
1573 
1574                 rw_exit(&exi->exi_cache_lock);
1575         }
1576 }