Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.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->net_dev->name);
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
319         if ((!bat_priv->primary_if) ||
320             (bat_priv->primary_if->if_status != IF_ACTIVE)) {
321                 if (!bat_priv->primary_if)
322                         return seq_printf(seq, "BATMAN mesh %s disabled - "
323                                      "please specify interfaces to enable it\n",
324                                      net_dev->name);
325
326                 return seq_printf(seq, "BATMAN mesh %s "
327                                   "disabled - primary interface not active\n",
328                                   net_dev->name);
329         }
330
331         seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
332                    SOURCE_VERSION, REVISION_VERSION_STR,
333                    bat_priv->primary_if->net_dev->name,
334                    bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
335         seq_printf(seq, "  %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
336                    "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
337                    "outgoingIF", "Potential nexthops");
338
339         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
340
341         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
342
343                 orig_node = hashit.bucket->data;
344
345                 if (!orig_node->router)
346                         continue;
347
348                 if (orig_node->router->tq_avg == 0)
349                         continue;
350
351                 last_seen_secs = jiffies_to_msecs(jiffies -
352                                                 orig_node->last_valid) / 1000;
353                 last_seen_msecs = jiffies_to_msecs(jiffies -
354                                                 orig_node->last_valid) % 1000;
355
356                 seq_printf(seq, "%pM %4i.%03is   (%3i) %pM [%10s]:",
357                            orig_node->orig, last_seen_secs, last_seen_msecs,
358                            orig_node->router->tq_avg, orig_node->router->addr,
359                            orig_node->router->if_incoming->net_dev->name);
360
361                 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
362                         seq_printf(seq, " %pM (%3i)", neigh_node->addr,
363                                            neigh_node->tq_avg);
364                 }
365
366                 seq_printf(seq, "\n");
367                 batman_count++;
368         }
369
370         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
371
372         if ((batman_count == 0))
373                 seq_printf(seq, "No batman nodes in range ...\n");
374
375         return 0;
376 }
377
378 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
379 {
380         void *data_ptr;
381
382         data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
383                            GFP_ATOMIC);
384         if (!data_ptr) {
385                 pr_err("Can't resize orig: out of memory\n");
386                 return -1;
387         }
388
389         memcpy(data_ptr, orig_node->bcast_own,
390                (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
391         kfree(orig_node->bcast_own);
392         orig_node->bcast_own = data_ptr;
393
394         data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
395         if (!data_ptr) {
396                 pr_err("Can't resize orig: out of memory\n");
397                 return -1;
398         }
399
400         memcpy(data_ptr, orig_node->bcast_own_sum,
401                (max_if_num - 1) * sizeof(uint8_t));
402         kfree(orig_node->bcast_own_sum);
403         orig_node->bcast_own_sum = data_ptr;
404
405         return 0;
406 }
407
408 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
409 {
410         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
411         struct orig_node *orig_node;
412         unsigned long flags;
413         HASHIT(hashit);
414
415         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
416          * if_num */
417         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
418
419         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
420                 orig_node = hashit.bucket->data;
421
422                 if (orig_node_add_if(orig_node, max_if_num) == -1)
423                         goto err;
424         }
425
426         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
427         return 0;
428
429 err:
430         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
431         return -ENOMEM;
432 }
433
434 static int orig_node_del_if(struct orig_node *orig_node,
435                      int max_if_num, int del_if_num)
436 {
437         void *data_ptr = NULL;
438         int chunk_size;
439
440         /* last interface was removed */
441         if (max_if_num == 0)
442                 goto free_bcast_own;
443
444         chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
445         data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
446         if (!data_ptr) {
447                 pr_err("Can't resize orig: out of memory\n");
448                 return -1;
449         }
450
451         /* copy first part */
452         memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
453
454         /* copy second part */
455         memcpy(data_ptr + del_if_num * chunk_size,
456                orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
457                (max_if_num - del_if_num) * chunk_size);
458
459 free_bcast_own:
460         kfree(orig_node->bcast_own);
461         orig_node->bcast_own = data_ptr;
462
463         if (max_if_num == 0)
464                 goto free_own_sum;
465
466         data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
467         if (!data_ptr) {
468                 pr_err("Can't resize orig: out of memory\n");
469                 return -1;
470         }
471
472         memcpy(data_ptr, orig_node->bcast_own_sum,
473                del_if_num * sizeof(uint8_t));
474
475         memcpy(data_ptr + del_if_num * sizeof(uint8_t),
476                orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
477                (max_if_num - del_if_num) * sizeof(uint8_t));
478
479 free_own_sum:
480         kfree(orig_node->bcast_own_sum);
481         orig_node->bcast_own_sum = data_ptr;
482
483         return 0;
484 }
485
486 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
487 {
488         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
489         struct batman_if *batman_if_tmp;
490         struct orig_node *orig_node;
491         unsigned long flags;
492         HASHIT(hashit);
493         int ret;
494
495         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
496          * if_num */
497         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
498
499         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
500                 orig_node = hashit.bucket->data;
501
502                 ret = orig_node_del_if(orig_node, max_if_num,
503                                        batman_if->if_num);
504
505                 if (ret == -1)
506                         goto err;
507         }
508
509         /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
510         rcu_read_lock();
511         list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
512                 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
513                         continue;
514
515                 if (batman_if == batman_if_tmp)
516                         continue;
517
518                 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
519                         continue;
520
521                 if (batman_if_tmp->if_num > batman_if->if_num)
522                         batman_if_tmp->if_num--;
523         }
524         rcu_read_unlock();
525
526         batman_if->if_num = -1;
527         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
528         return 0;
529
530 err:
531         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
532         return -ENOMEM;
533 }