Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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  * Copyright (C) 2006 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_SEC
26
27 #ifndef __KERNEL__
28 #include <liblustre.h>
29 #endif
30
31 #include <obd_support.h>
32 #include <obd_class.h>
33 #include <lustre_net.h>
34 #include <lustre_sec.h>
35
36 static struct ptlrpc_sec_policy plain_policy;
37 static struct ptlrpc_sec        plain_sec;
38 static struct ptlrpc_cli_ctx    plain_cli_ctx;
39 static struct ptlrpc_svc_ctx    plain_svc_ctx;
40
41 static
42 int plain_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
43 {
44         /* should never reach here */
45         LBUG();
46         return 0;
47 }
48
49 static
50 int plain_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
51 {
52         struct lustre_msg_v2 *msg = req->rq_reqbuf;
53         ENTRY;
54
55         msg->lm_secflvr = req->rq_sec_flavor;
56         req->rq_reqdata_len = lustre_msg_size_v2(msg->lm_bufcount,
57                                                  msg->lm_buflens);
58         RETURN(0);
59 }
60
61 static
62 int plain_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
63 {
64         struct lustre_msg *msg = req->rq_repbuf;
65         ENTRY;
66
67         if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
68                 if (msg->lm_bufcount != 2) {
69                         CERROR("Protocol error: invalid buf count %d\n",
70                                msg->lm_bufcount);
71                         RETURN(-EPROTO);
72                 }
73
74                 if (bulk_sec_desc_unpack(msg, 1)) {
75                         CERROR("Mal-formed bulk checksum reply\n");
76                         RETURN(-EINVAL);
77                 }
78         }
79
80         req->rq_repmsg = lustre_msg_buf(msg, 0, 0);
81         req->rq_replen = msg->lm_buflens[0];
82         RETURN(0);
83 }
84
85 static
86 int plain_cli_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
87                         struct ptlrpc_request *req,
88                         struct ptlrpc_bulk_desc *desc)
89 {
90         struct sec_flavor_config *conf;
91
92         LASSERT(req->rq_import);
93         LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
94         LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
95
96         conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
97         return bulk_csum_cli_request(desc, req->rq_bulk_read,
98                                      conf->sfc_bulk_csum,
99                                      req->rq_reqbuf,
100                                      req->rq_reqbuf->lm_bufcount - 1);
101 }
102
103 static
104 int plain_cli_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
105                           struct ptlrpc_request *req,
106                           struct ptlrpc_bulk_desc *desc)
107 {
108         LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
109         LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
110         LASSERT(req->rq_repbuf->lm_bufcount >= 2);
111
112         return bulk_csum_cli_reply(desc, req->rq_bulk_read,
113                                    req->rq_reqbuf,
114                                    req->rq_reqbuf->lm_bufcount - 1,
115                                    req->rq_repbuf,
116                                    req->rq_repbuf->lm_bufcount - 1);
117 }
118
119 static struct ptlrpc_ctx_ops plain_ctx_ops = {
120         .refresh        = plain_ctx_refresh,
121         .sign           = plain_ctx_sign,
122         .verify         = plain_ctx_verify,
123         .wrap_bulk      = plain_cli_wrap_bulk,
124         .unwrap_bulk    = plain_cli_unwrap_bulk,
125 };
126
127 static struct ptlrpc_svc_ctx plain_svc_ctx = {
128         .sc_refcount    = ATOMIC_INIT(1),
129         .sc_policy      = &plain_policy,
130 };
131
132 static
133 struct ptlrpc_sec* plain_create_sec(struct obd_import *imp,
134                                     struct ptlrpc_svc_ctx *ctx,
135                                     __u32 flavor,
136                                     unsigned long flags)
137 {
138         ENTRY;
139         LASSERT(SEC_FLAVOR_POLICY(flavor) == SPTLRPC_POLICY_PLAIN);
140         RETURN(&plain_sec);
141 }
142
143 static
144 void plain_destroy_sec(struct ptlrpc_sec *sec)
145 {
146         ENTRY;
147         LASSERT(sec == &plain_sec);
148         EXIT;
149 }
150
151 static
152 struct ptlrpc_cli_ctx *plain_lookup_ctx(struct ptlrpc_sec *sec,
153                                         struct vfs_cred *vcred)
154 {
155         ENTRY;
156         atomic_inc(&plain_cli_ctx.cc_refcount);
157         RETURN(&plain_cli_ctx);
158 }
159
160 static
161 int plain_alloc_reqbuf(struct ptlrpc_sec *sec,
162                        struct ptlrpc_request *req,
163                        int msgsize)
164 {
165         struct sec_flavor_config *conf;
166         int bufcnt = 1, buflens[2], alloc_len;
167         ENTRY;
168
169         buflens[0] = msgsize;
170
171         if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor))
172                 buflens[bufcnt++] = sptlrpc_current_user_desc_size();
173
174         if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
175                 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
176
177                 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
178                 buflens[bufcnt++] = bulk_sec_desc_size(conf->sfc_bulk_csum, 1,
179                                                        req->rq_bulk_read);
180         }
181
182         alloc_len = lustre_msg_size_v2(bufcnt, buflens);
183
184         if (!req->rq_reqbuf) {
185                 LASSERT(!req->rq_pool);
186
187                 alloc_len = size_roundup_power2(alloc_len);
188                 OBD_ALLOC(req->rq_reqbuf, alloc_len);
189                 if (!req->rq_reqbuf)
190                         RETURN(-ENOMEM);
191
192                 req->rq_reqbuf_len = alloc_len;
193         } else {
194                 LASSERT(req->rq_pool);
195                 LASSERT(req->rq_reqbuf_len >= alloc_len);
196                 memset(req->rq_reqbuf, 0, alloc_len);
197         }
198
199         lustre_init_msg_v2(req->rq_reqbuf, bufcnt, buflens, NULL);
200         req->rq_reqmsg = lustre_msg_buf_v2(req->rq_reqbuf, 0, 0);
201
202         if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor))
203                 sptlrpc_pack_user_desc(req->rq_reqbuf, 1);
204
205         RETURN(0);
206 }
207
208 static
209 void plain_free_reqbuf(struct ptlrpc_sec *sec,
210                        struct ptlrpc_request *req)
211 {
212         ENTRY;
213         if (!req->rq_pool) {
214                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
215                 req->rq_reqbuf = NULL;
216                 req->rq_reqbuf_len = 0;
217         }
218         EXIT;
219 }
220
221 static
222 int plain_alloc_repbuf(struct ptlrpc_sec *sec,
223                        struct ptlrpc_request *req,
224                        int msgsize)
225 {
226         struct sec_flavor_config *conf;
227         int bufcnt = 1, buflens[2], alloc_len;
228         ENTRY;
229
230         buflens[0] = msgsize;
231
232         if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
233                 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
234
235                 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
236                 buflens[bufcnt++] = bulk_sec_desc_size(conf->sfc_bulk_csum, 0,
237                                                        req->rq_bulk_read);
238         }
239
240         alloc_len = lustre_msg_size_v2(bufcnt, buflens);
241         alloc_len = size_roundup_power2(alloc_len);
242
243         OBD_ALLOC(req->rq_repbuf, alloc_len);
244         if (!req->rq_repbuf)
245                 RETURN(-ENOMEM);
246
247         req->rq_repbuf_len = alloc_len;
248         RETURN(0);
249 }
250
251 static
252 void plain_free_repbuf(struct ptlrpc_sec *sec,
253                        struct ptlrpc_request *req)
254 {
255         ENTRY;
256         OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
257         req->rq_repbuf = NULL;
258         req->rq_repbuf_len = 0;
259         EXIT;
260 }
261
262 static
263 int plain_enlarge_reqbuf(struct ptlrpc_sec *sec,
264                          struct ptlrpc_request *req,
265                          int segment, int newsize)
266 {
267         struct lustre_msg      *newbuf;
268         int                     oldsize;
269         int                     newmsg_size, newbuf_size;
270         ENTRY;
271
272         /* embedded msg always at seg 0 */
273         LASSERT(req->rq_reqbuf);
274         LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
275         LASSERT(lustre_msg_buf(req->rq_reqbuf, 0, 0) == req->rq_reqmsg);
276
277         /* compute new embedded msg size.  */
278         oldsize = req->rq_reqmsg->lm_buflens[segment];
279         req->rq_reqmsg->lm_buflens[segment] = newsize;
280         newmsg_size = lustre_msg_size_v2(req->rq_reqmsg->lm_bufcount,
281                                          req->rq_reqmsg->lm_buflens);
282         req->rq_reqmsg->lm_buflens[segment] = oldsize;
283
284         /* compute new wrapper msg size.  */
285         oldsize = req->rq_reqbuf->lm_buflens[0];
286         req->rq_reqbuf->lm_buflens[0] = newmsg_size;
287         newbuf_size = lustre_msg_size_v2(req->rq_reqbuf->lm_bufcount,
288                                          req->rq_reqbuf->lm_buflens);
289         req->rq_reqbuf->lm_buflens[0] = oldsize;
290
291         /* request from pool should always have enough buffer */
292         LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
293
294         if (req->rq_reqbuf_len < newbuf_size) {
295                 newbuf_size = size_roundup_power2(newbuf_size);
296
297                 OBD_ALLOC(newbuf, newbuf_size);
298                 if (newbuf == NULL)
299                         RETURN(-ENOMEM);
300
301                 memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
302
303                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
304                 req->rq_reqbuf = newbuf;
305                 req->rq_reqbuf_len = newbuf_size;
306                 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 0, 0);
307         }
308
309         _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, 0, newmsg_size);
310         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
311
312         req->rq_reqlen = newmsg_size;
313         RETURN(0);
314 }
315
316 static
317 int plain_accept(struct ptlrpc_request *req)
318 {
319         struct lustre_msg *msg = req->rq_reqbuf;
320         int                bufcnt = 1;
321         ENTRY;
322
323         LASSERT(SEC_FLAVOR_POLICY(req->rq_sec_flavor) == SPTLRPC_POLICY_PLAIN);
324
325         if (SEC_FLAVOR_RPC(req->rq_sec_flavor) != SPTLRPC_FLVR_PLAIN) {
326                 CERROR("Invalid flavor 0x%x\n", req->rq_sec_flavor);
327                 return SECSVC_DROP;
328         }
329
330         if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor)) {
331                 if (msg->lm_bufcount < ++bufcnt) {
332                         CERROR("Protocal error: too small buf count %d\n",
333                                msg->lm_bufcount);
334                         RETURN(SECSVC_DROP);
335                 }
336
337                 if (sptlrpc_unpack_user_desc(msg, bufcnt - 1)) {
338                         CERROR("Mal-formed user descriptor\n");
339                         RETURN(SECSVC_DROP);
340                 }
341
342                 req->rq_user_desc = lustre_msg_buf(msg, bufcnt - 1, 0);
343         }
344
345         if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
346                 if (msg->lm_bufcount != ++bufcnt) {
347                         CERROR("Protocal error: invalid buf count %d\n",
348                                msg->lm_bufcount);
349                         RETURN(SECSVC_DROP);
350                 }
351
352                 if (bulk_sec_desc_unpack(msg, bufcnt - 1)) {
353                         CERROR("Mal-formed bulk checksum request\n");
354                         RETURN(SECSVC_DROP);
355                 }
356         }
357
358         req->rq_reqmsg = lustre_msg_buf(msg, 0, 0);
359         req->rq_reqlen = msg->lm_buflens[0];
360
361         req->rq_svc_ctx = &plain_svc_ctx;
362         atomic_inc(&req->rq_svc_ctx->sc_refcount);
363
364         RETURN(SECSVC_OK);
365 }
366
367 static
368 int plain_alloc_rs(struct ptlrpc_request *req, int msgsize)
369 {
370         struct ptlrpc_reply_state *rs;
371         struct ptlrpc_bulk_sec_desc *bsd;
372         int bufcnt = 1, buflens[2];
373         int rs_size = sizeof(*rs);
374         ENTRY;
375
376         LASSERT(msgsize % 8 == 0);
377
378         buflens[0] = msgsize;
379         if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor) &&
380             (req->rq_bulk_read || req->rq_bulk_write)) {
381                 bsd = lustre_msg_buf(req->rq_reqbuf,
382                                      req->rq_reqbuf->lm_bufcount - 1,
383                                      sizeof(*bsd));
384                 LASSERT(bsd);
385
386                 buflens[bufcnt++] = bulk_sec_desc_size(bsd->bsd_csum_alg, 0,
387                                                        req->rq_bulk_read);
388         }
389         rs_size += lustre_msg_size_v2(bufcnt, buflens);
390
391         rs = req->rq_reply_state;
392
393         if (rs) {
394                 /* pre-allocated */
395                 LASSERT(rs->rs_size >= rs_size);
396         } else {
397                 OBD_ALLOC(rs, rs_size);
398                 if (rs == NULL)
399                         RETURN(-ENOMEM);
400
401                 rs->rs_size = rs_size;
402         }
403
404         rs->rs_svc_ctx = req->rq_svc_ctx;
405         atomic_inc(&req->rq_svc_ctx->sc_refcount);
406         rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
407         rs->rs_repbuf_len = rs_size - sizeof(*rs);
408
409         lustre_init_msg_v2(rs->rs_repbuf, bufcnt, buflens, NULL);
410         rs->rs_msg = lustre_msg_buf_v2(rs->rs_repbuf, 0, 0);
411
412         req->rq_reply_state = rs;
413         RETURN(0);
414 }
415
416 static
417 void plain_free_rs(struct ptlrpc_reply_state *rs)
418 {
419         ENTRY;
420
421         LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
422         atomic_dec(&rs->rs_svc_ctx->sc_refcount);
423
424         if (!rs->rs_prealloc)
425                 OBD_FREE(rs, rs->rs_size);
426         EXIT;
427 }
428
429 static
430 int plain_authorize(struct ptlrpc_request *req)
431 {
432         struct ptlrpc_reply_state *rs = req->rq_reply_state;
433         struct lustre_msg_v2      *msg = rs->rs_repbuf;
434         int                        len;
435         ENTRY;
436
437         LASSERT(rs);
438         LASSERT(msg);
439
440         if (req->rq_replen != msg->lm_buflens[0])
441                 len = lustre_shrink_msg(msg, 0, req->rq_replen, 1);
442         else
443                 len = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
444
445         msg->lm_secflvr = req->rq_sec_flavor;
446         rs->rs_repdata_len = len;
447         RETURN(0);
448 }
449
450 static
451 int plain_svc_unwrap_bulk(struct ptlrpc_request *req,
452                           struct ptlrpc_bulk_desc *desc)
453 {
454         struct ptlrpc_reply_state *rs = req->rq_reply_state;
455
456         LASSERT(rs);
457
458         return bulk_csum_svc(desc, req->rq_bulk_read,
459                              req->rq_reqbuf, req->rq_reqbuf->lm_bufcount - 1,
460                              rs->rs_repbuf, rs->rs_repbuf->lm_bufcount - 1);
461 }
462
463 static
464 int plain_svc_wrap_bulk(struct ptlrpc_request *req,
465                         struct ptlrpc_bulk_desc *desc)
466 {
467         struct ptlrpc_reply_state *rs = req->rq_reply_state;
468
469         LASSERT(rs);
470
471         return bulk_csum_svc(desc, req->rq_bulk_read,
472                              req->rq_reqbuf, req->rq_reqbuf->lm_bufcount - 1,
473                              rs->rs_repbuf, rs->rs_repbuf->lm_bufcount - 1);
474 }
475
476 static struct ptlrpc_sec_cops plain_sec_cops = {
477         .create_sec             = plain_create_sec,
478         .destroy_sec            = plain_destroy_sec,
479         .lookup_ctx             = plain_lookup_ctx,
480         .alloc_reqbuf           = plain_alloc_reqbuf,
481         .alloc_repbuf           = plain_alloc_repbuf,
482         .free_reqbuf            = plain_free_reqbuf,
483         .free_repbuf            = plain_free_repbuf,
484         .enlarge_reqbuf         = plain_enlarge_reqbuf,
485 };
486
487 static struct ptlrpc_sec_sops plain_sec_sops = {
488         .accept                 = plain_accept,
489         .alloc_rs               = plain_alloc_rs,
490         .authorize              = plain_authorize,
491         .free_rs                = plain_free_rs,
492         .unwrap_bulk            = plain_svc_unwrap_bulk,
493         .wrap_bulk              = plain_svc_wrap_bulk,
494 };
495
496 static struct ptlrpc_sec_policy plain_policy = {
497         .sp_owner               = THIS_MODULE,
498         .sp_name                = "sec.plain",
499         .sp_policy              = SPTLRPC_POLICY_PLAIN,
500         .sp_cops                = &plain_sec_cops,
501         .sp_sops                = &plain_sec_sops,
502 };
503
504 static
505 void plain_init_internal(void)
506 {
507         static HLIST_HEAD(__list);
508
509         plain_sec.ps_policy = &plain_policy;
510         atomic_set(&plain_sec.ps_refcount, 1);     /* always busy */
511         plain_sec.ps_import = NULL;
512         plain_sec.ps_flavor = SPTLRPC_FLVR_PLAIN;
513         plain_sec.ps_flags = 0;
514         plain_sec.ps_gc_interval = 0;
515         plain_sec.ps_gc_next = 0;
516         spin_lock_init(&plain_sec.ps_lock);
517         plain_sec.ps_ccache_size = 1;
518         plain_sec.ps_ccache = &__list;
519         atomic_set(&plain_sec.ps_busy, 1);         /* for "plain_cli_ctx" */
520
521         hlist_add_head(&plain_cli_ctx.cc_hash, &__list);
522         atomic_set(&plain_cli_ctx.cc_refcount, 1);    /* for hash */
523         plain_cli_ctx.cc_sec = &plain_sec;
524         plain_cli_ctx.cc_ops = &plain_ctx_ops;
525         plain_cli_ctx.cc_expire = 0;
526         plain_cli_ctx.cc_flags = PTLRPC_CTX_HASHED | PTLRPC_CTX_ETERNAL |
527                                  PTLRPC_CTX_UPTODATE;
528         plain_cli_ctx.cc_vcred.vc_uid = 0;
529         spin_lock_init(&plain_cli_ctx.cc_lock);
530         INIT_LIST_HEAD(&plain_cli_ctx.cc_req_list);
531 }
532
533 int sptlrpc_plain_init(void)
534 {
535         int rc;
536
537         plain_init_internal();
538
539         rc = sptlrpc_register_policy(&plain_policy);
540         if (rc)
541                 CERROR("failed to register sec.plain: %d\n", rc);
542
543         return rc;
544 }
545
546 void sptlrpc_plain_fini(void)
547 {
548         int rc;
549
550         rc = sptlrpc_unregister_policy(&plain_policy);
551         if (rc)
552                 CERROR("cannot unregister sec.plain: %d\n", rc);
553 }