xref: /openbmc/linux/net/batman-adv/bat_algo.c (revision 11696c5e)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  */
6 
7 #include "main.h"
8 
9 #include <linux/errno.h>
10 #include <linux/list.h>
11 #include <linux/moduleparam.h>
12 #include <linux/netlink.h>
13 #include <linux/printk.h>
14 #include <linux/skbuff.h>
15 #include <linux/stddef.h>
16 #include <linux/string.h>
17 #include <net/genetlink.h>
18 #include <net/netlink.h>
19 #include <uapi/linux/batman_adv.h>
20 
21 #include "bat_algo.h"
22 #include "netlink.h"
23 
24 char batadv_routing_algo[20] = "BATMAN_IV";
25 static struct hlist_head batadv_algo_list;
26 
27 /**
28  * batadv_algo_init() - Initialize batman-adv algorithm management data
29  *  structures
30  */
31 void batadv_algo_init(void)
32 {
33 	INIT_HLIST_HEAD(&batadv_algo_list);
34 }
35 
36 /**
37  * batadv_algo_get() - Search for algorithm with specific name
38  * @name: algorithm name to find
39  *
40  * Return: Pointer to batadv_algo_ops on success, NULL otherwise
41  */
42 struct batadv_algo_ops *batadv_algo_get(const char *name)
43 {
44 	struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
45 
46 	hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
47 		if (strcmp(bat_algo_ops_tmp->name, name) != 0)
48 			continue;
49 
50 		bat_algo_ops = bat_algo_ops_tmp;
51 		break;
52 	}
53 
54 	return bat_algo_ops;
55 }
56 
57 /**
58  * batadv_algo_register() - Register callbacks for a mesh algorithm
59  * @bat_algo_ops: mesh algorithm callbacks to add
60  *
61  * Return: 0 on success or negative error number in case of failure
62  */
63 int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
64 {
65 	struct batadv_algo_ops *bat_algo_ops_tmp;
66 
67 	bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
68 	if (bat_algo_ops_tmp) {
69 		pr_info("Trying to register already registered routing algorithm: %s\n",
70 			bat_algo_ops->name);
71 		return -EEXIST;
72 	}
73 
74 	/* all algorithms must implement all ops (for now) */
75 	if (!bat_algo_ops->iface.enable ||
76 	    !bat_algo_ops->iface.disable ||
77 	    !bat_algo_ops->iface.update_mac ||
78 	    !bat_algo_ops->iface.primary_set ||
79 	    !bat_algo_ops->neigh.cmp ||
80 	    !bat_algo_ops->neigh.is_similar_or_better) {
81 		pr_info("Routing algo '%s' does not implement required ops\n",
82 			bat_algo_ops->name);
83 		return -EINVAL;
84 	}
85 
86 	INIT_HLIST_NODE(&bat_algo_ops->list);
87 	hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
88 
89 	return 0;
90 }
91 
92 /**
93  * batadv_algo_select() - Select algorithm of soft interface
94  * @bat_priv: the bat priv with all the soft interface information
95  * @name: name of the algorithm to select
96  *
97  * The algorithm callbacks for the soft interface will be set when the algorithm
98  * with the correct name was found. Any previous selected algorithm will not be
99  * deinitialized and the new selected algorithm will also not be initialized.
100  * It is therefore not allowed to call batadv_algo_select outside the creation
101  * function of the soft interface.
102  *
103  * Return: 0 on success or negative error number in case of failure
104  */
105 int batadv_algo_select(struct batadv_priv *bat_priv, const char *name)
106 {
107 	struct batadv_algo_ops *bat_algo_ops;
108 
109 	bat_algo_ops = batadv_algo_get(name);
110 	if (!bat_algo_ops)
111 		return -EINVAL;
112 
113 	bat_priv->algo_ops = bat_algo_ops;
114 
115 	return 0;
116 }
117 
118 static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
119 {
120 	struct batadv_algo_ops *bat_algo_ops;
121 	char *algo_name = (char *)val;
122 	size_t name_len = strlen(algo_name);
123 
124 	if (name_len > 0 && algo_name[name_len - 1] == '\n')
125 		algo_name[name_len - 1] = '\0';
126 
127 	bat_algo_ops = batadv_algo_get(algo_name);
128 	if (!bat_algo_ops) {
129 		pr_err("Routing algorithm '%s' is not supported\n", algo_name);
130 		return -EINVAL;
131 	}
132 
133 	return param_set_copystring(algo_name, kp);
134 }
135 
136 static const struct kernel_param_ops batadv_param_ops_ra = {
137 	.set = batadv_param_set_ra,
138 	.get = param_get_string,
139 };
140 
141 static struct kparam_string batadv_param_string_ra = {
142 	.maxlen = sizeof(batadv_routing_algo),
143 	.string = batadv_routing_algo,
144 };
145 
146 module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
147 		0644);
148 
149 /**
150  * batadv_algo_dump_entry() - fill in information about one supported routing
151  *  algorithm
152  * @msg: netlink message to be sent back
153  * @portid: Port to reply to
154  * @seq: Sequence number of message
155  * @bat_algo_ops: Algorithm to be dumped
156  *
157  * Return: Error number, or 0 on success
158  */
159 static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
160 				  struct batadv_algo_ops *bat_algo_ops)
161 {
162 	void *hdr;
163 
164 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
165 			  NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS);
166 	if (!hdr)
167 		return -EMSGSIZE;
168 
169 	if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name))
170 		goto nla_put_failure;
171 
172 	genlmsg_end(msg, hdr);
173 	return 0;
174 
175  nla_put_failure:
176 	genlmsg_cancel(msg, hdr);
177 	return -EMSGSIZE;
178 }
179 
180 /**
181  * batadv_algo_dump() - fill in information about supported routing
182  *  algorithms
183  * @msg: netlink message to be sent back
184  * @cb: Parameters to the netlink request
185  *
186  * Return: Length of reply message.
187  */
188 int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb)
189 {
190 	int portid = NETLINK_CB(cb->skb).portid;
191 	struct batadv_algo_ops *bat_algo_ops;
192 	int skip = cb->args[0];
193 	int i = 0;
194 
195 	hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
196 		if (i++ < skip)
197 			continue;
198 
199 		if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
200 					   bat_algo_ops)) {
201 			i--;
202 			break;
203 		}
204 	}
205 
206 	cb->args[0] = i;
207 
208 	return msg->len;
209 }
210