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