1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/sec_null.c
38 * Author: Eric Mei <ericm@clusterfs.com>
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_SEC
47 #include <liblustre.h>
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>
56 static struct ptlrpc_sec_policy null_policy;
57 static struct ptlrpc_sec null_sec;
58 static struct ptlrpc_cli_ctx null_cli_ctx;
59 static struct ptlrpc_svc_ctx null_svc_ctx;
62 * null sec temporarily use the third byte of lm_secflvr to identify
63 * the source sec part.
66 void null_encode_sec_part(struct lustre_msg *msg, enum lustre_sec_part sp)
68 msg->lm_secflvr |= (((__u32) sp) & 0xFF) << 16;
72 enum lustre_sec_part null_decode_sec_part(struct lustre_msg *msg)
74 switch (msg->lm_magic) {
75 case LUSTRE_MSG_MAGIC_V2:
76 return (msg->lm_secflvr >> 16) & 0xFF;
77 case LUSTRE_MSG_MAGIC_V2_SWABBED:
78 return (msg->lm_secflvr >> 8) & 0xFF;
84 static int null_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
86 /* should never reach here */
92 int null_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
94 req->rq_reqbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
96 if (!req->rq_import->imp_dlm_fake) {
97 struct obd_device *obd = req->rq_import->imp_obd;
98 null_encode_sec_part(req->rq_reqbuf,
99 obd->u.cli.cl_sec_part);
101 req->rq_reqdata_len = req->rq_reqlen;
106 int null_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
108 __u32 cksums, cksumc;
110 LASSERT(req->rq_repdata);
113 cksums = req->rq_repdata->lm_cksum;
114 req->rq_repdata->lm_cksum = 0;
116 if (req->rq_repdata->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
119 cksumc = crc32_le(!(__u32) 0, (unsigned char *)req->rq_repdata,
120 req->rq_repdata_len);
121 if (cksumc != cksums) {
122 CWARN("early reply checksum mismatch: %08x != %08x\n",
128 req->rq_repmsg = req->rq_repdata;
129 req->rq_replen = req->rq_repdata_len;
134 struct ptlrpc_sec *null_create_sec(struct obd_import *imp,
135 struct ptlrpc_svc_ctx *svc_ctx,
136 struct sptlrpc_flavor *sf)
138 LASSERT(RPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_NULL);
140 if (sf->sf_bulk_ciph != BULK_CIPH_ALG_NULL ||
141 sf->sf_bulk_hash != BULK_HASH_ALG_NULL) {
142 CERROR("null sec don't support bulk algorithm: %u/%u\n",
143 sf->sf_bulk_ciph, sf->sf_bulk_hash);
147 /* general layer has take a module reference for us, because we never
148 * really destroy the sec, simply release the reference here.
150 sptlrpc_policy_put(&null_policy);
155 void null_destroy_sec(struct ptlrpc_sec *sec)
157 LASSERT(sec == &null_sec);
161 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
162 struct vfs_cred *vcred,
163 int create, int remove_dead)
165 atomic_inc(&null_cli_ctx.cc_refcount);
166 return &null_cli_ctx;
170 int null_flush_ctx_cache(struct ptlrpc_sec *sec,
172 int grace, int force)
178 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
179 struct ptlrpc_request *req,
182 if (!req->rq_reqbuf) {
183 int alloc_size = size_roundup_power2(msgsize);
185 LASSERT(!req->rq_pool);
186 OBD_ALLOC(req->rq_reqbuf, alloc_size);
190 req->rq_reqbuf_len = alloc_size;
192 LASSERT(req->rq_pool);
193 LASSERT(req->rq_reqbuf_len >= msgsize);
194 memset(req->rq_reqbuf, 0, msgsize);
197 req->rq_reqmsg = req->rq_reqbuf;
202 void null_free_reqbuf(struct ptlrpc_sec *sec,
203 struct ptlrpc_request *req)
206 LASSERTF(req->rq_reqmsg == req->rq_reqbuf,
207 "req %p: reqmsg %p is not reqbuf %p in null sec\n",
208 req, req->rq_reqmsg, req->rq_reqbuf);
209 LASSERTF(req->rq_reqbuf_len >= req->rq_reqlen,
210 "req %p: reqlen %d should smaller than buflen %d\n",
211 req, req->rq_reqlen, req->rq_reqbuf_len);
213 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
214 req->rq_reqmsg = req->rq_reqbuf = NULL;
215 req->rq_reqbuf_len = 0;
218 req->rq_reqmsg = NULL;
222 int null_alloc_repbuf(struct ptlrpc_sec *sec,
223 struct ptlrpc_request *req,
226 /* add space for early replied */
227 msgsize += lustre_msg_early_size();
229 msgsize = size_roundup_power2(msgsize);
231 OBD_ALLOC(req->rq_repbuf, msgsize);
235 req->rq_repbuf_len = msgsize;
240 void null_free_repbuf(struct ptlrpc_sec *sec,
241 struct ptlrpc_request *req)
243 LASSERT(req->rq_repbuf);
245 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
246 req->rq_repbuf = NULL;
247 req->rq_repbuf_len = 0;
249 req->rq_repmsg = NULL;
253 int null_enlarge_reqbuf(struct ptlrpc_sec *sec,
254 struct ptlrpc_request *req,
255 int segment, int newsize)
257 struct lustre_msg *newbuf;
258 struct lustre_msg *oldbuf = req->rq_reqmsg;
259 int oldsize, newmsg_size, alloc_size;
261 LASSERT(req->rq_reqbuf);
262 LASSERT(req->rq_reqbuf == req->rq_reqmsg);
263 LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
264 LASSERT(req->rq_reqlen == lustre_packed_msg_size(oldbuf));
266 /* compute new message size */
267 oldsize = req->rq_reqbuf->lm_buflens[segment];
268 req->rq_reqbuf->lm_buflens[segment] = newsize;
269 newmsg_size = lustre_packed_msg_size(oldbuf);
270 req->rq_reqbuf->lm_buflens[segment] = oldsize;
272 /* request from pool should always have enough buffer */
273 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
275 if (req->rq_reqbuf_len < newmsg_size) {
276 alloc_size = size_roundup_power2(newmsg_size);
278 OBD_ALLOC(newbuf, alloc_size);
282 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
284 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
285 req->rq_reqbuf = req->rq_reqmsg = newbuf;
286 req->rq_reqbuf_len = alloc_size;
289 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
290 req->rq_reqlen = newmsg_size;
295 static struct ptlrpc_svc_ctx null_svc_ctx = {
296 .sc_refcount = ATOMIC_INIT(1),
297 .sc_policy = &null_policy,
301 int null_accept(struct ptlrpc_request *req)
303 LASSERT(RPC_FLVR_POLICY(req->rq_flvr.sf_rpc) == SPTLRPC_POLICY_NULL);
305 if (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL) {
306 CERROR("Invalid rpc flavor 0x%x\n", req->rq_flvr.sf_rpc);
310 req->rq_sp_from = null_decode_sec_part(req->rq_reqbuf);
312 req->rq_reqmsg = req->rq_reqbuf;
313 req->rq_reqlen = req->rq_reqdata_len;
315 req->rq_svc_ctx = &null_svc_ctx;
316 atomic_inc(&req->rq_svc_ctx->sc_refcount);
322 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
324 struct ptlrpc_reply_state *rs;
325 int rs_size = sizeof(*rs) + msgsize;
327 LASSERT(msgsize % 8 == 0);
329 rs = req->rq_reply_state;
333 LASSERT(rs->rs_size >= rs_size);
335 OBD_ALLOC(rs, rs_size);
339 rs->rs_size = rs_size;
342 rs->rs_svc_ctx = req->rq_svc_ctx;
343 atomic_inc(&req->rq_svc_ctx->sc_refcount);
345 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
346 rs->rs_repbuf_len = rs_size - sizeof(*rs);
347 rs->rs_msg = rs->rs_repbuf;
349 req->rq_reply_state = rs;
354 void null_free_rs(struct ptlrpc_reply_state *rs)
356 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
357 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
359 if (!rs->rs_prealloc)
360 OBD_FREE(rs, rs->rs_size);
364 int null_authorize(struct ptlrpc_request *req)
366 struct ptlrpc_reply_state *rs = req->rq_reply_state;
370 rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
371 rs->rs_repdata_len = req->rq_replen;
373 if (likely(req->rq_packed_final)) {
374 req->rq_reply_off = lustre_msg_early_size();
376 rs->rs_repbuf->lm_cksum =
378 (unsigned char *)rs->rs_repbuf,
380 req->rq_reply_off = 0;
386 static struct ptlrpc_ctx_ops null_ctx_ops = {
387 .refresh = null_ctx_refresh,
388 .sign = null_ctx_sign,
389 .verify = null_ctx_verify,
392 static struct ptlrpc_sec_cops null_sec_cops = {
393 .create_sec = null_create_sec,
394 .destroy_sec = null_destroy_sec,
395 .lookup_ctx = null_lookup_ctx,
396 .flush_ctx_cache = null_flush_ctx_cache,
397 .alloc_reqbuf = null_alloc_reqbuf,
398 .alloc_repbuf = null_alloc_repbuf,
399 .free_reqbuf = null_free_reqbuf,
400 .free_repbuf = null_free_repbuf,
401 .enlarge_reqbuf = null_enlarge_reqbuf,
404 static struct ptlrpc_sec_sops null_sec_sops = {
405 .accept = null_accept,
406 .alloc_rs = null_alloc_rs,
407 .authorize = null_authorize,
408 .free_rs = null_free_rs,
411 static struct ptlrpc_sec_policy null_policy = {
412 .sp_owner = THIS_MODULE,
413 .sp_name = "sec.null",
414 .sp_policy = SPTLRPC_POLICY_NULL,
415 .sp_cops = &null_sec_cops,
416 .sp_sops = &null_sec_sops,
419 static void null_init_internal(void)
421 static HLIST_HEAD(__list);
423 null_sec.ps_policy = &null_policy;
424 atomic_set(&null_sec.ps_refcount, 1); /* always busy */
426 null_sec.ps_import = NULL;
427 null_sec.ps_flvr.sf_rpc = SPTLRPC_FLVR_NULL;
428 null_sec.ps_flvr.sf_bulk_ciph = BULK_CIPH_ALG_NULL;
429 null_sec.ps_flvr.sf_bulk_hash = BULK_HASH_ALG_NULL;
430 null_sec.ps_flvr.sf_flags = 0;
431 null_sec.ps_part = LUSTRE_SP_ANY;
432 null_sec.ps_dying = 0;
433 spin_lock_init(&null_sec.ps_lock);
434 atomic_set(&null_sec.ps_nctx, 1); /* for "null_cli_ctx" */
435 CFS_INIT_LIST_HEAD(&null_sec.ps_gc_list);
436 null_sec.ps_gc_interval = 0;
437 null_sec.ps_gc_next = 0;
439 hlist_add_head(&null_cli_ctx.cc_cache, &__list);
440 atomic_set(&null_cli_ctx.cc_refcount, 1); /* for hash */
441 null_cli_ctx.cc_sec = &null_sec;
442 null_cli_ctx.cc_ops = &null_ctx_ops;
443 null_cli_ctx.cc_expire = 0;
444 null_cli_ctx.cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_ETERNAL |
446 null_cli_ctx.cc_vcred.vc_uid = 0;
447 spin_lock_init(&null_cli_ctx.cc_lock);
448 CFS_INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
449 CFS_INIT_LIST_HEAD(&null_cli_ctx.cc_gc_chain);
452 int sptlrpc_null_init(void)
456 null_init_internal();
458 rc = sptlrpc_register_policy(&null_policy);
460 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
465 void sptlrpc_null_fini(void)
469 rc = sptlrpc_unregister_policy(&null_policy);
471 CERROR("failed to unregister %s: %d\n", null_policy.sp_name,rc);