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