Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_bulk.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2006 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_SEC
26 #ifdef __KERNEL__
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/dcache.h>
31 #include <linux/fs.h>
32 #include <linux/random.h>
33 #include <linux/crypto.h>
34 #else
35 #include <liblustre.h>
36 #endif
37
38 #include <obd.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>
45
46 #include "gss_err.h"
47 #include "gss_internal.h"
48 #include "gss_api.h"
49
50 static
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)
55 {
56         struct crypto_tfm  *tfm;
57         struct scatterlist  sg, sg2, *sgd;
58         int                 i, rc;
59         __u8                local_iv[sizeof(bsd->bsd_iv)];
60
61         LASSERT(alg < BULK_PRIV_ALG_MAX);
62
63         if (encrypt)
64                 bsd->bsd_priv_alg = BULK_PRIV_ALG_NULL;
65
66         if (alg == BULK_PRIV_ALG_NULL)
67                 return 0;
68
69         if (encrypt)
70                 get_random_bytes(bsd->bsd_iv, sizeof(bsd->bsd_iv));
71
72         /* compute the secret iv */
73         lgss_plain_encrypt(gctx, sizeof(local_iv), bsd->bsd_iv, local_iv);
74
75         tfm = crypto_alloc_tfm(priv_types[alg].name, priv_types[alg].flags);
76         if (tfm == NULL) {
77                 CERROR("Failed to allocate TFM %s\n", priv_types[alg].name);
78                 return -ENOMEM;
79         }
80
81         rc = crypto_cipher_setkey(tfm, local_iv, sizeof(local_iv));
82         if (rc) {
83                 CERROR("Failed to set key for TFM %s: %d\n",
84                        priv_types[alg].name, rc);
85                 crypto_free_tfm(tfm);
86                 return rc;
87         }
88
89         for (i = 0; i < desc->bd_iov_count; i++) {
90                 sg.page = desc->bd_iov[i].kiov_page;
91                 sg.offset = desc->bd_iov[i].kiov_offset;
92                 sg.length = desc->bd_iov[i].kiov_len;
93
94                 if (desc->bd_enc_iov) {
95                         sg2.page = desc->bd_enc_iov[i].kiov_page;
96                         sg2.offset = desc->bd_enc_iov[i].kiov_offset;
97                         sg2.length = desc->bd_enc_iov[i].kiov_len;
98
99                         sgd = &sg2;
100                 } else
101                         sgd = &sg;
102
103                 if (encrypt)
104                         rc = crypto_cipher_encrypt(tfm, sgd, &sg, sg.length);
105                 else
106                         rc = crypto_cipher_decrypt(tfm, sgd, &sg, sg.length);
107
108                 LASSERT(rc == 0);
109
110                 /* although the procedure might be lengthy, the crypto functions
111                  * internally called cond_resched() from time to time.
112                  */
113         }
114
115         crypto_free_tfm(tfm);
116
117         if (encrypt)
118                 bsd->bsd_priv_alg = alg;
119
120         return 0;
121 }
122
123 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
124                           struct ptlrpc_request *req,
125                           struct ptlrpc_bulk_desc *desc)
126 {
127         struct gss_cli_ctx              *gctx;
128         struct lustre_msg               *msg;
129         struct ptlrpc_bulk_sec_desc     *bsdr;
130         struct sec_flavor_config        *conf;
131         int                              offset, rc;
132         ENTRY;
133
134         LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
135         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
136
137         switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
138         case SPTLRPC_SVC_AUTH:
139                 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
140                 msg = req->rq_reqbuf;
141                 offset = msg->lm_bufcount - 2;
142                 break;
143         case SPTLRPC_SVC_PRIV:
144                 LASSERT(req->rq_clrbuf->lm_bufcount >= 2);
145                 msg = req->rq_clrbuf;
146                 offset = msg->lm_bufcount - 1;
147                 break;
148         default:
149                 LBUG();
150         }
151
152         /* make checksum */
153         conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
154         rc = bulk_csum_cli_request(desc, req->rq_bulk_read, conf->sfc_bulk_csum,
155                                    msg, offset);
156         if (rc) {
157                 CERROR("client bulk %s: failed to generate checksum: %d\n",
158                        req->rq_bulk_read ? "read" : "write", rc);
159                 RETURN(rc);
160         }
161
162         if (conf->sfc_bulk_priv == BULK_PRIV_ALG_NULL)
163                 RETURN(0);
164
165         /* previous bulk_csum_cli_request() has verified bsdr is good */
166         bsdr = lustre_msg_buf(msg, offset, 0);
167
168         if (req->rq_bulk_read) {
169                 bsdr->bsd_priv_alg = conf->sfc_bulk_priv;
170                 RETURN(0);
171         }
172
173         /* it turn out to be bulk write */
174         rc = sptlrpc_enc_pool_get_pages(desc);
175         if (rc) {
176                 CERROR("bulk write: failed to allocate encryption pages\n");
177                 RETURN(rc);
178         }
179
180         gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
181         LASSERT(gctx->gc_mechctx);
182
183         rc = do_bulk_privacy(gctx->gc_mechctx, desc, 1,
184                              conf->sfc_bulk_priv, bsdr);
185         if (rc)
186                 CERROR("bulk write: client failed to encrypt pages\n");
187
188         RETURN(rc);
189 }
190
191 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
192                             struct ptlrpc_request *req,
193                             struct ptlrpc_bulk_desc *desc)
194 {
195         struct gss_cli_ctx              *gctx;
196         struct lustre_msg               *rmsg, *vmsg;
197         struct ptlrpc_bulk_sec_desc     *bsdr, *bsdv;
198         int                              roff, voff, rc;
199         ENTRY;
200
201         LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
202         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
203
204         switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
205         case SPTLRPC_SVC_AUTH:
206                 vmsg = req->rq_repbuf;
207                 voff = vmsg->lm_bufcount - 2;
208                 LASSERT(vmsg && vmsg->lm_bufcount >= 4);
209
210                 rmsg = req->rq_reqbuf;
211                 roff = rmsg->lm_bufcount - 2; /* second last segment */
212                 LASSERT(rmsg && rmsg->lm_bufcount >= 4);
213                 break;
214         case SPTLRPC_SVC_PRIV:
215                 vmsg = req->rq_repbuf;
216                 voff = vmsg->lm_bufcount - 1;
217                 LASSERT(vmsg && vmsg->lm_bufcount >= 2);
218
219                 rmsg = req->rq_clrbuf;
220                 roff = rmsg->lm_bufcount - 1; /* last segment */
221                 LASSERT(rmsg && rmsg->lm_bufcount >= 2);
222                 break;
223         default:
224                 LBUG();
225         }
226
227         if (req->rq_bulk_read) {
228                 bsdr = lustre_msg_buf(rmsg, roff, 0);
229                 if (bsdr->bsd_priv_alg == BULK_PRIV_ALG_NULL)
230                         goto verify_csum;
231
232                 bsdv = lustre_msg_buf(vmsg, voff, 0);
233                 if (bsdr->bsd_priv_alg != bsdv->bsd_priv_alg) {
234                         CERROR("bulk read: cipher algorithm mismatch: client "
235                                "request %s but server reply with %s. try to "
236                                "use the new one for decryption\n",
237                                sptlrpc_bulk_priv_alg2name(bsdr->bsd_priv_alg),
238                                sptlrpc_bulk_priv_alg2name(bsdv->bsd_priv_alg));
239                 }
240
241                 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
242                 LASSERT(gctx->gc_mechctx);
243
244                 rc = do_bulk_privacy(gctx->gc_mechctx, desc, 0,
245                                      bsdv->bsd_priv_alg, bsdv);
246                 if (rc) {
247                         CERROR("bulk read: client failed to decrypt data\n");
248                         RETURN(rc);
249                 }
250         }
251
252 verify_csum:
253         rc = bulk_csum_cli_reply(desc, req->rq_bulk_read,
254                                  rmsg, roff, vmsg, voff);
255         RETURN(rc);
256 }
257
258 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
259                         struct ptlrpc_bulk_desc *desc)
260 {
261         struct ptlrpc_reply_state    *rs = req->rq_reply_state;
262         struct gss_svc_reqctx        *grctx;
263         struct ptlrpc_bulk_sec_desc  *bsdv;
264         int                           voff, roff, rc;
265         ENTRY;
266
267         LASSERT(rs);
268         LASSERT(req->rq_bulk_write);
269
270         if (SEC_FLAVOR_SVC(req->rq_sec_flavor) == SPTLRPC_SVC_PRIV) {
271                 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
272                 LASSERT(rs->rs_repbuf->lm_bufcount >= 2);
273                 voff = req->rq_reqbuf->lm_bufcount - 1;
274                 roff = rs->rs_repbuf->lm_bufcount - 1;
275         } else {
276                 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
277                 LASSERT(rs->rs_repbuf->lm_bufcount >= 4);
278                 voff = req->rq_reqbuf->lm_bufcount - 2;
279                 roff = rs->rs_repbuf->lm_bufcount - 2;
280         }
281
282         bsdv = lustre_msg_buf(req->rq_reqbuf, voff, sizeof(*bsdv));
283         if (bsdv->bsd_priv_alg != BULK_PRIV_ALG_NULL) {
284                 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
285                 LASSERT(grctx->src_ctx);
286                 LASSERT(grctx->src_ctx->gsc_mechctx);
287
288                 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 0,
289                                      bsdv->bsd_priv_alg, bsdv);
290                 if (rc) {
291                         CERROR("bulk write: server failed to decrypt data\n");
292                         RETURN(rc);
293                 }
294         }
295
296         rc = bulk_csum_svc(desc, req->rq_bulk_read,
297                            req->rq_reqbuf, voff, rs->rs_repbuf, roff);
298
299         RETURN(rc);
300 }
301
302 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
303                       struct ptlrpc_bulk_desc *desc)
304 {
305         struct ptlrpc_reply_state    *rs = req->rq_reply_state;
306         struct gss_svc_reqctx        *grctx;
307         struct ptlrpc_bulk_sec_desc  *bsdv, *bsdr;
308         int                           voff, roff, rc;
309         ENTRY;
310
311         LASSERT(rs);
312         LASSERT(req->rq_bulk_read);
313
314         if (SEC_FLAVOR_SVC(req->rq_sec_flavor) == SPTLRPC_SVC_PRIV) {
315                 voff = req->rq_reqbuf->lm_bufcount - 1;
316                 roff = rs->rs_repbuf->lm_bufcount - 1;
317         } else {
318                 voff = req->rq_reqbuf->lm_bufcount - 2;
319                 roff = rs->rs_repbuf->lm_bufcount - 2;
320         }
321
322         rc = bulk_csum_svc(desc, req->rq_bulk_read,
323                            req->rq_reqbuf, voff, rs->rs_repbuf, roff);
324         if (rc)
325                 RETURN(rc);
326
327         bsdv = lustre_msg_buf(req->rq_reqbuf, voff, sizeof(*bsdv));
328         if (bsdv->bsd_priv_alg != BULK_PRIV_ALG_NULL) {
329                 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
330                 LASSERT(grctx->src_ctx);
331                 LASSERT(grctx->src_ctx->gsc_mechctx);
332
333                 bsdr = lustre_msg_buf(rs->rs_repbuf, roff, sizeof(*bsdr));
334
335                 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 1,
336                                      bsdv->bsd_priv_alg, bsdr);
337                 if (rc)
338                         CERROR("bulk read: server failed to encrypt data\n");
339         }
340
341         RETURN(rc);
342 }
343