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