1 /* Copyright (C) 2007-2012 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"
33 static int batadv_route_unicast_packet(struct sk_buff
*skb
,
34 struct batadv_hard_iface
*recv_if
);
36 void batadv_slide_own_bcast_window(struct batadv_hard_iface
*hard_iface
)
38 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
39 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
40 struct hlist_node
*node
;
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
, 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
)) {
84 batadv_dbg(BATADV_DBG_ROUTES
, bat_priv
,
85 "Adding route towards: %pM (via %pM)\n",
86 orig_node
->orig
, neigh_node
->addr
);
88 } else if (neigh_node
&& curr_router
) {
89 batadv_dbg(BATADV_DBG_ROUTES
, bat_priv
,
90 "Changing route towards: %pM (now via %pM - was via %pM)\n",
91 orig_node
->orig
, neigh_node
->addr
,
96 batadv_neigh_node_free_ref(curr_router
);
98 /* increase refcount of new best neighbor */
99 if (neigh_node
&& !atomic_inc_not_zero(&neigh_node
->refcount
))
102 spin_lock_bh(&orig_node
->neigh_list_lock
);
103 rcu_assign_pointer(orig_node
->router
, neigh_node
);
104 spin_unlock_bh(&orig_node
->neigh_list_lock
);
106 /* decrease refcount of previous best neighbor */
108 batadv_neigh_node_free_ref(curr_router
);
111 void batadv_update_route(struct batadv_priv
*bat_priv
,
112 struct batadv_orig_node
*orig_node
,
113 struct batadv_neigh_node
*neigh_node
)
115 struct batadv_neigh_node
*router
= NULL
;
120 router
= batadv_orig_node_get_router(orig_node
);
122 if (router
!= neigh_node
)
123 _batadv_update_route(bat_priv
, orig_node
, neigh_node
);
127 batadv_neigh_node_free_ref(router
);
130 /* caller must hold the neigh_list_lock */
131 void batadv_bonding_candidate_del(struct batadv_orig_node
*orig_node
,
132 struct batadv_neigh_node
*neigh_node
)
134 /* this neighbor is not part of our candidate list */
135 if (list_empty(&neigh_node
->bonding_list
))
138 list_del_rcu(&neigh_node
->bonding_list
);
139 INIT_LIST_HEAD(&neigh_node
->bonding_list
);
140 batadv_neigh_node_free_ref(neigh_node
);
141 atomic_dec(&orig_node
->bond_candidates
);
147 void batadv_bonding_candidate_add(struct batadv_orig_node
*orig_node
,
148 struct batadv_neigh_node
*neigh_node
)
150 struct hlist_node
*node
;
151 struct batadv_neigh_node
*tmp_neigh_node
, *router
= NULL
;
152 uint8_t interference_candidate
= 0;
154 spin_lock_bh(&orig_node
->neigh_list_lock
);
156 /* only consider if it has the same primary address ... */
157 if (!batadv_compare_eth(orig_node
->orig
,
158 neigh_node
->orig_node
->primary_addr
))
161 router
= batadv_orig_node_get_router(orig_node
);
165 /* ... and is good enough to be considered */
166 if (neigh_node
->tq_avg
< router
->tq_avg
- BATADV_BONDING_TQ_THRESHOLD
)
169 /* check if we have another candidate with the same mac address or
170 * interface. If we do, we won't select this candidate because of
171 * possible interference.
173 hlist_for_each_entry_rcu(tmp_neigh_node
, node
,
174 &orig_node
->neigh_list
, list
) {
176 if (tmp_neigh_node
== neigh_node
)
179 /* we only care if the other candidate is even
180 * considered as candidate.
182 if (list_empty(&tmp_neigh_node
->bonding_list
))
185 if ((neigh_node
->if_incoming
== tmp_neigh_node
->if_incoming
) ||
186 (batadv_compare_eth(neigh_node
->addr
,
187 tmp_neigh_node
->addr
))) {
188 interference_candidate
= 1;
193 /* don't care further if it is an interference candidate */
194 if (interference_candidate
)
197 /* this neighbor already is part of our candidate list */
198 if (!list_empty(&neigh_node
->bonding_list
))
201 if (!atomic_inc_not_zero(&neigh_node
->refcount
))
204 list_add_rcu(&neigh_node
->bonding_list
, &orig_node
->bond_list
);
205 atomic_inc(&orig_node
->bond_candidates
);
209 batadv_bonding_candidate_del(orig_node
, neigh_node
);
212 spin_unlock_bh(&orig_node
->neigh_list_lock
);
215 batadv_neigh_node_free_ref(router
);
218 /* copy primary address for bonding */
220 batadv_bonding_save_primary(const struct batadv_orig_node
*orig_node
,
221 struct batadv_orig_node
*orig_neigh_node
,
222 const struct batadv_ogm_packet
*batman_ogm_packet
)
224 if (!(batman_ogm_packet
->flags
& BATADV_PRIMARIES_FIRST_HOP
))
227 memcpy(orig_neigh_node
->primary_addr
, orig_node
->orig
, ETH_ALEN
);
230 /* checks whether the host restarted and is in the protection time.
232 * 0 if the packet is to be accepted
233 * 1 if the packet is to be ignored.
235 int batadv_window_protected(struct batadv_priv
*bat_priv
, int32_t seq_num_diff
,
236 unsigned long *last_reset
)
238 if (seq_num_diff
<= -BATADV_TQ_LOCAL_WINDOW_SIZE
||
239 seq_num_diff
>= BATADV_EXPECTED_SEQNO_RANGE
) {
240 if (!batadv_has_timed_out(*last_reset
,
241 BATADV_RESET_PROTECTION_MS
))
244 *last_reset
= jiffies
;
245 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
246 "old packet received, start protection\n");
252 bool batadv_check_management_packet(struct sk_buff
*skb
,
253 struct batadv_hard_iface
*hard_iface
,
256 struct ethhdr
*ethhdr
;
258 /* drop packet if it has not necessary minimum size */
259 if (unlikely(!pskb_may_pull(skb
, header_len
)))
262 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
264 /* packet with broadcast indication but unicast recipient */
265 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
268 /* packet with broadcast sender address */
269 if (is_broadcast_ether_addr(ethhdr
->h_source
))
272 /* create a copy of the skb, if needed, to modify it. */
273 if (skb_cow(skb
, 0) < 0)
276 /* keep skb linear */
277 if (skb_linearize(skb
) < 0)
283 static int batadv_recv_my_icmp_packet(struct batadv_priv
*bat_priv
,
284 struct sk_buff
*skb
, size_t icmp_len
)
286 struct batadv_hard_iface
*primary_if
= NULL
;
287 struct batadv_orig_node
*orig_node
= NULL
;
288 struct batadv_neigh_node
*router
= NULL
;
289 struct batadv_icmp_packet_rr
*icmp_packet
;
290 int ret
= NET_RX_DROP
;
292 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
294 /* add data to device queue */
295 if (icmp_packet
->msg_type
!= BATADV_ECHO_REQUEST
) {
296 batadv_socket_receive_packet(icmp_packet
, icmp_len
);
300 primary_if
= batadv_primary_if_get_selected(bat_priv
);
304 /* answer echo request (ping) */
305 /* get routing information */
306 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->orig
);
310 router
= batadv_orig_node_get_router(orig_node
);
314 /* create a copy of the skb, if needed, to modify it. */
315 if (skb_cow(skb
, ETH_HLEN
) < 0)
318 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
320 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
321 memcpy(icmp_packet
->orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
322 icmp_packet
->msg_type
= BATADV_ECHO_REPLY
;
323 icmp_packet
->header
.ttl
= BATADV_TTL
;
325 batadv_send_skb_packet(skb
, router
->if_incoming
, router
->addr
);
326 ret
= NET_RX_SUCCESS
;
330 batadv_hardif_free_ref(primary_if
);
332 batadv_neigh_node_free_ref(router
);
334 batadv_orig_node_free_ref(orig_node
);
338 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv
*bat_priv
,
341 struct batadv_hard_iface
*primary_if
= NULL
;
342 struct batadv_orig_node
*orig_node
= NULL
;
343 struct batadv_neigh_node
*router
= NULL
;
344 struct batadv_icmp_packet
*icmp_packet
;
345 int ret
= NET_RX_DROP
;
347 icmp_packet
= (struct batadv_icmp_packet
*)skb
->data
;
349 /* send TTL exceeded if packet is an echo request (traceroute) */
350 if (icmp_packet
->msg_type
!= BATADV_ECHO_REQUEST
) {
351 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
352 icmp_packet
->orig
, icmp_packet
->dst
);
356 primary_if
= batadv_primary_if_get_selected(bat_priv
);
360 /* get routing information */
361 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->orig
);
365 router
= batadv_orig_node_get_router(orig_node
);
369 /* create a copy of the skb, if needed, to modify it. */
370 if (skb_cow(skb
, ETH_HLEN
) < 0)
373 icmp_packet
= (struct batadv_icmp_packet
*)skb
->data
;
375 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
376 memcpy(icmp_packet
->orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
377 icmp_packet
->msg_type
= BATADV_TTL_EXCEEDED
;
378 icmp_packet
->header
.ttl
= BATADV_TTL
;
380 batadv_send_skb_packet(skb
, router
->if_incoming
, router
->addr
);
381 ret
= NET_RX_SUCCESS
;
385 batadv_hardif_free_ref(primary_if
);
387 batadv_neigh_node_free_ref(router
);
389 batadv_orig_node_free_ref(orig_node
);
394 int batadv_recv_icmp_packet(struct sk_buff
*skb
,
395 struct batadv_hard_iface
*recv_if
)
397 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
398 struct batadv_icmp_packet_rr
*icmp_packet
;
399 struct ethhdr
*ethhdr
;
400 struct batadv_orig_node
*orig_node
= NULL
;
401 struct batadv_neigh_node
*router
= NULL
;
402 int hdr_size
= sizeof(struct batadv_icmp_packet
);
403 int ret
= NET_RX_DROP
;
405 /* we truncate all incoming icmp packets if they don't match our size */
406 if (skb
->len
>= sizeof(struct batadv_icmp_packet_rr
))
407 hdr_size
= sizeof(struct batadv_icmp_packet_rr
);
409 /* drop packet if it has not necessary minimum size */
410 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
413 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
415 /* packet with unicast indication but broadcast recipient */
416 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
419 /* packet with broadcast sender address */
420 if (is_broadcast_ether_addr(ethhdr
->h_source
))
424 if (!batadv_is_my_mac(ethhdr
->h_dest
))
427 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
429 /* add record route information if not full */
430 if ((hdr_size
== sizeof(struct batadv_icmp_packet_rr
)) &&
431 (icmp_packet
->rr_cur
< BATADV_RR_LEN
)) {
432 memcpy(&(icmp_packet
->rr
[icmp_packet
->rr_cur
]),
433 ethhdr
->h_dest
, ETH_ALEN
);
434 icmp_packet
->rr_cur
++;
438 if (batadv_is_my_mac(icmp_packet
->dst
))
439 return batadv_recv_my_icmp_packet(bat_priv
, skb
, hdr_size
);
442 if (icmp_packet
->header
.ttl
< 2)
443 return batadv_recv_icmp_ttl_exceeded(bat_priv
, skb
);
445 /* get routing information */
446 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->dst
);
450 router
= batadv_orig_node_get_router(orig_node
);
454 /* create a copy of the skb, if needed, to modify it. */
455 if (skb_cow(skb
, ETH_HLEN
) < 0)
458 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
461 icmp_packet
->header
.ttl
--;
464 batadv_send_skb_packet(skb
, router
->if_incoming
, router
->addr
);
465 ret
= NET_RX_SUCCESS
;
469 batadv_neigh_node_free_ref(router
);
471 batadv_orig_node_free_ref(orig_node
);
475 /* In the bonding case, send the packets in a round
476 * robin fashion over the remaining interfaces.
478 * This method rotates the bonding list and increases the
479 * returned router's refcount.
481 static struct batadv_neigh_node
*
482 batadv_find_bond_router(struct batadv_orig_node
*primary_orig
,
483 const struct batadv_hard_iface
*recv_if
)
485 struct batadv_neigh_node
*tmp_neigh_node
;
486 struct batadv_neigh_node
*router
= NULL
, *first_candidate
= NULL
;
489 list_for_each_entry_rcu(tmp_neigh_node
, &primary_orig
->bond_list
,
491 if (!first_candidate
)
492 first_candidate
= tmp_neigh_node
;
494 /* recv_if == NULL on the first node. */
495 if (tmp_neigh_node
->if_incoming
== recv_if
)
498 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
501 router
= tmp_neigh_node
;
505 /* use the first candidate if nothing was found. */
506 if (!router
&& first_candidate
&&
507 atomic_inc_not_zero(&first_candidate
->refcount
))
508 router
= first_candidate
;
513 /* selected should point to the next element
514 * after the current router
516 spin_lock_bh(&primary_orig
->neigh_list_lock
);
517 /* this is a list_move(), which unfortunately
518 * does not exist as rcu version
520 list_del_rcu(&primary_orig
->bond_list
);
521 list_add_rcu(&primary_orig
->bond_list
,
522 &router
->bonding_list
);
523 spin_unlock_bh(&primary_orig
->neigh_list_lock
);
530 /* Interface Alternating: Use the best of the
531 * remaining candidates which are not using
534 * Increases the returned router's refcount
536 static struct batadv_neigh_node
*
537 batadv_find_ifalter_router(struct batadv_orig_node
*primary_orig
,
538 const struct batadv_hard_iface
*recv_if
)
540 struct batadv_neigh_node
*tmp_neigh_node
;
541 struct batadv_neigh_node
*router
= NULL
, *first_candidate
= NULL
;
544 list_for_each_entry_rcu(tmp_neigh_node
, &primary_orig
->bond_list
,
546 if (!first_candidate
)
547 first_candidate
= tmp_neigh_node
;
549 /* recv_if == NULL on the first node. */
550 if (tmp_neigh_node
->if_incoming
== recv_if
)
553 if (router
&& tmp_neigh_node
->tq_avg
<= router
->tq_avg
)
556 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
559 /* decrement refcount of previously selected router */
561 batadv_neigh_node_free_ref(router
);
563 /* we found a better router (or at least one valid router) */
564 router
= tmp_neigh_node
;
567 /* use the first candidate if nothing was found. */
568 if (!router
&& first_candidate
&&
569 atomic_inc_not_zero(&first_candidate
->refcount
))
570 router
= first_candidate
;
576 static int batadv_check_unicast_packet(struct sk_buff
*skb
, int hdr_size
)
578 struct ethhdr
*ethhdr
;
580 /* drop packet if it has not necessary minimum size */
581 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
584 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
586 /* packet with unicast indication but broadcast recipient */
587 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
590 /* packet with broadcast sender address */
591 if (is_broadcast_ether_addr(ethhdr
->h_source
))
595 if (!batadv_is_my_mac(ethhdr
->h_dest
))
601 int batadv_recv_tt_query(struct sk_buff
*skb
, struct batadv_hard_iface
*recv_if
)
603 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
604 struct batadv_tt_query_packet
*tt_query
;
606 int hdr_size
= sizeof(*tt_query
);
610 if (batadv_check_unicast_packet(skb
, hdr_size
) < 0)
613 /* I could need to modify it */
614 if (skb_cow(skb
, sizeof(struct batadv_tt_query_packet
)) < 0)
617 tt_query
= (struct batadv_tt_query_packet
*)skb
->data
;
619 switch (tt_query
->flags
& BATADV_TT_QUERY_TYPE_MASK
) {
620 case BATADV_TT_REQUEST
:
621 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
623 /* If we cannot provide an answer the tt_request is
626 if (!batadv_send_tt_response(bat_priv
, tt_query
)) {
627 if (tt_query
->flags
& BATADV_TT_FULL_TABLE
)
632 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
633 "Routing TT_REQUEST to %pM [%c]\n",
636 return batadv_route_unicast_packet(skb
, recv_if
);
639 case BATADV_TT_RESPONSE
:
640 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
642 if (batadv_is_my_mac(tt_query
->dst
)) {
643 /* packet needs to be linearized to access the TT
646 if (skb_linearize(skb
) < 0)
648 /* skb_linearize() possibly changed skb->data */
649 tt_query
= (struct batadv_tt_query_packet
*)skb
->data
;
651 tt_size
= batadv_tt_len(ntohs(tt_query
->tt_data
));
653 /* Ensure we have all the claimed data */
654 packet_size
= sizeof(struct batadv_tt_query_packet
);
655 packet_size
+= tt_size
;
656 if (unlikely(skb_headlen(skb
) < packet_size
))
659 batadv_handle_tt_response(bat_priv
, tt_query
);
661 if (tt_query
->flags
& BATADV_TT_FULL_TABLE
)
665 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
666 "Routing TT_RESPONSE to %pM [%c]\n",
669 return batadv_route_unicast_packet(skb
, recv_if
);
675 /* returning NET_RX_DROP will make the caller function kfree the skb */
679 int batadv_recv_roam_adv(struct sk_buff
*skb
, struct batadv_hard_iface
*recv_if
)
681 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
682 struct batadv_roam_adv_packet
*roam_adv_packet
;
683 struct batadv_orig_node
*orig_node
;
685 if (batadv_check_unicast_packet(skb
, sizeof(*roam_adv_packet
)) < 0)
688 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
690 roam_adv_packet
= (struct batadv_roam_adv_packet
*)skb
->data
;
692 if (!batadv_is_my_mac(roam_adv_packet
->dst
))
693 return batadv_route_unicast_packet(skb
, recv_if
);
695 /* check if it is a backbone gateway. we don't accept
696 * roaming advertisement from it, as it has the same
697 * entries as we have.
699 if (batadv_bla_is_backbone_gw_orig(bat_priv
, roam_adv_packet
->src
))
702 orig_node
= batadv_orig_hash_find(bat_priv
, roam_adv_packet
->src
);
706 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
707 "Received ROAMING_ADV from %pM (client %pM)\n",
708 roam_adv_packet
->src
, roam_adv_packet
->client
);
710 batadv_tt_global_add(bat_priv
, orig_node
, roam_adv_packet
->client
,
711 BATADV_TT_CLIENT_ROAM
,
712 atomic_read(&orig_node
->last_ttvn
) + 1);
714 /* Roaming phase starts: I have new information but the ttvn has not
715 * been incremented yet. This flag will make me check all the incoming
716 * packets for the correct destination.
718 bat_priv
->tt
.poss_change
= true;
720 batadv_orig_node_free_ref(orig_node
);
722 /* returning NET_RX_DROP will make the caller function kfree the skb */
726 /* find a suitable router for this originator, and use
727 * bonding if possible. increases the found neighbors
730 struct batadv_neigh_node
*
731 batadv_find_router(struct batadv_priv
*bat_priv
,
732 struct batadv_orig_node
*orig_node
,
733 const struct batadv_hard_iface
*recv_if
)
735 struct batadv_orig_node
*primary_orig_node
;
736 struct batadv_orig_node
*router_orig
;
737 struct batadv_neigh_node
*router
;
738 static uint8_t zero_mac
[ETH_ALEN
] = {0, 0, 0, 0, 0, 0};
740 uint8_t *primary_addr
;
745 router
= batadv_orig_node_get_router(orig_node
);
749 /* without bonding, the first node should
750 * always choose the default router.
752 bonding_enabled
= atomic_read(&bat_priv
->bonding
);
755 /* select default router to output */
756 router_orig
= router
->orig_node
;
760 if ((!recv_if
) && (!bonding_enabled
))
763 primary_addr
= router_orig
->primary_addr
;
765 /* if we have something in the primary_addr, we can search
766 * for a potential bonding candidate.
768 if (batadv_compare_eth(primary_addr
, zero_mac
))
771 /* find the orig_node which has the primary interface. might
772 * even be the same as our router_orig in many cases
774 if (batadv_compare_eth(primary_addr
, router_orig
->orig
)) {
775 primary_orig_node
= router_orig
;
777 primary_orig_node
= batadv_orig_hash_find(bat_priv
,
779 if (!primary_orig_node
)
782 batadv_orig_node_free_ref(primary_orig_node
);
785 /* with less than 2 candidates, we can't do any
786 * bonding and prefer the original router.
788 if (atomic_read(&primary_orig_node
->bond_candidates
) < 2)
791 /* all nodes between should choose a candidate which
792 * is is not on the interface where the packet came
795 batadv_neigh_node_free_ref(router
);
798 router
= batadv_find_bond_router(primary_orig_node
, recv_if
);
800 router
= batadv_find_ifalter_router(primary_orig_node
, recv_if
);
803 if (router
&& router
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
812 batadv_neigh_node_free_ref(router
);
816 static int batadv_route_unicast_packet(struct sk_buff
*skb
,
817 struct batadv_hard_iface
*recv_if
)
819 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
820 struct batadv_orig_node
*orig_node
= NULL
;
821 struct batadv_neigh_node
*neigh_node
= NULL
;
822 struct batadv_unicast_packet
*unicast_packet
;
823 struct ethhdr
*ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
824 int ret
= NET_RX_DROP
;
825 struct sk_buff
*new_skb
;
827 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
830 if (unicast_packet
->header
.ttl
< 2) {
831 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
832 ethhdr
->h_source
, unicast_packet
->dest
);
836 /* get routing information */
837 orig_node
= batadv_orig_hash_find(bat_priv
, unicast_packet
->dest
);
842 /* find_router() increases neigh_nodes refcount if found. */
843 neigh_node
= batadv_find_router(bat_priv
, orig_node
, recv_if
);
848 /* create a copy of the skb, if needed, to modify it. */
849 if (skb_cow(skb
, ETH_HLEN
) < 0)
852 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
854 if (unicast_packet
->header
.packet_type
== BATADV_UNICAST
&&
855 atomic_read(&bat_priv
->fragmentation
) &&
856 skb
->len
> neigh_node
->if_incoming
->net_dev
->mtu
) {
857 ret
= batadv_frag_send_skb(skb
, bat_priv
,
858 neigh_node
->if_incoming
,
863 if (unicast_packet
->header
.packet_type
== BATADV_UNICAST_FRAG
&&
864 batadv_frag_can_reassemble(skb
,
865 neigh_node
->if_incoming
->net_dev
->mtu
)) {
867 ret
= batadv_frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
869 if (ret
== NET_RX_DROP
)
872 /* packet was buffered for late merge */
874 ret
= NET_RX_SUCCESS
;
879 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
883 unicast_packet
->header
.ttl
--;
885 /* Update stats counter */
886 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
887 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
888 skb
->len
+ ETH_HLEN
);
891 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
892 ret
= NET_RX_SUCCESS
;
896 batadv_neigh_node_free_ref(neigh_node
);
898 batadv_orig_node_free_ref(orig_node
);
902 static int batadv_check_unicast_ttvn(struct batadv_priv
*bat_priv
,
903 struct sk_buff
*skb
) {
905 struct batadv_orig_node
*orig_node
;
906 struct ethhdr
*ethhdr
;
907 struct batadv_hard_iface
*primary_if
;
908 struct batadv_unicast_packet
*unicast_packet
;
912 /* check if there is enough data before accessing it */
913 if (pskb_may_pull(skb
, sizeof(*unicast_packet
) + ETH_HLEN
) < 0)
916 /* create a copy of the skb (in case of for re-routing) to modify it. */
917 if (skb_cow(skb
, sizeof(*unicast_packet
)) < 0)
920 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
922 if (batadv_is_my_mac(unicast_packet
->dest
)) {
923 tt_poss_change
= bat_priv
->tt
.poss_change
;
924 curr_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
926 orig_node
= batadv_orig_hash_find(bat_priv
,
927 unicast_packet
->dest
);
932 curr_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
933 tt_poss_change
= orig_node
->tt_poss_change
;
934 batadv_orig_node_free_ref(orig_node
);
937 /* Check whether I have to reroute the packet */
938 is_old_ttvn
= batadv_seq_before(unicast_packet
->ttvn
, curr_ttvn
);
939 if (is_old_ttvn
|| tt_poss_change
) {
940 /* check if there is enough data before accessing it */
941 if (pskb_may_pull(skb
, sizeof(struct batadv_unicast_packet
) +
945 ethhdr
= (struct ethhdr
*)(skb
->data
+ sizeof(*unicast_packet
));
947 /* we don't have an updated route for this client, so we should
948 * not try to reroute the packet!!
950 if (batadv_tt_global_client_is_roaming(bat_priv
,
954 orig_node
= batadv_transtable_search(bat_priv
, NULL
,
958 if (!batadv_is_my_client(bat_priv
, ethhdr
->h_dest
))
960 primary_if
= batadv_primary_if_get_selected(bat_priv
);
963 memcpy(unicast_packet
->dest
,
964 primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
965 batadv_hardif_free_ref(primary_if
);
967 memcpy(unicast_packet
->dest
, orig_node
->orig
,
969 curr_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
970 batadv_orig_node_free_ref(orig_node
);
973 net_ratelimited_function(batadv_dbg
, BATADV_DBG_TT
, bat_priv
,
974 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
975 unicast_packet
->ttvn
, curr_ttvn
,
976 ethhdr
->h_dest
, unicast_packet
->dest
);
978 unicast_packet
->ttvn
= curr_ttvn
;
983 int batadv_recv_unicast_packet(struct sk_buff
*skb
,
984 struct batadv_hard_iface
*recv_if
)
986 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
987 struct batadv_unicast_packet
*unicast_packet
;
988 struct batadv_unicast_4addr_packet
*unicast_4addr_packet
;
989 int hdr_size
= sizeof(*unicast_packet
);
992 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
993 unicast_4addr_packet
= (struct batadv_unicast_4addr_packet
*)skb
->data
;
995 is4addr
= unicast_packet
->header
.packet_type
== BATADV_UNICAST_4ADDR
;
996 /* the caller function should have already pulled 2 bytes */
998 hdr_size
= sizeof(*unicast_4addr_packet
);
1000 if (batadv_check_unicast_packet(skb
, hdr_size
) < 0)
1003 if (!batadv_check_unicast_ttvn(bat_priv
, skb
))
1007 if (batadv_is_my_mac(unicast_packet
->dest
)) {
1009 batadv_dat_inc_counter(bat_priv
,
1010 unicast_4addr_packet
->subtype
);
1012 if (batadv_dat_snoop_incoming_arp_request(bat_priv
, skb
,
1015 if (batadv_dat_snoop_incoming_arp_reply(bat_priv
, skb
,
1019 batadv_interface_rx(recv_if
->soft_iface
, skb
, recv_if
, hdr_size
,
1023 return NET_RX_SUCCESS
;
1026 return batadv_route_unicast_packet(skb
, recv_if
);
1029 int batadv_recv_ucast_frag_packet(struct sk_buff
*skb
,
1030 struct batadv_hard_iface
*recv_if
)
1032 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1033 struct batadv_unicast_frag_packet
*unicast_packet
;
1034 int hdr_size
= sizeof(*unicast_packet
);
1035 struct sk_buff
*new_skb
= NULL
;
1038 if (batadv_check_unicast_packet(skb
, hdr_size
) < 0)
1041 if (!batadv_check_unicast_ttvn(bat_priv
, skb
))
1044 unicast_packet
= (struct batadv_unicast_frag_packet
*)skb
->data
;
1047 if (batadv_is_my_mac(unicast_packet
->dest
)) {
1049 ret
= batadv_frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
1051 if (ret
== NET_RX_DROP
)
1054 /* packet was buffered for late merge */
1056 return NET_RX_SUCCESS
;
1058 if (batadv_dat_snoop_incoming_arp_request(bat_priv
, new_skb
,
1061 if (batadv_dat_snoop_incoming_arp_reply(bat_priv
, new_skb
,
1065 batadv_interface_rx(recv_if
->soft_iface
, new_skb
, recv_if
,
1066 sizeof(struct batadv_unicast_packet
), NULL
);
1069 return NET_RX_SUCCESS
;
1072 return batadv_route_unicast_packet(skb
, recv_if
);
1076 int batadv_recv_bcast_packet(struct sk_buff
*skb
,
1077 struct batadv_hard_iface
*recv_if
)
1079 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1080 struct batadv_orig_node
*orig_node
= NULL
;
1081 struct batadv_bcast_packet
*bcast_packet
;
1082 struct ethhdr
*ethhdr
;
1083 int hdr_size
= sizeof(*bcast_packet
);
1084 int ret
= NET_RX_DROP
;
1087 /* drop packet if it has not necessary minimum size */
1088 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1091 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1093 /* packet with broadcast indication but unicast recipient */
1094 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
1097 /* packet with broadcast sender address */
1098 if (is_broadcast_ether_addr(ethhdr
->h_source
))
1101 /* ignore broadcasts sent by myself */
1102 if (batadv_is_my_mac(ethhdr
->h_source
))
1105 bcast_packet
= (struct batadv_bcast_packet
*)skb
->data
;
1107 /* ignore broadcasts originated by myself */
1108 if (batadv_is_my_mac(bcast_packet
->orig
))
1111 if (bcast_packet
->header
.ttl
< 2)
1114 orig_node
= batadv_orig_hash_find(bat_priv
, bcast_packet
->orig
);
1119 spin_lock_bh(&orig_node
->bcast_seqno_lock
);
1121 /* check whether the packet is a duplicate */
1122 if (batadv_test_bit(orig_node
->bcast_bits
, orig_node
->last_bcast_seqno
,
1123 ntohl(bcast_packet
->seqno
)))
1126 seq_diff
= ntohl(bcast_packet
->seqno
) - orig_node
->last_bcast_seqno
;
1128 /* check whether the packet is old and the host just restarted. */
1129 if (batadv_window_protected(bat_priv
, seq_diff
,
1130 &orig_node
->bcast_seqno_reset
))
1133 /* mark broadcast in flood history, update window position
1136 if (batadv_bit_get_packet(bat_priv
, orig_node
->bcast_bits
, seq_diff
, 1))
1137 orig_node
->last_bcast_seqno
= ntohl(bcast_packet
->seqno
);
1139 spin_unlock_bh(&orig_node
->bcast_seqno_lock
);
1141 /* keep skb linear for crc calculation */
1142 if (skb_linearize(skb
) < 0)
1145 bcast_packet
= (struct batadv_bcast_packet
*)skb
->data
;
1147 /* check whether this has been sent by another originator before */
1148 if (batadv_bla_check_bcast_duplist(bat_priv
, bcast_packet
, skb
->len
))
1151 /* rebroadcast packet */
1152 batadv_add_bcast_packet_to_list(bat_priv
, skb
, 1);
1154 /* don't hand the broadcast up if it is from an originator
1155 * from the same backbone.
1157 if (batadv_bla_is_backbone_gw(skb
, orig_node
, hdr_size
))
1160 if (batadv_dat_snoop_incoming_arp_request(bat_priv
, skb
, hdr_size
))
1162 if (batadv_dat_snoop_incoming_arp_reply(bat_priv
, skb
, hdr_size
))
1165 /* broadcast for me */
1166 batadv_interface_rx(recv_if
->soft_iface
, skb
, recv_if
, hdr_size
,
1170 ret
= NET_RX_SUCCESS
;
1174 spin_unlock_bh(&orig_node
->bcast_seqno_lock
);
1177 batadv_orig_node_free_ref(orig_node
);
1181 int batadv_recv_vis_packet(struct sk_buff
*skb
,
1182 struct batadv_hard_iface
*recv_if
)
1184 struct batadv_vis_packet
*vis_packet
;
1185 struct ethhdr
*ethhdr
;
1186 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1187 int hdr_size
= sizeof(*vis_packet
);
1189 /* keep skb linear */
1190 if (skb_linearize(skb
) < 0)
1193 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1196 vis_packet
= (struct batadv_vis_packet
*)skb
->data
;
1197 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1200 if (!batadv_is_my_mac(ethhdr
->h_dest
))
1203 /* ignore own packets */
1204 if (batadv_is_my_mac(vis_packet
->vis_orig
))
1207 if (batadv_is_my_mac(vis_packet
->sender_orig
))
1210 switch (vis_packet
->vis_type
) {
1211 case BATADV_VIS_TYPE_SERVER_SYNC
:
1212 batadv_receive_server_sync_packet(bat_priv
, vis_packet
,
1216 case BATADV_VIS_TYPE_CLIENT_UPDATE
:
1217 batadv_receive_client_update_packet(bat_priv
, vis_packet
,
1221 default: /* ignore unknown packet */
1225 /* We take a copy of the data in the packet, so we should
1226 * always free the skbuf.