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