Whamcloud - gitweb
LU-8880 gss: fix GSS support for DNE
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_svc_upcall.c
1 /*
2  * Modifications for Lustre
3  *
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  *
6  * Copyright (c) 2012, 2014, Intel Corporation.
7  *
8  * Author: Eric Mei <ericm@clusterfs.com>
9  */
10
11 /*
12  * Neil Brown <neilb@cse.unsw.edu.au>
13  * J. Bruce Fields <bfields@umich.edu>
14  * Andy Adamson <andros@umich.edu>
15  * Dug Song <dugsong@monkey.org>
16  *
17  * RPCSEC_GSS server authentication.
18  * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
19  * (gssapi)
20  *
21  * The RPCSEC_GSS involves three stages:
22  *  1/ context creation
23  *  2/ data exchange
24  *  3/ context destruction
25  *
26  * Context creation is handled largely by upcalls to user-space.
27  *  In particular, GSS_Accept_sec_context is handled by an upcall
28  * Data exchange is handled entirely within the kernel
29  *  In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
30  * Context destruction is handled in-kernel
31  *  GSS_Delete_sec_context is in-kernel
32  *
33  * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
34  * The context handle and gss_token are used as a key into the rpcsec_init cache.
35  * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
36  * being major_status, minor_status, context_handle, reply_token.
37  * These are sent back to the client.
38  * Sequence window management is handled by the kernel.  The window size if currently
39  * a compile time constant.
40  *
41  * When user-space is happy that a context is established, it places an entry
42  * in the rpcsec_context cache. The key for this cache is the context_handle.
43  * The content includes:
44  *   uid/gidlist - for determining access rights
45  *   mechanism type
46  *   mechanism specific information, such as a key
47  *
48  */
49
50 #define DEBUG_SUBSYSTEM S_SEC
51 #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 #include <net/sock.h>
59
60 #include <obd.h>
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <lustre/lustre_idl.h>
64 #include <lustre_import.h>
65 #include <lustre_net.h>
66 #include <lustre_nodemap.h>
67 #include <lustre_sec.h>
68
69 #include "gss_err.h"
70 #include "gss_internal.h"
71 #include "gss_api.h"
72
73 #define GSS_SVC_UPCALL_TIMEOUT  (20)
74
75 static spinlock_t __ctx_index_lock;
76 static __u64 __ctx_index;
77
78 __u64 gss_get_next_ctx_index(void)
79 {
80         __u64 idx;
81
82         spin_lock(&__ctx_index_lock);
83         idx = __ctx_index++;
84         spin_unlock(&__ctx_index_lock);
85
86         return idx;
87 }
88
89 static inline unsigned long hash_mem(char *buf, int length, int bits)
90 {
91         unsigned long hash = 0;
92         unsigned long l = 0;
93         int len = 0;
94         unsigned char c;
95
96         do {
97                 if (len == length) {
98                         c = (char) len;
99                         len = -1;
100                 } else
101                         c = *buf++;
102
103                 l = (l << 8) | c;
104                 len++;
105
106                 if ((len & (BITS_PER_LONG/8-1)) == 0)
107                         hash = hash_long(hash^l, BITS_PER_LONG);
108         } while (len);
109
110         return hash >> (BITS_PER_LONG - bits);
111 }
112
113 /* This compatibility can be removed once kernel 3.3 is used,
114  * since cache_register_net/cache_unregister_net are exported.
115  * Note that since kernel 3.4 cache_register and cache_unregister
116  * are removed.
117 */
118 static inline int _cache_register_net(struct cache_detail *cd, struct net *net)
119 {
120 #ifdef HAVE_CACHE_REGISTER
121         return cache_register(cd);
122 #else
123         return cache_register_net(cd, net);
124 #endif
125 }
126 static inline void _cache_unregister_net(struct cache_detail *cd,
127                                          struct net *net)
128 {
129 #ifdef HAVE_CACHE_REGISTER
130         cache_unregister(cd);
131 #else
132         cache_unregister_net(cd, net);
133 #endif
134 }
135 /****************************************
136  * rpc sec init (rsi) cache *
137  ****************************************/
138
139 #define RSI_HASHBITS    (6)
140 #define RSI_HASHMAX     (1 << RSI_HASHBITS)
141 #define RSI_HASHMASK    (RSI_HASHMAX - 1)
142
143 struct rsi {
144         struct cache_head       h;
145         __u32                   lustre_svc;
146         __u64                   nid;
147         char                    nm_name[LUSTRE_NODEMAP_NAME_LENGTH + 1];
148         wait_queue_head_t       waitq;
149         rawobj_t                in_handle, in_token;
150         rawobj_t                out_handle, out_token;
151         int                     major_status, minor_status;
152 };
153
154 #ifdef HAVE_CACHE_HEAD_HLIST
155 static struct hlist_head rsi_table[RSI_HASHMAX];
156 #else
157 static struct cache_head *rsi_table[RSI_HASHMAX];
158 #endif
159 static struct cache_detail rsi_cache;
160 static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
161 static struct rsi *rsi_lookup(struct rsi *item);
162
163 static inline int rsi_hash(struct rsi *item)
164 {
165         return hash_mem((char *)item->in_handle.data, item->in_handle.len,
166                         RSI_HASHBITS) ^
167                hash_mem((char *)item->in_token.data, item->in_token.len,
168                         RSI_HASHBITS);
169 }
170
171 static inline 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 void rsi_free(struct rsi *rsi)
178 {
179         rawobj_free(&rsi->in_handle);
180         rawobj_free(&rsi->in_token);
181         rawobj_free(&rsi->out_handle);
182         rawobj_free(&rsi->out_token);
183 }
184
185 /* See handle_channel_req() userspace for where the upcall data is read */
186 static void rsi_request(struct cache_detail *cd,
187                         struct cache_head *h,
188                         char **bpp, int *blen)
189 {
190         struct rsi *rsi = container_of(h, struct rsi, h);
191         __u64 index = 0;
192
193         /* if in_handle is null, provide kernel suggestion */
194         if (rsi->in_handle.len == 0)
195                 index = gss_get_next_ctx_index();
196
197         qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
198                         sizeof(rsi->lustre_svc));
199         qword_addhex(bpp, blen, (char *) &rsi->nid, sizeof(rsi->nid));
200         qword_addhex(bpp, blen, (char *) &index, sizeof(index));
201         qword_addhex(bpp, blen, (char *) rsi->nm_name,
202                      strlen(rsi->nm_name) + 1);
203         qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
204         qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
205         (*bpp)[-1] = '\n';
206 }
207
208 #ifdef HAVE_SUNRPC_UPCALL_HAS_3ARGS
209 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
210 {
211         return sunrpc_cache_pipe_upcall(cd, h, rsi_request);
212 }
213 #else
214
215 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
216 {
217         return sunrpc_cache_pipe_upcall(cd, h);
218 }
219 #endif
220
221 static inline void __rsi_init(struct rsi *new, struct rsi *item)
222 {
223         new->out_handle = RAWOBJ_EMPTY;
224         new->out_token = RAWOBJ_EMPTY;
225
226         new->in_handle = item->in_handle;
227         item->in_handle = RAWOBJ_EMPTY;
228         new->in_token = item->in_token;
229         item->in_token = RAWOBJ_EMPTY;
230
231         new->lustre_svc = item->lustre_svc;
232         new->nid = item->nid;
233         memcpy(new->nm_name, item->nm_name, sizeof(item->nm_name));
234         init_waitqueue_head(&new->waitq);
235 }
236
237 static inline void __rsi_update(struct rsi *new, struct rsi *item)
238 {
239         LASSERT(new->out_handle.len == 0);
240         LASSERT(new->out_token.len == 0);
241
242         new->out_handle = item->out_handle;
243         item->out_handle = RAWOBJ_EMPTY;
244         new->out_token = item->out_token;
245         item->out_token = RAWOBJ_EMPTY;
246
247         new->major_status = item->major_status;
248         new->minor_status = item->minor_status;
249 }
250
251 static void rsi_put(struct kref *ref)
252 {
253         struct rsi *rsi = container_of(ref, struct rsi, h.ref);
254
255 #ifdef HAVE_CACHE_HEAD_HLIST
256         LASSERT(rsi->h.cache_list.next == NULL);
257 #else
258         LASSERT(rsi->h.next == NULL);
259 #endif
260         rsi_free(rsi);
261         OBD_FREE_PTR(rsi);
262 }
263
264 static int rsi_match(struct cache_head *a, struct cache_head *b)
265 {
266         struct rsi *item = container_of(a, struct rsi, h);
267         struct rsi *tmp = container_of(b, struct rsi, h);
268
269         return __rsi_match(item, tmp);
270 }
271
272 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
273 {
274         struct rsi *new = container_of(cnew, struct rsi, h);
275         struct rsi *item = container_of(citem, struct rsi, h);
276
277         __rsi_init(new, item);
278 }
279
280 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
281 {
282         struct rsi *new = container_of(cnew, struct rsi, h);
283         struct rsi *item = container_of(citem, struct rsi, h);
284
285         __rsi_update(new, item);
286 }
287
288 static struct cache_head *rsi_alloc(void)
289 {
290         struct rsi *rsi;
291
292         OBD_ALLOC_PTR(rsi);
293         if (rsi) 
294                 return &rsi->h;
295         else
296                 return NULL;
297 }
298
299 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
300 {
301         char           *buf = mesg;
302         char           *ep;
303         int             len;
304         struct rsi      rsii, *rsip = NULL;
305         time_t          expiry;
306         int             status = -EINVAL;
307         ENTRY;
308
309
310         memset(&rsii, 0, sizeof(rsii));
311
312         /* handle */
313         len = qword_get(&mesg, buf, mlen);
314         if (len < 0)
315                 goto out;
316         if (rawobj_alloc(&rsii.in_handle, buf, len)) {
317                 status = -ENOMEM;
318                 goto out;
319         }
320
321         /* token */
322         len = qword_get(&mesg, buf, mlen);
323         if (len < 0)
324                 goto out;
325         if (rawobj_alloc(&rsii.in_token, buf, len)) {
326                 status = -ENOMEM;
327                 goto out;
328         }
329
330         rsip = rsi_lookup(&rsii);
331         if (!rsip)
332                 goto out;
333
334         rsii.h.flags = 0;
335         /* expiry */
336         expiry = get_expiry(&mesg);
337         if (expiry == 0)
338                 goto out;
339
340         len = qword_get(&mesg, buf, mlen);
341         if (len <= 0)
342                 goto out;
343
344         /* major */
345         rsii.major_status = simple_strtol(buf, &ep, 10);
346         if (*ep)
347                 goto out;
348
349         /* minor */
350         len = qword_get(&mesg, buf, mlen);
351         if (len <= 0)
352                 goto out;
353         rsii.minor_status = simple_strtol(buf, &ep, 10);
354         if (*ep)
355                 goto out;
356
357         /* out_handle */
358         len = qword_get(&mesg, buf, mlen);
359         if (len < 0)
360                 goto out;
361         if (rawobj_alloc(&rsii.out_handle, buf, len)) {
362                 status = -ENOMEM;
363                 goto out;
364         }
365
366         /* out_token */
367         len = qword_get(&mesg, buf, mlen);
368         if (len < 0)
369                 goto out;
370         if (rawobj_alloc(&rsii.out_token, buf, len)) {
371                 status = -ENOMEM;
372                 goto out;
373         }
374
375         rsii.h.expiry_time = expiry;
376         rsip = rsi_update(&rsii, rsip);
377         status = 0;
378 out:
379         rsi_free(&rsii);
380         if (rsip) {
381                 wake_up_all(&rsip->waitq);
382                 cache_put(&rsip->h, &rsi_cache);
383         } else {
384                 status = -ENOMEM;
385         }
386
387         if (status)
388                 CERROR("rsi parse error %d\n", status);
389         RETURN(status);
390 }
391
392 static struct cache_detail rsi_cache = {
393         .hash_size      = RSI_HASHMAX,
394         .hash_table     = rsi_table,
395         .name           = "auth.sptlrpc.init",
396         .cache_put      = rsi_put,
397 #ifndef HAVE_SUNRPC_UPCALL_HAS_3ARGS
398         .cache_request  = rsi_request,
399 #endif
400         .cache_upcall   = rsi_upcall,
401         .cache_parse    = rsi_parse,
402         .match          = rsi_match,
403         .init           = rsi_init,
404         .update         = update_rsi,
405         .alloc          = rsi_alloc,
406 };
407
408 static struct rsi *rsi_lookup(struct rsi *item)
409 {
410         struct cache_head *ch;
411         int hash = rsi_hash(item);
412
413         ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
414         if (ch)
415                 return container_of(ch, struct rsi, h);
416         else
417                 return NULL;
418 }
419
420 static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
421 {
422         struct cache_head *ch;
423         int hash = rsi_hash(new);
424
425         ch = sunrpc_cache_update(&rsi_cache, &new->h, &old->h, hash);
426         if (ch)
427                 return container_of(ch, struct rsi, h);
428         else
429                 return NULL;
430 }
431
432 /****************************************
433  * rpc sec context (rsc) cache                            *
434  ****************************************/
435
436 #define RSC_HASHBITS    (10)
437 #define RSC_HASHMAX     (1 << RSC_HASHBITS)
438 #define RSC_HASHMASK    (RSC_HASHMAX - 1)
439
440 struct rsc {
441         struct cache_head       h;
442         struct obd_device      *target;
443         rawobj_t                handle;
444         struct gss_svc_ctx      ctx;
445 };
446
447 #ifdef HAVE_CACHE_HEAD_HLIST
448 static struct hlist_head rsc_table[RSC_HASHMAX];
449 #else
450 static struct cache_head *rsc_table[RSC_HASHMAX];
451 #endif
452 static struct cache_detail rsc_cache;
453 static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
454 static struct rsc *rsc_lookup(struct rsc *item);
455
456 static void rsc_free(struct rsc *rsci)
457 {
458         rawobj_free(&rsci->handle);
459         rawobj_free(&rsci->ctx.gsc_rvs_hdl);
460         lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
461 }
462
463 static inline int rsc_hash(struct rsc *rsci)
464 {
465         return hash_mem((char *)rsci->handle.data,
466                         rsci->handle.len, RSC_HASHBITS);
467 }
468
469 static inline int __rsc_match(struct rsc *new, struct rsc *tmp)
470 {
471         return rawobj_equal(&new->handle, &tmp->handle);
472 }
473
474 static inline void __rsc_init(struct rsc *new, struct rsc *tmp)
475 {
476         new->handle = tmp->handle;
477         tmp->handle = RAWOBJ_EMPTY;
478
479         new->target = NULL;
480         memset(&new->ctx, 0, sizeof(new->ctx));
481         new->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
482 }
483
484 static inline void __rsc_update(struct rsc *new, struct rsc *tmp)
485 {
486         new->ctx = tmp->ctx;
487         tmp->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
488         tmp->ctx.gsc_mechctx = NULL;
489
490         memset(&new->ctx.gsc_seqdata, 0, sizeof(new->ctx.gsc_seqdata));
491         spin_lock_init(&new->ctx.gsc_seqdata.ssd_lock);
492 }
493
494 static void rsc_put(struct kref *ref)
495 {
496         struct rsc *rsci = container_of(ref, struct rsc, h.ref);
497
498 #ifdef HAVE_CACHE_HEAD_HLIST
499         LASSERT(rsci->h.cache_list.next == NULL);
500 #else
501         LASSERT(rsci->h.next == NULL);
502 #endif
503         rsc_free(rsci);
504         OBD_FREE_PTR(rsci);
505 }
506
507 static int rsc_match(struct cache_head *a, struct cache_head *b)
508 {
509         struct rsc *new = container_of(a, struct rsc, h);
510         struct rsc *tmp = container_of(b, struct rsc, h);
511
512         return __rsc_match(new, tmp);
513 }
514
515 static void rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
516 {
517         struct rsc *new = container_of(cnew, struct rsc, h);
518         struct rsc *tmp = container_of(ctmp, struct rsc, h);
519
520         __rsc_init(new, tmp);
521 }
522
523 static void update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
524 {
525         struct rsc *new = container_of(cnew, struct rsc, h);
526         struct rsc *tmp = container_of(ctmp, struct rsc, h);
527
528         __rsc_update(new, tmp);
529 }
530
531 static struct cache_head * rsc_alloc(void)
532 {
533         struct rsc *rsc;
534
535         OBD_ALLOC_PTR(rsc);
536         if (rsc)
537                 return &rsc->h;
538         else
539                 return NULL;
540 }
541
542 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
543 {
544         char                *buf = mesg;
545         int                  len, rv, tmp_int;
546         struct rsc           rsci, *rscp = NULL;
547         time_t               expiry;
548         int                  status = -EINVAL;
549         struct gss_api_mech *gm = NULL;
550
551         memset(&rsci, 0, sizeof(rsci));
552
553         /* context handle */
554         len = qword_get(&mesg, buf, mlen);
555         if (len < 0) goto out;
556         status = -ENOMEM;
557         if (rawobj_alloc(&rsci.handle, buf, len))
558                 goto out;
559
560         rsci.h.flags = 0;
561         /* expiry */
562         expiry = get_expiry(&mesg);
563         status = -EINVAL;
564         if (expiry == 0)
565                 goto out;
566
567         /* remote flag */
568         rv = get_int(&mesg, &tmp_int);
569         if (rv) {
570                 CERROR("fail to get remote flag\n");
571                 goto out;
572         }
573         rsci.ctx.gsc_remote = (tmp_int != 0);
574
575         /* root user flag */
576         rv = get_int(&mesg, &tmp_int);
577         if (rv) {
578                 CERROR("fail to get root user flag\n");
579                 goto out;
580         }
581         rsci.ctx.gsc_usr_root = (tmp_int != 0);
582
583         /* mds user flag */
584         rv = get_int(&mesg, &tmp_int);
585         if (rv) {
586                 CERROR("fail to get mds user flag\n");
587                 goto out;
588         }
589         rsci.ctx.gsc_usr_mds = (tmp_int != 0);
590
591         /* oss user flag */
592         rv = get_int(&mesg, &tmp_int);
593         if (rv) {
594                 CERROR("fail to get oss user flag\n");
595                 goto out;
596         }
597         rsci.ctx.gsc_usr_oss = (tmp_int != 0);
598
599         /* mapped uid */
600         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
601         if (rv) {
602                 CERROR("fail to get mapped uid\n");
603                 goto out;
604         }
605
606         rscp = rsc_lookup(&rsci);
607         if (!rscp)
608                 goto out;
609
610         /* uid, or NEGATIVE */
611         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
612         if (rv == -EINVAL)
613                 goto out;
614         if (rv == -ENOENT) {
615                 CERROR("NOENT? set rsc entry negative\n");
616                 set_bit(CACHE_NEGATIVE, &rsci.h.flags);
617         } else {
618                 rawobj_t tmp_buf;
619                 unsigned long ctx_expiry;
620
621                 /* gid */
622                 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
623                         goto out;
624
625                 /* mech name */
626                 len = qword_get(&mesg, buf, mlen);
627                 if (len < 0)
628                         goto out;
629                 gm = lgss_name_to_mech(buf);
630                 status = -EOPNOTSUPP;
631                 if (!gm)
632                         goto out;
633
634                 status = -EINVAL;
635                 /* mech-specific data: */
636                 len = qword_get(&mesg, buf, mlen);
637                 if (len < 0)
638                         goto out;
639
640                 tmp_buf.len = len;
641                 tmp_buf.data = (unsigned char *)buf;
642                 if (lgss_import_sec_context(&tmp_buf, gm,
643                                             &rsci.ctx.gsc_mechctx))
644                         goto out;
645
646                 /* currently the expiry time passed down from user-space
647                  * is invalid, here we retrive it from mech. */
648                 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
649                         CERROR("unable to get expire time, drop it\n");
650                         goto out;
651                 }
652                 expiry = (time_t) ctx_expiry;
653         }
654
655         rsci.h.expiry_time = expiry;
656         rscp = rsc_update(&rsci, rscp);
657         status = 0;
658 out:
659         if (gm)
660                 lgss_mech_put(gm);
661         rsc_free(&rsci);
662         if (rscp)
663                 cache_put(&rscp->h, &rsc_cache);
664         else
665                 status = -ENOMEM;
666
667         if (status)
668                 CERROR("parse rsc error %d\n", status);
669         return status;
670 }
671
672 static struct cache_detail rsc_cache = {
673         .hash_size      = RSC_HASHMAX,
674         .hash_table     = rsc_table,
675         .name           = "auth.sptlrpc.context",
676         .cache_put      = rsc_put,
677         .cache_parse    = rsc_parse,
678         .match          = rsc_match,
679         .init           = rsc_init,
680         .update         = update_rsc,
681         .alloc          = rsc_alloc,
682 };
683
684 static struct rsc *rsc_lookup(struct rsc *item)
685 {
686         struct cache_head *ch;
687         int                hash = rsc_hash(item);
688
689         ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
690         if (ch)
691                 return container_of(ch, struct rsc, h);
692         else
693                 return NULL;
694 }
695
696 static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
697 {
698         struct cache_head *ch;
699         int                hash = rsc_hash(new);
700
701         ch = sunrpc_cache_update(&rsc_cache, &new->h, &old->h, hash);
702         if (ch)
703                 return container_of(ch, struct rsc, h);
704         else
705                 return NULL;
706 }
707
708 #define COMPAT_RSC_PUT(item, cd)        cache_put((item), (cd))
709
710 /****************************************
711  * rsc cache flush                      *
712  ****************************************/
713
714 typedef int rsc_entry_match(struct rsc *rscp, long data);
715
716 static void rsc_flush(rsc_entry_match *match, long data)
717 {
718 #ifdef HAVE_CACHE_HEAD_HLIST
719         struct cache_head *ch = NULL;
720         struct hlist_head *head;
721 #else
722         struct cache_head **ch;
723 #endif
724         struct rsc *rscp;
725         int n;
726         ENTRY;
727
728         write_lock(&rsc_cache.hash_lock);
729         for (n = 0; n < RSC_HASHMAX; n++) {
730 #ifdef HAVE_CACHE_HEAD_HLIST
731                 head = &rsc_cache.hash_table[n];
732                 hlist_for_each_entry(ch, head, cache_list) {
733                         rscp = container_of(ch, struct rsc, h);
734 #else
735                 for (ch = &rsc_cache.hash_table[n]; *ch;) {
736                         rscp = container_of(*ch, struct rsc, h);
737 #endif
738
739                         if (!match(rscp, data)) {
740 #ifndef HAVE_CACHE_HEAD_HLIST
741                                 ch = &((*ch)->next);
742 #endif
743                                 continue;
744                         }
745
746                         /* it seems simply set NEGATIVE doesn't work */
747 #ifdef HAVE_CACHE_HEAD_HLIST
748                         hlist_del_init(&ch->cache_list);
749 #else
750                         *ch = (*ch)->next;
751                         rscp->h.next = NULL;
752 #endif
753                         cache_get(&rscp->h);
754                         set_bit(CACHE_NEGATIVE, &rscp->h.flags);
755                         COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
756                         rsc_cache.entries--;
757                 }
758         }
759         write_unlock(&rsc_cache.hash_lock);
760         EXIT;
761 }
762
763 static int match_uid(struct rsc *rscp, long uid)
764 {
765         if ((int) uid == -1)
766                 return 1;
767         return ((int) rscp->ctx.gsc_uid == (int) uid);
768 }
769
770 static int match_target(struct rsc *rscp, long target)
771 {
772         return (rscp->target == (struct obd_device *) target);
773 }
774
775 static inline void rsc_flush_uid(int uid)
776 {
777         if (uid == -1)
778                 CWARN("flush all gss contexts...\n");
779
780         rsc_flush(match_uid, (long) uid);
781 }
782
783 static inline void rsc_flush_target(struct obd_device *target)
784 {
785         rsc_flush(match_target, (long) target);
786 }
787
788 void gss_secsvc_flush(struct obd_device *target)
789 {
790         rsc_flush_target(target);
791 }
792
793 static struct rsc *gss_svc_searchbyctx(rawobj_t *handle)
794 {
795         struct rsc  rsci;
796         struct rsc *found;
797
798         memset(&rsci, 0, sizeof(rsci));
799         if (rawobj_dup(&rsci.handle, handle))
800                 return NULL;
801
802         found = rsc_lookup(&rsci);
803         rsc_free(&rsci);
804         if (!found)
805                 return NULL;
806         if (cache_check(&rsc_cache, &found->h, NULL))
807                 return NULL;
808         return found;
809 }
810
811 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
812                                    struct gss_sec *gsec,
813                                    struct gss_cli_ctx *gctx)
814 {
815         struct rsc      rsci, *rscp = NULL;
816         unsigned long   ctx_expiry;
817         __u32           major;
818         int             rc;
819         ENTRY;
820
821         memset(&rsci, 0, sizeof(rsci));
822
823         if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
824                          sizeof(gsec->gs_rvs_hdl)))
825                 GOTO(out, rc = -ENOMEM);
826
827         rscp = rsc_lookup(&rsci);
828         if (rscp == NULL)
829                 GOTO(out, rc = -ENOMEM);
830
831         major = lgss_copy_reverse_context(gctx->gc_mechctx,
832                                           &rsci.ctx.gsc_mechctx);
833         if (major != GSS_S_COMPLETE)
834                 GOTO(out, rc = -ENOMEM);
835
836         if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
837                 CERROR("unable to get expire time, drop it\n");
838                 GOTO(out, rc = -EINVAL);
839         }
840         rsci.h.expiry_time = (time_t) ctx_expiry;
841
842         switch (imp->imp_obd->u.cli.cl_sp_to) {
843         case LUSTRE_SP_MDT:
844                 rsci.ctx.gsc_usr_mds = 1;
845                 break;
846         case LUSTRE_SP_OST:
847                 rsci.ctx.gsc_usr_oss = 1;
848                 break;
849         case LUSTRE_SP_CLI:
850                 rsci.ctx.gsc_usr_root = 1;
851         default:
852                 break;
853         }
854
855         rscp = rsc_update(&rsci, rscp);
856         if (rscp == NULL)
857                 GOTO(out, rc = -ENOMEM);
858
859         rscp->target = imp->imp_obd;
860         rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
861
862         CWARN("create reverse svc ctx %p to %s: idx %#llx\n",
863               &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
864         rc = 0;
865 out:
866         if (rscp)
867                 cache_put(&rscp->h, &rsc_cache);
868         rsc_free(&rsci);
869
870         if (rc)
871                 CERROR("create reverse svc ctx: idx %#llx, rc %d\n",
872                        gsec->gs_rvs_hdl, rc);
873         RETURN(rc);
874 }
875
876 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle)
877 {
878         const cfs_time_t        expire = 20;
879         struct rsc             *rscp;
880
881         rscp = gss_svc_searchbyctx(handle);
882         if (rscp) {
883                 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) expire soon\n",
884                        &rscp->ctx, rscp);
885
886                 rscp->h.expiry_time = cfs_time_current_sec() + expire;
887                 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
888         }
889         return 0;
890 }
891
892 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx)
893 {
894         struct rsc *rscp = container_of(ctx, struct rsc, ctx);
895
896         return rawobj_dup(handle, &rscp->handle);
897 }
898
899 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq)
900 {
901         struct rsc             *rscp;
902
903         rscp = gss_svc_searchbyctx(handle);
904         if (rscp) {
905                 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) update seq to %u\n",
906                        &rscp->ctx, rscp, seq + 1);
907
908                 rscp->ctx.gsc_rvs_seq = seq + 1;
909                 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
910         }
911         return 0;
912 }
913
914 static struct cache_deferred_req* cache_upcall_defer(struct cache_req *req)
915 {
916         return NULL;
917 }
918 static struct cache_req cache_upcall_chandle = { cache_upcall_defer };
919
920 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
921                                struct gss_svc_reqctx *grctx,
922                                struct gss_wire_ctx *gw,
923                                struct obd_device *target,
924                                __u32 lustre_svc,
925                                rawobj_t *rvs_hdl,
926                                rawobj_t *in_token)
927 {
928         struct ptlrpc_reply_state *rs;
929         struct rsc                *rsci = NULL;
930         struct rsi                *rsip = NULL, rsikey;
931         wait_queue_t             wait;
932         int                        replen = sizeof(struct ptlrpc_body);
933         struct gss_rep_header     *rephdr;
934         int                        first_check = 1;
935         int                        rc = SECSVC_DROP;
936         ENTRY;
937
938         memset(&rsikey, 0, sizeof(rsikey));
939         rsikey.lustre_svc = lustre_svc;
940         rsikey.nid = (__u64) req->rq_peer.nid;
941         nodemap_test_nid(req->rq_peer.nid, rsikey.nm_name,
942                          sizeof(rsikey.nm_name));
943
944         /* duplicate context handle. for INIT it always 0 */
945         if (rawobj_dup(&rsikey.in_handle, &gw->gw_handle)) {
946                 CERROR("fail to dup context handle\n");
947                 GOTO(out, rc);
948         }
949
950         if (rawobj_dup(&rsikey.in_token, in_token)) {
951                 CERROR("can't duplicate token\n");
952                 rawobj_free(&rsikey.in_handle);
953                 GOTO(out, rc);
954         }
955
956         rsip = rsi_lookup(&rsikey);
957         rsi_free(&rsikey);
958         if (!rsip) {
959                 CERROR("error in rsi_lookup.\n");
960
961                 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
962                         rc = SECSVC_COMPLETE;
963
964                 GOTO(out, rc);
965         }
966
967         cache_get(&rsip->h); /* take an extra ref */
968         init_waitqueue_head(&rsip->waitq);
969         init_waitqueue_entry(&wait, current);
970         add_wait_queue(&rsip->waitq, &wait);
971
972 cache_check:
973         /* Note each time cache_check() will drop a reference if return
974          * non-zero. We hold an extra reference on initial rsip, but must
975          * take care of following calls. */
976         rc = cache_check(&rsi_cache, &rsip->h, &cache_upcall_chandle);
977         switch (rc) {
978         case -ETIMEDOUT:
979         case -EAGAIN: {
980                 int valid;
981
982                 if (first_check) {
983                         first_check = 0;
984
985                         read_lock(&rsi_cache.hash_lock);
986                         valid = test_bit(CACHE_VALID, &rsip->h.flags);
987                         if (valid == 0)
988                                 set_current_state(TASK_INTERRUPTIBLE);
989                         read_unlock(&rsi_cache.hash_lock);
990
991                         if (valid == 0) {
992                                 unsigned long jiffies;
993                                 jiffies = msecs_to_jiffies(MSEC_PER_SEC *
994                                           GSS_SVC_UPCALL_TIMEOUT);
995                                 schedule_timeout(jiffies);
996                         }
997                         cache_get(&rsip->h);
998                         goto cache_check;
999                 }
1000                 CWARN("waited %ds timeout, drop\n", GSS_SVC_UPCALL_TIMEOUT);
1001                 break;
1002         }
1003         case -ENOENT:
1004                 CWARN("cache_check return ENOENT, drop\n");
1005                 break;
1006         case 0:
1007                 /* if not the first check, we have to release the extra
1008                  * reference we just added on it. */
1009                 if (!first_check)
1010                         cache_put(&rsip->h, &rsi_cache);
1011                 CDEBUG(D_SEC, "cache_check is good\n");
1012                 break;
1013         }
1014
1015         remove_wait_queue(&rsip->waitq, &wait);
1016         cache_put(&rsip->h, &rsi_cache);
1017
1018         if (rc)
1019                 GOTO(out, rc = SECSVC_DROP);
1020
1021         rc = SECSVC_DROP;
1022         rsci = gss_svc_searchbyctx(&rsip->out_handle);
1023         if (!rsci) {
1024                 CERROR("authentication failed\n");
1025
1026                 /* gss mechanism returned major and minor code so we return
1027                  * those in error message */
1028                 if (!gss_pack_err_notify(req, rsip->major_status,
1029                                          rsip->minor_status))
1030                         rc = SECSVC_COMPLETE;
1031
1032                 GOTO(out, rc);
1033         } else {
1034                 cache_get(&rsci->h);
1035                 grctx->src_ctx = &rsci->ctx;
1036         }
1037
1038         if (rawobj_dup(&rsci->ctx.gsc_rvs_hdl, rvs_hdl)) {
1039                 CERROR("failed duplicate reverse handle\n");
1040                 GOTO(out, rc);
1041         }
1042
1043         rsci->target = target;
1044
1045         CDEBUG(D_SEC, "server create rsc %p(%u->%s)\n",
1046                rsci, rsci->ctx.gsc_uid, libcfs_nid2str(req->rq_peer.nid));
1047
1048         if (rsip->out_handle.len > PTLRPC_GSS_MAX_HANDLE_SIZE) {
1049                 CERROR("handle size %u too large\n", rsip->out_handle.len);
1050                 GOTO(out, rc = SECSVC_DROP);
1051         }
1052
1053         grctx->src_init = 1;
1054         grctx->src_reserve_len = cfs_size_round4(rsip->out_token.len);
1055
1056         rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1057         if (rc) {
1058                 CERROR("failed to pack reply: %d\n", rc);
1059                 GOTO(out, rc = SECSVC_DROP);
1060         }
1061
1062         rs = req->rq_reply_state;
1063         LASSERT(rs->rs_repbuf->lm_bufcount == 3);
1064         LASSERT(rs->rs_repbuf->lm_buflens[0] >=
1065                 sizeof(*rephdr) + rsip->out_handle.len);
1066         LASSERT(rs->rs_repbuf->lm_buflens[2] >= rsip->out_token.len);
1067
1068         rephdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1069         rephdr->gh_version = PTLRPC_GSS_VERSION;
1070         rephdr->gh_flags = 0;
1071         rephdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1072         rephdr->gh_major = rsip->major_status;
1073         rephdr->gh_minor = rsip->minor_status;
1074         rephdr->gh_seqwin = GSS_SEQ_WIN;
1075         rephdr->gh_handle.len = rsip->out_handle.len;
1076         memcpy(rephdr->gh_handle.data, rsip->out_handle.data,
1077                rsip->out_handle.len);
1078
1079         memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0), rsip->out_token.data,
1080                rsip->out_token.len);
1081
1082         rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
1083                                                rsip->out_token.len, 0);
1084
1085         rc = SECSVC_OK;
1086
1087 out:
1088         /* it looks like here we should put rsip also, but this mess up
1089          * with NFS cache mgmt code... FIXME
1090          * something like:
1091          * if (rsip)
1092          *     rsi_put(&rsip->h, &rsi_cache); */
1093
1094         if (rsci) {
1095                 /* if anything went wrong, we don't keep the context too */
1096                 if (rc != SECSVC_OK)
1097                         set_bit(CACHE_NEGATIVE, &rsci->h.flags);
1098                 else
1099                         CDEBUG(D_SEC, "create rsc with idx %#llx\n",
1100                                gss_handle_to_u64(&rsci->handle));
1101
1102                 COMPAT_RSC_PUT(&rsci->h, &rsc_cache);
1103         }
1104         RETURN(rc);
1105 }
1106
1107 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
1108                                            struct gss_wire_ctx *gw)
1109 {
1110         struct rsc *rsc;
1111
1112         rsc = gss_svc_searchbyctx(&gw->gw_handle);
1113         if (!rsc) {
1114                 CWARN("Invalid gss ctx idx %#llx from %s\n",
1115                       gss_handle_to_u64(&gw->gw_handle),
1116                       libcfs_nid2str(req->rq_peer.nid));
1117                 return NULL;
1118         }
1119
1120         return &rsc->ctx;
1121 }
1122
1123 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx)
1124 {
1125         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1126
1127         COMPAT_RSC_PUT(&rsc->h, &rsc_cache);
1128 }
1129
1130 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx)
1131 {
1132         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1133
1134         /* can't be found */
1135         set_bit(CACHE_NEGATIVE, &rsc->h.flags);
1136         /* to be removed at next scan */
1137         rsc->h.expiry_time = 1;
1138 }
1139
1140 int __init gss_init_svc_upcall(void)
1141 {
1142         int     i, rc;
1143
1144         spin_lock_init(&__ctx_index_lock);
1145         /*
1146          * this helps reducing context index confliction. after server reboot,
1147          * conflicting request from clients might be filtered out by initial
1148          * sequence number checking, thus no chance to sent error notification
1149          * back to clients.
1150          */
1151         cfs_get_random_bytes(&__ctx_index, sizeof(__ctx_index));
1152
1153         rc = _cache_register_net(&rsi_cache, &init_net);
1154         if (rc != 0)
1155                 return rc;
1156
1157         rc = _cache_register_net(&rsc_cache, &init_net);
1158         if (rc != 0) {
1159                 _cache_unregister_net(&rsi_cache, &init_net);
1160                 return rc;
1161         }
1162
1163         /* FIXME this looks stupid. we intend to give lsvcgssd a chance to open
1164          * the init upcall channel, otherwise there's big chance that the first
1165          * upcall issued before the channel be opened thus nfsv4 cache code will
1166          * drop the request direclty, thus lead to unnecessary recovery time.
1167          * here we wait at miximum 1.5 seconds. */
1168         for (i = 0; i < 6; i++) {
1169                 if (atomic_read(&rsi_cache.readers) > 0)
1170                         break;
1171                 set_current_state(TASK_UNINTERRUPTIBLE);
1172                 LASSERT(msecs_to_jiffies(MSEC_PER_SEC) >= 4);
1173                 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC / 4));
1174         }
1175
1176         if (atomic_read(&rsi_cache.readers) == 0)
1177                 CWARN("Init channel is not opened by lsvcgssd, following "
1178                       "request might be dropped until lsvcgssd is active\n");
1179
1180         return 0;
1181 }
1182
1183 void gss_exit_svc_upcall(void)
1184 {
1185         cache_purge(&rsi_cache);
1186         _cache_unregister_net(&rsi_cache, &init_net);
1187
1188         cache_purge(&rsc_cache);
1189         _cache_unregister_net(&rsc_cache, &init_net);
1190 }