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