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