Staging: batman-adv: layer2 unicast packet fragmentation
[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 static DECLARE_WAIT_QUEUE_HEAD(thread_wait);
38
39 void slide_own_bcast_window(struct batman_if *batman_if)
40 {
41         HASHIT(hashit);
42         struct orig_node *orig_node;
43         TYPE_OF_WORD *word;
44         unsigned long flags;
45
46         spin_lock_irqsave(&orig_hash_lock, flags);
47
48         while (hash_iterate(orig_hash, &hashit)) {
49                 orig_node = hashit.bucket->data;
50                 word = &(orig_node->bcast_own[batman_if->if_num * NUM_WORDS]);
51
52                 bit_get_packet(word, 1, 0);
53                 orig_node->bcast_own_sum[batman_if->if_num] =
54                         bit_packet_count(word);
55         }
56
57         spin_unlock_irqrestore(&orig_hash_lock, flags);
58 }
59
60 static void update_HNA(struct orig_node *orig_node,
61                        unsigned char *hna_buff, int hna_buff_len)
62 {
63         if ((hna_buff_len != orig_node->hna_buff_len) ||
64             ((hna_buff_len > 0) &&
65              (orig_node->hna_buff_len > 0) &&
66              (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
67
68                 if (orig_node->hna_buff_len > 0)
69                         hna_global_del_orig(orig_node,
70                                             "originator changed hna");
71
72                 if ((hna_buff_len > 0) && (hna_buff != NULL))
73                         hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
74         }
75 }
76
77 static void update_route(struct orig_node *orig_node,
78                          struct neigh_node *neigh_node,
79                          unsigned char *hna_buff, int hna_buff_len)
80 {
81         /* FIXME: each orig_node->batman_if will be attached to a softif */
82         struct bat_priv *bat_priv = netdev_priv(soft_device);
83
84         /* route deleted */
85         if ((orig_node->router != NULL) && (neigh_node == NULL)) {
86
87                 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
88                         orig_node->orig);
89                 hna_global_del_orig(orig_node, "originator timed out");
90
91                 /* route added */
92         } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
93
94                 bat_dbg(DBG_ROUTES, bat_priv,
95                         "Adding route towards: %pM (via %pM)\n",
96                         orig_node->orig, neigh_node->addr);
97                 hna_global_add_orig(orig_node, 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 orig_node *orig_node,
113                           struct neigh_node *neigh_node,
114                           unsigned char *hna_buff, int hna_buff_len)
115 {
116
117         if (orig_node == NULL)
118                 return;
119
120         if (orig_node->router != neigh_node)
121                 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
122         /* may be just HNA changed */
123         else
124                 update_HNA(orig_node, hna_buff, hna_buff_len);
125 }
126
127 static int is_bidirectional_neigh(struct orig_node *orig_node,
128                                 struct orig_node *orig_neigh_node,
129                                 struct batman_packet *batman_packet,
130                                 struct batman_if *if_incoming)
131 {
132         /* FIXME: each orig_node->batman_if will be attached to a softif */
133         struct bat_priv *bat_priv = netdev_priv(soft_device);
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 orig_node *orig_node, struct ethhdr *ethhdr,
238                         struct batman_packet *batman_packet,
239                         struct batman_if *if_incoming,
240                         unsigned char *hna_buff, int hna_buff_len,
241                         char is_duplicate)
242 {
243         /* FIXME: get bat_priv */
244         struct bat_priv *bat_priv = netdev_priv(soft_device);
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(ethhdr->h_source);
271                 if (!orig_tmp)
272                         return;
273
274                 neigh_node = create_neighbor(orig_node,
275                                              orig_tmp,
276                                              ethhdr->h_source, if_incoming);
277                 if (!neigh_node)
278                         return;
279         } else
280                 bat_dbg(DBG_BATMAN, bat_priv,
281                         "Updating existing last-hop neighbor of originator\n");
282
283         orig_node->flags = batman_packet->flags;
284         neigh_node->last_valid = jiffies;
285
286         ring_buffer_set(neigh_node->tq_recv,
287                         &neigh_node->tq_index,
288                         batman_packet->tq);
289         neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
290
291         if (!is_duplicate) {
292                 orig_node->last_ttl = batman_packet->ttl;
293                 neigh_node->last_ttl = batman_packet->ttl;
294         }
295
296         tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
297                             batman_packet->num_hna * ETH_ALEN : hna_buff_len);
298
299         /* if this neighbor already is our next hop there is nothing
300          * to change */
301         if (orig_node->router == neigh_node)
302                 goto update_hna;
303
304         /* if this neighbor does not offer a better TQ we won't consider it */
305         if ((orig_node->router) &&
306             (orig_node->router->tq_avg > neigh_node->tq_avg))
307                 goto update_hna;
308
309         /* if the TQ is the same and the link not more symetric we
310          * won't consider it either */
311         if ((orig_node->router) &&
312              ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
313              (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
314               >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
315                 goto update_hna;
316
317         update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
318         return;
319
320 update_hna:
321         update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
322 }
323
324 /* checks whether the host restarted and is in the protection time.
325  * returns:
326  *  0 if the packet is to be accepted
327  *  1 if the packet is to be ignored.
328  */
329 static int window_protected(int32_t seq_num_diff,
330                                 unsigned long *last_reset)
331 {
332         /* FIXME: each orig_node->batman_if will be attached to a softif */
333         struct bat_priv *bat_priv = netdev_priv(soft_device);
334
335         if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
336                 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
337                 if (time_after(jiffies, *last_reset +
338                         msecs_to_jiffies(RESET_PROTECTION_MS))) {
339
340                         *last_reset = jiffies;
341                         bat_dbg(DBG_BATMAN, bat_priv,
342                                 "old packet received, start protection\n");
343
344                         return 0;
345                 } else
346                         return 1;
347         }
348         return 0;
349 }
350
351 /* processes a batman packet for all interfaces, adjusts the sequence number and
352  * finds out whether it is a duplicate.
353  * returns:
354  *   1 the packet is a duplicate
355  *   0 the packet has not yet been received
356  *  -1 the packet is old and has been received while the seqno window
357  *     was protected. Caller should drop it.
358  */
359 static char count_real_packets(struct ethhdr *ethhdr,
360                                struct batman_packet *batman_packet,
361                                struct batman_if *if_incoming)
362 {
363         /* FIXME: each orig_node->batman_if will be attached to a softif */
364         struct bat_priv *bat_priv = netdev_priv(soft_device);
365         struct orig_node *orig_node;
366         struct neigh_node *tmp_neigh_node;
367         char is_duplicate = 0;
368         int32_t seq_diff;
369         int need_update = 0;
370         int set_mark;
371
372         orig_node = get_orig_node(batman_packet->orig);
373         if (orig_node == NULL)
374                 return 0;
375
376         seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
377
378         /* signalize caller that the packet is to be dropped. */
379         if (window_protected(seq_diff, &orig_node->batman_seqno_reset))
380                 return -1;
381
382         list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
383
384                 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
385                                                orig_node->last_real_seqno,
386                                                batman_packet->seqno);
387
388                 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
389                     (tmp_neigh_node->if_incoming == if_incoming))
390                         set_mark = 1;
391                 else
392                         set_mark = 0;
393
394                 /* if the window moved, set the update flag. */
395                 need_update |= bit_get_packet(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         /* FIXME: each orig_node->batman_if will be attached to a softif */
525         struct bat_priv *bat_priv = netdev_priv(soft_device);
526         struct batman_if *batman_if;
527         struct orig_node *orig_neigh_node, *orig_node;
528         char has_directlink_flag;
529         char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
530         char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
531         char is_duplicate;
532         uint32_t if_incoming_seqno;
533
534         /* Silently drop when the batman packet is actually not a
535          * correct packet.
536          *
537          * This might happen if a packet is padded (e.g. Ethernet has a
538          * minimum frame length of 64 byte) and the aggregation interprets
539          * it as an additional length.
540          *
541          * TODO: A more sane solution would be to have a bit in the
542          * batman_packet to detect whether the packet is the last
543          * packet in an aggregation.  Here we expect that the padding
544          * is always zero (or not 0x01)
545          */
546         if (batman_packet->packet_type != BAT_PACKET)
547                 return;
548
549         /* could be changed by schedule_own_packet() */
550         if_incoming_seqno = atomic_read(&if_incoming->seqno);
551
552         has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
553
554         is_single_hop_neigh = (compare_orig(ethhdr->h_source,
555                                             batman_packet->orig) ? 1 : 0);
556
557         bat_dbg(DBG_BATMAN, bat_priv,
558                 "Received BATMAN packet via NB: %pM, IF: %s [%s] "
559                 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
560                 "TTL %d, V %d, IDF %d)\n",
561                 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
562                 batman_packet->orig, batman_packet->prev_sender,
563                 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
564                 batman_packet->version, has_directlink_flag);
565
566         list_for_each_entry_rcu(batman_if, &if_list, list) {
567                 if (batman_if->if_status != IF_ACTIVE)
568                         continue;
569
570                 if (compare_orig(ethhdr->h_source,
571                                  batman_if->net_dev->dev_addr))
572                         is_my_addr = 1;
573
574                 if (compare_orig(batman_packet->orig,
575                                  batman_if->net_dev->dev_addr))
576                         is_my_orig = 1;
577
578                 if (compare_orig(batman_packet->prev_sender,
579                                  batman_if->net_dev->dev_addr))
580                         is_my_oldorig = 1;
581
582                 if (compare_orig(ethhdr->h_source, broadcast_addr))
583                         is_broadcast = 1;
584         }
585
586         if (batman_packet->version != COMPAT_VERSION) {
587                 bat_dbg(DBG_BATMAN, bat_priv,
588                         "Drop packet: incompatible batman version (%i)\n",
589                         batman_packet->version);
590                 return;
591         }
592
593         if (is_my_addr) {
594                 bat_dbg(DBG_BATMAN, bat_priv,
595                         "Drop packet: received my own broadcast (sender: %pM"
596                         ")\n",
597                         ethhdr->h_source);
598                 return;
599         }
600
601         if (is_broadcast) {
602                 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
603                 "ignoring all packets with broadcast source addr (sender: %pM"
604                 ")\n", ethhdr->h_source);
605                 return;
606         }
607
608         if (is_my_orig) {
609                 TYPE_OF_WORD *word;
610                 int offset;
611
612                 orig_neigh_node = get_orig_node(ethhdr->h_source);
613
614                 if (!orig_neigh_node)
615                         return;
616
617                 /* neighbor has to indicate direct link and it has to
618                  * come via the corresponding interface */
619                 /* if received seqno equals last send seqno save new
620                  * seqno for bidirectional check */
621                 if (has_directlink_flag &&
622                     compare_orig(if_incoming->net_dev->dev_addr,
623                                  batman_packet->orig) &&
624                     (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
625                         offset = if_incoming->if_num * NUM_WORDS;
626                         word = &(orig_neigh_node->bcast_own[offset]);
627                         bit_mark(word, 0);
628                         orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
629                                 bit_packet_count(word);
630                 }
631
632                 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
633                         "originator packet from myself (via neighbor)\n");
634                 return;
635         }
636
637         if (is_my_oldorig) {
638                 bat_dbg(DBG_BATMAN, bat_priv,
639                         "Drop packet: ignoring all rebroadcast echos (sender: "
640                         "%pM)\n", ethhdr->h_source);
641                 return;
642         }
643
644         orig_node = get_orig_node(batman_packet->orig);
645         if (orig_node == NULL)
646                 return;
647
648         is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
649
650         if (is_duplicate == -1) {
651                 bat_dbg(DBG_BATMAN, bat_priv,
652                         "Drop packet: packet within seqno protection time "
653                         "(sender: %pM)\n", ethhdr->h_source);
654                 return;
655         }
656
657         if (batman_packet->tq == 0) {
658                 bat_dbg(DBG_BATMAN, bat_priv,
659                         "Drop packet: originator packet with tq equal 0\n");
660                 return;
661         }
662
663         /* avoid temporary routing loops */
664         if ((orig_node->router) &&
665             (orig_node->router->orig_node->router) &&
666             (compare_orig(orig_node->router->addr,
667                           batman_packet->prev_sender)) &&
668             !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
669             (compare_orig(orig_node->router->addr,
670                           orig_node->router->orig_node->router->addr))) {
671                 bat_dbg(DBG_BATMAN, bat_priv,
672                         "Drop packet: ignoring all rebroadcast packets that "
673                         "may make me loop (sender: %pM)\n", ethhdr->h_source);
674                 return;
675         }
676
677         /* if sender is a direct neighbor the sender mac equals
678          * originator mac */
679         orig_neigh_node = (is_single_hop_neigh ?
680                            orig_node : get_orig_node(ethhdr->h_source));
681         if (orig_neigh_node == NULL)
682                 return;
683
684         /* drop packet if sender is not a direct neighbor and if we
685          * don't route towards it */
686         if (!is_single_hop_neigh &&
687             (orig_neigh_node->router == NULL)) {
688                 bat_dbg(DBG_BATMAN, bat_priv,
689                         "Drop packet: OGM via unknown neighbor!\n");
690                 return;
691         }
692
693         is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
694                                                 batman_packet, if_incoming);
695
696         /* update ranking if it is not a duplicate or has the same
697          * seqno and similar ttl as the non-duplicate */
698         if (is_bidirectional &&
699             (!is_duplicate ||
700              ((orig_node->last_real_seqno == batman_packet->seqno) &&
701               (orig_node->last_ttl - 3 <= batman_packet->ttl))))
702                 update_orig(orig_node, ethhdr, batman_packet,
703                             if_incoming, hna_buff, hna_buff_len, is_duplicate);
704
705         mark_bonding_address(bat_priv, orig_node,
706                              orig_neigh_node, batman_packet);
707         update_bonding_candidates(bat_priv, orig_node);
708
709         /* is single hop (direct) neighbor */
710         if (is_single_hop_neigh) {
711
712                 /* mark direct link on incoming interface */
713                 schedule_forward_packet(orig_node, ethhdr, batman_packet,
714                                         1, hna_buff_len, if_incoming);
715
716                 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
717                         "rebroadcast neighbor packet with direct link flag\n");
718                 return;
719         }
720
721         /* multihop originator */
722         if (!is_bidirectional) {
723                 bat_dbg(DBG_BATMAN, bat_priv,
724                         "Drop packet: not received via bidirectional link\n");
725                 return;
726         }
727
728         if (is_duplicate) {
729                 bat_dbg(DBG_BATMAN, bat_priv,
730                         "Drop packet: duplicate packet received\n");
731                 return;
732         }
733
734         bat_dbg(DBG_BATMAN, bat_priv,
735                 "Forwarding packet: rebroadcast originator packet\n");
736         schedule_forward_packet(orig_node, ethhdr, batman_packet,
737                                 0, hna_buff_len, if_incoming);
738 }
739
740 int recv_bat_packet(struct sk_buff *skb,
741                                 struct batman_if *batman_if)
742 {
743         struct ethhdr *ethhdr;
744         unsigned long flags;
745         struct sk_buff *skb_old;
746
747         /* drop packet if it has not necessary minimum size */
748         if (skb_headlen(skb) < sizeof(struct batman_packet))
749                 return NET_RX_DROP;
750
751         ethhdr = (struct ethhdr *)skb_mac_header(skb);
752
753         /* packet with broadcast indication but unicast recipient */
754         if (!is_bcast(ethhdr->h_dest))
755                 return NET_RX_DROP;
756
757         /* packet with broadcast sender address */
758         if (is_bcast(ethhdr->h_source))
759                 return NET_RX_DROP;
760
761         /* TODO: we use headlen instead of "length", because
762          * only this data is paged in. */
763
764         /* create a copy of the skb, if needed, to modify it. */
765         if (!skb_clone_writable(skb, skb_headlen(skb))) {
766                 skb_old = skb;
767                 skb = skb_copy(skb, GFP_ATOMIC);
768                 if (!skb)
769                         return NET_RX_DROP;
770                 ethhdr = (struct ethhdr *)skb_mac_header(skb);
771                 kfree_skb(skb_old);
772         }
773
774         spin_lock_irqsave(&orig_hash_lock, flags);
775         receive_aggr_bat_packet(ethhdr,
776                                 skb->data,
777                                 skb_headlen(skb),
778                                 batman_if);
779         spin_unlock_irqrestore(&orig_hash_lock, flags);
780
781         kfree_skb(skb);
782         return NET_RX_SUCCESS;
783 }
784
785 static int recv_my_icmp_packet(struct sk_buff *skb, size_t icmp_len)
786 {
787         /* FIXME: each batman_if will be attached to a softif */
788         struct bat_priv *bat_priv = netdev_priv(soft_device);
789         struct orig_node *orig_node;
790         struct icmp_packet_rr *icmp_packet;
791         struct ethhdr *ethhdr;
792         struct sk_buff *skb_old;
793         struct batman_if *batman_if;
794         int ret;
795         unsigned long flags;
796         uint8_t dstaddr[ETH_ALEN];
797
798         icmp_packet = (struct icmp_packet_rr *)skb->data;
799         ethhdr = (struct ethhdr *)skb_mac_header(skb);
800
801         /* add data to device queue */
802         if (icmp_packet->msg_type != ECHO_REQUEST) {
803                 bat_socket_receive_packet(icmp_packet, icmp_len);
804                 return NET_RX_DROP;
805         }
806
807         if (!bat_priv->primary_if)
808                 return NET_RX_DROP;
809
810         /* answer echo request (ping) */
811         /* get routing information */
812         spin_lock_irqsave(&orig_hash_lock, flags);
813         orig_node = ((struct orig_node *)hash_find(orig_hash,
814                                                    icmp_packet->orig));
815         ret = NET_RX_DROP;
816
817         if ((orig_node != NULL) &&
818             (orig_node->router != NULL)) {
819
820                 /* don't lock while sending the packets ... we therefore
821                  * copy the required data before sending */
822                 batman_if = orig_node->router->if_incoming;
823                 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
824                 spin_unlock_irqrestore(&orig_hash_lock, flags);
825
826                 /* create a copy of the skb, if needed, to modify it. */
827                 skb_old = NULL;
828                 if (!skb_clone_writable(skb, icmp_len)) {
829                         skb_old = skb;
830                         skb = skb_copy(skb, GFP_ATOMIC);
831                         if (!skb)
832                                 return NET_RX_DROP;
833                         icmp_packet = (struct icmp_packet_rr *)skb->data;
834                         ethhdr = (struct ethhdr *)skb_mac_header(skb);
835                         kfree_skb(skb_old);
836                 }
837
838                 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
839                 memcpy(icmp_packet->orig,
840                        bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
841                 icmp_packet->msg_type = ECHO_REPLY;
842                 icmp_packet->ttl = TTL;
843
844                 send_skb_packet(skb, batman_if, dstaddr);
845                 ret = NET_RX_SUCCESS;
846
847         } else
848                 spin_unlock_irqrestore(&orig_hash_lock, flags);
849
850         return ret;
851 }
852
853 static int recv_icmp_ttl_exceeded(struct sk_buff *skb, size_t icmp_len)
854 {
855         /* FIXME: each batman_if will be attached to a softif */
856         struct bat_priv *bat_priv = netdev_priv(soft_device);
857         struct orig_node *orig_node;
858         struct icmp_packet *icmp_packet;
859         struct ethhdr *ethhdr;
860         struct sk_buff *skb_old;
861         struct batman_if *batman_if;
862         int ret;
863         unsigned long flags;
864         uint8_t dstaddr[ETH_ALEN];
865
866         icmp_packet = (struct icmp_packet *)skb->data;
867         ethhdr = (struct ethhdr *)skb_mac_header(skb);
868
869         /* send TTL exceeded if packet is an echo request (traceroute) */
870         if (icmp_packet->msg_type != ECHO_REQUEST) {
871                 pr_warning("Warning - can't forward icmp packet from %pM to "
872                            "%pM: ttl exceeded\n", icmp_packet->orig,
873                            icmp_packet->dst);
874                 return NET_RX_DROP;
875         }
876
877         if (!bat_priv->primary_if)
878                 return NET_RX_DROP;
879
880         /* get routing information */
881         spin_lock_irqsave(&orig_hash_lock, flags);
882         orig_node = ((struct orig_node *)
883                      hash_find(orig_hash, icmp_packet->orig));
884         ret = NET_RX_DROP;
885
886         if ((orig_node != NULL) &&
887             (orig_node->router != NULL)) {
888
889                 /* don't lock while sending the packets ... we therefore
890                  * copy the required data before sending */
891                 batman_if = orig_node->router->if_incoming;
892                 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
893                 spin_unlock_irqrestore(&orig_hash_lock, flags);
894
895                 /* create a copy of the skb, if needed, to modify it. */
896                 if (!skb_clone_writable(skb, icmp_len)) {
897                         skb_old = skb;
898                         skb = skb_copy(skb, GFP_ATOMIC);
899                         if (!skb)
900                                 return NET_RX_DROP;
901                         icmp_packet = (struct icmp_packet *) skb->data;
902                         ethhdr = (struct ethhdr *)skb_mac_header(skb);
903                         kfree_skb(skb_old);
904                 }
905
906                 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
907                 memcpy(icmp_packet->orig,
908                        bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
909                 icmp_packet->msg_type = TTL_EXCEEDED;
910                 icmp_packet->ttl = TTL;
911
912                 send_skb_packet(skb, batman_if, dstaddr);
913                 ret = NET_RX_SUCCESS;
914
915         } else
916                 spin_unlock_irqrestore(&orig_hash_lock, flags);
917
918         return ret;
919 }
920
921
922 int recv_icmp_packet(struct sk_buff *skb)
923 {
924         struct icmp_packet_rr *icmp_packet;
925         struct ethhdr *ethhdr;
926         struct orig_node *orig_node;
927         struct sk_buff *skb_old;
928         struct batman_if *batman_if;
929         int hdr_size = sizeof(struct icmp_packet);
930         int ret;
931         unsigned long flags;
932         uint8_t dstaddr[ETH_ALEN];
933
934         /**
935          * we truncate all incoming icmp packets if they don't match our size
936          */
937         if (skb_headlen(skb) >= sizeof(struct icmp_packet_rr))
938                 hdr_size = sizeof(struct icmp_packet_rr);
939
940         /* drop packet if it has not necessary minimum size */
941         if (skb_headlen(skb) < hdr_size)
942                 return NET_RX_DROP;
943
944         ethhdr = (struct ethhdr *)skb_mac_header(skb);
945
946         /* packet with unicast indication but broadcast recipient */
947         if (is_bcast(ethhdr->h_dest))
948                 return NET_RX_DROP;
949
950         /* packet with broadcast sender address */
951         if (is_bcast(ethhdr->h_source))
952                 return NET_RX_DROP;
953
954         /* not for me */
955         if (!is_my_mac(ethhdr->h_dest))
956                 return NET_RX_DROP;
957
958         icmp_packet = (struct icmp_packet_rr *)skb->data;
959
960         /* add record route information if not full */
961         if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
962             (icmp_packet->rr_cur < BAT_RR_LEN)) {
963                 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
964                         ethhdr->h_dest, ETH_ALEN);
965                 icmp_packet->rr_cur++;
966         }
967
968         /* packet for me */
969         if (is_my_mac(icmp_packet->dst))
970                 return recv_my_icmp_packet(skb, hdr_size);
971
972         /* TTL exceeded */
973         if (icmp_packet->ttl < 2)
974                 return recv_icmp_ttl_exceeded(skb, hdr_size);
975
976         ret = NET_RX_DROP;
977
978         /* get routing information */
979         spin_lock_irqsave(&orig_hash_lock, flags);
980         orig_node = ((struct orig_node *)
981                      hash_find(orig_hash, icmp_packet->dst));
982
983         if ((orig_node != NULL) &&
984             (orig_node->router != NULL)) {
985
986                 /* don't lock while sending the packets ... we therefore
987                  * copy the required data before sending */
988                 batman_if = orig_node->router->if_incoming;
989                 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
990                 spin_unlock_irqrestore(&orig_hash_lock, flags);
991
992                 /* create a copy of the skb, if needed, to modify it. */
993                 if (!skb_clone_writable(skb, hdr_size)) {
994                         skb_old = skb;
995                         skb = skb_copy(skb, GFP_ATOMIC);
996                         if (!skb)
997                                 return NET_RX_DROP;
998                         icmp_packet = (struct icmp_packet_rr *)skb->data;
999                         ethhdr = (struct ethhdr *)skb_mac_header(skb);
1000                         kfree_skb(skb_old);
1001                 }
1002
1003                 /* decrement ttl */
1004                 icmp_packet->ttl--;
1005
1006                 /* route it */
1007                 send_skb_packet(skb, batman_if, dstaddr);
1008                 ret = NET_RX_SUCCESS;
1009
1010         } else
1011                 spin_unlock_irqrestore(&orig_hash_lock, flags);
1012
1013         return ret;
1014 }
1015
1016 /* find a suitable router for this originator, and use
1017  * bonding if possible. */
1018 struct neigh_node *find_router(struct orig_node *orig_node,
1019                 struct batman_if *recv_if)
1020 {
1021         /* FIXME: each orig_node->batman_if will be attached to a softif */
1022         struct bat_priv *bat_priv = netdev_priv(soft_device);
1023         struct orig_node *primary_orig_node;
1024         struct orig_node *router_orig;
1025         struct neigh_node *router, *first_candidate, *best_router;
1026         static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1027         int bonding_enabled;
1028
1029         if (!orig_node)
1030                 return NULL;
1031
1032         if (!orig_node->router)
1033                 return NULL;
1034
1035         /* without bonding, the first node should
1036          * always choose the default router. */
1037
1038         bonding_enabled = atomic_read(&bat_priv->bonding_enabled);
1039         if (!bonding_enabled && (recv_if == NULL))
1040                         return orig_node->router;
1041
1042         router_orig = orig_node->router->orig_node;
1043
1044         /* if we have something in the primary_addr, we can search
1045          * for a potential bonding candidate. */
1046         if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1047                 return orig_node->router;
1048
1049         /* find the orig_node which has the primary interface. might
1050          * even be the same as our router_orig in many cases */
1051
1052         if (memcmp(router_orig->primary_addr,
1053                                 router_orig->orig, ETH_ALEN) == 0) {
1054                 primary_orig_node = router_orig;
1055         } else {
1056                 primary_orig_node = hash_find(orig_hash,
1057                                                 router_orig->primary_addr);
1058                 if (!primary_orig_node)
1059                         return orig_node->router;
1060         }
1061
1062         /* with less than 2 candidates, we can't do any
1063          * bonding and prefer the original router. */
1064
1065         if (primary_orig_node->bond.candidates < 2)
1066                 return orig_node->router;
1067
1068
1069         /* all nodes between should choose a candidate which
1070          * is is not on the interface where the packet came
1071          * in. */
1072         first_candidate = primary_orig_node->bond.selected;
1073         router = first_candidate;
1074
1075         if (bonding_enabled) {
1076                 /* in the bonding case, send the packets in a round
1077                  * robin fashion over the remaining interfaces. */
1078                 do {
1079                         /* recv_if == NULL on the first node. */
1080                         if (router->if_incoming != recv_if)
1081                                 break;
1082
1083                         router = router->next_bond_candidate;
1084                 } while (router != first_candidate);
1085
1086                 primary_orig_node->bond.selected = router->next_bond_candidate;
1087
1088         } else {
1089                 /* if bonding is disabled, use the best of the
1090                  * remaining candidates which are not using
1091                  * this interface. */
1092                 best_router = first_candidate;
1093
1094                 do {
1095                         /* recv_if == NULL on the first node. */
1096                         if ((router->if_incoming != recv_if) &&
1097                                 (router->tq_avg > best_router->tq_avg))
1098                                         best_router = router;
1099
1100                         router = router->next_bond_candidate;
1101                 } while (router != first_candidate);
1102
1103                 router = best_router;
1104         }
1105
1106         return router;
1107 }
1108
1109 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1110 {
1111         struct ethhdr *ethhdr;
1112
1113         /* drop packet if it has not necessary minimum size */
1114         if (skb_headlen(skb) < hdr_size)
1115                 return -1;
1116
1117         ethhdr = (struct ethhdr *) skb_mac_header(skb);
1118
1119         /* packet with unicast indication but broadcast recipient */
1120         if (is_bcast(ethhdr->h_dest))
1121                 return -1;
1122
1123         /* packet with broadcast sender address */
1124         if (is_bcast(ethhdr->h_source))
1125                 return -1;
1126
1127         /* not for me */
1128         if (!is_my_mac(ethhdr->h_dest))
1129                 return -1;
1130
1131         return 0;
1132 }
1133
1134 static int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1135                 int hdr_size)
1136 {
1137         struct orig_node *orig_node;
1138         struct neigh_node *router;
1139         struct batman_if *batman_if;
1140         struct sk_buff *skb_old;
1141         uint8_t dstaddr[ETH_ALEN];
1142         unsigned long flags;
1143         struct unicast_packet *unicast_packet =
1144                 (struct unicast_packet *) skb->data;
1145         struct ethhdr *ethhdr = (struct ethhdr *) skb_mac_header(skb);
1146
1147         /* TTL exceeded */
1148         if (unicast_packet->ttl < 2) {
1149                 pr_warning("Warning - can't forward unicast packet from %pM to "
1150                            "%pM: ttl exceeded\n", ethhdr->h_source,
1151                            unicast_packet->dest);
1152                 return NET_RX_DROP;
1153         }
1154
1155         /* get routing information */
1156         spin_lock_irqsave(&orig_hash_lock, flags);
1157         orig_node = ((struct orig_node *)
1158                      hash_find(orig_hash, unicast_packet->dest));
1159
1160         router = find_router(orig_node, recv_if);
1161
1162         if (!router) {
1163                 spin_unlock_irqrestore(&orig_hash_lock, flags);
1164                 return NET_RX_DROP;
1165         }
1166
1167         /* don't lock while sending the packets ... we therefore
1168          * copy the required data before sending */
1169
1170         batman_if = router->if_incoming;
1171         memcpy(dstaddr, router->addr, ETH_ALEN);
1172
1173         spin_unlock_irqrestore(&orig_hash_lock, flags);
1174
1175         /* create a copy of the skb, if needed, to modify it. */
1176         if (!skb_clone_writable(skb, hdr_size)) {
1177                 skb_old = skb;
1178                 skb = skb_copy(skb, GFP_ATOMIC);
1179                 if (!skb)
1180                         return NET_RX_DROP;
1181                 unicast_packet = (struct unicast_packet *) skb->data;
1182                 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1183                 kfree_skb(skb_old);
1184         }
1185
1186         /* decrement ttl */
1187         unicast_packet->ttl--;
1188
1189         /* route it */
1190         send_skb_packet(skb, batman_if, dstaddr);
1191
1192         return NET_RX_SUCCESS;
1193 }
1194
1195 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1196 {
1197         struct unicast_packet *unicast_packet;
1198         int hdr_size = sizeof(struct unicast_packet);
1199
1200         if (check_unicast_packet(skb, hdr_size) < 0)
1201                 return NET_RX_DROP;
1202
1203         unicast_packet = (struct unicast_packet *) skb->data;
1204
1205         /* packet for me */
1206         if (is_my_mac(unicast_packet->dest)) {
1207                 interface_rx(skb, hdr_size);
1208                 return NET_RX_SUCCESS;
1209         }
1210
1211         return route_unicast_packet(skb, recv_if, hdr_size);
1212 }
1213
1214 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1215 {
1216         struct unicast_frag_packet *unicast_packet;
1217         struct orig_node *orig_node;
1218         struct frag_packet_list_entry *tmp_frag_entry;
1219         int hdr_size = sizeof(struct unicast_frag_packet);
1220         unsigned long flags;
1221
1222         if (check_unicast_packet(skb, hdr_size) < 0)
1223                 return NET_RX_DROP;
1224
1225         unicast_packet = (struct unicast_frag_packet *) skb->data;
1226
1227         /* packet for me */
1228         if (is_my_mac(unicast_packet->dest)) {
1229
1230                 spin_lock_irqsave(&orig_hash_lock, flags);
1231                 orig_node = ((struct orig_node *)
1232                         hash_find(orig_hash, unicast_packet->orig));
1233
1234                 if (!orig_node) {
1235                         pr_warning("couldn't find orig node for "
1236                                 "fragmentation\n");
1237                         spin_unlock_irqrestore(&orig_hash_lock, flags);
1238                         return NET_RX_DROP;
1239                 }
1240
1241                 orig_node->last_frag_packet = jiffies;
1242
1243                 if (list_empty(&orig_node->frag_list))
1244                         create_frag_buffer(&orig_node->frag_list);
1245
1246                 tmp_frag_entry =
1247                         search_frag_packet(&orig_node->frag_list,
1248                         unicast_packet);
1249
1250                 if (!tmp_frag_entry) {
1251                         create_frag_entry(&orig_node->frag_list, skb);
1252                         spin_unlock_irqrestore(&orig_hash_lock, flags);
1253                         return NET_RX_SUCCESS;
1254                 }
1255
1256                 skb = merge_frag_packet(&orig_node->frag_list,
1257                         tmp_frag_entry, skb);
1258                 spin_unlock_irqrestore(&orig_hash_lock, flags);
1259                 if (!skb)
1260                         return NET_RX_DROP;
1261
1262                 interface_rx(skb, hdr_size);
1263                 return NET_RX_SUCCESS;
1264         }
1265
1266         return route_unicast_packet(skb, recv_if, hdr_size);
1267 }
1268
1269 int recv_bcast_packet(struct sk_buff *skb)
1270 {
1271         struct orig_node *orig_node;
1272         struct bcast_packet *bcast_packet;
1273         struct ethhdr *ethhdr;
1274         int hdr_size = sizeof(struct bcast_packet);
1275         int32_t seq_diff;
1276         unsigned long flags;
1277
1278         /* drop packet if it has not necessary minimum size */
1279         if (skb_headlen(skb) < hdr_size)
1280                 return NET_RX_DROP;
1281
1282         ethhdr = (struct ethhdr *)skb_mac_header(skb);
1283
1284         /* packet with broadcast indication but unicast recipient */
1285         if (!is_bcast(ethhdr->h_dest))
1286                 return NET_RX_DROP;
1287
1288         /* packet with broadcast sender address */
1289         if (is_bcast(ethhdr->h_source))
1290                 return NET_RX_DROP;
1291
1292         /* ignore broadcasts sent by myself */
1293         if (is_my_mac(ethhdr->h_source))
1294                 return NET_RX_DROP;
1295
1296         bcast_packet = (struct bcast_packet *)skb->data;
1297
1298         /* ignore broadcasts originated by myself */
1299         if (is_my_mac(bcast_packet->orig))
1300                 return NET_RX_DROP;
1301
1302         if (bcast_packet->ttl < 2)
1303                 return NET_RX_DROP;
1304
1305         spin_lock_irqsave(&orig_hash_lock, flags);
1306         orig_node = ((struct orig_node *)
1307                      hash_find(orig_hash, bcast_packet->orig));
1308
1309         if (orig_node == NULL) {
1310                 spin_unlock_irqrestore(&orig_hash_lock, flags);
1311                 return NET_RX_DROP;
1312         }
1313
1314         /* check whether the packet is a duplicate */
1315         if (get_bit_status(orig_node->bcast_bits,
1316                            orig_node->last_bcast_seqno,
1317                            ntohl(bcast_packet->seqno))) {
1318                 spin_unlock_irqrestore(&orig_hash_lock, flags);
1319                 return NET_RX_DROP;
1320         }
1321
1322         seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1323
1324         /* check whether the packet is old and the host just restarted. */
1325         if (window_protected(seq_diff, &orig_node->bcast_seqno_reset)) {
1326                 spin_unlock_irqrestore(&orig_hash_lock, flags);
1327                 return NET_RX_DROP;
1328         }
1329
1330         /* mark broadcast in flood history, update window position
1331          * if required. */
1332         if (bit_get_packet(orig_node->bcast_bits, seq_diff, 1))
1333                 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1334
1335         spin_unlock_irqrestore(&orig_hash_lock, flags);
1336         /* rebroadcast packet */
1337         add_bcast_packet_to_list(skb);
1338
1339         /* broadcast for me */
1340         interface_rx(skb, hdr_size);
1341
1342         return NET_RX_SUCCESS;
1343 }
1344
1345 int recv_vis_packet(struct sk_buff *skb)
1346 {
1347         struct vis_packet *vis_packet;
1348         struct ethhdr *ethhdr;
1349         struct bat_priv *bat_priv;
1350         int hdr_size = sizeof(struct vis_packet);
1351
1352         if (skb_headlen(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         /* FIXME: each batman_if will be attached to a softif */
1370         bat_priv = netdev_priv(soft_device);
1371
1372         switch (vis_packet->vis_type) {
1373         case VIS_TYPE_SERVER_SYNC:
1374                 /* TODO: handle fragmented skbs properly */
1375                 receive_server_sync_packet(bat_priv, vis_packet,
1376                                            skb_headlen(skb));
1377                 break;
1378
1379         case VIS_TYPE_CLIENT_UPDATE:
1380                 /* TODO: handle fragmented skbs properly */
1381                 receive_client_update_packet(bat_priv, vis_packet,
1382                                              skb_headlen(skb));
1383                 break;
1384
1385         default:        /* ignore unknown packet */
1386                 break;
1387         }
1388
1389         /* We take a copy of the data in the packet, so we should
1390            always free the skbuf. */
1391         return NET_RX_DROP;
1392 }