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