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