1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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/seq_file.h>
15 #include <linux/skbuff.h>
16 #include <linux/stddef.h>
17 #include <linux/string.h>
18 #include <net/genetlink.h>
19 #include <net/netlink.h>
20 #include <uapi/linux/batman_adv.h>
25 char batadv_routing_algo
[20] = "BATMAN_IV";
26 static struct hlist_head batadv_algo_list
;
29 * batadv_algo_init() - Initialize batman-adv algorithm management data
32 void batadv_algo_init(void)
34 INIT_HLIST_HEAD(&batadv_algo_list
);
37 static struct batadv_algo_ops
*batadv_algo_get(char *name
)
39 struct batadv_algo_ops
*bat_algo_ops
= NULL
, *bat_algo_ops_tmp
;
41 hlist_for_each_entry(bat_algo_ops_tmp
, &batadv_algo_list
, list
) {
42 if (strcmp(bat_algo_ops_tmp
->name
, name
) != 0)
45 bat_algo_ops
= bat_algo_ops_tmp
;
53 * batadv_algo_register() - Register callbacks for a mesh algorithm
54 * @bat_algo_ops: mesh algorithm callbacks to add
56 * Return: 0 on success or negative error number in case of failure
58 int batadv_algo_register(struct batadv_algo_ops
*bat_algo_ops
)
60 struct batadv_algo_ops
*bat_algo_ops_tmp
;
62 bat_algo_ops_tmp
= batadv_algo_get(bat_algo_ops
->name
);
63 if (bat_algo_ops_tmp
) {
64 pr_info("Trying to register already registered routing algorithm: %s\n",
69 /* all algorithms must implement all ops (for now) */
70 if (!bat_algo_ops
->iface
.enable
||
71 !bat_algo_ops
->iface
.disable
||
72 !bat_algo_ops
->iface
.update_mac
||
73 !bat_algo_ops
->iface
.primary_set
||
74 !bat_algo_ops
->neigh
.cmp
||
75 !bat_algo_ops
->neigh
.is_similar_or_better
) {
76 pr_info("Routing algo '%s' does not implement required ops\n",
81 INIT_HLIST_NODE(&bat_algo_ops
->list
);
82 hlist_add_head(&bat_algo_ops
->list
, &batadv_algo_list
);
88 * batadv_algo_select() - Select algorithm of soft interface
89 * @bat_priv: the bat priv with all the soft interface information
90 * @name: name of the algorithm to select
92 * The algorithm callbacks for the soft interface will be set when the algorithm
93 * with the correct name was found. Any previous selected algorithm will not be
94 * deinitialized and the new selected algorithm will also not be initialized.
95 * It is therefore not allowed to call batadv_algo_select outside the creation
96 * function of the soft interface.
98 * Return: 0 on success or negative error number in case of failure
100 int batadv_algo_select(struct batadv_priv
*bat_priv
, char *name
)
102 struct batadv_algo_ops
*bat_algo_ops
;
104 bat_algo_ops
= batadv_algo_get(name
);
108 bat_priv
->algo_ops
= bat_algo_ops
;
113 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
116 * batadv_algo_seq_print_text() - Print the supported algorithms in a seq file
117 * @seq: seq file to print on
122 int batadv_algo_seq_print_text(struct seq_file
*seq
, void *offset
)
124 struct batadv_algo_ops
*bat_algo_ops
;
126 seq_puts(seq
, "Available routing algorithms:\n");
128 hlist_for_each_entry(bat_algo_ops
, &batadv_algo_list
, list
) {
129 seq_printf(seq
, " * %s\n", bat_algo_ops
->name
);
136 static int batadv_param_set_ra(const char *val
, const struct kernel_param
*kp
)
138 struct batadv_algo_ops
*bat_algo_ops
;
139 char *algo_name
= (char *)val
;
140 size_t name_len
= strlen(algo_name
);
142 if (name_len
> 0 && algo_name
[name_len
- 1] == '\n')
143 algo_name
[name_len
- 1] = '\0';
145 bat_algo_ops
= batadv_algo_get(algo_name
);
147 pr_err("Routing algorithm '%s' is not supported\n", algo_name
);
151 return param_set_copystring(algo_name
, kp
);
154 static const struct kernel_param_ops batadv_param_ops_ra
= {
155 .set
= batadv_param_set_ra
,
156 .get
= param_get_string
,
159 static struct kparam_string batadv_param_string_ra
= {
160 .maxlen
= sizeof(batadv_routing_algo
),
161 .string
= batadv_routing_algo
,
164 module_param_cb(routing_algo
, &batadv_param_ops_ra
, &batadv_param_string_ra
,
168 * batadv_algo_dump_entry() - fill in information about one supported routing
170 * @msg: netlink message to be sent back
171 * @portid: Port to reply to
172 * @seq: Sequence number of message
173 * @bat_algo_ops: Algorithm to be dumped
175 * Return: Error number, or 0 on success
177 static int batadv_algo_dump_entry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
178 struct batadv_algo_ops
*bat_algo_ops
)
182 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
,
183 NLM_F_MULTI
, BATADV_CMD_GET_ROUTING_ALGOS
);
187 if (nla_put_string(msg
, BATADV_ATTR_ALGO_NAME
, bat_algo_ops
->name
))
188 goto nla_put_failure
;
190 genlmsg_end(msg
, hdr
);
194 genlmsg_cancel(msg
, hdr
);
199 * batadv_algo_dump() - fill in information about supported routing
201 * @msg: netlink message to be sent back
202 * @cb: Parameters to the netlink request
204 * Return: Length of reply message.
206 int batadv_algo_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
)
208 int portid
= NETLINK_CB(cb
->skb
).portid
;
209 struct batadv_algo_ops
*bat_algo_ops
;
210 int skip
= cb
->args
[0];
213 hlist_for_each_entry(bat_algo_ops
, &batadv_algo_list
, list
) {
217 if (batadv_algo_dump_entry(msg
, portid
, cb
->nlh
->nlmsg_seq
,