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