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