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.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_SEC
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/dcache.h>
32 #include <linux/random.h>
33 #include <linux/crypto.h>
35 #include <liblustre.h>
39 #include <obd_class.h>
40 #include <obd_support.h>
41 #include <lustre/lustre_idl.h>
42 #include <lustre_net.h>
43 #include <lustre_import.h>
44 #include <lustre_sec.h>
47 #include "gss_internal.h"
51 int do_bulk_privacy(struct gss_ctx *gctx,
52 struct ptlrpc_bulk_desc *desc,
53 int encrypt, __u32 alg,
54 struct ptlrpc_bulk_sec_desc *bsd)
56 struct crypto_tfm *tfm;
57 struct scatterlist sg, sg2, *sgd;
59 __u8 local_iv[sizeof(bsd->bsd_iv)];
61 LASSERT(alg < BULK_PRIV_ALG_MAX);
64 bsd->bsd_priv_alg = BULK_PRIV_ALG_NULL;
66 if (alg == BULK_PRIV_ALG_NULL)
70 get_random_bytes(bsd->bsd_iv, sizeof(bsd->bsd_iv));
72 /* compute the secret iv */
73 lgss_plain_encrypt(gctx, sizeof(local_iv), bsd->bsd_iv, local_iv);
75 tfm = crypto_alloc_tfm(sptlrpc_bulk_priv_alg2name(alg),
76 sptlrpc_bulk_priv_alg2flags(alg));
78 CERROR("Failed to allocate TFM %s\n",
79 sptlrpc_bulk_priv_alg2name(alg));
83 rc = crypto_cipher_setkey(tfm, local_iv, sizeof(local_iv));
85 CERROR("Failed to set key for TFM %s: %d\n",
86 sptlrpc_bulk_priv_alg2name(alg), rc);
91 for (i = 0; i < desc->bd_iov_count; i++) {
92 sg.page = desc->bd_iov[i].kiov_page;
93 sg.offset = desc->bd_iov[i].kiov_offset;
94 sg.length = desc->bd_iov[i].kiov_len;
96 if (desc->bd_enc_pages) {
97 sg2.page = desc->bd_enc_pages[i];
98 sg2.offset = desc->bd_iov[i].kiov_offset;
99 sg2.length = desc->bd_iov[i].kiov_len;
106 rc = crypto_cipher_encrypt(tfm, sgd, &sg, sg.length);
108 rc = crypto_cipher_decrypt(tfm, sgd, &sg, sg.length);
112 if (desc->bd_enc_pages)
113 desc->bd_iov[i].kiov_page = desc->bd_enc_pages[i];
115 /* although the procedure might be lengthy, the crypto functions
116 * internally called cond_resched() from time to time.
120 crypto_free_tfm(tfm);
123 bsd->bsd_priv_alg = alg;
128 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
129 struct ptlrpc_request *req,
130 struct ptlrpc_bulk_desc *desc)
132 struct gss_cli_ctx *gctx;
133 struct lustre_msg *msg;
134 struct ptlrpc_bulk_sec_desc *bsdr;
135 struct sec_flavor_config *conf;
139 LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
140 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
142 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
143 case SPTLRPC_SVC_AUTH:
144 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
145 msg = req->rq_reqbuf;
146 offset = msg->lm_bufcount - 2;
148 case SPTLRPC_SVC_PRIV:
149 LASSERT(req->rq_clrbuf->lm_bufcount >= 2);
150 msg = req->rq_clrbuf;
151 offset = msg->lm_bufcount - 1;
158 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
159 rc = bulk_csum_cli_request(desc, req->rq_bulk_read, conf->sfc_bulk_csum,
162 CERROR("client bulk %s: failed to generate checksum: %d\n",
163 req->rq_bulk_read ? "read" : "write", rc);
167 if (conf->sfc_bulk_priv == BULK_PRIV_ALG_NULL)
170 /* previous bulk_csum_cli_request() has verified bsdr is good */
171 bsdr = lustre_msg_buf(msg, offset, 0);
173 if (req->rq_bulk_read) {
174 bsdr->bsd_priv_alg = conf->sfc_bulk_priv;
178 /* it turn out to be bulk write */
179 rc = sptlrpc_enc_pool_get_pages(desc);
181 CERROR("bulk write: failed to allocate encryption pages\n");
185 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
186 LASSERT(gctx->gc_mechctx);
188 rc = do_bulk_privacy(gctx->gc_mechctx, desc, 1,
189 conf->sfc_bulk_priv, bsdr);
191 CERROR("bulk write: client failed to encrypt pages\n");
196 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
197 struct ptlrpc_request *req,
198 struct ptlrpc_bulk_desc *desc)
200 struct gss_cli_ctx *gctx;
201 struct lustre_msg *rmsg, *vmsg;
202 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
206 LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
207 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
209 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
210 case SPTLRPC_SVC_AUTH:
211 vmsg = req->rq_repbuf;
212 voff = vmsg->lm_bufcount - 2;
213 LASSERT(vmsg && vmsg->lm_bufcount >= 4);
215 rmsg = req->rq_reqbuf;
216 roff = rmsg->lm_bufcount - 2; /* second last segment */
217 LASSERT(rmsg && rmsg->lm_bufcount >= 4);
219 case SPTLRPC_SVC_PRIV:
220 vmsg = req->rq_repbuf;
221 voff = vmsg->lm_bufcount - 1;
222 LASSERT(vmsg && vmsg->lm_bufcount >= 2);
224 rmsg = req->rq_clrbuf;
225 roff = rmsg->lm_bufcount - 1; /* last segment */
226 LASSERT(rmsg && rmsg->lm_bufcount >= 2);
232 if (req->rq_bulk_read) {
233 bsdr = lustre_msg_buf(rmsg, roff, 0);
234 if (bsdr->bsd_priv_alg == BULK_PRIV_ALG_NULL)
237 bsdv = lustre_msg_buf(vmsg, voff, 0);
238 if (bsdr->bsd_priv_alg != bsdv->bsd_priv_alg) {
239 CERROR("bulk read: cipher algorithm mismatch: client "
240 "request %s but server reply with %s. try to "
241 "use the new one for decryption\n",
242 sptlrpc_bulk_priv_alg2name(bsdr->bsd_priv_alg),
243 sptlrpc_bulk_priv_alg2name(bsdv->bsd_priv_alg));
246 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
247 LASSERT(gctx->gc_mechctx);
249 rc = do_bulk_privacy(gctx->gc_mechctx, desc, 0,
250 bsdv->bsd_priv_alg, bsdv);
252 CERROR("bulk read: client failed to decrypt data\n");
258 rc = bulk_csum_cli_reply(desc, req->rq_bulk_read,
259 rmsg, roff, vmsg, voff);
263 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
264 struct ptlrpc_bulk_desc *desc)
266 struct ptlrpc_reply_state *rs = req->rq_reply_state;
267 struct gss_svc_reqctx *grctx;
268 struct ptlrpc_bulk_sec_desc *bsdv;
273 LASSERT(req->rq_bulk_write);
275 if (SEC_FLAVOR_SVC(req->rq_sec_flavor) == SPTLRPC_SVC_PRIV) {
276 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
277 LASSERT(rs->rs_repbuf->lm_bufcount >= 2);
278 voff = req->rq_reqbuf->lm_bufcount - 1;
279 roff = rs->rs_repbuf->lm_bufcount - 1;
281 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
282 LASSERT(rs->rs_repbuf->lm_bufcount >= 4);
283 voff = req->rq_reqbuf->lm_bufcount - 2;
284 roff = rs->rs_repbuf->lm_bufcount - 2;
287 bsdv = lustre_msg_buf(req->rq_reqbuf, voff, sizeof(*bsdv));
288 if (bsdv->bsd_priv_alg != BULK_PRIV_ALG_NULL) {
289 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
290 LASSERT(grctx->src_ctx);
291 LASSERT(grctx->src_ctx->gsc_mechctx);
293 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 0,
294 bsdv->bsd_priv_alg, bsdv);
296 CERROR("bulk write: server failed to decrypt data\n");
301 rc = bulk_csum_svc(desc, req->rq_bulk_read,
302 req->rq_reqbuf, voff, rs->rs_repbuf, roff);
307 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
308 struct ptlrpc_bulk_desc *desc)
310 struct ptlrpc_reply_state *rs = req->rq_reply_state;
311 struct gss_svc_reqctx *grctx;
312 struct ptlrpc_bulk_sec_desc *bsdv, *bsdr;
317 LASSERT(req->rq_bulk_read);
319 if (SEC_FLAVOR_SVC(req->rq_sec_flavor) == SPTLRPC_SVC_PRIV) {
320 voff = req->rq_reqbuf->lm_bufcount - 1;
321 roff = rs->rs_repbuf->lm_bufcount - 1;
323 voff = req->rq_reqbuf->lm_bufcount - 2;
324 roff = rs->rs_repbuf->lm_bufcount - 2;
327 rc = bulk_csum_svc(desc, req->rq_bulk_read,
328 req->rq_reqbuf, voff, rs->rs_repbuf, roff);
332 bsdv = lustre_msg_buf(req->rq_reqbuf, voff, sizeof(*bsdv));
333 if (bsdv->bsd_priv_alg != BULK_PRIV_ALG_NULL) {
334 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
335 LASSERT(grctx->src_ctx);
336 LASSERT(grctx->src_ctx->gsc_mechctx);
338 bsdr = lustre_msg_buf(rs->rs_repbuf, roff, sizeof(*bsdr));
340 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 1,
341 bsdv->bsd_priv_alg, bsdr);
343 CERROR("bulk read: server failed to encrypt data\n");