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