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