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