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