Staging: batman-adv: Unify sysfs file names with their bat_priv atomics
[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 #include "soft-interface.h"
32
33 static void purge_orig(struct work_struct *work);
34
35 static void start_purge_timer(struct bat_priv *bat_priv)
36 {
37         INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
38         queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
39 }
40
41 int originator_init(struct bat_priv *bat_priv)
42 {
43         unsigned long flags;
44         if (bat_priv->orig_hash)
45                 return 1;
46
47         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
48         bat_priv->orig_hash = hash_new(128, compare_orig, choose_orig);
49
50         if (!bat_priv->orig_hash)
51                 goto err;
52
53         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
54         start_purge_timer(bat_priv);
55         return 1;
56
57 err:
58         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
59         return 0;
60 }
61
62 struct neigh_node *
63 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
64                 uint8_t *neigh, struct batman_if *if_incoming)
65 {
66         struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
67         struct neigh_node *neigh_node;
68
69         bat_dbg(DBG_BATMAN, bat_priv,
70                 "Creating new last-hop neighbor of originator\n");
71
72         neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
73         if (!neigh_node)
74                 return NULL;
75
76         INIT_LIST_HEAD(&neigh_node->list);
77
78         memcpy(neigh_node->addr, neigh, ETH_ALEN);
79         neigh_node->orig_node = orig_neigh_node;
80         neigh_node->if_incoming = if_incoming;
81
82         list_add_tail(&neigh_node->list, &orig_node->neigh_list);
83         return neigh_node;
84 }
85
86 static void free_orig_node(void *data, void *arg)
87 {
88         struct list_head *list_pos, *list_pos_tmp;
89         struct neigh_node *neigh_node;
90         struct orig_node *orig_node = (struct orig_node *)data;
91         struct bat_priv *bat_priv = (struct bat_priv *)arg;
92
93         /* for all neighbors towards this originator ... */
94         list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
95                 neigh_node = list_entry(list_pos, struct neigh_node, list);
96
97                 list_del(list_pos);
98                 kfree(neigh_node);
99         }
100
101         frag_list_free(&orig_node->frag_list);
102         hna_global_del_orig(bat_priv, orig_node, "originator timed out");
103
104         kfree(orig_node->bcast_own);
105         kfree(orig_node->bcast_own_sum);
106         kfree(orig_node);
107 }
108
109 void originator_free(struct bat_priv *bat_priv)
110 {
111         unsigned long flags;
112
113         if (!bat_priv->orig_hash)
114                 return;
115
116         cancel_delayed_work_sync(&bat_priv->orig_work);
117
118         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
119         hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv);
120         bat_priv->orig_hash = NULL;
121         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
122 }
123
124 /* this function finds or creates an originator entry for the given
125  * address if it does not exits */
126 struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
127 {
128         struct orig_node *orig_node;
129         struct hashtable_t *swaphash;
130         int size;
131
132         orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash, addr));
133
134         if (orig_node)
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(bat_priv->orig_hash, orig_node) < 0)
170                 goto free_bcast_own_sum;
171
172         if (bat_priv->orig_hash->elements * 4 > bat_priv->orig_hash->size) {
173                 swaphash = hash_resize(bat_priv->orig_hash,
174                                        bat_priv->orig_hash->size * 2);
175
176                 if (!swaphash)
177                         bat_dbg(DBG_BATMAN, bat_priv,
178                                 "Couldn't resize orig hash table\n");
179                 else
180                         bat_priv->orig_hash = swaphash;
181         }
182
183         return orig_node;
184 free_bcast_own_sum:
185         kfree(orig_node->bcast_own_sum);
186 free_bcast_own:
187         kfree(orig_node->bcast_own);
188 free_orig_node:
189         kfree(orig_node);
190         return NULL;
191 }
192
193 static bool purge_orig_neighbors(struct bat_priv *bat_priv,
194                                  struct orig_node *orig_node,
195                                  struct neigh_node **best_neigh_node)
196 {
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 == IF_INACTIVE) ||
210                     (neigh_node->if_incoming->if_status == 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->net_dev->name);
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 bat_priv *bat_priv,
239                             struct orig_node *orig_node)
240 {
241         struct neigh_node *best_neigh_node;
242
243         if (time_after(jiffies,
244                 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
245
246                 bat_dbg(DBG_BATMAN, bat_priv,
247                         "Originator timeout: originator %pM, last_valid %lu\n",
248                         orig_node->orig, (orig_node->last_valid / HZ));
249                 return true;
250         } else {
251                 if (purge_orig_neighbors(bat_priv, orig_node,
252                                                         &best_neigh_node)) {
253                         update_routes(bat_priv, orig_node,
254                                       best_neigh_node,
255                                       orig_node->hna_buff,
256                                       orig_node->hna_buff_len);
257                         /* update bonding candidates, we could have lost
258                          * some candidates. */
259                         update_bonding_candidates(bat_priv, orig_node);
260                 }
261         }
262
263         return false;
264 }
265
266 static void _purge_orig(struct bat_priv *bat_priv)
267 {
268         HASHIT(hashit);
269         struct orig_node *orig_node;
270         unsigned long flags;
271
272         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
273
274         /* for all origins... */
275         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
276                 orig_node = hashit.bucket->data;
277
278                 if (purge_orig_node(bat_priv, orig_node)) {
279                         hash_remove_bucket(bat_priv->orig_hash, &hashit);
280                         free_orig_node(orig_node, bat_priv);
281                 }
282
283                 if (time_after(jiffies, (orig_node->last_frag_packet +
284                                         msecs_to_jiffies(FRAG_TIMEOUT))))
285                         frag_list_free(&orig_node->frag_list);
286         }
287
288         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
289
290         softif_neigh_purge(bat_priv);
291 }
292
293 static void purge_orig(struct work_struct *work)
294 {
295         struct delayed_work *delayed_work =
296                 container_of(work, struct delayed_work, work);
297         struct bat_priv *bat_priv =
298                 container_of(delayed_work, struct bat_priv, orig_work);
299
300         _purge_orig(bat_priv);
301         start_purge_timer(bat_priv);
302 }
303
304 void purge_orig_ref(struct bat_priv *bat_priv)
305 {
306         _purge_orig(bat_priv);
307 }
308
309 int orig_seq_print_text(struct seq_file *seq, void *offset)
310 {
311         HASHIT(hashit);
312         struct net_device *net_dev = (struct net_device *)seq->private;
313         struct bat_priv *bat_priv = netdev_priv(net_dev);
314         struct orig_node *orig_node;
315         struct neigh_node *neigh_node;
316         int batman_count = 0;
317         int last_seen_secs;
318         int last_seen_msecs;
319         unsigned long flags;
320
321         if ((!bat_priv->primary_if) ||
322             (bat_priv->primary_if->if_status != IF_ACTIVE)) {
323                 if (!bat_priv->primary_if)
324                         return seq_printf(seq, "BATMAN mesh %s disabled - "
325                                      "please specify interfaces to enable it\n",
326                                      net_dev->name);
327
328                 return seq_printf(seq, "BATMAN mesh %s "
329                                   "disabled - primary interface not active\n",
330                                   net_dev->name);
331         }
332
333         seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
334                    SOURCE_VERSION, REVISION_VERSION_STR,
335                    bat_priv->primary_if->net_dev->name,
336                    bat_priv->primary_if->net_dev->dev_addr, 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
341         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
342
343         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
344
345                 orig_node = hashit.bucket->data;
346
347                 if (!orig_node->router)
348                         continue;
349
350                 if (orig_node->router->tq_avg == 0)
351                         continue;
352
353                 last_seen_secs = jiffies_to_msecs(jiffies -
354                                                 orig_node->last_valid) / 1000;
355                 last_seen_msecs = jiffies_to_msecs(jiffies -
356                                                 orig_node->last_valid) % 1000;
357
358                 seq_printf(seq, "%pM %4i.%03is   (%3i) %pM [%10s]:",
359                            orig_node->orig, last_seen_secs, last_seen_msecs,
360                            orig_node->router->tq_avg, orig_node->router->addr,
361                            orig_node->router->if_incoming->net_dev->name);
362
363                 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
364                         seq_printf(seq, " %pM (%3i)", neigh_node->addr,
365                                            neigh_node->tq_avg);
366                 }
367
368                 seq_printf(seq, "\n");
369                 batman_count++;
370         }
371
372         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
373
374         if ((batman_count == 0))
375                 seq_printf(seq, "No batman nodes in range ...\n");
376
377         return 0;
378 }
379
380 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
381 {
382         void *data_ptr;
383
384         data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
385                            GFP_ATOMIC);
386         if (!data_ptr) {
387                 pr_err("Can't resize orig: out of memory\n");
388                 return -1;
389         }
390
391         memcpy(data_ptr, orig_node->bcast_own,
392                (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
393         kfree(orig_node->bcast_own);
394         orig_node->bcast_own = data_ptr;
395
396         data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
397         if (!data_ptr) {
398                 pr_err("Can't resize orig: out of memory\n");
399                 return -1;
400         }
401
402         memcpy(data_ptr, orig_node->bcast_own_sum,
403                (max_if_num - 1) * sizeof(uint8_t));
404         kfree(orig_node->bcast_own_sum);
405         orig_node->bcast_own_sum = data_ptr;
406
407         return 0;
408 }
409
410 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
411 {
412         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
413         struct orig_node *orig_node;
414         unsigned long flags;
415         HASHIT(hashit);
416
417         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
418          * if_num */
419         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
420
421         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
422                 orig_node = hashit.bucket->data;
423
424                 if (orig_node_add_if(orig_node, max_if_num) == -1)
425                         goto err;
426         }
427
428         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
429         return 0;
430
431 err:
432         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
433         return -ENOMEM;
434 }
435
436 static int orig_node_del_if(struct orig_node *orig_node,
437                      int max_if_num, int del_if_num)
438 {
439         void *data_ptr = NULL;
440         int chunk_size;
441
442         /* last interface was removed */
443         if (max_if_num == 0)
444                 goto free_bcast_own;
445
446         chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
447         data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
448         if (!data_ptr) {
449                 pr_err("Can't resize orig: out of memory\n");
450                 return -1;
451         }
452
453         /* copy first part */
454         memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
455
456         /* copy second part */
457         memcpy(data_ptr + del_if_num * chunk_size,
458                orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
459                (max_if_num - del_if_num) * chunk_size);
460
461 free_bcast_own:
462         kfree(orig_node->bcast_own);
463         orig_node->bcast_own = data_ptr;
464
465         if (max_if_num == 0)
466                 goto free_own_sum;
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                del_if_num * sizeof(uint8_t));
476
477         memcpy(data_ptr + del_if_num * sizeof(uint8_t),
478                orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
479                (max_if_num - del_if_num) * sizeof(uint8_t));
480
481 free_own_sum:
482         kfree(orig_node->bcast_own_sum);
483         orig_node->bcast_own_sum = data_ptr;
484
485         return 0;
486 }
487
488 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
489 {
490         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
491         struct batman_if *batman_if_tmp;
492         struct orig_node *orig_node;
493         unsigned long flags;
494         HASHIT(hashit);
495         int ret;
496
497         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
498          * if_num */
499         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
500
501         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
502                 orig_node = hashit.bucket->data;
503
504                 ret = orig_node_del_if(orig_node, max_if_num,
505                                        batman_if->if_num);
506
507                 if (ret == -1)
508                         goto err;
509         }
510
511         /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
512         rcu_read_lock();
513         list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
514                 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
515                         continue;
516
517                 if (batman_if == batman_if_tmp)
518                         continue;
519
520                 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
521                         continue;
522
523                 if (batman_if_tmp->if_num > batman_if->if_num)
524                         batman_if_tmp->if_num--;
525         }
526         rcu_read_unlock();
527
528         batman_if->if_num = -1;
529         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
530         return 0;
531
532 err:
533         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
534         return -ENOMEM;
535 }