Staging: batman-adv: multiple mesh clouds
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / routing.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 "routing.h"
24 #include "send.h"
25 #include "hash.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "icmp_socket.h"
29 #include "translation-table.h"
30 #include "originator.h"
31 #include "types.h"
32 #include "ring_buffer.h"
33 #include "vis.h"
34 #include "aggregation.h"
35 #include "unicast.h"
36
37 void slide_own_bcast_window(struct batman_if *batman_if)
38 {
39         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
40         HASHIT(hashit);
41         struct orig_node *orig_node;
42         TYPE_OF_WORD *word;
43         unsigned long flags;
44
45         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
46
47         while (hash_iterate(bat_priv->orig_hash, &hashit)) {
48                 orig_node = hashit.bucket->data;
49                 word = &(orig_node->bcast_own[batman_if->if_num * NUM_WORDS]);
50
51                 bit_get_packet(bat_priv, word, 1, 0);
52                 orig_node->bcast_own_sum[batman_if->if_num] =
53                         bit_packet_count(word);
54         }
55
56         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
57 }
58
59 static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
60                        unsigned char *hna_buff, int hna_buff_len)
61 {
62         if ((hna_buff_len != orig_node->hna_buff_len) ||
63             ((hna_buff_len > 0) &&
64              (orig_node->hna_buff_len > 0) &&
65              (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
66
67                 if (orig_node->hna_buff_len > 0)
68                         hna_global_del_orig(bat_priv, orig_node,
69                                             "originator changed hna");
70
71                 if ((hna_buff_len > 0) && (hna_buff != NULL))
72                         hna_global_add_orig(bat_priv, orig_node,
73                                             hna_buff, hna_buff_len);
74         }
75 }
76
77 static void update_route(struct bat_priv *bat_priv,
78                          struct orig_node *orig_node,
79                          struct neigh_node *neigh_node,
80                          unsigned char *hna_buff, int hna_buff_len)
81 {
82         /* route deleted */
83         if ((orig_node->router != NULL) && (neigh_node == NULL)) {
84
85                 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
86                         orig_node->orig);
87                 hna_global_del_orig(bat_priv, orig_node,
88                                     "originator timed out");
89
90                 /* route added */
91         } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
92
93                 bat_dbg(DBG_ROUTES, bat_priv,
94                         "Adding route towards: %pM (via %pM)\n",
95                         orig_node->orig, neigh_node->addr);
96                 hna_global_add_orig(bat_priv, orig_node,
97                                     hna_buff, hna_buff_len);
98
99                 /* route changed */
100         } else {
101                 bat_dbg(DBG_ROUTES, bat_priv,
102                         "Changing route towards: %pM "
103                         "(now via %pM - was via %pM)\n",
104                         orig_node->orig, neigh_node->addr,
105                         orig_node->router->addr);
106         }
107
108         orig_node->router = neigh_node;
109 }
110
111
112 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
113                    struct neigh_node *neigh_node, unsigned char *hna_buff,
114                    int hna_buff_len)
115 {
116
117         if (orig_node == NULL)
118                 return;
119
120         if (orig_node->router != neigh_node)
121                 update_route(bat_priv, orig_node, neigh_node,
122                              hna_buff, hna_buff_len);
123         /* may be just HNA changed */
124         else
125                 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
126 }
127
128 static int is_bidirectional_neigh(struct orig_node *orig_node,
129                                 struct orig_node *orig_neigh_node,
130                                 struct batman_packet *batman_packet,
131                                 struct batman_if *if_incoming)
132 {
133         struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
134         struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
135         unsigned char total_count;
136
137         if (orig_node == orig_neigh_node) {
138                 list_for_each_entry(tmp_neigh_node,
139                                     &orig_node->neigh_list,
140                                     list) {
141
142                         if (compare_orig(tmp_neigh_node->addr,
143                                          orig_neigh_node->orig) &&
144                             (tmp_neigh_node->if_incoming == if_incoming))
145                                 neigh_node = tmp_neigh_node;
146                 }
147
148                 if (!neigh_node)
149                         neigh_node = create_neighbor(orig_node,
150                                                      orig_neigh_node,
151                                                      orig_neigh_node->orig,
152                                                      if_incoming);
153                 /* create_neighbor failed, return 0 */
154                 if (!neigh_node)
155                         return 0;
156
157                 neigh_node->last_valid = jiffies;
158         } else {
159                 /* find packet count of corresponding one hop neighbor */
160                 list_for_each_entry(tmp_neigh_node,
161                                     &orig_neigh_node->neigh_list, list) {
162
163                         if (compare_orig(tmp_neigh_node->addr,
164                                          orig_neigh_node->orig) &&
165                             (tmp_neigh_node->if_incoming == if_incoming))
166                                 neigh_node = tmp_neigh_node;
167                 }
168
169                 if (!neigh_node)
170                         neigh_node = create_neighbor(orig_neigh_node,
171                                                      orig_neigh_node,
172                                                      orig_neigh_node->orig,
173                                                      if_incoming);
174                 /* create_neighbor failed, return 0 */
175                 if (!neigh_node)
176                         return 0;
177         }
178
179         orig_node->last_valid = jiffies;
180
181         /* pay attention to not get a value bigger than 100 % */
182         total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
183                        neigh_node->real_packet_count ?
184                        neigh_node->real_packet_count :
185                        orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
186
187         /* if we have too few packets (too less data) we set tq_own to zero */
188         /* if we receive too few packets it is not considered bidirectional */
189         if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
190             (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
191                 orig_neigh_node->tq_own = 0;
192         else
193                 /* neigh_node->real_packet_count is never zero as we
194                  * only purge old information when getting new
195                  * information */
196                 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
197                         neigh_node->real_packet_count;
198
199         /*
200          * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
201          * affect the nearly-symmetric links only a little, but
202          * punishes asymmetric links more.  This will give a value
203          * between 0 and TQ_MAX_VALUE
204          */
205         orig_neigh_node->tq_asym_penalty =
206                 TQ_MAX_VALUE -
207                 (TQ_MAX_VALUE *
208                  (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
209                  (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
210                  (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
211                 (TQ_LOCAL_WINDOW_SIZE *
212                  TQ_LOCAL_WINDOW_SIZE *
213                  TQ_LOCAL_WINDOW_SIZE);
214
215         batman_packet->tq = ((batman_packet->tq *
216                               orig_neigh_node->tq_own *
217                               orig_neigh_node->tq_asym_penalty) /
218                              (TQ_MAX_VALUE * TQ_MAX_VALUE));
219
220         bat_dbg(DBG_BATMAN, bat_priv,
221                 "bidirectional: "
222                 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
223                 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
224                 "total tq: %3i\n",
225                 orig_node->orig, orig_neigh_node->orig, total_count,
226                 neigh_node->real_packet_count, orig_neigh_node->tq_own,
227                 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
228
229         /* if link has the minimum required transmission quality
230          * consider it bidirectional */
231         if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
232                 return 1;
233
234         return 0;
235 }
236
237 static void update_orig(struct bat_priv *bat_priv,
238                         struct orig_node *orig_node,
239                         struct ethhdr *ethhdr,
240                         struct batman_packet *batman_packet,
241                         struct batman_if *if_incoming,
242                         unsigned char *hna_buff, int hna_buff_len,
243                         char is_duplicate)
244 {
245         struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
246         int tmp_hna_buff_len;
247
248         bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
249                 "Searching and updating originator entry of received packet\n");
250
251         list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
252                 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
253                     (tmp_neigh_node->if_incoming == if_incoming)) {
254                         neigh_node = tmp_neigh_node;
255                         continue;
256                 }
257
258                 if (is_duplicate)
259                         continue;
260
261                 ring_buffer_set(tmp_neigh_node->tq_recv,
262                                 &tmp_neigh_node->tq_index, 0);
263                 tmp_neigh_node->tq_avg =
264                         ring_buffer_avg(tmp_neigh_node->tq_recv);
265         }
266
267         if (!neigh_node) {
268                 struct orig_node *orig_tmp;
269
270                 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
271                 if (!orig_tmp)
272                         return;
273
274                 neigh_node = create_neighbor(orig_node, orig_tmp,
275                                              ethhdr->h_source, if_incoming);
276                 if (!neigh_node)
277                         return;
278         } else
279                 bat_dbg(DBG_BATMAN, bat_priv,
280                         "Updating existing last-hop neighbor of originator\n");
281
282         orig_node->flags = batman_packet->flags;
283         neigh_node->last_valid = jiffies;
284
285         ring_buffer_set(neigh_node->tq_recv,
286                         &neigh_node->tq_index,
287                         batman_packet->tq);
288         neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
289
290         if (!is_duplicate) {
291                 orig_node->last_ttl = batman_packet->ttl;
292                 neigh_node->last_ttl = batman_packet->ttl;
293         }
294
295         tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
296                             batman_packet->num_hna * ETH_ALEN : hna_buff_len);
297
298         /* if this neighbor already is our next hop there is nothing
299          * to change */
300         if (orig_node->router == neigh_node)
301                 goto update_hna;
302
303         /* if this neighbor does not offer a better TQ we won't consider it */
304         if ((orig_node->router) &&
305             (orig_node->router->tq_avg > neigh_node->tq_avg))
306                 goto update_hna;
307
308         /* if the TQ is the same and the link not more symetric we
309          * won't consider it either */
310         if ((orig_node->router) &&
311              ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
312              (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
313               >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
314                 goto update_hna;
315
316         update_routes(bat_priv, orig_node, neigh_node,
317                       hna_buff, tmp_hna_buff_len);
318         return;
319
320 update_hna:
321         update_routes(bat_priv, orig_node, orig_node->router,
322                       hna_buff, tmp_hna_buff_len);
323 }
324
325 /* checks whether the host restarted and is in the protection time.
326  * returns:
327  *  0 if the packet is to be accepted
328  *  1 if the packet is to be ignored.
329  */
330 static int window_protected(struct bat_priv *bat_priv,
331                             int32_t seq_num_diff,
332                             unsigned long *last_reset)
333 {
334         if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
335                 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
336                 if (time_after(jiffies, *last_reset +
337                         msecs_to_jiffies(RESET_PROTECTION_MS))) {
338
339                         *last_reset = jiffies;
340                         bat_dbg(DBG_BATMAN, bat_priv,
341                                 "old packet received, start protection\n");
342
343                         return 0;
344                 } else
345                         return 1;
346         }
347         return 0;
348 }
349
350 /* processes a batman packet for all interfaces, adjusts the sequence number and
351  * finds out whether it is a duplicate.
352  * returns:
353  *   1 the packet is a duplicate
354  *   0 the packet has not yet been received
355  *  -1 the packet is old and has been received while the seqno window
356  *     was protected. Caller should drop it.
357  */
358 static char count_real_packets(struct ethhdr *ethhdr,
359                                struct batman_packet *batman_packet,
360                                struct batman_if *if_incoming)
361 {
362         struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
363         struct orig_node *orig_node;
364         struct neigh_node *tmp_neigh_node;
365         char is_duplicate = 0;
366         int32_t seq_diff;
367         int need_update = 0;
368         int set_mark;
369
370         orig_node = get_orig_node(bat_priv, batman_packet->orig);
371         if (orig_node == NULL)
372                 return 0;
373
374         seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
375
376         /* signalize caller that the packet is to be dropped. */
377         if (window_protected(bat_priv, seq_diff,
378                              &orig_node->batman_seqno_reset))
379                 return -1;
380
381         list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
382
383                 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
384                                                orig_node->last_real_seqno,
385                                                batman_packet->seqno);
386
387                 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
388                     (tmp_neigh_node->if_incoming == if_incoming))
389                         set_mark = 1;
390                 else
391                         set_mark = 0;
392
393                 /* if the window moved, set the update flag. */
394                 need_update |= bit_get_packet(bat_priv,
395                                               tmp_neigh_node->real_bits,
396                                               seq_diff, set_mark);
397
398                 tmp_neigh_node->real_packet_count =
399                         bit_packet_count(tmp_neigh_node->real_bits);
400         }
401
402         if (need_update) {
403                 bat_dbg(DBG_BATMAN, bat_priv,
404                         "updating last_seqno: old %d, new %d\n",
405                         orig_node->last_real_seqno, batman_packet->seqno);
406                 orig_node->last_real_seqno = batman_packet->seqno;
407         }
408
409         return is_duplicate;
410 }
411
412 /* copy primary address for bonding */
413 static void mark_bonding_address(struct bat_priv *bat_priv,
414                                  struct orig_node *orig_node,
415                                  struct orig_node *orig_neigh_node,
416                                  struct batman_packet *batman_packet)
417
418 {
419         if (batman_packet->flags & PRIMARIES_FIRST_HOP)
420                 memcpy(orig_neigh_node->primary_addr,
421                        orig_node->orig, ETH_ALEN);
422
423         return;
424 }
425
426 /* mark possible bond.candidates in the neighbor list */
427 void update_bonding_candidates(struct bat_priv *bat_priv,
428                                struct orig_node *orig_node)
429 {
430         int candidates;
431         int interference_candidate;
432         int best_tq;
433         struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
434         struct neigh_node *first_candidate, *last_candidate;
435
436         /* update the candidates for this originator */
437         if (!orig_node->router) {
438                 orig_node->bond.candidates = 0;
439                 return;
440         }
441
442         best_tq = orig_node->router->tq_avg;
443
444         /* update bond.candidates */
445
446         candidates = 0;
447
448         /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
449          * as "bonding partner" */
450
451         /* first, zero the list */
452         list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
453                 tmp_neigh_node->next_bond_candidate = NULL;
454         }
455
456         first_candidate = NULL;
457         last_candidate = NULL;
458         list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
459
460                 /* only consider if it has the same primary address ...  */
461                 if (memcmp(orig_node->orig,
462                                 tmp_neigh_node->orig_node->primary_addr,
463                                 ETH_ALEN) != 0)
464                         continue;
465
466                 /* ... and is good enough to be considered */
467                 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
468                         continue;
469
470                 /* check if we have another candidate with the same
471                  * mac address or interface. If we do, we won't
472                  * select this candidate because of possible interference. */
473
474                 interference_candidate = 0;
475                 list_for_each_entry(tmp_neigh_node2,
476                                 &orig_node->neigh_list, list) {
477
478                         if (tmp_neigh_node2 == tmp_neigh_node)
479                                 continue;
480
481                         /* we only care if the other candidate is even
482                          * considered as candidate. */
483                         if (tmp_neigh_node2->next_bond_candidate == NULL)
484                                 continue;
485
486
487                         if ((tmp_neigh_node->if_incoming ==
488                                 tmp_neigh_node2->if_incoming)
489                                 || (memcmp(tmp_neigh_node->addr,
490                                 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
491
492                                 interference_candidate = 1;
493                                 break;
494                         }
495                 }
496                 /* don't care further if it is an interference candidate */
497                 if (interference_candidate)
498                         continue;
499
500                 if (first_candidate == NULL) {
501                         first_candidate = tmp_neigh_node;
502                         tmp_neigh_node->next_bond_candidate = first_candidate;
503                 } else
504                         tmp_neigh_node->next_bond_candidate = last_candidate;
505
506                 last_candidate = tmp_neigh_node;
507
508                 candidates++;
509         }
510
511         if (candidates > 0) {
512                 first_candidate->next_bond_candidate = last_candidate;
513                 orig_node->bond.selected = first_candidate;
514         }
515
516         orig_node->bond.candidates = candidates;
517 }
518
519 void receive_bat_packet(struct ethhdr *ethhdr,
520                                 struct batman_packet *batman_packet,
521                                 unsigned char *hna_buff, int hna_buff_len,
522                                 struct batman_if *if_incoming)
523 {
524         struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
525         struct batman_if *batman_if;
526         struct orig_node *orig_neigh_node, *orig_node;
527         char has_directlink_flag;
528         char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
529         char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
530         char is_duplicate;
531         uint32_t if_incoming_seqno;
532
533         /* Silently drop when the batman packet is actually not a
534          * correct packet.
535          *
536          * This might happen if a packet is padded (e.g. Ethernet has a
537          * minimum frame length of 64 byte) and the aggregation interprets
538          * it as an additional length.
539          *
540          * TODO: A more sane solution would be to have a bit in the
541          * batman_packet to detect whether the packet is the last
542          * packet in an aggregation.  Here we expect that the padding
543          * is always zero (or not 0x01)
544          */
545         if (batman_packet->packet_type != BAT_PACKET)
546                 return;
547
548         /* could be changed by schedule_own_packet() */
549         if_incoming_seqno = atomic_read(&if_incoming->seqno);
550
551         has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
552
553         is_single_hop_neigh = (compare_orig(ethhdr->h_source,
554                                             batman_packet->orig) ? 1 : 0);
555
556         bat_dbg(DBG_BATMAN, bat_priv,
557                 "Received BATMAN packet via NB: %pM, IF: %s [%s] "
558                 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
559                 "TTL %d, V %d, IDF %d)\n",
560                 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
561                 batman_packet->orig, batman_packet->prev_sender,
562                 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
563                 batman_packet->version, has_directlink_flag);
564
565         list_for_each_entry_rcu(batman_if, &if_list, list) {
566                 if (batman_if->if_status != IF_ACTIVE)
567                         continue;
568
569                 if (batman_if->soft_iface != if_incoming->soft_iface)
570                         continue;
571
572                 if (compare_orig(ethhdr->h_source,
573                                  batman_if->net_dev->dev_addr))
574                         is_my_addr = 1;
575
576                 if (compare_orig(batman_packet->orig,
577                                  batman_if->net_dev->dev_addr))
578                         is_my_orig = 1;
579
580                 if (compare_orig(batman_packet->prev_sender,
581                                  batman_if->net_dev->dev_addr))
582                         is_my_oldorig = 1;
583
584                 if (compare_orig(ethhdr->h_source, broadcast_addr))
585                         is_broadcast = 1;
586         }
587
588         if (batman_packet->version != COMPAT_VERSION) {
589                 bat_dbg(DBG_BATMAN, bat_priv,
590                         "Drop packet: incompatible batman version (%i)\n",
591                         batman_packet->version);
592                 return;
593         }
594
595         if (is_my_addr) {
596                 bat_dbg(DBG_BATMAN, bat_priv,
597                         "Drop packet: received my own broadcast (sender: %pM"
598                         ")\n",
599                         ethhdr->h_source);
600                 return;
601         }
602
603         if (is_broadcast) {
604                 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
605                 "ignoring all packets with broadcast source addr (sender: %pM"
606                 ")\n", ethhdr->h_source);
607                 return;
608         }
609
610         if (is_my_orig) {
611                 TYPE_OF_WORD *word;
612                 int offset;
613
614                 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
615
616                 if (!orig_neigh_node)
617                         return;
618
619                 /* neighbor has to indicate direct link and it has to
620                  * come via the corresponding interface */
621                 /* if received seqno equals last send seqno save new
622                  * seqno for bidirectional check */
623                 if (has_directlink_flag &&
624                     compare_orig(if_incoming->net_dev->dev_addr,
625                                  batman_packet->orig) &&
626                     (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
627                         offset = if_incoming->if_num * NUM_WORDS;
628                         word = &(orig_neigh_node->bcast_own[offset]);
629                         bit_mark(word, 0);
630                         orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
631                                 bit_packet_count(word);
632                 }
633
634                 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
635                         "originator packet from myself (via neighbor)\n");
636                 return;
637         }
638
639         if (is_my_oldorig) {
640                 bat_dbg(DBG_BATMAN, bat_priv,
641                         "Drop packet: ignoring all rebroadcast echos (sender: "
642                         "%pM)\n", ethhdr->h_source);
643                 return;
644         }
645
646         orig_node = get_orig_node(bat_priv, batman_packet->orig);
647         if (orig_node == NULL)
648                 return;
649
650         is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
651
652         if (is_duplicate == -1) {
653                 bat_dbg(DBG_BATMAN, bat_priv,
654                         "Drop packet: packet within seqno protection time "
655                         "(sender: %pM)\n", ethhdr->h_source);
656                 return;
657         }
658
659         if (batman_packet->tq == 0) {
660                 bat_dbg(DBG_BATMAN, bat_priv,
661                         "Drop packet: originator packet with tq equal 0\n");
662                 return;
663         }
664
665         /* avoid temporary routing loops */
666         if ((orig_node->router) &&
667             (orig_node->router->orig_node->router) &&
668             (compare_orig(orig_node->router->addr,
669                           batman_packet->prev_sender)) &&
670             !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
671             (compare_orig(orig_node->router->addr,
672                           orig_node->router->orig_node->router->addr))) {
673                 bat_dbg(DBG_BATMAN, bat_priv,
674                         "Drop packet: ignoring all rebroadcast packets that "
675                         "may make me loop (sender: %pM)\n", ethhdr->h_source);
676                 return;
677         }
678
679         /* if sender is a direct neighbor the sender mac equals
680          * originator mac */
681         orig_neigh_node = (is_single_hop_neigh ?
682                            orig_node :
683                            get_orig_node(bat_priv, ethhdr->h_source));
684         if (orig_neigh_node == NULL)
685                 return;
686
687         /* drop packet if sender is not a direct neighbor and if we
688          * don't route towards it */
689         if (!is_single_hop_neigh &&
690             (orig_neigh_node->router == NULL)) {
691                 bat_dbg(DBG_BATMAN, bat_priv,
692                         "Drop packet: OGM via unknown neighbor!\n");
693                 return;
694         }
695
696         is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
697                                                 batman_packet, if_incoming);
698
699         /* update ranking if it is not a duplicate or has the same
700          * seqno and similar ttl as the non-duplicate */
701         if (is_bidirectional &&
702             (!is_duplicate ||
703              ((orig_node->last_real_seqno == batman_packet->seqno) &&
704               (orig_node->last_ttl - 3 <= batman_packet->ttl))))
705                 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
706                             if_incoming, hna_buff, hna_buff_len, is_duplicate);
707
708         mark_bonding_address(bat_priv, orig_node,
709                              orig_neigh_node, batman_packet);
710         update_bonding_candidates(bat_priv, orig_node);
711
712         /* is single hop (direct) neighbor */
713         if (is_single_hop_neigh) {
714
715                 /* mark direct link on incoming interface */
716                 schedule_forward_packet(orig_node, ethhdr, batman_packet,
717                                         1, hna_buff_len, if_incoming);
718
719                 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
720                         "rebroadcast neighbor packet with direct link flag\n");
721                 return;
722         }
723
724         /* multihop originator */
725         if (!is_bidirectional) {
726                 bat_dbg(DBG_BATMAN, bat_priv,
727                         "Drop packet: not received via bidirectional link\n");
728                 return;
729         }
730
731         if (is_duplicate) {
732                 bat_dbg(DBG_BATMAN, bat_priv,
733                         "Drop packet: duplicate packet received\n");
734                 return;
735         }
736
737         bat_dbg(DBG_BATMAN, bat_priv,
738                 "Forwarding packet: rebroadcast originator packet\n");
739         schedule_forward_packet(orig_node, ethhdr, batman_packet,
740                                 0, hna_buff_len, if_incoming);
741 }
742
743 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
744 {
745         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
746         struct ethhdr *ethhdr;
747         unsigned long flags;
748
749         /* drop packet if it has not necessary minimum size */
750         if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
751                 return NET_RX_DROP;
752
753         ethhdr = (struct ethhdr *)skb_mac_header(skb);
754
755         /* packet with broadcast indication but unicast recipient */
756         if (!is_bcast(ethhdr->h_dest))
757                 return NET_RX_DROP;
758
759         /* packet with broadcast sender address */
760         if (is_bcast(ethhdr->h_source))
761                 return NET_RX_DROP;
762
763         /* create a copy of the skb, if needed, to modify it. */
764         if (skb_cow(skb, 0) < 0)
765                 return NET_RX_DROP;
766
767         /* keep skb linear */
768         if (skb_linearize(skb) < 0)
769                 return NET_RX_DROP;
770
771         ethhdr = (struct ethhdr *)skb_mac_header(skb);
772
773         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
774         receive_aggr_bat_packet(ethhdr,
775                                 skb->data,
776                                 skb_headlen(skb),
777                                 batman_if);
778         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
779
780         kfree_skb(skb);
781         return NET_RX_SUCCESS;
782 }
783
784 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
785                                struct sk_buff *skb, size_t icmp_len)
786 {
787         struct orig_node *orig_node;
788         struct icmp_packet_rr *icmp_packet;
789         struct ethhdr *ethhdr;
790         struct batman_if *batman_if;
791         int ret;
792         unsigned long flags;
793         uint8_t dstaddr[ETH_ALEN];
794
795         icmp_packet = (struct icmp_packet_rr *)skb->data;
796         ethhdr = (struct ethhdr *)skb_mac_header(skb);
797
798         /* add data to device queue */
799         if (icmp_packet->msg_type != ECHO_REQUEST) {
800                 bat_socket_receive_packet(icmp_packet, icmp_len);
801                 return NET_RX_DROP;
802         }
803
804         if (!bat_priv->primary_if)
805                 return NET_RX_DROP;
806
807         /* answer echo request (ping) */
808         /* get routing information */
809         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
810         orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
811                                                    icmp_packet->orig));
812         ret = NET_RX_DROP;
813
814         if ((orig_node != NULL) &&
815             (orig_node->router != NULL)) {
816
817                 /* don't lock while sending the packets ... we therefore
818                  * copy the required data before sending */
819                 batman_if = orig_node->router->if_incoming;
820                 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
821                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
822
823                 /* create a copy of the skb, if needed, to modify it. */
824                 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
825                         return NET_RX_DROP;
826
827                 icmp_packet = (struct icmp_packet_rr *)skb->data;
828                 ethhdr = (struct ethhdr *)skb_mac_header(skb);
829
830                 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
831                 memcpy(icmp_packet->orig,
832                        bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
833                 icmp_packet->msg_type = ECHO_REPLY;
834                 icmp_packet->ttl = TTL;
835
836                 send_skb_packet(skb, batman_if, dstaddr);
837                 ret = NET_RX_SUCCESS;
838
839         } else
840                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
841
842         return ret;
843 }
844
845 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
846                                   struct sk_buff *skb, size_t icmp_len)
847 {
848         struct orig_node *orig_node;
849         struct icmp_packet *icmp_packet;
850         struct ethhdr *ethhdr;
851         struct batman_if *batman_if;
852         int ret;
853         unsigned long flags;
854         uint8_t dstaddr[ETH_ALEN];
855
856         icmp_packet = (struct icmp_packet *)skb->data;
857         ethhdr = (struct ethhdr *)skb_mac_header(skb);
858
859         /* send TTL exceeded if packet is an echo request (traceroute) */
860         if (icmp_packet->msg_type != ECHO_REQUEST) {
861                 pr_warning("Warning - can't forward icmp packet from %pM to "
862                            "%pM: ttl exceeded\n", icmp_packet->orig,
863                            icmp_packet->dst);
864                 return NET_RX_DROP;
865         }
866
867         if (!bat_priv->primary_if)
868                 return NET_RX_DROP;
869
870         /* get routing information */
871         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
872         orig_node = ((struct orig_node *)
873                      hash_find(bat_priv->orig_hash, icmp_packet->orig));
874         ret = NET_RX_DROP;
875
876         if ((orig_node != NULL) &&
877             (orig_node->router != NULL)) {
878
879                 /* don't lock while sending the packets ... we therefore
880                  * copy the required data before sending */
881                 batman_if = orig_node->router->if_incoming;
882                 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
883                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
884
885                 /* create a copy of the skb, if needed, to modify it. */
886                 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
887                         return NET_RX_DROP;
888
889                 icmp_packet = (struct icmp_packet *) skb->data;
890                 ethhdr = (struct ethhdr *)skb_mac_header(skb);
891
892                 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
893                 memcpy(icmp_packet->orig,
894                        bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
895                 icmp_packet->msg_type = TTL_EXCEEDED;
896                 icmp_packet->ttl = TTL;
897
898                 send_skb_packet(skb, batman_if, dstaddr);
899                 ret = NET_RX_SUCCESS;
900
901         } else
902                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
903
904         return ret;
905 }
906
907
908 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
909 {
910         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
911         struct icmp_packet_rr *icmp_packet;
912         struct ethhdr *ethhdr;
913         struct orig_node *orig_node;
914         struct batman_if *batman_if;
915         int hdr_size = sizeof(struct icmp_packet);
916         int ret;
917         unsigned long flags;
918         uint8_t dstaddr[ETH_ALEN];
919
920         /**
921          * we truncate all incoming icmp packets if they don't match our size
922          */
923         if (skb->len >= sizeof(struct icmp_packet_rr))
924                 hdr_size = sizeof(struct icmp_packet_rr);
925
926         /* drop packet if it has not necessary minimum size */
927         if (unlikely(!pskb_may_pull(skb, hdr_size)))
928                 return NET_RX_DROP;
929
930         ethhdr = (struct ethhdr *)skb_mac_header(skb);
931
932         /* packet with unicast indication but broadcast recipient */
933         if (is_bcast(ethhdr->h_dest))
934                 return NET_RX_DROP;
935
936         /* packet with broadcast sender address */
937         if (is_bcast(ethhdr->h_source))
938                 return NET_RX_DROP;
939
940         /* not for me */
941         if (!is_my_mac(ethhdr->h_dest))
942                 return NET_RX_DROP;
943
944         icmp_packet = (struct icmp_packet_rr *)skb->data;
945
946         /* add record route information if not full */
947         if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
948             (icmp_packet->rr_cur < BAT_RR_LEN)) {
949                 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
950                         ethhdr->h_dest, ETH_ALEN);
951                 icmp_packet->rr_cur++;
952         }
953
954         /* packet for me */
955         if (is_my_mac(icmp_packet->dst))
956                 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
957
958         /* TTL exceeded */
959         if (icmp_packet->ttl < 2)
960                 return recv_icmp_ttl_exceeded(bat_priv, skb, hdr_size);
961
962         ret = NET_RX_DROP;
963
964         /* get routing information */
965         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
966         orig_node = ((struct orig_node *)
967                      hash_find(bat_priv->orig_hash, icmp_packet->dst));
968
969         if ((orig_node != NULL) &&
970             (orig_node->router != NULL)) {
971
972                 /* don't lock while sending the packets ... we therefore
973                  * copy the required data before sending */
974                 batman_if = orig_node->router->if_incoming;
975                 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
976                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
977
978                 /* create a copy of the skb, if needed, to modify it. */
979                 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
980                         return NET_RX_DROP;
981
982                 icmp_packet = (struct icmp_packet_rr *)skb->data;
983                 ethhdr = (struct ethhdr *)skb_mac_header(skb);
984
985                 /* decrement ttl */
986                 icmp_packet->ttl--;
987
988                 /* route it */
989                 send_skb_packet(skb, batman_if, dstaddr);
990                 ret = NET_RX_SUCCESS;
991
992         } else
993                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
994
995         return ret;
996 }
997
998 /* find a suitable router for this originator, and use
999  * bonding if possible. */
1000 struct neigh_node *find_router(struct orig_node *orig_node,
1001                                struct batman_if *recv_if)
1002 {
1003         struct bat_priv *bat_priv;
1004         struct orig_node *primary_orig_node;
1005         struct orig_node *router_orig;
1006         struct neigh_node *router, *first_candidate, *best_router;
1007         static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1008         int bonding_enabled;
1009
1010         if (!orig_node)
1011                 return NULL;
1012
1013         if (!orig_node->router)
1014                 return NULL;
1015
1016         /* without bonding, the first node should
1017          * always choose the default router. */
1018
1019         if (!recv_if)
1020                 return orig_node->router;
1021
1022         bat_priv = netdev_priv(recv_if->soft_iface);
1023         bonding_enabled = atomic_read(&bat_priv->bonding_enabled);
1024
1025         if (!bonding_enabled)
1026                 return orig_node->router;
1027
1028         router_orig = orig_node->router->orig_node;
1029
1030         /* if we have something in the primary_addr, we can search
1031          * for a potential bonding candidate. */
1032         if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1033                 return orig_node->router;
1034
1035         /* find the orig_node which has the primary interface. might
1036          * even be the same as our router_orig in many cases */
1037
1038         if (memcmp(router_orig->primary_addr,
1039                                 router_orig->orig, ETH_ALEN) == 0) {
1040                 primary_orig_node = router_orig;
1041         } else {
1042                 primary_orig_node = hash_find(bat_priv->orig_hash,
1043                                                 router_orig->primary_addr);
1044
1045                 if (!primary_orig_node)
1046                         return orig_node->router;
1047         }
1048
1049         /* with less than 2 candidates, we can't do any
1050          * bonding and prefer the original router. */
1051
1052         if (primary_orig_node->bond.candidates < 2)
1053                 return orig_node->router;
1054
1055
1056         /* all nodes between should choose a candidate which
1057          * is is not on the interface where the packet came
1058          * in. */
1059         first_candidate = primary_orig_node->bond.selected;
1060         router = first_candidate;
1061
1062         if (bonding_enabled) {
1063                 /* in the bonding case, send the packets in a round
1064                  * robin fashion over the remaining interfaces. */
1065                 do {
1066                         /* recv_if == NULL on the first node. */
1067                         if (router->if_incoming != recv_if)
1068                                 break;
1069
1070                         router = router->next_bond_candidate;
1071                 } while (router != first_candidate);
1072
1073                 primary_orig_node->bond.selected = router->next_bond_candidate;
1074
1075         } else {
1076                 /* if bonding is disabled, use the best of the
1077                  * remaining candidates which are not using
1078                  * this interface. */
1079                 best_router = first_candidate;
1080
1081                 do {
1082                         /* recv_if == NULL on the first node. */
1083                         if ((router->if_incoming != recv_if) &&
1084                                 (router->tq_avg > best_router->tq_avg))
1085                                         best_router = router;
1086
1087                         router = router->next_bond_candidate;
1088                 } while (router != first_candidate);
1089
1090                 router = best_router;
1091         }
1092
1093         return router;
1094 }
1095
1096 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1097 {
1098         struct ethhdr *ethhdr;
1099
1100         /* drop packet if it has not necessary minimum size */
1101         if (unlikely(!pskb_may_pull(skb, hdr_size)))
1102                 return -1;
1103
1104         ethhdr = (struct ethhdr *)skb_mac_header(skb);
1105
1106         /* packet with unicast indication but broadcast recipient */
1107         if (is_bcast(ethhdr->h_dest))
1108                 return -1;
1109
1110         /* packet with broadcast sender address */
1111         if (is_bcast(ethhdr->h_source))
1112                 return -1;
1113
1114         /* not for me */
1115         if (!is_my_mac(ethhdr->h_dest))
1116                 return -1;
1117
1118         return 0;
1119 }
1120
1121 static int route_unicast_packet(struct sk_buff *skb,
1122                                 struct batman_if *recv_if, int hdr_size)
1123 {
1124         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1125         struct orig_node *orig_node;
1126         struct neigh_node *router;
1127         struct batman_if *batman_if;
1128         uint8_t dstaddr[ETH_ALEN];
1129         unsigned long flags;
1130         struct unicast_packet *unicast_packet;
1131         struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1132
1133         unicast_packet = (struct unicast_packet *)skb->data;
1134
1135         /* packet for me */
1136         if (is_my_mac(unicast_packet->dest)) {
1137                 interface_rx(recv_if->soft_iface, skb, hdr_size);
1138                 return NET_RX_SUCCESS;
1139         }
1140
1141         /* TTL exceeded */
1142         if (unicast_packet->ttl < 2) {
1143                 pr_warning("Warning - can't forward unicast packet from %pM to "
1144                            "%pM: ttl exceeded\n", ethhdr->h_source,
1145                            unicast_packet->dest);
1146                 return NET_RX_DROP;
1147         }
1148
1149         /* get routing information */
1150         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
1151         orig_node = ((struct orig_node *)
1152                      hash_find(bat_priv->orig_hash, unicast_packet->dest));
1153
1154         router = find_router(orig_node, recv_if);
1155
1156         if (!router) {
1157                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1158                 return NET_RX_DROP;
1159         }
1160
1161         /* don't lock while sending the packets ... we therefore
1162          * copy the required data before sending */
1163
1164         batman_if = router->if_incoming;
1165         memcpy(dstaddr, router->addr, ETH_ALEN);
1166
1167         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1168
1169         /* create a copy of the skb, if needed, to modify it. */
1170         if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1171                 return NET_RX_DROP;
1172
1173         unicast_packet = (struct unicast_packet *)skb->data;
1174         ethhdr = (struct ethhdr *)skb_mac_header(skb);
1175
1176         /* decrement ttl */
1177         unicast_packet->ttl--;
1178
1179         /* route it */
1180         send_skb_packet(skb, batman_if, dstaddr);
1181
1182         return NET_RX_SUCCESS;
1183 }
1184
1185 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1186 {
1187         struct unicast_packet *unicast_packet;
1188         int hdr_size = sizeof(struct unicast_packet);
1189
1190         if (check_unicast_packet(skb, hdr_size) < 0)
1191                 return NET_RX_DROP;
1192
1193         unicast_packet = (struct unicast_packet *)skb->data;
1194
1195         /* packet for me */
1196         if (is_my_mac(unicast_packet->dest)) {
1197                 interface_rx(recv_if->soft_iface, skb, hdr_size);
1198                 return NET_RX_SUCCESS;
1199         }
1200
1201         return route_unicast_packet(skb, recv_if, hdr_size);
1202 }
1203
1204 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1205 {
1206         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1207         struct unicast_frag_packet *unicast_packet;
1208         struct orig_node *orig_node;
1209         struct frag_packet_list_entry *tmp_frag_entry;
1210         int hdr_size = sizeof(struct unicast_frag_packet);
1211         unsigned long flags;
1212
1213         if (check_unicast_packet(skb, hdr_size) < 0)
1214                 return NET_RX_DROP;
1215
1216         unicast_packet = (struct unicast_frag_packet *)skb->data;
1217
1218         /* packet for me */
1219         if (is_my_mac(unicast_packet->dest)) {
1220
1221                 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
1222                 orig_node = ((struct orig_node *)
1223                         hash_find(bat_priv->orig_hash, unicast_packet->orig));
1224
1225                 if (!orig_node) {
1226                         pr_warning("couldn't find orig node for "
1227                                 "fragmentation\n");
1228                         spin_unlock_irqrestore(&bat_priv->orig_hash_lock,
1229                                                flags);
1230                         return NET_RX_DROP;
1231                 }
1232
1233                 orig_node->last_frag_packet = jiffies;
1234
1235                 if (list_empty(&orig_node->frag_list))
1236                         create_frag_buffer(&orig_node->frag_list);
1237
1238                 tmp_frag_entry =
1239                         search_frag_packet(&orig_node->frag_list,
1240                                            unicast_packet);
1241
1242                 if (!tmp_frag_entry) {
1243                         create_frag_entry(&orig_node->frag_list, skb);
1244                         spin_unlock_irqrestore(&bat_priv->orig_hash_lock,
1245                                                flags);
1246                         return NET_RX_SUCCESS;
1247                 }
1248
1249                 skb = merge_frag_packet(&orig_node->frag_list,
1250                                         tmp_frag_entry, skb);
1251                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1252                 if (!skb)
1253                         return NET_RX_DROP;
1254
1255                 interface_rx(recv_if->soft_iface, skb, hdr_size);
1256                 return NET_RX_SUCCESS;
1257         }
1258
1259         return route_unicast_packet(skb, recv_if, hdr_size);
1260 }
1261
1262
1263 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1264 {
1265         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1266         struct orig_node *orig_node;
1267         struct bcast_packet *bcast_packet;
1268         struct ethhdr *ethhdr;
1269         int hdr_size = sizeof(struct bcast_packet);
1270         int32_t seq_diff;
1271         unsigned long flags;
1272
1273         /* drop packet if it has not necessary minimum size */
1274         if (unlikely(!pskb_may_pull(skb, hdr_size)))
1275                 return NET_RX_DROP;
1276
1277         ethhdr = (struct ethhdr *)skb_mac_header(skb);
1278
1279         /* packet with broadcast indication but unicast recipient */
1280         if (!is_bcast(ethhdr->h_dest))
1281                 return NET_RX_DROP;
1282
1283         /* packet with broadcast sender address */
1284         if (is_bcast(ethhdr->h_source))
1285                 return NET_RX_DROP;
1286
1287         /* ignore broadcasts sent by myself */
1288         if (is_my_mac(ethhdr->h_source))
1289                 return NET_RX_DROP;
1290
1291         bcast_packet = (struct bcast_packet *)skb->data;
1292
1293         /* ignore broadcasts originated by myself */
1294         if (is_my_mac(bcast_packet->orig))
1295                 return NET_RX_DROP;
1296
1297         if (bcast_packet->ttl < 2)
1298                 return NET_RX_DROP;
1299
1300         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
1301         orig_node = ((struct orig_node *)
1302                      hash_find(bat_priv->orig_hash, bcast_packet->orig));
1303
1304         if (orig_node == NULL) {
1305                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1306                 return NET_RX_DROP;
1307         }
1308
1309         /* check whether the packet is a duplicate */
1310         if (get_bit_status(orig_node->bcast_bits,
1311                            orig_node->last_bcast_seqno,
1312                            ntohl(bcast_packet->seqno))) {
1313                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1314                 return NET_RX_DROP;
1315         }
1316
1317         seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1318
1319         /* check whether the packet is old and the host just restarted. */
1320         if (window_protected(bat_priv, seq_diff,
1321                              &orig_node->bcast_seqno_reset)) {
1322                 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1323                 return NET_RX_DROP;
1324         }
1325
1326         /* mark broadcast in flood history, update window position
1327          * if required. */
1328         if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1329                 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1330
1331         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1332         /* rebroadcast packet */
1333         add_bcast_packet_to_list(bat_priv, skb);
1334
1335         /* broadcast for me */
1336         interface_rx(recv_if->soft_iface, skb, hdr_size);
1337
1338         return NET_RX_SUCCESS;
1339 }
1340
1341 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1342 {
1343         struct vis_packet *vis_packet;
1344         struct ethhdr *ethhdr;
1345         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1346         int hdr_size = sizeof(struct vis_packet);
1347
1348         /* keep skb linear */
1349         if (skb_linearize(skb) < 0)
1350                 return NET_RX_DROP;
1351
1352         if (unlikely(!pskb_may_pull(skb, hdr_size)))
1353                 return NET_RX_DROP;
1354
1355         vis_packet = (struct vis_packet *)skb->data;
1356         ethhdr = (struct ethhdr *)skb_mac_header(skb);
1357
1358         /* not for me */
1359         if (!is_my_mac(ethhdr->h_dest))
1360                 return NET_RX_DROP;
1361
1362         /* ignore own packets */
1363         if (is_my_mac(vis_packet->vis_orig))
1364                 return NET_RX_DROP;
1365
1366         if (is_my_mac(vis_packet->sender_orig))
1367                 return NET_RX_DROP;
1368
1369         switch (vis_packet->vis_type) {
1370         case VIS_TYPE_SERVER_SYNC:
1371                 receive_server_sync_packet(bat_priv, vis_packet,
1372                                            skb_headlen(skb));
1373                 break;
1374
1375         case VIS_TYPE_CLIENT_UPDATE:
1376                 receive_client_update_packet(bat_priv, vis_packet,
1377                                              skb_headlen(skb));
1378                 break;
1379
1380         default:        /* ignore unknown packet */
1381                 break;
1382         }
1383
1384         /* We take a copy of the data in the packet, so we should
1385            always free the skbuf. */
1386         return NET_RX_DROP;
1387 }