1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2006 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_SEC
28 #include <liblustre.h>
31 #include <obd_support.h>
32 #include <obd_class.h>
33 #include <lustre_net.h>
34 #include <lustre_sec.h>
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;
42 int plain_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
44 /* should never reach here */
50 int plain_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
52 struct lustre_msg_v2 *msg = req->rq_reqbuf;
55 msg->lm_secflvr = req->rq_sec_flavor;
56 req->rq_reqdata_len = lustre_msg_size_v2(msg->lm_bufcount,
62 int plain_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
64 struct lustre_msg *msg = req->rq_repbuf;
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",
74 if (bulk_sec_desc_unpack(msg, 1)) {
75 CERROR("Mal-formed bulk checksum reply\n");
80 req->rq_repmsg = lustre_msg_buf(msg, 0, 0);
81 req->rq_replen = msg->lm_buflens[0];
86 int plain_cli_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
87 struct ptlrpc_request *req,
88 struct ptlrpc_bulk_desc *desc)
90 struct sec_flavor_config *conf;
92 LASSERT(req->rq_import);
93 LASSERT(SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor));
94 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
96 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
97 return bulk_csum_cli_request(desc, req->rq_bulk_read,
100 req->rq_reqbuf->lm_bufcount - 1);
104 int plain_cli_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
105 struct ptlrpc_request *req,
106 struct ptlrpc_bulk_desc *desc)
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);
112 return bulk_csum_cli_reply(desc, req->rq_bulk_read,
114 req->rq_reqbuf->lm_bufcount - 1,
116 req->rq_repbuf->lm_bufcount - 1);
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,
127 static struct ptlrpc_svc_ctx plain_svc_ctx = {
128 .sc_refcount = ATOMIC_INIT(1),
129 .sc_policy = &plain_policy,
133 struct ptlrpc_sec* plain_create_sec(struct obd_import *imp,
134 struct ptlrpc_svc_ctx *ctx,
139 LASSERT(SEC_FLAVOR_POLICY(flavor) == SPTLRPC_POLICY_PLAIN);
144 void plain_destroy_sec(struct ptlrpc_sec *sec)
147 LASSERT(sec == &plain_sec);
152 struct ptlrpc_cli_ctx *plain_lookup_ctx(struct ptlrpc_sec *sec,
153 struct vfs_cred *vcred)
156 atomic_inc(&plain_cli_ctx.cc_refcount);
157 RETURN(&plain_cli_ctx);
161 int plain_alloc_reqbuf(struct ptlrpc_sec *sec,
162 struct ptlrpc_request *req,
165 struct sec_flavor_config *conf;
166 int bufcnt = 1, buflens[2], alloc_len;
169 buflens[0] = msgsize;
171 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor))
172 buflens[bufcnt++] = sptlrpc_user_desc_size();
174 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
175 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
177 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
178 buflens[bufcnt++] = bulk_sec_desc_size(conf->sfc_bulk_csum, 1,
182 alloc_len = lustre_msg_size_v2(bufcnt, buflens);
185 if (!req->rq_reqbuf) {
186 LASSERT(!req->rq_pool);
187 OBD_ALLOC(req->rq_reqbuf, alloc_len);
191 req->rq_reqbuf_len = alloc_len;
193 LASSERT(req->rq_pool);
194 LASSERT(req->rq_reqbuf_len >= alloc_len);
195 memset(req->rq_reqbuf, 0, alloc_len);
198 lustre_init_msg_v2(req->rq_reqbuf, bufcnt, buflens, NULL);
199 req->rq_reqmsg = lustre_msg_buf_v2(req->rq_reqbuf, 0, 0);
201 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor))
202 sptlrpc_pack_user_desc(req->rq_reqbuf, 1);
208 void plain_free_reqbuf(struct ptlrpc_sec *sec,
209 struct ptlrpc_request *req)
213 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
214 req->rq_reqbuf = NULL;
215 req->rq_reqbuf_len = 0;
221 int plain_alloc_repbuf(struct ptlrpc_sec *sec,
222 struct ptlrpc_request *req,
225 struct sec_flavor_config *conf;
226 int bufcnt = 1, buflens[2], alloc_len;
229 buflens[0] = msgsize;
231 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
232 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
234 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
235 buflens[bufcnt++] = bulk_sec_desc_size(conf->sfc_bulk_csum, 0,
239 alloc_len = lustre_msg_size_v2(bufcnt, buflens);
241 OBD_ALLOC(req->rq_repbuf, alloc_len);
245 req->rq_repbuf_len = alloc_len;
250 void plain_free_repbuf(struct ptlrpc_sec *sec,
251 struct ptlrpc_request *req)
254 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
255 req->rq_repbuf = NULL;
256 req->rq_repbuf_len = 0;
261 int plain_enlarge_reqbuf(struct ptlrpc_sec *sec,
262 struct ptlrpc_request *req,
263 int segment, int newsize, int move_data)
270 int plain_accept(struct ptlrpc_request *req)
272 struct lustre_msg *msg = req->rq_reqbuf;
276 LASSERT(SEC_FLAVOR_POLICY(req->rq_sec_flavor) == SPTLRPC_POLICY_PLAIN);
278 if (SEC_FLAVOR_RPC(req->rq_sec_flavor) != SPTLRPC_FLVR_PLAIN) {
279 CERROR("Invalid flavor 0x%x\n", req->rq_sec_flavor);
283 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor)) {
284 if (msg->lm_bufcount < ++bufcnt) {
285 CERROR("Protocal error: too small buf count %d\n",
290 if (sptlrpc_unpack_user_desc(msg, bufcnt - 1)) {
291 CERROR("Mal-formed user descriptor\n");
295 req->rq_user_desc = lustre_msg_buf(msg, bufcnt - 1, 0);
298 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
299 if (msg->lm_bufcount != ++bufcnt) {
300 CERROR("Protocal error: invalid buf count %d\n",
305 if (bulk_sec_desc_unpack(msg, bufcnt - 1)) {
306 CERROR("Mal-formed bulk checksum request\n");
311 req->rq_reqmsg = lustre_msg_buf(msg, 0, 0);
312 req->rq_reqlen = msg->lm_buflens[0];
314 req->rq_svc_ctx = &plain_svc_ctx;
315 atomic_inc(&req->rq_svc_ctx->sc_refcount);
321 int plain_alloc_rs(struct ptlrpc_request *req, int msgsize)
323 struct ptlrpc_reply_state *rs;
324 struct ptlrpc_bulk_sec_desc *bsd;
325 int bufcnt = 1, buflens[2];
326 int rs_size = sizeof(*rs);
329 LASSERT(msgsize % 8 == 0);
331 buflens[0] = msgsize;
332 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor) &&
333 (req->rq_bulk_read || req->rq_bulk_write)) {
334 bsd = lustre_msg_buf(req->rq_reqbuf,
335 req->rq_reqbuf->lm_bufcount - 1,
339 buflens[bufcnt++] = bulk_sec_desc_size(bsd->bsd_csum_alg, 0,
342 rs_size += lustre_msg_size_v2(bufcnt, buflens);
344 rs = req->rq_reply_state;
348 LASSERT(rs->rs_size >= rs_size);
350 OBD_ALLOC(rs, rs_size);
354 rs->rs_size = rs_size;
357 rs->rs_svc_ctx = req->rq_svc_ctx;
358 atomic_inc(&req->rq_svc_ctx->sc_refcount);
359 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
360 rs->rs_repbuf_len = rs_size - sizeof(*rs);
362 lustre_init_msg_v2(rs->rs_repbuf, bufcnt, buflens, NULL);
363 rs->rs_msg = lustre_msg_buf_v2(rs->rs_repbuf, 0, 0);
365 req->rq_reply_state = rs;
370 void plain_free_rs(struct ptlrpc_reply_state *rs)
374 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
375 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
377 if (!rs->rs_prealloc)
378 OBD_FREE(rs, rs->rs_size);
383 int plain_authorize(struct ptlrpc_request *req)
385 struct ptlrpc_reply_state *rs = req->rq_reply_state;
386 struct lustre_msg_v2 *msg = rs->rs_repbuf;
393 if (req->rq_replen != msg->lm_buflens[0])
394 len = lustre_shrink_msg(msg, 0, req->rq_replen, 1);
396 len = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
398 msg->lm_secflvr = req->rq_sec_flavor;
399 rs->rs_repdata_len = len;
404 int plain_svc_unwrap_bulk(struct ptlrpc_request *req,
405 struct ptlrpc_bulk_desc *desc)
407 struct ptlrpc_reply_state *rs = req->rq_reply_state;
411 return bulk_csum_svc(desc, req->rq_bulk_read,
412 req->rq_reqbuf, req->rq_reqbuf->lm_bufcount - 1,
413 rs->rs_repbuf, rs->rs_repbuf->lm_bufcount - 1);
417 int plain_svc_wrap_bulk(struct ptlrpc_request *req,
418 struct ptlrpc_bulk_desc *desc)
420 struct ptlrpc_reply_state *rs = req->rq_reply_state;
424 return bulk_csum_svc(desc, req->rq_bulk_read,
425 req->rq_reqbuf, req->rq_reqbuf->lm_bufcount - 1,
426 rs->rs_repbuf, rs->rs_repbuf->lm_bufcount - 1);
429 static struct ptlrpc_sec_cops plain_sec_cops = {
430 .create_sec = plain_create_sec,
431 .destroy_sec = plain_destroy_sec,
432 .lookup_ctx = plain_lookup_ctx,
433 .alloc_reqbuf = plain_alloc_reqbuf,
434 .alloc_repbuf = plain_alloc_repbuf,
435 .free_reqbuf = plain_free_reqbuf,
436 .free_repbuf = plain_free_repbuf,
437 .enlarge_reqbuf = plain_enlarge_reqbuf,
440 static struct ptlrpc_sec_sops plain_sec_sops = {
441 .accept = plain_accept,
442 .alloc_rs = plain_alloc_rs,
443 .authorize = plain_authorize,
444 .free_rs = plain_free_rs,
445 .unwrap_bulk = plain_svc_unwrap_bulk,
446 .wrap_bulk = plain_svc_wrap_bulk,
449 static struct ptlrpc_sec_policy plain_policy = {
450 .sp_owner = THIS_MODULE,
451 .sp_name = "sec.plain",
452 .sp_policy = SPTLRPC_POLICY_PLAIN,
453 .sp_cops = &plain_sec_cops,
454 .sp_sops = &plain_sec_sops,
458 void plain_init_internal(void)
460 static HLIST_HEAD(__list);
462 plain_sec.ps_policy = &plain_policy;
463 atomic_set(&plain_sec.ps_refcount, 1); /* always busy */
464 plain_sec.ps_import = NULL;
465 plain_sec.ps_flavor = SPTLRPC_FLVR_PLAIN;
466 plain_sec.ps_flags = 0;
467 plain_sec.ps_gc_interval = 0;
468 plain_sec.ps_gc_next = 0;
469 spin_lock_init(&plain_sec.ps_lock);
470 plain_sec.ps_ccache_size = 1;
471 plain_sec.ps_ccache = &__list;
472 atomic_set(&plain_sec.ps_busy, 1); /* for "plain_cli_ctx" */
474 hlist_add_head(&plain_cli_ctx.cc_hash, &__list);
475 atomic_set(&plain_cli_ctx.cc_refcount, 1); /* for hash */
476 plain_cli_ctx.cc_sec = &plain_sec;
477 plain_cli_ctx.cc_ops = &plain_ctx_ops;
478 plain_cli_ctx.cc_expire = 0;
479 plain_cli_ctx.cc_flags = PTLRPC_CTX_HASHED | PTLRPC_CTX_ETERNAL |
481 plain_cli_ctx.cc_vcred.vc_uid = 0;
482 spin_lock_init(&plain_cli_ctx.cc_lock);
483 INIT_LIST_HEAD(&plain_cli_ctx.cc_req_list);
486 int sptlrpc_plain_init(void)
490 plain_init_internal();
492 rc = sptlrpc_register_policy(&plain_policy);
494 CERROR("failed to register sec.plain: %d\n", rc);
499 void sptlrpc_plain_fini(void)
503 rc = sptlrpc_unregister_policy(&plain_policy);
505 CERROR("cannot unregister sec.plain: %d\n", rc);