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 static void _batadv_update_route(struct batadv_priv
*bat_priv
,
38 struct batadv_orig_node
*orig_node
,
39 struct batadv_neigh_node
*neigh_node
)
41 struct batadv_neigh_node
*curr_router
;
43 curr_router
= batadv_orig_node_get_router(orig_node
);
46 if ((curr_router
) && (!neigh_node
)) {
47 batadv_dbg(BATADV_DBG_ROUTES
, bat_priv
,
48 "Deleting route towards: %pM\n", orig_node
->orig
);
49 batadv_tt_global_del_orig(bat_priv
, orig_node
,
50 "Deleted route towards originator");
53 } else if ((!curr_router
) && (neigh_node
)) {
54 batadv_dbg(BATADV_DBG_ROUTES
, bat_priv
,
55 "Adding route towards: %pM (via %pM)\n",
56 orig_node
->orig
, neigh_node
->addr
);
58 } else if (neigh_node
&& curr_router
) {
59 batadv_dbg(BATADV_DBG_ROUTES
, bat_priv
,
60 "Changing route towards: %pM (now via %pM - was via %pM)\n",
61 orig_node
->orig
, neigh_node
->addr
,
66 batadv_neigh_node_free_ref(curr_router
);
68 /* increase refcount of new best neighbor */
69 if (neigh_node
&& !atomic_inc_not_zero(&neigh_node
->refcount
))
72 spin_lock_bh(&orig_node
->neigh_list_lock
);
73 /* curr_router used earlier may not be the current orig_node->router
74 * anymore because it was dereferenced outside of the neigh_list_lock
75 * protected region. After the new best neighbor has replace the current
76 * best neighbor the reference counter needs to decrease. Consequently,
77 * the code needs to ensure the curr_router variable contains a pointer
78 * to the replaced best neighbor.
80 curr_router
= rcu_dereference_protected(orig_node
->router
, true);
82 rcu_assign_pointer(orig_node
->router
, neigh_node
);
83 spin_unlock_bh(&orig_node
->neigh_list_lock
);
85 /* decrease refcount of previous best neighbor */
87 batadv_neigh_node_free_ref(curr_router
);
90 void batadv_update_route(struct batadv_priv
*bat_priv
,
91 struct batadv_orig_node
*orig_node
,
92 struct batadv_neigh_node
*neigh_node
)
94 struct batadv_neigh_node
*router
= NULL
;
99 router
= batadv_orig_node_get_router(orig_node
);
101 if (router
!= neigh_node
)
102 _batadv_update_route(bat_priv
, orig_node
, neigh_node
);
106 batadv_neigh_node_free_ref(router
);
109 /* caller must hold the neigh_list_lock */
110 void batadv_bonding_candidate_del(struct batadv_orig_node
*orig_node
,
111 struct batadv_neigh_node
*neigh_node
)
113 /* this neighbor is not part of our candidate list */
114 if (list_empty(&neigh_node
->bonding_list
))
117 list_del_rcu(&neigh_node
->bonding_list
);
118 INIT_LIST_HEAD(&neigh_node
->bonding_list
);
119 batadv_neigh_node_free_ref(neigh_node
);
120 atomic_dec(&orig_node
->bond_candidates
);
126 void batadv_bonding_candidate_add(struct batadv_orig_node
*orig_node
,
127 struct batadv_neigh_node
*neigh_node
)
129 struct batadv_neigh_node
*tmp_neigh_node
, *router
= NULL
;
130 uint8_t interference_candidate
= 0;
132 spin_lock_bh(&orig_node
->neigh_list_lock
);
134 /* only consider if it has the same primary address ... */
135 if (!batadv_compare_eth(orig_node
->orig
,
136 neigh_node
->orig_node
->primary_addr
))
139 router
= batadv_orig_node_get_router(orig_node
);
143 /* ... and is good enough to be considered */
144 if (neigh_node
->tq_avg
< router
->tq_avg
- BATADV_BONDING_TQ_THRESHOLD
)
147 /* check if we have another candidate with the same mac address or
148 * interface. If we do, we won't select this candidate because of
149 * possible interference.
151 hlist_for_each_entry_rcu(tmp_neigh_node
,
152 &orig_node
->neigh_list
, list
) {
153 if (tmp_neigh_node
== neigh_node
)
156 /* we only care if the other candidate is even
157 * considered as candidate.
159 if (list_empty(&tmp_neigh_node
->bonding_list
))
162 if ((neigh_node
->if_incoming
== tmp_neigh_node
->if_incoming
) ||
163 (batadv_compare_eth(neigh_node
->addr
,
164 tmp_neigh_node
->addr
))) {
165 interference_candidate
= 1;
170 /* don't care further if it is an interference candidate */
171 if (interference_candidate
)
174 /* this neighbor already is part of our candidate list */
175 if (!list_empty(&neigh_node
->bonding_list
))
178 if (!atomic_inc_not_zero(&neigh_node
->refcount
))
181 list_add_rcu(&neigh_node
->bonding_list
, &orig_node
->bond_list
);
182 atomic_inc(&orig_node
->bond_candidates
);
186 batadv_bonding_candidate_del(orig_node
, neigh_node
);
189 spin_unlock_bh(&orig_node
->neigh_list_lock
);
192 batadv_neigh_node_free_ref(router
);
195 /* copy primary address for bonding */
197 batadv_bonding_save_primary(const struct batadv_orig_node
*orig_node
,
198 struct batadv_orig_node
*orig_neigh_node
,
199 const struct batadv_ogm_packet
*batman_ogm_packet
)
201 if (!(batman_ogm_packet
->flags
& BATADV_PRIMARIES_FIRST_HOP
))
204 memcpy(orig_neigh_node
->primary_addr
, orig_node
->orig
, ETH_ALEN
);
207 /* checks whether the host restarted and is in the protection time.
209 * 0 if the packet is to be accepted
210 * 1 if the packet is to be ignored.
212 int batadv_window_protected(struct batadv_priv
*bat_priv
, int32_t seq_num_diff
,
213 unsigned long *last_reset
)
215 if (seq_num_diff
<= -BATADV_TQ_LOCAL_WINDOW_SIZE
||
216 seq_num_diff
>= BATADV_EXPECTED_SEQNO_RANGE
) {
217 if (!batadv_has_timed_out(*last_reset
,
218 BATADV_RESET_PROTECTION_MS
))
221 *last_reset
= jiffies
;
222 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
223 "old packet received, start protection\n");
229 bool batadv_check_management_packet(struct sk_buff
*skb
,
230 struct batadv_hard_iface
*hard_iface
,
233 struct ethhdr
*ethhdr
;
235 /* drop packet if it has not necessary minimum size */
236 if (unlikely(!pskb_may_pull(skb
, header_len
)))
239 ethhdr
= eth_hdr(skb
);
241 /* packet with broadcast indication but unicast recipient */
242 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
245 /* packet with broadcast sender address */
246 if (is_broadcast_ether_addr(ethhdr
->h_source
))
249 /* create a copy of the skb, if needed, to modify it. */
250 if (skb_cow(skb
, 0) < 0)
253 /* keep skb linear */
254 if (skb_linearize(skb
) < 0)
260 static int batadv_recv_my_icmp_packet(struct batadv_priv
*bat_priv
,
261 struct sk_buff
*skb
, size_t icmp_len
)
263 struct batadv_hard_iface
*primary_if
= NULL
;
264 struct batadv_orig_node
*orig_node
= NULL
;
265 struct batadv_icmp_packet_rr
*icmp_packet
;
266 int ret
= NET_RX_DROP
;
268 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
270 /* add data to device queue */
271 if (icmp_packet
->msg_type
!= BATADV_ECHO_REQUEST
) {
272 batadv_socket_receive_packet(icmp_packet
, icmp_len
);
276 primary_if
= batadv_primary_if_get_selected(bat_priv
);
280 /* answer echo request (ping) */
281 /* get routing information */
282 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->orig
);
286 /* create a copy of the skb, if needed, to modify it. */
287 if (skb_cow(skb
, ETH_HLEN
) < 0)
290 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
292 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
293 memcpy(icmp_packet
->orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
294 icmp_packet
->msg_type
= BATADV_ECHO_REPLY
;
295 icmp_packet
->header
.ttl
= BATADV_TTL
;
297 if (batadv_send_skb_to_orig(skb
, orig_node
, NULL
) != NET_XMIT_DROP
)
298 ret
= NET_RX_SUCCESS
;
302 batadv_hardif_free_ref(primary_if
);
304 batadv_orig_node_free_ref(orig_node
);
308 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv
*bat_priv
,
311 struct batadv_hard_iface
*primary_if
= NULL
;
312 struct batadv_orig_node
*orig_node
= NULL
;
313 struct batadv_icmp_packet
*icmp_packet
;
314 int ret
= NET_RX_DROP
;
316 icmp_packet
= (struct batadv_icmp_packet
*)skb
->data
;
318 /* send TTL exceeded if packet is an echo request (traceroute) */
319 if (icmp_packet
->msg_type
!= BATADV_ECHO_REQUEST
) {
320 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
321 icmp_packet
->orig
, icmp_packet
->dst
);
325 primary_if
= batadv_primary_if_get_selected(bat_priv
);
329 /* get routing information */
330 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->orig
);
334 /* create a copy of the skb, if needed, to modify it. */
335 if (skb_cow(skb
, ETH_HLEN
) < 0)
338 icmp_packet
= (struct batadv_icmp_packet
*)skb
->data
;
340 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
341 memcpy(icmp_packet
->orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
342 icmp_packet
->msg_type
= BATADV_TTL_EXCEEDED
;
343 icmp_packet
->header
.ttl
= BATADV_TTL
;
345 if (batadv_send_skb_to_orig(skb
, orig_node
, NULL
) != NET_XMIT_DROP
)
346 ret
= NET_RX_SUCCESS
;
350 batadv_hardif_free_ref(primary_if
);
352 batadv_orig_node_free_ref(orig_node
);
357 int batadv_recv_icmp_packet(struct sk_buff
*skb
,
358 struct batadv_hard_iface
*recv_if
)
360 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
361 struct batadv_icmp_packet_rr
*icmp_packet
;
362 struct ethhdr
*ethhdr
;
363 struct batadv_orig_node
*orig_node
= NULL
;
364 int hdr_size
= sizeof(struct batadv_icmp_packet
);
365 int ret
= NET_RX_DROP
;
367 /* we truncate all incoming icmp packets if they don't match our size */
368 if (skb
->len
>= sizeof(struct batadv_icmp_packet_rr
))
369 hdr_size
= sizeof(struct batadv_icmp_packet_rr
);
371 /* drop packet if it has not necessary minimum size */
372 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
375 ethhdr
= eth_hdr(skb
);
377 /* packet with unicast indication but broadcast recipient */
378 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
381 /* packet with broadcast sender address */
382 if (is_broadcast_ether_addr(ethhdr
->h_source
))
386 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
389 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
391 /* add record route information if not full */
392 if ((hdr_size
== sizeof(struct batadv_icmp_packet_rr
)) &&
393 (icmp_packet
->rr_cur
< BATADV_RR_LEN
)) {
394 memcpy(&(icmp_packet
->rr
[icmp_packet
->rr_cur
]),
395 ethhdr
->h_dest
, ETH_ALEN
);
396 icmp_packet
->rr_cur
++;
400 if (batadv_is_my_mac(bat_priv
, icmp_packet
->dst
))
401 return batadv_recv_my_icmp_packet(bat_priv
, skb
, hdr_size
);
404 if (icmp_packet
->header
.ttl
< 2)
405 return batadv_recv_icmp_ttl_exceeded(bat_priv
, skb
);
407 /* get routing information */
408 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->dst
);
412 /* create a copy of the skb, if needed, to modify it. */
413 if (skb_cow(skb
, ETH_HLEN
) < 0)
416 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb
->data
;
419 icmp_packet
->header
.ttl
--;
422 if (batadv_send_skb_to_orig(skb
, orig_node
, recv_if
) != NET_XMIT_DROP
)
423 ret
= NET_RX_SUCCESS
;
427 batadv_orig_node_free_ref(orig_node
);
431 /* In the bonding case, send the packets in a round
432 * robin fashion over the remaining interfaces.
434 * This method rotates the bonding list and increases the
435 * returned router's refcount.
437 static struct batadv_neigh_node
*
438 batadv_find_bond_router(struct batadv_orig_node
*primary_orig
,
439 const struct batadv_hard_iface
*recv_if
)
441 struct batadv_neigh_node
*tmp_neigh_node
;
442 struct batadv_neigh_node
*router
= NULL
, *first_candidate
= NULL
;
445 list_for_each_entry_rcu(tmp_neigh_node
, &primary_orig
->bond_list
,
447 if (!first_candidate
)
448 first_candidate
= tmp_neigh_node
;
450 /* recv_if == NULL on the first node. */
451 if (tmp_neigh_node
->if_incoming
== recv_if
)
454 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
457 router
= tmp_neigh_node
;
461 /* use the first candidate if nothing was found. */
462 if (!router
&& first_candidate
&&
463 atomic_inc_not_zero(&first_candidate
->refcount
))
464 router
= first_candidate
;
469 /* selected should point to the next element
470 * after the current router
472 spin_lock_bh(&primary_orig
->neigh_list_lock
);
473 /* this is a list_move(), which unfortunately
474 * does not exist as rcu version
476 list_del_rcu(&primary_orig
->bond_list
);
477 list_add_rcu(&primary_orig
->bond_list
,
478 &router
->bonding_list
);
479 spin_unlock_bh(&primary_orig
->neigh_list_lock
);
486 /* Interface Alternating: Use the best of the
487 * remaining candidates which are not using
490 * Increases the returned router's refcount
492 static struct batadv_neigh_node
*
493 batadv_find_ifalter_router(struct batadv_orig_node
*primary_orig
,
494 const struct batadv_hard_iface
*recv_if
)
496 struct batadv_neigh_node
*tmp_neigh_node
;
497 struct batadv_neigh_node
*router
= NULL
, *first_candidate
= NULL
;
500 list_for_each_entry_rcu(tmp_neigh_node
, &primary_orig
->bond_list
,
502 if (!first_candidate
)
503 first_candidate
= tmp_neigh_node
;
505 /* recv_if == NULL on the first node. */
506 if (tmp_neigh_node
->if_incoming
== recv_if
)
509 if (router
&& tmp_neigh_node
->tq_avg
<= router
->tq_avg
)
512 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
515 /* decrement refcount of previously selected router */
517 batadv_neigh_node_free_ref(router
);
519 /* we found a better router (or at least one valid router) */
520 router
= tmp_neigh_node
;
523 /* use the first candidate if nothing was found. */
524 if (!router
&& first_candidate
&&
525 atomic_inc_not_zero(&first_candidate
->refcount
))
526 router
= first_candidate
;
533 * batadv_check_unicast_packet - Check for malformed unicast packets
534 * @bat_priv: the bat priv with all the soft interface information
535 * @skb: packet to check
536 * @hdr_size: size of header to pull
538 * Check for short header and bad addresses in given packet. Returns negative
539 * value when check fails and 0 otherwise. The negative value depends on the
540 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
541 * and -EREMOTE for non-local (other host) destination.
543 static int batadv_check_unicast_packet(struct batadv_priv
*bat_priv
,
544 struct sk_buff
*skb
, int hdr_size
)
546 struct ethhdr
*ethhdr
;
548 /* drop packet if it has not necessary minimum size */
549 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
552 ethhdr
= eth_hdr(skb
);
554 /* packet with unicast indication but broadcast recipient */
555 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
558 /* packet with broadcast sender address */
559 if (is_broadcast_ether_addr(ethhdr
->h_source
))
563 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
569 int batadv_recv_tt_query(struct sk_buff
*skb
, struct batadv_hard_iface
*recv_if
)
571 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
572 struct batadv_tt_query_packet
*tt_query
;
574 int hdr_size
= sizeof(*tt_query
);
578 if (batadv_check_unicast_packet(bat_priv
, skb
, hdr_size
) < 0)
581 /* I could need to modify it */
582 if (skb_cow(skb
, sizeof(struct batadv_tt_query_packet
)) < 0)
585 tt_query
= (struct batadv_tt_query_packet
*)skb
->data
;
587 switch (tt_query
->flags
& BATADV_TT_QUERY_TYPE_MASK
) {
588 case BATADV_TT_REQUEST
:
589 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
591 /* If we cannot provide an answer the tt_request is
594 if (!batadv_send_tt_response(bat_priv
, tt_query
)) {
595 if (tt_query
->flags
& BATADV_TT_FULL_TABLE
)
600 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
601 "Routing TT_REQUEST to %pM [%c]\n",
604 return batadv_route_unicast_packet(skb
, recv_if
);
607 case BATADV_TT_RESPONSE
:
608 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
610 if (batadv_is_my_mac(bat_priv
, tt_query
->dst
)) {
611 /* packet needs to be linearized to access the TT
614 if (skb_linearize(skb
) < 0)
616 /* skb_linearize() possibly changed skb->data */
617 tt_query
= (struct batadv_tt_query_packet
*)skb
->data
;
619 tt_size
= batadv_tt_len(ntohs(tt_query
->tt_data
));
621 /* Ensure we have all the claimed data */
622 packet_size
= sizeof(struct batadv_tt_query_packet
);
623 packet_size
+= tt_size
;
624 if (unlikely(skb_headlen(skb
) < packet_size
))
627 batadv_handle_tt_response(bat_priv
, tt_query
);
629 if (tt_query
->flags
& BATADV_TT_FULL_TABLE
)
633 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
634 "Routing TT_RESPONSE to %pM [%c]\n",
637 return batadv_route_unicast_packet(skb
, recv_if
);
643 /* returning NET_RX_DROP will make the caller function kfree the skb */
647 int batadv_recv_roam_adv(struct sk_buff
*skb
, struct batadv_hard_iface
*recv_if
)
649 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
650 struct batadv_roam_adv_packet
*roam_adv_packet
;
651 struct batadv_orig_node
*orig_node
;
653 if (batadv_check_unicast_packet(bat_priv
, skb
,
654 sizeof(*roam_adv_packet
)) < 0)
657 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
659 roam_adv_packet
= (struct batadv_roam_adv_packet
*)skb
->data
;
661 if (!batadv_is_my_mac(bat_priv
, roam_adv_packet
->dst
))
662 return batadv_route_unicast_packet(skb
, recv_if
);
664 /* check if it is a backbone gateway. we don't accept
665 * roaming advertisement from it, as it has the same
666 * entries as we have.
668 if (batadv_bla_is_backbone_gw_orig(bat_priv
, roam_adv_packet
->src
))
671 orig_node
= batadv_orig_hash_find(bat_priv
, roam_adv_packet
->src
);
675 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
676 "Received ROAMING_ADV from %pM (client %pM)\n",
677 roam_adv_packet
->src
, roam_adv_packet
->client
);
679 batadv_tt_global_add(bat_priv
, orig_node
, roam_adv_packet
->client
,
680 BATADV_TT_CLIENT_ROAM
,
681 atomic_read(&orig_node
->last_ttvn
) + 1);
683 batadv_orig_node_free_ref(orig_node
);
685 /* returning NET_RX_DROP will make the caller function kfree the skb */
689 /* find a suitable router for this originator, and use
690 * bonding if possible. increases the found neighbors
693 struct batadv_neigh_node
*
694 batadv_find_router(struct batadv_priv
*bat_priv
,
695 struct batadv_orig_node
*orig_node
,
696 const struct batadv_hard_iface
*recv_if
)
698 struct batadv_orig_node
*primary_orig_node
;
699 struct batadv_orig_node
*router_orig
;
700 struct batadv_neigh_node
*router
;
701 static uint8_t zero_mac
[ETH_ALEN
] = {0, 0, 0, 0, 0, 0};
703 uint8_t *primary_addr
;
708 router
= batadv_orig_node_get_router(orig_node
);
712 /* without bonding, the first node should
713 * always choose the default router.
715 bonding_enabled
= atomic_read(&bat_priv
->bonding
);
718 /* select default router to output */
719 router_orig
= router
->orig_node
;
723 if ((!recv_if
) && (!bonding_enabled
))
726 primary_addr
= router_orig
->primary_addr
;
728 /* if we have something in the primary_addr, we can search
729 * for a potential bonding candidate.
731 if (batadv_compare_eth(primary_addr
, zero_mac
))
734 /* find the orig_node which has the primary interface. might
735 * even be the same as our router_orig in many cases
737 if (batadv_compare_eth(primary_addr
, router_orig
->orig
)) {
738 primary_orig_node
= router_orig
;
740 primary_orig_node
= batadv_orig_hash_find(bat_priv
,
742 if (!primary_orig_node
)
745 batadv_orig_node_free_ref(primary_orig_node
);
748 /* with less than 2 candidates, we can't do any
749 * bonding and prefer the original router.
751 if (atomic_read(&primary_orig_node
->bond_candidates
) < 2)
754 /* all nodes between should choose a candidate which
755 * is is not on the interface where the packet came
758 batadv_neigh_node_free_ref(router
);
761 router
= batadv_find_bond_router(primary_orig_node
, recv_if
);
763 router
= batadv_find_ifalter_router(primary_orig_node
, recv_if
);
766 if (router
&& router
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
775 batadv_neigh_node_free_ref(router
);
779 static int batadv_route_unicast_packet(struct sk_buff
*skb
,
780 struct batadv_hard_iface
*recv_if
)
782 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
783 struct batadv_orig_node
*orig_node
= NULL
;
784 struct batadv_neigh_node
*neigh_node
= NULL
;
785 struct batadv_unicast_packet
*unicast_packet
;
786 struct ethhdr
*ethhdr
= eth_hdr(skb
);
787 int res
, hdr_len
, ret
= NET_RX_DROP
;
788 struct sk_buff
*new_skb
;
790 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
793 if (unicast_packet
->header
.ttl
< 2) {
794 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
795 ethhdr
->h_source
, unicast_packet
->dest
);
799 /* get routing information */
800 orig_node
= batadv_orig_hash_find(bat_priv
, unicast_packet
->dest
);
805 /* find_router() increases neigh_nodes refcount if found. */
806 neigh_node
= batadv_find_router(bat_priv
, orig_node
, recv_if
);
811 /* create a copy of the skb, if needed, to modify it. */
812 if (skb_cow(skb
, ETH_HLEN
) < 0)
815 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
817 if (unicast_packet
->header
.packet_type
== BATADV_UNICAST
&&
818 atomic_read(&bat_priv
->fragmentation
) &&
819 skb
->len
> neigh_node
->if_incoming
->net_dev
->mtu
) {
820 ret
= batadv_frag_send_skb(skb
, bat_priv
,
821 neigh_node
->if_incoming
,
826 if (unicast_packet
->header
.packet_type
== BATADV_UNICAST_FRAG
&&
827 batadv_frag_can_reassemble(skb
,
828 neigh_node
->if_incoming
->net_dev
->mtu
)) {
829 ret
= batadv_frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
831 if (ret
== NET_RX_DROP
)
834 /* packet was buffered for late merge */
836 ret
= NET_RX_SUCCESS
;
841 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
845 unicast_packet
->header
.ttl
--;
847 switch (unicast_packet
->header
.packet_type
) {
848 case BATADV_UNICAST_4ADDR
:
849 hdr_len
= sizeof(struct batadv_unicast_4addr_packet
);
852 hdr_len
= sizeof(struct batadv_unicast_packet
);
855 /* other packet types not supported - yet */
861 batadv_skb_set_priority(skb
, hdr_len
);
863 res
= batadv_send_skb_to_orig(skb
, orig_node
, recv_if
);
865 /* translate transmit result into receive result */
866 if (res
== NET_XMIT_SUCCESS
) {
867 /* skb was transmitted and consumed */
868 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
869 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
870 skb
->len
+ ETH_HLEN
);
872 ret
= NET_RX_SUCCESS
;
873 } else if (res
== NET_XMIT_POLICED
) {
874 /* skb was buffered and consumed */
875 ret
= NET_RX_SUCCESS
;
880 batadv_neigh_node_free_ref(neigh_node
);
882 batadv_orig_node_free_ref(orig_node
);
887 * batadv_reroute_unicast_packet - update the unicast header for re-routing
888 * @bat_priv: the bat priv with all the soft interface information
889 * @unicast_packet: the unicast header to be updated
890 * @dst_addr: the payload destination
892 * Search the translation table for dst_addr and update the unicast header with
893 * the new corresponding information (originator address where the destination
894 * client currently is and its known TTVN)
896 * Returns true if the packet header has been updated, false otherwise
899 batadv_reroute_unicast_packet(struct batadv_priv
*bat_priv
,
900 struct batadv_unicast_packet
*unicast_packet
,
903 struct batadv_orig_node
*orig_node
= NULL
;
904 struct batadv_hard_iface
*primary_if
= NULL
;
906 uint8_t *orig_addr
, orig_ttvn
;
908 if (batadv_is_my_client(bat_priv
, dst_addr
)) {
909 primary_if
= batadv_primary_if_get_selected(bat_priv
);
912 orig_addr
= primary_if
->net_dev
->dev_addr
;
913 orig_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
915 orig_node
= batadv_transtable_search(bat_priv
, NULL
, dst_addr
);
919 if (batadv_compare_eth(orig_node
->orig
, unicast_packet
->dest
))
922 orig_addr
= orig_node
->orig
;
923 orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
926 /* update the packet header */
927 memcpy(unicast_packet
->dest
, orig_addr
, ETH_ALEN
);
928 unicast_packet
->ttvn
= orig_ttvn
;
933 batadv_hardif_free_ref(primary_if
);
935 batadv_orig_node_free_ref(orig_node
);
940 static int batadv_check_unicast_ttvn(struct batadv_priv
*bat_priv
,
941 struct sk_buff
*skb
, int hdr_len
) {
942 uint8_t curr_ttvn
, old_ttvn
;
943 struct batadv_orig_node
*orig_node
;
944 struct ethhdr
*ethhdr
;
945 struct batadv_hard_iface
*primary_if
;
946 struct batadv_unicast_packet
*unicast_packet
;
949 /* check if there is enough data before accessing it */
950 if (pskb_may_pull(skb
, hdr_len
+ ETH_HLEN
) < 0)
953 /* create a copy of the skb (in case of for re-routing) to modify it. */
954 if (skb_cow(skb
, sizeof(*unicast_packet
)) < 0)
957 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
958 ethhdr
= (struct ethhdr
*)(skb
->data
+ hdr_len
);
960 /* check if the destination client was served by this node and it is now
961 * roaming. In this case, it means that the node has got a ROAM_ADV
962 * message and that it knows the new destination in the mesh to re-route
965 if (batadv_tt_local_client_is_roaming(bat_priv
, ethhdr
->h_dest
)) {
966 if (batadv_reroute_unicast_packet(bat_priv
, unicast_packet
,
968 net_ratelimited_function(batadv_dbg
, BATADV_DBG_TT
,
970 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
971 unicast_packet
->dest
,
973 /* at this point the mesh destination should have been
974 * substituted with the originator address found in the global
975 * table. If not, let the packet go untouched anyway because
976 * there is nothing the node can do
981 /* retrieve the TTVN known by this node for the packet destination. This
982 * value is used later to check if the node which sent (or re-routed
983 * last time) the packet had an updated information or not
985 curr_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
986 if (!batadv_is_my_mac(bat_priv
, unicast_packet
->dest
)) {
987 orig_node
= batadv_orig_hash_find(bat_priv
,
988 unicast_packet
->dest
);
989 /* if it is not possible to find the orig_node representing the
990 * destination, the packet can immediately be dropped as it will
991 * not be possible to deliver it
996 curr_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
997 batadv_orig_node_free_ref(orig_node
);
1000 /* check if the TTVN contained in the packet is fresher than what the
1003 is_old_ttvn
= batadv_seq_before(unicast_packet
->ttvn
, curr_ttvn
);
1007 old_ttvn
= unicast_packet
->ttvn
;
1008 /* the packet was forged based on outdated network information. Its
1009 * destination can possibly be updated and forwarded towards the new
1012 if (batadv_reroute_unicast_packet(bat_priv
, unicast_packet
,
1014 net_ratelimited_function(batadv_dbg
, BATADV_DBG_TT
, bat_priv
,
1015 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
1016 unicast_packet
->dest
, ethhdr
->h_dest
,
1017 old_ttvn
, curr_ttvn
);
1021 /* the packet has not been re-routed: either the destination is
1022 * currently served by this node or there is no destination at all and
1023 * it is possible to drop the packet
1025 if (!batadv_is_my_client(bat_priv
, ethhdr
->h_dest
))
1028 /* update the header in order to let the packet be delivered to this
1029 * node's soft interface
1031 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1035 memcpy(unicast_packet
->dest
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1037 batadv_hardif_free_ref(primary_if
);
1039 unicast_packet
->ttvn
= curr_ttvn
;
1044 int batadv_recv_unicast_packet(struct sk_buff
*skb
,
1045 struct batadv_hard_iface
*recv_if
)
1047 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1048 struct batadv_unicast_packet
*unicast_packet
;
1049 struct batadv_unicast_4addr_packet
*unicast_4addr_packet
;
1051 struct batadv_orig_node
*orig_node
= NULL
;
1052 int check
, hdr_size
= sizeof(*unicast_packet
);
1055 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
1056 unicast_4addr_packet
= (struct batadv_unicast_4addr_packet
*)skb
->data
;
1058 is4addr
= unicast_packet
->header
.packet_type
== BATADV_UNICAST_4ADDR
;
1059 /* the caller function should have already pulled 2 bytes */
1061 hdr_size
= sizeof(*unicast_4addr_packet
);
1063 /* function returns -EREMOTE for promiscuous packets */
1064 check
= batadv_check_unicast_packet(bat_priv
, skb
, hdr_size
);
1066 /* Even though the packet is not for us, we might save it to use for
1067 * decoding a later received coded packet
1069 if (check
== -EREMOTE
)
1070 batadv_nc_skb_store_sniffed_unicast(bat_priv
, skb
);
1074 if (!batadv_check_unicast_ttvn(bat_priv
, skb
, hdr_size
))
1078 if (batadv_is_my_mac(bat_priv
, unicast_packet
->dest
)) {
1080 batadv_dat_inc_counter(bat_priv
,
1081 unicast_4addr_packet
->subtype
);
1082 orig_addr
= unicast_4addr_packet
->src
;
1083 orig_node
= batadv_orig_hash_find(bat_priv
, orig_addr
);
1086 if (batadv_dat_snoop_incoming_arp_request(bat_priv
, skb
,
1089 if (batadv_dat_snoop_incoming_arp_reply(bat_priv
, skb
,
1093 batadv_interface_rx(recv_if
->soft_iface
, skb
, recv_if
, hdr_size
,
1098 batadv_orig_node_free_ref(orig_node
);
1100 return NET_RX_SUCCESS
;
1103 return batadv_route_unicast_packet(skb
, recv_if
);
1106 int batadv_recv_ucast_frag_packet(struct sk_buff
*skb
,
1107 struct batadv_hard_iface
*recv_if
)
1109 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1110 struct batadv_unicast_frag_packet
*unicast_packet
;
1111 int hdr_size
= sizeof(*unicast_packet
);
1112 struct sk_buff
*new_skb
= NULL
;
1115 if (batadv_check_unicast_packet(bat_priv
, skb
, hdr_size
) < 0)
1118 if (!batadv_check_unicast_ttvn(bat_priv
, skb
, hdr_size
))
1121 unicast_packet
= (struct batadv_unicast_frag_packet
*)skb
->data
;
1124 if (batadv_is_my_mac(bat_priv
, unicast_packet
->dest
)) {
1125 ret
= batadv_frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
1127 if (ret
== NET_RX_DROP
)
1130 /* packet was buffered for late merge */
1132 return NET_RX_SUCCESS
;
1134 if (batadv_dat_snoop_incoming_arp_request(bat_priv
, new_skb
,
1137 if (batadv_dat_snoop_incoming_arp_reply(bat_priv
, new_skb
,
1141 batadv_interface_rx(recv_if
->soft_iface
, new_skb
, recv_if
,
1142 sizeof(struct batadv_unicast_packet
), NULL
);
1145 return NET_RX_SUCCESS
;
1148 return batadv_route_unicast_packet(skb
, recv_if
);
1152 int batadv_recv_bcast_packet(struct sk_buff
*skb
,
1153 struct batadv_hard_iface
*recv_if
)
1155 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1156 struct batadv_orig_node
*orig_node
= NULL
;
1157 struct batadv_bcast_packet
*bcast_packet
;
1158 struct ethhdr
*ethhdr
;
1159 int hdr_size
= sizeof(*bcast_packet
);
1160 int ret
= NET_RX_DROP
;
1163 /* drop packet if it has not necessary minimum size */
1164 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1167 ethhdr
= eth_hdr(skb
);
1169 /* packet with broadcast indication but unicast recipient */
1170 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
1173 /* packet with broadcast sender address */
1174 if (is_broadcast_ether_addr(ethhdr
->h_source
))
1177 /* ignore broadcasts sent by myself */
1178 if (batadv_is_my_mac(bat_priv
, ethhdr
->h_source
))
1181 bcast_packet
= (struct batadv_bcast_packet
*)skb
->data
;
1183 /* ignore broadcasts originated by myself */
1184 if (batadv_is_my_mac(bat_priv
, bcast_packet
->orig
))
1187 if (bcast_packet
->header
.ttl
< 2)
1190 orig_node
= batadv_orig_hash_find(bat_priv
, bcast_packet
->orig
);
1195 spin_lock_bh(&orig_node
->bcast_seqno_lock
);
1197 /* check whether the packet is a duplicate */
1198 if (batadv_test_bit(orig_node
->bcast_bits
, orig_node
->last_bcast_seqno
,
1199 ntohl(bcast_packet
->seqno
)))
1202 seq_diff
= ntohl(bcast_packet
->seqno
) - orig_node
->last_bcast_seqno
;
1204 /* check whether the packet is old and the host just restarted. */
1205 if (batadv_window_protected(bat_priv
, seq_diff
,
1206 &orig_node
->bcast_seqno_reset
))
1209 /* mark broadcast in flood history, update window position
1212 if (batadv_bit_get_packet(bat_priv
, orig_node
->bcast_bits
, seq_diff
, 1))
1213 orig_node
->last_bcast_seqno
= ntohl(bcast_packet
->seqno
);
1215 spin_unlock_bh(&orig_node
->bcast_seqno_lock
);
1217 /* check whether this has been sent by another originator before */
1218 if (batadv_bla_check_bcast_duplist(bat_priv
, skb
))
1221 batadv_skb_set_priority(skb
, sizeof(struct batadv_bcast_packet
));
1223 /* rebroadcast packet */
1224 batadv_add_bcast_packet_to_list(bat_priv
, skb
, 1);
1226 /* don't hand the broadcast up if it is from an originator
1227 * from the same backbone.
1229 if (batadv_bla_is_backbone_gw(skb
, orig_node
, hdr_size
))
1232 if (batadv_dat_snoop_incoming_arp_request(bat_priv
, skb
, hdr_size
))
1234 if (batadv_dat_snoop_incoming_arp_reply(bat_priv
, skb
, hdr_size
))
1237 /* broadcast for me */
1238 batadv_interface_rx(recv_if
->soft_iface
, skb
, recv_if
, hdr_size
,
1242 ret
= NET_RX_SUCCESS
;
1246 spin_unlock_bh(&orig_node
->bcast_seqno_lock
);
1249 batadv_orig_node_free_ref(orig_node
);
1253 int batadv_recv_vis_packet(struct sk_buff
*skb
,
1254 struct batadv_hard_iface
*recv_if
)
1256 struct batadv_vis_packet
*vis_packet
;
1257 struct ethhdr
*ethhdr
;
1258 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1259 int hdr_size
= sizeof(*vis_packet
);
1261 /* keep skb linear */
1262 if (skb_linearize(skb
) < 0)
1265 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1268 vis_packet
= (struct batadv_vis_packet
*)skb
->data
;
1269 ethhdr
= eth_hdr(skb
);
1272 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
1275 /* ignore own packets */
1276 if (batadv_is_my_mac(bat_priv
, vis_packet
->vis_orig
))
1279 if (batadv_is_my_mac(bat_priv
, vis_packet
->sender_orig
))
1282 switch (vis_packet
->vis_type
) {
1283 case BATADV_VIS_TYPE_SERVER_SYNC
:
1284 batadv_receive_server_sync_packet(bat_priv
, vis_packet
,
1288 case BATADV_VIS_TYPE_CLIENT_UPDATE
:
1289 batadv_receive_client_update_packet(bat_priv
, vis_packet
,
1293 default: /* ignore unknown packet */
1297 /* We take a copy of the data in the packet, so we should
1298 * always free the skbuf.