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"
28 #include "bridge_loop_avoidance.h"
29 #include "distributed-arp-table.h"
30 #include "network-coding.h"
31 #include "fragmentation.h"
33 #include <linux/if_vlan.h>
35 static int batadv_route_unicast_packet(struct sk_buff *skb,
36 struct batadv_hard_iface *recv_if);
38 static void _batadv_update_route(struct batadv_priv *bat_priv,
39 struct batadv_orig_node *orig_node,
40 struct batadv_neigh_node *neigh_node)
42 struct batadv_neigh_node *curr_router;
44 curr_router = batadv_orig_node_get_router(orig_node);
47 if ((curr_router) && (!neigh_node)) {
48 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
49 "Deleting route towards: %pM\n", orig_node->orig);
50 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
51 "Deleted route towards originator");
54 } else if ((!curr_router) && (neigh_node)) {
55 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
56 "Adding route towards: %pM (via %pM)\n",
57 orig_node->orig, neigh_node->addr);
59 } else if (neigh_node && curr_router) {
60 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
61 "Changing route towards: %pM (now via %pM - was via %pM)\n",
62 orig_node->orig, neigh_node->addr,
67 batadv_neigh_node_free_ref(curr_router);
69 /* increase refcount of new best neighbor */
70 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
73 spin_lock_bh(&orig_node->neigh_list_lock);
74 rcu_assign_pointer(orig_node->router, neigh_node);
75 spin_unlock_bh(&orig_node->neigh_list_lock);
77 /* decrease refcount of previous best neighbor */
79 batadv_neigh_node_free_ref(curr_router);
82 void batadv_update_route(struct batadv_priv *bat_priv,
83 struct batadv_orig_node *orig_node,
84 struct batadv_neigh_node *neigh_node)
86 struct batadv_neigh_node *router = NULL;
91 router = batadv_orig_node_get_router(orig_node);
93 if (router != neigh_node)
94 _batadv_update_route(bat_priv, orig_node, neigh_node);
98 batadv_neigh_node_free_ref(router);
101 /* caller must hold the neigh_list_lock */
102 void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
103 struct batadv_neigh_node *neigh_node)
105 /* this neighbor is not part of our candidate list */
106 if (list_empty(&neigh_node->bonding_list))
109 list_del_rcu(&neigh_node->bonding_list);
110 INIT_LIST_HEAD(&neigh_node->bonding_list);
111 batadv_neigh_node_free_ref(neigh_node);
112 atomic_dec(&orig_node->bond_candidates);
118 void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
119 struct batadv_neigh_node *neigh_node)
121 struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
122 uint8_t interference_candidate = 0, tq;
124 spin_lock_bh(&orig_node->neigh_list_lock);
126 /* only consider if it has the same primary address ... */
127 if (!batadv_compare_eth(orig_node->orig,
128 neigh_node->orig_node->primary_addr))
131 router = batadv_orig_node_get_router(orig_node);
136 /* ... and is good enough to be considered */
137 tq = router->bat_iv.tq_avg - BATADV_BONDING_TQ_THRESHOLD;
138 if (neigh_node->bat_iv.tq_avg < tq)
141 /* check if we have another candidate with the same mac address or
142 * interface. If we do, we won't select this candidate because of
143 * possible interference.
145 hlist_for_each_entry_rcu(tmp_neigh_node,
146 &orig_node->neigh_list, list) {
147 if (tmp_neigh_node == neigh_node)
150 /* we only care if the other candidate is even
151 * considered as candidate.
153 if (list_empty(&tmp_neigh_node->bonding_list))
156 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
157 (batadv_compare_eth(neigh_node->addr,
158 tmp_neigh_node->addr))) {
159 interference_candidate = 1;
164 /* don't care further if it is an interference candidate */
165 if (interference_candidate)
168 /* this neighbor already is part of our candidate list */
169 if (!list_empty(&neigh_node->bonding_list))
172 if (!atomic_inc_not_zero(&neigh_node->refcount))
175 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
176 atomic_inc(&orig_node->bond_candidates);
180 batadv_bonding_candidate_del(orig_node, neigh_node);
183 spin_unlock_bh(&orig_node->neigh_list_lock);
186 batadv_neigh_node_free_ref(router);
189 /* copy primary address for bonding */
191 batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
192 struct batadv_orig_node *orig_neigh_node,
193 const struct batadv_ogm_packet *batman_ogm_packet)
195 if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
198 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
201 /* checks whether the host restarted and is in the protection time.
203 * 0 if the packet is to be accepted
204 * 1 if the packet is to be ignored.
206 int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
207 unsigned long *last_reset)
209 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
210 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
211 if (!batadv_has_timed_out(*last_reset,
212 BATADV_RESET_PROTECTION_MS))
215 *last_reset = jiffies;
216 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
217 "old packet received, start protection\n");
223 bool batadv_check_management_packet(struct sk_buff *skb,
224 struct batadv_hard_iface *hard_iface,
227 struct ethhdr *ethhdr;
229 /* drop packet if it has not necessary minimum size */
230 if (unlikely(!pskb_may_pull(skb, header_len)))
233 ethhdr = eth_hdr(skb);
235 /* packet with broadcast indication but unicast recipient */
236 if (!is_broadcast_ether_addr(ethhdr->h_dest))
239 /* packet with broadcast sender address */
240 if (is_broadcast_ether_addr(ethhdr->h_source))
243 /* create a copy of the skb, if needed, to modify it. */
244 if (skb_cow(skb, 0) < 0)
247 /* keep skb linear */
248 if (skb_linearize(skb) < 0)
254 static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
255 struct sk_buff *skb, size_t icmp_len)
257 struct batadv_hard_iface *primary_if = NULL;
258 struct batadv_orig_node *orig_node = NULL;
259 struct batadv_icmp_packet_rr *icmp_packet;
260 int ret = NET_RX_DROP;
262 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
264 /* add data to device queue */
265 if (icmp_packet->icmph.msg_type != BATADV_ECHO_REQUEST) {
266 batadv_socket_receive_packet(icmp_packet, icmp_len);
270 primary_if = batadv_primary_if_get_selected(bat_priv);
274 /* answer echo request (ping) */
275 /* get routing information */
276 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.orig);
280 /* create a copy of the skb, if needed, to modify it. */
281 if (skb_cow(skb, ETH_HLEN) < 0)
284 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
286 memcpy(icmp_packet->icmph.dst, icmp_packet->icmph.orig, ETH_ALEN);
287 memcpy(icmp_packet->icmph.orig, primary_if->net_dev->dev_addr,
289 icmp_packet->icmph.msg_type = BATADV_ECHO_REPLY;
290 icmp_packet->icmph.header.ttl = BATADV_TTL;
292 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
293 ret = NET_RX_SUCCESS;
297 batadv_hardif_free_ref(primary_if);
299 batadv_orig_node_free_ref(orig_node);
303 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
306 struct batadv_hard_iface *primary_if = NULL;
307 struct batadv_orig_node *orig_node = NULL;
308 struct batadv_icmp_packet *icmp_packet;
309 int ret = NET_RX_DROP;
311 icmp_packet = (struct batadv_icmp_packet *)skb->data;
313 /* send TTL exceeded if packet is an echo request (traceroute) */
314 if (icmp_packet->icmph.msg_type != BATADV_ECHO_REQUEST) {
315 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
316 icmp_packet->icmph.orig, icmp_packet->icmph.dst);
320 primary_if = batadv_primary_if_get_selected(bat_priv);
324 /* get routing information */
325 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.orig);
329 /* create a copy of the skb, if needed, to modify it. */
330 if (skb_cow(skb, ETH_HLEN) < 0)
333 icmp_packet = (struct batadv_icmp_packet *)skb->data;
335 memcpy(icmp_packet->icmph.dst, icmp_packet->icmph.orig, ETH_ALEN);
336 memcpy(icmp_packet->icmph.orig, primary_if->net_dev->dev_addr,
338 icmp_packet->icmph.msg_type = BATADV_TTL_EXCEEDED;
339 icmp_packet->icmph.header.ttl = BATADV_TTL;
341 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
342 ret = NET_RX_SUCCESS;
346 batadv_hardif_free_ref(primary_if);
348 batadv_orig_node_free_ref(orig_node);
353 int batadv_recv_icmp_packet(struct sk_buff *skb,
354 struct batadv_hard_iface *recv_if)
356 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
357 struct batadv_icmp_packet_rr *icmp_packet;
358 struct ethhdr *ethhdr;
359 struct batadv_orig_node *orig_node = NULL;
360 int hdr_size = sizeof(struct batadv_icmp_packet);
361 int ret = NET_RX_DROP;
363 /* we truncate all incoming icmp packets if they don't match our size */
364 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
365 hdr_size = sizeof(struct batadv_icmp_packet_rr);
367 /* drop packet if it has not necessary minimum size */
368 if (unlikely(!pskb_may_pull(skb, hdr_size)))
371 ethhdr = eth_hdr(skb);
373 /* packet with unicast indication but broadcast recipient */
374 if (is_broadcast_ether_addr(ethhdr->h_dest))
377 /* packet with broadcast sender address */
378 if (is_broadcast_ether_addr(ethhdr->h_source))
382 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
385 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
387 /* add record route information if not full */
388 if ((icmp_packet->icmph.msg_type == BATADV_ECHO_REPLY ||
389 icmp_packet->icmph.msg_type == BATADV_ECHO_REQUEST) &&
390 (hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
391 (icmp_packet->rr_cur < BATADV_RR_LEN)) {
392 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
393 ethhdr->h_dest, ETH_ALEN);
394 icmp_packet->rr_cur++;
398 if (batadv_is_my_mac(bat_priv, icmp_packet->icmph.dst))
399 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
402 if (icmp_packet->icmph.header.ttl < 2)
403 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
405 /* get routing information */
406 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.dst);
410 /* create a copy of the skb, if needed, to modify it. */
411 if (skb_cow(skb, ETH_HLEN) < 0)
414 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
417 icmp_packet->icmph.header.ttl--;
420 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
421 ret = NET_RX_SUCCESS;
425 batadv_orig_node_free_ref(orig_node);
429 /* In the bonding case, send the packets in a round
430 * robin fashion over the remaining interfaces.
432 * This method rotates the bonding list and increases the
433 * returned router's refcount.
435 static struct batadv_neigh_node *
436 batadv_find_bond_router(struct batadv_orig_node *primary_orig,
437 const struct batadv_hard_iface *recv_if)
439 struct batadv_neigh_node *tmp_neigh_node;
440 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
443 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
445 if (!first_candidate)
446 first_candidate = tmp_neigh_node;
448 /* recv_if == NULL on the first node. */
449 if (tmp_neigh_node->if_incoming == recv_if)
452 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
455 router = tmp_neigh_node;
459 /* use the first candidate if nothing was found. */
460 if (!router && first_candidate &&
461 atomic_inc_not_zero(&first_candidate->refcount))
462 router = first_candidate;
467 /* selected should point to the next element
468 * after the current router
470 spin_lock_bh(&primary_orig->neigh_list_lock);
471 /* this is a list_move(), which unfortunately
472 * does not exist as rcu version
474 list_del_rcu(&primary_orig->bond_list);
475 list_add_rcu(&primary_orig->bond_list,
476 &router->bonding_list);
477 spin_unlock_bh(&primary_orig->neigh_list_lock);
484 /* Interface Alternating: Use the best of the
485 * remaining candidates which are not using
488 * Increases the returned router's refcount
490 static struct batadv_neigh_node *
491 batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
492 const struct batadv_hard_iface *recv_if)
494 struct batadv_neigh_node *tmp_neigh_node;
495 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
498 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
500 if (!first_candidate)
501 first_candidate = tmp_neigh_node;
503 /* recv_if == NULL on the first node. */
504 if (tmp_neigh_node->if_incoming == recv_if)
508 tmp_neigh_node->bat_iv.tq_avg <= router->bat_iv.tq_avg)
511 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
514 /* decrement refcount of previously selected router */
516 batadv_neigh_node_free_ref(router);
518 /* we found a better router (or at least one valid router) */
519 router = tmp_neigh_node;
522 /* use the first candidate if nothing was found. */
523 if (!router && first_candidate &&
524 atomic_inc_not_zero(&first_candidate->refcount))
525 router = first_candidate;
532 * batadv_check_unicast_packet - Check for malformed unicast packets
533 * @bat_priv: the bat priv with all the soft interface information
534 * @skb: packet to check
535 * @hdr_size: size of header to pull
537 * Check for short header and bad addresses in given packet. Returns negative
538 * value when check fails and 0 otherwise. The negative value depends on the
539 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
540 * and -EREMOTE for non-local (other host) destination.
542 static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
543 struct sk_buff *skb, int hdr_size)
545 struct ethhdr *ethhdr;
547 /* drop packet if it has not necessary minimum size */
548 if (unlikely(!pskb_may_pull(skb, hdr_size)))
551 ethhdr = eth_hdr(skb);
553 /* packet with unicast indication but broadcast recipient */
554 if (is_broadcast_ether_addr(ethhdr->h_dest))
557 /* packet with broadcast sender address */
558 if (is_broadcast_ether_addr(ethhdr->h_source))
562 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
568 /* find a suitable router for this originator, and use
569 * bonding if possible. increases the found neighbors
572 struct batadv_neigh_node *
573 batadv_find_router(struct batadv_priv *bat_priv,
574 struct batadv_orig_node *orig_node,
575 const struct batadv_hard_iface *recv_if)
577 struct batadv_orig_node *primary_orig_node;
578 struct batadv_orig_node *router_orig;
579 struct batadv_neigh_node *router;
580 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
582 uint8_t *primary_addr;
587 router = batadv_orig_node_get_router(orig_node);
591 /* without bonding, the first node should
592 * always choose the default router.
594 bonding_enabled = atomic_read(&bat_priv->bonding);
597 /* select default router to output */
598 router_orig = router->orig_node;
602 if ((!recv_if) && (!bonding_enabled))
605 primary_addr = router_orig->primary_addr;
607 /* if we have something in the primary_addr, we can search
608 * for a potential bonding candidate.
610 if (batadv_compare_eth(primary_addr, zero_mac))
613 /* find the orig_node which has the primary interface. might
614 * even be the same as our router_orig in many cases
616 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
617 primary_orig_node = router_orig;
619 primary_orig_node = batadv_orig_hash_find(bat_priv,
621 if (!primary_orig_node)
624 batadv_orig_node_free_ref(primary_orig_node);
627 /* with less than 2 candidates, we can't do any
628 * bonding and prefer the original router.
630 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
633 /* all nodes between should choose a candidate which
634 * is is not on the interface where the packet came
637 batadv_neigh_node_free_ref(router);
640 router = batadv_find_bond_router(primary_orig_node, recv_if);
642 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
645 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
654 batadv_neigh_node_free_ref(router);
658 static int batadv_route_unicast_packet(struct sk_buff *skb,
659 struct batadv_hard_iface *recv_if)
661 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
662 struct batadv_orig_node *orig_node = NULL;
663 struct batadv_unicast_packet *unicast_packet;
664 struct ethhdr *ethhdr = eth_hdr(skb);
665 int res, hdr_len, ret = NET_RX_DROP;
667 unicast_packet = (struct batadv_unicast_packet *)skb->data;
670 if (unicast_packet->header.ttl < 2) {
671 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
672 ethhdr->h_source, unicast_packet->dest);
676 /* get routing information */
677 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
682 /* create a copy of the skb, if needed, to modify it. */
683 if (skb_cow(skb, ETH_HLEN) < 0)
687 unicast_packet = (struct batadv_unicast_packet *)skb->data;
688 unicast_packet->header.ttl--;
690 switch (unicast_packet->header.packet_type) {
691 case BATADV_UNICAST_4ADDR:
692 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
695 hdr_len = sizeof(struct batadv_unicast_packet);
698 /* other packet types not supported - yet */
704 batadv_skb_set_priority(skb, hdr_len);
706 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
708 /* translate transmit result into receive result */
709 if (res == NET_XMIT_SUCCESS) {
710 /* skb was transmitted and consumed */
711 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
712 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
713 skb->len + ETH_HLEN);
715 ret = NET_RX_SUCCESS;
716 } else if (res == NET_XMIT_POLICED) {
717 /* skb was buffered and consumed */
718 ret = NET_RX_SUCCESS;
723 batadv_orig_node_free_ref(orig_node);
728 * batadv_reroute_unicast_packet - update the unicast header for re-routing
729 * @bat_priv: the bat priv with all the soft interface information
730 * @unicast_packet: the unicast header to be updated
731 * @dst_addr: the payload destination
732 * @vid: VLAN identifier
734 * Search the translation table for dst_addr and update the unicast header with
735 * the new corresponding information (originator address where the destination
736 * client currently is and its known TTVN)
738 * Returns true if the packet header has been updated, false otherwise
741 batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
742 struct batadv_unicast_packet *unicast_packet,
743 uint8_t *dst_addr, unsigned short vid)
745 struct batadv_orig_node *orig_node = NULL;
746 struct batadv_hard_iface *primary_if = NULL;
748 uint8_t *orig_addr, orig_ttvn;
750 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
751 primary_if = batadv_primary_if_get_selected(bat_priv);
754 orig_addr = primary_if->net_dev->dev_addr;
755 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
757 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
762 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
765 orig_addr = orig_node->orig;
766 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
769 /* update the packet header */
770 memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
771 unicast_packet->ttvn = orig_ttvn;
776 batadv_hardif_free_ref(primary_if);
778 batadv_orig_node_free_ref(orig_node);
783 static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
784 struct sk_buff *skb, int hdr_len) {
785 struct batadv_unicast_packet *unicast_packet;
786 struct batadv_hard_iface *primary_if;
787 struct batadv_orig_node *orig_node;
788 uint8_t curr_ttvn, old_ttvn;
789 struct ethhdr *ethhdr;
793 /* check if there is enough data before accessing it */
794 if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
797 /* create a copy of the skb (in case of for re-routing) to modify it. */
798 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
801 unicast_packet = (struct batadv_unicast_packet *)skb->data;
802 vid = batadv_get_vid(skb, hdr_len);
803 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
805 /* check if the destination client was served by this node and it is now
806 * roaming. In this case, it means that the node has got a ROAM_ADV
807 * message and that it knows the new destination in the mesh to re-route
810 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
811 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
812 ethhdr->h_dest, vid))
813 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
815 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
816 unicast_packet->dest,
818 /* at this point the mesh destination should have been
819 * substituted with the originator address found in the global
820 * table. If not, let the packet go untouched anyway because
821 * there is nothing the node can do
826 /* retrieve the TTVN known by this node for the packet destination. This
827 * value is used later to check if the node which sent (or re-routed
828 * last time) the packet had an updated information or not
830 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
831 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
832 orig_node = batadv_orig_hash_find(bat_priv,
833 unicast_packet->dest);
834 /* if it is not possible to find the orig_node representing the
835 * destination, the packet can immediately be dropped as it will
836 * not be possible to deliver it
841 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
842 batadv_orig_node_free_ref(orig_node);
845 /* check if the TTVN contained in the packet is fresher than what the
848 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
852 old_ttvn = unicast_packet->ttvn;
853 /* the packet was forged based on outdated network information. Its
854 * destination can possibly be updated and forwarded towards the new
857 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
858 ethhdr->h_dest, vid)) {
859 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
860 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
861 unicast_packet->dest, ethhdr->h_dest,
862 old_ttvn, curr_ttvn);
866 /* the packet has not been re-routed: either the destination is
867 * currently served by this node or there is no destination at all and
868 * it is possible to drop the packet
870 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
873 /* update the header in order to let the packet be delivered to this
874 * node's soft interface
876 primary_if = batadv_primary_if_get_selected(bat_priv);
880 memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
882 batadv_hardif_free_ref(primary_if);
884 unicast_packet->ttvn = curr_ttvn;
890 * batadv_recv_unhandled_unicast_packet - receive and process packets which
891 * are in the unicast number space but not yet known to the implementation
892 * @skb: unicast tvlv packet to process
893 * @recv_if: pointer to interface this packet was received on
895 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
898 int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
899 struct batadv_hard_iface *recv_if)
901 struct batadv_unicast_packet *unicast_packet;
902 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
903 int check, hdr_size = sizeof(*unicast_packet);
905 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
909 /* we don't know about this type, drop it. */
910 unicast_packet = (struct batadv_unicast_packet *)skb->data;
911 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
914 return batadv_route_unicast_packet(skb, recv_if);
917 int batadv_recv_unicast_packet(struct sk_buff *skb,
918 struct batadv_hard_iface *recv_if)
920 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
921 struct batadv_unicast_packet *unicast_packet;
922 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
924 struct batadv_orig_node *orig_node = NULL;
925 int check, hdr_size = sizeof(*unicast_packet);
928 unicast_packet = (struct batadv_unicast_packet *)skb->data;
929 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
931 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
932 /* the caller function should have already pulled 2 bytes */
934 hdr_size = sizeof(*unicast_4addr_packet);
936 /* function returns -EREMOTE for promiscuous packets */
937 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
939 /* Even though the packet is not for us, we might save it to use for
940 * decoding a later received coded packet
942 if (check == -EREMOTE)
943 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
947 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
951 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
953 batadv_dat_inc_counter(bat_priv,
954 unicast_4addr_packet->subtype);
955 orig_addr = unicast_4addr_packet->src;
956 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
959 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
962 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
966 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
971 batadv_orig_node_free_ref(orig_node);
973 return NET_RX_SUCCESS;
976 return batadv_route_unicast_packet(skb, recv_if);
980 * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
981 * @skb: unicast tvlv packet to process
982 * @recv_if: pointer to interface this packet was received on
983 * @dst_addr: the payload destination
985 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
988 int batadv_recv_unicast_tvlv(struct sk_buff *skb,
989 struct batadv_hard_iface *recv_if)
991 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
992 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
993 unsigned char *tvlv_buff;
994 uint16_t tvlv_buff_len;
995 int hdr_size = sizeof(*unicast_tvlv_packet);
996 int ret = NET_RX_DROP;
998 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
1001 /* the header is likely to be modified while forwarding */
1002 if (skb_cow(skb, hdr_size) < 0)
1005 /* packet needs to be linearized to access the tvlv content */
1006 if (skb_linearize(skb) < 0)
1009 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
1011 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1012 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1014 if (tvlv_buff_len > skb->len - hdr_size)
1017 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
1018 unicast_tvlv_packet->src,
1019 unicast_tvlv_packet->dst,
1020 tvlv_buff, tvlv_buff_len);
1022 if (ret != NET_RX_SUCCESS)
1023 ret = batadv_route_unicast_packet(skb, recv_if);
1029 * batadv_recv_frag_packet - process received fragment
1030 * @skb: the received fragment
1031 * @recv_if: interface that the skb is received on
1033 * This function does one of the three following things: 1) Forward fragment, if
1034 * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
1035 * lack further fragments; 3) Merge fragments, if we have all needed parts.
1037 * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
1039 int batadv_recv_frag_packet(struct sk_buff *skb,
1040 struct batadv_hard_iface *recv_if)
1042 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1043 struct batadv_orig_node *orig_node_src = NULL;
1044 struct batadv_frag_packet *frag_packet;
1045 int ret = NET_RX_DROP;
1047 if (batadv_check_unicast_packet(bat_priv, skb,
1048 sizeof(*frag_packet)) < 0)
1051 frag_packet = (struct batadv_frag_packet *)skb->data;
1052 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
1056 /* Route the fragment if it is not for us and too big to be merged. */
1057 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
1058 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
1059 ret = NET_RX_SUCCESS;
1063 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
1064 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
1066 /* Add fragment to buffer and merge if possible. */
1067 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
1070 /* Deliver merged packet to the appropriate handler, if it was
1074 batadv_batman_skb_recv(skb, recv_if->net_dev,
1075 &recv_if->batman_adv_ptype, NULL);
1077 ret = NET_RX_SUCCESS;
1081 batadv_orig_node_free_ref(orig_node_src);
1086 int batadv_recv_bcast_packet(struct sk_buff *skb,
1087 struct batadv_hard_iface *recv_if)
1089 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1090 struct batadv_orig_node *orig_node = NULL;
1091 struct batadv_bcast_packet *bcast_packet;
1092 struct ethhdr *ethhdr;
1093 int hdr_size = sizeof(*bcast_packet);
1094 int ret = NET_RX_DROP;
1097 /* drop packet if it has not necessary minimum size */
1098 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1101 ethhdr = eth_hdr(skb);
1103 /* packet with broadcast indication but unicast recipient */
1104 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1107 /* packet with broadcast sender address */
1108 if (is_broadcast_ether_addr(ethhdr->h_source))
1111 /* ignore broadcasts sent by myself */
1112 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
1115 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1117 /* ignore broadcasts originated by myself */
1118 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
1121 if (bcast_packet->header.ttl < 2)
1124 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1129 spin_lock_bh(&orig_node->bcast_seqno_lock);
1131 /* check whether the packet is a duplicate */
1132 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1133 ntohl(bcast_packet->seqno)))
1136 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1138 /* check whether the packet is old and the host just restarted. */
1139 if (batadv_window_protected(bat_priv, seq_diff,
1140 &orig_node->bcast_seqno_reset))
1143 /* mark broadcast in flood history, update window position
1146 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1147 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1149 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1151 /* check whether this has been sent by another originator before */
1152 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
1155 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1157 /* rebroadcast packet */
1158 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
1160 /* don't hand the broadcast up if it is from an originator
1161 * from the same backbone.
1163 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1166 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1168 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1171 /* broadcast for me */
1172 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1176 ret = NET_RX_SUCCESS;
1180 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1183 batadv_orig_node_free_ref(orig_node);