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