Whamcloud - gitweb
6adce851130bbe05ab8a32e23cec0abd252c2c4c
[fs/lustre-release.git] / lustre / ptlrpc / sec_plain.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  * Copyright (c) 2011, 2012, Intel Corporation.
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/sec_plain.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42
43 #ifndef __KERNEL__
44 #include <liblustre.h>
45 #endif
46
47 #include <obd_support.h>
48 #include <obd_cksum.h>
49 #include <obd_class.h>
50 #include <lustre_net.h>
51 #include <lustre_sec.h>
52
53 struct plain_sec {
54         struct ptlrpc_sec       pls_base;
55         rwlock_t            pls_lock;
56         struct ptlrpc_cli_ctx  *pls_ctx;
57 };
58
59 static inline struct plain_sec *sec2plsec(struct ptlrpc_sec *sec)
60 {
61         return container_of(sec, struct plain_sec, pls_base);
62 }
63
64 static struct ptlrpc_sec_policy plain_policy;
65 static struct ptlrpc_ctx_ops    plain_ctx_ops;
66 static struct ptlrpc_svc_ctx    plain_svc_ctx;
67
68 static unsigned int plain_at_offset;
69
70 /*
71  * for simplicity, plain policy rpc use fixed layout.
72  */
73 #define PLAIN_PACK_SEGMENTS             (4)
74
75 #define PLAIN_PACK_HDR_OFF              (0)
76 #define PLAIN_PACK_MSG_OFF              (1)
77 #define PLAIN_PACK_USER_OFF             (2)
78 #define PLAIN_PACK_BULK_OFF             (3)
79
80 #define PLAIN_FL_USER                   (0x01)
81 #define PLAIN_FL_BULK                   (0x02)
82
83 struct plain_header {
84         __u8            ph_ver;            /* 0 */
85         __u8            ph_flags;
86         __u8            ph_sp;             /* source */
87         __u8            ph_bulk_hash_alg;  /* complete flavor desc */
88         __u8            ph_pad[4];
89 };
90
91 struct plain_bulk_token {
92         __u8            pbt_hash[8];
93 };
94
95 #define PLAIN_BSD_SIZE \
96         (sizeof(struct ptlrpc_bulk_sec_desc) + sizeof(struct plain_bulk_token))
97
98 /****************************************
99  * bulk checksum helpers                *
100  ****************************************/
101
102 static int plain_unpack_bsd(struct lustre_msg *msg, int swabbed)
103 {
104         struct ptlrpc_bulk_sec_desc *bsd;
105
106         if (bulk_sec_desc_unpack(msg, PLAIN_PACK_BULK_OFF, swabbed))
107                 return -EPROTO;
108
109         bsd = lustre_msg_buf(msg, PLAIN_PACK_BULK_OFF, PLAIN_BSD_SIZE);
110         if (bsd == NULL) {
111                 CERROR("bulk sec desc has short size %d\n",
112                        lustre_msg_buflen(msg, PLAIN_PACK_BULK_OFF));
113                 return -EPROTO;
114         }
115
116         if (bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
117             bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG) {
118                 CERROR("invalid bulk svc %u\n", bsd->bsd_svc);
119                 return -EPROTO;
120         }
121
122         return 0;
123 }
124
125 static int plain_generate_bulk_csum(struct ptlrpc_bulk_desc *desc,
126                                     __u8 hash_alg,
127                                     struct plain_bulk_token *token)
128 {
129         if (hash_alg == BULK_HASH_ALG_NULL)
130                 return 0;
131
132         memset(token->pbt_hash, 0, sizeof(token->pbt_hash));
133         return sptlrpc_get_bulk_checksum(desc, hash_alg, token->pbt_hash,
134                                          sizeof(token->pbt_hash));
135 }
136
137 static int plain_verify_bulk_csum(struct ptlrpc_bulk_desc *desc,
138                                   __u8 hash_alg,
139                                   struct plain_bulk_token *tokenr)
140 {
141         struct plain_bulk_token tokenv;
142         int                     rc;
143
144         if (hash_alg == BULK_HASH_ALG_NULL)
145                 return 0;
146
147         memset(&tokenv.pbt_hash, 0, sizeof(tokenv.pbt_hash));
148         rc = sptlrpc_get_bulk_checksum(desc, hash_alg, tokenv.pbt_hash,
149                                        sizeof(tokenv.pbt_hash));
150         if (rc)
151                 return rc;
152
153         if (memcmp(tokenr->pbt_hash, tokenv.pbt_hash, sizeof(tokenr->pbt_hash)))
154                 return -EACCES;
155         return 0;
156 }
157
158 #ifdef __KERNEL__
159 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
160 {
161         char           *ptr;
162         unsigned int    off, i;
163
164         for (i = 0; i < desc->bd_iov_count; i++) {
165                 if (desc->bd_iov[i].kiov_len == 0)
166                         continue;
167
168                 ptr = kmap(desc->bd_iov[i].kiov_page);
169                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
170                 ptr[off] ^= 0x1;
171                 kunmap(desc->bd_iov[i].kiov_page);
172                 return;
173         }
174 }
175 #else
176 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
177 {
178         unsigned int    i;
179
180         for (i = 0; i < desc->bd_iov_count; i++) {
181                 if (desc->bd_iov[i].iov_len == 0)
182                         continue;
183
184                 ((char *)desc->bd_iov[i].iov_base)[i] ^= 0x1;
185                 return;
186         }
187 }
188 #endif /* __KERNEL__ */
189
190 /****************************************
191  * cli_ctx apis                         *
192  ****************************************/
193
194 static
195 int plain_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
196 {
197         /* should never reach here */
198         LBUG();
199         return 0;
200 }
201
202 static
203 int plain_ctx_validate(struct ptlrpc_cli_ctx *ctx)
204 {
205         return 0;
206 }
207
208 static
209 int plain_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
210 {
211         struct lustre_msg   *msg = req->rq_reqbuf;
212         struct plain_header *phdr;
213         ENTRY;
214
215         msg->lm_secflvr = req->rq_flvr.sf_rpc;
216
217         phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, 0);
218         phdr->ph_ver = 0;
219         phdr->ph_flags = 0;
220         phdr->ph_sp = ctx->cc_sec->ps_part;
221         phdr->ph_bulk_hash_alg = req->rq_flvr.u_bulk.hash.hash_alg;
222
223         if (req->rq_pack_udesc)
224                 phdr->ph_flags |= PLAIN_FL_USER;
225         if (req->rq_pack_bulk)
226                 phdr->ph_flags |= PLAIN_FL_BULK;
227
228         req->rq_reqdata_len = lustre_msg_size_v2(msg->lm_bufcount,
229                                                  msg->lm_buflens);
230         RETURN(0);
231 }
232
233 static
234 int plain_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
235 {
236         struct lustre_msg   *msg = req->rq_repdata;
237         struct plain_header *phdr;
238         __u32                cksum;
239         int                  swabbed;
240         ENTRY;
241
242         if (msg->lm_bufcount != PLAIN_PACK_SEGMENTS) {
243                 CERROR("unexpected reply buf count %u\n", msg->lm_bufcount);
244                 RETURN(-EPROTO);
245         }
246
247         swabbed = ptlrpc_rep_need_swab(req);
248
249         phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
250         if (phdr == NULL) {
251                 CERROR("missing plain header\n");
252                 RETURN(-EPROTO);
253         }
254
255         if (phdr->ph_ver != 0) {
256                 CERROR("Invalid header version\n");
257                 RETURN(-EPROTO);
258         }
259
260         /* expect no user desc in reply */
261         if (phdr->ph_flags & PLAIN_FL_USER) {
262                 CERROR("Unexpected udesc flag in reply\n");
263                 RETURN(-EPROTO);
264         }
265
266         if (phdr->ph_bulk_hash_alg != req->rq_flvr.u_bulk.hash.hash_alg) {
267                 CERROR("reply bulk flavor %u != %u\n", phdr->ph_bulk_hash_alg,
268                        req->rq_flvr.u_bulk.hash.hash_alg);
269                 RETURN(-EPROTO);
270         }
271
272         if (unlikely(req->rq_early)) {
273                 unsigned int hsize = 4;
274
275                 cfs_crypto_hash_digest(CFS_HASH_ALG_CRC32,
276                                 lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0),
277                                 lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF),
278                                 NULL, 0, (unsigned char *)&cksum, &hsize);
279                 if (cksum != msg->lm_cksum) {
280                         CDEBUG(D_SEC,
281                                "early reply checksum mismatch: %08x != %08x\n",
282                                cpu_to_le32(cksum), msg->lm_cksum);
283                         RETURN(-EINVAL);
284                 }
285         } else {
286                 /* whether we sent with bulk or not, we expect the same
287                  * in reply, except for early reply */
288                 if (!req->rq_early &&
289                     !equi(req->rq_pack_bulk == 1,
290                           phdr->ph_flags & PLAIN_FL_BULK)) {
291                         CERROR("%s bulk checksum in reply\n",
292                                req->rq_pack_bulk ? "Missing" : "Unexpected");
293                         RETURN(-EPROTO);
294                 }
295
296                 if (phdr->ph_flags & PLAIN_FL_BULK) {
297                         if (plain_unpack_bsd(msg, swabbed))
298                                 RETURN(-EPROTO);
299                 }
300         }
301
302         req->rq_repmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
303         req->rq_replen = lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF);
304         RETURN(0);
305 }
306
307 static
308 int plain_cli_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
309                         struct ptlrpc_request *req,
310                         struct ptlrpc_bulk_desc *desc)
311 {
312         struct ptlrpc_bulk_sec_desc *bsd;
313         struct plain_bulk_token     *token;
314         int                          rc;
315
316         LASSERT(req->rq_pack_bulk);
317         LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
318
319         bsd = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
320         token = (struct plain_bulk_token *) bsd->bsd_data;
321
322         bsd->bsd_version = 0;
323         bsd->bsd_flags = 0;
324         bsd->bsd_type = SPTLRPC_BULK_DEFAULT;
325         bsd->bsd_svc = SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc);
326
327         if (bsd->bsd_svc == SPTLRPC_BULK_SVC_NULL)
328                 RETURN(0);
329
330         if (req->rq_bulk_read)
331                 RETURN(0);
332
333         rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
334                                       token);
335         if (rc) {
336                 CERROR("bulk write: failed to compute checksum: %d\n", rc);
337         } else {
338                 /*
339                  * for sending we only compute the wrong checksum instead
340                  * of corrupting the data so it is still correct on a redo
341                  */
342                 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_SEND) &&
343                     req->rq_flvr.u_bulk.hash.hash_alg != BULK_HASH_ALG_NULL)
344                         token->pbt_hash[0] ^= 0x1;
345         }
346
347         return rc;
348 }
349
350 static
351 int plain_cli_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
352                           struct ptlrpc_request *req,
353                           struct ptlrpc_bulk_desc *desc)
354 {
355         struct ptlrpc_bulk_sec_desc *bsdv;
356         struct plain_bulk_token     *tokenv;
357         int                          rc;
358 #ifdef __KERNEL__
359         int                          i, nob;
360 #endif
361
362         LASSERT(req->rq_pack_bulk);
363         LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
364         LASSERT(req->rq_repdata->lm_bufcount == PLAIN_PACK_SEGMENTS);
365
366         bsdv = lustre_msg_buf(req->rq_repdata, PLAIN_PACK_BULK_OFF, 0);
367         tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
368
369         if (req->rq_bulk_write) {
370                 if (bsdv->bsd_flags & BSD_FL_ERR)
371                         return -EIO;
372                 return 0;
373         }
374
375 #ifdef __KERNEL__
376         /* fix the actual data size */
377         for (i = 0, nob = 0; i < desc->bd_iov_count; i++) {
378                 if (desc->bd_iov[i].kiov_len + nob > desc->bd_nob_transferred) {
379                         desc->bd_iov[i].kiov_len =
380                                 desc->bd_nob_transferred - nob;
381                 }
382                 nob += desc->bd_iov[i].kiov_len;
383         }
384 #endif
385
386         rc = plain_verify_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
387                                     tokenv);
388         if (rc)
389                 CERROR("bulk read: client verify failed: %d\n", rc);
390
391         return rc;
392 }
393
394 /****************************************
395  * sec apis                             *
396  ****************************************/
397
398 static
399 struct ptlrpc_cli_ctx *plain_sec_install_ctx(struct plain_sec *plsec)
400 {
401         struct ptlrpc_cli_ctx  *ctx, *ctx_new;
402
403         OBD_ALLOC_PTR(ctx_new);
404
405         write_lock(&plsec->pls_lock);
406
407         ctx = plsec->pls_ctx;
408         if (ctx) {
409                 atomic_inc(&ctx->cc_refcount);
410
411                 if (ctx_new)
412                         OBD_FREE_PTR(ctx_new);
413         } else if (ctx_new) {
414                 ctx = ctx_new;
415
416                 atomic_set(&ctx->cc_refcount, 1);       /* for cache */
417                 ctx->cc_sec = &plsec->pls_base;
418                 ctx->cc_ops = &plain_ctx_ops;
419                 ctx->cc_expire = 0;
420                 ctx->cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_UPTODATE;
421                 ctx->cc_vcred.vc_uid = 0;
422                 spin_lock_init(&ctx->cc_lock);
423                 CFS_INIT_LIST_HEAD(&ctx->cc_req_list);
424                 CFS_INIT_LIST_HEAD(&ctx->cc_gc_chain);
425
426                 plsec->pls_ctx = ctx;
427                 atomic_inc(&plsec->pls_base.ps_nctx);
428                 atomic_inc(&plsec->pls_base.ps_refcount);
429
430                 atomic_inc(&ctx->cc_refcount);  /* for caller */
431         }
432
433         write_unlock(&plsec->pls_lock);
434
435         return ctx;
436 }
437
438 static
439 void plain_destroy_sec(struct ptlrpc_sec *sec)
440 {
441         struct plain_sec *plsec = sec2plsec(sec);
442         ENTRY;
443
444         LASSERT(sec->ps_policy == &plain_policy);
445         LASSERT(sec->ps_import);
446         LASSERT(atomic_read(&sec->ps_refcount) == 0);
447         LASSERT(atomic_read(&sec->ps_nctx) == 0);
448         LASSERT(plsec->pls_ctx == NULL);
449
450         class_import_put(sec->ps_import);
451
452         OBD_FREE_PTR(plsec);
453         EXIT;
454 }
455
456 static
457 void plain_kill_sec(struct ptlrpc_sec *sec)
458 {
459         sec->ps_dying = 1;
460 }
461
462 static
463 struct ptlrpc_sec *plain_create_sec(struct obd_import *imp,
464                                     struct ptlrpc_svc_ctx *svc_ctx,
465                                     struct sptlrpc_flavor *sf)
466 {
467         struct plain_sec       *plsec;
468         struct ptlrpc_sec      *sec;
469         struct ptlrpc_cli_ctx  *ctx;
470         ENTRY;
471
472         LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN);
473
474         OBD_ALLOC_PTR(plsec);
475         if (plsec == NULL)
476                 RETURN(NULL);
477
478         /*
479          * initialize plain_sec
480          */
481         rwlock_init(&plsec->pls_lock);
482         plsec->pls_ctx = NULL;
483
484         sec = &plsec->pls_base;
485         sec->ps_policy = &plain_policy;
486         atomic_set(&sec->ps_refcount, 0);
487         atomic_set(&sec->ps_nctx, 0);
488         sec->ps_id = sptlrpc_get_next_secid();
489         sec->ps_import = class_import_get(imp);
490         sec->ps_flvr = *sf;
491         spin_lock_init(&sec->ps_lock);
492         CFS_INIT_LIST_HEAD(&sec->ps_gc_list);
493         sec->ps_gc_interval = 0;
494         sec->ps_gc_next = 0;
495
496         /* install ctx immediately if this is a reverse sec */
497         if (svc_ctx) {
498                 ctx = plain_sec_install_ctx(plsec);
499                 if (ctx == NULL) {
500                         plain_destroy_sec(sec);
501                         RETURN(NULL);
502                 }
503                 sptlrpc_cli_ctx_put(ctx, 1);
504         }
505
506         RETURN(sec);
507 }
508
509 static
510 struct ptlrpc_cli_ctx *plain_lookup_ctx(struct ptlrpc_sec *sec,
511                                         struct vfs_cred *vcred,
512                                         int create, int remove_dead)
513 {
514         struct plain_sec       *plsec = sec2plsec(sec);
515         struct ptlrpc_cli_ctx  *ctx;
516         ENTRY;
517
518         read_lock(&plsec->pls_lock);
519         ctx = plsec->pls_ctx;
520         if (ctx)
521                 atomic_inc(&ctx->cc_refcount);
522         read_unlock(&plsec->pls_lock);
523
524         if (unlikely(ctx == NULL))
525                 ctx = plain_sec_install_ctx(plsec);
526
527         RETURN(ctx);
528 }
529
530 static
531 void plain_release_ctx(struct ptlrpc_sec *sec,
532                        struct ptlrpc_cli_ctx *ctx, int sync)
533 {
534         LASSERT(atomic_read(&sec->ps_refcount) > 0);
535         LASSERT(atomic_read(&sec->ps_nctx) > 0);
536         LASSERT(atomic_read(&ctx->cc_refcount) == 0);
537         LASSERT(ctx->cc_sec == sec);
538
539         OBD_FREE_PTR(ctx);
540
541         atomic_dec(&sec->ps_nctx);
542         sptlrpc_sec_put(sec);
543 }
544
545 static
546 int plain_flush_ctx_cache(struct ptlrpc_sec *sec,
547                           uid_t uid, int grace, int force)
548 {
549         struct plain_sec       *plsec = sec2plsec(sec);
550         struct ptlrpc_cli_ctx  *ctx;
551         ENTRY;
552
553         /* do nothing unless caller want to flush for 'all' */
554         if (uid != -1)
555                 RETURN(0);
556
557         write_lock(&plsec->pls_lock);
558         ctx = plsec->pls_ctx;
559         plsec->pls_ctx = NULL;
560         write_unlock(&plsec->pls_lock);
561
562         if (ctx)
563                 sptlrpc_cli_ctx_put(ctx, 1);
564         RETURN(0);
565 }
566
567 static
568 int plain_alloc_reqbuf(struct ptlrpc_sec *sec,
569                        struct ptlrpc_request *req,
570                        int msgsize)
571 {
572         __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
573         int   alloc_len;
574         ENTRY;
575
576         buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
577         buflens[PLAIN_PACK_MSG_OFF] = msgsize;
578
579         if (req->rq_pack_udesc)
580                 buflens[PLAIN_PACK_USER_OFF] = sptlrpc_current_user_desc_size();
581
582         if (req->rq_pack_bulk) {
583                 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
584                 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
585         }
586
587         alloc_len = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
588
589         if (!req->rq_reqbuf) {
590                 LASSERT(!req->rq_pool);
591
592                 alloc_len = size_roundup_power2(alloc_len);
593                 OBD_ALLOC_LARGE(req->rq_reqbuf, alloc_len);
594                 if (!req->rq_reqbuf)
595                         RETURN(-ENOMEM);
596
597                 req->rq_reqbuf_len = alloc_len;
598         } else {
599                 LASSERT(req->rq_pool);
600                 LASSERT(req->rq_reqbuf_len >= alloc_len);
601                 memset(req->rq_reqbuf, 0, alloc_len);
602         }
603
604         lustre_init_msg_v2(req->rq_reqbuf, PLAIN_PACK_SEGMENTS, buflens, NULL);
605         req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_MSG_OFF, 0);
606
607         if (req->rq_pack_udesc)
608                 sptlrpc_pack_user_desc(req->rq_reqbuf, PLAIN_PACK_USER_OFF);
609
610         RETURN(0);
611 }
612
613 static
614 void plain_free_reqbuf(struct ptlrpc_sec *sec,
615                        struct ptlrpc_request *req)
616 {
617         ENTRY;
618         if (!req->rq_pool) {
619                 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
620                 req->rq_reqbuf = NULL;
621                 req->rq_reqbuf_len = 0;
622         }
623         EXIT;
624 }
625
626 static
627 int plain_alloc_repbuf(struct ptlrpc_sec *sec,
628                        struct ptlrpc_request *req,
629                        int msgsize)
630 {
631         __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
632         int alloc_len;
633         ENTRY;
634
635         buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
636         buflens[PLAIN_PACK_MSG_OFF] = msgsize;
637
638         if (req->rq_pack_bulk) {
639                 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
640                 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
641         }
642
643         alloc_len = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
644
645         /* add space for early reply */
646         alloc_len += plain_at_offset;
647
648         alloc_len = size_roundup_power2(alloc_len);
649
650         OBD_ALLOC_LARGE(req->rq_repbuf, alloc_len);
651         if (!req->rq_repbuf)
652                 RETURN(-ENOMEM);
653
654         req->rq_repbuf_len = alloc_len;
655         RETURN(0);
656 }
657
658 static
659 void plain_free_repbuf(struct ptlrpc_sec *sec,
660                        struct ptlrpc_request *req)
661 {
662         ENTRY;
663         OBD_FREE_LARGE(req->rq_repbuf, req->rq_repbuf_len);
664         req->rq_repbuf = NULL;
665         req->rq_repbuf_len = 0;
666         EXIT;
667 }
668
669 static
670 int plain_enlarge_reqbuf(struct ptlrpc_sec *sec,
671                          struct ptlrpc_request *req,
672                          int segment, int newsize)
673 {
674         struct lustre_msg      *newbuf;
675         int                     oldsize;
676         int                     newmsg_size, newbuf_size;
677         ENTRY;
678
679         LASSERT(req->rq_reqbuf);
680         LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
681         LASSERT(lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_MSG_OFF, 0) ==
682                 req->rq_reqmsg);
683
684         /* compute new embedded msg size.  */
685         oldsize = req->rq_reqmsg->lm_buflens[segment];
686         req->rq_reqmsg->lm_buflens[segment] = newsize;
687         newmsg_size = lustre_msg_size_v2(req->rq_reqmsg->lm_bufcount,
688                                          req->rq_reqmsg->lm_buflens);
689         req->rq_reqmsg->lm_buflens[segment] = oldsize;
690
691         /* compute new wrapper msg size.  */
692         oldsize = req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF];
693         req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = newmsg_size;
694         newbuf_size = lustre_msg_size_v2(req->rq_reqbuf->lm_bufcount,
695                                          req->rq_reqbuf->lm_buflens);
696         req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = oldsize;
697
698         /* request from pool should always have enough buffer */
699         LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
700
701         if (req->rq_reqbuf_len < newbuf_size) {
702                 newbuf_size = size_roundup_power2(newbuf_size);
703
704                 OBD_ALLOC_LARGE(newbuf, newbuf_size);
705                 if (newbuf == NULL)
706                         RETURN(-ENOMEM);
707
708                 /* Must lock this, so that otherwise unprotected change of
709                  * rq_reqmsg is not racing with parallel processing of
710                  * imp_replay_list traversing threads. See LU-3333
711                  * This is a bandaid at best, we really need to deal with this
712                  * in request enlarging code before unpacking that's already
713                  * there */
714                 if (req->rq_import)
715                         spin_lock(&req->rq_import->imp_lock);
716
717                 memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
718
719                 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
720                 req->rq_reqbuf = newbuf;
721                 req->rq_reqbuf_len = newbuf_size;
722                 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf,
723                                                 PLAIN_PACK_MSG_OFF, 0);
724
725                 if (req->rq_import)
726                         spin_unlock(&req->rq_import->imp_lock);
727         }
728
729         _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, PLAIN_PACK_MSG_OFF,
730                                      newmsg_size);
731         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
732
733         req->rq_reqlen = newmsg_size;
734         RETURN(0);
735 }
736
737 /****************************************
738  * service apis                         *
739  ****************************************/
740
741 static struct ptlrpc_svc_ctx plain_svc_ctx = {
742         .sc_refcount    = ATOMIC_INIT(1),
743         .sc_policy      = &plain_policy,
744 };
745
746 static
747 int plain_accept(struct ptlrpc_request *req)
748 {
749         struct lustre_msg   *msg = req->rq_reqbuf;
750         struct plain_header *phdr;
751         int                  swabbed;
752         ENTRY;
753
754         LASSERT(SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) ==
755                 SPTLRPC_POLICY_PLAIN);
756
757         if (SPTLRPC_FLVR_BASE(req->rq_flvr.sf_rpc) !=
758             SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN) ||
759             SPTLRPC_FLVR_BULK_TYPE(req->rq_flvr.sf_rpc) !=
760             SPTLRPC_FLVR_BULK_TYPE(SPTLRPC_FLVR_PLAIN)) {
761                 CERROR("Invalid rpc flavor %x\n", req->rq_flvr.sf_rpc);
762                 RETURN(SECSVC_DROP);
763         }
764
765         if (msg->lm_bufcount < PLAIN_PACK_SEGMENTS) {
766                 CERROR("unexpected request buf count %u\n", msg->lm_bufcount);
767                 RETURN(SECSVC_DROP);
768         }
769
770         swabbed = ptlrpc_req_need_swab(req);
771
772         phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
773         if (phdr == NULL) {
774                 CERROR("missing plain header\n");
775                 RETURN(-EPROTO);
776         }
777
778         if (phdr->ph_ver != 0) {
779                 CERROR("Invalid header version\n");
780                 RETURN(-EPROTO);
781         }
782
783         if (phdr->ph_bulk_hash_alg >= BULK_HASH_ALG_MAX) {
784                 CERROR("invalid hash algorithm: %u\n", phdr->ph_bulk_hash_alg);
785                 RETURN(-EPROTO);
786         }
787
788         req->rq_sp_from = phdr->ph_sp;
789         req->rq_flvr.u_bulk.hash.hash_alg = phdr->ph_bulk_hash_alg;
790
791         if (phdr->ph_flags & PLAIN_FL_USER) {
792                 if (sptlrpc_unpack_user_desc(msg, PLAIN_PACK_USER_OFF,
793                                              swabbed)) {
794                         CERROR("Mal-formed user descriptor\n");
795                         RETURN(SECSVC_DROP);
796                 }
797
798                 req->rq_pack_udesc = 1;
799                 req->rq_user_desc = lustre_msg_buf(msg, PLAIN_PACK_USER_OFF, 0);
800         }
801
802         if (phdr->ph_flags & PLAIN_FL_BULK) {
803                 if (plain_unpack_bsd(msg, swabbed))
804                         RETURN(SECSVC_DROP);
805
806                 req->rq_pack_bulk = 1;
807         }
808
809         req->rq_reqmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
810         req->rq_reqlen = msg->lm_buflens[PLAIN_PACK_MSG_OFF];
811
812         req->rq_svc_ctx = &plain_svc_ctx;
813         atomic_inc(&req->rq_svc_ctx->sc_refcount);
814
815         RETURN(SECSVC_OK);
816 }
817
818 static
819 int plain_alloc_rs(struct ptlrpc_request *req, int msgsize)
820 {
821         struct ptlrpc_reply_state   *rs;
822         __u32                        buflens[PLAIN_PACK_SEGMENTS] = { 0, };
823         int                          rs_size = sizeof(*rs);
824         ENTRY;
825
826         LASSERT(msgsize % 8 == 0);
827
828         buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
829         buflens[PLAIN_PACK_MSG_OFF] = msgsize;
830
831         if (req->rq_pack_bulk && (req->rq_bulk_read || req->rq_bulk_write))
832                 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
833
834         rs_size += lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
835
836         rs = req->rq_reply_state;
837
838         if (rs) {
839                 /* pre-allocated */
840                 LASSERT(rs->rs_size >= rs_size);
841         } else {
842                 OBD_ALLOC_LARGE(rs, rs_size);
843                 if (rs == NULL)
844                         RETURN(-ENOMEM);
845
846                 rs->rs_size = rs_size;
847         }
848
849         rs->rs_svc_ctx = req->rq_svc_ctx;
850         atomic_inc(&req->rq_svc_ctx->sc_refcount);
851         rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
852         rs->rs_repbuf_len = rs_size - sizeof(*rs);
853
854         lustre_init_msg_v2(rs->rs_repbuf, PLAIN_PACK_SEGMENTS, buflens, NULL);
855         rs->rs_msg = lustre_msg_buf_v2(rs->rs_repbuf, PLAIN_PACK_MSG_OFF, 0);
856
857         req->rq_reply_state = rs;
858         RETURN(0);
859 }
860
861 static
862 void plain_free_rs(struct ptlrpc_reply_state *rs)
863 {
864         ENTRY;
865
866         LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
867         atomic_dec(&rs->rs_svc_ctx->sc_refcount);
868
869         if (!rs->rs_prealloc)
870                 OBD_FREE_LARGE(rs, rs->rs_size);
871         EXIT;
872 }
873
874 static
875 int plain_authorize(struct ptlrpc_request *req)
876 {
877         struct ptlrpc_reply_state *rs = req->rq_reply_state;
878         struct lustre_msg_v2      *msg = rs->rs_repbuf;
879         struct plain_header       *phdr;
880         int                        len;
881         ENTRY;
882
883         LASSERT(rs);
884         LASSERT(msg);
885
886         if (req->rq_replen != msg->lm_buflens[PLAIN_PACK_MSG_OFF])
887                 len = lustre_shrink_msg(msg, PLAIN_PACK_MSG_OFF,
888                                         req->rq_replen, 1);
889         else
890                 len = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
891
892         msg->lm_secflvr = req->rq_flvr.sf_rpc;
893
894         phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, 0);
895         phdr->ph_ver = 0;
896         phdr->ph_flags = 0;
897         phdr->ph_bulk_hash_alg = req->rq_flvr.u_bulk.hash.hash_alg;
898
899         if (req->rq_pack_bulk)
900                 phdr->ph_flags |= PLAIN_FL_BULK;
901
902         rs->rs_repdata_len = len;
903
904         if (likely(req->rq_packed_final)) {
905                 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
906                         req->rq_reply_off = plain_at_offset;
907                 else
908                         req->rq_reply_off = 0;
909         } else {
910                 unsigned int hsize = 4;
911
912                 cfs_crypto_hash_digest(CFS_HASH_ALG_CRC32,
913                         lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0),
914                         lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF),
915                         NULL, 0, (unsigned char *)&msg->lm_cksum, &hsize);
916                         req->rq_reply_off = 0;
917         }
918
919         RETURN(0);
920 }
921
922 static
923 int plain_svc_unwrap_bulk(struct ptlrpc_request *req,
924                           struct ptlrpc_bulk_desc *desc)
925 {
926         struct ptlrpc_reply_state   *rs = req->rq_reply_state;
927         struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
928         struct plain_bulk_token     *tokenr;
929         int                          rc;
930
931         LASSERT(req->rq_bulk_write);
932         LASSERT(req->rq_pack_bulk);
933
934         bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
935         tokenr = (struct plain_bulk_token *) bsdr->bsd_data;
936         bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
937
938         bsdv->bsd_version = 0;
939         bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
940         bsdv->bsd_svc = bsdr->bsd_svc;
941         bsdv->bsd_flags = 0;
942
943         if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
944                 return 0;
945
946         rc = plain_verify_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
947                                     tokenr);
948         if (rc) {
949                 bsdv->bsd_flags |= BSD_FL_ERR;
950                 CERROR("bulk write: server verify failed: %d\n", rc);
951         }
952
953         return rc;
954 }
955
956 static
957 int plain_svc_wrap_bulk(struct ptlrpc_request *req,
958                         struct ptlrpc_bulk_desc *desc)
959 {
960         struct ptlrpc_reply_state   *rs = req->rq_reply_state;
961         struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
962         struct plain_bulk_token     *tokenv;
963         int                          rc;
964
965         LASSERT(req->rq_bulk_read);
966         LASSERT(req->rq_pack_bulk);
967
968         bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
969         bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
970         tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
971
972         bsdv->bsd_version = 0;
973         bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
974         bsdv->bsd_svc = bsdr->bsd_svc;
975         bsdv->bsd_flags = 0;
976
977         if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
978                 return 0;
979
980         rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
981                                       tokenv);
982         if (rc) {
983                 CERROR("bulk read: server failed to compute "
984                        "checksum: %d\n", rc);
985         } else {
986                 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_RECEIVE))
987                         corrupt_bulk_data(desc);
988         }
989
990         return rc;
991 }
992
993 static struct ptlrpc_ctx_ops plain_ctx_ops = {
994         .refresh                = plain_ctx_refresh,
995         .validate               = plain_ctx_validate,
996         .sign                   = plain_ctx_sign,
997         .verify                 = plain_ctx_verify,
998         .wrap_bulk              = plain_cli_wrap_bulk,
999         .unwrap_bulk            = plain_cli_unwrap_bulk,
1000 };
1001
1002 static struct ptlrpc_sec_cops plain_sec_cops = {
1003         .create_sec             = plain_create_sec,
1004         .destroy_sec            = plain_destroy_sec,
1005         .kill_sec               = plain_kill_sec,
1006         .lookup_ctx             = plain_lookup_ctx,
1007         .release_ctx            = plain_release_ctx,
1008         .flush_ctx_cache        = plain_flush_ctx_cache,
1009         .alloc_reqbuf           = plain_alloc_reqbuf,
1010         .free_reqbuf            = plain_free_reqbuf,
1011         .alloc_repbuf           = plain_alloc_repbuf,
1012         .free_repbuf            = plain_free_repbuf,
1013         .enlarge_reqbuf         = plain_enlarge_reqbuf,
1014 };
1015
1016 static struct ptlrpc_sec_sops plain_sec_sops = {
1017         .accept                 = plain_accept,
1018         .alloc_rs               = plain_alloc_rs,
1019         .authorize              = plain_authorize,
1020         .free_rs                = plain_free_rs,
1021         .unwrap_bulk            = plain_svc_unwrap_bulk,
1022         .wrap_bulk              = plain_svc_wrap_bulk,
1023 };
1024
1025 static struct ptlrpc_sec_policy plain_policy = {
1026         .sp_owner               = THIS_MODULE,
1027         .sp_name                = "plain",
1028         .sp_policy              = SPTLRPC_POLICY_PLAIN,
1029         .sp_cops                = &plain_sec_cops,
1030         .sp_sops                = &plain_sec_sops,
1031 };
1032
1033 int sptlrpc_plain_init(void)
1034 {
1035         __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
1036         int rc;
1037
1038         buflens[PLAIN_PACK_MSG_OFF] = lustre_msg_early_size();
1039         plain_at_offset = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
1040
1041         rc = sptlrpc_register_policy(&plain_policy);
1042         if (rc)
1043                 CERROR("failed to register: %d\n", rc);
1044
1045         return rc;
1046 }
1047
1048 void sptlrpc_plain_fini(void)
1049 {
1050         int rc;
1051
1052         rc = sptlrpc_unregister_policy(&plain_policy);
1053         if (rc)
1054                 CERROR("cannot unregister: %d\n", rc);
1055 }