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 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
53 const char *type = obd->obd_type->typ_name;
55 if (!strcmp(type, LUSTRE_MDT_NAME))
57 if (!strcmp(type, LUSTRE_OST_NAME))
59 if (!strcmp(type, LUSTRE_MGS_NAME))
62 CERROR("unknown target %p(%s)\n", obd, type);
65 EXPORT_SYMBOL(sptlrpc_target_sec_part);
67 /****************************************
68 * user supplied flavor string parsing *
69 ****************************************/
72 * format: <base_flavor>[-<bulk_type:alg_spec>]
74 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
79 memset(flvr, 0, sizeof(*flvr));
81 if (str == NULL || str[0] == '\0') {
82 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
86 strncpy(buf, str, sizeof(buf));
87 buf[sizeof(buf) - 1] = '\0';
89 bulk = strchr(buf, '-');
93 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
94 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
98 * currently only base flavor "plain" can have bulk specification.
100 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
101 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
104 * format: plain-hash:<hash_alg>
106 alg = strchr(bulk, ':');
111 if (strcmp(bulk, "hash"))
114 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
115 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
119 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
120 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
122 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
132 CERROR("invalid flavor string: %s\n", str);
135 EXPORT_SYMBOL(sptlrpc_parse_flavor);
137 /****************************************
139 ****************************************/
141 static void get_default_flavor(struct sptlrpc_flavor *sf)
143 memset(sf, 0, sizeof(*sf));
145 sf->sf_rpc = SPTLRPC_FLVR_NULL;
149 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
151 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
152 rule->sr_from = LUSTRE_SP_ANY;
153 rule->sr_to = LUSTRE_SP_ANY;
154 rule->sr_padding = 0;
156 get_default_flavor(&rule->sr_flvr);
160 * format: network[.direction]=flavor
162 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
167 sptlrpc_rule_init(rule);
169 flavor = strchr(param, '=');
170 if (flavor == NULL) {
171 CERROR("invalid param, no '='\n");
176 dir = strchr(param, '.');
181 if (strcmp(param, "default")) {
182 rule->sr_netid = libcfs_str2net(param);
183 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
184 CERROR("invalid network name: %s\n", param);
191 if (!strcmp(dir, "mdt2ost")) {
192 rule->sr_from = LUSTRE_SP_MDT;
193 rule->sr_to = LUSTRE_SP_OST;
194 } else if (!strcmp(dir, "mdt2mdt")) {
195 rule->sr_from = LUSTRE_SP_MDT;
196 rule->sr_to = LUSTRE_SP_MDT;
197 } else if (!strcmp(dir, "cli2ost")) {
198 rule->sr_from = LUSTRE_SP_CLI;
199 rule->sr_to = LUSTRE_SP_OST;
200 } else if (!strcmp(dir, "cli2mdt")) {
201 rule->sr_from = LUSTRE_SP_CLI;
202 rule->sr_to = LUSTRE_SP_MDT;
204 CERROR("invalid rule dir segment: %s\n", dir);
210 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
216 EXPORT_SYMBOL(sptlrpc_parse_rule);
218 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
220 LASSERT(rset->srs_nslot ||
221 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
223 if (rset->srs_nslot) {
224 kfree(rset->srs_rules);
225 sptlrpc_rule_set_init(rset);
228 EXPORT_SYMBOL(sptlrpc_rule_set_free);
231 * return 0 if the rule set could accommodate one more rule.
233 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
235 struct sptlrpc_rule *rules;
240 if (rset->srs_nrule < rset->srs_nslot)
243 nslot = rset->srs_nslot + 8;
245 /* better use realloc() if available */
246 rules = kcalloc(nslot, sizeof(*rset->srs_rules), GFP_NOFS);
250 if (rset->srs_nrule) {
251 LASSERT(rset->srs_nslot && rset->srs_rules);
252 memcpy(rules, rset->srs_rules,
253 rset->srs_nrule * sizeof(*rset->srs_rules));
255 kfree(rset->srs_rules);
258 rset->srs_rules = rules;
259 rset->srs_nslot = nslot;
262 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
264 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
266 return (rule->sr_from != LUSTRE_SP_ANY ||
267 rule->sr_to != LUSTRE_SP_ANY);
269 static inline int rule_spec_net(struct sptlrpc_rule *rule)
271 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
273 static inline int rule_match_dir(struct sptlrpc_rule *r1,
274 struct sptlrpc_rule *r2)
276 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
278 static inline int rule_match_net(struct sptlrpc_rule *r1,
279 struct sptlrpc_rule *r2)
281 return (r1->sr_netid == r2->sr_netid);
285 * merge @rule into @rset.
286 * the @rset slots might be expanded.
288 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
289 struct sptlrpc_rule *rule)
291 struct sptlrpc_rule *p = rset->srs_rules;
292 int spec_dir, spec_net;
293 int rc, n, match = 0;
297 spec_net = rule_spec_net(rule);
298 spec_dir = rule_spec_dir(rule);
300 for (n = 0; n < rset->srs_nrule; n++) {
301 p = &rset->srs_rules[n];
303 /* test network match, if failed:
304 * - spec rule: skip rules which is also spec rule match, until
305 * we hit a wild rule, which means no more chance
306 * - wild rule: skip until reach the one which is also wild
309 if (!rule_match_net(p, rule)) {
311 if (rule_spec_net(p))
320 /* test dir match, same logic as net matching */
321 if (!rule_match_dir(p, rule)) {
323 if (rule_spec_dir(p))
338 LASSERT(n >= 0 && n < rset->srs_nrule);
340 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
341 /* remove this rule */
342 if (n < rset->srs_nrule - 1)
343 memmove(&rset->srs_rules[n],
344 &rset->srs_rules[n + 1],
345 (rset->srs_nrule - n - 1) *
349 /* override the rule */
350 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
353 LASSERT(n >= 0 && n <= rset->srs_nrule);
355 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
356 rc = sptlrpc_rule_set_expand(rset);
360 if (n < rset->srs_nrule)
361 memmove(&rset->srs_rules[n + 1],
363 (rset->srs_nrule - n) * sizeof(*rule));
364 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
367 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
373 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
376 * given from/to/nid, determine a matching flavor in ruleset.
377 * return 1 if a match found, otherwise return 0.
379 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
380 enum lustre_sec_part from,
381 enum lustre_sec_part to,
383 struct sptlrpc_flavor *sf)
385 struct sptlrpc_rule *r;
388 for (n = 0; n < rset->srs_nrule; n++) {
389 r = &rset->srs_rules[n];
391 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
392 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
393 LNET_NIDNET(nid) != r->sr_netid)
396 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
400 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
410 EXPORT_SYMBOL(sptlrpc_rule_set_choose);
412 /**********************************
413 * sptlrpc configuration support *
414 **********************************/
416 struct sptlrpc_conf_tgt {
417 struct list_head sct_list;
418 char sct_name[MAX_OBD_NAME];
419 struct sptlrpc_rule_set sct_rset;
422 struct sptlrpc_conf {
423 struct list_head sc_list;
424 char sc_fsname[MTI_NAME_MAXLEN];
425 unsigned int sc_modified; /* modified during updating */
426 unsigned int sc_updated:1, /* updated copy from MGS */
427 sc_local:1; /* local copy from target */
428 struct sptlrpc_rule_set sc_rset; /* fs general rules */
429 struct list_head sc_tgts; /* target-specific rules */
432 static struct mutex sptlrpc_conf_lock;
433 static LIST_HEAD(sptlrpc_confs);
435 static inline int is_hex(char c)
437 return ((c >= '0' && c <= '9') ||
438 (c >= 'a' && c <= 'f'));
441 static void target2fsname(const char *tgt, char *fsname, int buflen)
446 ptr = strrchr(tgt, '-');
448 if ((strncmp(ptr, "-MDT", 4) != 0 &&
449 strncmp(ptr, "-OST", 4) != 0) ||
450 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
451 !is_hex(ptr[6]) || !is_hex(ptr[7]))
455 /* if we didn't find the pattern, treat the whole string as fsname */
461 len = min(len, buflen - 1);
462 memcpy(fsname, tgt, len);
466 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
468 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
470 sptlrpc_rule_set_free(&conf->sc_rset);
472 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
473 &conf->sc_tgts, sct_list) {
474 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
475 list_del(&conf_tgt->sct_list);
478 LASSERT(list_empty(&conf->sc_tgts));
480 conf->sc_updated = 0;
484 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
486 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
488 sptlrpc_conf_free_rsets(conf);
489 list_del(&conf->sc_list);
494 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
498 struct sptlrpc_conf_tgt *conf_tgt;
500 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
501 if (strcmp(conf_tgt->sct_name, name) == 0)
508 conf_tgt = kzalloc(sizeof(*conf_tgt), GFP_NOFS);
510 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
511 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
512 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
519 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
522 struct sptlrpc_conf *conf;
524 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
525 if (strcmp(conf->sc_fsname, fsname) == 0)
532 conf = kzalloc(sizeof(*conf), GFP_NOFS);
536 strcpy(conf->sc_fsname, fsname);
537 sptlrpc_rule_set_init(&conf->sc_rset);
538 INIT_LIST_HEAD(&conf->sc_tgts);
539 list_add(&conf->sc_list, &sptlrpc_confs);
541 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
546 * caller must hold conf_lock already.
548 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
550 struct sptlrpc_rule *rule)
552 struct sptlrpc_conf_tgt *conf_tgt;
553 struct sptlrpc_rule_set *rule_set;
555 /* fsname == target means general rules for the whole fs */
556 if (strcmp(conf->sc_fsname, target) == 0) {
557 rule_set = &conf->sc_rset;
559 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
561 rule_set = &conf_tgt->sct_rset;
563 CERROR("out of memory, can't merge rule!\n");
568 return sptlrpc_rule_set_merge(rule_set, rule);
572 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
573 * find one through the target name in the record inside conf_lock;
574 * otherwise means caller already hold conf_lock.
576 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
577 struct sptlrpc_conf *conf)
579 char *target, *param;
580 char fsname[MTI_NAME_MAXLEN];
581 struct sptlrpc_rule rule;
584 target = lustre_cfg_string(lcfg, 1);
585 if (target == NULL) {
586 CERROR("missing target name\n");
590 param = lustre_cfg_string(lcfg, 2);
592 CERROR("missing parameter\n");
596 CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
598 /* parse rule to make sure the format is correct */
599 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
600 CERROR("Invalid sptlrpc parameter: %s\n", param);
603 param += sizeof(PARAM_SRPC_FLVR) - 1;
605 rc = sptlrpc_parse_rule(param, &rule);
610 target2fsname(target, fsname, sizeof(fsname));
612 mutex_lock(&sptlrpc_conf_lock);
613 conf = sptlrpc_conf_get(fsname, 0);
615 CERROR("can't find conf\n");
618 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
620 mutex_unlock(&sptlrpc_conf_lock);
622 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
623 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
632 int sptlrpc_process_config(struct lustre_cfg *lcfg)
634 return __sptlrpc_process_config(lcfg, NULL);
636 EXPORT_SYMBOL(sptlrpc_process_config);
638 static int logname2fsname(const char *logname, char *buf, int buflen)
643 ptr = strrchr(logname, '-');
644 if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
645 CERROR("%s is not a sptlrpc config log\n", logname);
649 len = min((int) (ptr - logname), buflen - 1);
651 memcpy(buf, logname, len);
656 void sptlrpc_conf_log_update_begin(const char *logname)
658 struct sptlrpc_conf *conf;
661 if (logname2fsname(logname, fsname, sizeof(fsname)))
664 mutex_lock(&sptlrpc_conf_lock);
666 conf = sptlrpc_conf_get(fsname, 0);
668 if (conf->sc_local) {
669 LASSERT(conf->sc_updated == 0);
670 sptlrpc_conf_free_rsets(conf);
672 conf->sc_modified = 0;
675 mutex_unlock(&sptlrpc_conf_lock);
677 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
680 * mark a config log has been updated
682 void sptlrpc_conf_log_update_end(const char *logname)
684 struct sptlrpc_conf *conf;
687 if (logname2fsname(logname, fsname, sizeof(fsname)))
690 mutex_lock(&sptlrpc_conf_lock);
692 conf = sptlrpc_conf_get(fsname, 0);
695 * if original state is not updated, make sure the
696 * modified counter > 0 to enforce updating local copy.
698 if (conf->sc_updated == 0)
701 conf->sc_updated = 1;
704 mutex_unlock(&sptlrpc_conf_lock);
706 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
708 void sptlrpc_conf_log_start(const char *logname)
712 if (logname2fsname(logname, fsname, sizeof(fsname)))
715 mutex_lock(&sptlrpc_conf_lock);
716 sptlrpc_conf_get(fsname, 1);
717 mutex_unlock(&sptlrpc_conf_lock);
719 EXPORT_SYMBOL(sptlrpc_conf_log_start);
721 void sptlrpc_conf_log_stop(const char *logname)
723 struct sptlrpc_conf *conf;
726 if (logname2fsname(logname, fsname, sizeof(fsname)))
729 mutex_lock(&sptlrpc_conf_lock);
730 conf = sptlrpc_conf_get(fsname, 0);
732 sptlrpc_conf_free(conf);
733 mutex_unlock(&sptlrpc_conf_lock);
735 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
737 static inline void flavor_set_flags(struct sptlrpc_flavor *sf,
738 enum lustre_sec_part from,
739 enum lustre_sec_part to,
740 unsigned int fl_udesc)
743 * null flavor doesn't need to set any flavor, and in fact
744 * we'd better not do that because everybody share a single sec.
746 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
749 if (from == LUSTRE_SP_MDT) {
750 /* MDT->MDT; MDT->OST */
751 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
752 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
754 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
755 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
757 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
758 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
762 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
763 enum lustre_sec_part to,
764 struct obd_uuid *target,
766 struct sptlrpc_flavor *sf)
768 struct sptlrpc_conf *conf;
769 struct sptlrpc_conf_tgt *conf_tgt;
770 char name[MTI_NAME_MAXLEN];
773 target2fsname(target->uuid, name, sizeof(name));
775 mutex_lock(&sptlrpc_conf_lock);
777 conf = sptlrpc_conf_get(name, 0);
781 /* convert uuid name (supposed end with _UUID) to target name */
782 len = strlen(target->uuid);
784 memcpy(name, target->uuid, len - 5);
785 name[len - 5] = '\0';
787 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
789 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
795 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
797 mutex_unlock(&sptlrpc_conf_lock);
800 get_default_flavor(sf);
802 flavor_set_flags(sf, from, to, 1);
805 #define SEC_ADAPT_DELAY (10)
808 * called by client devices, notify the sptlrpc config has changed and
809 * do import_sec_adapt later.
811 void sptlrpc_conf_client_adapt(struct obd_device *obd)
813 struct obd_import *imp;
815 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
816 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0);
817 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
819 /* serialize with connect/disconnect import */
820 down_read(&obd->u.cli.cl_sem);
822 imp = obd->u.cli.cl_import;
824 spin_lock(&imp->imp_lock);
826 imp->imp_sec_expire = ktime_get_real_seconds() +
828 spin_unlock(&imp->imp_lock);
831 up_read(&obd->u.cli.cl_sem);
833 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
835 int sptlrpc_conf_init(void)
837 mutex_init(&sptlrpc_conf_lock);
841 void sptlrpc_conf_fini(void)
843 struct sptlrpc_conf *conf, *conf_next;
845 mutex_lock(&sptlrpc_conf_lock);
846 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
847 sptlrpc_conf_free(conf);
849 LASSERT(list_empty(&sptlrpc_confs));
850 mutex_unlock(&sptlrpc_conf_lock);