blob: fcdb0b7fe5862fe3b923d57390760f0f3d2dd950 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2009-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22/* increase the reference counter for this originator */
23
24#include "main.h"
25#include "originator.h"
26#include "hash.h"
27#include "translation-table.h"
28#include "routing.h"
29#include "gateway_client.h"
30#include "hard-interface.h"
31#include "unicast.h"
32#include "soft-interface.h"
33
34static void purge_orig(struct work_struct *work);
35
36static void start_purge_timer(struct bat_priv *bat_priv)
37{
38 INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
39 queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
40}
41
42int originator_init(struct bat_priv *bat_priv)
43{
44 if (bat_priv->orig_hash)
45 return 1;
46
47 spin_lock_bh(&bat_priv->orig_hash_lock);
48 bat_priv->orig_hash = hash_new(1024);
49
50 if (!bat_priv->orig_hash)
51 goto err;
52
53 spin_unlock_bh(&bat_priv->orig_hash_lock);
54 start_purge_timer(bat_priv);
55 return 1;
56
57err:
58 spin_unlock_bh(&bat_priv->orig_hash_lock);
59 return 0;
60}
61
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000062void neigh_node_free_ref(struct kref *refcount)
63{
64 struct neigh_node *neigh_node;
65
66 neigh_node = container_of(refcount, struct neigh_node, refcount);
67 kfree(neigh_node);
68}
69
Marek Lindnerf987ed62010-12-12 21:57:12 +000070static void neigh_node_free_rcu(struct rcu_head *rcu)
71{
72 struct neigh_node *neigh_node;
73
74 neigh_node = container_of(rcu, struct neigh_node, rcu);
75 kref_put(&neigh_node->refcount, neigh_node_free_ref);
76}
77
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000078struct neigh_node *create_neighbor(struct orig_node *orig_node,
79 struct orig_node *orig_neigh_node,
80 uint8_t *neigh,
81 struct batman_if *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082{
83 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
84 struct neigh_node *neigh_node;
85
86 bat_dbg(DBG_BATMAN, bat_priv,
87 "Creating new last-hop neighbor of originator\n");
88
89 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
90 if (!neigh_node)
91 return NULL;
92
Marek Lindner9591a792010-12-12 21:57:11 +000093 INIT_HLIST_NODE(&neigh_node->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000094
95 memcpy(neigh_node->addr, neigh, ETH_ALEN);
96 neigh_node->orig_node = orig_neigh_node;
97 neigh_node->if_incoming = if_incoming;
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000098 kref_init(&neigh_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099
Marek Lindnerf987ed62010-12-12 21:57:12 +0000100 spin_lock_bh(&orig_node->neigh_list_lock);
101 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
102 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000103 return neigh_node;
104}
105
Marek Lindner16b1aba2011-01-19 20:01:42 +0000106void orig_node_free_ref(struct kref *refcount)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000107{
Marek Lindner9591a792010-12-12 21:57:11 +0000108 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000109 struct neigh_node *neigh_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000110 struct orig_node *orig_node;
111
112 orig_node = container_of(refcount, struct orig_node, refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113
Marek Lindnerf987ed62010-12-12 21:57:12 +0000114 spin_lock_bh(&orig_node->neigh_list_lock);
115
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116 /* for all neighbors towards this originator ... */
Marek Lindner9591a792010-12-12 21:57:11 +0000117 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
118 &orig_node->neigh_list, list) {
Marek Lindnerf987ed62010-12-12 21:57:12 +0000119 hlist_del_rcu(&neigh_node->list);
120 call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000121 }
122
Marek Lindnerf987ed62010-12-12 21:57:12 +0000123 spin_unlock_bh(&orig_node->neigh_list_lock);
124
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125 frag_list_free(&orig_node->frag_list);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000126 hna_global_del_orig(orig_node->bat_priv, orig_node,
127 "originator timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128
129 kfree(orig_node->bcast_own);
130 kfree(orig_node->bcast_own_sum);
131 kfree(orig_node);
132}
133
134void originator_free(struct bat_priv *bat_priv)
135{
Marek Lindner16b1aba2011-01-19 20:01:42 +0000136 struct hashtable_t *hash = bat_priv->orig_hash;
137 struct hlist_node *walk, *safe;
138 struct hlist_head *head;
139 struct element_t *bucket;
140 spinlock_t *list_lock; /* spinlock to protect write access */
141 struct orig_node *orig_node;
142 int i;
143
144 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000145 return;
146
147 cancel_delayed_work_sync(&bat_priv->orig_work);
148
149 spin_lock_bh(&bat_priv->orig_hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000151
152 for (i = 0; i < hash->size; i++) {
153 head = &hash->table[i];
154 list_lock = &hash->list_locks[i];
155
156 spin_lock_bh(list_lock);
157 hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
158 orig_node = bucket->data;
159
160 hlist_del_rcu(walk);
161 call_rcu(&bucket->rcu, bucket_free_rcu);
162 kref_put(&orig_node->refcount, orig_node_free_ref);
163 }
164 spin_unlock_bh(list_lock);
165 }
166
167 hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168 spin_unlock_bh(&bat_priv->orig_hash_lock);
169}
170
Marek Lindner16b1aba2011-01-19 20:01:42 +0000171static void bucket_free_orig_rcu(struct rcu_head *rcu)
172{
173 struct element_t *bucket;
174 struct orig_node *orig_node;
175
176 bucket = container_of(rcu, struct element_t, rcu);
177 orig_node = bucket->data;
178
179 kref_put(&orig_node->refcount, orig_node_free_ref);
180 kfree(bucket);
181}
182
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183/* this function finds or creates an originator entry for the given
184 * address if it does not exits */
185struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
186{
187 struct orig_node *orig_node;
188 int size;
189 int hash_added;
190
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000191 rcu_read_lock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
193 compare_orig, choose_orig,
194 addr));
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000195 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196
Marek Lindner16b1aba2011-01-19 20:01:42 +0000197 if (orig_node) {
198 kref_get(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199 return orig_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000200 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201
202 bat_dbg(DBG_BATMAN, bat_priv,
203 "Creating new originator: %pM\n", addr);
204
205 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
206 if (!orig_node)
207 return NULL;
208
Marek Lindner9591a792010-12-12 21:57:11 +0000209 INIT_HLIST_HEAD(&orig_node->neigh_list);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000210 spin_lock_init(&orig_node->neigh_list_lock);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000211 kref_init(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212
Marek Lindner16b1aba2011-01-19 20:01:42 +0000213 orig_node->bat_priv = bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214 memcpy(orig_node->orig, addr, ETH_ALEN);
215 orig_node->router = NULL;
216 orig_node->hna_buff = NULL;
217 orig_node->bcast_seqno_reset = jiffies - 1
218 - msecs_to_jiffies(RESET_PROTECTION_MS);
219 orig_node->batman_seqno_reset = jiffies - 1
220 - msecs_to_jiffies(RESET_PROTECTION_MS);
221
222 size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
223
224 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
225 if (!orig_node->bcast_own)
226 goto free_orig_node;
227
228 size = bat_priv->num_ifaces * sizeof(uint8_t);
229 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
230
231 INIT_LIST_HEAD(&orig_node->frag_list);
232 orig_node->last_frag_packet = 0;
233
234 if (!orig_node->bcast_own_sum)
235 goto free_bcast_own;
236
237 hash_added = hash_add(bat_priv->orig_hash, compare_orig, choose_orig,
238 orig_node);
239 if (hash_added < 0)
240 goto free_bcast_own_sum;
241
Marek Lindner16b1aba2011-01-19 20:01:42 +0000242 /* extra reference for return */
243 kref_get(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244 return orig_node;
245free_bcast_own_sum:
246 kfree(orig_node->bcast_own_sum);
247free_bcast_own:
248 kfree(orig_node->bcast_own);
249free_orig_node:
250 kfree(orig_node);
251 return NULL;
252}
253
254static bool purge_orig_neighbors(struct bat_priv *bat_priv,
255 struct orig_node *orig_node,
256 struct neigh_node **best_neigh_node)
257{
Marek Lindner9591a792010-12-12 21:57:11 +0000258 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259 struct neigh_node *neigh_node;
260 bool neigh_purged = false;
261
262 *best_neigh_node = NULL;
263
Marek Lindnerf987ed62010-12-12 21:57:12 +0000264 spin_lock_bh(&orig_node->neigh_list_lock);
265
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266 /* for all neighbors towards this originator ... */
Marek Lindner9591a792010-12-12 21:57:11 +0000267 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
268 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
270 if ((time_after(jiffies,
271 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
272 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
Marek Lindner1a241a52011-01-19 19:16:10 +0000273 (neigh_node->if_incoming->if_status == IF_NOT_IN_USE) ||
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
275
Marek Lindner1a241a52011-01-19 19:16:10 +0000276 if ((neigh_node->if_incoming->if_status ==
277 IF_INACTIVE) ||
278 (neigh_node->if_incoming->if_status ==
279 IF_NOT_IN_USE) ||
280 (neigh_node->if_incoming->if_status ==
281 IF_TO_BE_REMOVED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282 bat_dbg(DBG_BATMAN, bat_priv,
283 "neighbor purge: originator %pM, "
284 "neighbor: %pM, iface: %s\n",
285 orig_node->orig, neigh_node->addr,
286 neigh_node->if_incoming->net_dev->name);
287 else
288 bat_dbg(DBG_BATMAN, bat_priv,
289 "neighbor timeout: originator %pM, "
290 "neighbor: %pM, last_valid: %lu\n",
291 orig_node->orig, neigh_node->addr,
292 (neigh_node->last_valid / HZ));
293
294 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +0000295
Marek Lindnerf987ed62010-12-12 21:57:12 +0000296 hlist_del_rcu(&neigh_node->list);
297 call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298 } else {
299 if ((!*best_neigh_node) ||
300 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
301 *best_neigh_node = neigh_node;
302 }
303 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000304
305 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306 return neigh_purged;
307}
308
309static bool purge_orig_node(struct bat_priv *bat_priv,
310 struct orig_node *orig_node)
311{
312 struct neigh_node *best_neigh_node;
313
314 if (time_after(jiffies,
315 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
316
317 bat_dbg(DBG_BATMAN, bat_priv,
318 "Originator timeout: originator %pM, last_valid %lu\n",
319 orig_node->orig, (orig_node->last_valid / HZ));
320 return true;
321 } else {
322 if (purge_orig_neighbors(bat_priv, orig_node,
323 &best_neigh_node)) {
324 update_routes(bat_priv, orig_node,
325 best_neigh_node,
326 orig_node->hna_buff,
327 orig_node->hna_buff_len);
328 /* update bonding candidates, we could have lost
329 * some candidates. */
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000330 update_bonding_candidates(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331 }
332 }
333
334 return false;
335}
336
337static void _purge_orig(struct bat_priv *bat_priv)
338{
339 struct hashtable_t *hash = bat_priv->orig_hash;
340 struct hlist_node *walk, *safe;
341 struct hlist_head *head;
342 struct element_t *bucket;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000343 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344 struct orig_node *orig_node;
345 int i;
346
347 if (!hash)
348 return;
349
350 spin_lock_bh(&bat_priv->orig_hash_lock);
351
352 /* for all origins... */
353 for (i = 0; i < hash->size; i++) {
354 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000355 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000357 spin_lock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
359 orig_node = bucket->data;
360
361 if (purge_orig_node(bat_priv, orig_node)) {
362 if (orig_node->gw_flags)
363 gw_node_delete(bat_priv, orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000364 hlist_del_rcu(walk);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000365 call_rcu(&bucket->rcu, bucket_free_orig_rcu);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000366 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367 }
368
369 if (time_after(jiffies, orig_node->last_frag_packet +
370 msecs_to_jiffies(FRAG_TIMEOUT)))
371 frag_list_free(&orig_node->frag_list);
372 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000373 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374 }
375
376 spin_unlock_bh(&bat_priv->orig_hash_lock);
377
378 gw_node_purge(bat_priv);
379 gw_election(bat_priv);
380
381 softif_neigh_purge(bat_priv);
382}
383
384static void purge_orig(struct work_struct *work)
385{
386 struct delayed_work *delayed_work =
387 container_of(work, struct delayed_work, work);
388 struct bat_priv *bat_priv =
389 container_of(delayed_work, struct bat_priv, orig_work);
390
391 _purge_orig(bat_priv);
392 start_purge_timer(bat_priv);
393}
394
395void purge_orig_ref(struct bat_priv *bat_priv)
396{
397 _purge_orig(bat_priv);
398}
399
400int orig_seq_print_text(struct seq_file *seq, void *offset)
401{
402 struct net_device *net_dev = (struct net_device *)seq->private;
403 struct bat_priv *bat_priv = netdev_priv(net_dev);
404 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner9591a792010-12-12 21:57:11 +0000405 struct hlist_node *walk, *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406 struct hlist_head *head;
407 struct element_t *bucket;
408 struct orig_node *orig_node;
409 struct neigh_node *neigh_node;
410 int batman_count = 0;
411 int last_seen_secs;
412 int last_seen_msecs;
413 int i;
414
415 if ((!bat_priv->primary_if) ||
416 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
417 if (!bat_priv->primary_if)
418 return seq_printf(seq, "BATMAN mesh %s disabled - "
419 "please specify interfaces to enable it\n",
420 net_dev->name);
421
422 return seq_printf(seq, "BATMAN mesh %s "
423 "disabled - primary interface not active\n",
424 net_dev->name);
425 }
426
427 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
428 SOURCE_VERSION, REVISION_VERSION_STR,
429 bat_priv->primary_if->net_dev->name,
430 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
431 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
432 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
433 "outgoingIF", "Potential nexthops");
434
435 spin_lock_bh(&bat_priv->orig_hash_lock);
436
437 for (i = 0; i < hash->size; i++) {
438 head = &hash->table[i];
439
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000440 rcu_read_lock();
441 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442 orig_node = bucket->data;
443
444 if (!orig_node->router)
445 continue;
446
447 if (orig_node->router->tq_avg == 0)
448 continue;
449
450 last_seen_secs = jiffies_to_msecs(jiffies -
451 orig_node->last_valid) / 1000;
452 last_seen_msecs = jiffies_to_msecs(jiffies -
453 orig_node->last_valid) % 1000;
454
455 neigh_node = orig_node->router;
456 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
457 orig_node->orig, last_seen_secs,
458 last_seen_msecs, neigh_node->tq_avg,
459 neigh_node->addr,
460 neigh_node->if_incoming->net_dev->name);
461
Marek Lindnerf987ed62010-12-12 21:57:12 +0000462 hlist_for_each_entry_rcu(neigh_node, node,
463 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
465 neigh_node->tq_avg);
466 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467
468 seq_printf(seq, "\n");
469 batman_count++;
470 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000471 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472 }
473
474 spin_unlock_bh(&bat_priv->orig_hash_lock);
475
476 if ((batman_count == 0))
477 seq_printf(seq, "No batman nodes in range ...\n");
478
479 return 0;
480}
481
482static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
483{
484 void *data_ptr;
485
486 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
487 GFP_ATOMIC);
488 if (!data_ptr) {
489 pr_err("Can't resize orig: out of memory\n");
490 return -1;
491 }
492
493 memcpy(data_ptr, orig_node->bcast_own,
494 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
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);
499 if (!data_ptr) {
500 pr_err("Can't resize orig: out of memory\n");
501 return -1;
502 }
503
504 memcpy(data_ptr, orig_node->bcast_own_sum,
505 (max_if_num - 1) * sizeof(uint8_t));
506 kfree(orig_node->bcast_own_sum);
507 orig_node->bcast_own_sum = data_ptr;
508
509 return 0;
510}
511
512int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
513{
514 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
515 struct hashtable_t *hash = bat_priv->orig_hash;
516 struct hlist_node *walk;
517 struct hlist_head *head;
518 struct element_t *bucket;
519 struct orig_node *orig_node;
520 int i;
521
522 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
523 * if_num */
524 spin_lock_bh(&bat_priv->orig_hash_lock);
525
526 for (i = 0; i < hash->size; i++) {
527 head = &hash->table[i];
528
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000529 rcu_read_lock();
530 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531 orig_node = bucket->data;
532
533 if (orig_node_add_if(orig_node, max_if_num) == -1)
534 goto err;
535 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000536 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537 }
538
539 spin_unlock_bh(&bat_priv->orig_hash_lock);
540 return 0;
541
542err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000543 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544 spin_unlock_bh(&bat_priv->orig_hash_lock);
545 return -ENOMEM;
546}
547
548static int orig_node_del_if(struct orig_node *orig_node,
549 int max_if_num, int del_if_num)
550{
551 void *data_ptr = NULL;
552 int chunk_size;
553
554 /* last interface was removed */
555 if (max_if_num == 0)
556 goto free_bcast_own;
557
558 chunk_size = sizeof(unsigned long) * NUM_WORDS;
559 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
560 if (!data_ptr) {
561 pr_err("Can't resize orig: out of memory\n");
562 return -1;
563 }
564
565 /* copy first part */
566 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
567
568 /* copy second part */
569 memcpy(data_ptr + del_if_num * chunk_size,
570 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
571 (max_if_num - del_if_num) * chunk_size);
572
573free_bcast_own:
574 kfree(orig_node->bcast_own);
575 orig_node->bcast_own = data_ptr;
576
577 if (max_if_num == 0)
578 goto free_own_sum;
579
580 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
581 if (!data_ptr) {
582 pr_err("Can't resize orig: out of memory\n");
583 return -1;
584 }
585
586 memcpy(data_ptr, orig_node->bcast_own_sum,
587 del_if_num * sizeof(uint8_t));
588
589 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
590 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
591 (max_if_num - del_if_num) * sizeof(uint8_t));
592
593free_own_sum:
594 kfree(orig_node->bcast_own_sum);
595 orig_node->bcast_own_sum = data_ptr;
596
597 return 0;
598}
599
600int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
601{
602 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
603 struct hashtable_t *hash = bat_priv->orig_hash;
604 struct hlist_node *walk;
605 struct hlist_head *head;
606 struct element_t *bucket;
607 struct batman_if *batman_if_tmp;
608 struct orig_node *orig_node;
609 int i, ret;
610
611 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
612 * if_num */
613 spin_lock_bh(&bat_priv->orig_hash_lock);
614
615 for (i = 0; i < hash->size; i++) {
616 head = &hash->table[i];
617
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000618 rcu_read_lock();
619 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620 orig_node = bucket->data;
621
622 ret = orig_node_del_if(orig_node, max_if_num,
623 batman_if->if_num);
624
625 if (ret == -1)
626 goto err;
627 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000628 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 }
630
631 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
632 rcu_read_lock();
633 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
634 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
635 continue;
636
637 if (batman_if == batman_if_tmp)
638 continue;
639
640 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
641 continue;
642
643 if (batman_if_tmp->if_num > batman_if->if_num)
644 batman_if_tmp->if_num--;
645 }
646 rcu_read_unlock();
647
648 batman_if->if_num = -1;
649 spin_unlock_bh(&bat_priv->orig_hash_lock);
650 return 0;
651
652err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000653 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000654 spin_unlock_bh(&bat_priv->orig_hash_lock);
655 return -ENOMEM;
656}