Whamcloud - gitweb
LU-16802 build: compatibility for 6.4 kernels
[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/mutex.h>
57 #include <linux/sunrpc/cache.h>
58 #include <linux/binfmts.h>
59 #include <net/sock.h>
60 #include <linux/un.h>
61
62 #include <obd.h>
63 #include <obd_class.h>
64 #include <obd_support.h>
65 #include <lustre_import.h>
66 #include <lustre_net.h>
67 #include <lustre_nodemap.h>
68 #include <lustre_sec.h>
69 #include <libcfs/linux/linux-hash.h>
70
71 #include "gss_err.h"
72 #include "gss_internal.h"
73 #include "gss_api.h"
74 #include "gss_crypto.h"
75
76 #ifndef HAVE_GET_EXPIRY_2ARGS
77 static inline int __get_expiry2(char **bpp, time64_t *rvp)
78 {
79         *rvp = get_expiry(bpp);
80         return *rvp ? 0 : -EINVAL;
81 }
82 #define get_expiry(ps, pt)      __get_expiry2((ps), (pt))
83 #endif
84
85 #define GSS_SVC_UPCALL_TIMEOUT  (20)
86
87 static DEFINE_SPINLOCK(__ctx_index_lock);
88 static __u64 __ctx_index;
89
90 unsigned int krb5_allow_old_client_csum;
91
92 __u64 gss_get_next_ctx_index(void)
93 {
94         __u64 idx;
95
96         spin_lock(&__ctx_index_lock);
97         idx = __ctx_index++;
98         spin_unlock(&__ctx_index_lock);
99
100         return idx;
101 }
102
103 static inline unsigned long hash_mem(char *buf, int length, int bits)
104 {
105         unsigned long hash = 0;
106         unsigned long l = 0;
107         int len = 0;
108         unsigned char c;
109
110         do {
111                 if (len == length) {
112                         c = (char) len;
113                         len = -1;
114                 } else
115                         c = *buf++;
116
117                 l = (l << 8) | c;
118                 len++;
119
120                 if ((len & (BITS_PER_LONG/8-1)) == 0)
121                         hash = cfs_hash_long(hash^l, BITS_PER_LONG);
122         } while (len);
123
124         return hash >> (BITS_PER_LONG - bits);
125 }
126
127 /* This is a little bit of a concern but we need to make our own hash64 function
128  * as the one from the kernel seems to be buggy by returning a u32:
129  * static __always_inline u32 hash_64_generic(u64 val, unsigned int bits)
130  */
131 #if BITS_PER_LONG == 64
132 static __always_inline __u64 gss_hash_64(__u64 val, unsigned int bits)
133 {
134         __u64 hash = val;
135         /*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
136         __u64 n = hash;
137
138         n <<= 18;
139         hash -= n;
140         n <<= 33;
141         hash -= n;
142         n <<= 3;
143         hash += n;
144         n <<= 3;
145         hash -= n;
146         n <<= 4;
147         hash += n;
148         n <<= 2;
149         hash += n;
150
151         /* High bits are more random, so use them. */
152         return hash >> (64 - bits);
153 }
154
155 static inline unsigned long hash_mem_64(char *buf, int length, int bits)
156 {
157         unsigned long hash = 0;
158         unsigned long l = 0;
159         int len = 0;
160         unsigned char c;
161
162         do {
163                 if (len == length) {
164                         c = (char) len;
165                         len = -1;
166                 } else
167                         c = *buf++;
168
169                 l = (l << 8) | c;
170                 len++;
171
172                 if ((len & (BITS_PER_LONG/8-1)) == 0)
173                         hash = gss_hash_64(hash^l, BITS_PER_LONG);
174         } while (len);
175
176         return hash >> (BITS_PER_LONG - bits);
177 }
178 #endif /* BITS_PER_LONG == 64 */
179
180 /****************************************
181  * rpc sec init (rsi) cache             *
182  ****************************************/
183
184 #define RSI_HASHBITS    (6)
185 #define RSI_HASHMAX     (1 << RSI_HASHBITS)
186 #define RSI_HASHMASK    (RSI_HASHMAX - 1)
187
188 static void rsi_entry_init(struct upcall_cache_entry *entry,
189                            void *args)
190 {
191         struct gss_rsi *rsi = &entry->u.rsi;
192         struct gss_rsi *tmp = args;
193
194         rsi->si_uc_entry = entry;
195         rawobj_dup(&rsi->si_in_handle, &tmp->si_in_handle);
196         rawobj_dup(&rsi->si_in_token, &tmp->si_in_token);
197         rsi->si_out_handle = RAWOBJ_EMPTY;
198         rsi->si_out_token = RAWOBJ_EMPTY;
199
200         rsi->si_lustre_svc = tmp->si_lustre_svc;
201         rsi->si_nid4 = tmp->si_nid4;
202         memcpy(rsi->si_nm_name, tmp->si_nm_name, sizeof(tmp->si_nm_name));
203 }
204
205 static void __rsi_free(struct gss_rsi *rsi)
206 {
207         rawobj_free(&rsi->si_in_handle);
208         rawobj_free(&rsi->si_in_token);
209         rawobj_free(&rsi->si_out_handle);
210         rawobj_free(&rsi->si_out_token);
211 }
212
213 static void rsi_entry_free(struct upcall_cache *cache,
214                            struct upcall_cache_entry *entry)
215 {
216         struct gss_rsi *rsi = &entry->u.rsi;
217
218         __rsi_free(rsi);
219 }
220
221 static inline int rsi_entry_hash(struct gss_rsi *rsi)
222 {
223 #if BITS_PER_LONG == 64
224         return hash_mem_64((char *)rsi->si_in_handle.data,
225                            rsi->si_in_handle.len, RSI_HASHBITS) ^
226                 hash_mem_64((char *)rsi->si_in_token.data,
227                             rsi->si_in_token.len, RSI_HASHBITS);
228 #else
229         return hash_mem((char *)rsi->si_in_handle.data, rsi->si_in_handle.len,
230                         RSI_HASHBITS) ^
231                 hash_mem((char *)rsi->si_in_token.data, rsi->si_in_token.len,
232                          RSI_HASHBITS);
233 #endif
234 }
235
236 static inline int __rsi_entry_match(rawobj_t *h1, rawobj_t *h2,
237                                     rawobj_t *t1, rawobj_t *t2)
238 {
239         return !(rawobj_equal(h1, h2) && rawobj_equal(t1, t2));
240 }
241
242 static inline int rsi_entry_match(struct gss_rsi *rsi, struct gss_rsi *tmp)
243 {
244         return __rsi_entry_match(&rsi->si_in_handle, &tmp->si_in_handle,
245                                  &rsi->si_in_token, &tmp->si_in_token);
246 }
247
248 /* Returns 0 to tell this is a match */
249 static inline int rsi_upcall_compare(struct upcall_cache *cache,
250                                      struct upcall_cache_entry *entry,
251                                      __u64 key, void *args)
252 {
253         struct gss_rsi *rsi1 = &entry->u.rsi;
254         struct gss_rsi *rsi2 = args;
255
256         return rsi_entry_match(rsi1, rsi2);
257 }
258
259 /* See handle_channel_request() userspace for where the upcall data is read */
260 static int rsi_do_upcall(struct upcall_cache *cache,
261                          struct upcall_cache_entry *entry)
262 {
263         int size, len, *blen;
264         char *buffer, *bp, **bpp;
265         char *argv[] = {
266                 [0] = cache->uc_upcall,
267                 [1] = "-c",
268                 [2] = cache->uc_name,
269                 [3] = "-r",
270                 [4] = NULL,
271                 [5] = NULL
272         };
273         char *envp[] = {
274                 [0] = "HOME=/",
275                 [1] = "PATH=/sbin:/usr/sbin",
276                 [2] = NULL
277         };
278         ktime_t start, end;
279         struct gss_rsi *rsi = &entry->u.rsi;
280         __u64 index = 0;
281         int rc;
282
283         ENTRY;
284         CDEBUG(D_SEC, "rsi upcall '%s' on '%s'\n",
285                cache->uc_upcall, cache->uc_name);
286
287         size = 24 + 1 + /* ue_key is uint64_t */
288                 12 + 1 + /* si_lustre_svc is __u32*/
289                 18 + 1 + /* si_nid4 is lnet_nid_t, hex with leading 0x */
290                 18 + 1 + /* index is __u64, hex with leading 0x */
291                 strlen(rsi->si_nm_name) + 1 +
292                 BASE64URL_CHARS(rsi->si_in_handle.len) + 1 +
293                 BASE64URL_CHARS(rsi->si_in_token.len) + 1 +
294                 1 + 1; /* eol */
295         if (size > MAX_ARG_STRLEN)
296                 RETURN(-E2BIG);
297         OBD_ALLOC_LARGE(buffer, size);
298         if (!buffer)
299                 RETURN(-ENOMEM);
300
301         bp = buffer;
302         bpp = &bp;
303         len = size;
304         blen = &len;
305
306         /* if in_handle is null, provide kernel suggestion */
307         if (rsi->si_in_handle.len == 0)
308                 index = gss_get_next_ctx_index();
309
310         /* entry->ue_key is put into args sent via upcall, so that it can be
311          * returned by userspace. This will help find cache entry at downcall,
312          * without unnecessary recomputation of the hash.
313          */
314         gss_u64_write_string(bpp, blen, entry->ue_key);
315         gss_u64_write_string(bpp, blen, rsi->si_lustre_svc);
316         gss_u64_write_hex_string(bpp, blen, rsi->si_nid4);
317         gss_u64_write_hex_string(bpp, blen, index);
318         gss_string_write(bpp, blen, (char *) rsi->si_nm_name);
319         gss_base64url_encode(bpp, blen, rsi->si_in_handle.data,
320                              rsi->si_in_handle.len);
321         gss_base64url_encode(bpp, blen, rsi->si_in_token.data,
322                              rsi->si_in_token.len);
323         (*bpp)[-1] = '\n';
324         (*bpp)[0] = '\0';
325
326         argv[4] = buffer;
327         down_read(&cache->uc_upcall_rwsem);
328         start = ktime_get();
329         rc = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
330         end = ktime_get();
331         up_read(&cache->uc_upcall_rwsem);
332         if (rc < 0) {
333                 CERROR("%s: error invoking upcall %s %s (time %ldus): rc = %d\n",
334                        cache->uc_name, argv[0], argv[2],
335                        (long)ktime_us_delta(end, start), rc);
336         } else {
337                 CDEBUG(D_SEC, "%s: invoked upcall %s %s (time %ldus)\n",
338                        cache->uc_name, argv[0], argv[2],
339                        (long)ktime_us_delta(end, start));
340                 rc = 0;
341         }
342
343         OBD_FREE_LARGE(buffer, size);
344         RETURN(rc);
345 }
346
347 static inline int rsi_downcall_compare(struct upcall_cache *cache,
348                                        struct upcall_cache_entry *entry,
349                                        __u64 key, void *args)
350 {
351         struct gss_rsi *rsi = &entry->u.rsi;
352         struct rsi_downcall_data *sid = args;
353         char *mesg = sid->sid_val;
354         rawobj_t handle, token;
355         char *p = mesg;
356         int len;
357
358         /* sid_val starts with handle and token */
359
360         /* First, handle */
361         len = gss_buffer_get(&mesg, &handle.len, &handle.data);
362         sid->sid_offset = mesg - p;
363         p = mesg;
364
365         /* Second, token */
366         len = gss_buffer_get(&mesg, &token.len, &token.data);
367         sid->sid_offset += mesg - p;
368
369         return __rsi_entry_match(&rsi->si_in_handle, &handle,
370                                  &rsi->si_in_token, &token);
371 }
372
373 static int rsi_parse_downcall(struct upcall_cache *cache,
374                               struct upcall_cache_entry *entry,
375                               void *args)
376 {
377         struct gss_rsi *rsi = &entry->u.rsi;
378         struct rsi_downcall_data *sid = args;
379         int mlen = sid->sid_len;
380         char *mesg = sid->sid_val + sid->sid_offset;
381         char *buf = sid->sid_val;
382         int status = -EINVAL;
383         int len;
384
385         ENTRY;
386
387         if (mlen <= 0)
388                 goto out;
389
390         rsi->si_major_status = sid->sid_maj_stat;
391         rsi->si_minor_status = sid->sid_min_stat;
392
393         /* in_handle and in_token have already been consumed in
394          * rsi_downcall_compare(). sid_offset gives next field.
395          */
396
397         /* out_handle */
398         len = gss_buffer_read(&mesg, buf, mlen);
399         if (len < 0)
400                 goto out;
401         if (rawobj_alloc(&rsi->si_out_handle, buf, len)) {
402                 status = -ENOMEM;
403                 goto out;
404         }
405
406         /* out_token */
407         len = gss_buffer_read(&mesg, buf, mlen);
408         if (len < 0)
409                 goto out;
410         if (rawobj_alloc(&rsi->si_out_token, buf, len)) {
411                 status = -ENOMEM;
412                 goto out;
413         }
414
415         entry->ue_expire = 0;
416         status = 0;
417
418 out:
419         CDEBUG(D_OTHER, "rsi parse %p: %d\n", rsi, status);
420         RETURN(status);
421 }
422
423 struct gss_rsi *rsi_entry_get(struct upcall_cache *cache, struct gss_rsi *rsi)
424 {
425         struct upcall_cache_entry *entry;
426         int hash = rsi_entry_hash(rsi);
427
428         if (!cache)
429                 return ERR_PTR(-ENOENT);
430
431         entry = upcall_cache_get_entry(cache, (__u64)hash, rsi);
432         if (unlikely(!entry))
433                 return ERR_PTR(-ENOENT);
434         if (IS_ERR(entry))
435                 return ERR_CAST(entry);
436
437         return &entry->u.rsi;
438 }
439
440 void rsi_entry_put(struct upcall_cache *cache, struct gss_rsi *rsi)
441 {
442         if (!cache || !rsi)
443                 return;
444
445         upcall_cache_put_entry(cache, rsi->si_uc_entry);
446 }
447
448 void rsi_flush(struct upcall_cache *cache, int hash)
449 {
450         if (hash < 0)
451                 upcall_cache_flush_idle(cache);
452         else
453                 upcall_cache_flush_one(cache, (__u64)hash, NULL);
454 }
455
456 struct upcall_cache_ops rsi_upcall_cache_ops = {
457         .init_entry       = rsi_entry_init,
458         .free_entry       = rsi_entry_free,
459         .upcall_compare   = rsi_upcall_compare,
460         .downcall_compare = rsi_downcall_compare,
461         .do_upcall        = rsi_do_upcall,
462         .parse_downcall   = rsi_parse_downcall,
463 };
464
465 struct upcall_cache *rsicache;
466
467 struct rsi {
468         struct cache_head       h;
469         __u32                   lustre_svc;
470         lnet_nid_t              nid4; /* FIXME Support larger NID */
471         char                    nm_name[LUSTRE_NODEMAP_NAME_LENGTH + 1];
472         wait_queue_head_t       waitq;
473         rawobj_t                in_handle, in_token;
474         rawobj_t                out_handle, out_token;
475         int                     major_status, minor_status;
476 #ifdef HAVE_CACHE_HASH_SPINLOCK
477         struct rcu_head         rcu_head;
478 #endif
479 };
480
481 #ifdef HAVE_CACHE_HEAD_HLIST
482 static struct hlist_head rsi_table[RSI_HASHMAX];
483 #else
484 static struct cache_head *rsi_table[RSI_HASHMAX];
485 #endif
486 static struct cache_detail rsi_cache;
487 static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
488 static struct rsi *rsi_lookup(struct rsi *item);
489
490 #ifdef HAVE_CACHE_DETAIL_WRITERS
491 static inline int channel_users(struct cache_detail *cd)
492 {
493         return atomic_read(&cd->writers);
494 }
495 #else
496 static inline int channel_users(struct cache_detail *cd)
497 {
498         return atomic_read(&cd->readers);
499 }
500 #endif
501
502 static inline int rsi_hash(struct rsi *item)
503 {
504         return hash_mem((char *)item->in_handle.data, item->in_handle.len,
505                         RSI_HASHBITS) ^
506                hash_mem((char *)item->in_token.data, item->in_token.len,
507                         RSI_HASHBITS);
508 }
509
510 static inline int __rsi_match(struct rsi *item, struct rsi *tmp)
511 {
512         return (rawobj_equal(&item->in_handle, &tmp->in_handle) &&
513                 rawobj_equal(&item->in_token, &tmp->in_token));
514 }
515
516 static void rsi_free(struct rsi *rsi)
517 {
518         rawobj_free(&rsi->in_handle);
519         rawobj_free(&rsi->in_token);
520         rawobj_free(&rsi->out_handle);
521         rawobj_free(&rsi->out_token);
522 }
523
524 /* See handle_channel_req() userspace for where the upcall data is read */
525 static void rsi_request(struct cache_detail *cd,
526                         struct cache_head *h,
527                         char **bpp, int *blen)
528 {
529         struct rsi *rsi = container_of(h, struct rsi, h);
530         __u64 index = 0;
531
532         /* if in_handle is null, provide kernel suggestion */
533         if (rsi->in_handle.len == 0)
534                 index = gss_get_next_ctx_index();
535
536         qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
537                         sizeof(rsi->lustre_svc));
538         qword_addhex(bpp, blen, (char *) &rsi->nid4, sizeof(rsi->nid4));
539         qword_addhex(bpp, blen, (char *) &index, sizeof(index));
540         qword_addhex(bpp, blen, (char *) rsi->nm_name,
541                      strlen(rsi->nm_name) + 1);
542         qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
543         qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
544         (*bpp)[-1] = '\n';
545 }
546
547 static inline void __rsi_init(struct rsi *new, struct rsi *item)
548 {
549         new->out_handle = RAWOBJ_EMPTY;
550         new->out_token = RAWOBJ_EMPTY;
551
552         new->in_handle = item->in_handle;
553         item->in_handle = RAWOBJ_EMPTY;
554         new->in_token = item->in_token;
555         item->in_token = RAWOBJ_EMPTY;
556
557         new->lustre_svc = item->lustre_svc;
558         new->nid4 = item->nid4;
559         memcpy(new->nm_name, item->nm_name, sizeof(item->nm_name));
560         init_waitqueue_head(&new->waitq);
561 }
562
563 static inline void __rsi_update(struct rsi *new, struct rsi *item)
564 {
565         LASSERT(new->out_handle.len == 0);
566         LASSERT(new->out_token.len == 0);
567
568         new->out_handle = item->out_handle;
569         item->out_handle = RAWOBJ_EMPTY;
570         new->out_token = item->out_token;
571         item->out_token = RAWOBJ_EMPTY;
572
573         new->major_status = item->major_status;
574         new->minor_status = item->minor_status;
575 }
576
577 #ifdef HAVE_CACHE_HASH_SPINLOCK
578 static void rsi_free_rcu(struct rcu_head *head)
579 {
580         struct rsi *rsi = container_of(head, struct rsi, rcu_head);
581
582 #ifdef HAVE_CACHE_HEAD_HLIST
583         LASSERT(hlist_unhashed(&rsi->h.cache_list));
584 #else
585         LASSERT(rsi->h.next == NULL);
586 #endif
587         rsi_free(rsi);
588         OBD_FREE_PTR(rsi);
589 }
590
591 static void rsi_put(struct kref *ref)
592 {
593         struct rsi *rsi = container_of(ref, struct rsi, h.ref);
594
595         call_rcu(&rsi->rcu_head, rsi_free_rcu);
596 }
597 #else /* !HAVE_CACHE_HASH_SPINLOCK */
598 static void rsi_put(struct kref *ref)
599 {
600         struct rsi *rsi = container_of(ref, struct rsi, h.ref);
601
602 #ifdef HAVE_CACHE_HEAD_HLIST
603         LASSERT(hlist_unhashed(&rsi->h.cache_list));
604 #else
605         LASSERT(rsi->h.next == NULL);
606 #endif
607         rsi_free(rsi);
608         OBD_FREE_PTR(rsi);
609 }
610 #endif /* HAVE_CACHE_HASH_SPINLOCK */
611
612 static int rsi_match(struct cache_head *a, struct cache_head *b)
613 {
614         struct rsi *item = container_of(a, struct rsi, h);
615         struct rsi *tmp = container_of(b, struct rsi, h);
616
617         return __rsi_match(item, tmp);
618 }
619
620 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
621 {
622         struct rsi *new = container_of(cnew, struct rsi, h);
623         struct rsi *item = container_of(citem, struct rsi, h);
624
625         __rsi_init(new, item);
626 }
627
628 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
629 {
630         struct rsi *new = container_of(cnew, struct rsi, h);
631         struct rsi *item = container_of(citem, struct rsi, h);
632
633         __rsi_update(new, item);
634 }
635
636 static struct cache_head *rsi_alloc(void)
637 {
638         struct rsi *rsi;
639
640         OBD_ALLOC_PTR(rsi);
641         if (rsi) 
642                 return &rsi->h;
643         else
644                 return NULL;
645 }
646
647 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
648 {
649         char *buf = mesg;
650         int len;
651         struct rsi rsii, *rsip = NULL;
652         time64_t expiry;
653         int status = -EINVAL;
654         ENTRY;
655
656         memset(&rsii, 0, sizeof(rsii));
657
658         /* handle */
659         len = qword_get(&mesg, buf, mlen);
660         if (len < 0)
661                 goto out;
662         if (rawobj_alloc(&rsii.in_handle, buf, len)) {
663                 status = -ENOMEM;
664                 goto out;
665         }
666
667         /* token */
668         len = qword_get(&mesg, buf, mlen);
669         if (len < 0)
670                 goto out;
671         if (rawobj_alloc(&rsii.in_token, buf, len)) {
672                 status = -ENOMEM;
673                 goto out;
674         }
675
676         rsip = rsi_lookup(&rsii);
677         if (!rsip)
678                 goto out;
679         if (!test_bit(CACHE_PENDING, &rsip->h.flags)) {
680                 /* If this is not a pending request, it probably means
681                  * someone wrote arbitrary data to the init channel.
682                  * Directly return -EINVAL in this case.
683                  */
684                 status = -EINVAL;
685                 goto out;
686         }
687
688         rsii.h.flags = 0;
689         /* expiry */
690         status = get_expiry(&mesg, &expiry);
691         if (status)
692                 goto out;
693
694         len = qword_get(&mesg, buf, mlen);
695         if (len <= 0)
696                 goto out;
697
698         /* major */
699         status = kstrtoint(buf, 10, &rsii.major_status);
700         if (status)
701                 goto out;
702
703         /* minor */
704         len = qword_get(&mesg, buf, mlen);
705         if (len <= 0) {
706                 status = -EINVAL;
707                 goto out;
708         }
709
710         status = kstrtoint(buf, 10, &rsii.minor_status);
711         if (status)
712                 goto out;
713
714         /* out_handle */
715         len = qword_get(&mesg, buf, mlen);
716         if (len < 0)
717                 goto out;
718         if (rawobj_alloc(&rsii.out_handle, buf, len)) {
719                 status = -ENOMEM;
720                 goto out;
721         }
722
723         /* out_token */
724         len = qword_get(&mesg, buf, mlen);
725         if (len < 0)
726                 goto out;
727         if (rawobj_alloc(&rsii.out_token, buf, len)) {
728                 status = -ENOMEM;
729                 goto out;
730         }
731
732         rsii.h.expiry_time = expiry;
733         rsip = rsi_update(&rsii, rsip);
734         status = 0;
735 out:
736         rsi_free(&rsii);
737         if (rsip) {
738                 wake_up(&rsip->waitq);
739                 cache_put(&rsip->h, &rsi_cache);
740         } else {
741                 status = -ENOMEM;
742         }
743
744         if (status)
745                 CERROR("rsi parse error %d\n", status);
746         RETURN(status);
747 }
748
749 static struct cache_detail rsi_cache = {
750         .hash_size      = RSI_HASHMAX,
751         .hash_table     = rsi_table,
752         .name           = "auth.sptlrpc.init",
753         .cache_put      = rsi_put,
754         .cache_request  = rsi_request,
755         .cache_upcall   = sunrpc_cache_pipe_upcall,
756         .cache_parse    = rsi_parse,
757         .match          = rsi_match,
758         .init           = rsi_init,
759         .update         = update_rsi,
760         .alloc          = rsi_alloc,
761 };
762
763 static struct rsi *rsi_lookup(struct rsi *item)
764 {
765         struct cache_head *ch;
766         int hash = rsi_hash(item);
767
768         ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
769         if (ch)
770                 return container_of(ch, struct rsi, h);
771         else
772                 return NULL;
773 }
774
775 static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
776 {
777         struct cache_head *ch;
778         int hash = rsi_hash(new);
779
780         ch = sunrpc_cache_update(&rsi_cache, &new->h, &old->h, hash);
781         if (ch)
782                 return container_of(ch, struct rsi, h);
783         else
784                 return NULL;
785 }
786
787 /****************************************
788  * rpc sec context (rsc) cache                            *
789  ****************************************/
790
791 #define RSC_HASHBITS    (10)
792 #define RSC_HASHMAX     (1 << RSC_HASHBITS)
793 #define RSC_HASHMASK    (RSC_HASHMAX - 1)
794
795 struct rsc {
796         struct cache_head       h;
797         struct obd_device      *target;
798         rawobj_t                handle;
799         struct gss_svc_ctx      ctx;
800 #ifdef HAVE_CACHE_HASH_SPINLOCK
801         struct rcu_head         rcu_head;
802 #endif
803 };
804
805 #ifdef HAVE_CACHE_HEAD_HLIST
806 static struct hlist_head rsc_table[RSC_HASHMAX];
807 #else
808 static struct cache_head *rsc_table[RSC_HASHMAX];
809 #endif
810 static struct cache_detail rsc_cache;
811 static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
812 static struct rsc *rsc_lookup(struct rsc *item);
813
814 static void rsc_free(struct rsc *rsci)
815 {
816         rawobj_free(&rsci->handle);
817         rawobj_free(&rsci->ctx.gsc_rvs_hdl);
818         lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
819 }
820
821 static inline int rsc_hash(struct rsc *rsci)
822 {
823         return hash_mem((char *)rsci->handle.data,
824                         rsci->handle.len, RSC_HASHBITS);
825 }
826
827 static inline int __rsc_match(struct rsc *new, struct rsc *tmp)
828 {
829         return rawobj_equal(&new->handle, &tmp->handle);
830 }
831
832 static inline void __rsc_init(struct rsc *new, struct rsc *tmp)
833 {
834         new->handle = tmp->handle;
835         tmp->handle = RAWOBJ_EMPTY;
836
837         new->target = NULL;
838         memset(&new->ctx, 0, sizeof(new->ctx));
839         new->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
840 }
841
842 static inline void __rsc_update(struct rsc *new, struct rsc *tmp)
843 {
844         new->ctx = tmp->ctx;
845         memset(&tmp->ctx, 0, sizeof(tmp->ctx));
846         tmp->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
847         tmp->ctx.gsc_mechctx = NULL;
848         tmp->target = NULL;
849
850         memset(&new->ctx.gsc_seqdata, 0, sizeof(new->ctx.gsc_seqdata));
851         spin_lock_init(&new->ctx.gsc_seqdata.ssd_lock);
852 }
853
854 #ifdef HAVE_CACHE_HASH_SPINLOCK
855 static void rsc_free_rcu(struct rcu_head *head)
856 {
857         struct rsc *rsci = container_of(head, struct rsc, rcu_head);
858
859 #ifdef HAVE_CACHE_HEAD_HLIST
860         LASSERT(hlist_unhashed(&rsci->h.cache_list));
861 #else
862         LASSERT(rsci->h.next == NULL);
863 #endif
864         rawobj_free(&rsci->handle);
865         OBD_FREE_PTR(rsci);
866 }
867
868 static void rsc_put(struct kref *ref)
869 {
870         struct rsc *rsci = container_of(ref, struct rsc, h.ref);
871
872         rawobj_free(&rsci->ctx.gsc_rvs_hdl);
873         lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
874         call_rcu(&rsci->rcu_head, rsc_free_rcu);
875 }
876 #else /* !HAVE_CACHE_HASH_SPINLOCK */
877 static void rsc_put(struct kref *ref)
878 {
879         struct rsc *rsci = container_of(ref, struct rsc, h.ref);
880
881 #ifdef HAVE_CACHE_HEAD_HLIST
882         LASSERT(hlist_unhashed(&rsci->h.cache_list));
883 #else
884         LASSERT(rsci->h.next == NULL);
885 #endif
886         rsc_free(rsci);
887         OBD_FREE_PTR(rsci);
888 }
889 #endif /* HAVE_CACHE_HASH_SPINLOCK */
890
891 static int rsc_match(struct cache_head *a, struct cache_head *b)
892 {
893         struct rsc *new = container_of(a, struct rsc, h);
894         struct rsc *tmp = container_of(b, struct rsc, h);
895
896         return __rsc_match(new, tmp);
897 }
898
899 static void rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
900 {
901         struct rsc *new = container_of(cnew, struct rsc, h);
902         struct rsc *tmp = container_of(ctmp, struct rsc, h);
903
904         __rsc_init(new, tmp);
905 }
906
907 static void update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
908 {
909         struct rsc *new = container_of(cnew, struct rsc, h);
910         struct rsc *tmp = container_of(ctmp, struct rsc, h);
911
912         __rsc_update(new, tmp);
913 }
914
915 static struct cache_head * rsc_alloc(void)
916 {
917         struct rsc *rsc;
918
919         OBD_ALLOC_PTR(rsc);
920         if (rsc)
921                 return &rsc->h;
922         else
923                 return NULL;
924 }
925
926 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
927 {
928         char *buf = mesg;
929         int len, rv, tmp_int;
930         struct rsc rsci, *rscp = NULL;
931         time64_t expiry;
932         int status = -EINVAL;
933         struct gss_api_mech *gm = NULL;
934
935         memset(&rsci, 0, sizeof(rsci));
936
937         /* context handle */
938         len = qword_get(&mesg, buf, mlen);
939         if (len < 0)
940                 goto out;
941
942         status = -ENOMEM;
943         if (rawobj_alloc(&rsci.handle, buf, len))
944                 goto out;
945
946         rsci.h.flags = 0;
947         /* expiry */
948         status = get_expiry(&mesg, &expiry);
949         if (status)
950                 goto out;
951
952         status = -EINVAL;
953         /* remote flag */
954         rv = get_int(&mesg, &tmp_int);
955         if (rv) {
956                 CERROR("fail to get remote flag\n");
957                 goto out;
958         }
959         rsci.ctx.gsc_remote = (tmp_int != 0);
960
961         /* root user flag */
962         rv = get_int(&mesg, &tmp_int);
963         if (rv) {
964                 CERROR("fail to get root user flag\n");
965                 goto out;
966         }
967         rsci.ctx.gsc_usr_root = (tmp_int != 0);
968
969         /* mds user flag */
970         rv = get_int(&mesg, &tmp_int);
971         if (rv) {
972                 CERROR("fail to get mds user flag\n");
973                 goto out;
974         }
975         rsci.ctx.gsc_usr_mds = (tmp_int != 0);
976
977         /* oss user flag */
978         rv = get_int(&mesg, &tmp_int);
979         if (rv) {
980                 CERROR("fail to get oss user flag\n");
981                 goto out;
982         }
983         rsci.ctx.gsc_usr_oss = (tmp_int != 0);
984
985         /* mapped uid */
986         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
987         if (rv) {
988                 CERROR("fail to get mapped uid\n");
989                 goto out;
990         }
991
992         rscp = rsc_lookup(&rsci);
993         if (!rscp)
994                 goto out;
995
996         /* uid, or NEGATIVE */
997         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
998         if (rv == -EINVAL)
999                 goto out;
1000         if (rv == -ENOENT) {
1001                 CERROR("NOENT? set rsc entry negative\n");
1002                 set_bit(CACHE_NEGATIVE, &rsci.h.flags);
1003         } else {
1004                 rawobj_t tmp_buf;
1005                 time64_t ctx_expiry;
1006
1007                 /* gid */
1008                 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
1009                         goto out;
1010
1011                 /* mech name */
1012                 len = qword_get(&mesg, buf, mlen);
1013                 if (len < 0)
1014                         goto out;
1015                 gm = lgss_name_to_mech(buf);
1016                 status = -EOPNOTSUPP;
1017                 if (!gm)
1018                         goto out;
1019
1020                 status = -EINVAL;
1021                 /* mech-specific data: */
1022                 len = qword_get(&mesg, buf, mlen);
1023                 if (len < 0)
1024                         goto out;
1025
1026                 tmp_buf.len = len;
1027                 tmp_buf.data = (unsigned char *)buf;
1028                 if (lgss_import_sec_context(&tmp_buf, gm,
1029                                             &rsci.ctx.gsc_mechctx))
1030                         goto out;
1031
1032                 /* set to seconds since machine booted */
1033                 expiry = ktime_get_seconds();
1034
1035                 /* currently the expiry time passed down from user-space
1036                  * is invalid, here we retrive it from mech.
1037                  */
1038                 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
1039                         CERROR("unable to get expire time, drop it\n");
1040                         goto out;
1041                 }
1042
1043                 /* ctx_expiry is the number of seconds since Jan 1 1970.
1044                  * We want just the  number of seconds into the future.
1045                  */
1046                 expiry += ctx_expiry - ktime_get_real_seconds();
1047         }
1048
1049         rsci.h.expiry_time = expiry;
1050         rscp = rsc_update(&rsci, rscp);
1051         status = 0;
1052 out:
1053         if (gm)
1054                 lgss_mech_put(gm);
1055         rsc_free(&rsci);
1056         if (rscp)
1057                 cache_put(&rscp->h, &rsc_cache);
1058         else
1059                 status = -ENOMEM;
1060
1061         if (status)
1062                 CERROR("parse rsc error %d\n", status);
1063         return status;
1064 }
1065
1066 static struct cache_detail rsc_cache = {
1067         .hash_size      = RSC_HASHMAX,
1068         .hash_table     = rsc_table,
1069         .name           = "auth.sptlrpc.context",
1070         .cache_put      = rsc_put,
1071         .cache_parse    = rsc_parse,
1072         .match          = rsc_match,
1073         .init           = rsc_init,
1074         .update         = update_rsc,
1075         .alloc          = rsc_alloc,
1076 };
1077
1078 static struct rsc *rsc_lookup(struct rsc *item)
1079 {
1080         struct cache_head *ch;
1081         int                hash = rsc_hash(item);
1082
1083         ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
1084         if (ch)
1085                 return container_of(ch, struct rsc, h);
1086         else
1087                 return NULL;
1088 }
1089
1090 static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
1091 {
1092         struct cache_head *ch;
1093         int                hash = rsc_hash(new);
1094
1095         ch = sunrpc_cache_update(&rsc_cache, &new->h, &old->h, hash);
1096         if (ch)
1097                 return container_of(ch, struct rsc, h);
1098         else
1099                 return NULL;
1100 }
1101
1102 #define COMPAT_RSC_PUT(item, cd)        cache_put((item), (cd))
1103
1104 /****************************************
1105  * rsc cache flush                      *
1106  ****************************************/
1107
1108 static struct rsc *gss_svc_searchbyctx(rawobj_t *handle)
1109 {
1110         struct rsc  rsci;
1111         struct rsc *found;
1112
1113         memset(&rsci, 0, sizeof(rsci));
1114         if (rawobj_dup(&rsci.handle, handle))
1115                 return NULL;
1116
1117         found = rsc_lookup(&rsci);
1118         rsc_free(&rsci);
1119         if (!found)
1120                 return NULL;
1121         if (cache_check(&rsc_cache, &found->h, NULL))
1122                 return NULL;
1123         return found;
1124 }
1125
1126 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
1127                                    struct gss_sec *gsec,
1128                                    struct gss_cli_ctx *gctx)
1129 {
1130         struct rsc      rsci, *rscp = NULL;
1131         time64_t ctx_expiry;
1132         __u32           major;
1133         int             rc;
1134         ENTRY;
1135
1136         memset(&rsci, 0, sizeof(rsci));
1137
1138         if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
1139                          sizeof(gsec->gs_rvs_hdl)))
1140                 GOTO(out, rc = -ENOMEM);
1141
1142         rscp = rsc_lookup(&rsci);
1143         if (rscp == NULL)
1144                 GOTO(out, rc = -ENOMEM);
1145
1146         major = lgss_copy_reverse_context(gctx->gc_mechctx,
1147                                           &rsci.ctx.gsc_mechctx);
1148         if (major != GSS_S_COMPLETE)
1149                 GOTO(out, rc = -ENOMEM);
1150
1151         if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
1152                 CERROR("unable to get expire time, drop it\n");
1153                 GOTO(out, rc = -EINVAL);
1154         }
1155         rsci.h.expiry_time = ctx_expiry;
1156
1157         switch (imp->imp_obd->u.cli.cl_sp_to) {
1158         case LUSTRE_SP_MDT:
1159                 rsci.ctx.gsc_usr_mds = 1;
1160                 break;
1161         case LUSTRE_SP_OST:
1162                 rsci.ctx.gsc_usr_oss = 1;
1163                 break;
1164         case LUSTRE_SP_CLI:
1165                 rsci.ctx.gsc_usr_root = 1;
1166                 break;
1167         case LUSTRE_SP_MGS:
1168                 /* by convention, all 3 set to 1 means MGS */
1169                 rsci.ctx.gsc_usr_mds = 1;
1170                 rsci.ctx.gsc_usr_oss = 1;
1171                 rsci.ctx.gsc_usr_root = 1;
1172                 break;
1173         default:
1174                 break;
1175         }
1176
1177         rscp = rsc_update(&rsci, rscp);
1178         if (rscp == NULL)
1179                 GOTO(out, rc = -ENOMEM);
1180
1181         rscp->target = imp->imp_obd;
1182         rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
1183
1184         CDEBUG(D_SEC, "create reverse svc ctx %p to %s: idx %#llx\n",
1185                &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
1186         rc = 0;
1187 out:
1188         if (rscp)
1189                 cache_put(&rscp->h, &rsc_cache);
1190         rsc_free(&rsci);
1191
1192         if (rc)
1193                 CERROR("create reverse svc ctx: idx %#llx, rc %d\n",
1194                        gsec->gs_rvs_hdl, rc);
1195         RETURN(rc);
1196 }
1197
1198 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle)
1199 {
1200         const time64_t expire = 20;
1201         struct rsc *rscp;
1202
1203         rscp = gss_svc_searchbyctx(handle);
1204         if (rscp) {
1205                 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) expire soon\n",
1206                        &rscp->ctx, rscp);
1207
1208                 rscp->h.expiry_time = ktime_get_real_seconds() + expire;
1209                 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
1210         }
1211         return 0;
1212 }
1213
1214 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx)
1215 {
1216         struct rsc *rscp = container_of(ctx, struct rsc, ctx);
1217
1218         return rawobj_dup(handle, &rscp->handle);
1219 }
1220
1221 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq)
1222 {
1223         struct rsc             *rscp;
1224
1225         rscp = gss_svc_searchbyctx(handle);
1226         if (rscp) {
1227                 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) update seq to %u\n",
1228                        &rscp->ctx, rscp, seq + 1);
1229
1230                 rscp->ctx.gsc_rvs_seq = seq + 1;
1231                 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
1232         }
1233         return 0;
1234 }
1235
1236 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
1237                                struct gss_svc_reqctx *grctx,
1238                                struct gss_wire_ctx *gw,
1239                                struct obd_device *target,
1240                                __u32 lustre_svc,
1241                                rawobj_t *rvs_hdl,
1242                                rawobj_t *in_token)
1243 {
1244         struct gss_rsi rsi = { 0 }, *rsip = NULL;
1245         struct ptlrpc_reply_state *rs;
1246         struct rsc *rsci = NULL;
1247         int replen = sizeof(struct ptlrpc_body);
1248         struct gss_rep_header *rephdr;
1249         int rc = SECSVC_DROP, rc2;
1250
1251         ENTRY;
1252
1253         rsi.si_lustre_svc = lustre_svc;
1254         /* In case of MR, rq_peer is not the NID from which request is received,
1255          * but primary NID of peer.
1256          * So we need LNetPrimaryNID(rq_source) to match what the clients uses.
1257          */
1258         LNetPrimaryNID(&req->rq_source.nid);
1259         rsi.si_nid4 = lnet_nid_to_nid4(&req->rq_source.nid);
1260         nodemap_test_nid(lnet_nid_to_nid4(&req->rq_peer.nid), rsi.si_nm_name,
1261                          sizeof(rsi.si_nm_name));
1262
1263         /* Note that context handle is always 0 for for INIT. */
1264         rc2 = rawobj_dup(&rsi.si_in_handle, &gw->gw_handle);
1265         if (rc2) {
1266                 CERROR("%s: failed to duplicate context handle: rc = %d\n",
1267                        target->obd_name, rc2);
1268                 GOTO(out, rc);
1269         }
1270
1271         rc2 = rawobj_dup(&rsi.si_in_token, in_token);
1272         if (rc2) {
1273                 CERROR("%s: failed to duplicate token: rc = %d\n",
1274                        target->obd_name, rc2);
1275                 rawobj_free(&rsi.si_in_handle);
1276                 GOTO(out, rc);
1277         }
1278
1279         rsip = rsi_entry_get(rsicache, &rsi);
1280         __rsi_free(&rsi);
1281         if (IS_ERR(rsip)) {
1282                 CERROR("%s: failed to get entry from rsi cache (nid %s): rc = %ld\n",
1283                        target->obd_name,
1284                        libcfs_nid2str(lnet_nid_to_nid4(&req->rq_source.nid)),
1285                        PTR_ERR(rsip));
1286
1287                 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
1288                         rc = SECSVC_COMPLETE;
1289
1290                 GOTO(out, rc);
1291         }
1292
1293         rc = SECSVC_DROP;
1294         rsci = gss_svc_searchbyctx(&rsip->si_out_handle);
1295         if (!rsci) {
1296                 /* gss mechanism returned major and minor code so we return
1297                  * those in error message */
1298                 if (!gss_pack_err_notify(req, rsip->si_major_status,
1299                                          rsip->si_minor_status))
1300                         rc = SECSVC_COMPLETE;
1301
1302                 CERROR("%s: authentication failed: rc = %d\n",
1303                        target->obd_name, rc);
1304                 GOTO(out, rc);
1305         } else {
1306                 cache_get(&rsci->h);
1307                 grctx->src_ctx = &rsci->ctx;
1308         }
1309
1310         if (gw->gw_flags & LUSTRE_GSS_PACK_KCSUM) {
1311                 grctx->src_ctx->gsc_mechctx->hash_func = gss_digest_hash;
1312         } else if (!strcmp(grctx->src_ctx->gsc_mechctx->mech_type->gm_name,
1313                            "krb5") &&
1314                    !krb5_allow_old_client_csum) {
1315                 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",
1316                       target->obd_name, libcfs_nidstr(&req->rq_peer.nid),
1317                       -EPROTO);
1318                 GOTO(out, rc = SECSVC_DROP);
1319         } else {
1320                 grctx->src_ctx->gsc_mechctx->hash_func =
1321                         gss_digest_hash_compat;
1322         }
1323
1324         if (rawobj_dup(&rsci->ctx.gsc_rvs_hdl, rvs_hdl)) {
1325                 CERROR("%s: failed duplicate reverse handle\n",
1326                        target->obd_name);
1327                 GOTO(out, rc);
1328         }
1329
1330         rsci->target = target;
1331
1332         CDEBUG(D_SEC, "%s: server create rsc %p(%u->%s)\n",
1333                target->obd_name, rsci, rsci->ctx.gsc_uid,
1334                libcfs_nidstr(&req->rq_peer.nid));
1335
1336         if (rsip->si_out_handle.len > PTLRPC_GSS_MAX_HANDLE_SIZE) {
1337                 CERROR("%s: handle size %u too large\n",
1338                        target->obd_name, rsip->si_out_handle.len);
1339                 GOTO(out, rc = SECSVC_DROP);
1340         }
1341
1342         grctx->src_init = 1;
1343         grctx->src_reserve_len = round_up(rsip->si_out_token.len, 4);
1344
1345         rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1346         if (rc) {
1347                 CERROR("%s: failed to pack reply: rc = %d\n",
1348                        target->obd_name, rc);
1349                 GOTO(out, rc = SECSVC_DROP);
1350         }
1351
1352         rs = req->rq_reply_state;
1353         LASSERT(rs->rs_repbuf->lm_bufcount == 3);
1354         LASSERT(rs->rs_repbuf->lm_buflens[0] >=
1355                 sizeof(*rephdr) + rsip->si_out_handle.len);
1356         LASSERT(rs->rs_repbuf->lm_buflens[2] >= rsip->si_out_token.len);
1357
1358         rephdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1359         rephdr->gh_version = PTLRPC_GSS_VERSION;
1360         rephdr->gh_flags = 0;
1361         rephdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1362         rephdr->gh_major = rsip->si_major_status;
1363         rephdr->gh_minor = rsip->si_minor_status;
1364         rephdr->gh_seqwin = GSS_SEQ_WIN;
1365         rephdr->gh_handle.len = rsip->si_out_handle.len;
1366         memcpy(rephdr->gh_handle.data, rsip->si_out_handle.data,
1367                rsip->si_out_handle.len);
1368
1369         memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0), rsip->si_out_token.data,
1370                rsip->si_out_token.len);
1371
1372         rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
1373                                                rsip->si_out_token.len, 0);
1374
1375         rc = SECSVC_OK;
1376
1377 out:
1378         if (!IS_ERR_OR_NULL(rsip))
1379                 rsi_entry_put(rsicache, rsip);
1380         if (rsci) {
1381                 /* if anything went wrong, we don't keep the context too */
1382                 if (rc != SECSVC_OK)
1383                         set_bit(CACHE_NEGATIVE, &rsci->h.flags);
1384                 else
1385                         CDEBUG(D_SEC, "%s: create rsc with idx %#llx\n",
1386                                target->obd_name,
1387                                gss_handle_to_u64(&rsci->handle));
1388
1389                 COMPAT_RSC_PUT(&rsci->h, &rsc_cache);
1390         }
1391         RETURN(rc);
1392 }
1393
1394 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
1395                                            struct gss_wire_ctx *gw)
1396 {
1397         struct rsc *rsc;
1398
1399         rsc = gss_svc_searchbyctx(&gw->gw_handle);
1400         if (!rsc) {
1401                 CWARN("Invalid gss ctx idx %#llx from %s\n",
1402                       gss_handle_to_u64(&gw->gw_handle),
1403                       libcfs_nidstr(&req->rq_peer.nid));
1404                 return NULL;
1405         }
1406
1407         return &rsc->ctx;
1408 }
1409
1410 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx)
1411 {
1412         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1413
1414         COMPAT_RSC_PUT(&rsc->h, &rsc_cache);
1415 }
1416
1417 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx)
1418 {
1419         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1420
1421         /* can't be found */
1422         set_bit(CACHE_NEGATIVE, &rsc->h.flags);
1423         /* to be removed at next scan */
1424         rsc->h.expiry_time = 1;
1425 }
1426
1427 /* Wait for userspace daemon to open socket, approx 1.5 s.
1428  * If socket is not open, upcall requests might fail.
1429  */
1430 static int check_gssd_socket(void)
1431 {
1432         struct sockaddr_un *sun;
1433         struct socket *sock;
1434         int tries = 0;
1435         int err;
1436
1437 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
1438         err = sock_create_kern(current->nsproxy->net_ns,
1439                                AF_UNIX, SOCK_STREAM, 0, &sock);
1440 #else
1441         err = sock_create_kern(AF_UNIX, SOCK_STREAM, 0, &sock);
1442 #endif
1443         if (err < 0) {
1444                 CDEBUG(D_SEC, "Failed to create socket: %d\n", err);
1445                 return err;
1446         }
1447
1448         OBD_ALLOC(sun, sizeof(*sun));
1449         if (!sun) {
1450                 sock_release(sock);
1451                 return -ENOMEM;
1452         }
1453         memset(sun, 0, sizeof(*sun));
1454         sun->sun_family = AF_UNIX;
1455         strncpy(sun->sun_path, GSS_SOCKET_PATH, sizeof(sun->sun_path));
1456
1457         /* Try to connect to the socket */
1458         while (tries++ < 6) {
1459                 err = kernel_connect(sock, (struct sockaddr *)sun,
1460                                      sizeof(*sun), 0);
1461                 if (!err)
1462                         break;
1463                 schedule_timeout_uninterruptible(cfs_time_seconds(1) / 4);
1464         }
1465         if (err < 0)
1466                 CDEBUG(D_SEC, "Failed to connect to socket: %d\n", err);
1467         else
1468                 kernel_sock_shutdown(sock, SHUT_RDWR);
1469
1470         sock_release(sock);
1471         OBD_FREE(sun, sizeof(*sun));
1472         return err;
1473 }
1474
1475 int __init gss_init_svc_upcall(void)
1476 {
1477         int rc;
1478
1479         /*
1480          * this helps reducing context index confliction. after server reboot,
1481          * conflicting request from clients might be filtered out by initial
1482          * sequence number checking, thus no chance to sent error notification
1483          * back to clients.
1484          */
1485         get_random_bytes(&__ctx_index, sizeof(__ctx_index));
1486
1487 #ifdef HAVE_CACHE_HEAD_HLIST
1488         for (rc = 0; rc < rsi_cache.hash_size; rc++)
1489                 INIT_HLIST_HEAD(&rsi_cache.hash_table[rc]);
1490 #endif
1491         rc = cache_register_net(&rsi_cache, &init_net);
1492         if (rc != 0)
1493                 return rc;
1494
1495 #ifdef HAVE_CACHE_HEAD_HLIST
1496         for (rc = 0; rc < rsc_cache.hash_size; rc++)
1497                 INIT_HLIST_HEAD(&rsc_cache.hash_table[rc]);
1498 #endif
1499         rc = cache_register_net(&rsc_cache, &init_net);
1500         if (rc != 0) {
1501                 cache_unregister_net(&rsi_cache, &init_net);
1502                 return rc;
1503         }
1504
1505         rsicache = upcall_cache_init(RSI_CACHE_NAME, RSI_UPCALL_PATH,
1506                                      UC_RSICACHE_HASH_SIZE,
1507                                      3600, /* entry expire: 1 h */
1508                                      20, /* acquire expire: 20 s */
1509                                      &rsi_upcall_cache_ops);
1510         if (IS_ERR(rsicache)) {
1511                 rc = PTR_ERR(rsicache);
1512                 rsicache = NULL;
1513                 return rc;
1514         }
1515
1516         if (check_gssd_socket())
1517                 CDEBUG(D_SEC,
1518                        "Init channel not opened by lsvcgssd, GSS might not work on server side until daemon is active\n");
1519
1520         return 0;
1521 }
1522
1523 void gss_exit_svc_upcall(void)
1524 {
1525         cache_purge(&rsi_cache);
1526         cache_unregister_net(&rsi_cache, &init_net);
1527
1528         cache_purge(&rsc_cache);
1529         cache_unregister_net(&rsc_cache, &init_net);
1530
1531         upcall_cache_cleanup(rsicache);
1532 }