Staging: batman-adv: multiple mesh clouds
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / translation-table.c
1 /*
2  * Copyright (C) 2007-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 #include "main.h"
23 #include "translation-table.h"
24 #include "soft-interface.h"
25 #include "types.h"
26 #include "hash.h"
27
28 static void hna_local_purge(struct work_struct *work);
29 static void _hna_global_del_orig(struct bat_priv *bat_priv,
30                                  struct hna_global_entry *hna_global_entry,
31                                  char *message);
32
33 static void hna_local_start_timer(struct bat_priv *bat_priv)
34 {
35         INIT_DELAYED_WORK(&bat_priv->hna_work, hna_local_purge);
36         queue_delayed_work(bat_event_workqueue, &bat_priv->hna_work, 10 * HZ);
37 }
38
39 int hna_local_init(struct bat_priv *bat_priv)
40 {
41         if (bat_priv->hna_local_hash)
42                 return 1;
43
44         bat_priv->hna_local_hash = hash_new(128, compare_orig, choose_orig);
45
46         if (!bat_priv->hna_local_hash)
47                 return 0;
48
49         atomic_set(&bat_priv->hna_local_changed, 0);
50         hna_local_start_timer(bat_priv);
51
52         return 1;
53 }
54
55 void hna_local_add(struct net_device *soft_iface, uint8_t *addr)
56 {
57         struct bat_priv *bat_priv = netdev_priv(soft_iface);
58         struct hna_local_entry *hna_local_entry;
59         struct hna_global_entry *hna_global_entry;
60         struct hashtable_t *swaphash;
61         unsigned long flags;
62
63         spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
64         hna_local_entry =
65                 ((struct hna_local_entry *)hash_find(bat_priv->hna_local_hash,
66                                                      addr));
67         spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
68
69         if (hna_local_entry) {
70                 hna_local_entry->last_seen = jiffies;
71                 return;
72         }
73
74         /* only announce as many hosts as possible in the batman-packet and
75            space in batman_packet->num_hna That also should give a limit to
76            MAC-flooding. */
77         if ((bat_priv->num_local_hna + 1 > (ETH_DATA_LEN - BAT_PACKET_LEN)
78                                                                 / ETH_ALEN) ||
79             (bat_priv->num_local_hna + 1 > 255)) {
80                 bat_dbg(DBG_ROUTES, bat_priv,
81                         "Can't add new local hna entry (%pM): "
82                         "number of local hna entries exceeds packet size\n",
83                         addr);
84                 return;
85         }
86
87         bat_dbg(DBG_ROUTES, bat_priv,
88                 "Creating new local hna entry: %pM\n", addr);
89
90         hna_local_entry = kmalloc(sizeof(struct hna_local_entry), GFP_ATOMIC);
91         if (!hna_local_entry)
92                 return;
93
94         memcpy(hna_local_entry->addr, addr, ETH_ALEN);
95         hna_local_entry->last_seen = jiffies;
96
97         /* the batman interface mac address should never be purged */
98         if (compare_orig(addr, soft_iface->dev_addr))
99                 hna_local_entry->never_purge = 1;
100         else
101                 hna_local_entry->never_purge = 0;
102
103         spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
104
105         hash_add(bat_priv->hna_local_hash, hna_local_entry);
106         bat_priv->num_local_hna++;
107         atomic_set(&bat_priv->hna_local_changed, 1);
108
109         if (bat_priv->hna_local_hash->elements * 4 >
110                                         bat_priv->hna_local_hash->size) {
111                 swaphash = hash_resize(bat_priv->hna_local_hash,
112                                        bat_priv->hna_local_hash->size * 2);
113
114                 if (!swaphash)
115                         pr_err("Couldn't resize local hna hash table\n");
116                 else
117                         bat_priv->hna_local_hash = swaphash;
118         }
119
120         spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
121
122         /* remove address from global hash if present */
123         spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
124
125         hna_global_entry = ((struct hna_global_entry *)
126                                 hash_find(bat_priv->hna_global_hash, addr));
127
128         if (hna_global_entry)
129                 _hna_global_del_orig(bat_priv, hna_global_entry,
130                                      "local hna received");
131
132         spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
133 }
134
135 int hna_local_fill_buffer(struct bat_priv *bat_priv,
136                           unsigned char *buff, int buff_len)
137 {
138         struct hna_local_entry *hna_local_entry;
139         HASHIT(hashit);
140         int i = 0;
141         unsigned long flags;
142
143         spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
144
145         while (hash_iterate(bat_priv->hna_local_hash, &hashit)) {
146
147                 if (buff_len < (i + 1) * ETH_ALEN)
148                         break;
149
150                 hna_local_entry = hashit.bucket->data;
151                 memcpy(buff + (i * ETH_ALEN), hna_local_entry->addr, ETH_ALEN);
152
153                 i++;
154         }
155
156         /* if we did not get all new local hnas see you next time  ;-) */
157         if (i == bat_priv->num_local_hna)
158                 atomic_set(&bat_priv->hna_local_changed, 0);
159
160         spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
161         return i;
162 }
163
164 int hna_local_seq_print_text(struct seq_file *seq, void *offset)
165 {
166         struct net_device *net_dev = (struct net_device *)seq->private;
167         struct bat_priv *bat_priv = netdev_priv(net_dev);
168         struct hna_local_entry *hna_local_entry;
169         HASHIT(hashit);
170         HASHIT(hashit_count);
171         unsigned long flags;
172         size_t buf_size, pos;
173         char *buff;
174
175         if (!bat_priv->primary_if) {
176                 return seq_printf(seq, "BATMAN mesh %s disabled - "
177                                "please specify interfaces to enable it\n",
178                                net_dev->name);
179         }
180
181         seq_printf(seq, "Locally retrieved addresses (from %s) "
182                    "announced via HNA:\n",
183                    net_dev->name);
184
185         spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
186
187         buf_size = 1;
188         /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
189         while (hash_iterate(bat_priv->hna_local_hash, &hashit_count))
190                 buf_size += 21;
191
192         buff = kmalloc(buf_size, GFP_ATOMIC);
193         if (!buff) {
194                 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
195                 return -ENOMEM;
196         }
197         buff[0] = '\0';
198         pos = 0;
199
200         while (hash_iterate(bat_priv->hna_local_hash, &hashit)) {
201                 hna_local_entry = hashit.bucket->data;
202
203                 pos += snprintf(buff + pos, 22, " * %pM\n",
204                                 hna_local_entry->addr);
205         }
206
207         spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
208
209         seq_printf(seq, "%s", buff);
210         kfree(buff);
211         return 0;
212 }
213
214 static void _hna_local_del(void *data, void *arg)
215 {
216         struct bat_priv *bat_priv = (struct bat_priv *)arg;
217
218         kfree(data);
219         bat_priv->num_local_hna--;
220         atomic_set(&bat_priv->hna_local_changed, 1);
221 }
222
223 static void hna_local_del(struct bat_priv *bat_priv,
224                           struct hna_local_entry *hna_local_entry,
225                           char *message)
226 {
227         bat_dbg(DBG_ROUTES, bat_priv, "Deleting local hna entry (%pM): %s\n",
228                 hna_local_entry->addr, message);
229
230         hash_remove(bat_priv->hna_local_hash, hna_local_entry->addr);
231         _hna_local_del(hna_local_entry, bat_priv);
232 }
233
234 void hna_local_remove(struct bat_priv *bat_priv,
235                       uint8_t *addr, char *message)
236 {
237         struct hna_local_entry *hna_local_entry;
238         unsigned long flags;
239
240         spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
241
242         hna_local_entry = (struct hna_local_entry *)
243                 hash_find(bat_priv->hna_local_hash, addr);
244         if (hna_local_entry)
245                 hna_local_del(bat_priv, hna_local_entry, message);
246
247         spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
248 }
249
250 static void hna_local_purge(struct work_struct *work)
251 {
252         struct delayed_work *delayed_work =
253                 container_of(work, struct delayed_work, work);
254         struct bat_priv *bat_priv =
255                 container_of(delayed_work, struct bat_priv, hna_work);
256         struct hna_local_entry *hna_local_entry;
257         HASHIT(hashit);
258         unsigned long flags;
259         unsigned long timeout;
260
261         spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
262
263         while (hash_iterate(bat_priv->hna_local_hash, &hashit)) {
264                 hna_local_entry = hashit.bucket->data;
265
266                 timeout = hna_local_entry->last_seen + LOCAL_HNA_TIMEOUT * HZ;
267
268                 if ((!hna_local_entry->never_purge) &&
269                     time_after(jiffies, timeout))
270                         hna_local_del(bat_priv, hna_local_entry,
271                                       "address timed out");
272         }
273
274         spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
275         hna_local_start_timer(bat_priv);
276 }
277
278 void hna_local_free(struct bat_priv *bat_priv)
279 {
280         if (!bat_priv->hna_local_hash)
281                 return;
282
283         cancel_delayed_work_sync(&bat_priv->hna_work);
284         hash_delete(bat_priv->hna_local_hash, _hna_local_del, bat_priv);
285         bat_priv->hna_local_hash = NULL;
286 }
287
288 int hna_global_init(struct bat_priv *bat_priv)
289 {
290         if (bat_priv->hna_global_hash)
291                 return 1;
292
293         bat_priv->hna_global_hash = hash_new(128, compare_orig, choose_orig);
294
295         if (!bat_priv->hna_global_hash)
296                 return 0;
297
298         return 1;
299 }
300
301 void hna_global_add_orig(struct bat_priv *bat_priv,
302                          struct orig_node *orig_node,
303                          unsigned char *hna_buff, int hna_buff_len)
304 {
305         struct hna_global_entry *hna_global_entry;
306         struct hna_local_entry *hna_local_entry;
307         struct hashtable_t *swaphash;
308         int hna_buff_count = 0;
309         unsigned long flags;
310         unsigned char *hna_ptr;
311
312         while ((hna_buff_count + 1) * ETH_ALEN <= hna_buff_len) {
313                 spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
314
315                 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
316                 hna_global_entry = (struct hna_global_entry *)
317                         hash_find(bat_priv->hna_global_hash, hna_ptr);
318
319                 if (!hna_global_entry) {
320                         spin_unlock_irqrestore(&bat_priv->hna_ghash_lock,
321                                                flags);
322
323                         hna_global_entry =
324                                 kmalloc(sizeof(struct hna_global_entry),
325                                         GFP_ATOMIC);
326
327                         if (!hna_global_entry)
328                                 break;
329
330                         memcpy(hna_global_entry->addr, hna_ptr, ETH_ALEN);
331
332                         bat_dbg(DBG_ROUTES, bat_priv,
333                                 "Creating new global hna entry: "
334                                 "%pM (via %pM)\n",
335                                 hna_global_entry->addr, orig_node->orig);
336
337                         spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
338                         hash_add(bat_priv->hna_global_hash, hna_global_entry);
339
340                 }
341
342                 hna_global_entry->orig_node = orig_node;
343                 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
344
345                 /* remove address from local hash if present */
346                 spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
347
348                 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
349                 hna_local_entry = (struct hna_local_entry *)
350                         hash_find(bat_priv->hna_local_hash, hna_ptr);
351
352                 if (hna_local_entry)
353                         hna_local_del(bat_priv, hna_local_entry,
354                                       "global hna received");
355
356                 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
357
358                 hna_buff_count++;
359         }
360
361         /* initialize, and overwrite if malloc succeeds */
362         orig_node->hna_buff = NULL;
363         orig_node->hna_buff_len = 0;
364
365         if (hna_buff_len > 0) {
366                 orig_node->hna_buff = kmalloc(hna_buff_len, GFP_ATOMIC);
367                 if (orig_node->hna_buff) {
368                         memcpy(orig_node->hna_buff, hna_buff, hna_buff_len);
369                         orig_node->hna_buff_len = hna_buff_len;
370                 }
371         }
372
373         spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
374
375         if (bat_priv->hna_global_hash->elements * 4 >
376                                         bat_priv->hna_global_hash->size) {
377                 swaphash = hash_resize(bat_priv->hna_global_hash,
378                                        bat_priv->hna_global_hash->size * 2);
379
380                 if (!swaphash)
381                         pr_err("Couldn't resize global hna hash table\n");
382                 else
383                         bat_priv->hna_global_hash = swaphash;
384         }
385
386         spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
387 }
388
389 int hna_global_seq_print_text(struct seq_file *seq, void *offset)
390 {
391         struct net_device *net_dev = (struct net_device *)seq->private;
392         struct bat_priv *bat_priv = netdev_priv(net_dev);
393         struct hna_global_entry *hna_global_entry;
394         HASHIT(hashit);
395         HASHIT(hashit_count);
396         unsigned long flags;
397         size_t buf_size, pos;
398         char *buff;
399
400         if (!bat_priv->primary_if) {
401                 return seq_printf(seq, "BATMAN mesh %s disabled - "
402                                   "please specify interfaces to enable it\n",
403                                   net_dev->name);
404         }
405
406         seq_printf(seq, "Globally announced HNAs received via the mesh %s\n",
407                    net_dev->name);
408
409         spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
410
411         buf_size = 1;
412         /* Estimate length for: " * xx:xx:xx:xx:xx:xx via xx:xx:xx:xx:xx:xx\n"*/
413         while (hash_iterate(bat_priv->hna_global_hash, &hashit_count))
414                 buf_size += 43;
415
416         buff = kmalloc(buf_size, GFP_ATOMIC);
417         if (!buff) {
418                 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
419                 return -ENOMEM;
420         }
421         buff[0] = '\0';
422         pos = 0;
423
424         while (hash_iterate(bat_priv->hna_global_hash, &hashit)) {
425                 hna_global_entry = hashit.bucket->data;
426
427                 pos += snprintf(buff + pos, 44,
428                                 " * %pM via %pM\n", hna_global_entry->addr,
429                                 hna_global_entry->orig_node->orig);
430         }
431
432         spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
433
434         seq_printf(seq, "%s", buff);
435         kfree(buff);
436         return 0;
437 }
438
439 static void _hna_global_del_orig(struct bat_priv *bat_priv,
440                                  struct hna_global_entry *hna_global_entry,
441                                  char *message)
442 {
443         bat_dbg(DBG_ROUTES, bat_priv,
444                 "Deleting global hna entry %pM (via %pM): %s\n",
445                 hna_global_entry->addr, hna_global_entry->orig_node->orig,
446                 message);
447
448         hash_remove(bat_priv->hna_global_hash, hna_global_entry->addr);
449         kfree(hna_global_entry);
450 }
451
452 void hna_global_del_orig(struct bat_priv *bat_priv,
453                          struct orig_node *orig_node, char *message)
454 {
455         struct hna_global_entry *hna_global_entry;
456         int hna_buff_count = 0;
457         unsigned long flags;
458         unsigned char *hna_ptr;
459
460         if (orig_node->hna_buff_len == 0)
461                 return;
462
463         spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
464
465         while ((hna_buff_count + 1) * ETH_ALEN <= orig_node->hna_buff_len) {
466                 hna_ptr = orig_node->hna_buff + (hna_buff_count * ETH_ALEN);
467                 hna_global_entry = (struct hna_global_entry *)
468                         hash_find(bat_priv->hna_global_hash, hna_ptr);
469
470                 if ((hna_global_entry) &&
471                     (hna_global_entry->orig_node == orig_node))
472                         _hna_global_del_orig(bat_priv, hna_global_entry,
473                                              message);
474
475                 hna_buff_count++;
476         }
477
478         spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
479
480         orig_node->hna_buff_len = 0;
481         kfree(orig_node->hna_buff);
482         orig_node->hna_buff = NULL;
483 }
484
485 static void hna_global_del(void *data, void *arg)
486 {
487         kfree(data);
488 }
489
490 void hna_global_free(struct bat_priv *bat_priv)
491 {
492         if (!bat_priv->hna_global_hash)
493                 return;
494
495         hash_delete(bat_priv->hna_global_hash, hna_global_del, NULL);
496         bat_priv->hna_global_hash = NULL;
497 }
498
499 struct orig_node *transtable_search(struct bat_priv *bat_priv, uint8_t *addr)
500 {
501         struct hna_global_entry *hna_global_entry;
502         unsigned long flags;
503
504         spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
505         hna_global_entry = (struct hna_global_entry *)
506                                 hash_find(bat_priv->hna_global_hash, addr);
507         spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
508
509         if (!hna_global_entry)
510                 return NULL;
511
512         return hna_global_entry->orig_node;
513 }