1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_SEC
47 #include <liblustre.h>
50 #include <obd_support.h>
51 #include <obd_cksum.h>
52 #include <obd_class.h>
53 #include <lustre_net.h>
54 #include <lustre_sec.h>
57 struct ptlrpc_sec pls_base;
59 struct ptlrpc_cli_ctx *pls_ctx;
62 static inline struct plain_sec *sec2plsec(struct ptlrpc_sec *sec)
64 return container_of(sec, struct plain_sec, pls_base);
67 static struct ptlrpc_sec_policy plain_policy;
68 static struct ptlrpc_ctx_ops plain_ctx_ops;
69 static struct ptlrpc_svc_ctx plain_svc_ctx;
71 static unsigned int plain_at_offset;
74 * for simplicity, plain policy rpc use fixed layout.
76 #define PLAIN_PACK_SEGMENTS (4)
78 #define PLAIN_PACK_HDR_OFF (0)
79 #define PLAIN_PACK_MSG_OFF (1)
80 #define PLAIN_PACK_USER_OFF (2)
81 #define PLAIN_PACK_BULK_OFF (3)
83 #define PLAIN_FL_USER (0x01)
84 #define PLAIN_FL_BULK (0x02)
89 __u8 ph_sp; /* source */
90 __u8 ph_bulk_hash_alg; /* complete flavor desc */
94 struct plain_bulk_token {
98 #define PLAIN_BSD_SIZE \
99 (sizeof(struct ptlrpc_bulk_sec_desc) + sizeof(struct plain_bulk_token))
101 /****************************************
102 * bulk checksum helpers *
103 ****************************************/
105 static int plain_unpack_bsd(struct lustre_msg *msg)
107 struct ptlrpc_bulk_sec_desc *bsd;
109 if (bulk_sec_desc_unpack(msg, PLAIN_PACK_BULK_OFF))
112 bsd = lustre_msg_buf(msg, PLAIN_PACK_BULK_OFF, PLAIN_BSD_SIZE);
114 CERROR("bulk sec desc has short size %d\n",
115 lustre_msg_buflen(msg, PLAIN_PACK_BULK_OFF));
119 if (bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
120 bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG) {
121 CERROR("invalid bulk svc %u\n", bsd->bsd_svc);
128 static int plain_generate_bulk_csum(struct ptlrpc_bulk_desc *desc,
130 struct plain_bulk_token *token)
132 if (hash_alg == BULK_HASH_ALG_NULL)
135 memset(token->pbt_hash, 0, sizeof(token->pbt_hash));
136 return sptlrpc_get_bulk_checksum(desc, hash_alg, token->pbt_hash,
137 sizeof(token->pbt_hash));
140 static int plain_verify_bulk_csum(struct ptlrpc_bulk_desc *desc,
142 struct plain_bulk_token *tokenr)
144 struct plain_bulk_token tokenv;
147 if (hash_alg == BULK_HASH_ALG_NULL)
150 memset(&tokenv.pbt_hash, 0, sizeof(tokenv.pbt_hash));
151 rc = sptlrpc_get_bulk_checksum(desc, hash_alg, tokenv.pbt_hash,
152 sizeof(tokenv.pbt_hash));
156 if (memcmp(tokenr->pbt_hash, tokenv.pbt_hash, sizeof(tokenr->pbt_hash)))
162 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
167 for (i = 0; i < desc->bd_iov_count; i++) {
168 if (desc->bd_iov[i].kiov_len == 0)
171 ptr = cfs_kmap(desc->bd_iov[i].kiov_page);
172 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
174 cfs_kunmap(desc->bd_iov[i].kiov_page);
179 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
183 for (i = 0; i < desc->bd_iov_count; i++) {
184 if (desc->bd_iov[i].iov_len == 0)
187 ((char *)desc->bd_iov[i].iov_base)[i] ^= 0x1;
191 #endif /* __KERNEL__ */
193 /****************************************
195 ****************************************/
198 int plain_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
200 /* should never reach here */
206 int plain_ctx_validate(struct ptlrpc_cli_ctx *ctx)
212 int plain_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
214 struct lustre_msg *msg = req->rq_reqbuf;
215 struct plain_header *phdr;
218 msg->lm_secflvr = req->rq_flvr.sf_rpc;
220 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, 0);
223 phdr->ph_sp = ctx->cc_sec->ps_part;
224 phdr->ph_bulk_hash_alg = req->rq_flvr.u_bulk.hash.hash_alg;
226 if (req->rq_pack_udesc)
227 phdr->ph_flags |= PLAIN_FL_USER;
228 if (req->rq_pack_bulk)
229 phdr->ph_flags |= PLAIN_FL_BULK;
231 req->rq_reqdata_len = lustre_msg_size_v2(msg->lm_bufcount,
237 int plain_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
239 struct lustre_msg *msg = req->rq_repdata;
240 struct plain_header *phdr;
244 if (msg->lm_bufcount != PLAIN_PACK_SEGMENTS) {
245 CERROR("unexpected reply buf count %u\n", msg->lm_bufcount);
249 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
251 CERROR("missing plain header\n");
255 if (phdr->ph_ver != 0) {
256 CERROR("Invalid header version\n");
260 /* expect no user desc in reply */
261 if (phdr->ph_flags & PLAIN_FL_USER) {
262 CERROR("Unexpected udesc flag in reply\n");
266 if (phdr->ph_bulk_hash_alg != req->rq_flvr.u_bulk.hash.hash_alg) {
267 CERROR("reply bulk flavor %u != %u\n", phdr->ph_bulk_hash_alg,
268 req->rq_flvr.u_bulk.hash.hash_alg);
272 if (unlikely(req->rq_early)) {
273 cksum = crc32_le(!(__u32) 0,
274 lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0),
275 lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF));
276 if (cksum != msg->lm_cksum) {
277 CWARN("early reply checksum mismatch: %08x != %08x\n",
278 cpu_to_le32(cksum), msg->lm_cksum);
282 /* whether we sent with bulk or not, we expect the same
283 * in reply, except for early reply */
284 if (!req->rq_early &&
285 !equi(req->rq_pack_bulk == 1,
286 phdr->ph_flags & PLAIN_FL_BULK)) {
287 CERROR("%s bulk checksum in reply\n",
288 req->rq_pack_bulk ? "Missing" : "Unexpected");
292 if (phdr->ph_flags & PLAIN_FL_BULK) {
293 if (plain_unpack_bsd(msg))
298 req->rq_repmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
299 req->rq_replen = lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF);
304 int plain_cli_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
305 struct ptlrpc_request *req,
306 struct ptlrpc_bulk_desc *desc)
308 struct ptlrpc_bulk_sec_desc *bsd;
309 struct plain_bulk_token *token;
312 LASSERT(req->rq_pack_bulk);
313 LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
315 bsd = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
316 token = (struct plain_bulk_token *) bsd->bsd_data;
318 bsd->bsd_version = 0;
320 bsd->bsd_type = SPTLRPC_BULK_DEFAULT;
321 bsd->bsd_svc = SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc);
323 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_NULL)
326 if (req->rq_bulk_read)
329 rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
332 CERROR("bulk write: failed to compute checksum: %d\n", rc);
335 * for sending we only compute the wrong checksum instead
336 * of corrupting the data so it is still correct on a redo
338 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_SEND) &&
339 req->rq_flvr.u_bulk.hash.hash_alg != BULK_HASH_ALG_NULL)
340 token->pbt_hash[0] ^= 0x1;
347 int plain_cli_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
348 struct ptlrpc_request *req,
349 struct ptlrpc_bulk_desc *desc)
351 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
352 struct plain_bulk_token *tokenr, *tokenv;
358 LASSERT(req->rq_pack_bulk);
359 LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
360 LASSERT(req->rq_repdata->lm_bufcount == PLAIN_PACK_SEGMENTS);
362 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
363 tokenr = (struct plain_bulk_token *) bsdr->bsd_data;
364 bsdv = lustre_msg_buf(req->rq_repdata, PLAIN_PACK_BULK_OFF, 0);
365 tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
367 if (req->rq_bulk_write) {
368 if (bsdv->bsd_flags & BSD_FL_ERR)
374 /* fix the actual data size */
375 for (i = 0, nob = 0; i < desc->bd_iov_count; i++) {
376 if (desc->bd_iov[i].kiov_len + nob > desc->bd_nob_transferred) {
377 desc->bd_iov[i].kiov_len =
378 desc->bd_nob_transferred - nob;
380 nob += desc->bd_iov[i].kiov_len;
384 rc = plain_verify_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
387 CERROR("bulk read: client verify failed: %d\n", rc);
392 /****************************************
394 ****************************************/
397 struct ptlrpc_cli_ctx *plain_sec_install_ctx(struct plain_sec *plsec)
399 struct ptlrpc_cli_ctx *ctx, *ctx_new;
401 OBD_ALLOC_PTR(ctx_new);
403 write_lock(&plsec->pls_lock);
405 ctx = plsec->pls_ctx;
407 atomic_inc(&ctx->cc_refcount);
410 OBD_FREE_PTR(ctx_new);
411 } else if (ctx_new) {
414 atomic_set(&ctx->cc_refcount, 1); /* for cache */
415 ctx->cc_sec = &plsec->pls_base;
416 ctx->cc_ops = &plain_ctx_ops;
418 ctx->cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_UPTODATE;
419 ctx->cc_vcred.vc_uid = 0;
420 spin_lock_init(&ctx->cc_lock);
421 CFS_INIT_LIST_HEAD(&ctx->cc_req_list);
422 CFS_INIT_LIST_HEAD(&ctx->cc_gc_chain);
424 plsec->pls_ctx = ctx;
425 atomic_inc(&plsec->pls_base.ps_nctx);
426 atomic_inc(&plsec->pls_base.ps_refcount);
428 atomic_inc(&ctx->cc_refcount); /* for caller */
431 write_unlock(&plsec->pls_lock);
437 void plain_destroy_sec(struct ptlrpc_sec *sec)
439 struct plain_sec *plsec = sec2plsec(sec);
442 LASSERT(sec->ps_policy == &plain_policy);
443 LASSERT(sec->ps_import);
444 LASSERT(atomic_read(&sec->ps_refcount) == 0);
445 LASSERT(atomic_read(&sec->ps_nctx) == 0);
446 LASSERT(plsec->pls_ctx == NULL);
448 class_import_put(sec->ps_import);
455 void plain_kill_sec(struct ptlrpc_sec *sec)
461 struct ptlrpc_sec *plain_create_sec(struct obd_import *imp,
462 struct ptlrpc_svc_ctx *svc_ctx,
463 struct sptlrpc_flavor *sf)
465 struct plain_sec *plsec;
466 struct ptlrpc_sec *sec;
467 struct ptlrpc_cli_ctx *ctx;
470 LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN);
472 OBD_ALLOC_PTR(plsec);
477 * initialize plain_sec
479 rwlock_init(&plsec->pls_lock);
480 plsec->pls_ctx = NULL;
482 sec = &plsec->pls_base;
483 sec->ps_policy = &plain_policy;
484 atomic_set(&sec->ps_refcount, 0);
485 atomic_set(&sec->ps_nctx, 0);
486 sec->ps_id = sptlrpc_get_next_secid();
487 sec->ps_import = class_import_get(imp);
489 spin_lock_init(&sec->ps_lock);
490 CFS_INIT_LIST_HEAD(&sec->ps_gc_list);
491 sec->ps_gc_interval = 0;
494 /* install ctx immediately if this is a reverse sec */
496 ctx = plain_sec_install_ctx(plsec);
498 plain_destroy_sec(sec);
501 sptlrpc_cli_ctx_put(ctx, 1);
508 struct ptlrpc_cli_ctx *plain_lookup_ctx(struct ptlrpc_sec *sec,
509 struct vfs_cred *vcred,
510 int create, int remove_dead)
512 struct plain_sec *plsec = sec2plsec(sec);
513 struct ptlrpc_cli_ctx *ctx;
516 read_lock(&plsec->pls_lock);
517 ctx = plsec->pls_ctx;
519 atomic_inc(&ctx->cc_refcount);
520 read_unlock(&plsec->pls_lock);
522 if (unlikely(ctx == NULL))
523 ctx = plain_sec_install_ctx(plsec);
529 void plain_release_ctx(struct ptlrpc_sec *sec,
530 struct ptlrpc_cli_ctx *ctx, int sync)
532 LASSERT(atomic_read(&sec->ps_refcount) > 0);
533 LASSERT(atomic_read(&sec->ps_nctx) > 0);
534 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
535 LASSERT(ctx->cc_sec == sec);
539 atomic_dec(&sec->ps_nctx);
540 sptlrpc_sec_put(sec);
544 int plain_flush_ctx_cache(struct ptlrpc_sec *sec,
545 uid_t uid, int grace, int force)
547 struct plain_sec *plsec = sec2plsec(sec);
548 struct ptlrpc_cli_ctx *ctx;
551 /* do nothing unless caller want to flush for 'all' */
555 write_lock(&plsec->pls_lock);
556 ctx = plsec->pls_ctx;
557 plsec->pls_ctx = NULL;
558 write_unlock(&plsec->pls_lock);
561 sptlrpc_cli_ctx_put(ctx, 1);
566 int plain_alloc_reqbuf(struct ptlrpc_sec *sec,
567 struct ptlrpc_request *req,
570 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
574 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
575 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
577 if (req->rq_pack_udesc)
578 buflens[PLAIN_PACK_USER_OFF] = sptlrpc_current_user_desc_size();
580 if (req->rq_pack_bulk) {
581 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
582 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
585 alloc_len = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
587 if (!req->rq_reqbuf) {
588 LASSERT(!req->rq_pool);
590 alloc_len = size_roundup_power2(alloc_len);
591 OBD_ALLOC(req->rq_reqbuf, alloc_len);
595 req->rq_reqbuf_len = alloc_len;
597 LASSERT(req->rq_pool);
598 LASSERT(req->rq_reqbuf_len >= alloc_len);
599 memset(req->rq_reqbuf, 0, alloc_len);
602 lustre_init_msg_v2(req->rq_reqbuf, PLAIN_PACK_SEGMENTS, buflens, NULL);
603 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_MSG_OFF, 0);
605 if (req->rq_pack_udesc)
606 sptlrpc_pack_user_desc(req->rq_reqbuf, PLAIN_PACK_USER_OFF);
612 void plain_free_reqbuf(struct ptlrpc_sec *sec,
613 struct ptlrpc_request *req)
617 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
618 req->rq_reqbuf = NULL;
619 req->rq_reqbuf_len = 0;
622 req->rq_reqmsg = NULL;
627 int plain_alloc_repbuf(struct ptlrpc_sec *sec,
628 struct ptlrpc_request *req,
631 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
635 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
636 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
638 if (req->rq_pack_bulk) {
639 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
640 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
643 alloc_len = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
645 /* add space for early reply */
646 alloc_len += plain_at_offset;
648 alloc_len = size_roundup_power2(alloc_len);
650 OBD_ALLOC(req->rq_repbuf, alloc_len);
654 req->rq_repbuf_len = alloc_len;
659 void plain_free_repbuf(struct ptlrpc_sec *sec,
660 struct ptlrpc_request *req)
663 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
664 req->rq_repbuf = NULL;
665 req->rq_repbuf_len = 0;
667 req->rq_repmsg = NULL;
672 int plain_enlarge_reqbuf(struct ptlrpc_sec *sec,
673 struct ptlrpc_request *req,
674 int segment, int newsize)
676 struct lustre_msg *newbuf;
678 int newmsg_size, newbuf_size;
681 LASSERT(req->rq_reqbuf);
682 LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
683 LASSERT(lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_MSG_OFF, 0) ==
686 /* compute new embedded msg size. */
687 oldsize = req->rq_reqmsg->lm_buflens[segment];
688 req->rq_reqmsg->lm_buflens[segment] = newsize;
689 newmsg_size = lustre_msg_size_v2(req->rq_reqmsg->lm_bufcount,
690 req->rq_reqmsg->lm_buflens);
691 req->rq_reqmsg->lm_buflens[segment] = oldsize;
693 /* compute new wrapper msg size. */
694 oldsize = req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF];
695 req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = newmsg_size;
696 newbuf_size = lustre_msg_size_v2(req->rq_reqbuf->lm_bufcount,
697 req->rq_reqbuf->lm_buflens);
698 req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = oldsize;
700 /* request from pool should always have enough buffer */
701 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
703 if (req->rq_reqbuf_len < newbuf_size) {
704 newbuf_size = size_roundup_power2(newbuf_size);
706 OBD_ALLOC(newbuf, newbuf_size);
710 memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
712 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
713 req->rq_reqbuf = newbuf;
714 req->rq_reqbuf_len = newbuf_size;
715 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf,
716 PLAIN_PACK_MSG_OFF, 0);
719 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, PLAIN_PACK_MSG_OFF,
721 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
723 req->rq_reqlen = newmsg_size;
727 /****************************************
729 ****************************************/
731 static struct ptlrpc_svc_ctx plain_svc_ctx = {
732 .sc_refcount = ATOMIC_INIT(1),
733 .sc_policy = &plain_policy,
737 int plain_accept(struct ptlrpc_request *req)
739 struct lustre_msg *msg = req->rq_reqbuf;
740 struct plain_header *phdr;
743 LASSERT(SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) ==
744 SPTLRPC_POLICY_PLAIN);
746 if (SPTLRPC_FLVR_BASE(req->rq_flvr.sf_rpc) !=
747 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN) ||
748 SPTLRPC_FLVR_BULK_TYPE(req->rq_flvr.sf_rpc) !=
749 SPTLRPC_FLVR_BULK_TYPE(SPTLRPC_FLVR_PLAIN)) {
750 CERROR("Invalid rpc flavor %x\n", req->rq_flvr.sf_rpc);
754 if (msg->lm_bufcount < PLAIN_PACK_SEGMENTS) {
755 CERROR("unexpected request buf count %u\n", msg->lm_bufcount);
759 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
761 CERROR("missing plain header\n");
765 if (phdr->ph_ver != 0) {
766 CERROR("Invalid header version\n");
770 if (phdr->ph_bulk_hash_alg >= BULK_HASH_ALG_MAX) {
771 CERROR("invalid hash algorithm: %u\n", phdr->ph_bulk_hash_alg);
775 req->rq_sp_from = phdr->ph_sp;
776 req->rq_flvr.u_bulk.hash.hash_alg = phdr->ph_bulk_hash_alg;
778 if (phdr->ph_flags & PLAIN_FL_USER) {
779 if (sptlrpc_unpack_user_desc(msg, PLAIN_PACK_USER_OFF)) {
780 CERROR("Mal-formed user descriptor\n");
784 req->rq_pack_udesc = 1;
785 req->rq_user_desc = lustre_msg_buf(msg, PLAIN_PACK_USER_OFF, 0);
788 if (phdr->ph_flags & PLAIN_FL_BULK) {
789 if (plain_unpack_bsd(msg))
792 req->rq_pack_bulk = 1;
795 req->rq_reqmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
796 req->rq_reqlen = msg->lm_buflens[PLAIN_PACK_MSG_OFF];
798 req->rq_svc_ctx = &plain_svc_ctx;
799 atomic_inc(&req->rq_svc_ctx->sc_refcount);
805 int plain_alloc_rs(struct ptlrpc_request *req, int msgsize)
807 struct ptlrpc_reply_state *rs;
808 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
809 int rs_size = sizeof(*rs);
812 LASSERT(msgsize % 8 == 0);
814 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
815 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
817 if (req->rq_pack_bulk && (req->rq_bulk_read || req->rq_bulk_write))
818 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
820 rs_size += lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
822 rs = req->rq_reply_state;
826 LASSERT(rs->rs_size >= rs_size);
828 OBD_ALLOC(rs, rs_size);
832 rs->rs_size = rs_size;
835 rs->rs_svc_ctx = req->rq_svc_ctx;
836 atomic_inc(&req->rq_svc_ctx->sc_refcount);
837 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
838 rs->rs_repbuf_len = rs_size - sizeof(*rs);
840 lustre_init_msg_v2(rs->rs_repbuf, PLAIN_PACK_SEGMENTS, buflens, NULL);
841 rs->rs_msg = lustre_msg_buf_v2(rs->rs_repbuf, PLAIN_PACK_MSG_OFF, 0);
843 req->rq_reply_state = rs;
848 void plain_free_rs(struct ptlrpc_reply_state *rs)
852 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
853 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
855 if (!rs->rs_prealloc)
856 OBD_FREE(rs, rs->rs_size);
861 int plain_authorize(struct ptlrpc_request *req)
863 struct ptlrpc_reply_state *rs = req->rq_reply_state;
864 struct lustre_msg_v2 *msg = rs->rs_repbuf;
865 struct plain_header *phdr;
872 if (req->rq_replen != msg->lm_buflens[PLAIN_PACK_MSG_OFF])
873 len = lustre_shrink_msg(msg, PLAIN_PACK_MSG_OFF,
876 len = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
878 msg->lm_secflvr = req->rq_flvr.sf_rpc;
880 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, 0);
883 phdr->ph_bulk_hash_alg = req->rq_flvr.u_bulk.hash.hash_alg;
885 if (req->rq_pack_bulk)
886 phdr->ph_flags |= PLAIN_FL_BULK;
888 rs->rs_repdata_len = len;
890 if (likely(req->rq_packed_final)) {
891 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
892 req->rq_reply_off = plain_at_offset;
894 req->rq_reply_off = 0;
896 msg->lm_cksum = crc32_le(!(__u32) 0,
897 lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0),
898 lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF));
899 req->rq_reply_off = 0;
906 int plain_svc_unwrap_bulk(struct ptlrpc_request *req,
907 struct ptlrpc_bulk_desc *desc)
909 struct ptlrpc_reply_state *rs = req->rq_reply_state;
910 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
911 struct plain_bulk_token *tokenr, *tokenv;
914 LASSERT(req->rq_bulk_write);
915 LASSERT(req->rq_pack_bulk);
917 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
918 tokenr = (struct plain_bulk_token *) bsdr->bsd_data;
919 bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
920 tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
922 bsdv->bsd_version = 0;
923 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
924 bsdv->bsd_svc = bsdr->bsd_svc;
927 if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
930 rc = plain_verify_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
933 bsdv->bsd_flags |= BSD_FL_ERR;
934 CERROR("bulk write: server verify failed: %d\n", rc);
941 int plain_svc_wrap_bulk(struct ptlrpc_request *req,
942 struct ptlrpc_bulk_desc *desc)
944 struct ptlrpc_reply_state *rs = req->rq_reply_state;
945 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
946 struct plain_bulk_token *tokenr, *tokenv;
949 LASSERT(req->rq_bulk_read);
950 LASSERT(req->rq_pack_bulk);
952 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
953 tokenr = (struct plain_bulk_token *) bsdr->bsd_data;
954 bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
955 tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
957 bsdv->bsd_version = 0;
958 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
959 bsdv->bsd_svc = bsdr->bsd_svc;
962 if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
965 rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
968 CERROR("bulk read: server failed to compute "
969 "checksum: %d\n", rc);
971 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_RECEIVE))
972 corrupt_bulk_data(desc);
978 static struct ptlrpc_ctx_ops plain_ctx_ops = {
979 .refresh = plain_ctx_refresh,
980 .validate = plain_ctx_validate,
981 .sign = plain_ctx_sign,
982 .verify = plain_ctx_verify,
983 .wrap_bulk = plain_cli_wrap_bulk,
984 .unwrap_bulk = plain_cli_unwrap_bulk,
987 static struct ptlrpc_sec_cops plain_sec_cops = {
988 .create_sec = plain_create_sec,
989 .destroy_sec = plain_destroy_sec,
990 .kill_sec = plain_kill_sec,
991 .lookup_ctx = plain_lookup_ctx,
992 .release_ctx = plain_release_ctx,
993 .flush_ctx_cache = plain_flush_ctx_cache,
994 .alloc_reqbuf = plain_alloc_reqbuf,
995 .free_reqbuf = plain_free_reqbuf,
996 .alloc_repbuf = plain_alloc_repbuf,
997 .free_repbuf = plain_free_repbuf,
998 .enlarge_reqbuf = plain_enlarge_reqbuf,
1001 static struct ptlrpc_sec_sops plain_sec_sops = {
1002 .accept = plain_accept,
1003 .alloc_rs = plain_alloc_rs,
1004 .authorize = plain_authorize,
1005 .free_rs = plain_free_rs,
1006 .unwrap_bulk = plain_svc_unwrap_bulk,
1007 .wrap_bulk = plain_svc_wrap_bulk,
1010 static struct ptlrpc_sec_policy plain_policy = {
1011 .sp_owner = THIS_MODULE,
1013 .sp_policy = SPTLRPC_POLICY_PLAIN,
1014 .sp_cops = &plain_sec_cops,
1015 .sp_sops = &plain_sec_sops,
1018 int sptlrpc_plain_init(void)
1020 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
1023 buflens[PLAIN_PACK_MSG_OFF] = lustre_msg_early_size();
1024 plain_at_offset = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
1026 rc = sptlrpc_register_policy(&plain_policy);
1028 CERROR("failed to register: %d\n", rc);
1033 void sptlrpc_plain_fini(void)
1037 rc = sptlrpc_unregister_policy(&plain_policy);
1039 CERROR("cannot unregister: %d\n", rc);