1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004-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 null_policy;
37 static struct ptlrpc_sec null_sec;
38 static struct ptlrpc_cli_ctx null_cli_ctx;
39 static struct ptlrpc_svc_ctx null_svc_ctx;
42 int null_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
44 /* should never reach here */
50 int null_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
52 if (req->rq_reqbuf->lm_magic != LUSTRE_MSG_MAGIC_V1)
53 req->rq_reqbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
54 req->rq_reqdata_len = req->rq_reqlen;
59 int null_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
61 req->rq_repmsg = req->rq_repbuf;
62 req->rq_replen = req->rq_repdata_len;
66 static struct ptlrpc_ctx_ops null_ctx_ops = {
67 .refresh = null_ctx_refresh,
68 .sign = null_ctx_sign,
69 .verify = null_ctx_verify,
72 static struct ptlrpc_svc_ctx null_svc_ctx = {
73 .sc_refcount = ATOMIC_INIT(1),
74 .sc_policy = &null_policy,
78 struct ptlrpc_sec* null_create_sec(struct obd_import *imp,
79 struct ptlrpc_svc_ctx *ctx,
83 LASSERT(SEC_FLAVOR_POLICY(flavor) == SPTLRPC_POLICY_NULL);
88 void null_destroy_sec(struct ptlrpc_sec *sec)
90 LASSERT(sec == &null_sec);
94 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
95 struct vfs_cred *vcred)
97 atomic_inc(&null_cli_ctx.cc_refcount);
102 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
103 struct ptlrpc_request *req,
106 if (!req->rq_reqbuf) {
107 LASSERT(!req->rq_pool);
108 OBD_ALLOC(req->rq_reqbuf, msgsize);
112 req->rq_reqbuf_len = msgsize;
114 LASSERT(req->rq_pool);
115 LASSERT(req->rq_reqbuf_len >= msgsize);
116 memset(req->rq_reqbuf, 0, msgsize);
119 req->rq_reqmsg = req->rq_reqbuf;
124 void null_free_reqbuf(struct ptlrpc_sec *sec,
125 struct ptlrpc_request *req)
128 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
129 req->rq_reqbuf = NULL;
130 req->rq_reqbuf_len = 0;
135 int null_alloc_repbuf(struct ptlrpc_sec *sec,
136 struct ptlrpc_request *req,
139 OBD_ALLOC(req->rq_repbuf, msgsize);
143 req->rq_repbuf_len = msgsize;
148 void null_free_repbuf(struct ptlrpc_sec *sec,
149 struct ptlrpc_request *req)
151 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
152 req->rq_repbuf = NULL;
153 req->rq_repbuf_len = 0;
157 int null_accept(struct ptlrpc_request *req)
159 LASSERT(SEC_FLAVOR_POLICY(req->rq_sec_flavor) == SPTLRPC_POLICY_NULL);
161 if (SEC_FLAVOR_RPC(req->rq_sec_flavor) != SPTLRPC_FLVR_NULL) {
162 CERROR("Invalid flavor 0x%x\n", req->rq_sec_flavor);
166 req->rq_reqmsg = req->rq_reqbuf;
167 req->rq_reqlen = req->rq_reqdata_len;
169 req->rq_svc_ctx = &null_svc_ctx;
170 atomic_inc(&req->rq_svc_ctx->sc_refcount);
176 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
178 struct ptlrpc_reply_state *rs;
179 int rs_size = sizeof(*rs) + msgsize;
181 LASSERT(msgsize % 8 == 0);
183 rs = req->rq_reply_state;
187 LASSERT(rs->rs_size >= rs_size);
189 OBD_ALLOC(rs, rs_size);
193 rs->rs_size = rs_size;
196 rs->rs_svc_ctx = req->rq_svc_ctx;
197 atomic_inc(&req->rq_svc_ctx->sc_refcount);
199 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
200 rs->rs_repbuf_len = rs_size - sizeof(*rs);
201 rs->rs_msg = rs->rs_repbuf;
203 req->rq_reply_state = rs;
208 void null_free_rs(struct ptlrpc_reply_state *rs)
210 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
211 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
213 if (!rs->rs_prealloc)
214 OBD_FREE(rs, rs->rs_size);
218 int null_authorize(struct ptlrpc_request *req)
220 struct ptlrpc_reply_state *rs = req->rq_reply_state;
223 if (rs->rs_repbuf->lm_magic != LUSTRE_MSG_MAGIC_V1)
224 rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
225 rs->rs_repdata_len = req->rq_replen;
229 static struct ptlrpc_sec_cops null_sec_cops = {
230 .create_sec = null_create_sec,
231 .destroy_sec = null_destroy_sec,
232 .lookup_ctx = null_lookup_ctx,
233 .alloc_reqbuf = null_alloc_reqbuf,
234 .alloc_repbuf = null_alloc_repbuf,
235 .free_reqbuf = null_free_reqbuf,
236 .free_repbuf = null_free_repbuf,
239 static struct ptlrpc_sec_sops null_sec_sops = {
240 .accept = null_accept,
241 .alloc_rs = null_alloc_rs,
242 .authorize = null_authorize,
243 .free_rs = null_free_rs,
246 static struct ptlrpc_sec_policy null_policy = {
247 .sp_owner = THIS_MODULE,
248 .sp_name = "sec.null",
249 .sp_policy = SPTLRPC_POLICY_NULL,
250 .sp_cops = &null_sec_cops,
251 .sp_sops = &null_sec_sops,
255 void null_init_internal(void)
257 static HLIST_HEAD(__list);
259 null_sec.ps_policy = &null_policy;
260 atomic_set(&null_sec.ps_refcount, 1); /* always busy */
261 null_sec.ps_import = NULL;
262 null_sec.ps_flavor = SPTLRPC_FLVR_NULL;
263 null_sec.ps_flags = 0;
264 null_sec.ps_gc_interval = 0;
265 null_sec.ps_gc_next = 0;
266 spin_lock_init(&null_sec.ps_lock);
267 null_sec.ps_ccache_size = 1;
268 null_sec.ps_ccache = &__list;
269 atomic_set(&null_sec.ps_busy, 1); /* for "null_cli_ctx" */
271 hlist_add_head(&null_cli_ctx.cc_hash, &__list);
272 atomic_set(&null_cli_ctx.cc_refcount, 1); /* for hash */
273 null_cli_ctx.cc_sec = &null_sec;
274 null_cli_ctx.cc_ops = &null_ctx_ops;
275 null_cli_ctx.cc_expire = 0;
276 null_cli_ctx.cc_flags = PTLRPC_CTX_HASHED | PTLRPC_CTX_ETERNAL |
278 null_cli_ctx.cc_vcred.vc_uid = 0;
279 spin_lock_init(&null_cli_ctx.cc_lock);
280 INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
283 int sptlrpc_null_init(void)
287 null_init_internal();
289 rc = sptlrpc_register_policy(&null_policy);
291 CERROR("failed to register sec.null: %d\n", rc);
296 int sptlrpc_null_exit(void)
300 rc = sptlrpc_unregister_policy(&null_policy);
302 CERROR("cannot unregister sec.null: %d\n", rc);