Whamcloud - gitweb
- make HEAD from b_post_cmd3
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_svc_upcall.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Modifications for Lustre
5  * Copyright 2004 - 2006, Cluster File Systems, Inc.
6  * All rights reserved
7  * Author: Eric Mei <ericm@clusterfs.com>
8  */
9
10 /*
11  * Neil Brown <neilb@cse.unsw.edu.au>
12  * J. Bruce Fields <bfields@umich.edu>
13  * Andy Adamson <andros@umich.edu>
14  * Dug Song <dugsong@monkey.org>
15  *
16  * RPCSEC_GSS server authentication.
17  * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
18  * (gssapi)
19  *
20  * The RPCSEC_GSS involves three stages:
21  *  1/ context creation
22  *  2/ data exchange
23  *  3/ context destruction
24  *
25  * Context creation is handled largely by upcalls to user-space.
26  *  In particular, GSS_Accept_sec_context is handled by an upcall
27  * Data exchange is handled entirely within the kernel
28  *  In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
29  * Context destruction is handled in-kernel
30  *  GSS_Delete_sec_context is in-kernel
31  *
32  * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
33  * The context handle and gss_token are used as a key into the rpcsec_init cache.
34  * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
35  * being major_status, minor_status, context_handle, reply_token.
36  * These are sent back to the client.
37  * Sequence window management is handled by the kernel.  The window size if currently
38  * a compile time constant.
39  *
40  * When user-space is happy that a context is established, it places an entry
41  * in the rpcsec_context cache. The key for this cache is the context_handle.
42  * The content includes:
43  *   uid/gidlist - for determining access rights
44  *   mechanism type
45  *   mechanism specific information, such as a key
46  *
47  */
48
49 #define DEBUG_SUBSYSTEM S_SEC
50 #ifdef __KERNEL__
51 #include <linux/types.h>
52 #include <linux/init.h>
53 #include <linux/module.h>
54 #include <linux/slab.h>
55 #include <linux/hash.h>
56 #else
57 #include <liblustre.h>
58 #endif
59
60 #include <linux/sunrpc/cache.h>
61
62 #include <obd.h>
63 #include <obd_class.h>
64 #include <obd_support.h>
65 #include <lustre/lustre_idl.h>
66 #include <lustre_net.h>
67 #include <lustre_import.h>
68 #include <lustre_sec.h>
69
70 #include "gss_err.h"
71 #include "gss_internal.h"
72 #include "gss_api.h"
73
74 #define GSS_SVC_UPCALL_TIMEOUT  (20)
75
76 static spinlock_t __ctx_index_lock = SPIN_LOCK_UNLOCKED;
77 static __u64 __ctx_index;
78
79 __u64 gss_get_next_ctx_index(void)
80 {
81         __u64 idx;
82
83         spin_lock(&__ctx_index_lock);
84         idx = __ctx_index++;
85         spin_unlock(&__ctx_index_lock);
86
87         return idx;
88 }
89
90 static inline
91 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 = 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_lookup(struct rsi *item, int set);
136
137 static
138 void rsi_free(struct rsi *rsi)
139 {
140         rawobj_free(&rsi->in_handle);
141         rawobj_free(&rsi->in_token);
142         rawobj_free(&rsi->out_handle);
143         rawobj_free(&rsi->out_token);
144 }
145
146 static
147 void rsi_put(struct cache_head *item, struct cache_detail *cd)
148 {
149         struct rsi *rsi = container_of(item, struct rsi, h);
150
151         LASSERT(atomic_read(&item->refcnt) > 0);
152
153         if (cache_put(item, cd)) {
154                 LASSERT(item->next == NULL);
155                 rsi_free(rsi);
156                 kfree(rsi); /* created by cache mgmt using kmalloc */
157         }
158 }
159
160 static inline
161 int rsi_hash(struct rsi *item)
162 {
163         return hash_mem((char *)item->in_handle.data, item->in_handle.len,
164                         RSI_HASHBITS) ^
165                hash_mem((char *)item->in_token.data, item->in_token.len,
166                         RSI_HASHBITS);
167 }
168
169 static inline
170 int rsi_match(struct rsi *item, struct rsi *tmp)
171 {
172         return (rawobj_equal(&item->in_handle, &tmp->in_handle) &&
173                 rawobj_equal(&item->in_token, &tmp->in_token));
174 }
175
176 static
177 void rsi_request(struct cache_detail *cd,
178                  struct cache_head *h,
179                  char **bpp, int *blen)
180 {
181         struct rsi *rsi = container_of(h, struct rsi, h);
182         __u64 index = 0;
183
184         /* if in_handle is null, provide kernel suggestion */
185         if (rsi->in_handle.len == 0)
186                 index = gss_get_next_ctx_index();
187
188         qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
189                      sizeof(rsi->lustre_svc));
190         qword_addhex(bpp, blen, (char *) &rsi->nid, sizeof(rsi->nid));
191         qword_addhex(bpp, blen, (char *) &index, sizeof(index));
192         qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
193         qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
194         (*bpp)[-1] = '\n';
195 }
196
197 static inline
198 void rsi_init(struct rsi *new, struct rsi *item)
199 {
200         new->out_handle = RAWOBJ_EMPTY;
201         new->out_token = RAWOBJ_EMPTY;
202
203         new->in_handle = item->in_handle;
204         item->in_handle = RAWOBJ_EMPTY;
205         new->in_token = item->in_token;
206         item->in_token = RAWOBJ_EMPTY;
207
208         new->lustre_svc = item->lustre_svc;
209         new->nid = item->nid;
210         init_waitqueue_head(&new->waitq);
211 }
212
213 static inline
214 void rsi_update(struct rsi *new, struct rsi *item)
215 {
216         LASSERT(new->out_handle.len == 0);
217         LASSERT(new->out_token.len == 0);
218
219         new->out_handle = item->out_handle;
220         item->out_handle = RAWOBJ_EMPTY;
221         new->out_token = item->out_token;
222         item->out_token = RAWOBJ_EMPTY;
223
224         new->major_status = item->major_status;
225         new->minor_status = item->minor_status;
226 }
227
228 static
229 int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
230 {
231         char           *buf = mesg;
232         char           *ep;
233         int             len;
234         struct rsi      rsii, *rsip = NULL;
235         time_t          expiry;
236         int             status = -EINVAL;
237         ENTRY;
238
239
240         memset(&rsii, 0, sizeof(rsii));
241
242         /* handle */
243         len = qword_get(&mesg, buf, mlen);
244         if (len < 0)
245                 goto out;
246         if (rawobj_alloc(&rsii.in_handle, buf, len)) {
247                 status = -ENOMEM;
248                 goto out;
249         }
250
251         /* token */
252         len = qword_get(&mesg, buf, mlen);
253         if (len < 0)
254                 goto out;
255         if (rawobj_alloc(&rsii.in_token, buf, len)) {
256                 status = -ENOMEM;
257                 goto out;
258         }
259
260         /* expiry */
261         expiry = get_expiry(&mesg);
262         if (expiry == 0)
263                 goto out;
264
265         len = qword_get(&mesg, buf, mlen);
266         if (len <= 0)
267                 goto out;
268
269         /* major */
270         rsii.major_status = simple_strtol(buf, &ep, 10);
271         if (*ep)
272                 goto out;
273
274         /* minor */
275         len = qword_get(&mesg, buf, mlen);
276         if (len <= 0)
277                 goto out;
278         rsii.minor_status = simple_strtol(buf, &ep, 10);
279         if (*ep)
280                 goto out;
281
282         /* out_handle */
283         len = qword_get(&mesg, buf, mlen);
284         if (len < 0)
285                 goto out;
286         if (rawobj_alloc(&rsii.out_handle, buf, len)) {
287                 status = -ENOMEM;
288                 goto out;
289         }
290
291         /* out_token */
292         len = qword_get(&mesg, buf, mlen);
293         if (len < 0)
294                 goto out;
295         if (rawobj_alloc(&rsii.out_token, buf, len)) {
296                 status = -ENOMEM;
297                 goto out;
298         }
299
300         rsii.h.expiry_time = expiry;
301         rsip = rsi_lookup(&rsii, 1);
302         status = 0;
303 out:
304         rsi_free(&rsii);
305         if (rsip) {
306                 wake_up_all(&rsip->waitq);
307                 rsi_put(&rsip->h, &rsi_cache);
308         }
309
310         if (status)
311                 CERROR("rsi parse error %d\n", status);
312         RETURN(status);
313 }
314
315 static struct cache_detail rsi_cache = {
316         .hash_size      = RSI_HASHMAX,
317         .hash_table     = rsi_table,
318         .name           = "auth.ptlrpcs.init",
319         .cache_put      = rsi_put,
320         .cache_request  = rsi_request,
321         .cache_parse    = rsi_parse,
322 };
323
324 static DefineSimpleCacheLookup(rsi, 0)
325
326 /****************************************
327  * rsc cache                            *
328  ****************************************/
329
330 #define RSC_HASHBITS    (10)
331 #define RSC_HASHMAX     (1 << RSC_HASHBITS)
332 #define RSC_HASHMASK    (RSC_HASHMAX - 1)
333
334 struct rsc {
335         struct cache_head       h;
336         struct obd_device      *target;
337         rawobj_t                handle;
338         struct gss_svc_ctx      ctx;
339 };
340
341 static struct cache_head *rsc_table[RSC_HASHMAX];
342 static struct cache_detail rsc_cache;
343 static struct rsc *rsc_lookup(struct rsc *item, int set);
344
345 static
346 void rsc_free(struct rsc *rsci)
347 {
348         rawobj_free(&rsci->handle);
349         rawobj_free(&rsci->ctx.gsc_rvs_hdl);
350         lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
351 }
352
353 static
354 void rsc_put(struct cache_head *item, struct cache_detail *cd)
355 {
356         struct rsc *rsci = container_of(item, struct rsc, h);
357
358         LASSERT(atomic_read(&item->refcnt) > 0);
359
360         if (cache_put(item, cd)) {
361                 LASSERT(item->next == NULL);
362                 rsc_free(rsci);
363                 kfree(rsci); /* created by cache mgmt using kmalloc */
364         }
365 }
366
367 static inline
368 int rsc_hash(struct rsc *rsci)
369 {
370         return hash_mem((char *)rsci->handle.data,
371                         rsci->handle.len, RSC_HASHBITS);
372 }
373
374 static inline
375 int rsc_match(struct rsc *new, struct rsc *tmp)
376 {
377         return rawobj_equal(&new->handle, &tmp->handle);
378 }
379
380 static inline
381 void rsc_init(struct rsc *new, struct rsc *tmp)
382 {
383         new->handle = tmp->handle;
384         tmp->handle = RAWOBJ_EMPTY;
385
386         new->target = NULL;
387         memset(&new->ctx, 0, sizeof(new->ctx));
388         new->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
389 }
390
391 static inline
392 void rsc_update(struct rsc *new, struct rsc *tmp)
393 {
394         new->ctx = tmp->ctx;
395         tmp->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
396         tmp->ctx.gsc_mechctx = NULL;
397
398         memset(&new->ctx.gsc_seqdata, 0, sizeof(new->ctx.gsc_seqdata));
399         spin_lock_init(&new->ctx.gsc_seqdata.ssd_lock);
400 }
401
402 static
403 int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
404 {
405         char       *buf = mesg;
406         int         len, rv, tmp_int;
407         struct rsc  rsci, *rscp = NULL;
408         time_t      expiry;
409         int         status = -EINVAL;
410
411         memset(&rsci, 0, sizeof(rsci));
412
413         /* context handle */
414         len = qword_get(&mesg, buf, mlen);
415         if (len < 0) goto out;
416         status = -ENOMEM;
417         if (rawobj_alloc(&rsci.handle, buf, len))
418                 goto out;
419
420         rsci.h.flags = 0;
421         /* expiry */
422         expiry = get_expiry(&mesg);
423         status = -EINVAL;
424         if (expiry == 0)
425                 goto out;
426
427         /* remote flag */
428         rv = get_int(&mesg, &tmp_int);
429         if (rv) {
430                 CERROR("fail to get remote flag\n");
431                 goto out;
432         }
433         rsci.ctx.gsc_remote = (tmp_int != 0);
434
435         /* root user flag */
436         rv = get_int(&mesg, &tmp_int);
437         if (rv) {
438                 CERROR("fail to get oss user flag\n");
439                 goto out;
440         }
441         rsci.ctx.gsc_usr_root = (tmp_int != 0);
442
443         /* mds user flag */
444         rv = get_int(&mesg, &tmp_int);
445         if (rv) {
446                 CERROR("fail to get mds user flag\n");
447                 goto out;
448         }
449         rsci.ctx.gsc_usr_mds = (tmp_int != 0);
450
451         /* mapped uid */
452         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
453         if (rv) {
454                 CERROR("fail to get mapped uid\n");
455                 goto out;
456         }
457
458         /* uid, or NEGATIVE */
459         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
460         if (rv == -EINVAL)
461                 goto out;
462         if (rv == -ENOENT) {
463                 CERROR("NOENT? set rsc entry negative\n");
464                 set_bit(CACHE_NEGATIVE, &rsci.h.flags);
465         } else {
466                 struct gss_api_mech *gm;
467                 rawobj_t tmp_buf;
468                 unsigned long ctx_expiry;
469
470                 /* gid */
471                 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
472                         goto out;
473
474                 /* mech name */
475                 len = qword_get(&mesg, buf, mlen);
476                 if (len < 0)
477                         goto out;
478                 gm = lgss_name_to_mech(buf);
479                 status = -EOPNOTSUPP;
480                 if (!gm)
481                         goto out;
482
483                 status = -EINVAL;
484                 /* mech-specific data: */
485                 len = qword_get(&mesg, buf, mlen);
486                 if (len < 0) {
487                         lgss_mech_put(gm);
488                         goto out;
489                 }
490                 tmp_buf.len = len;
491                 tmp_buf.data = (unsigned char *)buf;
492                 if (lgss_import_sec_context(&tmp_buf, gm,
493                                             &rsci.ctx.gsc_mechctx)) {
494                         lgss_mech_put(gm);
495                         goto out;
496                 }
497
498                 /* currently the expiry time passed down from user-space
499                  * is invalid, here we retrive it from mech.
500                  */
501                 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
502                         CERROR("unable to get expire time, drop it\n");
503                         lgss_mech_put(gm);
504                         goto out;
505                 }
506                 expiry = (time_t) ctx_expiry;
507
508                 lgss_mech_put(gm);
509         }
510
511         rsci.h.expiry_time = expiry;
512         rscp = rsc_lookup(&rsci, 1);
513         status = 0;
514 out:
515         rsc_free(&rsci);
516         if (rscp)
517                 rsc_put(&rscp->h, &rsc_cache);
518
519         if (status)
520                 CERROR("parse rsc error %d\n", status);
521         return status;
522 }
523
524 /****************************************
525  * rsc cache flush                      *
526  ****************************************/
527
528 typedef int rsc_entry_match(struct rsc *rscp, long data);
529
530 static
531 void rsc_flush(rsc_entry_match *match, long data)
532 {
533         struct cache_head **ch;
534         struct rsc *rscp;
535         int n;
536         ENTRY;
537
538         write_lock(&rsc_cache.hash_lock);
539         for (n = 0; n < RSC_HASHMAX; n++) {
540                 for (ch = &rsc_cache.hash_table[n]; *ch;) {
541                         rscp = container_of(*ch, struct rsc, h);
542
543                         if (!match(rscp, data)) {
544                                 ch = &((*ch)->next);
545                                 continue;
546                         }
547
548                         /* it seems simply set NEGATIVE doesn't work */
549                         *ch = (*ch)->next;
550                         rscp->h.next = NULL;
551                         cache_get(&rscp->h);
552                         set_bit(CACHE_NEGATIVE, &rscp->h.flags);
553                         rsc_put(&rscp->h, &rsc_cache);
554                         rsc_cache.entries--;
555                 }
556         }
557         write_unlock(&rsc_cache.hash_lock);
558         EXIT;
559 }
560
561 static
562 int match_uid(struct rsc *rscp, long uid)
563 {
564         if ((int) uid == -1)
565                 return 1;
566         return ((int) rscp->ctx.gsc_uid == (int) uid);
567 }
568
569 static
570 int match_target(struct rsc *rscp, long target)
571 {
572         return (rscp->target == (struct obd_device *) target);
573 }
574
575 static inline
576 void rsc_flush_uid(int uid)
577 {
578         if (uid == -1)
579                 CWARN("flush all gss contexts...\n");
580
581         rsc_flush(match_uid, (long) uid);
582 }
583
584 static inline
585 void rsc_flush_target(struct obd_device *target)
586 {
587         rsc_flush(match_target, (long) target);
588 }
589
590 void gss_secsvc_flush(struct obd_device *target)
591 {
592         rsc_flush_target(target);
593 }
594 EXPORT_SYMBOL(gss_secsvc_flush);
595
596 static struct cache_detail rsc_cache = {
597         .hash_size      = RSC_HASHMAX,
598         .hash_table     = rsc_table,
599         .name           = "auth.ptlrpcs.context",
600         .cache_put      = rsc_put,
601         .cache_parse    = rsc_parse,
602 };
603
604 static DefineSimpleCacheLookup(rsc, 0);
605
606 static
607 struct rsc *gss_svc_searchbyctx(rawobj_t *handle)
608 {
609         struct rsc  rsci;
610         struct rsc *found;
611
612         memset(&rsci, 0, sizeof(rsci));
613         if (rawobj_dup(&rsci.handle, handle))
614                 return NULL;
615
616         found = rsc_lookup(&rsci, 0);
617         rsc_free(&rsci);
618         if (!found)
619                 return NULL;
620         if (cache_check(&rsc_cache, &found->h, NULL))
621                 return NULL;
622         return found;
623 }
624
625 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
626                                    struct gss_sec *gsec,
627                                    struct gss_cli_ctx *gctx)
628 {
629         struct rsc      rsci, *rscp;
630         unsigned long   ctx_expiry;
631         __u32           major;
632         ENTRY;
633
634         memset(&rsci, 0, sizeof(rsci));
635
636         if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
637                          sizeof(gsec->gs_rvs_hdl))) {
638                 CERROR("unable alloc handle\n");
639                 RETURN(-ENOMEM);
640         }
641
642         major = lgss_copy_reverse_context(gctx->gc_mechctx,
643                                           &rsci.ctx.gsc_mechctx);
644         if (major != GSS_S_COMPLETE) {
645                 CERROR("unable to copy reverse context\n");
646                 rsc_free(&rsci);
647                 RETURN(-ENOMEM);
648         }
649
650         if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
651                 CERROR("unable to get expire time, drop it\n");
652                 rsc_free(&rsci);
653                 RETURN(-EINVAL);
654         }
655
656         rsci.h.expiry_time = (time_t) ctx_expiry;
657         rsci.target = imp->imp_obd;
658
659         rscp = rsc_lookup(&rsci, 1);
660         rsc_free(&rsci);
661         if (rscp)
662                 rsc_put(&rscp->h, &rsc_cache);
663
664         CWARN("client installed reverse svc ctx to %s: idx "LPX64"\n",
665               imp->imp_obd->u.cli.cl_target_uuid.uuid,
666               gsec->gs_rvs_hdl);
667
668         imp->imp_next_reconnect = gss_round_imp_reconnect(ctx_expiry);
669         CWARN("import(%s) to %s: set force reconnect at %lu(%lds valid time)\n",
670               ptlrpc_import_state_name(imp->imp_state),
671               imp->imp_obd->u.cli.cl_target_uuid.uuid,
672               imp->imp_next_reconnect,
673               (long) (imp->imp_next_reconnect - get_seconds()));
674
675         RETURN(0);
676 }
677
678 #if 0
679 static int
680 gss_svc_unseal_request(struct ptlrpc_request *req,
681                        struct rsc *rsci,
682                        struct gss_wire_cred *gc,
683                        __u32 *vp, __u32 vlen)
684 {
685         struct ptlrpcs_wire_hdr *sec_hdr;
686         struct gss_ctx *ctx = rsci->mechctx;
687         rawobj_t cipher_text, plain_text;
688         __u32 major;
689         ENTRY;
690
691         sec_hdr = (struct ptlrpcs_wire_hdr *) req->rq_reqbuf;
692
693         if (vlen < 4) {
694                 CERROR("vlen only %u\n", vlen);
695                 RETURN(GSS_S_CALL_BAD_STRUCTURE);
696         }
697
698         cipher_text.len = le32_to_cpu(*vp++);
699         cipher_text.data = (__u8 *) vp;
700         vlen -= 4;
701         
702         if (cipher_text.len > vlen) {
703                 CERROR("cipher claimed %u while buf only %u\n",
704                         cipher_text.len, vlen);
705                 RETURN(GSS_S_CALL_BAD_STRUCTURE);
706         }
707
708         plain_text = cipher_text;
709
710         major = lgss_unwrap(ctx, GSS_C_QOP_DEFAULT, &cipher_text, &plain_text);
711         if (major) {
712                 CERROR("unwrap error 0x%x\n", major);
713                 RETURN(major);
714         }
715
716         if (gss_check_seq_num(&rsci->seqdata, gc->gc_seq)) {
717                 CERROR("discard replayed request %p(o%u,x"LPU64",t"LPU64")\n",
718                         req, req->rq_reqmsg->opc, req->rq_xid,
719                         req->rq_reqmsg->transno);
720                 RETURN(GSS_S_DUPLICATE_TOKEN);
721         }
722
723         req->rq_reqmsg = (struct lustre_msg *) (vp);
724         req->rq_reqlen = plain_text.len;
725
726         CDEBUG(D_SEC, "msg len %d\n", req->rq_reqlen);
727
728         RETURN(GSS_S_COMPLETE);
729 }
730 #endif
731
732 static
733 struct cache_deferred_req* cache_upcall_defer(struct cache_req *req)
734 {
735         return NULL;
736 }
737 static struct cache_req cache_upcall_chandle = { cache_upcall_defer };
738
739 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
740                                struct gss_svc_reqctx *grctx,
741                                struct gss_wire_ctx *gw,
742                                struct obd_device *target,
743                                __u32 lustre_svc,
744                                rawobj_t *rvs_hdl,
745                                rawobj_t *in_token)
746 {
747         struct ptlrpc_reply_state *rs;
748         struct rsc                *rsci = NULL;
749         struct rsi                *rsip = NULL, rsikey;
750         wait_queue_t               wait;
751         int                        replen = sizeof(struct ptlrpc_body);
752         struct gss_rep_header     *rephdr;
753         int                        first_check = 1;
754         int                        rc = SECSVC_DROP;
755         ENTRY;
756
757         memset(&rsikey, 0, sizeof(rsikey));
758         rsikey.lustre_svc = lustre_svc;
759         rsikey.nid = (__u64) req->rq_peer.nid;
760
761         /* duplicate context handle. for INIT it always 0 */
762         if (rawobj_dup(&rsikey.in_handle, &gw->gw_handle)) {
763                 CERROR("fail to dup context handle\n");
764                 GOTO(out, rc);
765         }
766
767         if (rawobj_dup(&rsikey.in_token, in_token)) {
768                 CERROR("can't duplicate token\n");
769                 rawobj_free(&rsikey.in_handle);
770                 GOTO(out, rc);
771         }
772
773         rsip = rsi_lookup(&rsikey, 0);
774         rsi_free(&rsikey);
775         if (!rsip) {
776                 CERROR("error in rsi_lookup.\n");
777
778                 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
779                         rc = SECSVC_COMPLETE;
780
781                 GOTO(out, rc);
782         }
783
784         cache_get(&rsip->h); /* take an extra ref */
785         init_waitqueue_head(&rsip->waitq);
786         init_waitqueue_entry(&wait, current);
787         add_wait_queue(&rsip->waitq, &wait);
788
789 cache_check:
790         /* Note each time cache_check() will drop a reference if return
791          * non-zero. We hold an extra reference on initial rsip, but must
792          * take care of following calls.
793          */
794         rc = cache_check(&rsi_cache, &rsip->h, &cache_upcall_chandle);
795         switch (rc) {
796         case -EAGAIN: {
797                 int valid;
798
799                 if (first_check) {
800                         first_check = 0;
801
802                         read_lock(&rsi_cache.hash_lock);
803                         valid = test_bit(CACHE_VALID, &rsip->h.flags);
804                         if (valid == 0)
805                                 set_current_state(TASK_INTERRUPTIBLE);
806                         read_unlock(&rsi_cache.hash_lock);
807
808                         if (valid == 0)
809                                 schedule_timeout(GSS_SVC_UPCALL_TIMEOUT * HZ);
810
811                         cache_get(&rsip->h);
812                         goto cache_check;
813                 }
814                 CWARN("waited %ds timeout, drop\n", GSS_SVC_UPCALL_TIMEOUT);
815                 break;
816         }
817         case -ENOENT:
818                 CWARN("cache_check return ENOENT, drop\n");
819                 break;
820         case 0:
821                 /* if not the first check, we have to release the extra
822                  * reference we just added on it.
823                  */
824                 if (!first_check)
825                         cache_put(&rsip->h, &rsi_cache);
826                 CDEBUG(D_SEC, "cache_check is good\n");
827                 break;
828         }
829
830         remove_wait_queue(&rsip->waitq, &wait);
831         cache_put(&rsip->h, &rsi_cache);
832
833         if (rc)
834                 GOTO(out, rc = SECSVC_DROP);
835
836         rc = SECSVC_DROP;
837         rsci = gss_svc_searchbyctx(&rsip->out_handle);
838         if (!rsci) {
839                 CERROR("authentication failed\n");
840
841                 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
842                         rc = SECSVC_COMPLETE;
843
844                 GOTO(out, rc);
845         } else {
846                 cache_get(&rsci->h);
847                 grctx->src_ctx = &rsci->ctx;
848         }
849
850         if (rawobj_dup(&rsci->ctx.gsc_rvs_hdl, rvs_hdl)) {
851                 CERROR("failed duplicate reverse handle\n");
852                 GOTO(out, rc);
853         }
854
855         rsci->target = target;
856
857         CWARN("server create rsc %p(%u->%s)\n",
858               rsci, rsci->ctx.gsc_uid, libcfs_nid2str(req->rq_peer.nid));
859
860         if (rsip->out_handle.len > PTLRPC_GSS_MAX_HANDLE_SIZE) {
861                 CERROR("handle size %u too large\n", rsip->out_handle.len);
862                 GOTO(out, rc = SECSVC_DROP);
863         }
864
865         grctx->src_init = 1;
866         grctx->src_reserve_len = size_round4(rsip->out_token.len);
867
868         rc = lustre_pack_reply_v2(req, 1, &replen, NULL);
869         if (rc) {
870                 CERROR("failed to pack reply: %d\n", rc);
871                 GOTO(out, rc = SECSVC_DROP);
872         }
873
874         rs = req->rq_reply_state;
875         LASSERT(rs->rs_repbuf->lm_bufcount == 3);
876         LASSERT(rs->rs_repbuf->lm_buflens[0] >=
877                 sizeof(*rephdr) + rsip->out_handle.len);
878         LASSERT(rs->rs_repbuf->lm_buflens[2] >= rsip->out_token.len);
879
880         rephdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
881         rephdr->gh_version = PTLRPC_GSS_VERSION;
882         rephdr->gh_flags = 0;
883         rephdr->gh_proc = PTLRPC_GSS_PROC_ERR;
884         rephdr->gh_major = rsip->major_status;
885         rephdr->gh_minor = rsip->minor_status;
886         rephdr->gh_seqwin = GSS_SEQ_WIN;
887         rephdr->gh_handle.len = rsip->out_handle.len;
888         memcpy(rephdr->gh_handle.data, rsip->out_handle.data,
889                rsip->out_handle.len);
890
891         memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0), rsip->out_token.data,
892                rsip->out_token.len);
893
894         rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
895                                                rsip->out_token.len, 0);
896
897         if (rsci->ctx.gsc_usr_mds)
898                 CWARN("user from %s authenticated as mds\n",
899                       libcfs_nid2str(req->rq_peer.nid));
900
901         rc = SECSVC_OK;
902
903 out:
904         /* it looks like here we should put rsip also, but this mess up
905          * with NFS cache mgmt code... FIXME
906          */
907 #if 0
908         if (rsip)
909                 rsi_put(&rsip->h, &rsi_cache);
910 #endif
911
912         if (rsci) {
913                 /* if anything went wrong, we don't keep the context too */
914                 if (rc != SECSVC_OK)
915                         set_bit(CACHE_NEGATIVE, &rsci->h.flags);
916
917                 rsc_put(&rsci->h, &rsc_cache);
918         }
919         RETURN(rc);
920 }
921
922 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
923                                            struct gss_wire_ctx *gw)
924 {
925         struct rsc *rsc;
926
927         rsc = gss_svc_searchbyctx(&gw->gw_handle);
928         if (!rsc) {
929                 CWARN("Invalid gss context handle from %s\n",
930                       libcfs_nid2str(req->rq_peer.nid));
931                 return NULL;
932         }
933
934         return &rsc->ctx;
935 }
936
937 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx)
938 {
939         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
940
941         rsc_put(&rsc->h, &rsc_cache);
942 }
943
944 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx)
945 {
946         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
947
948         set_bit(CACHE_NEGATIVE, &rsc->h.flags);
949 }
950
951 int __init gss_svc_init_upcall(void)
952 {
953         int     i;
954
955         cache_register(&rsi_cache);
956         cache_register(&rsc_cache);
957
958         /* FIXME this looks stupid. we intend to give lsvcgssd a chance to open
959          * the init upcall channel, otherwise there's big chance that the first
960          * upcall issued before the channel be opened thus nfsv4 cache code will
961          * drop the request direclty, thus lead to unnecessary recovery time.
962          * here we wait at miximum 1.5 seconds.
963          */
964         for (i = 0; i < 6; i++) {
965                 if (atomic_read(&rsi_cache.readers) > 0)
966                         break;
967                 set_current_state(TASK_UNINTERRUPTIBLE);
968                 LASSERT(HZ >= 4);
969                 schedule_timeout(HZ / 4);
970         }
971
972         if (atomic_read(&rsi_cache.readers) == 0)
973                 CWARN("Init channel is not opened by lsvcgssd, following "
974                       "request might be dropped until lsvcgssd is active\n");
975
976         /*
977          * this helps reducing context index confliction. after server reboot,
978          * conflicting request from clients might be filtered out by initial
979          * sequence number checking, thus no chance to sent error notification
980          * back to clients.
981          */
982         get_random_bytes(&__ctx_index, sizeof(__ctx_index));
983
984         return 0;
985 }
986
987 void __exit gss_svc_exit_upcall(void)
988 {
989         int rc;
990
991         cache_purge(&rsi_cache);
992         if ((rc = cache_unregister(&rsi_cache)))
993                 CERROR("unregister rsi cache: %d\n", rc);
994
995         cache_purge(&rsc_cache);
996         if ((rc = cache_unregister(&rsc_cache)))
997                 CERROR("unregister rsc cache: %d\n", rc);
998 }