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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/ptlrpc/gss/gss_bulk.c
36 * Author: Eric Mei <eric.mei@sun.com>
39 #define DEBUG_SUBSYSTEM S_SEC
40 #include <linux/init.h>
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/dcache.h>
45 #include <linux/mutex.h>
46 #include <linux/crypto.h>
49 #include <obd_class.h>
50 #include <obd_support.h>
51 #include <lustre/lustre_idl.h>
52 #include <lustre_net.h>
53 #include <lustre_import.h>
54 #include <lustre_sec.h>
57 #include "gss_internal.h"
60 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
61 struct ptlrpc_request *req,
62 struct ptlrpc_bulk_desc *desc)
64 struct gss_cli_ctx *gctx;
65 struct lustre_msg *msg;
66 struct ptlrpc_bulk_sec_desc *bsd;
72 LASSERT(req->rq_pack_bulk);
73 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
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,
130 desc->bd_iov_count, desc->bd_iov,
132 if (maj != GSS_S_COMPLETE) {
133 CWARN("failed to sign bulk data: %x\n", maj);
138 if (desc->bd_iov_count == 0)
141 rc = sptlrpc_enc_pool_get_pages(desc);
143 CERROR("bulk write: failed to allocate "
144 "encryption pages: %d\n", rc);
148 token.data = bsd->bsd_data;
149 token.len = lustre_msg_buflen(msg, offset) -
152 maj = lgss_wrap_bulk(gctx->gc_mechctx, desc, &token, 0);
153 if (maj != GSS_S_COMPLETE) {
154 CWARN("fail to encrypt bulk data: %x\n", maj);
163 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
164 struct ptlrpc_request *req,
165 struct ptlrpc_bulk_desc *desc)
167 struct gss_cli_ctx *gctx;
168 struct lustre_msg *rmsg, *vmsg;
169 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
174 LASSERT(req->rq_pack_bulk);
175 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
177 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
178 case SPTLRPC_SVC_NULL:
179 vmsg = req->rq_repdata;
180 voff = vmsg->lm_bufcount - 1;
181 LASSERT(vmsg && vmsg->lm_bufcount >= 3);
183 rmsg = req->rq_reqbuf;
184 roff = rmsg->lm_bufcount - 1; /* last segment */
185 LASSERT(rmsg && rmsg->lm_bufcount >= 3);
187 case SPTLRPC_SVC_AUTH:
188 case SPTLRPC_SVC_INTG:
189 vmsg = req->rq_repdata;
190 voff = vmsg->lm_bufcount - 2;
191 LASSERT(vmsg && vmsg->lm_bufcount >= 4);
193 rmsg = req->rq_reqbuf;
194 roff = rmsg->lm_bufcount - 2; /* second last segment */
195 LASSERT(rmsg && rmsg->lm_bufcount >= 4);
197 case SPTLRPC_SVC_PRIV:
198 vmsg = req->rq_repdata;
199 voff = vmsg->lm_bufcount - 1;
200 LASSERT(vmsg && vmsg->lm_bufcount >= 2);
202 rmsg = req->rq_clrbuf;
203 roff = rmsg->lm_bufcount - 1; /* last segment */
204 LASSERT(rmsg && rmsg->lm_bufcount >= 2);
210 bsdr = lustre_msg_buf(rmsg, roff, sizeof(*bsdr));
211 bsdv = lustre_msg_buf(vmsg, voff, sizeof(*bsdv));
212 LASSERT(bsdr && bsdv);
214 if (bsdr->bsd_version != bsdv->bsd_version ||
215 bsdr->bsd_type != bsdv->bsd_type ||
216 bsdr->bsd_svc != bsdv->bsd_svc) {
217 CERROR("bulk security descriptor mismatch: "
218 "(%u,%u,%u) != (%u,%u,%u)\n",
219 bsdr->bsd_version, bsdr->bsd_type, bsdr->bsd_svc,
220 bsdv->bsd_version, bsdv->bsd_type, bsdv->bsd_svc);
224 LASSERT(bsdv->bsd_svc == SPTLRPC_BULK_SVC_NULL ||
225 bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG ||
226 bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV);
229 * in privacy mode if return success, make sure bd_nob_transferred
230 * is the actual size of the clear text, otherwise upper layer
233 if (req->rq_bulk_write) {
234 if (bsdv->bsd_flags & BSD_FL_ERR) {
235 CERROR("server reported bulk i/o failure\n");
239 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV)
240 desc->bd_nob_transferred = desc->bd_nob;
243 * bulk read, upon return success, bd_nob_transferred is
244 * the size of plain text actually received.
246 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
247 LASSERT(gctx->gc_mechctx);
249 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG) {
252 /* fix the actual data size */
253 for (i = 0, nob = 0; i < desc->bd_iov_count; i++) {
254 if (desc->bd_iov[i].kiov_len + nob >
255 desc->bd_nob_transferred) {
256 desc->bd_iov[i].kiov_len =
257 desc->bd_nob_transferred - nob;
259 nob += desc->bd_iov[i].kiov_len;
262 token.data = bsdv->bsd_data;
263 token.len = lustre_msg_buflen(vmsg, voff) -
266 maj = lgss_verify_mic(gctx->gc_mechctx, 0, NULL,
267 desc->bd_iov_count, desc->bd_iov,
269 if (maj != GSS_S_COMPLETE) {
270 CERROR("failed to verify bulk read: %x\n", maj);
273 } else if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV) {
274 desc->bd_nob = bsdv->bsd_nob;
275 if (desc->bd_nob == 0)
278 token.data = bsdv->bsd_data;
279 token.len = lustre_msg_buflen(vmsg, voff) -
282 maj = lgss_unwrap_bulk(gctx->gc_mechctx, desc,
284 if (maj != GSS_S_COMPLETE) {
285 CERROR("failed to decrypt bulk read: %x\n",
290 desc->bd_nob_transferred = desc->bd_nob;
297 static int gss_prep_bulk(struct ptlrpc_bulk_desc *desc,
298 struct gss_ctx *mechctx)
302 if (desc->bd_iov_count == 0)
305 rc = sptlrpc_enc_pool_get_pages(desc);
309 if (lgss_prep_bulk(mechctx, desc) != GSS_S_COMPLETE)
315 int gss_cli_prep_bulk(struct ptlrpc_request *req,
316 struct ptlrpc_bulk_desc *desc)
320 LASSERT(req->rq_cli_ctx);
321 LASSERT(req->rq_pack_bulk);
322 LASSERT(req->rq_bulk_read);
324 if (SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_BULK_SVC_PRIV)
327 rc = gss_prep_bulk(desc, ctx2gctx(req->rq_cli_ctx)->gc_mechctx);
329 CERROR("bulk read: failed to prepare encryption "
335 int gss_svc_prep_bulk(struct ptlrpc_request *req,
336 struct ptlrpc_bulk_desc *desc)
338 struct gss_svc_reqctx *grctx;
339 struct ptlrpc_bulk_sec_desc *bsd;
342 LASSERT(req->rq_svc_ctx);
343 LASSERT(req->rq_pack_bulk);
344 LASSERT(req->rq_bulk_write);
346 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
347 LASSERT(grctx->src_reqbsd);
348 LASSERT(grctx->src_repbsd);
349 LASSERT(grctx->src_ctx);
350 LASSERT(grctx->src_ctx->gsc_mechctx);
352 bsd = grctx->src_reqbsd;
353 if (bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)
356 rc = gss_prep_bulk(desc, grctx->src_ctx->gsc_mechctx);
358 CERROR("bulk write: failed to prepare encryption "
364 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
365 struct ptlrpc_bulk_desc *desc)
367 struct gss_svc_reqctx *grctx;
368 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
372 LASSERT(req->rq_svc_ctx);
373 LASSERT(req->rq_pack_bulk);
374 LASSERT(req->rq_bulk_write);
376 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
378 LASSERT(grctx->src_reqbsd);
379 LASSERT(grctx->src_repbsd);
380 LASSERT(grctx->src_ctx);
381 LASSERT(grctx->src_ctx->gsc_mechctx);
383 bsdr = grctx->src_reqbsd;
384 bsdv = grctx->src_repbsd;
386 /* bsdr has been sanity checked during unpacking */
387 bsdv->bsd_version = 0;
388 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
389 bsdv->bsd_svc = bsdr->bsd_svc;
392 switch (bsdv->bsd_svc) {
393 case SPTLRPC_BULK_SVC_INTG:
394 token.data = bsdr->bsd_data;
395 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
397 maj = lgss_verify_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
398 desc->bd_iov_count, desc->bd_iov, &token);
399 if (maj != GSS_S_COMPLETE) {
400 bsdv->bsd_flags |= BSD_FL_ERR;
401 CERROR("failed to verify bulk signature: %x\n", maj);
405 case SPTLRPC_BULK_SVC_PRIV:
406 if (bsdr->bsd_nob != desc->bd_nob) {
407 bsdv->bsd_flags |= BSD_FL_ERR;
408 CERROR("prepared nob %d doesn't match the actual "
409 "nob %d\n", desc->bd_nob, bsdr->bsd_nob);
413 if (desc->bd_iov_count == 0) {
414 LASSERT(desc->bd_nob == 0);
418 token.data = bsdr->bsd_data;
419 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
421 maj = lgss_unwrap_bulk(grctx->src_ctx->gsc_mechctx,
423 if (maj != GSS_S_COMPLETE) {
424 bsdv->bsd_flags |= BSD_FL_ERR;
425 CERROR("failed decrypt bulk data: %x\n", maj);
434 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
435 struct ptlrpc_bulk_desc *desc)
437 struct gss_svc_reqctx *grctx;
438 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
443 LASSERT(req->rq_svc_ctx);
444 LASSERT(req->rq_pack_bulk);
445 LASSERT(req->rq_bulk_read);
447 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
449 LASSERT(grctx->src_reqbsd);
450 LASSERT(grctx->src_repbsd);
451 LASSERT(grctx->src_ctx);
452 LASSERT(grctx->src_ctx->gsc_mechctx);
454 bsdr = grctx->src_reqbsd;
455 bsdv = grctx->src_repbsd;
457 /* bsdr has been sanity checked during unpacking */
458 bsdv->bsd_version = 0;
459 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
460 bsdv->bsd_svc = bsdr->bsd_svc;
463 switch (bsdv->bsd_svc) {
464 case SPTLRPC_BULK_SVC_INTG:
465 token.data = bsdv->bsd_data;
466 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
468 maj = lgss_get_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
469 desc->bd_iov_count, desc->bd_iov, &token);
470 if (maj != GSS_S_COMPLETE) {
471 bsdv->bsd_flags |= BSD_FL_ERR;
472 CERROR("failed to sign bulk data: %x\n", maj);
476 case SPTLRPC_BULK_SVC_PRIV:
477 bsdv->bsd_nob = desc->bd_nob;
479 if (desc->bd_iov_count == 0) {
480 LASSERT(desc->bd_nob == 0);
484 rc = sptlrpc_enc_pool_get_pages(desc);
486 bsdv->bsd_flags |= BSD_FL_ERR;
487 CERROR("bulk read: failed to allocate encryption "
492 token.data = bsdv->bsd_data;
493 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
495 maj = lgss_wrap_bulk(grctx->src_ctx->gsc_mechctx,
497 if (maj != GSS_S_COMPLETE) {
498 bsdv->bsd_flags |= BSD_FL_ERR;
499 CERROR("failed to encrypt bulk data: %x\n", maj);