batman-adv: tvlv - basic infrastructure
[linux-2.6-block.git] / net / batman-adv / originator.c
CommitLineData
0b873931 1/* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors:
c6c8fea2
SE
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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
c6c8fea2 20#include "main.h"
785ea114 21#include "distributed-arp-table.h"
c6c8fea2
SE
22#include "originator.h"
23#include "hash.h"
24#include "translation-table.h"
25#include "routing.h"
26#include "gateway_client.h"
27#include "hard-interface.h"
28#include "unicast.h"
29#include "soft-interface.h"
23721387 30#include "bridge_loop_avoidance.h"
d56b1705 31#include "network-coding.h"
c6c8fea2 32
dec05074
AQ
33/* hash class keys */
34static struct lock_class_key batadv_orig_hash_lock_class_key;
35
03fc7f86 36static void batadv_purge_orig(struct work_struct *work);
c6c8fea2 37
b8e2dd13 38/* returns 1 if they are the same originator */
03fc7f86 39static int batadv_compare_orig(const struct hlist_node *node, const void *data2)
b8e2dd13 40{
56303d34
SE
41 const void *data1 = container_of(node, struct batadv_orig_node,
42 hash_entry);
b8e2dd13
SE
43
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
56303d34 47int batadv_originator_init(struct batadv_priv *bat_priv)
c6c8fea2
SE
48{
49 if (bat_priv->orig_hash)
5346c35e 50 return 0;
c6c8fea2 51
1a8eaf07 52 bat_priv->orig_hash = batadv_hash_new(1024);
c6c8fea2
SE
53
54 if (!bat_priv->orig_hash)
55 goto err;
56
dec05074
AQ
57 batadv_hash_set_lock_class(bat_priv->orig_hash,
58 &batadv_orig_hash_lock_class_key);
59
72414442
AQ
60 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
61 queue_delayed_work(batadv_event_workqueue,
62 &bat_priv->orig_work,
63 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
64
5346c35e 65 return 0;
c6c8fea2
SE
66
67err:
5346c35e 68 return -ENOMEM;
c6c8fea2
SE
69}
70
56303d34 71void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
a4c135c5 72{
44524fcd 73 if (atomic_dec_and_test(&neigh_node->refcount))
ae179ae4 74 kfree_rcu(neigh_node, rcu);
a4c135c5
SW
75}
76
e1a5382f 77/* increases the refcounter of a found router */
56303d34
SE
78struct batadv_neigh_node *
79batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
e1a5382f 80{
56303d34 81 struct batadv_neigh_node *router;
e1a5382f
LL
82
83 rcu_read_lock();
84 router = rcu_dereference(orig_node->router);
85
86 if (router && !atomic_inc_not_zero(&router->refcount))
87 router = NULL;
88
89 rcu_read_unlock();
90 return router;
91}
92
56303d34
SE
93struct batadv_neigh_node *
94batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
863dd7a8 95 const uint8_t *neigh_addr)
c6c8fea2 96{
56303d34
SE
97 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
98 struct batadv_neigh_node *neigh_node;
c6c8fea2 99
704509b8 100 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
c6c8fea2 101 if (!neigh_node)
7ae8b285 102 goto out;
c6c8fea2 103
9591a79f 104 INIT_HLIST_NODE(&neigh_node->list);
c6c8fea2 105
7ae8b285 106 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
e3b0d0de 107 spin_lock_init(&neigh_node->lq_update_lock);
1605d0d6
ML
108
109 /* extra reference for return */
110 atomic_set(&neigh_node->refcount, 2);
c6c8fea2 111
39c75a51 112 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
d1dc3073
AQ
113 "Creating new neighbor %pM on interface %s\n", neigh_addr,
114 hard_iface->net_dev->name);
7ae8b285
ML
115
116out:
c6c8fea2
SE
117 return neigh_node;
118}
119
03fc7f86 120static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
c6c8fea2 121{
b67bfe0d 122 struct hlist_node *node_tmp;
56303d34
SE
123 struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
124 struct batadv_orig_node *orig_node;
16b1aba8 125
56303d34 126 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
c6c8fea2 127
f987ed6e
ML
128 spin_lock_bh(&orig_node->neigh_list_lock);
129
a4c135c5
SW
130 /* for all bonding members ... */
131 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
132 &orig_node->bond_list, bonding_list) {
133 list_del_rcu(&neigh_node->bonding_list);
7d211efc 134 batadv_neigh_node_free_ref(neigh_node);
a4c135c5
SW
135 }
136
c6c8fea2 137 /* for all neighbors towards this originator ... */
b67bfe0d 138 hlist_for_each_entry_safe(neigh_node, node_tmp,
9591a79f 139 &orig_node->neigh_list, list) {
f987ed6e 140 hlist_del_rcu(&neigh_node->list);
7d211efc 141 batadv_neigh_node_free_ref(neigh_node);
c6c8fea2
SE
142 }
143
f987ed6e
ML
144 spin_unlock_bh(&orig_node->neigh_list_lock);
145
d56b1705
MH
146 /* Free nc_nodes */
147 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
148
88ed1e77 149 batadv_frag_list_free(&orig_node->frag_list);
08c36d3e
SE
150 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
151 "originator timed out");
c6c8fea2 152
a73105b8 153 kfree(orig_node->tt_buff);
c6c8fea2
SE
154 kfree(orig_node->bcast_own);
155 kfree(orig_node->bcast_own_sum);
156 kfree(orig_node);
157}
158
72822225
LL
159/**
160 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
161 * schedule an rcu callback for freeing it
162 * @orig_node: the orig node to free
163 */
56303d34 164void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
7b36e8ee
ML
165{
166 if (atomic_dec_and_test(&orig_node->refcount))
03fc7f86 167 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
7b36e8ee
ML
168}
169
72822225
LL
170/**
171 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
172 * possibly free it (without rcu callback)
173 * @orig_node: the orig node to free
174 */
175void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
176{
177 if (atomic_dec_and_test(&orig_node->refcount))
178 batadv_orig_node_free_rcu(&orig_node->rcu);
179}
180
56303d34 181void batadv_originator_free(struct batadv_priv *bat_priv)
c6c8fea2 182{
5bf74e9c 183 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 184 struct hlist_node *node_tmp;
16b1aba8 185 struct hlist_head *head;
16b1aba8 186 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 187 struct batadv_orig_node *orig_node;
c90681b8 188 uint32_t i;
16b1aba8
ML
189
190 if (!hash)
c6c8fea2
SE
191 return;
192
193 cancel_delayed_work_sync(&bat_priv->orig_work);
194
c6c8fea2 195 bat_priv->orig_hash = NULL;
16b1aba8
ML
196
197 for (i = 0; i < hash->size; i++) {
198 head = &hash->table[i];
199 list_lock = &hash->list_locks[i];
200
201 spin_lock_bh(list_lock);
b67bfe0d 202 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 203 head, hash_entry) {
b67bfe0d 204 hlist_del_rcu(&orig_node->hash_entry);
7d211efc 205 batadv_orig_node_free_ref(orig_node);
16b1aba8
ML
206 }
207 spin_unlock_bh(list_lock);
208 }
209
1a8eaf07 210 batadv_hash_destroy(hash);
c6c8fea2
SE
211}
212
213/* this function finds or creates an originator entry for the given
9cfc7bd6
SE
214 * address if it does not exits
215 */
56303d34
SE
216struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
217 const uint8_t *addr)
c6c8fea2 218{
56303d34 219 struct batadv_orig_node *orig_node;
c6c8fea2
SE
220 int size;
221 int hash_added;
42d0b044 222 unsigned long reset_time;
c6c8fea2 223
da641193 224 orig_node = batadv_orig_hash_find(bat_priv, addr);
7aadf889 225 if (orig_node)
c6c8fea2
SE
226 return orig_node;
227
39c75a51
SE
228 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
229 "Creating new originator: %pM\n", addr);
c6c8fea2 230
704509b8 231 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
c6c8fea2
SE
232 if (!orig_node)
233 return NULL;
234
9591a79f 235 INIT_HLIST_HEAD(&orig_node->neigh_list);
a4c135c5 236 INIT_LIST_HEAD(&orig_node->bond_list);
2ae2daf6 237 spin_lock_init(&orig_node->ogm_cnt_lock);
f3e0008f 238 spin_lock_init(&orig_node->bcast_seqno_lock);
f987ed6e 239 spin_lock_init(&orig_node->neigh_list_lock);
a73105b8 240 spin_lock_init(&orig_node->tt_buff_lock);
7b36e8ee 241
d56b1705
MH
242 batadv_nc_init_orig(orig_node);
243
7b36e8ee
ML
244 /* extra reference for return */
245 atomic_set(&orig_node->refcount, 2);
c6c8fea2 246
17071578 247 orig_node->tt_initialised = false;
16b1aba8 248 orig_node->bat_priv = bat_priv;
c6c8fea2 249 memcpy(orig_node->orig, addr, ETH_ALEN);
785ea114 250 batadv_dat_init_orig_node_addr(orig_node);
c6c8fea2 251 orig_node->router = NULL;
c8c991bf
AQ
252 orig_node->tt_crc = 0;
253 atomic_set(&orig_node->last_ttvn, 0);
2dafb49d 254 orig_node->tt_buff = NULL;
a73105b8
AQ
255 orig_node->tt_buff_len = 0;
256 atomic_set(&orig_node->tt_size, 0);
42d0b044
SE
257 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
258 orig_node->bcast_seqno_reset = reset_time;
259 orig_node->batman_seqno_reset = reset_time;
c6c8fea2 260
a4c135c5
SW
261 atomic_set(&orig_node->bond_candidates, 0);
262
42d0b044 263 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
c6c8fea2
SE
264
265 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
266 if (!orig_node->bcast_own)
267 goto free_orig_node;
268
269 size = bat_priv->num_ifaces * sizeof(uint8_t);
270 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
271
272 INIT_LIST_HEAD(&orig_node->frag_list);
273 orig_node->last_frag_packet = 0;
274
275 if (!orig_node->bcast_own_sum)
276 goto free_bcast_own;
277
03fc7f86 278 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
da641193 279 batadv_choose_orig, orig_node,
c0a55929 280 &orig_node->hash_entry);
1a1f37d9 281 if (hash_added != 0)
c6c8fea2
SE
282 goto free_bcast_own_sum;
283
284 return orig_node;
285free_bcast_own_sum:
286 kfree(orig_node->bcast_own_sum);
287free_bcast_own:
288 kfree(orig_node->bcast_own);
289free_orig_node:
290 kfree(orig_node);
291 return NULL;
292}
293
56303d34
SE
294static bool
295batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
296 struct batadv_orig_node *orig_node,
297 struct batadv_neigh_node **best_neigh_node)
c6c8fea2 298{
b67bfe0d 299 struct hlist_node *node_tmp;
56303d34 300 struct batadv_neigh_node *neigh_node;
c6c8fea2 301 bool neigh_purged = false;
0b0094e0 302 unsigned long last_seen;
56303d34 303 struct batadv_hard_iface *if_incoming;
c6c8fea2
SE
304
305 *best_neigh_node = NULL;
306
f987ed6e
ML
307 spin_lock_bh(&orig_node->neigh_list_lock);
308
c6c8fea2 309 /* for all neighbors towards this originator ... */
b67bfe0d 310 hlist_for_each_entry_safe(neigh_node, node_tmp,
9591a79f 311 &orig_node->neigh_list, list) {
1eda58bf
SE
312 last_seen = neigh_node->last_seen;
313 if_incoming = neigh_node->if_incoming;
314
42d0b044 315 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
e9a4f295
SE
316 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
317 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
318 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
e9a4f295
SE
319 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
320 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
321 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
39c75a51 322 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
323 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
324 orig_node->orig, neigh_node->addr,
325 if_incoming->net_dev->name);
c6c8fea2 326 else
39c75a51 327 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
328 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
329 orig_node->orig, neigh_node->addr,
330 jiffies_to_msecs(last_seen));
c6c8fea2
SE
331
332 neigh_purged = true;
9591a79f 333
f987ed6e 334 hlist_del_rcu(&neigh_node->list);
30d3c511 335 batadv_bonding_candidate_del(orig_node, neigh_node);
7d211efc 336 batadv_neigh_node_free_ref(neigh_node);
c6c8fea2
SE
337 } else {
338 if ((!*best_neigh_node) ||
339 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
340 *best_neigh_node = neigh_node;
341 }
342 }
f987ed6e
ML
343
344 spin_unlock_bh(&orig_node->neigh_list_lock);
c6c8fea2
SE
345 return neigh_purged;
346}
347
56303d34
SE
348static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
349 struct batadv_orig_node *orig_node)
c6c8fea2 350{
56303d34 351 struct batadv_neigh_node *best_neigh_node;
c6c8fea2 352
42d0b044
SE
353 if (batadv_has_timed_out(orig_node->last_seen,
354 2 * BATADV_PURGE_TIMEOUT)) {
39c75a51 355 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
356 "Originator timeout: originator %pM, last_seen %u\n",
357 orig_node->orig,
358 jiffies_to_msecs(orig_node->last_seen));
c6c8fea2
SE
359 return true;
360 } else {
03fc7f86
SE
361 if (batadv_purge_orig_neighbors(bat_priv, orig_node,
362 &best_neigh_node))
30d3c511
SE
363 batadv_update_route(bat_priv, orig_node,
364 best_neigh_node);
c6c8fea2
SE
365 }
366
367 return false;
368}
369
56303d34 370static void _batadv_purge_orig(struct batadv_priv *bat_priv)
c6c8fea2 371{
5bf74e9c 372 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 373 struct hlist_node *node_tmp;
c6c8fea2 374 struct hlist_head *head;
fb778ea1 375 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 376 struct batadv_orig_node *orig_node;
c90681b8 377 uint32_t i;
c6c8fea2
SE
378
379 if (!hash)
380 return;
381
c6c8fea2
SE
382 /* for all origins... */
383 for (i = 0; i < hash->size; i++) {
384 head = &hash->table[i];
fb778ea1 385 list_lock = &hash->list_locks[i];
c6c8fea2 386
fb778ea1 387 spin_lock_bh(list_lock);
b67bfe0d 388 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 389 head, hash_entry) {
03fc7f86 390 if (batadv_purge_orig_node(bat_priv, orig_node)) {
c6c8fea2 391 if (orig_node->gw_flags)
7cf06bc6
SE
392 batadv_gw_node_delete(bat_priv,
393 orig_node);
b67bfe0d 394 hlist_del_rcu(&orig_node->hash_entry);
7d211efc 395 batadv_orig_node_free_ref(orig_node);
fb778ea1 396 continue;
c6c8fea2
SE
397 }
398
1eda58bf 399 if (batadv_has_timed_out(orig_node->last_frag_packet,
4d5d2db8 400 BATADV_FRAG_TIMEOUT))
88ed1e77 401 batadv_frag_list_free(&orig_node->frag_list);
c6c8fea2 402 }
fb778ea1 403 spin_unlock_bh(list_lock);
c6c8fea2
SE
404 }
405
7cf06bc6
SE
406 batadv_gw_node_purge(bat_priv);
407 batadv_gw_election(bat_priv);
c6c8fea2
SE
408}
409
03fc7f86 410static void batadv_purge_orig(struct work_struct *work)
c6c8fea2 411{
56303d34
SE
412 struct delayed_work *delayed_work;
413 struct batadv_priv *bat_priv;
c6c8fea2 414
56303d34
SE
415 delayed_work = container_of(work, struct delayed_work, work);
416 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
03fc7f86 417 _batadv_purge_orig(bat_priv);
72414442
AQ
418 queue_delayed_work(batadv_event_workqueue,
419 &bat_priv->orig_work,
420 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
c6c8fea2
SE
421}
422
56303d34 423void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
c6c8fea2 424{
03fc7f86 425 _batadv_purge_orig(bat_priv);
c6c8fea2
SE
426}
427
7d211efc 428int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
429{
430 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 431 struct batadv_priv *bat_priv = netdev_priv(net_dev);
5bf74e9c 432 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 433 struct hlist_head *head;
56303d34
SE
434 struct batadv_hard_iface *primary_if;
435 struct batadv_orig_node *orig_node;
436 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
c6c8fea2
SE
437 int batman_count = 0;
438 int last_seen_secs;
439 int last_seen_msecs;
0aca2369 440 unsigned long last_seen_jiffies;
c90681b8 441 uint32_t i;
32ae9b22 442
30da63a6
ML
443 primary_if = batadv_seq_print_text_primary_if_get(seq);
444 if (!primary_if)
32ae9b22 445 goto out;
c6c8fea2 446
44c4349a 447 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
42d0b044 448 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
32ae9b22 449 primary_if->net_dev->dev_addr, net_dev->name);
c6c8fea2 450 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
42d0b044
SE
451 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
452 "Nexthop", "outgoingIF", "Potential nexthops");
c6c8fea2 453
c6c8fea2
SE
454 for (i = 0; i < hash->size; i++) {
455 head = &hash->table[i];
456
fb778ea1 457 rcu_read_lock();
b67bfe0d 458 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
7d211efc 459 neigh_node = batadv_orig_node_get_router(orig_node);
e1a5382f 460 if (!neigh_node)
c6c8fea2
SE
461 continue;
462
e1a5382f
LL
463 if (neigh_node->tq_avg == 0)
464 goto next;
c6c8fea2 465
0aca2369
SE
466 last_seen_jiffies = jiffies - orig_node->last_seen;
467 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
468 last_seen_secs = last_seen_msecs / 1000;
469 last_seen_msecs = last_seen_msecs % 1000;
c6c8fea2 470
c6c8fea2
SE
471 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
472 orig_node->orig, last_seen_secs,
473 last_seen_msecs, neigh_node->tq_avg,
474 neigh_node->addr,
475 neigh_node->if_incoming->net_dev->name);
476
b67bfe0d 477 hlist_for_each_entry_rcu(neigh_node_tmp,
f987ed6e 478 &orig_node->neigh_list, list) {
e1a5382f
LL
479 seq_printf(seq, " %pM (%3i)",
480 neigh_node_tmp->addr,
481 neigh_node_tmp->tq_avg);
c6c8fea2
SE
482 }
483
0c814653 484 seq_puts(seq, "\n");
c6c8fea2 485 batman_count++;
e1a5382f
LL
486
487next:
7d211efc 488 batadv_neigh_node_free_ref(neigh_node);
c6c8fea2 489 }
fb778ea1 490 rcu_read_unlock();
c6c8fea2
SE
491 }
492
e1a5382f 493 if (batman_count == 0)
0c814653 494 seq_puts(seq, "No batman nodes in range ...\n");
c6c8fea2 495
32ae9b22
ML
496out:
497 if (primary_if)
e5d89254 498 batadv_hardif_free_ref(primary_if);
30da63a6 499 return 0;
c6c8fea2
SE
500}
501
56303d34
SE
502static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
503 int max_if_num)
c6c8fea2
SE
504{
505 void *data_ptr;
42d0b044 506 size_t data_size, old_size;
c6c8fea2 507
42d0b044
SE
508 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
509 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
510 data_ptr = kmalloc(data_size, GFP_ATOMIC);
320f422f 511 if (!data_ptr)
5346c35e 512 return -ENOMEM;
c6c8fea2 513
42d0b044 514 memcpy(data_ptr, orig_node->bcast_own, old_size);
c6c8fea2
SE
515 kfree(orig_node->bcast_own);
516 orig_node->bcast_own = data_ptr;
517
518 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
320f422f 519 if (!data_ptr)
5346c35e 520 return -ENOMEM;
c6c8fea2
SE
521
522 memcpy(data_ptr, orig_node->bcast_own_sum,
523 (max_if_num - 1) * sizeof(uint8_t));
524 kfree(orig_node->bcast_own_sum);
525 orig_node->bcast_own_sum = data_ptr;
526
527 return 0;
528}
529
56303d34
SE
530int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
531 int max_if_num)
c6c8fea2 532{
56303d34 533 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
5bf74e9c 534 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 535 struct hlist_head *head;
56303d34 536 struct batadv_orig_node *orig_node;
c90681b8
AQ
537 uint32_t i;
538 int ret;
c6c8fea2
SE
539
540 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
541 * if_num
542 */
c6c8fea2
SE
543 for (i = 0; i < hash->size; i++) {
544 head = &hash->table[i];
545
fb778ea1 546 rcu_read_lock();
b67bfe0d 547 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2ae2daf6 548 spin_lock_bh(&orig_node->ogm_cnt_lock);
03fc7f86 549 ret = batadv_orig_node_add_if(orig_node, max_if_num);
2ae2daf6
ML
550 spin_unlock_bh(&orig_node->ogm_cnt_lock);
551
5346c35e 552 if (ret == -ENOMEM)
c6c8fea2
SE
553 goto err;
554 }
fb778ea1 555 rcu_read_unlock();
c6c8fea2
SE
556 }
557
c6c8fea2
SE
558 return 0;
559
560err:
fb778ea1 561 rcu_read_unlock();
c6c8fea2
SE
562 return -ENOMEM;
563}
564
56303d34 565static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
03fc7f86 566 int max_if_num, int del_if_num)
c6c8fea2
SE
567{
568 void *data_ptr = NULL;
569 int chunk_size;
570
571 /* last interface was removed */
572 if (max_if_num == 0)
573 goto free_bcast_own;
574
42d0b044 575 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
c6c8fea2 576 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
320f422f 577 if (!data_ptr)
5346c35e 578 return -ENOMEM;
c6c8fea2
SE
579
580 /* copy first part */
581 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
582
583 /* copy second part */
38e3c5f0 584 memcpy((char *)data_ptr + del_if_num * chunk_size,
c6c8fea2
SE
585 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
586 (max_if_num - del_if_num) * chunk_size);
587
588free_bcast_own:
589 kfree(orig_node->bcast_own);
590 orig_node->bcast_own = data_ptr;
591
592 if (max_if_num == 0)
593 goto free_own_sum;
594
595 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
320f422f 596 if (!data_ptr)
5346c35e 597 return -ENOMEM;
c6c8fea2
SE
598
599 memcpy(data_ptr, orig_node->bcast_own_sum,
600 del_if_num * sizeof(uint8_t));
601
38e3c5f0 602 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
c6c8fea2
SE
603 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
604 (max_if_num - del_if_num) * sizeof(uint8_t));
605
606free_own_sum:
607 kfree(orig_node->bcast_own_sum);
608 orig_node->bcast_own_sum = data_ptr;
609
610 return 0;
611}
612
56303d34
SE
613int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
614 int max_if_num)
c6c8fea2 615{
56303d34 616 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
5bf74e9c 617 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 618 struct hlist_head *head;
56303d34
SE
619 struct batadv_hard_iface *hard_iface_tmp;
620 struct batadv_orig_node *orig_node;
c90681b8
AQ
621 uint32_t i;
622 int ret;
c6c8fea2
SE
623
624 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
625 * if_num
626 */
c6c8fea2
SE
627 for (i = 0; i < hash->size; i++) {
628 head = &hash->table[i];
629
fb778ea1 630 rcu_read_lock();
b67bfe0d 631 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2ae2daf6 632 spin_lock_bh(&orig_node->ogm_cnt_lock);
03fc7f86
SE
633 ret = batadv_orig_node_del_if(orig_node, max_if_num,
634 hard_iface->if_num);
2ae2daf6 635 spin_unlock_bh(&orig_node->ogm_cnt_lock);
c6c8fea2 636
5346c35e 637 if (ret == -ENOMEM)
c6c8fea2
SE
638 goto err;
639 }
fb778ea1 640 rcu_read_unlock();
c6c8fea2
SE
641 }
642
643 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
644 rcu_read_lock();
3193e8fd 645 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
e9a4f295 646 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
c6c8fea2
SE
647 continue;
648
e6c10f43 649 if (hard_iface == hard_iface_tmp)
c6c8fea2
SE
650 continue;
651
e6c10f43 652 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
c6c8fea2
SE
653 continue;
654
e6c10f43
ML
655 if (hard_iface_tmp->if_num > hard_iface->if_num)
656 hard_iface_tmp->if_num--;
c6c8fea2
SE
657 }
658 rcu_read_unlock();
659
e6c10f43 660 hard_iface->if_num = -1;
c6c8fea2
SE
661 return 0;
662
663err:
fb778ea1 664 rcu_read_unlock();
c6c8fea2
SE
665 return -ENOMEM;
666}