blob: a70debebfc5b3eb20f27f92a2cef9ea173f201df [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 Lindner7b36e8e2011-02-18 12:28:10 +0000105static void orig_node_free_rcu(struct rcu_head *rcu)
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
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000111 orig_node = container_of(rcu, struct orig_node, rcu);
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
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000140void orig_node_free_ref(struct orig_node *orig_node)
141{
142 if (atomic_dec_and_test(&orig_node->refcount))
143 call_rcu(&orig_node->rcu, orig_node_free_rcu);
144}
145
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146void originator_free(struct bat_priv *bat_priv)
147{
Marek Lindner16b1aba2011-01-19 20:01:42 +0000148 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000149 struct hlist_node *node, *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000150 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000151 spinlock_t *list_lock; /* spinlock to protect write access */
152 struct orig_node *orig_node;
153 int i;
154
155 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000156 return;
157
158 cancel_delayed_work_sync(&bat_priv->orig_work);
159
160 spin_lock_bh(&bat_priv->orig_hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000162
163 for (i = 0; i < hash->size; i++) {
164 head = &hash->table[i];
165 list_lock = &hash->list_locks[i];
166
167 spin_lock_bh(list_lock);
Marek Lindner7aadf882011-02-18 12:28:09 +0000168 hlist_for_each_entry_safe(orig_node, node, node_tmp,
169 head, hash_entry) {
Marek Lindner16b1aba2011-01-19 20:01:42 +0000170
Marek Lindner7aadf882011-02-18 12:28:09 +0000171 hlist_del_rcu(node);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000172 orig_node_free_ref(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000173 }
174 spin_unlock_bh(list_lock);
175 }
176
177 hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178 spin_unlock_bh(&bat_priv->orig_hash_lock);
179}
180
181/* this function finds or creates an originator entry for the given
182 * address if it does not exits */
183struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
184{
185 struct orig_node *orig_node;
186 int size;
187 int hash_added;
188
Marek Lindner7aadf882011-02-18 12:28:09 +0000189 orig_node = orig_hash_find(bat_priv, addr);
190 if (orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191 return orig_node;
192
193 bat_dbg(DBG_BATMAN, bat_priv,
194 "Creating new originator: %pM\n", addr);
195
196 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
197 if (!orig_node)
198 return NULL;
199
Marek Lindner9591a792010-12-12 21:57:11 +0000200 INIT_HLIST_HEAD(&orig_node->neigh_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000201 INIT_LIST_HEAD(&orig_node->bond_list);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000202 spin_lock_init(&orig_node->ogm_cnt_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000203 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000204 spin_lock_init(&orig_node->neigh_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000205
206 /* extra reference for return */
207 atomic_set(&orig_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208
Marek Lindner16b1aba2011-01-19 20:01:42 +0000209 orig_node->bat_priv = bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210 memcpy(orig_node->orig, addr, ETH_ALEN);
211 orig_node->router = NULL;
212 orig_node->hna_buff = NULL;
213 orig_node->bcast_seqno_reset = jiffies - 1
214 - msecs_to_jiffies(RESET_PROTECTION_MS);
215 orig_node->batman_seqno_reset = jiffies - 1
216 - msecs_to_jiffies(RESET_PROTECTION_MS);
217
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000218 atomic_set(&orig_node->bond_candidates, 0);
219
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220 size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
221
222 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
223 if (!orig_node->bcast_own)
224 goto free_orig_node;
225
226 size = bat_priv->num_ifaces * sizeof(uint8_t);
227 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
228
229 INIT_LIST_HEAD(&orig_node->frag_list);
230 orig_node->last_frag_packet = 0;
231
232 if (!orig_node->bcast_own_sum)
233 goto free_bcast_own;
234
Marek Lindner7aadf882011-02-18 12:28:09 +0000235 hash_added = hash_add(bat_priv->orig_hash, compare_orig,
236 choose_orig, orig_node, &orig_node->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237 if (hash_added < 0)
238 goto free_bcast_own_sum;
239
240 return orig_node;
241free_bcast_own_sum:
242 kfree(orig_node->bcast_own_sum);
243free_bcast_own:
244 kfree(orig_node->bcast_own);
245free_orig_node:
246 kfree(orig_node);
247 return NULL;
248}
249
250static bool purge_orig_neighbors(struct bat_priv *bat_priv,
251 struct orig_node *orig_node,
252 struct neigh_node **best_neigh_node)
253{
Marek Lindner9591a792010-12-12 21:57:11 +0000254 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255 struct neigh_node *neigh_node;
256 bool neigh_purged = false;
257
258 *best_neigh_node = NULL;
259
Marek Lindnerf987ed62010-12-12 21:57:12 +0000260 spin_lock_bh(&orig_node->neigh_list_lock);
261
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262 /* for all neighbors towards this originator ... */
Marek Lindner9591a792010-12-12 21:57:11 +0000263 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
264 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265
266 if ((time_after(jiffies,
267 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
268 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
Marek Lindner1a241a52011-01-19 19:16:10 +0000269 (neigh_node->if_incoming->if_status == IF_NOT_IN_USE) ||
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
271
Marek Lindner1a241a52011-01-19 19:16:10 +0000272 if ((neigh_node->if_incoming->if_status ==
273 IF_INACTIVE) ||
274 (neigh_node->if_incoming->if_status ==
275 IF_NOT_IN_USE) ||
276 (neigh_node->if_incoming->if_status ==
277 IF_TO_BE_REMOVED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278 bat_dbg(DBG_BATMAN, bat_priv,
279 "neighbor purge: originator %pM, "
280 "neighbor: %pM, iface: %s\n",
281 orig_node->orig, neigh_node->addr,
282 neigh_node->if_incoming->net_dev->name);
283 else
284 bat_dbg(DBG_BATMAN, bat_priv,
285 "neighbor timeout: originator %pM, "
286 "neighbor: %pM, last_valid: %lu\n",
287 orig_node->orig, neigh_node->addr,
288 (neigh_node->last_valid / HZ));
289
290 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +0000291
Marek Lindnerf987ed62010-12-12 21:57:12 +0000292 hlist_del_rcu(&neigh_node->list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000293 bonding_candidate_del(orig_node, neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000294 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295 } else {
296 if ((!*best_neigh_node) ||
297 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
298 *best_neigh_node = neigh_node;
299 }
300 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000301
302 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303 return neigh_purged;
304}
305
306static bool purge_orig_node(struct bat_priv *bat_priv,
307 struct orig_node *orig_node)
308{
309 struct neigh_node *best_neigh_node;
310
311 if (time_after(jiffies,
312 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
313
314 bat_dbg(DBG_BATMAN, bat_priv,
315 "Originator timeout: originator %pM, last_valid %lu\n",
316 orig_node->orig, (orig_node->last_valid / HZ));
317 return true;
318 } else {
319 if (purge_orig_neighbors(bat_priv, orig_node,
320 &best_neigh_node)) {
321 update_routes(bat_priv, orig_node,
322 best_neigh_node,
323 orig_node->hna_buff,
324 orig_node->hna_buff_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325 }
326 }
327
328 return false;
329}
330
331static void _purge_orig(struct bat_priv *bat_priv)
332{
333 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000334 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000336 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337 struct orig_node *orig_node;
338 int i;
339
340 if (!hash)
341 return;
342
343 spin_lock_bh(&bat_priv->orig_hash_lock);
344
345 /* for all origins... */
346 for (i = 0; i < hash->size; i++) {
347 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000348 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000350 spin_lock_bh(list_lock);
Marek Lindner7aadf882011-02-18 12:28:09 +0000351 hlist_for_each_entry_safe(orig_node, node, node_tmp,
352 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353 if (purge_orig_node(bat_priv, orig_node)) {
354 if (orig_node->gw_flags)
355 gw_node_delete(bat_priv, orig_node);
Marek Lindner7aadf882011-02-18 12:28:09 +0000356 hlist_del_rcu(node);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000357 orig_node_free_ref(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000358 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359 }
360
361 if (time_after(jiffies, orig_node->last_frag_packet +
362 msecs_to_jiffies(FRAG_TIMEOUT)))
363 frag_list_free(&orig_node->frag_list);
364 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000365 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366 }
367
368 spin_unlock_bh(&bat_priv->orig_hash_lock);
369
370 gw_node_purge(bat_priv);
371 gw_election(bat_priv);
372
373 softif_neigh_purge(bat_priv);
374}
375
376static void purge_orig(struct work_struct *work)
377{
378 struct delayed_work *delayed_work =
379 container_of(work, struct delayed_work, work);
380 struct bat_priv *bat_priv =
381 container_of(delayed_work, struct bat_priv, orig_work);
382
383 _purge_orig(bat_priv);
384 start_purge_timer(bat_priv);
385}
386
387void purge_orig_ref(struct bat_priv *bat_priv)
388{
389 _purge_orig(bat_priv);
390}
391
392int orig_seq_print_text(struct seq_file *seq, void *offset)
393{
394 struct net_device *net_dev = (struct net_device *)seq->private;
395 struct bat_priv *bat_priv = netdev_priv(net_dev);
396 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000397 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399 struct orig_node *orig_node;
400 struct neigh_node *neigh_node;
401 int batman_count = 0;
402 int last_seen_secs;
403 int last_seen_msecs;
404 int i;
405
406 if ((!bat_priv->primary_if) ||
407 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
408 if (!bat_priv->primary_if)
409 return seq_printf(seq, "BATMAN mesh %s disabled - "
410 "please specify interfaces to enable it\n",
411 net_dev->name);
412
413 return seq_printf(seq, "BATMAN mesh %s "
414 "disabled - primary interface not active\n",
415 net_dev->name);
416 }
417
418 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
419 SOURCE_VERSION, REVISION_VERSION_STR,
420 bat_priv->primary_if->net_dev->name,
421 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
422 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
423 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
424 "outgoingIF", "Potential nexthops");
425
426 spin_lock_bh(&bat_priv->orig_hash_lock);
427
428 for (i = 0; i < hash->size; i++) {
429 head = &hash->table[i];
430
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000431 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000432 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 if (!orig_node->router)
434 continue;
435
436 if (orig_node->router->tq_avg == 0)
437 continue;
438
439 last_seen_secs = jiffies_to_msecs(jiffies -
440 orig_node->last_valid) / 1000;
441 last_seen_msecs = jiffies_to_msecs(jiffies -
442 orig_node->last_valid) % 1000;
443
444 neigh_node = orig_node->router;
445 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
446 orig_node->orig, last_seen_secs,
447 last_seen_msecs, neigh_node->tq_avg,
448 neigh_node->addr,
449 neigh_node->if_incoming->net_dev->name);
450
Marek Lindner7aadf882011-02-18 12:28:09 +0000451 hlist_for_each_entry_rcu(neigh_node, node_tmp,
Marek Lindnerf987ed62010-12-12 21:57:12 +0000452 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
454 neigh_node->tq_avg);
455 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456
457 seq_printf(seq, "\n");
458 batman_count++;
459 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000460 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461 }
462
463 spin_unlock_bh(&bat_priv->orig_hash_lock);
464
465 if ((batman_count == 0))
466 seq_printf(seq, "No batman nodes in range ...\n");
467
468 return 0;
469}
470
471static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
472{
473 void *data_ptr;
474
475 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
476 GFP_ATOMIC);
477 if (!data_ptr) {
478 pr_err("Can't resize orig: out of memory\n");
479 return -1;
480 }
481
482 memcpy(data_ptr, orig_node->bcast_own,
483 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
484 kfree(orig_node->bcast_own);
485 orig_node->bcast_own = data_ptr;
486
487 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), 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_sum,
494 (max_if_num - 1) * sizeof(uint8_t));
495 kfree(orig_node->bcast_own_sum);
496 orig_node->bcast_own_sum = data_ptr;
497
498 return 0;
499}
500
501int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
502{
503 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
504 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000505 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507 struct orig_node *orig_node;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000508 int i, ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509
510 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
511 * if_num */
512 spin_lock_bh(&bat_priv->orig_hash_lock);
513
514 for (i = 0; i < hash->size; i++) {
515 head = &hash->table[i];
516
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000517 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000518 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000519 spin_lock_bh(&orig_node->ogm_cnt_lock);
520 ret = orig_node_add_if(orig_node, max_if_num);
521 spin_unlock_bh(&orig_node->ogm_cnt_lock);
522
523 if (ret == -1)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524 goto err;
525 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000526 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527 }
528
529 spin_unlock_bh(&bat_priv->orig_hash_lock);
530 return 0;
531
532err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000533 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534 spin_unlock_bh(&bat_priv->orig_hash_lock);
535 return -ENOMEM;
536}
537
538static int orig_node_del_if(struct orig_node *orig_node,
539 int max_if_num, int del_if_num)
540{
541 void *data_ptr = NULL;
542 int chunk_size;
543
544 /* last interface was removed */
545 if (max_if_num == 0)
546 goto free_bcast_own;
547
548 chunk_size = sizeof(unsigned long) * NUM_WORDS;
549 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
550 if (!data_ptr) {
551 pr_err("Can't resize orig: out of memory\n");
552 return -1;
553 }
554
555 /* copy first part */
556 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
557
558 /* copy second part */
559 memcpy(data_ptr + del_if_num * chunk_size,
560 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
561 (max_if_num - del_if_num) * chunk_size);
562
563free_bcast_own:
564 kfree(orig_node->bcast_own);
565 orig_node->bcast_own = data_ptr;
566
567 if (max_if_num == 0)
568 goto free_own_sum;
569
570 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
571 if (!data_ptr) {
572 pr_err("Can't resize orig: out of memory\n");
573 return -1;
574 }
575
576 memcpy(data_ptr, orig_node->bcast_own_sum,
577 del_if_num * sizeof(uint8_t));
578
579 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
580 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
581 (max_if_num - del_if_num) * sizeof(uint8_t));
582
583free_own_sum:
584 kfree(orig_node->bcast_own_sum);
585 orig_node->bcast_own_sum = data_ptr;
586
587 return 0;
588}
589
590int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
591{
592 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
593 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000594 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596 struct batman_if *batman_if_tmp;
597 struct orig_node *orig_node;
598 int i, ret;
599
600 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
601 * if_num */
602 spin_lock_bh(&bat_priv->orig_hash_lock);
603
604 for (i = 0; i < hash->size; i++) {
605 head = &hash->table[i];
606
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000607 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000608 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000609 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610 ret = orig_node_del_if(orig_node, max_if_num,
611 batman_if->if_num);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000612 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613
614 if (ret == -1)
615 goto err;
616 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000617 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618 }
619
620 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
621 rcu_read_lock();
622 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
623 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
624 continue;
625
626 if (batman_if == batman_if_tmp)
627 continue;
628
629 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
630 continue;
631
632 if (batman_if_tmp->if_num > batman_if->if_num)
633 batman_if_tmp->if_num--;
634 }
635 rcu_read_unlock();
636
637 batman_if->if_num = -1;
638 spin_unlock_bh(&bat_priv->orig_hash_lock);
639 return 0;
640
641err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000642 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643 spin_unlock_bh(&bat_priv->orig_hash_lock);
644 return -ENOMEM;
645}