^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Marek Lindner, Simon Wunderlich
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "routing.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/byteorder/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <uapi/linux/batadv_packet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "bitarray.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "bridge_loop_avoidance.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "distributed-arp-table.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "fragmentation.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "hard-interface.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "icmp_socket.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "network-coding.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "originator.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "send.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include "soft-interface.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include "tp_meter.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include "translation-table.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include "tvlv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int batadv_route_unicast_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct batadv_hard_iface *recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * _batadv_update_route() - set the router for this originator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @orig_node: orig node which is to be configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @recv_if: the receive interface for which this route is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @neigh_node: neighbor which should be the next router
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * This function does not perform any error checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static void _batadv_update_route(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct batadv_hard_iface *recv_if,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct batadv_neigh_node *neigh_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct batadv_orig_ifinfo *orig_ifinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct batadv_neigh_node *curr_router;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) orig_ifinfo = batadv_orig_ifinfo_get(orig_node, recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!orig_ifinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) spin_lock_bh(&orig_node->neigh_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* curr_router used earlier may not be the current orig_ifinfo->router
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * anymore because it was dereferenced outside of the neigh_list_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * protected region. After the new best neighbor has replace the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * best neighbor the reference counter needs to decrease. Consequently,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * the code needs to ensure the curr_router variable contains a pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * to the replaced best neighbor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* increase refcount of new best neighbor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (neigh_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) kref_get(&neigh_node->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) curr_router = rcu_replace_pointer(orig_ifinfo->router, neigh_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) spin_unlock_bh(&orig_node->neigh_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) batadv_orig_ifinfo_put(orig_ifinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* route deleted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (curr_router && !neigh_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) "Deleting route towards: %pM\n", orig_node->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) batadv_tt_global_del_orig(bat_priv, orig_node, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) "Deleted route towards originator");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* route added */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) } else if (!curr_router && neigh_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) "Adding route towards: %pM (via %pM)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) orig_node->orig, neigh_node->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* route changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) } else if (neigh_node && curr_router) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) "Changing route towards: %pM (now via %pM - was via %pM)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) orig_node->orig, neigh_node->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) curr_router->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* decrease refcount of previous best neighbor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (curr_router)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) batadv_neigh_node_put(curr_router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * batadv_update_route() - set the router for this originator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * @orig_node: orig node which is to be configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * @recv_if: the receive interface for which this route is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * @neigh_node: neighbor which should be the next router
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) void batadv_update_route(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct batadv_hard_iface *recv_if,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct batadv_neigh_node *neigh_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct batadv_neigh_node *router = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) router = batadv_orig_router_get(orig_node, recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (router != neigh_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) _batadv_update_route(bat_priv, orig_node, recv_if, neigh_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (router)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) batadv_neigh_node_put(router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * batadv_window_protected() - checks whether the host restarted and is in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * protection time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * @seq_num_diff: difference between the current/received sequence number and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * the last sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @seq_old_max_diff: maximum age of sequence number not considered as restart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * @last_reset: jiffies timestamp of the last reset, will be updated when reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * is detected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * @protection_started: is set to true if the protection window was started,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * doesn't change otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * false if the packet is to be accepted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * true if the packet is to be ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) bool batadv_window_protected(struct batadv_priv *bat_priv, s32 seq_num_diff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) s32 seq_old_max_diff, unsigned long *last_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) bool *protection_started)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (seq_num_diff <= -seq_old_max_diff ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!batadv_has_timed_out(*last_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) BATADV_RESET_PROTECTION_MS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *last_reset = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (protection_started)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) *protection_started = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) "old packet received, start protection\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * batadv_check_management_packet() - Check preconditions for management packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * @skb: incoming packet buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * @hard_iface: incoming hard interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * @header_len: minimal header length of packet type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * Return: true when management preconditions are met, false otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) bool batadv_check_management_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct batadv_hard_iface *hard_iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int header_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* drop packet if it has not necessary minimum size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (unlikely(!pskb_may_pull(skb, header_len)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* packet with broadcast indication but unicast recipient */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (!is_broadcast_ether_addr(ethhdr->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /* packet with invalid sender address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (!is_valid_ether_addr(ethhdr->h_source))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* create a copy of the skb, if needed, to modify it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (skb_cow(skb, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /* keep skb linear */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (skb_linearize(skb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * batadv_recv_my_icmp_packet() - receive an icmp packet locally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * @skb: icmp packet to process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct batadv_hard_iface *primary_if = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct batadv_orig_node *orig_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct batadv_icmp_header *icmph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int res, ret = NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) icmph = (struct batadv_icmp_header *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) switch (icmph->msg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) case BATADV_ECHO_REPLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) case BATADV_DESTINATION_UNREACHABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) case BATADV_TTL_EXCEEDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* receive the packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (skb_linearize(skb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) batadv_socket_receive_packet(icmph, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) case BATADV_ECHO_REQUEST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /* answer echo request (ping) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) primary_if = batadv_primary_if_get_selected(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) /* get routing information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* create a copy of the skb, if needed, to modify it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (skb_cow(skb, ETH_HLEN) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) icmph = (struct batadv_icmp_header *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) ether_addr_copy(icmph->dst, icmph->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) ether_addr_copy(icmph->orig, primary_if->net_dev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) icmph->msg_type = BATADV_ECHO_REPLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) icmph->ttl = BATADV_TTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) res = batadv_send_skb_to_orig(skb, orig_node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (res == NET_XMIT_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ret = NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /* skb was consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) case BATADV_TP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (!pskb_may_pull(skb, sizeof(struct batadv_icmp_tp_packet)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) batadv_tp_meter_recv(bat_priv, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* skb was consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /* drop unknown type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) batadv_hardif_put(primary_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct batadv_hard_iface *primary_if = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct batadv_orig_node *orig_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct batadv_icmp_packet *icmp_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) int res, ret = NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) icmp_packet = (struct batadv_icmp_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* send TTL exceeded if packet is an echo request (traceroute) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) icmp_packet->orig, icmp_packet->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) primary_if = batadv_primary_if_get_selected(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (!primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* get routing information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* create a copy of the skb, if needed, to modify it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (skb_cow(skb, ETH_HLEN) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) icmp_packet = (struct batadv_icmp_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ether_addr_copy(icmp_packet->dst, icmp_packet->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) ether_addr_copy(icmp_packet->orig, primary_if->net_dev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) icmp_packet->ttl = BATADV_TTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) res = batadv_send_skb_to_orig(skb, orig_node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (res == NET_RX_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) ret = NET_XMIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* skb was consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) batadv_hardif_put(primary_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * batadv_recv_icmp_packet() - Process incoming icmp packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * @skb: incoming packet buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * @recv_if: incoming hard interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int batadv_recv_icmp_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct batadv_icmp_header *icmph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct batadv_icmp_packet_rr *icmp_packet_rr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) struct batadv_orig_node *orig_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) int hdr_size = sizeof(struct batadv_icmp_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int res, ret = NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* drop packet if it has not necessary minimum size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (unlikely(!pskb_may_pull(skb, hdr_size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /* packet with unicast indication but non-unicast recipient */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (!is_valid_ether_addr(ethhdr->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* packet with broadcast/multicast sender address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (is_multicast_ether_addr(ethhdr->h_source))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /* not for me */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) icmph = (struct batadv_icmp_header *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* add record route information if not full */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if ((icmph->msg_type == BATADV_ECHO_REPLY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) icmph->msg_type == BATADV_ECHO_REQUEST) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) skb->len >= sizeof(struct batadv_icmp_packet_rr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (skb_linearize(skb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /* create a copy of the skb, if needed, to modify it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (skb_cow(skb, ETH_HLEN) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) icmph = (struct batadv_icmp_header *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) ether_addr_copy(icmp_packet_rr->rr[icmp_packet_rr->rr_cur],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ethhdr->h_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) icmp_packet_rr->rr_cur++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /* packet for me */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (batadv_is_my_mac(bat_priv, icmph->dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return batadv_recv_my_icmp_packet(bat_priv, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /* TTL exceeded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (icmph->ttl < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /* get routing information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* create a copy of the skb, if needed, to modify it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (skb_cow(skb, ETH_HLEN) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) goto put_orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) icmph = (struct batadv_icmp_header *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /* decrement ttl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) icmph->ttl--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /* route it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (res == NET_XMIT_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ret = NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /* skb was consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) put_orig_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * batadv_check_unicast_packet() - Check for malformed unicast packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * @skb: packet to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * @hdr_size: size of header to pull
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * Checks for short header and bad addresses in the given packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * Return: negative value when check fails and 0 otherwise. The negative value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * depends on the reason: -ENODATA for bad header, -EBADR for broadcast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * destination or source, and -EREMOTE for non-local (other host) destination.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) struct sk_buff *skb, int hdr_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* drop packet if it has not necessary minimum size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (unlikely(!pskb_may_pull(skb, hdr_size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /* packet with unicast indication but non-unicast recipient */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (!is_valid_ether_addr(ethhdr->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return -EBADR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* packet with broadcast/multicast sender address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (is_multicast_ether_addr(ethhdr->h_source))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return -EBADR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /* not for me */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return -EREMOTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * batadv_last_bonding_get() - Get last_bonding_candidate of orig_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * @orig_node: originator node whose last bonding candidate should be retrieved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * Return: last bonding candidate of router or NULL if not found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * The object is returned with refcounter increased by 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static struct batadv_orig_ifinfo *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) batadv_last_bonding_get(struct batadv_orig_node *orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct batadv_orig_ifinfo *last_bonding_candidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) spin_lock_bh(&orig_node->neigh_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) last_bonding_candidate = orig_node->last_bonding_candidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (last_bonding_candidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) kref_get(&last_bonding_candidate->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) spin_unlock_bh(&orig_node->neigh_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) return last_bonding_candidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * batadv_last_bonding_replace() - Replace last_bonding_candidate of orig_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * @orig_node: originator node whose bonding candidates should be replaced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * @new_candidate: new bonding candidate or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) batadv_last_bonding_replace(struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) struct batadv_orig_ifinfo *new_candidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct batadv_orig_ifinfo *old_candidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) spin_lock_bh(&orig_node->neigh_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) old_candidate = orig_node->last_bonding_candidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (new_candidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) kref_get(&new_candidate->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) orig_node->last_bonding_candidate = new_candidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) spin_unlock_bh(&orig_node->neigh_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (old_candidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) batadv_orig_ifinfo_put(old_candidate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * batadv_find_router() - find a suitable router for this originator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * @orig_node: the destination node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * @recv_if: pointer to interface this packet was received on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * Return: the router which should be used for this orig_node on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * this interface, or NULL if not available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) struct batadv_neigh_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) batadv_find_router(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) struct batadv_algo_ops *bao = bat_priv->algo_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) struct batadv_neigh_node *first_candidate_router = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) struct batadv_neigh_node *next_candidate_router = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct batadv_neigh_node *router, *cand_router = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) struct batadv_neigh_node *last_cand_router = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) struct batadv_orig_ifinfo *cand, *first_candidate = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct batadv_orig_ifinfo *next_candidate = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) struct batadv_orig_ifinfo *last_candidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) bool last_candidate_found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) router = batadv_orig_router_get(orig_node, recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (!router)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return router;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) /* only consider bonding for recv_if == BATADV_IF_DEFAULT (first hop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) * and if activated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (!(recv_if == BATADV_IF_DEFAULT && atomic_read(&bat_priv->bonding)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return router;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) /* bonding: loop through the list of possible routers found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * for the various outgoing interfaces and find a candidate after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * the last chosen bonding candidate (next_candidate). If no such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * router is found, use the first candidate found (the previously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * chosen bonding candidate might have been the last one in the list).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * If this can't be found either, return the previously chosen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * router - obviously there are no other candidates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) last_candidate = batadv_last_bonding_get(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (last_candidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) last_cand_router = rcu_dereference(last_candidate->router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) /* acquire some structures and references ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (!kref_get_unless_zero(&cand->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) cand_router = rcu_dereference(cand->router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!cand_router)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (!kref_get_unless_zero(&cand_router->refcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) cand_router = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) /* alternative candidate should be good enough to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) * considered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (!bao->neigh.is_similar_or_better(cand_router,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) cand->if_outgoing, router,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) recv_if))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) /* don't use the same router twice */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (last_cand_router == cand_router)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) /* mark the first possible candidate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (!first_candidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) kref_get(&cand_router->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) kref_get(&cand->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) first_candidate = cand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) first_candidate_router = cand_router;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) /* check if the loop has already passed the previously selected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * candidate ... this function should select the next candidate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * AFTER the previously used bonding candidate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (!last_candidate || last_candidate_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) next_candidate = cand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) next_candidate_router = cand_router;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (last_candidate == cand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) last_candidate_found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /* free references */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (cand_router) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) batadv_neigh_node_put(cand_router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) cand_router = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) batadv_orig_ifinfo_put(cand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) /* After finding candidates, handle the three cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * 1) there is a next candidate, use that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * 2) there is no next candidate, use the first of the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * 3) there is no candidate at all, return the default router
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (next_candidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) batadv_neigh_node_put(router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) kref_get(&next_candidate_router->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) router = next_candidate_router;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) batadv_last_bonding_replace(orig_node, next_candidate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) } else if (first_candidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) batadv_neigh_node_put(router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) kref_get(&first_candidate_router->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) router = first_candidate_router;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) batadv_last_bonding_replace(orig_node, first_candidate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) batadv_last_bonding_replace(orig_node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) /* cleanup of candidates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (first_candidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) batadv_neigh_node_put(first_candidate_router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) batadv_orig_ifinfo_put(first_candidate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (next_candidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) batadv_neigh_node_put(next_candidate_router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) batadv_orig_ifinfo_put(next_candidate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) if (last_candidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) batadv_orig_ifinfo_put(last_candidate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) return router;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static int batadv_route_unicast_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) struct batadv_orig_node *orig_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) struct batadv_unicast_packet *unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct ethhdr *ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) int res, hdr_len, ret = NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) unicast_packet = (struct batadv_unicast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /* TTL exceeded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (unicast_packet->ttl < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) ethhdr->h_source, unicast_packet->dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) /* get routing information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /* create a copy of the skb, if needed, to modify it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (skb_cow(skb, ETH_HLEN) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) goto put_orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) /* decrement ttl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) unicast_packet = (struct batadv_unicast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) unicast_packet->ttl--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) switch (unicast_packet->packet_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) case BATADV_UNICAST_4ADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) hdr_len = sizeof(struct batadv_unicast_4addr_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) case BATADV_UNICAST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) hdr_len = sizeof(struct batadv_unicast_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) /* other packet types not supported - yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) hdr_len = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (hdr_len > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) batadv_skb_set_priority(skb, hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) /* translate transmit result into receive result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (res == NET_XMIT_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) ret = NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) /* skb was transmitted and consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) len + ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) /* skb was consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) put_orig_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) * batadv_reroute_unicast_packet() - update the unicast header for re-routing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * @skb: unicast packet to process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) * @unicast_packet: the unicast header to be updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * @dst_addr: the payload destination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * @vid: VLAN identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * Search the translation table for dst_addr and update the unicast header with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * the new corresponding information (originator address where the destination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * client currently is and its known TTVN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * Return: true if the packet header has been updated, false otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) struct batadv_unicast_packet *unicast_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) u8 *dst_addr, unsigned short vid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) struct batadv_orig_node *orig_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) struct batadv_hard_iface *primary_if = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) u8 *orig_addr, orig_ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) primary_if = batadv_primary_if_get_selected(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (!primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) orig_addr = primary_if->net_dev->dev_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) orig_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) orig_addr = orig_node->orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) /* update the packet header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) ether_addr_copy(unicast_packet->dest, orig_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) unicast_packet->ttvn = orig_ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) if (primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) batadv_hardif_put(primary_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) if (orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) static bool batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) struct sk_buff *skb, int hdr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) struct batadv_unicast_packet *unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) struct batadv_hard_iface *primary_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) struct batadv_orig_node *orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) u8 curr_ttvn, old_ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) unsigned short vid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) int is_old_ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) /* check if there is enough data before accessing it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) /* create a copy of the skb (in case of for re-routing) to modify it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) unicast_packet = (struct batadv_unicast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) vid = batadv_get_vid(skb, hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) ethhdr = (struct ethhdr *)(skb->data + hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) /* do not reroute multicast frames in a unicast header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (is_multicast_ether_addr(ethhdr->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /* check if the destination client was served by this node and it is now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * roaming. In this case, it means that the node has got a ROAM_ADV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * message and that it knows the new destination in the mesh to re-route
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * the packet to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) ethhdr->h_dest, vid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) batadv_dbg_ratelimited(BATADV_DBG_TT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) unicast_packet->dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) ethhdr->h_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) /* at this point the mesh destination should have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) * substituted with the originator address found in the global
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) * table. If not, let the packet go untouched anyway because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) * there is nothing the node can do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) /* retrieve the TTVN known by this node for the packet destination. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * value is used later to check if the node which sent (or re-routed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * last time) the packet had an updated information or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) curr_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) orig_node = batadv_orig_hash_find(bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) unicast_packet->dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) /* if it is not possible to find the orig_node representing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * destination, the packet can immediately be dropped as it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * not be possible to deliver it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) curr_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) /* check if the TTVN contained in the packet is fresher than what the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) * node knows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) if (!is_old_ttvn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) old_ttvn = unicast_packet->ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) /* the packet was forged based on outdated network information. Its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * destination can possibly be updated and forwarded towards the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * target host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) ethhdr->h_dest, vid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) unicast_packet->dest, ethhdr->h_dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) old_ttvn, curr_ttvn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) /* the packet has not been re-routed: either the destination is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) * currently served by this node or there is no destination at all and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) * it is possible to drop the packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) /* update the header in order to let the packet be delivered to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) * node's soft interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) primary_if = batadv_primary_if_get_selected(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (!primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) /* update the packet header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) unicast_packet->ttvn = curr_ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) batadv_hardif_put(primary_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) * batadv_recv_unhandled_unicast_packet() - receive and process packets which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) * are in the unicast number space but not yet known to the implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) * @skb: unicast tvlv packet to process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) * @recv_if: pointer to interface this packet was received on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) struct batadv_unicast_packet *unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) int check, hdr_size = sizeof(*unicast_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) if (check < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) /* we don't know about this type, drop it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) unicast_packet = (struct batadv_unicast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) return batadv_route_unicast_packet(skb, recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * batadv_recv_unicast_packet() - Process incoming unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * @skb: incoming packet buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * @recv_if: incoming hard interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) int batadv_recv_unicast_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) struct batadv_unicast_packet *unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) struct batadv_unicast_4addr_packet *unicast_4addr_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) u8 *orig_addr, *orig_addr_gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) struct batadv_orig_node *orig_node = NULL, *orig_node_gw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) int check, hdr_size = sizeof(*unicast_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) enum batadv_subtype subtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) int ret = NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) bool is4addr, is_gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) unicast_packet = (struct batadv_unicast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) /* the caller function should have already pulled 2 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) if (is4addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) hdr_size = sizeof(*unicast_4addr_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) /* function returns -EREMOTE for promiscuous packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) /* Even though the packet is not for us, we might save it to use for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * decoding a later received coded packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) if (check == -EREMOTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (check < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) unicast_packet = (struct batadv_unicast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) /* packet for me */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) /* If this is a unicast packet from another backgone gw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * drop it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) orig_addr_gw = eth_hdr(skb)->h_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) orig_node_gw = batadv_orig_hash_find(bat_priv, orig_addr_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (orig_node_gw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) is_gw = batadv_bla_is_backbone_gw(skb, orig_node_gw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) hdr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) batadv_orig_node_put(orig_node_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) if (is_gw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) batadv_dbg(BATADV_DBG_BLA, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) "%s(): Dropped unicast pkt received from another backbone gw %pM.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) __func__, orig_addr_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) if (is4addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) unicast_4addr_packet =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) (struct batadv_unicast_4addr_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) subtype = unicast_4addr_packet->subtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) batadv_dat_inc_counter(bat_priv, subtype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) /* Only payload data should be considered for speedy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) * join. For example, DAT also uses unicast 4addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) * types, but those packets should not be considered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * for speedy join, since the clients do not actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * reside at the sending originator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (subtype == BATADV_P_DATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) orig_addr = unicast_4addr_packet->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) orig_node = batadv_orig_hash_find(bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) orig_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) hdr_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) goto rx_success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) hdr_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) goto rx_success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) batadv_interface_rx(recv_if->soft_iface, skb, hdr_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) rx_success:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) ret = batadv_route_unicast_packet(skb, recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) /* skb was consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * batadv_recv_unicast_tvlv() - receive and process unicast tvlv packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) * @skb: unicast tvlv packet to process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) * @recv_if: pointer to interface this packet was received on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) int batadv_recv_unicast_tvlv(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) unsigned char *tvlv_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) u16 tvlv_buff_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) int hdr_size = sizeof(*unicast_tvlv_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) int ret = NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) /* the header is likely to be modified while forwarding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) if (skb_cow(skb, hdr_size) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) /* packet needs to be linearized to access the tvlv content */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (skb_linearize(skb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) tvlv_buff = (unsigned char *)(skb->data + hdr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (tvlv_buff_len > skb->len - hdr_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) unicast_tvlv_packet->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) unicast_tvlv_packet->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) tvlv_buff, tvlv_buff_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) if (ret != NET_RX_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) ret = batadv_route_unicast_packet(skb, recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) /* skb was consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) * batadv_recv_frag_packet() - process received fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) * @skb: the received fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) * @recv_if: interface that the skb is received on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) * This function does one of the three following things: 1) Forward fragment, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) * the assembled packet will exceed our MTU; 2) Buffer fragment, if we still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) * lack further fragments; 3) Merge fragments, if we have all needed parts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) * Return: NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) int batadv_recv_frag_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) struct batadv_orig_node *orig_node_src = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) struct batadv_frag_packet *frag_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) int ret = NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) if (batadv_check_unicast_packet(bat_priv, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) sizeof(*frag_packet)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) frag_packet = (struct batadv_frag_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) if (!orig_node_src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) skb->priority = frag_packet->priority + 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) /* Route the fragment if it is not for us and too big to be merged. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) /* skb was consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) ret = NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) goto put_orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) /* Add fragment to buffer and merge if possible. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) if (!batadv_frag_skb_buffer(&skb, orig_node_src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) goto put_orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) /* Deliver merged packet to the appropriate handler, if it was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * merged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) batadv_batman_skb_recv(skb, recv_if->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) &recv_if->batman_adv_ptype, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) /* skb was consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) ret = NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) put_orig_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) batadv_orig_node_put(orig_node_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) * batadv_recv_bcast_packet() - Process incoming broadcast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) * @skb: incoming packet buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) * @recv_if: incoming hard interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) int batadv_recv_bcast_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) struct batadv_orig_node *orig_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) struct batadv_bcast_packet *bcast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) int hdr_size = sizeof(*bcast_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) int ret = NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) s32 seq_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) u32 seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) /* drop packet if it has not necessary minimum size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (unlikely(!pskb_may_pull(skb, hdr_size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) /* packet with broadcast indication but unicast recipient */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) if (!is_broadcast_ether_addr(ethhdr->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) /* packet with broadcast/multicast sender address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) if (is_multicast_ether_addr(ethhdr->h_source))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) /* ignore broadcasts sent by myself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) bcast_packet = (struct batadv_bcast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) /* ignore broadcasts originated by myself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) if (bcast_packet->ttl < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) spin_lock_bh(&orig_node->bcast_seqno_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) seqno = ntohl(bcast_packet->seqno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) /* check whether the packet is a duplicate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) seqno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) goto spin_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) seq_diff = seqno - orig_node->last_bcast_seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) /* check whether the packet is old and the host just restarted. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) if (batadv_window_protected(bat_priv, seq_diff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) BATADV_BCAST_MAX_AGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) &orig_node->bcast_seqno_reset, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) goto spin_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) /* mark broadcast in flood history, update window position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) * if required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) orig_node->last_bcast_seqno = seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) spin_unlock_bh(&orig_node->bcast_seqno_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) /* check whether this has been sent by another originator before */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) if (batadv_bla_check_bcast_duplist(bat_priv, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) /* rebroadcast packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) batadv_add_bcast_packet_to_list(bat_priv, skb, 1, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) /* don't hand the broadcast up if it is from an originator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) * from the same backbone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) goto rx_success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) goto rx_success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) /* broadcast for me */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) batadv_interface_rx(recv_if->soft_iface, skb, hdr_size, orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) rx_success:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) ret = NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) spin_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) spin_unlock_bh(&orig_node->bcast_seqno_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) if (orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) }