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 #ifdef HAVE_UIDGID_HEADER
41 # include <linux/uidgid.h>
43 #include <linux/crypto.h>
44 #include <linux/key.h>
46 #include <libcfs/libcfs.h>
48 #include <obd_class.h>
49 #include <obd_support.h>
50 #include <lustre_net.h>
51 #include <lustre_import.h>
52 #include <lustre_dlm.h>
53 #include <lustre_sec.h>
55 #include "ptlrpc_internal.h"
57 static int send_sepol;
58 module_param(send_sepol, int, 0644);
59 MODULE_PARM_DESC(send_sepol, "Client sends SELinux policy status");
61 /***********************************************
63 ***********************************************/
65 static rwlock_t policy_lock;
66 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
70 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
72 __u16 number = policy->sp_policy;
74 LASSERT(policy->sp_name);
75 LASSERT(policy->sp_cops);
76 LASSERT(policy->sp_sops);
78 if (number >= SPTLRPC_POLICY_MAX)
81 write_lock(&policy_lock);
82 if (unlikely(policies[number])) {
83 write_unlock(&policy_lock);
86 policies[number] = policy;
87 write_unlock(&policy_lock);
89 CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
92 EXPORT_SYMBOL(sptlrpc_register_policy);
94 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
96 __u16 number = policy->sp_policy;
98 LASSERT(number < SPTLRPC_POLICY_MAX);
100 write_lock(&policy_lock);
101 if (unlikely(policies[number] == NULL)) {
102 write_unlock(&policy_lock);
103 CERROR("%s: already unregistered\n", policy->sp_name);
107 LASSERT(policies[number] == policy);
108 policies[number] = NULL;
109 write_unlock(&policy_lock);
111 CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
114 EXPORT_SYMBOL(sptlrpc_unregister_policy);
117 struct ptlrpc_sec_policy * sptlrpc_wireflavor2policy(__u32 flavor)
119 static DEFINE_MUTEX(load_mutex);
120 static atomic_t loaded = ATOMIC_INIT(0);
121 struct ptlrpc_sec_policy *policy;
122 __u16 number = SPTLRPC_FLVR_POLICY(flavor);
125 if (number >= SPTLRPC_POLICY_MAX)
129 read_lock(&policy_lock);
130 policy = policies[number];
131 if (policy && !try_module_get(policy->sp_owner))
134 flag = atomic_read(&loaded);
135 read_unlock(&policy_lock);
137 if (policy != NULL || flag != 0 ||
138 number != SPTLRPC_POLICY_GSS)
141 /* try to load gss module, once */
142 mutex_lock(&load_mutex);
143 if (atomic_read(&loaded) == 0) {
144 if (request_module("ptlrpc_gss") == 0)
146 "module ptlrpc_gss loaded on demand\n");
148 CERROR("Unable to load module ptlrpc_gss\n");
150 atomic_set(&loaded, 1);
152 mutex_unlock(&load_mutex);
158 __u32 sptlrpc_name2flavor_base(const char *name)
160 if (!strcmp(name, "null"))
161 return SPTLRPC_FLVR_NULL;
162 if (!strcmp(name, "plain"))
163 return SPTLRPC_FLVR_PLAIN;
164 if (!strcmp(name, "gssnull"))
165 return SPTLRPC_FLVR_GSSNULL;
166 if (!strcmp(name, "krb5n"))
167 return SPTLRPC_FLVR_KRB5N;
168 if (!strcmp(name, "krb5a"))
169 return SPTLRPC_FLVR_KRB5A;
170 if (!strcmp(name, "krb5i"))
171 return SPTLRPC_FLVR_KRB5I;
172 if (!strcmp(name, "krb5p"))
173 return SPTLRPC_FLVR_KRB5P;
174 if (!strcmp(name, "skn"))
175 return SPTLRPC_FLVR_SKN;
176 if (!strcmp(name, "ska"))
177 return SPTLRPC_FLVR_SKA;
178 if (!strcmp(name, "ski"))
179 return SPTLRPC_FLVR_SKI;
180 if (!strcmp(name, "skpi"))
181 return SPTLRPC_FLVR_SKPI;
183 return SPTLRPC_FLVR_INVALID;
185 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
187 const char *sptlrpc_flavor2name_base(__u32 flvr)
189 __u32 base = SPTLRPC_FLVR_BASE(flvr);
191 if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
193 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
195 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_GSSNULL))
197 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
199 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
201 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
203 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
205 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKN))
207 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKA))
209 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKI))
211 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKPI))
214 CERROR("invalid wire flavor 0x%x\n", flvr);
217 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
219 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
220 char *buf, int bufsize)
222 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
223 snprintf(buf, bufsize, "hash:%s",
224 sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
226 snprintf(buf, bufsize, "%s",
227 sptlrpc_flavor2name_base(sf->sf_rpc));
229 buf[bufsize - 1] = '\0';
232 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
234 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
236 snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
239 * currently we don't support customized bulk specification for
240 * flavors other than plain
242 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
246 sptlrpc_flavor2name_bulk(sf, &bspec[1], sizeof(bspec) - 1);
247 strncat(buf, bspec, bufsize);
250 buf[bufsize - 1] = '\0';
253 EXPORT_SYMBOL(sptlrpc_flavor2name);
255 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
259 if (flags & PTLRPC_SEC_FL_REVERSE)
260 strlcat(buf, "reverse,", bufsize);
261 if (flags & PTLRPC_SEC_FL_ROOTONLY)
262 strlcat(buf, "rootonly,", bufsize);
263 if (flags & PTLRPC_SEC_FL_UDESC)
264 strlcat(buf, "udesc,", bufsize);
265 if (flags & PTLRPC_SEC_FL_BULK)
266 strlcat(buf, "bulk,", bufsize);
268 strlcat(buf, "-,", bufsize);
272 EXPORT_SYMBOL(sptlrpc_secflags2str);
274 /**************************************************
275 * client context APIs *
276 **************************************************/
279 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
281 struct vfs_cred vcred;
282 int create = 1, remove_dead = 1;
285 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
287 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
288 PTLRPC_SEC_FL_ROOTONLY)) {
291 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
296 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
297 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
300 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred, create,
304 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
306 atomic_inc(&ctx->cc_refcount);
309 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
311 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
313 struct ptlrpc_sec *sec = ctx->cc_sec;
316 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
318 if (!atomic_dec_and_test(&ctx->cc_refcount))
321 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
323 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
326 * Expire the client context immediately.
328 * \pre Caller must hold at least 1 reference on the \a ctx.
330 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
332 LASSERT(ctx->cc_ops->die);
333 ctx->cc_ops->die(ctx, 0);
335 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
338 * To wake up the threads who are waiting for this client context. Called
339 * after some status change happened on \a ctx.
341 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
343 struct ptlrpc_request *req, *next;
345 spin_lock(&ctx->cc_lock);
346 list_for_each_entry_safe(req, next, &ctx->cc_req_list,
348 list_del_init(&req->rq_ctx_chain);
349 ptlrpc_client_wake_req(req);
351 spin_unlock(&ctx->cc_lock);
353 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
355 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
357 LASSERT(ctx->cc_ops);
359 if (ctx->cc_ops->display == NULL)
362 return ctx->cc_ops->display(ctx, buf, bufsize);
365 static int import_sec_check_expire(struct obd_import *imp)
369 spin_lock(&imp->imp_lock);
370 if (imp->imp_sec_expire &&
371 imp->imp_sec_expire < ktime_get_real_seconds()) {
373 imp->imp_sec_expire = 0;
375 spin_unlock(&imp->imp_lock);
380 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
381 return sptlrpc_import_sec_adapt(imp, NULL, NULL);
385 * Get and validate the client side ptlrpc security facilities from
386 * \a imp. There is a race condition on client reconnect when the import is
387 * being destroyed while there are outstanding client bound requests. In
388 * this case do not output any error messages if import secuity is not
391 * \param[in] imp obd import associated with client
392 * \param[out] sec client side ptlrpc security
394 * \retval 0 if security retrieved successfully
395 * \retval -ve errno if there was a problem
397 static int import_sec_validate_get(struct obd_import *imp,
398 struct ptlrpc_sec **sec)
402 if (unlikely(imp->imp_sec_expire)) {
403 rc = import_sec_check_expire(imp);
408 *sec = sptlrpc_import_sec_ref(imp);
410 CERROR("import %p (%s) with no sec\n",
411 imp, ptlrpc_import_state_name(imp->imp_state));
415 if (unlikely((*sec)->ps_dying)) {
416 CERROR("attempt to use dying sec %p\n", sec);
417 sptlrpc_sec_put(*sec);
425 * Given a \a req, find or allocate an appropriate context for it.
426 * \pre req->rq_cli_ctx == NULL.
428 * \retval 0 succeed, and req->rq_cli_ctx is set.
429 * \retval -ev error number, and req->rq_cli_ctx == NULL.
431 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
433 struct obd_import *imp = req->rq_import;
434 struct ptlrpc_sec *sec;
438 LASSERT(!req->rq_cli_ctx);
441 rc = import_sec_validate_get(imp, &sec);
445 req->rq_cli_ctx = get_my_ctx(sec);
447 sptlrpc_sec_put(sec);
449 if (!req->rq_cli_ctx) {
450 CERROR("req %p: fail to get context\n", req);
451 RETURN(-ECONNREFUSED);
458 * Drop the context for \a req.
459 * \pre req->rq_cli_ctx != NULL.
460 * \post req->rq_cli_ctx == NULL.
462 * If \a sync == 0, this function should return quickly without sleep;
463 * otherwise it might trigger and wait for the whole process of sending
464 * an context-destroying rpc to server.
466 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
471 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);
501 CDEBUG(D_SEC, "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), "
502 "switch sec %p(%s) -> %p(%s)\n", req,
503 oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
504 newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
505 oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
506 newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
509 old_flvr = req->rq_flvr;
511 /* save request message */
512 reqmsg_size = req->rq_reqlen;
513 if (reqmsg_size != 0) {
514 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
517 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
520 /* release old req/rep buf */
521 req->rq_cli_ctx = oldctx;
522 sptlrpc_cli_free_reqbuf(req);
523 sptlrpc_cli_free_repbuf(req);
524 req->rq_cli_ctx = newctx;
526 /* recalculate the flavor */
527 sptlrpc_req_set_flavor(req, 0);
529 /* alloc new request buffer
530 * we don't need to alloc reply buffer here, leave it to the
531 * rest procedure of ptlrpc */
532 if (reqmsg_size != 0) {
533 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
535 LASSERT(req->rq_reqmsg);
536 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
538 CWARN("failed to alloc reqbuf: %d\n", rc);
539 req->rq_flvr = old_flvr;
542 OBD_FREE_LARGE(reqmsg, reqmsg_size);
548 * If current context of \a req is dead somehow, e.g. we just switched flavor
549 * thus marked original contexts dead, we'll find a new context for it. if
550 * no switch is needed, \a req will end up with the same context.
552 * \note a request must have a context, to keep other parts of code happy.
553 * In any case of failure during the switching, we must restore the old one.
555 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
557 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
558 struct ptlrpc_cli_ctx *newctx;
564 sptlrpc_cli_ctx_get(oldctx);
565 sptlrpc_req_put_ctx(req, 0);
567 rc = sptlrpc_req_get_ctx(req);
569 LASSERT(!req->rq_cli_ctx);
571 /* restore old ctx */
572 req->rq_cli_ctx = oldctx;
576 newctx = req->rq_cli_ctx;
579 if (unlikely(newctx == oldctx &&
580 test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
582 * still get the old dead ctx, usually means system too busy
585 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
586 newctx, newctx->cc_flags);
588 set_current_state(TASK_INTERRUPTIBLE);
589 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC));
590 } else if (unlikely(test_bit(PTLRPC_CTX_UPTODATE_BIT, &newctx->cc_flags)
593 * new ctx not up to date yet
596 "ctx (%p, fl %lx) doesn't switch, not up to date yet\n",
597 newctx, newctx->cc_flags);
600 * it's possible newctx == oldctx if we're switching
601 * subflavor with the same sec.
603 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
605 /* restore old ctx */
606 sptlrpc_req_put_ctx(req, 0);
607 req->rq_cli_ctx = oldctx;
611 LASSERT(req->rq_cli_ctx == newctx);
614 sptlrpc_cli_ctx_put(oldctx, 1);
617 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
620 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
622 if (cli_ctx_is_refreshed(ctx))
628 int ctx_refresh_timeout(void *data)
630 struct ptlrpc_request *req = data;
633 /* conn_cnt is needed in expire_one_request */
634 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
636 rc = ptlrpc_expire_one_request(req, 1);
637 /* if we started recovery, we should mark this ctx dead; otherwise
638 * in case of lgssd died nobody would retire this ctx, following
639 * connecting will still find the same ctx thus cause deadlock.
640 * there's an assumption that expire time of the request should be
641 * later than the context refresh expire time.
644 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
649 void ctx_refresh_interrupt(void *data)
651 struct ptlrpc_request *req = data;
653 spin_lock(&req->rq_lock);
655 spin_unlock(&req->rq_lock);
659 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
661 spin_lock(&ctx->cc_lock);
662 if (!list_empty(&req->rq_ctx_chain))
663 list_del_init(&req->rq_ctx_chain);
664 spin_unlock(&ctx->cc_lock);
668 * To refresh the context of \req, if it's not up-to-date.
671 * - = 0: wait until success or fatal error occur
672 * - > 0: timeout value (in seconds)
674 * The status of the context could be subject to be changed by other threads
675 * at any time. We allow this race, but once we return with 0, the caller will
676 * suppose it's uptodated and keep using it until the owning rpc is done.
678 * \retval 0 only if the context is uptodated.
679 * \retval -ev error number.
681 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
683 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
684 struct ptlrpc_sec *sec;
685 struct l_wait_info lwi;
691 if (req->rq_ctx_init || req->rq_ctx_fini)
695 * during the process a request's context might change type even
696 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
700 rc = import_sec_validate_get(req->rq_import, &sec);
704 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
705 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
706 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
707 req_off_ctx_list(req, ctx);
708 sptlrpc_req_replace_dead_ctx(req);
709 ctx = req->rq_cli_ctx;
711 sptlrpc_sec_put(sec);
713 if (cli_ctx_is_eternal(ctx))
716 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
717 LASSERT(ctx->cc_ops->refresh);
718 ctx->cc_ops->refresh(ctx);
720 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
722 LASSERT(ctx->cc_ops->validate);
723 if (ctx->cc_ops->validate(ctx) == 0) {
724 req_off_ctx_list(req, ctx);
728 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
729 spin_lock(&req->rq_lock);
731 spin_unlock(&req->rq_lock);
732 req_off_ctx_list(req, ctx);
737 * There's a subtle issue for resending RPCs, suppose following
739 * 1. the request was sent to server.
740 * 2. recovery was kicked start, after finished the request was
742 * 3. resend the request.
743 * 4. old reply from server received, we accept and verify the reply.
744 * this has to be success, otherwise the error will be aware
746 * 5. new reply from server received, dropped by LNet.
748 * Note the xid of old & new request is the same. We can't simply
749 * change xid for the resent request because the server replies on
750 * it for reply reconstruction.
752 * Commonly the original context should be uptodate because we
753 * have an expiry nice time; server will keep its context because
754 * we at least hold a ref of old context which prevent context
755 * from destroying RPC being sent. So server still can accept the
756 * request and finish the RPC. But if that's not the case:
757 * 1. If server side context has been trimmed, a NO_CONTEXT will
758 * be returned, gss_cli_ctx_verify/unseal will switch to new
760 * 2. Current context never be refreshed, then we are fine: we
761 * never really send request with old context before.
763 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
764 unlikely(req->rq_reqmsg) &&
765 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
766 req_off_ctx_list(req, ctx);
770 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
771 req_off_ctx_list(req, ctx);
773 * don't switch ctx if import was deactivated
775 if (req->rq_import->imp_deactive) {
776 spin_lock(&req->rq_lock);
778 spin_unlock(&req->rq_lock);
782 rc = sptlrpc_req_replace_dead_ctx(req);
784 LASSERT(ctx == req->rq_cli_ctx);
785 CERROR("req %p: failed to replace dead ctx %p: %d\n",
787 spin_lock(&req->rq_lock);
789 spin_unlock(&req->rq_lock);
793 ctx = req->rq_cli_ctx;
798 * Now we're sure this context is during upcall, add myself into
801 spin_lock(&ctx->cc_lock);
802 if (list_empty(&req->rq_ctx_chain))
803 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
804 spin_unlock(&ctx->cc_lock);
807 RETURN(-EWOULDBLOCK);
809 /* Clear any flags that may be present from previous sends */
810 LASSERT(req->rq_receiving_reply == 0);
811 spin_lock(&req->rq_lock);
813 req->rq_timedout = 0;
816 spin_unlock(&req->rq_lock);
818 lwi = LWI_TIMEOUT_INTR(msecs_to_jiffies(timeout * MSEC_PER_SEC),
820 ctx_refresh_interrupt, req);
821 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
824 * following cases could lead us here:
825 * - successfully refreshed;
827 * - timedout, and we don't want recover from the failure;
828 * - timedout, and waked up upon recovery finished;
829 * - someone else mark this ctx dead by force;
830 * - someone invalidate the req and call ptlrpc_client_wake_req(),
831 * e.g. ptlrpc_abort_inflight();
833 if (!cli_ctx_is_refreshed(ctx)) {
834 /* timed out or interruptted */
835 req_off_ctx_list(req, ctx);
845 * Initialize flavor settings for \a req, according to \a opcode.
847 * \note this could be called in two situations:
848 * - new request from ptlrpc_pre_req(), with proper @opcode
849 * - old request which changed ctx in the middle, with @opcode == 0
851 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
853 struct ptlrpc_sec *sec;
855 LASSERT(req->rq_import);
856 LASSERT(req->rq_cli_ctx);
857 LASSERT(req->rq_cli_ctx->cc_sec);
858 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
860 /* special security flags according to opcode */
864 case MGS_CONFIG_READ:
866 req->rq_bulk_read = 1;
870 req->rq_bulk_write = 1;
873 req->rq_ctx_init = 1;
876 req->rq_ctx_fini = 1;
879 /* init/fini rpc won't be resend, so can't be here */
880 LASSERT(req->rq_ctx_init == 0);
881 LASSERT(req->rq_ctx_fini == 0);
883 /* cleanup flags, which should be recalculated */
884 req->rq_pack_udesc = 0;
885 req->rq_pack_bulk = 0;
889 sec = req->rq_cli_ctx->cc_sec;
891 spin_lock(&sec->ps_lock);
892 req->rq_flvr = sec->ps_flvr;
893 spin_unlock(&sec->ps_lock);
895 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
897 if (unlikely(req->rq_ctx_init))
898 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
899 else if (unlikely(req->rq_ctx_fini))
900 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
902 /* user descriptor flag, null security can't do it anyway */
903 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
904 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
905 req->rq_pack_udesc = 1;
907 /* bulk security flag */
908 if ((req->rq_bulk_read || req->rq_bulk_write) &&
909 sptlrpc_flavor_has_bulk(&req->rq_flvr))
910 req->rq_pack_bulk = 1;
913 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
915 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
918 LASSERT(req->rq_clrbuf);
919 if (req->rq_pool || !req->rq_reqbuf)
922 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
923 req->rq_reqbuf = NULL;
924 req->rq_reqbuf_len = 0;
928 * Given an import \a imp, check whether current user has a valid context
929 * or not. We may create a new context and try to refresh it, and try
930 * repeatedly try in case of non-fatal errors. Return 0 means success.
932 int sptlrpc_import_check_ctx(struct obd_import *imp)
934 struct ptlrpc_sec *sec;
935 struct ptlrpc_cli_ctx *ctx;
936 struct ptlrpc_request *req = NULL;
942 sec = sptlrpc_import_sec_ref(imp);
943 ctx = get_my_ctx(sec);
944 sptlrpc_sec_put(sec);
949 if (cli_ctx_is_eternal(ctx) ||
950 ctx->cc_ops->validate(ctx) == 0) {
951 sptlrpc_cli_ctx_put(ctx, 1);
955 if (cli_ctx_is_error(ctx)) {
956 sptlrpc_cli_ctx_put(ctx, 1);
960 req = ptlrpc_request_cache_alloc(GFP_NOFS);
964 ptlrpc_cli_req_init(req);
965 atomic_set(&req->rq_refcount, 10000);
967 req->rq_import = imp;
968 req->rq_flvr = sec->ps_flvr;
969 req->rq_cli_ctx = ctx;
971 rc = sptlrpc_req_refresh_ctx(req, 0);
972 LASSERT(list_empty(&req->rq_ctx_chain));
973 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
974 ptlrpc_request_cache_free(req);
980 * Used by ptlrpc client, to perform the pre-defined security transformation
981 * upon the request message of \a req. After this function called,
982 * req->rq_reqmsg is still accessible as clear text.
984 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
986 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
991 LASSERT(ctx->cc_sec);
992 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
994 /* we wrap bulk request here because now we can be sure
995 * the context is uptodate.
998 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
1003 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1004 case SPTLRPC_SVC_NULL:
1005 case SPTLRPC_SVC_AUTH:
1006 case SPTLRPC_SVC_INTG:
1007 LASSERT(ctx->cc_ops->sign);
1008 rc = ctx->cc_ops->sign(ctx, req);
1010 case SPTLRPC_SVC_PRIV:
1011 LASSERT(ctx->cc_ops->seal);
1012 rc = ctx->cc_ops->seal(ctx, req);
1019 LASSERT(req->rq_reqdata_len);
1020 LASSERT(req->rq_reqdata_len % 8 == 0);
1021 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
1027 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
1029 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1034 LASSERT(ctx->cc_sec);
1035 LASSERT(req->rq_repbuf);
1036 LASSERT(req->rq_repdata);
1037 LASSERT(req->rq_repmsg == NULL);
1039 req->rq_rep_swab_mask = 0;
1041 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
1044 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1048 CERROR("failed unpack reply: x%llu\n", req->rq_xid);
1052 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
1053 CERROR("replied data length %d too small\n",
1054 req->rq_repdata_len);
1058 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1059 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1060 CERROR("reply policy %u doesn't match request policy %u\n",
1061 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1062 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1066 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1067 case SPTLRPC_SVC_NULL:
1068 case SPTLRPC_SVC_AUTH:
1069 case SPTLRPC_SVC_INTG:
1070 LASSERT(ctx->cc_ops->verify);
1071 rc = ctx->cc_ops->verify(ctx, req);
1073 case SPTLRPC_SVC_PRIV:
1074 LASSERT(ctx->cc_ops->unseal);
1075 rc = ctx->cc_ops->unseal(ctx, req);
1080 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1082 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1084 req->rq_rep_swab_mask = 0;
1089 * Used by ptlrpc client, to perform security transformation upon the reply
1090 * message of \a req. After return successfully, req->rq_repmsg points to
1091 * the reply message in clear text.
1093 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1096 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1098 LASSERT(req->rq_repbuf);
1099 LASSERT(req->rq_repdata == NULL);
1100 LASSERT(req->rq_repmsg == NULL);
1101 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1103 if (req->rq_reply_off == 0 &&
1104 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1105 CERROR("real reply with offset 0\n");
1109 if (req->rq_reply_off % 8 != 0) {
1110 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1114 req->rq_repdata = (struct lustre_msg *)
1115 (req->rq_repbuf + req->rq_reply_off);
1116 req->rq_repdata_len = req->rq_nob_received;
1118 return do_cli_unwrap_reply(req);
1122 * Used by ptlrpc client, to perform security transformation upon the early
1123 * reply message of \a req. We expect the rq_reply_off is 0, and
1124 * rq_nob_received is the early reply size.
1126 * Because the receive buffer might be still posted, the reply data might be
1127 * changed at any time, no matter we're holding rq_lock or not. For this reason
1128 * we allocate a separate ptlrpc_request and reply buffer for early reply
1131 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1132 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1133 * \a *req_ret to release it.
1134 * \retval -ev error number, and \a req_ret will not be set.
1136 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1137 struct ptlrpc_request **req_ret)
1139 struct ptlrpc_request *early_req;
1141 int early_bufsz, early_size;
1145 early_req = ptlrpc_request_cache_alloc(GFP_NOFS);
1146 if (early_req == NULL)
1149 ptlrpc_cli_req_init(early_req);
1151 early_size = req->rq_nob_received;
1152 early_bufsz = size_roundup_power2(early_size);
1153 OBD_ALLOC_LARGE(early_buf, early_bufsz);
1154 if (early_buf == NULL)
1155 GOTO(err_req, rc = -ENOMEM);
1157 /* sanity checkings and copy data out, do it inside spinlock */
1158 spin_lock(&req->rq_lock);
1160 if (req->rq_replied) {
1161 spin_unlock(&req->rq_lock);
1162 GOTO(err_buf, rc = -EALREADY);
1165 LASSERT(req->rq_repbuf);
1166 LASSERT(req->rq_repdata == NULL);
1167 LASSERT(req->rq_repmsg == NULL);
1169 if (req->rq_reply_off != 0) {
1170 CERROR("early reply with offset %u\n", req->rq_reply_off);
1171 spin_unlock(&req->rq_lock);
1172 GOTO(err_buf, rc = -EPROTO);
1175 if (req->rq_nob_received != early_size) {
1176 /* even another early arrived the size should be the same */
1177 CERROR("data size has changed from %u to %u\n",
1178 early_size, req->rq_nob_received);
1179 spin_unlock(&req->rq_lock);
1180 GOTO(err_buf, rc = -EINVAL);
1183 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1184 CERROR("early reply length %d too small\n",
1185 req->rq_nob_received);
1186 spin_unlock(&req->rq_lock);
1187 GOTO(err_buf, rc = -EALREADY);
1190 memcpy(early_buf, req->rq_repbuf, early_size);
1191 spin_unlock(&req->rq_lock);
1193 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1194 early_req->rq_flvr = req->rq_flvr;
1195 early_req->rq_repbuf = early_buf;
1196 early_req->rq_repbuf_len = early_bufsz;
1197 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1198 early_req->rq_repdata_len = early_size;
1199 early_req->rq_early = 1;
1200 early_req->rq_reqmsg = req->rq_reqmsg;
1202 rc = do_cli_unwrap_reply(early_req);
1204 DEBUG_REQ(D_ADAPTTO, early_req,
1205 "error %d unwrap early reply", rc);
1209 LASSERT(early_req->rq_repmsg);
1210 *req_ret = early_req;
1214 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1216 OBD_FREE_LARGE(early_buf, early_bufsz);
1218 ptlrpc_request_cache_free(early_req);
1223 * Used by ptlrpc client, to release a processed early reply \a early_req.
1225 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1227 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1229 LASSERT(early_req->rq_repbuf);
1230 LASSERT(early_req->rq_repdata);
1231 LASSERT(early_req->rq_repmsg);
1233 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1234 OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1235 ptlrpc_request_cache_free(early_req);
1238 /**************************************************
1240 **************************************************/
1243 * "fixed" sec (e.g. null) use sec_id < 0
1245 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1247 int sptlrpc_get_next_secid(void)
1249 return atomic_inc_return(&sptlrpc_sec_id);
1251 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1253 /**************************************************
1254 * client side high-level security APIs *
1255 **************************************************/
1257 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1258 int grace, int force)
1260 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1262 LASSERT(policy->sp_cops);
1263 LASSERT(policy->sp_cops->flush_ctx_cache);
1265 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1268 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1270 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1272 LASSERT_ATOMIC_ZERO(&sec->ps_refcount);
1273 LASSERT_ATOMIC_ZERO(&sec->ps_nctx);
1274 LASSERT(policy->sp_cops->destroy_sec);
1276 CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1278 policy->sp_cops->destroy_sec(sec);
1279 sptlrpc_policy_put(policy);
1282 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1284 sec_cop_destroy_sec(sec);
1286 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1288 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1290 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1292 if (sec->ps_policy->sp_cops->kill_sec) {
1293 sec->ps_policy->sp_cops->kill_sec(sec);
1295 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1299 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1302 atomic_inc(&sec->ps_refcount);
1306 EXPORT_SYMBOL(sptlrpc_sec_get);
1308 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1311 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1313 if (atomic_dec_and_test(&sec->ps_refcount)) {
1314 sptlrpc_gc_del_sec(sec);
1315 sec_cop_destroy_sec(sec);
1319 EXPORT_SYMBOL(sptlrpc_sec_put);
1322 * policy module is responsible for taking refrence of import
1325 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1326 struct ptlrpc_svc_ctx *svc_ctx,
1327 struct sptlrpc_flavor *sf,
1328 enum lustre_sec_part sp)
1330 struct ptlrpc_sec_policy *policy;
1331 struct ptlrpc_sec *sec;
1336 LASSERT(imp->imp_dlm_fake == 1);
1338 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1339 imp->imp_obd->obd_type->typ_name,
1340 imp->imp_obd->obd_name,
1341 sptlrpc_flavor2name(sf, str, sizeof(str)));
1343 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1344 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1346 LASSERT(imp->imp_dlm_fake == 0);
1348 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1349 imp->imp_obd->obd_type->typ_name,
1350 imp->imp_obd->obd_name,
1351 sptlrpc_flavor2name(sf, str, sizeof(str)));
1353 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1355 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1360 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1362 atomic_inc(&sec->ps_refcount);
1366 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1367 sptlrpc_gc_add_sec(sec);
1369 sptlrpc_policy_put(policy);
1375 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1377 struct ptlrpc_sec *sec;
1379 spin_lock(&imp->imp_lock);
1380 sec = sptlrpc_sec_get(imp->imp_sec);
1381 spin_unlock(&imp->imp_lock);
1385 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1387 static void sptlrpc_import_sec_install(struct obd_import *imp,
1388 struct ptlrpc_sec *sec)
1390 struct ptlrpc_sec *old_sec;
1392 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1394 spin_lock(&imp->imp_lock);
1395 old_sec = imp->imp_sec;
1397 spin_unlock(&imp->imp_lock);
1400 sptlrpc_sec_kill(old_sec);
1402 /* balance the ref taken by this import */
1403 sptlrpc_sec_put(old_sec);
1408 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1410 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1414 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1420 * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1421 * configuration. Upon called, imp->imp_sec may or may not be NULL.
1423 * - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1424 * - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1426 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1427 struct ptlrpc_svc_ctx *svc_ctx,
1428 struct sptlrpc_flavor *flvr)
1430 struct ptlrpc_connection *conn;
1431 struct sptlrpc_flavor sf;
1432 struct ptlrpc_sec *sec, *newsec;
1433 enum lustre_sec_part sp;
1443 conn = imp->imp_connection;
1445 if (svc_ctx == NULL) {
1446 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1448 * normal import, determine flavor from rule set, except
1449 * for mgc the flavor is predetermined.
1451 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1452 sf = cliobd->cl_flvr_mgc;
1454 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1456 &cliobd->cl_target_uuid,
1459 sp = imp->imp_obd->u.cli.cl_sp_me;
1461 /* reverse import, determine flavor from incoming reqeust */
1464 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1465 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1466 PTLRPC_SEC_FL_ROOTONLY;
1468 sp = sptlrpc_target_sec_part(imp->imp_obd);
1471 sec = sptlrpc_import_sec_ref(imp);
1475 if (flavor_equal(&sf, &sec->ps_flvr))
1478 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1479 imp->imp_obd->obd_name,
1480 obd_uuid2str(&conn->c_remote_uuid),
1481 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1482 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1483 } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1484 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1485 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1486 imp->imp_obd->obd_name,
1487 obd_uuid2str(&conn->c_remote_uuid),
1488 LNET_NIDNET(conn->c_self),
1489 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1492 mutex_lock(&imp->imp_sec_mutex);
1494 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1496 sptlrpc_import_sec_install(imp, newsec);
1498 CERROR("import %s->%s: failed to create new sec\n",
1499 imp->imp_obd->obd_name,
1500 obd_uuid2str(&conn->c_remote_uuid));
1504 mutex_unlock(&imp->imp_sec_mutex);
1506 sptlrpc_sec_put(sec);
1510 void sptlrpc_import_sec_put(struct obd_import *imp)
1513 sptlrpc_sec_kill(imp->imp_sec);
1515 sptlrpc_sec_put(imp->imp_sec);
1516 imp->imp_sec = NULL;
1520 static void import_flush_ctx_common(struct obd_import *imp,
1521 uid_t uid, int grace, int force)
1523 struct ptlrpc_sec *sec;
1528 sec = sptlrpc_import_sec_ref(imp);
1532 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1533 sptlrpc_sec_put(sec);
1536 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1538 /* it's important to use grace mode, see explain in
1539 * sptlrpc_req_refresh_ctx() */
1540 import_flush_ctx_common(imp, 0, 1, 1);
1543 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1545 import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1548 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1550 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1552 import_flush_ctx_common(imp, -1, 1, 1);
1554 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1557 * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1558 * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1560 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1562 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1563 struct ptlrpc_sec_policy *policy;
1567 LASSERT(ctx->cc_sec);
1568 LASSERT(ctx->cc_sec->ps_policy);
1569 LASSERT(req->rq_reqmsg == NULL);
1570 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1572 policy = ctx->cc_sec->ps_policy;
1573 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1575 LASSERT(req->rq_reqmsg);
1576 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1578 /* zeroing preallocated buffer */
1580 memset(req->rq_reqmsg, 0, msgsize);
1587 * Used by ptlrpc client to free request buffer of \a req. After this
1588 * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1590 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1592 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1593 struct ptlrpc_sec_policy *policy;
1596 LASSERT(ctx->cc_sec);
1597 LASSERT(ctx->cc_sec->ps_policy);
1598 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1600 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1603 policy = ctx->cc_sec->ps_policy;
1604 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1605 req->rq_reqmsg = NULL;
1609 * NOTE caller must guarantee the buffer size is enough for the enlargement
1611 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1612 int segment, int newsize)
1615 int oldsize, oldmsg_size, movesize;
1617 LASSERT(segment < msg->lm_bufcount);
1618 LASSERT(msg->lm_buflens[segment] <= newsize);
1620 if (msg->lm_buflens[segment] == newsize)
1623 /* nothing to do if we are enlarging the last segment */
1624 if (segment == msg->lm_bufcount - 1) {
1625 msg->lm_buflens[segment] = newsize;
1629 oldsize = msg->lm_buflens[segment];
1631 src = lustre_msg_buf(msg, segment + 1, 0);
1632 msg->lm_buflens[segment] = newsize;
1633 dst = lustre_msg_buf(msg, segment + 1, 0);
1634 msg->lm_buflens[segment] = oldsize;
1636 /* move from segment + 1 to end segment */
1637 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1638 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1639 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1640 LASSERT(movesize >= 0);
1643 memmove(dst, src, movesize);
1645 /* note we don't clear the ares where old data live, not secret */
1647 /* finally set new segment size */
1648 msg->lm_buflens[segment] = newsize;
1650 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1653 * Used by ptlrpc client to enlarge the \a segment of request message pointed
1654 * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1655 * preserved after the enlargement. this must be called after original request
1656 * buffer being allocated.
1658 * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1659 * so caller should refresh its local pointers if needed.
1661 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1662 const struct req_msg_field *field,
1665 struct req_capsule *pill = &req->rq_pill;
1666 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1667 struct ptlrpc_sec_cops *cops;
1668 struct lustre_msg *msg = req->rq_reqmsg;
1669 int segment = __req_capsule_offset(pill, field, RCL_CLIENT);
1673 LASSERT(msg->lm_bufcount > segment);
1674 LASSERT(msg->lm_buflens[segment] <= newsize);
1676 if (msg->lm_buflens[segment] == newsize)
1679 cops = ctx->cc_sec->ps_policy->sp_cops;
1680 LASSERT(cops->enlarge_reqbuf);
1681 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1683 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1686 * Used by ptlrpc client to allocate reply buffer of \a req.
1688 * \note After this, req->rq_repmsg is still not accessible.
1690 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1692 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1693 struct ptlrpc_sec_policy *policy;
1697 LASSERT(ctx->cc_sec);
1698 LASSERT(ctx->cc_sec->ps_policy);
1703 policy = ctx->cc_sec->ps_policy;
1704 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1708 * Used by ptlrpc client to free reply buffer of \a req. After this
1709 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1711 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1713 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1714 struct ptlrpc_sec_policy *policy;
1718 LASSERT(ctx->cc_sec);
1719 LASSERT(ctx->cc_sec->ps_policy);
1720 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1722 if (req->rq_repbuf == NULL)
1724 LASSERT(req->rq_repbuf_len);
1726 policy = ctx->cc_sec->ps_policy;
1727 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1728 req->rq_repmsg = NULL;
1731 EXPORT_SYMBOL(sptlrpc_cli_free_repbuf);
1733 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1734 struct ptlrpc_cli_ctx *ctx)
1736 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1738 if (!policy->sp_cops->install_rctx)
1740 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1743 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1744 struct ptlrpc_svc_ctx *ctx)
1746 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1748 if (!policy->sp_sops->install_rctx)
1750 return policy->sp_sops->install_rctx(imp, ctx);
1753 /* Get SELinux policy info from userspace */
1754 static int sepol_helper(struct obd_import *imp)
1756 char mtime_str[21] = { 0 }, mode_str[2] = { 0 };
1758 [0] = "/usr/sbin/l_getsepol",
1760 [2] = NULL, /* obd type */
1762 [4] = NULL, /* obd name */
1764 [6] = mtime_str, /* policy mtime */
1766 [8] = mode_str, /* enforcing mode */
1771 [1] = "PATH=/sbin:/usr/sbin",
1777 if (imp == NULL || imp->imp_obd == NULL ||
1778 imp->imp_obd->obd_type == NULL) {
1781 argv[2] = imp->imp_obd->obd_type->typ_name;
1782 argv[4] = imp->imp_obd->obd_name;
1783 spin_lock(&imp->imp_sec->ps_lock);
1784 if (imp->imp_sec->ps_sepol_mtime == 0 &&
1785 imp->imp_sec->ps_sepol[0] == '\0') {
1786 /* ps_sepol has not been initialized */
1790 snprintf(mtime_str, sizeof(mtime_str), "%lu",
1791 imp->imp_sec->ps_sepol_mtime);
1792 mode_str[0] = imp->imp_sec->ps_sepol[0];
1794 spin_unlock(&imp->imp_sec->ps_lock);
1795 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1802 static inline int sptlrpc_sepol_needs_check(struct ptlrpc_sec *imp_sec)
1806 if (send_sepol == 0 || !selinux_is_enabled())
1809 if (send_sepol == -1)
1810 /* send_sepol == -1 means fetch sepol status every time */
1813 spin_lock(&imp_sec->ps_lock);
1814 checknext = imp_sec->ps_sepol_checknext;
1815 spin_unlock(&imp_sec->ps_lock);
1817 /* next check is too far in time, please update */
1818 if (ktime_after(checknext,
1819 ktime_add(ktime_get(), ktime_set(send_sepol, 0))))
1822 if (ktime_before(ktime_get(), checknext))
1823 /* too early to fetch sepol status */
1827 /* define new sepol_checknext time */
1828 spin_lock(&imp_sec->ps_lock);
1829 imp_sec->ps_sepol_checknext = ktime_add(ktime_get(),
1830 ktime_set(send_sepol, 0));
1831 spin_unlock(&imp_sec->ps_lock);
1836 int sptlrpc_get_sepol(struct ptlrpc_request *req)
1838 struct ptlrpc_sec *imp_sec = req->rq_import->imp_sec;
1843 (req->rq_sepol)[0] = '\0';
1845 #ifndef HAVE_SELINUX
1846 if (unlikely(send_sepol != 0))
1847 CDEBUG(D_SEC, "Client cannot report SELinux status, "
1848 "it was not built against libselinux.\n");
1852 if (send_sepol == 0 || !selinux_is_enabled())
1855 if (imp_sec == NULL)
1858 /* Retrieve SELinux status info */
1859 if (sptlrpc_sepol_needs_check(imp_sec))
1860 rc = sepol_helper(req->rq_import);
1861 if (likely(rc == 0)) {
1862 spin_lock(&imp_sec->ps_lock);
1863 memcpy(req->rq_sepol, imp_sec->ps_sepol,
1864 sizeof(req->rq_sepol));
1865 spin_unlock(&imp_sec->ps_lock);
1870 EXPORT_SYMBOL(sptlrpc_get_sepol);
1872 /****************************************
1873 * server side security *
1874 ****************************************/
1876 static int flavor_allowed(struct sptlrpc_flavor *exp,
1877 struct ptlrpc_request *req)
1879 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1881 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1884 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1885 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1886 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1887 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1893 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1896 * Given an export \a exp, check whether the flavor of incoming \a req
1897 * is allowed by the export \a exp. Main logic is about taking care of
1898 * changing configurations. Return 0 means success.
1900 int sptlrpc_target_export_check(struct obd_export *exp,
1901 struct ptlrpc_request *req)
1903 struct sptlrpc_flavor flavor;
1908 /* client side export has no imp_reverse, skip
1909 * FIXME maybe we should check flavor this as well??? */
1910 if (exp->exp_imp_reverse == NULL)
1913 /* don't care about ctx fini rpc */
1914 if (req->rq_ctx_fini)
1917 spin_lock(&exp->exp_lock);
1919 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1920 * the first req with the new flavor, then treat it as current flavor,
1921 * adapt reverse sec according to it.
1922 * note the first rpc with new flavor might not be with root ctx, in
1923 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1924 if (unlikely(exp->exp_flvr_changed) &&
1925 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1926 /* make the new flavor as "current", and old ones as
1927 * about-to-expire */
1928 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1929 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1930 flavor = exp->exp_flvr_old[1];
1931 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1932 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1933 exp->exp_flvr_old[0] = exp->exp_flvr;
1934 exp->exp_flvr_expire[0] = ktime_get_real_seconds() +
1935 EXP_FLVR_UPDATE_EXPIRE;
1936 exp->exp_flvr = flavor;
1938 /* flavor change finished */
1939 exp->exp_flvr_changed = 0;
1940 LASSERT(exp->exp_flvr_adapt == 1);
1942 /* if it's gss, we only interested in root ctx init */
1943 if (req->rq_auth_gss &&
1944 !(req->rq_ctx_init &&
1945 (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1946 req->rq_auth_usr_ost))) {
1947 spin_unlock(&exp->exp_lock);
1948 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1949 req->rq_auth_gss, req->rq_ctx_init,
1950 req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1951 req->rq_auth_usr_ost);
1955 exp->exp_flvr_adapt = 0;
1956 spin_unlock(&exp->exp_lock);
1958 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1959 req->rq_svc_ctx, &flavor);
1962 /* if it equals to the current flavor, we accept it, but need to
1963 * dealing with reverse sec/ctx */
1964 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1965 /* most cases should return here, we only interested in
1966 * gss root ctx init */
1967 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1968 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1969 !req->rq_auth_usr_ost)) {
1970 spin_unlock(&exp->exp_lock);
1974 /* if flavor just changed, we should not proceed, just leave
1975 * it and current flavor will be discovered and replaced
1976 * shortly, and let _this_ rpc pass through */
1977 if (exp->exp_flvr_changed) {
1978 LASSERT(exp->exp_flvr_adapt);
1979 spin_unlock(&exp->exp_lock);
1983 if (exp->exp_flvr_adapt) {
1984 exp->exp_flvr_adapt = 0;
1985 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1986 exp, exp->exp_flvr.sf_rpc,
1987 exp->exp_flvr_old[0].sf_rpc,
1988 exp->exp_flvr_old[1].sf_rpc);
1989 flavor = exp->exp_flvr;
1990 spin_unlock(&exp->exp_lock);
1992 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1996 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1997 "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1998 exp->exp_flvr_old[0].sf_rpc,
1999 exp->exp_flvr_old[1].sf_rpc);
2000 spin_unlock(&exp->exp_lock);
2002 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
2007 if (exp->exp_flvr_expire[0]) {
2008 if (exp->exp_flvr_expire[0] >= ktime_get_real_seconds()) {
2009 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
2010 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the middle one (%lld)\n", exp,
2011 exp->exp_flvr.sf_rpc,
2012 exp->exp_flvr_old[0].sf_rpc,
2013 exp->exp_flvr_old[1].sf_rpc,
2014 (s64)(exp->exp_flvr_expire[0] -
2015 ktime_get_real_seconds()));
2016 spin_unlock(&exp->exp_lock);
2020 CDEBUG(D_SEC, "mark middle expired\n");
2021 exp->exp_flvr_expire[0] = 0;
2023 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
2024 exp->exp_flvr.sf_rpc,
2025 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2026 req->rq_flvr.sf_rpc);
2029 /* now it doesn't match the current flavor, the only chance we can
2030 * accept it is match the old flavors which is not expired. */
2031 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
2032 if (exp->exp_flvr_expire[1] >= ktime_get_real_seconds()) {
2033 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
2034 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the oldest one (%lld)\n",
2036 exp->exp_flvr.sf_rpc,
2037 exp->exp_flvr_old[0].sf_rpc,
2038 exp->exp_flvr_old[1].sf_rpc,
2039 (s64)(exp->exp_flvr_expire[1] -
2040 ktime_get_real_seconds()));
2041 spin_unlock(&exp->exp_lock);
2045 CDEBUG(D_SEC, "mark oldest expired\n");
2046 exp->exp_flvr_expire[1] = 0;
2048 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
2049 exp, exp->exp_flvr.sf_rpc,
2050 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2051 req->rq_flvr.sf_rpc);
2053 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
2054 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
2055 exp->exp_flvr_old[1].sf_rpc);
2058 spin_unlock(&exp->exp_lock);
2060 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with unauthorized flavor %x, expect %x|%x(%+lld)|%x(%+lld)\n",
2061 exp, exp->exp_obd->obd_name,
2062 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
2063 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
2064 req->rq_flvr.sf_rpc,
2065 exp->exp_flvr.sf_rpc,
2066 exp->exp_flvr_old[0].sf_rpc,
2067 exp->exp_flvr_expire[0] ?
2068 (s64)(exp->exp_flvr_expire[0] - ktime_get_real_seconds()) : 0,
2069 exp->exp_flvr_old[1].sf_rpc,
2070 exp->exp_flvr_expire[1] ?
2071 (s64)(exp->exp_flvr_expire[1] - ktime_get_real_seconds()) : 0);
2074 EXPORT_SYMBOL(sptlrpc_target_export_check);
2076 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
2077 struct sptlrpc_rule_set *rset)
2079 struct obd_export *exp;
2080 struct sptlrpc_flavor new_flvr;
2084 spin_lock(&obd->obd_dev_lock);
2086 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
2087 if (exp->exp_connection == NULL)
2090 /* note if this export had just been updated flavor
2091 * (exp_flvr_changed == 1), this will override the
2093 spin_lock(&exp->exp_lock);
2094 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
2095 exp->exp_connection->c_peer.nid,
2097 if (exp->exp_flvr_changed ||
2098 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
2099 exp->exp_flvr_old[1] = new_flvr;
2100 exp->exp_flvr_expire[1] = 0;
2101 exp->exp_flvr_changed = 1;
2102 exp->exp_flvr_adapt = 1;
2104 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
2105 exp, sptlrpc_part2name(exp->exp_sp_peer),
2106 exp->exp_flvr.sf_rpc,
2107 exp->exp_flvr_old[1].sf_rpc);
2109 spin_unlock(&exp->exp_lock);
2112 spin_unlock(&obd->obd_dev_lock);
2114 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
2116 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
2118 /* peer's claim is unreliable unless gss is being used */
2119 if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
2122 switch (req->rq_sp_from) {
2124 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
2125 DEBUG_REQ(D_ERROR, req, "faked source CLI");
2126 svc_rc = SECSVC_DROP;
2130 if (!req->rq_auth_usr_mdt) {
2131 DEBUG_REQ(D_ERROR, req, "faked source MDT");
2132 svc_rc = SECSVC_DROP;
2136 if (!req->rq_auth_usr_ost) {
2137 DEBUG_REQ(D_ERROR, req, "faked source OST");
2138 svc_rc = SECSVC_DROP;
2143 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2144 !req->rq_auth_usr_ost) {
2145 DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
2146 svc_rc = SECSVC_DROP;
2151 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2152 svc_rc = SECSVC_DROP;
2159 * Used by ptlrpc server, to perform transformation upon request message of
2160 * incoming \a req. This must be the first thing to do with an incoming
2161 * request in ptlrpc layer.
2163 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2164 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2165 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2166 * reply message has been prepared.
2167 * \retval SECSVC_DROP failed, this request should be dropped.
2169 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2171 struct ptlrpc_sec_policy *policy;
2172 struct lustre_msg *msg = req->rq_reqbuf;
2177 LASSERT(req->rq_reqmsg == NULL);
2178 LASSERT(req->rq_repmsg == NULL);
2179 LASSERT(req->rq_svc_ctx == NULL);
2181 req->rq_req_swab_mask = 0;
2183 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2186 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
2190 CERROR("error unpacking request from %s x%llu\n",
2191 libcfs_id2str(req->rq_peer), req->rq_xid);
2192 RETURN(SECSVC_DROP);
2195 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2196 req->rq_sp_from = LUSTRE_SP_ANY;
2197 req->rq_auth_uid = -1; /* set to INVALID_UID */
2198 req->rq_auth_mapped_uid = -1;
2200 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2202 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2203 RETURN(SECSVC_DROP);
2206 LASSERT(policy->sp_sops->accept);
2207 rc = policy->sp_sops->accept(req);
2208 sptlrpc_policy_put(policy);
2209 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2210 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2213 * if it's not null flavor (which means embedded packing msg),
2214 * reset the swab mask for the comming inner msg unpacking.
2216 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2217 req->rq_req_swab_mask = 0;
2219 /* sanity check for the request source */
2220 rc = sptlrpc_svc_check_from(req, rc);
2225 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2226 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2227 * a buffer of \a msglen size.
2229 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2231 struct ptlrpc_sec_policy *policy;
2232 struct ptlrpc_reply_state *rs;
2236 LASSERT(req->rq_svc_ctx);
2237 LASSERT(req->rq_svc_ctx->sc_policy);
2239 policy = req->rq_svc_ctx->sc_policy;
2240 LASSERT(policy->sp_sops->alloc_rs);
2242 rc = policy->sp_sops->alloc_rs(req, msglen);
2243 if (unlikely(rc == -ENOMEM)) {
2244 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
2245 if (svcpt->scp_service->srv_max_reply_size <
2246 msglen + sizeof(struct ptlrpc_reply_state)) {
2247 /* Just return failure if the size is too big */
2248 CERROR("size of message is too big (%zd), %d allowed\n",
2249 msglen + sizeof(struct ptlrpc_reply_state),
2250 svcpt->scp_service->srv_max_reply_size);
2254 /* failed alloc, try emergency pool */
2255 rs = lustre_get_emerg_rs(svcpt);
2259 req->rq_reply_state = rs;
2260 rc = policy->sp_sops->alloc_rs(req, msglen);
2262 lustre_put_emerg_rs(rs);
2263 req->rq_reply_state = NULL;
2268 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2274 * Used by ptlrpc server, to perform transformation upon reply message.
2276 * \post req->rq_reply_off is set to approriate server-controlled reply offset.
2277 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2279 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2281 struct ptlrpc_sec_policy *policy;
2285 LASSERT(req->rq_svc_ctx);
2286 LASSERT(req->rq_svc_ctx->sc_policy);
2288 policy = req->rq_svc_ctx->sc_policy;
2289 LASSERT(policy->sp_sops->authorize);
2291 rc = policy->sp_sops->authorize(req);
2292 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2298 * Used by ptlrpc server, to free reply_state.
2300 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2302 struct ptlrpc_sec_policy *policy;
2303 unsigned int prealloc;
2306 LASSERT(rs->rs_svc_ctx);
2307 LASSERT(rs->rs_svc_ctx->sc_policy);
2309 policy = rs->rs_svc_ctx->sc_policy;
2310 LASSERT(policy->sp_sops->free_rs);
2312 prealloc = rs->rs_prealloc;
2313 policy->sp_sops->free_rs(rs);
2316 lustre_put_emerg_rs(rs);
2320 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2322 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2325 atomic_inc(&ctx->sc_refcount);
2328 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2330 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2335 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2336 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2337 if (ctx->sc_policy->sp_sops->free_ctx)
2338 ctx->sc_policy->sp_sops->free_ctx(ctx);
2340 req->rq_svc_ctx = NULL;
2343 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2345 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2350 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2351 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2352 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2354 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2356 /****************************************
2358 ****************************************/
2361 * Perform transformation upon bulk data pointed by \a desc. This is called
2362 * before transforming the request message.
2364 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2365 struct ptlrpc_bulk_desc *desc)
2367 struct ptlrpc_cli_ctx *ctx;
2369 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2371 if (!req->rq_pack_bulk)
2374 ctx = req->rq_cli_ctx;
2375 if (ctx->cc_ops->wrap_bulk)
2376 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2379 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2382 * This is called after unwrap the reply message.
2383 * return nob of actual plain text size received, or error code.
2385 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2386 struct ptlrpc_bulk_desc *desc,
2389 struct ptlrpc_cli_ctx *ctx;
2392 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2394 if (!req->rq_pack_bulk)
2395 return desc->bd_nob_transferred;
2397 ctx = req->rq_cli_ctx;
2398 if (ctx->cc_ops->unwrap_bulk) {
2399 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2403 return desc->bd_nob_transferred;
2405 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2408 * This is called after unwrap the reply message.
2409 * return 0 for success or error code.
2411 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2412 struct ptlrpc_bulk_desc *desc)
2414 struct ptlrpc_cli_ctx *ctx;
2417 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2419 if (!req->rq_pack_bulk)
2422 ctx = req->rq_cli_ctx;
2423 if (ctx->cc_ops->unwrap_bulk) {
2424 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2430 * if everything is going right, nob should equals to nob_transferred.
2431 * in case of privacy mode, nob_transferred needs to be adjusted.
2433 if (desc->bd_nob != desc->bd_nob_transferred) {
2434 CERROR("nob %d doesn't match transferred nob %d\n",
2435 desc->bd_nob, desc->bd_nob_transferred);
2441 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2443 #ifdef HAVE_SERVER_SUPPORT
2445 * Performe transformation upon outgoing bulk read.
2447 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2448 struct ptlrpc_bulk_desc *desc)
2450 struct ptlrpc_svc_ctx *ctx;
2452 LASSERT(req->rq_bulk_read);
2454 if (!req->rq_pack_bulk)
2457 ctx = req->rq_svc_ctx;
2458 if (ctx->sc_policy->sp_sops->wrap_bulk)
2459 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2463 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2466 * Performe transformation upon incoming bulk write.
2468 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2469 struct ptlrpc_bulk_desc *desc)
2471 struct ptlrpc_svc_ctx *ctx;
2474 LASSERT(req->rq_bulk_write);
2477 * if it's in privacy mode, transferred should >= expected; otherwise
2478 * transferred should == expected.
2480 if (desc->bd_nob_transferred < desc->bd_nob ||
2481 (desc->bd_nob_transferred > desc->bd_nob &&
2482 SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2483 SPTLRPC_BULK_SVC_PRIV)) {
2484 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2485 desc->bd_nob_transferred, desc->bd_nob);
2489 if (!req->rq_pack_bulk)
2492 ctx = req->rq_svc_ctx;
2493 if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2494 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2496 CERROR("error unwrap bulk: %d\n", rc);
2499 /* return 0 to allow reply be sent */
2502 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2505 * Prepare buffers for incoming bulk write.
2507 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2508 struct ptlrpc_bulk_desc *desc)
2510 struct ptlrpc_svc_ctx *ctx;
2512 LASSERT(req->rq_bulk_write);
2514 if (!req->rq_pack_bulk)
2517 ctx = req->rq_svc_ctx;
2518 if (ctx->sc_policy->sp_sops->prep_bulk)
2519 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2523 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2525 #endif /* HAVE_SERVER_SUPPORT */
2527 /****************************************
2528 * user descriptor helpers *
2529 ****************************************/
2531 int sptlrpc_current_user_desc_size(void)
2535 ngroups = current_ngroups;
2537 if (ngroups > LUSTRE_MAX_GROUPS)
2538 ngroups = LUSTRE_MAX_GROUPS;
2539 return sptlrpc_user_desc_size(ngroups);
2541 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2543 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2545 struct ptlrpc_user_desc *pud;
2547 pud = lustre_msg_buf(msg, offset, 0);
2549 pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2550 pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2551 pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2552 pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2553 pud->pud_cap = cfs_curproc_cap_pack();
2554 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2557 if (pud->pud_ngroups > current_ngroups)
2558 pud->pud_ngroups = current_ngroups;
2559 #ifdef HAVE_GROUP_INFO_GID
2560 memcpy(pud->pud_groups, current_cred()->group_info->gid,
2561 pud->pud_ngroups * sizeof(__u32));
2562 #else /* !HAVE_GROUP_INFO_GID */
2563 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2564 pud->pud_ngroups * sizeof(__u32));
2565 #endif /* HAVE_GROUP_INFO_GID */
2566 task_unlock(current);
2570 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2572 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2574 struct ptlrpc_user_desc *pud;
2577 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2582 __swab32s(&pud->pud_uid);
2583 __swab32s(&pud->pud_gid);
2584 __swab32s(&pud->pud_fsuid);
2585 __swab32s(&pud->pud_fsgid);
2586 __swab32s(&pud->pud_cap);
2587 __swab32s(&pud->pud_ngroups);
2590 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2591 CERROR("%u groups is too large\n", pud->pud_ngroups);
2595 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2596 msg->lm_buflens[offset]) {
2597 CERROR("%u groups are claimed but bufsize only %u\n",
2598 pud->pud_ngroups, msg->lm_buflens[offset]);
2603 for (i = 0; i < pud->pud_ngroups; i++)
2604 __swab32s(&pud->pud_groups[i]);
2609 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2611 /****************************************
2613 ****************************************/
2615 const char * sec2target_str(struct ptlrpc_sec *sec)
2617 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2619 if (sec_is_reverse(sec))
2621 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2623 EXPORT_SYMBOL(sec2target_str);
2626 * return true if the bulk data is protected
2628 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2630 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2631 case SPTLRPC_BULK_SVC_INTG:
2632 case SPTLRPC_BULK_SVC_PRIV:
2638 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2640 /****************************************
2641 * crypto API helper/alloc blkciper *
2642 ****************************************/
2644 /****************************************
2645 * initialize/finalize *
2646 ****************************************/
2648 int sptlrpc_init(void)
2652 rwlock_init(&policy_lock);
2654 rc = sptlrpc_gc_init();
2658 rc = sptlrpc_conf_init();
2662 rc = sptlrpc_enc_pool_init();
2666 rc = sptlrpc_null_init();
2670 rc = sptlrpc_plain_init();
2674 rc = sptlrpc_lproc_init();
2681 sptlrpc_plain_fini();
2683 sptlrpc_null_fini();
2685 sptlrpc_enc_pool_fini();
2687 sptlrpc_conf_fini();
2694 void sptlrpc_fini(void)
2696 sptlrpc_lproc_fini();
2697 sptlrpc_plain_fini();
2698 sptlrpc_null_fini();
2699 sptlrpc_enc_pool_fini();
2700 sptlrpc_conf_fini();