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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_SEC
35 #include <linux/libcfs/libcfs.h>
36 #include <linux/crypto.h>
37 #include <linux/key.h>
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre_import.h>
43 #include <uapi/linux/lustre/lustre_param.h>
44 #include <lustre_sec.h>
46 #include "ptlrpc_internal.h"
48 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
50 const char *type = obd->obd_type->typ_name;
52 if (!strcmp(type, LUSTRE_MDT_NAME))
54 if (!strcmp(type, LUSTRE_OST_NAME))
56 if (!strcmp(type, LUSTRE_MGS_NAME))
59 CERROR("unknown target %p(%s)\n", obd, type);
63 /****************************************
64 * user supplied flavor string parsing *
65 ****************************************/
68 * format: <base_flavor>[-<bulk_type:alg_spec>]
70 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
75 memset(flvr, 0, sizeof(*flvr));
77 if (!str || str[0] == '\0') {
78 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
82 strlcpy(buf, str, sizeof(buf));
84 bulk = strchr(buf, '-');
88 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
89 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
93 * currently only base flavor "plain" can have bulk specification.
95 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
96 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
99 * format: plain-hash:<hash_alg>
101 alg = strchr(bulk, ':');
106 if (strcmp(bulk, "hash"))
109 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
110 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
114 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
115 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
117 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
127 CERROR("invalid flavor string: %s\n", str);
130 EXPORT_SYMBOL(sptlrpc_parse_flavor);
132 /****************************************
134 ****************************************/
136 static void get_default_flavor(struct sptlrpc_flavor *sf)
138 memset(sf, 0, sizeof(*sf));
140 sf->sf_rpc = SPTLRPC_FLVR_NULL;
144 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
146 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
147 rule->sr_from = LUSTRE_SP_ANY;
148 rule->sr_to = LUSTRE_SP_ANY;
149 rule->sr_padding = 0;
151 get_default_flavor(&rule->sr_flvr);
155 * format: network[.direction]=flavor
157 static int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
162 sptlrpc_rule_init(rule);
164 flavor = strchr(param, '=');
166 CERROR("invalid param, no '='\n");
171 dir = strchr(param, '.');
176 if (strcmp(param, "default")) {
177 rule->sr_netid = libcfs_str2net(param);
178 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
179 CERROR("invalid network name: %s\n", param);
186 if (!strcmp(dir, "mdt2ost")) {
187 rule->sr_from = LUSTRE_SP_MDT;
188 rule->sr_to = LUSTRE_SP_OST;
189 } else if (!strcmp(dir, "mdt2mdt")) {
190 rule->sr_from = LUSTRE_SP_MDT;
191 rule->sr_to = LUSTRE_SP_MDT;
192 } else if (!strcmp(dir, "cli2ost")) {
193 rule->sr_from = LUSTRE_SP_CLI;
194 rule->sr_to = LUSTRE_SP_OST;
195 } else if (!strcmp(dir, "cli2mdt")) {
196 rule->sr_from = LUSTRE_SP_CLI;
197 rule->sr_to = LUSTRE_SP_MDT;
199 CERROR("invalid rule dir segment: %s\n", dir);
205 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
212 static void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
214 LASSERT(rset->srs_nslot ||
215 (rset->srs_nrule == 0 && !rset->srs_rules));
217 if (rset->srs_nslot) {
218 kfree(rset->srs_rules);
219 sptlrpc_rule_set_init(rset);
224 * return 0 if the rule set could accommodate one more rule.
226 static int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
228 struct sptlrpc_rule *rules;
233 if (rset->srs_nrule < rset->srs_nslot)
236 nslot = rset->srs_nslot + 8;
238 /* better use realloc() if available */
239 rules = kcalloc(nslot, sizeof(*rset->srs_rules), GFP_NOFS);
243 if (rset->srs_nrule) {
244 LASSERT(rset->srs_nslot && rset->srs_rules);
245 memcpy(rules, rset->srs_rules,
246 rset->srs_nrule * sizeof(*rset->srs_rules));
248 kfree(rset->srs_rules);
251 rset->srs_rules = rules;
252 rset->srs_nslot = nslot;
256 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
258 return (rule->sr_from != LUSTRE_SP_ANY ||
259 rule->sr_to != LUSTRE_SP_ANY);
262 static inline int rule_spec_net(struct sptlrpc_rule *rule)
264 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
267 static inline int rule_match_dir(struct sptlrpc_rule *r1,
268 struct sptlrpc_rule *r2)
270 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
273 static inline int rule_match_net(struct sptlrpc_rule *r1,
274 struct sptlrpc_rule *r2)
276 return (r1->sr_netid == r2->sr_netid);
280 * merge @rule into @rset.
281 * the @rset slots might be expanded.
283 static int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
284 struct sptlrpc_rule *rule)
286 struct sptlrpc_rule *p = rset->srs_rules;
287 int spec_dir, spec_net;
288 int rc, n, match = 0;
292 spec_net = rule_spec_net(rule);
293 spec_dir = rule_spec_dir(rule);
295 for (n = 0; n < rset->srs_nrule; n++) {
296 p = &rset->srs_rules[n];
298 /* test network match, if failed:
299 * - spec rule: skip rules which is also spec rule match, until
300 * we hit a wild rule, which means no more chance
301 * - wild rule: skip until reach the one which is also wild
304 if (!rule_match_net(p, rule)) {
306 if (rule_spec_net(p))
315 /* test dir match, same logic as net matching */
316 if (!rule_match_dir(p, rule)) {
318 if (rule_spec_dir(p))
333 LASSERT(n >= 0 && n < rset->srs_nrule);
335 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
336 /* remove this rule */
337 if (n < rset->srs_nrule - 1)
338 memmove(&rset->srs_rules[n],
339 &rset->srs_rules[n + 1],
340 (rset->srs_nrule - n - 1) *
344 /* override the rule */
345 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
348 LASSERT(n >= 0 && n <= rset->srs_nrule);
350 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
351 rc = sptlrpc_rule_set_expand(rset);
355 if (n < rset->srs_nrule)
356 memmove(&rset->srs_rules[n + 1],
358 (rset->srs_nrule - n) * sizeof(*rule));
359 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
362 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
370 * given from/to/nid, determine a matching flavor in ruleset.
371 * return 1 if a match found, otherwise return 0.
373 static int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
374 enum lustre_sec_part from,
375 enum lustre_sec_part to,
377 struct sptlrpc_flavor *sf)
379 struct sptlrpc_rule *r;
382 for (n = 0; n < rset->srs_nrule; n++) {
383 r = &rset->srs_rules[n];
385 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
386 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
387 LNET_NIDNET(nid) != r->sr_netid)
390 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
394 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
405 /**********************************
406 * sptlrpc configuration support *
407 **********************************/
409 struct sptlrpc_conf_tgt {
410 struct list_head sct_list;
411 char sct_name[MAX_OBD_NAME];
412 struct sptlrpc_rule_set sct_rset;
415 struct sptlrpc_conf {
416 struct list_head sc_list;
417 char sc_fsname[MTI_NAME_MAXLEN];
418 unsigned int sc_modified; /* modified during updating */
419 unsigned int sc_updated:1, /* updated copy from MGS */
420 sc_local:1; /* local copy from target */
421 struct sptlrpc_rule_set sc_rset; /* fs general rules */
422 struct list_head sc_tgts; /* target-specific rules */
425 static struct mutex sptlrpc_conf_lock;
426 static LIST_HEAD(sptlrpc_confs);
428 static inline int is_hex(char c)
430 return ((c >= '0' && c <= '9') ||
431 (c >= 'a' && c <= 'f'));
434 static void target2fsname(const char *tgt, char *fsname, int buflen)
439 ptr = strrchr(tgt, '-');
441 if ((strncmp(ptr, "-MDT", 4) != 0 &&
442 strncmp(ptr, "-OST", 4) != 0) ||
443 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
444 !is_hex(ptr[6]) || !is_hex(ptr[7]))
448 /* if we didn't find the pattern, treat the whole string as fsname */
454 len = min(len, buflen - 1);
455 memcpy(fsname, tgt, len);
459 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
461 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
463 sptlrpc_rule_set_free(&conf->sc_rset);
465 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
466 &conf->sc_tgts, sct_list) {
467 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
468 list_del(&conf_tgt->sct_list);
471 LASSERT(list_empty(&conf->sc_tgts));
473 conf->sc_updated = 0;
477 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
479 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
481 sptlrpc_conf_free_rsets(conf);
482 list_del(&conf->sc_list);
487 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
491 struct sptlrpc_conf_tgt *conf_tgt;
493 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
494 if (strcmp(conf_tgt->sct_name, name) == 0)
501 conf_tgt = kzalloc(sizeof(*conf_tgt), GFP_NOFS);
503 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
504 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
505 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
512 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
515 struct sptlrpc_conf *conf;
518 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
519 if (strcmp(conf->sc_fsname, fsname) == 0)
526 conf = kzalloc(sizeof(*conf), GFP_NOFS);
530 len = strlcpy(conf->sc_fsname, fsname, sizeof(conf->sc_fsname));
531 if (len >= sizeof(conf->sc_fsname)) {
535 sptlrpc_rule_set_init(&conf->sc_rset);
536 INIT_LIST_HEAD(&conf->sc_tgts);
537 list_add(&conf->sc_list, &sptlrpc_confs);
539 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
544 * caller must hold conf_lock already.
546 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
548 struct sptlrpc_rule *rule)
550 struct sptlrpc_conf_tgt *conf_tgt;
551 struct sptlrpc_rule_set *rule_set;
553 /* fsname == target means general rules for the whole fs */
554 if (strcmp(conf->sc_fsname, target) == 0) {
555 rule_set = &conf->sc_rset;
557 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
559 rule_set = &conf_tgt->sct_rset;
561 CERROR("out of memory, can't merge rule!\n");
566 return sptlrpc_rule_set_merge(rule_set, rule);
570 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
571 * find one through the target name in the record inside conf_lock;
572 * otherwise means caller already hold conf_lock.
574 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
575 struct sptlrpc_conf *conf)
577 char *target, *param;
578 char fsname[MTI_NAME_MAXLEN];
579 struct sptlrpc_rule rule;
582 target = lustre_cfg_string(lcfg, 1);
584 CERROR("missing target name\n");
588 param = lustre_cfg_string(lcfg, 2);
590 CERROR("missing parameter\n");
594 CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
596 /* parse rule to make sure the format is correct */
597 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
598 CERROR("Invalid sptlrpc parameter: %s\n", param);
601 param += sizeof(PARAM_SRPC_FLVR) - 1;
603 rc = sptlrpc_parse_rule(param, &rule);
608 target2fsname(target, fsname, sizeof(fsname));
610 mutex_lock(&sptlrpc_conf_lock);
611 conf = sptlrpc_conf_get(fsname, 0);
613 CERROR("can't find conf\n");
616 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
618 mutex_unlock(&sptlrpc_conf_lock);
620 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
621 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
630 int sptlrpc_process_config(struct lustre_cfg *lcfg)
632 return __sptlrpc_process_config(lcfg, NULL);
634 EXPORT_SYMBOL(sptlrpc_process_config);
636 static int logname2fsname(const char *logname, char *buf, int buflen)
641 ptr = strrchr(logname, '-');
642 if (!ptr || strcmp(ptr, "-sptlrpc")) {
643 CERROR("%s is not a sptlrpc config log\n", logname);
647 len = min((int)(ptr - logname), buflen - 1);
649 memcpy(buf, logname, len);
654 void sptlrpc_conf_log_update_begin(const char *logname)
656 struct sptlrpc_conf *conf;
659 if (logname2fsname(logname, fsname, sizeof(fsname)))
662 mutex_lock(&sptlrpc_conf_lock);
664 conf = sptlrpc_conf_get(fsname, 0);
666 if (conf->sc_local) {
667 LASSERT(conf->sc_updated == 0);
668 sptlrpc_conf_free_rsets(conf);
670 conf->sc_modified = 0;
673 mutex_unlock(&sptlrpc_conf_lock);
675 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
678 * mark a config log has been updated
680 void sptlrpc_conf_log_update_end(const char *logname)
682 struct sptlrpc_conf *conf;
685 if (logname2fsname(logname, fsname, sizeof(fsname)))
688 mutex_lock(&sptlrpc_conf_lock);
690 conf = sptlrpc_conf_get(fsname, 0);
693 * if original state is not updated, make sure the
694 * modified counter > 0 to enforce updating local copy.
696 if (conf->sc_updated == 0)
699 conf->sc_updated = 1;
702 mutex_unlock(&sptlrpc_conf_lock);
704 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
706 void sptlrpc_conf_log_start(const char *logname)
710 if (logname2fsname(logname, fsname, sizeof(fsname)))
713 mutex_lock(&sptlrpc_conf_lock);
714 sptlrpc_conf_get(fsname, 1);
715 mutex_unlock(&sptlrpc_conf_lock);
717 EXPORT_SYMBOL(sptlrpc_conf_log_start);
719 void sptlrpc_conf_log_stop(const char *logname)
721 struct sptlrpc_conf *conf;
724 if (logname2fsname(logname, fsname, sizeof(fsname)))
727 mutex_lock(&sptlrpc_conf_lock);
728 conf = sptlrpc_conf_get(fsname, 0);
730 sptlrpc_conf_free(conf);
731 mutex_unlock(&sptlrpc_conf_lock);
733 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
735 static inline void flavor_set_flags(struct sptlrpc_flavor *sf,
736 enum lustre_sec_part from,
737 enum lustre_sec_part to,
738 unsigned int fl_udesc)
741 * null flavor doesn't need to set any flavor, and in fact
742 * we'd better not do that because everybody share a single sec.
744 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
747 if (from == LUSTRE_SP_MDT) {
748 /* MDT->MDT; MDT->OST */
749 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
750 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
752 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
753 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
755 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
756 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
760 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
761 enum lustre_sec_part to,
762 struct obd_uuid *target,
764 struct sptlrpc_flavor *sf)
766 struct sptlrpc_conf *conf;
767 struct sptlrpc_conf_tgt *conf_tgt;
768 char name[MTI_NAME_MAXLEN];
771 target2fsname(target->uuid, name, sizeof(name));
773 mutex_lock(&sptlrpc_conf_lock);
775 conf = sptlrpc_conf_get(name, 0);
779 /* convert uuid name (supposed end with _UUID) to target name */
780 len = strlen(target->uuid);
782 memcpy(name, target->uuid, len - 5);
783 name[len - 5] = '\0';
785 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
787 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
793 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
795 mutex_unlock(&sptlrpc_conf_lock);
798 get_default_flavor(sf);
800 flavor_set_flags(sf, from, to, 1);
803 #define SEC_ADAPT_DELAY (10)
806 * called by client devices, notify the sptlrpc config has changed and
807 * do import_sec_adapt later.
809 void sptlrpc_conf_client_adapt(struct obd_device *obd)
811 struct obd_import *imp;
813 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
814 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0);
815 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
817 /* serialize with connect/disconnect import */
818 down_read_nested(&obd->u.cli.cl_sem, OBD_CLI_SEM_MDCOSC);
820 imp = obd->u.cli.cl_import;
822 spin_lock(&imp->imp_lock);
824 imp->imp_sec_expire = ktime_get_real_seconds() +
826 spin_unlock(&imp->imp_lock);
829 up_read(&obd->u.cli.cl_sem);
831 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
833 int sptlrpc_conf_init(void)
835 mutex_init(&sptlrpc_conf_lock);
839 void sptlrpc_conf_fini(void)
841 struct sptlrpc_conf *conf, *conf_next;
843 mutex_lock(&sptlrpc_conf_lock);
844 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
845 sptlrpc_conf_free(conf);
847 LASSERT(list_empty(&sptlrpc_confs));
848 mutex_unlock(&sptlrpc_conf_lock);