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