blob: bdcb399329dd3eaba2a03e4617d0291cf985a8d1 [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 Lindnerf987ed62010-12-12 21:57:12 +000062static void neigh_node_free_rcu(struct rcu_head *rcu)
63{
64 struct neigh_node *neigh_node;
65
66 neigh_node = container_of(rcu, struct neigh_node, rcu);
Marek Lindner44524fc2011-02-10 14:33:53 +000067 kfree(neigh_node);
Marek Lindnerf987ed62010-12-12 21:57:12 +000068}
69
Marek Lindner44524fc2011-02-10 14:33:53 +000070void neigh_node_free_ref(struct neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +000071{
Marek Lindner44524fc2011-02-10 14:33:53 +000072 if (atomic_dec_and_test(&neigh_node->refcount))
73 call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +000074}
75
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000076struct neigh_node *create_neighbor(struct orig_node *orig_node,
77 struct orig_node *orig_neigh_node,
78 uint8_t *neigh,
79 struct batman_if *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080{
81 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
82 struct neigh_node *neigh_node;
83
84 bat_dbg(DBG_BATMAN, bat_priv,
85 "Creating new last-hop neighbor of originator\n");
86
87 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
88 if (!neigh_node)
89 return NULL;
90
Marek Lindner9591a792010-12-12 21:57:11 +000091 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +000092 INIT_LIST_HEAD(&neigh_node->bonding_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000093
94 memcpy(neigh_node->addr, neigh, ETH_ALEN);
95 neigh_node->orig_node = orig_neigh_node;
96 neigh_node->if_incoming = if_incoming;
Marek Lindner44524fc2011-02-10 14:33:53 +000097 atomic_set(&neigh_node->refcount, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098
Marek Lindnerf987ed62010-12-12 21:57:12 +000099 spin_lock_bh(&orig_node->neigh_list_lock);
100 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
101 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102 return neigh_node;
103}
104
Marek Lindner16b1aba2011-01-19 20:01:42 +0000105void orig_node_free_ref(struct kref *refcount)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000106{
Marek Lindner9591a792010-12-12 21:57:11 +0000107 struct hlist_node *node, *node_tmp;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000108 struct neigh_node *neigh_node, *tmp_neigh_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000109 struct orig_node *orig_node;
110
111 orig_node = container_of(refcount, struct orig_node, refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000112
Marek Lindnerf987ed62010-12-12 21:57:12 +0000113 spin_lock_bh(&orig_node->neigh_list_lock);
114
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000115 /* for all bonding members ... */
116 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
117 &orig_node->bond_list, bonding_list) {
118 list_del_rcu(&neigh_node->bonding_list);
Marek Lindner44524fc2011-02-10 14:33:53 +0000119 neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000120 }
121
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000122 /* for all neighbors towards this originator ... */
Marek Lindner9591a792010-12-12 21:57:11 +0000123 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
124 &orig_node->neigh_list, list) {
Marek Lindnerf987ed62010-12-12 21:57:12 +0000125 hlist_del_rcu(&neigh_node->list);
Marek Lindner44524fc2011-02-10 14:33:53 +0000126 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000127 }
128
Marek Lindnerf987ed62010-12-12 21:57:12 +0000129 spin_unlock_bh(&orig_node->neigh_list_lock);
130
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131 frag_list_free(&orig_node->frag_list);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000132 hna_global_del_orig(orig_node->bat_priv, orig_node,
133 "originator timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134
135 kfree(orig_node->bcast_own);
136 kfree(orig_node->bcast_own_sum);
137 kfree(orig_node);
138}
139
140void originator_free(struct bat_priv *bat_priv)
141{
Marek Lindner16b1aba2011-01-19 20:01:42 +0000142 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000143 struct hlist_node *node, *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000144 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000145 spinlock_t *list_lock; /* spinlock to protect write access */
146 struct orig_node *orig_node;
147 int i;
148
149 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150 return;
151
152 cancel_delayed_work_sync(&bat_priv->orig_work);
153
154 spin_lock_bh(&bat_priv->orig_hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000156
157 for (i = 0; i < hash->size; i++) {
158 head = &hash->table[i];
159 list_lock = &hash->list_locks[i];
160
161 spin_lock_bh(list_lock);
Marek Lindner7aadf882011-02-18 12:28:09 +0000162 hlist_for_each_entry_safe(orig_node, node, node_tmp,
163 head, hash_entry) {
Marek Lindner16b1aba2011-01-19 20:01:42 +0000164
Marek Lindner7aadf882011-02-18 12:28:09 +0000165 hlist_del_rcu(node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000166 kref_put(&orig_node->refcount, orig_node_free_ref);
167 }
168 spin_unlock_bh(list_lock);
169 }
170
171 hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172 spin_unlock_bh(&bat_priv->orig_hash_lock);
173}
174
175/* this function finds or creates an originator entry for the given
176 * address if it does not exits */
177struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
178{
179 struct orig_node *orig_node;
180 int size;
181 int hash_added;
182
Marek Lindner7aadf882011-02-18 12:28:09 +0000183 orig_node = orig_hash_find(bat_priv, addr);
184 if (orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185 return orig_node;
186
187 bat_dbg(DBG_BATMAN, bat_priv,
188 "Creating new originator: %pM\n", addr);
189
190 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
191 if (!orig_node)
192 return NULL;
193
Marek Lindner9591a792010-12-12 21:57:11 +0000194 INIT_HLIST_HEAD(&orig_node->neigh_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000195 INIT_LIST_HEAD(&orig_node->bond_list);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000196 spin_lock_init(&orig_node->ogm_cnt_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000197 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000198 spin_lock_init(&orig_node->neigh_list_lock);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000199 kref_init(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200
Marek Lindner16b1aba2011-01-19 20:01:42 +0000201 orig_node->bat_priv = bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202 memcpy(orig_node->orig, addr, ETH_ALEN);
203 orig_node->router = NULL;
204 orig_node->hna_buff = NULL;
205 orig_node->bcast_seqno_reset = jiffies - 1
206 - msecs_to_jiffies(RESET_PROTECTION_MS);
207 orig_node->batman_seqno_reset = jiffies - 1
208 - msecs_to_jiffies(RESET_PROTECTION_MS);
209
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000210 atomic_set(&orig_node->bond_candidates, 0);
211
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212 size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
213
214 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
215 if (!orig_node->bcast_own)
216 goto free_orig_node;
217
218 size = bat_priv->num_ifaces * sizeof(uint8_t);
219 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
220
221 INIT_LIST_HEAD(&orig_node->frag_list);
222 orig_node->last_frag_packet = 0;
223
224 if (!orig_node->bcast_own_sum)
225 goto free_bcast_own;
226
Marek Lindner7aadf882011-02-18 12:28:09 +0000227 hash_added = hash_add(bat_priv->orig_hash, compare_orig,
228 choose_orig, orig_node, &orig_node->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229 if (hash_added < 0)
230 goto free_bcast_own_sum;
231
Marek Lindner16b1aba2011-01-19 20:01:42 +0000232 /* extra reference for return */
233 kref_get(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234 return orig_node;
235free_bcast_own_sum:
236 kfree(orig_node->bcast_own_sum);
237free_bcast_own:
238 kfree(orig_node->bcast_own);
239free_orig_node:
240 kfree(orig_node);
241 return NULL;
242}
243
244static bool purge_orig_neighbors(struct bat_priv *bat_priv,
245 struct orig_node *orig_node,
246 struct neigh_node **best_neigh_node)
247{
Marek Lindner9591a792010-12-12 21:57:11 +0000248 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249 struct neigh_node *neigh_node;
250 bool neigh_purged = false;
251
252 *best_neigh_node = NULL;
253
Marek Lindnerf987ed62010-12-12 21:57:12 +0000254 spin_lock_bh(&orig_node->neigh_list_lock);
255
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256 /* for all neighbors towards this originator ... */
Marek Lindner9591a792010-12-12 21:57:11 +0000257 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
258 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
260 if ((time_after(jiffies,
261 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
262 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
Marek Lindner1a241a52011-01-19 19:16:10 +0000263 (neigh_node->if_incoming->if_status == IF_NOT_IN_USE) ||
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
265
Marek Lindner1a241a52011-01-19 19:16:10 +0000266 if ((neigh_node->if_incoming->if_status ==
267 IF_INACTIVE) ||
268 (neigh_node->if_incoming->if_status ==
269 IF_NOT_IN_USE) ||
270 (neigh_node->if_incoming->if_status ==
271 IF_TO_BE_REMOVED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272 bat_dbg(DBG_BATMAN, bat_priv,
273 "neighbor purge: originator %pM, "
274 "neighbor: %pM, iface: %s\n",
275 orig_node->orig, neigh_node->addr,
276 neigh_node->if_incoming->net_dev->name);
277 else
278 bat_dbg(DBG_BATMAN, bat_priv,
279 "neighbor timeout: originator %pM, "
280 "neighbor: %pM, last_valid: %lu\n",
281 orig_node->orig, neigh_node->addr,
282 (neigh_node->last_valid / HZ));
283
284 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +0000285
Marek Lindnerf987ed62010-12-12 21:57:12 +0000286 hlist_del_rcu(&neigh_node->list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000287 bonding_candidate_del(orig_node, neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000288 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289 } else {
290 if ((!*best_neigh_node) ||
291 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
292 *best_neigh_node = neigh_node;
293 }
294 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000295
296 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 return neigh_purged;
298}
299
300static bool purge_orig_node(struct bat_priv *bat_priv,
301 struct orig_node *orig_node)
302{
303 struct neigh_node *best_neigh_node;
304
305 if (time_after(jiffies,
306 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
307
308 bat_dbg(DBG_BATMAN, bat_priv,
309 "Originator timeout: originator %pM, last_valid %lu\n",
310 orig_node->orig, (orig_node->last_valid / HZ));
311 return true;
312 } else {
313 if (purge_orig_neighbors(bat_priv, orig_node,
314 &best_neigh_node)) {
315 update_routes(bat_priv, orig_node,
316 best_neigh_node,
317 orig_node->hna_buff,
318 orig_node->hna_buff_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319 }
320 }
321
322 return false;
323}
324
325static void _purge_orig(struct bat_priv *bat_priv)
326{
327 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000328 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000330 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331 struct orig_node *orig_node;
332 int i;
333
334 if (!hash)
335 return;
336
337 spin_lock_bh(&bat_priv->orig_hash_lock);
338
339 /* for all origins... */
340 for (i = 0; i < hash->size; i++) {
341 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000342 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000344 spin_lock_bh(list_lock);
Marek Lindner7aadf882011-02-18 12:28:09 +0000345 hlist_for_each_entry_safe(orig_node, node, node_tmp,
346 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 if (purge_orig_node(bat_priv, orig_node)) {
348 if (orig_node->gw_flags)
349 gw_node_delete(bat_priv, orig_node);
Marek Lindner7aadf882011-02-18 12:28:09 +0000350 hlist_del_rcu(node);
351 kref_put(&orig_node->refcount,
352 orig_node_free_ref);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000353 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354 }
355
356 if (time_after(jiffies, orig_node->last_frag_packet +
357 msecs_to_jiffies(FRAG_TIMEOUT)))
358 frag_list_free(&orig_node->frag_list);
359 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000360 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361 }
362
363 spin_unlock_bh(&bat_priv->orig_hash_lock);
364
365 gw_node_purge(bat_priv);
366 gw_election(bat_priv);
367
368 softif_neigh_purge(bat_priv);
369}
370
371static void purge_orig(struct work_struct *work)
372{
373 struct delayed_work *delayed_work =
374 container_of(work, struct delayed_work, work);
375 struct bat_priv *bat_priv =
376 container_of(delayed_work, struct bat_priv, orig_work);
377
378 _purge_orig(bat_priv);
379 start_purge_timer(bat_priv);
380}
381
382void purge_orig_ref(struct bat_priv *bat_priv)
383{
384 _purge_orig(bat_priv);
385}
386
387int orig_seq_print_text(struct seq_file *seq, void *offset)
388{
389 struct net_device *net_dev = (struct net_device *)seq->private;
390 struct bat_priv *bat_priv = netdev_priv(net_dev);
391 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000392 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394 struct orig_node *orig_node;
395 struct neigh_node *neigh_node;
396 int batman_count = 0;
397 int last_seen_secs;
398 int last_seen_msecs;
399 int i;
400
401 if ((!bat_priv->primary_if) ||
402 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
403 if (!bat_priv->primary_if)
404 return seq_printf(seq, "BATMAN mesh %s disabled - "
405 "please specify interfaces to enable it\n",
406 net_dev->name);
407
408 return seq_printf(seq, "BATMAN mesh %s "
409 "disabled - primary interface not active\n",
410 net_dev->name);
411 }
412
413 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
414 SOURCE_VERSION, REVISION_VERSION_STR,
415 bat_priv->primary_if->net_dev->name,
416 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
417 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
418 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
419 "outgoingIF", "Potential nexthops");
420
421 spin_lock_bh(&bat_priv->orig_hash_lock);
422
423 for (i = 0; i < hash->size; i++) {
424 head = &hash->table[i];
425
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000426 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000427 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428 if (!orig_node->router)
429 continue;
430
431 if (orig_node->router->tq_avg == 0)
432 continue;
433
434 last_seen_secs = jiffies_to_msecs(jiffies -
435 orig_node->last_valid) / 1000;
436 last_seen_msecs = jiffies_to_msecs(jiffies -
437 orig_node->last_valid) % 1000;
438
439 neigh_node = orig_node->router;
440 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
441 orig_node->orig, last_seen_secs,
442 last_seen_msecs, neigh_node->tq_avg,
443 neigh_node->addr,
444 neigh_node->if_incoming->net_dev->name);
445
Marek Lindner7aadf882011-02-18 12:28:09 +0000446 hlist_for_each_entry_rcu(neigh_node, node_tmp,
Marek Lindnerf987ed62010-12-12 21:57:12 +0000447 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
449 neigh_node->tq_avg);
450 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000451
452 seq_printf(seq, "\n");
453 batman_count++;
454 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000455 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456 }
457
458 spin_unlock_bh(&bat_priv->orig_hash_lock);
459
460 if ((batman_count == 0))
461 seq_printf(seq, "No batman nodes in range ...\n");
462
463 return 0;
464}
465
466static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
467{
468 void *data_ptr;
469
470 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
471 GFP_ATOMIC);
472 if (!data_ptr) {
473 pr_err("Can't resize orig: out of memory\n");
474 return -1;
475 }
476
477 memcpy(data_ptr, orig_node->bcast_own,
478 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
479 kfree(orig_node->bcast_own);
480 orig_node->bcast_own = data_ptr;
481
482 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
483 if (!data_ptr) {
484 pr_err("Can't resize orig: out of memory\n");
485 return -1;
486 }
487
488 memcpy(data_ptr, orig_node->bcast_own_sum,
489 (max_if_num - 1) * sizeof(uint8_t));
490 kfree(orig_node->bcast_own_sum);
491 orig_node->bcast_own_sum = data_ptr;
492
493 return 0;
494}
495
496int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
497{
498 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
499 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000500 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502 struct orig_node *orig_node;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000503 int i, ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504
505 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
506 * if_num */
507 spin_lock_bh(&bat_priv->orig_hash_lock);
508
509 for (i = 0; i < hash->size; i++) {
510 head = &hash->table[i];
511
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000512 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000513 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000514 spin_lock_bh(&orig_node->ogm_cnt_lock);
515 ret = orig_node_add_if(orig_node, max_if_num);
516 spin_unlock_bh(&orig_node->ogm_cnt_lock);
517
518 if (ret == -1)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519 goto err;
520 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000521 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522 }
523
524 spin_unlock_bh(&bat_priv->orig_hash_lock);
525 return 0;
526
527err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000528 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529 spin_unlock_bh(&bat_priv->orig_hash_lock);
530 return -ENOMEM;
531}
532
533static int orig_node_del_if(struct orig_node *orig_node,
534 int max_if_num, int del_if_num)
535{
536 void *data_ptr = NULL;
537 int chunk_size;
538
539 /* last interface was removed */
540 if (max_if_num == 0)
541 goto free_bcast_own;
542
543 chunk_size = sizeof(unsigned long) * NUM_WORDS;
544 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
545 if (!data_ptr) {
546 pr_err("Can't resize orig: out of memory\n");
547 return -1;
548 }
549
550 /* copy first part */
551 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
552
553 /* copy second part */
554 memcpy(data_ptr + del_if_num * chunk_size,
555 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
556 (max_if_num - del_if_num) * chunk_size);
557
558free_bcast_own:
559 kfree(orig_node->bcast_own);
560 orig_node->bcast_own = data_ptr;
561
562 if (max_if_num == 0)
563 goto free_own_sum;
564
565 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
566 if (!data_ptr) {
567 pr_err("Can't resize orig: out of memory\n");
568 return -1;
569 }
570
571 memcpy(data_ptr, orig_node->bcast_own_sum,
572 del_if_num * sizeof(uint8_t));
573
574 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
575 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
576 (max_if_num - del_if_num) * sizeof(uint8_t));
577
578free_own_sum:
579 kfree(orig_node->bcast_own_sum);
580 orig_node->bcast_own_sum = data_ptr;
581
582 return 0;
583}
584
585int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
586{
587 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
588 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000589 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591 struct batman_if *batman_if_tmp;
592 struct orig_node *orig_node;
593 int i, ret;
594
595 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
596 * if_num */
597 spin_lock_bh(&bat_priv->orig_hash_lock);
598
599 for (i = 0; i < hash->size; i++) {
600 head = &hash->table[i];
601
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000602 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000603 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000604 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605 ret = orig_node_del_if(orig_node, max_if_num,
606 batman_if->if_num);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000607 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608
609 if (ret == -1)
610 goto err;
611 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000612 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613 }
614
615 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
616 rcu_read_lock();
617 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
618 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
619 continue;
620
621 if (batman_if == batman_if_tmp)
622 continue;
623
624 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
625 continue;
626
627 if (batman_if_tmp->if_num > batman_if->if_num)
628 batman_if_tmp->if_num--;
629 }
630 rcu_read_unlock();
631
632 batman_if->if_num = -1;
633 spin_unlock_bh(&bat_priv->orig_hash_lock);
634 return 0;
635
636err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000637 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000638 spin_unlock_bh(&bat_priv->orig_hash_lock);
639 return -ENOMEM;
640}