1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
10 #include <linux/atomic.h>
11 #include <linux/byteorder/generic.h>
12 #include <linux/compiler.h>
13 #include <linux/errno.h>
14 #include <linux/etherdevice.h>
15 #include <linux/if_ether.h>
16 #include <linux/jiffies.h>
17 #include <linux/kref.h>
18 #include <linux/netdevice.h>
19 #include <linux/printk.h>
20 #include <linux/rculist.h>
21 #include <linux/rcupdate.h>
22 #include <linux/skbuff.h>
23 #include <linux/spinlock.h>
24 #include <linux/stddef.h>
25 #include <uapi/linux/batadv_packet.h>
28 #include "bridge_loop_avoidance.h"
29 #include "distributed-arp-table.h"
30 #include "fragmentation.h"
31 #include "hard-interface.h"
33 #include "network-coding.h"
34 #include "originator.h"
36 #include "soft-interface.h"
38 #include "translation-table.h"
41 static int batadv_route_unicast_packet(struct sk_buff *skb,
42 struct batadv_hard_iface *recv_if);
45 * _batadv_update_route() - set the router for this originator
46 * @bat_priv: the bat priv with all the soft interface information
47 * @orig_node: orig node which is to be configured
48 * @recv_if: the receive interface for which this route is set
49 * @neigh_node: neighbor which should be the next router
51 * This function does not perform any error checks
53 static void _batadv_update_route(struct batadv_priv *bat_priv,
54 struct batadv_orig_node *orig_node,
55 struct batadv_hard_iface *recv_if,
56 struct batadv_neigh_node *neigh_node)
58 struct batadv_orig_ifinfo *orig_ifinfo;
59 struct batadv_neigh_node *curr_router;
61 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, recv_if);
65 spin_lock_bh(&orig_node->neigh_list_lock);
66 /* curr_router used earlier may not be the current orig_ifinfo->router
67 * anymore because it was dereferenced outside of the neigh_list_lock
68 * protected region. After the new best neighbor has replace the current
69 * best neighbor the reference counter needs to decrease. Consequently,
70 * the code needs to ensure the curr_router variable contains a pointer
71 * to the replaced best neighbor.
74 /* increase refcount of new best neighbor */
76 kref_get(&neigh_node->refcount);
78 curr_router = rcu_replace_pointer(orig_ifinfo->router, neigh_node,
80 spin_unlock_bh(&orig_node->neigh_list_lock);
81 batadv_orig_ifinfo_put(orig_ifinfo);
84 if (curr_router && !neigh_node) {
85 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
86 "Deleting route towards: %pM\n", orig_node->orig);
87 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
88 "Deleted route towards originator");
91 } else if (!curr_router && neigh_node) {
92 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
93 "Adding route towards: %pM (via %pM)\n",
94 orig_node->orig, neigh_node->addr);
96 } else if (neigh_node && curr_router) {
97 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
98 "Changing route towards: %pM (now via %pM - was via %pM)\n",
99 orig_node->orig, neigh_node->addr,
103 /* decrease refcount of previous best neighbor */
104 batadv_neigh_node_put(curr_router);
108 * batadv_update_route() - set the router for this originator
109 * @bat_priv: the bat priv with all the soft interface information
110 * @orig_node: orig node which is to be configured
111 * @recv_if: the receive interface for which this route is set
112 * @neigh_node: neighbor which should be the next router
114 void batadv_update_route(struct batadv_priv *bat_priv,
115 struct batadv_orig_node *orig_node,
116 struct batadv_hard_iface *recv_if,
117 struct batadv_neigh_node *neigh_node)
119 struct batadv_neigh_node *router = NULL;
124 router = batadv_orig_router_get(orig_node, recv_if);
126 if (router != neigh_node)
127 _batadv_update_route(bat_priv, orig_node, recv_if, neigh_node);
130 batadv_neigh_node_put(router);
134 * batadv_window_protected() - checks whether the host restarted and is in the
136 * @bat_priv: the bat priv with all the soft interface information
137 * @seq_num_diff: difference between the current/received sequence number and
138 * the last sequence number
139 * @seq_old_max_diff: maximum age of sequence number not considered as restart
140 * @last_reset: jiffies timestamp of the last reset, will be updated when reset
142 * @protection_started: is set to true if the protection window was started,
143 * doesn't change otherwise.
146 * false if the packet is to be accepted.
147 * true if the packet is to be ignored.
149 bool batadv_window_protected(struct batadv_priv *bat_priv, s32 seq_num_diff,
150 s32 seq_old_max_diff, unsigned long *last_reset,
151 bool *protection_started)
153 if (seq_num_diff <= -seq_old_max_diff ||
154 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
155 if (!batadv_has_timed_out(*last_reset,
156 BATADV_RESET_PROTECTION_MS))
159 *last_reset = jiffies;
160 if (protection_started)
161 *protection_started = true;
162 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
163 "old packet received, start protection\n");
170 * batadv_check_management_packet() - Check preconditions for management packets
171 * @skb: incoming packet buffer
172 * @hard_iface: incoming hard interface
173 * @header_len: minimal header length of packet type
175 * Return: true when management preconditions are met, false otherwise
177 bool batadv_check_management_packet(struct sk_buff *skb,
178 struct batadv_hard_iface *hard_iface,
181 struct ethhdr *ethhdr;
183 /* drop packet if it has not necessary minimum size */
184 if (unlikely(!pskb_may_pull(skb, header_len)))
187 ethhdr = eth_hdr(skb);
189 /* packet with broadcast indication but unicast recipient */
190 if (!is_broadcast_ether_addr(ethhdr->h_dest))
193 /* packet with invalid sender address */
194 if (!is_valid_ether_addr(ethhdr->h_source))
197 /* create a copy of the skb, if needed, to modify it. */
198 if (skb_cow(skb, 0) < 0)
201 /* keep skb linear */
202 if (skb_linearize(skb) < 0)
209 * batadv_recv_my_icmp_packet() - receive an icmp packet locally
210 * @bat_priv: the bat priv with all the soft interface information
211 * @skb: icmp packet to process
213 * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
216 static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
219 struct batadv_hard_iface *primary_if = NULL;
220 struct batadv_orig_node *orig_node = NULL;
221 struct batadv_icmp_header *icmph;
222 int res, ret = NET_RX_DROP;
224 icmph = (struct batadv_icmp_header *)skb->data;
226 switch (icmph->msg_type) {
227 case BATADV_ECHO_REQUEST:
228 /* answer echo request (ping) */
229 primary_if = batadv_primary_if_get_selected(bat_priv);
233 /* get routing information */
234 orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
238 /* create a copy of the skb, if needed, to modify it. */
239 if (skb_cow(skb, ETH_HLEN) < 0)
242 icmph = (struct batadv_icmp_header *)skb->data;
244 ether_addr_copy(icmph->dst, icmph->orig);
245 ether_addr_copy(icmph->orig, primary_if->net_dev->dev_addr);
246 icmph->msg_type = BATADV_ECHO_REPLY;
247 icmph->ttl = BATADV_TTL;
249 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
250 if (res == NET_XMIT_SUCCESS)
251 ret = NET_RX_SUCCESS;
253 /* skb was consumed */
257 if (!pskb_may_pull(skb, sizeof(struct batadv_icmp_tp_packet)))
260 batadv_tp_meter_recv(bat_priv, skb);
261 ret = NET_RX_SUCCESS;
262 /* skb was consumed */
266 /* drop unknown type */
270 batadv_hardif_put(primary_if);
271 batadv_orig_node_put(orig_node);
278 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
281 struct batadv_hard_iface *primary_if = NULL;
282 struct batadv_orig_node *orig_node = NULL;
283 struct batadv_icmp_packet *icmp_packet;
284 int res, ret = NET_RX_DROP;
286 icmp_packet = (struct batadv_icmp_packet *)skb->data;
288 /* send TTL exceeded if packet is an echo request (traceroute) */
289 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
290 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
291 icmp_packet->orig, icmp_packet->dst);
295 primary_if = batadv_primary_if_get_selected(bat_priv);
299 /* get routing information */
300 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
304 /* create a copy of the skb, if needed, to modify it. */
305 if (skb_cow(skb, ETH_HLEN) < 0)
308 icmp_packet = (struct batadv_icmp_packet *)skb->data;
310 ether_addr_copy(icmp_packet->dst, icmp_packet->orig);
311 ether_addr_copy(icmp_packet->orig, primary_if->net_dev->dev_addr);
312 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
313 icmp_packet->ttl = BATADV_TTL;
315 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
316 if (res == NET_RX_SUCCESS)
317 ret = NET_XMIT_SUCCESS;
319 /* skb was consumed */
323 batadv_hardif_put(primary_if);
324 batadv_orig_node_put(orig_node);
332 * batadv_recv_icmp_packet() - Process incoming icmp packet
333 * @skb: incoming packet buffer
334 * @recv_if: incoming hard interface
336 * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
338 int batadv_recv_icmp_packet(struct sk_buff *skb,
339 struct batadv_hard_iface *recv_if)
341 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
342 struct batadv_icmp_header *icmph;
343 struct batadv_icmp_packet_rr *icmp_packet_rr;
344 struct ethhdr *ethhdr;
345 struct batadv_orig_node *orig_node = NULL;
346 int hdr_size = sizeof(struct batadv_icmp_header);
347 int res, ret = NET_RX_DROP;
349 /* drop packet if it has not necessary minimum size */
350 if (unlikely(!pskb_may_pull(skb, hdr_size)))
353 ethhdr = eth_hdr(skb);
355 /* packet with unicast indication but non-unicast recipient */
356 if (!is_valid_ether_addr(ethhdr->h_dest))
359 /* packet with broadcast/multicast sender address */
360 if (is_multicast_ether_addr(ethhdr->h_source))
364 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
367 icmph = (struct batadv_icmp_header *)skb->data;
369 /* add record route information if not full */
370 if ((icmph->msg_type == BATADV_ECHO_REPLY ||
371 icmph->msg_type == BATADV_ECHO_REQUEST) &&
372 skb->len >= sizeof(struct batadv_icmp_packet_rr)) {
373 if (skb_linearize(skb) < 0)
376 /* create a copy of the skb, if needed, to modify it. */
377 if (skb_cow(skb, ETH_HLEN) < 0)
380 ethhdr = eth_hdr(skb);
381 icmph = (struct batadv_icmp_header *)skb->data;
382 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
383 if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
386 ether_addr_copy(icmp_packet_rr->rr[icmp_packet_rr->rr_cur],
388 icmp_packet_rr->rr_cur++;
392 if (batadv_is_my_mac(bat_priv, icmph->dst))
393 return batadv_recv_my_icmp_packet(bat_priv, skb);
397 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
399 /* get routing information */
400 orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
404 /* create a copy of the skb, if needed, to modify it. */
405 if (skb_cow(skb, ETH_HLEN) < 0)
408 icmph = (struct batadv_icmp_header *)skb->data;
414 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
415 if (res == NET_XMIT_SUCCESS)
416 ret = NET_RX_SUCCESS;
418 /* skb was consumed */
422 batadv_orig_node_put(orig_node);
430 * batadv_check_unicast_packet() - Check for malformed unicast packets
431 * @bat_priv: the bat priv with all the soft interface information
432 * @skb: packet to check
433 * @hdr_size: size of header to pull
435 * Checks for short header and bad addresses in the given packet.
437 * Return: negative value when check fails and 0 otherwise. The negative value
438 * depends on the reason: -ENODATA for bad header, -EBADR for broadcast
439 * destination or source, and -EREMOTE for non-local (other host) destination.
441 static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
442 struct sk_buff *skb, int hdr_size)
444 struct ethhdr *ethhdr;
446 /* drop packet if it has not necessary minimum size */
447 if (unlikely(!pskb_may_pull(skb, hdr_size)))
450 ethhdr = eth_hdr(skb);
452 /* packet with unicast indication but non-unicast recipient */
453 if (!is_valid_ether_addr(ethhdr->h_dest))
456 /* packet with broadcast/multicast sender address */
457 if (is_multicast_ether_addr(ethhdr->h_source))
461 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
468 * batadv_last_bonding_get() - Get last_bonding_candidate of orig_node
469 * @orig_node: originator node whose last bonding candidate should be retrieved
471 * Return: last bonding candidate of router or NULL if not found
473 * The object is returned with refcounter increased by 1.
475 static struct batadv_orig_ifinfo *
476 batadv_last_bonding_get(struct batadv_orig_node *orig_node)
478 struct batadv_orig_ifinfo *last_bonding_candidate;
480 spin_lock_bh(&orig_node->neigh_list_lock);
481 last_bonding_candidate = orig_node->last_bonding_candidate;
483 if (last_bonding_candidate)
484 kref_get(&last_bonding_candidate->refcount);
485 spin_unlock_bh(&orig_node->neigh_list_lock);
487 return last_bonding_candidate;
491 * batadv_last_bonding_replace() - Replace last_bonding_candidate of orig_node
492 * @orig_node: originator node whose bonding candidates should be replaced
493 * @new_candidate: new bonding candidate or NULL
496 batadv_last_bonding_replace(struct batadv_orig_node *orig_node,
497 struct batadv_orig_ifinfo *new_candidate)
499 struct batadv_orig_ifinfo *old_candidate;
501 spin_lock_bh(&orig_node->neigh_list_lock);
502 old_candidate = orig_node->last_bonding_candidate;
505 kref_get(&new_candidate->refcount);
506 orig_node->last_bonding_candidate = new_candidate;
507 spin_unlock_bh(&orig_node->neigh_list_lock);
509 batadv_orig_ifinfo_put(old_candidate);
513 * batadv_find_router() - find a suitable router for this originator
514 * @bat_priv: the bat priv with all the soft interface information
515 * @orig_node: the destination node
516 * @recv_if: pointer to interface this packet was received on
518 * Return: the router which should be used for this orig_node on
519 * this interface, or NULL if not available.
521 struct batadv_neigh_node *
522 batadv_find_router(struct batadv_priv *bat_priv,
523 struct batadv_orig_node *orig_node,
524 struct batadv_hard_iface *recv_if)
526 struct batadv_algo_ops *bao = bat_priv->algo_ops;
527 struct batadv_neigh_node *first_candidate_router = NULL;
528 struct batadv_neigh_node *next_candidate_router = NULL;
529 struct batadv_neigh_node *router, *cand_router = NULL;
530 struct batadv_neigh_node *last_cand_router = NULL;
531 struct batadv_orig_ifinfo *cand, *first_candidate = NULL;
532 struct batadv_orig_ifinfo *next_candidate = NULL;
533 struct batadv_orig_ifinfo *last_candidate;
534 bool last_candidate_found = false;
539 router = batadv_orig_router_get(orig_node, recv_if);
544 /* only consider bonding for recv_if == BATADV_IF_DEFAULT (first hop)
547 if (!(recv_if == BATADV_IF_DEFAULT && atomic_read(&bat_priv->bonding)))
550 /* bonding: loop through the list of possible routers found
551 * for the various outgoing interfaces and find a candidate after
552 * the last chosen bonding candidate (next_candidate). If no such
553 * router is found, use the first candidate found (the previously
554 * chosen bonding candidate might have been the last one in the list).
555 * If this can't be found either, return the previously chosen
556 * router - obviously there are no other candidates.
559 last_candidate = batadv_last_bonding_get(orig_node);
561 last_cand_router = rcu_dereference(last_candidate->router);
563 hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
564 /* acquire some structures and references ... */
565 if (!kref_get_unless_zero(&cand->refcount))
568 cand_router = rcu_dereference(cand->router);
572 if (!kref_get_unless_zero(&cand_router->refcount)) {
577 /* alternative candidate should be good enough to be
580 if (!bao->neigh.is_similar_or_better(cand_router,
581 cand->if_outgoing, router,
585 /* don't use the same router twice */
586 if (last_cand_router == cand_router)
589 /* mark the first possible candidate */
590 if (!first_candidate) {
591 kref_get(&cand_router->refcount);
592 kref_get(&cand->refcount);
593 first_candidate = cand;
594 first_candidate_router = cand_router;
597 /* check if the loop has already passed the previously selected
598 * candidate ... this function should select the next candidate
599 * AFTER the previously used bonding candidate.
601 if (!last_candidate || last_candidate_found) {
602 next_candidate = cand;
603 next_candidate_router = cand_router;
607 if (last_candidate == cand)
608 last_candidate_found = true;
610 /* free references */
612 batadv_neigh_node_put(cand_router);
615 batadv_orig_ifinfo_put(cand);
619 /* After finding candidates, handle the three cases:
620 * 1) there is a next candidate, use that
621 * 2) there is no next candidate, use the first of the list
622 * 3) there is no candidate at all, return the default router
624 if (next_candidate) {
625 batadv_neigh_node_put(router);
627 kref_get(&next_candidate_router->refcount);
628 router = next_candidate_router;
629 batadv_last_bonding_replace(orig_node, next_candidate);
630 } else if (first_candidate) {
631 batadv_neigh_node_put(router);
633 kref_get(&first_candidate_router->refcount);
634 router = first_candidate_router;
635 batadv_last_bonding_replace(orig_node, first_candidate);
637 batadv_last_bonding_replace(orig_node, NULL);
640 /* cleanup of candidates */
641 if (first_candidate) {
642 batadv_neigh_node_put(first_candidate_router);
643 batadv_orig_ifinfo_put(first_candidate);
646 if (next_candidate) {
647 batadv_neigh_node_put(next_candidate_router);
648 batadv_orig_ifinfo_put(next_candidate);
651 batadv_orig_ifinfo_put(last_candidate);
656 static int batadv_route_unicast_packet(struct sk_buff *skb,
657 struct batadv_hard_iface *recv_if)
659 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
660 struct batadv_orig_node *orig_node = NULL;
661 struct batadv_unicast_packet *unicast_packet;
662 struct ethhdr *ethhdr = eth_hdr(skb);
663 int res, hdr_len, ret = NET_RX_DROP;
666 unicast_packet = (struct batadv_unicast_packet *)skb->data;
669 if (unicast_packet->ttl < 2) {
670 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
671 ethhdr->h_source, unicast_packet->dest);
675 /* get routing information */
676 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
681 /* create a copy of the skb, if needed, to modify it. */
682 if (skb_cow(skb, ETH_HLEN) < 0)
686 unicast_packet = (struct batadv_unicast_packet *)skb->data;
687 unicast_packet->ttl--;
689 switch (unicast_packet->packet_type) {
690 case BATADV_UNICAST_4ADDR:
691 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
694 hdr_len = sizeof(struct batadv_unicast_packet);
697 /* other packet types not supported - yet */
703 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 ret = NET_RX_SUCCESS;
711 /* skb was transmitted and consumed */
712 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
713 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
717 /* skb was consumed */
721 batadv_orig_node_put(orig_node);
729 * batadv_reroute_unicast_packet() - update the unicast header for re-routing
730 * @bat_priv: the bat priv with all the soft interface information
731 * @skb: unicast packet to process
732 * @unicast_packet: the unicast header to be updated
733 * @dst_addr: the payload destination
734 * @vid: VLAN identifier
736 * Search the translation table for dst_addr and update the unicast header with
737 * the new corresponding information (originator address where the destination
738 * client currently is and its known TTVN)
740 * Return: true if the packet header has been updated, false otherwise
743 batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
744 struct batadv_unicast_packet *unicast_packet,
745 u8 *dst_addr, unsigned short vid)
747 struct batadv_orig_node *orig_node = NULL;
748 struct batadv_hard_iface *primary_if = NULL;
753 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
754 primary_if = batadv_primary_if_get_selected(bat_priv);
757 orig_addr = primary_if->net_dev->dev_addr;
758 orig_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
760 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
765 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
768 orig_addr = orig_node->orig;
769 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
772 /* update the packet header */
773 skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
774 ether_addr_copy(unicast_packet->dest, orig_addr);
775 unicast_packet->ttvn = orig_ttvn;
776 skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
780 batadv_hardif_put(primary_if);
781 batadv_orig_node_put(orig_node);
786 static bool batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
787 struct sk_buff *skb, int hdr_len)
789 struct batadv_unicast_packet *unicast_packet;
790 struct batadv_hard_iface *primary_if;
791 struct batadv_orig_node *orig_node;
792 u8 curr_ttvn, old_ttvn;
793 struct ethhdr *ethhdr;
797 /* check if there is enough data before accessing it */
798 if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
801 /* create a copy of the skb (in case of for re-routing) to modify it. */
802 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
805 unicast_packet = (struct batadv_unicast_packet *)skb->data;
806 vid = batadv_get_vid(skb, hdr_len);
807 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
809 /* do not reroute multicast frames in a unicast header */
810 if (is_multicast_ether_addr(ethhdr->h_dest))
813 /* check if the destination client was served by this node and it is now
814 * roaming. In this case, it means that the node has got a ROAM_ADV
815 * message and that it knows the new destination in the mesh to re-route
818 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
819 if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
820 ethhdr->h_dest, vid))
821 batadv_dbg_ratelimited(BATADV_DBG_TT,
823 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
824 unicast_packet->dest,
826 /* at this point the mesh destination should have been
827 * substituted with the originator address found in the global
828 * table. If not, let the packet go untouched anyway because
829 * there is nothing the node can do
834 /* retrieve the TTVN known by this node for the packet destination. This
835 * value is used later to check if the node which sent (or re-routed
836 * last time) the packet had an updated information or not
838 curr_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
839 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
840 orig_node = batadv_orig_hash_find(bat_priv,
841 unicast_packet->dest);
842 /* if it is not possible to find the orig_node representing the
843 * destination, the packet can immediately be dropped as it will
844 * not be possible to deliver it
849 curr_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
850 batadv_orig_node_put(orig_node);
853 /* check if the TTVN contained in the packet is fresher than what the
856 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
860 old_ttvn = unicast_packet->ttvn;
861 /* the packet was forged based on outdated network information. Its
862 * destination can possibly be updated and forwarded towards the new
865 if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
866 ethhdr->h_dest, vid)) {
867 batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
868 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
869 unicast_packet->dest, ethhdr->h_dest,
870 old_ttvn, curr_ttvn);
874 /* the packet has not been re-routed: either the destination is
875 * currently served by this node or there is no destination at all and
876 * it is possible to drop the packet
878 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
881 /* update the header in order to let the packet be delivered to this
882 * node's soft interface
884 primary_if = batadv_primary_if_get_selected(bat_priv);
888 /* update the packet header */
889 skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
890 ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
891 unicast_packet->ttvn = curr_ttvn;
892 skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
894 batadv_hardif_put(primary_if);
900 * batadv_recv_unhandled_unicast_packet() - receive and process packets which
901 * are in the unicast number space but not yet known to the implementation
902 * @skb: unicast tvlv packet to process
903 * @recv_if: pointer to interface this packet was received on
905 * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
908 int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
909 struct batadv_hard_iface *recv_if)
911 struct batadv_unicast_packet *unicast_packet;
912 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
913 int check, hdr_size = sizeof(*unicast_packet);
915 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
919 /* we don't know about this type, drop it. */
920 unicast_packet = (struct batadv_unicast_packet *)skb->data;
921 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
924 return batadv_route_unicast_packet(skb, recv_if);
932 * batadv_recv_unicast_packet() - Process incoming unicast packet
933 * @skb: incoming packet buffer
934 * @recv_if: incoming hard interface
936 * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
938 int batadv_recv_unicast_packet(struct sk_buff *skb,
939 struct batadv_hard_iface *recv_if)
941 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
942 struct batadv_unicast_packet *unicast_packet;
943 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
944 u8 *orig_addr, *orig_addr_gw;
945 struct batadv_orig_node *orig_node = NULL, *orig_node_gw = NULL;
946 int check, hdr_size = sizeof(*unicast_packet);
947 enum batadv_subtype subtype;
948 int ret = NET_RX_DROP;
951 unicast_packet = (struct batadv_unicast_packet *)skb->data;
952 is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
953 /* the caller function should have already pulled 2 bytes */
955 hdr_size = sizeof(*unicast_4addr_packet);
957 /* function returns -EREMOTE for promiscuous packets */
958 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
960 /* Even though the packet is not for us, we might save it to use for
961 * decoding a later received coded packet
963 if (check == -EREMOTE)
964 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
968 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
971 unicast_packet = (struct batadv_unicast_packet *)skb->data;
974 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
975 /* If this is a unicast packet from another backgone gw,
978 orig_addr_gw = eth_hdr(skb)->h_source;
979 orig_node_gw = batadv_orig_hash_find(bat_priv, orig_addr_gw);
981 is_gw = batadv_bla_is_backbone_gw(skb, orig_node_gw,
983 batadv_orig_node_put(orig_node_gw);
985 batadv_dbg(BATADV_DBG_BLA, bat_priv,
986 "%s(): Dropped unicast pkt received from another backbone gw %pM.\n",
987 __func__, orig_addr_gw);
993 unicast_4addr_packet =
994 (struct batadv_unicast_4addr_packet *)skb->data;
995 subtype = unicast_4addr_packet->subtype;
996 batadv_dat_inc_counter(bat_priv, subtype);
998 /* Only payload data should be considered for speedy
999 * join. For example, DAT also uses unicast 4addr
1000 * types, but those packets should not be considered
1001 * for speedy join, since the clients do not actually
1002 * reside at the sending originator.
1004 if (subtype == BATADV_P_DATA) {
1005 orig_addr = unicast_4addr_packet->src;
1006 orig_node = batadv_orig_hash_find(bat_priv,
1011 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
1014 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
1018 batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1020 batadv_interface_rx(recv_if->soft_iface, skb, hdr_size,
1024 batadv_orig_node_put(orig_node);
1026 return NET_RX_SUCCESS;
1029 ret = batadv_route_unicast_packet(skb, recv_if);
1030 /* skb was consumed */
1040 * batadv_recv_unicast_tvlv() - receive and process unicast tvlv packets
1041 * @skb: unicast tvlv packet to process
1042 * @recv_if: pointer to interface this packet was received on
1044 * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
1047 int batadv_recv_unicast_tvlv(struct sk_buff *skb,
1048 struct batadv_hard_iface *recv_if)
1050 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1051 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1052 unsigned char *tvlv_buff;
1054 int hdr_size = sizeof(*unicast_tvlv_packet);
1055 int ret = NET_RX_DROP;
1057 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
1060 /* the header is likely to be modified while forwarding */
1061 if (skb_cow(skb, hdr_size) < 0)
1064 /* packet needs to be linearized to access the tvlv content */
1065 if (skb_linearize(skb) < 0)
1068 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
1070 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1071 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1073 if (tvlv_buff_len > skb->len - hdr_size)
1076 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
1077 unicast_tvlv_packet->src,
1078 unicast_tvlv_packet->dst,
1079 tvlv_buff, tvlv_buff_len);
1081 if (ret != NET_RX_SUCCESS) {
1082 ret = batadv_route_unicast_packet(skb, recv_if);
1083 /* skb was consumed */
1094 * batadv_recv_frag_packet() - process received fragment
1095 * @skb: the received fragment
1096 * @recv_if: interface that the skb is received on
1098 * This function does one of the three following things: 1) Forward fragment, if
1099 * the assembled packet will exceed our MTU; 2) Buffer fragment, if we still
1100 * lack further fragments; 3) Merge fragments, if we have all needed parts.
1102 * Return: NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
1104 int batadv_recv_frag_packet(struct sk_buff *skb,
1105 struct batadv_hard_iface *recv_if)
1107 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1108 struct batadv_orig_node *orig_node_src = NULL;
1109 struct batadv_frag_packet *frag_packet;
1110 int ret = NET_RX_DROP;
1112 if (batadv_check_unicast_packet(bat_priv, skb,
1113 sizeof(*frag_packet)) < 0)
1116 frag_packet = (struct batadv_frag_packet *)skb->data;
1117 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
1121 skb->priority = frag_packet->priority + 256;
1123 /* Route the fragment if it is not for us and too big to be merged. */
1124 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
1125 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
1126 /* skb was consumed */
1128 ret = NET_RX_SUCCESS;
1132 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
1133 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
1135 /* Add fragment to buffer and merge if possible. */
1136 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
1139 /* Deliver merged packet to the appropriate handler, if it was
1143 batadv_batman_skb_recv(skb, recv_if->net_dev,
1144 &recv_if->batman_adv_ptype, NULL);
1145 /* skb was consumed */
1149 ret = NET_RX_SUCCESS;
1152 batadv_orig_node_put(orig_node_src);
1160 * batadv_recv_bcast_packet() - Process incoming broadcast packet
1161 * @skb: incoming packet buffer
1162 * @recv_if: incoming hard interface
1164 * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
1166 int batadv_recv_bcast_packet(struct sk_buff *skb,
1167 struct batadv_hard_iface *recv_if)
1169 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1170 struct batadv_orig_node *orig_node = NULL;
1171 struct batadv_bcast_packet *bcast_packet;
1172 struct ethhdr *ethhdr;
1173 int hdr_size = sizeof(*bcast_packet);
1178 /* drop packet if it has not necessary minimum size */
1179 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1182 ethhdr = eth_hdr(skb);
1184 /* packet with broadcast indication but unicast recipient */
1185 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1188 /* packet with broadcast/multicast sender address */
1189 if (is_multicast_ether_addr(ethhdr->h_source))
1192 /* ignore broadcasts sent by myself */
1193 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
1196 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1198 /* ignore broadcasts originated by myself */
1199 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
1202 if (bcast_packet->ttl-- < 2)
1205 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1210 spin_lock_bh(&orig_node->bcast_seqno_lock);
1212 seqno = ntohl(bcast_packet->seqno);
1213 /* check whether the packet is a duplicate */
1214 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1218 seq_diff = seqno - orig_node->last_bcast_seqno;
1220 /* check whether the packet is old and the host just restarted. */
1221 if (batadv_window_protected(bat_priv, seq_diff,
1222 BATADV_BCAST_MAX_AGE,
1223 &orig_node->bcast_seqno_reset, NULL))
1226 /* mark broadcast in flood history, update window position
1229 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1230 orig_node->last_bcast_seqno = seqno;
1232 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1234 /* check whether this has been sent by another originator before */
1235 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
1238 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1240 /* rebroadcast packet */
1241 ret = batadv_forw_bcast_packet(bat_priv, skb, 0, false);
1242 if (ret == NETDEV_TX_BUSY)
1245 /* don't hand the broadcast up if it is from an originator
1246 * from the same backbone.
1248 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1251 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1253 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1256 batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1258 /* broadcast for me */
1259 batadv_interface_rx(recv_if->soft_iface, skb, hdr_size, orig_node);
1262 ret = NET_RX_SUCCESS;
1266 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1271 batadv_orig_node_put(orig_node);