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