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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/sec_plain.c
38 * Author: Eric Mei <ericm@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_SEC
44 #include <liblustre.h>
47 #include <obd_support.h>
48 #include <obd_cksum.h>
49 #include <obd_class.h>
50 #include <lustre_net.h>
51 #include <lustre_sec.h>
52 #include "ptlrpc_internal.h"
55 struct ptlrpc_sec pls_base;
57 struct ptlrpc_cli_ctx *pls_ctx;
60 static inline struct plain_sec *sec2plsec(struct ptlrpc_sec *sec)
62 return container_of(sec, struct plain_sec, pls_base);
65 static struct ptlrpc_sec_policy plain_policy;
66 static struct ptlrpc_ctx_ops plain_ctx_ops;
67 static struct ptlrpc_svc_ctx plain_svc_ctx;
69 static unsigned int plain_at_offset;
72 * for simplicity, plain policy rpc use fixed layout.
74 #define PLAIN_PACK_SEGMENTS (4)
76 #define PLAIN_PACK_HDR_OFF (0)
77 #define PLAIN_PACK_MSG_OFF (1)
78 #define PLAIN_PACK_USER_OFF (2)
79 #define PLAIN_PACK_BULK_OFF (3)
81 #define PLAIN_FL_USER (0x01)
82 #define PLAIN_FL_BULK (0x02)
87 __u8 ph_sp; /* source */
88 __u8 ph_bulk_hash_alg; /* complete flavor desc */
92 struct plain_bulk_token {
96 #define PLAIN_BSD_SIZE \
97 (sizeof(struct ptlrpc_bulk_sec_desc) + sizeof(struct plain_bulk_token))
99 /****************************************
100 * bulk checksum helpers *
101 ****************************************/
103 static int plain_unpack_bsd(struct lustre_msg *msg, int swabbed)
105 struct ptlrpc_bulk_sec_desc *bsd;
107 if (bulk_sec_desc_unpack(msg, PLAIN_PACK_BULK_OFF, swabbed))
110 bsd = lustre_msg_buf(msg, PLAIN_PACK_BULK_OFF, PLAIN_BSD_SIZE);
112 CERROR("bulk sec desc has short size %d\n",
113 lustre_msg_buflen(msg, PLAIN_PACK_BULK_OFF));
117 if (bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
118 bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG) {
119 CERROR("invalid bulk svc %u\n", bsd->bsd_svc);
126 static int plain_generate_bulk_csum(struct ptlrpc_bulk_desc *desc,
128 struct plain_bulk_token *token)
130 if (hash_alg == BULK_HASH_ALG_NULL)
133 memset(token->pbt_hash, 0, sizeof(token->pbt_hash));
134 return sptlrpc_get_bulk_checksum(desc, hash_alg, token->pbt_hash,
135 sizeof(token->pbt_hash));
138 static int plain_verify_bulk_csum(struct ptlrpc_bulk_desc *desc,
140 struct plain_bulk_token *tokenr)
142 struct plain_bulk_token tokenv;
145 if (hash_alg == BULK_HASH_ALG_NULL)
148 memset(&tokenv.pbt_hash, 0, sizeof(tokenv.pbt_hash));
149 rc = sptlrpc_get_bulk_checksum(desc, hash_alg, tokenv.pbt_hash,
150 sizeof(tokenv.pbt_hash));
154 if (memcmp(tokenr->pbt_hash, tokenv.pbt_hash, sizeof(tokenr->pbt_hash)))
160 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
165 for (i = 0; i < desc->bd_iov_count; i++) {
166 if (desc->bd_iov[i].kiov_len == 0)
169 ptr = kmap(desc->bd_iov[i].kiov_page);
170 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
172 kunmap(desc->bd_iov[i].kiov_page);
177 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
181 for (i = 0; i < desc->bd_iov_count; i++) {
182 if (desc->bd_iov[i].iov_len == 0)
185 ((char *)desc->bd_iov[i].iov_base)[i] ^= 0x1;
189 #endif /* __KERNEL__ */
191 /****************************************
193 ****************************************/
196 int plain_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
198 /* should never reach here */
204 int plain_ctx_validate(struct ptlrpc_cli_ctx *ctx)
210 int plain_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
212 struct lustre_msg *msg = req->rq_reqbuf;
213 struct plain_header *phdr;
216 msg->lm_secflvr = req->rq_flvr.sf_rpc;
218 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, 0);
221 phdr->ph_sp = ctx->cc_sec->ps_part;
222 phdr->ph_bulk_hash_alg = req->rq_flvr.u_bulk.hash.hash_alg;
224 if (req->rq_pack_udesc)
225 phdr->ph_flags |= PLAIN_FL_USER;
226 if (req->rq_pack_bulk)
227 phdr->ph_flags |= PLAIN_FL_BULK;
229 req->rq_reqdata_len = lustre_msg_size_v2(msg->lm_bufcount,
235 int plain_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
237 struct lustre_msg *msg = req->rq_repdata;
238 struct plain_header *phdr;
243 if (msg->lm_bufcount != PLAIN_PACK_SEGMENTS) {
244 CERROR("unexpected reply buf count %u\n", msg->lm_bufcount);
248 swabbed = ptlrpc_rep_need_swab(req);
250 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
252 CERROR("missing plain header\n");
256 if (phdr->ph_ver != 0) {
257 CERROR("Invalid header version\n");
261 /* expect no user desc in reply */
262 if (phdr->ph_flags & PLAIN_FL_USER) {
263 CERROR("Unexpected udesc flag in reply\n");
267 if (phdr->ph_bulk_hash_alg != req->rq_flvr.u_bulk.hash.hash_alg) {
268 CERROR("reply bulk flavor %u != %u\n", phdr->ph_bulk_hash_alg,
269 req->rq_flvr.u_bulk.hash.hash_alg);
273 if (unlikely(req->rq_early)) {
274 unsigned int hsize = 4;
276 cfs_crypto_hash_digest(CFS_HASH_ALG_CRC32,
277 lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0),
278 lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF),
279 NULL, 0, (unsigned char *)&cksum, &hsize);
280 if (cksum != msg->lm_cksum) {
282 "early reply checksum mismatch: %08x != %08x\n",
283 cpu_to_le32(cksum), msg->lm_cksum);
287 /* whether we sent with bulk or not, we expect the same
288 * in reply, except for early reply */
289 if (!req->rq_early &&
290 !equi(req->rq_pack_bulk == 1,
291 phdr->ph_flags & PLAIN_FL_BULK)) {
292 CERROR("%s bulk checksum in reply\n",
293 req->rq_pack_bulk ? "Missing" : "Unexpected");
297 if (phdr->ph_flags & PLAIN_FL_BULK) {
298 if (plain_unpack_bsd(msg, swabbed))
303 req->rq_repmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
304 req->rq_replen = lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF);
309 int plain_cli_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
310 struct ptlrpc_request *req,
311 struct ptlrpc_bulk_desc *desc)
313 struct ptlrpc_bulk_sec_desc *bsd;
314 struct plain_bulk_token *token;
317 LASSERT(req->rq_pack_bulk);
318 LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
320 bsd = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
321 token = (struct plain_bulk_token *) bsd->bsd_data;
323 bsd->bsd_version = 0;
325 bsd->bsd_type = SPTLRPC_BULK_DEFAULT;
326 bsd->bsd_svc = SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc);
328 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_NULL)
331 if (req->rq_bulk_read)
334 rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
337 CERROR("bulk write: failed to compute checksum: %d\n", rc);
340 * for sending we only compute the wrong checksum instead
341 * of corrupting the data so it is still correct on a redo
343 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_SEND) &&
344 req->rq_flvr.u_bulk.hash.hash_alg != BULK_HASH_ALG_NULL)
345 token->pbt_hash[0] ^= 0x1;
352 int plain_cli_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
353 struct ptlrpc_request *req,
354 struct ptlrpc_bulk_desc *desc)
356 struct ptlrpc_bulk_sec_desc *bsdv;
357 struct plain_bulk_token *tokenv;
363 LASSERT(req->rq_pack_bulk);
364 LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
365 LASSERT(req->rq_repdata->lm_bufcount == PLAIN_PACK_SEGMENTS);
367 bsdv = lustre_msg_buf(req->rq_repdata, PLAIN_PACK_BULK_OFF, 0);
368 tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
370 if (req->rq_bulk_write) {
371 if (bsdv->bsd_flags & BSD_FL_ERR)
377 /* fix the actual data size */
378 for (i = 0, nob = 0; i < desc->bd_iov_count; i++) {
379 if (desc->bd_iov[i].kiov_len + nob > desc->bd_nob_transferred) {
380 desc->bd_iov[i].kiov_len =
381 desc->bd_nob_transferred - nob;
383 nob += desc->bd_iov[i].kiov_len;
387 rc = plain_verify_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
390 CERROR("bulk read: client verify failed: %d\n", rc);
395 /****************************************
397 ****************************************/
400 struct ptlrpc_cli_ctx *plain_sec_install_ctx(struct plain_sec *plsec)
402 struct ptlrpc_cli_ctx *ctx, *ctx_new;
404 OBD_ALLOC_PTR(ctx_new);
406 write_lock(&plsec->pls_lock);
408 ctx = plsec->pls_ctx;
410 atomic_inc(&ctx->cc_refcount);
413 OBD_FREE_PTR(ctx_new);
414 } else if (ctx_new) {
417 atomic_set(&ctx->cc_refcount, 1); /* for cache */
418 ctx->cc_sec = &plsec->pls_base;
419 ctx->cc_ops = &plain_ctx_ops;
421 ctx->cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_UPTODATE;
422 ctx->cc_vcred.vc_uid = 0;
423 spin_lock_init(&ctx->cc_lock);
424 CFS_INIT_LIST_HEAD(&ctx->cc_req_list);
425 CFS_INIT_LIST_HEAD(&ctx->cc_gc_chain);
427 plsec->pls_ctx = ctx;
428 atomic_inc(&plsec->pls_base.ps_nctx);
429 atomic_inc(&plsec->pls_base.ps_refcount);
431 atomic_inc(&ctx->cc_refcount); /* for caller */
434 write_unlock(&plsec->pls_lock);
440 void plain_destroy_sec(struct ptlrpc_sec *sec)
442 struct plain_sec *plsec = sec2plsec(sec);
445 LASSERT(sec->ps_policy == &plain_policy);
446 LASSERT(sec->ps_import);
447 LASSERT(atomic_read(&sec->ps_refcount) == 0);
448 LASSERT(atomic_read(&sec->ps_nctx) == 0);
449 LASSERT(plsec->pls_ctx == NULL);
451 class_import_put(sec->ps_import);
458 void plain_kill_sec(struct ptlrpc_sec *sec)
464 struct ptlrpc_sec *plain_create_sec(struct obd_import *imp,
465 struct ptlrpc_svc_ctx *svc_ctx,
466 struct sptlrpc_flavor *sf)
468 struct plain_sec *plsec;
469 struct ptlrpc_sec *sec;
470 struct ptlrpc_cli_ctx *ctx;
473 LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN);
475 OBD_ALLOC_PTR(plsec);
480 * initialize plain_sec
482 rwlock_init(&plsec->pls_lock);
483 plsec->pls_ctx = NULL;
485 sec = &plsec->pls_base;
486 sec->ps_policy = &plain_policy;
487 atomic_set(&sec->ps_refcount, 0);
488 atomic_set(&sec->ps_nctx, 0);
489 sec->ps_id = sptlrpc_get_next_secid();
490 sec->ps_import = class_import_get(imp);
492 spin_lock_init(&sec->ps_lock);
493 CFS_INIT_LIST_HEAD(&sec->ps_gc_list);
494 sec->ps_gc_interval = 0;
497 /* install ctx immediately if this is a reverse sec */
499 ctx = plain_sec_install_ctx(plsec);
501 plain_destroy_sec(sec);
504 sptlrpc_cli_ctx_put(ctx, 1);
511 struct ptlrpc_cli_ctx *plain_lookup_ctx(struct ptlrpc_sec *sec,
512 struct vfs_cred *vcred,
513 int create, int remove_dead)
515 struct plain_sec *plsec = sec2plsec(sec);
516 struct ptlrpc_cli_ctx *ctx;
519 read_lock(&plsec->pls_lock);
520 ctx = plsec->pls_ctx;
522 atomic_inc(&ctx->cc_refcount);
523 read_unlock(&plsec->pls_lock);
525 if (unlikely(ctx == NULL))
526 ctx = plain_sec_install_ctx(plsec);
532 void plain_release_ctx(struct ptlrpc_sec *sec,
533 struct ptlrpc_cli_ctx *ctx, int sync)
535 LASSERT(atomic_read(&sec->ps_refcount) > 0);
536 LASSERT(atomic_read(&sec->ps_nctx) > 0);
537 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
538 LASSERT(ctx->cc_sec == sec);
542 atomic_dec(&sec->ps_nctx);
543 sptlrpc_sec_put(sec);
547 int plain_flush_ctx_cache(struct ptlrpc_sec *sec,
548 uid_t uid, int grace, int force)
550 struct plain_sec *plsec = sec2plsec(sec);
551 struct ptlrpc_cli_ctx *ctx;
554 /* do nothing unless caller want to flush for 'all' */
558 write_lock(&plsec->pls_lock);
559 ctx = plsec->pls_ctx;
560 plsec->pls_ctx = NULL;
561 write_unlock(&plsec->pls_lock);
564 sptlrpc_cli_ctx_put(ctx, 1);
569 int plain_alloc_reqbuf(struct ptlrpc_sec *sec,
570 struct ptlrpc_request *req,
573 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
577 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
578 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
580 if (req->rq_pack_udesc)
581 buflens[PLAIN_PACK_USER_OFF] = sptlrpc_current_user_desc_size();
583 if (req->rq_pack_bulk) {
584 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
585 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
588 alloc_len = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
590 if (!req->rq_reqbuf) {
591 LASSERT(!req->rq_pool);
593 alloc_len = size_roundup_power2(alloc_len);
594 OBD_ALLOC_LARGE(req->rq_reqbuf, alloc_len);
598 req->rq_reqbuf_len = alloc_len;
600 LASSERT(req->rq_pool);
601 LASSERT(req->rq_reqbuf_len >= alloc_len);
602 memset(req->rq_reqbuf, 0, alloc_len);
605 lustre_init_msg_v2(req->rq_reqbuf, PLAIN_PACK_SEGMENTS, buflens, NULL);
606 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_MSG_OFF, 0);
608 if (req->rq_pack_udesc)
609 sptlrpc_pack_user_desc(req->rq_reqbuf, PLAIN_PACK_USER_OFF);
615 void plain_free_reqbuf(struct ptlrpc_sec *sec,
616 struct ptlrpc_request *req)
620 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
621 req->rq_reqbuf = NULL;
622 req->rq_reqbuf_len = 0;
628 int plain_alloc_repbuf(struct ptlrpc_sec *sec,
629 struct ptlrpc_request *req,
632 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
636 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
637 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
639 if (req->rq_pack_bulk) {
640 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
641 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
644 alloc_len = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
646 /* add space for early reply */
647 alloc_len += plain_at_offset;
649 alloc_len = size_roundup_power2(alloc_len);
651 OBD_ALLOC_LARGE(req->rq_repbuf, alloc_len);
655 req->rq_repbuf_len = alloc_len;
660 void plain_free_repbuf(struct ptlrpc_sec *sec,
661 struct ptlrpc_request *req)
664 OBD_FREE_LARGE(req->rq_repbuf, req->rq_repbuf_len);
665 req->rq_repbuf = NULL;
666 req->rq_repbuf_len = 0;
671 int plain_enlarge_reqbuf(struct ptlrpc_sec *sec,
672 struct ptlrpc_request *req,
673 int segment, int newsize)
676 int newmsg_size, newbuf_size;
680 LASSERT(req->rq_reqbuf);
681 LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
682 LASSERT(lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_MSG_OFF, 0) ==
685 /* compute new embedded msg size. */
686 oldsize = req->rq_reqmsg->lm_buflens[segment];
687 req->rq_reqmsg->lm_buflens[segment] = newsize;
688 newmsg_size = lustre_msg_size_v2(req->rq_reqmsg->lm_bufcount,
689 req->rq_reqmsg->lm_buflens);
690 req->rq_reqmsg->lm_buflens[segment] = oldsize;
692 /* compute new wrapper msg size. */
693 oldsize = req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF];
694 req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = newmsg_size;
695 newbuf_size = lustre_msg_size_v2(req->rq_reqbuf->lm_bufcount,
696 req->rq_reqbuf->lm_buflens);
697 req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = oldsize;
699 /* request from pool should always have enough buffer */
700 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
702 if (req->rq_reqbuf_len < newbuf_size) {
703 rc = ptlrpc_enlarge_req_buffer(req, newbuf_size);
708 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, PLAIN_PACK_MSG_OFF,
710 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
712 req->rq_reqlen = newmsg_size;
716 /****************************************
718 ****************************************/
720 static struct ptlrpc_svc_ctx plain_svc_ctx = {
721 .sc_refcount = ATOMIC_INIT(1),
722 .sc_policy = &plain_policy,
726 int plain_accept(struct ptlrpc_request *req)
728 struct lustre_msg *msg = req->rq_reqbuf;
729 struct plain_header *phdr;
733 LASSERT(SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) ==
734 SPTLRPC_POLICY_PLAIN);
736 if (SPTLRPC_FLVR_BASE(req->rq_flvr.sf_rpc) !=
737 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN) ||
738 SPTLRPC_FLVR_BULK_TYPE(req->rq_flvr.sf_rpc) !=
739 SPTLRPC_FLVR_BULK_TYPE(SPTLRPC_FLVR_PLAIN)) {
740 CERROR("Invalid rpc flavor %x\n", req->rq_flvr.sf_rpc);
744 if (msg->lm_bufcount < PLAIN_PACK_SEGMENTS) {
745 CERROR("unexpected request buf count %u\n", msg->lm_bufcount);
749 swabbed = ptlrpc_req_need_swab(req);
751 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
753 CERROR("missing plain header\n");
757 if (phdr->ph_ver != 0) {
758 CERROR("Invalid header version\n");
762 if (phdr->ph_bulk_hash_alg >= BULK_HASH_ALG_MAX) {
763 CERROR("invalid hash algorithm: %u\n", phdr->ph_bulk_hash_alg);
767 req->rq_sp_from = phdr->ph_sp;
768 req->rq_flvr.u_bulk.hash.hash_alg = phdr->ph_bulk_hash_alg;
770 if (phdr->ph_flags & PLAIN_FL_USER) {
771 if (sptlrpc_unpack_user_desc(msg, PLAIN_PACK_USER_OFF,
773 CERROR("Mal-formed user descriptor\n");
777 req->rq_pack_udesc = 1;
778 req->rq_user_desc = lustre_msg_buf(msg, PLAIN_PACK_USER_OFF, 0);
781 if (phdr->ph_flags & PLAIN_FL_BULK) {
782 if (plain_unpack_bsd(msg, swabbed))
785 req->rq_pack_bulk = 1;
788 req->rq_reqmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
789 req->rq_reqlen = msg->lm_buflens[PLAIN_PACK_MSG_OFF];
791 req->rq_svc_ctx = &plain_svc_ctx;
792 atomic_inc(&req->rq_svc_ctx->sc_refcount);
798 int plain_alloc_rs(struct ptlrpc_request *req, int msgsize)
800 struct ptlrpc_reply_state *rs;
801 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
802 int rs_size = sizeof(*rs);
805 LASSERT(msgsize % 8 == 0);
807 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
808 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
810 if (req->rq_pack_bulk && (req->rq_bulk_read || req->rq_bulk_write))
811 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
813 rs_size += lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
815 rs = req->rq_reply_state;
819 LASSERT(rs->rs_size >= rs_size);
821 OBD_ALLOC_LARGE(rs, rs_size);
825 rs->rs_size = rs_size;
828 rs->rs_svc_ctx = req->rq_svc_ctx;
829 atomic_inc(&req->rq_svc_ctx->sc_refcount);
830 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
831 rs->rs_repbuf_len = rs_size - sizeof(*rs);
833 lustre_init_msg_v2(rs->rs_repbuf, PLAIN_PACK_SEGMENTS, buflens, NULL);
834 rs->rs_msg = lustre_msg_buf_v2(rs->rs_repbuf, PLAIN_PACK_MSG_OFF, 0);
836 req->rq_reply_state = rs;
841 void plain_free_rs(struct ptlrpc_reply_state *rs)
845 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
846 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
848 if (!rs->rs_prealloc)
849 OBD_FREE_LARGE(rs, rs->rs_size);
854 int plain_authorize(struct ptlrpc_request *req)
856 struct ptlrpc_reply_state *rs = req->rq_reply_state;
857 struct lustre_msg_v2 *msg = rs->rs_repbuf;
858 struct plain_header *phdr;
865 if (req->rq_replen != msg->lm_buflens[PLAIN_PACK_MSG_OFF])
866 len = lustre_shrink_msg(msg, PLAIN_PACK_MSG_OFF,
869 len = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
871 msg->lm_secflvr = req->rq_flvr.sf_rpc;
873 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, 0);
876 phdr->ph_bulk_hash_alg = req->rq_flvr.u_bulk.hash.hash_alg;
878 if (req->rq_pack_bulk)
879 phdr->ph_flags |= PLAIN_FL_BULK;
881 rs->rs_repdata_len = len;
883 if (likely(req->rq_packed_final)) {
884 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
885 req->rq_reply_off = plain_at_offset;
887 req->rq_reply_off = 0;
889 unsigned int hsize = 4;
891 cfs_crypto_hash_digest(CFS_HASH_ALG_CRC32,
892 lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0),
893 lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF),
894 NULL, 0, (unsigned char *)&msg->lm_cksum, &hsize);
895 req->rq_reply_off = 0;
902 int plain_svc_unwrap_bulk(struct ptlrpc_request *req,
903 struct ptlrpc_bulk_desc *desc)
905 struct ptlrpc_reply_state *rs = req->rq_reply_state;
906 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
907 struct plain_bulk_token *tokenr;
910 LASSERT(req->rq_bulk_write);
911 LASSERT(req->rq_pack_bulk);
913 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
914 tokenr = (struct plain_bulk_token *) bsdr->bsd_data;
915 bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
917 bsdv->bsd_version = 0;
918 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
919 bsdv->bsd_svc = bsdr->bsd_svc;
922 if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
925 rc = plain_verify_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
928 bsdv->bsd_flags |= BSD_FL_ERR;
929 CERROR("bulk write: server verify failed: %d\n", rc);
936 int plain_svc_wrap_bulk(struct ptlrpc_request *req,
937 struct ptlrpc_bulk_desc *desc)
939 struct ptlrpc_reply_state *rs = req->rq_reply_state;
940 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
941 struct plain_bulk_token *tokenv;
944 LASSERT(req->rq_bulk_read);
945 LASSERT(req->rq_pack_bulk);
947 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
948 bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
949 tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
951 bsdv->bsd_version = 0;
952 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
953 bsdv->bsd_svc = bsdr->bsd_svc;
956 if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
959 rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
962 CERROR("bulk read: server failed to compute "
963 "checksum: %d\n", rc);
965 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_RECEIVE))
966 corrupt_bulk_data(desc);
972 static struct ptlrpc_ctx_ops plain_ctx_ops = {
973 .refresh = plain_ctx_refresh,
974 .validate = plain_ctx_validate,
975 .sign = plain_ctx_sign,
976 .verify = plain_ctx_verify,
977 .wrap_bulk = plain_cli_wrap_bulk,
978 .unwrap_bulk = plain_cli_unwrap_bulk,
981 static struct ptlrpc_sec_cops plain_sec_cops = {
982 .create_sec = plain_create_sec,
983 .destroy_sec = plain_destroy_sec,
984 .kill_sec = plain_kill_sec,
985 .lookup_ctx = plain_lookup_ctx,
986 .release_ctx = plain_release_ctx,
987 .flush_ctx_cache = plain_flush_ctx_cache,
988 .alloc_reqbuf = plain_alloc_reqbuf,
989 .free_reqbuf = plain_free_reqbuf,
990 .alloc_repbuf = plain_alloc_repbuf,
991 .free_repbuf = plain_free_repbuf,
992 .enlarge_reqbuf = plain_enlarge_reqbuf,
995 static struct ptlrpc_sec_sops plain_sec_sops = {
996 .accept = plain_accept,
997 .alloc_rs = plain_alloc_rs,
998 .authorize = plain_authorize,
999 .free_rs = plain_free_rs,
1000 .unwrap_bulk = plain_svc_unwrap_bulk,
1001 .wrap_bulk = plain_svc_wrap_bulk,
1004 static struct ptlrpc_sec_policy plain_policy = {
1005 .sp_owner = THIS_MODULE,
1007 .sp_policy = SPTLRPC_POLICY_PLAIN,
1008 .sp_cops = &plain_sec_cops,
1009 .sp_sops = &plain_sec_sops,
1012 int sptlrpc_plain_init(void)
1014 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
1017 buflens[PLAIN_PACK_MSG_OFF] = lustre_msg_early_size();
1018 plain_at_offset = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
1020 rc = sptlrpc_register_policy(&plain_policy);
1022 CERROR("failed to register: %d\n", rc);
1027 void sptlrpc_plain_fini(void)
1031 rc = sptlrpc_unregister_policy(&plain_policy);
1033 CERROR("cannot unregister: %d\n", rc);