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