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