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