Whamcloud - gitweb
remove ptlrpc msg v1.
[fs/lustre-release.git] / lustre / ptlrpc / sec_null.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004-2006 Cluster File Systems, Inc.
5  *   Author: Eric Mei <ericm@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26 #define DEBUG_SUBSYSTEM S_SEC
27
28 #ifndef __KERNEL__
29 #include <liblustre.h>
30 #endif
31
32 #include <obd_support.h>
33 #include <obd_class.h>
34 #include <lustre_net.h>
35 #include <lustre_sec.h>
36
37 static struct ptlrpc_sec_policy null_policy;
38 static struct ptlrpc_sec        null_sec;
39 static struct ptlrpc_cli_ctx    null_cli_ctx;
40 static struct ptlrpc_svc_ctx    null_svc_ctx;
41
42 /*
43  * null sec temporarily use the third byte of lm_secflvr to identify
44  * the source sec part.
45  */
46 static inline
47 void null_encode_sec_part(struct lustre_msg *msg, enum lustre_sec_part sp)
48 {
49         msg->lm_secflvr |= (((__u32) sp) & 0xFF) << 16;
50 }
51
52 static inline
53 enum lustre_sec_part null_decode_sec_part(struct lustre_msg *msg)
54 {
55         switch (msg->lm_magic) {
56         case LUSTRE_MSG_MAGIC_V2:
57                 return (msg->lm_secflvr >> 16) & 0xFF;
58         case LUSTRE_MSG_MAGIC_V2_SWABBED:
59                 return (msg->lm_secflvr >> 8) & 0xFF;
60         default:
61                 return LUSTRE_SP_ANY;
62         }
63 }
64
65 static
66 int null_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
67 {
68         /* should never reach here */
69         LBUG();
70         return 0;
71 }
72
73 static
74 int null_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
75 {
76         req->rq_reqbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
77
78         if (!req->rq_import->imp_dlm_fake) {
79                 struct obd_device *obd = req->rq_import->imp_obd;
80                 null_encode_sec_part(req->rq_reqbuf,
81                                      obd->u.cli.cl_sec_part);
82         }
83         req->rq_reqdata_len = req->rq_reqlen;
84         return 0;
85 }
86
87 static
88 int null_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
89 {
90         req->rq_repmsg = req->rq_repbuf;
91         req->rq_replen = req->rq_repdata_len;
92         return 0;
93 }
94
95 static struct ptlrpc_ctx_ops null_ctx_ops = {
96         .refresh        = null_ctx_refresh,
97         .sign           = null_ctx_sign,
98         .verify         = null_ctx_verify,
99 };
100
101 static struct ptlrpc_svc_ctx null_svc_ctx = {
102         .sc_refcount    = ATOMIC_INIT(1),
103         .sc_policy      = &null_policy,
104 };
105
106 static
107 struct ptlrpc_sec *null_create_sec(struct obd_import *imp,
108                                    struct ptlrpc_svc_ctx *svc_ctx,
109                                    struct sptlrpc_flavor *sf)
110 {
111         LASSERT(RPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_NULL);
112
113         if (sf->sf_bulk_ciph != BULK_CIPH_ALG_NULL ||
114             sf->sf_bulk_hash != BULK_HASH_ALG_NULL) {
115                 CERROR("null sec don't support bulk algorithm: %u/%u\n",
116                        sf->sf_bulk_ciph, sf->sf_bulk_hash);
117                 return NULL;
118         }
119
120         /* general layer has take a module reference for us, because we never
121          * really destroy the sec, simply release the reference here.
122          */
123         sptlrpc_policy_put(&null_policy);
124         return &null_sec;
125 }
126
127 static
128 void null_destroy_sec(struct ptlrpc_sec *sec)
129 {
130         LASSERT(sec == &null_sec);
131 }
132
133 static
134 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
135                                        struct vfs_cred *vcred,
136                                        int create, int remove_dead)
137 {
138         atomic_inc(&null_cli_ctx.cc_refcount);
139         return &null_cli_ctx;
140 }
141
142 static
143 int null_flush_ctx_cache(struct ptlrpc_sec *sec,
144                          uid_t uid,
145                          int grace, int force)
146 {
147         return 0;
148 }
149
150 static
151 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
152                       struct ptlrpc_request *req,
153                       int msgsize)
154 {
155         if (!req->rq_reqbuf) {
156                 int alloc_size = size_roundup_power2(msgsize);
157
158                 LASSERT(!req->rq_pool);
159                 OBD_ALLOC(req->rq_reqbuf, alloc_size);
160                 if (!req->rq_reqbuf)
161                         return -ENOMEM;
162
163                 req->rq_reqbuf_len = alloc_size;
164         } else {
165                 LASSERT(req->rq_pool);
166                 LASSERT(req->rq_reqbuf_len >= msgsize);
167                 memset(req->rq_reqbuf, 0, msgsize);
168         }
169
170         req->rq_reqmsg = req->rq_reqbuf;
171         return 0;
172 }
173
174 static
175 void null_free_reqbuf(struct ptlrpc_sec *sec,
176                       struct ptlrpc_request *req)
177 {
178         if (!req->rq_pool) {
179                 LASSERTF(req->rq_reqmsg == req->rq_reqbuf,
180                          "req %p: reqmsg %p is not reqbuf %p in null sec\n",
181                          req, req->rq_reqmsg, req->rq_reqbuf);
182                 LASSERTF(req->rq_reqbuf_len >= req->rq_reqlen,
183                          "req %p: reqlen %d should smaller than buflen %d\n",
184                          req, req->rq_reqlen, req->rq_reqbuf_len);
185
186                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
187                 req->rq_reqmsg = req->rq_reqbuf = NULL;
188                 req->rq_reqbuf_len = 0;
189         }
190
191         req->rq_reqmsg = NULL;
192 }
193
194 static
195 int null_alloc_repbuf(struct ptlrpc_sec *sec,
196                       struct ptlrpc_request *req,
197                       int msgsize)
198 {
199         msgsize = size_roundup_power2(msgsize);
200
201         OBD_ALLOC(req->rq_repbuf, msgsize);
202         if (!req->rq_repbuf)
203                 return -ENOMEM;
204
205         req->rq_repbuf_len = msgsize;
206         return 0;
207 }
208
209 static
210 void null_free_repbuf(struct ptlrpc_sec *sec,
211                       struct ptlrpc_request *req)
212 {
213         OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
214         req->rq_repbuf = NULL;
215         req->rq_repbuf_len = 0;
216
217         req->rq_repmsg = NULL;
218 }
219
220 static
221 int null_enlarge_reqbuf(struct ptlrpc_sec *sec,
222                         struct ptlrpc_request *req,
223                         int segment, int newsize)
224 {
225         struct lustre_msg      *newbuf;
226         struct lustre_msg      *oldbuf = req->rq_reqmsg;
227         int                     oldsize, newmsg_size, alloc_size;
228
229         LASSERT(req->rq_reqbuf);
230         LASSERT(req->rq_reqbuf == req->rq_reqmsg);
231         LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
232         LASSERT(req->rq_reqlen == lustre_packed_msg_size(oldbuf));
233
234         /* compute new message size */
235         oldsize = req->rq_reqbuf->lm_buflens[segment];
236         req->rq_reqbuf->lm_buflens[segment] = newsize;
237         newmsg_size = lustre_packed_msg_size(oldbuf);
238         req->rq_reqbuf->lm_buflens[segment] = oldsize;
239
240         /* request from pool should always have enough buffer */
241         LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
242
243         if (req->rq_reqbuf_len < newmsg_size) {
244                 alloc_size = size_roundup_power2(newmsg_size);
245
246                 OBD_ALLOC(newbuf, alloc_size);
247                 if (newbuf == NULL)
248                         return -ENOMEM;
249
250                 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
251
252                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
253                 req->rq_reqbuf = req->rq_reqmsg = newbuf;
254                 req->rq_reqbuf_len = alloc_size;
255         }
256
257         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
258         req->rq_reqlen = newmsg_size;
259
260         return 0;
261 }
262
263 static
264 int null_accept(struct ptlrpc_request *req)
265 {
266         LASSERT(RPC_FLVR_POLICY(req->rq_flvr.sf_rpc) == SPTLRPC_POLICY_NULL);
267
268         if (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL) {
269                 CERROR("Invalid rpc flavor 0x%x\n", req->rq_flvr.sf_rpc);
270                 return SECSVC_DROP;
271         }
272
273         req->rq_sp_from = null_decode_sec_part(req->rq_reqbuf);
274
275         req->rq_reqmsg = req->rq_reqbuf;
276         req->rq_reqlen = req->rq_reqdata_len;
277
278         req->rq_svc_ctx = &null_svc_ctx;
279         atomic_inc(&req->rq_svc_ctx->sc_refcount);
280
281         return SECSVC_OK;
282 }
283
284 static
285 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
286 {
287         struct ptlrpc_reply_state *rs;
288         int rs_size = sizeof(*rs) + msgsize;
289
290         LASSERT(msgsize % 8 == 0);
291
292         rs = req->rq_reply_state;
293
294         if (rs) {
295                 /* pre-allocated */
296                 LASSERT(rs->rs_size >= rs_size);
297         } else {
298                 OBD_ALLOC(rs, rs_size);
299                 if (rs == NULL)
300                         return -ENOMEM;
301
302                 rs->rs_size = rs_size;
303         }
304
305         rs->rs_svc_ctx = req->rq_svc_ctx;
306         atomic_inc(&req->rq_svc_ctx->sc_refcount);
307
308         rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
309         rs->rs_repbuf_len = rs_size - sizeof(*rs);
310         rs->rs_msg = rs->rs_repbuf;
311
312         req->rq_reply_state = rs;
313         return 0;
314 }
315
316 static
317 void null_free_rs(struct ptlrpc_reply_state *rs)
318 {
319         LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
320         atomic_dec(&rs->rs_svc_ctx->sc_refcount);
321
322         if (!rs->rs_prealloc)
323                 OBD_FREE(rs, rs->rs_size);
324 }
325
326 static
327 int null_authorize(struct ptlrpc_request *req)
328 {
329         struct ptlrpc_reply_state *rs = req->rq_reply_state;
330
331         LASSERT(rs);
332         rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
333         rs->rs_repdata_len = req->rq_replen;
334         return 0;
335 }
336
337 static struct ptlrpc_sec_cops null_sec_cops = {
338         .create_sec             = null_create_sec,
339         .destroy_sec            = null_destroy_sec,
340         .lookup_ctx             = null_lookup_ctx,
341         .flush_ctx_cache        = null_flush_ctx_cache,
342         .alloc_reqbuf           = null_alloc_reqbuf,
343         .alloc_repbuf           = null_alloc_repbuf,
344         .free_reqbuf            = null_free_reqbuf,
345         .free_repbuf            = null_free_repbuf,
346         .enlarge_reqbuf         = null_enlarge_reqbuf,
347 };
348
349 static struct ptlrpc_sec_sops null_sec_sops = {
350         .accept                 = null_accept,
351         .alloc_rs               = null_alloc_rs,
352         .authorize              = null_authorize,
353         .free_rs                = null_free_rs,
354 };
355
356 static struct ptlrpc_sec_policy null_policy = {
357         .sp_owner               = THIS_MODULE,
358         .sp_name                = "sec.null",
359         .sp_policy              = SPTLRPC_POLICY_NULL,
360         .sp_cops                = &null_sec_cops,
361         .sp_sops                = &null_sec_sops,
362 };
363
364 static
365 void null_init_internal(void)
366 {
367         static HLIST_HEAD(__list);
368
369         null_sec.ps_policy = &null_policy;
370         atomic_set(&null_sec.ps_refcount, 1);     /* always busy */
371         null_sec.ps_id = -1;
372         null_sec.ps_import = NULL;
373         null_sec.ps_flvr.sf_rpc = SPTLRPC_FLVR_NULL;
374         null_sec.ps_flvr.sf_bulk_ciph = BULK_CIPH_ALG_NULL;
375         null_sec.ps_flvr.sf_bulk_hash = BULK_HASH_ALG_NULL;
376         null_sec.ps_flvr.sf_flags = 0;
377         null_sec.ps_part = LUSTRE_SP_ANY;
378         null_sec.ps_dying = 0;
379         spin_lock_init(&null_sec.ps_lock);
380         atomic_set(&null_sec.ps_nctx, 1);         /* for "null_cli_ctx" */
381         CFS_INIT_LIST_HEAD(&null_sec.ps_gc_list);
382         null_sec.ps_gc_interval = 0;
383         null_sec.ps_gc_next = 0;
384
385         hlist_add_head(&null_cli_ctx.cc_cache, &__list);
386         atomic_set(&null_cli_ctx.cc_refcount, 1);    /* for hash */
387         null_cli_ctx.cc_sec = &null_sec;
388         null_cli_ctx.cc_ops = &null_ctx_ops;
389         null_cli_ctx.cc_expire = 0;
390         null_cli_ctx.cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_ETERNAL |
391                                 PTLRPC_CTX_UPTODATE;
392         null_cli_ctx.cc_vcred.vc_uid = 0;
393         spin_lock_init(&null_cli_ctx.cc_lock);
394         CFS_INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
395         CFS_INIT_LIST_HEAD(&null_cli_ctx.cc_gc_chain);
396 }
397
398 int sptlrpc_null_init(void)
399 {
400         int rc;
401
402         null_init_internal();
403
404         rc = sptlrpc_register_policy(&null_policy);
405         if (rc)
406                 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
407
408         return rc;
409 }
410
411 void sptlrpc_null_fini(void)
412 {
413         int rc;
414
415         rc = sptlrpc_unregister_policy(&null_policy);
416         if (rc)
417                 CERROR("failed to unregister %s: %d\n", null_policy.sp_name,rc);
418 }