blob: 69e27a243fd016db84431742f4650dd08b1244e6 [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 Lindner1605d0d2011-02-18 12:28:11 +000097
98 /* extra reference for return */
99 atomic_set(&neigh_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000100
Marek Lindnerf987ed62010-12-12 21:57:12 +0000101 spin_lock_bh(&orig_node->neigh_list_lock);
102 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
103 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000104 return neigh_node;
105}
106
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000107static void orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108{
Marek Lindner9591a792010-12-12 21:57:11 +0000109 struct hlist_node *node, *node_tmp;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000110 struct neigh_node *neigh_node, *tmp_neigh_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000111 struct orig_node *orig_node;
112
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000113 orig_node = container_of(rcu, struct orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114
Marek Lindnerf987ed62010-12-12 21:57:12 +0000115 spin_lock_bh(&orig_node->neigh_list_lock);
116
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000117 /* for all bonding members ... */
118 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
119 &orig_node->bond_list, bonding_list) {
120 list_del_rcu(&neigh_node->bonding_list);
Marek Lindner44524fc2011-02-10 14:33:53 +0000121 neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000122 }
123
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000124 /* for all neighbors towards this originator ... */
Marek Lindner9591a792010-12-12 21:57:11 +0000125 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
126 &orig_node->neigh_list, list) {
Marek Lindnerf987ed62010-12-12 21:57:12 +0000127 hlist_del_rcu(&neigh_node->list);
Marek Lindner44524fc2011-02-10 14:33:53 +0000128 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000129 }
130
Marek Lindnerf987ed62010-12-12 21:57:12 +0000131 spin_unlock_bh(&orig_node->neigh_list_lock);
132
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133 frag_list_free(&orig_node->frag_list);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000134 hna_global_del_orig(orig_node->bat_priv, orig_node,
135 "originator timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136
137 kfree(orig_node->bcast_own);
138 kfree(orig_node->bcast_own_sum);
139 kfree(orig_node);
140}
141
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000142void orig_node_free_ref(struct orig_node *orig_node)
143{
144 if (atomic_dec_and_test(&orig_node->refcount))
145 call_rcu(&orig_node->rcu, orig_node_free_rcu);
146}
147
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000148void originator_free(struct bat_priv *bat_priv)
149{
Marek Lindner16b1aba2011-01-19 20:01:42 +0000150 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000151 struct hlist_node *node, *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000152 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000153 spinlock_t *list_lock; /* spinlock to protect write access */
154 struct orig_node *orig_node;
155 int i;
156
157 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158 return;
159
160 cancel_delayed_work_sync(&bat_priv->orig_work);
161
162 spin_lock_bh(&bat_priv->orig_hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000164
165 for (i = 0; i < hash->size; i++) {
166 head = &hash->table[i];
167 list_lock = &hash->list_locks[i];
168
169 spin_lock_bh(list_lock);
Marek Lindner7aadf882011-02-18 12:28:09 +0000170 hlist_for_each_entry_safe(orig_node, node, node_tmp,
171 head, hash_entry) {
Marek Lindner16b1aba2011-01-19 20:01:42 +0000172
Marek Lindner7aadf882011-02-18 12:28:09 +0000173 hlist_del_rcu(node);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000174 orig_node_free_ref(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000175 }
176 spin_unlock_bh(list_lock);
177 }
178
179 hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180 spin_unlock_bh(&bat_priv->orig_hash_lock);
181}
182
183/* 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 Lindner7aadf882011-02-18 12:28:09 +0000191 orig_node = orig_hash_find(bat_priv, addr);
192 if (orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193 return orig_node;
194
195 bat_dbg(DBG_BATMAN, bat_priv,
196 "Creating new originator: %pM\n", addr);
197
198 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
199 if (!orig_node)
200 return NULL;
201
Marek Lindner9591a792010-12-12 21:57:11 +0000202 INIT_HLIST_HEAD(&orig_node->neigh_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000203 INIT_LIST_HEAD(&orig_node->bond_list);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000204 spin_lock_init(&orig_node->ogm_cnt_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000205 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000206 spin_lock_init(&orig_node->neigh_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000207
208 /* extra reference for return */
209 atomic_set(&orig_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210
Marek Lindner16b1aba2011-01-19 20:01:42 +0000211 orig_node->bat_priv = bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212 memcpy(orig_node->orig, addr, ETH_ALEN);
213 orig_node->router = NULL;
214 orig_node->hna_buff = NULL;
215 orig_node->bcast_seqno_reset = jiffies - 1
216 - msecs_to_jiffies(RESET_PROTECTION_MS);
217 orig_node->batman_seqno_reset = jiffies - 1
218 - msecs_to_jiffies(RESET_PROTECTION_MS);
219
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000220 atomic_set(&orig_node->bond_candidates, 0);
221
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222 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
Marek Lindner7aadf882011-02-18 12:28:09 +0000237 hash_added = hash_add(bat_priv->orig_hash, compare_orig,
238 choose_orig, orig_node, &orig_node->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239 if (hash_added < 0)
240 goto free_bcast_own_sum;
241
242 return orig_node;
243free_bcast_own_sum:
244 kfree(orig_node->bcast_own_sum);
245free_bcast_own:
246 kfree(orig_node->bcast_own);
247free_orig_node:
248 kfree(orig_node);
249 return NULL;
250}
251
252static bool purge_orig_neighbors(struct bat_priv *bat_priv,
253 struct orig_node *orig_node,
254 struct neigh_node **best_neigh_node)
255{
Marek Lindner9591a792010-12-12 21:57:11 +0000256 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257 struct neigh_node *neigh_node;
258 bool neigh_purged = false;
259
260 *best_neigh_node = NULL;
261
Marek Lindnerf987ed62010-12-12 21:57:12 +0000262 spin_lock_bh(&orig_node->neigh_list_lock);
263
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264 /* for all neighbors towards this originator ... */
Marek Lindner9591a792010-12-12 21:57:11 +0000265 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
266 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
268 if ((time_after(jiffies,
269 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
270 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
Marek Lindner1a241a52011-01-19 19:16:10 +0000271 (neigh_node->if_incoming->if_status == IF_NOT_IN_USE) ||
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
273
Marek Lindner1a241a52011-01-19 19:16:10 +0000274 if ((neigh_node->if_incoming->if_status ==
275 IF_INACTIVE) ||
276 (neigh_node->if_incoming->if_status ==
277 IF_NOT_IN_USE) ||
278 (neigh_node->if_incoming->if_status ==
279 IF_TO_BE_REMOVED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280 bat_dbg(DBG_BATMAN, bat_priv,
281 "neighbor purge: originator %pM, "
282 "neighbor: %pM, iface: %s\n",
283 orig_node->orig, neigh_node->addr,
284 neigh_node->if_incoming->net_dev->name);
285 else
286 bat_dbg(DBG_BATMAN, bat_priv,
287 "neighbor timeout: originator %pM, "
288 "neighbor: %pM, last_valid: %lu\n",
289 orig_node->orig, neigh_node->addr,
290 (neigh_node->last_valid / HZ));
291
292 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +0000293
Marek Lindnerf987ed62010-12-12 21:57:12 +0000294 hlist_del_rcu(&neigh_node->list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000295 bonding_candidate_del(orig_node, neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000296 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 } else {
298 if ((!*best_neigh_node) ||
299 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
300 *best_neigh_node = neigh_node;
301 }
302 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000303
304 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305 return neigh_purged;
306}
307
308static bool purge_orig_node(struct bat_priv *bat_priv,
309 struct orig_node *orig_node)
310{
311 struct neigh_node *best_neigh_node;
312
313 if (time_after(jiffies,
314 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
315
316 bat_dbg(DBG_BATMAN, bat_priv,
317 "Originator timeout: originator %pM, last_valid %lu\n",
318 orig_node->orig, (orig_node->last_valid / HZ));
319 return true;
320 } else {
321 if (purge_orig_neighbors(bat_priv, orig_node,
322 &best_neigh_node)) {
323 update_routes(bat_priv, orig_node,
324 best_neigh_node,
325 orig_node->hna_buff,
326 orig_node->hna_buff_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327 }
328 }
329
330 return false;
331}
332
333static void _purge_orig(struct bat_priv *bat_priv)
334{
335 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000336 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000338 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339 struct orig_node *orig_node;
340 int i;
341
342 if (!hash)
343 return;
344
345 spin_lock_bh(&bat_priv->orig_hash_lock);
346
347 /* for all origins... */
348 for (i = 0; i < hash->size; i++) {
349 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000350 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000352 spin_lock_bh(list_lock);
Marek Lindner7aadf882011-02-18 12:28:09 +0000353 hlist_for_each_entry_safe(orig_node, node, node_tmp,
354 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355 if (purge_orig_node(bat_priv, orig_node)) {
356 if (orig_node->gw_flags)
357 gw_node_delete(bat_priv, orig_node);
Marek Lindner7aadf882011-02-18 12:28:09 +0000358 hlist_del_rcu(node);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000359 orig_node_free_ref(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000360 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361 }
362
363 if (time_after(jiffies, orig_node->last_frag_packet +
364 msecs_to_jiffies(FRAG_TIMEOUT)))
365 frag_list_free(&orig_node->frag_list);
366 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000367 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368 }
369
370 spin_unlock_bh(&bat_priv->orig_hash_lock);
371
372 gw_node_purge(bat_priv);
373 gw_election(bat_priv);
374
375 softif_neigh_purge(bat_priv);
376}
377
378static void purge_orig(struct work_struct *work)
379{
380 struct delayed_work *delayed_work =
381 container_of(work, struct delayed_work, work);
382 struct bat_priv *bat_priv =
383 container_of(delayed_work, struct bat_priv, orig_work);
384
385 _purge_orig(bat_priv);
386 start_purge_timer(bat_priv);
387}
388
389void purge_orig_ref(struct bat_priv *bat_priv)
390{
391 _purge_orig(bat_priv);
392}
393
394int orig_seq_print_text(struct seq_file *seq, void *offset)
395{
396 struct net_device *net_dev = (struct net_device *)seq->private;
397 struct bat_priv *bat_priv = netdev_priv(net_dev);
398 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000399 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401 struct orig_node *orig_node;
402 struct neigh_node *neigh_node;
403 int batman_count = 0;
404 int last_seen_secs;
405 int last_seen_msecs;
406 int i;
407
408 if ((!bat_priv->primary_if) ||
409 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
410 if (!bat_priv->primary_if)
411 return seq_printf(seq, "BATMAN mesh %s disabled - "
412 "please specify interfaces to enable it\n",
413 net_dev->name);
414
415 return seq_printf(seq, "BATMAN mesh %s "
416 "disabled - primary interface not active\n",
417 net_dev->name);
418 }
419
420 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
421 SOURCE_VERSION, REVISION_VERSION_STR,
422 bat_priv->primary_if->net_dev->name,
423 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
424 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
425 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
426 "outgoingIF", "Potential nexthops");
427
428 spin_lock_bh(&bat_priv->orig_hash_lock);
429
430 for (i = 0; i < hash->size; i++) {
431 head = &hash->table[i];
432
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000433 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000434 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435 if (!orig_node->router)
436 continue;
437
438 if (orig_node->router->tq_avg == 0)
439 continue;
440
441 last_seen_secs = jiffies_to_msecs(jiffies -
442 orig_node->last_valid) / 1000;
443 last_seen_msecs = jiffies_to_msecs(jiffies -
444 orig_node->last_valid) % 1000;
445
446 neigh_node = orig_node->router;
447 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
448 orig_node->orig, last_seen_secs,
449 last_seen_msecs, neigh_node->tq_avg,
450 neigh_node->addr,
451 neigh_node->if_incoming->net_dev->name);
452
Marek Lindner7aadf882011-02-18 12:28:09 +0000453 hlist_for_each_entry_rcu(neigh_node, node_tmp,
Marek Lindnerf987ed62010-12-12 21:57:12 +0000454 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
456 neigh_node->tq_avg);
457 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458
459 seq_printf(seq, "\n");
460 batman_count++;
461 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000462 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463 }
464
465 spin_unlock_bh(&bat_priv->orig_hash_lock);
466
467 if ((batman_count == 0))
468 seq_printf(seq, "No batman nodes in range ...\n");
469
470 return 0;
471}
472
473static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
474{
475 void *data_ptr;
476
477 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
478 GFP_ATOMIC);
479 if (!data_ptr) {
480 pr_err("Can't resize orig: out of memory\n");
481 return -1;
482 }
483
484 memcpy(data_ptr, orig_node->bcast_own,
485 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
486 kfree(orig_node->bcast_own);
487 orig_node->bcast_own = data_ptr;
488
489 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
490 if (!data_ptr) {
491 pr_err("Can't resize orig: out of memory\n");
492 return -1;
493 }
494
495 memcpy(data_ptr, orig_node->bcast_own_sum,
496 (max_if_num - 1) * sizeof(uint8_t));
497 kfree(orig_node->bcast_own_sum);
498 orig_node->bcast_own_sum = data_ptr;
499
500 return 0;
501}
502
503int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
504{
505 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
506 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000507 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509 struct orig_node *orig_node;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000510 int i, ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511
512 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
513 * if_num */
514 spin_lock_bh(&bat_priv->orig_hash_lock);
515
516 for (i = 0; i < hash->size; i++) {
517 head = &hash->table[i];
518
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000519 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000520 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000521 spin_lock_bh(&orig_node->ogm_cnt_lock);
522 ret = orig_node_add_if(orig_node, max_if_num);
523 spin_unlock_bh(&orig_node->ogm_cnt_lock);
524
525 if (ret == -1)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526 goto err;
527 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000528 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529 }
530
531 spin_unlock_bh(&bat_priv->orig_hash_lock);
532 return 0;
533
534err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000535 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536 spin_unlock_bh(&bat_priv->orig_hash_lock);
537 return -ENOMEM;
538}
539
540static int orig_node_del_if(struct orig_node *orig_node,
541 int max_if_num, int del_if_num)
542{
543 void *data_ptr = NULL;
544 int chunk_size;
545
546 /* last interface was removed */
547 if (max_if_num == 0)
548 goto free_bcast_own;
549
550 chunk_size = sizeof(unsigned long) * NUM_WORDS;
551 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
552 if (!data_ptr) {
553 pr_err("Can't resize orig: out of memory\n");
554 return -1;
555 }
556
557 /* copy first part */
558 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
559
560 /* copy second part */
561 memcpy(data_ptr + del_if_num * chunk_size,
562 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
563 (max_if_num - del_if_num) * chunk_size);
564
565free_bcast_own:
566 kfree(orig_node->bcast_own);
567 orig_node->bcast_own = data_ptr;
568
569 if (max_if_num == 0)
570 goto free_own_sum;
571
572 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
573 if (!data_ptr) {
574 pr_err("Can't resize orig: out of memory\n");
575 return -1;
576 }
577
578 memcpy(data_ptr, orig_node->bcast_own_sum,
579 del_if_num * sizeof(uint8_t));
580
581 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
582 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
583 (max_if_num - del_if_num) * sizeof(uint8_t));
584
585free_own_sum:
586 kfree(orig_node->bcast_own_sum);
587 orig_node->bcast_own_sum = data_ptr;
588
589 return 0;
590}
591
592int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
593{
594 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
595 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000596 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598 struct batman_if *batman_if_tmp;
599 struct orig_node *orig_node;
600 int i, ret;
601
602 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
603 * if_num */
604 spin_lock_bh(&bat_priv->orig_hash_lock);
605
606 for (i = 0; i < hash->size; i++) {
607 head = &hash->table[i];
608
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000609 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000610 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000611 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612 ret = orig_node_del_if(orig_node, max_if_num,
613 batman_if->if_num);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000614 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615
616 if (ret == -1)
617 goto err;
618 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000619 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620 }
621
622 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
623 rcu_read_lock();
624 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
625 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
626 continue;
627
628 if (batman_if == batman_if_tmp)
629 continue;
630
631 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
632 continue;
633
634 if (batman_if_tmp->if_num > batman_if->if_num)
635 batman_if_tmp->if_num--;
636 }
637 rcu_read_unlock();
638
639 batman_if->if_num = -1;
640 spin_unlock_bh(&bat_priv->orig_hash_lock);
641 return 0;
642
643err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000644 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645 spin_unlock_bh(&bat_priv->orig_hash_lock);
646 return -ENOMEM;
647}