1 /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "soft-interface.h"
24 #include "hard-interface.h"
25 #include "icmp_socket.h"
26 #include "translation-table.h"
27 #include "originator.h"
30 #include "bridge_loop_avoidance.h"
31 #include "distributed-arp-table.h"
32 #include "network-coding.h"
34 static int batadv_route_unicast_packet(struct sk_buff
*skb
,
35 struct batadv_hard_iface
*recv_if
);
37 void batadv_slide_own_bcast_window(struct batadv_hard_iface
*hard_iface
)
39 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
40 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
41 struct hlist_head
*head
;
42 struct batadv_orig_node
*orig_node
;
48 for (i
= 0; i
< hash
->size
; i
++) {
49 head
= &hash
->table
[i
];
52 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
53 spin_lock_bh(&orig_node
->ogm_cnt_lock
);
54 word_index
= hard_iface
->if_num
* BATADV_NUM_WORDS
;
55 word
= &(orig_node
->bcast_own
[word_index
]);
57 batadv_bit_get_packet(bat_priv
, word
, 1, 0);
58 w
= &orig_node
->bcast_own_sum
[hard_iface
->if_num
];
59 *w
= bitmap_weight(word
, BATADV_TQ_LOCAL_WINDOW_SIZE
);
60 spin_unlock_bh(&orig_node
->ogm_cnt_lock
);
66 static void _batadv_update_route(struct batadv_priv
*bat_priv
,
67 struct batadv_orig_node
*orig_node
,
68 struct batadv_neigh_node
*neigh_node
)
70 struct batadv_neigh_node
*curr_router
;
72 curr_router
= batadv_orig_node_get_router(orig_node
);
75 if ((curr_router
) && (!neigh_node
)) {
76 batadv_dbg(BATADV_DBG_ROUTES
, bat_priv
,
77 "Deleting route towards: %pM\n", orig_node
->orig
);
78 batadv_tt_global_del_orig(bat_priv
, orig_node
,
79 "Deleted route towards originator");
82 } else if ((!curr_router
) && (neigh_node
)) {
83 batadv_dbg(BATADV_DBG_ROUTES
, bat_priv
,
84 "Adding route towards: %pM (via %pM)\n",
85 orig_node
->orig
, neigh_node
->addr
);
87 } else if (neigh_node
&& curr_router
) {
88 batadv_dbg(BATADV_DBG_ROUTES
, bat_priv
,
89 "Changing route towards: %pM (now via %pM - was via %pM)\n",
90 orig_node
->orig
, neigh_node
->addr
,
95 batadv_neigh_node_free_ref(curr_router
);
97 /* increase refcount of new best neighbor */
98 if (neigh_node
&& !atomic_inc_not_zero(&neigh_node
->refcount
))
101 spin_lock_bh(&orig_node
->neigh_list_lock
);
102 rcu_assign_pointer(orig_node
->router
, neigh_node
);
103 spin_unlock_bh(&orig_node
->neigh_list_lock
);
105 /* decrease refcount of previous best neighbor */
107 batadv_neigh_node_free_ref(curr_router
);
110 void batadv_update_route(struct batadv_priv
*bat_priv
,
111 struct batadv_orig_node
*orig_node
,
112 struct batadv_neigh_node
*neigh_node
)
114 struct batadv_neigh_node
*router
= NULL
;
119 router
= batadv_orig_node_get_router(orig_node
);
121 if (router
!= neigh_node
)
122 _batadv_update_route(bat_priv
, orig_node
, neigh_node
);
126 batadv_neigh_node_free_ref(router
);
129 /* caller must hold the neigh_list_lock */
130 void batadv_bonding_candidate_del(struct batadv_orig_node
*orig_node
,
131 struct batadv_neigh_node
*neigh_node
)
133 /* this neighbor is not part of our candidate list */
134 if (list_empty(&neigh_node
->bonding_list
))
137 list_del_rcu(&neigh_node
->bonding_list
);
138 INIT_LIST_HEAD(&neigh_node
->bonding_list
);
139 batadv_neigh_node_free_ref(neigh_node
);
140 atomic_dec(&orig_node
->bond_candidates
);
146 void batadv_bonding_candidate_add(struct batadv_orig_node
*orig_node
,
147 struct batadv_neigh_node
*neigh_node
)
149 struct batadv_neigh_node
*tmp_neigh_node
, *router
= NULL
;
150 uint8_t interference_candidate
= 0;
152 spin_lock_bh(&orig_node
->neigh_list_lock
);
154 /* only consider if it has the same primary address ... */
155 if (!batadv_compare_eth(orig_node
->orig
,
156 neigh_node
->orig_node
->primary_addr
))
159 router
= batadv_orig_node_get_router(orig_node
);
163 /* ... and is good enough to be considered */
164 if (neigh_node
->tq_avg
< router
->tq_avg
- BATADV_BONDING_TQ_THRESHOLD
)
167 /* check if we have another candidate with the same mac address or
168 * interface. If we do, we won't select this candidate because of
169 * possible interference.
171 hlist_for_each_entry_rcu(tmp_neigh_node
,
172 &orig_node
->neigh_list
, list
) {
173 if (tmp_neigh_node
== neigh_node
)
176 /* we only care if the other candidate is even
177 * considered as candidate.
179 if (list_empty(&tmp_neigh_node
->bonding_list
))
182 if ((neigh_node
->if_incoming
== tmp_neigh_node
->if_incoming
) ||
183 (batadv_compare_eth(neigh_node
->addr
,
184 tmp_neigh_node
->addr
))) {
185 interference_candidate
= 1;
190 /* don't care further if it is an interference candidate */
191 if (interference_candidate
)
194 /* this neighbor already is part of our candidate list */
195 if (!list_empty(&neigh_node
->bonding_list
))
198 if (!atomic_inc_not_zero(&neigh_node
->refcount
))
201 list_add_rcu(&neigh_node
->bonding_list
, &orig_node
->bond_list
);
202 atomic_inc(&orig_node
->bond_candidates
);
206 batadv_bonding_candidate_del(orig_node
, neigh_node
);
209 spin_unlock_bh(&orig_node
->neigh_list_lock
);
212 batadv_neigh_node_free_ref(router
);
215 /* copy primary address for bonding */
217 batadv_bonding_save_primary(const struct batadv_orig_node
*orig_node
,
218 struct batadv_orig_node
*orig_neigh_node
,
219 const struct batadv_ogm_packet
*batman_ogm_packet
)
221 if (!(batman_ogm_packet
->flags
& BATADV_PRIMARIES_FIRST_HOP
))
224 memcpy(orig_neigh_node
->primary_addr
, orig_node
->orig
, ETH_ALEN
);
227 /* checks whether the host restarted and is in the protection time.
229 * 0 if the packet is to be accepted
230 * 1 if the packet is to be ignored.
232 int batadv_window_protected(struct batadv_priv
*bat_priv
, int32_t seq_num_diff
,
233 unsigned long *last_reset
)
235 if (seq_num_diff
<= -BATADV_TQ_LOCAL_WINDOW_SIZE
||
236 seq_num_diff
>= BATADV_EXPECTED_SEQNO_RANGE
) {
237 if (!batadv_has_timed_out(*last_reset
,
238 BATADV_RESET_PROTECTION_MS
))
241 *last_reset
= jiffies
;
242 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
243 "old packet received, start protection\n");
249 bool batadv_check_management_packet(struct sk_buff
*skb
,
250 struct batadv_hard_iface
*hard_iface
,
253 struct ethhdr
*ethhdr
;
255 /* drop packet if it has not necessary minimum size */
256 if (unlikely(!pskb_may_pull(skb
, header_len
)))
259 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
261 /* packet with broadcast indication but unicast recipient */
262 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
265 /* packet with broadcast sender address */
266 if (is_broadcast_ether_addr(ethhdr
->h_source
))
269 /* create a copy of the skb, if needed, to modify it. */
270 if (skb_cow(skb
, 0) < 0)
273 /* keep skb linear */
274 if (skb_linearize(skb
) < 0)
280 static int batadv_recv_my_icmp_packet(struct batadv_priv
*bat_priv
,
281 struct sk_buff
*skb
, size_t icmp_len
)
283 struct batadv_hard_iface
*primary_if
= NULL
;
284 struct batadv_orig_node
*orig_node
= NULL
;
285 struct batadv_icmp_packet_rr
*icmp_packet
;
286 int ret
= NET_RX_DROP
;
288 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
290 /* add data to device queue */
291 if (icmp_packet
->msg_type
!= BATADV_ECHO_REQUEST
) {
292 batadv_socket_receive_packet(icmp_packet
, icmp_len
);
296 primary_if
= batadv_primary_if_get_selected(bat_priv
);
300 /* answer echo request (ping) */
301 /* get routing information */
302 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->orig
);
306 /* create a copy of the skb, if needed, to modify it. */
307 if (skb_cow(skb
, ETH_HLEN
) < 0)
310 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
312 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
313 memcpy(icmp_packet
->orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
314 icmp_packet
->msg_type
= BATADV_ECHO_REPLY
;
315 icmp_packet
->header
.ttl
= BATADV_TTL
;
317 if (batadv_send_skb_to_orig(skb
, orig_node
, NULL
))
318 ret
= NET_RX_SUCCESS
;
322 batadv_hardif_free_ref(primary_if
);
324 batadv_orig_node_free_ref(orig_node
);
328 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv
*bat_priv
,
331 struct batadv_hard_iface
*primary_if
= NULL
;
332 struct batadv_orig_node
*orig_node
= NULL
;
333 struct batadv_icmp_packet
*icmp_packet
;
334 int ret
= NET_RX_DROP
;
336 icmp_packet
= (struct batadv_icmp_packet
*)skb
->data
;
338 /* send TTL exceeded if packet is an echo request (traceroute) */
339 if (icmp_packet
->msg_type
!= BATADV_ECHO_REQUEST
) {
340 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
341 icmp_packet
->orig
, icmp_packet
->dst
);
345 primary_if
= batadv_primary_if_get_selected(bat_priv
);
349 /* get routing information */
350 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->orig
);
354 /* create a copy of the skb, if needed, to modify it. */
355 if (skb_cow(skb
, ETH_HLEN
) < 0)
358 icmp_packet
= (struct batadv_icmp_packet
*)skb
->data
;
360 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
361 memcpy(icmp_packet
->orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
362 icmp_packet
->msg_type
= BATADV_TTL_EXCEEDED
;
363 icmp_packet
->header
.ttl
= BATADV_TTL
;
365 if (batadv_send_skb_to_orig(skb
, orig_node
, NULL
))
366 ret
= NET_RX_SUCCESS
;
370 batadv_hardif_free_ref(primary_if
);
372 batadv_orig_node_free_ref(orig_node
);
377 int batadv_recv_icmp_packet(struct sk_buff
*skb
,
378 struct batadv_hard_iface
*recv_if
)
380 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
381 struct batadv_icmp_packet_rr
*icmp_packet
;
382 struct ethhdr
*ethhdr
;
383 struct batadv_orig_node
*orig_node
= NULL
;
384 int hdr_size
= sizeof(struct batadv_icmp_packet
);
385 int ret
= NET_RX_DROP
;
387 /* we truncate all incoming icmp packets if they don't match our size */
388 if (skb
->len
>= sizeof(struct batadv_icmp_packet_rr
))
389 hdr_size
= sizeof(struct batadv_icmp_packet_rr
);
391 /* drop packet if it has not necessary minimum size */
392 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
395 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
397 /* packet with unicast indication but broadcast recipient */
398 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
401 /* packet with broadcast sender address */
402 if (is_broadcast_ether_addr(ethhdr
->h_source
))
406 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
409 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
411 /* add record route information if not full */
412 if ((hdr_size
== sizeof(struct batadv_icmp_packet_rr
)) &&
413 (icmp_packet
->rr_cur
< BATADV_RR_LEN
)) {
414 memcpy(&(icmp_packet
->rr
[icmp_packet
->rr_cur
]),
415 ethhdr
->h_dest
, ETH_ALEN
);
416 icmp_packet
->rr_cur
++;
420 if (batadv_is_my_mac(bat_priv
, icmp_packet
->dst
))
421 return batadv_recv_my_icmp_packet(bat_priv
, skb
, hdr_size
);
424 if (icmp_packet
->header
.ttl
< 2)
425 return batadv_recv_icmp_ttl_exceeded(bat_priv
, skb
);
427 /* get routing information */
428 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->dst
);
432 /* create a copy of the skb, if needed, to modify it. */
433 if (skb_cow(skb
, ETH_HLEN
) < 0)
436 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
439 icmp_packet
->header
.ttl
--;
442 if (batadv_send_skb_to_orig(skb
, orig_node
, recv_if
))
443 ret
= NET_RX_SUCCESS
;
447 batadv_orig_node_free_ref(orig_node
);
451 /* In the bonding case, send the packets in a round
452 * robin fashion over the remaining interfaces.
454 * This method rotates the bonding list and increases the
455 * returned router's refcount.
457 static struct batadv_neigh_node
*
458 batadv_find_bond_router(struct batadv_orig_node
*primary_orig
,
459 const struct batadv_hard_iface
*recv_if
)
461 struct batadv_neigh_node
*tmp_neigh_node
;
462 struct batadv_neigh_node
*router
= NULL
, *first_candidate
= NULL
;
465 list_for_each_entry_rcu(tmp_neigh_node
, &primary_orig
->bond_list
,
467 if (!first_candidate
)
468 first_candidate
= tmp_neigh_node
;
470 /* recv_if == NULL on the first node. */
471 if (tmp_neigh_node
->if_incoming
== recv_if
)
474 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
477 router
= tmp_neigh_node
;
481 /* use the first candidate if nothing was found. */
482 if (!router
&& first_candidate
&&
483 atomic_inc_not_zero(&first_candidate
->refcount
))
484 router
= first_candidate
;
489 /* selected should point to the next element
490 * after the current router
492 spin_lock_bh(&primary_orig
->neigh_list_lock
);
493 /* this is a list_move(), which unfortunately
494 * does not exist as rcu version
496 list_del_rcu(&primary_orig
->bond_list
);
497 list_add_rcu(&primary_orig
->bond_list
,
498 &router
->bonding_list
);
499 spin_unlock_bh(&primary_orig
->neigh_list_lock
);
506 /* Interface Alternating: Use the best of the
507 * remaining candidates which are not using
510 * Increases the returned router's refcount
512 static struct batadv_neigh_node
*
513 batadv_find_ifalter_router(struct batadv_orig_node
*primary_orig
,
514 const struct batadv_hard_iface
*recv_if
)
516 struct batadv_neigh_node
*tmp_neigh_node
;
517 struct batadv_neigh_node
*router
= NULL
, *first_candidate
= NULL
;
520 list_for_each_entry_rcu(tmp_neigh_node
, &primary_orig
->bond_list
,
522 if (!first_candidate
)
523 first_candidate
= tmp_neigh_node
;
525 /* recv_if == NULL on the first node. */
526 if (tmp_neigh_node
->if_incoming
== recv_if
)
529 if (router
&& tmp_neigh_node
->tq_avg
<= router
->tq_avg
)
532 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
535 /* decrement refcount of previously selected router */
537 batadv_neigh_node_free_ref(router
);
539 /* we found a better router (or at least one valid router) */
540 router
= tmp_neigh_node
;
543 /* use the first candidate if nothing was found. */
544 if (!router
&& first_candidate
&&
545 atomic_inc_not_zero(&first_candidate
->refcount
))
546 router
= first_candidate
;
553 * batadv_check_unicast_packet - Check for malformed unicast packets
554 * @bat_priv: the bat priv with all the soft interface information
555 * @skb: packet to check
556 * @hdr_size: size of header to pull
558 * Check for short header and bad addresses in given packet. Returns negative
559 * value when check fails and 0 otherwise. The negative value depends on the
560 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
561 * and -EREMOTE for non-local (other host) destination.
563 static int batadv_check_unicast_packet(struct batadv_priv
*bat_priv
,
564 struct sk_buff
*skb
, int hdr_size
)
566 struct ethhdr
*ethhdr
;
568 /* drop packet if it has not necessary minimum size */
569 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
572 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
574 /* packet with unicast indication but broadcast recipient */
575 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
578 /* packet with broadcast sender address */
579 if (is_broadcast_ether_addr(ethhdr
->h_source
))
583 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
589 int batadv_recv_tt_query(struct sk_buff
*skb
, struct batadv_hard_iface
*recv_if
)
591 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
592 struct batadv_tt_query_packet
*tt_query
;
594 int hdr_size
= sizeof(*tt_query
);
598 if (batadv_check_unicast_packet(bat_priv
, skb
, hdr_size
) < 0)
601 /* I could need to modify it */
602 if (skb_cow(skb
, sizeof(struct batadv_tt_query_packet
)) < 0)
605 tt_query
= (struct batadv_tt_query_packet
*)skb
->data
;
607 switch (tt_query
->flags
& BATADV_TT_QUERY_TYPE_MASK
) {
608 case BATADV_TT_REQUEST
:
609 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
611 /* If we cannot provide an answer the tt_request is
614 if (!batadv_send_tt_response(bat_priv
, tt_query
)) {
615 if (tt_query
->flags
& BATADV_TT_FULL_TABLE
)
620 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
621 "Routing TT_REQUEST to %pM [%c]\n",
624 return batadv_route_unicast_packet(skb
, recv_if
);
627 case BATADV_TT_RESPONSE
:
628 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
630 if (batadv_is_my_mac(bat_priv
, tt_query
->dst
)) {
631 /* packet needs to be linearized to access the TT
634 if (skb_linearize(skb
) < 0)
636 /* skb_linearize() possibly changed skb->data */
637 tt_query
= (struct batadv_tt_query_packet
*)skb
->data
;
639 tt_size
= batadv_tt_len(ntohs(tt_query
->tt_data
));
641 /* Ensure we have all the claimed data */
642 packet_size
= sizeof(struct batadv_tt_query_packet
);
643 packet_size
+= tt_size
;
644 if (unlikely(skb_headlen(skb
) < packet_size
))
647 batadv_handle_tt_response(bat_priv
, tt_query
);
649 if (tt_query
->flags
& BATADV_TT_FULL_TABLE
)
653 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
654 "Routing TT_RESPONSE to %pM [%c]\n",
657 return batadv_route_unicast_packet(skb
, recv_if
);
663 /* returning NET_RX_DROP will make the caller function kfree the skb */
667 int batadv_recv_roam_adv(struct sk_buff
*skb
, struct batadv_hard_iface
*recv_if
)
669 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
670 struct batadv_roam_adv_packet
*roam_adv_packet
;
671 struct batadv_orig_node
*orig_node
;
673 if (batadv_check_unicast_packet(bat_priv
, skb
,
674 sizeof(*roam_adv_packet
)) < 0)
677 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
679 roam_adv_packet
= (struct batadv_roam_adv_packet
*)skb
->data
;
681 if (!batadv_is_my_mac(bat_priv
, roam_adv_packet
->dst
))
682 return batadv_route_unicast_packet(skb
, recv_if
);
684 /* check if it is a backbone gateway. we don't accept
685 * roaming advertisement from it, as it has the same
686 * entries as we have.
688 if (batadv_bla_is_backbone_gw_orig(bat_priv
, roam_adv_packet
->src
))
691 orig_node
= batadv_orig_hash_find(bat_priv
, roam_adv_packet
->src
);
695 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
696 "Received ROAMING_ADV from %pM (client %pM)\n",
697 roam_adv_packet
->src
, roam_adv_packet
->client
);
699 batadv_tt_global_add(bat_priv
, orig_node
, roam_adv_packet
->client
,
700 BATADV_TT_CLIENT_ROAM
,
701 atomic_read(&orig_node
->last_ttvn
) + 1);
703 batadv_orig_node_free_ref(orig_node
);
705 /* returning NET_RX_DROP will make the caller function kfree the skb */
709 /* find a suitable router for this originator, and use
710 * bonding if possible. increases the found neighbors
713 struct batadv_neigh_node
*
714 batadv_find_router(struct batadv_priv
*bat_priv
,
715 struct batadv_orig_node
*orig_node
,
716 const struct batadv_hard_iface
*recv_if
)
718 struct batadv_orig_node
*primary_orig_node
;
719 struct batadv_orig_node
*router_orig
;
720 struct batadv_neigh_node
*router
;
721 static uint8_t zero_mac
[ETH_ALEN
] = {0, 0, 0, 0, 0, 0};
723 uint8_t *primary_addr
;
728 router
= batadv_orig_node_get_router(orig_node
);
732 /* without bonding, the first node should
733 * always choose the default router.
735 bonding_enabled
= atomic_read(&bat_priv
->bonding
);
738 /* select default router to output */
739 router_orig
= router
->orig_node
;
743 if ((!recv_if
) && (!bonding_enabled
))
746 primary_addr
= router_orig
->primary_addr
;
748 /* if we have something in the primary_addr, we can search
749 * for a potential bonding candidate.
751 if (batadv_compare_eth(primary_addr
, zero_mac
))
754 /* find the orig_node which has the primary interface. might
755 * even be the same as our router_orig in many cases
757 if (batadv_compare_eth(primary_addr
, router_orig
->orig
)) {
758 primary_orig_node
= router_orig
;
760 primary_orig_node
= batadv_orig_hash_find(bat_priv
,
762 if (!primary_orig_node
)
765 batadv_orig_node_free_ref(primary_orig_node
);
768 /* with less than 2 candidates, we can't do any
769 * bonding and prefer the original router.
771 if (atomic_read(&primary_orig_node
->bond_candidates
) < 2)
774 /* all nodes between should choose a candidate which
775 * is is not on the interface where the packet came
778 batadv_neigh_node_free_ref(router
);
781 router
= batadv_find_bond_router(primary_orig_node
, recv_if
);
783 router
= batadv_find_ifalter_router(primary_orig_node
, recv_if
);
786 if (router
&& router
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
795 batadv_neigh_node_free_ref(router
);
799 static int batadv_route_unicast_packet(struct sk_buff
*skb
,
800 struct batadv_hard_iface
*recv_if
)
802 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
803 struct batadv_orig_node
*orig_node
= NULL
;
804 struct batadv_neigh_node
*neigh_node
= NULL
;
805 struct batadv_unicast_packet
*unicast_packet
;
806 struct ethhdr
*ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
807 int ret
= NET_RX_DROP
;
808 struct sk_buff
*new_skb
;
810 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
813 if (unicast_packet
->header
.ttl
< 2) {
814 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
815 ethhdr
->h_source
, unicast_packet
->dest
);
819 /* get routing information */
820 orig_node
= batadv_orig_hash_find(bat_priv
, unicast_packet
->dest
);
825 /* find_router() increases neigh_nodes refcount if found. */
826 neigh_node
= batadv_find_router(bat_priv
, orig_node
, recv_if
);
831 /* create a copy of the skb, if needed, to modify it. */
832 if (skb_cow(skb
, ETH_HLEN
) < 0)
835 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
837 if (unicast_packet
->header
.packet_type
== BATADV_UNICAST
&&
838 atomic_read(&bat_priv
->fragmentation
) &&
839 skb
->len
> neigh_node
->if_incoming
->net_dev
->mtu
) {
840 ret
= batadv_frag_send_skb(skb
, bat_priv
,
841 neigh_node
->if_incoming
,
846 if (unicast_packet
->header
.packet_type
== BATADV_UNICAST_FRAG
&&
847 batadv_frag_can_reassemble(skb
,
848 neigh_node
->if_incoming
->net_dev
->mtu
)) {
849 ret
= batadv_frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
851 if (ret
== NET_RX_DROP
)
854 /* packet was buffered for late merge */
856 ret
= NET_RX_SUCCESS
;
861 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
865 unicast_packet
->header
.ttl
--;
867 /* network code packet if possible */
868 if (batadv_nc_skb_forward(skb
, neigh_node
, ethhdr
)) {
869 ret
= NET_RX_SUCCESS
;
870 } else if (batadv_send_skb_to_orig(skb
, orig_node
, recv_if
)) {
871 ret
= NET_RX_SUCCESS
;
873 /* Update stats counter */
874 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
875 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
876 skb
->len
+ ETH_HLEN
);
881 batadv_neigh_node_free_ref(neigh_node
);
883 batadv_orig_node_free_ref(orig_node
);
888 * batadv_reroute_unicast_packet - update the unicast header for re-routing
889 * @bat_priv: the bat priv with all the soft interface information
890 * @unicast_packet: the unicast header to be updated
891 * @dst_addr: the payload destination
893 * Search the translation table for dst_addr and update the unicast header with
894 * the new corresponding information (originator address where the destination
895 * client currently is and its known TTVN)
897 * Returns true if the packet header has been updated, false otherwise
900 batadv_reroute_unicast_packet(struct batadv_priv
*bat_priv
,
901 struct batadv_unicast_packet
*unicast_packet
,
904 struct batadv_orig_node
*orig_node
= NULL
;
905 struct batadv_hard_iface
*primary_if
= NULL
;
907 uint8_t *orig_addr
, orig_ttvn
;
909 if (batadv_is_my_client(bat_priv
, dst_addr
)) {
910 primary_if
= batadv_primary_if_get_selected(bat_priv
);
913 orig_addr
= primary_if
->net_dev
->dev_addr
;
914 orig_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
916 orig_node
= batadv_transtable_search(bat_priv
, NULL
, dst_addr
);
920 if (batadv_compare_eth(orig_node
->orig
, unicast_packet
->dest
))
923 orig_addr
= orig_node
->orig
;
924 orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
927 /* update the packet header */
928 memcpy(unicast_packet
->dest
, orig_addr
, ETH_ALEN
);
929 unicast_packet
->ttvn
= orig_ttvn
;
934 batadv_hardif_free_ref(primary_if
);
936 batadv_orig_node_free_ref(orig_node
);
941 static int batadv_check_unicast_ttvn(struct batadv_priv
*bat_priv
,
942 struct sk_buff
*skb
, int hdr_len
) {
943 uint8_t curr_ttvn
, old_ttvn
;
944 struct batadv_orig_node
*orig_node
;
945 struct ethhdr
*ethhdr
;
946 struct batadv_hard_iface
*primary_if
;
947 struct batadv_unicast_packet
*unicast_packet
;
950 /* check if there is enough data before accessing it */
951 if (pskb_may_pull(skb
, hdr_len
+ ETH_HLEN
) < 0)
954 /* create a copy of the skb (in case of for re-routing) to modify it. */
955 if (skb_cow(skb
, sizeof(*unicast_packet
)) < 0)
958 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
959 ethhdr
= (struct ethhdr
*)(skb
->data
+ hdr_len
);
961 /* check if the destination client was served by this node and it is now
962 * roaming. In this case, it means that the node has got a ROAM_ADV
963 * message and that it knows the new destination in the mesh to re-route
966 if (batadv_tt_local_client_is_roaming(bat_priv
, ethhdr
->h_dest
)) {
967 if (batadv_reroute_unicast_packet(bat_priv
, unicast_packet
,
969 net_ratelimited_function(batadv_dbg
, BATADV_DBG_TT
,
971 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
972 unicast_packet
->dest
,
974 /* at this point the mesh destination should have been
975 * substituted with the originator address found in the global
976 * table. If not, let the packet go untouched anyway because
977 * there is nothing the node can do
982 /* retrieve the TTVN known by this node for the packet destination. This
983 * value is used later to check if the node which sent (or re-routed
984 * last time) the packet had an updated information or not
986 curr_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
987 if (!batadv_is_my_mac(bat_priv
, unicast_packet
->dest
)) {
988 orig_node
= batadv_orig_hash_find(bat_priv
,
989 unicast_packet
->dest
);
990 /* if it is not possible to find the orig_node representing the
991 * destination, the packet can immediately be dropped as it will
992 * not be possible to deliver it
997 curr_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
998 batadv_orig_node_free_ref(orig_node
);
1001 /* check if the TTVN contained in the packet is fresher than what the
1004 is_old_ttvn
= batadv_seq_before(unicast_packet
->ttvn
, curr_ttvn
);
1008 old_ttvn
= unicast_packet
->ttvn
;
1009 /* the packet was forged based on outdated network information. Its
1010 * destination can possibly be updated and forwarded towards the new
1013 if (batadv_reroute_unicast_packet(bat_priv
, unicast_packet
,
1015 net_ratelimited_function(batadv_dbg
, BATADV_DBG_TT
, bat_priv
,
1016 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
1017 unicast_packet
->dest
, ethhdr
->h_dest
,
1018 old_ttvn
, curr_ttvn
);
1022 /* the packet has not been re-routed: either the destination is
1023 * currently served by this node or there is no destination at all and
1024 * it is possible to drop the packet
1026 if (!batadv_is_my_client(bat_priv
, ethhdr
->h_dest
))
1029 /* update the header in order to let the packet be delivered to this
1030 * node's soft interface
1032 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1036 memcpy(unicast_packet
->dest
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1038 batadv_hardif_free_ref(primary_if
);
1040 unicast_packet
->ttvn
= curr_ttvn
;
1045 int batadv_recv_unicast_packet(struct sk_buff
*skb
,
1046 struct batadv_hard_iface
*recv_if
)
1048 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1049 struct batadv_unicast_packet
*unicast_packet
;
1050 struct batadv_unicast_4addr_packet
*unicast_4addr_packet
;
1052 struct batadv_orig_node
*orig_node
= NULL
;
1053 int check
, hdr_size
= sizeof(*unicast_packet
);
1056 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
1057 unicast_4addr_packet
= (struct batadv_unicast_4addr_packet
*)skb
->data
;
1059 is4addr
= unicast_packet
->header
.packet_type
== BATADV_UNICAST_4ADDR
;
1060 /* the caller function should have already pulled 2 bytes */
1062 hdr_size
= sizeof(*unicast_4addr_packet
);
1064 /* function returns -EREMOTE for promiscuous packets */
1065 check
= batadv_check_unicast_packet(bat_priv
, skb
, hdr_size
);
1067 /* Even though the packet is not for us, we might save it to use for
1068 * decoding a later received coded packet
1070 if (check
== -EREMOTE
)
1071 batadv_nc_skb_store_sniffed_unicast(bat_priv
, skb
);
1075 if (!batadv_check_unicast_ttvn(bat_priv
, skb
, hdr_size
))
1079 if (batadv_is_my_mac(bat_priv
, unicast_packet
->dest
)) {
1081 batadv_dat_inc_counter(bat_priv
,
1082 unicast_4addr_packet
->subtype
);
1083 orig_addr
= unicast_4addr_packet
->src
;
1084 orig_node
= batadv_orig_hash_find(bat_priv
, orig_addr
);
1087 if (batadv_dat_snoop_incoming_arp_request(bat_priv
, skb
,
1090 if (batadv_dat_snoop_incoming_arp_reply(bat_priv
, skb
,
1094 batadv_interface_rx(recv_if
->soft_iface
, skb
, recv_if
, hdr_size
,
1099 batadv_orig_node_free_ref(orig_node
);
1101 return NET_RX_SUCCESS
;
1104 return batadv_route_unicast_packet(skb
, recv_if
);
1107 int batadv_recv_ucast_frag_packet(struct sk_buff
*skb
,
1108 struct batadv_hard_iface
*recv_if
)
1110 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1111 struct batadv_unicast_frag_packet
*unicast_packet
;
1112 int hdr_size
= sizeof(*unicast_packet
);
1113 struct sk_buff
*new_skb
= NULL
;
1116 if (batadv_check_unicast_packet(bat_priv
, skb
, hdr_size
) < 0)
1119 if (!batadv_check_unicast_ttvn(bat_priv
, skb
, hdr_size
))
1122 unicast_packet
= (struct batadv_unicast_frag_packet
*)skb
->data
;
1125 if (batadv_is_my_mac(bat_priv
, unicast_packet
->dest
)) {
1126 ret
= batadv_frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
1128 if (ret
== NET_RX_DROP
)
1131 /* packet was buffered for late merge */
1133 return NET_RX_SUCCESS
;
1135 if (batadv_dat_snoop_incoming_arp_request(bat_priv
, new_skb
,
1138 if (batadv_dat_snoop_incoming_arp_reply(bat_priv
, new_skb
,
1142 batadv_interface_rx(recv_if
->soft_iface
, new_skb
, recv_if
,
1143 sizeof(struct batadv_unicast_packet
), NULL
);
1146 return NET_RX_SUCCESS
;
1149 return batadv_route_unicast_packet(skb
, recv_if
);
1153 int batadv_recv_bcast_packet(struct sk_buff
*skb
,
1154 struct batadv_hard_iface
*recv_if
)
1156 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1157 struct batadv_orig_node
*orig_node
= NULL
;
1158 struct batadv_bcast_packet
*bcast_packet
;
1159 struct ethhdr
*ethhdr
;
1160 int hdr_size
= sizeof(*bcast_packet
);
1161 int ret
= NET_RX_DROP
;
1164 /* drop packet if it has not necessary minimum size */
1165 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1168 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1170 /* packet with broadcast indication but unicast recipient */
1171 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
1174 /* packet with broadcast sender address */
1175 if (is_broadcast_ether_addr(ethhdr
->h_source
))
1178 /* ignore broadcasts sent by myself */
1179 if (batadv_is_my_mac(bat_priv
, ethhdr
->h_source
))
1182 bcast_packet
= (struct batadv_bcast_packet
*)skb
->data
;
1184 /* ignore broadcasts originated by myself */
1185 if (batadv_is_my_mac(bat_priv
, bcast_packet
->orig
))
1188 if (bcast_packet
->header
.ttl
< 2)
1191 orig_node
= batadv_orig_hash_find(bat_priv
, bcast_packet
->orig
);
1196 spin_lock_bh(&orig_node
->bcast_seqno_lock
);
1198 /* check whether the packet is a duplicate */
1199 if (batadv_test_bit(orig_node
->bcast_bits
, orig_node
->last_bcast_seqno
,
1200 ntohl(bcast_packet
->seqno
)))
1203 seq_diff
= ntohl(bcast_packet
->seqno
) - orig_node
->last_bcast_seqno
;
1205 /* check whether the packet is old and the host just restarted. */
1206 if (batadv_window_protected(bat_priv
, seq_diff
,
1207 &orig_node
->bcast_seqno_reset
))
1210 /* mark broadcast in flood history, update window position
1213 if (batadv_bit_get_packet(bat_priv
, orig_node
->bcast_bits
, seq_diff
, 1))
1214 orig_node
->last_bcast_seqno
= ntohl(bcast_packet
->seqno
);
1216 spin_unlock_bh(&orig_node
->bcast_seqno_lock
);
1218 /* check whether this has been sent by another originator before */
1219 if (batadv_bla_check_bcast_duplist(bat_priv
, skb
))
1222 /* rebroadcast packet */
1223 batadv_add_bcast_packet_to_list(bat_priv
, skb
, 1);
1225 /* don't hand the broadcast up if it is from an originator
1226 * from the same backbone.
1228 if (batadv_bla_is_backbone_gw(skb
, orig_node
, hdr_size
))
1231 if (batadv_dat_snoop_incoming_arp_request(bat_priv
, skb
, hdr_size
))
1233 if (batadv_dat_snoop_incoming_arp_reply(bat_priv
, skb
, hdr_size
))
1236 /* broadcast for me */
1237 batadv_interface_rx(recv_if
->soft_iface
, skb
, recv_if
, hdr_size
,
1241 ret
= NET_RX_SUCCESS
;
1245 spin_unlock_bh(&orig_node
->bcast_seqno_lock
);
1248 batadv_orig_node_free_ref(orig_node
);
1252 int batadv_recv_vis_packet(struct sk_buff
*skb
,
1253 struct batadv_hard_iface
*recv_if
)
1255 struct batadv_vis_packet
*vis_packet
;
1256 struct ethhdr
*ethhdr
;
1257 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1258 int hdr_size
= sizeof(*vis_packet
);
1260 /* keep skb linear */
1261 if (skb_linearize(skb
) < 0)
1264 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1267 vis_packet
= (struct batadv_vis_packet
*)skb
->data
;
1268 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1271 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
1274 /* ignore own packets */
1275 if (batadv_is_my_mac(bat_priv
, vis_packet
->vis_orig
))
1278 if (batadv_is_my_mac(bat_priv
, vis_packet
->sender_orig
))
1281 switch (vis_packet
->vis_type
) {
1282 case BATADV_VIS_TYPE_SERVER_SYNC
:
1283 batadv_receive_server_sync_packet(bat_priv
, vis_packet
,
1287 case BATADV_VIS_TYPE_CLIENT_UPDATE
:
1288 batadv_receive_client_update_packet(bat_priv
, vis_packet
,
1292 default: /* ignore unknown packet */
1296 /* We take a copy of the data in the packet, so we should
1297 * always free the skbuf.