Merge tag 'ntb-4.8' of git://github.com/jonmason/ntb
[cascardo/linux.git] / drivers / staging / lustre / lustre / ptlrpc / sec.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ptlrpc/sec.c
33  *
34  * Author: Eric Mei <ericm@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_SEC
38
39 #include "../../include/linux/libcfs/libcfs.h"
40 #include <linux/crypto.h>
41 #include <linux/key.h>
42
43 #include "../include/obd.h"
44 #include "../include/obd_class.h"
45 #include "../include/obd_support.h"
46 #include "../include/lustre_net.h"
47 #include "../include/lustre_import.h"
48 #include "../include/lustre_dlm.h"
49 #include "../include/lustre_sec.h"
50
51 #include "ptlrpc_internal.h"
52
53 /***********************************************
54  * policy registers                         *
55  ***********************************************/
56
57 static rwlock_t policy_lock;
58 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
59         NULL,
60 };
61
62 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
63 {
64         __u16 number = policy->sp_policy;
65
66         LASSERT(policy->sp_name);
67         LASSERT(policy->sp_cops);
68         LASSERT(policy->sp_sops);
69
70         if (number >= SPTLRPC_POLICY_MAX)
71                 return -EINVAL;
72
73         write_lock(&policy_lock);
74         if (unlikely(policies[number])) {
75                 write_unlock(&policy_lock);
76                 return -EALREADY;
77         }
78         policies[number] = policy;
79         write_unlock(&policy_lock);
80
81         CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
82         return 0;
83 }
84 EXPORT_SYMBOL(sptlrpc_register_policy);
85
86 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
87 {
88         __u16 number = policy->sp_policy;
89
90         LASSERT(number < SPTLRPC_POLICY_MAX);
91
92         write_lock(&policy_lock);
93         if (unlikely(!policies[number])) {
94                 write_unlock(&policy_lock);
95                 CERROR("%s: already unregistered\n", policy->sp_name);
96                 return -EINVAL;
97         }
98
99         LASSERT(policies[number] == policy);
100         policies[number] = NULL;
101         write_unlock(&policy_lock);
102
103         CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
104         return 0;
105 }
106 EXPORT_SYMBOL(sptlrpc_unregister_policy);
107
108 static
109 struct ptlrpc_sec_policy *sptlrpc_wireflavor2policy(__u32 flavor)
110 {
111         static DEFINE_MUTEX(load_mutex);
112         static atomic_t loaded = ATOMIC_INIT(0);
113         struct ptlrpc_sec_policy *policy;
114         __u16 number = SPTLRPC_FLVR_POLICY(flavor);
115         __u16 flag = 0;
116
117         if (number >= SPTLRPC_POLICY_MAX)
118                 return NULL;
119
120         while (1) {
121                 read_lock(&policy_lock);
122                 policy = policies[number];
123                 if (policy && !try_module_get(policy->sp_owner))
124                         policy = NULL;
125                 if (!policy)
126                         flag = atomic_read(&loaded);
127                 read_unlock(&policy_lock);
128
129                 if (policy || flag != 0 ||
130                     number != SPTLRPC_POLICY_GSS)
131                         break;
132
133                 /* try to load gss module, once */
134                 mutex_lock(&load_mutex);
135                 if (atomic_read(&loaded) == 0) {
136                         if (request_module("ptlrpc_gss") == 0)
137                                 CDEBUG(D_SEC,
138                                        "module ptlrpc_gss loaded on demand\n");
139                         else
140                                 CERROR("Unable to load module ptlrpc_gss\n");
141
142                         atomic_set(&loaded, 1);
143                 }
144                 mutex_unlock(&load_mutex);
145         }
146
147         return policy;
148 }
149
150 __u32 sptlrpc_name2flavor_base(const char *name)
151 {
152         if (!strcmp(name, "null"))
153                 return SPTLRPC_FLVR_NULL;
154         if (!strcmp(name, "plain"))
155                 return SPTLRPC_FLVR_PLAIN;
156         if (!strcmp(name, "krb5n"))
157                 return SPTLRPC_FLVR_KRB5N;
158         if (!strcmp(name, "krb5a"))
159                 return SPTLRPC_FLVR_KRB5A;
160         if (!strcmp(name, "krb5i"))
161                 return SPTLRPC_FLVR_KRB5I;
162         if (!strcmp(name, "krb5p"))
163                 return SPTLRPC_FLVR_KRB5P;
164
165         return SPTLRPC_FLVR_INVALID;
166 }
167 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
168
169 const char *sptlrpc_flavor2name_base(__u32 flvr)
170 {
171         __u32   base = SPTLRPC_FLVR_BASE(flvr);
172
173         if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
174                 return "null";
175         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
176                 return "plain";
177         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
178                 return "krb5n";
179         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
180                 return "krb5a";
181         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
182                 return "krb5i";
183         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
184                 return "krb5p";
185
186         CERROR("invalid wire flavor 0x%x\n", flvr);
187         return "invalid";
188 }
189 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
190
191 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
192                                char *buf, int bufsize)
193 {
194         if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
195                 snprintf(buf, bufsize, "hash:%s",
196                          sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
197         else
198                 snprintf(buf, bufsize, "%s",
199                          sptlrpc_flavor2name_base(sf->sf_rpc));
200
201         buf[bufsize - 1] = '\0';
202         return buf;
203 }
204 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
205
206 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
207 {
208         strlcpy(buf, sptlrpc_flavor2name_base(sf->sf_rpc), bufsize);
209
210         /*
211          * currently we don't support customized bulk specification for
212          * flavors other than plain
213          */
214         if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
215                 char bspec[16];
216
217                 bspec[0] = '-';
218                 sptlrpc_flavor2name_bulk(sf, &bspec[1], sizeof(bspec) - 1);
219                 strlcat(buf, bspec, bufsize);
220         }
221
222         return buf;
223 }
224 EXPORT_SYMBOL(sptlrpc_flavor2name);
225
226 static char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
227 {
228         buf[0] = '\0';
229
230         if (flags & PTLRPC_SEC_FL_REVERSE)
231                 strlcat(buf, "reverse,", bufsize);
232         if (flags & PTLRPC_SEC_FL_ROOTONLY)
233                 strlcat(buf, "rootonly,", bufsize);
234         if (flags & PTLRPC_SEC_FL_UDESC)
235                 strlcat(buf, "udesc,", bufsize);
236         if (flags & PTLRPC_SEC_FL_BULK)
237                 strlcat(buf, "bulk,", bufsize);
238         if (buf[0] == '\0')
239                 strlcat(buf, "-,", bufsize);
240
241         return buf;
242 }
243
244 /**************************************************
245  * client context APIs                      *
246  **************************************************/
247
248 static
249 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
250 {
251         struct vfs_cred vcred;
252         int create = 1, remove_dead = 1;
253
254         LASSERT(sec);
255         LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
256
257         if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
258                                      PTLRPC_SEC_FL_ROOTONLY)) {
259                 vcred.vc_uid = 0;
260                 vcred.vc_gid = 0;
261                 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
262                         create = 0;
263                         remove_dead = 0;
264                 }
265         } else {
266                 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
267                 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
268         }
269
270         return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
271                                                    create, remove_dead);
272 }
273
274 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
275 {
276         atomic_inc(&ctx->cc_refcount);
277         return ctx;
278 }
279 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
280
281 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
282 {
283         struct ptlrpc_sec *sec = ctx->cc_sec;
284
285         LASSERT(sec);
286         LASSERT_ATOMIC_POS(&ctx->cc_refcount);
287
288         if (!atomic_dec_and_test(&ctx->cc_refcount))
289                 return;
290
291         sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
292 }
293 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
294
295 static int import_sec_check_expire(struct obd_import *imp)
296 {
297         int adapt = 0;
298
299         spin_lock(&imp->imp_lock);
300         if (imp->imp_sec_expire &&
301             imp->imp_sec_expire < ktime_get_real_seconds()) {
302                 adapt = 1;
303                 imp->imp_sec_expire = 0;
304         }
305         spin_unlock(&imp->imp_lock);
306
307         if (!adapt)
308                 return 0;
309
310         CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
311         return sptlrpc_import_sec_adapt(imp, NULL, NULL);
312 }
313
314 static int import_sec_validate_get(struct obd_import *imp,
315                                    struct ptlrpc_sec **sec)
316 {
317         int rc;
318
319         if (unlikely(imp->imp_sec_expire)) {
320                 rc = import_sec_check_expire(imp);
321                 if (rc)
322                         return rc;
323         }
324
325         *sec = sptlrpc_import_sec_ref(imp);
326         if (!*sec) {
327                 CERROR("import %p (%s) with no sec\n",
328                        imp, ptlrpc_import_state_name(imp->imp_state));
329                 return -EACCES;
330         }
331
332         if (unlikely((*sec)->ps_dying)) {
333                 CERROR("attempt to use dying sec %p\n", sec);
334                 sptlrpc_sec_put(*sec);
335                 return -EACCES;
336         }
337
338         return 0;
339 }
340
341 /**
342  * Given a \a req, find or allocate a appropriate context for it.
343  * \pre req->rq_cli_ctx == NULL.
344  *
345  * \retval 0 succeed, and req->rq_cli_ctx is set.
346  * \retval -ev error number, and req->rq_cli_ctx == NULL.
347  */
348 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
349 {
350         struct obd_import *imp = req->rq_import;
351         struct ptlrpc_sec *sec;
352         int             rc;
353
354         LASSERT(!req->rq_cli_ctx);
355         LASSERT(imp);
356
357         rc = import_sec_validate_get(imp, &sec);
358         if (rc)
359                 return rc;
360
361         req->rq_cli_ctx = get_my_ctx(sec);
362
363         sptlrpc_sec_put(sec);
364
365         if (!req->rq_cli_ctx) {
366                 CERROR("req %p: fail to get context\n", req);
367                 return -ENOMEM;
368         }
369
370         return 0;
371 }
372
373 /**
374  * Drop the context for \a req.
375  * \pre req->rq_cli_ctx != NULL.
376  * \post req->rq_cli_ctx == NULL.
377  *
378  * If \a sync == 0, this function should return quickly without sleep;
379  * otherwise it might trigger and wait for the whole process of sending
380  * an context-destroying rpc to server.
381  */
382 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
383 {
384         LASSERT(req);
385         LASSERT(req->rq_cli_ctx);
386
387         /* request might be asked to release earlier while still
388          * in the context waiting list.
389          */
390         if (!list_empty(&req->rq_ctx_chain)) {
391                 spin_lock(&req->rq_cli_ctx->cc_lock);
392                 list_del_init(&req->rq_ctx_chain);
393                 spin_unlock(&req->rq_cli_ctx->cc_lock);
394         }
395
396         sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
397         req->rq_cli_ctx = NULL;
398 }
399
400 static
401 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
402                            struct ptlrpc_cli_ctx *oldctx,
403                            struct ptlrpc_cli_ctx *newctx)
404 {
405         struct sptlrpc_flavor old_flvr;
406         char *reqmsg = NULL; /* to workaround old gcc */
407         int reqmsg_size;
408         int rc = 0;
409
410         LASSERT(req->rq_reqmsg);
411         LASSERT(req->rq_reqlen);
412         LASSERT(req->rq_replen);
413
414         CDEBUG(D_SEC, "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), switch sec %p(%s) -> %p(%s)\n",
415                req,
416                oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
417                newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
418                oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
419                newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
420
421         /* save flavor */
422         old_flvr = req->rq_flvr;
423
424         /* save request message */
425         reqmsg_size = req->rq_reqlen;
426         if (reqmsg_size != 0) {
427                 reqmsg = libcfs_kvzalloc(reqmsg_size, GFP_NOFS);
428                 if (!reqmsg)
429                         return -ENOMEM;
430                 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
431         }
432
433         /* release old req/rep buf */
434         req->rq_cli_ctx = oldctx;
435         sptlrpc_cli_free_reqbuf(req);
436         sptlrpc_cli_free_repbuf(req);
437         req->rq_cli_ctx = newctx;
438
439         /* recalculate the flavor */
440         sptlrpc_req_set_flavor(req, 0);
441
442         /* alloc new request buffer
443          * we don't need to alloc reply buffer here, leave it to the
444          * rest procedure of ptlrpc
445          */
446         if (reqmsg_size != 0) {
447                 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
448                 if (!rc) {
449                         LASSERT(req->rq_reqmsg);
450                         memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
451                 } else {
452                         CWARN("failed to alloc reqbuf: %d\n", rc);
453                         req->rq_flvr = old_flvr;
454                 }
455
456                 kvfree(reqmsg);
457         }
458         return rc;
459 }
460
461 /**
462  * If current context of \a req is dead somehow, e.g. we just switched flavor
463  * thus marked original contexts dead, we'll find a new context for it. if
464  * no switch is needed, \a req will end up with the same context.
465  *
466  * \note a request must have a context, to keep other parts of code happy.
467  * In any case of failure during the switching, we must restore the old one.
468  */
469 static int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
470 {
471         struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
472         struct ptlrpc_cli_ctx *newctx;
473         int rc;
474
475         LASSERT(oldctx);
476
477         sptlrpc_cli_ctx_get(oldctx);
478         sptlrpc_req_put_ctx(req, 0);
479
480         rc = sptlrpc_req_get_ctx(req);
481         if (unlikely(rc)) {
482                 LASSERT(!req->rq_cli_ctx);
483
484                 /* restore old ctx */
485                 req->rq_cli_ctx = oldctx;
486                 return rc;
487         }
488
489         newctx = req->rq_cli_ctx;
490         LASSERT(newctx);
491
492         if (unlikely(newctx == oldctx &&
493                      test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
494                 /*
495                  * still get the old dead ctx, usually means system too busy
496                  */
497                 CDEBUG(D_SEC,
498                        "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
499                        newctx, newctx->cc_flags);
500
501                 set_current_state(TASK_INTERRUPTIBLE);
502                 schedule_timeout(HZ);
503         } else {
504                 /*
505                  * it's possible newctx == oldctx if we're switching
506                  * subflavor with the same sec.
507                  */
508                 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
509                 if (rc) {
510                         /* restore old ctx */
511                         sptlrpc_req_put_ctx(req, 0);
512                         req->rq_cli_ctx = oldctx;
513                         return rc;
514                 }
515
516                 LASSERT(req->rq_cli_ctx == newctx);
517         }
518
519         sptlrpc_cli_ctx_put(oldctx, 1);
520         return 0;
521 }
522
523 static
524 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
525 {
526         if (cli_ctx_is_refreshed(ctx))
527                 return 1;
528         return 0;
529 }
530
531 static
532 int ctx_refresh_timeout(void *data)
533 {
534         struct ptlrpc_request *req = data;
535         int rc;
536
537         /* conn_cnt is needed in expire_one_request */
538         lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
539
540         rc = ptlrpc_expire_one_request(req, 1);
541         /* if we started recovery, we should mark this ctx dead; otherwise
542          * in case of lgssd died nobody would retire this ctx, following
543          * connecting will still find the same ctx thus cause deadlock.
544          * there's an assumption that expire time of the request should be
545          * later than the context refresh expire time.
546          */
547         if (rc == 0)
548                 req->rq_cli_ctx->cc_ops->force_die(req->rq_cli_ctx, 0);
549         return rc;
550 }
551
552 static
553 void ctx_refresh_interrupt(void *data)
554 {
555         struct ptlrpc_request *req = data;
556
557         spin_lock(&req->rq_lock);
558         req->rq_intr = 1;
559         spin_unlock(&req->rq_lock);
560 }
561
562 static
563 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
564 {
565         spin_lock(&ctx->cc_lock);
566         if (!list_empty(&req->rq_ctx_chain))
567                 list_del_init(&req->rq_ctx_chain);
568         spin_unlock(&ctx->cc_lock);
569 }
570
571 /**
572  * To refresh the context of \req, if it's not up-to-date.
573  * \param timeout
574  * - < 0: don't wait
575  * - = 0: wait until success or fatal error occur
576  * - > 0: timeout value (in seconds)
577  *
578  * The status of the context could be subject to be changed by other threads
579  * at any time. We allow this race, but once we return with 0, the caller will
580  * suppose it's uptodated and keep using it until the owning rpc is done.
581  *
582  * \retval 0 only if the context is uptodated.
583  * \retval -ev error number.
584  */
585 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
586 {
587         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
588         struct ptlrpc_sec *sec;
589         struct l_wait_info lwi;
590         int rc;
591
592         LASSERT(ctx);
593
594         if (req->rq_ctx_init || req->rq_ctx_fini)
595                 return 0;
596
597         /*
598          * during the process a request's context might change type even
599          * (e.g. from gss ctx to null ctx), so each loop we need to re-check
600          * everything
601          */
602 again:
603         rc = import_sec_validate_get(req->rq_import, &sec);
604         if (rc)
605                 return rc;
606
607         if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
608                 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
609                        req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
610                 req_off_ctx_list(req, ctx);
611                 sptlrpc_req_replace_dead_ctx(req);
612                 ctx = req->rq_cli_ctx;
613         }
614         sptlrpc_sec_put(sec);
615
616         if (cli_ctx_is_eternal(ctx))
617                 return 0;
618
619         if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
620                 LASSERT(ctx->cc_ops->refresh);
621                 ctx->cc_ops->refresh(ctx);
622         }
623         LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
624
625         LASSERT(ctx->cc_ops->validate);
626         if (ctx->cc_ops->validate(ctx) == 0) {
627                 req_off_ctx_list(req, ctx);
628                 return 0;
629         }
630
631         if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
632                 spin_lock(&req->rq_lock);
633                 req->rq_err = 1;
634                 spin_unlock(&req->rq_lock);
635                 req_off_ctx_list(req, ctx);
636                 return -EPERM;
637         }
638
639         /*
640          * There's a subtle issue for resending RPCs, suppose following
641          * situation:
642          *  1. the request was sent to server.
643          *  2. recovery was kicked start, after finished the request was
644          *     marked as resent.
645          *  3. resend the request.
646          *  4. old reply from server received, we accept and verify the reply.
647          *     this has to be success, otherwise the error will be aware
648          *     by application.
649          *  5. new reply from server received, dropped by LNet.
650          *
651          * Note the xid of old & new request is the same. We can't simply
652          * change xid for the resent request because the server replies on
653          * it for reply reconstruction.
654          *
655          * Commonly the original context should be uptodate because we
656          * have a expiry nice time; server will keep its context because
657          * we at least hold a ref of old context which prevent context
658          * destroying RPC being sent. So server still can accept the request
659          * and finish the RPC. But if that's not the case:
660          *  1. If server side context has been trimmed, a NO_CONTEXT will
661          *     be returned, gss_cli_ctx_verify/unseal will switch to new
662          *     context by force.
663          *  2. Current context never be refreshed, then we are fine: we
664          *     never really send request with old context before.
665          */
666         if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
667             unlikely(req->rq_reqmsg) &&
668             lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
669                 req_off_ctx_list(req, ctx);
670                 return 0;
671         }
672
673         if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
674                 req_off_ctx_list(req, ctx);
675                 /*
676                  * don't switch ctx if import was deactivated
677                  */
678                 if (req->rq_import->imp_deactive) {
679                         spin_lock(&req->rq_lock);
680                         req->rq_err = 1;
681                         spin_unlock(&req->rq_lock);
682                         return -EINTR;
683                 }
684
685                 rc = sptlrpc_req_replace_dead_ctx(req);
686                 if (rc) {
687                         LASSERT(ctx == req->rq_cli_ctx);
688                         CERROR("req %p: failed to replace dead ctx %p: %d\n",
689                                req, ctx, rc);
690                         spin_lock(&req->rq_lock);
691                         req->rq_err = 1;
692                         spin_unlock(&req->rq_lock);
693                         return rc;
694                 }
695
696                 ctx = req->rq_cli_ctx;
697                 goto again;
698         }
699
700         /*
701          * Now we're sure this context is during upcall, add myself into
702          * waiting list
703          */
704         spin_lock(&ctx->cc_lock);
705         if (list_empty(&req->rq_ctx_chain))
706                 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
707         spin_unlock(&ctx->cc_lock);
708
709         if (timeout < 0)
710                 return -EWOULDBLOCK;
711
712         /* Clear any flags that may be present from previous sends */
713         LASSERT(req->rq_receiving_reply == 0);
714         spin_lock(&req->rq_lock);
715         req->rq_err = 0;
716         req->rq_timedout = 0;
717         req->rq_resend = 0;
718         req->rq_restart = 0;
719         spin_unlock(&req->rq_lock);
720
721         lwi = LWI_TIMEOUT_INTR(timeout * HZ, ctx_refresh_timeout,
722                                ctx_refresh_interrupt, req);
723         rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
724
725         /*
726          * following cases could lead us here:
727          * - successfully refreshed;
728          * - interrupted;
729          * - timedout, and we don't want recover from the failure;
730          * - timedout, and waked up upon recovery finished;
731          * - someone else mark this ctx dead by force;
732          * - someone invalidate the req and call ptlrpc_client_wake_req(),
733          *   e.g. ptlrpc_abort_inflight();
734          */
735         if (!cli_ctx_is_refreshed(ctx)) {
736                 /* timed out or interrupted */
737                 req_off_ctx_list(req, ctx);
738
739                 LASSERT(rc != 0);
740                 return rc;
741         }
742
743         goto again;
744 }
745
746 /**
747  * Initialize flavor settings for \a req, according to \a opcode.
748  *
749  * \note this could be called in two situations:
750  * - new request from ptlrpc_pre_req(), with proper @opcode
751  * - old request which changed ctx in the middle, with @opcode == 0
752  */
753 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
754 {
755         struct ptlrpc_sec *sec;
756
757         LASSERT(req->rq_import);
758         LASSERT(req->rq_cli_ctx);
759         LASSERT(req->rq_cli_ctx->cc_sec);
760         LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
761
762         /* special security flags according to opcode */
763         switch (opcode) {
764         case OST_READ:
765         case MDS_READPAGE:
766         case MGS_CONFIG_READ:
767         case OBD_IDX_READ:
768                 req->rq_bulk_read = 1;
769                 break;
770         case OST_WRITE:
771         case MDS_WRITEPAGE:
772                 req->rq_bulk_write = 1;
773                 break;
774         case SEC_CTX_INIT:
775                 req->rq_ctx_init = 1;
776                 break;
777         case SEC_CTX_FINI:
778                 req->rq_ctx_fini = 1;
779                 break;
780         case 0:
781                 /* init/fini rpc won't be resend, so can't be here */
782                 LASSERT(req->rq_ctx_init == 0);
783                 LASSERT(req->rq_ctx_fini == 0);
784
785                 /* cleanup flags, which should be recalculated */
786                 req->rq_pack_udesc = 0;
787                 req->rq_pack_bulk = 0;
788                 break;
789         }
790
791         sec = req->rq_cli_ctx->cc_sec;
792
793         spin_lock(&sec->ps_lock);
794         req->rq_flvr = sec->ps_flvr;
795         spin_unlock(&sec->ps_lock);
796
797         /* force SVC_NULL for context initiation rpc, SVC_INTG for context
798          * destruction rpc
799          */
800         if (unlikely(req->rq_ctx_init))
801                 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
802         else if (unlikely(req->rq_ctx_fini))
803                 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
804
805         /* user descriptor flag, null security can't do it anyway */
806         if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
807             (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
808                 req->rq_pack_udesc = 1;
809
810         /* bulk security flag */
811         if ((req->rq_bulk_read || req->rq_bulk_write) &&
812             sptlrpc_flavor_has_bulk(&req->rq_flvr))
813                 req->rq_pack_bulk = 1;
814 }
815
816 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
817 {
818         if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
819                 return;
820
821         LASSERT(req->rq_clrbuf);
822         if (req->rq_pool || !req->rq_reqbuf)
823                 return;
824
825         kfree(req->rq_reqbuf);
826         req->rq_reqbuf = NULL;
827         req->rq_reqbuf_len = 0;
828 }
829
830 /**
831  * Given an import \a imp, check whether current user has a valid context
832  * or not. We may create a new context and try to refresh it, and try
833  * repeatedly try in case of non-fatal errors. Return 0 means success.
834  */
835 int sptlrpc_import_check_ctx(struct obd_import *imp)
836 {
837         struct ptlrpc_sec *sec;
838         struct ptlrpc_cli_ctx *ctx;
839         struct ptlrpc_request *req = NULL;
840         int rc;
841
842         might_sleep();
843
844         sec = sptlrpc_import_sec_ref(imp);
845         ctx = get_my_ctx(sec);
846         sptlrpc_sec_put(sec);
847
848         if (!ctx)
849                 return -ENOMEM;
850
851         if (cli_ctx_is_eternal(ctx) ||
852             ctx->cc_ops->validate(ctx) == 0) {
853                 sptlrpc_cli_ctx_put(ctx, 1);
854                 return 0;
855         }
856
857         if (cli_ctx_is_error(ctx)) {
858                 sptlrpc_cli_ctx_put(ctx, 1);
859                 return -EACCES;
860         }
861
862         req = ptlrpc_request_cache_alloc(GFP_NOFS);
863         if (!req)
864                 return -ENOMEM;
865
866         ptlrpc_cli_req_init(req);
867         atomic_set(&req->rq_refcount, 10000);
868
869         req->rq_import = imp;
870         req->rq_flvr = sec->ps_flvr;
871         req->rq_cli_ctx = ctx;
872
873         rc = sptlrpc_req_refresh_ctx(req, 0);
874         LASSERT(list_empty(&req->rq_ctx_chain));
875         sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
876         ptlrpc_request_cache_free(req);
877
878         return rc;
879 }
880
881 /**
882  * Used by ptlrpc client, to perform the pre-defined security transformation
883  * upon the request message of \a req. After this function called,
884  * req->rq_reqmsg is still accessible as clear text.
885  */
886 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
887 {
888         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
889         int rc = 0;
890
891         LASSERT(ctx);
892         LASSERT(ctx->cc_sec);
893         LASSERT(req->rq_reqbuf || req->rq_clrbuf);
894
895         /* we wrap bulk request here because now we can be sure
896          * the context is uptodate.
897          */
898         if (req->rq_bulk) {
899                 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
900                 if (rc)
901                         return rc;
902         }
903
904         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
905         case SPTLRPC_SVC_NULL:
906         case SPTLRPC_SVC_AUTH:
907         case SPTLRPC_SVC_INTG:
908                 LASSERT(ctx->cc_ops->sign);
909                 rc = ctx->cc_ops->sign(ctx, req);
910                 break;
911         case SPTLRPC_SVC_PRIV:
912                 LASSERT(ctx->cc_ops->seal);
913                 rc = ctx->cc_ops->seal(ctx, req);
914                 break;
915         default:
916                 LBUG();
917         }
918
919         if (rc == 0) {
920                 LASSERT(req->rq_reqdata_len);
921                 LASSERT(req->rq_reqdata_len % 8 == 0);
922                 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
923         }
924
925         return rc;
926 }
927
928 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
929 {
930         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
931         int rc;
932
933         LASSERT(ctx);
934         LASSERT(ctx->cc_sec);
935         LASSERT(req->rq_repbuf);
936         LASSERT(req->rq_repdata);
937         LASSERT(!req->rq_repmsg);
938
939         req->rq_rep_swab_mask = 0;
940
941         rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
942         switch (rc) {
943         case 1:
944                 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
945         case 0:
946                 break;
947         default:
948                 CERROR("failed unpack reply: x%llu\n", req->rq_xid);
949                 return -EPROTO;
950         }
951
952         if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
953                 CERROR("replied data length %d too small\n",
954                        req->rq_repdata_len);
955                 return -EPROTO;
956         }
957
958         if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
959             SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
960                 CERROR("reply policy %u doesn't match request policy %u\n",
961                        SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
962                        SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
963                 return -EPROTO;
964         }
965
966         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
967         case SPTLRPC_SVC_NULL:
968         case SPTLRPC_SVC_AUTH:
969         case SPTLRPC_SVC_INTG:
970                 LASSERT(ctx->cc_ops->verify);
971                 rc = ctx->cc_ops->verify(ctx, req);
972                 break;
973         case SPTLRPC_SVC_PRIV:
974                 LASSERT(ctx->cc_ops->unseal);
975                 rc = ctx->cc_ops->unseal(ctx, req);
976                 break;
977         default:
978                 LBUG();
979         }
980         LASSERT(rc || req->rq_repmsg || req->rq_resend);
981
982         if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
983             !req->rq_ctx_init)
984                 req->rq_rep_swab_mask = 0;
985         return rc;
986 }
987
988 /**
989  * Used by ptlrpc client, to perform security transformation upon the reply
990  * message of \a req. After return successfully, req->rq_repmsg points to
991  * the reply message in clear text.
992  *
993  * \pre the reply buffer should have been un-posted from LNet, so nothing is
994  * going to change.
995  */
996 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
997 {
998         LASSERT(req->rq_repbuf);
999         LASSERT(!req->rq_repdata);
1000         LASSERT(!req->rq_repmsg);
1001         LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1002
1003         if (req->rq_reply_off == 0 &&
1004             (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1005                 CERROR("real reply with offset 0\n");
1006                 return -EPROTO;
1007         }
1008
1009         if (req->rq_reply_off % 8 != 0) {
1010                 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1011                 return -EPROTO;
1012         }
1013
1014         req->rq_repdata = (struct lustre_msg *)
1015                                 (req->rq_repbuf + req->rq_reply_off);
1016         req->rq_repdata_len = req->rq_nob_received;
1017
1018         return do_cli_unwrap_reply(req);
1019 }
1020
1021 /**
1022  * Used by ptlrpc client, to perform security transformation upon the early
1023  * reply message of \a req. We expect the rq_reply_off is 0, and
1024  * rq_nob_received is the early reply size.
1025  *
1026  * Because the receive buffer might be still posted, the reply data might be
1027  * changed at any time, no matter we're holding rq_lock or not. For this reason
1028  * we allocate a separate ptlrpc_request and reply buffer for early reply
1029  * processing.
1030  *
1031  * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1032  * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1033  * \a *req_ret to release it.
1034  * \retval -ev error number, and \a req_ret will not be set.
1035  */
1036 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1037                                    struct ptlrpc_request **req_ret)
1038 {
1039         struct ptlrpc_request *early_req;
1040         char *early_buf;
1041         int early_bufsz, early_size;
1042         int rc;
1043
1044         early_req = ptlrpc_request_cache_alloc(GFP_NOFS);
1045         if (!early_req)
1046                 return -ENOMEM;
1047
1048         ptlrpc_cli_req_init(early_req);
1049
1050         early_size = req->rq_nob_received;
1051         early_bufsz = size_roundup_power2(early_size);
1052         early_buf = libcfs_kvzalloc(early_bufsz, GFP_NOFS);
1053         if (!early_buf) {
1054                 rc = -ENOMEM;
1055                 goto err_req;
1056         }
1057
1058         /* sanity checkings and copy data out, do it inside spinlock */
1059         spin_lock(&req->rq_lock);
1060
1061         if (req->rq_replied) {
1062                 spin_unlock(&req->rq_lock);
1063                 rc = -EALREADY;
1064                 goto err_buf;
1065         }
1066
1067         LASSERT(req->rq_repbuf);
1068         LASSERT(!req->rq_repdata);
1069         LASSERT(!req->rq_repmsg);
1070
1071         if (req->rq_reply_off != 0) {
1072                 CERROR("early reply with offset %u\n", req->rq_reply_off);
1073                 spin_unlock(&req->rq_lock);
1074                 rc = -EPROTO;
1075                 goto err_buf;
1076         }
1077
1078         if (req->rq_nob_received != early_size) {
1079                 /* even another early arrived the size should be the same */
1080                 CERROR("data size has changed from %u to %u\n",
1081                        early_size, req->rq_nob_received);
1082                 spin_unlock(&req->rq_lock);
1083                 rc = -EINVAL;
1084                 goto err_buf;
1085         }
1086
1087         if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1088                 CERROR("early reply length %d too small\n",
1089                        req->rq_nob_received);
1090                 spin_unlock(&req->rq_lock);
1091                 rc = -EALREADY;
1092                 goto err_buf;
1093         }
1094
1095         memcpy(early_buf, req->rq_repbuf, early_size);
1096         spin_unlock(&req->rq_lock);
1097
1098         early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1099         early_req->rq_flvr = req->rq_flvr;
1100         early_req->rq_repbuf = early_buf;
1101         early_req->rq_repbuf_len = early_bufsz;
1102         early_req->rq_repdata = (struct lustre_msg *)early_buf;
1103         early_req->rq_repdata_len = early_size;
1104         early_req->rq_early = 1;
1105         early_req->rq_reqmsg = req->rq_reqmsg;
1106
1107         rc = do_cli_unwrap_reply(early_req);
1108         if (rc) {
1109                 DEBUG_REQ(D_ADAPTTO, early_req,
1110                           "error %d unwrap early reply", rc);
1111                 goto err_ctx;
1112         }
1113
1114         LASSERT(early_req->rq_repmsg);
1115         *req_ret = early_req;
1116         return 0;
1117
1118 err_ctx:
1119         sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1120 err_buf:
1121         kvfree(early_buf);
1122 err_req:
1123         ptlrpc_request_cache_free(early_req);
1124         return rc;
1125 }
1126
1127 /**
1128  * Used by ptlrpc client, to release a processed early reply \a early_req.
1129  *
1130  * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1131  */
1132 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1133 {
1134         LASSERT(early_req->rq_repbuf);
1135         LASSERT(early_req->rq_repdata);
1136         LASSERT(early_req->rq_repmsg);
1137
1138         sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1139         kvfree(early_req->rq_repbuf);
1140         ptlrpc_request_cache_free(early_req);
1141 }
1142
1143 /**************************************************
1144  * sec ID                                        *
1145  **************************************************/
1146
1147 /*
1148  * "fixed" sec (e.g. null) use sec_id < 0
1149  */
1150 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1151
1152 int sptlrpc_get_next_secid(void)
1153 {
1154         return atomic_inc_return(&sptlrpc_sec_id);
1155 }
1156 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1157
1158 /**************************************************
1159  * client side high-level security APIs    *
1160  **************************************************/
1161
1162 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1163                                    int grace, int force)
1164 {
1165         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1166
1167         LASSERT(policy->sp_cops);
1168         LASSERT(policy->sp_cops->flush_ctx_cache);
1169
1170         return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1171 }
1172
1173 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1174 {
1175         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1176
1177         LASSERT_ATOMIC_ZERO(&sec->ps_refcount);
1178         LASSERT_ATOMIC_ZERO(&sec->ps_nctx);
1179         LASSERT(policy->sp_cops->destroy_sec);
1180
1181         CDEBUG(D_SEC, "%s@%p: being destroyed\n", sec->ps_policy->sp_name, sec);
1182
1183         policy->sp_cops->destroy_sec(sec);
1184         sptlrpc_policy_put(policy);
1185 }
1186
1187 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1188 {
1189         LASSERT_ATOMIC_POS(&sec->ps_refcount);
1190
1191         if (sec->ps_policy->sp_cops->kill_sec) {
1192                 sec->ps_policy->sp_cops->kill_sec(sec);
1193
1194                 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1195         }
1196 }
1197
1198 static struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1199 {
1200         if (sec)
1201                 atomic_inc(&sec->ps_refcount);
1202
1203         return sec;
1204 }
1205
1206 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1207 {
1208         if (sec) {
1209                 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1210
1211                 if (atomic_dec_and_test(&sec->ps_refcount)) {
1212                         sptlrpc_gc_del_sec(sec);
1213                         sec_cop_destroy_sec(sec);
1214                 }
1215         }
1216 }
1217 EXPORT_SYMBOL(sptlrpc_sec_put);
1218
1219 /*
1220  * policy module is responsible for taking reference of import
1221  */
1222 static
1223 struct ptlrpc_sec *sptlrpc_sec_create(struct obd_import *imp,
1224                                       struct ptlrpc_svc_ctx *svc_ctx,
1225                                       struct sptlrpc_flavor *sf,
1226                                       enum lustre_sec_part sp)
1227 {
1228         struct ptlrpc_sec_policy *policy;
1229         struct ptlrpc_sec *sec;
1230         char str[32];
1231
1232         if (svc_ctx) {
1233                 LASSERT(imp->imp_dlm_fake == 1);
1234
1235                 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1236                        imp->imp_obd->obd_type->typ_name,
1237                        imp->imp_obd->obd_name,
1238                        sptlrpc_flavor2name(sf, str, sizeof(str)));
1239
1240                 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1241                 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1242         } else {
1243                 LASSERT(imp->imp_dlm_fake == 0);
1244
1245                 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1246                        imp->imp_obd->obd_type->typ_name,
1247                        imp->imp_obd->obd_name,
1248                        sptlrpc_flavor2name(sf, str, sizeof(str)));
1249
1250                 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1251                 if (!policy) {
1252                         CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1253                         return NULL;
1254                 }
1255         }
1256
1257         sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1258         if (sec) {
1259                 atomic_inc(&sec->ps_refcount);
1260
1261                 sec->ps_part = sp;
1262
1263                 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1264                         sptlrpc_gc_add_sec(sec);
1265         } else {
1266                 sptlrpc_policy_put(policy);
1267         }
1268
1269         return sec;
1270 }
1271
1272 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1273 {
1274         struct ptlrpc_sec *sec;
1275
1276         spin_lock(&imp->imp_lock);
1277         sec = sptlrpc_sec_get(imp->imp_sec);
1278         spin_unlock(&imp->imp_lock);
1279
1280         return sec;
1281 }
1282 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1283
1284 static void sptlrpc_import_sec_install(struct obd_import *imp,
1285                                        struct ptlrpc_sec *sec)
1286 {
1287         struct ptlrpc_sec *old_sec;
1288
1289         LASSERT_ATOMIC_POS(&sec->ps_refcount);
1290
1291         spin_lock(&imp->imp_lock);
1292         old_sec = imp->imp_sec;
1293         imp->imp_sec = sec;
1294         spin_unlock(&imp->imp_lock);
1295
1296         if (old_sec) {
1297                 sptlrpc_sec_kill(old_sec);
1298
1299                 /* balance the ref taken by this import */
1300                 sptlrpc_sec_put(old_sec);
1301         }
1302 }
1303
1304 static inline
1305 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1306 {
1307         return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1308 }
1309
1310 static inline
1311 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1312 {
1313         *dst = *src;
1314 }
1315
1316 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1317                                              struct ptlrpc_sec *sec,
1318                                              struct sptlrpc_flavor *sf)
1319 {
1320         char str1[32], str2[32];
1321
1322         if (sec->ps_flvr.sf_flags != sf->sf_flags)
1323                 CDEBUG(D_SEC, "changing sec flags: %s -> %s\n",
1324                        sptlrpc_secflags2str(sec->ps_flvr.sf_flags,
1325                                             str1, sizeof(str1)),
1326                        sptlrpc_secflags2str(sf->sf_flags,
1327                                             str2, sizeof(str2)));
1328
1329         spin_lock(&sec->ps_lock);
1330         flavor_copy(&sec->ps_flvr, sf);
1331         spin_unlock(&sec->ps_lock);
1332 }
1333
1334 /**
1335  * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1336  * configuration. Upon called, imp->imp_sec may or may not be NULL.
1337  *
1338  *  - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1339  *  - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1340  */
1341 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1342                              struct ptlrpc_svc_ctx *svc_ctx,
1343                              struct sptlrpc_flavor *flvr)
1344 {
1345         struct ptlrpc_connection *conn;
1346         struct sptlrpc_flavor sf;
1347         struct ptlrpc_sec *sec, *newsec;
1348         enum lustre_sec_part sp;
1349         char str[24];
1350         int rc = 0;
1351
1352         might_sleep();
1353
1354         if (!imp)
1355                 return 0;
1356
1357         conn = imp->imp_connection;
1358
1359         if (!svc_ctx) {
1360                 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1361                 /*
1362                  * normal import, determine flavor from rule set, except
1363                  * for mgc the flavor is predetermined.
1364                  */
1365                 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1366                         sf = cliobd->cl_flvr_mgc;
1367                 else
1368                         sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1369                                                    cliobd->cl_sp_to,
1370                                                    &cliobd->cl_target_uuid,
1371                                                    conn->c_self, &sf);
1372
1373                 sp = imp->imp_obd->u.cli.cl_sp_me;
1374         } else {
1375                 /* reverse import, determine flavor from incoming request */
1376                 sf = *flvr;
1377
1378                 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1379                         sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1380                                       PTLRPC_SEC_FL_ROOTONLY;
1381
1382                 sp = sptlrpc_target_sec_part(imp->imp_obd);
1383         }
1384
1385         sec = sptlrpc_import_sec_ref(imp);
1386         if (sec) {
1387                 char str2[24];
1388
1389                 if (flavor_equal(&sf, &sec->ps_flvr))
1390                         goto out;
1391
1392                 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1393                        imp->imp_obd->obd_name,
1394                        obd_uuid2str(&conn->c_remote_uuid),
1395                        sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1396                        sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1397
1398                 if (SPTLRPC_FLVR_POLICY(sf.sf_rpc) ==
1399                     SPTLRPC_FLVR_POLICY(sec->ps_flvr.sf_rpc) &&
1400                     SPTLRPC_FLVR_MECH(sf.sf_rpc) ==
1401                     SPTLRPC_FLVR_MECH(sec->ps_flvr.sf_rpc)) {
1402                         sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1403                         goto out;
1404                 }
1405         } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1406                    SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1407                 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1408                        imp->imp_obd->obd_name,
1409                        obd_uuid2str(&conn->c_remote_uuid),
1410                        LNET_NIDNET(conn->c_self),
1411                        sptlrpc_flavor2name(&sf, str, sizeof(str)));
1412         }
1413
1414         mutex_lock(&imp->imp_sec_mutex);
1415
1416         newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1417         if (newsec) {
1418                 sptlrpc_import_sec_install(imp, newsec);
1419         } else {
1420                 CERROR("import %s->%s: failed to create new sec\n",
1421                        imp->imp_obd->obd_name,
1422                        obd_uuid2str(&conn->c_remote_uuid));
1423                 rc = -EPERM;
1424         }
1425
1426         mutex_unlock(&imp->imp_sec_mutex);
1427 out:
1428         sptlrpc_sec_put(sec);
1429         return rc;
1430 }
1431
1432 void sptlrpc_import_sec_put(struct obd_import *imp)
1433 {
1434         if (imp->imp_sec) {
1435                 sptlrpc_sec_kill(imp->imp_sec);
1436
1437                 sptlrpc_sec_put(imp->imp_sec);
1438                 imp->imp_sec = NULL;
1439         }
1440 }
1441
1442 static void import_flush_ctx_common(struct obd_import *imp,
1443                                     uid_t uid, int grace, int force)
1444 {
1445         struct ptlrpc_sec *sec;
1446
1447         if (!imp)
1448                 return;
1449
1450         sec = sptlrpc_import_sec_ref(imp);
1451         if (!sec)
1452                 return;
1453
1454         sec_cop_flush_ctx_cache(sec, uid, grace, force);
1455         sptlrpc_sec_put(sec);
1456 }
1457
1458 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1459 {
1460         import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1461                                 1, 1);
1462 }
1463 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1464
1465 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1466 {
1467         import_flush_ctx_common(imp, -1, 1, 1);
1468 }
1469 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1470
1471 /**
1472  * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1473  * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1474  */
1475 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1476 {
1477         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1478         struct ptlrpc_sec_policy *policy;
1479         int rc;
1480
1481         LASSERT(ctx);
1482         LASSERT(ctx->cc_sec);
1483         LASSERT(ctx->cc_sec->ps_policy);
1484         LASSERT(!req->rq_reqmsg);
1485         LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1486
1487         policy = ctx->cc_sec->ps_policy;
1488         rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1489         if (!rc) {
1490                 LASSERT(req->rq_reqmsg);
1491                 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1492
1493                 /* zeroing preallocated buffer */
1494                 if (req->rq_pool)
1495                         memset(req->rq_reqmsg, 0, msgsize);
1496         }
1497
1498         return rc;
1499 }
1500
1501 /**
1502  * Used by ptlrpc client to free request buffer of \a req. After this
1503  * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1504  */
1505 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1506 {
1507         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1508         struct ptlrpc_sec_policy *policy;
1509
1510         LASSERT(ctx);
1511         LASSERT(ctx->cc_sec);
1512         LASSERT(ctx->cc_sec->ps_policy);
1513         LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1514
1515         if (!req->rq_reqbuf && !req->rq_clrbuf)
1516                 return;
1517
1518         policy = ctx->cc_sec->ps_policy;
1519         policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1520         req->rq_reqmsg = NULL;
1521 }
1522
1523 /*
1524  * NOTE caller must guarantee the buffer size is enough for the enlargement
1525  */
1526 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1527                                   int segment, int newsize)
1528 {
1529         void *src, *dst;
1530         int oldsize, oldmsg_size, movesize;
1531
1532         LASSERT(segment < msg->lm_bufcount);
1533         LASSERT(msg->lm_buflens[segment] <= newsize);
1534
1535         if (msg->lm_buflens[segment] == newsize)
1536                 return;
1537
1538         /* nothing to do if we are enlarging the last segment */
1539         if (segment == msg->lm_bufcount - 1) {
1540                 msg->lm_buflens[segment] = newsize;
1541                 return;
1542         }
1543
1544         oldsize = msg->lm_buflens[segment];
1545
1546         src = lustre_msg_buf(msg, segment + 1, 0);
1547         msg->lm_buflens[segment] = newsize;
1548         dst = lustre_msg_buf(msg, segment + 1, 0);
1549         msg->lm_buflens[segment] = oldsize;
1550
1551         /* move from segment + 1 to end segment */
1552         LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1553         oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1554         movesize = oldmsg_size - ((unsigned long)src - (unsigned long)msg);
1555         LASSERT(movesize >= 0);
1556
1557         if (movesize)
1558                 memmove(dst, src, movesize);
1559
1560         /* note we don't clear the ares where old data live, not secret */
1561
1562         /* finally set new segment size */
1563         msg->lm_buflens[segment] = newsize;
1564 }
1565 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1566
1567 /**
1568  * Used by ptlrpc client to enlarge the \a segment of request message pointed
1569  * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1570  * preserved after the enlargement. this must be called after original request
1571  * buffer being allocated.
1572  *
1573  * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1574  * so caller should refresh its local pointers if needed.
1575  */
1576 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1577                                int segment, int newsize)
1578 {
1579         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1580         struct ptlrpc_sec_cops *cops;
1581         struct lustre_msg *msg = req->rq_reqmsg;
1582
1583         LASSERT(ctx);
1584         LASSERT(msg);
1585         LASSERT(msg->lm_bufcount > segment);
1586         LASSERT(msg->lm_buflens[segment] <= newsize);
1587
1588         if (msg->lm_buflens[segment] == newsize)
1589                 return 0;
1590
1591         cops = ctx->cc_sec->ps_policy->sp_cops;
1592         LASSERT(cops->enlarge_reqbuf);
1593         return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1594 }
1595 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1596
1597 /**
1598  * Used by ptlrpc client to allocate reply buffer of \a req.
1599  *
1600  * \note After this, req->rq_repmsg is still not accessible.
1601  */
1602 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1603 {
1604         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1605         struct ptlrpc_sec_policy *policy;
1606
1607         LASSERT(ctx);
1608         LASSERT(ctx->cc_sec);
1609         LASSERT(ctx->cc_sec->ps_policy);
1610
1611         if (req->rq_repbuf)
1612                 return 0;
1613
1614         policy = ctx->cc_sec->ps_policy;
1615         return policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize);
1616 }
1617
1618 /**
1619  * Used by ptlrpc client to free reply buffer of \a req. After this
1620  * req->rq_repmsg is set to NULL and should not be accessed anymore.
1621  */
1622 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1623 {
1624         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1625         struct ptlrpc_sec_policy *policy;
1626
1627         LASSERT(ctx);
1628         LASSERT(ctx->cc_sec);
1629         LASSERT(ctx->cc_sec->ps_policy);
1630         LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1631
1632         if (!req->rq_repbuf)
1633                 return;
1634         LASSERT(req->rq_repbuf_len);
1635
1636         policy = ctx->cc_sec->ps_policy;
1637         policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1638         req->rq_repmsg = NULL;
1639 }
1640
1641 static int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1642                                        struct ptlrpc_svc_ctx *ctx)
1643 {
1644         struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1645
1646         if (!policy->sp_sops->install_rctx)
1647                 return 0;
1648         return policy->sp_sops->install_rctx(imp, ctx);
1649 }
1650
1651 /****************************************
1652  * server side security          *
1653  ****************************************/
1654
1655 static int flavor_allowed(struct sptlrpc_flavor *exp,
1656                           struct ptlrpc_request *req)
1657 {
1658         struct sptlrpc_flavor *flvr = &req->rq_flvr;
1659
1660         if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1661                 return 1;
1662
1663         if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1664             SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1665             SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1666             SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1667                 return 1;
1668
1669         return 0;
1670 }
1671
1672 #define EXP_FLVR_UPDATE_EXPIRE      (OBD_TIMEOUT_DEFAULT + 10)
1673
1674 /**
1675  * Given an export \a exp, check whether the flavor of incoming \a req
1676  * is allowed by the export \a exp. Main logic is about taking care of
1677  * changing configurations. Return 0 means success.
1678  */
1679 int sptlrpc_target_export_check(struct obd_export *exp,
1680                                 struct ptlrpc_request *req)
1681 {
1682         struct sptlrpc_flavor flavor;
1683
1684         if (!exp)
1685                 return 0;
1686
1687         /* client side export has no imp_reverse, skip
1688          * FIXME maybe we should check flavor this as well???
1689          */
1690         if (!exp->exp_imp_reverse)
1691                 return 0;
1692
1693         /* don't care about ctx fini rpc */
1694         if (req->rq_ctx_fini)
1695                 return 0;
1696
1697         spin_lock(&exp->exp_lock);
1698
1699         /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1700          * the first req with the new flavor, then treat it as current flavor,
1701          * adapt reverse sec according to it.
1702          * note the first rpc with new flavor might not be with root ctx, in
1703          * which case delay the sec_adapt by leaving exp_flvr_adapt == 1.
1704          */
1705         if (unlikely(exp->exp_flvr_changed) &&
1706             flavor_allowed(&exp->exp_flvr_old[1], req)) {
1707                 /* make the new flavor as "current", and old ones as
1708                  * about-to-expire
1709                  */
1710                 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1711                        exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1712                 flavor = exp->exp_flvr_old[1];
1713                 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1714                 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1715                 exp->exp_flvr_old[0] = exp->exp_flvr;
1716                 exp->exp_flvr_expire[0] = ktime_get_real_seconds() +
1717                                           EXP_FLVR_UPDATE_EXPIRE;
1718                 exp->exp_flvr = flavor;
1719
1720                 /* flavor change finished */
1721                 exp->exp_flvr_changed = 0;
1722                 LASSERT(exp->exp_flvr_adapt == 1);
1723
1724                 /* if it's gss, we only interested in root ctx init */
1725                 if (req->rq_auth_gss &&
1726                     !(req->rq_ctx_init &&
1727                       (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1728                        req->rq_auth_usr_ost))) {
1729                         spin_unlock(&exp->exp_lock);
1730                         CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1731                                req->rq_auth_gss, req->rq_ctx_init,
1732                                req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1733                                req->rq_auth_usr_ost);
1734                         return 0;
1735                 }
1736
1737                 exp->exp_flvr_adapt = 0;
1738                 spin_unlock(&exp->exp_lock);
1739
1740                 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1741                                                 req->rq_svc_ctx, &flavor);
1742         }
1743
1744         /* if it equals to the current flavor, we accept it, but need to
1745          * dealing with reverse sec/ctx
1746          */
1747         if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1748                 /* most cases should return here, we only interested in
1749                  * gss root ctx init
1750                  */
1751                 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1752                     (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1753                      !req->rq_auth_usr_ost)) {
1754                         spin_unlock(&exp->exp_lock);
1755                         return 0;
1756                 }
1757
1758                 /* if flavor just changed, we should not proceed, just leave
1759                  * it and current flavor will be discovered and replaced
1760                  * shortly, and let _this_ rpc pass through
1761                  */
1762                 if (exp->exp_flvr_changed) {
1763                         LASSERT(exp->exp_flvr_adapt);
1764                         spin_unlock(&exp->exp_lock);
1765                         return 0;
1766                 }
1767
1768                 if (exp->exp_flvr_adapt) {
1769                         exp->exp_flvr_adapt = 0;
1770                         CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1771                                exp, exp->exp_flvr.sf_rpc,
1772                                exp->exp_flvr_old[0].sf_rpc,
1773                                exp->exp_flvr_old[1].sf_rpc);
1774                         flavor = exp->exp_flvr;
1775                         spin_unlock(&exp->exp_lock);
1776
1777                         return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1778                                                         req->rq_svc_ctx,
1779                                                         &flavor);
1780                 } else {
1781                         CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, install rvs ctx\n",
1782                                exp, exp->exp_flvr.sf_rpc,
1783                                exp->exp_flvr_old[0].sf_rpc,
1784                                exp->exp_flvr_old[1].sf_rpc);
1785                         spin_unlock(&exp->exp_lock);
1786
1787                         return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1788                                                            req->rq_svc_ctx);
1789                 }
1790         }
1791
1792         if (exp->exp_flvr_expire[0]) {
1793                 if (exp->exp_flvr_expire[0] >= ktime_get_real_seconds()) {
1794                         if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1795                                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the middle one (%lld)\n", exp,
1796                                        exp->exp_flvr.sf_rpc,
1797                                        exp->exp_flvr_old[0].sf_rpc,
1798                                        exp->exp_flvr_old[1].sf_rpc,
1799                                        (s64)(exp->exp_flvr_expire[0] -
1800                                        ktime_get_real_seconds()));
1801                                 spin_unlock(&exp->exp_lock);
1802                                 return 0;
1803                         }
1804                 } else {
1805                         CDEBUG(D_SEC, "mark middle expired\n");
1806                         exp->exp_flvr_expire[0] = 0;
1807                 }
1808                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1809                        exp->exp_flvr.sf_rpc,
1810                        exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1811                        req->rq_flvr.sf_rpc);
1812         }
1813
1814         /* now it doesn't match the current flavor, the only chance we can
1815          * accept it is match the old flavors which is not expired.
1816          */
1817         if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1818                 if (exp->exp_flvr_expire[1] >= ktime_get_real_seconds()) {
1819                         if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1820                                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the oldest one (%lld)\n",
1821                                        exp,
1822                                        exp->exp_flvr.sf_rpc,
1823                                        exp->exp_flvr_old[0].sf_rpc,
1824                                        exp->exp_flvr_old[1].sf_rpc,
1825                                        (s64)(exp->exp_flvr_expire[1] -
1826                                        ktime_get_real_seconds()));
1827                                 spin_unlock(&exp->exp_lock);
1828                                 return 0;
1829                         }
1830                 } else {
1831                         CDEBUG(D_SEC, "mark oldest expired\n");
1832                         exp->exp_flvr_expire[1] = 0;
1833                 }
1834                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1835                        exp, exp->exp_flvr.sf_rpc,
1836                        exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1837                        req->rq_flvr.sf_rpc);
1838         } else {
1839                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1840                        exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1841                        exp->exp_flvr_old[1].sf_rpc);
1842         }
1843
1844         spin_unlock(&exp->exp_lock);
1845
1846         CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with unauthorized flavor %x, expect %x|%x(%+lld)|%x(%+lld)\n",
1847               exp, exp->exp_obd->obd_name,
1848               req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1849               req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
1850               req->rq_flvr.sf_rpc,
1851               exp->exp_flvr.sf_rpc,
1852               exp->exp_flvr_old[0].sf_rpc,
1853               exp->exp_flvr_expire[0] ?
1854               (s64)(exp->exp_flvr_expire[0] - ktime_get_real_seconds()) : 0,
1855               exp->exp_flvr_old[1].sf_rpc,
1856               exp->exp_flvr_expire[1] ?
1857               (s64)(exp->exp_flvr_expire[1] - ktime_get_real_seconds()) : 0);
1858         return -EACCES;
1859 }
1860 EXPORT_SYMBOL(sptlrpc_target_export_check);
1861
1862 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1863 {
1864         /* peer's claim is unreliable unless gss is being used */
1865         if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
1866                 return svc_rc;
1867
1868         switch (req->rq_sp_from) {
1869         case LUSTRE_SP_CLI:
1870                 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
1871                         DEBUG_REQ(D_ERROR, req, "faked source CLI");
1872                         svc_rc = SECSVC_DROP;
1873                 }
1874                 break;
1875         case LUSTRE_SP_MDT:
1876                 if (!req->rq_auth_usr_mdt) {
1877                         DEBUG_REQ(D_ERROR, req, "faked source MDT");
1878                         svc_rc = SECSVC_DROP;
1879                 }
1880                 break;
1881         case LUSTRE_SP_OST:
1882                 if (!req->rq_auth_usr_ost) {
1883                         DEBUG_REQ(D_ERROR, req, "faked source OST");
1884                         svc_rc = SECSVC_DROP;
1885                 }
1886                 break;
1887         case LUSTRE_SP_MGS:
1888         case LUSTRE_SP_MGC:
1889                 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1890                     !req->rq_auth_usr_ost) {
1891                         DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
1892                         svc_rc = SECSVC_DROP;
1893                 }
1894                 break;
1895         case LUSTRE_SP_ANY:
1896         default:
1897                 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
1898                 svc_rc = SECSVC_DROP;
1899         }
1900
1901         return svc_rc;
1902 }
1903
1904 /**
1905  * Used by ptlrpc server, to perform transformation upon request message of
1906  * incoming \a req. This must be the first thing to do with a incoming
1907  * request in ptlrpc layer.
1908  *
1909  * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
1910  * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
1911  * \retval SECSVC_COMPLETE success, the request has been fully processed, and
1912  * reply message has been prepared.
1913  * \retval SECSVC_DROP failed, this request should be dropped.
1914  */
1915 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1916 {
1917         struct ptlrpc_sec_policy *policy;
1918         struct lustre_msg *msg = req->rq_reqbuf;
1919         int rc;
1920
1921         LASSERT(msg);
1922         LASSERT(!req->rq_reqmsg);
1923         LASSERT(!req->rq_repmsg);
1924         LASSERT(!req->rq_svc_ctx);
1925
1926         req->rq_req_swab_mask = 0;
1927
1928         rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
1929         switch (rc) {
1930         case 1:
1931                 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1932         case 0:
1933                 break;
1934         default:
1935                 CERROR("error unpacking request from %s x%llu\n",
1936                        libcfs_id2str(req->rq_peer), req->rq_xid);
1937                 return SECSVC_DROP;
1938         }
1939
1940         req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
1941         req->rq_sp_from = LUSTRE_SP_ANY;
1942         req->rq_auth_uid = -1;
1943         req->rq_auth_mapped_uid = -1;
1944
1945         policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
1946         if (!policy) {
1947                 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
1948                 return SECSVC_DROP;
1949         }
1950
1951         LASSERT(policy->sp_sops->accept);
1952         rc = policy->sp_sops->accept(req);
1953         sptlrpc_policy_put(policy);
1954         LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1955         LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
1956
1957         /*
1958          * if it's not null flavor (which means embedded packing msg),
1959          * reset the swab mask for the coming inner msg unpacking.
1960          */
1961         if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
1962                 req->rq_req_swab_mask = 0;
1963
1964         /* sanity check for the request source */
1965         rc = sptlrpc_svc_check_from(req, rc);
1966         return rc;
1967 }
1968
1969 /**
1970  * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
1971  * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
1972  * a buffer of \a msglen size.
1973  */
1974 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
1975 {
1976         struct ptlrpc_sec_policy *policy;
1977         struct ptlrpc_reply_state *rs;
1978         int rc;
1979
1980         LASSERT(req->rq_svc_ctx);
1981         LASSERT(req->rq_svc_ctx->sc_policy);
1982
1983         policy = req->rq_svc_ctx->sc_policy;
1984         LASSERT(policy->sp_sops->alloc_rs);
1985
1986         rc = policy->sp_sops->alloc_rs(req, msglen);
1987         if (unlikely(rc == -ENOMEM)) {
1988                 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1989
1990                 if (svcpt->scp_service->srv_max_reply_size <
1991                    msglen + sizeof(struct ptlrpc_reply_state)) {
1992                         /* Just return failure if the size is too big */
1993                         CERROR("size of message is too big (%zd), %d allowed\n",
1994                                msglen + sizeof(struct ptlrpc_reply_state),
1995                                svcpt->scp_service->srv_max_reply_size);
1996                         return -ENOMEM;
1997                 }
1998
1999                 /* failed alloc, try emergency pool */
2000                 rs = lustre_get_emerg_rs(svcpt);
2001                 if (!rs)
2002                         return -ENOMEM;
2003
2004                 req->rq_reply_state = rs;
2005                 rc = policy->sp_sops->alloc_rs(req, msglen);
2006                 if (rc) {
2007                         lustre_put_emerg_rs(rs);
2008                         req->rq_reply_state = NULL;
2009                 }
2010         }
2011
2012         LASSERT(rc != 0 ||
2013                 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2014
2015         return rc;
2016 }
2017
2018 /**
2019  * Used by ptlrpc server, to perform transformation upon reply message.
2020  *
2021  * \post req->rq_reply_off is set to appropriate server-controlled reply offset.
2022  * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2023  */
2024 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2025 {
2026         struct ptlrpc_sec_policy *policy;
2027         int rc;
2028
2029         LASSERT(req->rq_svc_ctx);
2030         LASSERT(req->rq_svc_ctx->sc_policy);
2031
2032         policy = req->rq_svc_ctx->sc_policy;
2033         LASSERT(policy->sp_sops->authorize);
2034
2035         rc = policy->sp_sops->authorize(req);
2036         LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2037
2038         return rc;
2039 }
2040
2041 /**
2042  * Used by ptlrpc server, to free reply_state.
2043  */
2044 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2045 {
2046         struct ptlrpc_sec_policy *policy;
2047         unsigned int prealloc;
2048
2049         LASSERT(rs->rs_svc_ctx);
2050         LASSERT(rs->rs_svc_ctx->sc_policy);
2051
2052         policy = rs->rs_svc_ctx->sc_policy;
2053         LASSERT(policy->sp_sops->free_rs);
2054
2055         prealloc = rs->rs_prealloc;
2056         policy->sp_sops->free_rs(rs);
2057
2058         if (prealloc)
2059                 lustre_put_emerg_rs(rs);
2060 }
2061
2062 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2063 {
2064         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2065
2066         if (ctx)
2067                 atomic_inc(&ctx->sc_refcount);
2068 }
2069
2070 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2071 {
2072         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2073
2074         if (!ctx)
2075                 return;
2076
2077         LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2078         if (atomic_dec_and_test(&ctx->sc_refcount)) {
2079                 if (ctx->sc_policy->sp_sops->free_ctx)
2080                         ctx->sc_policy->sp_sops->free_ctx(ctx);
2081         }
2082         req->rq_svc_ctx = NULL;
2083 }
2084
2085 /****************************************
2086  * bulk security                        *
2087  ****************************************/
2088
2089 /**
2090  * Perform transformation upon bulk data pointed by \a desc. This is called
2091  * before transforming the request message.
2092  */
2093 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2094                           struct ptlrpc_bulk_desc *desc)
2095 {
2096         struct ptlrpc_cli_ctx *ctx;
2097
2098         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2099
2100         if (!req->rq_pack_bulk)
2101                 return 0;
2102
2103         ctx = req->rq_cli_ctx;
2104         if (ctx->cc_ops->wrap_bulk)
2105                 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2106         return 0;
2107 }
2108 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2109
2110 /**
2111  * This is called after unwrap the reply message.
2112  * return nob of actual plain text size received, or error code.
2113  */
2114 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2115                                  struct ptlrpc_bulk_desc *desc,
2116                                  int nob)
2117 {
2118         struct ptlrpc_cli_ctx *ctx;
2119         int rc;
2120
2121         LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2122
2123         if (!req->rq_pack_bulk)
2124                 return desc->bd_nob_transferred;
2125
2126         ctx = req->rq_cli_ctx;
2127         if (ctx->cc_ops->unwrap_bulk) {
2128                 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2129                 if (rc < 0)
2130                         return rc;
2131         }
2132         return desc->bd_nob_transferred;
2133 }
2134 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2135
2136 /**
2137  * This is called after unwrap the reply message.
2138  * return 0 for success or error code.
2139  */
2140 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2141                                   struct ptlrpc_bulk_desc *desc)
2142 {
2143         struct ptlrpc_cli_ctx *ctx;
2144         int rc;
2145
2146         LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2147
2148         if (!req->rq_pack_bulk)
2149                 return 0;
2150
2151         ctx = req->rq_cli_ctx;
2152         if (ctx->cc_ops->unwrap_bulk) {
2153                 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2154                 if (rc < 0)
2155                         return rc;
2156         }
2157
2158         /*
2159          * if everything is going right, nob should equals to nob_transferred.
2160          * in case of privacy mode, nob_transferred needs to be adjusted.
2161          */
2162         if (desc->bd_nob != desc->bd_nob_transferred) {
2163                 CERROR("nob %d doesn't match transferred nob %d\n",
2164                        desc->bd_nob, desc->bd_nob_transferred);
2165                 return -EPROTO;
2166         }
2167
2168         return 0;
2169 }
2170 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2171
2172 /****************************************
2173  * user descriptor helpers            *
2174  ****************************************/
2175
2176 int sptlrpc_current_user_desc_size(void)
2177 {
2178         int ngroups;
2179
2180         ngroups = current_ngroups;
2181
2182         if (ngroups > LUSTRE_MAX_GROUPS)
2183                 ngroups = LUSTRE_MAX_GROUPS;
2184         return sptlrpc_user_desc_size(ngroups);
2185 }
2186 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2187
2188 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2189 {
2190         struct ptlrpc_user_desc *pud;
2191
2192         pud = lustre_msg_buf(msg, offset, 0);
2193
2194         if (!pud)
2195                 return -EINVAL;
2196
2197         pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2198         pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2199         pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2200         pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2201         pud->pud_cap = cfs_curproc_cap_pack();
2202         pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2203
2204         task_lock(current);
2205         if (pud->pud_ngroups > current_ngroups)
2206                 pud->pud_ngroups = current_ngroups;
2207         memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2208                pud->pud_ngroups * sizeof(__u32));
2209         task_unlock(current);
2210
2211         return 0;
2212 }
2213 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2214
2215 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2216 {
2217         struct ptlrpc_user_desc *pud;
2218         int i;
2219
2220         pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2221         if (!pud)
2222                 return -EINVAL;
2223
2224         if (swabbed) {
2225                 __swab32s(&pud->pud_uid);
2226                 __swab32s(&pud->pud_gid);
2227                 __swab32s(&pud->pud_fsuid);
2228                 __swab32s(&pud->pud_fsgid);
2229                 __swab32s(&pud->pud_cap);
2230                 __swab32s(&pud->pud_ngroups);
2231         }
2232
2233         if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2234                 CERROR("%u groups is too large\n", pud->pud_ngroups);
2235                 return -EINVAL;
2236         }
2237
2238         if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2239             msg->lm_buflens[offset]) {
2240                 CERROR("%u groups are claimed but bufsize only %u\n",
2241                        pud->pud_ngroups, msg->lm_buflens[offset]);
2242                 return -EINVAL;
2243         }
2244
2245         if (swabbed) {
2246                 for (i = 0; i < pud->pud_ngroups; i++)
2247                         __swab32s(&pud->pud_groups[i]);
2248         }
2249
2250         return 0;
2251 }
2252 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2253
2254 /****************************************
2255  * misc helpers                  *
2256  ****************************************/
2257
2258 const char *sec2target_str(struct ptlrpc_sec *sec)
2259 {
2260         if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2261                 return "*";
2262         if (sec_is_reverse(sec))
2263                 return "c";
2264         return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2265 }
2266 EXPORT_SYMBOL(sec2target_str);
2267
2268 /*
2269  * return true if the bulk data is protected
2270  */
2271 bool sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2272 {
2273         switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2274         case SPTLRPC_BULK_SVC_INTG:
2275         case SPTLRPC_BULK_SVC_PRIV:
2276                 return true;
2277         default:
2278                 return false;
2279         }
2280 }
2281 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2282
2283 /****************************************
2284  * crypto API helper/alloc blkciper     *
2285  ****************************************/
2286
2287 /****************************************
2288  * initialize/finalize            *
2289  ****************************************/
2290
2291 int sptlrpc_init(void)
2292 {
2293         int rc;
2294
2295         rwlock_init(&policy_lock);
2296
2297         rc = sptlrpc_gc_init();
2298         if (rc)
2299                 goto out;
2300
2301         rc = sptlrpc_conf_init();
2302         if (rc)
2303                 goto out_gc;
2304
2305         rc = sptlrpc_enc_pool_init();
2306         if (rc)
2307                 goto out_conf;
2308
2309         rc = sptlrpc_null_init();
2310         if (rc)
2311                 goto out_pool;
2312
2313         rc = sptlrpc_plain_init();
2314         if (rc)
2315                 goto out_null;
2316
2317         rc = sptlrpc_lproc_init();
2318         if (rc)
2319                 goto out_plain;
2320
2321         return 0;
2322
2323 out_plain:
2324         sptlrpc_plain_fini();
2325 out_null:
2326         sptlrpc_null_fini();
2327 out_pool:
2328         sptlrpc_enc_pool_fini();
2329 out_conf:
2330         sptlrpc_conf_fini();
2331 out_gc:
2332         sptlrpc_gc_fini();
2333 out:
2334         return rc;
2335 }
2336
2337 void sptlrpc_fini(void)
2338 {
2339         sptlrpc_lproc_fini();
2340         sptlrpc_plain_fini();
2341         sptlrpc_null_fini();
2342         sptlrpc_enc_pool_fini();
2343         sptlrpc_conf_fini();
2344         sptlrpc_gc_fini();
2345 }