1 /* Copyright (C) 2007-2017 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
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.
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.
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/>.
20 #include <linux/errno.h>
21 #include <linux/list.h>
22 #include <linux/moduleparam.h>
23 #include <linux/netlink.h>
24 #include <linux/printk.h>
25 #include <linux/seq_file.h>
26 #include <linux/skbuff.h>
27 #include <linux/stddef.h>
28 #include <linux/string.h>
29 #include <net/genetlink.h>
30 #include <net/netlink.h>
31 #include <uapi/linux/batman_adv.h>
36 char batadv_routing_algo[20] = "BATMAN_IV";
37 static struct hlist_head batadv_algo_list;
40 * batadv_algo_init - Initialize batman-adv algorithm management data structures
42 void batadv_algo_init(void)
44 INIT_HLIST_HEAD(&batadv_algo_list);
47 static struct batadv_algo_ops *batadv_algo_get(char *name)
49 struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
51 hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
52 if (strcmp(bat_algo_ops_tmp->name, name) != 0)
55 bat_algo_ops = bat_algo_ops_tmp;
62 int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
64 struct batadv_algo_ops *bat_algo_ops_tmp;
66 bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
67 if (bat_algo_ops_tmp) {
68 pr_info("Trying to register already registered routing algorithm: %s\n",
73 /* all algorithms must implement all ops (for now) */
74 if (!bat_algo_ops->iface.enable ||
75 !bat_algo_ops->iface.disable ||
76 !bat_algo_ops->iface.update_mac ||
77 !bat_algo_ops->iface.primary_set ||
78 !bat_algo_ops->neigh.cmp ||
79 !bat_algo_ops->neigh.is_similar_or_better) {
80 pr_info("Routing algo '%s' does not implement required ops\n",
85 INIT_HLIST_NODE(&bat_algo_ops->list);
86 hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
91 int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
93 struct batadv_algo_ops *bat_algo_ops;
95 bat_algo_ops = batadv_algo_get(name);
99 bat_priv->algo_ops = bat_algo_ops;
104 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
105 int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
107 struct batadv_algo_ops *bat_algo_ops;
109 seq_puts(seq, "Available routing algorithms:\n");
111 hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
112 seq_printf(seq, " * %s\n", bat_algo_ops->name);
119 static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
121 struct batadv_algo_ops *bat_algo_ops;
122 char *algo_name = (char *)val;
123 size_t name_len = strlen(algo_name);
125 if (name_len > 0 && algo_name[name_len - 1] == '\n')
126 algo_name[name_len - 1] = '\0';
128 bat_algo_ops = batadv_algo_get(algo_name);
130 pr_err("Routing algorithm '%s' is not supported\n", algo_name);
134 return param_set_copystring(algo_name, kp);
137 static const struct kernel_param_ops batadv_param_ops_ra = {
138 .set = batadv_param_set_ra,
139 .get = param_get_string,
142 static struct kparam_string batadv_param_string_ra = {
143 .maxlen = sizeof(batadv_routing_algo),
144 .string = batadv_routing_algo,
147 module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
151 * batadv_algo_dump_entry - fill in information about one supported routing
153 * @msg: netlink message to be sent back
154 * @portid: Port to reply to
155 * @seq: Sequence number of message
156 * @bat_algo_ops: Algorithm to be dumped
158 * Return: Error number, or 0 on success
160 static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
161 struct batadv_algo_ops *bat_algo_ops)
165 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
166 NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS);
170 if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name))
171 goto nla_put_failure;
173 genlmsg_end(msg, hdr);
177 genlmsg_cancel(msg, hdr);
182 * batadv_algo_dump - fill in information about supported routing
184 * @msg: netlink message to be sent back
185 * @cb: Parameters to the netlink request
187 * Return: Length of reply message.
189 int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb)
191 int portid = NETLINK_CB(cb->skb).portid;
192 struct batadv_algo_ops *bat_algo_ops;
193 int skip = cb->args[0];
196 hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
200 if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq,