]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/batman-adv/originator.c
Staging: batman-adv: Fix resizing of broadcast seqno buffers on if deletion
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / batman-adv / originator.c
CommitLineData
8a2e042c 1/*
9b6d10b7 2 * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors:
8a2e042c
AL
3 *
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"
14741240 29#include "hard-interface.h"
e63760e5 30#include "unicast.h"
8a2e042c 31
8c70f138 32static void purge_orig(struct work_struct *work);
8a2e042c 33
8c70f138 34static void start_purge_timer(struct bat_priv *bat_priv)
8a2e042c 35{
8c70f138
ML
36 INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
37 queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
8a2e042c
AL
38}
39
8c70f138 40int originator_init(struct bat_priv *bat_priv)
8a2e042c 41{
e7017195 42 unsigned long flags;
8c70f138 43 if (bat_priv->orig_hash)
8a2e042c
AL
44 return 1;
45
8c70f138
ML
46 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
47 bat_priv->orig_hash = hash_new(128, compare_orig, choose_orig);
8a2e042c 48
8c70f138 49 if (!bat_priv->orig_hash)
8a2e042c
AL
50 goto err;
51
8c70f138
ML
52 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
53 start_purge_timer(bat_priv);
8a2e042c
AL
54 return 1;
55
56err:
8c70f138 57 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
8a2e042c
AL
58 return 0;
59}
60
8a2e042c
AL
61struct neigh_node *
62create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
63 uint8_t *neigh, struct batman_if *if_incoming)
64{
6a0e9fa8 65 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
8a2e042c
AL
66 struct neigh_node *neigh_node;
67
84ec0864
ML
68 bat_dbg(DBG_BATMAN, bat_priv,
69 "Creating new last-hop neighbor of originator\n");
8a2e042c 70
abad5446 71 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
c4bf05d3
SW
72 if (!neigh_node)
73 return NULL;
74
8a2e042c
AL
75 INIT_LIST_HEAD(&neigh_node->list);
76
77 memcpy(neigh_node->addr, neigh, ETH_ALEN);
78 neigh_node->orig_node = orig_neigh_node;
79 neigh_node->if_incoming = if_incoming;
80
81 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
82 return neigh_node;
83}
84
6a0e9fa8 85static void free_orig_node(void *data, void *arg)
8a2e042c
AL
86{
87 struct list_head *list_pos, *list_pos_tmp;
88 struct neigh_node *neigh_node;
89 struct orig_node *orig_node = (struct orig_node *)data;
6a0e9fa8 90 struct bat_priv *bat_priv = (struct bat_priv *)arg;
8a2e042c 91
5ea84fa3 92 /* for all neighbors towards this originator ... */
8a2e042c
AL
93 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
94 neigh_node = list_entry(list_pos, struct neigh_node, list);
95
96 list_del(list_pos);
97 kfree(neigh_node);
98 }
99
e63760e5 100 frag_list_free(&orig_node->frag_list);
6a0e9fa8 101 hna_global_del_orig(bat_priv, orig_node, "originator timed out");
8a2e042c
AL
102
103 kfree(orig_node->bcast_own);
104 kfree(orig_node->bcast_own_sum);
105 kfree(orig_node);
106}
107
8c70f138 108void originator_free(struct bat_priv *bat_priv)
42fa1b92
SE
109{
110 unsigned long flags;
111
8c70f138 112 if (!bat_priv->orig_hash)
42fa1b92
SE
113 return;
114
8c70f138 115 cancel_delayed_work_sync(&bat_priv->orig_work);
42fa1b92 116
8c70f138
ML
117 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
118 hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv);
119 bat_priv->orig_hash = NULL;
120 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
42fa1b92
SE
121}
122
8a2e042c
AL
123/* this function finds or creates an originator entry for the given
124 * address if it does not exits */
6a0e9fa8 125struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
8a2e042c
AL
126{
127 struct orig_node *orig_node;
128 struct hashtable_t *swaphash;
8a2e042c
AL
129 int size;
130
8c70f138 131 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash, addr));
8a2e042c 132
8c70f138 133 if (orig_node)
8a2e042c
AL
134 return orig_node;
135
84ec0864
ML
136 bat_dbg(DBG_BATMAN, bat_priv,
137 "Creating new originator: %pM\n", addr);
8a2e042c 138
abad5446 139 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
c4bf05d3
SW
140 if (!orig_node)
141 return NULL;
142
8a2e042c
AL
143 INIT_LIST_HEAD(&orig_node->neigh_list);
144
145 memcpy(orig_node->orig, addr, ETH_ALEN);
146 orig_node->router = NULL;
8a2e042c 147 orig_node->hna_buff = NULL;
6d45d8df
SE
148 orig_node->bcast_seqno_reset = jiffies - 1
149 - msecs_to_jiffies(RESET_PROTECTION_MS);
150 orig_node->batman_seqno_reset = jiffies - 1
151 - msecs_to_jiffies(RESET_PROTECTION_MS);
8a2e042c 152
208e13e4 153 size = bat_priv->num_ifaces * sizeof(TYPE_OF_WORD) * NUM_WORDS;
8a2e042c 154
abad5446 155 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
c4bf05d3
SW
156 if (!orig_node->bcast_own)
157 goto free_orig_node;
158
208e13e4 159 size = bat_priv->num_ifaces * sizeof(uint8_t);
abad5446 160 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
e63760e5
AL
161
162 INIT_LIST_HEAD(&orig_node->frag_list);
163 orig_node->last_frag_packet = 0;
164
c4bf05d3
SW
165 if (!orig_node->bcast_own_sum)
166 goto free_bcast_own;
167
8c70f138 168 if (hash_add(bat_priv->orig_hash, orig_node) < 0)
c4bf05d3 169 goto free_bcast_own_sum;
8a2e042c 170
8c70f138
ML
171 if (bat_priv->orig_hash->elements * 4 > bat_priv->orig_hash->size) {
172 swaphash = hash_resize(bat_priv->orig_hash,
173 bat_priv->orig_hash->size * 2);
8a2e042c 174
8c70f138 175 if (!swaphash)
6a0e9fa8 176 bat_dbg(DBG_BATMAN, bat_priv,
c1641862 177 "Couldn't resize orig hash table\n");
8a2e042c 178 else
8c70f138 179 bat_priv->orig_hash = swaphash;
8a2e042c
AL
180 }
181
182 return orig_node;
c4bf05d3
SW
183free_bcast_own_sum:
184 kfree(orig_node->bcast_own_sum);
185free_bcast_own:
186 kfree(orig_node->bcast_own);
187free_orig_node:
188 kfree(orig_node);
189 return NULL;
8a2e042c
AL
190}
191
6a0e9fa8
ML
192static bool purge_orig_neighbors(struct bat_priv *bat_priv,
193 struct orig_node *orig_node,
8a2e042c
AL
194 struct neigh_node **best_neigh_node)
195{
196 struct list_head *list_pos, *list_pos_tmp;
8a2e042c
AL
197 struct neigh_node *neigh_node;
198 bool neigh_purged = false;
199
200 *best_neigh_node = NULL;
201
5ea84fa3 202 /* for all neighbors towards this originator ... */
8a2e042c
AL
203 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
204 neigh_node = list_entry(list_pos, struct neigh_node, list);
205
208e13e4 206 if ((time_after(jiffies,
b6be4535 207 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
8c70f138
ML
208 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
209 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
208e13e4
ML
210
211 if (neigh_node->if_incoming->if_status ==
212 IF_TO_BE_REMOVED)
84ec0864 213 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df
SE
214 "neighbor purge: originator %pM, "
215 "neighbor: %pM, iface: %s\n",
208e13e4 216 orig_node->orig, neigh_node->addr,
57b7117a 217 neigh_node->if_incoming->net_dev->name);
208e13e4 218 else
84ec0864 219 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df
SE
220 "neighbor timeout: originator %pM, "
221 "neighbor: %pM, last_valid: %lu\n",
208e13e4
ML
222 orig_node->orig, neigh_node->addr,
223 (neigh_node->last_valid / HZ));
8a2e042c
AL
224
225 neigh_purged = true;
226 list_del(list_pos);
227 kfree(neigh_node);
228 } else {
229 if ((*best_neigh_node == NULL) ||
230 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
231 *best_neigh_node = neigh_node;
232 }
233 }
234 return neigh_purged;
235}
236
6a0e9fa8
ML
237static bool purge_orig_node(struct bat_priv *bat_priv,
238 struct orig_node *orig_node)
8a2e042c
AL
239{
240 struct neigh_node *best_neigh_node;
8a2e042c
AL
241
242 if (time_after(jiffies,
b6be4535 243 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
8a2e042c 244
84ec0864 245 bat_dbg(DBG_BATMAN, bat_priv,
b9b27e4e
AL
246 "Originator timeout: originator %pM, last_valid %lu\n",
247 orig_node->orig, (orig_node->last_valid / HZ));
8a2e042c
AL
248 return true;
249 } else {
6a0e9fa8
ML
250 if (purge_orig_neighbors(bat_priv, orig_node,
251 &best_neigh_node)) {
252 update_routes(bat_priv, orig_node,
253 best_neigh_node,
8a2e042c
AL
254 orig_node->hna_buff,
255 orig_node->hna_buff_len);
e35fd5ec
SW
256 /* update bonding candidates, we could have lost
257 * some candidates. */
258 update_bonding_candidates(bat_priv, orig_node);
259 }
8a2e042c 260 }
14741240 261
8a2e042c
AL
262 return false;
263}
264
8c70f138 265static void _purge_orig(struct bat_priv *bat_priv)
8a2e042c 266{
b6c35976 267 HASHIT(hashit);
8a2e042c 268 struct orig_node *orig_node;
e7017195 269 unsigned long flags;
8a2e042c 270
8c70f138 271 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
8a2e042c
AL
272
273 /* for all origins... */
8c70f138 274 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
b6c35976 275 orig_node = hashit.bucket->data;
6a0e9fa8 276
8c70f138
ML
277 if (purge_orig_node(bat_priv, orig_node)) {
278 hash_remove_bucket(bat_priv->orig_hash, &hashit);
279 free_orig_node(orig_node, bat_priv);
280 }
e63760e5
AL
281
282 if (time_after(jiffies, (orig_node->last_frag_packet +
6a0e9fa8 283 msecs_to_jiffies(FRAG_TIMEOUT))))
e63760e5 284 frag_list_free(&orig_node->frag_list);
8a2e042c
AL
285 }
286
8c70f138
ML
287 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
288
289}
290
291static void purge_orig(struct work_struct *work)
292{
293 struct delayed_work *delayed_work =
294 container_of(work, struct delayed_work, work);
295 struct bat_priv *bat_priv =
296 container_of(delayed_work, struct bat_priv, orig_work);
297
298 _purge_orig(bat_priv);
299 start_purge_timer(bat_priv);
300}
8a2e042c 301
8c70f138
ML
302void purge_orig_ref(struct bat_priv *bat_priv)
303{
304 _purge_orig(bat_priv);
8a2e042c
AL
305}
306
4caecbc0 307int orig_seq_print_text(struct seq_file *seq, void *offset)
47fdf097
ML
308{
309 HASHIT(hashit);
4caecbc0 310 struct net_device *net_dev = (struct net_device *)seq->private;
208e13e4 311 struct bat_priv *bat_priv = netdev_priv(net_dev);
47fdf097
ML
312 struct orig_node *orig_node;
313 struct neigh_node *neigh_node;
4caecbc0 314 int batman_count = 0;
80f6c34b
LL
315 int last_seen_secs;
316 int last_seen_msecs;
47fdf097
ML
317 unsigned long flags;
318 char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
319
4caecbc0
SE
320 if ((!bat_priv->primary_if) ||
321 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
322 if (!bat_priv->primary_if)
323 return seq_printf(seq, "BATMAN mesh %s disabled - "
6d45d8df
SE
324 "please specify interfaces to enable it\n",
325 net_dev->name);
14741240 326
4caecbc0
SE
327 return seq_printf(seq, "BATMAN mesh %s "
328 "disabled - primary interface not active\n",
329 net_dev->name);
14741240
ML
330 }
331
80f6c34b
LL
332 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s (%s)]\n",
333 SOURCE_VERSION, REVISION_VERSION_STR,
57b7117a
SE
334 bat_priv->primary_if->net_dev->name,
335 bat_priv->primary_if->addr_str, net_dev->name);
80f6c34b
LL
336 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
337 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
338 "outgoingIF", "Potential nexthops");
47fdf097 339
8c70f138 340 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
47fdf097 341
8c70f138 342 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
47fdf097
ML
343
344 orig_node = hashit.bucket->data;
345
346 if (!orig_node->router)
347 continue;
348
349 if (orig_node->router->tq_avg == 0)
350 continue;
351
47fdf097
ML
352 addr_to_string(orig_str, orig_node->orig);
353 addr_to_string(router_str, orig_node->router->addr);
80f6c34b
LL
354 last_seen_secs = jiffies_to_msecs(jiffies -
355 orig_node->last_valid) / 1000;
356 last_seen_msecs = jiffies_to_msecs(jiffies -
357 orig_node->last_valid) % 1000;
358
359 seq_printf(seq, "%-17s %4i.%03is (%3i) %17s [%10s]:",
360 orig_str, last_seen_secs, last_seen_msecs,
361 orig_node->router->tq_avg, router_str,
57b7117a 362 orig_node->router->if_incoming->net_dev->name);
47fdf097
ML
363
364 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
365 addr_to_string(orig_str, neigh_node->addr);
4caecbc0 366 seq_printf(seq, " %17s (%3i)", orig_str,
47fdf097
ML
367 neigh_node->tq_avg);
368 }
369
4caecbc0 370 seq_printf(seq, "\n");
47fdf097 371 batman_count++;
47fdf097
ML
372 }
373
8c70f138 374 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
47fdf097 375
4caecbc0
SE
376 if ((batman_count == 0))
377 seq_printf(seq, "No batman nodes in range ...\n");
47fdf097 378
4caecbc0 379 return 0;
47fdf097 380}
8a2e042c 381
208e13e4
ML
382static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
383{
384 void *data_ptr;
385
386 data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
387 GFP_ATOMIC);
388 if (!data_ptr) {
c1641862 389 pr_err("Can't resize orig: out of memory\n");
208e13e4
ML
390 return -1;
391 }
392
393 memcpy(data_ptr, orig_node->bcast_own,
394 (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
395 kfree(orig_node->bcast_own);
396 orig_node->bcast_own = data_ptr;
397
398 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
399 if (!data_ptr) {
c1641862 400 pr_err("Can't resize orig: out of memory\n");
208e13e4
ML
401 return -1;
402 }
403
404 memcpy(data_ptr, orig_node->bcast_own_sum,
405 (max_if_num - 1) * sizeof(uint8_t));
406 kfree(orig_node->bcast_own_sum);
407 orig_node->bcast_own_sum = data_ptr;
408
409 return 0;
410}
411
412int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
413{
8c70f138 414 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
208e13e4 415 struct orig_node *orig_node;
9abc1023 416 unsigned long flags;
208e13e4
ML
417 HASHIT(hashit);
418
419 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
420 * if_num */
8c70f138 421 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
208e13e4 422
8c70f138 423 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
208e13e4
ML
424 orig_node = hashit.bucket->data;
425
426 if (orig_node_add_if(orig_node, max_if_num) == -1)
427 goto err;
428 }
429
8c70f138 430 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
208e13e4
ML
431 return 0;
432
433err:
8c70f138 434 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
208e13e4
ML
435 return -ENOMEM;
436}
437
438static int orig_node_del_if(struct orig_node *orig_node,
439 int max_if_num, int del_if_num)
440{
441 void *data_ptr = NULL;
442 int chunk_size;
443
444 /* last interface was removed */
445 if (max_if_num == 0)
446 goto free_bcast_own;
447
448 chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
449 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
450 if (!data_ptr) {
c1641862 451 pr_err("Can't resize orig: out of memory\n");
208e13e4
ML
452 return -1;
453 }
454
455 /* copy first part */
456 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
457
458 /* copy second part */
5dfa2ba9 459 memcpy(data_ptr + del_if_num * chunk_size,
208e13e4
ML
460 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
461 (max_if_num - del_if_num) * chunk_size);
462
463free_bcast_own:
464 kfree(orig_node->bcast_own);
465 orig_node->bcast_own = data_ptr;
466
467 if (max_if_num == 0)
468 goto free_own_sum;
469
470 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
471 if (!data_ptr) {
c1641862 472 pr_err("Can't resize orig: out of memory\n");
208e13e4
ML
473 return -1;
474 }
475
476 memcpy(data_ptr, orig_node->bcast_own_sum,
477 del_if_num * sizeof(uint8_t));
478
5dfa2ba9 479 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
208e13e4
ML
480 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
481 (max_if_num - del_if_num) * sizeof(uint8_t));
482
483free_own_sum:
484 kfree(orig_node->bcast_own_sum);
485 orig_node->bcast_own_sum = data_ptr;
486
487 return 0;
488}
489
490int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
491{
8c70f138 492 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
208e13e4
ML
493 struct batman_if *batman_if_tmp;
494 struct orig_node *orig_node;
9abc1023 495 unsigned long flags;
208e13e4
ML
496 HASHIT(hashit);
497 int ret;
498
499 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
500 * if_num */
8c70f138 501 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
208e13e4 502
8c70f138 503 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
208e13e4
ML
504 orig_node = hashit.bucket->data;
505
506 ret = orig_node_del_if(orig_node, max_if_num,
507 batman_if->if_num);
508
509 if (ret == -1)
510 goto err;
511 }
512
513 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
514 rcu_read_lock();
515 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
516 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
517 continue;
518
519 if (batman_if == batman_if_tmp)
520 continue;
521
8c70f138
ML
522 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
523 continue;
524
208e13e4
ML
525 if (batman_if_tmp->if_num > batman_if->if_num)
526 batman_if_tmp->if_num--;
527 }
528 rcu_read_unlock();
529
530 batman_if->if_num = -1;
8c70f138 531 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
208e13e4
ML
532 return 0;
533
534err:
8c70f138 535 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
208e13e4
ML
536 return -ENOMEM;
537}