xref: /openbmc/linux/net/batman-adv/bat_algo.c (revision 293d5b43)
1 /* Copyright (C) 2007-2016  B.A.T.M.A.N. contributors:
2  *
3  * Marek Lindner, Simon Wunderlich
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "main.h"
19 
20 #include <linux/errno.h>
21 #include <linux/list.h>
22 #include <linux/moduleparam.h>
23 #include <linux/printk.h>
24 #include <linux/seq_file.h>
25 #include <linux/stddef.h>
26 #include <linux/string.h>
27 
28 #include "bat_algo.h"
29 
30 char batadv_routing_algo[20] = "BATMAN_IV";
31 static struct hlist_head batadv_algo_list;
32 
33 /**
34  * batadv_algo_init - Initialize batman-adv algorithm management data structures
35  */
36 void batadv_algo_init(void)
37 {
38 	INIT_HLIST_HEAD(&batadv_algo_list);
39 }
40 
41 static struct batadv_algo_ops *batadv_algo_get(char *name)
42 {
43 	struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
44 
45 	hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
46 		if (strcmp(bat_algo_ops_tmp->name, name) != 0)
47 			continue;
48 
49 		bat_algo_ops = bat_algo_ops_tmp;
50 		break;
51 	}
52 
53 	return bat_algo_ops;
54 }
55 
56 int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
57 {
58 	struct batadv_algo_ops *bat_algo_ops_tmp;
59 
60 	bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
61 	if (bat_algo_ops_tmp) {
62 		pr_info("Trying to register already registered routing algorithm: %s\n",
63 			bat_algo_ops->name);
64 		return -EEXIST;
65 	}
66 
67 	/* all algorithms must implement all ops (for now) */
68 	if (!bat_algo_ops->iface.enable ||
69 	    !bat_algo_ops->iface.disable ||
70 	    !bat_algo_ops->iface.update_mac ||
71 	    !bat_algo_ops->iface.primary_set ||
72 	    !bat_algo_ops->neigh.cmp ||
73 	    !bat_algo_ops->neigh.is_similar_or_better) {
74 		pr_info("Routing algo '%s' does not implement required ops\n",
75 			bat_algo_ops->name);
76 		return -EINVAL;
77 	}
78 
79 	INIT_HLIST_NODE(&bat_algo_ops->list);
80 	hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
81 
82 	return 0;
83 }
84 
85 int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
86 {
87 	struct batadv_algo_ops *bat_algo_ops;
88 
89 	bat_algo_ops = batadv_algo_get(name);
90 	if (!bat_algo_ops)
91 		return -EINVAL;
92 
93 	bat_priv->algo_ops = bat_algo_ops;
94 
95 	return 0;
96 }
97 
98 int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
99 {
100 	struct batadv_algo_ops *bat_algo_ops;
101 
102 	seq_puts(seq, "Available routing algorithms:\n");
103 
104 	hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
105 		seq_printf(seq, " * %s\n", bat_algo_ops->name);
106 	}
107 
108 	return 0;
109 }
110 
111 static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
112 {
113 	struct batadv_algo_ops *bat_algo_ops;
114 	char *algo_name = (char *)val;
115 	size_t name_len = strlen(algo_name);
116 
117 	if (name_len > 0 && algo_name[name_len - 1] == '\n')
118 		algo_name[name_len - 1] = '\0';
119 
120 	bat_algo_ops = batadv_algo_get(algo_name);
121 	if (!bat_algo_ops) {
122 		pr_err("Routing algorithm '%s' is not supported\n", algo_name);
123 		return -EINVAL;
124 	}
125 
126 	return param_set_copystring(algo_name, kp);
127 }
128 
129 static const struct kernel_param_ops batadv_param_ops_ra = {
130 	.set = batadv_param_set_ra,
131 	.get = param_get_string,
132 };
133 
134 static struct kparam_string batadv_param_string_ra = {
135 	.maxlen = sizeof(batadv_routing_algo),
136 	.string = batadv_routing_algo,
137 };
138 
139 module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
140 		0644);
141