1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_SEC
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
31 #include <liblustre.h>
34 #include <libcfs/kp30.h>
35 #include <linux/obd.h>
36 #include <linux/obd_class.h>
37 #include <linux/obd_support.h>
38 #include <linux/lustre_net.h>
39 #include <linux/lustre_import.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/lustre_sec.h>
43 static spinlock_t sectypes_lock = SPIN_LOCK_UNLOCKED;
44 static struct ptlrpc_sec_type *sectypes[PTLRPCS_FLVR_MAJOR_MAX] = {
48 int ptlrpcs_register(struct ptlrpc_sec_type *type)
50 __u32 flavor = type->pst_flavor;
52 LASSERT(type->pst_name);
53 LASSERT(type->pst_ops);
55 if (flavor >= PTLRPCS_FLVR_MAJOR_MAX)
58 spin_lock(§ypes_lock);
59 if (sectypes[flavor]) {
60 spin_unlock(§ypes_lock);
63 sectypes[flavor] = type;
64 atomic_set(&type->pst_inst, 0);
65 spin_unlock(§ypes_lock);
67 CWARN("%s: registered\n", type->pst_name);
71 int ptlrpcs_unregister(struct ptlrpc_sec_type *type)
73 __u32 major = type->pst_flavor;
75 LASSERT(major < PTLRPCS_FLVR_MAJOR_MAX);
77 spin_lock(§ypes_lock);
78 if (!sectypes[major]) {
79 spin_unlock(§ypes_lock);
80 CERROR("%s: already unregistered?\n", type->pst_name);
84 LASSERT(sectypes[major] == type);
86 if (atomic_read(&type->pst_inst)) {
87 CERROR("%s: still have %d instances\n",
88 type->pst_name, atomic_read(&type->pst_inst));
89 spin_unlock(§ypes_lock);
93 sectypes[major] = NULL;
94 spin_unlock(§ypes_lock);
96 CDEBUG(D_SEC, "%s: unregistered\n", type->pst_name);
101 struct ptlrpc_sec_type * ptlrpcs_flavor2type(__u32 flavor)
103 struct ptlrpc_sec_type *type;
104 __u32 major = SEC_FLAVOR_MAJOR(flavor);
106 if (major >= PTLRPCS_FLVR_MAJOR_MAX)
109 spin_lock(§ypes_lock);
110 type = sectypes[major];
111 if (type && !try_module_get(type->pst_owner))
113 spin_unlock(§ypes_lock);
118 void ptlrpcs_type_put(struct ptlrpc_sec_type *type)
120 module_put(type->pst_owner);
123 __u32 ptlrpcs_name2flavor(const char *name)
125 if (!strcmp(name, "null"))
126 return PTLRPCS_FLVR_NULL;
127 if (!strcmp(name, "krb5"))
128 return PTLRPCS_FLVR_KRB5;
129 if (!strcmp(name, "krb5i"))
130 return PTLRPCS_FLVR_KRB5I;
131 if (!strcmp(name, "krb5p"))
132 return PTLRPCS_FLVR_KRB5P;
134 return PTLRPCS_FLVR_INVALID;
137 char *ptlrpcs_flavor2name(__u32 flavor)
140 case PTLRPCS_FLVR_NULL:
142 case PTLRPCS_FLVR_KRB5:
144 case PTLRPCS_FLVR_KRB5I:
146 case PTLRPCS_FLVR_KRB5P:
149 CERROR("invalid flavor 0x%x\n", flavor);
154 /***********************************************
155 * credential cache helpers *
156 ***********************************************/
158 void ptlrpcs_init_credcache(struct ptlrpc_sec *sec)
161 for (i = 0; i < PTLRPC_CREDCACHE_NR; i++)
162 INIT_LIST_HEAD(&sec->ps_credcache[i]);
164 /* ps_nextgc == 0 means never do gc */
166 sec->ps_nextgc = get_seconds() + (sec->ps_expire >> 1);
170 * return 1 means we should also destroy the sec structure.
173 static int ptlrpcs_cred_destroy(struct ptlrpc_cred *cred)
175 struct ptlrpc_sec *sec = cred->pc_sec;
178 LASSERT(cred->pc_sec);
179 LASSERT(atomic_read(&cred->pc_refcount) == 0);
180 LASSERT(list_empty(&cred->pc_hash));
182 cred->pc_ops->destroy(cred);
184 /* spinlock to protect against ptlrpcs_sec_put() */
185 LASSERT(atomic_read(&sec->ps_credcount));
186 spin_lock(&sec->ps_lock);
187 if (atomic_dec_and_test(&sec->ps_credcount) &&
188 !atomic_read(&sec->ps_refcount))
190 spin_unlock(&sec->ps_lock);
194 static void ptlrpcs_destroy_credlist(struct list_head *head)
196 struct ptlrpc_cred *cred;
198 while (!list_empty(head)) {
199 cred = list_entry(head->next, struct ptlrpc_cred, pc_hash);
200 list_del_init(&cred->pc_hash);
201 ptlrpcs_cred_destroy(cred);
206 int cred_check_dead(struct ptlrpc_cred *cred,
207 struct list_head *freelist, int removal)
209 /* here we do the exact thing as asked. but an alternative
210 * way is remove dead entries immediately without be asked
211 * remove, since dead entry will not lead to further rpcs.
213 if (unlikely(ptlrpcs_cred_is_dead(cred))) {
214 /* don't try to destroy a busy entry */
215 if (atomic_read(&cred->pc_refcount))
220 /* a busy non-dead entry is considered as "good" one.
221 * Note in a very busy client where cred always busy, we
222 * will not be able to find the expire here, but some other
223 * part will, e.g. checking during refresh, or got error
224 * notification from server, etc. We don't touch busy cred
225 * here is because a busy cred's flag might be changed at
226 * anytime by the owner, we don't want to compete with them.
228 if (atomic_read(&cred->pc_refcount) != 0)
231 /* expire is 0 means never expire. a newly created gss cred
232 * which during upcall also has 0 expiration
234 if (cred->pc_expire == 0)
237 /* check real expiration */
238 if (time_after(cred->pc_expire, get_seconds()))
241 /* although we'v checked the bit right above, there's still
242 * possibility that somebody else set the bit elsewhere.
244 ptlrpcs_cred_expire(cred);
248 LASSERT(atomic_read(&cred->pc_refcount) >= 0);
249 LASSERT(cred->pc_sec);
250 LASSERT(spin_is_locked(&cred->pc_sec->ps_lock));
253 list_move(&cred->pc_hash, freelist);
259 void ptlrpcs_credcache_gc(struct ptlrpc_sec *sec,
260 struct list_head *freelist)
262 struct ptlrpc_cred *cred, *n;
266 CDEBUG(D_SEC, "do gc on sec %s\n", sec->ps_type->pst_name);
267 for (i = 0; i < PTLRPC_CREDCACHE_NR; i++) {
268 list_for_each_entry_safe(cred, n, &sec->ps_credcache[i],
270 cred_check_dead(cred, freelist, 1);
272 sec->ps_nextgc = get_seconds() + sec->ps_expire;
277 * @uid: which user. "-1" means flush all.
278 * @grace: mark cred DEAD, allow graceful destroy like notify
280 * @force: flush all entries, otherwise only free ones be flushed.
283 int flush_credcache(struct ptlrpc_sec *sec, uid_t uid,
284 int grace, int force)
286 struct ptlrpc_cred *cred, *n;
291 might_sleep_if(grace);
293 spin_lock(&sec->ps_lock);
294 for (i = 0; i < PTLRPC_CREDCACHE_NR; i++) {
295 list_for_each_entry_safe(cred, n, &sec->ps_credcache[i],
297 LASSERT(atomic_read(&cred->pc_refcount) >= 0);
299 if (uid != -1 && uid != cred->pc_uid)
301 if (atomic_read(&cred->pc_refcount)) {
305 list_del_init(&cred->pc_hash);
306 CDEBUG(D_SEC, "sec %p: flush busy(%d) cred %p "
308 atomic_read(&cred->pc_refcount), cred);
310 list_move(&cred->pc_hash, &freelist);
312 set_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags);
314 clear_bit(PTLRPC_CRED_UPTODATE_BIT,
318 spin_unlock(&sec->ps_lock);
320 ptlrpcs_destroy_credlist(&freelist);
324 /**************************************************
326 **************************************************/
329 int ptlrpcs_cred_get_hash(__u64 pag)
331 LASSERT((pag & PTLRPC_CREDCACHE_MASK) < PTLRPC_CREDCACHE_NR);
332 return (pag & PTLRPC_CREDCACHE_MASK);
336 * return an uptodate or newly created cred entry.
339 struct ptlrpc_cred * cred_cache_lookup(struct ptlrpc_sec *sec,
340 struct vfs_cred *vcred,
341 int create, int remove_dead)
343 struct ptlrpc_cred *cred, *new = NULL, *n;
350 hash = ptlrpcs_cred_get_hash(vcred->vc_pag);
353 spin_lock(&sec->ps_lock);
355 /* do gc if expired */
357 sec->ps_nextgc && time_after(get_seconds(), sec->ps_nextgc))
358 ptlrpcs_credcache_gc(sec, &freelist);
360 list_for_each_entry_safe(cred, n, &sec->ps_credcache[hash], pc_hash) {
361 if (cred_check_dead(cred, &freelist, remove_dead))
363 if (cred->pc_ops->match(cred, vcred)) {
370 if (new && new != cred) {
371 /* lost the race, just free it */
372 list_add(&new->pc_hash, &freelist);
374 list_move(&cred->pc_hash, &sec->ps_credcache[hash]);
377 list_add(&new->pc_hash, &sec->ps_credcache[hash]);
380 spin_unlock(&sec->ps_lock);
381 new = sec->ps_type->pst_ops->create_cred(sec, vcred);
383 atomic_inc(&sec->ps_credcount);
392 atomic_inc(&cred->pc_refcount);
394 spin_unlock(&sec->ps_lock);
396 ptlrpcs_destroy_credlist(&freelist);
400 struct ptlrpc_cred * ptlrpcs_cred_lookup(struct ptlrpc_sec *sec,
401 struct vfs_cred *vcred)
403 struct ptlrpc_cred *cred;
406 cred = cred_cache_lookup(sec, vcred, 0, 1);
410 static struct ptlrpc_cred *get_cred(struct ptlrpc_sec *sec)
412 struct vfs_cred vcred;
416 * for now we simply let PAG == real uid
418 if (sec->ps_flags & (PTLRPC_SEC_FL_MDS | PTLRPC_SEC_FL_REVERSE)) {
422 vcred.vc_pag = (__u64) current->uid;
423 vcred.vc_uid = current->uid;
426 return cred_cache_lookup(sec, &vcred, 1, 1);
429 int ptlrpcs_req_get_cred(struct ptlrpc_request *req)
431 struct obd_import *imp = req->rq_import;
434 LASSERT(!req->rq_cred);
437 req->rq_cred = get_cred(imp->imp_sec);
440 CERROR("req %p: fail to get cred from cache\n", req);
448 * check whether current user have valid credential for an import or not.
449 * might repeatedly try in case of non-fatal errors.
450 * return 0 on success, 1 on failure
452 int ptlrpcs_check_cred(struct obd_import *imp)
454 struct ptlrpc_cred *cred;
459 cred = get_cred(imp->imp_sec);
463 if (ptlrpcs_cred_is_uptodate(cred)) {
464 /* get_cred() has done expire checking, so we don't
465 * expect it could expire so quickly, and actually
468 ptlrpcs_cred_put(cred, 1);
472 ptlrpcs_cred_refresh(cred);
473 if (ptlrpcs_cred_is_uptodate(cred)) {
474 ptlrpcs_cred_put(cred, 1);
478 if (cred->pc_flags & PTLRPC_CRED_ERROR ||
479 !imp->imp_replayable) {
480 ptlrpcs_cred_put(cred, 1);
484 ptlrpcs_cred_put(cred, 1);
486 if (signal_pending(current)) {
487 CWARN("%s: interrupted\n", current->comm);
493 static void ptlrpcs_sec_destroy(struct ptlrpc_sec *sec);
495 void ptlrpcs_cred_put(struct ptlrpc_cred *cred, int sync)
497 struct ptlrpc_sec *sec = cred->pc_sec;
500 LASSERT(atomic_read(&cred->pc_refcount));
502 spin_lock(&sec->ps_lock);
504 /* this has to be protected by ps_lock, because cred cache
505 * management code might increase ref against a 0-refed cred.
507 if (!atomic_dec_and_test(&cred->pc_refcount)) {
508 spin_unlock(&sec->ps_lock);
512 /* if sec already unused, we have to destroy the cred (prevent it
513 * hanging there for ever)
515 if (atomic_read(&sec->ps_refcount) == 0) {
516 if (!test_and_set_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags))
517 CWARN("cred %p: force expire on a unused sec\n", cred);
518 list_del_init(&cred->pc_hash);
519 } else if (unlikely(sync && ptlrpcs_cred_is_dead(cred)))
520 list_del_init(&cred->pc_hash);
522 if (!list_empty(&cred->pc_hash)) {
523 spin_unlock(&sec->ps_lock);
527 /* if required async, and we reached here, we have to clear
528 * the UPTODATE bit, thus no rpc is needed in destroy procedure.
531 clear_bit(PTLRPC_CRED_UPTODATE_BIT, &cred->pc_flags);
533 spin_unlock(&sec->ps_lock);
535 /* destroy this cred */
536 if (!ptlrpcs_cred_destroy(cred))
539 LASSERT(!atomic_read(&sec->ps_credcount));
540 LASSERT(!atomic_read(&sec->ps_refcount));
542 CWARN("sec %p(%s), put last cred, also destroy the sec\n",
543 sec, sec->ps_type->pst_name);
546 void ptlrpcs_req_drop_cred(struct ptlrpc_request *req)
551 LASSERT(req->rq_cred);
554 /* this could be called with spinlock hold, use async mode */
555 ptlrpcs_cred_put(req->rq_cred, 0);
558 CDEBUG(D_SEC, "req %p have no cred\n", req);
563 * request must have a cred. if failed to get new cred,
564 * just restore the old one
566 int ptlrpcs_req_replace_dead_cred(struct ptlrpc_request *req)
568 struct ptlrpc_cred *cred = req->rq_cred;
573 LASSERT(test_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags));
575 ptlrpcs_cred_get(cred);
576 ptlrpcs_req_drop_cred(req);
577 LASSERT(!req->rq_cred);
578 rc = ptlrpcs_req_get_cred(req);
580 LASSERT(req->rq_cred);
581 LASSERT(req->rq_cred != cred);
582 ptlrpcs_cred_put(cred, 1);
584 LASSERT(!req->rq_cred);
591 * since there's no lock on the cred, its status could be changed
592 * by other threads at any time, we allow this race. If an uptodate
593 * cred turn to dead quickly under us, we don't know and continue
594 * using it, that's fine. if necessary the later error handling code
597 int ptlrpcs_req_refresh_cred(struct ptlrpc_request *req)
599 struct ptlrpc_cred *cred = req->rq_cred;
604 if (!ptlrpcs_cred_check_uptodate(cred))
607 if (test_bit(PTLRPC_CRED_ERROR_BIT, &cred->pc_flags)) {
608 req->rq_ptlrpcs_err = 1;
612 if (test_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags)) {
613 if (ptlrpcs_req_replace_dead_cred(req) == 0) {
614 LASSERT(cred != req->rq_cred);
615 CDEBUG(D_SEC, "req %p: replace cred %p => %p\n",
616 req, cred, req->rq_cred);
619 LASSERT(cred == req->rq_cred);
620 CERROR("req %p: failed to replace dead cred %p\n",
622 req->rq_ptlrpcs_err = 1;
627 ptlrpcs_cred_refresh(cred);
629 if (!ptlrpcs_cred_is_uptodate(cred)) {
630 if (test_bit(PTLRPC_CRED_ERROR_BIT, &cred->pc_flags))
631 req->rq_ptlrpcs_err = 1;
633 CERROR("req %p: failed to refresh cred %p, fatal %d\n",
634 req, cred, req->rq_ptlrpcs_err);
640 int ptlrpcs_cli_wrap_request(struct ptlrpc_request *req)
642 struct ptlrpc_cred *cred;
646 LASSERT(req->rq_cred);
647 LASSERT(req->rq_cred->pc_sec);
648 LASSERT(req->rq_cred->pc_ops);
649 LASSERT(req->rq_reqbuf);
650 LASSERT(req->rq_reqbuf_len);
652 rc = ptlrpcs_req_refresh_cred(req);
656 CDEBUG(D_SEC, "wrap req %p\n", req);
659 switch (SEC_FLAVOR_SVC(req->rq_req_secflvr)) {
660 case PTLRPCS_SVC_NONE:
661 case PTLRPCS_SVC_AUTH:
662 if (req->rq_req_wrapped) {
663 CDEBUG(D_SEC, "req %p(o%u,x"LPU64",t"LPU64") "
664 "already signed, resend?\n", req,
665 req->rq_reqmsg ? req->rq_reqmsg->opc : -1,
666 req->rq_xid, req->rq_transno);
667 req->rq_req_wrapped = 0;
668 req->rq_reqdata_len = sizeof(struct ptlrpcs_wire_hdr) +
670 LASSERT(req->rq_reqdata_len % 8 == 0);
673 LASSERT(cred->pc_ops->sign);
674 rc = cred->pc_ops->sign(cred, req);
676 req->rq_req_wrapped = 1;
678 case PTLRPCS_SVC_PRIV:
679 if (req->rq_req_wrapped) {
680 CDEBUG(D_SEC, "req %p(o%u,x"LPU64",t"LPU64") "
681 "already encrypted, resend?\n", req,
682 req->rq_reqmsg ? req->rq_reqmsg->opc : -1,
683 req->rq_xid, req->rq_transno);
684 req->rq_req_wrapped = 0;
685 req->rq_reqdata_len = sizeof(struct ptlrpcs_wire_hdr);
686 LASSERT(req->rq_reqdata_len % 8 == 0);
689 LASSERT(cred->pc_ops->seal);
690 rc = cred->pc_ops->seal(cred, req);
692 req->rq_req_wrapped = 1;
697 LASSERT(req->rq_reqdata_len);
698 LASSERT(req->rq_reqdata_len % 8 == 0);
699 LASSERT(req->rq_reqdata_len >= sizeof(struct ptlrpcs_wire_hdr));
700 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
705 /* rq_nob_received is the actual received data length */
706 int ptlrpcs_cli_unwrap_reply(struct ptlrpc_request *req)
708 struct ptlrpc_cred *cred = req->rq_cred;
709 struct ptlrpc_sec *sec;
710 struct ptlrpcs_wire_hdr *sec_hdr;
715 LASSERT(cred->pc_sec);
716 LASSERT(cred->pc_ops);
717 LASSERT(req->rq_repbuf);
719 if (req->rq_nob_received < sizeof(*sec_hdr)) {
720 CERROR("req %p: reply size only %d\n",
721 req, req->rq_nob_received);
725 sec_hdr = (struct ptlrpcs_wire_hdr *) req->rq_repbuf;
726 sec_hdr->flavor = le32_to_cpu(sec_hdr->flavor);
727 sec_hdr->msg_len = le32_to_cpu(sec_hdr->msg_len);
728 sec_hdr->sec_len = le32_to_cpu(sec_hdr->sec_len);
730 CDEBUG(D_SEC, "req %p, cred %p, flavor 0x%x\n",
731 req, cred, sec_hdr->flavor);
735 /* only compare major flavor, reply might use different subflavor.
737 if (SEC_FLAVOR_MAJOR(sec_hdr->flavor) !=
738 SEC_FLAVOR_MAJOR(req->rq_req_secflvr)) {
739 CERROR("got major flavor %u while expect %u\n",
740 SEC_FLAVOR_MAJOR(sec_hdr->flavor),
741 SEC_FLAVOR_MAJOR(req->rq_req_secflvr));
745 if (sizeof(*sec_hdr) + sec_hdr->msg_len + sec_hdr->sec_len >
746 req->rq_nob_received) {
747 CERROR("msg %u, sec %u, while only get %d\n",
748 sec_hdr->msg_len, sec_hdr->sec_len,
749 req->rq_nob_received);
753 switch (SEC_FLAVOR_SVC(sec_hdr->flavor)) {
754 case PTLRPCS_SVC_NONE:
755 case PTLRPCS_SVC_AUTH: {
756 LASSERT(cred->pc_ops->verify);
757 rc = cred->pc_ops->verify(cred, req);
758 LASSERT(rc || req->rq_repmsg || req->rq_ptlrpcs_restart);
760 case PTLRPCS_SVC_PRIV:
761 LASSERT(cred->pc_ops->unseal);
762 rc = cred->pc_ops->unseal(cred, req);
763 LASSERT(rc || req->rq_repmsg || req->rq_ptlrpcs_restart);
773 /**************************************************
775 **************************************************/
777 struct ptlrpc_sec * ptlrpcs_sec_create(__u32 flavor,
779 struct obd_import *import,
780 const char *pipe_dir,
783 struct ptlrpc_sec_type *type;
784 struct ptlrpc_sec *sec;
787 type = ptlrpcs_flavor2type(flavor);
789 CERROR("invalid flavor 0x%x\n", flavor);
793 sec = type->pst_ops->create_sec(flavor, pipe_dir, pipe_data);
795 spin_lock_init(&sec->ps_lock);
796 ptlrpcs_init_credcache(sec);
798 sec->ps_flavor = flavor;
799 sec->ps_flags = flags;
800 sec->ps_import = class_import_get(import);
801 atomic_set(&sec->ps_refcount, 1);
802 atomic_set(&sec->ps_credcount, 0);
803 atomic_inc(&type->pst_inst);
805 ptlrpcs_type_put(type);
810 static void ptlrpcs_sec_destroy(struct ptlrpc_sec *sec)
812 struct ptlrpc_sec_type *type = sec->ps_type;
813 struct obd_import *imp = sec->ps_import;
815 LASSERT(type && type->pst_ops);
816 LASSERT(type->pst_ops->destroy_sec);
818 type->pst_ops->destroy_sec(sec);
819 atomic_dec(&type->pst_inst);
820 ptlrpcs_type_put(type);
821 class_import_put(imp);
824 void ptlrpcs_sec_put(struct ptlrpc_sec *sec)
828 if (atomic_dec_and_test(&sec->ps_refcount)) {
829 flush_credcache(sec, -1, 1, 1);
831 /* this spinlock is protect against ptlrpcs_cred_destroy() */
832 spin_lock(&sec->ps_lock);
833 ncred = atomic_read(&sec->ps_credcount);
834 spin_unlock(&sec->ps_lock);
837 ptlrpcs_sec_destroy(sec);
839 CWARN("%s %p is no usage while %d cred still "
840 "holded, destroy delayed\n",
841 sec->ps_type->pst_name, sec,
842 atomic_read(&sec->ps_credcount));
847 void ptlrpcs_sec_invalidate_cache(struct ptlrpc_sec *sec)
849 flush_credcache(sec, -1, 0, 1);
852 int sec_alloc_reqbuf(struct ptlrpc_sec *sec,
853 struct ptlrpc_request *req,
854 int msgsize, int secsize)
856 struct ptlrpcs_wire_hdr *hdr;
859 LASSERT(msgsize % 8 == 0);
860 LASSERT(secsize % 8 == 0);
862 req->rq_reqbuf_len = sizeof(*hdr) + msgsize + secsize;
863 OBD_ALLOC(req->rq_reqbuf, req->rq_reqbuf_len);
864 if (!req->rq_reqbuf) {
865 CERROR("can't alloc %d\n", req->rq_reqbuf_len);
869 hdr = buf_to_sec_hdr(req->rq_reqbuf);
870 hdr->flavor = cpu_to_le32(req->rq_req_secflvr);
871 hdr->msg_len = msgsize;
872 /* security length will be filled later */
874 /* later reqdata_len will be added on actual security payload */
875 req->rq_reqdata_len = sizeof(*hdr) + msgsize;
876 req->rq_reqmsg = buf_to_lustre_msg(req->rq_reqbuf);
878 CDEBUG(D_SEC, "req %p: rqbuf at %p, len %d, msg %d, sec %d\n",
879 req, req->rq_reqbuf, req->rq_reqbuf_len,
885 /* when complete successfully, req->rq_reqmsg should point to the
888 int ptlrpcs_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
890 struct ptlrpc_cred *cred = req->rq_cred;
891 struct ptlrpc_sec *sec;
892 struct ptlrpc_secops *ops;
894 LASSERT(msgsize % 8 == 0);
895 LASSERT(sizeof(struct ptlrpcs_wire_hdr) % 8 == 0);
897 LASSERT(atomic_read(&cred->pc_refcount));
898 LASSERT(cred->pc_sec);
899 LASSERT(cred->pc_sec->ps_type);
900 LASSERT(cred->pc_sec->ps_type->pst_ops);
901 LASSERT(req->rq_reqbuf == NULL);
902 LASSERT(req->rq_reqmsg == NULL);
905 ops = sec->ps_type->pst_ops;
906 if (ops->alloc_reqbuf)
907 return ops->alloc_reqbuf(sec, req, msgsize);
909 return sec_alloc_reqbuf(sec, req, msgsize, 0);
912 void sec_free_reqbuf(struct ptlrpc_sec *sec,
913 struct ptlrpc_request *req)
915 LASSERT(req->rq_reqbuf);
916 LASSERT(req->rq_reqbuf_len);
919 if (req->rq_reqmsg) {
920 LASSERT((char *) req->rq_reqmsg >= req->rq_reqbuf &&
921 (char *) req->rq_reqmsg < req->rq_reqbuf +
925 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
926 req->rq_reqbuf = NULL;
927 req->rq_reqmsg = NULL;
930 void ptlrpcs_cli_free_reqbuf(struct ptlrpc_request *req)
932 struct ptlrpc_cred *cred = req->rq_cred;
933 struct ptlrpc_sec *sec;
934 struct ptlrpc_secops *ops;
937 LASSERT(atomic_read(&cred->pc_refcount));
938 LASSERT(cred->pc_sec);
939 LASSERT(cred->pc_sec->ps_type);
940 LASSERT(cred->pc_sec->ps_type->pst_ops);
941 LASSERT(req->rq_reqbuf);
944 ops = sec->ps_type->pst_ops;
945 if (ops->free_reqbuf)
946 ops->free_reqbuf(sec, req);
948 sec_free_reqbuf(sec, req);
951 int ptlrpcs_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
953 struct ptlrpc_cred *cred = req->rq_cred;
954 struct ptlrpc_sec *sec;
955 struct ptlrpc_secops *ops;
956 int msg_payload, sec_payload;
959 LASSERT(msgsize % 8 == 0);
960 LASSERT(sizeof(struct ptlrpcs_wire_hdr) % 8 == 0);
962 LASSERT(atomic_read(&cred->pc_refcount));
963 LASSERT(cred->pc_sec);
964 LASSERT(cred->pc_sec->ps_type);
965 LASSERT(cred->pc_sec->ps_type->pst_ops);
966 LASSERT(req->rq_repbuf == NULL);
969 ops = sec->ps_type->pst_ops;
970 if (ops->alloc_repbuf)
971 RETURN(ops->alloc_repbuf(sec, req, msgsize));
973 /* default allocation scheme */
974 msg_payload = SEC_FLAVOR_SVC(req->rq_req_secflvr) == PTLRPCS_SVC_PRIV ?
976 sec_payload = size_round(ptlrpcs_est_rep_payload(req, msgsize));
978 req->rq_repbuf_len = sizeof(struct ptlrpcs_wire_hdr) +
979 msg_payload + sec_payload;
980 OBD_ALLOC(req->rq_repbuf, req->rq_repbuf_len);
984 CDEBUG(D_SEC, "req %p: repbuf at %p, len %d, msg %d, sec %d\n",
985 req, req->rq_repbuf, req->rq_repbuf_len,
986 msg_payload, sec_payload);
991 void ptlrpcs_cli_free_repbuf(struct ptlrpc_request *req)
993 struct ptlrpc_cred *cred = req->rq_cred;
994 struct ptlrpc_sec *sec;
995 struct ptlrpc_secops *ops;
999 LASSERT(atomic_read(&cred->pc_refcount));
1000 LASSERT(cred->pc_sec);
1001 LASSERT(cred->pc_sec->ps_type);
1002 LASSERT(cred->pc_sec->ps_type->pst_ops);
1003 LASSERT(req->rq_repbuf);
1006 ops = sec->ps_type->pst_ops;
1007 if (ops->free_repbuf)
1008 ops->free_repbuf(sec, req);
1010 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
1011 req->rq_repbuf = NULL;
1012 req->rq_repmsg = NULL;
1017 int ptlrpcs_import_get_sec(struct obd_import *imp)
1019 __u32 flavor = PTLRPCS_FLVR_NULL;
1020 unsigned long flags = 0;
1021 char *pipedir = NULL;
1024 LASSERT(imp->imp_obd);
1025 LASSERT(imp->imp_obd->obd_type);
1027 /* old sec might be still there in reconnecting */
1031 /* find actual flavor for client obd. right now server side
1032 * obd (reverse imp, etc) will simply use NULL.
1034 if (!strcmp(imp->imp_obd->obd_type->typ_name, "mdc") ||
1035 !strcmp(imp->imp_obd->obd_type->typ_name, "osc")) {
1036 struct client_obd *cli = &imp->imp_obd->u.cli;
1038 switch (SEC_FLAVOR_MAJOR(cli->cl_sec_flavor)) {
1039 case PTLRPCS_FLVR_MAJOR_NULL:
1040 CWARN("select security null for %s(%s)\n",
1041 imp->imp_obd->obd_type->typ_name,
1042 imp->imp_obd->obd_name);
1044 case PTLRPCS_FLVR_MAJOR_GSS:
1045 CWARN("select security %s for %s(%s)\n",
1046 ptlrpcs_flavor2name(cli->cl_sec_flavor),
1047 imp->imp_obd->obd_type->typ_name,
1048 imp->imp_obd->obd_name);
1049 flavor = cli->cl_sec_flavor;
1050 pipedir = imp->imp_obd->obd_name;
1053 CWARN("unknown security flavor for %s(%s), use null\n",
1054 imp->imp_obd->obd_type->typ_name,
1055 imp->imp_obd->obd_name);
1058 flags = cli->cl_sec_flags;
1061 imp->imp_sec = ptlrpcs_sec_create(flavor, flags, imp, pipedir, imp);
1068 void ptlrpcs_import_drop_sec(struct obd_import *imp)
1072 ptlrpcs_sec_put(imp->imp_sec);
1073 imp->imp_sec = NULL;
1078 void ptlrpcs_import_flush_creds(struct obd_import *imp, uid_t uid)
1082 class_import_get(imp);
1084 flush_credcache(imp->imp_sec, uid, 1, 1);
1085 class_import_put(imp);
1088 int __init ptlrpc_sec_init(void)
1092 if ((rc = ptlrpcs_null_init()))
1095 if ((rc = svcsec_null_init())) {
1096 ptlrpcs_null_exit();
1101 #if !defined __KERNEL__ && defined ENABLE_GSS
1108 static void __exit ptlrpc_sec_exit(void)
1111 ptlrpcs_null_exit();
1115 EXPORT_SYMBOL(ptlrpcs_register);
1116 EXPORT_SYMBOL(ptlrpcs_unregister);
1117 EXPORT_SYMBOL(ptlrpcs_sec_create);
1118 EXPORT_SYMBOL(ptlrpcs_sec_put);
1119 EXPORT_SYMBOL(ptlrpcs_sec_invalidate_cache);
1120 EXPORT_SYMBOL(ptlrpcs_import_get_sec);
1121 EXPORT_SYMBOL(ptlrpcs_import_drop_sec);
1122 EXPORT_SYMBOL(ptlrpcs_import_flush_creds);
1123 EXPORT_SYMBOL(ptlrpcs_cred_lookup);
1124 EXPORT_SYMBOL(ptlrpcs_cred_put);
1125 EXPORT_SYMBOL(ptlrpcs_req_get_cred);
1126 EXPORT_SYMBOL(ptlrpcs_req_drop_cred);
1127 EXPORT_SYMBOL(ptlrpcs_req_replace_dead_cred);
1128 EXPORT_SYMBOL(ptlrpcs_req_refresh_cred);
1129 EXPORT_SYMBOL(ptlrpcs_check_cred);
1130 EXPORT_SYMBOL(ptlrpcs_cli_alloc_reqbuf);
1131 EXPORT_SYMBOL(ptlrpcs_cli_free_reqbuf);
1132 EXPORT_SYMBOL(ptlrpcs_cli_alloc_repbuf);
1133 EXPORT_SYMBOL(ptlrpcs_cli_free_repbuf);
1134 EXPORT_SYMBOL(ptlrpcs_cli_wrap_request);
1135 EXPORT_SYMBOL(ptlrpcs_cli_unwrap_reply);
1136 EXPORT_SYMBOL(sec_alloc_reqbuf);
1137 EXPORT_SYMBOL(sec_free_reqbuf);
1139 EXPORT_SYMBOL(svcsec_register);
1140 EXPORT_SYMBOL(svcsec_unregister);
1141 EXPORT_SYMBOL(svcsec_accept);
1142 EXPORT_SYMBOL(svcsec_authorize);
1143 EXPORT_SYMBOL(svcsec_alloc_repbuf);
1144 EXPORT_SYMBOL(svcsec_cleanup_req);
1145 EXPORT_SYMBOL(svcsec_get);
1146 EXPORT_SYMBOL(svcsec_put);
1147 EXPORT_SYMBOL(svcsec_alloc_reply_state);
1148 EXPORT_SYMBOL(svcsec_free_reply_state);
1150 EXPORT_SYMBOL(ptlrpcs_name2flavor);
1151 EXPORT_SYMBOL(ptlrpcs_flavor2name);
1153 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1154 MODULE_DESCRIPTION("Lustre Security Support");
1155 MODULE_LICENSE("GPL");
1157 module_init(ptlrpc_sec_init);
1158 module_exit(ptlrpc_sec_exit);