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