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