Staging: batman-adv: multiple mesh clouds
[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 void purge_orig(struct work_struct *work);
33
34 static void start_purge_timer(struct bat_priv *bat_priv)
35 {
36         INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
37         queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
38 }
39
40 int originator_init(struct bat_priv *bat_priv)
41 {
42         unsigned long flags;
43         if (bat_priv->orig_hash)
44                 return 1;
45
46         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
47         bat_priv->orig_hash = hash_new(128, compare_orig, choose_orig);
48
49         if (!bat_priv->orig_hash)
50                 goto err;
51
52         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
53         start_purge_timer(bat_priv);
54         return 1;
55
56 err:
57         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
58         return 0;
59 }
60
61 struct neigh_node *
62 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
63                 uint8_t *neigh, struct batman_if *if_incoming)
64 {
65         struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
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, void *arg)
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         struct bat_priv *bat_priv = (struct bat_priv *)arg;
91
92         /* for all neighbors towards this originator ... */
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
100         frag_list_free(&orig_node->frag_list);
101         hna_global_del_orig(bat_priv, orig_node, "originator timed out");
102
103         kfree(orig_node->bcast_own);
104         kfree(orig_node->bcast_own_sum);
105         kfree(orig_node);
106 }
107
108 void originator_free(struct bat_priv *bat_priv)
109 {
110         unsigned long flags;
111
112         if (!bat_priv->orig_hash)
113                 return;
114
115         cancel_delayed_work_sync(&bat_priv->orig_work);
116
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);
121 }
122
123 /* this function finds or creates an originator entry for the given
124  * address if it does not exits */
125 struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
126 {
127         struct orig_node *orig_node;
128         struct hashtable_t *swaphash;
129         int size;
130
131         orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash, addr));
132
133         if (orig_node)
134                 return orig_node;
135
136         bat_dbg(DBG_BATMAN, bat_priv,
137                 "Creating new originator: %pM\n", addr);
138
139         orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
140         if (!orig_node)
141                 return NULL;
142
143         INIT_LIST_HEAD(&orig_node->neigh_list);
144
145         memcpy(orig_node->orig, addr, ETH_ALEN);
146         orig_node->router = NULL;
147         orig_node->hna_buff = NULL;
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);
152
153         size = bat_priv->num_ifaces * sizeof(TYPE_OF_WORD) * NUM_WORDS;
154
155         orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
156         if (!orig_node->bcast_own)
157                 goto free_orig_node;
158
159         size = bat_priv->num_ifaces * sizeof(uint8_t);
160         orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
161
162         INIT_LIST_HEAD(&orig_node->frag_list);
163         orig_node->last_frag_packet = 0;
164
165         if (!orig_node->bcast_own_sum)
166                 goto free_bcast_own;
167
168         if (hash_add(bat_priv->orig_hash, orig_node) < 0)
169                 goto free_bcast_own_sum;
170
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);
174
175                 if (!swaphash)
176                         bat_dbg(DBG_BATMAN, bat_priv,
177                                 "Couldn't resize orig hash table\n");
178                 else
179                         bat_priv->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 bat_priv *bat_priv,
193                                  struct orig_node *orig_node,
194                                  struct neigh_node **best_neigh_node)
195 {
196         struct list_head *list_pos, *list_pos_tmp;
197         struct neigh_node *neigh_node;
198         bool neigh_purged = false;
199
200         *best_neigh_node = NULL;
201
202         /* for all neighbors towards this originator ... */
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
206                 if ((time_after(jiffies,
207                         neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
208                     (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
209                     (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
210
211                         if (neigh_node->if_incoming->if_status ==
212                                                         IF_TO_BE_REMOVED)
213                                 bat_dbg(DBG_BATMAN, bat_priv,
214                                         "neighbor purge: originator %pM, "
215                                         "neighbor: %pM, iface: %s\n",
216                                         orig_node->orig, neigh_node->addr,
217                                         neigh_node->if_incoming->dev);
218                         else
219                                 bat_dbg(DBG_BATMAN, bat_priv,
220                                         "neighbor timeout: originator %pM, "
221                                         "neighbor: %pM, last_valid: %lu\n",
222                                         orig_node->orig, neigh_node->addr,
223                                         (neigh_node->last_valid / HZ));
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
237 static bool purge_orig_node(struct bat_priv *bat_priv,
238                             struct orig_node *orig_node)
239 {
240         struct neigh_node *best_neigh_node;
241
242         if (time_after(jiffies,
243                 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
244
245                 bat_dbg(DBG_BATMAN, bat_priv,
246                         "Originator timeout: originator %pM, last_valid %lu\n",
247                         orig_node->orig, (orig_node->last_valid / HZ));
248                 return true;
249         } else {
250                 if (purge_orig_neighbors(bat_priv, orig_node,
251                                                         &best_neigh_node)) {
252                         update_routes(bat_priv, orig_node,
253                                       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 static void _purge_orig(struct bat_priv *bat_priv)
266 {
267         HASHIT(hashit);
268         struct orig_node *orig_node;
269         unsigned long flags;
270
271         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
272
273         /* for all origins... */
274         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
275                 orig_node = hashit.bucket->data;
276
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                 }
281
282                 if (time_after(jiffies, (orig_node->last_frag_packet +
283                                         msecs_to_jiffies(FRAG_TIMEOUT))))
284                         frag_list_free(&orig_node->frag_list);
285         }
286
287         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
288
289 }
290
291 static 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 }
301
302 void purge_orig_ref(struct bat_priv *bat_priv)
303 {
304         _purge_orig(bat_priv);
305 }
306
307 int orig_seq_print_text(struct seq_file *seq, void *offset)
308 {
309         HASHIT(hashit);
310         struct net_device *net_dev = (struct net_device *)seq->private;
311         struct bat_priv *bat_priv = netdev_priv(net_dev);
312         struct orig_node *orig_node;
313         struct neigh_node *neigh_node;
314         int batman_count = 0;
315         int last_seen_secs;
316         int last_seen_msecs;
317         unsigned long flags;
318         char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
319
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 - "
324                                      "please specify interfaces to enable it\n",
325                                      net_dev->name);
326
327                 return seq_printf(seq, "BATMAN mesh %s "
328                                   "disabled - primary interface not active\n",
329                                   net_dev->name);
330         }
331
332         rcu_read_lock();
333         seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s (%s)]\n",
334                    SOURCE_VERSION, REVISION_VERSION_STR,
335                    bat_priv->primary_if->dev, bat_priv->primary_if->addr_str,
336                    net_dev->name);
337         seq_printf(seq, "  %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
338                    "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
339                    "outgoingIF", "Potential nexthops");
340         rcu_read_unlock();
341
342         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
343
344         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
345
346                 orig_node = hashit.bucket->data;
347
348                 if (!orig_node->router)
349                         continue;
350
351                 if (orig_node->router->tq_avg == 0)
352                         continue;
353
354                 addr_to_string(orig_str, orig_node->orig);
355                 addr_to_string(router_str, orig_node->router->addr);
356                 last_seen_secs = jiffies_to_msecs(jiffies -
357                                                 orig_node->last_valid) / 1000;
358                 last_seen_msecs = jiffies_to_msecs(jiffies -
359                                                 orig_node->last_valid) % 1000;
360
361                 seq_printf(seq, "%-17s %4i.%03is   (%3i) %17s [%10s]:",
362                            orig_str, last_seen_secs, last_seen_msecs,
363                            orig_node->router->tq_avg, router_str,
364                            orig_node->router->if_incoming->dev);
365
366                 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
367                         addr_to_string(orig_str, neigh_node->addr);
368                         seq_printf(seq, " %17s (%3i)", orig_str,
369                                            neigh_node->tq_avg);
370                 }
371
372                 seq_printf(seq, "\n");
373                 batman_count++;
374         }
375
376         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
377
378         if ((batman_count == 0))
379                 seq_printf(seq, "No batman nodes in range ...\n");
380
381         return 0;
382 }
383
384 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
385 {
386         void *data_ptr;
387
388         data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
389                            GFP_ATOMIC);
390         if (!data_ptr) {
391                 pr_err("Can't resize orig: out of memory\n");
392                 return -1;
393         }
394
395         memcpy(data_ptr, orig_node->bcast_own,
396                (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
397         kfree(orig_node->bcast_own);
398         orig_node->bcast_own = data_ptr;
399
400         data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
401         if (!data_ptr) {
402                 pr_err("Can't resize orig: out of memory\n");
403                 return -1;
404         }
405
406         memcpy(data_ptr, orig_node->bcast_own_sum,
407                (max_if_num - 1) * sizeof(uint8_t));
408         kfree(orig_node->bcast_own_sum);
409         orig_node->bcast_own_sum = data_ptr;
410
411         return 0;
412 }
413
414 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
415 {
416         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
417         struct orig_node *orig_node;
418         unsigned long flags;
419         HASHIT(hashit);
420
421         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
422          * if_num */
423         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
424
425         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
426                 orig_node = hashit.bucket->data;
427
428                 if (orig_node_add_if(orig_node, max_if_num) == -1)
429                         goto err;
430         }
431
432         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
433         return 0;
434
435 err:
436         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
437         return -ENOMEM;
438 }
439
440 static int orig_node_del_if(struct orig_node *orig_node,
441                      int max_if_num, int del_if_num)
442 {
443         void *data_ptr = NULL;
444         int chunk_size;
445
446         /* last interface was removed */
447         if (max_if_num == 0)
448                 goto free_bcast_own;
449
450         chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
451         data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
452         if (!data_ptr) {
453                 pr_err("Can't resize orig: out of memory\n");
454                 return -1;
455         }
456
457         /* copy first part */
458         memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
459
460         /* copy second part */
461         memcpy(data_ptr,
462                orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
463                (max_if_num - del_if_num) * chunk_size);
464
465 free_bcast_own:
466         kfree(orig_node->bcast_own);
467         orig_node->bcast_own = data_ptr;
468
469         if (max_if_num == 0)
470                 goto free_own_sum;
471
472         data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
473         if (!data_ptr) {
474                 pr_err("Can't resize orig: out of memory\n");
475                 return -1;
476         }
477
478         memcpy(data_ptr, orig_node->bcast_own_sum,
479                del_if_num * sizeof(uint8_t));
480
481         memcpy(data_ptr,
482                orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
483                (max_if_num - del_if_num) * sizeof(uint8_t));
484
485 free_own_sum:
486         kfree(orig_node->bcast_own_sum);
487         orig_node->bcast_own_sum = data_ptr;
488
489         return 0;
490 }
491
492 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
493 {
494         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
495         struct batman_if *batman_if_tmp;
496         struct orig_node *orig_node;
497         unsigned long flags;
498         HASHIT(hashit);
499         int ret;
500
501         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
502          * if_num */
503         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
504
505         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
506                 orig_node = hashit.bucket->data;
507
508                 ret = orig_node_del_if(orig_node, max_if_num,
509                                        batman_if->if_num);
510
511                 if (ret == -1)
512                         goto err;
513         }
514
515         /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
516         rcu_read_lock();
517         list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
518                 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
519                         continue;
520
521                 if (batman_if == batman_if_tmp)
522                         continue;
523
524                 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
525                         continue;
526
527                 if (batman_if_tmp->if_num > batman_if->if_num)
528                         batman_if_tmp->if_num--;
529         }
530         rcu_read_unlock();
531
532         batman_if->if_num = -1;
533         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
534         return 0;
535
536 err:
537         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
538         return -ENOMEM;
539 }