batman-adv: remove bogus comment
[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"
610bfc6b 31#include "fragmentation.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
610bfc6b
MH
149 batadv_frag_purge_orig(orig_node, NULL);
150
08c36d3e
SE
151 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
152 "originator timed out");
c6c8fea2 153
a73105b8 154 kfree(orig_node->tt_buff);
c6c8fea2
SE
155 kfree(orig_node->bcast_own);
156 kfree(orig_node->bcast_own_sum);
157 kfree(orig_node);
158}
159
72822225
LL
160/**
161 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
162 * schedule an rcu callback for freeing it
163 * @orig_node: the orig node to free
164 */
56303d34 165void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
7b36e8ee
ML
166{
167 if (atomic_dec_and_test(&orig_node->refcount))
03fc7f86 168 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
7b36e8ee
ML
169}
170
72822225
LL
171/**
172 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
173 * possibly free it (without rcu callback)
174 * @orig_node: the orig node to free
175 */
176void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
177{
178 if (atomic_dec_and_test(&orig_node->refcount))
179 batadv_orig_node_free_rcu(&orig_node->rcu);
180}
181
56303d34 182void batadv_originator_free(struct batadv_priv *bat_priv)
c6c8fea2 183{
5bf74e9c 184 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 185 struct hlist_node *node_tmp;
16b1aba8 186 struct hlist_head *head;
16b1aba8 187 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 188 struct batadv_orig_node *orig_node;
c90681b8 189 uint32_t i;
16b1aba8
ML
190
191 if (!hash)
c6c8fea2
SE
192 return;
193
194 cancel_delayed_work_sync(&bat_priv->orig_work);
195
c6c8fea2 196 bat_priv->orig_hash = NULL;
16b1aba8
ML
197
198 for (i = 0; i < hash->size; i++) {
199 head = &hash->table[i];
200 list_lock = &hash->list_locks[i];
201
202 spin_lock_bh(list_lock);
b67bfe0d 203 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 204 head, hash_entry) {
b67bfe0d 205 hlist_del_rcu(&orig_node->hash_entry);
7d211efc 206 batadv_orig_node_free_ref(orig_node);
16b1aba8
ML
207 }
208 spin_unlock_bh(list_lock);
209 }
210
1a8eaf07 211 batadv_hash_destroy(hash);
c6c8fea2
SE
212}
213
214/* this function finds or creates an originator entry for the given
9cfc7bd6
SE
215 * address if it does not exits
216 */
56303d34
SE
217struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
218 const uint8_t *addr)
c6c8fea2 219{
56303d34 220 struct batadv_orig_node *orig_node;
610bfc6b 221 int size, i;
c6c8fea2 222 int hash_added;
42d0b044 223 unsigned long reset_time;
c6c8fea2 224
da641193 225 orig_node = batadv_orig_hash_find(bat_priv, addr);
7aadf889 226 if (orig_node)
c6c8fea2
SE
227 return orig_node;
228
39c75a51
SE
229 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
230 "Creating new originator: %pM\n", addr);
c6c8fea2 231
704509b8 232 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
c6c8fea2
SE
233 if (!orig_node)
234 return NULL;
235
9591a79f 236 INIT_HLIST_HEAD(&orig_node->neigh_list);
a4c135c5 237 INIT_LIST_HEAD(&orig_node->bond_list);
2ae2daf6 238 spin_lock_init(&orig_node->ogm_cnt_lock);
f3e0008f 239 spin_lock_init(&orig_node->bcast_seqno_lock);
f987ed6e 240 spin_lock_init(&orig_node->neigh_list_lock);
a73105b8 241 spin_lock_init(&orig_node->tt_buff_lock);
7b36e8ee 242
d56b1705
MH
243 batadv_nc_init_orig(orig_node);
244
7b36e8ee
ML
245 /* extra reference for return */
246 atomic_set(&orig_node->refcount, 2);
c6c8fea2 247
17071578 248 orig_node->tt_initialised = false;
16b1aba8 249 orig_node->bat_priv = bat_priv;
c6c8fea2 250 memcpy(orig_node->orig, addr, ETH_ALEN);
785ea114 251 batadv_dat_init_orig_node_addr(orig_node);
c6c8fea2 252 orig_node->router = NULL;
c8c991bf
AQ
253 orig_node->tt_crc = 0;
254 atomic_set(&orig_node->last_ttvn, 0);
2dafb49d 255 orig_node->tt_buff = NULL;
a73105b8
AQ
256 orig_node->tt_buff_len = 0;
257 atomic_set(&orig_node->tt_size, 0);
42d0b044
SE
258 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
259 orig_node->bcast_seqno_reset = reset_time;
260 orig_node->batman_seqno_reset = reset_time;
c6c8fea2 261
a4c135c5
SW
262 atomic_set(&orig_node->bond_candidates, 0);
263
42d0b044 264 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
c6c8fea2
SE
265
266 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
267 if (!orig_node->bcast_own)
268 goto free_orig_node;
269
270 size = bat_priv->num_ifaces * sizeof(uint8_t);
271 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
272
610bfc6b
MH
273 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
274 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
275 spin_lock_init(&orig_node->fragments[i].lock);
276 orig_node->fragments[i].size = 0;
277 }
278
c6c8fea2
SE
279 if (!orig_node->bcast_own_sum)
280 goto free_bcast_own;
281
03fc7f86 282 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
da641193 283 batadv_choose_orig, orig_node,
c0a55929 284 &orig_node->hash_entry);
1a1f37d9 285 if (hash_added != 0)
c6c8fea2
SE
286 goto free_bcast_own_sum;
287
288 return orig_node;
289free_bcast_own_sum:
290 kfree(orig_node->bcast_own_sum);
291free_bcast_own:
292 kfree(orig_node->bcast_own);
293free_orig_node:
294 kfree(orig_node);
295 return NULL;
296}
297
56303d34
SE
298static bool
299batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
300 struct batadv_orig_node *orig_node,
301 struct batadv_neigh_node **best_neigh_node)
c6c8fea2 302{
b67bfe0d 303 struct hlist_node *node_tmp;
56303d34 304 struct batadv_neigh_node *neigh_node;
c6c8fea2 305 bool neigh_purged = false;
0b0094e0 306 unsigned long last_seen;
56303d34 307 struct batadv_hard_iface *if_incoming;
c6c8fea2
SE
308
309 *best_neigh_node = NULL;
310
f987ed6e
ML
311 spin_lock_bh(&orig_node->neigh_list_lock);
312
c6c8fea2 313 /* for all neighbors towards this originator ... */
b67bfe0d 314 hlist_for_each_entry_safe(neigh_node, node_tmp,
9591a79f 315 &orig_node->neigh_list, list) {
1eda58bf
SE
316 last_seen = neigh_node->last_seen;
317 if_incoming = neigh_node->if_incoming;
318
42d0b044 319 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
e9a4f295
SE
320 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
321 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
322 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
e9a4f295
SE
323 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
324 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
325 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
39c75a51 326 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
327 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
328 orig_node->orig, neigh_node->addr,
329 if_incoming->net_dev->name);
c6c8fea2 330 else
39c75a51 331 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
332 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
333 orig_node->orig, neigh_node->addr,
334 jiffies_to_msecs(last_seen));
c6c8fea2
SE
335
336 neigh_purged = true;
9591a79f 337
f987ed6e 338 hlist_del_rcu(&neigh_node->list);
30d3c511 339 batadv_bonding_candidate_del(orig_node, neigh_node);
7d211efc 340 batadv_neigh_node_free_ref(neigh_node);
c6c8fea2
SE
341 } else {
342 if ((!*best_neigh_node) ||
343 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
344 *best_neigh_node = neigh_node;
345 }
346 }
f987ed6e
ML
347
348 spin_unlock_bh(&orig_node->neigh_list_lock);
c6c8fea2
SE
349 return neigh_purged;
350}
351
56303d34
SE
352static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
353 struct batadv_orig_node *orig_node)
c6c8fea2 354{
56303d34 355 struct batadv_neigh_node *best_neigh_node;
c6c8fea2 356
42d0b044
SE
357 if (batadv_has_timed_out(orig_node->last_seen,
358 2 * BATADV_PURGE_TIMEOUT)) {
39c75a51 359 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
360 "Originator timeout: originator %pM, last_seen %u\n",
361 orig_node->orig,
362 jiffies_to_msecs(orig_node->last_seen));
c6c8fea2
SE
363 return true;
364 } else {
03fc7f86
SE
365 if (batadv_purge_orig_neighbors(bat_priv, orig_node,
366 &best_neigh_node))
30d3c511
SE
367 batadv_update_route(bat_priv, orig_node,
368 best_neigh_node);
c6c8fea2
SE
369 }
370
371 return false;
372}
373
56303d34 374static void _batadv_purge_orig(struct batadv_priv *bat_priv)
c6c8fea2 375{
5bf74e9c 376 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 377 struct hlist_node *node_tmp;
c6c8fea2 378 struct hlist_head *head;
fb778ea1 379 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 380 struct batadv_orig_node *orig_node;
c90681b8 381 uint32_t i;
c6c8fea2
SE
382
383 if (!hash)
384 return;
385
c6c8fea2
SE
386 /* for all origins... */
387 for (i = 0; i < hash->size; i++) {
388 head = &hash->table[i];
fb778ea1 389 list_lock = &hash->list_locks[i];
c6c8fea2 390
fb778ea1 391 spin_lock_bh(list_lock);
b67bfe0d 392 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 393 head, hash_entry) {
03fc7f86 394 if (batadv_purge_orig_node(bat_priv, orig_node)) {
414254e3 395 batadv_gw_node_delete(bat_priv, orig_node);
b67bfe0d 396 hlist_del_rcu(&orig_node->hash_entry);
7d211efc 397 batadv_orig_node_free_ref(orig_node);
fb778ea1 398 continue;
c6c8fea2 399 }
610bfc6b
MH
400
401 batadv_frag_purge_orig(orig_node,
402 batadv_frag_check_entry);
c6c8fea2 403 }
fb778ea1 404 spin_unlock_bh(list_lock);
c6c8fea2
SE
405 }
406
7cf06bc6
SE
407 batadv_gw_node_purge(bat_priv);
408 batadv_gw_election(bat_priv);
c6c8fea2
SE
409}
410
03fc7f86 411static void batadv_purge_orig(struct work_struct *work)
c6c8fea2 412{
56303d34
SE
413 struct delayed_work *delayed_work;
414 struct batadv_priv *bat_priv;
c6c8fea2 415
56303d34
SE
416 delayed_work = container_of(work, struct delayed_work, work);
417 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
03fc7f86 418 _batadv_purge_orig(bat_priv);
72414442
AQ
419 queue_delayed_work(batadv_event_workqueue,
420 &bat_priv->orig_work,
421 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
c6c8fea2
SE
422}
423
56303d34 424void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
c6c8fea2 425{
03fc7f86 426 _batadv_purge_orig(bat_priv);
c6c8fea2
SE
427}
428
7d211efc 429int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
430{
431 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 432 struct batadv_priv *bat_priv = netdev_priv(net_dev);
5bf74e9c 433 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 434 struct hlist_head *head;
56303d34
SE
435 struct batadv_hard_iface *primary_if;
436 struct batadv_orig_node *orig_node;
437 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
c6c8fea2
SE
438 int batman_count = 0;
439 int last_seen_secs;
440 int last_seen_msecs;
0aca2369 441 unsigned long last_seen_jiffies;
c90681b8 442 uint32_t i;
32ae9b22 443
30da63a6
ML
444 primary_if = batadv_seq_print_text_primary_if_get(seq);
445 if (!primary_if)
32ae9b22 446 goto out;
c6c8fea2 447
44c4349a 448 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
42d0b044 449 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
32ae9b22 450 primary_if->net_dev->dev_addr, net_dev->name);
c6c8fea2 451 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
42d0b044
SE
452 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
453 "Nexthop", "outgoingIF", "Potential nexthops");
c6c8fea2 454
c6c8fea2
SE
455 for (i = 0; i < hash->size; i++) {
456 head = &hash->table[i];
457
fb778ea1 458 rcu_read_lock();
b67bfe0d 459 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
7d211efc 460 neigh_node = batadv_orig_node_get_router(orig_node);
e1a5382f 461 if (!neigh_node)
c6c8fea2
SE
462 continue;
463
e1a5382f
LL
464 if (neigh_node->tq_avg == 0)
465 goto next;
c6c8fea2 466
0aca2369
SE
467 last_seen_jiffies = jiffies - orig_node->last_seen;
468 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
469 last_seen_secs = last_seen_msecs / 1000;
470 last_seen_msecs = last_seen_msecs % 1000;
c6c8fea2 471
c6c8fea2
SE
472 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
473 orig_node->orig, last_seen_secs,
474 last_seen_msecs, neigh_node->tq_avg,
475 neigh_node->addr,
476 neigh_node->if_incoming->net_dev->name);
477
b67bfe0d 478 hlist_for_each_entry_rcu(neigh_node_tmp,
f987ed6e 479 &orig_node->neigh_list, list) {
e1a5382f
LL
480 seq_printf(seq, " %pM (%3i)",
481 neigh_node_tmp->addr,
482 neigh_node_tmp->tq_avg);
c6c8fea2
SE
483 }
484
0c814653 485 seq_puts(seq, "\n");
c6c8fea2 486 batman_count++;
e1a5382f
LL
487
488next:
7d211efc 489 batadv_neigh_node_free_ref(neigh_node);
c6c8fea2 490 }
fb778ea1 491 rcu_read_unlock();
c6c8fea2
SE
492 }
493
e1a5382f 494 if (batman_count == 0)
0c814653 495 seq_puts(seq, "No batman nodes in range ...\n");
c6c8fea2 496
32ae9b22
ML
497out:
498 if (primary_if)
e5d89254 499 batadv_hardif_free_ref(primary_if);
30da63a6 500 return 0;
c6c8fea2
SE
501}
502
56303d34
SE
503static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
504 int max_if_num)
c6c8fea2
SE
505{
506 void *data_ptr;
42d0b044 507 size_t data_size, old_size;
c6c8fea2 508
42d0b044
SE
509 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
510 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
511 data_ptr = kmalloc(data_size, GFP_ATOMIC);
320f422f 512 if (!data_ptr)
5346c35e 513 return -ENOMEM;
c6c8fea2 514
42d0b044 515 memcpy(data_ptr, orig_node->bcast_own, old_size);
c6c8fea2
SE
516 kfree(orig_node->bcast_own);
517 orig_node->bcast_own = data_ptr;
518
519 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
320f422f 520 if (!data_ptr)
5346c35e 521 return -ENOMEM;
c6c8fea2
SE
522
523 memcpy(data_ptr, orig_node->bcast_own_sum,
524 (max_if_num - 1) * sizeof(uint8_t));
525 kfree(orig_node->bcast_own_sum);
526 orig_node->bcast_own_sum = data_ptr;
527
528 return 0;
529}
530
56303d34
SE
531int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
532 int max_if_num)
c6c8fea2 533{
56303d34 534 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
5bf74e9c 535 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 536 struct hlist_head *head;
56303d34 537 struct batadv_orig_node *orig_node;
c90681b8
AQ
538 uint32_t i;
539 int ret;
c6c8fea2
SE
540
541 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
542 * if_num
543 */
c6c8fea2
SE
544 for (i = 0; i < hash->size; i++) {
545 head = &hash->table[i];
546
fb778ea1 547 rcu_read_lock();
b67bfe0d 548 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2ae2daf6 549 spin_lock_bh(&orig_node->ogm_cnt_lock);
03fc7f86 550 ret = batadv_orig_node_add_if(orig_node, max_if_num);
2ae2daf6
ML
551 spin_unlock_bh(&orig_node->ogm_cnt_lock);
552
5346c35e 553 if (ret == -ENOMEM)
c6c8fea2
SE
554 goto err;
555 }
fb778ea1 556 rcu_read_unlock();
c6c8fea2
SE
557 }
558
c6c8fea2
SE
559 return 0;
560
561err:
fb778ea1 562 rcu_read_unlock();
c6c8fea2
SE
563 return -ENOMEM;
564}
565
56303d34 566static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
03fc7f86 567 int max_if_num, int del_if_num)
c6c8fea2
SE
568{
569 void *data_ptr = NULL;
570 int chunk_size;
571
572 /* last interface was removed */
573 if (max_if_num == 0)
574 goto free_bcast_own;
575
42d0b044 576 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
c6c8fea2 577 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
320f422f 578 if (!data_ptr)
5346c35e 579 return -ENOMEM;
c6c8fea2
SE
580
581 /* copy first part */
582 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
583
584 /* copy second part */
38e3c5f0 585 memcpy((char *)data_ptr + del_if_num * chunk_size,
c6c8fea2
SE
586 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
587 (max_if_num - del_if_num) * chunk_size);
588
589free_bcast_own:
590 kfree(orig_node->bcast_own);
591 orig_node->bcast_own = data_ptr;
592
593 if (max_if_num == 0)
594 goto free_own_sum;
595
596 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
320f422f 597 if (!data_ptr)
5346c35e 598 return -ENOMEM;
c6c8fea2
SE
599
600 memcpy(data_ptr, orig_node->bcast_own_sum,
601 del_if_num * sizeof(uint8_t));
602
38e3c5f0 603 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
c6c8fea2
SE
604 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
605 (max_if_num - del_if_num) * sizeof(uint8_t));
606
607free_own_sum:
608 kfree(orig_node->bcast_own_sum);
609 orig_node->bcast_own_sum = data_ptr;
610
611 return 0;
612}
613
56303d34
SE
614int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
615 int max_if_num)
c6c8fea2 616{
56303d34 617 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
5bf74e9c 618 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 619 struct hlist_head *head;
56303d34
SE
620 struct batadv_hard_iface *hard_iface_tmp;
621 struct batadv_orig_node *orig_node;
c90681b8
AQ
622 uint32_t i;
623 int ret;
c6c8fea2
SE
624
625 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
626 * if_num
627 */
c6c8fea2
SE
628 for (i = 0; i < hash->size; i++) {
629 head = &hash->table[i];
630
fb778ea1 631 rcu_read_lock();
b67bfe0d 632 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2ae2daf6 633 spin_lock_bh(&orig_node->ogm_cnt_lock);
03fc7f86
SE
634 ret = batadv_orig_node_del_if(orig_node, max_if_num,
635 hard_iface->if_num);
2ae2daf6 636 spin_unlock_bh(&orig_node->ogm_cnt_lock);
c6c8fea2 637
5346c35e 638 if (ret == -ENOMEM)
c6c8fea2
SE
639 goto err;
640 }
fb778ea1 641 rcu_read_unlock();
c6c8fea2
SE
642 }
643
644 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
645 rcu_read_lock();
3193e8fd 646 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
e9a4f295 647 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
c6c8fea2
SE
648 continue;
649
e6c10f43 650 if (hard_iface == hard_iface_tmp)
c6c8fea2
SE
651 continue;
652
e6c10f43 653 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
c6c8fea2
SE
654 continue;
655
e6c10f43
ML
656 if (hard_iface_tmp->if_num > hard_iface->if_num)
657 hard_iface_tmp->if_num--;
c6c8fea2
SE
658 }
659 rcu_read_unlock();
660
e6c10f43 661 hard_iface->if_num = -1;
c6c8fea2
SE
662 return 0;
663
664err:
fb778ea1 665 rcu_read_unlock();
c6c8fea2
SE
666 return -ENOMEM;
667}
This page took 0.222433 seconds and 5 git commands to generate.