Whamcloud - gitweb
b=17310
[fs/lustre-release.git] / lustre / ptlrpc / sec.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 #define EXPORT_SYMTAB
43 #endif
44 #define DEBUG_SUBSYSTEM S_SEC
45
46 #include <libcfs/libcfs.h>
47 #ifndef __KERNEL__
48 #include <liblustre.h>
49 #include <libcfs/list.h>
50 #else
51 #include <linux/crypto.h>
52 #include <linux/key.h>
53 #endif
54
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <obd_support.h>
58 #include <lustre_net.h>
59 #include <lustre_import.h>
60 #include <lustre_dlm.h>
61 #include <lustre_sec.h>
62
63 #include "ptlrpc_internal.h"
64
65 /***********************************************
66  * policy registers                            *
67  ***********************************************/
68
69 static rwlock_t policy_lock;
70 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
71         NULL,
72 };
73
74 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
75 {
76         __u16 number = policy->sp_policy;
77
78         LASSERT(policy->sp_name);
79         LASSERT(policy->sp_cops);
80         LASSERT(policy->sp_sops);
81
82         if (number >= SPTLRPC_POLICY_MAX)
83                 return -EINVAL;
84
85         write_lock(&policy_lock);
86         if (unlikely(policies[number])) {
87                 write_unlock(&policy_lock);
88                 return -EALREADY;
89         }
90         policies[number] = policy;
91         write_unlock(&policy_lock);
92
93         CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
94         return 0;
95 }
96 EXPORT_SYMBOL(sptlrpc_register_policy);
97
98 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
99 {
100         __u16 number = policy->sp_policy;
101
102         LASSERT(number < SPTLRPC_POLICY_MAX);
103
104         write_lock(&policy_lock);
105         if (unlikely(policies[number] == NULL)) {
106                 write_unlock(&policy_lock);
107                 CERROR("%s: already unregistered\n", policy->sp_name);
108                 return -EINVAL;
109         }
110
111         LASSERT(policies[number] == policy);
112         policies[number] = NULL;
113         write_unlock(&policy_lock);
114
115         CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
116         return 0;
117 }
118 EXPORT_SYMBOL(sptlrpc_unregister_policy);
119
120 static
121 struct ptlrpc_sec_policy * sptlrpc_rpcflavor2policy(__u16 flavor)
122 {
123         static DECLARE_MUTEX(load_mutex);
124         static atomic_t           loaded = ATOMIC_INIT(0);
125         struct ptlrpc_sec_policy *policy;
126         __u16                     number = RPC_FLVR_POLICY(flavor), flag = 0;
127
128         if (number >= SPTLRPC_POLICY_MAX)
129                 return NULL;
130
131         while (1) {
132                 read_lock(&policy_lock);
133                 policy = policies[number];
134                 if (policy && !try_module_get(policy->sp_owner))
135                         policy = NULL;
136                 if (policy == NULL)
137                         flag = atomic_read(&loaded);
138                 read_unlock(&policy_lock);
139
140                 if (policy != NULL || flag != 0 ||
141                     number != SPTLRPC_POLICY_GSS)
142                         break;
143
144                 /* try to load gss module, once */
145                 mutex_down(&load_mutex);
146                 if (atomic_read(&loaded) == 0) {
147                         if (request_module("ptlrpc_gss") == 0)
148                                 CWARN("module ptlrpc_gss loaded on demand\n");
149                         else
150                                 CERROR("Unable to load module ptlrpc_gss\n");
151
152                         atomic_set(&loaded, 1);
153                 }
154                 mutex_up(&load_mutex);
155         }
156
157         return policy;
158 }
159
160 __u16 sptlrpc_name2rpcflavor(const char *name)
161 {
162         if (!strcmp(name, "null"))
163                 return SPTLRPC_FLVR_NULL;
164         if (!strcmp(name, "plain"))
165                 return SPTLRPC_FLVR_PLAIN;
166         if (!strcmp(name, "krb5n"))
167                 return SPTLRPC_FLVR_KRB5N;
168         if (!strcmp(name, "krb5a"))
169                 return SPTLRPC_FLVR_KRB5A;
170         if (!strcmp(name, "krb5i"))
171                 return SPTLRPC_FLVR_KRB5I;
172         if (!strcmp(name, "krb5p"))
173                 return SPTLRPC_FLVR_KRB5P;
174
175         return SPTLRPC_FLVR_INVALID;
176 }
177 EXPORT_SYMBOL(sptlrpc_name2rpcflavor);
178
179 const char *sptlrpc_rpcflavor2name(__u16 flavor)
180 {
181         switch (flavor) {
182         case SPTLRPC_FLVR_NULL:
183                 return "null";
184         case SPTLRPC_FLVR_PLAIN:
185                 return "plain";
186         case SPTLRPC_FLVR_KRB5N:
187                 return "krb5n";
188         case SPTLRPC_FLVR_KRB5A:
189                 return "krb5a";
190         case SPTLRPC_FLVR_KRB5I:
191                 return "krb5i";
192         case SPTLRPC_FLVR_KRB5P:
193                 return "krb5p";
194         default:
195                 CERROR("invalid rpc flavor 0x%x(p%u,s%u,v%u)\n", flavor,
196                        RPC_FLVR_POLICY(flavor), RPC_FLVR_MECH(flavor),
197                        RPC_FLVR_SVC(flavor));
198         }
199         return "unknown";
200 }
201 EXPORT_SYMBOL(sptlrpc_rpcflavor2name);
202
203 int sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
204 {
205         char           *bulk;
206
207         if (sf->sf_bulk_ciph != BULK_CIPH_ALG_NULL)
208                 bulk = "bulkp";
209         else if (sf->sf_bulk_hash != BULK_HASH_ALG_NULL)
210                 bulk = "bulki";
211         else
212                 bulk = "bulkn";
213
214         snprintf(buf, bufsize, "%s-%s:%s/%s",
215                  sptlrpc_rpcflavor2name(sf->sf_rpc), bulk,
216                  sptlrpc_get_hash_name(sf->sf_bulk_hash),
217                  sptlrpc_get_ciph_name(sf->sf_bulk_ciph));
218         return 0;
219 }
220 EXPORT_SYMBOL(sptlrpc_flavor2name);
221
222 /**************************************************
223  * client context APIs                            *
224  **************************************************/
225
226 static
227 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
228 {
229         struct vfs_cred vcred;
230         int create = 1, remove_dead = 1;
231
232         LASSERT(sec);
233         LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
234
235         if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
236                                      PTLRPC_SEC_FL_ROOTONLY)) {
237                 vcred.vc_uid = 0;
238                 vcred.vc_gid = 0;
239                 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
240                         create = 0;
241                         remove_dead = 0;
242                 }
243         } else {
244                 vcred.vc_uid = cfs_current()->uid;
245                 vcred.vc_gid = cfs_current()->gid;
246         }
247
248         return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
249                                                    create, remove_dead);
250 }
251
252 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
253 {
254         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
255         atomic_inc(&ctx->cc_refcount);
256         return ctx;
257 }
258 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
259
260 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
261 {
262         struct ptlrpc_sec *sec = ctx->cc_sec;
263
264         LASSERT(sec);
265         LASSERT(atomic_read(&ctx->cc_refcount));
266
267         if (!atomic_dec_and_test(&ctx->cc_refcount))
268                 return;
269
270         sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
271 }
272 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
273
274 /*
275  * expire the context immediately.
276  * the caller must hold at least 1 ref on the ctx.
277  */
278 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
279 {
280         LASSERT(ctx->cc_ops->die);
281         ctx->cc_ops->die(ctx, 0);
282 }
283 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
284
285 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
286 {
287         struct ptlrpc_request *req, *next;
288
289         spin_lock(&ctx->cc_lock);
290         list_for_each_entry_safe(req, next, &ctx->cc_req_list, rq_ctx_chain) {
291                 list_del_init(&req->rq_ctx_chain);
292                 ptlrpc_client_wake_req(req);
293         }
294         spin_unlock(&ctx->cc_lock);
295 }
296 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
297
298 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
299 {
300         LASSERT(ctx->cc_ops);
301
302         if (ctx->cc_ops->display == NULL)
303                 return 0;
304
305         return ctx->cc_ops->display(ctx, buf, bufsize);
306 }
307
308 static int sptlrpc_import_sec_check_expire(struct obd_import *imp)
309 {
310         int     adapt = 0;
311
312         spin_lock(&imp->imp_lock);
313         if (imp->imp_sec_expire &&
314             imp->imp_sec_expire < cfs_time_current_sec()) {
315                 adapt = 1;
316                 imp->imp_sec_expire = 0;
317         }
318         spin_unlock(&imp->imp_lock);
319
320         if (!adapt)
321                 return 0;
322
323         CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
324         return sptlrpc_import_sec_adapt(imp, NULL, 0);
325 }
326
327 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
328 {
329         struct obd_import *imp = req->rq_import;
330         struct ptlrpc_sec *sec;
331         int                rc;
332         ENTRY;
333
334         LASSERT(!req->rq_cli_ctx);
335         LASSERT(imp);
336
337         if (unlikely(imp->imp_sec_expire)) {
338                 rc = sptlrpc_import_sec_check_expire(imp);
339                 if (rc)
340                         RETURN(rc);
341         }
342
343         sec = sptlrpc_import_sec_ref(imp);
344         if (sec == NULL) {
345                 CERROR("import %p (%s) with no ptlrpc_sec\n",
346                        imp, ptlrpc_import_state_name(imp->imp_state));
347                 RETURN(-EACCES);
348         }
349
350         if (unlikely(sec->ps_dying)) {
351                 CERROR("attempt to use dying sec %p\n", sec);
352                 return -EACCES;
353         }
354
355         req->rq_cli_ctx = get_my_ctx(sec);
356
357         sptlrpc_sec_put(sec);
358
359         if (!req->rq_cli_ctx) {
360                 CERROR("req %p: fail to get context\n", req);
361                 RETURN(-ENOMEM);
362         }
363
364         RETURN(0);
365 }
366
367 /*
368  * if @sync == 0, this function should return quickly without sleep;
369  * otherwise might trigger ctx destroying rpc to server.
370  */
371 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
372 {
373         ENTRY;
374
375         LASSERT(req);
376         LASSERT(req->rq_cli_ctx);
377
378         /* request might be asked to release earlier while still
379          * in the context waiting list.
380          */
381         if (!list_empty(&req->rq_ctx_chain)) {
382                 spin_lock(&req->rq_cli_ctx->cc_lock);
383                 list_del_init(&req->rq_ctx_chain);
384                 spin_unlock(&req->rq_cli_ctx->cc_lock);
385         }
386
387         sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
388         req->rq_cli_ctx = NULL;
389         EXIT;
390 }
391
392 static
393 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
394                            struct ptlrpc_cli_ctx *oldctx,
395                            struct ptlrpc_cli_ctx *newctx)
396 {
397         struct sptlrpc_flavor   old_flvr;
398         char                   *reqmsg;
399         int                     reqmsg_size;
400         int                     rc;
401
402         if (likely(oldctx->cc_sec == newctx->cc_sec))
403                 return 0;
404
405         LASSERT(req->rq_reqmsg);
406         LASSERT(req->rq_reqlen);
407         LASSERT(req->rq_replen);
408
409         CWARN("req %p: switch ctx %p -> %p, switch sec %p(%s) -> %p(%s)\n",
410               req, oldctx, newctx,
411               oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
412               newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
413
414         /* save flavor */
415         old_flvr = req->rq_flvr;
416
417         /* save request message */
418         reqmsg_size = req->rq_reqlen;
419         OBD_ALLOC(reqmsg, reqmsg_size);
420         if (reqmsg == NULL)
421                 return -ENOMEM;
422         memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
423
424         /* release old req/rep buf */
425         req->rq_cli_ctx = oldctx;
426         sptlrpc_cli_free_reqbuf(req);
427         sptlrpc_cli_free_repbuf(req);
428         req->rq_cli_ctx = newctx;
429
430         /* recalculate the flavor */
431         sptlrpc_req_set_flavor(req, 0);
432
433         /* alloc new request buffer
434          * we don't need to alloc reply buffer here, leave it to the
435          * rest procedure of ptlrpc
436          */
437         rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
438         if (!rc) {
439                 LASSERT(req->rq_reqmsg);
440                 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
441         } else {
442                 CWARN("failed to alloc reqbuf: %d\n", rc);
443                 req->rq_flvr = old_flvr;
444         }
445
446         OBD_FREE(reqmsg, reqmsg_size);
447         return rc;
448 }
449
450 /*
451  * request must have a context. in any case of failure, restore the
452  * restore the old one. a request must have a ctx.
453  */
454 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
455 {
456         struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
457         struct ptlrpc_cli_ctx *newctx;
458         int                    rc;
459         ENTRY;
460
461         LASSERT(oldctx);
462         LASSERT(test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags));
463
464         sptlrpc_cli_ctx_get(oldctx);
465         sptlrpc_req_put_ctx(req, 0);
466
467         rc = sptlrpc_req_get_ctx(req);
468         if (unlikely(rc)) {
469                 LASSERT(!req->rq_cli_ctx);
470
471                 /* restore old ctx */
472                 req->rq_cli_ctx = oldctx;
473                 RETURN(rc);
474         }
475
476         newctx = req->rq_cli_ctx;
477         LASSERT(newctx);
478
479         if (unlikely(newctx == oldctx)) {
480                 /*
481                  * still get the old ctx, usually means system busy
482                  */
483                 CWARN("ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
484                       newctx, newctx->cc_flags);
485
486                 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE, HZ);
487         } else {
488                 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
489                 if (rc) {
490                         /* restore old ctx */
491                         sptlrpc_req_put_ctx(req, 0);
492                         req->rq_cli_ctx = oldctx;
493                         RETURN(rc);
494                 }
495
496                 LASSERT(req->rq_cli_ctx == newctx);
497         }
498
499         sptlrpc_cli_ctx_put(oldctx, 1);
500         RETURN(0);
501 }
502 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
503
504 static
505 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
506 {
507         if (cli_ctx_is_refreshed(ctx))
508                 return 1;
509         return 0;
510 }
511
512 static
513 int ctx_refresh_timeout(void *data)
514 {
515         struct ptlrpc_request *req = data;
516         int rc;
517
518         /* conn_cnt is needed in expire_one_request */
519         lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
520
521         rc = ptlrpc_expire_one_request(req, 1);
522         /* if we started recovery, we should mark this ctx dead; otherwise
523          * in case of lgssd died nobody would retire this ctx, following
524          * connecting will still find the same ctx thus cause deadlock.
525          * there's an assumption that expire time of the request should be
526          * later than the context refresh expire time.
527          */
528         if (rc == 0)
529                 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
530         return rc;
531 }
532
533 static
534 void ctx_refresh_interrupt(void *data)
535 {
536         struct ptlrpc_request *req = data;
537
538         spin_lock(&req->rq_lock);
539         req->rq_intr = 1;
540         spin_unlock(&req->rq_lock);
541 }
542
543 static
544 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
545 {
546         spin_lock(&ctx->cc_lock);
547         if (!list_empty(&req->rq_ctx_chain))
548                 list_del_init(&req->rq_ctx_chain);
549         spin_unlock(&ctx->cc_lock);
550 }
551
552 /*
553  * the status of context could be subject to be changed by other threads at any
554  * time. we allow this race. but once we return with 0, the caller will
555  * suppose it's uptodated and keep using it until the owning rpc is done.
556  *
557  * @timeout:
558  *    < 0  - don't wait
559  *    = 0  - wait until success or fatal error occur
560  *    > 0  - timeout value
561  *
562  * return 0 only if the context is uptodated.
563  */
564 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
565 {
566         struct ptlrpc_cli_ctx  *ctx = req->rq_cli_ctx;
567         struct l_wait_info      lwi;
568         int                     rc;
569         ENTRY;
570
571         LASSERT(ctx);
572
573         /*
574          * during the process a request's context might change type even
575          * (e.g. from gss ctx to plain ctx), so each loop we need to re-check
576          * everything
577          */
578 again:
579         /* skip special ctxs */
580         if (cli_ctx_is_eternal(ctx) || req->rq_ctx_init || req->rq_ctx_fini)
581                 RETURN(0);
582
583         if (test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags)) {
584                 LASSERT(ctx->cc_ops->refresh);
585                 ctx->cc_ops->refresh(ctx);
586         }
587         LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
588
589         LASSERT(ctx->cc_ops->validate);
590         if (ctx->cc_ops->validate(ctx) == 0) {
591                 req_off_ctx_list(req, ctx);
592                 RETURN(0);
593         }
594
595         if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
596                 req->rq_err = 1;
597                 req_off_ctx_list(req, ctx);
598                 RETURN(-EPERM);
599         }
600
601         /* This is subtle. For resent message we have to keep original
602          * context to survive following situation:
603          *  1. the request sent to server
604          *  2. recovery was kick start
605          *  3. recovery finished, the request marked as resent
606          *  4. resend the request
607          *  5. old reply from server received (because xid is the same)
608          *  6. verify reply (has to be success)
609          *  7. new reply from server received, lnet drop it
610          *
611          * Note we can't simply change xid for resent request because
612          * server reply on it for reply reconstruction.
613          *
614          * Commonly the original context should be uptodate because we
615          * have a expiry nice time; And server will keep their half part
616          * context because we at least hold a ref of old context which
617          * prevent the context detroy RPC be sent. So server still can
618          * accept the request and finish RPC. Two cases:
619          *  1. If server side context has been trimed, a NO_CONTEXT will
620          *     be returned, gss_cli_ctx_verify/unseal will switch to new
621          *     context by force.
622          *  2. Current context never be refreshed, then we are fine: we
623          *     never really send request with old context before.
624          */
625         if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
626             unlikely(req->rq_reqmsg) &&
627             lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
628                 req_off_ctx_list(req, ctx);
629                 RETURN(0);
630         }
631
632         if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
633                 rc = sptlrpc_req_replace_dead_ctx(req);
634                 if (rc) {
635                         LASSERT(ctx == req->rq_cli_ctx);
636                         CERROR("req %p: failed to replace dead ctx %p: %d\n",
637                                 req, ctx, rc);
638                         req->rq_err = 1;
639                         LASSERT(list_empty(&req->rq_ctx_chain));
640                         RETURN(rc);
641                 }
642
643                 CWARN("req %p: replace dead ctx %p => ctx %p (%u->%s)\n",
644                       req, ctx, req->rq_cli_ctx,
645                       req->rq_cli_ctx->cc_vcred.vc_uid,
646                       sec2target_str(req->rq_cli_ctx->cc_sec));
647
648                 ctx = req->rq_cli_ctx;
649                 LASSERT(list_empty(&req->rq_ctx_chain));
650
651                 goto again;
652         }
653
654         /* Now we're sure this context is during upcall, add myself into
655          * waiting list
656          */
657         spin_lock(&ctx->cc_lock);
658         if (list_empty(&req->rq_ctx_chain))
659                 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
660         spin_unlock(&ctx->cc_lock);
661
662         if (timeout < 0) {
663                 RETURN(-EWOULDBLOCK);
664         }
665
666         /* Clear any flags that may be present from previous sends */
667         LASSERT(req->rq_receiving_reply == 0);
668         spin_lock(&req->rq_lock);
669         req->rq_err = 0;
670         req->rq_timedout = 0;
671         req->rq_resend = 0;
672         req->rq_restart = 0;
673         spin_unlock(&req->rq_lock);
674
675         lwi = LWI_TIMEOUT_INTR(timeout * HZ, ctx_refresh_timeout,
676                                ctx_refresh_interrupt, req);
677         rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
678
679         /* following cases we could be here:
680          * - successfully refreshed;
681          * - interruptted;
682          * - timedout, and we don't want recover from the failure;
683          * - timedout, and waked up upon recovery finished;
684          * - someone else mark this ctx dead by force;
685          * - someone invalidate the req and call ptlrpc_client_wake_req(),
686          *   e.g. ptlrpc_abort_inflight();
687          */
688         if (!cli_ctx_is_refreshed(ctx)) {
689                 /* timed out or interruptted */
690                 req_off_ctx_list(req, ctx);
691
692                 LASSERT(rc != 0);
693                 RETURN(rc);
694         }
695
696         goto again;
697 }
698
699 /*
700  * Note this could be called in two situations:
701  * - new request from ptlrpc_pre_req(), with proper @opcode
702  * - old request which changed ctx in the middle, with @opcode == 0
703  */
704 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
705 {
706         struct ptlrpc_sec *sec;
707
708         LASSERT(req->rq_import);
709         LASSERT(req->rq_cli_ctx);
710         LASSERT(req->rq_cli_ctx->cc_sec);
711         LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
712
713         /* special security flags accoding to opcode */
714         switch (opcode) {
715         case OST_READ:
716                 req->rq_bulk_read = 1;
717                 break;
718         case OST_WRITE:
719                 req->rq_bulk_write = 1;
720                 break;
721         case SEC_CTX_INIT:
722                 req->rq_ctx_init = 1;
723                 break;
724         case SEC_CTX_FINI:
725                 req->rq_ctx_fini = 1;
726                 break;
727         case 0:
728                 /* init/fini rpc won't be resend, so can't be here */
729                 LASSERT(req->rq_ctx_init == 0);
730                 LASSERT(req->rq_ctx_fini == 0);
731
732                 /* cleanup flags, which should be recalculated */
733                 req->rq_pack_udesc = 0;
734                 req->rq_pack_bulk = 0;
735                 break;
736         }
737
738         sec = req->rq_cli_ctx->cc_sec;
739
740         spin_lock(&sec->ps_lock);
741         req->rq_flvr = sec->ps_flvr;
742         spin_unlock(&sec->ps_lock);
743
744         /* force SVC_NULL for context initiation rpc, SVC_INTG for context
745          * destruction rpc */
746         if (unlikely(req->rq_ctx_init))
747                 rpc_flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
748         else if (unlikely(req->rq_ctx_fini))
749                 rpc_flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
750
751         /* user descriptor flag, null security can't do it anyway */
752         if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
753             (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
754                 req->rq_pack_udesc = 1;
755
756         /* bulk security flag */
757         if ((req->rq_bulk_read || req->rq_bulk_write) &&
758             (req->rq_flvr.sf_bulk_ciph != BULK_CIPH_ALG_NULL ||
759              req->rq_flvr.sf_bulk_hash != BULK_HASH_ALG_NULL))
760                 req->rq_pack_bulk = 1;
761 }
762
763 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
764 {
765         if (RPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
766                 return;
767
768         LASSERT(req->rq_clrbuf);
769         if (req->rq_pool || !req->rq_reqbuf)
770                 return;
771
772         OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
773         req->rq_reqbuf = NULL;
774         req->rq_reqbuf_len = 0;
775 }
776
777 /*
778  * check whether current user have valid context for an import or not.
779  * might repeatedly try in case of non-fatal errors.
780  * return 0 on success, < 0 on failure
781  */
782 int sptlrpc_import_check_ctx(struct obd_import *imp)
783 {
784         struct ptlrpc_sec     *sec;
785         struct ptlrpc_cli_ctx *ctx;
786         struct ptlrpc_request *req = NULL;
787         int rc;
788         ENTRY;
789
790         might_sleep();
791
792         sec = sptlrpc_import_sec_ref(imp);
793         ctx = get_my_ctx(sec);
794         sptlrpc_sec_put(sec);
795
796         if (!ctx)
797                 RETURN(1);
798
799         if (cli_ctx_is_eternal(ctx) ||
800             ctx->cc_ops->validate(ctx) == 0) {
801                 sptlrpc_cli_ctx_put(ctx, 1);
802                 RETURN(0);
803         }
804
805         OBD_ALLOC_PTR(req);
806         if (!req)
807                 RETURN(-ENOMEM);
808
809         spin_lock_init(&req->rq_lock);
810         atomic_set(&req->rq_refcount, 10000);
811         CFS_INIT_LIST_HEAD(&req->rq_ctx_chain);
812         cfs_waitq_init(&req->rq_reply_waitq);
813         req->rq_import = imp;
814         req->rq_cli_ctx = ctx;
815
816         rc = sptlrpc_req_refresh_ctx(req, 0);
817         LASSERT(list_empty(&req->rq_ctx_chain));
818         sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
819         OBD_FREE_PTR(req);
820
821         RETURN(rc);
822 }
823
824 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
825 {
826         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
827         int rc = 0;
828         ENTRY;
829
830         LASSERT(ctx);
831         LASSERT(ctx->cc_sec);
832         LASSERT(req->rq_reqbuf || req->rq_clrbuf);
833
834         /* we wrap bulk request here because now we can be sure
835          * the context is uptodate.
836          */
837         if (req->rq_bulk) {
838                 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
839                 if (rc)
840                         RETURN(rc);
841         }
842
843         switch (RPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
844         case SPTLRPC_SVC_NULL:
845         case SPTLRPC_SVC_AUTH:
846         case SPTLRPC_SVC_INTG:
847                 LASSERT(ctx->cc_ops->sign);
848                 rc = ctx->cc_ops->sign(ctx, req);
849                 break;
850         case SPTLRPC_SVC_PRIV:
851                 LASSERT(ctx->cc_ops->seal);
852                 rc = ctx->cc_ops->seal(ctx, req);
853                 break;
854         default:
855                 LBUG();
856         }
857
858         if (rc == 0) {
859                 LASSERT(req->rq_reqdata_len);
860                 LASSERT(req->rq_reqdata_len % 8 == 0);
861                 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
862         }
863
864         RETURN(rc);
865 }
866
867 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
868 {
869         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
870         int                    rc;
871         __u16                  rpc_flvr;
872         ENTRY;
873
874         LASSERT(ctx);
875         LASSERT(ctx->cc_sec);
876         LASSERT(req->rq_repbuf);
877         LASSERT(req->rq_repdata);
878         LASSERT(req->rq_repmsg == NULL);
879
880         if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
881                 CERROR("replied data length %d too small\n",
882                        req->rq_repdata_len);
883                 RETURN(-EPROTO);
884         }
885
886         /* v2 message, check request/reply policy match */
887         rpc_flvr = WIRE_FLVR_RPC(req->rq_repdata->lm_secflvr);
888
889         if (req->rq_repdata->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
890                 __swab16s(&rpc_flvr);
891
892         if (RPC_FLVR_POLICY(rpc_flvr) !=
893             RPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
894                 CERROR("request policy was %u while reply with %u\n",
895                        RPC_FLVR_POLICY(req->rq_flvr.sf_rpc),
896                        RPC_FLVR_POLICY(rpc_flvr));
897                 RETURN(-EPROTO);
898         }
899
900         /* do nothing if it's null policy; otherwise unpack the
901          * wrapper message */
902         if (RPC_FLVR_POLICY(rpc_flvr) != SPTLRPC_POLICY_NULL &&
903             lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len))
904                 RETURN(-EPROTO);
905
906         switch (RPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
907         case SPTLRPC_SVC_NULL:
908         case SPTLRPC_SVC_AUTH:
909         case SPTLRPC_SVC_INTG:
910                 LASSERT(ctx->cc_ops->verify);
911                 rc = ctx->cc_ops->verify(ctx, req);
912                 break;
913         case SPTLRPC_SVC_PRIV:
914                 LASSERT(ctx->cc_ops->unseal);
915                 rc = ctx->cc_ops->unseal(ctx, req);
916                 break;
917         default:
918                 LBUG();
919         }
920
921         LASSERT(rc || req->rq_repmsg || req->rq_resend);
922         RETURN(rc);
923 }
924
925 /*
926  * upon this be called, the reply buffer should have been un-posted,
927  * so nothing is going to change.
928  */
929 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
930 {
931         LASSERT(req->rq_repbuf);
932         LASSERT(req->rq_repdata == NULL);
933         LASSERT(req->rq_repmsg == NULL);
934         LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
935
936         if (req->rq_reply_off == 0 &&
937             (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
938                 CERROR("real reply with offset 0\n");
939                 return -EPROTO;
940         }
941
942         if (req->rq_reply_off % 8 != 0) {
943                 CERROR("reply at odd offset %u\n", req->rq_reply_off);
944                 return -EPROTO;
945         }
946
947         req->rq_repdata = (struct lustre_msg *)
948                                 (req->rq_repbuf + req->rq_reply_off);
949         req->rq_repdata_len = req->rq_nob_received;
950
951         return do_cli_unwrap_reply(req);
952 }
953
954 /**
955  * Upon called, the receive buffer might be still posted, so the reply data
956  * might be changed at any time, no matter we're holding rq_lock or not. we
957  * expect the rq_reply_off be 0, rq_nob_received is the early reply size.
958  *
959  * we allocate separate ptlrpc_request and reply buffer for early reply
960  * processing, return 0 and @req_ret is a duplicated ptlrpc_request. caller
961  * must call sptlrpc_cli_finish_early_reply() on the returned request to
962  * release it. if anything goes wrong @req_ret will not be set.
963  */
964 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
965                                    struct ptlrpc_request **req_ret)
966 {
967         struct ptlrpc_request  *early_req;
968         char                   *early_buf;
969         int                     early_bufsz, early_size;
970         int                     rc;
971         ENTRY;
972
973         OBD_ALLOC_PTR(early_req);
974         if (early_req == NULL)
975                 RETURN(-ENOMEM);
976
977         early_size = req->rq_nob_received;
978         early_bufsz = size_roundup_power2(early_size);
979         OBD_ALLOC(early_buf, early_bufsz);
980         if (early_buf == NULL)
981                 GOTO(err_req, rc = -ENOMEM);
982
983         /* sanity checkings and copy data out, do it inside spinlock */
984         spin_lock(&req->rq_lock);
985
986         if (req->rq_replied) {
987                 spin_unlock(&req->rq_lock);
988                 GOTO(err_buf, rc = -EALREADY);
989         }
990
991         LASSERT(req->rq_repbuf);
992         LASSERT(req->rq_repdata == NULL);
993         LASSERT(req->rq_repmsg == NULL);
994
995         if (req->rq_reply_off != 0) {
996                 CERROR("early reply with offset %u\n", req->rq_reply_off);
997                 spin_unlock(&req->rq_lock);
998                 GOTO(err_buf, rc = -EPROTO);
999         }
1000
1001         if (req->rq_nob_received != early_size) {
1002                 /* even another early arrived the size should be the same */
1003                 CERROR("data size has changed from %u to %u\n",
1004                        early_size, req->rq_nob_received);
1005                 spin_unlock(&req->rq_lock);
1006                 GOTO(err_buf, rc = -EINVAL);
1007         }
1008
1009         if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1010                 CERROR("early reply length %d too small\n",
1011                        req->rq_nob_received);
1012                 spin_unlock(&req->rq_lock);
1013                 GOTO(err_buf, rc = -EALREADY);
1014         }
1015
1016         memcpy(early_buf, req->rq_repbuf, early_size);
1017         spin_unlock(&req->rq_lock);
1018
1019         early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1020         early_req->rq_flvr = req->rq_flvr;
1021         early_req->rq_repbuf = early_buf;
1022         early_req->rq_repbuf_len = early_bufsz;
1023         early_req->rq_repdata = (struct lustre_msg *) early_buf;
1024         early_req->rq_repdata_len = early_size;
1025         early_req->rq_early = 1;
1026
1027         rc = do_cli_unwrap_reply(early_req);
1028         if (rc) {
1029                 DEBUG_REQ(D_ADAPTTO, early_req,
1030                           "error %d unwrap early reply", rc);
1031                 GOTO(err_ctx, rc);
1032         }
1033
1034         LASSERT(early_req->rq_repmsg);
1035         *req_ret = early_req;
1036         RETURN(0);
1037
1038 err_ctx:
1039         sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1040 err_buf:
1041         OBD_FREE(early_buf, early_bufsz);
1042 err_req:
1043         OBD_FREE_PTR(early_req);
1044         RETURN(rc);
1045 }
1046
1047 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1048 {
1049         LASSERT(early_req->rq_repbuf);
1050         LASSERT(early_req->rq_repdata);
1051         LASSERT(early_req->rq_repmsg);
1052
1053         sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1054         OBD_FREE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1055         OBD_FREE_PTR(early_req);
1056 }
1057
1058 /**************************************************
1059  * sec ID                                         *
1060  **************************************************/
1061
1062 /*
1063  * "fixed" sec (e.g. null) use sec_id < 0
1064  */
1065 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1066
1067 int sptlrpc_get_next_secid(void)
1068 {
1069         return atomic_inc_return(&sptlrpc_sec_id);
1070 }
1071 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1072
1073 /**************************************************
1074  * client side high-level security APIs           *
1075  **************************************************/
1076
1077 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1078                                    int grace, int force)
1079 {
1080         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1081
1082         LASSERT(policy->sp_cops);
1083         LASSERT(policy->sp_cops->flush_ctx_cache);
1084
1085         return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1086 }
1087
1088 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1089 {
1090         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1091
1092         LASSERT(atomic_read(&sec->ps_refcount) == 0);
1093         LASSERT(atomic_read(&sec->ps_nctx) == 0);
1094         LASSERT(policy->sp_cops->destroy_sec);
1095
1096         CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1097
1098         policy->sp_cops->destroy_sec(sec);
1099         sptlrpc_policy_put(policy);
1100 }
1101
1102 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1103 {
1104         sec_cop_destroy_sec(sec);
1105 }
1106 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1107
1108 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1109 {
1110         LASSERT(atomic_read(&sec->ps_refcount) > 0);
1111
1112         if (sec->ps_policy->sp_cops->kill_sec) {
1113                 sec->ps_policy->sp_cops->kill_sec(sec);
1114
1115                 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1116         }
1117 }
1118
1119 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1120 {
1121         if (sec) {
1122                 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1123                 atomic_inc(&sec->ps_refcount);
1124         }
1125
1126         return sec;
1127 }
1128 EXPORT_SYMBOL(sptlrpc_sec_get);
1129
1130 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1131 {
1132         if (sec) {
1133                 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1134
1135                 if (atomic_dec_and_test(&sec->ps_refcount)) {
1136                         LASSERT(atomic_read(&sec->ps_nctx) == 0);
1137
1138                         sptlrpc_gc_del_sec(sec);
1139                         sec_cop_destroy_sec(sec);
1140                 }
1141         }
1142 }
1143 EXPORT_SYMBOL(sptlrpc_sec_put);
1144
1145 /*
1146  * it's policy module responsible for taking refrence of import
1147  */
1148 static
1149 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1150                                        struct ptlrpc_svc_ctx *svc_ctx,
1151                                        struct sptlrpc_flavor *sf,
1152                                        enum lustre_sec_part sp)
1153 {
1154         struct ptlrpc_sec_policy *policy;
1155         struct ptlrpc_sec        *sec;
1156         ENTRY;
1157
1158         if (svc_ctx) {
1159                 LASSERT(imp->imp_dlm_fake == 1);
1160
1161                 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1162                        imp->imp_obd->obd_type->typ_name,
1163                        imp->imp_obd->obd_name,
1164                        sptlrpc_rpcflavor2name(sf->sf_rpc));
1165
1166                 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1167                 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1168         } else {
1169                 LASSERT(imp->imp_dlm_fake == 0);
1170
1171                 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1172                        imp->imp_obd->obd_type->typ_name,
1173                        imp->imp_obd->obd_name,
1174                        sptlrpc_rpcflavor2name(sf->sf_rpc));
1175
1176                 policy = sptlrpc_rpcflavor2policy(sf->sf_rpc);
1177                 if (!policy) {
1178                         CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1179                         RETURN(NULL);
1180                 }
1181         }
1182
1183         sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1184         if (sec) {
1185                 atomic_inc(&sec->ps_refcount);
1186
1187                 sec->ps_part = sp;
1188
1189                 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1190                         sptlrpc_gc_add_sec(sec);
1191         } else {
1192                 sptlrpc_policy_put(policy);
1193         }
1194
1195         RETURN(sec);
1196 }
1197
1198 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1199 {
1200         struct ptlrpc_sec *sec;
1201
1202         spin_lock(&imp->imp_lock);
1203         sec = sptlrpc_sec_get(imp->imp_sec);
1204         spin_unlock(&imp->imp_lock);
1205
1206         return sec;
1207 }
1208 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1209
1210 static void sptlrpc_import_sec_install(struct obd_import *imp,
1211                                        struct ptlrpc_sec *sec)
1212 {
1213         struct ptlrpc_sec *old_sec;
1214
1215         LASSERT(atomic_read(&sec->ps_refcount) > 0);
1216
1217         spin_lock(&imp->imp_lock);
1218         old_sec = imp->imp_sec;
1219         imp->imp_sec = sec;
1220         spin_unlock(&imp->imp_lock);
1221
1222         if (old_sec) {
1223                 sptlrpc_sec_kill(old_sec);
1224
1225                 /* balance the ref taken by this import */
1226                 sptlrpc_sec_put(old_sec);
1227         }
1228 }
1229
1230 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1231                                              struct ptlrpc_sec *sec,
1232                                              struct sptlrpc_flavor *sf)
1233 {
1234         if (sf->sf_bulk_ciph != sec->ps_flvr.sf_bulk_ciph ||
1235             sf->sf_bulk_hash != sec->ps_flvr.sf_bulk_hash) {
1236                 CWARN("imp %p (%s->%s): changing bulk flavor %s/%s -> %s/%s\n",
1237                       imp, imp->imp_obd->obd_name,
1238                       obd_uuid2str(&imp->imp_connection->c_remote_uuid),
1239                       sptlrpc_get_ciph_name(sec->ps_flvr.sf_bulk_ciph),
1240                       sptlrpc_get_hash_name(sec->ps_flvr.sf_bulk_hash),
1241                       sptlrpc_get_ciph_name(sf->sf_bulk_ciph),
1242                       sptlrpc_get_hash_name(sf->sf_bulk_hash));
1243
1244                 spin_lock(&sec->ps_lock);
1245                 sec->ps_flvr.sf_bulk_ciph = sf->sf_bulk_ciph;
1246                 sec->ps_flvr.sf_bulk_hash = sf->sf_bulk_hash;
1247                 spin_unlock(&sec->ps_lock);
1248         }
1249
1250         if (!equi(sf->sf_flags & PTLRPC_SEC_FL_UDESC,
1251                   sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC)) {
1252                 CWARN("imp %p (%s->%s): %s shipping user descriptor\n",
1253                       imp, imp->imp_obd->obd_name,
1254                       obd_uuid2str(&imp->imp_connection->c_remote_uuid),
1255                       (sf->sf_flags & PTLRPC_SEC_FL_UDESC) ? "start" : "stop");
1256
1257                 spin_lock(&sec->ps_lock);
1258                 sec->ps_flvr.sf_flags &= ~PTLRPC_SEC_FL_UDESC;
1259                 sec->ps_flvr.sf_flags |= sf->sf_flags & PTLRPC_SEC_FL_UDESC;
1260                 spin_unlock(&sec->ps_lock);
1261         }
1262 }
1263
1264 /*
1265  * for normal import, @svc_ctx should be NULL and @rpc_flavor is ignored;
1266  * for reverse import, @svc_ctx and @rpc_flavor is from incoming request.
1267  */
1268 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1269                              struct ptlrpc_svc_ctx *svc_ctx,
1270                              __u16 rpc_flavor)
1271 {
1272         struct ptlrpc_connection   *conn;
1273         struct sptlrpc_flavor       sf;
1274         struct ptlrpc_sec          *sec, *newsec;
1275         enum lustre_sec_part        sp;
1276         int                         rc;
1277
1278         if (imp == NULL)
1279                 return 0;
1280
1281         conn = imp->imp_connection;
1282
1283         if (svc_ctx == NULL) {
1284                 /* normal import, determine flavor from rule set */
1285                 sptlrpc_rule_set_choose(&imp->imp_obd->u.cli.cl_sptlrpc_rset,
1286                                         LUSTRE_SP_ANY, conn->c_self, &sf);
1287
1288                 sp = imp->imp_obd->u.cli.cl_sec_part;
1289         } else {
1290                 /* reverse import, determine flavor from incoming reqeust */
1291                 sf.sf_rpc = rpc_flavor;
1292                 sf.sf_bulk_ciph = BULK_CIPH_ALG_NULL;
1293                 sf.sf_bulk_hash = BULK_HASH_ALG_NULL;
1294                 sf.sf_flags = PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1295
1296                 sp = sptlrpc_target_sec_part(imp->imp_obd);
1297         }
1298
1299         sec = sptlrpc_import_sec_ref(imp);
1300         if (sec) {
1301                 if (svc_ctx == NULL) {
1302                         /* normal import, only check rpc flavor, if just bulk
1303                          * flavor or flags changed, we can handle it on the fly
1304                          * without switching sec. */
1305                         if (sf.sf_rpc == sec->ps_flvr.sf_rpc) {
1306                                 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1307
1308                                 rc = 0;
1309                                 goto out;
1310                         }
1311                 } else {
1312                         /* reverse import, do not compare bulk flavor */
1313                         if (sf.sf_rpc == sec->ps_flvr.sf_rpc) {
1314                                 rc = 0;
1315                                 goto out;
1316                         }
1317                 }
1318
1319                 CWARN("%simport %p (%s%s%s): changing flavor "
1320                       "(%s, %s/%s) -> (%s, %s/%s)\n",
1321                       svc_ctx ? "reverse " : "",
1322                       imp, imp->imp_obd->obd_name,
1323                       svc_ctx == NULL ? "->" : "<-",
1324                       obd_uuid2str(&conn->c_remote_uuid),
1325                       sptlrpc_rpcflavor2name(sec->ps_flvr.sf_rpc),
1326                       sptlrpc_get_hash_name(sec->ps_flvr.sf_bulk_hash),
1327                       sptlrpc_get_ciph_name(sec->ps_flvr.sf_bulk_ciph),
1328                       sptlrpc_rpcflavor2name(sf.sf_rpc),
1329                       sptlrpc_get_hash_name(sf.sf_bulk_hash),
1330                       sptlrpc_get_ciph_name(sf.sf_bulk_ciph));
1331         } else {
1332                 CWARN("%simport %p (%s%s%s) netid %x: "
1333                       "select initial flavor (%s, %s/%s)\n",
1334                       svc_ctx == NULL ? "" : "reverse ",
1335                       imp, imp->imp_obd->obd_name,
1336                       svc_ctx == NULL ? "->" : "<-",
1337                       obd_uuid2str(&conn->c_remote_uuid),
1338                       LNET_NIDNET(conn->c_self),
1339                       sptlrpc_rpcflavor2name(sf.sf_rpc),
1340                       sptlrpc_get_hash_name(sf.sf_bulk_hash),
1341                       sptlrpc_get_ciph_name(sf.sf_bulk_ciph));
1342         }
1343
1344         mutex_down(&imp->imp_sec_mutex);
1345
1346         newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1347         if (newsec) {
1348                 sptlrpc_import_sec_install(imp, newsec);
1349                 rc = 0;
1350         } else {
1351                 CERROR("%simport %p (%s): failed to create new sec\n",
1352                        svc_ctx == NULL ? "" : "reverse ",
1353                        imp, obd_uuid2str(&conn->c_remote_uuid));
1354                 rc = -EPERM;
1355         }
1356
1357         mutex_up(&imp->imp_sec_mutex);
1358
1359 out:
1360         sptlrpc_sec_put(sec);
1361         return 0;
1362 }
1363
1364 void sptlrpc_import_sec_put(struct obd_import *imp)
1365 {
1366         if (imp->imp_sec) {
1367                 sptlrpc_sec_kill(imp->imp_sec);
1368
1369                 sptlrpc_sec_put(imp->imp_sec);
1370                 imp->imp_sec = NULL;
1371         }
1372 }
1373
1374 static void import_flush_ctx_common(struct obd_import *imp,
1375                                     uid_t uid, int grace, int force)
1376 {
1377         struct ptlrpc_sec *sec;
1378
1379         if (imp == NULL)
1380                 return;
1381
1382         sec = sptlrpc_import_sec_ref(imp);
1383         if (sec == NULL)
1384                 return;
1385
1386         sec_cop_flush_ctx_cache(sec, uid, grace, force);
1387         sptlrpc_sec_put(sec);
1388 }
1389
1390 void sptlrpc_import_inval_all_ctx(struct obd_import *imp)
1391 {
1392         /* use grace == 0 */
1393         import_flush_ctx_common(imp, -1, 0, 1);
1394 }
1395
1396 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1397 {
1398         /* it's important to use grace mode, see explain in
1399          * sptlrpc_req_refresh_ctx() */
1400         import_flush_ctx_common(imp, 0, 1, 1);
1401 }
1402
1403 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1404 {
1405         import_flush_ctx_common(imp, cfs_current()->uid, 1, 1);
1406 }
1407 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1408
1409 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1410 {
1411         import_flush_ctx_common(imp, -1, 1, 1);
1412 }
1413 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1414
1415 /*
1416  * when complete successfully, req->rq_reqmsg should point to the
1417  * right place.
1418  */
1419 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1420 {
1421         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1422         struct ptlrpc_sec_policy *policy;
1423         int rc;
1424
1425         LASSERT(ctx);
1426         LASSERT(atomic_read(&ctx->cc_refcount));
1427         LASSERT(ctx->cc_sec);
1428         LASSERT(ctx->cc_sec->ps_policy);
1429         LASSERT(req->rq_reqmsg == NULL);
1430
1431         policy = ctx->cc_sec->ps_policy;
1432         rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1433         if (!rc) {
1434                 LASSERT(req->rq_reqmsg);
1435                 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1436
1437                 /* zeroing preallocated buffer */
1438                 if (req->rq_pool)
1439                         memset(req->rq_reqmsg, 0, msgsize);
1440         }
1441
1442         return rc;
1443 }
1444
1445 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1446 {
1447         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1448         struct ptlrpc_sec_policy *policy;
1449
1450         LASSERT(ctx);
1451         LASSERT(atomic_read(&ctx->cc_refcount));
1452         LASSERT(ctx->cc_sec);
1453         LASSERT(ctx->cc_sec->ps_policy);
1454
1455         if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1456                 return;
1457
1458         policy = ctx->cc_sec->ps_policy;
1459         policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1460 }
1461
1462 /*
1463  * NOTE caller must guarantee the buffer size is enough for the enlargement
1464  */
1465 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1466                                   int segment, int newsize)
1467 {
1468         void   *src, *dst;
1469         int     oldsize, oldmsg_size, movesize;
1470
1471         LASSERT(segment < msg->lm_bufcount);
1472         LASSERT(msg->lm_buflens[segment] <= newsize);
1473
1474         if (msg->lm_buflens[segment] == newsize)
1475                 return;
1476
1477         /* nothing to do if we are enlarging the last segment */
1478         if (segment == msg->lm_bufcount - 1) {
1479                 msg->lm_buflens[segment] = newsize;
1480                 return;
1481         }
1482
1483         oldsize = msg->lm_buflens[segment];
1484
1485         src = lustre_msg_buf(msg, segment + 1, 0);
1486         msg->lm_buflens[segment] = newsize;
1487         dst = lustre_msg_buf(msg, segment + 1, 0);
1488         msg->lm_buflens[segment] = oldsize;
1489
1490         /* move from segment + 1 to end segment */
1491         LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1492         oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1493         movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1494         LASSERT(movesize >= 0);
1495
1496         if (movesize)
1497                 memmove(dst, src, movesize);
1498
1499         /* note we don't clear the ares where old data live, not secret */
1500
1501         /* finally set new segment size */
1502         msg->lm_buflens[segment] = newsize;
1503 }
1504 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1505
1506 /*
1507  * enlarge @segment of upper message req->rq_reqmsg to @newsize, all data
1508  * will be preserved after enlargement. this must be called after rq_reqmsg has
1509  * been intialized at least.
1510  *
1511  * caller's attention: upon return, rq_reqmsg and rq_reqlen might have
1512  * been changed.
1513  */
1514 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1515                                int segment, int newsize)
1516 {
1517         struct ptlrpc_cli_ctx    *ctx = req->rq_cli_ctx;
1518         struct ptlrpc_sec_cops   *cops;
1519         struct lustre_msg        *msg = req->rq_reqmsg;
1520
1521         LASSERT(ctx);
1522         LASSERT(msg);
1523         LASSERT(msg->lm_bufcount > segment);
1524         LASSERT(msg->lm_buflens[segment] <= newsize);
1525
1526         if (msg->lm_buflens[segment] == newsize)
1527                 return 0;
1528
1529         cops = ctx->cc_sec->ps_policy->sp_cops;
1530         LASSERT(cops->enlarge_reqbuf);
1531         return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1532 }
1533 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1534
1535 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1536 {
1537         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1538         struct ptlrpc_sec_policy *policy;
1539         ENTRY;
1540
1541         LASSERT(ctx);
1542         LASSERT(atomic_read(&ctx->cc_refcount));
1543         LASSERT(ctx->cc_sec);
1544         LASSERT(ctx->cc_sec->ps_policy);
1545
1546         if (req->rq_repbuf)
1547                 RETURN(0);
1548
1549         policy = ctx->cc_sec->ps_policy;
1550         RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1551 }
1552
1553 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1554 {
1555         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1556         struct ptlrpc_sec_policy *policy;
1557         ENTRY;
1558
1559         LASSERT(ctx);
1560         LASSERT(atomic_read(&ctx->cc_refcount));
1561         LASSERT(ctx->cc_sec);
1562         LASSERT(ctx->cc_sec->ps_policy);
1563
1564         if (req->rq_repbuf == NULL)
1565                 return;
1566         LASSERT(req->rq_repbuf_len);
1567
1568         policy = ctx->cc_sec->ps_policy;
1569         policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1570         EXIT;
1571 }
1572
1573 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1574                                 struct ptlrpc_cli_ctx *ctx)
1575 {
1576         struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1577
1578         if (!policy->sp_cops->install_rctx)
1579                 return 0;
1580         return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1581 }
1582
1583 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1584                                 struct ptlrpc_svc_ctx *ctx)
1585 {
1586         struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1587
1588         if (!policy->sp_sops->install_rctx)
1589                 return 0;
1590         return policy->sp_sops->install_rctx(imp, ctx);
1591 }
1592
1593 /****************************************
1594  * server side security                 *
1595  ****************************************/
1596
1597 static int flavor_allowed(struct sptlrpc_flavor *exp,
1598                           struct ptlrpc_request *req)
1599 {
1600         struct sptlrpc_flavor *flvr = &req->rq_flvr;
1601
1602         if (exp->sf_rpc == flvr->sf_rpc)
1603                 return 1;
1604
1605         if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1606             RPC_FLVR_POLICY(exp->sf_rpc) == RPC_FLVR_POLICY(flvr->sf_rpc) &&
1607             RPC_FLVR_MECH(exp->sf_rpc) == RPC_FLVR_MECH(flvr->sf_rpc))
1608                 return 1;
1609
1610         return 0;
1611 }
1612
1613 #define EXP_FLVR_UPDATE_EXPIRE      (OBD_TIMEOUT_DEFAULT + 10)
1614
1615 int sptlrpc_target_export_check(struct obd_export *exp,
1616                                 struct ptlrpc_request *req)
1617 {
1618         struct sptlrpc_flavor   flavor;
1619
1620         if (exp == NULL)
1621                 return 0;
1622
1623         /* client side export has no imp_reverse, skip
1624          * FIXME maybe we should check flavor this as well??? */
1625         if (exp->exp_imp_reverse == NULL)
1626                 return 0;
1627
1628         /* don't care about ctx fini rpc */
1629         if (req->rq_ctx_fini)
1630                 return 0;
1631
1632         spin_lock(&exp->exp_lock);
1633
1634         /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1635          * the first req with the new flavor, then treat it as current flavor,
1636          * adapt reverse sec according to it.
1637          * note the first rpc with new flavor might not be with root ctx, in
1638          * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1639         if (unlikely(exp->exp_flvr_changed) &&
1640             flavor_allowed(&exp->exp_flvr_old[1], req)) {
1641                 /* make the new flavor as "current", and old ones as
1642                  * about-to-expire */
1643                 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1644                        exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1645                 flavor = exp->exp_flvr_old[1];
1646                 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1647                 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1648                 exp->exp_flvr_old[0] = exp->exp_flvr;
1649                 exp->exp_flvr_expire[0] = cfs_time_current_sec() +
1650                                           EXP_FLVR_UPDATE_EXPIRE;
1651                 exp->exp_flvr = flavor;
1652
1653                 /* flavor change finished */
1654                 exp->exp_flvr_changed = 0;
1655                 LASSERT(exp->exp_flvr_adapt == 1);
1656
1657                 /* if it's gss, we only interested in root ctx init */
1658                 if (req->rq_auth_gss &&
1659                     !(req->rq_ctx_init && (req->rq_auth_usr_root ||
1660                                            req->rq_auth_usr_mdt))) {
1661                         spin_unlock(&exp->exp_lock);
1662                         CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d)\n",
1663                                req->rq_auth_gss, req->rq_ctx_init,
1664                                req->rq_auth_usr_root, req->rq_auth_usr_mdt);
1665                         return 0;
1666                 }
1667
1668                 exp->exp_flvr_adapt = 0;
1669                 spin_unlock(&exp->exp_lock);
1670
1671                 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1672                                                 req->rq_svc_ctx, flavor.sf_rpc);
1673         }
1674
1675         /* if it equals to the current flavor, we accept it, but need to
1676          * dealing with reverse sec/ctx */
1677         if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1678                 /* most cases should return here, we only interested in
1679                  * gss root ctx init */
1680                 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1681                     (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt)) {
1682                         spin_unlock(&exp->exp_lock);
1683                         return 0;
1684                 }
1685
1686                 /* if flavor just changed, we should not proceed, just leave
1687                  * it and current flavor will be discovered and replaced
1688                  * shortly, and let _this_ rpc pass through */
1689                 if (exp->exp_flvr_changed) {
1690                         LASSERT(exp->exp_flvr_adapt);
1691                         spin_unlock(&exp->exp_lock);
1692                         return 0;
1693                 }
1694
1695                 if (exp->exp_flvr_adapt) {
1696                         exp->exp_flvr_adapt = 0;
1697                         CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1698                                exp, exp->exp_flvr.sf_rpc,
1699                                exp->exp_flvr_old[0].sf_rpc,
1700                                exp->exp_flvr_old[1].sf_rpc);
1701                         flavor = exp->exp_flvr;
1702                         spin_unlock(&exp->exp_lock);
1703
1704                         return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1705                                                         req->rq_svc_ctx,
1706                                                         flavor.sf_rpc);
1707                 } else {
1708                         CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1709                                "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1710                                exp->exp_flvr_old[0].sf_rpc,
1711                                exp->exp_flvr_old[1].sf_rpc);
1712                         spin_unlock(&exp->exp_lock);
1713
1714                         return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1715                                                            req->rq_svc_ctx);
1716                 }
1717         }
1718
1719         if (exp->exp_flvr_expire[0]) {
1720                 if (exp->exp_flvr_expire[0] >= cfs_time_current_sec()) {
1721                         if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1722                                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1723                                        "middle one ("CFS_DURATION_T")\n", exp,
1724                                        exp->exp_flvr.sf_rpc,
1725                                        exp->exp_flvr_old[0].sf_rpc,
1726                                        exp->exp_flvr_old[1].sf_rpc,
1727                                        exp->exp_flvr_expire[0] -
1728                                                 cfs_time_current_sec());
1729                                 spin_unlock(&exp->exp_lock);
1730                                 return 0;
1731                         }
1732                 } else {
1733                         CDEBUG(D_SEC, "mark middle expired\n");
1734                         exp->exp_flvr_expire[0] = 0;
1735                 }
1736                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1737                        exp->exp_flvr.sf_rpc,
1738                        exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1739                        req->rq_flvr.sf_rpc);
1740         }
1741
1742         /* now it doesn't match the current flavor, the only chance we can
1743          * accept it is match the old flavors which is not expired. */
1744         if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1745                 if (exp->exp_flvr_expire[1] >= cfs_time_current_sec()) {
1746                         if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1747                                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1748                                        "oldest one ("CFS_DURATION_T")\n", exp,
1749                                        exp->exp_flvr.sf_rpc,
1750                                        exp->exp_flvr_old[0].sf_rpc,
1751                                        exp->exp_flvr_old[1].sf_rpc,
1752                                        exp->exp_flvr_expire[1] -
1753                                                 cfs_time_current_sec());
1754                                 spin_unlock(&exp->exp_lock);
1755                                 return 0;
1756                         }
1757                 } else {
1758                         CDEBUG(D_SEC, "mark oldest expired\n");
1759                         exp->exp_flvr_expire[1] = 0;
1760                 }
1761                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1762                        exp, exp->exp_flvr.sf_rpc,
1763                        exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1764                        req->rq_flvr.sf_rpc);
1765         } else {
1766                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1767                        exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1768                        exp->exp_flvr_old[1].sf_rpc);
1769         }
1770
1771         spin_unlock(&exp->exp_lock);
1772
1773         CWARN("req %p: (%u|%u|%u|%u|%u) with unauthorized flavor %x\n",
1774               req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1775               req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_flvr.sf_rpc);
1776         return -EACCES;
1777 }
1778
1779 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1780                                       struct sptlrpc_rule_set *rset)
1781 {
1782         struct obd_export       *exp;
1783         struct sptlrpc_flavor    new_flvr;
1784
1785         LASSERT(obd);
1786
1787         spin_lock(&obd->obd_dev_lock);
1788
1789         list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1790                 if (exp->exp_connection == NULL)
1791                         continue;
1792
1793                 /* note if this export had just been updated flavor
1794                  * (exp_flvr_changed == 1), this will override the
1795                  * previous one. */
1796                 spin_lock(&exp->exp_lock);
1797                 sptlrpc_rule_set_choose(rset, exp->exp_sp_peer,
1798                                         exp->exp_connection->c_peer.nid,
1799                                         &new_flvr);
1800                 if (exp->exp_flvr_changed ||
1801                     memcmp(&new_flvr, &exp->exp_flvr, sizeof(new_flvr))) {
1802                         exp->exp_flvr_old[1] = new_flvr;
1803                         exp->exp_flvr_expire[1] = 0;
1804                         exp->exp_flvr_changed = 1;
1805                         exp->exp_flvr_adapt = 1;
1806                         CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1807                                exp, sptlrpc_part2name(exp->exp_sp_peer),
1808                                exp->exp_flvr.sf_rpc,
1809                                exp->exp_flvr_old[1].sf_rpc);
1810                 }
1811                 spin_unlock(&exp->exp_lock);
1812         }
1813
1814         spin_unlock(&obd->obd_dev_lock);
1815 }
1816 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1817
1818 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1819 {
1820         if (svc_rc == SECSVC_DROP)
1821                 return SECSVC_DROP;
1822
1823         switch (req->rq_sp_from) {
1824         case LUSTRE_SP_CLI:
1825         case LUSTRE_SP_MDT:
1826         case LUSTRE_SP_OST:
1827         case LUSTRE_SP_MGS:
1828         case LUSTRE_SP_ANY:
1829                 break;
1830         default:
1831                 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
1832                 return SECSVC_DROP;
1833         }
1834
1835         if (!req->rq_auth_gss)
1836                 return svc_rc;
1837
1838         if (unlikely(req->rq_sp_from == LUSTRE_SP_ANY)) {
1839                 CERROR("not specific part\n");
1840                 return SECSVC_DROP;
1841         }
1842
1843         /* from MDT, must be authenticated as MDT */
1844         if (unlikely(req->rq_sp_from == LUSTRE_SP_MDT &&
1845                      !req->rq_auth_usr_mdt)) {
1846                 DEBUG_REQ(D_ERROR, req, "fake source MDT");
1847                 return SECSVC_DROP;
1848         }
1849
1850         /* from OST, must be callback to MDT and CLI, the reverse sec
1851          * was from mdt/root keytab, so it should be MDT or root FIXME */
1852         if (unlikely(req->rq_sp_from == LUSTRE_SP_OST &&
1853                      !req->rq_auth_usr_mdt && !req->rq_auth_usr_root)) {
1854                 DEBUG_REQ(D_ERROR, req, "fake source OST");
1855                 return SECSVC_DROP;
1856         }
1857
1858         return svc_rc;
1859 }
1860
1861 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1862 {
1863         struct ptlrpc_sec_policy *policy;
1864         struct lustre_msg *msg = req->rq_reqbuf;
1865         int rc;
1866         ENTRY;
1867
1868         LASSERT(msg);
1869         LASSERT(req->rq_reqmsg == NULL);
1870         LASSERT(req->rq_repmsg == NULL);
1871
1872         req->rq_sp_from = LUSTRE_SP_ANY;
1873         req->rq_auth_uid = INVALID_UID;
1874         req->rq_auth_mapped_uid = INVALID_UID;
1875
1876         if (req->rq_reqdata_len < sizeof(struct lustre_msg)) {
1877                 CERROR("request size %d too small\n", req->rq_reqdata_len);
1878                 RETURN(SECSVC_DROP);
1879         }
1880
1881         /*
1882          * v2 message.
1883          */
1884         if (msg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1885                 req->rq_flvr.sf_rpc = WIRE_FLVR_RPC(msg->lm_secflvr);
1886         else
1887                 req->rq_flvr.sf_rpc = WIRE_FLVR_RPC(__swab32(msg->lm_secflvr));
1888
1889         /* unpack the wrapper message if the policy is not null */
1890         if ((RPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) &&
1891              lustre_unpack_msg(msg, req->rq_reqdata_len))
1892                 RETURN(SECSVC_DROP);
1893
1894         policy = sptlrpc_rpcflavor2policy(req->rq_flvr.sf_rpc);
1895         if (!policy) {
1896                 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
1897                 RETURN(SECSVC_DROP);
1898         }
1899
1900         LASSERT(policy->sp_sops->accept);
1901         rc = policy->sp_sops->accept(req);
1902
1903         LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1904         sptlrpc_policy_put(policy);
1905
1906         /* sanity check for the request source */
1907         rc = sptlrpc_svc_check_from(req, rc);
1908
1909         /* FIXME move to proper place */
1910         if (rc == SECSVC_OK) {
1911                 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1912
1913                 if (opc == OST_WRITE)
1914                         req->rq_bulk_write = 1;
1915                 else if (opc == OST_READ)
1916                         req->rq_bulk_read = 1;
1917         }
1918
1919         LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
1920         RETURN(rc);
1921 }
1922
1923 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req,
1924                          int msglen)
1925 {
1926         struct ptlrpc_sec_policy *policy;
1927         struct ptlrpc_reply_state *rs;
1928         int rc;
1929         ENTRY;
1930
1931         LASSERT(req->rq_svc_ctx);
1932         LASSERT(req->rq_svc_ctx->sc_policy);
1933
1934         policy = req->rq_svc_ctx->sc_policy;
1935         LASSERT(policy->sp_sops->alloc_rs);
1936
1937         rc = policy->sp_sops->alloc_rs(req, msglen);
1938         if (unlikely(rc == -ENOMEM)) {
1939                 /* failed alloc, try emergency pool */
1940                 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service);
1941                 if (rs == NULL)
1942                         RETURN(-ENOMEM);
1943
1944                 req->rq_reply_state = rs;
1945                 rc = policy->sp_sops->alloc_rs(req, msglen);
1946                 if (rc) {
1947                         lustre_put_emerg_rs(rs);
1948                         req->rq_reply_state = NULL;
1949                 }
1950         }
1951
1952         LASSERT(rc != 0 ||
1953                 (req->rq_reply_state && req->rq_reply_state->rs_msg));
1954
1955         RETURN(rc);
1956 }
1957
1958 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
1959 {
1960         struct ptlrpc_sec_policy *policy;
1961         int rc;
1962         ENTRY;
1963
1964         LASSERT(req->rq_svc_ctx);
1965         LASSERT(req->rq_svc_ctx->sc_policy);
1966
1967         policy = req->rq_svc_ctx->sc_policy;
1968         LASSERT(policy->sp_sops->authorize);
1969
1970         rc = policy->sp_sops->authorize(req);
1971         LASSERT(rc || req->rq_reply_state->rs_repdata_len);
1972
1973         RETURN(rc);
1974 }
1975
1976 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
1977 {
1978         struct ptlrpc_sec_policy *policy;
1979         unsigned int prealloc;
1980         ENTRY;
1981
1982         LASSERT(rs->rs_svc_ctx);
1983         LASSERT(rs->rs_svc_ctx->sc_policy);
1984
1985         policy = rs->rs_svc_ctx->sc_policy;
1986         LASSERT(policy->sp_sops->free_rs);
1987
1988         prealloc = rs->rs_prealloc;
1989         policy->sp_sops->free_rs(rs);
1990
1991         if (prealloc)
1992                 lustre_put_emerg_rs(rs);
1993         EXIT;
1994 }
1995
1996 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
1997 {
1998         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1999
2000         if (ctx == NULL)
2001                 return;
2002
2003         LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2004         atomic_inc(&ctx->sc_refcount);
2005 }
2006
2007 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2008 {
2009         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2010
2011         if (ctx == NULL)
2012                 return;
2013
2014         LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2015         if (atomic_dec_and_test(&ctx->sc_refcount)) {
2016                 if (ctx->sc_policy->sp_sops->free_ctx)
2017                         ctx->sc_policy->sp_sops->free_ctx(ctx);
2018         }
2019         req->rq_svc_ctx = NULL;
2020 }
2021
2022 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2023 {
2024         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2025
2026         if (ctx == NULL)
2027                 return;
2028
2029         LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2030         if (ctx->sc_policy->sp_sops->invalidate_ctx)
2031                 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2032 }
2033 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2034
2035 /****************************************
2036  * bulk security                        *
2037  ****************************************/
2038
2039 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2040                           struct ptlrpc_bulk_desc *desc)
2041 {
2042         struct ptlrpc_cli_ctx *ctx;
2043
2044         if (!req->rq_pack_bulk)
2045                 return 0;
2046
2047         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2048
2049         ctx = req->rq_cli_ctx;
2050         if (ctx->cc_ops->wrap_bulk)
2051                 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2052         return 0;
2053 }
2054 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2055
2056 static
2057 void pga_to_bulk_desc(int nob, obd_count pg_count, struct brw_page **pga,
2058                       struct ptlrpc_bulk_desc *desc)
2059 {
2060         int i;
2061
2062         LASSERT(pga);
2063         LASSERT(*pga);
2064
2065         for (i = 0; i < pg_count && nob > 0; i++) {
2066 #ifdef __KERNEL__
2067                 desc->bd_iov[i].kiov_page = pga[i]->pg;
2068                 desc->bd_iov[i].kiov_len = pga[i]->count > nob ?
2069                                            nob : pga[i]->count;
2070                 desc->bd_iov[i].kiov_offset = pga[i]->off & ~CFS_PAGE_MASK;
2071 #else
2072                 /* FIXME currently liblustre doesn't support bulk encryption.
2073                  * if we do, check again following may not be right. */
2074                 LASSERTF(0, "Bulk encryption not implemented for liblustre\n");
2075                 desc->bd_iov[i].iov_base = pga[i]->pg->addr;
2076                 desc->bd_iov[i].iov_len = pga[i]->count > nob ?
2077                                            nob : pga[i]->count;
2078 #endif
2079
2080                 desc->bd_iov_count++;
2081                 nob -= pga[i]->count;
2082         }
2083 }
2084
2085 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2086                                  int nob, obd_count pg_count,
2087                                  struct brw_page **pga)
2088 {
2089         struct ptlrpc_bulk_desc *desc;
2090         struct ptlrpc_cli_ctx *ctx;
2091         int rc = 0;
2092
2093         if (!req->rq_pack_bulk)
2094                 return 0;
2095
2096         LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2097
2098         OBD_ALLOC(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
2099         if (desc == NULL) {
2100                 CERROR("out of memory, can't verify bulk read data\n");
2101                 return -ENOMEM;
2102         }
2103
2104         pga_to_bulk_desc(nob, pg_count, pga, desc);
2105
2106         ctx = req->rq_cli_ctx;
2107         if (ctx->cc_ops->unwrap_bulk)
2108                 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2109
2110         OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
2111
2112         return rc;
2113 }
2114 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2115
2116 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2117                                   struct ptlrpc_bulk_desc *desc)
2118 {
2119         struct ptlrpc_cli_ctx *ctx;
2120
2121         if (!req->rq_pack_bulk)
2122                 return 0;
2123
2124         LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2125
2126         ctx = req->rq_cli_ctx;
2127         if (ctx->cc_ops->unwrap_bulk)
2128                 return ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2129
2130         return 0;
2131 }
2132 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2133
2134 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2135                           struct ptlrpc_bulk_desc *desc)
2136 {
2137         struct ptlrpc_svc_ctx *ctx;
2138
2139         if (!req->rq_pack_bulk)
2140                 return 0;
2141
2142         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2143
2144         ctx = req->rq_svc_ctx;
2145         if (ctx->sc_policy->sp_sops->wrap_bulk)
2146                 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2147
2148         return 0;
2149 }
2150 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2151
2152 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2153                             struct ptlrpc_bulk_desc *desc)
2154 {
2155         struct ptlrpc_svc_ctx *ctx;
2156
2157         if (!req->rq_pack_bulk)
2158                 return 0;
2159
2160         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2161
2162         ctx = req->rq_svc_ctx;
2163         if (ctx->sc_policy->sp_sops->unwrap_bulk);
2164                 return ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2165
2166         return 0;
2167 }
2168 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2169
2170
2171 /****************************************
2172  * user descriptor helpers              *
2173  ****************************************/
2174
2175 int sptlrpc_current_user_desc_size(void)
2176 {
2177         int ngroups;
2178
2179 #ifdef __KERNEL__
2180         ngroups = current_ngroups;
2181
2182         if (ngroups > LUSTRE_MAX_GROUPS)
2183                 ngroups = LUSTRE_MAX_GROUPS;
2184 #else
2185         ngroups = 0;
2186 #endif
2187         return sptlrpc_user_desc_size(ngroups);
2188 }
2189 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2190
2191 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2192 {
2193         struct ptlrpc_user_desc *pud;
2194
2195         pud = lustre_msg_buf(msg, offset, 0);
2196
2197         pud->pud_uid = cfs_current()->uid;
2198         pud->pud_gid = cfs_current()->gid;
2199         pud->pud_fsuid = cfs_current()->fsuid;
2200         pud->pud_fsgid = cfs_current()->fsgid;
2201         pud->pud_cap = cfs_curproc_cap_pack();
2202         pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2203
2204 #ifdef __KERNEL__
2205         task_lock(current);
2206         if (pud->pud_ngroups > current_ngroups)
2207                 pud->pud_ngroups = current_ngroups;
2208         memcpy(pud->pud_groups, cfs_current()->group_info->blocks[0],
2209                pud->pud_ngroups * sizeof(__u32));
2210         task_unlock(current);
2211 #endif
2212
2213         return 0;
2214 }
2215 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2216
2217 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset)
2218 {
2219         struct ptlrpc_user_desc *pud;
2220         int                      i;
2221
2222         pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2223         if (!pud)
2224                 return -EINVAL;
2225
2226         if (lustre_msg_swabbed(msg)) {
2227                 __swab32s(&pud->pud_uid);
2228                 __swab32s(&pud->pud_gid);
2229                 __swab32s(&pud->pud_fsuid);
2230                 __swab32s(&pud->pud_fsgid);
2231                 __swab32s(&pud->pud_cap);
2232                 __swab32s(&pud->pud_ngroups);
2233         }
2234
2235         if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2236                 CERROR("%u groups is too large\n", pud->pud_ngroups);
2237                 return -EINVAL;
2238         }
2239
2240         if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2241             msg->lm_buflens[offset]) {
2242                 CERROR("%u groups are claimed but bufsize only %u\n",
2243                        pud->pud_ngroups, msg->lm_buflens[offset]);
2244                 return -EINVAL;
2245         }
2246
2247         if (lustre_msg_swabbed(msg)) {
2248                 for (i = 0; i < pud->pud_ngroups; i++)
2249                         __swab32s(&pud->pud_groups[i]);
2250         }
2251
2252         return 0;
2253 }
2254 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2255
2256 /****************************************
2257  * misc helpers                         *
2258  ****************************************/
2259
2260 const char * sec2target_str(struct ptlrpc_sec *sec)
2261 {
2262         if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2263                 return "*";
2264         if (sec_is_reverse(sec))
2265                 return "c";
2266         return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2267 }
2268 EXPORT_SYMBOL(sec2target_str);
2269
2270 /****************************************
2271  * crypto API helper/alloc blkciper     *
2272  ****************************************/
2273
2274 #ifdef __KERNEL__
2275 #ifndef HAVE_ASYNC_BLOCK_CIPHER
2276 struct ll_crypto_cipher *ll_crypto_alloc_blkcipher(const char * algname,
2277                                                    u32 type, u32 mask)
2278 {
2279         char        buf[CRYPTO_MAX_ALG_NAME + 1];
2280         const char *pan = algname;
2281         u32         flag = 0; 
2282
2283         if (strncmp("cbc(", algname, 4) == 0)
2284                 flag |= CRYPTO_TFM_MODE_CBC;
2285         else if (strncmp("ecb(", algname, 4) == 0)
2286                 flag |= CRYPTO_TFM_MODE_ECB;
2287         if (flag) {
2288                 char *vp = strnchr(algname, CRYPTO_MAX_ALG_NAME, ')');
2289                 if (vp) {
2290                         memcpy(buf, algname + 4, vp - algname - 4);
2291                         buf[vp - algname - 4] = '\0';
2292                         pan = buf;
2293                 } else {
2294                         flag = 0;
2295                 }
2296         }
2297         return crypto_alloc_tfm(pan, flag);
2298 }
2299 EXPORT_SYMBOL(ll_crypto_alloc_blkcipher);
2300 #endif
2301 #endif
2302
2303 /****************************************
2304  * initialize/finalize                  *
2305  ****************************************/
2306
2307 int __init sptlrpc_init(void)
2308 {
2309         int rc;
2310
2311         rwlock_init(&policy_lock);
2312
2313         rc = sptlrpc_gc_start_thread();
2314         if (rc)
2315                 goto out;
2316
2317         rc = sptlrpc_enc_pool_init();
2318         if (rc)
2319                 goto out_gc;
2320
2321         rc = sptlrpc_null_init();
2322         if (rc)
2323                 goto out_pool;
2324
2325         rc = sptlrpc_plain_init();
2326         if (rc)
2327                 goto out_null;
2328
2329         rc = sptlrpc_lproc_init();
2330         if (rc)
2331                 goto out_plain;
2332
2333         return 0;
2334
2335 out_plain:
2336         sptlrpc_plain_fini();
2337 out_null:
2338         sptlrpc_null_fini();
2339 out_pool:
2340         sptlrpc_enc_pool_fini();
2341 out_gc:
2342         sptlrpc_gc_stop_thread();
2343 out:
2344         return rc;
2345 }
2346
2347 void __exit sptlrpc_fini(void)
2348 {
2349         sptlrpc_lproc_fini();
2350         sptlrpc_plain_fini();
2351         sptlrpc_null_fini();
2352         sptlrpc_enc_pool_fini();
2353         sptlrpc_gc_stop_thread();
2354 }