4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/gss/gss_bulk.c
34 * Author: Eric Mei <eric.mei@sun.com>
37 #define DEBUG_SUBSYSTEM S_SEC
38 #include <linux/init.h>
39 #include <linux/module.h>
40 #include <linux/slab.h>
41 #include <linux/dcache.h>
43 #include <linux/mutex.h>
44 #include <linux/crypto.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <lustre/lustre_idl.h>
50 #include <lustre_net.h>
51 #include <lustre_import.h>
52 #include <lustre_sec.h>
55 #include "gss_internal.h"
58 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
59 struct ptlrpc_request *req,
60 struct ptlrpc_bulk_desc *desc)
62 struct gss_cli_ctx *gctx;
63 struct lustre_msg *msg;
64 struct ptlrpc_bulk_sec_desc *bsd;
71 LASSERT(req->rq_pack_bulk);
72 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
73 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
75 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
76 LASSERT(gctx->gc_mechctx);
78 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
79 case SPTLRPC_SVC_NULL:
80 LASSERT(req->rq_reqbuf->lm_bufcount >= 3);
82 offset = msg->lm_bufcount - 1;
84 case SPTLRPC_SVC_AUTH:
85 case SPTLRPC_SVC_INTG:
86 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
88 offset = msg->lm_bufcount - 2;
90 case SPTLRPC_SVC_PRIV:
91 LASSERT(req->rq_clrbuf->lm_bufcount >= 2);
93 offset = msg->lm_bufcount - 1;
99 bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
100 bsd->bsd_version = 0;
102 bsd->bsd_type = SPTLRPC_BULK_DEFAULT;
103 bsd->bsd_svc = SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc);
105 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_NULL)
108 LASSERT(bsd->bsd_svc == SPTLRPC_BULK_SVC_INTG ||
109 bsd->bsd_svc == SPTLRPC_BULK_SVC_PRIV);
111 if (req->rq_bulk_read) {
113 * bulk read: prepare receiving pages only for privacy mode.
115 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_PRIV)
116 return gss_cli_prep_bulk(req, desc);
119 * bulk write: sign or encrypt bulk pages.
121 bsd->bsd_nob = desc->bd_nob;
123 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_INTG) {
125 token.data = bsd->bsd_data;
126 token.len = lustre_msg_buflen(msg, offset) -
129 maj = lgss_get_mic(gctx->gc_mechctx, 0, NULL,
133 if (maj != GSS_S_COMPLETE) {
134 CWARN("failed to sign bulk data: %x\n", maj);
139 if (desc->bd_iov_count == 0)
142 rc = sptlrpc_enc_pool_get_pages(desc);
144 CERROR("bulk write: failed to allocate "
145 "encryption pages: %d\n", rc);
149 token.data = bsd->bsd_data;
150 token.len = lustre_msg_buflen(msg, offset) -
153 maj = lgss_wrap_bulk(gctx->gc_mechctx, desc, &token, 0);
154 if (maj != GSS_S_COMPLETE) {
155 CWARN("fail to encrypt bulk data: %x\n", maj);
164 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
165 struct ptlrpc_request *req,
166 struct ptlrpc_bulk_desc *desc)
168 struct gss_cli_ctx *gctx;
169 struct lustre_msg *rmsg, *vmsg;
170 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
176 LASSERT(req->rq_pack_bulk);
177 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
178 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
180 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
181 case SPTLRPC_SVC_NULL:
182 vmsg = req->rq_repdata;
183 LASSERT(vmsg != NULL && vmsg->lm_bufcount >= 3);
184 voff = vmsg->lm_bufcount - 1;
186 rmsg = req->rq_reqbuf;
187 LASSERT(rmsg != NULL && rmsg->lm_bufcount >= 3);
188 roff = rmsg->lm_bufcount - 1; /* last segment */
190 case SPTLRPC_SVC_AUTH:
191 case SPTLRPC_SVC_INTG:
192 vmsg = req->rq_repdata;
193 LASSERT(vmsg != NULL && vmsg->lm_bufcount >= 4);
194 voff = vmsg->lm_bufcount - 2;
196 rmsg = req->rq_reqbuf;
197 LASSERT(rmsg != NULL && rmsg->lm_bufcount >= 4);
198 roff = rmsg->lm_bufcount - 2; /* second last segment */
200 case SPTLRPC_SVC_PRIV:
201 vmsg = req->rq_repdata;
202 LASSERT(vmsg != NULL && vmsg->lm_bufcount >= 2);
203 voff = vmsg->lm_bufcount - 1;
205 rmsg = req->rq_clrbuf;
206 LASSERT(rmsg != NULL && rmsg->lm_bufcount >= 2);
207 roff = rmsg->lm_bufcount - 1; /* last segment */
213 bsdr = lustre_msg_buf(rmsg, roff, sizeof(*bsdr));
214 bsdv = lustre_msg_buf(vmsg, voff, sizeof(*bsdv));
215 LASSERT(bsdr && bsdv);
217 if (bsdr->bsd_version != bsdv->bsd_version ||
218 bsdr->bsd_type != bsdv->bsd_type ||
219 bsdr->bsd_svc != bsdv->bsd_svc) {
220 CERROR("bulk security descriptor mismatch: "
221 "(%u,%u,%u) != (%u,%u,%u)\n",
222 bsdr->bsd_version, bsdr->bsd_type, bsdr->bsd_svc,
223 bsdv->bsd_version, bsdv->bsd_type, bsdv->bsd_svc);
227 LASSERT(bsdv->bsd_svc == SPTLRPC_BULK_SVC_NULL ||
228 bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG ||
229 bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV);
232 * in privacy mode if return success, make sure bd_nob_transferred
233 * is the actual size of the clear text, otherwise upper layer
236 if (req->rq_bulk_write) {
237 if (bsdv->bsd_flags & BSD_FL_ERR) {
238 CERROR("server reported bulk i/o failure\n");
242 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV)
243 desc->bd_nob_transferred = desc->bd_nob;
246 * bulk read, upon return success, bd_nob_transferred is
247 * the size of plain text actually received.
249 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
250 LASSERT(gctx->gc_mechctx);
252 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG) {
255 /* fix the actual data size */
256 for (i = 0, nob = 0; i < desc->bd_iov_count; i++) {
257 if (BD_GET_KIOV(desc, i).kiov_len + nob >
258 desc->bd_nob_transferred) {
259 BD_GET_KIOV(desc, i).kiov_len =
260 desc->bd_nob_transferred - nob;
262 nob += BD_GET_KIOV(desc, i).kiov_len;
265 token.data = bsdv->bsd_data;
266 token.len = lustre_msg_buflen(vmsg, voff) -
269 maj = lgss_verify_mic(gctx->gc_mechctx, 0, NULL,
273 if (maj != GSS_S_COMPLETE) {
274 CERROR("failed to verify bulk read: %x\n", maj);
277 } else if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV) {
278 desc->bd_nob = bsdv->bsd_nob;
279 if (desc->bd_nob == 0)
282 token.data = bsdv->bsd_data;
283 token.len = lustre_msg_buflen(vmsg, voff) -
286 maj = lgss_unwrap_bulk(gctx->gc_mechctx, desc,
288 if (maj != GSS_S_COMPLETE) {
289 CERROR("failed to decrypt bulk read: %x\n",
294 desc->bd_nob_transferred = desc->bd_nob;
301 static int gss_prep_bulk(struct ptlrpc_bulk_desc *desc,
302 struct gss_ctx *mechctx)
306 if (desc->bd_iov_count == 0)
309 rc = sptlrpc_enc_pool_get_pages(desc);
313 if (lgss_prep_bulk(mechctx, desc) != GSS_S_COMPLETE)
319 int gss_cli_prep_bulk(struct ptlrpc_request *req,
320 struct ptlrpc_bulk_desc *desc)
325 LASSERT(req->rq_cli_ctx);
326 LASSERT(req->rq_pack_bulk);
327 LASSERT(req->rq_bulk_read);
329 if (SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_BULK_SVC_PRIV)
332 rc = gss_prep_bulk(desc, ctx2gctx(req->rq_cli_ctx)->gc_mechctx);
334 CERROR("bulk read: failed to prepare encryption "
340 int gss_svc_prep_bulk(struct ptlrpc_request *req,
341 struct ptlrpc_bulk_desc *desc)
343 struct gss_svc_reqctx *grctx;
344 struct ptlrpc_bulk_sec_desc *bsd;
348 LASSERT(req->rq_svc_ctx);
349 LASSERT(req->rq_pack_bulk);
350 LASSERT(req->rq_bulk_write);
352 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
353 LASSERT(grctx->src_reqbsd);
354 LASSERT(grctx->src_repbsd);
355 LASSERT(grctx->src_ctx);
356 LASSERT(grctx->src_ctx->gsc_mechctx);
358 bsd = grctx->src_reqbsd;
359 if (bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)
362 rc = gss_prep_bulk(desc, grctx->src_ctx->gsc_mechctx);
364 CERROR("bulk write: failed to prepare encryption "
370 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
371 struct ptlrpc_bulk_desc *desc)
373 struct gss_svc_reqctx *grctx;
374 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
379 LASSERT(req->rq_svc_ctx);
380 LASSERT(req->rq_pack_bulk);
381 LASSERT(req->rq_bulk_write);
382 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
384 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
386 LASSERT(grctx->src_reqbsd);
387 LASSERT(grctx->src_repbsd);
388 LASSERT(grctx->src_ctx);
389 LASSERT(grctx->src_ctx->gsc_mechctx);
391 bsdr = grctx->src_reqbsd;
392 bsdv = grctx->src_repbsd;
394 /* bsdr has been sanity checked during unpacking */
395 bsdv->bsd_version = 0;
396 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
397 bsdv->bsd_svc = bsdr->bsd_svc;
400 switch (bsdv->bsd_svc) {
401 case SPTLRPC_BULK_SVC_INTG:
402 token.data = bsdr->bsd_data;
403 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
405 maj = lgss_verify_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
407 GET_KIOV(desc), &token);
408 if (maj != GSS_S_COMPLETE) {
409 bsdv->bsd_flags |= BSD_FL_ERR;
410 CERROR("failed to verify bulk signature: %x\n", maj);
414 case SPTLRPC_BULK_SVC_PRIV:
415 if (bsdr->bsd_nob != desc->bd_nob) {
416 bsdv->bsd_flags |= BSD_FL_ERR;
417 CERROR("prepared nob %d doesn't match the actual "
418 "nob %d\n", desc->bd_nob, bsdr->bsd_nob);
422 if (desc->bd_iov_count == 0) {
423 LASSERT(desc->bd_nob == 0);
427 token.data = bsdr->bsd_data;
428 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
430 maj = lgss_unwrap_bulk(grctx->src_ctx->gsc_mechctx,
432 if (maj != GSS_S_COMPLETE) {
433 bsdv->bsd_flags |= BSD_FL_ERR;
434 CERROR("failed decrypt bulk data: %x\n", maj);
438 /* mimic gss_cli_ctx_unwrap_bulk */
439 desc->bd_nob_transferred = desc->bd_nob;
447 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
448 struct ptlrpc_bulk_desc *desc)
450 struct gss_svc_reqctx *grctx;
451 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
457 LASSERT(req->rq_svc_ctx);
458 LASSERT(req->rq_pack_bulk);
459 LASSERT(req->rq_bulk_read);
460 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
462 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
464 LASSERT(grctx->src_reqbsd);
465 LASSERT(grctx->src_repbsd);
466 LASSERT(grctx->src_ctx);
467 LASSERT(grctx->src_ctx->gsc_mechctx);
469 bsdr = grctx->src_reqbsd;
470 bsdv = grctx->src_repbsd;
472 /* bsdr has been sanity checked during unpacking */
473 bsdv->bsd_version = 0;
474 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
475 bsdv->bsd_svc = bsdr->bsd_svc;
478 switch (bsdv->bsd_svc) {
479 case SPTLRPC_BULK_SVC_INTG:
480 token.data = bsdv->bsd_data;
481 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
483 maj = lgss_get_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
485 GET_KIOV(desc), &token);
486 if (maj != GSS_S_COMPLETE) {
487 bsdv->bsd_flags |= BSD_FL_ERR;
488 CERROR("failed to sign bulk data: %x\n", maj);
492 case SPTLRPC_BULK_SVC_PRIV:
493 bsdv->bsd_nob = desc->bd_nob;
495 if (desc->bd_iov_count == 0) {
496 LASSERT(desc->bd_nob == 0);
500 rc = sptlrpc_enc_pool_get_pages(desc);
502 bsdv->bsd_flags |= BSD_FL_ERR;
503 CERROR("bulk read: failed to allocate encryption "
508 token.data = bsdv->bsd_data;
509 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
511 maj = lgss_wrap_bulk(grctx->src_ctx->gsc_mechctx,
513 if (maj != GSS_S_COMPLETE) {
514 bsdv->bsd_flags |= BSD_FL_ERR;
515 CERROR("failed to encrypt bulk data: %x\n", maj);