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