Whamcloud - gitweb
b=18751 Move prng.c to libcfs
[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 (c) 2007, 2010, Oracle and/or its affiliates. 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/mutex.h>
52 #include <linux/crypto.h>
53 #else
54 #include <liblustre.h>
55 #endif
56
57 #include <obd.h>
58 #include <obd_class.h>
59 #include <obd_support.h>
60 #include <lustre/lustre_idl.h>
61 #include <lustre_net.h>
62 #include <lustre_import.h>
63 #include <lustre_sec.h>
64
65 #include "gss_err.h"
66 #include "gss_internal.h"
67 #include "gss_api.h"
68
69 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
70                           struct ptlrpc_request *req,
71                           struct ptlrpc_bulk_desc *desc)
72 {
73         struct gss_cli_ctx              *gctx;
74         struct lustre_msg               *msg;
75         struct ptlrpc_bulk_sec_desc     *bsd;
76         rawobj_t                         token;
77         __u32                            maj;
78         int                              offset;
79         int                              rc;
80         ENTRY;
81
82         LASSERT(req->rq_pack_bulk);
83         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
84
85         gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
86         LASSERT(gctx->gc_mechctx);
87
88         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
89         case SPTLRPC_SVC_NULL:
90                 LASSERT(req->rq_reqbuf->lm_bufcount >= 3);
91                 msg = req->rq_reqbuf;
92                 offset = msg->lm_bufcount - 1;
93                 break;
94         case SPTLRPC_SVC_AUTH:
95         case SPTLRPC_SVC_INTG:
96                 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
97                 msg = req->rq_reqbuf;
98                 offset = msg->lm_bufcount - 2;
99                 break;
100         case SPTLRPC_SVC_PRIV:
101                 LASSERT(req->rq_clrbuf->lm_bufcount >= 2);
102                 msg = req->rq_clrbuf;
103                 offset = msg->lm_bufcount - 1;
104                 break;
105         default:
106                 LBUG();
107         }
108
109         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
110         bsd->bsd_version = 0;
111         bsd->bsd_flags = 0;
112         bsd->bsd_type = SPTLRPC_BULK_DEFAULT;
113         bsd->bsd_svc = SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc);
114
115         if (bsd->bsd_svc == SPTLRPC_BULK_SVC_NULL)
116                 RETURN(0);
117
118         LASSERT(bsd->bsd_svc == SPTLRPC_BULK_SVC_INTG ||
119                 bsd->bsd_svc == SPTLRPC_BULK_SVC_PRIV);
120
121         if (req->rq_bulk_read) {
122                 /*
123                  * bulk read: prepare receiving pages only for privacy mode.
124                  */
125                 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_PRIV)
126                         return gss_cli_prep_bulk(req, desc);
127         } else {
128                 /*
129                  * bulk write: sign or encrypt bulk pages.
130                  */
131                 bsd->bsd_nob = desc->bd_nob;
132
133                 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_INTG) {
134                         /* integrity mode */
135                         token.data = bsd->bsd_data;
136                         token.len = lustre_msg_buflen(msg, offset) -
137                                     sizeof(*bsd);
138
139                         maj = lgss_get_mic(gctx->gc_mechctx, 0, NULL,
140                                            desc->bd_iov_count, desc->bd_iov,
141                                            &token);
142                         if (maj != GSS_S_COMPLETE) {
143                                 CWARN("failed to sign bulk data: %x\n", maj);
144                                 RETURN(-EACCES);
145                         }
146                 } else {
147                         /* privacy mode */
148                         if (desc->bd_iov_count == 0)
149                                 RETURN(0);
150
151                         rc = sptlrpc_enc_pool_get_pages(desc);
152                         if (rc) {
153                                 CERROR("bulk write: failed to allocate "
154                                        "encryption pages: %d\n", rc);
155                                 RETURN(rc);
156                         }
157
158                         token.data = bsd->bsd_data;
159                         token.len = lustre_msg_buflen(msg, offset) -
160                                     sizeof(*bsd);
161
162                         maj = lgss_wrap_bulk(gctx->gc_mechctx, desc, &token, 0);
163                         if (maj != GSS_S_COMPLETE) {
164                                 CWARN("fail to encrypt bulk data: %x\n", maj);
165                                 RETURN(-EACCES);
166                         }
167                 }
168         }
169
170         RETURN(0);
171 }
172
173 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
174                             struct ptlrpc_request *req,
175                             struct ptlrpc_bulk_desc *desc)
176 {
177         struct gss_cli_ctx              *gctx;
178         struct lustre_msg               *rmsg, *vmsg;
179         struct ptlrpc_bulk_sec_desc     *bsdr, *bsdv;
180         rawobj_t                         token;
181         __u32                            maj;
182         int                              roff, voff;
183         ENTRY;
184
185         LASSERT(req->rq_pack_bulk);
186         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
187
188         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
189         case SPTLRPC_SVC_NULL:
190                 vmsg = req->rq_repdata;
191                 voff = vmsg->lm_bufcount - 1;
192                 LASSERT(vmsg && vmsg->lm_bufcount >= 3);
193
194                 rmsg = req->rq_reqbuf;
195                 roff = rmsg->lm_bufcount - 1; /* last segment */
196                 LASSERT(rmsg && rmsg->lm_bufcount >= 3);
197                 break;
198         case SPTLRPC_SVC_AUTH:
199         case SPTLRPC_SVC_INTG:
200                 vmsg = req->rq_repdata;
201                 voff = vmsg->lm_bufcount - 2;
202                 LASSERT(vmsg && vmsg->lm_bufcount >= 4);
203
204                 rmsg = req->rq_reqbuf;
205                 roff = rmsg->lm_bufcount - 2; /* second last segment */
206                 LASSERT(rmsg && rmsg->lm_bufcount >= 4);
207                 break;
208         case SPTLRPC_SVC_PRIV:
209                 vmsg = req->rq_repdata;
210                 voff = vmsg->lm_bufcount - 1;
211                 LASSERT(vmsg && vmsg->lm_bufcount >= 2);
212
213                 rmsg = req->rq_clrbuf;
214                 roff = rmsg->lm_bufcount - 1; /* last segment */
215                 LASSERT(rmsg && rmsg->lm_bufcount >= 2);
216                 break;
217         default:
218                 LBUG();
219         }
220
221         bsdr = lustre_msg_buf(rmsg, roff, sizeof(*bsdr));
222         bsdv = lustre_msg_buf(vmsg, voff, sizeof(*bsdv));
223         LASSERT(bsdr && bsdv);
224
225         if (bsdr->bsd_version != bsdv->bsd_version ||
226             bsdr->bsd_type != bsdv->bsd_type ||
227             bsdr->bsd_svc != bsdv->bsd_svc) {
228                 CERROR("bulk security descriptor mismatch: "
229                        "(%u,%u,%u) != (%u,%u,%u)\n",
230                        bsdr->bsd_version, bsdr->bsd_type, bsdr->bsd_svc,
231                        bsdv->bsd_version, bsdv->bsd_type, bsdv->bsd_svc);
232                 RETURN(-EPROTO);
233         }
234
235         LASSERT(bsdv->bsd_svc == SPTLRPC_BULK_SVC_NULL ||
236                 bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG ||
237                 bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV);
238
239         /*
240          * in privacy mode if return success, make sure bd_nob_transferred
241          * is the actual size of the clear text, otherwise upper layer
242          * may be surprised.
243          */
244         if (req->rq_bulk_write) {
245                 if (bsdv->bsd_flags & BSD_FL_ERR) {
246                         CERROR("server reported bulk i/o failure\n");
247                         RETURN(-EIO);
248                 }
249
250                 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV)
251                         desc->bd_nob_transferred = desc->bd_nob;
252         } else {
253                 /*
254                  * bulk read, upon return success, bd_nob_transferred is
255                  * the size of plain text actually received.
256                  */
257                 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
258                 LASSERT(gctx->gc_mechctx);
259
260                 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG) {
261                         int i, nob;
262
263                         /* fix the actual data size */
264                         for (i = 0, nob = 0; i < desc->bd_iov_count; i++) {
265                                 if (desc->bd_iov[i].kiov_len + nob >
266                                     desc->bd_nob_transferred) {
267                                         desc->bd_iov[i].kiov_len =
268                                                 desc->bd_nob_transferred - nob;
269                                 }
270                                 nob += desc->bd_iov[i].kiov_len;
271                         }
272
273                         token.data = bsdv->bsd_data;
274                         token.len = lustre_msg_buflen(vmsg, voff) -
275                                     sizeof(*bsdv);
276
277                         maj = lgss_verify_mic(gctx->gc_mechctx, 0, NULL,
278                                               desc->bd_iov_count, desc->bd_iov,
279                                               &token);
280                         if (maj != GSS_S_COMPLETE) {
281                                 CERROR("failed to verify bulk read: %x\n", maj);
282                                 RETURN(-EACCES);
283                         }
284                 } else if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV) {
285                         desc->bd_nob = bsdv->bsd_nob;
286                         if (desc->bd_nob == 0)
287                                 RETURN(0);
288
289                         token.data = bsdv->bsd_data;
290                         token.len = lustre_msg_buflen(vmsg, voff) -
291                                     sizeof(*bsdr);
292
293                         maj = lgss_unwrap_bulk(gctx->gc_mechctx, desc,
294                                                &token, 1);
295                         if (maj != GSS_S_COMPLETE) {
296                                 CERROR("failed to decrypt bulk read: %x\n",
297                                        maj);
298                                 RETURN(-EACCES);
299                         }
300
301                         desc->bd_nob_transferred = desc->bd_nob;
302                 }
303         }
304
305         RETURN(0);
306 }
307
308 static int gss_prep_bulk(struct ptlrpc_bulk_desc *desc,
309                          struct gss_ctx *mechctx)
310 {
311         int     rc;
312
313         if (desc->bd_iov_count == 0)
314                 return 0;
315
316         rc = sptlrpc_enc_pool_get_pages(desc);
317         if (rc)
318                 return rc;
319
320         if (lgss_prep_bulk(mechctx, desc) != GSS_S_COMPLETE)
321                 return -EACCES;
322
323         return 0;
324 }
325
326 int gss_cli_prep_bulk(struct ptlrpc_request *req,
327                       struct ptlrpc_bulk_desc *desc)
328 {
329         int             rc;
330         ENTRY;
331
332         LASSERT(req->rq_cli_ctx);
333         LASSERT(req->rq_pack_bulk);
334         LASSERT(req->rq_bulk_read);
335
336         if (SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_BULK_SVC_PRIV)
337                 RETURN(0);
338
339         rc = gss_prep_bulk(desc, ctx2gctx(req->rq_cli_ctx)->gc_mechctx);
340         if (rc)
341                 CERROR("bulk read: failed to prepare encryption "
342                        "pages: %d\n", rc);
343
344         RETURN(rc);
345 }
346
347 int gss_svc_prep_bulk(struct ptlrpc_request *req,
348                       struct ptlrpc_bulk_desc *desc)
349 {
350         struct gss_svc_reqctx        *grctx;
351         struct ptlrpc_bulk_sec_desc  *bsd;
352         int                           rc;
353         ENTRY;
354
355         LASSERT(req->rq_svc_ctx);
356         LASSERT(req->rq_pack_bulk);
357         LASSERT(req->rq_bulk_write);
358
359         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
360         LASSERT(grctx->src_reqbsd);
361         LASSERT(grctx->src_repbsd);
362         LASSERT(grctx->src_ctx);
363         LASSERT(grctx->src_ctx->gsc_mechctx);
364
365         bsd = grctx->src_reqbsd;
366         if (bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)
367                 RETURN(0);
368
369         rc = gss_prep_bulk(desc, grctx->src_ctx->gsc_mechctx);
370         if (rc)
371                 CERROR("bulk write: failed to prepare encryption "
372                        "pages: %d\n", rc);
373
374         RETURN(rc);
375 }
376
377 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
378                         struct ptlrpc_bulk_desc *desc)
379 {
380         struct gss_svc_reqctx        *grctx;
381         struct ptlrpc_bulk_sec_desc  *bsdr, *bsdv;
382         rawobj_t                      token;
383         __u32                         maj;
384         ENTRY;
385
386         LASSERT(req->rq_svc_ctx);
387         LASSERT(req->rq_pack_bulk);
388         LASSERT(req->rq_bulk_write);
389
390         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
391
392         LASSERT(grctx->src_reqbsd);
393         LASSERT(grctx->src_repbsd);
394         LASSERT(grctx->src_ctx);
395         LASSERT(grctx->src_ctx->gsc_mechctx);
396
397         bsdr = grctx->src_reqbsd;
398         bsdv = grctx->src_repbsd;
399
400         /* bsdr has been sanity checked during unpacking */
401         bsdv->bsd_version = 0;
402         bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
403         bsdv->bsd_svc = bsdr->bsd_svc;
404         bsdv->bsd_flags = 0;
405
406         switch (bsdv->bsd_svc) {
407         case SPTLRPC_BULK_SVC_INTG:
408                 token.data = bsdr->bsd_data;
409                 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
410
411                 maj = lgss_verify_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
412                                       desc->bd_iov_count, desc->bd_iov, &token);
413                 if (maj != GSS_S_COMPLETE) {
414                         bsdv->bsd_flags |= BSD_FL_ERR;
415                         CERROR("failed to verify bulk signature: %x\n", maj);
416                         RETURN(-EACCES);
417                 }
418                 break;
419         case SPTLRPC_BULK_SVC_PRIV:
420                 if (bsdr->bsd_nob != desc->bd_nob) {
421                         bsdv->bsd_flags |= BSD_FL_ERR;
422                         CERROR("prepared nob %d doesn't match the actual "
423                                "nob %d\n", desc->bd_nob, bsdr->bsd_nob);
424                         RETURN(-EPROTO);
425                 }
426
427                 if (desc->bd_iov_count == 0) {
428                         LASSERT(desc->bd_nob == 0);
429                         break;
430                 }
431
432                 token.data = bsdr->bsd_data;
433                 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
434
435                 maj = lgss_unwrap_bulk(grctx->src_ctx->gsc_mechctx,
436                                        desc, &token, 0);
437                 if (maj != GSS_S_COMPLETE) {
438                         bsdv->bsd_flags |= BSD_FL_ERR;
439                         CERROR("failed decrypt bulk data: %x\n", maj);
440                         RETURN(-EACCES);
441                 }
442                 break;
443         }
444
445         RETURN(0);
446 }
447
448 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
449                       struct ptlrpc_bulk_desc *desc)
450 {
451         struct gss_svc_reqctx        *grctx;
452         struct ptlrpc_bulk_sec_desc  *bsdr, *bsdv;
453         rawobj_t                      token;
454         __u32                         maj;
455         int                           rc;
456         ENTRY;
457
458         LASSERT(req->rq_svc_ctx);
459         LASSERT(req->rq_pack_bulk);
460         LASSERT(req->rq_bulk_read);
461
462         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
463
464         LASSERT(grctx->src_reqbsd);
465         LASSERT(grctx->src_repbsd);
466         LASSERT(grctx->src_ctx);
467         LASSERT(grctx->src_ctx->gsc_mechctx);
468
469         bsdr = grctx->src_reqbsd;
470         bsdv = grctx->src_repbsd;
471
472         /* bsdr has been sanity checked during unpacking */
473         bsdv->bsd_version = 0;
474         bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
475         bsdv->bsd_svc = bsdr->bsd_svc;
476         bsdv->bsd_flags = 0;
477
478         switch (bsdv->bsd_svc) {
479         case SPTLRPC_BULK_SVC_INTG:
480                 token.data = bsdv->bsd_data;
481                 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
482
483                 maj = lgss_get_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
484                                    desc->bd_iov_count, desc->bd_iov, &token);
485                 if (maj != GSS_S_COMPLETE) {
486                         bsdv->bsd_flags |= BSD_FL_ERR;
487                         CERROR("failed to sign bulk data: %x\n", maj);
488                         RETURN(-EACCES);
489                 }
490                 break;
491         case SPTLRPC_BULK_SVC_PRIV:
492                 bsdv->bsd_nob = desc->bd_nob;
493
494                 if (desc->bd_iov_count == 0) {
495                         LASSERT(desc->bd_nob == 0);
496                         break;
497                 }
498
499                 rc = sptlrpc_enc_pool_get_pages(desc);
500                 if (rc) {
501                         bsdv->bsd_flags |= BSD_FL_ERR;
502                         CERROR("bulk read: failed to allocate encryption "
503                                "pages: %d\n", rc);
504                         RETURN(rc);
505                 }
506
507                 token.data = bsdv->bsd_data;
508                 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
509
510                 maj = lgss_wrap_bulk(grctx->src_ctx->gsc_mechctx,
511                                      desc, &token, 1);
512                 if (maj != GSS_S_COMPLETE) {
513                         bsdv->bsd_flags |= BSD_FL_ERR;
514                         CERROR("failed to encrypt bulk data: %x\n", maj);
515                         RETURN(-EACCES);
516                 }
517                 break;
518         }
519
520         RETURN(0);
521 }