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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/gss/gss_bulk.c
37  *
38  * Author: Eric Mei <eric.mei@sun.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44 #define DEBUG_SUBSYSTEM S_SEC
45 #ifdef __KERNEL__
46 #include <linux/init.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/dcache.h>
50 #include <linux/fs.h>
51 #include <linux/random.h>
52 #include <linux/mutex.h>
53 #include <linux/crypto.h>
54 #else
55 #include <liblustre.h>
56 #endif
57
58 #include <obd.h>
59 #include <obd_class.h>
60 #include <obd_support.h>
61 #include <lustre/lustre_idl.h>
62 #include <lustre_net.h>
63 #include <lustre_import.h>
64 #include <lustre_sec.h>
65
66 #include "gss_err.h"
67 #include "gss_internal.h"
68 #include "gss_api.h"
69
70 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
71                           struct ptlrpc_request *req,
72                           struct ptlrpc_bulk_desc *desc)
73 {
74         struct gss_cli_ctx              *gctx;
75         struct lustre_msg               *msg;
76         struct ptlrpc_bulk_sec_desc     *bsd;
77         rawobj_t                         token;
78         __u32                            maj;
79         int                              offset;
80         int                              rc;
81         ENTRY;
82
83         LASSERT(req->rq_pack_bulk);
84         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
85
86         gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
87         LASSERT(gctx->gc_mechctx);
88
89         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
90         case SPTLRPC_SVC_NULL:
91                 LASSERT(req->rq_reqbuf->lm_bufcount >= 3);
92                 msg = req->rq_reqbuf;
93                 offset = msg->lm_bufcount - 1;
94                 break;
95         case SPTLRPC_SVC_AUTH:
96         case SPTLRPC_SVC_INTG:
97                 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
98                 msg = req->rq_reqbuf;
99                 offset = msg->lm_bufcount - 2;
100                 break;
101         case SPTLRPC_SVC_PRIV:
102                 LASSERT(req->rq_clrbuf->lm_bufcount >= 2);
103                 msg = req->rq_clrbuf;
104                 offset = msg->lm_bufcount - 1;
105                 break;
106         default:
107                 LBUG();
108         }
109
110         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
111         bsd->bsd_version = 0;
112         bsd->bsd_flags = 0;
113         bsd->bsd_type = SPTLRPC_BULK_DEFAULT;
114         bsd->bsd_svc = SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc);
115
116         if (bsd->bsd_svc == SPTLRPC_BULK_SVC_NULL)
117                 RETURN(0);
118
119         LASSERT(bsd->bsd_svc == SPTLRPC_BULK_SVC_INTG ||
120                 bsd->bsd_svc == SPTLRPC_BULK_SVC_PRIV);
121
122         if (req->rq_bulk_read) {
123                 /*
124                  * bulk read: prepare receiving pages only for privacy mode.
125                  */
126                 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_PRIV)
127                         return gss_cli_prep_bulk(req, desc);
128         } else {
129                 /*
130                  * bulk write: sign or encrypt bulk pages.
131                  */
132                 bsd->bsd_nob = desc->bd_nob;
133
134                 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_INTG) {
135                         /* integrity mode */
136                         token.data = bsd->bsd_data;
137                         token.len = lustre_msg_buflen(msg, offset) -
138                                     sizeof(*bsd);
139
140                         maj = lgss_get_mic(gctx->gc_mechctx, 0, NULL,
141                                            desc->bd_iov_count, desc->bd_iov,
142                                            &token);
143                         if (maj != GSS_S_COMPLETE) {
144                                 CWARN("failed to sign bulk data: %x\n", maj);
145                                 RETURN(-EACCES);
146                         }
147                 } else {
148                         /* privacy mode */
149                         if (desc->bd_iov_count == 0)
150                                 RETURN(0);
151
152                         rc = sptlrpc_enc_pool_get_pages(desc);
153                         if (rc) {
154                                 CERROR("bulk write: failed to allocate "
155                                        "encryption pages: %d\n", rc);
156                                 RETURN(rc);
157                         }
158
159                         token.data = bsd->bsd_data;
160                         token.len = lustre_msg_buflen(msg, offset) -
161                                     sizeof(*bsd);
162
163                         maj = lgss_wrap_bulk(gctx->gc_mechctx, desc, &token, 0);
164                         if (maj != GSS_S_COMPLETE) {
165                                 CWARN("fail to encrypt bulk data: %x\n", maj);
166                                 RETURN(-EACCES);
167                         }
168                 }
169         }
170
171         RETURN(0);
172 }
173
174 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
175                             struct ptlrpc_request *req,
176                             struct ptlrpc_bulk_desc *desc)
177 {
178         struct gss_cli_ctx              *gctx;
179         struct lustre_msg               *rmsg, *vmsg;
180         struct ptlrpc_bulk_sec_desc     *bsdr, *bsdv;
181         rawobj_t                         token;
182         __u32                            maj;
183         int                              roff, voff;
184         ENTRY;
185
186         LASSERT(req->rq_pack_bulk);
187         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
188
189         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
190         case SPTLRPC_SVC_NULL:
191                 vmsg = req->rq_repdata;
192                 voff = vmsg->lm_bufcount - 1;
193                 LASSERT(vmsg && vmsg->lm_bufcount >= 3);
194
195                 rmsg = req->rq_reqbuf;
196                 roff = rmsg->lm_bufcount - 1; /* last segment */
197                 LASSERT(rmsg && rmsg->lm_bufcount >= 3);
198                 break;
199         case SPTLRPC_SVC_AUTH:
200         case SPTLRPC_SVC_INTG:
201                 vmsg = req->rq_repdata;
202                 voff = vmsg->lm_bufcount - 2;
203                 LASSERT(vmsg && vmsg->lm_bufcount >= 4);
204
205                 rmsg = req->rq_reqbuf;
206                 roff = rmsg->lm_bufcount - 2; /* second last segment */
207                 LASSERT(rmsg && rmsg->lm_bufcount >= 4);
208                 break;
209         case SPTLRPC_SVC_PRIV:
210                 vmsg = req->rq_repdata;
211                 voff = vmsg->lm_bufcount - 1;
212                 LASSERT(vmsg && vmsg->lm_bufcount >= 2);
213
214                 rmsg = req->rq_clrbuf;
215                 roff = rmsg->lm_bufcount - 1; /* last segment */
216                 LASSERT(rmsg && rmsg->lm_bufcount >= 2);
217                 break;
218         default:
219                 LBUG();
220         }
221
222         bsdr = lustre_msg_buf(rmsg, roff, sizeof(*bsdr));
223         bsdv = lustre_msg_buf(vmsg, voff, sizeof(*bsdv));
224         LASSERT(bsdr && bsdv);
225
226         if (bsdr->bsd_version != bsdv->bsd_version ||
227             bsdr->bsd_type != bsdv->bsd_type ||
228             bsdr->bsd_svc != bsdv->bsd_svc) {
229                 CERROR("bulk security descriptor mismatch: "
230                        "(%u,%u,%u) != (%u,%u,%u)\n",
231                        bsdr->bsd_version, bsdr->bsd_type, bsdr->bsd_svc,
232                        bsdv->bsd_version, bsdv->bsd_type, bsdv->bsd_svc);
233                 RETURN(-EPROTO);
234         }
235
236         LASSERT(bsdv->bsd_svc == SPTLRPC_BULK_SVC_NULL ||
237                 bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG ||
238                 bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV);
239
240         /*
241          * in privacy mode if return success, make sure bd_nob_transferred
242          * is the actual size of the clear text, otherwise upper layer
243          * may be surprised.
244          */
245         if (req->rq_bulk_write) {
246                 if (bsdv->bsd_flags & BSD_FL_ERR) {
247                         CERROR("server reported bulk i/o failure\n");
248                         RETURN(-EIO);
249                 }
250
251                 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV)
252                         desc->bd_nob_transferred = desc->bd_nob;
253         } else {
254                 /*
255                  * bulk read, upon return success, bd_nob_transferred is
256                  * the size of plain text actually received.
257                  */
258                 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
259                 LASSERT(gctx->gc_mechctx);
260
261                 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG) {
262                         int i, nob;
263
264                         /* fix the actual data size */
265                         for (i = 0, nob = 0; i < desc->bd_iov_count; i++) {
266                                 if (desc->bd_iov[i].kiov_len + nob >
267                                     desc->bd_nob_transferred) {
268                                         desc->bd_iov[i].kiov_len =
269                                                 desc->bd_nob_transferred - nob;
270                                 }
271                                 nob += desc->bd_iov[i].kiov_len;
272                         }
273
274                         token.data = bsdv->bsd_data;
275                         token.len = lustre_msg_buflen(vmsg, voff) -
276                                     sizeof(*bsdv);
277
278                         maj = lgss_verify_mic(gctx->gc_mechctx, 0, NULL,
279                                               desc->bd_iov_count, desc->bd_iov,
280                                               &token);
281                         if (maj != GSS_S_COMPLETE) {
282                                 CERROR("failed to verify bulk read: %x\n", maj);
283                                 RETURN(-EACCES);
284                         }
285                 } else if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV) {
286                         desc->bd_nob = bsdv->bsd_nob;
287                         if (desc->bd_nob == 0)
288                                 RETURN(0);
289
290                         token.data = bsdv->bsd_data;
291                         token.len = lustre_msg_buflen(vmsg, voff) -
292                                     sizeof(*bsdr);
293
294                         maj = lgss_unwrap_bulk(gctx->gc_mechctx, desc,
295                                                &token, 1);
296                         if (maj != GSS_S_COMPLETE) {
297                                 CERROR("failed to decrypt bulk read: %x\n",
298                                        maj);
299                                 RETURN(-EACCES);
300                         }
301
302                         desc->bd_nob_transferred = desc->bd_nob;
303                 }
304         }
305
306         RETURN(0);
307 }
308
309 static int gss_prep_bulk(struct ptlrpc_bulk_desc *desc,
310                          struct gss_ctx *mechctx)
311 {
312         int     rc;
313
314         if (desc->bd_iov_count == 0)
315                 return 0;
316
317         rc = sptlrpc_enc_pool_get_pages(desc);
318         if (rc)
319                 return rc;
320
321         if (lgss_prep_bulk(mechctx, desc) != GSS_S_COMPLETE)
322                 return -EACCES;
323
324         return 0;
325 }
326
327 int gss_cli_prep_bulk(struct ptlrpc_request *req,
328                       struct ptlrpc_bulk_desc *desc)
329 {
330         int             rc;
331         ENTRY;
332
333         LASSERT(req->rq_cli_ctx);
334         LASSERT(req->rq_pack_bulk);
335         LASSERT(req->rq_bulk_read);
336
337         if (SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_BULK_SVC_PRIV)
338                 RETURN(0);
339
340         rc = gss_prep_bulk(desc, ctx2gctx(req->rq_cli_ctx)->gc_mechctx);
341         if (rc)
342                 CERROR("bulk read: failed to prepare encryption "
343                        "pages: %d\n", rc);
344
345         RETURN(rc);
346 }
347
348 int gss_svc_prep_bulk(struct ptlrpc_request *req,
349                       struct ptlrpc_bulk_desc *desc)
350 {
351         struct gss_svc_reqctx        *grctx;
352         struct ptlrpc_bulk_sec_desc  *bsd;
353         int                           rc;
354         ENTRY;
355
356         LASSERT(req->rq_svc_ctx);
357         LASSERT(req->rq_pack_bulk);
358         LASSERT(req->rq_bulk_write);
359
360         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
361         LASSERT(grctx->src_reqbsd);
362         LASSERT(grctx->src_repbsd);
363         LASSERT(grctx->src_ctx);
364         LASSERT(grctx->src_ctx->gsc_mechctx);
365
366         bsd = grctx->src_reqbsd;
367         if (bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)
368                 RETURN(0);
369
370         rc = gss_prep_bulk(desc, grctx->src_ctx->gsc_mechctx);
371         if (rc)
372                 CERROR("bulk write: failed to prepare encryption "
373                        "pages: %d\n", rc);
374
375         RETURN(rc);
376 }
377
378 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
379                         struct ptlrpc_bulk_desc *desc)
380 {
381         struct gss_svc_reqctx        *grctx;
382         struct ptlrpc_bulk_sec_desc  *bsdr, *bsdv;
383         rawobj_t                      token;
384         __u32                         maj;
385         ENTRY;
386
387         LASSERT(req->rq_svc_ctx);
388         LASSERT(req->rq_pack_bulk);
389         LASSERT(req->rq_bulk_write);
390
391         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
392
393         LASSERT(grctx->src_reqbsd);
394         LASSERT(grctx->src_repbsd);
395         LASSERT(grctx->src_ctx);
396         LASSERT(grctx->src_ctx->gsc_mechctx);
397
398         bsdr = grctx->src_reqbsd;
399         bsdv = grctx->src_repbsd;
400
401         /* bsdr has been sanity checked during unpacking */
402         bsdv->bsd_version = 0;
403         bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
404         bsdv->bsd_svc = bsdr->bsd_svc;
405         bsdv->bsd_flags = 0;
406
407         switch (bsdv->bsd_svc) {
408         case SPTLRPC_BULK_SVC_INTG:
409                 token.data = bsdr->bsd_data;
410                 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
411
412                 maj = lgss_verify_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
413                                       desc->bd_iov_count, desc->bd_iov, &token);
414                 if (maj != GSS_S_COMPLETE) {
415                         bsdv->bsd_flags |= BSD_FL_ERR;
416                         CERROR("failed to verify bulk signature: %x\n", maj);
417                         RETURN(-EACCES);
418                 }
419                 break;
420         case SPTLRPC_BULK_SVC_PRIV:
421                 if (bsdr->bsd_nob != desc->bd_nob) {
422                         bsdv->bsd_flags |= BSD_FL_ERR;
423                         CERROR("prepared nob %d doesn't match the actual "
424                                "nob %d\n", desc->bd_nob, bsdr->bsd_nob);
425                         RETURN(-EPROTO);
426                 }
427
428                 if (desc->bd_iov_count == 0) {
429                         LASSERT(desc->bd_nob == 0);
430                         break;
431                 }
432
433                 token.data = bsdr->bsd_data;
434                 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
435
436                 maj = lgss_unwrap_bulk(grctx->src_ctx->gsc_mechctx,
437                                        desc, &token, 0);
438                 if (maj != GSS_S_COMPLETE) {
439                         bsdv->bsd_flags |= BSD_FL_ERR;
440                         CERROR("failed decrypt bulk data: %x\n", maj);
441                         RETURN(-EACCES);
442                 }
443                 break;
444         }
445
446         RETURN(0);
447 }
448
449 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
450                       struct ptlrpc_bulk_desc *desc)
451 {
452         struct gss_svc_reqctx        *grctx;
453         struct ptlrpc_bulk_sec_desc  *bsdr, *bsdv;
454         rawobj_t                      token;
455         __u32                         maj;
456         int                           rc;
457         ENTRY;
458
459         LASSERT(req->rq_svc_ctx);
460         LASSERT(req->rq_pack_bulk);
461         LASSERT(req->rq_bulk_read);
462
463         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
464
465         LASSERT(grctx->src_reqbsd);
466         LASSERT(grctx->src_repbsd);
467         LASSERT(grctx->src_ctx);
468         LASSERT(grctx->src_ctx->gsc_mechctx);
469
470         bsdr = grctx->src_reqbsd;
471         bsdv = grctx->src_repbsd;
472
473         /* bsdr has been sanity checked during unpacking */
474         bsdv->bsd_version = 0;
475         bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
476         bsdv->bsd_svc = bsdr->bsd_svc;
477         bsdv->bsd_flags = 0;
478
479         switch (bsdv->bsd_svc) {
480         case SPTLRPC_BULK_SVC_INTG:
481                 token.data = bsdv->bsd_data;
482                 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
483
484                 maj = lgss_get_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
485                                    desc->bd_iov_count, desc->bd_iov, &token);
486                 if (maj != GSS_S_COMPLETE) {
487                         bsdv->bsd_flags |= BSD_FL_ERR;
488                         CERROR("failed to sign bulk data: %x\n", maj);
489                         RETURN(-EACCES);
490                 }
491                 break;
492         case SPTLRPC_BULK_SVC_PRIV:
493                 bsdv->bsd_nob = desc->bd_nob;
494
495                 if (desc->bd_iov_count == 0) {
496                         LASSERT(desc->bd_nob == 0);
497                         break;
498                 }
499
500                 rc = sptlrpc_enc_pool_get_pages(desc);
501                 if (rc) {
502                         bsdv->bsd_flags |= BSD_FL_ERR;
503                         CERROR("bulk read: failed to allocate encryption "
504                                "pages: %d\n", rc);
505                         RETURN(rc);
506                 }
507
508                 token.data = bsdv->bsd_data;
509                 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
510
511                 maj = lgss_wrap_bulk(grctx->src_ctx->gsc_mechctx,
512                                      desc, &token, 1);
513                 if (maj != GSS_S_COMPLETE) {
514                         bsdv->bsd_flags |= BSD_FL_ERR;
515                         CERROR("failed to encrypt bulk data: %x\n", maj);
516                         RETURN(-EACCES);
517                 }
518                 break;
519         }
520
521         RETURN(0);
522 }