blob: 44fd073b7546c47ceb25238c5686aad2967bc15f [file] [log] [blame]
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01001/* Copyright (C) 2007-2017 B.A.T.M.A.N. contributors:
Sven Eckelmann01d350d2016-05-15 11:07:44 +02002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "main.h"
19
20#include <linux/errno.h>
21#include <linux/list.h>
22#include <linux/moduleparam.h>
Matthias Schiffer07a30612016-07-03 13:31:35 +020023#include <linux/netlink.h>
Sven Eckelmann01d350d2016-05-15 11:07:44 +020024#include <linux/printk.h>
25#include <linux/seq_file.h>
Matthias Schiffer07a30612016-07-03 13:31:35 +020026#include <linux/skbuff.h>
Sven Eckelmann01d350d2016-05-15 11:07:44 +020027#include <linux/stddef.h>
28#include <linux/string.h>
Matthias Schiffer07a30612016-07-03 13:31:35 +020029#include <net/genetlink.h>
30#include <net/netlink.h>
31#include <uapi/linux/batman_adv.h>
Sven Eckelmann01d350d2016-05-15 11:07:44 +020032
33#include "bat_algo.h"
Matthias Schiffer07a30612016-07-03 13:31:35 +020034#include "netlink.h"
Sven Eckelmann01d350d2016-05-15 11:07:44 +020035
36char batadv_routing_algo[20] = "BATMAN_IV";
37static struct hlist_head batadv_algo_list;
38
39/**
40 * batadv_algo_init - Initialize batman-adv algorithm management data structures
41 */
42void batadv_algo_init(void)
43{
44 INIT_HLIST_HEAD(&batadv_algo_list);
45}
46
47static struct batadv_algo_ops *batadv_algo_get(char *name)
48{
49 struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
50
51 hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
52 if (strcmp(bat_algo_ops_tmp->name, name) != 0)
53 continue;
54
55 bat_algo_ops = bat_algo_ops_tmp;
56 break;
57 }
58
59 return bat_algo_ops;
60}
61
62int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
63{
64 struct batadv_algo_ops *bat_algo_ops_tmp;
65
66 bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
67 if (bat_algo_ops_tmp) {
68 pr_info("Trying to register already registered routing algorithm: %s\n",
69 bat_algo_ops->name);
70 return -EEXIST;
71 }
72
73 /* all algorithms must implement all ops (for now) */
Antonio Quartulli29824a52016-05-25 23:27:31 +080074 if (!bat_algo_ops->iface.enable ||
75 !bat_algo_ops->iface.disable ||
76 !bat_algo_ops->iface.update_mac ||
77 !bat_algo_ops->iface.primary_set ||
78 !bat_algo_ops->neigh.cmp ||
79 !bat_algo_ops->neigh.is_similar_or_better) {
Sven Eckelmann01d350d2016-05-15 11:07:44 +020080 pr_info("Routing algo '%s' does not implement required ops\n",
81 bat_algo_ops->name);
82 return -EINVAL;
83 }
84
85 INIT_HLIST_NODE(&bat_algo_ops->list);
86 hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
87
88 return 0;
89}
90
91int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
92{
93 struct batadv_algo_ops *bat_algo_ops;
94
95 bat_algo_ops = batadv_algo_get(name);
96 if (!bat_algo_ops)
97 return -EINVAL;
98
Antonio Quartulli29824a52016-05-25 23:27:31 +080099 bat_priv->algo_ops = bat_algo_ops;
Sven Eckelmann01d350d2016-05-15 11:07:44 +0200100
101 return 0;
102}
103
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200104#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmann01d350d2016-05-15 11:07:44 +0200105int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
106{
107 struct batadv_algo_ops *bat_algo_ops;
108
109 seq_puts(seq, "Available routing algorithms:\n");
110
111 hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
112 seq_printf(seq, " * %s\n", bat_algo_ops->name);
113 }
114
115 return 0;
116}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200117#endif
Sven Eckelmann01d350d2016-05-15 11:07:44 +0200118
119static 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
137static const struct kernel_param_ops batadv_param_ops_ra = {
138 .set = batadv_param_set_ra,
139 .get = param_get_string,
140};
141
142static struct kparam_string batadv_param_string_ra = {
143 .maxlen = sizeof(batadv_routing_algo),
144 .string = batadv_routing_algo,
145};
146
147module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
148 0644);
Matthias Schiffer07a30612016-07-03 13:31:35 +0200149
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 */
160static 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 */
189int 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}