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, int swabbed)
107 struct ptlrpc_bulk_sec_desc *bsd;
109 if (bulk_sec_desc_unpack(msg, PLAIN_PACK_BULK_OFF, swabbed))
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;
245 if (msg->lm_bufcount != PLAIN_PACK_SEGMENTS) {
246 CERROR("unexpected reply buf count %u\n", msg->lm_bufcount);
250 swabbed = ptlrpc_rep_need_swab(req);
252 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
254 CERROR("missing plain header\n");
258 if (phdr->ph_ver != 0) {
259 CERROR("Invalid header version\n");
263 /* expect no user desc in reply */
264 if (phdr->ph_flags & PLAIN_FL_USER) {
265 CERROR("Unexpected udesc flag in reply\n");
269 if (phdr->ph_bulk_hash_alg != req->rq_flvr.u_bulk.hash.hash_alg) {
270 CERROR("reply bulk flavor %u != %u\n", phdr->ph_bulk_hash_alg,
271 req->rq_flvr.u_bulk.hash.hash_alg);
275 if (unlikely(req->rq_early)) {
276 cksum = crc32_le(!(__u32) 0,
277 lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0),
278 lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF));
279 if (cksum != msg->lm_cksum) {
280 CWARN("early reply checksum mismatch: %08x != %08x\n",
281 cpu_to_le32(cksum), msg->lm_cksum);
285 /* whether we sent with bulk or not, we expect the same
286 * in reply, except for early reply */
287 if (!req->rq_early &&
288 !equi(req->rq_pack_bulk == 1,
289 phdr->ph_flags & PLAIN_FL_BULK)) {
290 CERROR("%s bulk checksum in reply\n",
291 req->rq_pack_bulk ? "Missing" : "Unexpected");
295 if (phdr->ph_flags & PLAIN_FL_BULK) {
296 if (plain_unpack_bsd(msg, swabbed))
301 req->rq_repmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
302 req->rq_replen = lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF);
307 int plain_cli_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
308 struct ptlrpc_request *req,
309 struct ptlrpc_bulk_desc *desc)
311 struct ptlrpc_bulk_sec_desc *bsd;
312 struct plain_bulk_token *token;
315 LASSERT(req->rq_pack_bulk);
316 LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
318 bsd = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
319 token = (struct plain_bulk_token *) bsd->bsd_data;
321 bsd->bsd_version = 0;
323 bsd->bsd_type = SPTLRPC_BULK_DEFAULT;
324 bsd->bsd_svc = SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc);
326 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_NULL)
329 if (req->rq_bulk_read)
332 rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
335 CERROR("bulk write: failed to compute checksum: %d\n", rc);
338 * for sending we only compute the wrong checksum instead
339 * of corrupting the data so it is still correct on a redo
341 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_SEND) &&
342 req->rq_flvr.u_bulk.hash.hash_alg != BULK_HASH_ALG_NULL)
343 token->pbt_hash[0] ^= 0x1;
350 int plain_cli_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
351 struct ptlrpc_request *req,
352 struct ptlrpc_bulk_desc *desc)
354 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
355 struct plain_bulk_token *tokenr, *tokenv;
361 LASSERT(req->rq_pack_bulk);
362 LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
363 LASSERT(req->rq_repdata->lm_bufcount == PLAIN_PACK_SEGMENTS);
365 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
366 tokenr = (struct plain_bulk_token *) bsdr->bsd_data;
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(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(req->rq_reqbuf, req->rq_reqbuf_len);
621 req->rq_reqbuf = NULL;
622 req->rq_reqbuf_len = 0;
625 req->rq_reqmsg = NULL;
630 int plain_alloc_repbuf(struct ptlrpc_sec *sec,
631 struct ptlrpc_request *req,
634 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
638 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
639 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
641 if (req->rq_pack_bulk) {
642 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
643 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
646 alloc_len = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
648 /* add space for early reply */
649 alloc_len += plain_at_offset;
651 alloc_len = size_roundup_power2(alloc_len);
653 OBD_ALLOC(req->rq_repbuf, alloc_len);
657 req->rq_repbuf_len = alloc_len;
662 void plain_free_repbuf(struct ptlrpc_sec *sec,
663 struct ptlrpc_request *req)
666 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
667 req->rq_repbuf = NULL;
668 req->rq_repbuf_len = 0;
670 req->rq_repmsg = NULL;
675 int plain_enlarge_reqbuf(struct ptlrpc_sec *sec,
676 struct ptlrpc_request *req,
677 int segment, int newsize)
679 struct lustre_msg *newbuf;
681 int newmsg_size, newbuf_size;
684 LASSERT(req->rq_reqbuf);
685 LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
686 LASSERT(lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_MSG_OFF, 0) ==
689 /* compute new embedded msg size. */
690 oldsize = req->rq_reqmsg->lm_buflens[segment];
691 req->rq_reqmsg->lm_buflens[segment] = newsize;
692 newmsg_size = lustre_msg_size_v2(req->rq_reqmsg->lm_bufcount,
693 req->rq_reqmsg->lm_buflens);
694 req->rq_reqmsg->lm_buflens[segment] = oldsize;
696 /* compute new wrapper msg size. */
697 oldsize = req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF];
698 req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = newmsg_size;
699 newbuf_size = lustre_msg_size_v2(req->rq_reqbuf->lm_bufcount,
700 req->rq_reqbuf->lm_buflens);
701 req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = oldsize;
703 /* request from pool should always have enough buffer */
704 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
706 if (req->rq_reqbuf_len < newbuf_size) {
707 newbuf_size = size_roundup_power2(newbuf_size);
709 OBD_ALLOC(newbuf, newbuf_size);
713 memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
715 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
716 req->rq_reqbuf = newbuf;
717 req->rq_reqbuf_len = newbuf_size;
718 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf,
719 PLAIN_PACK_MSG_OFF, 0);
722 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, PLAIN_PACK_MSG_OFF,
724 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
726 req->rq_reqlen = newmsg_size;
730 /****************************************
732 ****************************************/
734 static struct ptlrpc_svc_ctx plain_svc_ctx = {
735 .sc_refcount = ATOMIC_INIT(1),
736 .sc_policy = &plain_policy,
740 int plain_accept(struct ptlrpc_request *req)
742 struct lustre_msg *msg = req->rq_reqbuf;
743 struct plain_header *phdr;
747 LASSERT(SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) ==
748 SPTLRPC_POLICY_PLAIN);
750 if (SPTLRPC_FLVR_BASE(req->rq_flvr.sf_rpc) !=
751 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN) ||
752 SPTLRPC_FLVR_BULK_TYPE(req->rq_flvr.sf_rpc) !=
753 SPTLRPC_FLVR_BULK_TYPE(SPTLRPC_FLVR_PLAIN)) {
754 CERROR("Invalid rpc flavor %x\n", req->rq_flvr.sf_rpc);
758 if (msg->lm_bufcount < PLAIN_PACK_SEGMENTS) {
759 CERROR("unexpected request buf count %u\n", msg->lm_bufcount);
763 swabbed = ptlrpc_req_need_swab(req);
765 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
767 CERROR("missing plain header\n");
771 if (phdr->ph_ver != 0) {
772 CERROR("Invalid header version\n");
776 if (phdr->ph_bulk_hash_alg >= BULK_HASH_ALG_MAX) {
777 CERROR("invalid hash algorithm: %u\n", phdr->ph_bulk_hash_alg);
781 req->rq_sp_from = phdr->ph_sp;
782 req->rq_flvr.u_bulk.hash.hash_alg = phdr->ph_bulk_hash_alg;
784 if (phdr->ph_flags & PLAIN_FL_USER) {
785 if (sptlrpc_unpack_user_desc(msg, PLAIN_PACK_USER_OFF,
787 CERROR("Mal-formed user descriptor\n");
791 req->rq_pack_udesc = 1;
792 req->rq_user_desc = lustre_msg_buf(msg, PLAIN_PACK_USER_OFF, 0);
795 if (phdr->ph_flags & PLAIN_FL_BULK) {
796 if (plain_unpack_bsd(msg, swabbed))
799 req->rq_pack_bulk = 1;
802 req->rq_reqmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
803 req->rq_reqlen = msg->lm_buflens[PLAIN_PACK_MSG_OFF];
805 req->rq_svc_ctx = &plain_svc_ctx;
806 atomic_inc(&req->rq_svc_ctx->sc_refcount);
812 int plain_alloc_rs(struct ptlrpc_request *req, int msgsize)
814 struct ptlrpc_reply_state *rs;
815 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
816 int rs_size = sizeof(*rs);
819 LASSERT(msgsize % 8 == 0);
821 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
822 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
824 if (req->rq_pack_bulk && (req->rq_bulk_read || req->rq_bulk_write))
825 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
827 rs_size += lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
829 rs = req->rq_reply_state;
833 LASSERT(rs->rs_size >= rs_size);
835 OBD_ALLOC(rs, rs_size);
839 rs->rs_size = rs_size;
842 rs->rs_svc_ctx = req->rq_svc_ctx;
843 atomic_inc(&req->rq_svc_ctx->sc_refcount);
844 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
845 rs->rs_repbuf_len = rs_size - sizeof(*rs);
847 lustre_init_msg_v2(rs->rs_repbuf, PLAIN_PACK_SEGMENTS, buflens, NULL);
848 rs->rs_msg = lustre_msg_buf_v2(rs->rs_repbuf, PLAIN_PACK_MSG_OFF, 0);
850 req->rq_reply_state = rs;
855 void plain_free_rs(struct ptlrpc_reply_state *rs)
859 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
860 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
862 if (!rs->rs_prealloc)
863 OBD_FREE(rs, rs->rs_size);
868 int plain_authorize(struct ptlrpc_request *req)
870 struct ptlrpc_reply_state *rs = req->rq_reply_state;
871 struct lustre_msg_v2 *msg = rs->rs_repbuf;
872 struct plain_header *phdr;
879 if (req->rq_replen != msg->lm_buflens[PLAIN_PACK_MSG_OFF])
880 len = lustre_shrink_msg(msg, PLAIN_PACK_MSG_OFF,
883 len = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
885 msg->lm_secflvr = req->rq_flvr.sf_rpc;
887 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, 0);
890 phdr->ph_bulk_hash_alg = req->rq_flvr.u_bulk.hash.hash_alg;
892 if (req->rq_pack_bulk)
893 phdr->ph_flags |= PLAIN_FL_BULK;
895 rs->rs_repdata_len = len;
897 if (likely(req->rq_packed_final)) {
898 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
899 req->rq_reply_off = plain_at_offset;
901 req->rq_reply_off = 0;
903 msg->lm_cksum = crc32_le(!(__u32) 0,
904 lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0),
905 lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF));
906 req->rq_reply_off = 0;
913 int plain_svc_unwrap_bulk(struct ptlrpc_request *req,
914 struct ptlrpc_bulk_desc *desc)
916 struct ptlrpc_reply_state *rs = req->rq_reply_state;
917 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
918 struct plain_bulk_token *tokenr, *tokenv;
921 LASSERT(req->rq_bulk_write);
922 LASSERT(req->rq_pack_bulk);
924 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
925 tokenr = (struct plain_bulk_token *) bsdr->bsd_data;
926 bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
927 tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
929 bsdv->bsd_version = 0;
930 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
931 bsdv->bsd_svc = bsdr->bsd_svc;
934 if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
937 rc = plain_verify_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
940 bsdv->bsd_flags |= BSD_FL_ERR;
941 CERROR("bulk write: server verify failed: %d\n", rc);
948 int plain_svc_wrap_bulk(struct ptlrpc_request *req,
949 struct ptlrpc_bulk_desc *desc)
951 struct ptlrpc_reply_state *rs = req->rq_reply_state;
952 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
953 struct plain_bulk_token *tokenr, *tokenv;
956 LASSERT(req->rq_bulk_read);
957 LASSERT(req->rq_pack_bulk);
959 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
960 tokenr = (struct plain_bulk_token *) bsdr->bsd_data;
961 bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
962 tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
964 bsdv->bsd_version = 0;
965 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
966 bsdv->bsd_svc = bsdr->bsd_svc;
969 if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
972 rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
975 CERROR("bulk read: server failed to compute "
976 "checksum: %d\n", rc);
978 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_RECEIVE))
979 corrupt_bulk_data(desc);
985 static struct ptlrpc_ctx_ops plain_ctx_ops = {
986 .refresh = plain_ctx_refresh,
987 .validate = plain_ctx_validate,
988 .sign = plain_ctx_sign,
989 .verify = plain_ctx_verify,
990 .wrap_bulk = plain_cli_wrap_bulk,
991 .unwrap_bulk = plain_cli_unwrap_bulk,
994 static struct ptlrpc_sec_cops plain_sec_cops = {
995 .create_sec = plain_create_sec,
996 .destroy_sec = plain_destroy_sec,
997 .kill_sec = plain_kill_sec,
998 .lookup_ctx = plain_lookup_ctx,
999 .release_ctx = plain_release_ctx,
1000 .flush_ctx_cache = plain_flush_ctx_cache,
1001 .alloc_reqbuf = plain_alloc_reqbuf,
1002 .free_reqbuf = plain_free_reqbuf,
1003 .alloc_repbuf = plain_alloc_repbuf,
1004 .free_repbuf = plain_free_repbuf,
1005 .enlarge_reqbuf = plain_enlarge_reqbuf,
1008 static struct ptlrpc_sec_sops plain_sec_sops = {
1009 .accept = plain_accept,
1010 .alloc_rs = plain_alloc_rs,
1011 .authorize = plain_authorize,
1012 .free_rs = plain_free_rs,
1013 .unwrap_bulk = plain_svc_unwrap_bulk,
1014 .wrap_bulk = plain_svc_wrap_bulk,
1017 static struct ptlrpc_sec_policy plain_policy = {
1018 .sp_owner = THIS_MODULE,
1020 .sp_policy = SPTLRPC_POLICY_PLAIN,
1021 .sp_cops = &plain_sec_cops,
1022 .sp_sops = &plain_sec_sops,
1025 int sptlrpc_plain_init(void)
1027 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
1030 buflens[PLAIN_PACK_MSG_OFF] = lustre_msg_early_size();
1031 plain_at_offset = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
1033 rc = sptlrpc_register_policy(&plain_policy);
1035 CERROR("failed to register: %d\n", rc);
1040 void sptlrpc_plain_fini(void)
1044 rc = sptlrpc_unregister_policy(&plain_policy);
1046 CERROR("cannot unregister: %d\n", rc);