batman-adv: Fix unexpected free of bcast_own on add_if error
[deliverable/linux.git] / net / batman-adv / bat_v.c
CommitLineData
d6f94d91
LL
1/* Copyright (C) 2013-2016 B.A.T.M.A.N. contributors:
2 *
3 * Linus Lüssing, Marek Lindner
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 "bat_algo.h"
19#include "main.h"
20
0b5ecc68 21#include <linux/atomic.h>
97869060 22#include <linux/bug.h>
d6f94d91
LL
23#include <linux/cache.h>
24#include <linux/init.h>
261e264d
AQ
25#include <linux/jiffies.h>
26#include <linux/netdevice.h>
27#include <linux/rculist.h>
28#include <linux/rcupdate.h>
29#include <linux/seq_file.h>
a45e932a 30#include <linux/stddef.h>
97869060 31#include <linux/types.h>
c833484e 32#include <linux/workqueue.h>
d6f94d91
LL
33
34#include "bat_v_elp.h"
0da00359 35#include "bat_v_ogm.h"
b6cf5d49 36#include "hard-interface.h"
261e264d 37#include "hash.h"
97869060 38#include "originator.h"
162bd64c 39#include "packet.h"
d6f94d91 40
b6cf5d49
AQ
41static void batadv_v_iface_activate(struct batadv_hard_iface *hard_iface)
42{
43 /* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
44 * set the interface as ACTIVE right away, without any risk of race
45 * condition
46 */
47 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
48 hard_iface->if_status = BATADV_IF_ACTIVE;
49}
50
d6f94d91
LL
51static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
52{
0da00359
AQ
53 int ret;
54
55 ret = batadv_v_elp_iface_enable(hard_iface);
56 if (ret < 0)
57 return ret;
58
59 ret = batadv_v_ogm_iface_enable(hard_iface);
60 if (ret < 0)
61 batadv_v_elp_iface_disable(hard_iface);
62
0b5ecc68
AQ
63 /* enable link throughput auto-detection by setting the throughput
64 * override to zero
65 */
66 atomic_set(&hard_iface->bat_v.throughput_override, 0);
67
0da00359 68 return ret;
d6f94d91
LL
69}
70
71static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
72{
73 batadv_v_elp_iface_disable(hard_iface);
74}
75
76static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
77{
78}
79
80static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
81{
82 batadv_v_elp_primary_iface_set(hard_iface);
0da00359 83 batadv_v_ogm_primary_iface_set(hard_iface);
d6f94d91
LL
84}
85
162bd64c
LL
86static void
87batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
88{
89 ewma_throughput_init(&hardif_neigh->bat_v.throughput);
c833484e
AQ
90 INIT_WORK(&hardif_neigh->bat_v.metric_work,
91 batadv_v_elp_throughput_metric_update);
162bd64c
LL
92}
93
d6f94d91
LL
94static void batadv_v_ogm_schedule(struct batadv_hard_iface *hard_iface)
95{
96}
97
98static void batadv_v_ogm_emit(struct batadv_forw_packet *forw_packet)
99{
100}
101
261e264d
AQ
102/**
103 * batadv_v_orig_print_neigh - print neighbors for the originator table
104 * @orig_node: the orig_node for which the neighbors are printed
105 * @if_outgoing: outgoing interface for these entries
106 * @seq: debugfs table seq_file struct
107 *
108 * Must be called while holding an rcu lock.
109 */
110static void
111batadv_v_orig_print_neigh(struct batadv_orig_node *orig_node,
112 struct batadv_hard_iface *if_outgoing,
113 struct seq_file *seq)
114{
115 struct batadv_neigh_node *neigh_node;
116 struct batadv_neigh_ifinfo *n_ifinfo;
117
118 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
119 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
120 if (!n_ifinfo)
121 continue;
122
123 seq_printf(seq, " %pM (%9u.%1u)",
124 neigh_node->addr,
125 n_ifinfo->bat_v.throughput / 10,
126 n_ifinfo->bat_v.throughput % 10);
127
128 batadv_neigh_ifinfo_put(n_ifinfo);
129 }
130}
131
626d23e8
LL
132/**
133 * batadv_v_hardif_neigh_print - print a single ELP neighbour node
134 * @seq: neighbour table seq_file struct
135 * @hardif_neigh: hardif neighbour information
136 */
137static void
138batadv_v_hardif_neigh_print(struct seq_file *seq,
139 struct batadv_hardif_neigh_node *hardif_neigh)
140{
141 int last_secs, last_msecs;
142 u32 throughput;
143
144 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
145 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
146 throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
147
148 seq_printf(seq, "%pM %4i.%03is (%9u.%1u) [%10s]\n",
149 hardif_neigh->addr, last_secs, last_msecs, throughput / 10,
150 throughput % 10, hardif_neigh->if_incoming->net_dev->name);
151}
152
153/**
154 * batadv_v_neigh_print - print the single hop neighbour list
155 * @bat_priv: the bat priv with all the soft interface information
156 * @seq: neighbour table seq_file struct
157 */
158static void batadv_v_neigh_print(struct batadv_priv *bat_priv,
159 struct seq_file *seq)
160{
161 struct net_device *net_dev = (struct net_device *)seq->private;
162 struct batadv_hardif_neigh_node *hardif_neigh;
163 struct batadv_hard_iface *hard_iface;
164 int batman_count = 0;
165
925a6f37
AQ
166 seq_puts(seq,
167 " Neighbor last-seen ( throughput) [ IF]\n");
626d23e8
LL
168
169 rcu_read_lock();
170 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
171 if (hard_iface->soft_iface != net_dev)
172 continue;
173
174 hlist_for_each_entry_rcu(hardif_neigh,
175 &hard_iface->neigh_list, list) {
176 batadv_v_hardif_neigh_print(seq, hardif_neigh);
177 batman_count++;
178 }
179 }
180 rcu_read_unlock();
181
182 if (batman_count == 0)
183 seq_puts(seq, "No batman nodes in range ...\n");
184}
185
261e264d
AQ
186/**
187 * batadv_v_orig_print - print the originator table
188 * @bat_priv: the bat priv with all the soft interface information
189 * @seq: debugfs table seq_file struct
190 * @if_outgoing: the outgoing interface for which this should be printed
191 */
192static void batadv_v_orig_print(struct batadv_priv *bat_priv,
193 struct seq_file *seq,
194 struct batadv_hard_iface *if_outgoing)
195{
196 struct batadv_neigh_node *neigh_node;
197 struct batadv_hashtable *hash = bat_priv->orig_hash;
198 int last_seen_msecs, last_seen_secs;
199 struct batadv_orig_node *orig_node;
200 struct batadv_neigh_ifinfo *n_ifinfo;
201 unsigned long last_seen_jiffies;
202 struct hlist_head *head;
203 int batman_count = 0;
204 u32 i;
205
925a6f37
AQ
206 seq_puts(seq,
207 " Originator last-seen ( throughput) Nexthop [outgoingIF]: Potential nexthops ...\n");
261e264d
AQ
208
209 for (i = 0; i < hash->size; i++) {
210 head = &hash->table[i];
211
212 rcu_read_lock();
213 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
214 neigh_node = batadv_orig_router_get(orig_node,
215 if_outgoing);
216 if (!neigh_node)
217 continue;
218
219 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
220 if_outgoing);
221 if (!n_ifinfo)
222 goto next;
223
224 last_seen_jiffies = jiffies - orig_node->last_seen;
225 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
226 last_seen_secs = last_seen_msecs / 1000;
227 last_seen_msecs = last_seen_msecs % 1000;
228
229 seq_printf(seq, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
230 orig_node->orig, last_seen_secs,
231 last_seen_msecs,
232 n_ifinfo->bat_v.throughput / 10,
233 n_ifinfo->bat_v.throughput % 10,
234 neigh_node->addr,
235 neigh_node->if_incoming->net_dev->name);
236
237 batadv_v_orig_print_neigh(orig_node, if_outgoing, seq);
238 seq_puts(seq, "\n");
239 batman_count++;
240
241next:
242 batadv_neigh_node_put(neigh_node);
243 if (n_ifinfo)
244 batadv_neigh_ifinfo_put(n_ifinfo);
245 }
246 rcu_read_unlock();
247 }
248
249 if (batman_count == 0)
250 seq_puts(seq, "No batman nodes in range ...\n");
251}
252
97869060
AQ
253static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
254 struct batadv_hard_iface *if_outgoing1,
255 struct batadv_neigh_node *neigh2,
256 struct batadv_hard_iface *if_outgoing2)
257{
258 struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
71f9d27d 259 int ret = 0;
97869060
AQ
260
261 ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
71f9d27d
SE
262 if (WARN_ON(!ifinfo1))
263 goto err_ifinfo1;
264
97869060 265 ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
71f9d27d
SE
266 if (WARN_ON(!ifinfo2))
267 goto err_ifinfo2;
97869060 268
71f9d27d 269 ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
97869060 270
71f9d27d
SE
271 batadv_neigh_ifinfo_put(ifinfo2);
272err_ifinfo2:
273 batadv_neigh_ifinfo_put(ifinfo1);
274err_ifinfo1:
275 return ret;
97869060
AQ
276}
277
278static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
279 struct batadv_hard_iface *if_outgoing1,
280 struct batadv_neigh_node *neigh2,
281 struct batadv_hard_iface *if_outgoing2)
282{
283 struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
284 u32 threshold;
71f9d27d 285 bool ret = false;
97869060
AQ
286
287 ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
71f9d27d
SE
288 if (WARN_ON(!ifinfo1))
289 goto err_ifinfo1;
97869060 290
71f9d27d
SE
291 ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
292 if (WARN_ON(!ifinfo2))
293 goto err_ifinfo2;
a45e932a 294
97869060
AQ
295 threshold = ifinfo1->bat_v.throughput / 4;
296 threshold = ifinfo1->bat_v.throughput - threshold;
297
71f9d27d
SE
298 ret = ifinfo2->bat_v.throughput > threshold;
299
300 batadv_neigh_ifinfo_put(ifinfo2);
301err_ifinfo2:
302 batadv_neigh_ifinfo_put(ifinfo1);
303err_ifinfo1:
304 return ret;
97869060
AQ
305}
306
d6f94d91
LL
307static struct batadv_algo_ops batadv_batman_v __read_mostly = {
308 .name = "BATMAN_V",
b6cf5d49 309 .bat_iface_activate = batadv_v_iface_activate,
d6f94d91
LL
310 .bat_iface_enable = batadv_v_iface_enable,
311 .bat_iface_disable = batadv_v_iface_disable,
312 .bat_iface_update_mac = batadv_v_iface_update_mac,
313 .bat_primary_iface_set = batadv_v_primary_iface_set,
162bd64c 314 .bat_hardif_neigh_init = batadv_v_hardif_neigh_init,
d6f94d91
LL
315 .bat_ogm_emit = batadv_v_ogm_emit,
316 .bat_ogm_schedule = batadv_v_ogm_schedule,
261e264d 317 .bat_orig_print = batadv_v_orig_print,
97869060
AQ
318 .bat_neigh_cmp = batadv_v_neigh_cmp,
319 .bat_neigh_is_similar_or_better = batadv_v_neigh_is_sob,
626d23e8 320 .bat_neigh_print = batadv_v_neigh_print,
d6f94d91
LL
321};
322
0da00359
AQ
323/**
324 * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a
325 * mesh
326 * @bat_priv: the object representing the mesh interface to initialise
327 *
328 * Return: 0 on success or a negative error code otherwise
329 */
330int batadv_v_mesh_init(struct batadv_priv *bat_priv)
331{
332 return batadv_v_ogm_init(bat_priv);
333}
334
335/**
336 * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh
337 * @bat_priv: the object representing the mesh interface to free
338 */
339void batadv_v_mesh_free(struct batadv_priv *bat_priv)
340{
341 batadv_v_ogm_free(bat_priv);
342}
343
d6f94d91
LL
344/**
345 * batadv_v_init - B.A.T.M.A.N. V initialization function
346 *
347 * Description: Takes care of initializing all the subcomponents.
348 * It is invoked upon module load only.
349 *
350 * Return: 0 on success or a negative error code otherwise
351 */
352int __init batadv_v_init(void)
353{
162bd64c
LL
354 int ret;
355
356 /* B.A.T.M.A.N. V echo location protocol packet */
357 ret = batadv_recv_handler_register(BATADV_ELP,
358 batadv_v_elp_packet_recv);
359 if (ret < 0)
360 return ret;
361
0da00359
AQ
362 ret = batadv_recv_handler_register(BATADV_OGM2,
363 batadv_v_ogm_packet_recv);
364 if (ret < 0)
365 goto elp_unregister;
162bd64c 366
0da00359 367 ret = batadv_algo_register(&batadv_batman_v);
162bd64c 368 if (ret < 0)
0da00359
AQ
369 goto ogm_unregister;
370
371 return ret;
372
373ogm_unregister:
374 batadv_recv_handler_unregister(BATADV_OGM2);
375
376elp_unregister:
377 batadv_recv_handler_unregister(BATADV_ELP);
162bd64c
LL
378
379 return ret;
d6f94d91 380}
This page took 0.069521 seconds and 5 git commands to generate.