blob: 4eee53d19eb047bea986184498a3ef98ed6c84df [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmanncfa55c62021-01-01 00:00:01 +01002/* Copyright (C) B.A.T.M.A.N. contributors:
Sven Eckelmann01d350d2016-05-15 11:07:44 +02003 *
4 * Marek Lindner, Simon Wunderlich
Sven Eckelmann01d350d2016-05-15 11:07:44 +02005 */
6
7#include "main.h"
8
9#include <linux/errno.h>
10#include <linux/list.h>
11#include <linux/moduleparam.h>
Matthias Schiffer07a30612016-07-03 13:31:35 +020012#include <linux/netlink.h>
Sven Eckelmann01d350d2016-05-15 11:07:44 +020013#include <linux/printk.h>
Matthias Schiffer07a30612016-07-03 13:31:35 +020014#include <linux/skbuff.h>
Sven Eckelmann01d350d2016-05-15 11:07:44 +020015#include <linux/stddef.h>
16#include <linux/string.h>
Matthias Schiffer07a30612016-07-03 13:31:35 +020017#include <net/genetlink.h>
18#include <net/netlink.h>
19#include <uapi/linux/batman_adv.h>
Sven Eckelmann01d350d2016-05-15 11:07:44 +020020
21#include "bat_algo.h"
Matthias Schiffer07a30612016-07-03 13:31:35 +020022#include "netlink.h"
Sven Eckelmann01d350d2016-05-15 11:07:44 +020023
24char batadv_routing_algo[20] = "BATMAN_IV";
25static struct hlist_head batadv_algo_list;
26
27/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010028 * batadv_algo_init() - Initialize batman-adv algorithm management data
29 * structures
Sven Eckelmann01d350d2016-05-15 11:07:44 +020030 */
31void batadv_algo_init(void)
32{
33 INIT_HLIST_HEAD(&batadv_algo_list);
34}
35
Sven Eckelmanna5ad4572020-10-11 12:25:24 +020036/**
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)
Sven Eckelmann01d350d2016-05-15 11:07:44 +020043{
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
Sven Eckelmannff15c272017-12-02 19:51:53 +010057/**
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 */
Sven Eckelmann01d350d2016-05-15 11:07:44 +020063int 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) */
Antonio Quartulli29824a52016-05-25 23:27:31 +080075 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) {
Sven Eckelmann01d350d2016-05-15 11:07:44 +020081 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
Sven Eckelmannff15c272017-12-02 19:51:53 +010092/**
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 */
Sven Eckelmanna5ad4572020-10-11 12:25:24 +0200105int batadv_algo_select(struct batadv_priv *bat_priv, const char *name)
Sven Eckelmann01d350d2016-05-15 11:07:44 +0200106{
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
Antonio Quartulli29824a52016-05-25 23:27:31 +0800113 bat_priv->algo_ops = bat_algo_ops;
Sven Eckelmann01d350d2016-05-15 11:07:44 +0200114
115 return 0;
116}
117
Sven Eckelmann01d350d2016-05-15 11:07:44 +0200118static 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);
Matthias Schiffer07a30612016-07-03 13:31:35 +0200148
149/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100150 * batadv_algo_dump_entry() - fill in information about one supported routing
Matthias Schiffer07a30612016-07-03 13:31:35 +0200151 * 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/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100181 * batadv_algo_dump() - fill in information about supported routing
Matthias Schiffer07a30612016-07-03 13:31:35 +0200182 * 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}