xref: /openbmc/linux/net/batman-adv/bat_algo.c (revision 07a3061e0832fe22932e0fa977581e45b9c42431)
101d350d1SSven Eckelmann /* Copyright (C) 2007-2016  B.A.T.M.A.N. contributors:
201d350d1SSven Eckelmann  *
301d350d1SSven Eckelmann  * Marek Lindner, Simon Wunderlich
401d350d1SSven Eckelmann  *
501d350d1SSven Eckelmann  * This program is free software; you can redistribute it and/or
601d350d1SSven Eckelmann  * modify it under the terms of version 2 of the GNU General Public
701d350d1SSven Eckelmann  * License as published by the Free Software Foundation.
801d350d1SSven Eckelmann  *
901d350d1SSven Eckelmann  * This program is distributed in the hope that it will be useful, but
1001d350d1SSven Eckelmann  * WITHOUT ANY WARRANTY; without even the implied warranty of
1101d350d1SSven Eckelmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1201d350d1SSven Eckelmann  * General Public License for more details.
1301d350d1SSven Eckelmann  *
1401d350d1SSven Eckelmann  * You should have received a copy of the GNU General Public License
1501d350d1SSven Eckelmann  * along with this program; if not, see <http://www.gnu.org/licenses/>.
1601d350d1SSven Eckelmann  */
1701d350d1SSven Eckelmann 
1801d350d1SSven Eckelmann #include "main.h"
1901d350d1SSven Eckelmann 
2001d350d1SSven Eckelmann #include <linux/errno.h>
2101d350d1SSven Eckelmann #include <linux/list.h>
2201d350d1SSven Eckelmann #include <linux/moduleparam.h>
23*07a3061eSMatthias Schiffer #include <linux/netlink.h>
2401d350d1SSven Eckelmann #include <linux/printk.h>
2501d350d1SSven Eckelmann #include <linux/seq_file.h>
26*07a3061eSMatthias Schiffer #include <linux/skbuff.h>
2701d350d1SSven Eckelmann #include <linux/stddef.h>
2801d350d1SSven Eckelmann #include <linux/string.h>
29*07a3061eSMatthias Schiffer #include <net/genetlink.h>
30*07a3061eSMatthias Schiffer #include <net/netlink.h>
31*07a3061eSMatthias Schiffer #include <uapi/linux/batman_adv.h>
3201d350d1SSven Eckelmann 
3301d350d1SSven Eckelmann #include "bat_algo.h"
34*07a3061eSMatthias Schiffer #include "netlink.h"
3501d350d1SSven Eckelmann 
3601d350d1SSven Eckelmann char batadv_routing_algo[20] = "BATMAN_IV";
3701d350d1SSven Eckelmann static struct hlist_head batadv_algo_list;
3801d350d1SSven Eckelmann 
3901d350d1SSven Eckelmann /**
4001d350d1SSven Eckelmann  * batadv_algo_init - Initialize batman-adv algorithm management data structures
4101d350d1SSven Eckelmann  */
4201d350d1SSven Eckelmann void batadv_algo_init(void)
4301d350d1SSven Eckelmann {
4401d350d1SSven Eckelmann 	INIT_HLIST_HEAD(&batadv_algo_list);
4501d350d1SSven Eckelmann }
4601d350d1SSven Eckelmann 
4701d350d1SSven Eckelmann static struct batadv_algo_ops *batadv_algo_get(char *name)
4801d350d1SSven Eckelmann {
4901d350d1SSven Eckelmann 	struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
5001d350d1SSven Eckelmann 
5101d350d1SSven Eckelmann 	hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
5201d350d1SSven Eckelmann 		if (strcmp(bat_algo_ops_tmp->name, name) != 0)
5301d350d1SSven Eckelmann 			continue;
5401d350d1SSven Eckelmann 
5501d350d1SSven Eckelmann 		bat_algo_ops = bat_algo_ops_tmp;
5601d350d1SSven Eckelmann 		break;
5701d350d1SSven Eckelmann 	}
5801d350d1SSven Eckelmann 
5901d350d1SSven Eckelmann 	return bat_algo_ops;
6001d350d1SSven Eckelmann }
6101d350d1SSven Eckelmann 
6201d350d1SSven Eckelmann int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
6301d350d1SSven Eckelmann {
6401d350d1SSven Eckelmann 	struct batadv_algo_ops *bat_algo_ops_tmp;
6501d350d1SSven Eckelmann 
6601d350d1SSven Eckelmann 	bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
6701d350d1SSven Eckelmann 	if (bat_algo_ops_tmp) {
6801d350d1SSven Eckelmann 		pr_info("Trying to register already registered routing algorithm: %s\n",
6901d350d1SSven Eckelmann 			bat_algo_ops->name);
7001d350d1SSven Eckelmann 		return -EEXIST;
7101d350d1SSven Eckelmann 	}
7201d350d1SSven Eckelmann 
7301d350d1SSven Eckelmann 	/* all algorithms must implement all ops (for now) */
7429824a55SAntonio Quartulli 	if (!bat_algo_ops->iface.enable ||
7529824a55SAntonio Quartulli 	    !bat_algo_ops->iface.disable ||
7629824a55SAntonio Quartulli 	    !bat_algo_ops->iface.update_mac ||
7729824a55SAntonio Quartulli 	    !bat_algo_ops->iface.primary_set ||
7829824a55SAntonio Quartulli 	    !bat_algo_ops->neigh.cmp ||
7929824a55SAntonio Quartulli 	    !bat_algo_ops->neigh.is_similar_or_better) {
8001d350d1SSven Eckelmann 		pr_info("Routing algo '%s' does not implement required ops\n",
8101d350d1SSven Eckelmann 			bat_algo_ops->name);
8201d350d1SSven Eckelmann 		return -EINVAL;
8301d350d1SSven Eckelmann 	}
8401d350d1SSven Eckelmann 
8501d350d1SSven Eckelmann 	INIT_HLIST_NODE(&bat_algo_ops->list);
8601d350d1SSven Eckelmann 	hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
8701d350d1SSven Eckelmann 
8801d350d1SSven Eckelmann 	return 0;
8901d350d1SSven Eckelmann }
9001d350d1SSven Eckelmann 
9101d350d1SSven Eckelmann int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
9201d350d1SSven Eckelmann {
9301d350d1SSven Eckelmann 	struct batadv_algo_ops *bat_algo_ops;
9401d350d1SSven Eckelmann 
9501d350d1SSven Eckelmann 	bat_algo_ops = batadv_algo_get(name);
9601d350d1SSven Eckelmann 	if (!bat_algo_ops)
9701d350d1SSven Eckelmann 		return -EINVAL;
9801d350d1SSven Eckelmann 
9929824a55SAntonio Quartulli 	bat_priv->algo_ops = bat_algo_ops;
10001d350d1SSven Eckelmann 
10101d350d1SSven Eckelmann 	return 0;
10201d350d1SSven Eckelmann }
10301d350d1SSven Eckelmann 
10401d350d1SSven Eckelmann int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
10501d350d1SSven Eckelmann {
10601d350d1SSven Eckelmann 	struct batadv_algo_ops *bat_algo_ops;
10701d350d1SSven Eckelmann 
10801d350d1SSven Eckelmann 	seq_puts(seq, "Available routing algorithms:\n");
10901d350d1SSven Eckelmann 
11001d350d1SSven Eckelmann 	hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
11101d350d1SSven Eckelmann 		seq_printf(seq, " * %s\n", bat_algo_ops->name);
11201d350d1SSven Eckelmann 	}
11301d350d1SSven Eckelmann 
11401d350d1SSven Eckelmann 	return 0;
11501d350d1SSven Eckelmann }
11601d350d1SSven Eckelmann 
11701d350d1SSven Eckelmann static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
11801d350d1SSven Eckelmann {
11901d350d1SSven Eckelmann 	struct batadv_algo_ops *bat_algo_ops;
12001d350d1SSven Eckelmann 	char *algo_name = (char *)val;
12101d350d1SSven Eckelmann 	size_t name_len = strlen(algo_name);
12201d350d1SSven Eckelmann 
12301d350d1SSven Eckelmann 	if (name_len > 0 && algo_name[name_len - 1] == '\n')
12401d350d1SSven Eckelmann 		algo_name[name_len - 1] = '\0';
12501d350d1SSven Eckelmann 
12601d350d1SSven Eckelmann 	bat_algo_ops = batadv_algo_get(algo_name);
12701d350d1SSven Eckelmann 	if (!bat_algo_ops) {
12801d350d1SSven Eckelmann 		pr_err("Routing algorithm '%s' is not supported\n", algo_name);
12901d350d1SSven Eckelmann 		return -EINVAL;
13001d350d1SSven Eckelmann 	}
13101d350d1SSven Eckelmann 
13201d350d1SSven Eckelmann 	return param_set_copystring(algo_name, kp);
13301d350d1SSven Eckelmann }
13401d350d1SSven Eckelmann 
13501d350d1SSven Eckelmann static const struct kernel_param_ops batadv_param_ops_ra = {
13601d350d1SSven Eckelmann 	.set = batadv_param_set_ra,
13701d350d1SSven Eckelmann 	.get = param_get_string,
13801d350d1SSven Eckelmann };
13901d350d1SSven Eckelmann 
14001d350d1SSven Eckelmann static struct kparam_string batadv_param_string_ra = {
14101d350d1SSven Eckelmann 	.maxlen = sizeof(batadv_routing_algo),
14201d350d1SSven Eckelmann 	.string = batadv_routing_algo,
14301d350d1SSven Eckelmann };
14401d350d1SSven Eckelmann 
14501d350d1SSven Eckelmann module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
14601d350d1SSven Eckelmann 		0644);
147*07a3061eSMatthias Schiffer 
148*07a3061eSMatthias Schiffer /**
149*07a3061eSMatthias Schiffer  * batadv_algo_dump_entry - fill in information about one supported routing
150*07a3061eSMatthias Schiffer  *  algorithm
151*07a3061eSMatthias Schiffer  * @msg: netlink message to be sent back
152*07a3061eSMatthias Schiffer  * @portid: Port to reply to
153*07a3061eSMatthias Schiffer  * @seq: Sequence number of message
154*07a3061eSMatthias Schiffer  * @bat_algo_ops: Algorithm to be dumped
155*07a3061eSMatthias Schiffer  *
156*07a3061eSMatthias Schiffer  * Return: Error number, or 0 on success
157*07a3061eSMatthias Schiffer  */
158*07a3061eSMatthias Schiffer static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
159*07a3061eSMatthias Schiffer 				  struct batadv_algo_ops *bat_algo_ops)
160*07a3061eSMatthias Schiffer {
161*07a3061eSMatthias Schiffer 	void *hdr;
162*07a3061eSMatthias Schiffer 
163*07a3061eSMatthias Schiffer 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
164*07a3061eSMatthias Schiffer 			  NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS);
165*07a3061eSMatthias Schiffer 	if (!hdr)
166*07a3061eSMatthias Schiffer 		return -EMSGSIZE;
167*07a3061eSMatthias Schiffer 
168*07a3061eSMatthias Schiffer 	if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name))
169*07a3061eSMatthias Schiffer 		goto nla_put_failure;
170*07a3061eSMatthias Schiffer 
171*07a3061eSMatthias Schiffer 	genlmsg_end(msg, hdr);
172*07a3061eSMatthias Schiffer 	return 0;
173*07a3061eSMatthias Schiffer 
174*07a3061eSMatthias Schiffer  nla_put_failure:
175*07a3061eSMatthias Schiffer 	genlmsg_cancel(msg, hdr);
176*07a3061eSMatthias Schiffer 	return -EMSGSIZE;
177*07a3061eSMatthias Schiffer }
178*07a3061eSMatthias Schiffer 
179*07a3061eSMatthias Schiffer /**
180*07a3061eSMatthias Schiffer  * batadv_algo_dump - fill in information about supported routing
181*07a3061eSMatthias Schiffer  *  algorithms
182*07a3061eSMatthias Schiffer  * @msg: netlink message to be sent back
183*07a3061eSMatthias Schiffer  * @cb: Parameters to the netlink request
184*07a3061eSMatthias Schiffer  *
185*07a3061eSMatthias Schiffer  * Return: Length of reply message.
186*07a3061eSMatthias Schiffer  */
187*07a3061eSMatthias Schiffer int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb)
188*07a3061eSMatthias Schiffer {
189*07a3061eSMatthias Schiffer 	int portid = NETLINK_CB(cb->skb).portid;
190*07a3061eSMatthias Schiffer 	struct batadv_algo_ops *bat_algo_ops;
191*07a3061eSMatthias Schiffer 	int skip = cb->args[0];
192*07a3061eSMatthias Schiffer 	int i = 0;
193*07a3061eSMatthias Schiffer 
194*07a3061eSMatthias Schiffer 	hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
195*07a3061eSMatthias Schiffer 		if (i++ < skip)
196*07a3061eSMatthias Schiffer 			continue;
197*07a3061eSMatthias Schiffer 
198*07a3061eSMatthias Schiffer 		if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
199*07a3061eSMatthias Schiffer 					   bat_algo_ops)) {
200*07a3061eSMatthias Schiffer 			i--;
201*07a3061eSMatthias Schiffer 			break;
202*07a3061eSMatthias Schiffer 		}
203*07a3061eSMatthias Schiffer 	}
204*07a3061eSMatthias Schiffer 
205*07a3061eSMatthias Schiffer 	cb->args[0] = i;
206*07a3061eSMatthias Schiffer 
207*07a3061eSMatthias Schiffer 	return msg->len;
208*07a3061eSMatthias Schiffer }
209