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