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