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