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/mutex.h>
34 #include <linux/crypto.h>
36 #include <liblustre.h>
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre/lustre_idl.h>
43 #include <lustre_net.h>
44 #include <lustre_import.h>
45 #include <lustre_sec.h>
48 #include "gss_internal.h"
52 int do_bulk_privacy(struct gss_ctx *gctx,
53 struct ptlrpc_bulk_desc *desc,
54 int encrypt, __u32 alg,
55 struct ptlrpc_bulk_sec_desc *bsd)
57 struct crypto_tfm *tfm;
58 struct scatterlist sg, sg2, *sgd;
60 __u8 local_iv[sizeof(bsd->bsd_iv)];
62 LASSERT(alg < BULK_PRIV_ALG_MAX);
65 bsd->bsd_priv_alg = BULK_PRIV_ALG_NULL;
67 if (alg == BULK_PRIV_ALG_NULL)
71 get_random_bytes(bsd->bsd_iv, sizeof(bsd->bsd_iv));
73 /* compute the secret iv */
74 lgss_plain_encrypt(gctx, sizeof(local_iv), bsd->bsd_iv, local_iv);
76 tfm = crypto_alloc_tfm(sptlrpc_bulk_priv_alg2name(alg),
77 sptlrpc_bulk_priv_alg2flags(alg));
79 CERROR("Failed to allocate TFM %s\n",
80 sptlrpc_bulk_priv_alg2name(alg));
84 rc = crypto_cipher_setkey(tfm, local_iv, sizeof(local_iv));
86 CERROR("Failed to set key for TFM %s: %d\n",
87 sptlrpc_bulk_priv_alg2name(alg), rc);
92 for (i = 0; i < desc->bd_iov_count; i++) {
93 sg.page = desc->bd_iov[i].kiov_page;
94 sg.offset = desc->bd_iov[i].kiov_offset;
95 sg.length = desc->bd_iov[i].kiov_len;
97 if (desc->bd_enc_pages) {
98 sg2.page = desc->bd_enc_pages[i];
99 sg2.offset = desc->bd_iov[i].kiov_offset;
100 sg2.length = desc->bd_iov[i].kiov_len;
107 rc = crypto_cipher_encrypt(tfm, sgd, &sg, sg.length);
109 rc = crypto_cipher_decrypt(tfm, sgd, &sg, sg.length);
113 if (desc->bd_enc_pages)
114 desc->bd_iov[i].kiov_page = desc->bd_enc_pages[i];
116 /* although the procedure might be lengthy, the crypto functions
117 * internally called cond_resched() from time to time.
121 crypto_free_tfm(tfm);
124 bsd->bsd_priv_alg = alg;
129 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
130 struct ptlrpc_request *req,
131 struct ptlrpc_bulk_desc *desc)
133 struct gss_cli_ctx *gctx;
134 struct lustre_msg *msg;
135 struct ptlrpc_bulk_sec_desc *bsdr;
136 struct sec_flavor_config *conf;
140 LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
141 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
143 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
144 case SPTLRPC_SVC_AUTH:
145 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
146 msg = req->rq_reqbuf;
147 offset = msg->lm_bufcount - 2;
149 case SPTLRPC_SVC_PRIV:
150 LASSERT(req->rq_clrbuf->lm_bufcount >= 2);
151 msg = req->rq_clrbuf;
152 offset = msg->lm_bufcount - 1;
159 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
160 rc = bulk_csum_cli_request(desc, req->rq_bulk_read, conf->sfc_bulk_csum,
163 CERROR("client bulk %s: failed to generate checksum: %d\n",
164 req->rq_bulk_read ? "read" : "write", rc);
168 if (conf->sfc_bulk_priv == BULK_PRIV_ALG_NULL)
171 /* previous bulk_csum_cli_request() has verified bsdr is good */
172 bsdr = lustre_msg_buf(msg, offset, 0);
174 if (req->rq_bulk_read) {
175 bsdr->bsd_priv_alg = conf->sfc_bulk_priv;
179 /* it turn out to be bulk write */
180 rc = sptlrpc_enc_pool_get_pages(desc);
182 CERROR("bulk write: failed to allocate encryption pages\n");
186 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
187 LASSERT(gctx->gc_mechctx);
189 rc = do_bulk_privacy(gctx->gc_mechctx, desc, 1,
190 conf->sfc_bulk_priv, bsdr);
192 CERROR("bulk write: client failed to encrypt pages\n");
197 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
198 struct ptlrpc_request *req,
199 struct ptlrpc_bulk_desc *desc)
201 struct gss_cli_ctx *gctx;
202 struct lustre_msg *rmsg, *vmsg;
203 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
207 LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
208 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
210 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
211 case SPTLRPC_SVC_AUTH:
212 vmsg = req->rq_repbuf;
213 voff = vmsg->lm_bufcount - 2;
214 LASSERT(vmsg && vmsg->lm_bufcount >= 4);
216 rmsg = req->rq_reqbuf;
217 roff = rmsg->lm_bufcount - 2; /* second last segment */
218 LASSERT(rmsg && rmsg->lm_bufcount >= 4);
220 case SPTLRPC_SVC_PRIV:
221 vmsg = req->rq_repbuf;
222 voff = vmsg->lm_bufcount - 1;
223 LASSERT(vmsg && vmsg->lm_bufcount >= 2);
225 rmsg = req->rq_clrbuf;
226 roff = rmsg->lm_bufcount - 1; /* last segment */
227 LASSERT(rmsg && rmsg->lm_bufcount >= 2);
233 if (req->rq_bulk_read) {
234 bsdr = lustre_msg_buf(rmsg, roff, 0);
235 if (bsdr->bsd_priv_alg == BULK_PRIV_ALG_NULL)
238 bsdv = lustre_msg_buf(vmsg, voff, 0);
239 if (bsdr->bsd_priv_alg != bsdv->bsd_priv_alg) {
240 CERROR("bulk read: cipher algorithm mismatch: client "
241 "request %s but server reply with %s. try to "
242 "use the new one for decryption\n",
243 sptlrpc_bulk_priv_alg2name(bsdr->bsd_priv_alg),
244 sptlrpc_bulk_priv_alg2name(bsdv->bsd_priv_alg));
247 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
248 LASSERT(gctx->gc_mechctx);
250 rc = do_bulk_privacy(gctx->gc_mechctx, desc, 0,
251 bsdv->bsd_priv_alg, bsdv);
253 CERROR("bulk read: client failed to decrypt data\n");
259 rc = bulk_csum_cli_reply(desc, req->rq_bulk_read,
260 rmsg, roff, vmsg, voff);
264 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
265 struct ptlrpc_bulk_desc *desc)
267 struct ptlrpc_reply_state *rs = req->rq_reply_state;
268 struct gss_svc_reqctx *grctx;
269 struct ptlrpc_bulk_sec_desc *bsdv;
274 LASSERT(req->rq_bulk_write);
276 if (SEC_FLAVOR_SVC(req->rq_sec_flavor) == SPTLRPC_SVC_PRIV) {
277 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
278 LASSERT(rs->rs_repbuf->lm_bufcount >= 2);
279 voff = req->rq_reqbuf->lm_bufcount - 1;
280 roff = rs->rs_repbuf->lm_bufcount - 1;
282 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
283 LASSERT(rs->rs_repbuf->lm_bufcount >= 4);
284 voff = req->rq_reqbuf->lm_bufcount - 2;
285 roff = rs->rs_repbuf->lm_bufcount - 2;
288 bsdv = lustre_msg_buf(req->rq_reqbuf, voff, sizeof(*bsdv));
289 if (bsdv->bsd_priv_alg != BULK_PRIV_ALG_NULL) {
290 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
291 LASSERT(grctx->src_ctx);
292 LASSERT(grctx->src_ctx->gsc_mechctx);
294 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 0,
295 bsdv->bsd_priv_alg, bsdv);
297 CERROR("bulk write: server failed to decrypt data\n");
302 rc = bulk_csum_svc(desc, req->rq_bulk_read,
303 req->rq_reqbuf, voff, rs->rs_repbuf, roff);
308 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
309 struct ptlrpc_bulk_desc *desc)
311 struct ptlrpc_reply_state *rs = req->rq_reply_state;
312 struct gss_svc_reqctx *grctx;
313 struct ptlrpc_bulk_sec_desc *bsdv, *bsdr;
318 LASSERT(req->rq_bulk_read);
320 if (SEC_FLAVOR_SVC(req->rq_sec_flavor) == SPTLRPC_SVC_PRIV) {
321 voff = req->rq_reqbuf->lm_bufcount - 1;
322 roff = rs->rs_repbuf->lm_bufcount - 1;
324 voff = req->rq_reqbuf->lm_bufcount - 2;
325 roff = rs->rs_repbuf->lm_bufcount - 2;
328 rc = bulk_csum_svc(desc, req->rq_bulk_read,
329 req->rq_reqbuf, voff, rs->rs_repbuf, roff);
333 bsdv = lustre_msg_buf(req->rq_reqbuf, voff, sizeof(*bsdv));
334 if (bsdv->bsd_priv_alg != BULK_PRIV_ALG_NULL) {
335 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
336 LASSERT(grctx->src_ctx);
337 LASSERT(grctx->src_ctx->gsc_mechctx);
339 bsdr = lustre_msg_buf(rs->rs_repbuf, roff, sizeof(*bsdr));
341 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 1,
342 bsdv->bsd_priv_alg, bsdr);
344 CERROR("bulk read: server failed to encrypt data\n");