4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_SEC
39 #include "../../include/linux/libcfs/libcfs.h"
40 #include <linux/crypto.h>
41 #include <linux/key.h>
43 #include "../include/obd.h"
44 #include "../include/obd_support.h"
45 #include "../include/lustre_import.h"
46 #include "../include/lustre_param.h"
47 #include "../include/lustre_sec.h"
49 #include "ptlrpc_internal.h"
51 const char *sptlrpc_part2name(enum lustre_sec_part part)
70 EXPORT_SYMBOL(sptlrpc_part2name);
72 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
74 const char *type = obd->obd_type->typ_name;
76 if (!strcmp(type, LUSTRE_MDT_NAME))
78 if (!strcmp(type, LUSTRE_OST_NAME))
80 if (!strcmp(type, LUSTRE_MGS_NAME))
83 CERROR("unknown target %p(%s)\n", obd, type);
86 EXPORT_SYMBOL(sptlrpc_target_sec_part);
88 /****************************************
89 * user supplied flavor string parsing *
90 ****************************************/
93 * format: <base_flavor>[-<bulk_type:alg_spec>]
95 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
100 memset(flvr, 0, sizeof(*flvr));
102 if (str == NULL || str[0] == '\0') {
103 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
107 strncpy(buf, str, sizeof(buf));
108 buf[sizeof(buf) - 1] = '\0';
110 bulk = strchr(buf, '-');
114 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
115 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
119 * currently only base flavor "plain" can have bulk specification.
121 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
122 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
125 * format: plain-hash:<hash_alg>
127 alg = strchr(bulk, ':');
132 if (strcmp(bulk, "hash"))
135 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
136 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
140 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
141 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
143 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
153 CERROR("invalid flavor string: %s\n", str);
156 EXPORT_SYMBOL(sptlrpc_parse_flavor);
158 /****************************************
160 ****************************************/
162 static void get_default_flavor(struct sptlrpc_flavor *sf)
164 memset(sf, 0, sizeof(*sf));
166 sf->sf_rpc = SPTLRPC_FLVR_NULL;
170 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
172 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
173 rule->sr_from = LUSTRE_SP_ANY;
174 rule->sr_to = LUSTRE_SP_ANY;
175 rule->sr_padding = 0;
177 get_default_flavor(&rule->sr_flvr);
181 * format: network[.direction]=flavor
183 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
188 sptlrpc_rule_init(rule);
190 flavor = strchr(param, '=');
191 if (flavor == NULL) {
192 CERROR("invalid param, no '='\n");
197 dir = strchr(param, '.');
202 if (strcmp(param, "default")) {
203 rule->sr_netid = libcfs_str2net(param);
204 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
205 CERROR("invalid network name: %s\n", param);
212 if (!strcmp(dir, "mdt2ost")) {
213 rule->sr_from = LUSTRE_SP_MDT;
214 rule->sr_to = LUSTRE_SP_OST;
215 } else if (!strcmp(dir, "mdt2mdt")) {
216 rule->sr_from = LUSTRE_SP_MDT;
217 rule->sr_to = LUSTRE_SP_MDT;
218 } else if (!strcmp(dir, "cli2ost")) {
219 rule->sr_from = LUSTRE_SP_CLI;
220 rule->sr_to = LUSTRE_SP_OST;
221 } else if (!strcmp(dir, "cli2mdt")) {
222 rule->sr_from = LUSTRE_SP_CLI;
223 rule->sr_to = LUSTRE_SP_MDT;
225 CERROR("invalid rule dir segment: %s\n", dir);
231 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
237 EXPORT_SYMBOL(sptlrpc_parse_rule);
239 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
241 LASSERT(rset->srs_nslot ||
242 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
244 if (rset->srs_nslot) {
245 OBD_FREE(rset->srs_rules,
246 rset->srs_nslot * sizeof(*rset->srs_rules));
247 sptlrpc_rule_set_init(rset);
250 EXPORT_SYMBOL(sptlrpc_rule_set_free);
253 * return 0 if the rule set could accommodate one more rule.
255 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
257 struct sptlrpc_rule *rules;
262 if (rset->srs_nrule < rset->srs_nslot)
265 nslot = rset->srs_nslot + 8;
267 /* better use realloc() if available */
268 OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
272 if (rset->srs_nrule) {
273 LASSERT(rset->srs_nslot && rset->srs_rules);
274 memcpy(rules, rset->srs_rules,
275 rset->srs_nrule * sizeof(*rset->srs_rules));
277 OBD_FREE(rset->srs_rules,
278 rset->srs_nslot * sizeof(*rset->srs_rules));
281 rset->srs_rules = rules;
282 rset->srs_nslot = nslot;
285 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
287 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
289 return (rule->sr_from != LUSTRE_SP_ANY ||
290 rule->sr_to != LUSTRE_SP_ANY);
292 static inline int rule_spec_net(struct sptlrpc_rule *rule)
294 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
296 static inline int rule_match_dir(struct sptlrpc_rule *r1,
297 struct sptlrpc_rule *r2)
299 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
301 static inline int rule_match_net(struct sptlrpc_rule *r1,
302 struct sptlrpc_rule *r2)
304 return (r1->sr_netid == r2->sr_netid);
308 * merge @rule into @rset.
309 * the @rset slots might be expanded.
311 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
312 struct sptlrpc_rule *rule)
314 struct sptlrpc_rule *p = rset->srs_rules;
315 int spec_dir, spec_net;
316 int rc, n, match = 0;
320 spec_net = rule_spec_net(rule);
321 spec_dir = rule_spec_dir(rule);
323 for (n = 0; n < rset->srs_nrule; n++) {
324 p = &rset->srs_rules[n];
326 /* test network match, if failed:
327 * - spec rule: skip rules which is also spec rule match, until
328 * we hit a wild rule, which means no more chance
329 * - wild rule: skip until reach the one which is also wild
332 if (!rule_match_net(p, rule)) {
334 if (rule_spec_net(p))
343 /* test dir match, same logic as net matching */
344 if (!rule_match_dir(p, rule)) {
346 if (rule_spec_dir(p))
361 LASSERT(n >= 0 && n < rset->srs_nrule);
363 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
364 /* remove this rule */
365 if (n < rset->srs_nrule - 1)
366 memmove(&rset->srs_rules[n],
367 &rset->srs_rules[n + 1],
368 (rset->srs_nrule - n - 1) *
372 /* override the rule */
373 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
376 LASSERT(n >= 0 && n <= rset->srs_nrule);
378 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
379 rc = sptlrpc_rule_set_expand(rset);
383 if (n < rset->srs_nrule)
384 memmove(&rset->srs_rules[n + 1],
386 (rset->srs_nrule - n) * sizeof(*rule));
387 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
390 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
396 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
399 * given from/to/nid, determine a matching flavor in ruleset.
400 * return 1 if a match found, otherwise return 0.
402 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
403 enum lustre_sec_part from,
404 enum lustre_sec_part to,
406 struct sptlrpc_flavor *sf)
408 struct sptlrpc_rule *r;
411 for (n = 0; n < rset->srs_nrule; n++) {
412 r = &rset->srs_rules[n];
414 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
415 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
416 LNET_NIDNET(nid) != r->sr_netid)
419 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
423 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
433 EXPORT_SYMBOL(sptlrpc_rule_set_choose);
435 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
437 struct sptlrpc_rule *r;
440 for (n = 0; n < rset->srs_nrule; n++) {
441 r = &rset->srs_rules[n];
442 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
443 r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
446 EXPORT_SYMBOL(sptlrpc_rule_set_dump);
448 /**********************************
449 * sptlrpc configuration support *
450 **********************************/
452 struct sptlrpc_conf_tgt {
453 struct list_head sct_list;
454 char sct_name[MAX_OBD_NAME];
455 struct sptlrpc_rule_set sct_rset;
458 struct sptlrpc_conf {
459 struct list_head sc_list;
460 char sc_fsname[MTI_NAME_MAXLEN];
461 unsigned int sc_modified; /* modified during updating */
462 unsigned int sc_updated:1, /* updated copy from MGS */
463 sc_local:1; /* local copy from target */
464 struct sptlrpc_rule_set sc_rset; /* fs general rules */
465 struct list_head sc_tgts; /* target-specific rules */
468 static struct mutex sptlrpc_conf_lock;
469 static LIST_HEAD(sptlrpc_confs);
471 static inline int is_hex(char c)
473 return ((c >= '0' && c <= '9') ||
474 (c >= 'a' && c <= 'f'));
477 static void target2fsname(const char *tgt, char *fsname, int buflen)
482 ptr = strrchr(tgt, '-');
484 if ((strncmp(ptr, "-MDT", 4) != 0 &&
485 strncmp(ptr, "-OST", 4) != 0) ||
486 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
487 !is_hex(ptr[6]) || !is_hex(ptr[7]))
491 /* if we didn't find the pattern, treat the whole string as fsname */
497 len = min(len, buflen - 1);
498 memcpy(fsname, tgt, len);
502 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
504 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
506 sptlrpc_rule_set_free(&conf->sc_rset);
508 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
509 &conf->sc_tgts, sct_list) {
510 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
511 list_del(&conf_tgt->sct_list);
512 OBD_FREE_PTR(conf_tgt);
514 LASSERT(list_empty(&conf->sc_tgts));
516 conf->sc_updated = 0;
520 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
522 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
524 sptlrpc_conf_free_rsets(conf);
525 list_del(&conf->sc_list);
530 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
534 struct sptlrpc_conf_tgt *conf_tgt;
536 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
537 if (strcmp(conf_tgt->sct_name, name) == 0)
544 OBD_ALLOC_PTR(conf_tgt);
546 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
547 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
548 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
555 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
558 struct sptlrpc_conf *conf;
560 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
561 if (strcmp(conf->sc_fsname, fsname) == 0)
572 strcpy(conf->sc_fsname, fsname);
573 sptlrpc_rule_set_init(&conf->sc_rset);
574 INIT_LIST_HEAD(&conf->sc_tgts);
575 list_add(&conf->sc_list, &sptlrpc_confs);
577 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
582 * caller must hold conf_lock already.
584 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
586 struct sptlrpc_rule *rule)
588 struct sptlrpc_conf_tgt *conf_tgt;
589 struct sptlrpc_rule_set *rule_set;
591 /* fsname == target means general rules for the whole fs */
592 if (strcmp(conf->sc_fsname, target) == 0) {
593 rule_set = &conf->sc_rset;
595 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
597 rule_set = &conf_tgt->sct_rset;
599 CERROR("out of memory, can't merge rule!\n");
604 return sptlrpc_rule_set_merge(rule_set, rule);
608 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
609 * find one through the target name in the record inside conf_lock;
610 * otherwise means caller already hold conf_lock.
612 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
613 struct sptlrpc_conf *conf)
615 char *target, *param;
616 char fsname[MTI_NAME_MAXLEN];
617 struct sptlrpc_rule rule;
620 target = lustre_cfg_string(lcfg, 1);
621 if (target == NULL) {
622 CERROR("missing target name\n");
626 param = lustre_cfg_string(lcfg, 2);
628 CERROR("missing parameter\n");
632 CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
634 /* parse rule to make sure the format is correct */
635 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
636 CERROR("Invalid sptlrpc parameter: %s\n", param);
639 param += sizeof(PARAM_SRPC_FLVR) - 1;
641 rc = sptlrpc_parse_rule(param, &rule);
646 target2fsname(target, fsname, sizeof(fsname));
648 mutex_lock(&sptlrpc_conf_lock);
649 conf = sptlrpc_conf_get(fsname, 0);
651 CERROR("can't find conf\n");
654 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
656 mutex_unlock(&sptlrpc_conf_lock);
658 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
659 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
668 int sptlrpc_process_config(struct lustre_cfg *lcfg)
670 return __sptlrpc_process_config(lcfg, NULL);
672 EXPORT_SYMBOL(sptlrpc_process_config);
674 static int logname2fsname(const char *logname, char *buf, int buflen)
679 ptr = strrchr(logname, '-');
680 if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
681 CERROR("%s is not a sptlrpc config log\n", logname);
685 len = min((int) (ptr - logname), buflen - 1);
687 memcpy(buf, logname, len);
692 void sptlrpc_conf_log_update_begin(const char *logname)
694 struct sptlrpc_conf *conf;
697 if (logname2fsname(logname, fsname, sizeof(fsname)))
700 mutex_lock(&sptlrpc_conf_lock);
702 conf = sptlrpc_conf_get(fsname, 0);
704 if (conf->sc_local) {
705 LASSERT(conf->sc_updated == 0);
706 sptlrpc_conf_free_rsets(conf);
708 conf->sc_modified = 0;
711 mutex_unlock(&sptlrpc_conf_lock);
713 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
716 * mark a config log has been updated
718 void sptlrpc_conf_log_update_end(const char *logname)
720 struct sptlrpc_conf *conf;
723 if (logname2fsname(logname, fsname, sizeof(fsname)))
726 mutex_lock(&sptlrpc_conf_lock);
728 conf = sptlrpc_conf_get(fsname, 0);
731 * if original state is not updated, make sure the
732 * modified counter > 0 to enforce updating local copy.
734 if (conf->sc_updated == 0)
737 conf->sc_updated = 1;
740 mutex_unlock(&sptlrpc_conf_lock);
742 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
744 void sptlrpc_conf_log_start(const char *logname)
748 if (logname2fsname(logname, fsname, sizeof(fsname)))
751 mutex_lock(&sptlrpc_conf_lock);
752 sptlrpc_conf_get(fsname, 1);
753 mutex_unlock(&sptlrpc_conf_lock);
755 EXPORT_SYMBOL(sptlrpc_conf_log_start);
757 void sptlrpc_conf_log_stop(const char *logname)
759 struct sptlrpc_conf *conf;
762 if (logname2fsname(logname, fsname, sizeof(fsname)))
765 mutex_lock(&sptlrpc_conf_lock);
766 conf = sptlrpc_conf_get(fsname, 0);
768 sptlrpc_conf_free(conf);
769 mutex_unlock(&sptlrpc_conf_lock);
771 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
773 static inline void flavor_set_flags(struct sptlrpc_flavor *sf,
774 enum lustre_sec_part from,
775 enum lustre_sec_part to,
776 unsigned int fl_udesc)
779 * null flavor doesn't need to set any flavor, and in fact
780 * we'd better not do that because everybody share a single sec.
782 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
785 if (from == LUSTRE_SP_MDT) {
786 /* MDT->MDT; MDT->OST */
787 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
788 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
790 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
791 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
793 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
794 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
798 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
799 enum lustre_sec_part to,
800 struct obd_uuid *target,
802 struct sptlrpc_flavor *sf)
804 struct sptlrpc_conf *conf;
805 struct sptlrpc_conf_tgt *conf_tgt;
806 char name[MTI_NAME_MAXLEN];
809 target2fsname(target->uuid, name, sizeof(name));
811 mutex_lock(&sptlrpc_conf_lock);
813 conf = sptlrpc_conf_get(name, 0);
817 /* convert uuid name (supposed end with _UUID) to target name */
818 len = strlen(target->uuid);
820 memcpy(name, target->uuid, len - 5);
821 name[len - 5] = '\0';
823 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
825 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
831 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
833 mutex_unlock(&sptlrpc_conf_lock);
836 get_default_flavor(sf);
838 flavor_set_flags(sf, from, to, 1);
842 * called by target devices, determine the expected flavor from
843 * certain peer (from, nid).
845 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
846 enum lustre_sec_part from,
848 struct sptlrpc_flavor *sf)
850 if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
851 get_default_flavor(sf);
853 EXPORT_SYMBOL(sptlrpc_target_choose_flavor);
855 #define SEC_ADAPT_DELAY (10)
858 * called by client devices, notify the sptlrpc config has changed and
859 * do import_sec_adapt later.
861 void sptlrpc_conf_client_adapt(struct obd_device *obd)
863 struct obd_import *imp;
865 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
866 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0);
867 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
869 /* serialize with connect/disconnect import */
870 down_read(&obd->u.cli.cl_sem);
872 imp = obd->u.cli.cl_import;
874 spin_lock(&imp->imp_lock);
876 imp->imp_sec_expire = get_seconds() +
878 spin_unlock(&imp->imp_lock);
881 up_read(&obd->u.cli.cl_sem);
883 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
885 int sptlrpc_conf_init(void)
887 mutex_init(&sptlrpc_conf_lock);
891 void sptlrpc_conf_fini(void)
893 struct sptlrpc_conf *conf, *conf_next;
895 mutex_lock(&sptlrpc_conf_lock);
896 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
897 sptlrpc_conf_free(conf);
899 LASSERT(list_empty(&sptlrpc_confs));
900 mutex_unlock(&sptlrpc_conf_lock);