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