1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2006 Cluster File Systems, Inc.
5 * Author: Eric Mei <ericm@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # define EXPORT_SYMTAB
26 #define DEBUG_SUBSYSTEM S_SEC
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/dcache.h>
33 #include <linux/random.h>
34 #include <linux/mutex.h>
35 #include <linux/crypto.h>
37 #include <liblustre.h>
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre/lustre_idl.h>
44 #include <lustre_net.h>
45 #include <lustre_import.h>
46 #include <lustre_sec.h>
49 #include "gss_internal.h"
53 int do_bulk_privacy(struct gss_ctx *gctx,
54 struct ptlrpc_bulk_desc *desc,
55 int encrypt, __u32 alg,
56 struct ptlrpc_bulk_sec_desc *bsd)
58 struct crypto_tfm *tfm;
59 struct scatterlist sg, sg2, *sgd;
61 __u8 local_iv[sizeof(bsd->bsd_iv)];
63 LASSERT(alg < BULK_PRIV_ALG_MAX);
66 bsd->bsd_priv_alg = BULK_PRIV_ALG_NULL;
68 if (alg == BULK_PRIV_ALG_NULL)
72 get_random_bytes(bsd->bsd_iv, sizeof(bsd->bsd_iv));
74 /* compute the secret iv */
75 lgss_plain_encrypt(gctx, sizeof(local_iv), bsd->bsd_iv, local_iv);
77 tfm = crypto_alloc_tfm(sptlrpc_bulk_priv_alg2name(alg),
78 sptlrpc_bulk_priv_alg2flags(alg));
80 CERROR("Failed to allocate TFM %s\n",
81 sptlrpc_bulk_priv_alg2name(alg));
85 rc = crypto_cipher_setkey(tfm, local_iv, sizeof(local_iv));
87 CERROR("Failed to set key for TFM %s: %d\n",
88 sptlrpc_bulk_priv_alg2name(alg), rc);
93 for (i = 0; i < desc->bd_iov_count; i++) {
94 sg.page = desc->bd_iov[i].kiov_page;
95 sg.offset = desc->bd_iov[i].kiov_offset;
96 sg.length = desc->bd_iov[i].kiov_len;
98 if (desc->bd_enc_pages) {
99 sg2.page = desc->bd_enc_pages[i];
100 sg2.offset = desc->bd_iov[i].kiov_offset;
101 sg2.length = desc->bd_iov[i].kiov_len;
108 rc = crypto_cipher_encrypt(tfm, sgd, &sg, sg.length);
110 rc = crypto_cipher_decrypt(tfm, sgd, &sg, sg.length);
114 if (desc->bd_enc_pages)
115 desc->bd_iov[i].kiov_page = desc->bd_enc_pages[i];
117 /* although the procedure might be lengthy, the crypto functions
118 * internally called cond_resched() from time to time.
122 crypto_free_tfm(tfm);
125 bsd->bsd_priv_alg = alg;
130 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
131 struct ptlrpc_request *req,
132 struct ptlrpc_bulk_desc *desc)
134 struct gss_cli_ctx *gctx;
135 struct lustre_msg *msg;
136 struct ptlrpc_bulk_sec_desc *bsdr;
137 struct sec_flavor_config *conf;
141 LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
142 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
144 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
145 case SPTLRPC_SVC_NULL:
146 LASSERT(req->rq_reqbuf->lm_bufcount >= 3);
147 msg = req->rq_reqbuf;
148 offset = msg->lm_bufcount - 1;
150 case SPTLRPC_SVC_AUTH:
151 case SPTLRPC_SVC_INTG:
152 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
153 msg = req->rq_reqbuf;
154 offset = msg->lm_bufcount - 2;
156 case SPTLRPC_SVC_PRIV:
157 LASSERT(req->rq_clrbuf->lm_bufcount >= 2);
158 msg = req->rq_clrbuf;
159 offset = msg->lm_bufcount - 1;
166 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
167 rc = bulk_csum_cli_request(desc, req->rq_bulk_read, conf->sfc_bulk_csum,
170 CERROR("client bulk %s: failed to generate checksum: %d\n",
171 req->rq_bulk_read ? "read" : "write", rc);
175 if (conf->sfc_bulk_priv == BULK_PRIV_ALG_NULL)
178 /* previous bulk_csum_cli_request() has verified bsdr is good */
179 bsdr = lustre_msg_buf(msg, offset, 0);
181 if (req->rq_bulk_read) {
182 bsdr->bsd_priv_alg = conf->sfc_bulk_priv;
186 /* it turn out to be bulk write */
187 rc = sptlrpc_enc_pool_get_pages(desc);
189 CERROR("bulk write: failed to allocate encryption pages\n");
193 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
194 LASSERT(gctx->gc_mechctx);
196 rc = do_bulk_privacy(gctx->gc_mechctx, desc, 1,
197 conf->sfc_bulk_priv, bsdr);
199 CERROR("bulk write: client failed to encrypt pages\n");
204 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
205 struct ptlrpc_request *req,
206 struct ptlrpc_bulk_desc *desc)
208 struct gss_cli_ctx *gctx;
209 struct lustre_msg *rmsg, *vmsg;
210 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
214 LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
215 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
217 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
218 case SPTLRPC_SVC_NULL:
219 vmsg = req->rq_repbuf;
220 voff = vmsg->lm_bufcount - 1;
221 LASSERT(vmsg && vmsg->lm_bufcount >= 3);
223 rmsg = req->rq_reqbuf;
224 roff = rmsg->lm_bufcount - 1; /* last segment */
225 LASSERT(rmsg && rmsg->lm_bufcount >= 3);
227 case SPTLRPC_SVC_AUTH:
228 case SPTLRPC_SVC_INTG:
229 vmsg = req->rq_repbuf;
230 voff = vmsg->lm_bufcount - 2;
231 LASSERT(vmsg && vmsg->lm_bufcount >= 4);
233 rmsg = req->rq_reqbuf;
234 roff = rmsg->lm_bufcount - 2; /* second last segment */
235 LASSERT(rmsg && rmsg->lm_bufcount >= 4);
237 case SPTLRPC_SVC_PRIV:
238 vmsg = req->rq_repbuf;
239 voff = vmsg->lm_bufcount - 1;
240 LASSERT(vmsg && vmsg->lm_bufcount >= 2);
242 rmsg = req->rq_clrbuf;
243 roff = rmsg->lm_bufcount - 1; /* last segment */
244 LASSERT(rmsg && rmsg->lm_bufcount >= 2);
250 if (req->rq_bulk_read) {
251 bsdr = lustre_msg_buf(rmsg, roff, 0);
252 if (bsdr->bsd_priv_alg == BULK_PRIV_ALG_NULL)
255 bsdv = lustre_msg_buf(vmsg, voff, 0);
256 if (bsdr->bsd_priv_alg != bsdv->bsd_priv_alg) {
257 CERROR("bulk read: cipher algorithm mismatch: client "
258 "request %s but server reply with %s. try to "
259 "use the new one for decryption\n",
260 sptlrpc_bulk_priv_alg2name(bsdr->bsd_priv_alg),
261 sptlrpc_bulk_priv_alg2name(bsdv->bsd_priv_alg));
264 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
265 LASSERT(gctx->gc_mechctx);
267 rc = do_bulk_privacy(gctx->gc_mechctx, desc, 0,
268 bsdv->bsd_priv_alg, bsdv);
270 CERROR("bulk read: client failed to decrypt data\n");
276 rc = bulk_csum_cli_reply(desc, req->rq_bulk_read,
277 rmsg, roff, vmsg, voff);
281 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
282 struct ptlrpc_bulk_desc *desc)
284 struct gss_svc_reqctx *grctx;
288 LASSERT(req->rq_svc_ctx);
289 LASSERT(req->rq_bulk_write);
291 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
293 LASSERT(grctx->src_reqbsd);
294 LASSERT(grctx->src_repbsd);
295 LASSERT(grctx->src_ctx);
296 LASSERT(grctx->src_ctx->gsc_mechctx);
298 /* decrypt bulk data if it's encrypted */
299 if (grctx->src_reqbsd->bsd_priv_alg != BULK_PRIV_ALG_NULL) {
300 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 0,
301 grctx->src_reqbsd->bsd_priv_alg,
304 CERROR("bulk write: server failed to decrypt data\n");
309 /* verify bulk data checksum */
310 rc = bulk_csum_svc(desc, req->rq_bulk_read,
311 grctx->src_reqbsd, grctx->src_reqbsd_size,
312 grctx->src_repbsd, grctx->src_repbsd_size);
317 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
318 struct ptlrpc_bulk_desc *desc)
320 struct gss_svc_reqctx *grctx;
324 LASSERT(req->rq_svc_ctx);
325 LASSERT(req->rq_bulk_read);
327 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
329 LASSERT(grctx->src_reqbsd);
330 LASSERT(grctx->src_repbsd);
331 LASSERT(grctx->src_ctx);
332 LASSERT(grctx->src_ctx->gsc_mechctx);
334 /* generate bulk data checksum */
335 rc = bulk_csum_svc(desc, req->rq_bulk_read,
336 grctx->src_reqbsd, grctx->src_reqbsd_size,
337 grctx->src_repbsd, grctx->src_repbsd_size);
341 /* encrypt bulk data if required */
342 if (grctx->src_reqbsd->bsd_priv_alg != BULK_PRIV_ALG_NULL) {
343 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 1,
344 grctx->src_reqbsd->bsd_priv_alg,
347 CERROR("bulk read: server failed to encrypt data: "