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