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