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