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