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