]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/batman-adv/routing.c
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"
32 static int route_unicast_packet(struct sk_buff
*skb
,
33 struct hard_iface
*recv_if
);
35 void batadv_slide_own_bcast_window(struct hard_iface
*hard_iface
)
37 struct bat_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
38 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
39 struct hlist_node
*node
;
40 struct hlist_head
*head
;
41 struct orig_node
*orig_node
;
46 for (i
= 0; i
< hash
->size
; i
++) {
47 head
= &hash
->table
[i
];
50 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
51 spin_lock_bh(&orig_node
->ogm_cnt_lock
);
52 word_index
= hard_iface
->if_num
* NUM_WORDS
;
53 word
= &(orig_node
->bcast_own
[word_index
]);
55 batadv_bit_get_packet(bat_priv
, word
, 1, 0);
56 orig_node
->bcast_own_sum
[hard_iface
->if_num
] =
57 bitmap_weight(word
, TQ_LOCAL_WINDOW_SIZE
);
58 spin_unlock_bh(&orig_node
->ogm_cnt_lock
);
64 static void _update_route(struct bat_priv
*bat_priv
,
65 struct orig_node
*orig_node
,
66 struct neigh_node
*neigh_node
)
68 struct neigh_node
*curr_router
;
70 curr_router
= batadv_orig_node_get_router(orig_node
);
73 if ((curr_router
) && (!neigh_node
)) {
74 bat_dbg(DBG_ROUTES
, bat_priv
, "Deleting route towards: %pM\n",
76 batadv_tt_global_del_orig(bat_priv
, orig_node
,
77 "Deleted route towards originator");
80 } else if ((!curr_router
) && (neigh_node
)) {
82 bat_dbg(DBG_ROUTES
, bat_priv
,
83 "Adding route towards: %pM (via %pM)\n",
84 orig_node
->orig
, neigh_node
->addr
);
86 } else if (neigh_node
&& curr_router
) {
87 bat_dbg(DBG_ROUTES
, bat_priv
,
88 "Changing route towards: %pM (now via %pM - was via %pM)\n",
89 orig_node
->orig
, neigh_node
->addr
,
94 batadv_neigh_node_free_ref(curr_router
);
96 /* increase refcount of new best neighbor */
97 if (neigh_node
&& !atomic_inc_not_zero(&neigh_node
->refcount
))
100 spin_lock_bh(&orig_node
->neigh_list_lock
);
101 rcu_assign_pointer(orig_node
->router
, neigh_node
);
102 spin_unlock_bh(&orig_node
->neigh_list_lock
);
104 /* decrease refcount of previous best neighbor */
106 batadv_neigh_node_free_ref(curr_router
);
109 void batadv_update_route(struct bat_priv
*bat_priv
, struct orig_node
*orig_node
,
110 struct neigh_node
*neigh_node
)
112 struct neigh_node
*router
= NULL
;
117 router
= batadv_orig_node_get_router(orig_node
);
119 if (router
!= neigh_node
)
120 _update_route(bat_priv
, orig_node
, neigh_node
);
124 batadv_neigh_node_free_ref(router
);
127 /* caller must hold the neigh_list_lock */
128 void batadv_bonding_candidate_del(struct orig_node
*orig_node
,
129 struct neigh_node
*neigh_node
)
131 /* this neighbor is not part of our candidate list */
132 if (list_empty(&neigh_node
->bonding_list
))
135 list_del_rcu(&neigh_node
->bonding_list
);
136 INIT_LIST_HEAD(&neigh_node
->bonding_list
);
137 batadv_neigh_node_free_ref(neigh_node
);
138 atomic_dec(&orig_node
->bond_candidates
);
144 void batadv_bonding_candidate_add(struct orig_node
*orig_node
,
145 struct neigh_node
*neigh_node
)
147 struct hlist_node
*node
;
148 struct neigh_node
*tmp_neigh_node
, *router
= NULL
;
149 uint8_t interference_candidate
= 0;
151 spin_lock_bh(&orig_node
->neigh_list_lock
);
153 /* only consider if it has the same primary address ... */
154 if (!compare_eth(orig_node
->orig
,
155 neigh_node
->orig_node
->primary_addr
))
158 router
= batadv_orig_node_get_router(orig_node
);
162 /* ... and is good enough to be considered */
163 if (neigh_node
->tq_avg
< router
->tq_avg
- BONDING_TQ_THRESHOLD
)
166 /* check if we have another candidate with the same mac address or
167 * interface. If we do, we won't select this candidate because of
168 * possible interference.
170 hlist_for_each_entry_rcu(tmp_neigh_node
, node
,
171 &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 (compare_eth(neigh_node
->addr
, tmp_neigh_node
->addr
))) {
184 interference_candidate
= 1;
189 /* don't care further if it is an interference candidate */
190 if (interference_candidate
)
193 /* this neighbor already is part of our candidate list */
194 if (!list_empty(&neigh_node
->bonding_list
))
197 if (!atomic_inc_not_zero(&neigh_node
->refcount
))
200 list_add_rcu(&neigh_node
->bonding_list
, &orig_node
->bond_list
);
201 atomic_inc(&orig_node
->bond_candidates
);
205 batadv_bonding_candidate_del(orig_node
, neigh_node
);
208 spin_unlock_bh(&orig_node
->neigh_list_lock
);
211 batadv_neigh_node_free_ref(router
);
214 /* copy primary address for bonding */
216 batadv_bonding_save_primary(const struct orig_node
*orig_node
,
217 struct orig_node
*orig_neigh_node
,
218 const struct batman_ogm_packet
*batman_ogm_packet
)
220 if (!(batman_ogm_packet
->flags
& PRIMARIES_FIRST_HOP
))
223 memcpy(orig_neigh_node
->primary_addr
, orig_node
->orig
, ETH_ALEN
);
226 /* checks whether the host restarted and is in the protection time.
228 * 0 if the packet is to be accepted
229 * 1 if the packet is to be ignored.
231 int batadv_window_protected(struct bat_priv
*bat_priv
, int32_t seq_num_diff
,
232 unsigned long *last_reset
)
234 if ((seq_num_diff
<= -TQ_LOCAL_WINDOW_SIZE
) ||
235 (seq_num_diff
>= EXPECTED_SEQNO_RANGE
)) {
236 if (!has_timed_out(*last_reset
, RESET_PROTECTION_MS
))
239 *last_reset
= jiffies
;
240 bat_dbg(DBG_BATMAN
, bat_priv
,
241 "old packet received, start protection\n");
247 bool batadv_check_management_packet(struct sk_buff
*skb
,
248 struct hard_iface
*hard_iface
,
251 struct ethhdr
*ethhdr
;
253 /* drop packet if it has not necessary minimum size */
254 if (unlikely(!pskb_may_pull(skb
, header_len
)))
257 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
259 /* packet with broadcast indication but unicast recipient */
260 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
263 /* packet with broadcast sender address */
264 if (is_broadcast_ether_addr(ethhdr
->h_source
))
267 /* create a copy of the skb, if needed, to modify it. */
268 if (skb_cow(skb
, 0) < 0)
271 /* keep skb linear */
272 if (skb_linearize(skb
) < 0)
278 static int recv_my_icmp_packet(struct bat_priv
*bat_priv
,
279 struct sk_buff
*skb
, size_t icmp_len
)
281 struct hard_iface
*primary_if
= NULL
;
282 struct orig_node
*orig_node
= NULL
;
283 struct neigh_node
*router
= NULL
;
284 struct icmp_packet_rr
*icmp_packet
;
285 int ret
= NET_RX_DROP
;
287 icmp_packet
= (struct icmp_packet_rr
*)skb
->data
;
289 /* add data to device queue */
290 if (icmp_packet
->msg_type
!= ECHO_REQUEST
) {
291 batadv_socket_receive_packet(icmp_packet
, icmp_len
);
295 primary_if
= batadv_primary_if_get_selected(bat_priv
);
299 /* answer echo request (ping) */
300 /* get routing information */
301 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->orig
);
305 router
= batadv_orig_node_get_router(orig_node
);
309 /* create a copy of the skb, if needed, to modify it. */
310 if (skb_cow(skb
, ETH_HLEN
) < 0)
313 icmp_packet
= (struct icmp_packet_rr
*)skb
->data
;
315 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
316 memcpy(icmp_packet
->orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
317 icmp_packet
->msg_type
= ECHO_REPLY
;
318 icmp_packet
->header
.ttl
= TTL
;
320 batadv_send_skb_packet(skb
, router
->if_incoming
, router
->addr
);
321 ret
= NET_RX_SUCCESS
;
325 batadv_hardif_free_ref(primary_if
);
327 batadv_neigh_node_free_ref(router
);
329 batadv_orig_node_free_ref(orig_node
);
333 static int recv_icmp_ttl_exceeded(struct bat_priv
*bat_priv
,
336 struct hard_iface
*primary_if
= NULL
;
337 struct orig_node
*orig_node
= NULL
;
338 struct neigh_node
*router
= NULL
;
339 struct icmp_packet
*icmp_packet
;
340 int ret
= NET_RX_DROP
;
342 icmp_packet
= (struct icmp_packet
*)skb
->data
;
344 /* send TTL exceeded if packet is an echo request (traceroute) */
345 if (icmp_packet
->msg_type
!= ECHO_REQUEST
) {
346 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
347 icmp_packet
->orig
, icmp_packet
->dst
);
351 primary_if
= batadv_primary_if_get_selected(bat_priv
);
355 /* get routing information */
356 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->orig
);
360 router
= batadv_orig_node_get_router(orig_node
);
364 /* create a copy of the skb, if needed, to modify it. */
365 if (skb_cow(skb
, ETH_HLEN
) < 0)
368 icmp_packet
= (struct icmp_packet
*)skb
->data
;
370 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
371 memcpy(icmp_packet
->orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
372 icmp_packet
->msg_type
= TTL_EXCEEDED
;
373 icmp_packet
->header
.ttl
= TTL
;
375 batadv_send_skb_packet(skb
, router
->if_incoming
, router
->addr
);
376 ret
= NET_RX_SUCCESS
;
380 batadv_hardif_free_ref(primary_if
);
382 batadv_neigh_node_free_ref(router
);
384 batadv_orig_node_free_ref(orig_node
);
389 int batadv_recv_icmp_packet(struct sk_buff
*skb
, struct hard_iface
*recv_if
)
391 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
392 struct icmp_packet_rr
*icmp_packet
;
393 struct ethhdr
*ethhdr
;
394 struct orig_node
*orig_node
= NULL
;
395 struct neigh_node
*router
= NULL
;
396 int hdr_size
= sizeof(struct icmp_packet
);
397 int ret
= NET_RX_DROP
;
399 /* we truncate all incoming icmp packets if they don't match our size */
400 if (skb
->len
>= sizeof(struct icmp_packet_rr
))
401 hdr_size
= sizeof(struct icmp_packet_rr
);
403 /* drop packet if it has not necessary minimum size */
404 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
407 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
409 /* packet with unicast indication but broadcast recipient */
410 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
413 /* packet with broadcast sender address */
414 if (is_broadcast_ether_addr(ethhdr
->h_source
))
418 if (!batadv_is_my_mac(ethhdr
->h_dest
))
421 icmp_packet
= (struct icmp_packet_rr
*)skb
->data
;
423 /* add record route information if not full */
424 if ((hdr_size
== sizeof(struct icmp_packet_rr
)) &&
425 (icmp_packet
->rr_cur
< BAT_RR_LEN
)) {
426 memcpy(&(icmp_packet
->rr
[icmp_packet
->rr_cur
]),
427 ethhdr
->h_dest
, ETH_ALEN
);
428 icmp_packet
->rr_cur
++;
432 if (batadv_is_my_mac(icmp_packet
->dst
))
433 return recv_my_icmp_packet(bat_priv
, skb
, hdr_size
);
436 if (icmp_packet
->header
.ttl
< 2)
437 return recv_icmp_ttl_exceeded(bat_priv
, skb
);
439 /* get routing information */
440 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->dst
);
444 router
= batadv_orig_node_get_router(orig_node
);
448 /* create a copy of the skb, if needed, to modify it. */
449 if (skb_cow(skb
, ETH_HLEN
) < 0)
452 icmp_packet
= (struct icmp_packet_rr
*)skb
->data
;
455 icmp_packet
->header
.ttl
--;
458 batadv_send_skb_packet(skb
, router
->if_incoming
, router
->addr
);
459 ret
= NET_RX_SUCCESS
;
463 batadv_neigh_node_free_ref(router
);
465 batadv_orig_node_free_ref(orig_node
);
469 /* In the bonding case, send the packets in a round
470 * robin fashion over the remaining interfaces.
472 * This method rotates the bonding list and increases the
473 * returned router's refcount.
475 static struct neigh_node
*find_bond_router(struct orig_node
*primary_orig
,
476 const struct hard_iface
*recv_if
)
478 struct neigh_node
*tmp_neigh_node
;
479 struct neigh_node
*router
= NULL
, *first_candidate
= NULL
;
482 list_for_each_entry_rcu(tmp_neigh_node
, &primary_orig
->bond_list
,
484 if (!first_candidate
)
485 first_candidate
= tmp_neigh_node
;
487 /* recv_if == NULL on the first node. */
488 if (tmp_neigh_node
->if_incoming
== recv_if
)
491 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
494 router
= tmp_neigh_node
;
498 /* use the first candidate if nothing was found. */
499 if (!router
&& first_candidate
&&
500 atomic_inc_not_zero(&first_candidate
->refcount
))
501 router
= first_candidate
;
506 /* selected should point to the next element
507 * after the current router
509 spin_lock_bh(&primary_orig
->neigh_list_lock
);
510 /* this is a list_move(), which unfortunately
511 * does not exist as rcu version
513 list_del_rcu(&primary_orig
->bond_list
);
514 list_add_rcu(&primary_orig
->bond_list
,
515 &router
->bonding_list
);
516 spin_unlock_bh(&primary_orig
->neigh_list_lock
);
523 /* Interface Alternating: Use the best of the
524 * remaining candidates which are not using
527 * Increases the returned router's refcount
529 static struct neigh_node
*find_ifalter_router(struct orig_node
*primary_orig
,
530 const struct hard_iface
*recv_if
)
532 struct neigh_node
*tmp_neigh_node
;
533 struct neigh_node
*router
= NULL
, *first_candidate
= NULL
;
536 list_for_each_entry_rcu(tmp_neigh_node
, &primary_orig
->bond_list
,
538 if (!first_candidate
)
539 first_candidate
= tmp_neigh_node
;
541 /* recv_if == NULL on the first node. */
542 if (tmp_neigh_node
->if_incoming
== recv_if
)
545 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
548 /* if we don't have a router yet
549 * or this one is better, choose it.
552 (tmp_neigh_node
->tq_avg
> router
->tq_avg
)) {
553 /* decrement refcount of
554 * previously selected router
557 batadv_neigh_node_free_ref(router
);
559 router
= tmp_neigh_node
;
560 atomic_inc_not_zero(&router
->refcount
);
563 batadv_neigh_node_free_ref(tmp_neigh_node
);
566 /* use the first candidate if nothing was found. */
567 if (!router
&& first_candidate
&&
568 atomic_inc_not_zero(&first_candidate
->refcount
))
569 router
= first_candidate
;
575 int batadv_recv_tt_query(struct sk_buff
*skb
, struct hard_iface
*recv_if
)
577 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
578 struct tt_query_packet
*tt_query
;
580 struct ethhdr
*ethhdr
;
582 /* drop packet if it has not necessary minimum size */
583 if (unlikely(!pskb_may_pull(skb
, sizeof(struct tt_query_packet
))))
586 /* I could need to modify it */
587 if (skb_cow(skb
, sizeof(struct tt_query_packet
)) < 0)
590 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
592 /* packet with unicast indication but broadcast recipient */
593 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
596 /* packet with broadcast sender address */
597 if (is_broadcast_ether_addr(ethhdr
->h_source
))
600 tt_query
= (struct tt_query_packet
*)skb
->data
;
602 switch (tt_query
->flags
& TT_QUERY_TYPE_MASK
) {
604 batadv_inc_counter(bat_priv
, BAT_CNT_TT_REQUEST_RX
);
606 /* If we cannot provide an answer the tt_request is
609 if (!batadv_send_tt_response(bat_priv
, tt_query
)) {
610 bat_dbg(DBG_TT
, bat_priv
,
611 "Routing TT_REQUEST to %pM [%c]\n",
613 (tt_query
->flags
& TT_FULL_TABLE
? 'F' : '.'));
614 return route_unicast_packet(skb
, recv_if
);
618 batadv_inc_counter(bat_priv
, BAT_CNT_TT_RESPONSE_RX
);
620 if (batadv_is_my_mac(tt_query
->dst
)) {
621 /* packet needs to be linearized to access the TT
624 if (skb_linearize(skb
) < 0)
626 /* skb_linearize() possibly changed skb->data */
627 tt_query
= (struct tt_query_packet
*)skb
->data
;
629 tt_size
= batadv_tt_len(ntohs(tt_query
->tt_data
));
631 /* Ensure we have all the claimed data */
632 if (unlikely(skb_headlen(skb
) <
633 sizeof(struct tt_query_packet
) + tt_size
))
636 batadv_handle_tt_response(bat_priv
, tt_query
);
638 bat_dbg(DBG_TT
, bat_priv
,
639 "Routing TT_RESPONSE to %pM [%c]\n",
641 (tt_query
->flags
& TT_FULL_TABLE
? 'F' : '.'));
642 return route_unicast_packet(skb
, recv_if
);
648 /* returning NET_RX_DROP will make the caller function kfree the skb */
652 int batadv_recv_roam_adv(struct sk_buff
*skb
, struct hard_iface
*recv_if
)
654 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
655 struct roam_adv_packet
*roam_adv_packet
;
656 struct orig_node
*orig_node
;
657 struct ethhdr
*ethhdr
;
659 /* drop packet if it has not necessary minimum size */
660 if (unlikely(!pskb_may_pull(skb
, sizeof(struct roam_adv_packet
))))
663 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
665 /* packet with unicast indication but broadcast recipient */
666 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
669 /* packet with broadcast sender address */
670 if (is_broadcast_ether_addr(ethhdr
->h_source
))
673 batadv_inc_counter(bat_priv
, BAT_CNT_TT_ROAM_ADV_RX
);
675 roam_adv_packet
= (struct roam_adv_packet
*)skb
->data
;
677 if (!batadv_is_my_mac(roam_adv_packet
->dst
))
678 return route_unicast_packet(skb
, recv_if
);
680 /* check if it is a backbone gateway. we don't accept
681 * roaming advertisement from it, as it has the same
682 * entries as we have.
684 if (batadv_bla_is_backbone_gw_orig(bat_priv
, roam_adv_packet
->src
))
687 orig_node
= batadv_orig_hash_find(bat_priv
, roam_adv_packet
->src
);
691 bat_dbg(DBG_TT
, bat_priv
,
692 "Received ROAMING_ADV from %pM (client %pM)\n",
693 roam_adv_packet
->src
, roam_adv_packet
->client
);
695 batadv_tt_global_add(bat_priv
, orig_node
, roam_adv_packet
->client
,
696 atomic_read(&orig_node
->last_ttvn
) + 1, true,
699 /* Roaming phase starts: I have new information but the ttvn has not
700 * been incremented yet. This flag will make me check all the incoming
701 * packets for the correct destination.
703 bat_priv
->tt_poss_change
= true;
705 batadv_orig_node_free_ref(orig_node
);
707 /* returning NET_RX_DROP will make the caller function kfree the skb */
711 /* find a suitable router for this originator, and use
712 * bonding if possible. increases the found neighbors
715 struct neigh_node
*batadv_find_router(struct bat_priv
*bat_priv
,
716 struct orig_node
*orig_node
,
717 const struct hard_iface
*recv_if
)
719 struct orig_node
*primary_orig_node
;
720 struct orig_node
*router_orig
;
721 struct neigh_node
*router
;
722 static uint8_t zero_mac
[ETH_ALEN
] = {0, 0, 0, 0, 0, 0};
724 uint8_t *primary_addr
;
729 router
= batadv_orig_node_get_router(orig_node
);
733 /* without bonding, the first node should
734 * always choose the default router.
736 bonding_enabled
= atomic_read(&bat_priv
->bonding
);
739 /* select default router to output */
740 router_orig
= router
->orig_node
;
744 if ((!recv_if
) && (!bonding_enabled
))
747 primary_addr
= router_orig
->primary_addr
;
749 /* if we have something in the primary_addr, we can search
750 * for a potential bonding candidate.
752 if (compare_eth(primary_addr
, zero_mac
))
755 /* find the orig_node which has the primary interface. might
756 * even be the same as our router_orig in many cases
758 if (compare_eth(primary_addr
, router_orig
->orig
)) {
759 primary_orig_node
= router_orig
;
761 primary_orig_node
= batadv_orig_hash_find(bat_priv
,
763 if (!primary_orig_node
)
766 batadv_orig_node_free_ref(primary_orig_node
);
769 /* with less than 2 candidates, we can't do any
770 * bonding and prefer the original router.
772 if (atomic_read(&primary_orig_node
->bond_candidates
) < 2)
775 /* all nodes between should choose a candidate which
776 * is is not on the interface where the packet came
779 batadv_neigh_node_free_ref(router
);
782 router
= find_bond_router(primary_orig_node
, recv_if
);
784 router
= find_ifalter_router(primary_orig_node
, recv_if
);
787 if (router
&& router
->if_incoming
->if_status
!= IF_ACTIVE
)
796 batadv_neigh_node_free_ref(router
);
800 static int check_unicast_packet(struct sk_buff
*skb
, int hdr_size
)
802 struct ethhdr
*ethhdr
;
804 /* drop packet if it has not necessary minimum size */
805 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
808 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
810 /* packet with unicast indication but broadcast recipient */
811 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
814 /* packet with broadcast sender address */
815 if (is_broadcast_ether_addr(ethhdr
->h_source
))
819 if (!batadv_is_my_mac(ethhdr
->h_dest
))
825 static int route_unicast_packet(struct sk_buff
*skb
, struct hard_iface
*recv_if
)
827 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
828 struct orig_node
*orig_node
= NULL
;
829 struct neigh_node
*neigh_node
= NULL
;
830 struct unicast_packet
*unicast_packet
;
831 struct ethhdr
*ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
832 int ret
= NET_RX_DROP
;
833 struct sk_buff
*new_skb
;
835 unicast_packet
= (struct unicast_packet
*)skb
->data
;
838 if (unicast_packet
->header
.ttl
< 2) {
839 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
840 ethhdr
->h_source
, unicast_packet
->dest
);
844 /* get routing information */
845 orig_node
= batadv_orig_hash_find(bat_priv
, unicast_packet
->dest
);
850 /* find_router() increases neigh_nodes refcount if found. */
851 neigh_node
= batadv_find_router(bat_priv
, orig_node
, recv_if
);
856 /* create a copy of the skb, if needed, to modify it. */
857 if (skb_cow(skb
, ETH_HLEN
) < 0)
860 unicast_packet
= (struct unicast_packet
*)skb
->data
;
862 if (unicast_packet
->header
.packet_type
== BAT_UNICAST
&&
863 atomic_read(&bat_priv
->fragmentation
) &&
864 skb
->len
> neigh_node
->if_incoming
->net_dev
->mtu
) {
865 ret
= batadv_frag_send_skb(skb
, bat_priv
,
866 neigh_node
->if_incoming
,
871 if (unicast_packet
->header
.packet_type
== BAT_UNICAST_FRAG
&&
872 batadv_frag_can_reassemble(skb
,
873 neigh_node
->if_incoming
->net_dev
->mtu
)) {
875 ret
= batadv_frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
877 if (ret
== NET_RX_DROP
)
880 /* packet was buffered for late merge */
882 ret
= NET_RX_SUCCESS
;
887 unicast_packet
= (struct unicast_packet
*)skb
->data
;
891 unicast_packet
->header
.ttl
--;
893 /* Update stats counter */
894 batadv_inc_counter(bat_priv
, BAT_CNT_FORWARD
);
895 batadv_add_counter(bat_priv
, BAT_CNT_FORWARD_BYTES
,
896 skb
->len
+ ETH_HLEN
);
899 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
900 ret
= NET_RX_SUCCESS
;
904 batadv_neigh_node_free_ref(neigh_node
);
906 batadv_orig_node_free_ref(orig_node
);
910 static int check_unicast_ttvn(struct bat_priv
*bat_priv
,
911 struct sk_buff
*skb
) {
913 struct orig_node
*orig_node
;
914 struct ethhdr
*ethhdr
;
915 struct hard_iface
*primary_if
;
916 struct unicast_packet
*unicast_packet
;
919 /* I could need to modify it */
920 if (skb_cow(skb
, sizeof(struct unicast_packet
)) < 0)
923 unicast_packet
= (struct unicast_packet
*)skb
->data
;
925 if (batadv_is_my_mac(unicast_packet
->dest
)) {
926 tt_poss_change
= bat_priv
->tt_poss_change
;
927 curr_ttvn
= (uint8_t)atomic_read(&bat_priv
->ttvn
);
929 orig_node
= batadv_orig_hash_find(bat_priv
,
930 unicast_packet
->dest
);
935 curr_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
936 tt_poss_change
= orig_node
->tt_poss_change
;
937 batadv_orig_node_free_ref(orig_node
);
940 /* Check whether I have to reroute the packet */
941 if (seq_before(unicast_packet
->ttvn
, curr_ttvn
) || tt_poss_change
) {
942 /* check if there is enough data before accessing it */
943 if (pskb_may_pull(skb
, sizeof(struct unicast_packet
) +
947 ethhdr
= (struct ethhdr
*)(skb
->data
+
948 sizeof(struct unicast_packet
));
950 /* we don't have an updated route for this client, so we should
951 * not try to reroute the packet!!
953 if (batadv_tt_global_client_is_roaming(bat_priv
,
957 orig_node
= batadv_transtable_search(bat_priv
, NULL
,
961 if (!batadv_is_my_client(bat_priv
, ethhdr
->h_dest
))
963 primary_if
= batadv_primary_if_get_selected(bat_priv
);
966 memcpy(unicast_packet
->dest
,
967 primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
968 batadv_hardif_free_ref(primary_if
);
970 memcpy(unicast_packet
->dest
, orig_node
->orig
,
972 curr_ttvn
= (uint8_t)
973 atomic_read(&orig_node
->last_ttvn
);
974 batadv_orig_node_free_ref(orig_node
);
977 bat_dbg(DBG_ROUTES
, bat_priv
,
978 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
979 unicast_packet
->ttvn
, curr_ttvn
, ethhdr
->h_dest
,
980 unicast_packet
->dest
);
982 unicast_packet
->ttvn
= curr_ttvn
;
987 int batadv_recv_unicast_packet(struct sk_buff
*skb
, struct hard_iface
*recv_if
)
989 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
990 struct unicast_packet
*unicast_packet
;
991 int hdr_size
= sizeof(*unicast_packet
);
993 if (check_unicast_packet(skb
, hdr_size
) < 0)
996 if (!check_unicast_ttvn(bat_priv
, skb
))
999 unicast_packet
= (struct unicast_packet
*)skb
->data
;
1002 if (batadv_is_my_mac(unicast_packet
->dest
)) {
1003 batadv_interface_rx(recv_if
->soft_iface
, skb
, recv_if
,
1005 return NET_RX_SUCCESS
;
1008 return route_unicast_packet(skb
, recv_if
);
1011 int batadv_recv_ucast_frag_packet(struct sk_buff
*skb
,
1012 struct hard_iface
*recv_if
)
1014 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1015 struct unicast_frag_packet
*unicast_packet
;
1016 int hdr_size
= sizeof(*unicast_packet
);
1017 struct sk_buff
*new_skb
= NULL
;
1020 if (check_unicast_packet(skb
, hdr_size
) < 0)
1023 if (!check_unicast_ttvn(bat_priv
, skb
))
1026 unicast_packet
= (struct unicast_frag_packet
*)skb
->data
;
1029 if (batadv_is_my_mac(unicast_packet
->dest
)) {
1031 ret
= batadv_frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
1033 if (ret
== NET_RX_DROP
)
1036 /* packet was buffered for late merge */
1038 return NET_RX_SUCCESS
;
1040 batadv_interface_rx(recv_if
->soft_iface
, new_skb
, recv_if
,
1041 sizeof(struct unicast_packet
));
1042 return NET_RX_SUCCESS
;
1045 return route_unicast_packet(skb
, recv_if
);
1049 int batadv_recv_bcast_packet(struct sk_buff
*skb
, struct hard_iface
*recv_if
)
1051 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1052 struct orig_node
*orig_node
= NULL
;
1053 struct bcast_packet
*bcast_packet
;
1054 struct ethhdr
*ethhdr
;
1055 int hdr_size
= sizeof(*bcast_packet
);
1056 int ret
= NET_RX_DROP
;
1059 /* drop packet if it has not necessary minimum size */
1060 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1063 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1065 /* packet with broadcast indication but unicast recipient */
1066 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
1069 /* packet with broadcast sender address */
1070 if (is_broadcast_ether_addr(ethhdr
->h_source
))
1073 /* ignore broadcasts sent by myself */
1074 if (batadv_is_my_mac(ethhdr
->h_source
))
1077 bcast_packet
= (struct bcast_packet
*)skb
->data
;
1079 /* ignore broadcasts originated by myself */
1080 if (batadv_is_my_mac(bcast_packet
->orig
))
1083 if (bcast_packet
->header
.ttl
< 2)
1086 orig_node
= batadv_orig_hash_find(bat_priv
, bcast_packet
->orig
);
1091 spin_lock_bh(&orig_node
->bcast_seqno_lock
);
1093 /* check whether the packet is a duplicate */
1094 if (batadv_test_bit(orig_node
->bcast_bits
, orig_node
->last_bcast_seqno
,
1095 ntohl(bcast_packet
->seqno
)))
1098 seq_diff
= ntohl(bcast_packet
->seqno
) - orig_node
->last_bcast_seqno
;
1100 /* check whether the packet is old and the host just restarted. */
1101 if (batadv_window_protected(bat_priv
, seq_diff
,
1102 &orig_node
->bcast_seqno_reset
))
1105 /* mark broadcast in flood history, update window position
1108 if (batadv_bit_get_packet(bat_priv
, orig_node
->bcast_bits
, seq_diff
, 1))
1109 orig_node
->last_bcast_seqno
= ntohl(bcast_packet
->seqno
);
1111 spin_unlock_bh(&orig_node
->bcast_seqno_lock
);
1113 /* check whether this has been sent by another originator before */
1114 if (batadv_bla_check_bcast_duplist(bat_priv
, bcast_packet
, hdr_size
))
1117 /* rebroadcast packet */
1118 batadv_add_bcast_packet_to_list(bat_priv
, skb
, 1);
1120 /* don't hand the broadcast up if it is from an originator
1121 * from the same backbone.
1123 if (batadv_bla_is_backbone_gw(skb
, orig_node
, hdr_size
))
1126 /* broadcast for me */
1127 batadv_interface_rx(recv_if
->soft_iface
, skb
, recv_if
, hdr_size
);
1128 ret
= NET_RX_SUCCESS
;
1132 spin_unlock_bh(&orig_node
->bcast_seqno_lock
);
1135 batadv_orig_node_free_ref(orig_node
);
1139 int batadv_recv_vis_packet(struct sk_buff
*skb
, struct hard_iface
*recv_if
)
1141 struct vis_packet
*vis_packet
;
1142 struct ethhdr
*ethhdr
;
1143 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1144 int hdr_size
= sizeof(*vis_packet
);
1146 /* keep skb linear */
1147 if (skb_linearize(skb
) < 0)
1150 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1153 vis_packet
= (struct vis_packet
*)skb
->data
;
1154 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1157 if (!batadv_is_my_mac(ethhdr
->h_dest
))
1160 /* ignore own packets */
1161 if (batadv_is_my_mac(vis_packet
->vis_orig
))
1164 if (batadv_is_my_mac(vis_packet
->sender_orig
))
1167 switch (vis_packet
->vis_type
) {
1168 case VIS_TYPE_SERVER_SYNC
:
1169 batadv_receive_server_sync_packet(bat_priv
, vis_packet
,
1173 case VIS_TYPE_CLIENT_UPDATE
:
1174 batadv_receive_client_update_packet(bat_priv
, vis_packet
,
1178 default: /* ignore unknown packet */
1182 /* We take a copy of the data in the packet, so we should
1183 * always free the skbuf.