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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Eric Mei <ericm@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_SEC
39 #include <linux/user_namespace.h>
40 #include <linux/uidgid.h>
41 #include <linux/crypto.h>
42 #include <linux/key.h>
44 #include <libcfs/libcfs.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lustre_net.h>
49 #include <lustre_import.h>
50 #include <lustre_dlm.h>
51 #include <lustre_sec.h>
53 #include "ptlrpc_internal.h"
55 static int send_sepol;
56 module_param(send_sepol, int, 0644);
57 MODULE_PARM_DESC(send_sepol, "Client sends SELinux policy status");
63 static rwlock_t policy_lock;
64 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
68 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
70 __u16 number = policy->sp_policy;
72 LASSERT(policy->sp_name);
73 LASSERT(policy->sp_cops);
74 LASSERT(policy->sp_sops);
76 if (number >= SPTLRPC_POLICY_MAX)
79 write_lock(&policy_lock);
80 if (unlikely(policies[number])) {
81 write_unlock(&policy_lock);
84 policies[number] = policy;
85 write_unlock(&policy_lock);
87 CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
90 EXPORT_SYMBOL(sptlrpc_register_policy);
92 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
94 __u16 number = policy->sp_policy;
96 LASSERT(number < SPTLRPC_POLICY_MAX);
98 write_lock(&policy_lock);
99 if (unlikely(policies[number] == NULL)) {
100 write_unlock(&policy_lock);
101 CERROR("%s: already unregistered\n", policy->sp_name);
105 LASSERT(policies[number] == policy);
106 policies[number] = NULL;
107 write_unlock(&policy_lock);
109 CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
112 EXPORT_SYMBOL(sptlrpc_unregister_policy);
115 struct ptlrpc_sec_policy *sptlrpc_wireflavor2policy(__u32 flavor)
117 static DEFINE_MUTEX(load_mutex);
118 static atomic_t loaded = ATOMIC_INIT(0);
119 struct ptlrpc_sec_policy *policy;
120 __u16 number = SPTLRPC_FLVR_POLICY(flavor);
123 if (number >= SPTLRPC_POLICY_MAX)
127 read_lock(&policy_lock);
128 policy = policies[number];
129 if (policy && !try_module_get(policy->sp_owner))
132 flag = atomic_read(&loaded);
133 read_unlock(&policy_lock);
135 if (policy != NULL || flag != 0 ||
136 number != SPTLRPC_POLICY_GSS)
139 /* try to load gss module, once */
140 mutex_lock(&load_mutex);
141 if (atomic_read(&loaded) == 0) {
142 if (request_module("ptlrpc_gss") == 0)
144 "module ptlrpc_gss loaded on demand\n");
146 CERROR("Unable to load module ptlrpc_gss\n");
148 atomic_set(&loaded, 1);
150 mutex_unlock(&load_mutex);
156 __u32 sptlrpc_name2flavor_base(const char *name)
158 if (!strcmp(name, "null"))
159 return SPTLRPC_FLVR_NULL;
160 if (!strcmp(name, "plain"))
161 return SPTLRPC_FLVR_PLAIN;
162 if (!strcmp(name, "gssnull"))
163 return SPTLRPC_FLVR_GSSNULL;
164 if (!strcmp(name, "krb5n"))
165 return SPTLRPC_FLVR_KRB5N;
166 if (!strcmp(name, "krb5a"))
167 return SPTLRPC_FLVR_KRB5A;
168 if (!strcmp(name, "krb5i"))
169 return SPTLRPC_FLVR_KRB5I;
170 if (!strcmp(name, "krb5p"))
171 return SPTLRPC_FLVR_KRB5P;
172 if (!strcmp(name, "skn"))
173 return SPTLRPC_FLVR_SKN;
174 if (!strcmp(name, "ska"))
175 return SPTLRPC_FLVR_SKA;
176 if (!strcmp(name, "ski"))
177 return SPTLRPC_FLVR_SKI;
178 if (!strcmp(name, "skpi"))
179 return SPTLRPC_FLVR_SKPI;
181 return SPTLRPC_FLVR_INVALID;
183 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
185 const char *sptlrpc_flavor2name_base(__u32 flvr)
187 __u32 base = SPTLRPC_FLVR_BASE(flvr);
189 if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
191 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
193 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_GSSNULL))
195 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
197 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
199 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
201 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
203 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKN))
205 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKA))
207 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKI))
209 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKPI))
212 CERROR("invalid wire flavor 0x%x\n", flvr);
215 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
217 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
218 char *buf, int bufsize)
220 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
221 snprintf(buf, bufsize, "hash:%s",
222 sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
224 snprintf(buf, bufsize, "%s",
225 sptlrpc_flavor2name_base(sf->sf_rpc));
227 buf[bufsize - 1] = '\0';
230 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
232 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
234 snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
237 * currently we don't support customized bulk specification for
238 * flavors other than plain
240 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
244 sptlrpc_flavor2name_bulk(sf, &bspec[1], sizeof(bspec) - 1);
245 strncat(buf, bspec, bufsize);
248 buf[bufsize - 1] = '\0';
251 EXPORT_SYMBOL(sptlrpc_flavor2name);
253 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
257 if (flags & PTLRPC_SEC_FL_REVERSE)
258 strlcat(buf, "reverse,", bufsize);
259 if (flags & PTLRPC_SEC_FL_ROOTONLY)
260 strlcat(buf, "rootonly,", bufsize);
261 if (flags & PTLRPC_SEC_FL_UDESC)
262 strlcat(buf, "udesc,", bufsize);
263 if (flags & PTLRPC_SEC_FL_BULK)
264 strlcat(buf, "bulk,", bufsize);
266 strlcat(buf, "-,", bufsize);
270 EXPORT_SYMBOL(sptlrpc_secflags2str);
273 * client context APIs
277 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
279 struct vfs_cred vcred;
280 int create = 1, remove_dead = 1;
283 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
285 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
286 PTLRPC_SEC_FL_ROOTONLY)) {
289 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
294 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
295 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
298 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred, create,
302 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
304 atomic_inc(&ctx->cc_refcount);
307 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
309 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
311 struct ptlrpc_sec *sec = ctx->cc_sec;
314 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
316 if (!atomic_dec_and_test(&ctx->cc_refcount))
319 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
321 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
324 * Expire the client context immediately.
326 * \pre Caller must hold at least 1 reference on the \a ctx.
328 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
330 LASSERT(ctx->cc_ops->die);
331 ctx->cc_ops->die(ctx, 0);
333 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
336 * To wake up the threads who are waiting for this client context. Called
337 * after some status change happened on \a ctx.
339 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
341 struct ptlrpc_request *req, *next;
343 spin_lock(&ctx->cc_lock);
344 list_for_each_entry_safe(req, next, &ctx->cc_req_list,
346 list_del_init(&req->rq_ctx_chain);
347 ptlrpc_client_wake_req(req);
349 spin_unlock(&ctx->cc_lock);
351 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
353 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
355 LASSERT(ctx->cc_ops);
357 if (ctx->cc_ops->display == NULL)
360 return ctx->cc_ops->display(ctx, buf, bufsize);
363 static int import_sec_check_expire(struct obd_import *imp)
367 write_lock(&imp->imp_sec_lock);
368 if (imp->imp_sec_expire &&
369 imp->imp_sec_expire < ktime_get_real_seconds()) {
371 imp->imp_sec_expire = 0;
373 write_unlock(&imp->imp_sec_lock);
378 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
379 return sptlrpc_import_sec_adapt(imp, NULL, NULL);
383 * Get and validate the client side ptlrpc security facilities from
384 * \a imp. There is a race condition on client reconnect when the import is
385 * being destroyed while there are outstanding client bound requests. In
386 * this case do not output any error messages if import secuity is not
389 * \param[in] imp obd import associated with client
390 * \param[out] sec client side ptlrpc security
392 * \retval 0 if security retrieved successfully
393 * \retval -ve errno if there was a problem
395 static int import_sec_validate_get(struct obd_import *imp,
396 struct ptlrpc_sec **sec)
400 if (unlikely(imp->imp_sec_expire)) {
401 rc = import_sec_check_expire(imp);
406 *sec = sptlrpc_import_sec_ref(imp);
408 CERROR("import %p (%s) with no sec\n",
409 imp, ptlrpc_import_state_name(imp->imp_state));
413 if (unlikely((*sec)->ps_dying)) {
414 CERROR("attempt to use dying sec %p\n", sec);
415 sptlrpc_sec_put(*sec);
423 * Given a \a req, find or allocate an appropriate context for it.
424 * \pre req->rq_cli_ctx == NULL.
426 * \retval 0 succeed, and req->rq_cli_ctx is set.
427 * \retval -ev error number, and req->rq_cli_ctx == NULL.
429 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
431 struct obd_import *imp = req->rq_import;
432 struct ptlrpc_sec *sec;
437 LASSERT(!req->rq_cli_ctx);
440 rc = import_sec_validate_get(imp, &sec);
444 req->rq_cli_ctx = get_my_ctx(sec);
446 sptlrpc_sec_put(sec);
448 if (!req->rq_cli_ctx) {
449 CERROR("req %p: fail to get context\n", req);
450 RETURN(-ECONNREFUSED);
457 * Drop the context for \a req.
458 * \pre req->rq_cli_ctx != NULL.
459 * \post req->rq_cli_ctx == NULL.
461 * If \a sync == 0, this function should return quickly without sleep;
462 * otherwise it might trigger and wait for the whole process of sending
463 * an context-destroying rpc to server.
465 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
470 LASSERT(req->rq_cli_ctx);
473 * request might be asked to release earlier while still
474 * in the context waiting list.
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);
482 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
483 req->rq_cli_ctx = NULL;
488 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
489 struct ptlrpc_cli_ctx *oldctx,
490 struct ptlrpc_cli_ctx *newctx)
492 struct sptlrpc_flavor old_flvr;
493 char *reqmsg = NULL; /* to workaround old gcc */
497 LASSERT(req->rq_reqmsg);
498 LASSERT(req->rq_reqlen);
499 LASSERT(req->rq_replen);
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 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
518 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
521 /* release old req/rep buf */
522 req->rq_cli_ctx = oldctx;
523 sptlrpc_cli_free_reqbuf(req);
524 sptlrpc_cli_free_repbuf(req);
525 req->rq_cli_ctx = newctx;
527 /* recalculate the flavor */
528 sptlrpc_req_set_flavor(req, 0);
531 * alloc new request buffer
532 * we don't need to alloc reply buffer here, leave it to the
533 * rest procedure of ptlrpc
535 if (reqmsg_size != 0) {
536 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
538 LASSERT(req->rq_reqmsg);
539 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
541 CWARN("failed to alloc reqbuf: %d\n", rc);
542 req->rq_flvr = old_flvr;
545 OBD_FREE_LARGE(reqmsg, reqmsg_size);
551 * If current context of \a req is dead somehow, e.g. we just switched flavor
552 * thus marked original contexts dead, we'll find a new context for it. if
553 * no switch is needed, \a req will end up with the same context.
555 * \note a request must have a context, to keep other parts of code happy.
556 * In any case of failure during the switching, we must restore the old one.
558 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
560 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
561 struct ptlrpc_cli_ctx *newctx;
568 sptlrpc_cli_ctx_get(oldctx);
569 sptlrpc_req_put_ctx(req, 0);
571 rc = sptlrpc_req_get_ctx(req);
573 LASSERT(!req->rq_cli_ctx);
575 /* restore old ctx */
576 req->rq_cli_ctx = oldctx;
580 newctx = req->rq_cli_ctx;
583 if (unlikely(newctx == oldctx &&
584 test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
586 * still get the old dead ctx, usually means system too busy
589 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
590 newctx, newctx->cc_flags);
592 schedule_timeout_interruptible(cfs_time_seconds(1));
593 } else if (unlikely(test_bit(PTLRPC_CTX_UPTODATE_BIT, &newctx->cc_flags)
596 * new ctx not up to date yet
599 "ctx (%p, fl %lx) doesn't switch, not up to date yet\n",
600 newctx, newctx->cc_flags);
603 * it's possible newctx == oldctx if we're switching
604 * subflavor with the same sec.
606 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
608 /* restore old ctx */
609 sptlrpc_req_put_ctx(req, 0);
610 req->rq_cli_ctx = oldctx;
614 LASSERT(req->rq_cli_ctx == newctx);
617 sptlrpc_cli_ctx_put(oldctx, 1);
620 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
623 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
625 if (cli_ctx_is_refreshed(ctx))
631 int ctx_refresh_timeout(struct ptlrpc_request *req)
635 /* conn_cnt is needed in expire_one_request */
636 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
638 rc = ptlrpc_expire_one_request(req, 1);
640 * if we started recovery, we should mark this ctx dead; otherwise
641 * in case of lgssd died nobody would retire this ctx, following
642 * connecting will still find the same ctx thus cause deadlock.
643 * there's an assumption that expire time of the request should be
644 * later than the context refresh expire time.
647 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
652 void ctx_refresh_interrupt(struct ptlrpc_request *req)
655 spin_lock(&req->rq_lock);
657 spin_unlock(&req->rq_lock);
661 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
663 spin_lock(&ctx->cc_lock);
664 if (!list_empty(&req->rq_ctx_chain))
665 list_del_init(&req->rq_ctx_chain);
666 spin_unlock(&ctx->cc_lock);
670 * To refresh the context of \req, if it's not up-to-date.
673 * - = 0: wait until success or fatal error occur
674 * - > 0: timeout value (in seconds)
676 * The status of the context could be subject to be changed by other threads
677 * at any time. We allow this race, but once we return with 0, the caller will
678 * suppose it's uptodated and keep using it until the owning rpc is done.
680 * \retval 0 only if the context is uptodated.
681 * \retval -ev error number.
683 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
685 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
686 struct ptlrpc_sec *sec;
693 if (req->rq_ctx_init || req->rq_ctx_fini)
697 * during the process a request's context might change type even
698 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
702 rc = import_sec_validate_get(req->rq_import, &sec);
706 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
707 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
708 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
709 req_off_ctx_list(req, ctx);
710 sptlrpc_req_replace_dead_ctx(req);
711 ctx = req->rq_cli_ctx;
713 sptlrpc_sec_put(sec);
715 if (cli_ctx_is_eternal(ctx))
718 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
719 LASSERT(ctx->cc_ops->refresh);
720 ctx->cc_ops->refresh(ctx);
722 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
724 LASSERT(ctx->cc_ops->validate);
725 if (ctx->cc_ops->validate(ctx) == 0) {
726 req_off_ctx_list(req, ctx);
730 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
731 spin_lock(&req->rq_lock);
733 spin_unlock(&req->rq_lock);
734 req_off_ctx_list(req, ctx);
739 * There's a subtle issue for resending RPCs, suppose following
741 * 1. the request was sent to server.
742 * 2. recovery was kicked start, after finished the request was
744 * 3. resend the request.
745 * 4. old reply from server received, we accept and verify the reply.
746 * this has to be success, otherwise the error will be aware
748 * 5. new reply from server received, dropped by LNet.
750 * Note the xid of old & new request is the same. We can't simply
751 * change xid for the resent request because the server replies on
752 * it for reply reconstruction.
754 * Commonly the original context should be uptodate because we
755 * have an expiry nice time; server will keep its context because
756 * we at least hold a ref of old context which prevent context
757 * from destroying RPC being sent. So server still can accept the
758 * request and finish the RPC. But if that's not the case:
759 * 1. If server side context has been trimmed, a NO_CONTEXT will
760 * be returned, gss_cli_ctx_verify/unseal will switch to new
762 * 2. Current context never be refreshed, then we are fine: we
763 * never really send request with old context before.
765 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
766 unlikely(req->rq_reqmsg) &&
767 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
768 req_off_ctx_list(req, ctx);
772 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
773 req_off_ctx_list(req, ctx);
775 * don't switch ctx if import was deactivated
777 if (req->rq_import->imp_deactive) {
778 spin_lock(&req->rq_lock);
780 spin_unlock(&req->rq_lock);
784 rc = sptlrpc_req_replace_dead_ctx(req);
786 LASSERT(ctx == req->rq_cli_ctx);
787 CERROR("req %p: failed to replace dead ctx %p: %d\n",
789 spin_lock(&req->rq_lock);
791 spin_unlock(&req->rq_lock);
795 ctx = req->rq_cli_ctx;
800 * Now we're sure this context is during upcall, add myself into
803 spin_lock(&ctx->cc_lock);
804 if (list_empty(&req->rq_ctx_chain))
805 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
806 spin_unlock(&ctx->cc_lock);
809 RETURN(-EWOULDBLOCK);
811 /* Clear any flags that may be present from previous sends */
812 LASSERT(req->rq_receiving_reply == 0);
813 spin_lock(&req->rq_lock);
815 req->rq_timedout = 0;
818 spin_unlock(&req->rq_lock);
820 if (wait_event_idle_timeout(req->rq_reply_waitq,
821 ctx_check_refresh(ctx),
822 cfs_time_seconds(timeout)) == 0) {
824 if (!ctx_refresh_timeout(req) &&
825 l_wait_event_abortable(req->rq_reply_waitq,
826 ctx_check_refresh(ctx))
829 ctx_refresh_interrupt(req);
834 * following cases could lead us here:
835 * - successfully refreshed;
837 * - timedout, and we don't want recover from the failure;
838 * - timedout, and waked up upon recovery finished;
839 * - someone else mark this ctx dead by force;
840 * - someone invalidate the req and call ptlrpc_client_wake_req(),
841 * e.g. ptlrpc_abort_inflight();
843 if (!cli_ctx_is_refreshed(ctx)) {
844 /* timed out or interruptted */
845 req_off_ctx_list(req, ctx);
855 * Initialize flavor settings for \a req, according to \a opcode.
857 * \note this could be called in two situations:
858 * - new request from ptlrpc_pre_req(), with proper @opcode
859 * - old request which changed ctx in the middle, with @opcode == 0
861 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
863 struct ptlrpc_sec *sec;
865 LASSERT(req->rq_import);
866 LASSERT(req->rq_cli_ctx);
867 LASSERT(req->rq_cli_ctx->cc_sec);
868 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
870 /* special security flags according to opcode */
874 case MGS_CONFIG_READ:
876 req->rq_bulk_read = 1;
880 req->rq_bulk_write = 1;
883 req->rq_ctx_init = 1;
886 req->rq_ctx_fini = 1;
889 /* init/fini rpc won't be resend, so can't be here */
890 LASSERT(req->rq_ctx_init == 0);
891 LASSERT(req->rq_ctx_fini == 0);
893 /* cleanup flags, which should be recalculated */
894 req->rq_pack_udesc = 0;
895 req->rq_pack_bulk = 0;
899 sec = req->rq_cli_ctx->cc_sec;
901 spin_lock(&sec->ps_lock);
902 req->rq_flvr = sec->ps_flvr;
903 spin_unlock(&sec->ps_lock);
906 * force SVC_NULL for context initiation rpc, SVC_INTG for context
909 if (unlikely(req->rq_ctx_init))
910 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
911 else if (unlikely(req->rq_ctx_fini))
912 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
914 /* user descriptor flag, null security can't do it anyway */
915 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
916 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
917 req->rq_pack_udesc = 1;
919 /* bulk security flag */
920 if ((req->rq_bulk_read || req->rq_bulk_write) &&
921 sptlrpc_flavor_has_bulk(&req->rq_flvr))
922 req->rq_pack_bulk = 1;
925 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
927 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
930 LASSERT(req->rq_clrbuf);
931 if (req->rq_pool || !req->rq_reqbuf)
934 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
935 req->rq_reqbuf = NULL;
936 req->rq_reqbuf_len = 0;
940 * Given an import \a imp, check whether current user has a valid context
941 * or not. We may create a new context and try to refresh it, and try
942 * repeatedly try in case of non-fatal errors. Return 0 means success.
944 int sptlrpc_import_check_ctx(struct obd_import *imp)
946 struct ptlrpc_sec *sec;
947 struct ptlrpc_cli_ctx *ctx;
948 struct ptlrpc_request *req = NULL;
955 sec = sptlrpc_import_sec_ref(imp);
956 ctx = get_my_ctx(sec);
957 sptlrpc_sec_put(sec);
962 if (cli_ctx_is_eternal(ctx) ||
963 ctx->cc_ops->validate(ctx) == 0) {
964 sptlrpc_cli_ctx_put(ctx, 1);
968 if (cli_ctx_is_error(ctx)) {
969 sptlrpc_cli_ctx_put(ctx, 1);
973 req = ptlrpc_request_cache_alloc(GFP_NOFS);
977 ptlrpc_cli_req_init(req);
978 atomic_set(&req->rq_refcount, 10000);
980 req->rq_import = imp;
981 req->rq_flvr = sec->ps_flvr;
982 req->rq_cli_ctx = ctx;
984 rc = sptlrpc_req_refresh_ctx(req, 0);
985 LASSERT(list_empty(&req->rq_ctx_chain));
986 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
987 ptlrpc_request_cache_free(req);
993 * Used by ptlrpc client, to perform the pre-defined security transformation
994 * upon the request message of \a req. After this function called,
995 * req->rq_reqmsg is still accessible as clear text.
997 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
999 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1005 LASSERT(ctx->cc_sec);
1006 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1009 * we wrap bulk request here because now we can be sure
1010 * the context is uptodate.
1013 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
1018 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1019 case SPTLRPC_SVC_NULL:
1020 case SPTLRPC_SVC_AUTH:
1021 case SPTLRPC_SVC_INTG:
1022 LASSERT(ctx->cc_ops->sign);
1023 rc = ctx->cc_ops->sign(ctx, req);
1025 case SPTLRPC_SVC_PRIV:
1026 LASSERT(ctx->cc_ops->seal);
1027 rc = ctx->cc_ops->seal(ctx, req);
1034 LASSERT(req->rq_reqdata_len);
1035 LASSERT(req->rq_reqdata_len % 8 == 0);
1036 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
1042 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
1044 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1050 LASSERT(ctx->cc_sec);
1051 LASSERT(req->rq_repbuf);
1052 LASSERT(req->rq_repdata);
1053 LASSERT(req->rq_repmsg == NULL);
1055 req->rq_rep_swab_mask = 0;
1057 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
1060 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1064 CERROR("failed unpack reply: x%llu\n", req->rq_xid);
1068 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
1069 CERROR("replied data length %d too small\n",
1070 req->rq_repdata_len);
1074 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1075 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1076 CERROR("reply policy %u doesn't match request policy %u\n",
1077 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1078 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1082 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1083 case SPTLRPC_SVC_NULL:
1084 case SPTLRPC_SVC_AUTH:
1085 case SPTLRPC_SVC_INTG:
1086 LASSERT(ctx->cc_ops->verify);
1087 rc = ctx->cc_ops->verify(ctx, req);
1089 case SPTLRPC_SVC_PRIV:
1090 LASSERT(ctx->cc_ops->unseal);
1091 rc = ctx->cc_ops->unseal(ctx, req);
1096 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1098 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1100 req->rq_rep_swab_mask = 0;
1105 * Used by ptlrpc client, to perform security transformation upon the reply
1106 * message of \a req. After return successfully, req->rq_repmsg points to
1107 * the reply message in clear text.
1109 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1112 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1114 LASSERT(req->rq_repbuf);
1115 LASSERT(req->rq_repdata == NULL);
1116 LASSERT(req->rq_repmsg == NULL);
1117 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1119 if (req->rq_reply_off == 0 &&
1120 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1121 CERROR("real reply with offset 0\n");
1125 if (req->rq_reply_off % 8 != 0) {
1126 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1130 req->rq_repdata = (struct lustre_msg *)
1131 (req->rq_repbuf + req->rq_reply_off);
1132 req->rq_repdata_len = req->rq_nob_received;
1134 return do_cli_unwrap_reply(req);
1138 * Used by ptlrpc client, to perform security transformation upon the early
1139 * reply message of \a req. We expect the rq_reply_off is 0, and
1140 * rq_nob_received is the early reply size.
1142 * Because the receive buffer might be still posted, the reply data might be
1143 * changed at any time, no matter we're holding rq_lock or not. For this reason
1144 * we allocate a separate ptlrpc_request and reply buffer for early reply
1147 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1148 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1149 * \a *req_ret to release it.
1150 * \retval -ev error number, and \a req_ret will not be set.
1152 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1153 struct ptlrpc_request **req_ret)
1155 struct ptlrpc_request *early_req;
1157 int early_bufsz, early_size;
1162 early_req = ptlrpc_request_cache_alloc(GFP_NOFS);
1163 if (early_req == NULL)
1166 ptlrpc_cli_req_init(early_req);
1168 early_size = req->rq_nob_received;
1169 early_bufsz = size_roundup_power2(early_size);
1170 OBD_ALLOC_LARGE(early_buf, early_bufsz);
1171 if (early_buf == NULL)
1172 GOTO(err_req, rc = -ENOMEM);
1174 /* sanity checkings and copy data out, do it inside spinlock */
1175 spin_lock(&req->rq_lock);
1177 if (req->rq_replied) {
1178 spin_unlock(&req->rq_lock);
1179 GOTO(err_buf, rc = -EALREADY);
1182 LASSERT(req->rq_repbuf);
1183 LASSERT(req->rq_repdata == NULL);
1184 LASSERT(req->rq_repmsg == NULL);
1186 if (req->rq_reply_off != 0) {
1187 CERROR("early reply with offset %u\n", req->rq_reply_off);
1188 spin_unlock(&req->rq_lock);
1189 GOTO(err_buf, rc = -EPROTO);
1192 if (req->rq_nob_received != early_size) {
1193 /* even another early arrived the size should be the same */
1194 CERROR("data size has changed from %u to %u\n",
1195 early_size, req->rq_nob_received);
1196 spin_unlock(&req->rq_lock);
1197 GOTO(err_buf, rc = -EINVAL);
1200 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1201 CERROR("early reply length %d too small\n",
1202 req->rq_nob_received);
1203 spin_unlock(&req->rq_lock);
1204 GOTO(err_buf, rc = -EALREADY);
1207 memcpy(early_buf, req->rq_repbuf, early_size);
1208 spin_unlock(&req->rq_lock);
1210 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1211 early_req->rq_flvr = req->rq_flvr;
1212 early_req->rq_repbuf = early_buf;
1213 early_req->rq_repbuf_len = early_bufsz;
1214 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1215 early_req->rq_repdata_len = early_size;
1216 early_req->rq_early = 1;
1217 early_req->rq_reqmsg = req->rq_reqmsg;
1219 rc = do_cli_unwrap_reply(early_req);
1221 DEBUG_REQ(D_ADAPTTO, early_req,
1222 "unwrap early reply: rc = %d", rc);
1226 LASSERT(early_req->rq_repmsg);
1227 *req_ret = early_req;
1231 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1233 OBD_FREE_LARGE(early_buf, early_bufsz);
1235 ptlrpc_request_cache_free(early_req);
1240 * Used by ptlrpc client, to release a processed early reply \a early_req.
1242 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1244 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1246 LASSERT(early_req->rq_repbuf);
1247 LASSERT(early_req->rq_repdata);
1248 LASSERT(early_req->rq_repmsg);
1250 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1251 OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1252 ptlrpc_request_cache_free(early_req);
1255 /**************************************************
1257 **************************************************/
1260 * "fixed" sec (e.g. null) use sec_id < 0
1262 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1264 int sptlrpc_get_next_secid(void)
1266 return atomic_inc_return(&sptlrpc_sec_id);
1268 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1271 * client side high-level security APIs
1274 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1275 int grace, int force)
1277 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1279 LASSERT(policy->sp_cops);
1280 LASSERT(policy->sp_cops->flush_ctx_cache);
1282 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1285 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1287 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1289 LASSERT_ATOMIC_ZERO(&sec->ps_refcount);
1290 LASSERT_ATOMIC_ZERO(&sec->ps_nctx);
1291 LASSERT(policy->sp_cops->destroy_sec);
1293 CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1295 policy->sp_cops->destroy_sec(sec);
1296 sptlrpc_policy_put(policy);
1299 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1301 sec_cop_destroy_sec(sec);
1303 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1305 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1307 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1309 if (sec->ps_policy->sp_cops->kill_sec) {
1310 sec->ps_policy->sp_cops->kill_sec(sec);
1312 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1316 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1319 atomic_inc(&sec->ps_refcount);
1323 EXPORT_SYMBOL(sptlrpc_sec_get);
1325 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1328 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1330 if (atomic_dec_and_test(&sec->ps_refcount)) {
1331 sptlrpc_gc_del_sec(sec);
1332 sec_cop_destroy_sec(sec);
1336 EXPORT_SYMBOL(sptlrpc_sec_put);
1339 * policy module is responsible for taking refrence of import
1342 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1343 struct ptlrpc_svc_ctx *svc_ctx,
1344 struct sptlrpc_flavor *sf,
1345 enum lustre_sec_part sp)
1347 struct ptlrpc_sec_policy *policy;
1348 struct ptlrpc_sec *sec;
1354 LASSERT(imp->imp_dlm_fake == 1);
1356 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1357 imp->imp_obd->obd_type->typ_name,
1358 imp->imp_obd->obd_name,
1359 sptlrpc_flavor2name(sf, str, sizeof(str)));
1361 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1362 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1364 LASSERT(imp->imp_dlm_fake == 0);
1366 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1367 imp->imp_obd->obd_type->typ_name,
1368 imp->imp_obd->obd_name,
1369 sptlrpc_flavor2name(sf, str, sizeof(str)));
1371 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1373 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1378 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1380 atomic_inc(&sec->ps_refcount);
1384 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1385 sptlrpc_gc_add_sec(sec);
1387 sptlrpc_policy_put(policy);
1393 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1395 struct ptlrpc_sec *sec;
1397 read_lock(&imp->imp_sec_lock);
1398 sec = sptlrpc_sec_get(imp->imp_sec);
1399 read_unlock(&imp->imp_sec_lock);
1403 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1405 static void sptlrpc_import_sec_install(struct obd_import *imp,
1406 struct ptlrpc_sec *sec)
1408 struct ptlrpc_sec *old_sec;
1410 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1412 write_lock(&imp->imp_sec_lock);
1413 old_sec = imp->imp_sec;
1415 write_unlock(&imp->imp_sec_lock);
1418 sptlrpc_sec_kill(old_sec);
1420 /* balance the ref taken by this import */
1421 sptlrpc_sec_put(old_sec);
1426 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1428 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1432 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1438 * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1439 * configuration. Upon called, imp->imp_sec may or may not be NULL.
1441 * - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1442 * - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1444 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1445 struct ptlrpc_svc_ctx *svc_ctx,
1446 struct sptlrpc_flavor *flvr)
1448 struct ptlrpc_connection *conn;
1449 struct sptlrpc_flavor sf;
1450 struct ptlrpc_sec *sec, *newsec;
1451 enum lustre_sec_part sp;
1462 conn = imp->imp_connection;
1464 if (svc_ctx == NULL) {
1465 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1467 * normal import, determine flavor from rule set, except
1468 * for mgc the flavor is predetermined.
1470 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1471 sf = cliobd->cl_flvr_mgc;
1473 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1475 &cliobd->cl_target_uuid,
1478 sp = imp->imp_obd->u.cli.cl_sp_me;
1480 /* reverse import, determine flavor from incoming reqeust */
1483 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1484 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1485 PTLRPC_SEC_FL_ROOTONLY;
1487 sp = sptlrpc_target_sec_part(imp->imp_obd);
1490 sec = sptlrpc_import_sec_ref(imp);
1494 if (flavor_equal(&sf, &sec->ps_flvr))
1497 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1498 imp->imp_obd->obd_name,
1499 obd_uuid2str(&conn->c_remote_uuid),
1500 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1501 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1502 } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1503 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1504 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1505 imp->imp_obd->obd_name,
1506 obd_uuid2str(&conn->c_remote_uuid),
1507 LNET_NIDNET(conn->c_self),
1508 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1511 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1513 sptlrpc_import_sec_install(imp, newsec);
1515 CERROR("import %s->%s: failed to create new sec\n",
1516 imp->imp_obd->obd_name,
1517 obd_uuid2str(&conn->c_remote_uuid));
1522 sptlrpc_sec_put(sec);
1526 void sptlrpc_import_sec_put(struct obd_import *imp)
1529 sptlrpc_sec_kill(imp->imp_sec);
1531 sptlrpc_sec_put(imp->imp_sec);
1532 imp->imp_sec = NULL;
1536 static void import_flush_ctx_common(struct obd_import *imp,
1537 uid_t uid, int grace, int force)
1539 struct ptlrpc_sec *sec;
1544 sec = sptlrpc_import_sec_ref(imp);
1548 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1549 sptlrpc_sec_put(sec);
1552 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1555 * it's important to use grace mode, see explain in
1556 * sptlrpc_req_refresh_ctx()
1558 import_flush_ctx_common(imp, 0, 1, 1);
1561 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1563 import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1566 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1568 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1570 import_flush_ctx_common(imp, -1, 1, 1);
1572 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1575 * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1576 * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1578 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1580 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1581 struct ptlrpc_sec_policy *policy;
1585 LASSERT(ctx->cc_sec);
1586 LASSERT(ctx->cc_sec->ps_policy);
1587 LASSERT(req->rq_reqmsg == NULL);
1588 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1590 policy = ctx->cc_sec->ps_policy;
1591 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1593 LASSERT(req->rq_reqmsg);
1594 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1596 /* zeroing preallocated buffer */
1598 memset(req->rq_reqmsg, 0, msgsize);
1605 * Used by ptlrpc client to free request buffer of \a req. After this
1606 * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1608 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1610 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1611 struct ptlrpc_sec_policy *policy;
1614 LASSERT(ctx->cc_sec);
1615 LASSERT(ctx->cc_sec->ps_policy);
1616 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1618 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1621 policy = ctx->cc_sec->ps_policy;
1622 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1623 req->rq_reqmsg = NULL;
1627 * NOTE caller must guarantee the buffer size is enough for the enlargement
1629 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1630 int segment, int newsize)
1633 int oldsize, oldmsg_size, movesize;
1635 LASSERT(segment < msg->lm_bufcount);
1636 LASSERT(msg->lm_buflens[segment] <= newsize);
1638 if (msg->lm_buflens[segment] == newsize)
1641 /* nothing to do if we are enlarging the last segment */
1642 if (segment == msg->lm_bufcount - 1) {
1643 msg->lm_buflens[segment] = newsize;
1647 oldsize = msg->lm_buflens[segment];
1649 src = lustre_msg_buf(msg, segment + 1, 0);
1650 msg->lm_buflens[segment] = newsize;
1651 dst = lustre_msg_buf(msg, segment + 1, 0);
1652 msg->lm_buflens[segment] = oldsize;
1654 /* move from segment + 1 to end segment */
1655 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1656 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1657 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1658 LASSERT(movesize >= 0);
1661 memmove(dst, src, movesize);
1663 /* note we don't clear the ares where old data live, not secret */
1665 /* finally set new segment size */
1666 msg->lm_buflens[segment] = newsize;
1668 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1671 * Used by ptlrpc client to enlarge the \a segment of request message pointed
1672 * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1673 * preserved after the enlargement. this must be called after original request
1674 * buffer being allocated.
1676 * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1677 * so caller should refresh its local pointers if needed.
1679 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1680 const struct req_msg_field *field,
1683 struct req_capsule *pill = &req->rq_pill;
1684 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1685 struct ptlrpc_sec_cops *cops;
1686 struct lustre_msg *msg = req->rq_reqmsg;
1687 int segment = __req_capsule_offset(pill, field, RCL_CLIENT);
1691 LASSERT(msg->lm_bufcount > segment);
1692 LASSERT(msg->lm_buflens[segment] <= newsize);
1694 if (msg->lm_buflens[segment] == newsize)
1697 cops = ctx->cc_sec->ps_policy->sp_cops;
1698 LASSERT(cops->enlarge_reqbuf);
1699 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1701 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1704 * Used by ptlrpc client to allocate reply buffer of \a req.
1706 * \note After this, req->rq_repmsg is still not accessible.
1708 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1710 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1711 struct ptlrpc_sec_policy *policy;
1716 LASSERT(ctx->cc_sec);
1717 LASSERT(ctx->cc_sec->ps_policy);
1722 policy = ctx->cc_sec->ps_policy;
1723 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1727 * Used by ptlrpc client to free reply buffer of \a req. After this
1728 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1730 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1732 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1733 struct ptlrpc_sec_policy *policy;
1738 LASSERT(ctx->cc_sec);
1739 LASSERT(ctx->cc_sec->ps_policy);
1740 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1742 if (req->rq_repbuf == NULL)
1744 LASSERT(req->rq_repbuf_len);
1746 policy = ctx->cc_sec->ps_policy;
1747 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1748 req->rq_repmsg = NULL;
1751 EXPORT_SYMBOL(sptlrpc_cli_free_repbuf);
1753 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1754 struct ptlrpc_cli_ctx *ctx)
1756 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1758 if (!policy->sp_cops->install_rctx)
1760 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1763 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1764 struct ptlrpc_svc_ctx *ctx)
1766 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1768 if (!policy->sp_sops->install_rctx)
1770 return policy->sp_sops->install_rctx(imp, ctx);
1773 /* Get SELinux policy info from userspace */
1774 static int sepol_helper(struct obd_import *imp)
1776 char mtime_str[21] = { 0 }, mode_str[2] = { 0 };
1778 [0] = "/usr/sbin/l_getsepol",
1780 [2] = NULL, /* obd type */
1782 [4] = NULL, /* obd name */
1784 [6] = mtime_str, /* policy mtime */
1786 [8] = mode_str, /* enforcing mode */
1791 [1] = "PATH=/sbin:/usr/sbin",
1797 if (imp == NULL || imp->imp_obd == NULL ||
1798 imp->imp_obd->obd_type == NULL) {
1801 argv[2] = (char *)imp->imp_obd->obd_type->typ_name;
1802 argv[4] = imp->imp_obd->obd_name;
1803 spin_lock(&imp->imp_sec->ps_lock);
1804 if (imp->imp_sec->ps_sepol_mtime == 0 &&
1805 imp->imp_sec->ps_sepol[0] == '\0') {
1806 /* ps_sepol has not been initialized */
1810 snprintf(mtime_str, sizeof(mtime_str), "%lu",
1811 imp->imp_sec->ps_sepol_mtime);
1812 mode_str[0] = imp->imp_sec->ps_sepol[0];
1814 spin_unlock(&imp->imp_sec->ps_lock);
1815 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1822 static inline int sptlrpc_sepol_needs_check(struct ptlrpc_sec *imp_sec)
1826 if (send_sepol == 0 || !selinux_is_enabled())
1829 if (send_sepol == -1)
1830 /* send_sepol == -1 means fetch sepol status every time */
1833 spin_lock(&imp_sec->ps_lock);
1834 checknext = imp_sec->ps_sepol_checknext;
1835 spin_unlock(&imp_sec->ps_lock);
1837 /* next check is too far in time, please update */
1838 if (ktime_after(checknext,
1839 ktime_add(ktime_get(), ktime_set(send_sepol, 0))))
1842 if (ktime_before(ktime_get(), checknext))
1843 /* too early to fetch sepol status */
1847 /* define new sepol_checknext time */
1848 spin_lock(&imp_sec->ps_lock);
1849 imp_sec->ps_sepol_checknext = ktime_add(ktime_get(),
1850 ktime_set(send_sepol, 0));
1851 spin_unlock(&imp_sec->ps_lock);
1856 int sptlrpc_get_sepol(struct ptlrpc_request *req)
1858 struct ptlrpc_sec *imp_sec = req->rq_import->imp_sec;
1863 (req->rq_sepol)[0] = '\0';
1865 #ifndef HAVE_SELINUX
1866 if (unlikely(send_sepol != 0))
1868 "Client cannot report SELinux status, it was not built against libselinux.\n");
1872 if (send_sepol == 0 || !selinux_is_enabled())
1875 if (imp_sec == NULL)
1878 /* Retrieve SELinux status info */
1879 if (sptlrpc_sepol_needs_check(imp_sec))
1880 rc = sepol_helper(req->rq_import);
1881 if (likely(rc == 0)) {
1882 spin_lock(&imp_sec->ps_lock);
1883 memcpy(req->rq_sepol, imp_sec->ps_sepol,
1884 sizeof(req->rq_sepol));
1885 spin_unlock(&imp_sec->ps_lock);
1890 EXPORT_SYMBOL(sptlrpc_get_sepol);
1893 * server side security
1896 static int flavor_allowed(struct sptlrpc_flavor *exp,
1897 struct ptlrpc_request *req)
1899 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1901 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1904 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1905 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1906 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1907 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1913 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1916 * Given an export \a exp, check whether the flavor of incoming \a req
1917 * is allowed by the export \a exp. Main logic is about taking care of
1918 * changing configurations. Return 0 means success.
1920 int sptlrpc_target_export_check(struct obd_export *exp,
1921 struct ptlrpc_request *req)
1923 struct sptlrpc_flavor flavor;
1929 * client side export has no imp_reverse, skip
1930 * FIXME maybe we should check flavor this as well???
1932 if (exp->exp_imp_reverse == NULL)
1935 /* don't care about ctx fini rpc */
1936 if (req->rq_ctx_fini)
1939 spin_lock(&exp->exp_lock);
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.
1948 if (unlikely(exp->exp_flvr_changed) &&
1949 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1951 * make the new flavor as "current", and old ones as
1954 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1955 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1956 flavor = exp->exp_flvr_old[1];
1957 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1958 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1959 exp->exp_flvr_old[0] = exp->exp_flvr;
1960 exp->exp_flvr_expire[0] = ktime_get_real_seconds() +
1961 EXP_FLVR_UPDATE_EXPIRE;
1962 exp->exp_flvr = flavor;
1964 /* flavor change finished */
1965 exp->exp_flvr_changed = 0;
1966 LASSERT(exp->exp_flvr_adapt == 1);
1968 /* if it's gss, we only interested in root ctx init */
1969 if (req->rq_auth_gss &&
1970 !(req->rq_ctx_init &&
1971 (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1972 req->rq_auth_usr_ost))) {
1973 spin_unlock(&exp->exp_lock);
1974 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1975 req->rq_auth_gss, req->rq_ctx_init,
1976 req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1977 req->rq_auth_usr_ost);
1981 exp->exp_flvr_adapt = 0;
1982 spin_unlock(&exp->exp_lock);
1984 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1985 req->rq_svc_ctx, &flavor);
1989 * if it equals to the current flavor, we accept it, but need to
1990 * dealing with reverse sec/ctx
1992 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1994 * most cases should return here, we only interested in
1997 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1998 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1999 !req->rq_auth_usr_ost)) {
2000 spin_unlock(&exp->exp_lock);
2005 * if flavor just changed, we should not proceed, just leave
2006 * it and current flavor will be discovered and replaced
2007 * shortly, and let _this_ rpc pass through
2009 if (exp->exp_flvr_changed) {
2010 LASSERT(exp->exp_flvr_adapt);
2011 spin_unlock(&exp->exp_lock);
2015 if (exp->exp_flvr_adapt) {
2016 exp->exp_flvr_adapt = 0;
2017 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
2018 exp, exp->exp_flvr.sf_rpc,
2019 exp->exp_flvr_old[0].sf_rpc,
2020 exp->exp_flvr_old[1].sf_rpc);
2021 flavor = exp->exp_flvr;
2022 spin_unlock(&exp->exp_lock);
2024 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
2029 "exp %p (%x|%x|%x): is current flavor, install rvs ctx\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 spin_unlock(&exp->exp_lock);
2035 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
2040 if (exp->exp_flvr_expire[0]) {
2041 if (exp->exp_flvr_expire[0] >= ktime_get_real_seconds()) {
2042 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
2044 "exp %p (%x|%x|%x): match the middle one (%lld)\n",
2045 exp, exp->exp_flvr.sf_rpc,
2046 exp->exp_flvr_old[0].sf_rpc,
2047 exp->exp_flvr_old[1].sf_rpc,
2048 (s64)(exp->exp_flvr_expire[0] -
2049 ktime_get_real_seconds()));
2050 spin_unlock(&exp->exp_lock);
2054 CDEBUG(D_SEC, "mark middle expired\n");
2055 exp->exp_flvr_expire[0] = 0;
2057 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
2058 exp->exp_flvr.sf_rpc,
2059 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2060 req->rq_flvr.sf_rpc);
2064 * now it doesn't match the current flavor, the only chance we can
2065 * accept it is match the old flavors which is not expired.
2067 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
2068 if (exp->exp_flvr_expire[1] >= ktime_get_real_seconds()) {
2069 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
2070 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the oldest one (%lld)\n",
2072 exp->exp_flvr.sf_rpc,
2073 exp->exp_flvr_old[0].sf_rpc,
2074 exp->exp_flvr_old[1].sf_rpc,
2075 (s64)(exp->exp_flvr_expire[1] -
2076 ktime_get_real_seconds()));
2077 spin_unlock(&exp->exp_lock);
2081 CDEBUG(D_SEC, "mark oldest expired\n");
2082 exp->exp_flvr_expire[1] = 0;
2084 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
2085 exp, exp->exp_flvr.sf_rpc,
2086 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2087 req->rq_flvr.sf_rpc);
2089 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
2090 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
2091 exp->exp_flvr_old[1].sf_rpc);
2094 spin_unlock(&exp->exp_lock);
2096 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with unauthorized flavor %x, expect %x|%x(%+lld)|%x(%+lld)\n",
2097 exp, exp->exp_obd->obd_name,
2098 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
2099 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
2100 req->rq_flvr.sf_rpc,
2101 exp->exp_flvr.sf_rpc,
2102 exp->exp_flvr_old[0].sf_rpc,
2103 exp->exp_flvr_expire[0] ?
2104 (s64)(exp->exp_flvr_expire[0] - ktime_get_real_seconds()) : 0,
2105 exp->exp_flvr_old[1].sf_rpc,
2106 exp->exp_flvr_expire[1] ?
2107 (s64)(exp->exp_flvr_expire[1] - ktime_get_real_seconds()) : 0);
2110 EXPORT_SYMBOL(sptlrpc_target_export_check);
2112 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
2113 struct sptlrpc_rule_set *rset)
2115 struct obd_export *exp;
2116 struct sptlrpc_flavor new_flvr;
2120 spin_lock(&obd->obd_dev_lock);
2122 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
2123 if (exp->exp_connection == NULL)
2127 * note if this export had just been updated flavor
2128 * (exp_flvr_changed == 1), this will override the
2131 spin_lock(&exp->exp_lock);
2132 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
2133 exp->exp_connection->c_peer.nid,
2135 if (exp->exp_flvr_changed ||
2136 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
2137 exp->exp_flvr_old[1] = new_flvr;
2138 exp->exp_flvr_expire[1] = 0;
2139 exp->exp_flvr_changed = 1;
2140 exp->exp_flvr_adapt = 1;
2142 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
2143 exp, sptlrpc_part2name(exp->exp_sp_peer),
2144 exp->exp_flvr.sf_rpc,
2145 exp->exp_flvr_old[1].sf_rpc);
2147 spin_unlock(&exp->exp_lock);
2150 spin_unlock(&obd->obd_dev_lock);
2152 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
2154 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
2156 /* peer's claim is unreliable unless gss is being used */
2157 if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
2160 switch (req->rq_sp_from) {
2162 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
2163 /* The below message is checked in sanity-sec test_33 */
2164 DEBUG_REQ(D_ERROR, req, "faked source CLI");
2165 svc_rc = SECSVC_DROP;
2169 if (!req->rq_auth_usr_mdt) {
2170 /* The below message is checked in sanity-sec test_33 */
2171 DEBUG_REQ(D_ERROR, req, "faked source MDT");
2172 svc_rc = SECSVC_DROP;
2176 if (!req->rq_auth_usr_ost) {
2177 /* The below message is checked in sanity-sec test_33 */
2178 DEBUG_REQ(D_ERROR, req, "faked source OST");
2179 svc_rc = SECSVC_DROP;
2184 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2185 !req->rq_auth_usr_ost) {
2186 /* The below message is checked in sanity-sec test_33 */
2187 DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
2188 svc_rc = SECSVC_DROP;
2193 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2194 svc_rc = SECSVC_DROP;
2201 * Used by ptlrpc server, to perform transformation upon request message of
2202 * incoming \a req. This must be the first thing to do with an incoming
2203 * request in ptlrpc layer.
2205 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2206 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2207 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2208 * reply message has been prepared.
2209 * \retval SECSVC_DROP failed, this request should be dropped.
2211 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2213 struct ptlrpc_sec_policy *policy;
2214 struct lustre_msg *msg = req->rq_reqbuf;
2220 LASSERT(req->rq_reqmsg == NULL);
2221 LASSERT(req->rq_repmsg == NULL);
2222 LASSERT(req->rq_svc_ctx == NULL);
2224 req->rq_req_swab_mask = 0;
2226 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2229 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
2233 CERROR("error unpacking request from %s x%llu\n",
2234 libcfs_id2str(req->rq_peer), req->rq_xid);
2235 RETURN(SECSVC_DROP);
2238 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2239 req->rq_sp_from = LUSTRE_SP_ANY;
2240 req->rq_auth_uid = -1; /* set to INVALID_UID */
2241 req->rq_auth_mapped_uid = -1;
2243 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2245 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2246 RETURN(SECSVC_DROP);
2249 LASSERT(policy->sp_sops->accept);
2250 rc = policy->sp_sops->accept(req);
2251 sptlrpc_policy_put(policy);
2252 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2253 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2256 * if it's not null flavor (which means embedded packing msg),
2257 * reset the swab mask for the comming inner msg unpacking.
2259 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2260 req->rq_req_swab_mask = 0;
2262 /* sanity check for the request source */
2263 rc = sptlrpc_svc_check_from(req, rc);
2268 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2269 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2270 * a buffer of \a msglen size.
2272 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2274 struct ptlrpc_sec_policy *policy;
2275 struct ptlrpc_reply_state *rs;
2280 LASSERT(req->rq_svc_ctx);
2281 LASSERT(req->rq_svc_ctx->sc_policy);
2283 policy = req->rq_svc_ctx->sc_policy;
2284 LASSERT(policy->sp_sops->alloc_rs);
2286 rc = policy->sp_sops->alloc_rs(req, msglen);
2287 if (unlikely(rc == -ENOMEM)) {
2288 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
2290 if (svcpt->scp_service->srv_max_reply_size <
2291 msglen + sizeof(struct ptlrpc_reply_state)) {
2292 /* Just return failure if the size is too big */
2293 CERROR("size of message is too big (%zd), %d allowed\n",
2294 msglen + sizeof(struct ptlrpc_reply_state),
2295 svcpt->scp_service->srv_max_reply_size);
2299 /* failed alloc, try emergency pool */
2300 rs = lustre_get_emerg_rs(svcpt);
2304 req->rq_reply_state = rs;
2305 rc = policy->sp_sops->alloc_rs(req, msglen);
2307 lustre_put_emerg_rs(rs);
2308 req->rq_reply_state = NULL;
2313 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2319 * Used by ptlrpc server, to perform transformation upon reply message.
2321 * \post req->rq_reply_off is set to approriate server-controlled reply offset.
2322 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2324 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2326 struct ptlrpc_sec_policy *policy;
2331 LASSERT(req->rq_svc_ctx);
2332 LASSERT(req->rq_svc_ctx->sc_policy);
2334 policy = req->rq_svc_ctx->sc_policy;
2335 LASSERT(policy->sp_sops->authorize);
2337 rc = policy->sp_sops->authorize(req);
2338 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2344 * Used by ptlrpc server, to free reply_state.
2346 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2348 struct ptlrpc_sec_policy *policy;
2349 unsigned int prealloc;
2353 LASSERT(rs->rs_svc_ctx);
2354 LASSERT(rs->rs_svc_ctx->sc_policy);
2356 policy = rs->rs_svc_ctx->sc_policy;
2357 LASSERT(policy->sp_sops->free_rs);
2359 prealloc = rs->rs_prealloc;
2360 policy->sp_sops->free_rs(rs);
2363 lustre_put_emerg_rs(rs);
2367 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2369 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2372 atomic_inc(&ctx->sc_refcount);
2375 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2377 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2382 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2383 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2384 if (ctx->sc_policy->sp_sops->free_ctx)
2385 ctx->sc_policy->sp_sops->free_ctx(ctx);
2387 req->rq_svc_ctx = NULL;
2390 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2392 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2397 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2398 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2399 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2401 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2408 * Perform transformation upon bulk data pointed by \a desc. This is called
2409 * before transforming the request message.
2411 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2412 struct ptlrpc_bulk_desc *desc)
2414 struct ptlrpc_cli_ctx *ctx;
2416 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2418 if (!req->rq_pack_bulk)
2421 ctx = req->rq_cli_ctx;
2422 if (ctx->cc_ops->wrap_bulk)
2423 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2426 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2429 * This is called after unwrap the reply message.
2430 * return nob of actual plain text size received, or error code.
2432 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2433 struct ptlrpc_bulk_desc *desc,
2436 struct ptlrpc_cli_ctx *ctx;
2439 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2441 if (!req->rq_pack_bulk)
2442 return desc->bd_nob_transferred;
2444 ctx = req->rq_cli_ctx;
2445 if (ctx->cc_ops->unwrap_bulk) {
2446 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2450 return desc->bd_nob_transferred;
2452 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2455 * This is called after unwrap the reply message.
2456 * return 0 for success or error code.
2458 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2459 struct ptlrpc_bulk_desc *desc)
2461 struct ptlrpc_cli_ctx *ctx;
2464 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2466 if (!req->rq_pack_bulk)
2469 ctx = req->rq_cli_ctx;
2470 if (ctx->cc_ops->unwrap_bulk) {
2471 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2477 * if everything is going right, nob should equals to nob_transferred.
2478 * in case of privacy mode, nob_transferred needs to be adjusted.
2480 if (desc->bd_nob != desc->bd_nob_transferred) {
2481 CERROR("nob %d doesn't match transferred nob %d\n",
2482 desc->bd_nob, desc->bd_nob_transferred);
2488 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2490 #ifdef HAVE_SERVER_SUPPORT
2492 * Performe transformation upon outgoing bulk read.
2494 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2495 struct ptlrpc_bulk_desc *desc)
2497 struct ptlrpc_svc_ctx *ctx;
2499 LASSERT(req->rq_bulk_read);
2501 if (!req->rq_pack_bulk)
2504 ctx = req->rq_svc_ctx;
2505 if (ctx->sc_policy->sp_sops->wrap_bulk)
2506 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2510 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2513 * Performe transformation upon incoming bulk write.
2515 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2516 struct ptlrpc_bulk_desc *desc)
2518 struct ptlrpc_svc_ctx *ctx;
2521 LASSERT(req->rq_bulk_write);
2524 * if it's in privacy mode, transferred should >= expected; otherwise
2525 * transferred should == expected.
2527 if (desc->bd_nob_transferred < desc->bd_nob ||
2528 (desc->bd_nob_transferred > desc->bd_nob &&
2529 SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2530 SPTLRPC_BULK_SVC_PRIV)) {
2531 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2532 desc->bd_nob_transferred, desc->bd_nob);
2536 if (!req->rq_pack_bulk)
2539 ctx = req->rq_svc_ctx;
2540 if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2541 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2543 CERROR("error unwrap bulk: %d\n", rc);
2546 /* return 0 to allow reply be sent */
2549 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2552 * Prepare buffers for incoming bulk write.
2554 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2555 struct ptlrpc_bulk_desc *desc)
2557 struct ptlrpc_svc_ctx *ctx;
2559 LASSERT(req->rq_bulk_write);
2561 if (!req->rq_pack_bulk)
2564 ctx = req->rq_svc_ctx;
2565 if (ctx->sc_policy->sp_sops->prep_bulk)
2566 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2570 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2572 #endif /* HAVE_SERVER_SUPPORT */
2575 * user descriptor helpers
2578 int sptlrpc_current_user_desc_size(void)
2582 ngroups = current_cred()->group_info->ngroups;
2584 if (ngroups > LUSTRE_MAX_GROUPS)
2585 ngroups = LUSTRE_MAX_GROUPS;
2586 return sptlrpc_user_desc_size(ngroups);
2588 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2590 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2592 struct ptlrpc_user_desc *pud;
2595 pud = lustre_msg_buf(msg, offset, 0);
2597 pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2598 pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2599 pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2600 pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2601 pud->pud_cap = cfs_curproc_cap_pack();
2602 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2605 ngroups = current_cred()->group_info->ngroups;
2606 if (pud->pud_ngroups > ngroups)
2607 pud->pud_ngroups = ngroups;
2608 #ifdef HAVE_GROUP_INFO_GID
2609 memcpy(pud->pud_groups, current_cred()->group_info->gid,
2610 pud->pud_ngroups * sizeof(__u32));
2611 #else /* !HAVE_GROUP_INFO_GID */
2612 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2613 pud->pud_ngroups * sizeof(__u32));
2614 #endif /* HAVE_GROUP_INFO_GID */
2615 task_unlock(current);
2619 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2621 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2623 struct ptlrpc_user_desc *pud;
2626 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2631 __swab32s(&pud->pud_uid);
2632 __swab32s(&pud->pud_gid);
2633 __swab32s(&pud->pud_fsuid);
2634 __swab32s(&pud->pud_fsgid);
2635 __swab32s(&pud->pud_cap);
2636 __swab32s(&pud->pud_ngroups);
2639 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2640 CERROR("%u groups is too large\n", pud->pud_ngroups);
2644 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2645 msg->lm_buflens[offset]) {
2646 CERROR("%u groups are claimed but bufsize only %u\n",
2647 pud->pud_ngroups, msg->lm_buflens[offset]);
2652 for (i = 0; i < pud->pud_ngroups; i++)
2653 __swab32s(&pud->pud_groups[i]);
2658 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2664 const char *sec2target_str(struct ptlrpc_sec *sec)
2666 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2668 if (sec_is_reverse(sec))
2670 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2672 EXPORT_SYMBOL(sec2target_str);
2675 * return true if the bulk data is protected
2677 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2679 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2680 case SPTLRPC_BULK_SVC_INTG:
2681 case SPTLRPC_BULK_SVC_PRIV:
2687 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2690 * crypto API helper/alloc blkciper
2694 * initialize/finalize
2697 int sptlrpc_init(void)
2701 rwlock_init(&policy_lock);
2703 rc = sptlrpc_gc_init();
2707 rc = sptlrpc_conf_init();
2711 rc = sptlrpc_enc_pool_init();
2715 rc = sptlrpc_null_init();
2719 rc = sptlrpc_plain_init();
2723 rc = sptlrpc_lproc_init();
2730 sptlrpc_plain_fini();
2732 sptlrpc_null_fini();
2734 sptlrpc_enc_pool_fini();
2736 sptlrpc_conf_fini();
2743 void sptlrpc_fini(void)
2745 sptlrpc_lproc_fini();
2746 sptlrpc_plain_fini();
2747 sptlrpc_null_fini();
2748 sptlrpc_enc_pool_fini();
2749 sptlrpc_conf_fini();