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 *node;
43 struct hlist_head *head;
44 struct orig_node *orig_node;
49 spin_lock_bh(&bat_priv->orig_hash_lock);
51 for (i = 0; i < hash->size; i++) {
52 head = &hash->table[i];
55 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
56 spin_lock_bh(&orig_node->ogm_cnt_lock);
57 word_index = batman_if->if_num * NUM_WORDS;
58 word = &(orig_node->bcast_own[word_index]);
60 bit_get_packet(bat_priv, word, 1, 0);
61 orig_node->bcast_own_sum[batman_if->if_num] =
62 bit_packet_count(word);
63 spin_unlock_bh(&orig_node->ogm_cnt_lock);
68 spin_unlock_bh(&bat_priv->orig_hash_lock);
71 static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
72 unsigned char *hna_buff, int hna_buff_len)
74 if ((hna_buff_len != orig_node->hna_buff_len) ||
75 ((hna_buff_len > 0) &&
76 (orig_node->hna_buff_len > 0) &&
77 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
79 if (orig_node->hna_buff_len > 0)
80 hna_global_del_orig(bat_priv, orig_node,
81 "originator changed hna");
83 if ((hna_buff_len > 0) && (hna_buff))
84 hna_global_add_orig(bat_priv, orig_node,
85 hna_buff, hna_buff_len);
89 static void update_route(struct bat_priv *bat_priv,
90 struct orig_node *orig_node,
91 struct neigh_node *neigh_node,
92 unsigned char *hna_buff, int hna_buff_len)
94 struct neigh_node *neigh_node_tmp;
97 if ((orig_node->router) && (!neigh_node)) {
99 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
101 hna_global_del_orig(bat_priv, orig_node,
102 "originator timed out");
105 } else if ((!orig_node->router) && (neigh_node)) {
107 bat_dbg(DBG_ROUTES, bat_priv,
108 "Adding route towards: %pM (via %pM)\n",
109 orig_node->orig, neigh_node->addr);
110 hna_global_add_orig(bat_priv, orig_node,
111 hna_buff, hna_buff_len);
115 bat_dbg(DBG_ROUTES, bat_priv,
116 "Changing route towards: %pM "
117 "(now via %pM - was via %pM)\n",
118 orig_node->orig, neigh_node->addr,
119 orig_node->router->addr);
122 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
124 neigh_node_tmp = orig_node->router;
125 orig_node->router = neigh_node;
127 neigh_node_free_ref(neigh_node_tmp);
131 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
132 struct neigh_node *neigh_node, unsigned char *hna_buff,
139 if (orig_node->router != neigh_node)
140 update_route(bat_priv, orig_node, neigh_node,
141 hna_buff, hna_buff_len);
142 /* may be just HNA changed */
144 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
147 static int is_bidirectional_neigh(struct orig_node *orig_node,
148 struct orig_node *orig_neigh_node,
149 struct batman_packet *batman_packet,
150 struct batman_if *if_incoming)
152 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
153 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
154 struct hlist_node *node;
155 unsigned char total_count;
156 uint8_t orig_eq_count, neigh_rq_count, tq_own;
157 int tq_asym_penalty, ret = 0;
159 if (orig_node == orig_neigh_node) {
161 hlist_for_each_entry_rcu(tmp_neigh_node, node,
162 &orig_node->neigh_list, list) {
164 if (compare_eth(tmp_neigh_node->addr,
165 orig_neigh_node->orig) &&
166 (tmp_neigh_node->if_incoming == if_incoming))
167 neigh_node = tmp_neigh_node;
171 neigh_node = create_neighbor(orig_node,
173 orig_neigh_node->orig,
175 /* create_neighbor failed, return 0 */
179 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
186 neigh_node->last_valid = jiffies;
188 /* find packet count of corresponding one hop neighbor */
190 hlist_for_each_entry_rcu(tmp_neigh_node, node,
191 &orig_neigh_node->neigh_list, list) {
193 if (compare_eth(tmp_neigh_node->addr,
194 orig_neigh_node->orig) &&
195 (tmp_neigh_node->if_incoming == if_incoming))
196 neigh_node = tmp_neigh_node;
200 neigh_node = create_neighbor(orig_neigh_node,
202 orig_neigh_node->orig,
204 /* create_neighbor failed, return 0 */
208 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
216 orig_node->last_valid = jiffies;
218 spin_lock_bh(&orig_node->ogm_cnt_lock);
219 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
220 neigh_rq_count = neigh_node->real_packet_count;
221 spin_unlock_bh(&orig_node->ogm_cnt_lock);
223 /* pay attention to not get a value bigger than 100 % */
224 total_count = (orig_eq_count > neigh_rq_count ?
225 neigh_rq_count : orig_eq_count);
227 /* if we have too few packets (too less data) we set tq_own to zero */
228 /* if we receive too few packets it is not considered bidirectional */
229 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
230 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
233 /* neigh_node->real_packet_count is never zero as we
234 * only purge old information when getting new
236 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
239 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
240 * affect the nearly-symmetric links only a little, but
241 * punishes asymmetric links more. This will give a value
242 * between 0 and TQ_MAX_VALUE
244 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
245 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
246 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
247 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
248 (TQ_LOCAL_WINDOW_SIZE *
249 TQ_LOCAL_WINDOW_SIZE *
250 TQ_LOCAL_WINDOW_SIZE);
252 batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
253 (TQ_MAX_VALUE * TQ_MAX_VALUE));
255 bat_dbg(DBG_BATMAN, bat_priv,
257 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
258 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
260 orig_node->orig, orig_neigh_node->orig, total_count,
261 neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
263 /* if link has the minimum required transmission quality
264 * consider it bidirectional */
265 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
274 neigh_node_free_ref(neigh_node);
278 /* caller must hold the neigh_list_lock */
279 void bonding_candidate_del(struct orig_node *orig_node,
280 struct neigh_node *neigh_node)
282 /* this neighbor is not part of our candidate list */
283 if (list_empty(&neigh_node->bonding_list))
286 list_del_rcu(&neigh_node->bonding_list);
287 INIT_LIST_HEAD(&neigh_node->bonding_list);
288 neigh_node_free_ref(neigh_node);
289 atomic_dec(&orig_node->bond_candidates);
295 static void bonding_candidate_add(struct orig_node *orig_node,
296 struct neigh_node *neigh_node)
298 struct hlist_node *node;
299 struct neigh_node *tmp_neigh_node;
300 uint8_t best_tq, interference_candidate = 0;
302 spin_lock_bh(&orig_node->neigh_list_lock);
304 /* only consider if it has the same primary address ... */
305 if (!compare_eth(orig_node->orig,
306 neigh_node->orig_node->primary_addr))
309 if (!orig_node->router)
312 best_tq = orig_node->router->tq_avg;
314 /* ... and is good enough to be considered */
315 if (neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
319 * check if we have another candidate with the same mac address or
320 * interface. If we do, we won't select this candidate because of
321 * possible interference.
323 hlist_for_each_entry_rcu(tmp_neigh_node, node,
324 &orig_node->neigh_list, list) {
326 if (tmp_neigh_node == neigh_node)
329 /* we only care if the other candidate is even
330 * considered as candidate. */
331 if (list_empty(&tmp_neigh_node->bonding_list))
334 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
335 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
336 interference_candidate = 1;
341 /* don't care further if it is an interference candidate */
342 if (interference_candidate)
345 /* this neighbor already is part of our candidate list */
346 if (!list_empty(&neigh_node->bonding_list))
349 if (!atomic_inc_not_zero(&neigh_node->refcount))
352 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
353 atomic_inc(&orig_node->bond_candidates);
357 bonding_candidate_del(orig_node, neigh_node);
360 spin_unlock_bh(&orig_node->neigh_list_lock);
364 /* copy primary address for bonding */
365 static void bonding_save_primary(struct orig_node *orig_node,
366 struct orig_node *orig_neigh_node,
367 struct batman_packet *batman_packet)
369 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
372 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
375 static void update_orig(struct bat_priv *bat_priv,
376 struct orig_node *orig_node,
377 struct ethhdr *ethhdr,
378 struct batman_packet *batman_packet,
379 struct batman_if *if_incoming,
380 unsigned char *hna_buff, int hna_buff_len,
383 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
384 struct orig_node *orig_node_tmp;
385 struct hlist_node *node;
386 int tmp_hna_buff_len;
387 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
389 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
390 "Searching and updating originator entry of received packet\n");
393 hlist_for_each_entry_rcu(tmp_neigh_node, node,
394 &orig_node->neigh_list, list) {
395 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
396 (tmp_neigh_node->if_incoming == if_incoming) &&
397 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
399 neigh_node_free_ref(neigh_node);
400 neigh_node = tmp_neigh_node;
407 ring_buffer_set(tmp_neigh_node->tq_recv,
408 &tmp_neigh_node->tq_index, 0);
409 tmp_neigh_node->tq_avg =
410 ring_buffer_avg(tmp_neigh_node->tq_recv);
414 struct orig_node *orig_tmp;
416 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
420 neigh_node = create_neighbor(orig_node, orig_tmp,
421 ethhdr->h_source, if_incoming);
423 orig_node_free_ref(orig_tmp);
427 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
432 bat_dbg(DBG_BATMAN, bat_priv,
433 "Updating existing last-hop neighbor of originator\n");
437 orig_node->flags = batman_packet->flags;
438 neigh_node->last_valid = jiffies;
440 ring_buffer_set(neigh_node->tq_recv,
441 &neigh_node->tq_index,
443 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
446 orig_node->last_ttl = batman_packet->ttl;
447 neigh_node->last_ttl = batman_packet->ttl;
450 bonding_candidate_add(orig_node, neigh_node);
452 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
453 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
455 /* if this neighbor already is our next hop there is nothing
457 if (orig_node->router == neigh_node)
460 /* if this neighbor does not offer a better TQ we won't consider it */
461 if ((orig_node->router) &&
462 (orig_node->router->tq_avg > neigh_node->tq_avg))
465 /* if the TQ is the same and the link not more symetric we
466 * won't consider it either */
467 if ((orig_node->router) &&
468 (neigh_node->tq_avg == orig_node->router->tq_avg)) {
469 orig_node_tmp = orig_node->router->orig_node;
470 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
472 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
473 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
475 orig_node_tmp = neigh_node->orig_node;
476 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
477 bcast_own_sum_neigh =
478 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
479 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
481 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
485 update_routes(bat_priv, orig_node, neigh_node,
486 hna_buff, tmp_hna_buff_len);
490 update_routes(bat_priv, orig_node, orig_node->router,
491 hna_buff, tmp_hna_buff_len);
494 if (orig_node->gw_flags != batman_packet->gw_flags)
495 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
497 orig_node->gw_flags = batman_packet->gw_flags;
499 /* restart gateway selection if fast or late switching was enabled */
500 if ((orig_node->gw_flags) &&
501 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
502 (atomic_read(&bat_priv->gw_sel_class) > 2))
503 gw_check_election(bat_priv, orig_node);
511 neigh_node_free_ref(neigh_node);
514 /* checks whether the host restarted and is in the protection time.
516 * 0 if the packet is to be accepted
517 * 1 if the packet is to be ignored.
519 static int window_protected(struct bat_priv *bat_priv,
520 int32_t seq_num_diff,
521 unsigned long *last_reset)
523 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
524 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
525 if (time_after(jiffies, *last_reset +
526 msecs_to_jiffies(RESET_PROTECTION_MS))) {
528 *last_reset = jiffies;
529 bat_dbg(DBG_BATMAN, bat_priv,
530 "old packet received, start protection\n");
539 /* processes a batman packet for all interfaces, adjusts the sequence number and
540 * finds out whether it is a duplicate.
542 * 1 the packet is a duplicate
543 * 0 the packet has not yet been received
544 * -1 the packet is old and has been received while the seqno window
545 * was protected. Caller should drop it.
547 static char count_real_packets(struct ethhdr *ethhdr,
548 struct batman_packet *batman_packet,
549 struct batman_if *if_incoming)
551 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
552 struct orig_node *orig_node;
553 struct neigh_node *tmp_neigh_node;
554 struct hlist_node *node;
555 char is_duplicate = 0;
558 int set_mark, ret = -1;
560 orig_node = get_orig_node(bat_priv, batman_packet->orig);
564 spin_lock_bh(&orig_node->ogm_cnt_lock);
565 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
567 /* signalize caller that the packet is to be dropped. */
568 if (window_protected(bat_priv, seq_diff,
569 &orig_node->batman_seqno_reset))
573 hlist_for_each_entry_rcu(tmp_neigh_node, node,
574 &orig_node->neigh_list, list) {
576 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
577 orig_node->last_real_seqno,
578 batman_packet->seqno);
580 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
581 (tmp_neigh_node->if_incoming == if_incoming))
586 /* if the window moved, set the update flag. */
587 need_update |= bit_get_packet(bat_priv,
588 tmp_neigh_node->real_bits,
591 tmp_neigh_node->real_packet_count =
592 bit_packet_count(tmp_neigh_node->real_bits);
597 bat_dbg(DBG_BATMAN, bat_priv,
598 "updating last_seqno: old %d, new %d\n",
599 orig_node->last_real_seqno, batman_packet->seqno);
600 orig_node->last_real_seqno = batman_packet->seqno;
606 spin_unlock_bh(&orig_node->ogm_cnt_lock);
607 orig_node_free_ref(orig_node);
611 void receive_bat_packet(struct ethhdr *ethhdr,
612 struct batman_packet *batman_packet,
613 unsigned char *hna_buff, int hna_buff_len,
614 struct batman_if *if_incoming)
616 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
617 struct batman_if *batman_if;
618 struct orig_node *orig_neigh_node, *orig_node;
619 char has_directlink_flag;
620 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
621 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
623 uint32_t if_incoming_seqno;
625 /* Silently drop when the batman packet is actually not a
628 * This might happen if a packet is padded (e.g. Ethernet has a
629 * minimum frame length of 64 byte) and the aggregation interprets
630 * it as an additional length.
632 * TODO: A more sane solution would be to have a bit in the
633 * batman_packet to detect whether the packet is the last
634 * packet in an aggregation. Here we expect that the padding
635 * is always zero (or not 0x01)
637 if (batman_packet->packet_type != BAT_PACKET)
640 /* could be changed by schedule_own_packet() */
641 if_incoming_seqno = atomic_read(&if_incoming->seqno);
643 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
645 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
646 batman_packet->orig) ? 1 : 0);
648 bat_dbg(DBG_BATMAN, bat_priv,
649 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
650 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
651 "TTL %d, V %d, IDF %d)\n",
652 ethhdr->h_source, if_incoming->net_dev->name,
653 if_incoming->net_dev->dev_addr, batman_packet->orig,
654 batman_packet->prev_sender, batman_packet->seqno,
655 batman_packet->tq, batman_packet->ttl, batman_packet->version,
656 has_directlink_flag);
659 list_for_each_entry_rcu(batman_if, &if_list, list) {
660 if (batman_if->if_status != IF_ACTIVE)
663 if (batman_if->soft_iface != if_incoming->soft_iface)
666 if (compare_eth(ethhdr->h_source,
667 batman_if->net_dev->dev_addr))
670 if (compare_eth(batman_packet->orig,
671 batman_if->net_dev->dev_addr))
674 if (compare_eth(batman_packet->prev_sender,
675 batman_if->net_dev->dev_addr))
678 if (compare_eth(ethhdr->h_source, broadcast_addr))
683 if (batman_packet->version != COMPAT_VERSION) {
684 bat_dbg(DBG_BATMAN, bat_priv,
685 "Drop packet: incompatible batman version (%i)\n",
686 batman_packet->version);
691 bat_dbg(DBG_BATMAN, bat_priv,
692 "Drop packet: received my own broadcast (sender: %pM"
699 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
700 "ignoring all packets with broadcast source addr (sender: %pM"
701 ")\n", ethhdr->h_source);
709 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
710 if (!orig_neigh_node)
713 /* neighbor has to indicate direct link and it has to
714 * come via the corresponding interface */
715 /* if received seqno equals last send seqno save new
716 * seqno for bidirectional check */
717 if (has_directlink_flag &&
718 compare_eth(if_incoming->net_dev->dev_addr,
719 batman_packet->orig) &&
720 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
721 offset = if_incoming->if_num * NUM_WORDS;
723 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
724 word = &(orig_neigh_node->bcast_own[offset]);
726 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
727 bit_packet_count(word);
728 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
731 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
732 "originator packet from myself (via neighbor)\n");
733 orig_node_free_ref(orig_neigh_node);
738 bat_dbg(DBG_BATMAN, bat_priv,
739 "Drop packet: ignoring all rebroadcast echos (sender: "
740 "%pM)\n", ethhdr->h_source);
744 orig_node = get_orig_node(bat_priv, batman_packet->orig);
748 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
750 if (is_duplicate == -1) {
751 bat_dbg(DBG_BATMAN, bat_priv,
752 "Drop packet: packet within seqno protection time "
753 "(sender: %pM)\n", ethhdr->h_source);
757 if (batman_packet->tq == 0) {
758 bat_dbg(DBG_BATMAN, bat_priv,
759 "Drop packet: originator packet with tq equal 0\n");
763 /* avoid temporary routing loops */
764 if ((orig_node->router) &&
765 (orig_node->router->orig_node->router) &&
766 (compare_eth(orig_node->router->addr,
767 batman_packet->prev_sender)) &&
768 !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
769 (compare_eth(orig_node->router->addr,
770 orig_node->router->orig_node->router->addr))) {
771 bat_dbg(DBG_BATMAN, bat_priv,
772 "Drop packet: ignoring all rebroadcast packets that "
773 "may make me loop (sender: %pM)\n", ethhdr->h_source);
777 /* if sender is a direct neighbor the sender mac equals
779 orig_neigh_node = (is_single_hop_neigh ?
781 get_orig_node(bat_priv, ethhdr->h_source));
782 if (!orig_neigh_node)
785 /* drop packet if sender is not a direct neighbor and if we
786 * don't route towards it */
787 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
788 bat_dbg(DBG_BATMAN, bat_priv,
789 "Drop packet: OGM via unknown neighbor!\n");
793 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
794 batman_packet, if_incoming);
796 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
798 /* update ranking if it is not a duplicate or has the same
799 * seqno and similar ttl as the non-duplicate */
800 if (is_bidirectional &&
802 ((orig_node->last_real_seqno == batman_packet->seqno) &&
803 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
804 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
805 if_incoming, hna_buff, hna_buff_len, is_duplicate);
807 /* is single hop (direct) neighbor */
808 if (is_single_hop_neigh) {
810 /* mark direct link on incoming interface */
811 schedule_forward_packet(orig_node, ethhdr, batman_packet,
812 1, hna_buff_len, if_incoming);
814 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
815 "rebroadcast neighbor packet with direct link flag\n");
819 /* multihop originator */
820 if (!is_bidirectional) {
821 bat_dbg(DBG_BATMAN, bat_priv,
822 "Drop packet: not received via bidirectional link\n");
827 bat_dbg(DBG_BATMAN, bat_priv,
828 "Drop packet: duplicate packet received\n");
832 bat_dbg(DBG_BATMAN, bat_priv,
833 "Forwarding packet: rebroadcast originator packet\n");
834 schedule_forward_packet(orig_node, ethhdr, batman_packet,
835 0, hna_buff_len, if_incoming);
838 if ((orig_neigh_node) && (!is_single_hop_neigh))
839 orig_node_free_ref(orig_neigh_node);
841 orig_node_free_ref(orig_node);
844 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
846 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
847 struct ethhdr *ethhdr;
849 /* drop packet if it has not necessary minimum size */
850 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
853 ethhdr = (struct ethhdr *)skb_mac_header(skb);
855 /* packet with broadcast indication but unicast recipient */
856 if (!is_broadcast_ether_addr(ethhdr->h_dest))
859 /* packet with broadcast sender address */
860 if (is_broadcast_ether_addr(ethhdr->h_source))
863 /* create a copy of the skb, if needed, to modify it. */
864 if (skb_cow(skb, 0) < 0)
867 /* keep skb linear */
868 if (skb_linearize(skb) < 0)
871 ethhdr = (struct ethhdr *)skb_mac_header(skb);
873 spin_lock_bh(&bat_priv->orig_hash_lock);
874 receive_aggr_bat_packet(ethhdr,
878 spin_unlock_bh(&bat_priv->orig_hash_lock);
881 return NET_RX_SUCCESS;
884 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
885 struct sk_buff *skb, size_t icmp_len)
887 struct orig_node *orig_node = NULL;
888 struct neigh_node *neigh_node = NULL;
889 struct icmp_packet_rr *icmp_packet;
890 struct batman_if *batman_if;
891 uint8_t dstaddr[ETH_ALEN];
892 int ret = NET_RX_DROP;
894 icmp_packet = (struct icmp_packet_rr *)skb->data;
896 /* add data to device queue */
897 if (icmp_packet->msg_type != ECHO_REQUEST) {
898 bat_socket_receive_packet(icmp_packet, icmp_len);
902 if (!bat_priv->primary_if)
905 /* answer echo request (ping) */
906 /* get routing information */
907 spin_lock_bh(&bat_priv->orig_hash_lock);
909 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
914 neigh_node = orig_node->router;
919 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
926 /* don't lock while sending the packets ... we therefore
927 * copy the required data before sending */
928 batman_if = orig_node->router->if_incoming;
929 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
930 spin_unlock_bh(&bat_priv->orig_hash_lock);
932 /* create a copy of the skb, if needed, to modify it. */
933 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
936 icmp_packet = (struct icmp_packet_rr *)skb->data;
938 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
939 memcpy(icmp_packet->orig,
940 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
941 icmp_packet->msg_type = ECHO_REPLY;
942 icmp_packet->ttl = TTL;
944 send_skb_packet(skb, batman_if, dstaddr);
945 ret = NET_RX_SUCCESS;
950 spin_unlock_bh(&bat_priv->orig_hash_lock);
953 neigh_node_free_ref(neigh_node);
955 orig_node_free_ref(orig_node);
959 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
962 struct orig_node *orig_node = NULL;
963 struct neigh_node *neigh_node = NULL;
964 struct icmp_packet *icmp_packet;
965 struct batman_if *batman_if;
966 uint8_t dstaddr[ETH_ALEN];
967 int ret = NET_RX_DROP;
969 icmp_packet = (struct icmp_packet *)skb->data;
971 /* send TTL exceeded if packet is an echo request (traceroute) */
972 if (icmp_packet->msg_type != ECHO_REQUEST) {
973 pr_debug("Warning - can't forward icmp packet from %pM to "
974 "%pM: ttl exceeded\n", icmp_packet->orig,
979 if (!bat_priv->primary_if)
982 /* get routing information */
983 spin_lock_bh(&bat_priv->orig_hash_lock);
985 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
990 neigh_node = orig_node->router;
995 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
1002 /* don't lock while sending the packets ... we therefore
1003 * copy the required data before sending */
1004 batman_if = orig_node->router->if_incoming;
1005 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
1006 spin_unlock_bh(&bat_priv->orig_hash_lock);
1008 /* create a copy of the skb, if needed, to modify it. */
1009 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1012 icmp_packet = (struct icmp_packet *)skb->data;
1014 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
1015 memcpy(icmp_packet->orig,
1016 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
1017 icmp_packet->msg_type = TTL_EXCEEDED;
1018 icmp_packet->ttl = TTL;
1020 send_skb_packet(skb, batman_if, dstaddr);
1021 ret = NET_RX_SUCCESS;
1026 spin_unlock_bh(&bat_priv->orig_hash_lock);
1029 neigh_node_free_ref(neigh_node);
1031 orig_node_free_ref(orig_node);
1036 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
1038 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1039 struct icmp_packet_rr *icmp_packet;
1040 struct ethhdr *ethhdr;
1041 struct orig_node *orig_node = NULL;
1042 struct neigh_node *neigh_node = NULL;
1043 struct batman_if *batman_if;
1044 int hdr_size = sizeof(struct icmp_packet);
1045 uint8_t dstaddr[ETH_ALEN];
1046 int ret = NET_RX_DROP;
1049 * we truncate all incoming icmp packets if they don't match our size
1051 if (skb->len >= sizeof(struct icmp_packet_rr))
1052 hdr_size = sizeof(struct icmp_packet_rr);
1054 /* drop packet if it has not necessary minimum size */
1055 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1058 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1060 /* packet with unicast indication but broadcast recipient */
1061 if (is_broadcast_ether_addr(ethhdr->h_dest))
1064 /* packet with broadcast sender address */
1065 if (is_broadcast_ether_addr(ethhdr->h_source))
1069 if (!is_my_mac(ethhdr->h_dest))
1072 icmp_packet = (struct icmp_packet_rr *)skb->data;
1074 /* add record route information if not full */
1075 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1076 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1077 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1078 ethhdr->h_dest, ETH_ALEN);
1079 icmp_packet->rr_cur++;
1083 if (is_my_mac(icmp_packet->dst))
1084 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1087 if (icmp_packet->ttl < 2)
1088 return recv_icmp_ttl_exceeded(bat_priv, skb);
1090 /* get routing information */
1091 spin_lock_bh(&bat_priv->orig_hash_lock);
1093 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
1098 neigh_node = orig_node->router;
1103 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
1110 /* don't lock while sending the packets ... we therefore
1111 * copy the required data before sending */
1112 batman_if = orig_node->router->if_incoming;
1113 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
1114 spin_unlock_bh(&bat_priv->orig_hash_lock);
1116 /* create a copy of the skb, if needed, to modify it. */
1117 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1120 icmp_packet = (struct icmp_packet_rr *)skb->data;
1126 send_skb_packet(skb, batman_if, dstaddr);
1127 ret = NET_RX_SUCCESS;
1132 spin_unlock_bh(&bat_priv->orig_hash_lock);
1135 neigh_node_free_ref(neigh_node);
1137 orig_node_free_ref(orig_node);
1141 /* find a suitable router for this originator, and use
1142 * bonding if possible. increases the found neighbors
1144 struct neigh_node *find_router(struct bat_priv *bat_priv,
1145 struct orig_node *orig_node,
1146 struct batman_if *recv_if)
1148 struct orig_node *primary_orig_node;
1149 struct orig_node *router_orig;
1150 struct neigh_node *router, *first_candidate, *tmp_neigh_node;
1151 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1152 int bonding_enabled;
1157 if (!orig_node->router)
1160 /* without bonding, the first node should
1161 * always choose the default router. */
1162 bonding_enabled = atomic_read(&bat_priv->bonding);
1165 /* select default router to output */
1166 router = orig_node->router;
1167 router_orig = orig_node->router->orig_node;
1168 if (!router_orig || !atomic_inc_not_zero(&router->refcount)) {
1173 if ((!recv_if) && (!bonding_enabled))
1176 /* if we have something in the primary_addr, we can search
1177 * for a potential bonding candidate. */
1178 if (compare_eth(router_orig->primary_addr, zero_mac))
1181 /* find the orig_node which has the primary interface. might
1182 * even be the same as our router_orig in many cases */
1184 if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
1185 primary_orig_node = router_orig;
1187 primary_orig_node = orig_hash_find(bat_priv,
1188 router_orig->primary_addr);
1189 if (!primary_orig_node)
1192 orig_node_free_ref(primary_orig_node);
1195 /* with less than 2 candidates, we can't do any
1196 * bonding and prefer the original router. */
1197 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1201 /* all nodes between should choose a candidate which
1202 * is is not on the interface where the packet came
1205 neigh_node_free_ref(router);
1206 first_candidate = NULL;
1209 if (bonding_enabled) {
1210 /* in the bonding case, send the packets in a round
1211 * robin fashion over the remaining interfaces. */
1213 list_for_each_entry_rcu(tmp_neigh_node,
1214 &primary_orig_node->bond_list, bonding_list) {
1215 if (!first_candidate)
1216 first_candidate = tmp_neigh_node;
1217 /* recv_if == NULL on the first node. */
1218 if (tmp_neigh_node->if_incoming != recv_if &&
1219 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
1220 router = tmp_neigh_node;
1225 /* use the first candidate if nothing was found. */
1226 if (!router && first_candidate &&
1227 atomic_inc_not_zero(&first_candidate->refcount))
1228 router = first_candidate;
1235 /* selected should point to the next element
1236 * after the current router */
1237 spin_lock_bh(&primary_orig_node->neigh_list_lock);
1238 /* this is a list_move(), which unfortunately
1239 * does not exist as rcu version */
1240 list_del_rcu(&primary_orig_node->bond_list);
1241 list_add_rcu(&primary_orig_node->bond_list,
1242 &router->bonding_list);
1243 spin_unlock_bh(&primary_orig_node->neigh_list_lock);
1246 /* if bonding is disabled, use the best of the
1247 * remaining candidates which are not using
1248 * this interface. */
1249 list_for_each_entry_rcu(tmp_neigh_node,
1250 &primary_orig_node->bond_list, bonding_list) {
1251 if (!first_candidate)
1252 first_candidate = tmp_neigh_node;
1254 /* recv_if == NULL on the first node. */
1255 if (tmp_neigh_node->if_incoming == recv_if)
1258 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1261 /* if we don't have a router yet
1262 * or this one is better, choose it. */
1264 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1265 /* decrement refcount of
1266 * previously selected router */
1268 neigh_node_free_ref(router);
1270 router = tmp_neigh_node;
1271 atomic_inc_not_zero(&router->refcount);
1274 neigh_node_free_ref(tmp_neigh_node);
1277 /* use the first candidate if nothing was found. */
1278 if (!router && first_candidate &&
1279 atomic_inc_not_zero(&first_candidate->refcount))
1280 router = first_candidate;
1287 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1289 struct ethhdr *ethhdr;
1291 /* drop packet if it has not necessary minimum size */
1292 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1295 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1297 /* packet with unicast indication but broadcast recipient */
1298 if (is_broadcast_ether_addr(ethhdr->h_dest))
1301 /* packet with broadcast sender address */
1302 if (is_broadcast_ether_addr(ethhdr->h_source))
1306 if (!is_my_mac(ethhdr->h_dest))
1312 int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1315 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1316 struct orig_node *orig_node = NULL;
1317 struct neigh_node *neigh_node = NULL;
1318 struct batman_if *batman_if;
1319 uint8_t dstaddr[ETH_ALEN];
1320 struct unicast_packet *unicast_packet;
1321 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1322 int ret = NET_RX_DROP;
1323 struct sk_buff *new_skb;
1325 unicast_packet = (struct unicast_packet *)skb->data;
1328 if (unicast_packet->ttl < 2) {
1329 pr_debug("Warning - can't forward unicast packet from %pM to "
1330 "%pM: ttl exceeded\n", ethhdr->h_source,
1331 unicast_packet->dest);
1335 /* get routing information */
1336 spin_lock_bh(&bat_priv->orig_hash_lock);
1338 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1345 /* find_router() increases neigh_nodes refcount if found. */
1346 neigh_node = find_router(bat_priv, orig_node, recv_if);
1349 spin_unlock_bh(&bat_priv->orig_hash_lock);
1353 /* don't lock while sending the packets ... we therefore
1354 * copy the required data before sending */
1355 batman_if = neigh_node->if_incoming;
1356 memcpy(dstaddr, neigh_node->addr, ETH_ALEN);
1357 spin_unlock_bh(&bat_priv->orig_hash_lock);
1359 /* create a copy of the skb, if needed, to modify it. */
1360 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1363 unicast_packet = (struct unicast_packet *)skb->data;
1365 if (unicast_packet->packet_type == BAT_UNICAST &&
1366 atomic_read(&bat_priv->fragmentation) &&
1367 skb->len > batman_if->net_dev->mtu)
1368 return frag_send_skb(skb, bat_priv, batman_if,
1371 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1372 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
1374 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1376 if (ret == NET_RX_DROP)
1379 /* packet was buffered for late merge */
1381 ret = NET_RX_SUCCESS;
1386 unicast_packet = (struct unicast_packet *)skb->data;
1390 unicast_packet->ttl--;
1393 send_skb_packet(skb, batman_if, dstaddr);
1394 ret = NET_RX_SUCCESS;
1399 spin_unlock_bh(&bat_priv->orig_hash_lock);
1402 neigh_node_free_ref(neigh_node);
1404 orig_node_free_ref(orig_node);
1408 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1410 struct unicast_packet *unicast_packet;
1411 int hdr_size = sizeof(struct unicast_packet);
1413 if (check_unicast_packet(skb, hdr_size) < 0)
1416 unicast_packet = (struct unicast_packet *)skb->data;
1419 if (is_my_mac(unicast_packet->dest)) {
1420 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1421 return NET_RX_SUCCESS;
1424 return route_unicast_packet(skb, recv_if, hdr_size);
1427 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1429 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1430 struct unicast_frag_packet *unicast_packet;
1431 int hdr_size = sizeof(struct unicast_frag_packet);
1432 struct sk_buff *new_skb = NULL;
1435 if (check_unicast_packet(skb, hdr_size) < 0)
1438 unicast_packet = (struct unicast_frag_packet *)skb->data;
1441 if (is_my_mac(unicast_packet->dest)) {
1443 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1445 if (ret == NET_RX_DROP)
1448 /* packet was buffered for late merge */
1450 return NET_RX_SUCCESS;
1452 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1453 sizeof(struct unicast_packet));
1454 return NET_RX_SUCCESS;
1457 return route_unicast_packet(skb, recv_if, hdr_size);
1461 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1463 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1464 struct orig_node *orig_node = NULL;
1465 struct bcast_packet *bcast_packet;
1466 struct ethhdr *ethhdr;
1467 int hdr_size = sizeof(struct bcast_packet);
1468 int ret = NET_RX_DROP;
1471 /* drop packet if it has not necessary minimum size */
1472 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1475 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1477 /* packet with broadcast indication but unicast recipient */
1478 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1481 /* packet with broadcast sender address */
1482 if (is_broadcast_ether_addr(ethhdr->h_source))
1485 /* ignore broadcasts sent by myself */
1486 if (is_my_mac(ethhdr->h_source))
1489 bcast_packet = (struct bcast_packet *)skb->data;
1491 /* ignore broadcasts originated by myself */
1492 if (is_my_mac(bcast_packet->orig))
1495 if (bcast_packet->ttl < 2)
1498 spin_lock_bh(&bat_priv->orig_hash_lock);
1500 orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
1507 spin_lock_bh(&orig_node->bcast_seqno_lock);
1509 /* check whether the packet is a duplicate */
1510 if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1511 ntohl(bcast_packet->seqno)))
1514 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1516 /* check whether the packet is old and the host just restarted. */
1517 if (window_protected(bat_priv, seq_diff,
1518 &orig_node->bcast_seqno_reset))
1521 /* mark broadcast in flood history, update window position
1523 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1524 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1526 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1527 spin_unlock_bh(&bat_priv->orig_hash_lock);
1529 /* rebroadcast packet */
1530 add_bcast_packet_to_list(bat_priv, skb);
1532 /* broadcast for me */
1533 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1534 ret = NET_RX_SUCCESS;
1539 spin_unlock_bh(&bat_priv->orig_hash_lock);
1542 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1543 spin_unlock_bh(&bat_priv->orig_hash_lock);
1546 orig_node_free_ref(orig_node);
1550 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1552 struct vis_packet *vis_packet;
1553 struct ethhdr *ethhdr;
1554 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1555 int hdr_size = sizeof(struct vis_packet);
1557 /* keep skb linear */
1558 if (skb_linearize(skb) < 0)
1561 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1564 vis_packet = (struct vis_packet *)skb->data;
1565 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1568 if (!is_my_mac(ethhdr->h_dest))
1571 /* ignore own packets */
1572 if (is_my_mac(vis_packet->vis_orig))
1575 if (is_my_mac(vis_packet->sender_orig))
1578 switch (vis_packet->vis_type) {
1579 case VIS_TYPE_SERVER_SYNC:
1580 receive_server_sync_packet(bat_priv, vis_packet,
1584 case VIS_TYPE_CLIENT_UPDATE:
1585 receive_client_update_packet(bat_priv, vis_packet,
1589 default: /* ignore unknown packet */
1593 /* We take a copy of the data in the packet, so we should
1594 always free the skbuf. */