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 2016 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2015 by Delphix. 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
57 extern pri_t minclsyspri;
58
59 volatile uint_t nfsauth_cache_hit;
60 volatile uint_t nfsauth_cache_miss;
61 volatile uint_t nfsauth_cache_refresh;
62 volatile uint_t nfsauth_cache_reclaim;
63 volatile uint_t exi_cache_auth_reclaim_failed;
64 volatile uint_t exi_cache_clnt_reclaim_failed;
65
66 /*
67 * The lifetime of an auth cache entry:
68 * ------------------------------------
69 *
70 * An auth cache entry is created with both the auth_time
71 * and auth_freshness times set to the current time.
72 *
73 * Upon every client access which results in a hit, the
74 * auth_time will be updated.
75 *
76 * If a client access determines that the auth_freshness
77 * indicates that the entry is STALE, then it will be
78 * refreshed. Note that this will explicitly reset
102
103 /*
104 * While we could encapuslate the exi_list inside the
105 * exi structure, we can't do that for the auth_list.
106 * So, to keep things looking clean, we keep them both
107 * in these external lists.
108 */
109 typedef struct refreshq_exi_node {
110 struct exportinfo *ren_exi;
111 list_t ren_authlist;
112 list_node_t ren_node;
113 } refreshq_exi_node_t;
114
115 typedef struct refreshq_auth_node {
116 struct auth_cache *ran_auth;
117 char *ran_netid;
118 list_node_t ran_node;
119 } refreshq_auth_node_t;
120
121 /*
122 * Used to manipulate things on the refreshq_queue.
123 * Note that the refresh thread will effectively
124 * pop a node off of the queue, at which point it
125 * will no longer need to hold the mutex.
126 */
127 static kmutex_t refreshq_lock;
128 static list_t refreshq_queue;
129 static kcondvar_t refreshq_cv;
130
131 /*
132 * If there is ever a problem with loading the
133 * module, then nfsauth_fini() needs to be called
134 * to remove state. In that event, since the
135 * refreshq thread has been started, they need to
136 * work together to get rid of state.
137 */
138 typedef enum nfsauth_refreshq_thread_state {
139 REFRESHQ_THREAD_RUNNING,
140 REFRESHQ_THREAD_FINI_REQ,
141 REFRESHQ_THREAD_HALTED
142 } nfsauth_refreshq_thread_state_t;
143
144 nfsauth_refreshq_thread_state_t
145 refreshq_thread_state = REFRESHQ_THREAD_HALTED;
146
147 static void nfsauth_free_node(struct auth_cache *);
148 static void nfsauth_refresh_thread(void);
149
150 static int nfsauth_cache_compar(const void *, const void *);
151
152 /*
153 * mountd is a server-side only daemon. This will need to be
154 * revisited if the NFS server is ever made zones-aware.
155 */
156 kmutex_t mountd_lock;
157 door_handle_t mountd_dh;
158
159 void
160 mountd_args(uint_t did)
161 {
162 mutex_enter(&mountd_lock);
163 if (mountd_dh != NULL)
164 door_ki_rele(mountd_dh);
165 mountd_dh = door_ki_lookup(did);
166 mutex_exit(&mountd_lock);
167 }
168
169 void
170 nfsauth_init(void)
171 {
172 /*
173 * mountd can be restarted by smf(5). We need to make sure
174 * the updated door handle will safely make it to mountd_dh
175 */
176 mutex_init(&mountd_lock, NULL, MUTEX_DEFAULT, NULL);
177
178 mutex_init(&refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
179 list_create(&refreshq_queue, sizeof (refreshq_exi_node_t),
180 offsetof(refreshq_exi_node_t, ren_node));
181
182 cv_init(&refreshq_cv, NULL, CV_DEFAULT, NULL);
183
184 /*
185 * Allocate nfsauth cache handle
186 */
187 exi_cache_handle = kmem_cache_create("exi_cache_handle",
188 sizeof (struct auth_cache), 0, NULL, NULL,
189 exi_cache_reclaim, NULL, NULL, 0);
190
191 refreshq_thread_state = REFRESHQ_THREAD_RUNNING;
192 (void) zthread_create(NULL, 0, nfsauth_refresh_thread,
193 NULL, 0, minclsyspri);
194 }
195
196 /*
197 * Finalization routine for nfsauth. It is important to call this routine
198 * before destroying the exported_lock.
199 */
200 void
201 nfsauth_fini(void)
202 {
203 refreshq_exi_node_t *ren;
204
205 /*
206 * Prevent the nfsauth_refresh_thread from getting new
207 * work.
208 */
209 mutex_enter(&refreshq_lock);
210 if (refreshq_thread_state != REFRESHQ_THREAD_HALTED) {
211 refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
212 cv_broadcast(&refreshq_cv);
213
214 /*
215 * Also, wait for nfsauth_refresh_thread() to exit.
216 */
217 while (refreshq_thread_state != REFRESHQ_THREAD_HALTED) {
218 cv_wait(&refreshq_cv, &refreshq_lock);
219 }
220 }
221 mutex_exit(&refreshq_lock);
222
223 /*
224 * Walk the exi_list and in turn, walk the auth_lists and free all
225 * lists. In addition, free INVALID auth_cache entries.
226 */
227 while ((ren = list_remove_head(&refreshq_queue))) {
228 refreshq_auth_node_t *ran;
229
230 while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
231 struct auth_cache *p = ran->ran_auth;
232 if (p->auth_state == NFS_AUTH_INVALID)
233 nfsauth_free_node(p);
234 strfree(ran->ran_netid);
235 kmem_free(ran, sizeof (refreshq_auth_node_t));
236 }
237
238 list_destroy(&ren->ren_authlist);
239 exi_rele(ren->ren_exi);
240 kmem_free(ren, sizeof (refreshq_exi_node_t));
241 }
242 list_destroy(&refreshq_queue);
243
244 cv_destroy(&refreshq_cv);
245 mutex_destroy(&refreshq_lock);
246
247 mutex_destroy(&mountd_lock);
248
249 /*
250 * Deallocate nfsauth cache handle
251 */
252 kmem_cache_destroy(exi_cache_handle);
253 }
254
255 /*
256 * Convert the address in a netbuf to
257 * a hash index for the auth_cache table.
258 */
259 static int
260 hash(struct netbuf *a)
261 {
262 int i, h = 0;
263
264 for (i = 0; i < a->len; i++)
265 h ^= a->buf[i];
266
267 return (h & (AUTH_TABLESIZE - 1));
268 }
269
270 /*
271 * Mask out the components of an
272 * address that do not identify
324 static void
325 sys_log(const char *msg)
326 {
327 static time_t tstamp = 0;
328 time_t now;
329
330 /*
331 * msg is shown (at most) once per minute
332 */
333 now = gethrestime_sec();
334 if ((tstamp + 60) < now) {
335 tstamp = now;
336 cmn_err(CE_WARN, msg);
337 }
338 }
339
340 /*
341 * Callup to the mountd to get access information in the kernel.
342 */
343 static bool_t
344 nfsauth_retrieve(struct exportinfo *exi, char *req_netid, int flavor,
345 struct netbuf *addr, int *access, cred_t *clnt_cred, uid_t *srv_uid,
346 gid_t *srv_gid, uint_t *srv_gids_cnt, gid_t **srv_gids)
347 {
348 varg_t varg = {0};
349 nfsauth_res_t res = {0};
350 XDR xdrs;
351 size_t absz;
352 caddr_t abuf;
353 int last = 0;
354 door_arg_t da;
355 door_info_t di;
356 door_handle_t dh;
357 uint_t ntries = 0;
358
359 /*
360 * No entry in the cache for this client/flavor
361 * so we need to call the nfsauth service in the
362 * mount daemon.
363 */
364
365 varg.vers = V_PROTO;
366 varg.arg_u.arg.cmd = NFSAUTH_ACCESS;
399 XDR_DESTROY(&xdrs);
400
401 /*
402 * Prepare the door arguments
403 *
404 * We don't know the size of the message the daemon
405 * will pass back to us. By setting rbuf to NULL,
406 * we force the door code to allocate a buf of the
407 * appropriate size. We must set rsize > 0, however,
408 * else the door code acts as if no response was
409 * expected and doesn't pass the data to us.
410 */
411 da.data_ptr = (char *)abuf;
412 da.data_size = absz;
413 da.desc_ptr = NULL;
414 da.desc_num = 0;
415 da.rbuf = NULL;
416 da.rsize = 1;
417
418 retry:
419 mutex_enter(&mountd_lock);
420 dh = mountd_dh;
421 if (dh != NULL)
422 door_ki_hold(dh);
423 mutex_exit(&mountd_lock);
424
425 if (dh == NULL) {
426 /*
427 * The rendezvous point has not been established yet!
428 * This could mean that either mountd(1m) has not yet
429 * been started or that _this_ routine nuked the door
430 * handle after receiving an EINTR for a REVOKED door.
431 *
432 * Returning NFSAUTH_DROP will cause the NFS client
433 * to retransmit the request, so let's try to be more
434 * rescillient and attempt for ntries before we bail.
435 */
436 if (++ntries % NFSAUTH_DR_TRYCNT) {
437 delay(hz);
438 goto retry;
439 }
440
441 kmem_free(abuf, absz);
442
443 sys_log("nfsauth: mountd has not established door");
473 /*
474 * Server out of resources; back off for a bit
475 */
476 door_ki_rele(dh);
477 delay(hz);
478 goto retry;
479 /* NOTREACHED */
480
481 case EINTR:
482 if (!door_ki_info(dh, &di)) {
483 door_ki_rele(dh);
484
485 if (di.di_attributes & DOOR_REVOKED) {
486 /*
487 * The server barfed and revoked
488 * the (existing) door on us; we
489 * want to wait to give smf(5) a
490 * chance to restart mountd(1m)
491 * and establish a new door handle.
492 */
493 mutex_enter(&mountd_lock);
494 if (dh == mountd_dh) {
495 door_ki_rele(mountd_dh);
496 mountd_dh = NULL;
497 }
498 mutex_exit(&mountd_lock);
499 delay(hz);
500 goto retry;
501 }
502 /*
503 * If the door was _not_ revoked on us,
504 * then more than likely we took an INTR,
505 * so we need to fail the operation.
506 */
507 goto fail;
508 }
509 /*
510 * The only failure that can occur from getting
511 * the door info is EINVAL, so we let the code
512 * below handle it.
513 */
514 /* FALLTHROUGH */
515
516 case EBADF:
517 case EINVAL:
518 default:
570
571 case NFSAUTH_DR_EFAIL:
572 case NFSAUTH_DR_DECERR:
573 case NFSAUTH_DR_BADCMD:
574 default:
575 xdr_free(xdr_nfsauth_res, (char *)&res);
576 fail:
577 *access = NFSAUTH_DENIED;
578 kmem_free(abuf, absz);
579 return (FALSE);
580 /* NOTREACHED */
581 }
582
583 xdr_free(xdr_nfsauth_res, (char *)&res);
584 kmem_free(abuf, absz);
585
586 return (TRUE);
587 }
588
589 static void
590 nfsauth_refresh_thread(void)
591 {
592 refreshq_exi_node_t *ren;
593 refreshq_auth_node_t *ran;
594
595 struct exportinfo *exi;
596
597 int access;
598 bool_t retrieval;
599
600 callb_cpr_t cprinfo;
601
602 CALLB_CPR_INIT(&cprinfo, &refreshq_lock, callb_generic_cpr,
603 "nfsauth_refresh");
604
605 for (;;) {
606 mutex_enter(&refreshq_lock);
607 if (refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
608 /* Keep the hold on the lock! */
609 break;
610 }
611
612 ren = list_remove_head(&refreshq_queue);
613 if (ren == NULL) {
614 CALLB_CPR_SAFE_BEGIN(&cprinfo);
615 cv_wait(&refreshq_cv, &refreshq_lock);
616 CALLB_CPR_SAFE_END(&cprinfo, &refreshq_lock);
617 mutex_exit(&refreshq_lock);
618 continue;
619 }
620 mutex_exit(&refreshq_lock);
621
622 exi = ren->ren_exi;
623 ASSERT(exi != NULL);
624
625 /*
626 * Since the ren was removed from the refreshq_queue above,
627 * this is the only thread aware about the ren existence, so we
628 * have the exclusive ownership of it and we do not need to
629 * protect it by any lock.
630 */
631 while ((ran = list_remove_head(&ren->ren_authlist))) {
632 uid_t uid;
633 gid_t gid;
634 uint_t ngids;
635 gid_t *gids;
636 struct auth_cache *p = ran->ran_auth;
637 char *netid = ran->ran_netid;
638
639 ASSERT(p != NULL);
640 ASSERT(netid != NULL);
647 * Once the entry goes INVALID, it can not change
648 * state.
649 *
650 * No need to refresh entries also in a case we are
651 * just shutting down.
652 *
653 * In general, there is no need to hold the
654 * refreshq_lock to test the refreshq_thread_state. We
655 * do hold it at other places because there is some
656 * related thread synchronization (or some other tasks)
657 * close to the refreshq_thread_state check.
658 *
659 * The check for the refreshq_thread_state value here
660 * is purely advisory to allow the faster
661 * nfsauth_refresh_thread() shutdown. In a case we
662 * will miss such advisory, nothing catastrophic
663 * happens: we will just spin longer here before the
664 * shutdown.
665 */
666 if (p->auth_state == NFS_AUTH_INVALID ||
667 refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
668 mutex_exit(&p->auth_lock);
669
670 if (p->auth_state == NFS_AUTH_INVALID)
671 nfsauth_free_node(p);
672
673 strfree(netid);
674
675 continue;
676 }
677
678 /*
679 * Make sure the state is valid. Note that once we
680 * change the state to NFS_AUTH_REFRESHING, no other
681 * thread will be able to work on this entry.
682 */
683 ASSERT(p->auth_state == NFS_AUTH_STALE);
684
685 p->auth_state = NFS_AUTH_REFRESHING;
686 mutex_exit(&p->auth_lock);
687
688 DTRACE_PROBE2(nfsauth__debug__cache__refresh,
689 struct exportinfo *, exi,
690 struct auth_cache *, p);
691
692 /*
693 * The first caching of the access rights
694 * is done with the netid pulled out of the
695 * request from the client. All subsequent
696 * users of the cache may or may not have
697 * the same netid. It doesn't matter. So
698 * when we refresh, we simply use the netid
699 * of the request which triggered the
700 * refresh attempt.
701 */
702 retrieval = nfsauth_retrieve(exi, netid,
703 p->auth_flavor, &p->auth_clnt->authc_addr, &access,
704 p->auth_clnt_cred, &uid, &gid, &ngids, &gids);
705
706 /*
707 * This can only be set in one other place
708 * and the state has to be NFS_AUTH_FRESH.
709 */
710 strfree(netid);
711
712 mutex_enter(&p->auth_lock);
713 if (p->auth_state == NFS_AUTH_INVALID) {
714 mutex_exit(&p->auth_lock);
715 nfsauth_free_node(p);
716 if (retrieval == TRUE)
717 kmem_free(gids, ngids * sizeof (gid_t));
718 } else {
719 /*
720 * If we got an error, do not reset the
721 * time. This will cause the next access
722 * check for the client to reschedule this
725 if (retrieval == TRUE) {
726 p->auth_access = access;
727
728 p->auth_srv_uid = uid;
729 p->auth_srv_gid = gid;
730 kmem_free(p->auth_srv_gids,
731 p->auth_srv_ngids * sizeof (gid_t));
732 p->auth_srv_ngids = ngids;
733 p->auth_srv_gids = gids;
734
735 p->auth_freshness = gethrestime_sec();
736 }
737 p->auth_state = NFS_AUTH_FRESH;
738
739 cv_broadcast(&p->auth_cv);
740 mutex_exit(&p->auth_lock);
741 }
742 }
743
744 list_destroy(&ren->ren_authlist);
745 exi_rele(ren->ren_exi);
746 kmem_free(ren, sizeof (refreshq_exi_node_t));
747 }
748
749 refreshq_thread_state = REFRESHQ_THREAD_HALTED;
750 cv_broadcast(&refreshq_cv);
751 CALLB_CPR_EXIT(&cprinfo);
752 zthread_exit();
753 }
754
755 int
756 nfsauth_cache_clnt_compar(const void *v1, const void *v2)
757 {
758 int c;
759
760 const struct auth_cache_clnt *a1 = (const struct auth_cache_clnt *)v1;
761 const struct auth_cache_clnt *a2 = (const struct auth_cache_clnt *)v2;
762
763 if (a1->authc_addr.len < a2->authc_addr.len)
764 return (-1);
765 if (a1->authc_addr.len > a2->authc_addr.len)
766 return (1);
767
768 c = memcmp(a1->authc_addr.buf, a2->authc_addr.buf, a1->authc_addr.len);
769 if (c < 0)
770 return (-1);
771 if (c > 0)
803 return (1);
804
805 c = memcmp(crgetgroups(a1->auth_clnt_cred),
806 crgetgroups(a2->auth_clnt_cred), crgetngroups(a1->auth_clnt_cred));
807 if (c < 0)
808 return (-1);
809 if (c > 0)
810 return (1);
811
812 return (0);
813 }
814
815 /*
816 * Get the access information from the cache or callup to the mountd
817 * to get and cache the access information in the kernel.
818 */
819 static int
820 nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor,
821 cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
822 {
823 struct netbuf *taddrmask;
824 struct netbuf addr; /* temporary copy of client's address */
825 const struct netbuf *claddr;
826 avl_tree_t *tree;
827 struct auth_cache ac; /* used as a template for avl_find() */
828 struct auth_cache_clnt *c;
829 struct auth_cache_clnt acc; /* used as a template for avl_find() */
830 struct auth_cache *p = NULL;
831 int access;
832
833 uid_t tmpuid;
834 gid_t tmpgid;
835 uint_t tmpngids;
836 gid_t *tmpgids;
837
838 avl_index_t where; /* used for avl_find()/avl_insert() */
839
840 ASSERT(cr != NULL);
841
842 /*
843 * Now check whether this client already
844 * has an entry for this flavor in the cache
845 * for this export.
846 * Get the caller's address, mask off the
847 * parts of the address that do not identify
848 * the host (port number, etc), and then hash
849 * it to find the chain of cache entries.
850 */
851
852 claddr = svc_getrpccaller(req->rq_xprt);
853 addr = *claddr;
854 addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
855 bcopy(claddr->buf, addr.buf, claddr->len);
856
857 SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
858 ASSERT(taddrmask != NULL);
859 addrmask(&addr, taddrmask);
860
861 ac.auth_flavor = flavor;
981
982 /*
983 * If the cache entry is not valid yet, we need to retrieve the
984 * info ourselves.
985 */
986 if (p->auth_state == NFS_AUTH_NEW) {
987 bool_t res;
988 /*
989 * NFS_AUTH_NEW is the default output auth_state value in a
990 * case we failed somewhere below.
991 */
992 auth_state_t state = NFS_AUTH_NEW;
993
994 p->auth_state = NFS_AUTH_WAITING;
995 mutex_exit(&p->auth_lock);
996 kmem_free(addr.buf, addr.maxlen);
997 addr = p->auth_clnt->authc_addr;
998
999 atomic_inc_uint(&nfsauth_cache_miss);
1000
1001 res = nfsauth_retrieve(exi, svc_getnetid(req->rq_xprt), flavor,
1002 &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids);
1003
1004 p->auth_access = access;
1005 p->auth_time = p->auth_freshness = gethrestime_sec();
1006
1007 if (res == TRUE) {
1008 if (uid != NULL)
1009 *uid = tmpuid;
1010 if (gid != NULL)
1011 *gid = tmpgid;
1012 if (ngids != NULL && gids != NULL) {
1013 *ngids = tmpngids;
1014 *gids = tmpgids;
1015
1016 /*
1017 * We need a copy of gids for the
1018 * auth_cache entry
1019 */
1020 tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
1021 KM_NOSLEEP | KM_NORMALPRI);
1022 if (tmpgids != NULL)
1063
1064 if ((refresh > NFSAUTH_CACHE_REFRESH) &&
1065 p->auth_state == NFS_AUTH_FRESH) {
1066 refreshq_auth_node_t *ran;
1067 uint_t nacr;
1068
1069 p->auth_state = NFS_AUTH_STALE;
1070 mutex_exit(&p->auth_lock);
1071
1072 nacr = atomic_inc_uint_nv(&nfsauth_cache_refresh);
1073 DTRACE_PROBE3(nfsauth__debug__cache__stale,
1074 struct exportinfo *, exi,
1075 struct auth_cache *, p,
1076 uint_t, nacr);
1077
1078 ran = kmem_alloc(sizeof (refreshq_auth_node_t),
1079 KM_SLEEP);
1080 ran->ran_auth = p;
1081 ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
1082
1083 mutex_enter(&refreshq_lock);
1084 /*
1085 * We should not add a work queue
1086 * item if the thread is not
1087 * accepting them.
1088 */
1089 if (refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
1090 refreshq_exi_node_t *ren;
1091
1092 /*
1093 * Is there an existing exi_list?
1094 */
1095 for (ren = list_head(&refreshq_queue);
1096 ren != NULL;
1097 ren = list_next(&refreshq_queue, ren)) {
1098 if (ren->ren_exi == exi) {
1099 list_insert_tail(
1100 &ren->ren_authlist, ran);
1101 break;
1102 }
1103 }
1104
1105 if (ren == NULL) {
1106 ren = kmem_alloc(
1107 sizeof (refreshq_exi_node_t),
1108 KM_SLEEP);
1109
1110 exi_hold(exi);
1111 ren->ren_exi = exi;
1112
1113 list_create(&ren->ren_authlist,
1114 sizeof (refreshq_auth_node_t),
1115 offsetof(refreshq_auth_node_t,
1116 ran_node));
1117
1118 list_insert_tail(&ren->ren_authlist,
1119 ran);
1120 list_insert_tail(&refreshq_queue, ren);
1121 }
1122
1123 cv_broadcast(&refreshq_cv);
1124 } else {
1125 strfree(ran->ran_netid);
1126 kmem_free(ran, sizeof (refreshq_auth_node_t));
1127 }
1128
1129 mutex_exit(&refreshq_lock);
1130 } else {
1131 mutex_exit(&p->auth_lock);
1132 }
1133
1134 nach = atomic_inc_uint_nv(&nfsauth_cache_hit);
1135 DTRACE_PROBE2(nfsauth__debug__cache__hit,
1136 uint_t, nach,
1137 time_t, refresh);
1138
1139 kmem_free(addr.buf, addr.maxlen);
1140 }
1141
1142 return (access);
1143
1144 retrieve:
1145 crfree(ac.auth_clnt_cred);
1146
1147 /*
1148 * Retrieve the required data without caching.
1149 */
1150
1151 ASSERT(p == NULL);
1152
1153 atomic_inc_uint(&nfsauth_cache_miss);
1154
1155 if (nfsauth_retrieve(exi, svc_getnetid(req->rq_xprt), flavor, &addr,
1156 &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
1157 if (uid != NULL)
1158 *uid = tmpuid;
1159 if (gid != NULL)
1160 *gid = tmpgid;
1161 if (ngids != NULL && gids != NULL) {
1162 *ngids = tmpngids;
1163 *gids = tmpgids;
1164 } else {
1165 kmem_free(tmpgids, tmpngids * sizeof (gid_t));
1166 }
1167 }
1168
1169 kmem_free(addr.buf, addr.maxlen);
1170
1171 return (access);
1172 }
1173
1174 /*
1175 * Check if the requesting client has access to the filesystem with
1176 * a given nfs flavor number which is an explicitly shared flavor.
1394 void *cookie = NULL;
1395 struct auth_cache_clnt *node;
1396
1397 while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
1398 nfsauth_free_clnt_node(node);
1399 }
1400 }
1401
1402 /*
1403 * Called by the kernel memory allocator when
1404 * memory is low. Free unused cache entries.
1405 * If that's not enough, the VM system will
1406 * call again for some more.
1407 */
1408 /*ARGSUSED*/
1409 void
1410 exi_cache_reclaim(void *cdrarg)
1411 {
1412 int i;
1413 struct exportinfo *exi;
1414
1415 rw_enter(&exported_lock, RW_READER);
1416
1417 for (i = 0; i < EXPTABLESIZE; i++) {
1418 for (exi = exptable[i]; exi; exi = exi->fid_hash.next) {
1419 exi_cache_trim(exi);
1420 }
1421 }
1422
1423 rw_exit(&exported_lock);
1424
1425 atomic_inc_uint(&nfsauth_cache_reclaim);
1426 }
1427
1428 void
1429 exi_cache_trim(struct exportinfo *exi)
1430 {
1431 struct auth_cache_clnt *c;
1432 struct auth_cache_clnt *nextc;
1433 struct auth_cache *p;
1434 struct auth_cache *next;
1435 int i;
1436 time_t stale_time;
1437 avl_tree_t *tree;
1438
1439 for (i = 0; i < AUTH_TABLESIZE; i++) {
1440 tree = exi->exi_cache[i];
1441 stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
1442 rw_enter(&exi->exi_cache_lock, RW_READER);
1443
|
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 */
25
26 /*
27 * Copyright 2018 Nexenta Systems, Inc.
28 * Copyright (c) 2015 by Delphix. All rights reserved.
29 */
30
31 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/vfs.h>
34 #include <sys/vnode.h>
35 #include <sys/cred.h>
36 #include <sys/cmn_err.h>
37 #include <sys/systm.h>
38 #include <sys/kmem.h>
39 #include <sys/pathname.h>
40 #include <sys/utsname.h>
41 #include <sys/debug.h>
42 #include <sys/door.h>
43 #include <sys/sdt.h>
44 #include <sys/thread.h>
45 #include <sys/avl.h>
46
47 #include <rpc/types.h>
48 #include <rpc/auth.h>
49 #include <rpc/clnt.h>
50
51 #include <nfs/nfs.h>
52 #include <nfs/export.h>
53 #include <nfs/nfs_clnt.h>
54 #include <nfs/auth.h>
55
56 static struct kmem_cache *exi_cache_handle;
57 static void exi_cache_reclaim(void *);
58 static void exi_cache_trim(struct exportinfo *exi);
59 static void *nfsauth_zone_init(zoneid_t);
60 static void nfsauth_zone_shutdown(zoneid_t zoneid, void *data);
61 static void nfsauth_zone_fini(zoneid_t, void *);
62
63 extern pri_t minclsyspri;
64
65 /* NFS auth cache statistics */
66 volatile uint_t nfsauth_cache_hit;
67 volatile uint_t nfsauth_cache_miss;
68 volatile uint_t nfsauth_cache_refresh;
69 volatile uint_t nfsauth_cache_reclaim;
70 volatile uint_t exi_cache_auth_reclaim_failed;
71 volatile uint_t exi_cache_clnt_reclaim_failed;
72
73 /*
74 * The lifetime of an auth cache entry:
75 * ------------------------------------
76 *
77 * An auth cache entry is created with both the auth_time
78 * and auth_freshness times set to the current time.
79 *
80 * Upon every client access which results in a hit, the
81 * auth_time will be updated.
82 *
83 * If a client access determines that the auth_freshness
84 * indicates that the entry is STALE, then it will be
85 * refreshed. Note that this will explicitly reset
109
110 /*
111 * While we could encapuslate the exi_list inside the
112 * exi structure, we can't do that for the auth_list.
113 * So, to keep things looking clean, we keep them both
114 * in these external lists.
115 */
116 typedef struct refreshq_exi_node {
117 struct exportinfo *ren_exi;
118 list_t ren_authlist;
119 list_node_t ren_node;
120 } refreshq_exi_node_t;
121
122 typedef struct refreshq_auth_node {
123 struct auth_cache *ran_auth;
124 char *ran_netid;
125 list_node_t ran_node;
126 } refreshq_auth_node_t;
127
128 /*
129 * Used to manipulate things on the refreshq_queue. Note that the refresh
130 * thread will effectively pop a node off of the queue, at which point it
131 * will no longer need to hold the mutex.
132 */
133 static kmutex_t refreshq_lock;
134 static list_t refreshq_queue;
135 static kcondvar_t refreshq_cv;
136
137 /*
138 * If there is ever a problem with loading the module, then nfsauth_fini()
139 * needs to be called to remove state. In that event, since the refreshq
140 * thread has been started, they need to work together to get rid of state.
141 */
142 typedef enum nfsauth_refreshq_thread_state {
143 REFRESHQ_THREAD_RUNNING,
144 REFRESHQ_THREAD_FINI_REQ,
145 REFRESHQ_THREAD_HALTED,
146 REFRESHQ_THREAD_NEED_CREATE
147 } nfsauth_refreshq_thread_state_t;
148
149 typedef struct nfsauth_globals {
150 kmutex_t mountd_lock;
151 door_handle_t mountd_dh;
152
153 /*
154 * Used to manipulate things on the refreshq_queue. Note that the
155 * refresh thread will effectively pop a node off of the queue,
156 * at which point it will no longer need to hold the mutex.
157 */
158 kmutex_t refreshq_lock;
159 list_t refreshq_queue;
160 kcondvar_t refreshq_cv;
161
162 /*
163 * A list_t would be overkill. These are auth_cache entries which are
164 * no longer linked to an exi. It should be the case that all of their
165 * states are NFS_AUTH_INVALID, i.e., the only way to be put on this
166 * list is iff their state indicated that they had been placed on the
167 * refreshq_queue.
168 *
169 * Note that while there is no link from the exi or back to the exi,
170 * the exi can not go away until these entries are harvested.
171 */
172 struct auth_cache *refreshq_dead_entries;
173 nfsauth_refreshq_thread_state_t refreshq_thread_state;
174
175 } nfsauth_globals_t;
176
177 static void nfsauth_free_node(struct auth_cache *);
178 static void nfsauth_refresh_thread(nfsauth_globals_t *);
179
180 static int nfsauth_cache_compar(const void *, const void *);
181
182 static zone_key_t nfsauth_zone_key;
183
184 void
185 mountd_args(uint_t did)
186 {
187 nfsauth_globals_t *nag;
188
189 nag = zone_getspecific(nfsauth_zone_key, curzone);
190 mutex_enter(&nag->mountd_lock);
191 if (nag->mountd_dh != NULL)
192 door_ki_rele(nag->mountd_dh);
193 nag->mountd_dh = door_ki_lookup(did);
194 mutex_exit(&nag->mountd_lock);
195 }
196
197 void
198 nfsauth_init(void)
199 {
200 zone_key_create(&nfsauth_zone_key, nfsauth_zone_init,
201 nfsauth_zone_shutdown, nfsauth_zone_fini);
202
203 exi_cache_handle = kmem_cache_create("exi_cache_handle",
204 sizeof (struct auth_cache), 0, NULL, NULL,
205 exi_cache_reclaim, NULL, NULL, 0);
206 }
207
208 void
209 nfsauth_fini(void)
210 {
211 kmem_cache_destroy(exi_cache_handle);
212 }
213
214 /*ARGSUSED*/
215 static void *
216 nfsauth_zone_init(zoneid_t zoneid)
217 {
218 nfsauth_globals_t *nag;
219
220 nag = kmem_zalloc(sizeof (*nag), KM_SLEEP);
221
222 /*
223 * mountd can be restarted by smf(5). We need to make sure
224 * the updated door handle will safely make it to mountd_dh.
225 */
226 mutex_init(&nag->mountd_lock, NULL, MUTEX_DEFAULT, NULL);
227 mutex_init(&nag->refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
228 list_create(&nag->refreshq_queue, sizeof (refreshq_exi_node_t),
229 offsetof(refreshq_exi_node_t, ren_node));
230 cv_init(&nag->refreshq_cv, NULL, CV_DEFAULT, NULL);
231 nag->refreshq_thread_state = REFRESHQ_THREAD_NEED_CREATE;
232
233 return (nag);
234 }
235
236 /*ARGSUSED*/
237 static void
238 nfsauth_zone_shutdown(zoneid_t zoneid, void *data)
239 {
240 refreshq_exi_node_t *ren;
241 nfsauth_globals_t *nag = data;
242
243 /* Prevent the nfsauth_refresh_thread from getting new work */
244 mutex_enter(&nag->refreshq_lock);
245 if (nag->refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
246 nag->refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
247 cv_broadcast(&nag->refreshq_cv);
248
249 /* Wait for nfsauth_refresh_thread() to exit */
250 while (nag->refreshq_thread_state != REFRESHQ_THREAD_HALTED)
251 cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
252 }
253 mutex_exit(&nag->refreshq_lock);
254
255 /*
256 * Walk the exi_list and in turn, walk the auth_lists and free all
257 * lists. In addition, free INVALID auth_cache entries.
258 */
259 while ((ren = list_remove_head(&nag->refreshq_queue))) {
260 refreshq_auth_node_t *ran;
261
262 while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
263 struct auth_cache *p = ran->ran_auth;
264 if (p->auth_state == NFS_AUTH_INVALID)
265 nfsauth_free_node(p);
266 strfree(ran->ran_netid);
267 kmem_free(ran, sizeof (*ran));
268 }
269
270 list_destroy(&ren->ren_authlist);
271 exi_rele(&ren->ren_exi);
272 kmem_free(ren, sizeof (*ren));
273 }
274 }
275
276 /*ARGSUSED*/
277 static void
278 nfsauth_zone_fini(zoneid_t zoneid, void *data)
279 {
280 nfsauth_globals_t *nag = data;
281
282 list_destroy(&nag->refreshq_queue);
283 cv_destroy(&nag->refreshq_cv);
284 mutex_destroy(&nag->refreshq_lock);
285 mutex_destroy(&nag->mountd_lock);
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
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;
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");
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:
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);
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
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)
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 nag = zone_getspecific(nfsauth_zone_key, curzone);
881
882 /*
883 * Now check whether this client already
884 * has an entry for this flavor in the cache
885 * for this export.
886 * Get the caller's address, mask off the
887 * parts of the address that do not identify
888 * the host (port number, etc), and then hash
889 * it to find the chain of cache entries.
890 */
891
892 claddr = svc_getrpccaller(req->rq_xprt);
893 addr = *claddr;
894 addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
895 bcopy(claddr->buf, addr.buf, claddr->len);
896
897 SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
898 ASSERT(taddrmask != NULL);
899 addrmask(&addr, taddrmask);
900
901 ac.auth_flavor = flavor;
1021
1022 /*
1023 * If the cache entry is not valid yet, we need to retrieve the
1024 * info ourselves.
1025 */
1026 if (p->auth_state == NFS_AUTH_NEW) {
1027 bool_t res;
1028 /*
1029 * NFS_AUTH_NEW is the default output auth_state value in a
1030 * case we failed somewhere below.
1031 */
1032 auth_state_t state = NFS_AUTH_NEW;
1033
1034 p->auth_state = NFS_AUTH_WAITING;
1035 mutex_exit(&p->auth_lock);
1036 kmem_free(addr.buf, addr.maxlen);
1037 addr = p->auth_clnt->authc_addr;
1038
1039 atomic_inc_uint(&nfsauth_cache_miss);
1040
1041 res = nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt),
1042 flavor, &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids,
1043 &tmpgids);
1044
1045 p->auth_access = access;
1046 p->auth_time = p->auth_freshness = gethrestime_sec();
1047
1048 if (res == TRUE) {
1049 if (uid != NULL)
1050 *uid = tmpuid;
1051 if (gid != NULL)
1052 *gid = tmpgid;
1053 if (ngids != NULL && gids != NULL) {
1054 *ngids = tmpngids;
1055 *gids = tmpgids;
1056
1057 /*
1058 * We need a copy of gids for the
1059 * auth_cache entry
1060 */
1061 tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
1062 KM_NOSLEEP | KM_NORMALPRI);
1063 if (tmpgids != NULL)
1104
1105 if ((refresh > NFSAUTH_CACHE_REFRESH) &&
1106 p->auth_state == NFS_AUTH_FRESH) {
1107 refreshq_auth_node_t *ran;
1108 uint_t nacr;
1109
1110 p->auth_state = NFS_AUTH_STALE;
1111 mutex_exit(&p->auth_lock);
1112
1113 nacr = atomic_inc_uint_nv(&nfsauth_cache_refresh);
1114 DTRACE_PROBE3(nfsauth__debug__cache__stale,
1115 struct exportinfo *, exi,
1116 struct auth_cache *, p,
1117 uint_t, nacr);
1118
1119 ran = kmem_alloc(sizeof (refreshq_auth_node_t),
1120 KM_SLEEP);
1121 ran->ran_auth = p;
1122 ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
1123
1124 mutex_enter(&nag->refreshq_lock);
1125
1126 if (nag->refreshq_thread_state ==
1127 REFRESHQ_THREAD_NEED_CREATE) {
1128 /* Launch nfsauth refresh thread */
1129 nag->refreshq_thread_state =
1130 REFRESHQ_THREAD_RUNNING;
1131 (void) zthread_create(NULL, 0,
1132 nfsauth_refresh_thread, nag, 0,
1133 minclsyspri);
1134 }
1135
1136 /*
1137 * We should not add a work queue item if the thread
1138 * is not accepting them.
1139 */
1140 if (nag->refreshq_thread_state ==
1141 REFRESHQ_THREAD_RUNNING) {
1142 refreshq_exi_node_t *ren;
1143
1144 /*
1145 * Is there an existing exi_list?
1146 */
1147 for (ren = list_head(&nag->refreshq_queue);
1148 ren != NULL;
1149 ren = list_next(&nag->refreshq_queue,
1150 ren)) {
1151 if (ren->ren_exi == exi) {
1152 list_insert_tail(
1153 &ren->ren_authlist, ran);
1154 break;
1155 }
1156 }
1157
1158 if (ren == NULL) {
1159 ren = kmem_alloc(
1160 sizeof (refreshq_exi_node_t),
1161 KM_SLEEP);
1162
1163 exi_hold(exi);
1164 ren->ren_exi = exi;
1165
1166 list_create(&ren->ren_authlist,
1167 sizeof (refreshq_auth_node_t),
1168 offsetof(refreshq_auth_node_t,
1169 ran_node));
1170
1171 list_insert_tail(&ren->ren_authlist,
1172 ran);
1173 list_insert_tail(&nag->refreshq_queue,
1174 ren);
1175 }
1176
1177 cv_broadcast(&nag->refreshq_cv);
1178 } else {
1179 strfree(ran->ran_netid);
1180 kmem_free(ran, sizeof (refreshq_auth_node_t));
1181 }
1182
1183 mutex_exit(&nag->refreshq_lock);
1184 } else {
1185 mutex_exit(&p->auth_lock);
1186 }
1187
1188 nach = atomic_inc_uint_nv(&nfsauth_cache_hit);
1189 DTRACE_PROBE2(nfsauth__debug__cache__hit,
1190 uint_t, nach,
1191 time_t, refresh);
1192
1193 kmem_free(addr.buf, addr.maxlen);
1194 }
1195
1196 return (access);
1197
1198 retrieve:
1199 crfree(ac.auth_clnt_cred);
1200
1201 /*
1202 * Retrieve the required data without caching.
1203 */
1204
1205 ASSERT(p == NULL);
1206
1207 atomic_inc_uint(&nfsauth_cache_miss);
1208
1209 if (nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt), flavor,
1210 &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
1211 if (uid != NULL)
1212 *uid = tmpuid;
1213 if (gid != NULL)
1214 *gid = tmpgid;
1215 if (ngids != NULL && gids != NULL) {
1216 *ngids = tmpngids;
1217 *gids = tmpgids;
1218 } else {
1219 kmem_free(tmpgids, tmpngids * sizeof (gid_t));
1220 }
1221 }
1222
1223 kmem_free(addr.buf, addr.maxlen);
1224
1225 return (access);
1226 }
1227
1228 /*
1229 * Check if the requesting client has access to the filesystem with
1230 * a given nfs flavor number which is an explicitly shared flavor.
1448 void *cookie = NULL;
1449 struct auth_cache_clnt *node;
1450
1451 while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
1452 nfsauth_free_clnt_node(node);
1453 }
1454 }
1455
1456 /*
1457 * Called by the kernel memory allocator when
1458 * memory is low. Free unused cache entries.
1459 * If that's not enough, the VM system will
1460 * call again for some more.
1461 */
1462 /*ARGSUSED*/
1463 void
1464 exi_cache_reclaim(void *cdrarg)
1465 {
1466 int i;
1467 struct exportinfo *exi;
1468 nfs_export_t *ne = nfs_get_export();
1469
1470 rw_enter(&ne->exported_lock, RW_READER);
1471
1472 for (i = 0; i < EXPTABLESIZE; i++) {
1473 for (exi = ne->exptable[i]; exi; exi = exi->fid_hash.next)
1474 exi_cache_trim(exi);
1475 }
1476
1477 rw_exit(&ne->exported_lock);
1478
1479 atomic_inc_uint(&nfsauth_cache_reclaim);
1480 }
1481
1482 void
1483 exi_cache_trim(struct exportinfo *exi)
1484 {
1485 struct auth_cache_clnt *c;
1486 struct auth_cache_clnt *nextc;
1487 struct auth_cache *p;
1488 struct auth_cache *next;
1489 int i;
1490 time_t stale_time;
1491 avl_tree_t *tree;
1492
1493 for (i = 0; i < AUTH_TABLESIZE; i++) {
1494 tree = exi->exi_cache[i];
1495 stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
1496 rw_enter(&exi->exi_cache_lock, RW_READER);
1497
|