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