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 kfree(rset->srs_rules);
246 sptlrpc_rule_set_init(rset);
249 EXPORT_SYMBOL(sptlrpc_rule_set_free);
252 * return 0 if the rule set could accommodate one more rule.
254 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
256 struct sptlrpc_rule *rules;
261 if (rset->srs_nrule < rset->srs_nslot)
264 nslot = rset->srs_nslot + 8;
266 /* better use realloc() if available */
267 rules = kcalloc(nslot, sizeof(*rset->srs_rules), GFP_NOFS);
271 if (rset->srs_nrule) {
272 LASSERT(rset->srs_nslot && rset->srs_rules);
273 memcpy(rules, rset->srs_rules,
274 rset->srs_nrule * sizeof(*rset->srs_rules));
276 kfree(rset->srs_rules);
279 rset->srs_rules = rules;
280 rset->srs_nslot = nslot;
283 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
285 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
287 return (rule->sr_from != LUSTRE_SP_ANY ||
288 rule->sr_to != LUSTRE_SP_ANY);
290 static inline int rule_spec_net(struct sptlrpc_rule *rule)
292 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
294 static inline int rule_match_dir(struct sptlrpc_rule *r1,
295 struct sptlrpc_rule *r2)
297 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
299 static inline int rule_match_net(struct sptlrpc_rule *r1,
300 struct sptlrpc_rule *r2)
302 return (r1->sr_netid == r2->sr_netid);
306 * merge @rule into @rset.
307 * the @rset slots might be expanded.
309 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
310 struct sptlrpc_rule *rule)
312 struct sptlrpc_rule *p = rset->srs_rules;
313 int spec_dir, spec_net;
314 int rc, n, match = 0;
318 spec_net = rule_spec_net(rule);
319 spec_dir = rule_spec_dir(rule);
321 for (n = 0; n < rset->srs_nrule; n++) {
322 p = &rset->srs_rules[n];
324 /* test network match, if failed:
325 * - spec rule: skip rules which is also spec rule match, until
326 * we hit a wild rule, which means no more chance
327 * - wild rule: skip until reach the one which is also wild
330 if (!rule_match_net(p, rule)) {
332 if (rule_spec_net(p))
341 /* test dir match, same logic as net matching */
342 if (!rule_match_dir(p, rule)) {
344 if (rule_spec_dir(p))
359 LASSERT(n >= 0 && n < rset->srs_nrule);
361 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
362 /* remove this rule */
363 if (n < rset->srs_nrule - 1)
364 memmove(&rset->srs_rules[n],
365 &rset->srs_rules[n + 1],
366 (rset->srs_nrule - n - 1) *
370 /* override the rule */
371 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
374 LASSERT(n >= 0 && n <= rset->srs_nrule);
376 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
377 rc = sptlrpc_rule_set_expand(rset);
381 if (n < rset->srs_nrule)
382 memmove(&rset->srs_rules[n + 1],
384 (rset->srs_nrule - n) * sizeof(*rule));
385 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
388 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
394 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
397 * given from/to/nid, determine a matching flavor in ruleset.
398 * return 1 if a match found, otherwise return 0.
400 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
401 enum lustre_sec_part from,
402 enum lustre_sec_part to,
404 struct sptlrpc_flavor *sf)
406 struct sptlrpc_rule *r;
409 for (n = 0; n < rset->srs_nrule; n++) {
410 r = &rset->srs_rules[n];
412 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
413 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
414 LNET_NIDNET(nid) != r->sr_netid)
417 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
421 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
431 EXPORT_SYMBOL(sptlrpc_rule_set_choose);
433 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
435 struct sptlrpc_rule *r;
438 for (n = 0; n < rset->srs_nrule; n++) {
439 r = &rset->srs_rules[n];
440 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
441 r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
444 EXPORT_SYMBOL(sptlrpc_rule_set_dump);
446 /**********************************
447 * sptlrpc configuration support *
448 **********************************/
450 struct sptlrpc_conf_tgt {
451 struct list_head sct_list;
452 char sct_name[MAX_OBD_NAME];
453 struct sptlrpc_rule_set sct_rset;
456 struct sptlrpc_conf {
457 struct list_head sc_list;
458 char sc_fsname[MTI_NAME_MAXLEN];
459 unsigned int sc_modified; /* modified during updating */
460 unsigned int sc_updated:1, /* updated copy from MGS */
461 sc_local:1; /* local copy from target */
462 struct sptlrpc_rule_set sc_rset; /* fs general rules */
463 struct list_head sc_tgts; /* target-specific rules */
466 static struct mutex sptlrpc_conf_lock;
467 static LIST_HEAD(sptlrpc_confs);
469 static inline int is_hex(char c)
471 return ((c >= '0' && c <= '9') ||
472 (c >= 'a' && c <= 'f'));
475 static void target2fsname(const char *tgt, char *fsname, int buflen)
480 ptr = strrchr(tgt, '-');
482 if ((strncmp(ptr, "-MDT", 4) != 0 &&
483 strncmp(ptr, "-OST", 4) != 0) ||
484 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
485 !is_hex(ptr[6]) || !is_hex(ptr[7]))
489 /* if we didn't find the pattern, treat the whole string as fsname */
495 len = min(len, buflen - 1);
496 memcpy(fsname, tgt, len);
500 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
502 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
504 sptlrpc_rule_set_free(&conf->sc_rset);
506 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
507 &conf->sc_tgts, sct_list) {
508 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
509 list_del(&conf_tgt->sct_list);
512 LASSERT(list_empty(&conf->sc_tgts));
514 conf->sc_updated = 0;
518 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
520 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
522 sptlrpc_conf_free_rsets(conf);
523 list_del(&conf->sc_list);
528 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
532 struct sptlrpc_conf_tgt *conf_tgt;
534 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
535 if (strcmp(conf_tgt->sct_name, name) == 0)
542 conf_tgt = kzalloc(sizeof(*conf_tgt), GFP_NOFS);
544 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
545 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
546 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
553 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
556 struct sptlrpc_conf *conf;
558 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
559 if (strcmp(conf->sc_fsname, fsname) == 0)
566 conf = kzalloc(sizeof(*conf), GFP_NOFS);
570 strcpy(conf->sc_fsname, fsname);
571 sptlrpc_rule_set_init(&conf->sc_rset);
572 INIT_LIST_HEAD(&conf->sc_tgts);
573 list_add(&conf->sc_list, &sptlrpc_confs);
575 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
580 * caller must hold conf_lock already.
582 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
584 struct sptlrpc_rule *rule)
586 struct sptlrpc_conf_tgt *conf_tgt;
587 struct sptlrpc_rule_set *rule_set;
589 /* fsname == target means general rules for the whole fs */
590 if (strcmp(conf->sc_fsname, target) == 0) {
591 rule_set = &conf->sc_rset;
593 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
595 rule_set = &conf_tgt->sct_rset;
597 CERROR("out of memory, can't merge rule!\n");
602 return sptlrpc_rule_set_merge(rule_set, rule);
606 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
607 * find one through the target name in the record inside conf_lock;
608 * otherwise means caller already hold conf_lock.
610 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
611 struct sptlrpc_conf *conf)
613 char *target, *param;
614 char fsname[MTI_NAME_MAXLEN];
615 struct sptlrpc_rule rule;
618 target = lustre_cfg_string(lcfg, 1);
619 if (target == NULL) {
620 CERROR("missing target name\n");
624 param = lustre_cfg_string(lcfg, 2);
626 CERROR("missing parameter\n");
630 CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
632 /* parse rule to make sure the format is correct */
633 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
634 CERROR("Invalid sptlrpc parameter: %s\n", param);
637 param += sizeof(PARAM_SRPC_FLVR) - 1;
639 rc = sptlrpc_parse_rule(param, &rule);
644 target2fsname(target, fsname, sizeof(fsname));
646 mutex_lock(&sptlrpc_conf_lock);
647 conf = sptlrpc_conf_get(fsname, 0);
649 CERROR("can't find conf\n");
652 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
654 mutex_unlock(&sptlrpc_conf_lock);
656 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
657 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
666 int sptlrpc_process_config(struct lustre_cfg *lcfg)
668 return __sptlrpc_process_config(lcfg, NULL);
670 EXPORT_SYMBOL(sptlrpc_process_config);
672 static int logname2fsname(const char *logname, char *buf, int buflen)
677 ptr = strrchr(logname, '-');
678 if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
679 CERROR("%s is not a sptlrpc config log\n", logname);
683 len = min((int) (ptr - logname), buflen - 1);
685 memcpy(buf, logname, len);
690 void sptlrpc_conf_log_update_begin(const char *logname)
692 struct sptlrpc_conf *conf;
695 if (logname2fsname(logname, fsname, sizeof(fsname)))
698 mutex_lock(&sptlrpc_conf_lock);
700 conf = sptlrpc_conf_get(fsname, 0);
702 if (conf->sc_local) {
703 LASSERT(conf->sc_updated == 0);
704 sptlrpc_conf_free_rsets(conf);
706 conf->sc_modified = 0;
709 mutex_unlock(&sptlrpc_conf_lock);
711 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
714 * mark a config log has been updated
716 void sptlrpc_conf_log_update_end(const char *logname)
718 struct sptlrpc_conf *conf;
721 if (logname2fsname(logname, fsname, sizeof(fsname)))
724 mutex_lock(&sptlrpc_conf_lock);
726 conf = sptlrpc_conf_get(fsname, 0);
729 * if original state is not updated, make sure the
730 * modified counter > 0 to enforce updating local copy.
732 if (conf->sc_updated == 0)
735 conf->sc_updated = 1;
738 mutex_unlock(&sptlrpc_conf_lock);
740 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
742 void sptlrpc_conf_log_start(const char *logname)
746 if (logname2fsname(logname, fsname, sizeof(fsname)))
749 mutex_lock(&sptlrpc_conf_lock);
750 sptlrpc_conf_get(fsname, 1);
751 mutex_unlock(&sptlrpc_conf_lock);
753 EXPORT_SYMBOL(sptlrpc_conf_log_start);
755 void sptlrpc_conf_log_stop(const char *logname)
757 struct sptlrpc_conf *conf;
760 if (logname2fsname(logname, fsname, sizeof(fsname)))
763 mutex_lock(&sptlrpc_conf_lock);
764 conf = sptlrpc_conf_get(fsname, 0);
766 sptlrpc_conf_free(conf);
767 mutex_unlock(&sptlrpc_conf_lock);
769 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
771 static inline void flavor_set_flags(struct sptlrpc_flavor *sf,
772 enum lustre_sec_part from,
773 enum lustre_sec_part to,
774 unsigned int fl_udesc)
777 * null flavor doesn't need to set any flavor, and in fact
778 * we'd better not do that because everybody share a single sec.
780 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
783 if (from == LUSTRE_SP_MDT) {
784 /* MDT->MDT; MDT->OST */
785 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
786 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
788 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
789 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
791 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
792 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
796 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
797 enum lustre_sec_part to,
798 struct obd_uuid *target,
800 struct sptlrpc_flavor *sf)
802 struct sptlrpc_conf *conf;
803 struct sptlrpc_conf_tgt *conf_tgt;
804 char name[MTI_NAME_MAXLEN];
807 target2fsname(target->uuid, name, sizeof(name));
809 mutex_lock(&sptlrpc_conf_lock);
811 conf = sptlrpc_conf_get(name, 0);
815 /* convert uuid name (supposed end with _UUID) to target name */
816 len = strlen(target->uuid);
818 memcpy(name, target->uuid, len - 5);
819 name[len - 5] = '\0';
821 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
823 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
829 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
831 mutex_unlock(&sptlrpc_conf_lock);
834 get_default_flavor(sf);
836 flavor_set_flags(sf, from, to, 1);
840 * called by target devices, determine the expected flavor from
841 * certain peer (from, nid).
843 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
844 enum lustre_sec_part from,
846 struct sptlrpc_flavor *sf)
848 if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
849 get_default_flavor(sf);
851 EXPORT_SYMBOL(sptlrpc_target_choose_flavor);
853 #define SEC_ADAPT_DELAY (10)
856 * called by client devices, notify the sptlrpc config has changed and
857 * do import_sec_adapt later.
859 void sptlrpc_conf_client_adapt(struct obd_device *obd)
861 struct obd_import *imp;
863 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
864 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0);
865 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
867 /* serialize with connect/disconnect import */
868 down_read(&obd->u.cli.cl_sem);
870 imp = obd->u.cli.cl_import;
872 spin_lock(&imp->imp_lock);
874 imp->imp_sec_expire = get_seconds() +
876 spin_unlock(&imp->imp_lock);
879 up_read(&obd->u.cli.cl_sem);
881 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
883 int sptlrpc_conf_init(void)
885 mutex_init(&sptlrpc_conf_lock);
889 void sptlrpc_conf_fini(void)
891 struct sptlrpc_conf *conf, *conf_next;
893 mutex_lock(&sptlrpc_conf_lock);
894 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
895 sptlrpc_conf_free(conf);
897 LASSERT(list_empty(&sptlrpc_confs));
898 mutex_unlock(&sptlrpc_conf_lock);