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