blob: 6df12a2e36052b7f8a07dea276565c362890863f [file] [log] [blame]
Simon Wunderliche19f9752014-01-04 18:04:25 +01001/* Copyright (C) 2009-2014 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
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
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "main.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010019#include "distributed-arp-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020#include "originator.h"
21#include "hash.h"
22#include "translation-table.h"
23#include "routing.h"
24#include "gateway_client.h"
25#include "hard-interface.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000026#include "soft-interface.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010027#include "bridge_loop_avoidance.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010028#include "network-coding.h"
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +020029#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Antonio Quartullidec05072012-11-10 11:00:32 +010031/* hash class keys */
32static struct lock_class_key batadv_orig_hash_lock_class_key;
33
Sven Eckelmann03fc7f82012-05-12 18:34:00 +020034static void batadv_purge_orig(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000035
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020036/* returns 1 if they are the same originator */
Antonio Quartullibbad0a52013-09-02 12:15:02 +020037int batadv_compare_orig(const struct hlist_node *node, const void *data2)
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020038{
Sven Eckelmann56303d32012-06-05 22:31:31 +020039 const void *data1 = container_of(node, struct batadv_orig_node,
40 hash_entry);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020041
dingtianhong323813e2013-12-26 19:40:39 +080042 return batadv_compare_eth(data1, data2);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020043}
44
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020045/**
46 * batadv_orig_node_vlan_get - get an orig_node_vlan object
47 * @orig_node: the originator serving the VLAN
48 * @vid: the VLAN identifier
49 *
50 * Returns the vlan object identified by vid and belonging to orig_node or NULL
51 * if it does not exist.
52 */
53struct batadv_orig_node_vlan *
54batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
55 unsigned short vid)
56{
57 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
58
59 rcu_read_lock();
60 list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
61 if (tmp->vid != vid)
62 continue;
63
64 if (!atomic_inc_not_zero(&tmp->refcount))
65 continue;
66
67 vlan = tmp;
68
69 break;
70 }
71 rcu_read_unlock();
72
73 return vlan;
74}
75
76/**
77 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
78 * object
79 * @orig_node: the originator serving the VLAN
80 * @vid: the VLAN identifier
81 *
82 * Returns NULL in case of failure or the vlan object identified by vid and
83 * belonging to orig_node otherwise. The object is created and added to the list
84 * if it does not exist.
85 *
86 * The object is returned with refcounter increased by 1.
87 */
88struct batadv_orig_node_vlan *
89batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
90 unsigned short vid)
91{
92 struct batadv_orig_node_vlan *vlan;
93
94 spin_lock_bh(&orig_node->vlan_list_lock);
95
96 /* first look if an object for this vid already exists */
97 vlan = batadv_orig_node_vlan_get(orig_node, vid);
98 if (vlan)
99 goto out;
100
101 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
102 if (!vlan)
103 goto out;
104
105 atomic_set(&vlan->refcount, 2);
106 vlan->vid = vid;
107
108 list_add_rcu(&vlan->list, &orig_node->vlan_list);
109
110out:
111 spin_unlock_bh(&orig_node->vlan_list_lock);
112
113 return vlan;
114}
115
116/**
117 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
118 * the originator-vlan object
119 * @orig_vlan: the originator-vlan object to release
120 */
121void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
122{
123 if (atomic_dec_and_test(&orig_vlan->refcount))
124 kfree_rcu(orig_vlan, rcu);
125}
126
Sven Eckelmann56303d32012-06-05 22:31:31 +0200127int batadv_originator_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128{
129 if (bat_priv->orig_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200130 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200132 bat_priv->orig_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133
134 if (!bat_priv->orig_hash)
135 goto err;
136
Antonio Quartullidec05072012-11-10 11:00:32 +0100137 batadv_hash_set_lock_class(bat_priv->orig_hash,
138 &batadv_orig_hash_lock_class_key);
139
Antonio Quartulli72414442012-12-25 13:14:37 +0100140 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
141 queue_delayed_work(batadv_event_workqueue,
142 &bat_priv->orig_work,
143 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
144
Sven Eckelmann5346c352012-05-05 13:27:28 +0200145 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146
147err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200148 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149}
150
Simon Wunderlich89652332013-11-13 19:14:46 +0100151/**
152 * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
153 * @rcu: rcu pointer of the neigh_ifinfo object
154 */
155static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu)
156{
157 struct batadv_neigh_ifinfo *neigh_ifinfo;
158
159 neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu);
160
161 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
162 batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing);
163
164 kfree(neigh_ifinfo);
165}
166
167/**
168 * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free
169 * the neigh_ifinfo (without rcu callback)
170 * @neigh_ifinfo: the neigh_ifinfo object to release
171 */
172static void
173batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo)
174{
175 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
176 batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu);
177}
178
179/**
180 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free
181 * the neigh_ifinfo
182 * @neigh_ifinfo: the neigh_ifinfo object to release
183 */
184void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
185{
186 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
187 call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu);
188}
189
190/**
191 * batadv_neigh_node_free_rcu - free the neigh_node
192 * @rcu: rcu pointer of the neigh_node
193 */
194static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
195{
196 struct hlist_node *node_tmp;
197 struct batadv_neigh_node *neigh_node;
198 struct batadv_neigh_ifinfo *neigh_ifinfo;
199
200 neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
201
202 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
203 &neigh_node->ifinfo_list, list) {
204 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo);
205 }
206 batadv_hardif_free_ref_now(neigh_node->if_incoming);
207
208 kfree(neigh_node);
209}
210
211/**
212 * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter
213 * and possibly free it (without rcu callback)
214 * @neigh_node: neigh neighbor to free
215 */
216static void
217batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node)
218{
219 if (atomic_dec_and_test(&neigh_node->refcount))
220 batadv_neigh_node_free_rcu(&neigh_node->rcu);
221}
222
223/**
224 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
225 * and possibly free it
226 * @neigh_node: neigh neighbor to free
227 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200228void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000229{
Marek Lindner44524fc2011-02-10 14:33:53 +0000230 if (atomic_dec_and_test(&neigh_node->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +0100231 call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000232}
233
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100234/**
235 * batadv_orig_node_get_router - router to the originator depending on iface
236 * @orig_node: the orig node for the router
237 * @if_outgoing: the interface where the payload packet has been received or
238 * the OGM should be sent to
239 *
240 * Returns the neighbor which should be router for this orig_node/iface.
241 *
242 * The object is returned with refcounter increased by 1.
243 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200244struct batadv_neigh_node *
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100245batadv_orig_router_get(struct batadv_orig_node *orig_node,
246 const struct batadv_hard_iface *if_outgoing)
Linus LĂĽssinge1a5382f2011-03-14 22:43:37 +0000247{
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100248 struct batadv_orig_ifinfo *orig_ifinfo;
249 struct batadv_neigh_node *router = NULL;
Linus LĂĽssinge1a5382f2011-03-14 22:43:37 +0000250
251 rcu_read_lock();
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100252 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
253 if (orig_ifinfo->if_outgoing != if_outgoing)
254 continue;
255
256 router = rcu_dereference(orig_ifinfo->router);
257 break;
258 }
Linus LĂĽssinge1a5382f2011-03-14 22:43:37 +0000259
260 if (router && !atomic_inc_not_zero(&router->refcount))
261 router = NULL;
262
263 rcu_read_unlock();
264 return router;
265}
266
Antonio Quartulli0538f752013-09-02 12:15:01 +0200267/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100268 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
269 * @orig_node: the orig node to be queried
270 * @if_outgoing: the interface for which the ifinfo should be acquired
271 *
272 * Returns the requested orig_ifinfo or NULL if not found.
273 *
274 * The object is returned with refcounter increased by 1.
275 */
276struct batadv_orig_ifinfo *
277batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
278 struct batadv_hard_iface *if_outgoing)
279{
280 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
281
282 rcu_read_lock();
283 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
284 list) {
285 if (tmp->if_outgoing != if_outgoing)
286 continue;
287
288 if (!atomic_inc_not_zero(&tmp->refcount))
289 continue;
290
291 orig_ifinfo = tmp;
292 break;
293 }
294 rcu_read_unlock();
295
296 return orig_ifinfo;
297}
298
299/**
300 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
301 * @orig_node: the orig node to be queried
302 * @if_outgoing: the interface for which the ifinfo should be acquired
303 *
304 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
305 * interface otherwise. The object is created and added to the list
306 * if it does not exist.
307 *
308 * The object is returned with refcounter increased by 1.
309 */
310struct batadv_orig_ifinfo *
311batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
312 struct batadv_hard_iface *if_outgoing)
313{
314 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
315 unsigned long reset_time;
316
317 spin_lock_bh(&orig_node->neigh_list_lock);
318
319 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
320 if (orig_ifinfo)
321 goto out;
322
323 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
324 if (!orig_ifinfo)
325 goto out;
326
327 if (if_outgoing != BATADV_IF_DEFAULT &&
328 !atomic_inc_not_zero(&if_outgoing->refcount)) {
329 kfree(orig_ifinfo);
330 orig_ifinfo = NULL;
331 goto out;
332 }
333
334 reset_time = jiffies - 1;
335 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
336 orig_ifinfo->batman_seqno_reset = reset_time;
337 orig_ifinfo->if_outgoing = if_outgoing;
338 INIT_HLIST_NODE(&orig_ifinfo->list);
339 atomic_set(&orig_ifinfo->refcount, 2);
340 hlist_add_head_rcu(&orig_ifinfo->list,
341 &orig_node->ifinfo_list);
342out:
343 spin_unlock_bh(&orig_node->neigh_list_lock);
344 return orig_ifinfo;
345}
346
347/**
Simon Wunderlich89652332013-11-13 19:14:46 +0100348 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
349 * @neigh_node: the neigh node to be queried
350 * @if_outgoing: the interface for which the ifinfo should be acquired
351 *
352 * The object is returned with refcounter increased by 1.
353 *
354 * Returns the requested neigh_ifinfo or NULL if not found
355 */
356struct batadv_neigh_ifinfo *
357batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
358 struct batadv_hard_iface *if_outgoing)
359{
360 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
361 *tmp_neigh_ifinfo;
362
363 rcu_read_lock();
364 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
365 list) {
366 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
367 continue;
368
369 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
370 continue;
371
372 neigh_ifinfo = tmp_neigh_ifinfo;
373 break;
374 }
375 rcu_read_unlock();
376
377 return neigh_ifinfo;
378}
379
380/**
381 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
382 * @neigh_node: the neigh node to be queried
383 * @if_outgoing: the interface for which the ifinfo should be acquired
384 *
385 * Returns NULL in case of failure or the neigh_ifinfo object for the
386 * if_outgoing interface otherwise. The object is created and added to the list
387 * if it does not exist.
388 *
389 * The object is returned with refcounter increased by 1.
390 */
391struct batadv_neigh_ifinfo *
392batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
393 struct batadv_hard_iface *if_outgoing)
394{
395 struct batadv_neigh_ifinfo *neigh_ifinfo;
396
397 spin_lock_bh(&neigh->ifinfo_lock);
398
399 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
400 if (neigh_ifinfo)
401 goto out;
402
403 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
404 if (!neigh_ifinfo)
405 goto out;
406
407 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
408 kfree(neigh_ifinfo);
409 neigh_ifinfo = NULL;
410 goto out;
411 }
412
413 INIT_HLIST_NODE(&neigh_ifinfo->list);
414 atomic_set(&neigh_ifinfo->refcount, 2);
415 neigh_ifinfo->if_outgoing = if_outgoing;
416
417 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
418
419out:
420 spin_unlock_bh(&neigh->ifinfo_lock);
421
422 return neigh_ifinfo;
423}
424
425/**
Antonio Quartulli0538f752013-09-02 12:15:01 +0200426 * batadv_neigh_node_new - create and init a new neigh_node object
427 * @hard_iface: the interface where the neighbour is connected to
428 * @neigh_addr: the mac address of the neighbour interface
429 * @orig_node: originator object representing the neighbour
430 *
431 * Allocates a new neigh_node object and initialises all the generic fields.
432 * Returns the new object or NULL on failure.
433 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200434struct batadv_neigh_node *
435batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200436 const uint8_t *neigh_addr,
437 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200439 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440
Sven Eckelmann704509b2011-05-14 23:14:54 +0200441 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800443 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444
Marek Lindner9591a792010-12-12 21:57:11 +0000445 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlich89652332013-11-13 19:14:46 +0100446 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
447 spin_lock_init(&neigh_node->ifinfo_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448
Marek Lindner7ae8b282012-03-01 15:35:21 +0800449 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
Antonio Quartulli0538f752013-09-02 12:15:01 +0200450 neigh_node->if_incoming = hard_iface;
451 neigh_node->orig_node = orig_node;
452
Marek Lindner1605d0d2011-02-18 12:28:11 +0000453 /* extra reference for return */
454 atomic_set(&neigh_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455
Marek Lindner7ae8b282012-03-01 15:35:21 +0800456out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457 return neigh_node;
458}
459
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100460/**
461 * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
462 * @rcu: rcu pointer of the orig_ifinfo object
463 */
464static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu)
465{
466 struct batadv_orig_ifinfo *orig_ifinfo;
467
468 orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu);
469
470 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
471 batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing);
472
473 kfree(orig_ifinfo);
474}
475
476/**
477 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
478 * the orig_ifinfo (without rcu callback)
479 * @orig_ifinfo: the orig_ifinfo object to release
480 */
481static void
482batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo)
483{
484 if (atomic_dec_and_test(&orig_ifinfo->refcount))
485 batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu);
486}
487
488/**
489 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
490 * the orig_ifinfo
491 * @orig_ifinfo: the orig_ifinfo object to release
492 */
493void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
494{
495 if (atomic_dec_and_test(&orig_ifinfo->refcount))
496 call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu);
497}
498
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200499static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800501 struct hlist_node *node_tmp;
Simon Wunderlichf6c8b712013-11-13 19:14:45 +0100502 struct batadv_neigh_node *neigh_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200503 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100504 struct batadv_orig_ifinfo *orig_ifinfo;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000505
Sven Eckelmann56303d32012-06-05 22:31:31 +0200506 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507
Marek Lindnerf987ed62010-12-12 21:57:12 +0000508 spin_lock_bh(&orig_node->neigh_list_lock);
509
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800511 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +0000512 &orig_node->neigh_list, list) {
Marek Lindnerf987ed62010-12-12 21:57:12 +0000513 hlist_del_rcu(&neigh_node->list);
Simon Wunderlich89652332013-11-13 19:14:46 +0100514 batadv_neigh_node_free_ref_now(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515 }
516
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100517 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
518 &orig_node->ifinfo_list, list) {
519 hlist_del_rcu(&orig_ifinfo->list);
520 batadv_orig_ifinfo_free_ref_now(orig_ifinfo);
521 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000522 spin_unlock_bh(&orig_node->neigh_list_lock);
523
Martin Hundebølld56b1702013-01-25 11:12:39 +0100524 /* Free nc_nodes */
525 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
526
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200527 batadv_frag_purge_orig(orig_node, NULL);
528
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200529 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, -1,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200530 "originator timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200532 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
533 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
534
Antonio Quartullia73105b2011-04-27 14:27:44 +0200535 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536 kfree(orig_node);
537}
538
Linus LĂĽssing72822222013-04-15 21:43:29 +0800539/**
540 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
541 * schedule an rcu callback for freeing it
542 * @orig_node: the orig node to free
543 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200544void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000545{
546 if (atomic_dec_and_test(&orig_node->refcount))
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200547 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000548}
549
Linus LĂĽssing72822222013-04-15 21:43:29 +0800550/**
551 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
552 * possibly free it (without rcu callback)
553 * @orig_node: the orig node to free
554 */
555void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
556{
557 if (atomic_dec_and_test(&orig_node->refcount))
558 batadv_orig_node_free_rcu(&orig_node->rcu);
559}
560
Sven Eckelmann56303d32012-06-05 22:31:31 +0200561void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200563 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800564 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000565 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000566 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200567 struct batadv_orig_node *orig_node;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200568 uint32_t i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000569
570 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571 return;
572
573 cancel_delayed_work_sync(&bat_priv->orig_work);
574
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000576
577 for (i = 0; i < hash->size; i++) {
578 head = &hash->table[i];
579 list_lock = &hash->list_locks[i];
580
581 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800582 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000583 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800584 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200585 batadv_orig_node_free_ref(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000586 }
587 spin_unlock_bh(list_lock);
588 }
589
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200590 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591}
592
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200593/**
594 * batadv_orig_node_new - creates a new orig_node
595 * @bat_priv: the bat priv with all the soft interface information
596 * @addr: the mac address of the originator
597 *
598 * Creates a new originator object and initialise all the generic fields.
599 * The new object is not added to the originator list.
600 * Returns the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200601 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200602struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200603 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200605 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200606 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200607 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200608 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200610 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
611 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612
Sven Eckelmann704509b2011-05-14 23:14:54 +0200613 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614 if (!orig_node)
615 return NULL;
616
Marek Lindner9591a792010-12-12 21:57:11 +0000617 INIT_HLIST_HEAD(&orig_node->neigh_list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200618 INIT_LIST_HEAD(&orig_node->vlan_list);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100619 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000620 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000621 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200622 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +0200623 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200624 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000625
Martin Hundebølld56b1702013-01-25 11:12:39 +0100626 batadv_nc_init_orig(orig_node);
627
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000628 /* extra reference for return */
629 atomic_set(&orig_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630
Antonio Quartulli17071572011-11-07 16:36:40 +0100631 orig_node->tt_initialised = false;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000632 orig_node->bat_priv = bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633 memcpy(orig_node->orig, addr, ETH_ALEN);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100634 batadv_dat_init_orig_node_addr(orig_node);
Antonio Quartullic8c991b2011-07-07 01:40:57 +0200635 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200636 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200637 orig_node->tt_buff_len = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200638 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
639 orig_node->bcast_seqno_reset = reset_time;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200641 /* create a vlan object for the "untagged" LAN */
642 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
643 if (!vlan)
644 goto free_orig_node;
645 /* batadv_orig_node_vlan_new() increases the refcounter.
646 * Immediately release vlan since it is not needed anymore in this
647 * context
648 */
649 batadv_orig_node_vlan_free_ref(vlan);
650
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200651 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
652 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
653 spin_lock_init(&orig_node->fragments[i].lock);
654 orig_node->fragments[i].size = 0;
655 }
656
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000657 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658free_orig_node:
659 kfree(orig_node);
660 return NULL;
661}
662
Simon Wunderlich89652332013-11-13 19:14:46 +0100663/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100664 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
665 * @bat_priv: the bat priv with all the soft interface information
666 * @orig_node: orig node which is to be checked
667 *
668 * Returns true if any ifinfo entry was purged, false otherwise.
669 */
670static bool
671batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
672 struct batadv_orig_node *orig_node)
673{
674 struct batadv_orig_ifinfo *orig_ifinfo;
675 struct batadv_hard_iface *if_outgoing;
676 struct hlist_node *node_tmp;
677 bool ifinfo_purged = false;
678
679 spin_lock_bh(&orig_node->neigh_list_lock);
680
681 /* for all ifinfo objects for this originator */
682 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
683 &orig_node->ifinfo_list, list) {
684 if_outgoing = orig_ifinfo->if_outgoing;
685
686 /* always keep the default interface */
687 if (if_outgoing == BATADV_IF_DEFAULT)
688 continue;
689
690 /* don't purge if the interface is not (going) down */
691 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
692 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
693 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
694 continue;
695
696 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
697 "router/ifinfo purge: originator %pM, iface: %s\n",
698 orig_node->orig, if_outgoing->net_dev->name);
699
700 ifinfo_purged = true;
701
702 hlist_del_rcu(&orig_ifinfo->list);
703 batadv_orig_ifinfo_free_ref(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100704 if (orig_node->last_bonding_candidate == orig_ifinfo) {
705 orig_node->last_bonding_candidate = NULL;
706 batadv_orig_ifinfo_free_ref(orig_ifinfo);
707 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100708 }
709
710 spin_unlock_bh(&orig_node->neigh_list_lock);
711
712 return ifinfo_purged;
713}
714
715
716/**
Simon Wunderlich89652332013-11-13 19:14:46 +0100717 * batadv_purge_orig_neighbors - purges neighbors from originator
718 * @bat_priv: the bat priv with all the soft interface information
719 * @orig_node: orig node which is to be checked
720 *
721 * Returns true if any neighbor was purged, false otherwise
722 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200723static bool
724batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
Simon Wunderlich89652332013-11-13 19:14:46 +0100725 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800727 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200728 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +0800730 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200731 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000732
Marek Lindnerf987ed62010-12-12 21:57:12 +0000733 spin_lock_bh(&orig_node->neigh_list_lock);
734
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800736 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +0000737 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200738 last_seen = neigh_node->last_seen;
739 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200741 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200742 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
743 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
744 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200745 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
746 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
747 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200748 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200749 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
750 orig_node->orig, neigh_node->addr,
751 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200753 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200754 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
755 orig_node->orig, neigh_node->addr,
756 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000757
758 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +0000759
Marek Lindnerf987ed62010-12-12 21:57:12 +0000760 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200761 batadv_neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000762 }
763 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000764
765 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000766 return neigh_purged;
767}
768
Simon Wunderlich89652332013-11-13 19:14:46 +0100769/**
770 * batadv_find_best_neighbor - finds the best neighbor after purging
771 * @bat_priv: the bat priv with all the soft interface information
772 * @orig_node: orig node which is to be checked
773 * @if_outgoing: the interface for which the metric should be compared
774 *
775 * Returns the current best neighbor, with refcount increased.
776 */
777static struct batadv_neigh_node *
778batadv_find_best_neighbor(struct batadv_priv *bat_priv,
779 struct batadv_orig_node *orig_node,
780 struct batadv_hard_iface *if_outgoing)
781{
782 struct batadv_neigh_node *best = NULL, *neigh;
783 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
784
785 rcu_read_lock();
786 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
787 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
788 best, if_outgoing) <= 0))
789 continue;
790
791 if (!atomic_inc_not_zero(&neigh->refcount))
792 continue;
793
794 if (best)
795 batadv_neigh_node_free_ref(best);
796
797 best = neigh;
798 }
799 rcu_read_unlock();
800
801 return best;
802}
803
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100804/**
805 * batadv_purge_orig_node - purges obsolete information from an orig_node
806 * @bat_priv: the bat priv with all the soft interface information
807 * @orig_node: orig node which is to be checked
808 *
809 * This function checks if the orig_node or substructures of it have become
810 * obsolete, and purges this information if that's the case.
811 *
812 * Returns true if the orig_node is to be removed, false otherwise.
813 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200814static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
815 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200817 struct batadv_neigh_node *best_neigh_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100818 struct batadv_hard_iface *hard_iface;
819 bool changed;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200821 if (batadv_has_timed_out(orig_node->last_seen,
822 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200823 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200824 "Originator timeout: originator %pM, last_seen %u\n",
825 orig_node->orig,
826 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000828 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100829 changed = batadv_purge_orig_ifinfo(bat_priv, orig_node);
830 changed = changed || batadv_purge_orig_neighbors(bat_priv, orig_node);
831
832 if (!changed)
Simon Wunderlich89652332013-11-13 19:14:46 +0100833 return false;
834
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100835 /* first for NULL ... */
Simon Wunderlich89652332013-11-13 19:14:46 +0100836 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
837 BATADV_IF_DEFAULT);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100838 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
839 best_neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +0100840 if (best_neigh_node)
841 batadv_neigh_node_free_ref(best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100843 /* ... then for all other interfaces. */
844 rcu_read_lock();
845 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
846 if (hard_iface->if_status != BATADV_IF_ACTIVE)
847 continue;
848
849 if (hard_iface->soft_iface != bat_priv->soft_iface)
850 continue;
851
852 best_neigh_node = batadv_find_best_neighbor(bat_priv,
853 orig_node,
854 hard_iface);
855 batadv_update_route(bat_priv, orig_node, hard_iface,
856 best_neigh_node);
857 if (best_neigh_node)
858 batadv_neigh_node_free_ref(best_neigh_node);
859 }
860 rcu_read_unlock();
861
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000862 return false;
863}
864
Sven Eckelmann56303d32012-06-05 22:31:31 +0200865static void _batadv_purge_orig(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000866{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200867 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800868 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000870 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200871 struct batadv_orig_node *orig_node;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200872 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000873
874 if (!hash)
875 return;
876
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000877 /* for all origins... */
878 for (i = 0; i < hash->size; i++) {
879 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000880 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000881
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000882 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800883 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000884 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200885 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +0800886 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800887 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200888 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000889 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200891
892 batadv_frag_purge_orig(orig_node,
893 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000895 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000896 }
897
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200898 batadv_gw_node_purge(bat_priv);
899 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000900}
901
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200902static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000903{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200904 struct delayed_work *delayed_work;
905 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000906
Sven Eckelmann56303d32012-06-05 22:31:31 +0200907 delayed_work = container_of(work, struct delayed_work, work);
908 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200909 _batadv_purge_orig(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +0100910 queue_delayed_work(batadv_event_workqueue,
911 &bat_priv->orig_work,
912 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913}
914
Sven Eckelmann56303d32012-06-05 22:31:31 +0200915void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000916{
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200917 _batadv_purge_orig(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000918}
919
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200920int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000921{
922 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200923 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200924 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000925
Marek Lindner30da63a2012-08-03 17:15:46 +0200926 primary_if = batadv_seq_print_text_primary_if_get(seq);
927 if (!primary_if)
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200928 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000929
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200930 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200931 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200932 primary_if->net_dev->dev_addr, net_dev->name,
933 bat_priv->bat_algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000934
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200935 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000936
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200937 if (!bat_priv->bat_algo_ops->bat_orig_print) {
938 seq_puts(seq,
939 "No printing function for this routing protocol\n");
940 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000941 }
942
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +0100943 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
944 BATADV_IF_DEFAULT);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000945
Marek Lindner30da63a2012-08-03 17:15:46 +0200946 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000947}
948
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +0100949/**
950 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
951 * outgoing interface
952 * @seq: debugfs table seq_file struct
953 * @offset: not used
954 *
955 * Returns 0
956 */
957int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
958{
959 struct net_device *net_dev = (struct net_device *)seq->private;
960 struct batadv_hard_iface *hard_iface;
961 struct batadv_priv *bat_priv;
962
963 hard_iface = batadv_hardif_get_by_netdev(net_dev);
964
965 if (!hard_iface || !hard_iface->soft_iface) {
966 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
967 goto out;
968 }
969
970 bat_priv = netdev_priv(hard_iface->soft_iface);
971 if (!bat_priv->bat_algo_ops->bat_orig_print) {
972 seq_puts(seq,
973 "No printing function for this routing protocol\n");
974 goto out;
975 }
976
977 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
978 seq_puts(seq, "Interface not active\n");
979 goto out;
980 }
981
982 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
983 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
984 hard_iface->net_dev->dev_addr,
985 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
986
987 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
988
989out:
990 batadv_hardif_free_ref(hard_iface);
991 return 0;
992}
993
Sven Eckelmann56303d32012-06-05 22:31:31 +0200994int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
995 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000996{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200997 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200998 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200999 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001000 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001001 struct batadv_orig_node *orig_node;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001002 uint32_t i;
1003 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001004
1005 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001006 * if_num
1007 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001008 for (i = 0; i < hash->size; i++) {
1009 head = &hash->table[i];
1010
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001011 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001012 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001013 ret = 0;
1014 if (bao->bat_orig_add_if)
1015 ret = bao->bat_orig_add_if(orig_node,
1016 max_if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001017 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001018 goto err;
1019 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001020 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001021 }
1022
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023 return 0;
1024
1025err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001026 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001027 return -ENOMEM;
1028}
1029
Sven Eckelmann56303d32012-06-05 22:31:31 +02001030int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1031 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001032{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001033 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001034 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001036 struct batadv_hard_iface *hard_iface_tmp;
1037 struct batadv_orig_node *orig_node;
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001038 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001039 uint32_t i;
1040 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001041
1042 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001043 * if_num
1044 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001045 for (i = 0; i < hash->size; i++) {
1046 head = &hash->table[i];
1047
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001048 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001049 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001050 ret = 0;
1051 if (bao->bat_orig_del_if)
1052 ret = bao->bat_orig_del_if(orig_node,
1053 max_if_num,
1054 hard_iface->if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001055 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001056 goto err;
1057 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001058 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001059 }
1060
1061 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1062 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001063 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001064 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001065 continue;
1066
Marek Lindnere6c10f42011-02-18 12:33:20 +00001067 if (hard_iface == hard_iface_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001068 continue;
1069
Marek Lindnere6c10f42011-02-18 12:33:20 +00001070 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001071 continue;
1072
Marek Lindnere6c10f42011-02-18 12:33:20 +00001073 if (hard_iface_tmp->if_num > hard_iface->if_num)
1074 hard_iface_tmp->if_num--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001075 }
1076 rcu_read_unlock();
1077
Marek Lindnere6c10f42011-02-18 12:33:20 +00001078 hard_iface->if_num = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079 return 0;
1080
1081err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001082 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083 return -ENOMEM;
1084}