Whamcloud - gitweb
LU-9795 gss: properly handle mgssec
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_svc_upcall.c
1 /*
2  * Modifications for Lustre
3  *
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  *
6  * Copyright (c) 2012, 2014, Intel Corporation.
7  *
8  * Author: Eric Mei <ericm@clusterfs.com>
9  */
10
11 /*
12  * Neil Brown <neilb@cse.unsw.edu.au>
13  * J. Bruce Fields <bfields@umich.edu>
14  * Andy Adamson <andros@umich.edu>
15  * Dug Song <dugsong@monkey.org>
16  *
17  * RPCSEC_GSS server authentication.
18  * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
19  * (gssapi)
20  *
21  * The RPCSEC_GSS involves three stages:
22  *  1/ context creation
23  *  2/ data exchange
24  *  3/ context destruction
25  *
26  * Context creation is handled largely by upcalls to user-space.
27  *  In particular, GSS_Accept_sec_context is handled by an upcall
28  * Data exchange is handled entirely within the kernel
29  *  In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
30  * Context destruction is handled in-kernel
31  *  GSS_Delete_sec_context is in-kernel
32  *
33  * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
34  * The context handle and gss_token are used as a key into the rpcsec_init cache.
35  * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
36  * being major_status, minor_status, context_handle, reply_token.
37  * These are sent back to the client.
38  * Sequence window management is handled by the kernel.  The window size if currently
39  * a compile time constant.
40  *
41  * When user-space is happy that a context is established, it places an entry
42  * in the rpcsec_context cache. The key for this cache is the context_handle.
43  * The content includes:
44  *   uid/gidlist - for determining access rights
45  *   mechanism type
46  *   mechanism specific information, such as a key
47  *
48  */
49
50 #define DEBUG_SUBSYSTEM S_SEC
51 #include <linux/types.h>
52 #include <linux/init.h>
53 #include <linux/module.h>
54 #include <linux/slab.h>
55 #include <linux/hash.h>
56 #include <linux/mutex.h>
57 #include <linux/sunrpc/cache.h>
58 #include <net/sock.h>
59
60 #include <obd.h>
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <lustre_import.h>
64 #include <lustre_net.h>
65 #include <lustre_nodemap.h>
66 #include <lustre_sec.h>
67
68 #include "gss_err.h"
69 #include "gss_internal.h"
70 #include "gss_api.h"
71
72 #define GSS_SVC_UPCALL_TIMEOUT  (20)
73
74 static spinlock_t __ctx_index_lock;
75 static __u64 __ctx_index;
76
77 __u64 gss_get_next_ctx_index(void)
78 {
79         __u64 idx;
80
81         spin_lock(&__ctx_index_lock);
82         idx = __ctx_index++;
83         spin_unlock(&__ctx_index_lock);
84
85         return idx;
86 }
87
88 static inline unsigned long hash_mem(char *buf, int length, int bits)
89 {
90         unsigned long hash = 0;
91         unsigned long l = 0;
92         int len = 0;
93         unsigned char c;
94
95         do {
96                 if (len == length) {
97                         c = (char) len;
98                         len = -1;
99                 } else
100                         c = *buf++;
101
102                 l = (l << 8) | c;
103                 len++;
104
105                 if ((len & (BITS_PER_LONG/8-1)) == 0)
106                         hash = hash_long(hash^l, BITS_PER_LONG);
107         } while (len);
108
109         return hash >> (BITS_PER_LONG - bits);
110 }
111
112 /* This compatibility can be removed once kernel 3.3 is used,
113  * since cache_register_net/cache_unregister_net are exported.
114  * Note that since kernel 3.4 cache_register and cache_unregister
115  * are removed.
116 */
117 static inline int _cache_register_net(struct cache_detail *cd, struct net *net)
118 {
119 #ifdef HAVE_CACHE_REGISTER
120         return cache_register(cd);
121 #else
122         return cache_register_net(cd, net);
123 #endif
124 }
125 static inline void _cache_unregister_net(struct cache_detail *cd,
126                                          struct net *net)
127 {
128 #ifdef HAVE_CACHE_REGISTER
129         cache_unregister(cd);
130 #else
131         cache_unregister_net(cd, net);
132 #endif
133 }
134 /****************************************
135  * rpc sec init (rsi) cache *
136  ****************************************/
137
138 #define RSI_HASHBITS    (6)
139 #define RSI_HASHMAX     (1 << RSI_HASHBITS)
140 #define RSI_HASHMASK    (RSI_HASHMAX - 1)
141
142 struct rsi {
143         struct cache_head       h;
144         __u32                   lustre_svc;
145         __u64                   nid;
146         char                    nm_name[LUSTRE_NODEMAP_NAME_LENGTH + 1];
147         wait_queue_head_t       waitq;
148         rawobj_t                in_handle, in_token;
149         rawobj_t                out_handle, out_token;
150         int                     major_status, minor_status;
151 };
152
153 #ifdef HAVE_CACHE_HEAD_HLIST
154 static struct hlist_head rsi_table[RSI_HASHMAX];
155 #else
156 static struct cache_head *rsi_table[RSI_HASHMAX];
157 #endif
158 static struct cache_detail rsi_cache;
159 static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
160 static struct rsi *rsi_lookup(struct rsi *item);
161
162 static inline int rsi_hash(struct rsi *item)
163 {
164         return hash_mem((char *)item->in_handle.data, item->in_handle.len,
165                         RSI_HASHBITS) ^
166                hash_mem((char *)item->in_token.data, item->in_token.len,
167                         RSI_HASHBITS);
168 }
169
170 static inline int __rsi_match(struct rsi *item, struct rsi *tmp)
171 {
172         return (rawobj_equal(&item->in_handle, &tmp->in_handle) &&
173                 rawobj_equal(&item->in_token, &tmp->in_token));
174 }
175
176 static void rsi_free(struct rsi *rsi)
177 {
178         rawobj_free(&rsi->in_handle);
179         rawobj_free(&rsi->in_token);
180         rawobj_free(&rsi->out_handle);
181         rawobj_free(&rsi->out_token);
182 }
183
184 /* See handle_channel_req() userspace for where the upcall data is read */
185 static void rsi_request(struct cache_detail *cd,
186                         struct cache_head *h,
187                         char **bpp, int *blen)
188 {
189         struct rsi *rsi = container_of(h, struct rsi, h);
190         __u64 index = 0;
191
192         /* if in_handle is null, provide kernel suggestion */
193         if (rsi->in_handle.len == 0)
194                 index = gss_get_next_ctx_index();
195
196         qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
197                         sizeof(rsi->lustre_svc));
198         qword_addhex(bpp, blen, (char *) &rsi->nid, sizeof(rsi->nid));
199         qword_addhex(bpp, blen, (char *) &index, sizeof(index));
200         qword_addhex(bpp, blen, (char *) rsi->nm_name,
201                      strlen(rsi->nm_name) + 1);
202         qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
203         qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
204         (*bpp)[-1] = '\n';
205 }
206
207 #ifdef HAVE_SUNRPC_UPCALL_HAS_3ARGS
208 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
209 {
210         return sunrpc_cache_pipe_upcall(cd, h, rsi_request);
211 }
212 #else
213
214 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
215 {
216         return sunrpc_cache_pipe_upcall(cd, h);
217 }
218 #endif
219
220 static inline void __rsi_init(struct rsi *new, struct rsi *item)
221 {
222         new->out_handle = RAWOBJ_EMPTY;
223         new->out_token = RAWOBJ_EMPTY;
224
225         new->in_handle = item->in_handle;
226         item->in_handle = RAWOBJ_EMPTY;
227         new->in_token = item->in_token;
228         item->in_token = RAWOBJ_EMPTY;
229
230         new->lustre_svc = item->lustre_svc;
231         new->nid = item->nid;
232         memcpy(new->nm_name, item->nm_name, sizeof(item->nm_name));
233         init_waitqueue_head(&new->waitq);
234 }
235
236 static inline void __rsi_update(struct rsi *new, struct rsi *item)
237 {
238         LASSERT(new->out_handle.len == 0);
239         LASSERT(new->out_token.len == 0);
240
241         new->out_handle = item->out_handle;
242         item->out_handle = RAWOBJ_EMPTY;
243         new->out_token = item->out_token;
244         item->out_token = RAWOBJ_EMPTY;
245
246         new->major_status = item->major_status;
247         new->minor_status = item->minor_status;
248 }
249
250 static void rsi_put(struct kref *ref)
251 {
252         struct rsi *rsi = container_of(ref, struct rsi, h.ref);
253
254 #ifdef HAVE_CACHE_HEAD_HLIST
255         LASSERT(rsi->h.cache_list.next == NULL);
256 #else
257         LASSERT(rsi->h.next == NULL);
258 #endif
259         rsi_free(rsi);
260         OBD_FREE_PTR(rsi);
261 }
262
263 static int rsi_match(struct cache_head *a, struct cache_head *b)
264 {
265         struct rsi *item = container_of(a, struct rsi, h);
266         struct rsi *tmp = container_of(b, struct rsi, h);
267
268         return __rsi_match(item, tmp);
269 }
270
271 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
272 {
273         struct rsi *new = container_of(cnew, struct rsi, h);
274         struct rsi *item = container_of(citem, struct rsi, h);
275
276         __rsi_init(new, item);
277 }
278
279 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
280 {
281         struct rsi *new = container_of(cnew, struct rsi, h);
282         struct rsi *item = container_of(citem, struct rsi, h);
283
284         __rsi_update(new, item);
285 }
286
287 static struct cache_head *rsi_alloc(void)
288 {
289         struct rsi *rsi;
290
291         OBD_ALLOC_PTR(rsi);
292         if (rsi) 
293                 return &rsi->h;
294         else
295                 return NULL;
296 }
297
298 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
299 {
300         char           *buf = mesg;
301         int             len;
302         struct rsi      rsii, *rsip = NULL;
303         time_t          expiry;
304         int             status = -EINVAL;
305         ENTRY;
306
307
308         memset(&rsii, 0, sizeof(rsii));
309
310         /* handle */
311         len = qword_get(&mesg, buf, mlen);
312         if (len < 0)
313                 goto out;
314         if (rawobj_alloc(&rsii.in_handle, buf, len)) {
315                 status = -ENOMEM;
316                 goto out;
317         }
318
319         /* token */
320         len = qword_get(&mesg, buf, mlen);
321         if (len < 0)
322                 goto out;
323         if (rawobj_alloc(&rsii.in_token, buf, len)) {
324                 status = -ENOMEM;
325                 goto out;
326         }
327
328         rsip = rsi_lookup(&rsii);
329         if (!rsip)
330                 goto out;
331
332         rsii.h.flags = 0;
333         /* expiry */
334         expiry = get_expiry(&mesg);
335         if (expiry == 0)
336                 goto out;
337
338         len = qword_get(&mesg, buf, mlen);
339         if (len <= 0)
340                 goto out;
341
342         /* major */
343         status = kstrtoint(buf, 10, &rsii.major_status);
344         if (status)
345                 goto out;
346
347         /* minor */
348         len = qword_get(&mesg, buf, mlen);
349         if (len <= 0) {
350                 status = -EINVAL;
351                 goto out;
352         }
353
354         status = kstrtoint(buf, 10, &rsii.minor_status);
355         if (status)
356                 goto out;
357
358         /* out_handle */
359         len = qword_get(&mesg, buf, mlen);
360         if (len < 0)
361                 goto out;
362         if (rawobj_alloc(&rsii.out_handle, buf, len)) {
363                 status = -ENOMEM;
364                 goto out;
365         }
366
367         /* out_token */
368         len = qword_get(&mesg, buf, mlen);
369         if (len < 0)
370                 goto out;
371         if (rawobj_alloc(&rsii.out_token, buf, len)) {
372                 status = -ENOMEM;
373                 goto out;
374         }
375
376         rsii.h.expiry_time = expiry;
377         rsip = rsi_update(&rsii, rsip);
378         status = 0;
379 out:
380         rsi_free(&rsii);
381         if (rsip) {
382                 wake_up_all(&rsip->waitq);
383                 cache_put(&rsip->h, &rsi_cache);
384         } else {
385                 status = -ENOMEM;
386         }
387
388         if (status)
389                 CERROR("rsi parse error %d\n", status);
390         RETURN(status);
391 }
392
393 static struct cache_detail rsi_cache = {
394         .hash_size      = RSI_HASHMAX,
395         .hash_table     = rsi_table,
396         .name           = "auth.sptlrpc.init",
397         .cache_put      = rsi_put,
398 #ifndef HAVE_SUNRPC_UPCALL_HAS_3ARGS
399         .cache_request  = rsi_request,
400 #endif
401         .cache_upcall   = rsi_upcall,
402         .cache_parse    = rsi_parse,
403         .match          = rsi_match,
404         .init           = rsi_init,
405         .update         = update_rsi,
406         .alloc          = rsi_alloc,
407 };
408
409 static struct rsi *rsi_lookup(struct rsi *item)
410 {
411         struct cache_head *ch;
412         int hash = rsi_hash(item);
413
414         ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
415         if (ch)
416                 return container_of(ch, struct rsi, h);
417         else
418                 return NULL;
419 }
420
421 static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
422 {
423         struct cache_head *ch;
424         int hash = rsi_hash(new);
425
426         ch = sunrpc_cache_update(&rsi_cache, &new->h, &old->h, hash);
427         if (ch)
428                 return container_of(ch, struct rsi, h);
429         else
430                 return NULL;
431 }
432
433 /****************************************
434  * rpc sec context (rsc) cache                            *
435  ****************************************/
436
437 #define RSC_HASHBITS    (10)
438 #define RSC_HASHMAX     (1 << RSC_HASHBITS)
439 #define RSC_HASHMASK    (RSC_HASHMAX - 1)
440
441 struct rsc {
442         struct cache_head       h;
443         struct obd_device      *target;
444         rawobj_t                handle;
445         struct gss_svc_ctx      ctx;
446 };
447
448 #ifdef HAVE_CACHE_HEAD_HLIST
449 static struct hlist_head rsc_table[RSC_HASHMAX];
450 #else
451 static struct cache_head *rsc_table[RSC_HASHMAX];
452 #endif
453 static struct cache_detail rsc_cache;
454 static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
455 static struct rsc *rsc_lookup(struct rsc *item);
456
457 static void rsc_free(struct rsc *rsci)
458 {
459         rawobj_free(&rsci->handle);
460         rawobj_free(&rsci->ctx.gsc_rvs_hdl);
461         lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
462 }
463
464 static inline int rsc_hash(struct rsc *rsci)
465 {
466         return hash_mem((char *)rsci->handle.data,
467                         rsci->handle.len, RSC_HASHBITS);
468 }
469
470 static inline int __rsc_match(struct rsc *new, struct rsc *tmp)
471 {
472         return rawobj_equal(&new->handle, &tmp->handle);
473 }
474
475 static inline void __rsc_init(struct rsc *new, struct rsc *tmp)
476 {
477         new->handle = tmp->handle;
478         tmp->handle = RAWOBJ_EMPTY;
479
480         new->target = NULL;
481         memset(&new->ctx, 0, sizeof(new->ctx));
482         new->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
483 }
484
485 static inline void __rsc_update(struct rsc *new, struct rsc *tmp)
486 {
487         new->ctx = tmp->ctx;
488         tmp->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
489         tmp->ctx.gsc_mechctx = NULL;
490
491         memset(&new->ctx.gsc_seqdata, 0, sizeof(new->ctx.gsc_seqdata));
492         spin_lock_init(&new->ctx.gsc_seqdata.ssd_lock);
493 }
494
495 static void rsc_put(struct kref *ref)
496 {
497         struct rsc *rsci = container_of(ref, struct rsc, h.ref);
498
499 #ifdef HAVE_CACHE_HEAD_HLIST
500         LASSERT(rsci->h.cache_list.next == NULL);
501 #else
502         LASSERT(rsci->h.next == NULL);
503 #endif
504         rsc_free(rsci);
505         OBD_FREE_PTR(rsci);
506 }
507
508 static int rsc_match(struct cache_head *a, struct cache_head *b)
509 {
510         struct rsc *new = container_of(a, struct rsc, h);
511         struct rsc *tmp = container_of(b, struct rsc, h);
512
513         return __rsc_match(new, tmp);
514 }
515
516 static void rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
517 {
518         struct rsc *new = container_of(cnew, struct rsc, h);
519         struct rsc *tmp = container_of(ctmp, struct rsc, h);
520
521         __rsc_init(new, tmp);
522 }
523
524 static void update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
525 {
526         struct rsc *new = container_of(cnew, struct rsc, h);
527         struct rsc *tmp = container_of(ctmp, struct rsc, h);
528
529         __rsc_update(new, tmp);
530 }
531
532 static struct cache_head * rsc_alloc(void)
533 {
534         struct rsc *rsc;
535
536         OBD_ALLOC_PTR(rsc);
537         if (rsc)
538                 return &rsc->h;
539         else
540                 return NULL;
541 }
542
543 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
544 {
545         char                *buf = mesg;
546         int                  len, rv, tmp_int;
547         struct rsc           rsci, *rscp = NULL;
548         time_t               expiry;
549         int                  status = -EINVAL;
550         struct gss_api_mech *gm = NULL;
551
552         memset(&rsci, 0, sizeof(rsci));
553
554         /* context handle */
555         len = qword_get(&mesg, buf, mlen);
556         if (len < 0) goto out;
557         status = -ENOMEM;
558         if (rawobj_alloc(&rsci.handle, buf, len))
559                 goto out;
560
561         rsci.h.flags = 0;
562         /* expiry */
563         expiry = get_expiry(&mesg);
564         status = -EINVAL;
565         if (expiry == 0)
566                 goto out;
567
568         /* remote flag */
569         rv = get_int(&mesg, &tmp_int);
570         if (rv) {
571                 CERROR("fail to get remote flag\n");
572                 goto out;
573         }
574         rsci.ctx.gsc_remote = (tmp_int != 0);
575
576         /* root user flag */
577         rv = get_int(&mesg, &tmp_int);
578         if (rv) {
579                 CERROR("fail to get root user flag\n");
580                 goto out;
581         }
582         rsci.ctx.gsc_usr_root = (tmp_int != 0);
583
584         /* mds user flag */
585         rv = get_int(&mesg, &tmp_int);
586         if (rv) {
587                 CERROR("fail to get mds user flag\n");
588                 goto out;
589         }
590         rsci.ctx.gsc_usr_mds = (tmp_int != 0);
591
592         /* oss user flag */
593         rv = get_int(&mesg, &tmp_int);
594         if (rv) {
595                 CERROR("fail to get oss user flag\n");
596                 goto out;
597         }
598         rsci.ctx.gsc_usr_oss = (tmp_int != 0);
599
600         /* mapped uid */
601         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
602         if (rv) {
603                 CERROR("fail to get mapped uid\n");
604                 goto out;
605         }
606
607         rscp = rsc_lookup(&rsci);
608         if (!rscp)
609                 goto out;
610
611         /* uid, or NEGATIVE */
612         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
613         if (rv == -EINVAL)
614                 goto out;
615         if (rv == -ENOENT) {
616                 CERROR("NOENT? set rsc entry negative\n");
617                 set_bit(CACHE_NEGATIVE, &rsci.h.flags);
618         } else {
619                 rawobj_t tmp_buf;
620                 time64_t ctx_expiry;
621
622                 /* gid */
623                 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
624                         goto out;
625
626                 /* mech name */
627                 len = qword_get(&mesg, buf, mlen);
628                 if (len < 0)
629                         goto out;
630                 gm = lgss_name_to_mech(buf);
631                 status = -EOPNOTSUPP;
632                 if (!gm)
633                         goto out;
634
635                 status = -EINVAL;
636                 /* mech-specific data: */
637                 len = qword_get(&mesg, buf, mlen);
638                 if (len < 0)
639                         goto out;
640
641                 tmp_buf.len = len;
642                 tmp_buf.data = (unsigned char *)buf;
643                 if (lgss_import_sec_context(&tmp_buf, gm,
644                                             &rsci.ctx.gsc_mechctx))
645                         goto out;
646
647                 /* set to seconds since machine booted */
648                 expiry = ktime_get_seconds();
649
650                 /* currently the expiry time passed down from user-space
651                  * is invalid, here we retrive it from mech.
652                  */
653                 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
654                         CERROR("unable to get expire time, drop it\n");
655                         goto out;
656                 }
657
658                 /* ctx_expiry is the number of seconds since Jan 1 1970.
659                  * We want just the  number of seconds into the future.
660                  */
661                 expiry += ctx_expiry - ktime_get_real_seconds();
662         }
663
664         rsci.h.expiry_time = expiry;
665         rscp = rsc_update(&rsci, rscp);
666         status = 0;
667 out:
668         if (gm)
669                 lgss_mech_put(gm);
670         rsc_free(&rsci);
671         if (rscp)
672                 cache_put(&rscp->h, &rsc_cache);
673         else
674                 status = -ENOMEM;
675
676         if (status)
677                 CERROR("parse rsc error %d\n", status);
678         return status;
679 }
680
681 static struct cache_detail rsc_cache = {
682         .hash_size      = RSC_HASHMAX,
683         .hash_table     = rsc_table,
684         .name           = "auth.sptlrpc.context",
685         .cache_put      = rsc_put,
686         .cache_parse    = rsc_parse,
687         .match          = rsc_match,
688         .init           = rsc_init,
689         .update         = update_rsc,
690         .alloc          = rsc_alloc,
691 };
692
693 static struct rsc *rsc_lookup(struct rsc *item)
694 {
695         struct cache_head *ch;
696         int                hash = rsc_hash(item);
697
698         ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
699         if (ch)
700                 return container_of(ch, struct rsc, h);
701         else
702                 return NULL;
703 }
704
705 static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
706 {
707         struct cache_head *ch;
708         int                hash = rsc_hash(new);
709
710         ch = sunrpc_cache_update(&rsc_cache, &new->h, &old->h, hash);
711         if (ch)
712                 return container_of(ch, struct rsc, h);
713         else
714                 return NULL;
715 }
716
717 #define COMPAT_RSC_PUT(item, cd)        cache_put((item), (cd))
718
719 /****************************************
720  * rsc cache flush                      *
721  ****************************************/
722
723 typedef int rsc_entry_match(struct rsc *rscp, long data);
724
725 static void rsc_flush(rsc_entry_match *match, long data)
726 {
727 #ifdef HAVE_CACHE_HEAD_HLIST
728         struct cache_head *ch = NULL;
729         struct hlist_head *head;
730 #else
731         struct cache_head **ch;
732 #endif
733         struct rsc *rscp;
734         int n;
735         ENTRY;
736
737         write_lock(&rsc_cache.hash_lock);
738         for (n = 0; n < RSC_HASHMAX; n++) {
739 #ifdef HAVE_CACHE_HEAD_HLIST
740                 head = &rsc_cache.hash_table[n];
741                 hlist_for_each_entry(ch, head, cache_list) {
742                         rscp = container_of(ch, struct rsc, h);
743 #else
744                 for (ch = &rsc_cache.hash_table[n]; *ch;) {
745                         rscp = container_of(*ch, struct rsc, h);
746 #endif
747
748                         if (!match(rscp, data)) {
749 #ifndef HAVE_CACHE_HEAD_HLIST
750                                 ch = &((*ch)->next);
751 #endif
752                                 continue;
753                         }
754
755                         /* it seems simply set NEGATIVE doesn't work */
756 #ifdef HAVE_CACHE_HEAD_HLIST
757                         hlist_del_init(&ch->cache_list);
758 #else
759                         *ch = (*ch)->next;
760                         rscp->h.next = NULL;
761 #endif
762                         cache_get(&rscp->h);
763                         set_bit(CACHE_NEGATIVE, &rscp->h.flags);
764                         COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
765                         rsc_cache.entries--;
766                 }
767         }
768         write_unlock(&rsc_cache.hash_lock);
769         EXIT;
770 }
771
772 static int match_uid(struct rsc *rscp, long uid)
773 {
774         if ((int) uid == -1)
775                 return 1;
776         return ((int) rscp->ctx.gsc_uid == (int) uid);
777 }
778
779 static int match_target(struct rsc *rscp, long target)
780 {
781         return (rscp->target == (struct obd_device *) target);
782 }
783
784 static inline void rsc_flush_uid(int uid)
785 {
786         if (uid == -1)
787                 CWARN("flush all gss contexts...\n");
788
789         rsc_flush(match_uid, (long) uid);
790 }
791
792 static inline void rsc_flush_target(struct obd_device *target)
793 {
794         rsc_flush(match_target, (long) target);
795 }
796
797 void gss_secsvc_flush(struct obd_device *target)
798 {
799         rsc_flush_target(target);
800 }
801
802 static struct rsc *gss_svc_searchbyctx(rawobj_t *handle)
803 {
804         struct rsc  rsci;
805         struct rsc *found;
806
807         memset(&rsci, 0, sizeof(rsci));
808         if (rawobj_dup(&rsci.handle, handle))
809                 return NULL;
810
811         found = rsc_lookup(&rsci);
812         rsc_free(&rsci);
813         if (!found)
814                 return NULL;
815         if (cache_check(&rsc_cache, &found->h, NULL))
816                 return NULL;
817         return found;
818 }
819
820 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
821                                    struct gss_sec *gsec,
822                                    struct gss_cli_ctx *gctx)
823 {
824         struct rsc      rsci, *rscp = NULL;
825         time64_t ctx_expiry;
826         __u32           major;
827         int             rc;
828         ENTRY;
829
830         memset(&rsci, 0, sizeof(rsci));
831
832         if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
833                          sizeof(gsec->gs_rvs_hdl)))
834                 GOTO(out, rc = -ENOMEM);
835
836         rscp = rsc_lookup(&rsci);
837         if (rscp == NULL)
838                 GOTO(out, rc = -ENOMEM);
839
840         major = lgss_copy_reverse_context(gctx->gc_mechctx,
841                                           &rsci.ctx.gsc_mechctx);
842         if (major != GSS_S_COMPLETE)
843                 GOTO(out, rc = -ENOMEM);
844
845         if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
846                 CERROR("unable to get expire time, drop it\n");
847                 GOTO(out, rc = -EINVAL);
848         }
849         rsci.h.expiry_time = (time_t) ctx_expiry;
850
851         switch (imp->imp_obd->u.cli.cl_sp_to) {
852         case LUSTRE_SP_MDT:
853                 rsci.ctx.gsc_usr_mds = 1;
854                 break;
855         case LUSTRE_SP_OST:
856                 rsci.ctx.gsc_usr_oss = 1;
857                 break;
858         case LUSTRE_SP_CLI:
859                 rsci.ctx.gsc_usr_root = 1;
860                 break;
861         case LUSTRE_SP_MGS:
862                 /* by convention, all 3 set to 1 means MGS */
863                 rsci.ctx.gsc_usr_mds = 1;
864                 rsci.ctx.gsc_usr_oss = 1;
865                 rsci.ctx.gsc_usr_root = 1;
866                 break;
867         default:
868                 break;
869         }
870
871         rscp = rsc_update(&rsci, rscp);
872         if (rscp == NULL)
873                 GOTO(out, rc = -ENOMEM);
874
875         rscp->target = imp->imp_obd;
876         rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
877
878         CWARN("create reverse svc ctx %p to %s: idx %#llx\n",
879               &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
880         rc = 0;
881 out:
882         if (rscp)
883                 cache_put(&rscp->h, &rsc_cache);
884         rsc_free(&rsci);
885
886         if (rc)
887                 CERROR("create reverse svc ctx: idx %#llx, rc %d\n",
888                        gsec->gs_rvs_hdl, rc);
889         RETURN(rc);
890 }
891
892 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle)
893 {
894         const time64_t expire = 20;
895         struct rsc *rscp;
896
897         rscp = gss_svc_searchbyctx(handle);
898         if (rscp) {
899                 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) expire soon\n",
900                        &rscp->ctx, rscp);
901
902                 rscp->h.expiry_time = ktime_get_real_seconds() + expire;
903                 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
904         }
905         return 0;
906 }
907
908 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx)
909 {
910         struct rsc *rscp = container_of(ctx, struct rsc, ctx);
911
912         return rawobj_dup(handle, &rscp->handle);
913 }
914
915 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq)
916 {
917         struct rsc             *rscp;
918
919         rscp = gss_svc_searchbyctx(handle);
920         if (rscp) {
921                 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) update seq to %u\n",
922                        &rscp->ctx, rscp, seq + 1);
923
924                 rscp->ctx.gsc_rvs_seq = seq + 1;
925                 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
926         }
927         return 0;
928 }
929
930 static struct cache_deferred_req* cache_upcall_defer(struct cache_req *req)
931 {
932         return NULL;
933 }
934 static struct cache_req cache_upcall_chandle = { cache_upcall_defer };
935
936 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
937                                struct gss_svc_reqctx *grctx,
938                                struct gss_wire_ctx *gw,
939                                struct obd_device *target,
940                                __u32 lustre_svc,
941                                rawobj_t *rvs_hdl,
942                                rawobj_t *in_token)
943 {
944         struct ptlrpc_reply_state *rs;
945         struct rsc                *rsci = NULL;
946         struct rsi                *rsip = NULL, rsikey;
947         wait_queue_entry_t wait;
948         int                        replen = sizeof(struct ptlrpc_body);
949         struct gss_rep_header     *rephdr;
950         int                        first_check = 1;
951         int                        rc = SECSVC_DROP;
952         ENTRY;
953
954         memset(&rsikey, 0, sizeof(rsikey));
955         rsikey.lustre_svc = lustre_svc;
956         /* In case of MR, rq_peer is not the NID from which request is received,
957          * but primary NID of peer.
958          * So we need rq_source, which contains the NID actually in use.
959          */
960         rsikey.nid = (__u64) req->rq_source.nid;
961         nodemap_test_nid(req->rq_peer.nid, rsikey.nm_name,
962                          sizeof(rsikey.nm_name));
963
964         /* duplicate context handle. for INIT it always 0 */
965         if (rawobj_dup(&rsikey.in_handle, &gw->gw_handle)) {
966                 CERROR("fail to dup context handle\n");
967                 GOTO(out, rc);
968         }
969
970         if (rawobj_dup(&rsikey.in_token, in_token)) {
971                 CERROR("can't duplicate token\n");
972                 rawobj_free(&rsikey.in_handle);
973                 GOTO(out, rc);
974         }
975
976         rsip = rsi_lookup(&rsikey);
977         rsi_free(&rsikey);
978         if (!rsip) {
979                 CERROR("error in rsi_lookup.\n");
980
981                 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
982                         rc = SECSVC_COMPLETE;
983
984                 GOTO(out, rc);
985         }
986
987         cache_get(&rsip->h); /* take an extra ref */
988         init_waitqueue_head(&rsip->waitq);
989         init_waitqueue_entry(&wait, current);
990         add_wait_queue(&rsip->waitq, &wait);
991
992 cache_check:
993         /* Note each time cache_check() will drop a reference if return
994          * non-zero. We hold an extra reference on initial rsip, but must
995          * take care of following calls. */
996         rc = cache_check(&rsi_cache, &rsip->h, &cache_upcall_chandle);
997         switch (rc) {
998         case -ETIMEDOUT:
999         case -EAGAIN: {
1000                 int valid;
1001
1002                 if (first_check) {
1003                         first_check = 0;
1004
1005                         read_lock(&rsi_cache.hash_lock);
1006                         valid = test_bit(CACHE_VALID, &rsip->h.flags);
1007                         if (valid == 0)
1008                                 set_current_state(TASK_INTERRUPTIBLE);
1009                         read_unlock(&rsi_cache.hash_lock);
1010
1011                         if (valid == 0) {
1012                                 unsigned long jiffies;
1013                                 jiffies = msecs_to_jiffies(MSEC_PER_SEC *
1014                                           GSS_SVC_UPCALL_TIMEOUT);
1015                                 schedule_timeout(jiffies);
1016                         }
1017                         cache_get(&rsip->h);
1018                         goto cache_check;
1019                 }
1020                 CWARN("waited %ds timeout, drop\n", GSS_SVC_UPCALL_TIMEOUT);
1021                 break;
1022         }
1023         case -ENOENT:
1024                 CDEBUG(D_SEC, "cache_check return ENOENT, drop\n");
1025                 break;
1026         case 0:
1027                 /* if not the first check, we have to release the extra
1028                  * reference we just added on it. */
1029                 if (!first_check)
1030                         cache_put(&rsip->h, &rsi_cache);
1031                 CDEBUG(D_SEC, "cache_check is good\n");
1032                 break;
1033         }
1034
1035         remove_wait_queue(&rsip->waitq, &wait);
1036         cache_put(&rsip->h, &rsi_cache);
1037
1038         if (rc)
1039                 GOTO(out, rc = SECSVC_DROP);
1040
1041         rc = SECSVC_DROP;
1042         rsci = gss_svc_searchbyctx(&rsip->out_handle);
1043         if (!rsci) {
1044                 CERROR("authentication failed\n");
1045
1046                 /* gss mechanism returned major and minor code so we return
1047                  * those in error message */
1048                 if (!gss_pack_err_notify(req, rsip->major_status,
1049                                          rsip->minor_status))
1050                         rc = SECSVC_COMPLETE;
1051
1052                 GOTO(out, rc);
1053         } else {
1054                 cache_get(&rsci->h);
1055                 grctx->src_ctx = &rsci->ctx;
1056         }
1057
1058         if (rawobj_dup(&rsci->ctx.gsc_rvs_hdl, rvs_hdl)) {
1059                 CERROR("failed duplicate reverse handle\n");
1060                 GOTO(out, rc);
1061         }
1062
1063         rsci->target = target;
1064
1065         CDEBUG(D_SEC, "server create rsc %p(%u->%s)\n",
1066                rsci, rsci->ctx.gsc_uid, libcfs_nid2str(req->rq_peer.nid));
1067
1068         if (rsip->out_handle.len > PTLRPC_GSS_MAX_HANDLE_SIZE) {
1069                 CERROR("handle size %u too large\n", rsip->out_handle.len);
1070                 GOTO(out, rc = SECSVC_DROP);
1071         }
1072
1073         grctx->src_init = 1;
1074         grctx->src_reserve_len = cfs_size_round4(rsip->out_token.len);
1075
1076         rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1077         if (rc) {
1078                 CERROR("failed to pack reply: %d\n", rc);
1079                 GOTO(out, rc = SECSVC_DROP);
1080         }
1081
1082         rs = req->rq_reply_state;
1083         LASSERT(rs->rs_repbuf->lm_bufcount == 3);
1084         LASSERT(rs->rs_repbuf->lm_buflens[0] >=
1085                 sizeof(*rephdr) + rsip->out_handle.len);
1086         LASSERT(rs->rs_repbuf->lm_buflens[2] >= rsip->out_token.len);
1087
1088         rephdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1089         rephdr->gh_version = PTLRPC_GSS_VERSION;
1090         rephdr->gh_flags = 0;
1091         rephdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1092         rephdr->gh_major = rsip->major_status;
1093         rephdr->gh_minor = rsip->minor_status;
1094         rephdr->gh_seqwin = GSS_SEQ_WIN;
1095         rephdr->gh_handle.len = rsip->out_handle.len;
1096         memcpy(rephdr->gh_handle.data, rsip->out_handle.data,
1097                rsip->out_handle.len);
1098
1099         memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0), rsip->out_token.data,
1100                rsip->out_token.len);
1101
1102         rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
1103                                                rsip->out_token.len, 0);
1104
1105         rc = SECSVC_OK;
1106
1107 out:
1108         /* it looks like here we should put rsip also, but this mess up
1109          * with NFS cache mgmt code... FIXME
1110          * something like:
1111          * if (rsip)
1112          *     rsi_put(&rsip->h, &rsi_cache); */
1113
1114         if (rsci) {
1115                 /* if anything went wrong, we don't keep the context too */
1116                 if (rc != SECSVC_OK)
1117                         set_bit(CACHE_NEGATIVE, &rsci->h.flags);
1118                 else
1119                         CDEBUG(D_SEC, "create rsc with idx %#llx\n",
1120                                gss_handle_to_u64(&rsci->handle));
1121
1122                 COMPAT_RSC_PUT(&rsci->h, &rsc_cache);
1123         }
1124         RETURN(rc);
1125 }
1126
1127 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
1128                                            struct gss_wire_ctx *gw)
1129 {
1130         struct rsc *rsc;
1131
1132         rsc = gss_svc_searchbyctx(&gw->gw_handle);
1133         if (!rsc) {
1134                 CWARN("Invalid gss ctx idx %#llx from %s\n",
1135                       gss_handle_to_u64(&gw->gw_handle),
1136                       libcfs_nid2str(req->rq_peer.nid));
1137                 return NULL;
1138         }
1139
1140         return &rsc->ctx;
1141 }
1142
1143 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx)
1144 {
1145         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1146
1147         COMPAT_RSC_PUT(&rsc->h, &rsc_cache);
1148 }
1149
1150 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx)
1151 {
1152         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1153
1154         /* can't be found */
1155         set_bit(CACHE_NEGATIVE, &rsc->h.flags);
1156         /* to be removed at next scan */
1157         rsc->h.expiry_time = 1;
1158 }
1159
1160 int __init gss_init_svc_upcall(void)
1161 {
1162         int     i, rc;
1163
1164         spin_lock_init(&__ctx_index_lock);
1165         /*
1166          * this helps reducing context index confliction. after server reboot,
1167          * conflicting request from clients might be filtered out by initial
1168          * sequence number checking, thus no chance to sent error notification
1169          * back to clients.
1170          */
1171         cfs_get_random_bytes(&__ctx_index, sizeof(__ctx_index));
1172
1173         rc = _cache_register_net(&rsi_cache, &init_net);
1174         if (rc != 0)
1175                 return rc;
1176
1177         rc = _cache_register_net(&rsc_cache, &init_net);
1178         if (rc != 0) {
1179                 _cache_unregister_net(&rsi_cache, &init_net);
1180                 return rc;
1181         }
1182
1183         /* FIXME this looks stupid. we intend to give lsvcgssd a chance to open
1184          * the init upcall channel, otherwise there's big chance that the first
1185          * upcall issued before the channel be opened thus nfsv4 cache code will
1186          * drop the request directly, thus lead to unnecessary recovery time.
1187          * Here we wait at minimum 1.5 seconds.
1188          */
1189         for (i = 0; i < 6; i++) {
1190                 if (atomic_read(&rsi_cache.readers) > 0)
1191                         break;
1192                 set_current_state(TASK_UNINTERRUPTIBLE);
1193                 LASSERT(msecs_to_jiffies(MSEC_PER_SEC / 4) > 0);
1194                 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC / 4));
1195         }
1196
1197         if (atomic_read(&rsi_cache.readers) == 0)
1198                 CWARN("Init channel is not opened by lsvcgssd, following "
1199                       "request might be dropped until lsvcgssd is active\n");
1200
1201         return 0;
1202 }
1203
1204 void gss_exit_svc_upcall(void)
1205 {
1206         cache_purge(&rsi_cache);
1207         _cache_unregister_net(&rsi_cache, &init_net);
1208
1209         cache_purge(&rsc_cache);
1210         _cache_unregister_net(&rsc_cache, &init_net);
1211 }