blob: ed23a5895d6c416ea12e0c5bd87ee39859be0ed1 [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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047 bat_priv->orig_hash = hash_new(1024);
48
49 if (!bat_priv->orig_hash)
50 goto err;
51
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052 start_purge_timer(bat_priv);
53 return 1;
54
55err:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056 return 0;
57}
58
Marek Lindner44524fc2011-02-10 14:33:53 +000059void neigh_node_free_ref(struct neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +000060{
Marek Lindner44524fc2011-02-10 14:33:53 +000061 if (atomic_dec_and_test(&neigh_node->refcount))
Paul E. McKenneyae179ae2011-05-01 23:27:50 -070062 kfree_rcu(neigh_node, rcu);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +000063}
64
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000065struct neigh_node *create_neighbor(struct orig_node *orig_node,
66 struct orig_node *orig_neigh_node,
67 uint8_t *neigh,
Marek Lindnere6c10f42011-02-18 12:33:20 +000068 struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069{
70 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
71 struct neigh_node *neigh_node;
72
73 bat_dbg(DBG_BATMAN, bat_priv,
74 "Creating new last-hop neighbor of originator\n");
75
76 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
77 if (!neigh_node)
78 return NULL;
79
Marek Lindner9591a792010-12-12 21:57:11 +000080 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +000081 INIT_LIST_HEAD(&neigh_node->bonding_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082
83 memcpy(neigh_node->addr, neigh, ETH_ALEN);
84 neigh_node->orig_node = orig_neigh_node;
85 neigh_node->if_incoming = if_incoming;
Marek Lindner1605d0d2011-02-18 12:28:11 +000086
87 /* extra reference for return */
88 atomic_set(&neigh_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089
Marek Lindnerf987ed62010-12-12 21:57:12 +000090 spin_lock_bh(&orig_node->neigh_list_lock);
91 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
92 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000093 return neigh_node;
94}
95
Marek Lindner7b36e8e2011-02-18 12:28:10 +000096static void orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000097{
Marek Lindner9591a792010-12-12 21:57:11 +000098 struct hlist_node *node, *node_tmp;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +000099 struct neigh_node *neigh_node, *tmp_neigh_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000100 struct orig_node *orig_node;
101
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000102 orig_node = container_of(rcu, struct orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000103
Marek Lindnerf987ed62010-12-12 21:57:12 +0000104 spin_lock_bh(&orig_node->neigh_list_lock);
105
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000106 /* for all bonding members ... */
107 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
108 &orig_node->bond_list, bonding_list) {
109 list_del_rcu(&neigh_node->bonding_list);
Marek Lindner44524fc2011-02-10 14:33:53 +0000110 neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000111 }
112
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113 /* for all neighbors towards this originator ... */
Marek Lindner9591a792010-12-12 21:57:11 +0000114 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
115 &orig_node->neigh_list, list) {
Marek Lindnerf987ed62010-12-12 21:57:12 +0000116 hlist_del_rcu(&neigh_node->list);
Marek Lindner44524fc2011-02-10 14:33:53 +0000117 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118 }
119
Marek Lindnerf987ed62010-12-12 21:57:12 +0000120 spin_unlock_bh(&orig_node->neigh_list_lock);
121
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000122 frag_list_free(&orig_node->frag_list);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000123 hna_global_del_orig(orig_node->bat_priv, orig_node,
124 "originator timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125
126 kfree(orig_node->bcast_own);
127 kfree(orig_node->bcast_own_sum);
128 kfree(orig_node);
129}
130
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000131void orig_node_free_ref(struct orig_node *orig_node)
132{
133 if (atomic_dec_and_test(&orig_node->refcount))
134 call_rcu(&orig_node->rcu, orig_node_free_rcu);
135}
136
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000137void originator_free(struct bat_priv *bat_priv)
138{
Marek Lindner16b1aba2011-01-19 20:01:42 +0000139 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000140 struct hlist_node *node, *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000141 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000142 spinlock_t *list_lock; /* spinlock to protect write access */
143 struct orig_node *orig_node;
144 int i;
145
146 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000147 return;
148
149 cancel_delayed_work_sync(&bat_priv->orig_work);
150
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000152
153 for (i = 0; i < hash->size; i++) {
154 head = &hash->table[i];
155 list_lock = &hash->list_locks[i];
156
157 spin_lock_bh(list_lock);
Marek Lindner7aadf882011-02-18 12:28:09 +0000158 hlist_for_each_entry_safe(orig_node, node, node_tmp,
159 head, hash_entry) {
Marek Lindner16b1aba2011-01-19 20:01:42 +0000160
Marek Lindner7aadf882011-02-18 12:28:09 +0000161 hlist_del_rcu(node);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000162 orig_node_free_ref(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000163 }
164 spin_unlock_bh(list_lock);
165 }
166
167 hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168}
169
170/* this function finds or creates an originator entry for the given
171 * address if it does not exits */
172struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
173{
174 struct orig_node *orig_node;
175 int size;
176 int hash_added;
177
Marek Lindner7aadf882011-02-18 12:28:09 +0000178 orig_node = orig_hash_find(bat_priv, addr);
179 if (orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180 return orig_node;
181
182 bat_dbg(DBG_BATMAN, bat_priv,
183 "Creating new originator: %pM\n", addr);
184
185 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
186 if (!orig_node)
187 return NULL;
188
Marek Lindner9591a792010-12-12 21:57:11 +0000189 INIT_HLIST_HEAD(&orig_node->neigh_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000190 INIT_LIST_HEAD(&orig_node->bond_list);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000191 spin_lock_init(&orig_node->ogm_cnt_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000192 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000193 spin_lock_init(&orig_node->neigh_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000194
195 /* extra reference for return */
196 atomic_set(&orig_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197
Marek Lindner16b1aba2011-01-19 20:01:42 +0000198 orig_node->bat_priv = bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199 memcpy(orig_node->orig, addr, ETH_ALEN);
200 orig_node->router = NULL;
201 orig_node->hna_buff = NULL;
202 orig_node->bcast_seqno_reset = jiffies - 1
203 - msecs_to_jiffies(RESET_PROTECTION_MS);
204 orig_node->batman_seqno_reset = jiffies - 1
205 - msecs_to_jiffies(RESET_PROTECTION_MS);
206
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000207 atomic_set(&orig_node->bond_candidates, 0);
208
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209 size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
210
211 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
212 if (!orig_node->bcast_own)
213 goto free_orig_node;
214
215 size = bat_priv->num_ifaces * sizeof(uint8_t);
216 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
217
218 INIT_LIST_HEAD(&orig_node->frag_list);
219 orig_node->last_frag_packet = 0;
220
221 if (!orig_node->bcast_own_sum)
222 goto free_bcast_own;
223
Marek Lindner7aadf882011-02-18 12:28:09 +0000224 hash_added = hash_add(bat_priv->orig_hash, compare_orig,
225 choose_orig, orig_node, &orig_node->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226 if (hash_added < 0)
227 goto free_bcast_own_sum;
228
229 return orig_node;
230free_bcast_own_sum:
231 kfree(orig_node->bcast_own_sum);
232free_bcast_own:
233 kfree(orig_node->bcast_own);
234free_orig_node:
235 kfree(orig_node);
236 return NULL;
237}
238
239static bool purge_orig_neighbors(struct bat_priv *bat_priv,
240 struct orig_node *orig_node,
241 struct neigh_node **best_neigh_node)
242{
Marek Lindner9591a792010-12-12 21:57:11 +0000243 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244 struct neigh_node *neigh_node;
245 bool neigh_purged = false;
246
247 *best_neigh_node = NULL;
248
Marek Lindnerf987ed62010-12-12 21:57:12 +0000249 spin_lock_bh(&orig_node->neigh_list_lock);
250
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251 /* for all neighbors towards this originator ... */
Marek Lindner9591a792010-12-12 21:57:11 +0000252 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
253 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
255 if ((time_after(jiffies,
256 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
257 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
Marek Lindner1a241a52011-01-19 19:16:10 +0000258 (neigh_node->if_incoming->if_status == IF_NOT_IN_USE) ||
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
260
Marek Lindner1a241a52011-01-19 19:16:10 +0000261 if ((neigh_node->if_incoming->if_status ==
262 IF_INACTIVE) ||
263 (neigh_node->if_incoming->if_status ==
264 IF_NOT_IN_USE) ||
265 (neigh_node->if_incoming->if_status ==
266 IF_TO_BE_REMOVED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267 bat_dbg(DBG_BATMAN, bat_priv,
268 "neighbor purge: originator %pM, "
269 "neighbor: %pM, iface: %s\n",
270 orig_node->orig, neigh_node->addr,
271 neigh_node->if_incoming->net_dev->name);
272 else
273 bat_dbg(DBG_BATMAN, bat_priv,
274 "neighbor timeout: originator %pM, "
275 "neighbor: %pM, last_valid: %lu\n",
276 orig_node->orig, neigh_node->addr,
277 (neigh_node->last_valid / HZ));
278
279 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +0000280
Marek Lindnerf987ed62010-12-12 21:57:12 +0000281 hlist_del_rcu(&neigh_node->list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000282 bonding_candidate_del(orig_node, neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000283 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284 } else {
285 if ((!*best_neigh_node) ||
286 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
287 *best_neigh_node = neigh_node;
288 }
289 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000290
291 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 return neigh_purged;
293}
294
295static bool purge_orig_node(struct bat_priv *bat_priv,
296 struct orig_node *orig_node)
297{
298 struct neigh_node *best_neigh_node;
299
300 if (time_after(jiffies,
301 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
302
303 bat_dbg(DBG_BATMAN, bat_priv,
304 "Originator timeout: originator %pM, last_valid %lu\n",
305 orig_node->orig, (orig_node->last_valid / HZ));
306 return true;
307 } else {
308 if (purge_orig_neighbors(bat_priv, orig_node,
309 &best_neigh_node)) {
310 update_routes(bat_priv, orig_node,
311 best_neigh_node,
312 orig_node->hna_buff,
313 orig_node->hna_buff_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314 }
315 }
316
317 return false;
318}
319
320static void _purge_orig(struct bat_priv *bat_priv)
321{
322 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000323 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000325 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326 struct orig_node *orig_node;
327 int i;
328
329 if (!hash)
330 return;
331
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332 /* for all origins... */
333 for (i = 0; i < hash->size; i++) {
334 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000335 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000337 spin_lock_bh(list_lock);
Marek Lindner7aadf882011-02-18 12:28:09 +0000338 hlist_for_each_entry_safe(orig_node, node, node_tmp,
339 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340 if (purge_orig_node(bat_priv, orig_node)) {
341 if (orig_node->gw_flags)
342 gw_node_delete(bat_priv, orig_node);
Marek Lindner7aadf882011-02-18 12:28:09 +0000343 hlist_del_rcu(node);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000344 orig_node_free_ref(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000345 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346 }
347
348 if (time_after(jiffies, orig_node->last_frag_packet +
349 msecs_to_jiffies(FRAG_TIMEOUT)))
350 frag_list_free(&orig_node->frag_list);
351 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000352 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353 }
354
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355 gw_node_purge(bat_priv);
356 gw_election(bat_priv);
357
358 softif_neigh_purge(bat_priv);
359}
360
361static void purge_orig(struct work_struct *work)
362{
363 struct delayed_work *delayed_work =
364 container_of(work, struct delayed_work, work);
365 struct bat_priv *bat_priv =
366 container_of(delayed_work, struct bat_priv, orig_work);
367
368 _purge_orig(bat_priv);
369 start_purge_timer(bat_priv);
370}
371
372void purge_orig_ref(struct bat_priv *bat_priv)
373{
374 _purge_orig(bat_priv);
375}
376
377int orig_seq_print_text(struct seq_file *seq, void *offset)
378{
379 struct net_device *net_dev = (struct net_device *)seq->private;
380 struct bat_priv *bat_priv = netdev_priv(net_dev);
381 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000382 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384 struct orig_node *orig_node;
385 struct neigh_node *neigh_node;
386 int batman_count = 0;
387 int last_seen_secs;
388 int last_seen_msecs;
389 int i;
390
391 if ((!bat_priv->primary_if) ||
392 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
393 if (!bat_priv->primary_if)
394 return seq_printf(seq, "BATMAN mesh %s disabled - "
395 "please specify interfaces to enable it\n",
396 net_dev->name);
397
398 return seq_printf(seq, "BATMAN mesh %s "
399 "disabled - primary interface not active\n",
400 net_dev->name);
401 }
402
403 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
404 SOURCE_VERSION, REVISION_VERSION_STR,
405 bat_priv->primary_if->net_dev->name,
406 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
407 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
408 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
409 "outgoingIF", "Potential nexthops");
410
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411 for (i = 0; i < hash->size; i++) {
412 head = &hash->table[i];
413
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000414 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000415 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416 if (!orig_node->router)
417 continue;
418
419 if (orig_node->router->tq_avg == 0)
420 continue;
421
422 last_seen_secs = jiffies_to_msecs(jiffies -
423 orig_node->last_valid) / 1000;
424 last_seen_msecs = jiffies_to_msecs(jiffies -
425 orig_node->last_valid) % 1000;
426
427 neigh_node = orig_node->router;
428 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
429 orig_node->orig, last_seen_secs,
430 last_seen_msecs, neigh_node->tq_avg,
431 neigh_node->addr,
432 neigh_node->if_incoming->net_dev->name);
433
Marek Lindner7aadf882011-02-18 12:28:09 +0000434 hlist_for_each_entry_rcu(neigh_node, node_tmp,
Marek Lindnerf987ed62010-12-12 21:57:12 +0000435 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
437 neigh_node->tq_avg);
438 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439
440 seq_printf(seq, "\n");
441 batman_count++;
442 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000443 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444 }
445
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446 if ((batman_count == 0))
447 seq_printf(seq, "No batman nodes in range ...\n");
448
449 return 0;
450}
451
452static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
453{
454 void *data_ptr;
455
456 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
457 GFP_ATOMIC);
458 if (!data_ptr) {
459 pr_err("Can't resize orig: out of memory\n");
460 return -1;
461 }
462
463 memcpy(data_ptr, orig_node->bcast_own,
464 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
465 kfree(orig_node->bcast_own);
466 orig_node->bcast_own = data_ptr;
467
468 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
469 if (!data_ptr) {
470 pr_err("Can't resize orig: out of memory\n");
471 return -1;
472 }
473
474 memcpy(data_ptr, orig_node->bcast_own_sum,
475 (max_if_num - 1) * sizeof(uint8_t));
476 kfree(orig_node->bcast_own_sum);
477 orig_node->bcast_own_sum = data_ptr;
478
479 return 0;
480}
481
Marek Lindnere6c10f42011-02-18 12:33:20 +0000482int orig_hash_add_if(struct hard_iface *hard_iface, int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000484 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000486 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488 struct orig_node *orig_node;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000489 int i, ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490
491 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
492 * if_num */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493 for (i = 0; i < hash->size; i++) {
494 head = &hash->table[i];
495
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000496 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000497 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000498 spin_lock_bh(&orig_node->ogm_cnt_lock);
499 ret = orig_node_add_if(orig_node, max_if_num);
500 spin_unlock_bh(&orig_node->ogm_cnt_lock);
501
502 if (ret == -1)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503 goto err;
504 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000505 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506 }
507
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 return 0;
509
510err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000511 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512 return -ENOMEM;
513}
514
515static int orig_node_del_if(struct orig_node *orig_node,
516 int max_if_num, int del_if_num)
517{
518 void *data_ptr = NULL;
519 int chunk_size;
520
521 /* last interface was removed */
522 if (max_if_num == 0)
523 goto free_bcast_own;
524
525 chunk_size = sizeof(unsigned long) * NUM_WORDS;
526 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
527 if (!data_ptr) {
528 pr_err("Can't resize orig: out of memory\n");
529 return -1;
530 }
531
532 /* copy first part */
533 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
534
535 /* copy second part */
536 memcpy(data_ptr + del_if_num * chunk_size,
537 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
538 (max_if_num - del_if_num) * chunk_size);
539
540free_bcast_own:
541 kfree(orig_node->bcast_own);
542 orig_node->bcast_own = data_ptr;
543
544 if (max_if_num == 0)
545 goto free_own_sum;
546
547 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
548 if (!data_ptr) {
549 pr_err("Can't resize orig: out of memory\n");
550 return -1;
551 }
552
553 memcpy(data_ptr, orig_node->bcast_own_sum,
554 del_if_num * sizeof(uint8_t));
555
556 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
557 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
558 (max_if_num - del_if_num) * sizeof(uint8_t));
559
560free_own_sum:
561 kfree(orig_node->bcast_own_sum);
562 orig_node->bcast_own_sum = data_ptr;
563
564 return 0;
565}
566
Marek Lindnere6c10f42011-02-18 12:33:20 +0000567int orig_hash_del_if(struct hard_iface *hard_iface, int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000569 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000571 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572 struct hlist_head *head;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000573 struct hard_iface *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574 struct orig_node *orig_node;
575 int i, ret;
576
577 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
578 * if_num */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579 for (i = 0; i < hash->size; i++) {
580 head = &hash->table[i];
581
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000582 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000583 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000584 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585 ret = orig_node_del_if(orig_node, max_if_num,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000586 hard_iface->if_num);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000587 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588
589 if (ret == -1)
590 goto err;
591 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000592 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593 }
594
595 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
596 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000597 list_for_each_entry_rcu(hard_iface_tmp, &hardif_list, list) {
598 if (hard_iface_tmp->if_status == IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599 continue;
600
Marek Lindnere6c10f42011-02-18 12:33:20 +0000601 if (hard_iface == hard_iface_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602 continue;
603
Marek Lindnere6c10f42011-02-18 12:33:20 +0000604 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605 continue;
606
Marek Lindnere6c10f42011-02-18 12:33:20 +0000607 if (hard_iface_tmp->if_num > hard_iface->if_num)
608 hard_iface_tmp->if_num--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609 }
610 rcu_read_unlock();
611
Marek Lindnere6c10f42011-02-18 12:33:20 +0000612 hard_iface->if_num = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613 return 0;
614
615err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000616 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617 return -ENOMEM;
618}