Merge tag 'efi-fixes-for-v6.3-1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / net / batman-adv / bat_algo.c
CommitLineData
7db7d9f3 1// SPDX-License-Identifier: GPL-2.0
cfa55c6d 2/* Copyright (C) B.A.T.M.A.N. contributors:
01d350d1
SE
3 *
4 * Marek Lindner, Simon Wunderlich
01d350d1
SE
5 */
6
7#include "main.h"
8
9#include <linux/errno.h>
10#include <linux/list.h>
11#include <linux/moduleparam.h>
07a3061e 12#include <linux/netlink.h>
01d350d1 13#include <linux/printk.h>
07a3061e 14#include <linux/skbuff.h>
01d350d1
SE
15#include <linux/stddef.h>
16#include <linux/string.h>
07a3061e
MS
17#include <net/genetlink.h>
18#include <net/netlink.h>
19#include <uapi/linux/batman_adv.h>
01d350d1
SE
20
21#include "bat_algo.h"
07a3061e 22#include "netlink.h"
01d350d1
SE
23
24char batadv_routing_algo[20] = "BATMAN_IV";
25static struct hlist_head batadv_algo_list;
26
27/**
7e9a8c2c
SE
28 * batadv_algo_init() - Initialize batman-adv algorithm management data
29 * structures
01d350d1
SE
30 */
31void batadv_algo_init(void)
32{
33 INIT_HLIST_HEAD(&batadv_algo_list);
34}
35
a5ad457e
SE
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 */
42struct batadv_algo_ops *batadv_algo_get(const char *name)
01d350d1
SE
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
ff15c27c
SE
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 */
01d350d1
SE
63int 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) */
29824a55
AQ
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) {
01d350d1
SE
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
ff15c27c
SE
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 */
a5ad457e 105int batadv_algo_select(struct batadv_priv *bat_priv, const char *name)
01d350d1
SE
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
29824a55 113 bat_priv->algo_ops = bat_algo_ops;
01d350d1
SE
114
115 return 0;
116}
117
01d350d1
SE
118static 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
136static const struct kernel_param_ops batadv_param_ops_ra = {
137 .set = batadv_param_set_ra,
138 .get = param_get_string,
139};
140
141static struct kparam_string batadv_param_string_ra = {
142 .maxlen = sizeof(batadv_routing_algo),
143 .string = batadv_routing_algo,
144};
145
146module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
147 0644);
07a3061e
MS
148
149/**
7e9a8c2c 150 * batadv_algo_dump_entry() - fill in information about one supported routing
07a3061e
MS
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 */
159static 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/**
7e9a8c2c 181 * batadv_algo_dump() - fill in information about supported routing
07a3061e
MS
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 */
188int 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}