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(sptlrpc_bulk_priv_alg2name(alg),
76                                sptlrpc_bulk_priv_alg2flags(alg));
77         if (tfm == NULL) {
78                 CERROR("Failed to allocate TFM %s\n",
79                        sptlrpc_bulk_priv_alg2name(alg));
80                 return -ENOMEM;
81         }
82
83         rc = crypto_cipher_setkey(tfm, local_iv, sizeof(local_iv));
84         if (rc) {
85                 CERROR("Failed to set key for TFM %s: %d\n",
86                        sptlrpc_bulk_priv_alg2name(alg), rc);
87                 crypto_free_tfm(tfm);
88                 return rc;
89         }
90
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;
95
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;
100
101                         sgd = &sg2;
102                 } else
103                         sgd = &sg;
104
105                 if (encrypt)
106                         rc = crypto_cipher_encrypt(tfm, sgd, &sg, sg.length);
107                 else
108                         rc = crypto_cipher_decrypt(tfm, sgd, &sg, sg.length);
109
110                 LASSERT(rc == 0);
111
112                 if (desc->bd_enc_pages)
113                         desc->bd_iov[i].kiov_page = desc->bd_enc_pages[i];
114
115                 /* although the procedure might be lengthy, the crypto functions
116                  * internally called cond_resched() from time to time.
117                  */
118         }
119
120         crypto_free_tfm(tfm);
121
122         if (encrypt)
123                 bsd->bsd_priv_alg = alg;
124
125         return 0;
126 }
127
128 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
129                           struct ptlrpc_request *req,
130                           struct ptlrpc_bulk_desc *desc)
131 {
132         struct gss_cli_ctx              *gctx;
133         struct lustre_msg               *msg;
134         struct ptlrpc_bulk_sec_desc     *bsdr;
135         struct sec_flavor_config        *conf;
136         int                              offset, rc;
137         ENTRY;
138
139         LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
140         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
141
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;
147                 break;
148         case SPTLRPC_SVC_PRIV:
149                 LASSERT(req->rq_clrbuf->lm_bufcount >= 2);
150                 msg = req->rq_clrbuf;
151                 offset = msg->lm_bufcount - 1;
152                 break;
153         default:
154                 LBUG();
155         }
156
157         /* make checksum */
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,
160                                    msg, offset);
161         if (rc) {
162                 CERROR("client bulk %s: failed to generate checksum: %d\n",
163                        req->rq_bulk_read ? "read" : "write", rc);
164                 RETURN(rc);
165         }
166
167         if (conf->sfc_bulk_priv == BULK_PRIV_ALG_NULL)
168                 RETURN(0);
169
170         /* previous bulk_csum_cli_request() has verified bsdr is good */
171         bsdr = lustre_msg_buf(msg, offset, 0);
172
173         if (req->rq_bulk_read) {
174                 bsdr->bsd_priv_alg = conf->sfc_bulk_priv;
175                 RETURN(0);
176         }
177
178         /* it turn out to be bulk write */
179         rc = sptlrpc_enc_pool_get_pages(desc);
180         if (rc) {
181                 CERROR("bulk write: failed to allocate encryption pages\n");
182                 RETURN(rc);
183         }
184
185         gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
186         LASSERT(gctx->gc_mechctx);
187
188         rc = do_bulk_privacy(gctx->gc_mechctx, desc, 1,
189                              conf->sfc_bulk_priv, bsdr);
190         if (rc)
191                 CERROR("bulk write: client failed to encrypt pages\n");
192
193         RETURN(rc);
194 }
195
196 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
197                             struct ptlrpc_request *req,
198                             struct ptlrpc_bulk_desc *desc)
199 {
200         struct gss_cli_ctx              *gctx;
201         struct lustre_msg               *rmsg, *vmsg;
202         struct ptlrpc_bulk_sec_desc     *bsdr, *bsdv;
203         int                              roff, voff, rc;
204         ENTRY;
205
206         LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
207         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
208
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);
214
215                 rmsg = req->rq_reqbuf;
216                 roff = rmsg->lm_bufcount - 2; /* second last segment */
217                 LASSERT(rmsg && rmsg->lm_bufcount >= 4);
218                 break;
219         case SPTLRPC_SVC_PRIV:
220                 vmsg = req->rq_repbuf;
221                 voff = vmsg->lm_bufcount - 1;
222                 LASSERT(vmsg && vmsg->lm_bufcount >= 2);
223
224                 rmsg = req->rq_clrbuf;
225                 roff = rmsg->lm_bufcount - 1; /* last segment */
226                 LASSERT(rmsg && rmsg->lm_bufcount >= 2);
227                 break;
228         default:
229                 LBUG();
230         }
231
232         if (req->rq_bulk_read) {
233                 bsdr = lustre_msg_buf(rmsg, roff, 0);
234                 if (bsdr->bsd_priv_alg == BULK_PRIV_ALG_NULL)
235                         goto verify_csum;
236
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));
244                 }
245
246                 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
247                 LASSERT(gctx->gc_mechctx);
248
249                 rc = do_bulk_privacy(gctx->gc_mechctx, desc, 0,
250                                      bsdv->bsd_priv_alg, bsdv);
251                 if (rc) {
252                         CERROR("bulk read: client failed to decrypt data\n");
253                         RETURN(rc);
254                 }
255         }
256
257 verify_csum:
258         rc = bulk_csum_cli_reply(desc, req->rq_bulk_read,
259                                  rmsg, roff, vmsg, voff);
260         RETURN(rc);
261 }
262
263 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
264                         struct ptlrpc_bulk_desc *desc)
265 {
266         struct ptlrpc_reply_state    *rs = req->rq_reply_state;
267         struct gss_svc_reqctx        *grctx;
268         struct ptlrpc_bulk_sec_desc  *bsdv;
269         int                           voff, roff, rc;
270         ENTRY;
271
272         LASSERT(rs);
273         LASSERT(req->rq_bulk_write);
274
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;
280         } else {
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;
285         }
286
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);
292
293                 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 0,
294                                      bsdv->bsd_priv_alg, bsdv);
295                 if (rc) {
296                         CERROR("bulk write: server failed to decrypt data\n");
297                         RETURN(rc);
298                 }
299         }
300
301         rc = bulk_csum_svc(desc, req->rq_bulk_read,
302                            req->rq_reqbuf, voff, rs->rs_repbuf, roff);
303
304         RETURN(rc);
305 }
306
307 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
308                       struct ptlrpc_bulk_desc *desc)
309 {
310         struct ptlrpc_reply_state    *rs = req->rq_reply_state;
311         struct gss_svc_reqctx        *grctx;
312         struct ptlrpc_bulk_sec_desc  *bsdv, *bsdr;
313         int                           voff, roff, rc;
314         ENTRY;
315
316         LASSERT(rs);
317         LASSERT(req->rq_bulk_read);
318
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;
322         } else {
323                 voff = req->rq_reqbuf->lm_bufcount - 2;
324                 roff = rs->rs_repbuf->lm_bufcount - 2;
325         }
326
327         rc = bulk_csum_svc(desc, req->rq_bulk_read,
328                            req->rq_reqbuf, voff, rs->rs_repbuf, roff);
329         if (rc)
330                 RETURN(rc);
331
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);
337
338                 bsdr = lustre_msg_buf(rs->rs_repbuf, roff, sizeof(*bsdr));
339
340                 rc = do_bulk_privacy(grctx->src_ctx->gsc_mechctx, desc, 1,
341                                      bsdv->bsd_priv_alg, bsdr);
342                 if (rc)
343                         CERROR("bulk read: server failed to encrypt data\n");
344         }
345
346         RETURN(rc);
347 }
348