4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Author: Eric Mei <ericm@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_SEC
38 #include <linux/user_namespace.h>
39 #include <linux/uidgid.h>
40 #include <linux/crypto.h>
41 #include <linux/key.h>
43 #include <libcfs/libcfs.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_net.h>
48 #include <lustre_import.h>
49 #include <lustre_dlm.h>
50 #include <lustre_sec.h>
52 #include "ptlrpc_internal.h"
54 static int send_sepol;
55 module_param(send_sepol, int, 0644);
56 MODULE_PARM_DESC(send_sepol, "Client sends SELinux policy status");
62 static rwlock_t policy_lock;
63 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
67 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
69 __u16 number = policy->sp_policy;
71 LASSERT(policy->sp_name);
72 LASSERT(policy->sp_cops);
73 LASSERT(policy->sp_sops);
75 if (number >= SPTLRPC_POLICY_MAX)
78 write_lock(&policy_lock);
79 if (unlikely(policies[number])) {
80 write_unlock(&policy_lock);
83 policies[number] = policy;
84 write_unlock(&policy_lock);
86 CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
89 EXPORT_SYMBOL(sptlrpc_register_policy);
91 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
93 __u16 number = policy->sp_policy;
95 LASSERT(number < SPTLRPC_POLICY_MAX);
97 write_lock(&policy_lock);
98 if (unlikely(policies[number] == NULL)) {
99 write_unlock(&policy_lock);
100 CERROR("%s: already unregistered\n", policy->sp_name);
104 LASSERT(policies[number] == policy);
105 policies[number] = NULL;
106 write_unlock(&policy_lock);
108 CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
111 EXPORT_SYMBOL(sptlrpc_unregister_policy);
114 struct ptlrpc_sec_policy *sptlrpc_wireflavor2policy(__u32 flavor)
116 static DEFINE_MUTEX(load_mutex);
117 struct ptlrpc_sec_policy *policy;
118 __u16 number = SPTLRPC_FLVR_POLICY(flavor);
121 if (number >= SPTLRPC_POLICY_MAX)
125 read_lock(&policy_lock);
126 policy = policies[number];
127 if (policy && !try_module_get(policy->sp_owner))
129 read_unlock(&policy_lock);
131 if (policy != NULL || number != SPTLRPC_POLICY_GSS)
134 /* try to load gss module, happens only if policy at index
135 * SPTLRPC_POLICY_GSS is not already referenced in
136 * global array policies[]
138 mutex_lock(&load_mutex);
139 /* The fact that request_module() returns 0 does not guarantee
140 * the module has done its job. So we must check that the
141 * requested policy is now available. This is done by checking
142 * again for policies[number] in the loop.
144 rc = request_module("ptlrpc_gss");
146 CDEBUG(D_SEC, "module ptlrpc_gss loaded on demand\n");
148 CERROR("Unable to load module ptlrpc_gss: rc %d\n", rc);
149 mutex_unlock(&load_mutex);
155 __u32 sptlrpc_name2flavor_base(const char *name)
157 if (!strcmp(name, "null"))
158 return SPTLRPC_FLVR_NULL;
159 if (!strcmp(name, "plain"))
160 return SPTLRPC_FLVR_PLAIN;
161 if (!strcmp(name, "gssnull"))
162 return SPTLRPC_FLVR_GSSNULL;
163 if (!strcmp(name, "krb5n"))
164 return SPTLRPC_FLVR_KRB5N;
165 if (!strcmp(name, "krb5a"))
166 return SPTLRPC_FLVR_KRB5A;
167 if (!strcmp(name, "krb5i"))
168 return SPTLRPC_FLVR_KRB5I;
169 if (!strcmp(name, "krb5p"))
170 return SPTLRPC_FLVR_KRB5P;
171 if (!strcmp(name, "skn"))
172 return SPTLRPC_FLVR_SKN;
173 if (!strcmp(name, "ska"))
174 return SPTLRPC_FLVR_SKA;
175 if (!strcmp(name, "ski"))
176 return SPTLRPC_FLVR_SKI;
177 if (!strcmp(name, "skpi"))
178 return SPTLRPC_FLVR_SKPI;
180 return SPTLRPC_FLVR_INVALID;
182 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
184 const char *sptlrpc_flavor2name_base(__u32 flvr)
186 __u32 base = SPTLRPC_FLVR_BASE(flvr);
188 if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
190 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
192 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_GSSNULL))
194 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
196 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
198 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
200 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
202 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKN))
204 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKA))
206 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKI))
208 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKPI))
211 CERROR("invalid wire flavor 0x%x\n", flvr);
214 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
216 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
217 char *buf, int bufsize)
219 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
220 snprintf(buf, bufsize, "hash:%s",
221 sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
223 snprintf(buf, bufsize, "%s",
224 sptlrpc_flavor2name_base(sf->sf_rpc));
226 buf[bufsize - 1] = '\0';
229 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
231 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
235 ln = snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
238 * currently we don't support customized bulk specification for
239 * flavors other than plain
241 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
245 sptlrpc_flavor2name_bulk(sf, bspec + 1, sizeof(bspec) - 1);
246 strncat(buf, bspec, bufsize - ln);
249 buf[bufsize - 1] = '\0';
252 EXPORT_SYMBOL(sptlrpc_flavor2name);
254 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
258 if (flags & PTLRPC_SEC_FL_REVERSE)
259 strlcat(buf, "reverse,", bufsize);
260 if (flags & PTLRPC_SEC_FL_ROOTONLY)
261 strlcat(buf, "rootonly,", bufsize);
262 if (flags & PTLRPC_SEC_FL_UDESC)
263 strlcat(buf, "udesc,", bufsize);
264 if (flags & PTLRPC_SEC_FL_BULK)
265 strlcat(buf, "bulk,", bufsize);
267 strlcat(buf, "-,", bufsize);
271 EXPORT_SYMBOL(sptlrpc_secflags2str);
274 * client context APIs
278 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
280 struct vfs_cred vcred;
281 int create = 1, remove_dead = 1;
284 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
286 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
287 PTLRPC_SEC_FL_ROOTONLY)) {
290 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
295 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
296 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
299 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred, create,
303 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
305 atomic_inc(&ctx->cc_refcount);
308 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
310 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
312 struct ptlrpc_sec *sec = ctx->cc_sec;
315 LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
317 if (!atomic_dec_and_test(&ctx->cc_refcount))
320 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
322 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
325 * Expire the client context immediately.
327 * \pre Caller must hold at least 1 reference on the \a ctx.
329 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
331 LASSERT(ctx->cc_ops->die);
332 ctx->cc_ops->die(ctx, 0);
334 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
337 * To wake up the threads who are waiting for this client context. Called
338 * after some status change happened on \a ctx.
340 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
342 struct ptlrpc_request *req, *next;
344 spin_lock(&ctx->cc_lock);
345 list_for_each_entry_safe(req, next, &ctx->cc_req_list,
347 list_del_init(&req->rq_ctx_chain);
348 ptlrpc_client_wake_req(req);
350 spin_unlock(&ctx->cc_lock);
352 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
354 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
356 LASSERT(ctx->cc_ops);
358 if (ctx->cc_ops->display == NULL)
361 return ctx->cc_ops->display(ctx, buf, bufsize);
364 static int import_sec_check_expire(struct obd_import *imp)
368 write_lock(&imp->imp_sec_lock);
369 if (imp->imp_sec_expire &&
370 imp->imp_sec_expire < ktime_get_real_seconds()) {
372 imp->imp_sec_expire = 0;
374 write_unlock(&imp->imp_sec_lock);
379 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
380 return sptlrpc_import_sec_adapt(imp, NULL, NULL);
384 * Get and validate the client side ptlrpc security facilities from
385 * \a imp. There is a race condition on client reconnect when the import is
386 * being destroyed while there are outstanding client bound requests. In
387 * this case do not output any error messages if import secuity is not
390 * \param[in] imp obd import associated with client
391 * \param[out] sec client side ptlrpc security
393 * \retval 0 if security retrieved successfully
394 * \retval -ve errno if there was a problem
396 static int import_sec_validate_get(struct obd_import *imp,
397 struct ptlrpc_sec **sec)
401 if (unlikely(imp->imp_sec_expire)) {
402 rc = import_sec_check_expire(imp);
407 *sec = sptlrpc_import_sec_ref(imp);
409 /* Only output an error when the import is still active */
410 if (!test_bit(WORK_STRUCT_PENDING_BIT,
411 work_data_bits(&imp->imp_zombie_work)))
412 CERROR("import %p (%s) with no sec\n",
413 imp, ptlrpc_import_state_name(imp->imp_state));
417 if (unlikely((*sec)->ps_dying)) {
418 CERROR("attempt to use dying sec %p\n", sec);
419 sptlrpc_sec_put(*sec);
427 * Given a \a req, find or allocate an appropriate context for it.
428 * \pre req->rq_cli_ctx == NULL.
430 * \retval 0 succeed, and req->rq_cli_ctx is set.
431 * \retval -ev error number, and req->rq_cli_ctx == NULL.
433 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
435 struct obd_import *imp = req->rq_import;
436 struct ptlrpc_sec *sec;
441 LASSERT(!req->rq_cli_ctx);
444 rc = import_sec_validate_get(imp, &sec);
448 req->rq_cli_ctx = get_my_ctx(sec);
450 sptlrpc_sec_put(sec);
452 if (!req->rq_cli_ctx) {
453 CERROR("req %p: fail to get context\n", req);
454 RETURN(-ECONNREFUSED);
461 * Drop the context for \a req.
462 * \pre req->rq_cli_ctx != NULL.
463 * \post req->rq_cli_ctx == NULL.
465 * If \a sync == 0, this function should return quickly without sleep;
466 * otherwise it might trigger and wait for the whole process of sending
467 * an context-destroying rpc to server.
469 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
474 LASSERT(req->rq_cli_ctx);
477 * request might be asked to release earlier while still
478 * in the context waiting list.
480 if (!list_empty(&req->rq_ctx_chain)) {
481 spin_lock(&req->rq_cli_ctx->cc_lock);
482 list_del_init(&req->rq_ctx_chain);
483 spin_unlock(&req->rq_cli_ctx->cc_lock);
486 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
487 req->rq_cli_ctx = NULL;
492 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
493 struct ptlrpc_cli_ctx *oldctx,
494 struct ptlrpc_cli_ctx *newctx)
496 struct sptlrpc_flavor old_flvr;
497 char *reqmsg = NULL; /* to workaround old gcc */
502 "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), switch sec %p(%s) -> %p(%s)\n",
503 req, oldctx, oldctx->cc_vcred.vc_uid,
504 sec2target_str(oldctx->cc_sec), newctx, newctx->cc_vcred.vc_uid,
505 sec2target_str(newctx->cc_sec), oldctx->cc_sec,
506 oldctx->cc_sec->ps_policy->sp_name, newctx->cc_sec,
507 newctx->cc_sec->ps_policy->sp_name);
510 old_flvr = req->rq_flvr;
512 /* save request message */
513 reqmsg_size = req->rq_reqlen;
514 if (reqmsg_size != 0) {
515 LASSERT(req->rq_reqmsg);
516 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
519 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
522 /* release old req/rep buf */
523 req->rq_cli_ctx = oldctx;
524 sptlrpc_cli_free_reqbuf(req);
525 sptlrpc_cli_free_repbuf(req);
526 req->rq_cli_ctx = newctx;
528 /* recalculate the flavor */
529 sptlrpc_req_set_flavor(req, 0);
532 * alloc new request buffer
533 * we don't need to alloc reply buffer here, leave it to the
534 * rest procedure of ptlrpc
536 if (reqmsg_size != 0) {
537 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
539 LASSERT(req->rq_reqmsg);
540 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
542 CWARN("failed to alloc reqbuf: %d\n", rc);
543 req->rq_flvr = old_flvr;
546 OBD_FREE_LARGE(reqmsg, reqmsg_size);
552 * If current context of \a req is dead somehow, e.g. we just switched flavor
553 * thus marked original contexts dead, we'll find a new context for it. if
554 * no switch is needed, \a req will end up with the same context.
556 * \note a request must have a context, to keep other parts of code happy.
557 * In any case of failure during the switching, we must restore the old one.
559 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
561 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
562 struct ptlrpc_cli_ctx *newctx;
569 sptlrpc_cli_ctx_get(oldctx);
570 sptlrpc_req_put_ctx(req, 0);
572 rc = sptlrpc_req_get_ctx(req);
574 LASSERT(!req->rq_cli_ctx);
576 /* restore old ctx */
577 req->rq_cli_ctx = oldctx;
581 newctx = req->rq_cli_ctx;
584 if (unlikely(newctx == oldctx &&
585 test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
587 * still get the old dead ctx, usually means system too busy
590 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
591 newctx, newctx->cc_flags);
593 schedule_timeout_interruptible(cfs_time_seconds(1));
594 } else if (unlikely(test_bit(PTLRPC_CTX_UPTODATE_BIT, &newctx->cc_flags)
597 * new ctx not up to date yet
600 "ctx (%p, fl %lx) doesn't switch, not up to date yet\n",
601 newctx, newctx->cc_flags);
604 * it's possible newctx == oldctx if we're switching
605 * subflavor with the same sec.
607 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
609 /* restore old ctx */
610 sptlrpc_req_put_ctx(req, 0);
611 req->rq_cli_ctx = oldctx;
615 LASSERT(req->rq_cli_ctx == newctx);
618 sptlrpc_cli_ctx_put(oldctx, 1);
621 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
624 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
626 if (cli_ctx_is_refreshed(ctx))
632 void ctx_refresh_interrupt(struct ptlrpc_request *req)
635 spin_lock(&req->rq_lock);
637 spin_unlock(&req->rq_lock);
641 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
643 spin_lock(&ctx->cc_lock);
644 if (!list_empty(&req->rq_ctx_chain))
645 list_del_init(&req->rq_ctx_chain);
646 spin_unlock(&ctx->cc_lock);
650 * To refresh the context of \req, if it's not up-to-date.
652 * - == 0: do not wait
653 * - == MAX_SCHEDULE_TIMEOUT: wait indefinitely
654 * - > 0: not supported
656 * The status of the context could be subject to be changed by other threads
657 * at any time. We allow this race, but once we return with 0, the caller will
658 * suppose it's uptodated and keep using it until the owning rpc is done.
660 * \retval 0 only if the context is uptodated.
661 * \retval -ev error number.
663 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
665 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
666 struct ptlrpc_sec *sec;
673 if (req->rq_ctx_init || req->rq_ctx_fini)
676 if (timeout != 0 && timeout != MAX_SCHEDULE_TIMEOUT) {
677 CERROR("req %p: invalid timeout %lu\n", req, timeout);
682 * during the process a request's context might change type even
683 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
687 rc = import_sec_validate_get(req->rq_import, &sec);
691 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
692 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
693 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
694 req_off_ctx_list(req, ctx);
695 sptlrpc_req_replace_dead_ctx(req);
696 ctx = req->rq_cli_ctx;
698 sptlrpc_sec_put(sec);
700 if (cli_ctx_is_eternal(ctx))
703 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
704 if (ctx->cc_ops->refresh)
705 ctx->cc_ops->refresh(ctx);
707 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
709 LASSERT(ctx->cc_ops->validate);
710 if (ctx->cc_ops->validate(ctx) == 0) {
711 req_off_ctx_list(req, ctx);
715 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
716 spin_lock(&req->rq_lock);
718 spin_unlock(&req->rq_lock);
719 req_off_ctx_list(req, ctx);
724 * There's a subtle issue for resending RPCs, suppose following
726 * 1. the request was sent to server.
727 * 2. recovery was kicked start, after finished the request was
729 * 3. resend the request.
730 * 4. old reply from server received, we accept and verify the reply.
731 * this has to be success, otherwise the error will be aware
733 * 5. new reply from server received, dropped by LNet.
735 * Note the xid of old & new request is the same. We can't simply
736 * change xid for the resent request because the server replies on
737 * it for reply reconstruction.
739 * Commonly the original context should be uptodate because we
740 * have an expiry nice time; server will keep its context because
741 * we at least hold a ref of old context which prevent context
742 * from destroying RPC being sent. So server still can accept the
743 * request and finish the RPC. But if that's not the case:
744 * 1. If server side context has been trimmed, a NO_CONTEXT will
745 * be returned, gss_cli_ctx_verify/unseal will switch to new
747 * 2. Current context never be refreshed, then we are fine: we
748 * never really send request with old context before.
750 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
751 unlikely(req->rq_reqmsg) &&
752 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
753 req_off_ctx_list(req, ctx);
757 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
758 req_off_ctx_list(req, ctx);
760 * don't switch ctx if import was deactivated
762 if (req->rq_import->imp_deactive) {
763 spin_lock(&req->rq_lock);
765 spin_unlock(&req->rq_lock);
769 rc = sptlrpc_req_replace_dead_ctx(req);
771 LASSERT(ctx == req->rq_cli_ctx);
772 CERROR("req %p: failed to replace dead ctx %p: %d\n",
774 spin_lock(&req->rq_lock);
776 spin_unlock(&req->rq_lock);
780 ctx = req->rq_cli_ctx;
785 * Now we're sure this context is during upcall, add myself into
788 spin_lock(&ctx->cc_lock);
789 if (list_empty(&req->rq_ctx_chain))
790 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
791 spin_unlock(&ctx->cc_lock);
796 /* Clear any flags that may be present from previous sends */
797 LASSERT(req->rq_receiving_reply == 0);
798 spin_lock(&req->rq_lock);
800 req->rq_timedout = 0;
803 spin_unlock(&req->rq_lock);
805 /* by now we know that timeout value is MAX_SCHEDULE_TIMEOUT,
806 * so wait indefinitely with non-fatal signals blocked
808 if (l_wait_event_abortable(req->rq_reply_waitq,
809 ctx_check_refresh(ctx)) == -ERESTARTSYS) {
811 ctx_refresh_interrupt(req);
815 * following cases could lead us here:
816 * - successfully refreshed;
818 * - timedout, and we don't want recover from the failure;
819 * - timedout, and waked up upon recovery finished;
820 * - someone else mark this ctx dead by force;
821 * - someone invalidate the req and call ptlrpc_client_wake_req(),
822 * e.g. ptlrpc_abort_inflight();
824 if (!cli_ctx_is_refreshed(ctx)) {
825 /* timed out or interruptted */
826 req_off_ctx_list(req, ctx);
835 /* Bring ptlrpc_sec context up-to-date */
836 int sptlrpc_export_update_ctx(struct obd_export *exp)
838 struct obd_import *imp = exp ? exp->exp_imp_reverse : NULL;
839 struct ptlrpc_sec *sec = NULL;
840 struct ptlrpc_cli_ctx *ctx = NULL;
844 sec = sptlrpc_import_sec_ref(imp);
846 ctx = get_my_ctx(sec);
847 sptlrpc_sec_put(sec);
851 if (ctx->cc_ops->refresh)
852 rc = ctx->cc_ops->refresh(ctx);
853 sptlrpc_cli_ctx_put(ctx, 1);
859 * Initialize flavor settings for \a req, according to \a opcode.
861 * \note this could be called in two situations:
862 * - new request from ptlrpc_pre_req(), with proper @opcode
863 * - old request which changed ctx in the middle, with @opcode == 0
865 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
867 struct ptlrpc_sec *sec;
869 LASSERT(req->rq_import);
870 LASSERT(req->rq_cli_ctx);
871 LASSERT(req->rq_cli_ctx->cc_sec);
872 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
874 /* special security flags according to opcode */
878 case MGS_CONFIG_READ:
880 req->rq_bulk_read = 1;
884 req->rq_bulk_write = 1;
887 req->rq_ctx_init = 1;
890 req->rq_ctx_fini = 1;
893 /* init/fini rpc won't be resend, so can't be here */
894 LASSERT(req->rq_ctx_init == 0);
895 LASSERT(req->rq_ctx_fini == 0);
897 /* cleanup flags, which should be recalculated */
898 req->rq_pack_udesc = 0;
899 req->rq_pack_bulk = 0;
903 sec = req->rq_cli_ctx->cc_sec;
905 spin_lock(&sec->ps_lock);
906 req->rq_flvr = sec->ps_flvr;
907 spin_unlock(&sec->ps_lock);
910 * force SVC_NULL for context initiation rpc, SVC_INTG for context
913 if (unlikely(req->rq_ctx_init))
914 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
915 else if (unlikely(req->rq_ctx_fini))
916 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
918 /* user descriptor flag, null security can't do it anyway */
919 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
920 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
921 req->rq_pack_udesc = 1;
923 /* bulk security flag */
924 if ((req->rq_bulk_read || req->rq_bulk_write) &&
925 sptlrpc_flavor_has_bulk(&req->rq_flvr))
926 req->rq_pack_bulk = 1;
929 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
931 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
934 LASSERT(req->rq_clrbuf);
935 if (req->rq_pool || !req->rq_reqbuf)
938 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
939 req->rq_reqbuf = NULL;
940 req->rq_reqbuf_len = 0;
944 * Given an import \a imp, check whether current user has a valid context
945 * or not. We may create a new context and try to refresh it, and try
946 * repeatedly try in case of non-fatal errors. Return 0 means success.
948 int sptlrpc_import_check_ctx(struct obd_import *imp)
950 struct ptlrpc_sec *sec;
951 struct ptlrpc_cli_ctx *ctx;
952 struct ptlrpc_request *req = NULL;
959 sec = sptlrpc_import_sec_ref(imp);
960 ctx = get_my_ctx(sec);
961 sptlrpc_sec_put(sec);
966 if (cli_ctx_is_eternal(ctx) ||
967 ctx->cc_ops->validate(ctx) == 0) {
968 sptlrpc_cli_ctx_put(ctx, 1);
972 if (cli_ctx_is_error(ctx)) {
973 sptlrpc_cli_ctx_put(ctx, 1);
977 req = ptlrpc_request_cache_alloc(GFP_NOFS);
981 ptlrpc_cli_req_init(req);
982 atomic_set(&req->rq_refcount, 10000);
984 req->rq_import = imp;
985 req->rq_flvr = sec->ps_flvr;
986 req->rq_cli_ctx = ctx;
988 rc = sptlrpc_req_refresh_ctx(req, MAX_SCHEDULE_TIMEOUT);
989 LASSERT(list_empty(&req->rq_ctx_chain));
990 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
991 ptlrpc_request_cache_free(req);
997 * Used by ptlrpc client, to perform the pre-defined security transformation
998 * upon the request message of \a req. After this function called,
999 * req->rq_reqmsg is still accessible as clear text.
1001 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
1003 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1009 LASSERT(ctx->cc_sec);
1010 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1013 * we wrap bulk request here because now we can be sure
1014 * the context is uptodate.
1017 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
1022 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1023 case SPTLRPC_SVC_NULL:
1024 case SPTLRPC_SVC_AUTH:
1025 case SPTLRPC_SVC_INTG:
1026 LASSERT(ctx->cc_ops->sign);
1027 rc = ctx->cc_ops->sign(ctx, req);
1029 case SPTLRPC_SVC_PRIV:
1030 LASSERT(ctx->cc_ops->seal);
1031 rc = ctx->cc_ops->seal(ctx, req);
1038 LASSERT(req->rq_reqdata_len);
1039 LASSERT(req->rq_reqdata_len % 8 == 0);
1040 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
1046 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
1048 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1054 LASSERT(ctx->cc_sec);
1055 LASSERT(req->rq_repbuf);
1056 LASSERT(req->rq_repdata);
1057 LASSERT(req->rq_repmsg == NULL);
1059 req->rq_rep_swab_mask = 0;
1061 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
1064 req_capsule_set_rep_swabbed(&req->rq_pill,
1065 MSG_PTLRPC_HEADER_OFF);
1069 CERROR("failed unpack reply: x%llu\n", req->rq_xid);
1073 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
1074 CERROR("replied data length %d too small\n",
1075 req->rq_repdata_len);
1079 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1080 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1081 CERROR("reply policy %u doesn't match request policy %u\n",
1082 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1083 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1087 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1088 case SPTLRPC_SVC_NULL:
1089 case SPTLRPC_SVC_AUTH:
1090 case SPTLRPC_SVC_INTG:
1091 LASSERT(ctx->cc_ops->verify);
1092 rc = ctx->cc_ops->verify(ctx, req);
1094 case SPTLRPC_SVC_PRIV:
1095 LASSERT(ctx->cc_ops->unseal);
1096 rc = ctx->cc_ops->unseal(ctx, req);
1101 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1103 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1105 req->rq_rep_swab_mask = 0;
1110 * Used by ptlrpc client, to perform security transformation upon the reply
1111 * message of \a req. After return successfully, req->rq_repmsg points to
1112 * the reply message in clear text.
1114 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1117 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1119 LASSERT(req->rq_repbuf);
1120 LASSERT(req->rq_repdata == NULL);
1121 LASSERT(req->rq_repmsg == NULL);
1122 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1124 if (req->rq_reply_off == 0 &&
1125 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1126 CERROR("real reply with offset 0\n");
1130 if (req->rq_reply_off % 8 != 0) {
1131 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1135 req->rq_repdata = (struct lustre_msg *)
1136 (req->rq_repbuf + req->rq_reply_off);
1137 req->rq_repdata_len = req->rq_nob_received;
1139 return do_cli_unwrap_reply(req);
1143 * Used by ptlrpc client, to perform security transformation upon the early
1144 * reply message of \a req. We expect the rq_reply_off is 0, and
1145 * rq_nob_received is the early reply size.
1147 * Because the receive buffer might be still posted, the reply data might be
1148 * changed at any time, no matter we're holding rq_lock or not. For this reason
1149 * we allocate a separate ptlrpc_request and reply buffer for early reply
1152 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1153 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1154 * \a *req_ret to release it.
1155 * \retval -ev error number, and \a req_ret will not be set.
1157 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1158 struct ptlrpc_request **req_ret)
1160 struct ptlrpc_request *early_req;
1162 int early_bufsz, early_size;
1167 early_req = ptlrpc_request_cache_alloc(GFP_NOFS);
1168 if (early_req == NULL)
1171 ptlrpc_cli_req_init(early_req);
1173 early_size = req->rq_nob_received;
1174 early_bufsz = size_roundup_power2(early_size);
1175 OBD_ALLOC_LARGE(early_buf, early_bufsz);
1176 if (early_buf == NULL)
1177 GOTO(err_req, rc = -ENOMEM);
1179 /* sanity checkings and copy data out, do it inside spinlock */
1180 spin_lock(&req->rq_lock);
1182 if (req->rq_replied) {
1183 spin_unlock(&req->rq_lock);
1184 GOTO(err_buf, rc = -EALREADY);
1187 LASSERT(req->rq_repbuf);
1188 LASSERT(req->rq_repdata == NULL);
1189 LASSERT(req->rq_repmsg == NULL);
1191 if (req->rq_reply_off != 0) {
1192 CERROR("early reply with offset %u\n", req->rq_reply_off);
1193 spin_unlock(&req->rq_lock);
1194 GOTO(err_buf, rc = -EPROTO);
1197 if (req->rq_nob_received != early_size) {
1198 /* even another early arrived the size should be the same */
1199 CERROR("data size has changed from %u to %u\n",
1200 early_size, req->rq_nob_received);
1201 spin_unlock(&req->rq_lock);
1202 GOTO(err_buf, rc = -EINVAL);
1205 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1206 CERROR("early reply length %d too small\n",
1207 req->rq_nob_received);
1208 spin_unlock(&req->rq_lock);
1209 GOTO(err_buf, rc = -EALREADY);
1212 memcpy(early_buf, req->rq_repbuf, early_size);
1213 spin_unlock(&req->rq_lock);
1215 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1216 early_req->rq_flvr = req->rq_flvr;
1217 early_req->rq_repbuf = early_buf;
1218 early_req->rq_repbuf_len = early_bufsz;
1219 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1220 early_req->rq_repdata_len = early_size;
1221 early_req->rq_early = 1;
1222 early_req->rq_reqmsg = req->rq_reqmsg;
1224 rc = do_cli_unwrap_reply(early_req);
1226 DEBUG_REQ(D_ADAPTTO, early_req,
1227 "unwrap early reply: rc = %d", rc);
1231 LASSERT(early_req->rq_repmsg);
1232 *req_ret = early_req;
1236 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1238 OBD_FREE_LARGE(early_buf, early_bufsz);
1240 ptlrpc_request_cache_free(early_req);
1245 * Used by ptlrpc client, to release a processed early reply \a early_req.
1247 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1249 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1251 LASSERT(early_req->rq_repbuf);
1252 LASSERT(early_req->rq_repdata);
1253 LASSERT(early_req->rq_repmsg);
1255 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1256 OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1257 ptlrpc_request_cache_free(early_req);
1260 /**************************************************
1262 **************************************************/
1265 * "fixed" sec (e.g. null) use sec_id < 0
1267 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1269 int sptlrpc_get_next_secid(void)
1271 return atomic_inc_return(&sptlrpc_sec_id);
1273 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1276 * client side high-level security APIs
1279 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1280 int grace, int force)
1282 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1284 LASSERT(policy->sp_cops);
1285 LASSERT(policy->sp_cops->flush_ctx_cache);
1287 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1290 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1292 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1294 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1295 LASSERT(atomic_read(&sec->ps_nctx) == 0);
1296 LASSERT(policy->sp_cops->destroy_sec);
1298 CDEBUG(D_SEC, "%s@%p: being destroyed\n", sec->ps_policy->sp_name, sec);
1300 policy->sp_cops->destroy_sec(sec);
1301 sptlrpc_policy_put(policy);
1304 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1306 sec_cop_destroy_sec(sec);
1308 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1310 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1312 LASSERT(atomic_read(&(sec)->ps_refcount) > 0);
1314 if (sec->ps_policy->sp_cops->kill_sec) {
1315 sec->ps_policy->sp_cops->kill_sec(sec);
1317 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1321 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1324 atomic_inc(&sec->ps_refcount);
1328 EXPORT_SYMBOL(sptlrpc_sec_get);
1330 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1333 LASSERT(atomic_read(&(sec)->ps_refcount) > 0);
1335 if (atomic_dec_and_test(&sec->ps_refcount)) {
1336 sptlrpc_gc_del_sec(sec);
1337 sec_cop_destroy_sec(sec);
1341 EXPORT_SYMBOL(sptlrpc_sec_put);
1344 * policy module is responsible for taking refrence of import
1347 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1348 struct ptlrpc_svc_ctx *svc_ctx,
1349 struct sptlrpc_flavor *sf,
1350 enum lustre_sec_part sp)
1352 struct ptlrpc_sec_policy *policy;
1353 struct ptlrpc_sec *sec;
1359 LASSERT(imp->imp_dlm_fake == 1);
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)));
1366 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1367 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1369 LASSERT(imp->imp_dlm_fake == 0);
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)));
1376 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1378 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1383 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1385 atomic_inc(&sec->ps_refcount);
1389 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1390 sptlrpc_gc_add_sec(sec);
1392 sptlrpc_policy_put(policy);
1398 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1400 struct ptlrpc_sec *sec;
1402 read_lock(&imp->imp_sec_lock);
1403 sec = sptlrpc_sec_get(imp->imp_sec);
1404 read_unlock(&imp->imp_sec_lock);
1408 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1410 static void sptlrpc_import_sec_install(struct obd_import *imp,
1411 struct ptlrpc_sec *sec)
1413 struct ptlrpc_sec *old_sec;
1415 LASSERT(atomic_read(&(sec)->ps_refcount) > 0);
1417 write_lock(&imp->imp_sec_lock);
1418 old_sec = imp->imp_sec;
1420 write_unlock(&imp->imp_sec_lock);
1423 sptlrpc_sec_kill(old_sec);
1425 /* balance the ref taken by this import */
1426 sptlrpc_sec_put(old_sec);
1431 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1433 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1437 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
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.
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.
1449 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1450 struct ptlrpc_svc_ctx *svc_ctx,
1451 struct sptlrpc_flavor *flvr)
1453 struct ptlrpc_connection *conn;
1454 struct sptlrpc_flavor sf;
1455 struct ptlrpc_sec *sec, *newsec;
1456 enum lustre_sec_part sp;
1467 conn = imp->imp_connection;
1469 if (svc_ctx == NULL) {
1470 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1472 * normal import, determine flavor from rule set, except
1473 * for mgc the flavor is predetermined.
1475 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1476 sf = cliobd->cl_flvr_mgc;
1478 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1480 &cliobd->cl_target_uuid,
1481 &conn->c_self, &sf);
1483 sp = imp->imp_obd->u.cli.cl_sp_me;
1485 /* reverse import, determine flavor from incoming reqeust */
1488 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1489 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1490 PTLRPC_SEC_FL_ROOTONLY;
1492 sp = sptlrpc_target_sec_part(imp->imp_obd);
1495 sec = sptlrpc_import_sec_ref(imp);
1499 if (flavor_equal(&sf, &sec->ps_flvr))
1502 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1503 imp->imp_obd->obd_name,
1504 obd_uuid2str(&conn->c_remote_uuid),
1505 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1506 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1507 } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1508 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1509 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1510 imp->imp_obd->obd_name,
1511 obd_uuid2str(&conn->c_remote_uuid),
1512 LNET_NID_NET(&conn->c_self),
1513 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1516 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1518 sptlrpc_import_sec_install(imp, newsec);
1520 CERROR("import %s->%s: failed to create new sec\n",
1521 imp->imp_obd->obd_name,
1522 obd_uuid2str(&conn->c_remote_uuid));
1527 sptlrpc_sec_put(sec);
1531 void sptlrpc_import_sec_put(struct obd_import *imp)
1534 sptlrpc_sec_kill(imp->imp_sec);
1536 sptlrpc_sec_put(imp->imp_sec);
1537 imp->imp_sec = NULL;
1541 static void import_flush_ctx_common(struct obd_import *imp,
1542 uid_t uid, int grace, int force)
1544 struct ptlrpc_sec *sec;
1549 sec = sptlrpc_import_sec_ref(imp);
1553 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1554 sptlrpc_sec_put(sec);
1557 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1560 * it's important to use grace mode, see explain in
1561 * sptlrpc_req_refresh_ctx()
1563 import_flush_ctx_common(imp, 0, 1, 1);
1566 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1568 import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1571 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1573 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1575 import_flush_ctx_common(imp, -1, 1, 1);
1577 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
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.
1583 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1585 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1586 struct ptlrpc_sec_policy *policy;
1590 LASSERT(ctx->cc_sec);
1591 LASSERT(ctx->cc_sec->ps_policy);
1592 LASSERT(req->rq_reqmsg == NULL);
1593 LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
1595 policy = ctx->cc_sec->ps_policy;
1596 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1598 LASSERT(req->rq_reqmsg);
1599 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1601 /* zeroing preallocated buffer */
1603 memset(req->rq_reqmsg, 0, msgsize);
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.
1613 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1615 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1616 struct ptlrpc_sec_policy *policy;
1619 LASSERT(ctx->cc_sec);
1620 LASSERT(ctx->cc_sec->ps_policy);
1621 LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
1623 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1626 policy = ctx->cc_sec->ps_policy;
1627 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1628 req->rq_reqmsg = NULL;
1632 * NOTE caller must guarantee the buffer size is enough for the enlargement
1634 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1635 int segment, int newsize)
1638 int oldsize, oldmsg_size, movesize;
1640 LASSERT(segment < msg->lm_bufcount);
1641 LASSERT(msg->lm_buflens[segment] <= newsize);
1643 if (msg->lm_buflens[segment] == newsize)
1646 /* nothing to do if we are enlarging the last segment */
1647 if (segment == msg->lm_bufcount - 1) {
1648 msg->lm_buflens[segment] = newsize;
1652 oldsize = msg->lm_buflens[segment];
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;
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);
1666 memmove(dst, src, movesize);
1668 /* note we don't clear the ares where old data live, not secret */
1670 /* finally set new segment size */
1671 msg->lm_buflens[segment] = newsize;
1673 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
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.
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.
1684 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1685 const struct req_msg_field *field,
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);
1696 LASSERT(msg->lm_bufcount > segment);
1697 LASSERT(msg->lm_buflens[segment] <= newsize);
1699 if (msg->lm_buflens[segment] == newsize)
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);
1706 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1709 * Used by ptlrpc client to allocate reply buffer of \a req.
1711 * \note After this, req->rq_repmsg is still not accessible.
1713 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1715 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1716 struct ptlrpc_sec_policy *policy;
1721 LASSERT(ctx->cc_sec);
1722 LASSERT(ctx->cc_sec->ps_policy);
1727 policy = ctx->cc_sec->ps_policy;
1728 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1732 * Used by ptlrpc client to free reply buffer of \a req. After this
1733 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1735 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1737 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1738 struct ptlrpc_sec_policy *policy;
1743 LASSERT(ctx->cc_sec);
1744 LASSERT(ctx->cc_sec->ps_policy);
1745 LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
1747 if (req->rq_repbuf == NULL)
1749 LASSERT(req->rq_repbuf_len);
1751 policy = ctx->cc_sec->ps_policy;
1752 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1753 req->rq_repmsg = NULL;
1756 EXPORT_SYMBOL(sptlrpc_cli_free_repbuf);
1758 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1759 struct ptlrpc_cli_ctx *ctx)
1761 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1763 if (!policy->sp_cops->install_rctx)
1765 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1768 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1769 struct ptlrpc_svc_ctx *ctx)
1771 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1773 if (!policy->sp_sops->install_rctx)
1775 return policy->sp_sops->install_rctx(imp, ctx);
1778 /* Get SELinux policy info from userspace */
1779 static int sepol_helper(struct obd_import *imp)
1781 char mtime_str[21] = { 0 }, mode_str[2] = { 0 };
1783 [0] = "/usr/sbin/l_getsepol",
1785 [2] = NULL, /* obd type */
1787 [4] = NULL, /* obd name */
1789 [6] = mtime_str, /* policy mtime */
1791 [8] = mode_str, /* enforcing mode */
1796 [1] = "PATH=/sbin:/usr/sbin",
1802 if (imp == NULL || imp->imp_obd == NULL ||
1803 imp->imp_obd->obd_type == NULL) {
1806 argv[2] = (char *)imp->imp_obd->obd_type->typ_name;
1807 argv[4] = imp->imp_obd->obd_name;
1808 spin_lock(&imp->imp_sec->ps_lock);
1809 if (ktime_to_ns(imp->imp_sec->ps_sepol_mtime) == 0 &&
1810 imp->imp_sec->ps_sepol[0] == '\0') {
1811 /* ps_sepol has not been initialized */
1817 mtime_ms = ktime_to_ms(imp->imp_sec->ps_sepol_mtime);
1818 snprintf(mtime_str, sizeof(mtime_str), "%lld",
1819 mtime_ms / MSEC_PER_SEC);
1820 mode_str[0] = imp->imp_sec->ps_sepol[0];
1822 spin_unlock(&imp->imp_sec->ps_lock);
1823 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1830 static inline int sptlrpc_sepol_needs_check(struct ptlrpc_sec *imp_sec)
1834 if (send_sepol == 0)
1837 if (send_sepol == -1)
1838 /* send_sepol == -1 means fetch sepol status every time */
1841 spin_lock(&imp_sec->ps_lock);
1842 checknext = imp_sec->ps_sepol_checknext;
1843 spin_unlock(&imp_sec->ps_lock);
1845 /* next check is too far in time, please update */
1846 if (ktime_after(checknext,
1847 ktime_add(ktime_get(), ktime_set(send_sepol, 0))))
1850 if (ktime_before(ktime_get(), checknext))
1851 /* too early to fetch sepol status */
1855 /* define new sepol_checknext time */
1856 spin_lock(&imp_sec->ps_lock);
1857 imp_sec->ps_sepol_checknext = ktime_add(ktime_get(),
1858 ktime_set(send_sepol, 0));
1859 spin_unlock(&imp_sec->ps_lock);
1864 int sptlrpc_get_sepol(struct ptlrpc_request *req)
1866 struct ptlrpc_sec *imp_sec = req->rq_import->imp_sec;
1871 (req->rq_sepol)[0] = '\0';
1873 #ifndef HAVE_SELINUX
1874 if (unlikely(send_sepol != 0))
1876 "Client cannot report SELinux status, it was not built against libselinux.\n");
1880 if (send_sepol == 0)
1883 if (imp_sec == NULL)
1886 /* Retrieve SELinux status info */
1887 if (sptlrpc_sepol_needs_check(imp_sec))
1888 rc = sepol_helper(req->rq_import);
1889 if (likely(rc == 0)) {
1890 spin_lock(&imp_sec->ps_lock);
1891 memcpy(req->rq_sepol, imp_sec->ps_sepol,
1892 sizeof(req->rq_sepol));
1893 spin_unlock(&imp_sec->ps_lock);
1894 } else if (rc == -ENODEV) {
1896 "Client cannot report SELinux status, SELinux is disabled.\n");
1902 EXPORT_SYMBOL(sptlrpc_get_sepol);
1905 * server side security
1908 static int flavor_allowed(struct sptlrpc_flavor *exp,
1909 struct ptlrpc_request *req)
1911 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1913 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1916 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1917 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1918 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1919 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1925 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1928 * Given an export \a exp, check whether the flavor of incoming \a req
1929 * is allowed by the export \a exp. Main logic is about taking care of
1930 * changing configurations. Return 0 means success.
1932 int sptlrpc_target_export_check(struct obd_export *exp,
1933 struct ptlrpc_request *req)
1935 struct sptlrpc_flavor flavor;
1941 * client side export has no imp_reverse, skip
1942 * FIXME maybe we should check flavor this as well???
1944 if (exp->exp_imp_reverse == NULL)
1947 /* don't care about ctx fini rpc */
1948 if (req->rq_ctx_fini)
1951 spin_lock(&exp->exp_lock);
1954 * if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1955 * the first req with the new flavor, then treat it as current flavor,
1956 * adapt reverse sec according to it.
1957 * note the first rpc with new flavor might not be with root ctx, in
1958 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1.
1960 if (unlikely(exp->exp_flvr_changed) &&
1961 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1963 * make the new flavor as "current", and old ones as
1966 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1967 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1968 flavor = exp->exp_flvr_old[1];
1969 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1970 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1971 exp->exp_flvr_old[0] = exp->exp_flvr;
1972 exp->exp_flvr_expire[0] = ktime_get_real_seconds() +
1973 EXP_FLVR_UPDATE_EXPIRE;
1974 exp->exp_flvr = flavor;
1976 /* flavor change finished */
1977 exp->exp_flvr_changed = 0;
1978 LASSERT(exp->exp_flvr_adapt == 1);
1980 /* if it's gss, we only interested in root ctx init */
1981 if (req->rq_auth_gss &&
1982 !(req->rq_ctx_init &&
1983 (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1984 req->rq_auth_usr_ost))) {
1985 spin_unlock(&exp->exp_lock);
1986 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1987 req->rq_auth_gss, req->rq_ctx_init,
1988 req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1989 req->rq_auth_usr_ost);
1993 exp->exp_flvr_adapt = 0;
1994 spin_unlock(&exp->exp_lock);
1996 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1997 req->rq_svc_ctx, &flavor);
2001 * if it equals to the current flavor, we accept it, but need to
2002 * dealing with reverse sec/ctx
2004 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
2006 * most cases should return here, we only interested in
2009 if (!req->rq_auth_gss || !req->rq_ctx_init ||
2010 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2011 !req->rq_auth_usr_ost)) {
2012 spin_unlock(&exp->exp_lock);
2017 * if flavor just changed, we should not proceed, just leave
2018 * it and current flavor will be discovered and replaced
2019 * shortly, and let _this_ rpc pass through
2021 if (exp->exp_flvr_changed) {
2022 LASSERT(exp->exp_flvr_adapt);
2023 spin_unlock(&exp->exp_lock);
2027 if (exp->exp_flvr_adapt) {
2028 exp->exp_flvr_adapt = 0;
2029 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
2030 exp, exp->exp_flvr.sf_rpc,
2031 exp->exp_flvr_old[0].sf_rpc,
2032 exp->exp_flvr_old[1].sf_rpc);
2033 flavor = exp->exp_flvr;
2034 spin_unlock(&exp->exp_lock);
2036 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
2041 "exp %p (%x|%x|%x): is current flavor, install rvs ctx\n",
2042 exp, exp->exp_flvr.sf_rpc,
2043 exp->exp_flvr_old[0].sf_rpc,
2044 exp->exp_flvr_old[1].sf_rpc);
2045 spin_unlock(&exp->exp_lock);
2047 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
2052 if (exp->exp_flvr_expire[0]) {
2053 if (exp->exp_flvr_expire[0] >= ktime_get_real_seconds()) {
2054 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
2056 "exp %p (%x|%x|%x): match the middle one (%lld)\n",
2057 exp, exp->exp_flvr.sf_rpc,
2058 exp->exp_flvr_old[0].sf_rpc,
2059 exp->exp_flvr_old[1].sf_rpc,
2060 (s64)(exp->exp_flvr_expire[0] -
2061 ktime_get_real_seconds()));
2062 spin_unlock(&exp->exp_lock);
2066 CDEBUG(D_SEC, "mark middle expired\n");
2067 exp->exp_flvr_expire[0] = 0;
2069 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
2070 exp->exp_flvr.sf_rpc,
2071 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2072 req->rq_flvr.sf_rpc);
2076 * now it doesn't match the current flavor, the only chance we can
2077 * accept it is match the old flavors which is not expired.
2079 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
2080 if (exp->exp_flvr_expire[1] >= ktime_get_real_seconds()) {
2081 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
2082 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the oldest one (%lld)\n",
2084 exp->exp_flvr.sf_rpc,
2085 exp->exp_flvr_old[0].sf_rpc,
2086 exp->exp_flvr_old[1].sf_rpc,
2087 (s64)(exp->exp_flvr_expire[1] -
2088 ktime_get_real_seconds()));
2089 spin_unlock(&exp->exp_lock);
2093 CDEBUG(D_SEC, "mark oldest expired\n");
2094 exp->exp_flvr_expire[1] = 0;
2096 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
2097 exp, exp->exp_flvr.sf_rpc,
2098 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2099 req->rq_flvr.sf_rpc);
2101 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
2102 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
2103 exp->exp_flvr_old[1].sf_rpc);
2106 spin_unlock(&exp->exp_lock);
2108 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with unauthorized flavor %x, expect %x|%x(%+lld)|%x(%+lld)\n",
2109 exp, exp->exp_obd->obd_name,
2110 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
2111 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
2112 req->rq_flvr.sf_rpc,
2113 exp->exp_flvr.sf_rpc,
2114 exp->exp_flvr_old[0].sf_rpc,
2115 exp->exp_flvr_expire[0] ?
2116 (s64)(exp->exp_flvr_expire[0] - ktime_get_real_seconds()) : 0,
2117 exp->exp_flvr_old[1].sf_rpc,
2118 exp->exp_flvr_expire[1] ?
2119 (s64)(exp->exp_flvr_expire[1] - ktime_get_real_seconds()) : 0);
2122 EXPORT_SYMBOL(sptlrpc_target_export_check);
2124 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
2125 struct sptlrpc_rule_set *rset)
2127 struct obd_export *exp;
2128 struct sptlrpc_flavor new_flvr;
2132 spin_lock(&obd->obd_dev_lock);
2134 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
2135 if (exp->exp_connection == NULL)
2139 * note if this export had just been updated flavor
2140 * (exp_flvr_changed == 1), this will override the
2143 spin_lock(&exp->exp_lock);
2144 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
2145 &exp->exp_connection->c_peer.nid,
2147 if (exp->exp_flvr_changed ||
2148 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
2149 exp->exp_flvr_old[1] = new_flvr;
2150 exp->exp_flvr_expire[1] = 0;
2151 exp->exp_flvr_changed = 1;
2152 exp->exp_flvr_adapt = 1;
2154 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
2155 exp, sptlrpc_part2name(exp->exp_sp_peer),
2156 exp->exp_flvr.sf_rpc,
2157 exp->exp_flvr_old[1].sf_rpc);
2159 spin_unlock(&exp->exp_lock);
2162 spin_unlock(&obd->obd_dev_lock);
2164 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
2166 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
2168 /* peer's claim is unreliable unless gss is being used */
2169 if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
2172 switch (req->rq_sp_from) {
2174 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
2175 /* The below message is checked in sanity-sec test_33 */
2176 DEBUG_REQ(D_ERROR, req, "faked source CLI");
2177 svc_rc = SECSVC_DROP;
2181 if (!req->rq_auth_usr_mdt) {
2182 /* The below message is checked in sanity-sec test_33 */
2183 DEBUG_REQ(D_ERROR, req, "faked source MDT");
2184 svc_rc = SECSVC_DROP;
2188 if (!req->rq_auth_usr_ost) {
2189 /* The below message is checked in sanity-sec test_33 */
2190 DEBUG_REQ(D_ERROR, req, "faked source OST");
2191 svc_rc = SECSVC_DROP;
2196 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2197 !req->rq_auth_usr_ost) {
2198 /* The below message is checked in sanity-sec test_33 */
2199 DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
2200 svc_rc = SECSVC_DROP;
2205 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2206 svc_rc = SECSVC_DROP;
2213 * Used by ptlrpc server, to perform transformation upon request message of
2214 * incoming \a req. This must be the first thing to do with an incoming
2215 * request in ptlrpc layer.
2217 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2218 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2219 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2220 * reply message has been prepared.
2221 * \retval SECSVC_DROP failed, this request should be dropped.
2223 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2225 struct ptlrpc_sec_policy *policy;
2226 struct lustre_msg *msg = req->rq_reqbuf;
2232 LASSERT(req->rq_reqmsg == NULL);
2233 LASSERT(req->rq_repmsg == NULL);
2234 LASSERT(req->rq_svc_ctx == NULL);
2236 req->rq_req_swab_mask = 0;
2238 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2241 req_capsule_set_req_swabbed(&req->rq_pill,
2242 MSG_PTLRPC_HEADER_OFF);
2246 CERROR("error unpacking request from %s x%llu\n",
2247 libcfs_idstr(&req->rq_peer), req->rq_xid);
2248 RETURN(SECSVC_DROP);
2251 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2252 req->rq_sp_from = LUSTRE_SP_ANY;
2253 req->rq_auth_uid = -1; /* set to INVALID_UID */
2254 req->rq_auth_mapped_uid = -1;
2256 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2258 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2259 RETURN(SECSVC_DROP);
2262 LASSERT(policy->sp_sops->accept);
2263 rc = policy->sp_sops->accept(req);
2264 sptlrpc_policy_put(policy);
2265 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2266 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2269 * if it's not null flavor (which means embedded packing msg),
2270 * reset the swab mask for the comming inner msg unpacking.
2272 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2273 req->rq_req_swab_mask = 0;
2275 /* sanity check for the request source */
2276 rc = sptlrpc_svc_check_from(req, rc);
2281 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2282 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2283 * a buffer of \a msglen size.
2285 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2287 struct ptlrpc_sec_policy *policy;
2288 struct ptlrpc_reply_state *rs;
2293 LASSERT(req->rq_svc_ctx);
2294 LASSERT(req->rq_svc_ctx->sc_policy);
2296 policy = req->rq_svc_ctx->sc_policy;
2297 LASSERT(policy->sp_sops->alloc_rs);
2299 rc = policy->sp_sops->alloc_rs(req, msglen);
2300 if (unlikely(rc == -ENOMEM)) {
2301 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
2303 if (svcpt->scp_service->srv_max_reply_size <
2304 msglen + sizeof(struct ptlrpc_reply_state)) {
2305 /* Just return failure if the size is too big */
2306 CERROR("size of message is too big (%zd), %d allowed\n",
2307 msglen + sizeof(struct ptlrpc_reply_state),
2308 svcpt->scp_service->srv_max_reply_size);
2312 /* failed alloc, try emergency pool */
2313 rs = lustre_get_emerg_rs(svcpt);
2317 req->rq_reply_state = rs;
2318 rc = policy->sp_sops->alloc_rs(req, msglen);
2320 lustre_put_emerg_rs(rs);
2321 req->rq_reply_state = NULL;
2326 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2332 * Used by ptlrpc server, to perform transformation upon reply message.
2334 * \post req->rq_reply_off is set to approriate server-controlled reply offset.
2335 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2337 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2339 struct ptlrpc_sec_policy *policy;
2344 LASSERT(req->rq_svc_ctx);
2345 LASSERT(req->rq_svc_ctx->sc_policy);
2347 policy = req->rq_svc_ctx->sc_policy;
2348 LASSERT(policy->sp_sops->authorize);
2350 rc = policy->sp_sops->authorize(req);
2351 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2357 * Used by ptlrpc server, to free reply_state.
2359 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2361 struct ptlrpc_sec_policy *policy;
2362 unsigned int prealloc;
2366 LASSERT(rs->rs_svc_ctx);
2367 LASSERT(rs->rs_svc_ctx->sc_policy);
2369 policy = rs->rs_svc_ctx->sc_policy;
2370 LASSERT(policy->sp_sops->free_rs);
2372 prealloc = rs->rs_prealloc;
2373 policy->sp_sops->free_rs(rs);
2376 lustre_put_emerg_rs(rs);
2380 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2382 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2385 atomic_inc(&ctx->sc_refcount);
2388 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2390 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2395 LASSERT(atomic_read(&(ctx)->sc_refcount) > 0);
2396 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2397 if (ctx->sc_policy->sp_sops->free_ctx)
2398 ctx->sc_policy->sp_sops->free_ctx(ctx);
2400 req->rq_svc_ctx = NULL;
2403 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2405 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2410 LASSERT(atomic_read(&(ctx)->sc_refcount) > 0);
2411 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2412 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2414 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2421 * Perform transformation upon bulk data pointed by \a desc. This is called
2422 * before transforming the request message.
2424 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2425 struct ptlrpc_bulk_desc *desc)
2427 struct ptlrpc_cli_ctx *ctx;
2429 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2431 if (!req->rq_pack_bulk)
2434 ctx = req->rq_cli_ctx;
2435 if (ctx->cc_ops->wrap_bulk)
2436 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2439 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2442 * This is called after unwrap the reply message.
2443 * return nob of actual plain text size received, or error code.
2445 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2446 struct ptlrpc_bulk_desc *desc,
2449 struct ptlrpc_cli_ctx *ctx;
2452 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2454 if (!req->rq_pack_bulk)
2455 return desc->bd_nob_transferred;
2457 ctx = req->rq_cli_ctx;
2458 if (ctx->cc_ops->unwrap_bulk) {
2459 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2463 return desc->bd_nob_transferred;
2465 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2468 * This is called after unwrap the reply message.
2469 * return 0 for success or error code.
2471 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2472 struct ptlrpc_bulk_desc *desc)
2474 struct ptlrpc_cli_ctx *ctx;
2477 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2479 if (!req->rq_pack_bulk)
2482 ctx = req->rq_cli_ctx;
2483 if (ctx->cc_ops->unwrap_bulk) {
2484 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2490 * if everything is going right, nob should equals to nob_transferred.
2491 * in case of privacy mode, nob_transferred needs to be adjusted.
2493 if (desc->bd_nob != desc->bd_nob_transferred) {
2494 CERROR("nob %d doesn't match transferred nob %d\n",
2495 desc->bd_nob, desc->bd_nob_transferred);
2501 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2503 #ifdef HAVE_SERVER_SUPPORT
2505 * Performe transformation upon outgoing bulk read.
2507 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2508 struct ptlrpc_bulk_desc *desc)
2510 struct ptlrpc_svc_ctx *ctx;
2512 LASSERT(req->rq_bulk_read);
2514 if (!req->rq_pack_bulk)
2517 ctx = req->rq_svc_ctx;
2518 if (ctx->sc_policy->sp_sops->wrap_bulk)
2519 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2523 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2526 * Performe transformation upon incoming bulk write.
2528 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2529 struct ptlrpc_bulk_desc *desc)
2531 struct ptlrpc_svc_ctx *ctx;
2534 LASSERT(req->rq_bulk_write);
2537 * if it's in privacy mode, transferred should >= expected; otherwise
2538 * transferred should == expected.
2540 if (desc->bd_nob_transferred < desc->bd_nob ||
2541 (desc->bd_nob_transferred > desc->bd_nob &&
2542 SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2543 SPTLRPC_BULK_SVC_PRIV)) {
2544 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2545 desc->bd_nob_transferred, desc->bd_nob);
2549 if (!req->rq_pack_bulk)
2552 ctx = req->rq_svc_ctx;
2553 if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2554 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2556 CERROR("error unwrap bulk: %d\n", rc);
2559 /* return 0 to allow reply be sent */
2562 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2565 * Prepare buffers for incoming bulk write.
2567 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2568 struct ptlrpc_bulk_desc *desc)
2570 struct ptlrpc_svc_ctx *ctx;
2572 LASSERT(req->rq_bulk_write);
2574 if (!req->rq_pack_bulk)
2577 ctx = req->rq_svc_ctx;
2578 if (ctx->sc_policy->sp_sops->prep_bulk)
2579 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2583 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2585 #endif /* HAVE_SERVER_SUPPORT */
2588 * user descriptor helpers
2591 int sptlrpc_current_user_desc_size(void)
2595 ngroups = current_cred()->group_info->ngroups;
2597 if (ngroups > LUSTRE_MAX_GROUPS)
2598 ngroups = LUSTRE_MAX_GROUPS;
2599 return sptlrpc_user_desc_size(ngroups);
2601 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2603 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2605 struct ptlrpc_user_desc *pud;
2608 pud = lustre_msg_buf(msg, offset, 0);
2610 pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2611 pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2612 pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2613 pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2614 pud->pud_cap = ll_capability_u32(current_cap());
2615 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2618 ngroups = current_cred()->group_info->ngroups;
2619 if (pud->pud_ngroups > ngroups)
2620 pud->pud_ngroups = ngroups;
2621 #ifdef HAVE_GROUP_INFO_GID
2622 memcpy(pud->pud_groups, current_cred()->group_info->gid,
2623 pud->pud_ngroups * sizeof(__u32));
2624 #else /* !HAVE_GROUP_INFO_GID */
2625 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2626 pud->pud_ngroups * sizeof(__u32));
2627 #endif /* HAVE_GROUP_INFO_GID */
2628 task_unlock(current);
2632 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2634 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2636 struct ptlrpc_user_desc *pud;
2639 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2644 __swab32s(&pud->pud_uid);
2645 __swab32s(&pud->pud_gid);
2646 __swab32s(&pud->pud_fsuid);
2647 __swab32s(&pud->pud_fsgid);
2648 __swab32s(&pud->pud_cap);
2649 __swab32s(&pud->pud_ngroups);
2652 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2653 CERROR("%u groups is too large\n", pud->pud_ngroups);
2657 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2658 msg->lm_buflens[offset]) {
2659 CERROR("%u groups are claimed but bufsize only %u\n",
2660 pud->pud_ngroups, msg->lm_buflens[offset]);
2665 for (i = 0; i < pud->pud_ngroups; i++)
2666 __swab32s(&pud->pud_groups[i]);
2671 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2677 const char *sec2target_str(struct ptlrpc_sec *sec)
2679 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2681 if (sec_is_reverse(sec))
2683 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2685 EXPORT_SYMBOL(sec2target_str);
2688 * return true if the bulk data is protected
2690 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2692 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2693 case SPTLRPC_BULK_SVC_INTG:
2694 case SPTLRPC_BULK_SVC_PRIV:
2700 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2703 * crypto API helper/alloc blkciper
2707 * initialize/finalize
2710 int sptlrpc_init(void)
2714 rwlock_init(&policy_lock);
2716 rc = sptlrpc_gc_init();
2720 rc = sptlrpc_conf_init();
2724 rc = sptlrpc_enc_pool_init();
2728 rc = sptlrpc_null_init();
2732 rc = sptlrpc_plain_init();
2736 rc = sptlrpc_lproc_init();
2743 sptlrpc_plain_fini();
2745 sptlrpc_null_fini();
2747 sptlrpc_enc_pool_fini();
2749 sptlrpc_conf_fini();
2756 void sptlrpc_fini(void)
2758 sptlrpc_lproc_fini();
2759 sptlrpc_plain_fini();
2760 sptlrpc_null_fini();
2761 sptlrpc_enc_pool_fini();
2762 sptlrpc_conf_fini();