Commit | Line | Data |
---|---|---|
01d350d1 SE |
1 | /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors: |
2 | * | |
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> | |
07a3061e | 23 | #include <linux/netlink.h> |
01d350d1 SE |
24 | #include <linux/printk.h> |
25 | #include <linux/seq_file.h> | |
07a3061e | 26 | #include <linux/skbuff.h> |
01d350d1 SE |
27 | #include <linux/stddef.h> |
28 | #include <linux/string.h> | |
07a3061e MS |
29 | #include <net/genetlink.h> |
30 | #include <net/netlink.h> | |
31 | #include <uapi/linux/batman_adv.h> | |
01d350d1 SE |
32 | |
33 | #include "bat_algo.h" | |
07a3061e | 34 | #include "netlink.h" |
01d350d1 SE |
35 | |
36 | char batadv_routing_algo[20] = "BATMAN_IV"; | |
37 | static struct hlist_head batadv_algo_list; | |
38 | ||
39 | /** | |
40 | * batadv_algo_init - Initialize batman-adv algorithm management data structures | |
41 | */ | |
42 | void batadv_algo_init(void) | |
43 | { | |
44 | INIT_HLIST_HEAD(&batadv_algo_list); | |
45 | } | |
46 | ||
47 | static 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 | ||
62 | int 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) */ | |
29824a55 AQ |
74 | 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) { | |
01d350d1 SE |
80 | 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 | ||
91 | int 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 | ||
29824a55 | 99 | bat_priv->algo_ops = bat_algo_ops; |
01d350d1 SE |
100 | |
101 | return 0; | |
102 | } | |
103 | ||
104 | int batadv_algo_seq_print_text(struct seq_file *seq, void *offset) | |
105 | { | |
106 | struct batadv_algo_ops *bat_algo_ops; | |
107 | ||
108 | seq_puts(seq, "Available routing algorithms:\n"); | |
109 | ||
110 | hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) { | |
111 | seq_printf(seq, " * %s\n", bat_algo_ops->name); | |
112 | } | |
113 | ||
114 | return 0; | |
115 | } | |
116 | ||
117 | static int batadv_param_set_ra(const char *val, const struct kernel_param *kp) | |
118 | { | |
119 | struct batadv_algo_ops *bat_algo_ops; | |
120 | char *algo_name = (char *)val; | |
121 | size_t name_len = strlen(algo_name); | |
122 | ||
123 | if (name_len > 0 && algo_name[name_len - 1] == '\n') | |
124 | algo_name[name_len - 1] = '\0'; | |
125 | ||
126 | bat_algo_ops = batadv_algo_get(algo_name); | |
127 | if (!bat_algo_ops) { | |
128 | pr_err("Routing algorithm '%s' is not supported\n", algo_name); | |
129 | return -EINVAL; | |
130 | } | |
131 | ||
132 | return param_set_copystring(algo_name, kp); | |
133 | } | |
134 | ||
135 | static const struct kernel_param_ops batadv_param_ops_ra = { | |
136 | .set = batadv_param_set_ra, | |
137 | .get = param_get_string, | |
138 | }; | |
139 | ||
140 | static struct kparam_string batadv_param_string_ra = { | |
141 | .maxlen = sizeof(batadv_routing_algo), | |
142 | .string = batadv_routing_algo, | |
143 | }; | |
144 | ||
145 | module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra, | |
146 | 0644); | |
07a3061e MS |
147 | |
148 | /** | |
149 | * batadv_algo_dump_entry - fill in information about one supported routing | |
150 | * algorithm | |
151 | * @msg: netlink message to be sent back | |
152 | * @portid: Port to reply to | |
153 | * @seq: Sequence number of message | |
154 | * @bat_algo_ops: Algorithm to be dumped | |
155 | * | |
156 | * Return: Error number, or 0 on success | |
157 | */ | |
158 | static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq, | |
159 | struct batadv_algo_ops *bat_algo_ops) | |
160 | { | |
161 | void *hdr; | |
162 | ||
163 | hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, | |
164 | NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS); | |
165 | if (!hdr) | |
166 | return -EMSGSIZE; | |
167 | ||
168 | if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name)) | |
169 | goto nla_put_failure; | |
170 | ||
171 | genlmsg_end(msg, hdr); | |
172 | return 0; | |
173 | ||
174 | nla_put_failure: | |
175 | genlmsg_cancel(msg, hdr); | |
176 | return -EMSGSIZE; | |
177 | } | |
178 | ||
179 | /** | |
180 | * batadv_algo_dump - fill in information about supported routing | |
181 | * algorithms | |
182 | * @msg: netlink message to be sent back | |
183 | * @cb: Parameters to the netlink request | |
184 | * | |
185 | * Return: Length of reply message. | |
186 | */ | |
187 | int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb) | |
188 | { | |
189 | int portid = NETLINK_CB(cb->skb).portid; | |
190 | struct batadv_algo_ops *bat_algo_ops; | |
191 | int skip = cb->args[0]; | |
192 | int i = 0; | |
193 | ||
194 | hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) { | |
195 | if (i++ < skip) | |
196 | continue; | |
197 | ||
198 | if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq, | |
199 | bat_algo_ops)) { | |
200 | i--; | |
201 | break; | |
202 | } | |
203 | } | |
204 | ||
205 | cb->args[0] = i; | |
206 | ||
207 | return msg->len; | |
208 | } |