Whamcloud - gitweb
4c016820232c921abf327423cf135a7cb96593a4
[fs/lustre-release.git] / lustre / ptlrpc / sec_null.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/sec_null.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42
43 #ifndef __KERNEL__
44 #include <liblustre.h>
45 #endif
46
47 #include <obd_support.h>
48 #include <obd_cksum.h>
49 #include <obd_class.h>
50 #include <lustre_net.h>
51 #include <lustre_sec.h>
52 #include "ptlrpc_internal.h"
53
54 static struct ptlrpc_sec_policy null_policy;
55 static struct ptlrpc_sec        null_sec;
56 static struct ptlrpc_cli_ctx    null_cli_ctx;
57 static struct ptlrpc_svc_ctx    null_svc_ctx;
58
59 /*
60  * we can temporarily use the topmost 8-bits of lm_secflvr to identify
61  * the source sec part.
62  */
63 static inline
64 void null_encode_sec_part(struct lustre_msg *msg, enum lustre_sec_part sp)
65 {
66         msg->lm_secflvr |= (((__u32) sp) & 0xFF) << 24;
67 }
68
69 static inline
70 enum lustre_sec_part null_decode_sec_part(struct lustre_msg *msg)
71 {
72         return (msg->lm_secflvr >> 24) & 0xFF;
73 }
74
75 static int null_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
76 {
77         /* should never reach here */
78         LBUG();
79         return 0;
80 }
81
82 static
83 int null_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
84 {
85         req->rq_reqbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
86
87         if (!req->rq_import->imp_dlm_fake) {
88                 struct obd_device *obd = req->rq_import->imp_obd;
89                 null_encode_sec_part(req->rq_reqbuf,
90                                      obd->u.cli.cl_sp_me);
91         }
92         req->rq_reqdata_len = req->rq_reqlen;
93         return 0;
94 }
95
96 static
97 int null_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
98 {
99         __u32   cksums, cksumc;
100
101         LASSERT(req->rq_repdata);
102
103         req->rq_repmsg = req->rq_repdata;
104         req->rq_replen = req->rq_repdata_len;
105
106         if (req->rq_early) {
107                 cksums = lustre_msg_get_cksum(req->rq_repdata);
108 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
109                 if (lustre_msghdr_get_flags(req->rq_reqmsg) &
110                     MSGHDR_CKSUM_INCOMPAT18)
111                         cksumc = lustre_msg_calc_cksum(req->rq_repmsg, 0);
112                 else
113                         cksumc = lustre_msg_calc_cksum(req->rq_repmsg, 1);
114 #else
115 # warning "remove checksum compatibility support for b1_8"
116                 cksumc = lustre_msg_calc_cksum(req->rq_repmsg);
117 #endif
118                 if (cksumc != cksums) {
119                         CDEBUG(D_SEC,
120                                "early reply checksum mismatch: %08x != %08x\n",
121                                cksumc, cksums);
122                         return -EINVAL;
123                 }
124         }
125
126         return 0;
127 }
128
129 static
130 struct ptlrpc_sec *null_create_sec(struct obd_import *imp,
131                                    struct ptlrpc_svc_ctx *svc_ctx,
132                                    struct sptlrpc_flavor *sf)
133 {
134         LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_NULL);
135
136         /* general layer has take a module reference for us, because we never
137          * really destroy the sec, simply release the reference here.
138          */
139         sptlrpc_policy_put(&null_policy);
140         return &null_sec;
141 }
142
143 static
144 void null_destroy_sec(struct ptlrpc_sec *sec)
145 {
146         LASSERT(sec == &null_sec);
147 }
148
149 static
150 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
151                                        struct vfs_cred *vcred,
152                                        int create, int remove_dead)
153 {
154         atomic_inc(&null_cli_ctx.cc_refcount);
155         return &null_cli_ctx;
156 }
157
158 static
159 int null_flush_ctx_cache(struct ptlrpc_sec *sec,
160                          uid_t uid,
161                          int grace, int force)
162 {
163         return 0;
164 }
165
166 static
167 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
168                       struct ptlrpc_request *req,
169                       int msgsize)
170 {
171         if (!req->rq_reqbuf) {
172                 int alloc_size = size_roundup_power2(msgsize);
173
174                 LASSERT(!req->rq_pool);
175                 OBD_ALLOC_LARGE(req->rq_reqbuf, alloc_size);
176                 if (!req->rq_reqbuf)
177                         return -ENOMEM;
178
179                 req->rq_reqbuf_len = alloc_size;
180         } else {
181                 LASSERT(req->rq_pool);
182                 LASSERT(req->rq_reqbuf_len >= msgsize);
183                 memset(req->rq_reqbuf, 0, msgsize);
184         }
185
186         req->rq_reqmsg = req->rq_reqbuf;
187         return 0;
188 }
189
190 static
191 void null_free_reqbuf(struct ptlrpc_sec *sec,
192                       struct ptlrpc_request *req)
193 {
194         if (!req->rq_pool) {
195                 LASSERTF(req->rq_reqmsg == req->rq_reqbuf,
196                          "req %p: reqmsg %p is not reqbuf %p in null sec\n",
197                          req, req->rq_reqmsg, req->rq_reqbuf);
198                 LASSERTF(req->rq_reqbuf_len >= req->rq_reqlen,
199                          "req %p: reqlen %d should smaller than buflen %d\n",
200                          req, req->rq_reqlen, req->rq_reqbuf_len);
201
202                 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
203                 req->rq_reqbuf = NULL;
204                 req->rq_reqbuf_len = 0;
205         }
206 }
207
208 static
209 int null_alloc_repbuf(struct ptlrpc_sec *sec,
210                       struct ptlrpc_request *req,
211                       int msgsize)
212 {
213         /* add space for early replied */
214         msgsize += lustre_msg_early_size();
215
216         msgsize = size_roundup_power2(msgsize);
217
218         OBD_ALLOC_LARGE(req->rq_repbuf, msgsize);
219         if (!req->rq_repbuf)
220                 return -ENOMEM;
221
222         req->rq_repbuf_len = msgsize;
223         return 0;
224 }
225
226 static
227 void null_free_repbuf(struct ptlrpc_sec *sec,
228                       struct ptlrpc_request *req)
229 {
230         LASSERT(req->rq_repbuf);
231
232         OBD_FREE_LARGE(req->rq_repbuf, req->rq_repbuf_len);
233         req->rq_repbuf = NULL;
234         req->rq_repbuf_len = 0;
235 }
236
237 static
238 int null_enlarge_reqbuf(struct ptlrpc_sec *sec,
239                         struct ptlrpc_request *req,
240                         int segment, int newsize)
241 {
242         struct lustre_msg      *oldbuf = req->rq_reqmsg;
243         int                     oldsize, newmsg_size;
244         int rc;
245
246         LASSERT(req->rq_reqbuf);
247         LASSERT(req->rq_reqbuf == req->rq_reqmsg);
248         LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
249         LASSERT(req->rq_reqlen == lustre_packed_msg_size(oldbuf));
250
251         /* compute new message size */
252         oldsize = req->rq_reqbuf->lm_buflens[segment];
253         req->rq_reqbuf->lm_buflens[segment] = newsize;
254         newmsg_size = lustre_packed_msg_size(oldbuf);
255         req->rq_reqbuf->lm_buflens[segment] = oldsize;
256
257         /* request from pool should always have enough buffer */
258         LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
259
260         if (req->rq_reqbuf_len < newmsg_size) {
261                 rc = ptlrpc_enlarge_req_buffer(req, newmsg_size);
262                 if (rc != 0)
263                         return rc;
264         }
265
266         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
267         req->rq_reqlen = newmsg_size;
268
269         return 0;
270 }
271
272 static struct ptlrpc_svc_ctx null_svc_ctx = {
273         .sc_refcount    = ATOMIC_INIT(1),
274         .sc_policy      = &null_policy,
275 };
276
277 static
278 int null_accept(struct ptlrpc_request *req)
279 {
280         LASSERT(SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) ==
281                 SPTLRPC_POLICY_NULL);
282
283         if (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL) {
284                 CERROR("Invalid rpc flavor 0x%x\n", req->rq_flvr.sf_rpc);
285                 return SECSVC_DROP;
286         }
287
288         req->rq_sp_from = null_decode_sec_part(req->rq_reqbuf);
289
290         req->rq_reqmsg = req->rq_reqbuf;
291         req->rq_reqlen = req->rq_reqdata_len;
292
293         req->rq_svc_ctx = &null_svc_ctx;
294         atomic_inc(&req->rq_svc_ctx->sc_refcount);
295
296         return SECSVC_OK;
297 }
298
299 static
300 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
301 {
302         struct ptlrpc_reply_state *rs;
303         int rs_size = sizeof(*rs) + msgsize;
304
305         LASSERT(msgsize % 8 == 0);
306
307         rs = req->rq_reply_state;
308
309         if (rs) {
310                 /* pre-allocated */
311                 LASSERT(rs->rs_size >= rs_size);
312         } else {
313                 OBD_ALLOC_LARGE(rs, rs_size);
314                 if (rs == NULL)
315                         return -ENOMEM;
316
317                 rs->rs_size = rs_size;
318         }
319
320         rs->rs_svc_ctx = req->rq_svc_ctx;
321         atomic_inc(&req->rq_svc_ctx->sc_refcount);
322
323         rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
324         rs->rs_repbuf_len = rs_size - sizeof(*rs);
325         rs->rs_msg = rs->rs_repbuf;
326
327         req->rq_reply_state = rs;
328         return 0;
329 }
330
331 static
332 void null_free_rs(struct ptlrpc_reply_state *rs)
333 {
334         LASSERT_ATOMIC_GT(&rs->rs_svc_ctx->sc_refcount, 1);
335         atomic_dec(&rs->rs_svc_ctx->sc_refcount);
336
337         if (!rs->rs_prealloc)
338                 OBD_FREE_LARGE(rs, rs->rs_size);
339 }
340
341 static
342 int null_authorize(struct ptlrpc_request *req)
343 {
344         struct ptlrpc_reply_state *rs = req->rq_reply_state;
345
346         LASSERT(rs);
347
348         rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
349         rs->rs_repdata_len = req->rq_replen;
350
351         if (likely(req->rq_packed_final)) {
352                 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
353                         req->rq_reply_off = lustre_msg_early_size();
354                 else
355                         req->rq_reply_off = 0;
356         } else {
357                 __u32 cksum;
358
359 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
360                 if (lustre_msghdr_get_flags(req->rq_reqmsg) &
361                     MSGHDR_CKSUM_INCOMPAT18)
362                         cksum = lustre_msg_calc_cksum(rs->rs_repbuf, 0);
363                 else
364                         cksum = lustre_msg_calc_cksum(rs->rs_repbuf, 1);
365 #else
366 # warning "remove checksum compatibility support for b1_8"
367                 cksum = lustre_msg_calc_cksum(rs->rs_repbuf);
368 #endif
369                 lustre_msg_set_cksum(rs->rs_repbuf, cksum);
370                 req->rq_reply_off = 0;
371         }
372
373         return 0;
374 }
375
376 static struct ptlrpc_ctx_ops null_ctx_ops = {
377         .refresh                = null_ctx_refresh,
378         .sign                   = null_ctx_sign,
379         .verify                 = null_ctx_verify,
380 };
381
382 static struct ptlrpc_sec_cops null_sec_cops = {
383         .create_sec             = null_create_sec,
384         .destroy_sec            = null_destroy_sec,
385         .lookup_ctx             = null_lookup_ctx,
386         .flush_ctx_cache        = null_flush_ctx_cache,
387         .alloc_reqbuf           = null_alloc_reqbuf,
388         .alloc_repbuf           = null_alloc_repbuf,
389         .free_reqbuf            = null_free_reqbuf,
390         .free_repbuf            = null_free_repbuf,
391         .enlarge_reqbuf         = null_enlarge_reqbuf,
392 };
393
394 static struct ptlrpc_sec_sops null_sec_sops = {
395         .accept                 = null_accept,
396         .alloc_rs               = null_alloc_rs,
397         .authorize              = null_authorize,
398         .free_rs                = null_free_rs,
399 };
400
401 static struct ptlrpc_sec_policy null_policy = {
402         .sp_owner               = THIS_MODULE,
403         .sp_name                = "sec.null",
404         .sp_policy              = SPTLRPC_POLICY_NULL,
405         .sp_cops                = &null_sec_cops,
406         .sp_sops                = &null_sec_sops,
407 };
408
409 static void null_init_internal(void)
410 {
411         static CFS_HLIST_HEAD(__list);
412
413         null_sec.ps_policy = &null_policy;
414         atomic_set(&null_sec.ps_refcount, 1);   /* always busy */
415         null_sec.ps_id = -1;
416         null_sec.ps_import = NULL;
417         null_sec.ps_flvr.sf_rpc = SPTLRPC_FLVR_NULL;
418         null_sec.ps_flvr.sf_flags = 0;
419         null_sec.ps_part = LUSTRE_SP_ANY;
420         null_sec.ps_dying = 0;
421         spin_lock_init(&null_sec.ps_lock);
422         atomic_set(&null_sec.ps_nctx, 1);       /* for "null_cli_ctx" */
423         CFS_INIT_LIST_HEAD(&null_sec.ps_gc_list);
424         null_sec.ps_gc_interval = 0;
425         null_sec.ps_gc_next = 0;
426
427         cfs_hlist_add_head(&null_cli_ctx.cc_cache, &__list);
428         atomic_set(&null_cli_ctx.cc_refcount, 1);       /* for hash */
429         null_cli_ctx.cc_sec = &null_sec;
430         null_cli_ctx.cc_ops = &null_ctx_ops;
431         null_cli_ctx.cc_expire = 0;
432         null_cli_ctx.cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_ETERNAL |
433                                 PTLRPC_CTX_UPTODATE;
434         null_cli_ctx.cc_vcred.vc_uid = 0;
435         spin_lock_init(&null_cli_ctx.cc_lock);
436         CFS_INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
437         CFS_INIT_LIST_HEAD(&null_cli_ctx.cc_gc_chain);
438 }
439
440 int sptlrpc_null_init(void)
441 {
442         int rc;
443
444         null_init_internal();
445
446         rc = sptlrpc_register_policy(&null_policy);
447         if (rc)
448                 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
449
450         return rc;
451 }
452
453 void sptlrpc_null_fini(void)
454 {
455         int rc;
456
457         rc = sptlrpc_unregister_policy(&null_policy);
458         if (rc)
459                 CERROR("failed to unregister %s: %d\n", null_policy.sp_name,rc);
460 }