2 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
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"
33 #include "aggregation.h"
34 #include "gateway_common.h"
35 #include "gateway_client.h"
38 void slide_own_bcast_window(struct batman_if *batman_if)
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;
50 spin_lock_bh(&bat_priv->orig_hash_lock);
52 for (i = 0; i < hash->size; i++) {
53 head = &hash->table[i];
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]);
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);
70 spin_unlock_bh(&bat_priv->orig_hash_lock);
73 static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
74 unsigned char *hna_buff, int hna_buff_len)
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))) {
81 if (orig_node->hna_buff_len > 0)
82 hna_global_del_orig(bat_priv, orig_node,
83 "originator changed hna");
85 if ((hna_buff_len > 0) && (hna_buff))
86 hna_global_add_orig(bat_priv, orig_node,
87 hna_buff, hna_buff_len);
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)
96 struct neigh_node *neigh_node_tmp;
99 if ((orig_node->router) && (!neigh_node)) {
101 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
103 hna_global_del_orig(bat_priv, orig_node,
104 "originator timed out");
107 } else if ((!orig_node->router) && (neigh_node)) {
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);
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);
124 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
126 neigh_node_tmp = orig_node->router;
127 orig_node->router = neigh_node;
129 neigh_node_free_ref(neigh_node_tmp);
133 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
134 struct neigh_node *neigh_node, unsigned char *hna_buff,
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 */
146 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
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)
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;
160 if (orig_node == orig_neigh_node) {
162 hlist_for_each_entry_rcu(tmp_neigh_node, node,
163 &orig_node->neigh_list, list) {
165 if (compare_orig(tmp_neigh_node->addr,
166 orig_neigh_node->orig) &&
167 (tmp_neigh_node->if_incoming == if_incoming))
168 neigh_node = tmp_neigh_node;
172 neigh_node = create_neighbor(orig_node,
174 orig_neigh_node->orig,
176 /* create_neighbor failed, return 0 */
180 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
187 neigh_node->last_valid = jiffies;
189 /* find packet count of corresponding one hop neighbor */
191 hlist_for_each_entry_rcu(tmp_neigh_node, node,
192 &orig_neigh_node->neigh_list, list) {
194 if (compare_orig(tmp_neigh_node->addr,
195 orig_neigh_node->orig) &&
196 (tmp_neigh_node->if_incoming == if_incoming))
197 neigh_node = tmp_neigh_node;
201 neigh_node = create_neighbor(orig_neigh_node,
203 orig_neigh_node->orig,
205 /* create_neighbor failed, return 0 */
209 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
217 orig_node->last_valid = jiffies;
219 /* pay attention to not get a value bigger than 100 % */
220 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
221 neigh_node->real_packet_count ?
222 neigh_node->real_packet_count :
223 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
225 /* if we have too few packets (too less data) we set tq_own to zero */
226 /* if we receive too few packets it is not considered bidirectional */
227 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
228 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
229 orig_neigh_node->tq_own = 0;
231 /* neigh_node->real_packet_count is never zero as we
232 * only purge old information when getting new
234 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
235 neigh_node->real_packet_count;
238 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
239 * affect the nearly-symmetric links only a little, but
240 * punishes asymmetric links more. This will give a value
241 * between 0 and TQ_MAX_VALUE
243 orig_neigh_node->tq_asym_penalty =
246 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
247 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
248 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
249 (TQ_LOCAL_WINDOW_SIZE *
250 TQ_LOCAL_WINDOW_SIZE *
251 TQ_LOCAL_WINDOW_SIZE);
253 batman_packet->tq = ((batman_packet->tq *
254 orig_neigh_node->tq_own *
255 orig_neigh_node->tq_asym_penalty) /
256 (TQ_MAX_VALUE * TQ_MAX_VALUE));
258 bat_dbg(DBG_BATMAN, bat_priv,
260 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
261 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
263 orig_node->orig, orig_neigh_node->orig, total_count,
264 neigh_node->real_packet_count, orig_neigh_node->tq_own,
265 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
267 /* if link has the minimum required transmission quality
268 * consider it bidirectional */
269 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
278 neigh_node_free_ref(neigh_node);
282 /* caller must hold the neigh_list_lock */
283 void bonding_candidate_del(struct orig_node *orig_node,
284 struct neigh_node *neigh_node)
286 /* this neighbor is not part of our candidate list */
287 if (list_empty(&neigh_node->bonding_list))
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);
299 static void bonding_candidate_add(struct orig_node *orig_node,
300 struct neigh_node *neigh_node)
302 struct hlist_node *node;
303 struct neigh_node *tmp_neigh_node;
304 uint8_t best_tq, interference_candidate = 0;
306 spin_lock_bh(&orig_node->neigh_list_lock);
308 /* only consider if it has the same primary address ... */
309 if (!compare_orig(orig_node->orig,
310 neigh_node->orig_node->primary_addr))
313 if (!orig_node->router)
316 best_tq = orig_node->router->tq_avg;
318 /* ... and is good enough to be considered */
319 if (neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
323 * check if we have another candidate with the same mac address or
324 * interface. If we do, we won't select this candidate because of
325 * possible interference.
327 hlist_for_each_entry_rcu(tmp_neigh_node, node,
328 &orig_node->neigh_list, list) {
330 if (tmp_neigh_node == neigh_node)
333 /* we only care if the other candidate is even
334 * considered as candidate. */
335 if (list_empty(&tmp_neigh_node->bonding_list))
338 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
339 (compare_orig(neigh_node->addr, tmp_neigh_node->addr))) {
340 interference_candidate = 1;
345 /* don't care further if it is an interference candidate */
346 if (interference_candidate)
349 /* this neighbor already is part of our candidate list */
350 if (!list_empty(&neigh_node->bonding_list))
353 if (!atomic_inc_not_zero(&neigh_node->refcount))
356 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
357 atomic_inc(&orig_node->bond_candidates);
361 bonding_candidate_del(orig_node, neigh_node);
364 spin_unlock_bh(&orig_node->neigh_list_lock);
368 /* copy primary address for bonding */
369 static void bonding_save_primary(struct orig_node *orig_node,
370 struct orig_node *orig_neigh_node,
371 struct batman_packet *batman_packet)
373 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
376 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
379 static void update_orig(struct bat_priv *bat_priv,
380 struct orig_node *orig_node,
381 struct ethhdr *ethhdr,
382 struct batman_packet *batman_packet,
383 struct batman_if *if_incoming,
384 unsigned char *hna_buff, int hna_buff_len,
387 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
388 struct orig_node *orig_node_tmp;
389 struct hlist_node *node;
390 int tmp_hna_buff_len;
391 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
393 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
394 "Searching and updating originator entry of received packet\n");
397 hlist_for_each_entry_rcu(tmp_neigh_node, node,
398 &orig_node->neigh_list, list) {
399 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
400 (tmp_neigh_node->if_incoming == if_incoming) &&
401 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
403 neigh_node_free_ref(neigh_node);
404 neigh_node = tmp_neigh_node;
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);
418 struct orig_node *orig_tmp;
420 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
424 neigh_node = create_neighbor(orig_node, orig_tmp,
425 ethhdr->h_source, if_incoming);
427 kref_put(&orig_tmp->refcount, orig_node_free_ref);
431 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
436 bat_dbg(DBG_BATMAN, bat_priv,
437 "Updating existing last-hop neighbor of originator\n");
441 orig_node->flags = batman_packet->flags;
442 neigh_node->last_valid = jiffies;
444 ring_buffer_set(neigh_node->tq_recv,
445 &neigh_node->tq_index,
447 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
450 orig_node->last_ttl = batman_packet->ttl;
451 neigh_node->last_ttl = batman_packet->ttl;
454 bonding_candidate_add(orig_node, neigh_node);
456 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
457 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
459 /* if this neighbor already is our next hop there is nothing
461 if (orig_node->router == neigh_node)
464 /* if this neighbor does not offer a better TQ we won't consider it */
465 if ((orig_node->router) &&
466 (orig_node->router->tq_avg > neigh_node->tq_avg))
469 /* if the TQ is the same and the link not more symetric we
470 * won't consider it either */
471 if ((orig_node->router) &&
472 (neigh_node->tq_avg == orig_node->router->tq_avg)) {
473 orig_node_tmp = orig_node->router->orig_node;
474 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
476 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
477 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
479 orig_node_tmp = neigh_node->orig_node;
480 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
481 bcast_own_sum_neigh =
482 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
483 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
485 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
489 update_routes(bat_priv, orig_node, neigh_node,
490 hna_buff, tmp_hna_buff_len);
494 update_routes(bat_priv, orig_node, orig_node->router,
495 hna_buff, tmp_hna_buff_len);
498 if (orig_node->gw_flags != batman_packet->gw_flags)
499 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
501 orig_node->gw_flags = batman_packet->gw_flags;
503 /* restart gateway selection if fast or late switching was enabled */
504 if ((orig_node->gw_flags) &&
505 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
506 (atomic_read(&bat_priv->gw_sel_class) > 2))
507 gw_check_election(bat_priv, orig_node);
515 neigh_node_free_ref(neigh_node);
518 /* checks whether the host restarted and is in the protection time.
520 * 0 if the packet is to be accepted
521 * 1 if the packet is to be ignored.
523 static int window_protected(struct bat_priv *bat_priv,
524 int32_t seq_num_diff,
525 unsigned long *last_reset)
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))) {
532 *last_reset = jiffies;
533 bat_dbg(DBG_BATMAN, bat_priv,
534 "old packet received, start protection\n");
543 /* processes a batman packet for all interfaces, adjusts the sequence number and
544 * finds out whether it is a duplicate.
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.
551 static char count_real_packets(struct ethhdr *ethhdr,
552 struct batman_packet *batman_packet,
553 struct batman_if *if_incoming)
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 char is_duplicate = 0;
564 orig_node = get_orig_node(bat_priv, batman_packet->orig);
568 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
570 /* signalize caller that the packet is to be dropped. */
571 if (window_protected(bat_priv, seq_diff,
572 &orig_node->batman_seqno_reset))
576 hlist_for_each_entry_rcu(tmp_neigh_node, node,
577 &orig_node->neigh_list, list) {
579 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
580 orig_node->last_real_seqno,
581 batman_packet->seqno);
583 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
584 (tmp_neigh_node->if_incoming == if_incoming))
589 /* if the window moved, set the update flag. */
590 need_update |= bit_get_packet(bat_priv,
591 tmp_neigh_node->real_bits,
594 tmp_neigh_node->real_packet_count =
595 bit_packet_count(tmp_neigh_node->real_bits);
600 bat_dbg(DBG_BATMAN, bat_priv,
601 "updating last_seqno: old %d, new %d\n",
602 orig_node->last_real_seqno, batman_packet->seqno);
603 orig_node->last_real_seqno = batman_packet->seqno;
606 kref_put(&orig_node->refcount, orig_node_free_ref);
610 kref_put(&orig_node->refcount, orig_node_free_ref);
614 void receive_bat_packet(struct ethhdr *ethhdr,
615 struct batman_packet *batman_packet,
616 unsigned char *hna_buff, int hna_buff_len,
617 struct batman_if *if_incoming)
619 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
620 struct batman_if *batman_if;
621 struct orig_node *orig_neigh_node, *orig_node;
622 char has_directlink_flag;
623 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
624 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
626 uint32_t if_incoming_seqno;
628 /* Silently drop when the batman packet is actually not a
631 * This might happen if a packet is padded (e.g. Ethernet has a
632 * minimum frame length of 64 byte) and the aggregation interprets
633 * it as an additional length.
635 * TODO: A more sane solution would be to have a bit in the
636 * batman_packet to detect whether the packet is the last
637 * packet in an aggregation. Here we expect that the padding
638 * is always zero (or not 0x01)
640 if (batman_packet->packet_type != BAT_PACKET)
643 /* could be changed by schedule_own_packet() */
644 if_incoming_seqno = atomic_read(&if_incoming->seqno);
646 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
648 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
649 batman_packet->orig) ? 1 : 0);
651 bat_dbg(DBG_BATMAN, bat_priv,
652 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
653 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
654 "TTL %d, V %d, IDF %d)\n",
655 ethhdr->h_source, if_incoming->net_dev->name,
656 if_incoming->net_dev->dev_addr, batman_packet->orig,
657 batman_packet->prev_sender, batman_packet->seqno,
658 batman_packet->tq, batman_packet->ttl, batman_packet->version,
659 has_directlink_flag);
662 list_for_each_entry_rcu(batman_if, &if_list, list) {
663 if (batman_if->if_status != IF_ACTIVE)
666 if (batman_if->soft_iface != if_incoming->soft_iface)
669 if (compare_orig(ethhdr->h_source,
670 batman_if->net_dev->dev_addr))
673 if (compare_orig(batman_packet->orig,
674 batman_if->net_dev->dev_addr))
677 if (compare_orig(batman_packet->prev_sender,
678 batman_if->net_dev->dev_addr))
681 if (compare_orig(ethhdr->h_source, broadcast_addr))
686 if (batman_packet->version != COMPAT_VERSION) {
687 bat_dbg(DBG_BATMAN, bat_priv,
688 "Drop packet: incompatible batman version (%i)\n",
689 batman_packet->version);
694 bat_dbg(DBG_BATMAN, bat_priv,
695 "Drop packet: received my own broadcast (sender: %pM"
702 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
703 "ignoring all packets with broadcast source addr (sender: %pM"
704 ")\n", ethhdr->h_source);
712 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
713 if (!orig_neigh_node)
716 /* neighbor has to indicate direct link and it has to
717 * come via the corresponding interface */
718 /* if received seqno equals last send seqno save new
719 * seqno for bidirectional check */
720 if (has_directlink_flag &&
721 compare_orig(if_incoming->net_dev->dev_addr,
722 batman_packet->orig) &&
723 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
724 offset = if_incoming->if_num * NUM_WORDS;
726 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
727 word = &(orig_neigh_node->bcast_own[offset]);
729 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
730 bit_packet_count(word);
731 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
734 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
735 "originator packet from myself (via neighbor)\n");
736 kref_put(&orig_neigh_node->refcount, orig_node_free_ref);
741 bat_dbg(DBG_BATMAN, bat_priv,
742 "Drop packet: ignoring all rebroadcast echos (sender: "
743 "%pM)\n", ethhdr->h_source);
747 orig_node = get_orig_node(bat_priv, batman_packet->orig);
751 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
753 if (is_duplicate == -1) {
754 bat_dbg(DBG_BATMAN, bat_priv,
755 "Drop packet: packet within seqno protection time "
756 "(sender: %pM)\n", ethhdr->h_source);
760 if (batman_packet->tq == 0) {
761 bat_dbg(DBG_BATMAN, bat_priv,
762 "Drop packet: originator packet with tq equal 0\n");
766 /* avoid temporary routing loops */
767 if ((orig_node->router) &&
768 (orig_node->router->orig_node->router) &&
769 (compare_orig(orig_node->router->addr,
770 batman_packet->prev_sender)) &&
771 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
772 (compare_orig(orig_node->router->addr,
773 orig_node->router->orig_node->router->addr))) {
774 bat_dbg(DBG_BATMAN, bat_priv,
775 "Drop packet: ignoring all rebroadcast packets that "
776 "may make me loop (sender: %pM)\n", ethhdr->h_source);
780 /* if sender is a direct neighbor the sender mac equals
782 orig_neigh_node = (is_single_hop_neigh ?
784 get_orig_node(bat_priv, ethhdr->h_source));
785 if (!orig_neigh_node)
788 /* drop packet if sender is not a direct neighbor and if we
789 * don't route towards it */
790 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
791 bat_dbg(DBG_BATMAN, bat_priv,
792 "Drop packet: OGM via unknown neighbor!\n");
796 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
797 batman_packet, if_incoming);
799 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
801 /* update ranking if it is not a duplicate or has the same
802 * seqno and similar ttl as the non-duplicate */
803 if (is_bidirectional &&
805 ((orig_node->last_real_seqno == batman_packet->seqno) &&
806 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
807 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
808 if_incoming, hna_buff, hna_buff_len, is_duplicate);
810 /* is single hop (direct) neighbor */
811 if (is_single_hop_neigh) {
813 /* mark direct link on incoming interface */
814 schedule_forward_packet(orig_node, ethhdr, batman_packet,
815 1, hna_buff_len, if_incoming);
817 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
818 "rebroadcast neighbor packet with direct link flag\n");
822 /* multihop originator */
823 if (!is_bidirectional) {
824 bat_dbg(DBG_BATMAN, bat_priv,
825 "Drop packet: not received via bidirectional link\n");
830 bat_dbg(DBG_BATMAN, bat_priv,
831 "Drop packet: duplicate packet received\n");
835 bat_dbg(DBG_BATMAN, bat_priv,
836 "Forwarding packet: rebroadcast originator packet\n");
837 schedule_forward_packet(orig_node, ethhdr, batman_packet,
838 0, hna_buff_len, if_incoming);
841 if (!is_single_hop_neigh)
842 kref_put(&orig_neigh_node->refcount, orig_node_free_ref);
844 kref_put(&orig_node->refcount, orig_node_free_ref);
847 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
849 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
850 struct ethhdr *ethhdr;
852 /* drop packet if it has not necessary minimum size */
853 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
856 ethhdr = (struct ethhdr *)skb_mac_header(skb);
858 /* packet with broadcast indication but unicast recipient */
859 if (!is_broadcast_ether_addr(ethhdr->h_dest))
862 /* packet with broadcast sender address */
863 if (is_broadcast_ether_addr(ethhdr->h_source))
866 /* create a copy of the skb, if needed, to modify it. */
867 if (skb_cow(skb, 0) < 0)
870 /* keep skb linear */
871 if (skb_linearize(skb) < 0)
874 ethhdr = (struct ethhdr *)skb_mac_header(skb);
876 spin_lock_bh(&bat_priv->orig_hash_lock);
877 receive_aggr_bat_packet(ethhdr,
881 spin_unlock_bh(&bat_priv->orig_hash_lock);
884 return NET_RX_SUCCESS;
887 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
888 struct sk_buff *skb, size_t icmp_len)
890 struct orig_node *orig_node = NULL;
891 struct neigh_node *neigh_node = NULL;
892 struct icmp_packet_rr *icmp_packet;
893 struct batman_if *batman_if;
894 uint8_t dstaddr[ETH_ALEN];
895 int ret = NET_RX_DROP;
897 icmp_packet = (struct icmp_packet_rr *)skb->data;
899 /* add data to device queue */
900 if (icmp_packet->msg_type != ECHO_REQUEST) {
901 bat_socket_receive_packet(icmp_packet, icmp_len);
905 if (!bat_priv->primary_if)
908 /* answer echo request (ping) */
909 /* get routing information */
910 spin_lock_bh(&bat_priv->orig_hash_lock);
912 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
913 compare_orig, choose_orig,
919 kref_get(&orig_node->refcount);
920 neigh_node = orig_node->router;
925 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
932 /* don't lock while sending the packets ... we therefore
933 * copy the required data before sending */
934 batman_if = orig_node->router->if_incoming;
935 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
936 spin_unlock_bh(&bat_priv->orig_hash_lock);
938 /* create a copy of the skb, if needed, to modify it. */
939 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
942 icmp_packet = (struct icmp_packet_rr *)skb->data;
944 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
945 memcpy(icmp_packet->orig,
946 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
947 icmp_packet->msg_type = ECHO_REPLY;
948 icmp_packet->ttl = TTL;
950 send_skb_packet(skb, batman_if, dstaddr);
951 ret = NET_RX_SUCCESS;
956 spin_unlock_bh(&bat_priv->orig_hash_lock);
959 neigh_node_free_ref(neigh_node);
961 kref_put(&orig_node->refcount, orig_node_free_ref);
965 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
968 struct orig_node *orig_node = NULL;
969 struct neigh_node *neigh_node = NULL;
970 struct icmp_packet *icmp_packet;
971 struct batman_if *batman_if;
972 uint8_t dstaddr[ETH_ALEN];
973 int ret = NET_RX_DROP;
975 icmp_packet = (struct icmp_packet *)skb->data;
977 /* send TTL exceeded if packet is an echo request (traceroute) */
978 if (icmp_packet->msg_type != ECHO_REQUEST) {
979 pr_debug("Warning - can't forward icmp packet from %pM to "
980 "%pM: ttl exceeded\n", icmp_packet->orig,
985 if (!bat_priv->primary_if)
988 /* get routing information */
989 spin_lock_bh(&bat_priv->orig_hash_lock);
991 orig_node = ((struct orig_node *)
992 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
998 kref_get(&orig_node->refcount);
999 neigh_node = orig_node->router;
1004 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
1011 /* don't lock while sending the packets ... we therefore
1012 * copy the required data before sending */
1013 batman_if = orig_node->router->if_incoming;
1014 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
1015 spin_unlock_bh(&bat_priv->orig_hash_lock);
1017 /* create a copy of the skb, if needed, to modify it. */
1018 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1021 icmp_packet = (struct icmp_packet *)skb->data;
1023 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
1024 memcpy(icmp_packet->orig,
1025 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
1026 icmp_packet->msg_type = TTL_EXCEEDED;
1027 icmp_packet->ttl = TTL;
1029 send_skb_packet(skb, batman_if, dstaddr);
1030 ret = NET_RX_SUCCESS;
1035 spin_unlock_bh(&bat_priv->orig_hash_lock);
1038 neigh_node_free_ref(neigh_node);
1040 kref_put(&orig_node->refcount, orig_node_free_ref);
1045 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
1047 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1048 struct icmp_packet_rr *icmp_packet;
1049 struct ethhdr *ethhdr;
1050 struct orig_node *orig_node = NULL;
1051 struct neigh_node *neigh_node = NULL;
1052 struct batman_if *batman_if;
1053 int hdr_size = sizeof(struct icmp_packet);
1054 uint8_t dstaddr[ETH_ALEN];
1055 int ret = NET_RX_DROP;
1058 * we truncate all incoming icmp packets if they don't match our size
1060 if (skb->len >= sizeof(struct icmp_packet_rr))
1061 hdr_size = sizeof(struct icmp_packet_rr);
1063 /* drop packet if it has not necessary minimum size */
1064 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1067 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1069 /* packet with unicast indication but broadcast recipient */
1070 if (is_broadcast_ether_addr(ethhdr->h_dest))
1073 /* packet with broadcast sender address */
1074 if (is_broadcast_ether_addr(ethhdr->h_source))
1078 if (!is_my_mac(ethhdr->h_dest))
1081 icmp_packet = (struct icmp_packet_rr *)skb->data;
1083 /* add record route information if not full */
1084 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1085 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1086 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1087 ethhdr->h_dest, ETH_ALEN);
1088 icmp_packet->rr_cur++;
1092 if (is_my_mac(icmp_packet->dst))
1093 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1096 if (icmp_packet->ttl < 2)
1097 return recv_icmp_ttl_exceeded(bat_priv, skb);
1099 /* get routing information */
1100 spin_lock_bh(&bat_priv->orig_hash_lock);
1102 orig_node = ((struct orig_node *)
1103 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1108 kref_get(&orig_node->refcount);
1109 neigh_node = orig_node->router;
1114 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
1121 /* don't lock while sending the packets ... we therefore
1122 * copy the required data before sending */
1123 batman_if = orig_node->router->if_incoming;
1124 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
1125 spin_unlock_bh(&bat_priv->orig_hash_lock);
1127 /* create a copy of the skb, if needed, to modify it. */
1128 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1131 icmp_packet = (struct icmp_packet_rr *)skb->data;
1137 send_skb_packet(skb, batman_if, dstaddr);
1138 ret = NET_RX_SUCCESS;
1143 spin_unlock_bh(&bat_priv->orig_hash_lock);
1146 neigh_node_free_ref(neigh_node);
1148 kref_put(&orig_node->refcount, orig_node_free_ref);
1152 /* find a suitable router for this originator, and use
1153 * bonding if possible. increases the found neighbors
1155 struct neigh_node *find_router(struct bat_priv *bat_priv,
1156 struct orig_node *orig_node,
1157 struct batman_if *recv_if)
1159 struct orig_node *primary_orig_node;
1160 struct orig_node *router_orig;
1161 struct neigh_node *router, *first_candidate, *tmp_neigh_node;
1162 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1163 int bonding_enabled;
1168 if (!orig_node->router)
1171 /* without bonding, the first node should
1172 * always choose the default router. */
1173 bonding_enabled = atomic_read(&bat_priv->bonding);
1176 /* select default router to output */
1177 router = orig_node->router;
1178 router_orig = orig_node->router->orig_node;
1179 if (!router_orig || !atomic_inc_not_zero(&router->refcount)) {
1184 if ((!recv_if) && (!bonding_enabled))
1187 /* if we have something in the primary_addr, we can search
1188 * for a potential bonding candidate. */
1189 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1192 /* find the orig_node which has the primary interface. might
1193 * even be the same as our router_orig in many cases */
1195 if (memcmp(router_orig->primary_addr,
1196 router_orig->orig, ETH_ALEN) == 0) {
1197 primary_orig_node = router_orig;
1199 primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
1201 router_orig->primary_addr);
1202 if (!primary_orig_node)
1206 /* with less than 2 candidates, we can't do any
1207 * bonding and prefer the original router. */
1208 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1212 /* all nodes between should choose a candidate which
1213 * is is not on the interface where the packet came
1216 neigh_node_free_ref(router);
1217 first_candidate = NULL;
1220 if (bonding_enabled) {
1221 /* in the bonding case, send the packets in a round
1222 * robin fashion over the remaining interfaces. */
1224 list_for_each_entry_rcu(tmp_neigh_node,
1225 &primary_orig_node->bond_list, bonding_list) {
1226 if (!first_candidate)
1227 first_candidate = tmp_neigh_node;
1228 /* recv_if == NULL on the first node. */
1229 if (tmp_neigh_node->if_incoming != recv_if &&
1230 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
1231 router = tmp_neigh_node;
1236 /* use the first candidate if nothing was found. */
1237 if (!router && first_candidate &&
1238 atomic_inc_not_zero(&first_candidate->refcount))
1239 router = first_candidate;
1246 /* selected should point to the next element
1247 * after the current router */
1248 spin_lock_bh(&primary_orig_node->neigh_list_lock);
1249 /* this is a list_move(), which unfortunately
1250 * does not exist as rcu version */
1251 list_del_rcu(&primary_orig_node->bond_list);
1252 list_add_rcu(&primary_orig_node->bond_list,
1253 &router->bonding_list);
1254 spin_unlock_bh(&primary_orig_node->neigh_list_lock);
1257 /* if bonding is disabled, use the best of the
1258 * remaining candidates which are not using
1259 * this interface. */
1260 list_for_each_entry_rcu(tmp_neigh_node,
1261 &primary_orig_node->bond_list, bonding_list) {
1262 if (!first_candidate)
1263 first_candidate = tmp_neigh_node;
1265 /* recv_if == NULL on the first node. */
1266 if (tmp_neigh_node->if_incoming == recv_if)
1269 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1272 /* if we don't have a router yet
1273 * or this one is better, choose it. */
1275 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1276 /* decrement refcount of
1277 * previously selected router */
1279 neigh_node_free_ref(router);
1281 router = tmp_neigh_node;
1282 atomic_inc_not_zero(&router->refcount);
1285 neigh_node_free_ref(tmp_neigh_node);
1288 /* use the first candidate if nothing was found. */
1289 if (!router && first_candidate &&
1290 atomic_inc_not_zero(&first_candidate->refcount))
1291 router = first_candidate;
1298 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1300 struct ethhdr *ethhdr;
1302 /* drop packet if it has not necessary minimum size */
1303 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1306 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1308 /* packet with unicast indication but broadcast recipient */
1309 if (is_broadcast_ether_addr(ethhdr->h_dest))
1312 /* packet with broadcast sender address */
1313 if (is_broadcast_ether_addr(ethhdr->h_source))
1317 if (!is_my_mac(ethhdr->h_dest))
1323 int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1326 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1327 struct orig_node *orig_node = NULL;
1328 struct neigh_node *neigh_node = NULL;
1329 struct batman_if *batman_if;
1330 uint8_t dstaddr[ETH_ALEN];
1331 struct unicast_packet *unicast_packet;
1332 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1333 int ret = NET_RX_DROP;
1334 struct sk_buff *new_skb;
1336 unicast_packet = (struct unicast_packet *)skb->data;
1339 if (unicast_packet->ttl < 2) {
1340 pr_debug("Warning - can't forward unicast packet from %pM to "
1341 "%pM: ttl exceeded\n", ethhdr->h_source,
1342 unicast_packet->dest);
1346 /* get routing information */
1347 spin_lock_bh(&bat_priv->orig_hash_lock);
1349 orig_node = ((struct orig_node *)
1350 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1351 unicast_packet->dest));
1355 kref_get(&orig_node->refcount);
1358 /* find_router() increases neigh_nodes refcount if found. */
1359 neigh_node = find_router(bat_priv, orig_node, recv_if);
1362 spin_unlock_bh(&bat_priv->orig_hash_lock);
1366 /* don't lock while sending the packets ... we therefore
1367 * copy the required data before sending */
1368 batman_if = neigh_node->if_incoming;
1369 memcpy(dstaddr, neigh_node->addr, ETH_ALEN);
1370 spin_unlock_bh(&bat_priv->orig_hash_lock);
1372 /* create a copy of the skb, if needed, to modify it. */
1373 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1376 unicast_packet = (struct unicast_packet *)skb->data;
1378 if (unicast_packet->packet_type == BAT_UNICAST &&
1379 atomic_read(&bat_priv->fragmentation) &&
1380 skb->len > batman_if->net_dev->mtu)
1381 return frag_send_skb(skb, bat_priv, batman_if,
1384 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1385 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
1387 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1389 if (ret == NET_RX_DROP)
1392 /* packet was buffered for late merge */
1394 ret = NET_RX_SUCCESS;
1399 unicast_packet = (struct unicast_packet *)skb->data;
1403 unicast_packet->ttl--;
1406 send_skb_packet(skb, batman_if, dstaddr);
1407 ret = NET_RX_SUCCESS;
1412 spin_unlock_bh(&bat_priv->orig_hash_lock);
1415 neigh_node_free_ref(neigh_node);
1417 kref_put(&orig_node->refcount, orig_node_free_ref);
1421 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1423 struct unicast_packet *unicast_packet;
1424 int hdr_size = sizeof(struct unicast_packet);
1426 if (check_unicast_packet(skb, hdr_size) < 0)
1429 unicast_packet = (struct unicast_packet *)skb->data;
1432 if (is_my_mac(unicast_packet->dest)) {
1433 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1434 return NET_RX_SUCCESS;
1437 return route_unicast_packet(skb, recv_if, hdr_size);
1440 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1442 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1443 struct unicast_frag_packet *unicast_packet;
1444 int hdr_size = sizeof(struct unicast_frag_packet);
1445 struct sk_buff *new_skb = NULL;
1448 if (check_unicast_packet(skb, hdr_size) < 0)
1451 unicast_packet = (struct unicast_frag_packet *)skb->data;
1454 if (is_my_mac(unicast_packet->dest)) {
1456 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1458 if (ret == NET_RX_DROP)
1461 /* packet was buffered for late merge */
1463 return NET_RX_SUCCESS;
1465 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1466 sizeof(struct unicast_packet));
1467 return NET_RX_SUCCESS;
1470 return route_unicast_packet(skb, recv_if, hdr_size);
1474 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1476 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1477 struct orig_node *orig_node;
1478 struct bcast_packet *bcast_packet;
1479 struct ethhdr *ethhdr;
1480 int hdr_size = sizeof(struct bcast_packet);
1483 /* drop packet if it has not necessary minimum size */
1484 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1487 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1489 /* packet with broadcast indication but unicast recipient */
1490 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1493 /* packet with broadcast sender address */
1494 if (is_broadcast_ether_addr(ethhdr->h_source))
1497 /* ignore broadcasts sent by myself */
1498 if (is_my_mac(ethhdr->h_source))
1501 bcast_packet = (struct bcast_packet *)skb->data;
1503 /* ignore broadcasts originated by myself */
1504 if (is_my_mac(bcast_packet->orig))
1507 if (bcast_packet->ttl < 2)
1510 spin_lock_bh(&bat_priv->orig_hash_lock);
1512 orig_node = ((struct orig_node *)
1513 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1514 bcast_packet->orig));
1518 spin_unlock_bh(&bat_priv->orig_hash_lock);
1522 /* check whether the packet is a duplicate */
1523 if (get_bit_status(orig_node->bcast_bits,
1524 orig_node->last_bcast_seqno,
1525 ntohl(bcast_packet->seqno))) {
1526 spin_unlock_bh(&bat_priv->orig_hash_lock);
1530 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1532 /* check whether the packet is old and the host just restarted. */
1533 if (window_protected(bat_priv, seq_diff,
1534 &orig_node->bcast_seqno_reset)) {
1535 spin_unlock_bh(&bat_priv->orig_hash_lock);
1539 /* mark broadcast in flood history, update window position
1541 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1542 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1544 spin_unlock_bh(&bat_priv->orig_hash_lock);
1545 /* rebroadcast packet */
1546 add_bcast_packet_to_list(bat_priv, skb);
1548 /* broadcast for me */
1549 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1551 return NET_RX_SUCCESS;
1554 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1556 struct vis_packet *vis_packet;
1557 struct ethhdr *ethhdr;
1558 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1559 int hdr_size = sizeof(struct vis_packet);
1561 /* keep skb linear */
1562 if (skb_linearize(skb) < 0)
1565 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1568 vis_packet = (struct vis_packet *)skb->data;
1569 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1572 if (!is_my_mac(ethhdr->h_dest))
1575 /* ignore own packets */
1576 if (is_my_mac(vis_packet->vis_orig))
1579 if (is_my_mac(vis_packet->sender_orig))
1582 switch (vis_packet->vis_type) {
1583 case VIS_TYPE_SERVER_SYNC:
1584 receive_server_sync_packet(bat_priv, vis_packet,
1588 case VIS_TYPE_CLIENT_UPDATE:
1589 receive_client_update_packet(bat_priv, vis_packet,
1593 default: /* ignore unknown packet */
1597 /* We take a copy of the data in the packet, so we should
1598 always free the skbuf. */