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