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