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