Staging: batman-adv: stop persistent warnings if a device is deactivated.
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / routing.c
1 /*
2  * Copyright (C) 2007-2009 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 "routing.h"
24 #include "send.h"
25 #include "soft-interface.h"
26 #include "hard-interface.h"
27 #include "device.h"
28 #include "translation-table.h"
29 #include "types.h"
30 #include "hash.h"
31 #include "ring_buffer.h"
32 #include "vis.h"
33 #include "aggregation.h"
34 #include "compat.h"
35
36 DECLARE_WAIT_QUEUE_HEAD(thread_wait);
37 static DECLARE_DELAYED_WORK(purge_orig_wq, purge_orig);
38
39 static atomic_t data_ready_cond;
40 atomic_t exit_cond;
41
42 static void start_purge_timer(void)
43 {
44         queue_delayed_work(bat_event_workqueue, &purge_orig_wq, 1 * HZ);
45 }
46
47 int originator_init(void)
48 {
49         if (orig_hash)
50                 return 1;
51
52         spin_lock(&orig_hash_lock);
53         orig_hash = hash_new(128, compare_orig, choose_orig);
54
55         if (!orig_hash)
56                 goto err;
57
58         spin_unlock(&orig_hash_lock);
59         start_purge_timer();
60         return 1;
61
62 err:
63         spin_unlock(&orig_hash_lock);
64         return 0;
65 }
66
67 void originator_free(void)
68 {
69         if (!orig_hash)
70                 return;
71
72         cancel_delayed_work_sync(&purge_orig_wq);
73
74         spin_lock(&orig_hash_lock);
75         hash_delete(orig_hash, free_orig_node);
76         orig_hash = NULL;
77         spin_unlock(&orig_hash_lock);
78 }
79
80 static struct neigh_node *
81 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
82                 uint8_t *neigh, struct batman_if *if_incoming)
83 {
84         struct neigh_node *neigh_node;
85
86         bat_dbg(DBG_BATMAN, "Creating new last-hop neighbour of originator\n");
87
88         neigh_node = kmalloc(sizeof(struct neigh_node), GFP_ATOMIC);
89         memset(neigh_node, 0, sizeof(struct neigh_node));
90         INIT_LIST_HEAD(&neigh_node->list);
91
92         memcpy(neigh_node->addr, neigh, ETH_ALEN);
93         neigh_node->orig_node = orig_neigh_node;
94         neigh_node->if_incoming = if_incoming;
95
96         list_add_tail(&neigh_node->list, &orig_node->neigh_list);
97         return neigh_node;
98 }
99
100 void free_orig_node(void *data)
101 {
102         struct list_head *list_pos, *list_pos_tmp;
103         struct neigh_node *neigh_node;
104         struct orig_node *orig_node = (struct orig_node *)data;
105
106         /* for all neighbours towards this originator ... */
107         list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
108                 neigh_node = list_entry(list_pos, struct neigh_node, list);
109
110                 list_del(list_pos);
111                 kfree(neigh_node);
112         }
113
114         hna_global_del_orig(orig_node, "originator timed out");
115
116         kfree(orig_node->bcast_own);
117         kfree(orig_node->bcast_own_sum);
118         kfree(orig_node);
119 }
120
121 /* this function finds or creates an originator entry for the given
122  * address if it does not exits */
123 static struct orig_node *get_orig_node(uint8_t *addr)
124 {
125         struct orig_node *orig_node;
126         struct hashtable_t *swaphash;
127         char orig_str[ETH_STR_LEN];
128         int size;
129
130         orig_node = ((struct orig_node *)hash_find(orig_hash, addr));
131
132         if (orig_node != NULL)
133                 return orig_node;
134
135         addr_to_string(orig_str, addr);
136         bat_dbg(DBG_BATMAN, "Creating new originator: %s \n", orig_str);
137
138         orig_node = kmalloc(sizeof(struct orig_node), GFP_ATOMIC);
139         memset(orig_node, 0, sizeof(struct orig_node));
140         INIT_LIST_HEAD(&orig_node->neigh_list);
141
142         memcpy(orig_node->orig, addr, ETH_ALEN);
143         orig_node->router = NULL;
144         orig_node->batman_if = NULL;
145         orig_node->hna_buff = NULL;
146
147         size = num_ifs * sizeof(TYPE_OF_WORD) * NUM_WORDS;
148
149         orig_node->bcast_own = kmalloc(size, GFP_ATOMIC);
150         memset(orig_node->bcast_own, 0, size);
151
152         size = num_ifs * sizeof(uint8_t);
153         orig_node->bcast_own_sum = kmalloc(size, GFP_ATOMIC);
154         memset(orig_node->bcast_own_sum, 0, size);
155
156         hash_add(orig_hash, orig_node);
157
158         if (orig_hash->elements * 4 > orig_hash->size) {
159                 swaphash = hash_resize(orig_hash, orig_hash->size * 2);
160
161                 if (swaphash == NULL)
162                         printk(KERN_ERR
163                                "batman-adv:Couldn't resize orig hash table \n");
164                 else
165                         orig_hash = swaphash;
166         }
167
168         return orig_node;
169 }
170
171 void slide_own_bcast_window(struct batman_if *batman_if)
172 {
173         struct hash_it_t *hashit = NULL;
174         struct orig_node *orig_node;
175         TYPE_OF_WORD *word;
176
177         spin_lock(&orig_hash_lock);
178
179         while (NULL != (hashit = hash_iterate(orig_hash, hashit))) {
180                 orig_node = hashit->bucket->data;
181                 word = &(orig_node->bcast_own[batman_if->if_num * NUM_WORDS]);
182
183                 bit_get_packet(word, 1, 0);
184                 orig_node->bcast_own_sum[batman_if->if_num] =
185                         bit_packet_count(word);
186         }
187
188         spin_unlock(&orig_hash_lock);
189 }
190
191 static void update_HNA(struct orig_node *orig_node,
192                        unsigned char *hna_buff, int hna_buff_len)
193 {
194         if ((hna_buff_len != orig_node->hna_buff_len) ||
195             ((hna_buff_len > 0) &&
196              (orig_node->hna_buff_len > 0) &&
197              (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
198
199                 if (orig_node->hna_buff_len > 0)
200                         hna_global_del_orig(orig_node,
201                                             "originator changed hna");
202
203                 if ((hna_buff_len > 0) && (hna_buff != NULL))
204                         hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
205         }
206 }
207
208 static void update_route(struct orig_node *orig_node,
209                          struct neigh_node *neigh_node,
210                          unsigned char *hna_buff, int hna_buff_len)
211 {
212         char orig_str[ETH_STR_LEN], neigh_str[ETH_STR_LEN];
213         char router_str[ETH_STR_LEN];
214
215         addr_to_string(orig_str, orig_node->orig);
216
217         /* route deleted */
218         if ((orig_node->router != NULL) && (neigh_node == NULL)) {
219
220                 bat_dbg(DBG_ROUTES, "Deleting route towards: %s\n",
221                         orig_str);
222                 hna_global_del_orig(orig_node, "originator timed out");
223
224                 /* route added */
225         } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
226
227                 addr_to_string(neigh_str, neigh_node->addr);
228                 bat_dbg(DBG_ROUTES,
229                         "Adding route towards: %s (via %s)\n",
230                         orig_str, neigh_str);
231                 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
232
233                 /* route changed */
234         } else {
235                 addr_to_string(neigh_str, neigh_node->addr);
236                 addr_to_string(router_str, orig_node->router->addr);
237                 bat_dbg(DBG_ROUTES, "Changing route towards: %s (now via %s - was via %s)\n", orig_str, neigh_str, router_str);
238         }
239
240         if (neigh_node != NULL)
241                 orig_node->batman_if = neigh_node->if_incoming;
242         else
243                 orig_node->batman_if = NULL;
244
245         orig_node->router = neigh_node;
246 }
247
248
249 static void update_routes(struct orig_node *orig_node,
250                           struct neigh_node *neigh_node,
251                           unsigned char *hna_buff, int hna_buff_len)
252 {
253
254         if (orig_node == NULL)
255                 return;
256
257         if (orig_node->router != neigh_node)
258                 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
259         /* may be just HNA changed */
260         else
261                 update_HNA(orig_node, hna_buff, hna_buff_len);
262 }
263
264 static int isBidirectionalNeigh(struct orig_node *orig_node,
265                                 struct orig_node *orig_neigh_node,
266                                 struct batman_packet *batman_packet,
267                                 struct batman_if *if_incoming)
268 {
269         struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
270         char orig_str[ETH_STR_LEN], neigh_str[ETH_STR_LEN];
271         unsigned char total_count;
272
273         addr_to_string(orig_str, orig_node->orig);
274         addr_to_string(neigh_str, orig_neigh_node->orig);
275
276         if (orig_node == orig_neigh_node) {
277                 list_for_each_entry(tmp_neigh_node,
278                                     &orig_node->neigh_list,
279                                     list) {
280
281                         if (compare_orig(tmp_neigh_node->addr,
282                                          orig_neigh_node->orig) &&
283                             (tmp_neigh_node->if_incoming == if_incoming))
284                                 neigh_node = tmp_neigh_node;
285                 }
286
287                 if (neigh_node == NULL)
288                         neigh_node = create_neighbor(orig_node,
289                                                      orig_neigh_node,
290                                                      orig_neigh_node->orig,
291                                                      if_incoming);
292
293                 neigh_node->last_valid = jiffies;
294         } else {
295                 /* find packet count of corresponding one hop neighbor */
296                 list_for_each_entry(tmp_neigh_node,
297                                     &orig_neigh_node->neigh_list, list) {
298
299                         if (compare_orig(tmp_neigh_node->addr,
300                                          orig_neigh_node->orig) &&
301                             (tmp_neigh_node->if_incoming == if_incoming))
302                                 neigh_node = tmp_neigh_node;
303                 }
304
305                 if (neigh_node == NULL)
306                         neigh_node = create_neighbor(orig_neigh_node,
307                                                      orig_neigh_node,
308                                                      orig_neigh_node->orig,
309                                                      if_incoming);
310         }
311
312         orig_node->last_valid = jiffies;
313
314         /* pay attention to not get a value bigger than 100 % */
315         total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
316                        neigh_node->real_packet_count ?
317                        neigh_node->real_packet_count :
318                        orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
319
320         /* if we have too few packets (too less data) we set tq_own to zero */
321         /* if we receive too few packets it is not considered bidirectional */
322         if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
323             (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
324                 orig_neigh_node->tq_own = 0;
325         else
326                 /* neigh_node->real_packet_count is never zero as we
327                  * only purge old information when getting new
328                  * information */
329                 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
330                         neigh_node->real_packet_count;
331
332         /*
333          * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
334          * affect the nearly-symmetric links only a little, but
335          * punishes asymmetric links more.  This will give a value
336          * between 0 and TQ_MAX_VALUE
337          */
338         orig_neigh_node->tq_asym_penalty =
339                 TQ_MAX_VALUE -
340                 (TQ_MAX_VALUE *
341                  (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
342                  (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
343                  (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
344                 (TQ_LOCAL_WINDOW_SIZE *
345                  TQ_LOCAL_WINDOW_SIZE *
346                  TQ_LOCAL_WINDOW_SIZE);
347
348         batman_packet->tq = ((batman_packet->tq *
349                               orig_neigh_node->tq_own *
350                               orig_neigh_node->tq_asym_penalty) /
351                              (TQ_MAX_VALUE *     TQ_MAX_VALUE));
352
353         bat_dbg(DBG_BATMAN, "bidirectional: orig = %-15s neigh = %-15s => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i \n",
354                 orig_str, neigh_str, total_count,
355                 neigh_node->real_packet_count, orig_neigh_node->tq_own,
356                 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
357
358         /* if link has the minimum required transmission quality
359          * consider it bidirectional */
360         if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
361                 return 1;
362
363         return 0;
364 }
365
366 static void update_orig(struct orig_node *orig_node, struct ethhdr *ethhdr,
367                         struct batman_packet *batman_packet,
368                         struct batman_if *if_incoming,
369                         unsigned char *hna_buff, int hna_buff_len,
370                         char is_duplicate)
371 {
372         struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
373         int tmp_hna_buff_len;
374
375         bat_dbg(DBG_BATMAN, "update_originator(): Searching and updating originator entry of received packet \n");
376
377         list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
378                 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
379                     (tmp_neigh_node->if_incoming == if_incoming)) {
380                         neigh_node = tmp_neigh_node;
381                         continue;
382                 }
383
384                 if (is_duplicate)
385                         continue;
386
387                 ring_buffer_set(tmp_neigh_node->tq_recv,
388                                 &tmp_neigh_node->tq_index, 0);
389                 tmp_neigh_node->tq_avg =
390                         ring_buffer_avg(tmp_neigh_node->tq_recv);
391         }
392
393         if (neigh_node == NULL)
394                 neigh_node = create_neighbor(orig_node,
395                                              get_orig_node(ethhdr->h_source),
396                                              ethhdr->h_source, if_incoming);
397         else
398                 bat_dbg(DBG_BATMAN,
399                         "Updating existing last-hop neighbour of originator\n");
400
401         orig_node->flags = batman_packet->flags;
402         neigh_node->last_valid = jiffies;
403
404         ring_buffer_set(neigh_node->tq_recv,
405                         &neigh_node->tq_index,
406                         batman_packet->tq);
407         neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
408
409         if (!is_duplicate) {
410                 orig_node->last_ttl = batman_packet->ttl;
411                 neigh_node->last_ttl = batman_packet->ttl;
412         }
413
414         tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
415                             batman_packet->num_hna * ETH_ALEN : hna_buff_len);
416
417         /* if this neighbor already is our next hop there is nothing
418          * to change */
419         if (orig_node->router == neigh_node)
420                 goto update_hna;
421
422         /* if this neighbor does not offer a better TQ we won't consider it */
423         if ((orig_node->router) &&
424             (orig_node->router->tq_avg > neigh_node->tq_avg))
425                 goto update_hna;
426
427         /* if the TQ is the same and the link not more symetric we
428          * won't consider it either */
429         if ((orig_node->router) &&
430              ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
431              (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
432               >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
433                 goto update_hna;
434
435         update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
436         return;
437
438 update_hna:
439         update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
440         return;
441 }
442
443 static char count_real_packets(struct ethhdr *ethhdr,
444                                struct batman_packet *batman_packet,
445                                struct batman_if *if_incoming)
446 {
447         struct orig_node *orig_node;
448         struct neigh_node *tmp_neigh_node;
449         char is_duplicate = 0;
450         uint16_t seq_diff;
451
452         orig_node = get_orig_node(batman_packet->orig);
453         if (orig_node == NULL)
454                 return 0;
455
456         list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
457
458                 if (!is_duplicate)
459                         is_duplicate =
460                                 get_bit_status(tmp_neigh_node->real_bits,
461                                                orig_node->last_real_seqno,
462                                                batman_packet->seqno);
463                 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
464                 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
465                     (tmp_neigh_node->if_incoming == if_incoming))
466                         bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 1);
467                 else
468                         bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 0);
469
470                 tmp_neigh_node->real_packet_count =
471                         bit_packet_count(tmp_neigh_node->real_bits);
472         }
473
474         if (!is_duplicate) {
475                 bat_dbg(DBG_BATMAN, "updating last_seqno: old %d, new %d \n",
476                         orig_node->last_real_seqno, batman_packet->seqno);
477                 orig_node->last_real_seqno = batman_packet->seqno;
478         }
479
480         return is_duplicate;
481 }
482
483 void receive_bat_packet(struct ethhdr *ethhdr,
484                         struct batman_packet *batman_packet,
485                         unsigned char *hna_buff,
486                         int hna_buff_len,
487                         struct batman_if *if_incoming)
488 {
489         struct batman_if *batman_if;
490         struct orig_node *orig_neigh_node, *orig_node;
491         char orig_str[ETH_STR_LEN], prev_sender_str[ETH_STR_LEN];
492         char neigh_str[ETH_STR_LEN];
493         char has_directlink_flag;
494         char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
495         char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
496         char is_duplicate;
497         unsigned short if_incoming_seqno;
498
499         /* Silently drop when the batman packet is actually not a
500          * correct packet.
501          *
502          * This might happen if a packet is padded (e.g. Ethernet has a
503          * minimum frame length of 64 byte) and the aggregation interprets
504          * it as an additional length.
505          *
506          * TODO: A more sane solution would be to have a bit in the
507          * batman_packet to detect whether the packet is the last
508          * packet in an aggregation.  Here we expect that the padding
509          * is always zero (or not 0x01)
510          */
511         if (batman_packet->packet_type != BAT_PACKET)
512                 return;
513
514         /* could be changed by schedule_own_packet() */
515         if_incoming_seqno = atomic_read(&if_incoming->seqno);
516
517         addr_to_string(orig_str, batman_packet->orig);
518         addr_to_string(prev_sender_str, batman_packet->prev_sender);
519         addr_to_string(neigh_str, ethhdr->h_source);
520
521         has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
522
523         is_single_hop_neigh = (compare_orig(ethhdr->h_source,
524                                             batman_packet->orig) ? 1 : 0);
525
526         bat_dbg(DBG_BATMAN, "Received BATMAN packet via NB: %s, IF: %s [%s] (from OG: %s, via prev OG: %s, seqno %d, tq %d, TTL %d, V %d, IDF %d) \n",
527                 neigh_str, if_incoming->dev, if_incoming->addr_str,
528                 orig_str, prev_sender_str, batman_packet->seqno,
529                 batman_packet->tq, batman_packet->ttl, batman_packet->version,
530                 has_directlink_flag);
531
532         list_for_each_entry_rcu(batman_if, &if_list, list) {
533                 if (batman_if->if_active != IF_ACTIVE)
534                         continue;
535
536                 if (compare_orig(ethhdr->h_source,
537                                  batman_if->net_dev->dev_addr))
538                         is_my_addr = 1;
539
540                 if (compare_orig(batman_packet->orig,
541                                  batman_if->net_dev->dev_addr))
542                         is_my_orig = 1;
543
544                 if (compare_orig(batman_packet->prev_sender,
545                                  batman_if->net_dev->dev_addr))
546                         is_my_oldorig = 1;
547
548                 if (compare_orig(ethhdr->h_source, broadcastAddr))
549                         is_broadcast = 1;
550         }
551
552         if (batman_packet->version != COMPAT_VERSION) {
553                 bat_dbg(DBG_BATMAN,
554                         "Drop packet: incompatible batman version (%i)\n",
555                         batman_packet->version);
556                 return;
557         }
558
559         if (is_my_addr) {
560                 bat_dbg(DBG_BATMAN,
561                         "Drop packet: received my own broadcast (sender: %s)\n",
562                         neigh_str);
563                 return;
564         }
565
566         if (is_broadcast) {
567                 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all packets with broadcast source addr (sender: %s) \n", neigh_str);
568                 return;
569         }
570
571         if (is_my_orig) {
572                 TYPE_OF_WORD *word;
573                 int offset;
574
575                 orig_neigh_node = get_orig_node(ethhdr->h_source);
576
577                 /* neighbour has to indicate direct link and it has to
578                  * come via the corresponding interface */
579                 /* if received seqno equals last send seqno save new
580                  * seqno for bidirectional check */
581                 if (has_directlink_flag &&
582                     compare_orig(if_incoming->net_dev->dev_addr,
583                                  batman_packet->orig) &&
584                     (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
585                         offset = if_incoming->if_num * NUM_WORDS;
586                         word = &(orig_neigh_node->bcast_own[offset]);
587                         bit_mark(word, 0);
588                         orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
589                                 bit_packet_count(word);
590                 }
591
592                 bat_dbg(DBG_BATMAN, "Drop packet: originator packet from myself (via neighbour) \n");
593                 return;
594         }
595
596         if (batman_packet->tq == 0) {
597                 count_real_packets(ethhdr, batman_packet, if_incoming);
598
599                 bat_dbg(DBG_BATMAN, "Drop packet: originator packet with tq equal 0 \n");
600                 return;
601         }
602
603         if (is_my_oldorig) {
604                 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast echos (sender: %s) \n", neigh_str);
605                 return;
606         }
607
608         is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
609
610         orig_node = get_orig_node(batman_packet->orig);
611         if (orig_node == NULL)
612                 return;
613
614         /* avoid temporary routing loops */
615         if ((orig_node->router) &&
616             (orig_node->router->orig_node->router) &&
617             (compare_orig(orig_node->router->addr,
618                           batman_packet->prev_sender)) &&
619             !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
620             (compare_orig(orig_node->router->addr,
621                           orig_node->router->orig_node->router->addr))) {
622                 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %s) \n", neigh_str);
623                 return;
624         }
625
626         /* if sender is a direct neighbor the sender mac equals
627          * originator mac */
628         orig_neigh_node = (is_single_hop_neigh ?
629                            orig_node : get_orig_node(ethhdr->h_source));
630         if (orig_neigh_node == NULL)
631                 return;
632
633         /* drop packet if sender is not a direct neighbor and if we
634          * don't route towards it */
635         if (!is_single_hop_neigh &&
636             (orig_neigh_node->router == NULL)) {
637                 bat_dbg(DBG_BATMAN, "Drop packet: OGM via unknown neighbor!\n");
638                 return;
639         }
640
641         is_bidirectional = isBidirectionalNeigh(orig_node, orig_neigh_node,
642                                                 batman_packet, if_incoming);
643
644         /* update ranking if it is not a duplicate or has the same
645          * seqno and similar ttl as the non-duplicate */
646         if (is_bidirectional &&
647             (!is_duplicate ||
648              ((orig_node->last_real_seqno == batman_packet->seqno) &&
649               (orig_node->last_ttl - 3 <= batman_packet->ttl))))
650                 update_orig(orig_node, ethhdr, batman_packet,
651                             if_incoming, hna_buff, hna_buff_len, is_duplicate);
652
653         /* is single hop (direct) neighbour */
654         if (is_single_hop_neigh) {
655
656                 /* mark direct link on incoming interface */
657                 schedule_forward_packet(orig_node, ethhdr, batman_packet,
658                                         1, hna_buff_len, if_incoming);
659
660                 bat_dbg(DBG_BATMAN, "Forwarding packet: rebroadcast neighbour packet with direct link flag\n");
661                 return;
662         }
663
664         /* multihop originator */
665         if (!is_bidirectional) {
666                 bat_dbg(DBG_BATMAN,
667                         "Drop packet: not received via bidirectional link\n");
668                 return;
669         }
670
671         if (is_duplicate) {
672                 bat_dbg(DBG_BATMAN, "Drop packet: duplicate packet received\n");
673                 return;
674         }
675
676         bat_dbg(DBG_BATMAN,
677                 "Forwarding packet: rebroadcast originator packet\n");
678         schedule_forward_packet(orig_node, ethhdr, batman_packet,
679                                 0, hna_buff_len, if_incoming);
680 }
681
682 static bool purge_orig_neigbours(struct orig_node *orig_node,
683                                  struct neigh_node **best_neigh_node)
684 {
685         struct list_head *list_pos, *list_pos_tmp;
686         char neigh_str[ETH_STR_LEN], orig_str[ETH_STR_LEN];
687         struct neigh_node *neigh_node;
688         bool neigh_purged = false;
689
690         *best_neigh_node = NULL;
691
692
693         /* for all neighbours towards this originator ... */
694         list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
695                 neigh_node = list_entry(list_pos, struct neigh_node, list);
696
697                 if (time_after(jiffies,
698                                (neigh_node->last_valid +
699                                 ((PURGE_TIMEOUT * HZ) / 1000)))) {
700
701                         addr_to_string(neigh_str, neigh_node->addr);
702                         bat_dbg(DBG_BATMAN, "Neighbour timeout: originator %s, neighbour: %s, last_valid %lu\n", orig_str, neigh_str, (neigh_node->last_valid / HZ));
703
704                         neigh_purged = true;
705                         list_del(list_pos);
706                         kfree(neigh_node);
707                 } else {
708                         if ((*best_neigh_node == NULL) ||
709                             (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
710                                 *best_neigh_node = neigh_node;
711                 }
712         }
713         return neigh_purged;
714 }
715
716 static bool purge_orig_node(struct orig_node *orig_node)
717 {
718         struct neigh_node *best_neigh_node;
719         char orig_str[ETH_STR_LEN];
720
721         addr_to_string(orig_str, orig_node->orig);
722
723         if (time_after(jiffies,
724                        (orig_node->last_valid +
725                         ((2 * PURGE_TIMEOUT * HZ) / 1000)))) {
726
727                 bat_dbg(DBG_BATMAN,
728                         "Originator timeout: originator %s, last_valid %lu\n",
729                         orig_str, (orig_node->last_valid / HZ));
730                 return true;
731         } else {
732                 if (purge_orig_neigbours(orig_node, &best_neigh_node))
733                         update_routes(orig_node, best_neigh_node,
734                                       orig_node->hna_buff,
735                                       orig_node->hna_buff_len);
736         }
737         return false;
738 }
739
740
741 void purge_orig(struct work_struct *work)
742 {
743         struct hash_it_t *hashit = NULL;
744         struct orig_node *orig_node;
745
746         spin_lock(&orig_hash_lock);
747
748         /* for all origins... */
749         while (NULL != (hashit = hash_iterate(orig_hash, hashit))) {
750                 orig_node = hashit->bucket->data;
751                 if (purge_orig_node(orig_node)) {
752                         hash_remove_bucket(orig_hash, hashit);
753                         free_orig_node(orig_node);
754                 }
755         }
756
757         spin_unlock(&orig_hash_lock);
758
759         start_purge_timer();
760 }
761
762 static int receive_raw_packet(struct socket *raw_sock,
763                               unsigned char *packet_buff, int packet_buff_len)
764 {
765         struct kvec iov;
766         struct msghdr msg;
767
768         iov.iov_base = packet_buff;
769         iov.iov_len = packet_buff_len;
770
771         msg.msg_flags = MSG_DONTWAIT;   /* non-blocking */
772         msg.msg_name = NULL;
773         msg.msg_namelen = 0;
774         msg.msg_control = NULL;
775
776         return kernel_recvmsg(raw_sock, &msg, &iov, 1, packet_buff_len,
777                               MSG_DONTWAIT);
778 }
779
780 static void recv_bat_packet(struct ethhdr *ethhdr,
781                             unsigned char *packet_buff,
782                             int result,
783                             struct batman_if *batman_if)
784 {
785         /* packet with broadcast indication but unicast recipient */
786         if (!is_bcast(ethhdr->h_dest))
787                 return;
788
789         /* packet with broadcast sender address */
790         if (is_bcast(ethhdr->h_source))
791                 return;
792
793         /* drop packet if it has not at least one batman packet as payload */
794         if (result < sizeof(struct ethhdr) + sizeof(struct batman_packet))
795                 return;
796
797         spin_lock(&orig_hash_lock);
798         receive_aggr_bat_packet(ethhdr,
799                                 packet_buff + sizeof(struct ethhdr),
800                                 result - sizeof(struct ethhdr),
801                                 batman_if);
802         spin_unlock(&orig_hash_lock);
803 }
804
805 static void recv_my_icmp_packet(struct ethhdr *ethhdr,
806                                 struct icmp_packet *icmp_packet,
807                                 unsigned char *packet_buff,
808                                 int result)
809 {
810         struct orig_node *orig_node;
811
812         /* add data to device queue */
813         if (icmp_packet->msg_type != ECHO_REQUEST) {
814                 bat_device_receive_packet(icmp_packet);
815                 return;
816         }
817
818         /* answer echo request (ping) */
819         /* get routing information */
820         spin_lock(&orig_hash_lock);
821         orig_node = ((struct orig_node *)hash_find(orig_hash,
822                                                    icmp_packet->orig));
823
824         if ((orig_node != NULL) &&
825             (orig_node->batman_if != NULL) &&
826             (orig_node->router != NULL)) {
827                 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
828                 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
829                 icmp_packet->msg_type = ECHO_REPLY;
830                 icmp_packet->ttl = TTL;
831
832                 send_raw_packet(packet_buff + sizeof(struct ethhdr),
833                                 result - sizeof(struct ethhdr),
834                                 orig_node->batman_if,
835                                 orig_node->router->addr);
836         }
837
838         spin_unlock(&orig_hash_lock);
839         return;
840 }
841
842 static void recv_icmp_ttl_exceeded(struct icmp_packet *icmp_packet,
843                                    struct ethhdr *ethhdr,
844                                    unsigned char *packet_buff,
845                                    int result,
846                                    struct batman_if *batman_if)
847 {
848         unsigned char src_str[ETH_STR_LEN], dst_str[ETH_STR_LEN];
849         struct orig_node *orig_node;
850
851         addr_to_string(src_str, icmp_packet->orig);
852         addr_to_string(dst_str, icmp_packet->dst);
853
854         printk(KERN_WARNING "batman-adv:Warning - can't send packet from %s to %s: ttl exceeded\n", src_str, dst_str);
855
856         /* send TTL exceeded if packet is an echo request (traceroute) */
857         if (icmp_packet->msg_type != ECHO_REQUEST)
858                 return;
859
860         /* get routing information */
861         spin_lock(&orig_hash_lock);
862         orig_node = ((struct orig_node *)
863                      hash_find(orig_hash, icmp_packet->orig));
864
865         if ((orig_node != NULL) &&
866             (orig_node->batman_if != NULL) &&
867             (orig_node->router != NULL)) {
868                 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
869                 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
870                 icmp_packet->msg_type = TTL_EXCEEDED;
871                 icmp_packet->ttl = TTL;
872
873                 send_raw_packet(packet_buff + sizeof(struct ethhdr),
874                                 result - sizeof(struct ethhdr),
875                                 orig_node->batman_if,
876                                 orig_node->router->addr);
877
878         }
879
880         spin_unlock(&orig_hash_lock);
881 }
882
883
884
885 static void recv_icmp_packet(struct ethhdr *ethhdr,
886                              unsigned char *packet_buff,
887                              int result,
888                              struct batman_if *batman_if)
889 {
890         struct icmp_packet *icmp_packet;
891         struct orig_node *orig_node;
892
893         /* packet with unicast indication but broadcast recipient */
894         if (is_bcast(ethhdr->h_dest))
895                 return;
896
897         /* packet with broadcast sender address */
898         if (is_bcast(ethhdr->h_source))
899                 return;
900
901         /* not for me */
902         if (!is_my_mac(ethhdr->h_dest))
903                 return;
904
905         /* drop packet if it has not necessary minimum size */
906         if (result < sizeof(struct ethhdr) + sizeof(struct icmp_packet))
907                 return;
908
909         icmp_packet = (struct icmp_packet *)
910                 (packet_buff + sizeof(struct ethhdr));
911
912         /* packet for me */
913         if (is_my_mac(icmp_packet->dst))
914                 recv_my_icmp_packet(ethhdr, icmp_packet, packet_buff, result);
915
916         /* TTL exceeded */
917         if (icmp_packet->ttl < 2) {
918                 recv_icmp_ttl_exceeded(icmp_packet, ethhdr, packet_buff, result,
919                                        batman_if);
920                 return;
921
922         }
923
924         /* get routing information */
925         spin_lock(&orig_hash_lock);
926         orig_node = ((struct orig_node *)
927                      hash_find(orig_hash, icmp_packet->dst));
928
929         if ((orig_node != NULL) &&
930             (orig_node->batman_if != NULL) &&
931             (orig_node->router != NULL)) {
932
933                 /* decrement ttl */
934                 icmp_packet->ttl--;
935
936                 /* route it */
937                 send_raw_packet(packet_buff + sizeof(struct ethhdr),
938                                 result - sizeof(struct ethhdr),
939                                 orig_node->batman_if,
940                                 orig_node->router->addr);
941         }
942         spin_unlock(&orig_hash_lock);
943 }
944
945 static void recv_unicast_packet(struct ethhdr *ethhdr,
946                                 unsigned char *packet_buff,
947                                 int result,
948                                 struct batman_if *batman_if)
949 {
950         struct unicast_packet *unicast_packet;
951         unsigned char src_str[ETH_STR_LEN], dst_str[ETH_STR_LEN];
952         struct orig_node *orig_node;
953         int hdr_size = sizeof(struct ethhdr) + sizeof(struct unicast_packet);
954
955         /* packet with unicast indication but broadcast recipient */
956         if (is_bcast(ethhdr->h_dest))
957                 return;
958
959         /* packet with broadcast sender address */
960         if (is_bcast(ethhdr->h_source))
961                 return;
962
963         /* not for me */
964         if (!is_my_mac(ethhdr->h_dest))
965                 return;
966
967         /* drop packet if it has not necessary minimum size */
968         if (result < hdr_size)
969                 return;
970
971         unicast_packet = (struct unicast_packet *)
972                 (packet_buff + sizeof(struct ethhdr));
973
974         /* packet for me */
975         if (is_my_mac(unicast_packet->dest)) {
976                 interface_rx(soft_device, packet_buff + hdr_size,
977                              result - hdr_size);
978                 return;
979
980         }
981
982         /* TTL exceeded */
983         if (unicast_packet->ttl < 2) {
984                 addr_to_string(src_str, ((struct ethhdr *)
985                                          (unicast_packet + 1))->h_source);
986                 addr_to_string(dst_str, unicast_packet->dest);
987
988                 printk(KERN_WARNING "batman-adv:Warning - can't send packet from %s to %s: ttl exceeded\n", src_str, dst_str);
989                 return;
990         }
991
992         /* get routing information */
993         spin_lock(&orig_hash_lock);
994         orig_node = ((struct orig_node *)
995                      hash_find(orig_hash, unicast_packet->dest));
996
997         if ((orig_node != NULL) &&
998             (orig_node->batman_if != NULL) &&
999             (orig_node->router != NULL)) {
1000                 /* decrement ttl */
1001                 unicast_packet->ttl--;
1002
1003                 /* route it */
1004                 send_raw_packet(packet_buff + sizeof(struct ethhdr),
1005                                 result - sizeof(struct ethhdr),
1006                                 orig_node->batman_if,
1007                                 orig_node->router->addr);
1008         }
1009         spin_unlock(&orig_hash_lock);
1010 }
1011
1012
1013 static void recv_bcast_packet(struct ethhdr *ethhdr,
1014                               unsigned char *packet_buff,
1015                               int result,
1016                               struct batman_if *batman_if)
1017 {
1018         struct orig_node *orig_node;
1019         struct bcast_packet *bcast_packet;
1020         int hdr_size = sizeof(struct ethhdr) + sizeof(struct bcast_packet);
1021
1022         /* packet with broadcast indication but unicast recipient */
1023         if (!is_bcast(ethhdr->h_dest))
1024                 return;
1025
1026         /* packet with broadcast sender address */
1027         if (is_bcast(ethhdr->h_source))
1028                 return;
1029
1030         /* drop packet if it has not necessary minimum size */
1031         if (result < hdr_size)
1032                 return;
1033
1034         /* ignore broadcasts sent by myself */
1035         if (is_my_mac(ethhdr->h_source))
1036                 return;
1037
1038         bcast_packet = (struct bcast_packet *)
1039                 (packet_buff + sizeof(struct ethhdr));
1040
1041         /* ignore broadcasts originated by myself */
1042         if (is_my_mac(bcast_packet->orig))
1043                 return;
1044
1045         spin_lock(&orig_hash_lock);
1046         orig_node = ((struct orig_node *)
1047                      hash_find(orig_hash, bcast_packet->orig));
1048
1049         if (orig_node == NULL) {
1050                 spin_unlock(&orig_hash_lock);
1051                 return;
1052         }
1053
1054         /* check flood history */
1055         if (get_bit_status(orig_node->bcast_bits,
1056                            orig_node->last_bcast_seqno,
1057                            ntohs(bcast_packet->seqno))) {
1058                 spin_unlock(&orig_hash_lock);
1059                 return;
1060         }
1061
1062         /* mark broadcast in flood history */
1063         if (bit_get_packet(orig_node->bcast_bits,
1064                            ntohs(bcast_packet->seqno) -
1065                            orig_node->last_bcast_seqno, 1))
1066                 orig_node->last_bcast_seqno = ntohs(bcast_packet->seqno);
1067
1068         spin_unlock(&orig_hash_lock);
1069
1070         /* broadcast for me */
1071         interface_rx(soft_device, packet_buff + hdr_size, result - hdr_size);
1072
1073         /* rebroadcast packet */
1074         add_bcast_packet_to_list(packet_buff + sizeof(struct ethhdr),
1075                                  result - sizeof(struct ethhdr));
1076 }
1077
1078 static void recv_vis_packet(struct ethhdr *ethhdr,
1079                             unsigned char *packet_buff,
1080                             int result)
1081 {
1082         struct vis_packet *vis_packet;
1083         int hdr_size = sizeof(struct ethhdr) + sizeof(struct vis_packet);
1084         int vis_info_len;
1085
1086         /* drop if too short. */
1087         if (result < hdr_size)
1088                 return;
1089
1090         /* not for me */
1091         if (!is_my_mac(ethhdr->h_dest))
1092                 return;
1093
1094         vis_packet = (struct vis_packet *)(packet_buff + sizeof(struct ethhdr));
1095         vis_info_len = result  - hdr_size;
1096
1097         /* ignore own packets */
1098         if (is_my_mac(vis_packet->vis_orig))
1099                 return;
1100
1101         if (is_my_mac(vis_packet->sender_orig))
1102                 return;
1103
1104         switch (vis_packet->vis_type) {
1105         case VIS_TYPE_SERVER_SYNC:
1106                 receive_server_sync_packet(vis_packet, vis_info_len);
1107                 break;
1108
1109         case VIS_TYPE_CLIENT_UPDATE:
1110                 receive_client_update_packet(vis_packet, vis_info_len);
1111                 break;
1112
1113         default:        /* ignore unknown packet */
1114                 break;
1115         }
1116 }
1117
1118 static int recv_one_packet(struct batman_if *batman_if,
1119                            unsigned char *packet_buff)
1120 {
1121         int result;
1122         struct ethhdr *ethhdr;
1123         struct batman_packet *batman_packet;
1124
1125         result = receive_raw_packet(batman_if->raw_sock, packet_buff,
1126                                     PACKBUFF_SIZE);
1127         if (result <= 0)
1128                 return result;
1129
1130         if (result < sizeof(struct ethhdr) + 2)
1131                 return 0;
1132
1133         ethhdr = (struct ethhdr *)packet_buff;
1134         batman_packet = (struct batman_packet *)
1135                 (packet_buff + sizeof(struct ethhdr));
1136
1137         if (batman_packet->version != COMPAT_VERSION) {
1138                 bat_dbg(DBG_BATMAN,
1139                         "Drop packet: incompatible batman version (%i)\n",
1140                         batman_packet->version);
1141                 return 0;
1142         }
1143
1144         switch (batman_packet->packet_type) {
1145                 /* batman originator packet */
1146         case BAT_PACKET:
1147                 recv_bat_packet(ethhdr, packet_buff, result, batman_if);
1148                 break;
1149
1150                 /* batman icmp packet */
1151         case BAT_ICMP:
1152                 recv_icmp_packet(ethhdr, packet_buff, result, batman_if);
1153                 break;
1154
1155                 /* unicast packet */
1156         case BAT_UNICAST:
1157                 recv_unicast_packet(ethhdr, packet_buff, result, batman_if);
1158                 break;
1159
1160                 /* broadcast packet */
1161         case BAT_BCAST:
1162                 recv_bcast_packet(ethhdr,
1163                                   packet_buff, result, batman_if);
1164                 break;
1165
1166                 /* vis packet */
1167         case BAT_VIS:
1168                 recv_vis_packet(ethhdr, packet_buff, result);
1169                 break;
1170         }
1171         return 0;
1172 }
1173
1174
1175 static int discard_one_packet(struct batman_if *batman_if,
1176                               unsigned char *packet_buff)
1177 {
1178         int result = -EAGAIN;
1179
1180         if ((batman_if->if_active == IF_TO_BE_ACTIVATED)
1181                 && (batman_if->raw_sock)) {
1182                         result = receive_raw_packet(batman_if->raw_sock,
1183                                                     packet_buff,
1184                                                     PACKBUFF_SIZE);
1185         }
1186         return result;
1187 }
1188
1189
1190 static bool is_interface_active(struct batman_if *batman_if)
1191 {
1192         if (batman_if->if_active != IF_ACTIVE)
1193                 return false;
1194
1195         return true;
1196 }
1197
1198 static void service_interface(struct batman_if *batman_if,
1199                               unsigned char *packet_buff)
1200
1201 {
1202         int result;
1203
1204         do {
1205                 if (is_interface_active(batman_if))
1206                         result = recv_one_packet(batman_if, packet_buff);
1207                 else
1208                         result = discard_one_packet(batman_if, packet_buff);
1209         } while (result >= 0);
1210
1211         /* we perform none blocking reads, so EAGAIN indicates there
1212            are no more packets to read. Anything else is a real
1213            error.*/
1214
1215         if ((result < 0) && (result != -EAGAIN))
1216                 printk(KERN_ERR "batman-adv:Could not receive packet from interface %s: %i\n", batman_if->dev, result);
1217 }
1218
1219 static void service_interfaces(unsigned char *packet_buffer)
1220 {
1221         struct batman_if *batman_if;
1222         rcu_read_lock();
1223         list_for_each_entry_rcu(batman_if, &if_list, list) {
1224                 rcu_read_unlock();
1225                 service_interface(batman_if, packet_buffer);
1226                 rcu_read_lock();
1227         }
1228         rcu_read_unlock();
1229 }
1230
1231
1232 int packet_recv_thread(void *data)
1233 {
1234         unsigned char *packet_buff;
1235
1236         atomic_set(&data_ready_cond, 0);
1237         atomic_set(&exit_cond, 0);
1238         packet_buff = kmalloc(PACKBUFF_SIZE, GFP_KERNEL);
1239         if (!packet_buff) {
1240                 printk(KERN_ERR"batman-adv:Could allocate memory for the packet buffer. :(\n");
1241                 return -1;
1242         }
1243
1244         while ((!kthread_should_stop()) && (!atomic_read(&exit_cond))) {
1245
1246                 wait_event_interruptible(thread_wait,
1247                                          (atomic_read(&data_ready_cond) ||
1248                                           atomic_read(&exit_cond)));
1249
1250                 atomic_set(&data_ready_cond, 0);
1251
1252                 if (kthread_should_stop() || atomic_read(&exit_cond))
1253                         break;
1254
1255                 service_interfaces(packet_buff);
1256         }
1257         kfree(packet_buff);
1258
1259         /* do not exit until kthread_stop() is actually called,
1260          * otherwise it will wait for us forever. */
1261         while (!kthread_should_stop())
1262                 schedule();
1263
1264         return 0;
1265 }
1266
1267 void batman_data_ready(struct sock *sk, int len)
1268 {
1269         void (*data_ready)(struct sock *, int) = sk->sk_user_data;
1270
1271         data_ready(sk, len);
1272
1273         atomic_set(&data_ready_cond, 1);
1274         wake_up_interruptible(&thread_wait);
1275 }
1276