^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) 2009-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
^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 "gateway_client.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/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/gfp.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/if_vlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/lockdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <uapi/linux/batadv_packet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <uapi/linux/batman_adv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include "hard-interface.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include "log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include "netlink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include "originator.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include "routing.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include "soft-interface.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include "translation-table.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* These are the offsets of the "hw type" and "hw address length" in the dhcp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * packet starting at the beginning of the dhcp header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define BATADV_DHCP_HTYPE_OFFSET 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define BATADV_DHCP_HLEN_OFFSET 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* Value of htype representing Ethernet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define BATADV_DHCP_HTYPE_ETHERNET 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* This is the offset of the "chaddr" field in the dhcp packet starting at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * beginning of the dhcp header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define BATADV_DHCP_CHADDR_OFFSET 28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * batadv_gw_node_release() - release gw_node from lists and queue for free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * after rcu grace period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * @ref: kref pointer of the gw_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) void batadv_gw_node_release(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct batadv_gw_node *gw_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) gw_node = container_of(ref, struct batadv_gw_node, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) batadv_orig_node_put(gw_node->orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) kfree_rcu(gw_node, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * batadv_gw_get_selected_gw_node() - Get currently selected gateway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * Return: selected gateway (with increased refcnt), NULL on errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct batadv_gw_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct batadv_gw_node *gw_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) gw_node = rcu_dereference(bat_priv->gw.curr_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!kref_get_unless_zero(&gw_node->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) gw_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return gw_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * batadv_gw_get_selected_orig() - Get originator of currently selected gateway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * Return: orig_node of selected gateway (with increased refcnt), NULL on errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct batadv_orig_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct batadv_gw_node *gw_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct batadv_orig_node *orig_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) gw_node = batadv_gw_get_selected_gw_node(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) orig_node = gw_node->orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!kref_get_unless_zero(&orig_node->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) orig_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) batadv_gw_node_put(gw_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static void batadv_gw_select(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct batadv_gw_node *new_gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct batadv_gw_node *curr_gw_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) spin_lock_bh(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (new_gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) kref_get(&new_gw_node->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) curr_gw_node = rcu_replace_pointer(bat_priv->gw.curr_gw, new_gw_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (curr_gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) batadv_gw_node_put(curr_gw_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) spin_unlock_bh(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * batadv_gw_reselect() - force a gateway reselection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * Set a flag to remind the GW component to perform a new gateway reselection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * However this function does not ensure that the current gateway is going to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * deselected. The reselection mechanism may elect the same gateway once again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * This means that invoking batadv_gw_reselect() does not guarantee a gateway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * change and therefore a uevent is not necessarily expected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) void batadv_gw_reselect(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) atomic_set(&bat_priv->gw.reselect, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * batadv_gw_check_client_stop() - check if client mode has been switched off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * This function assumes the caller has checked that the gw state *is actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * changing*. This function is not supposed to be called when there is no state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct batadv_gw_node *curr_gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!curr_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* deselect the current gateway so that next time that client mode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * enabled a proper GW_ADD event can be sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) batadv_gw_select(bat_priv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* if batman-adv is switching the gw client mode off and a gateway was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * already selected, send a DEL uevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) batadv_gw_node_put(curr_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * batadv_gw_election() - Elect the best gateway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) void batadv_gw_election(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct batadv_gw_node *curr_gw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct batadv_gw_node *next_gw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct batadv_neigh_node *router = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct batadv_neigh_ifinfo *router_ifinfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) char gw_addr[18] = { '\0' };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (!bat_priv->algo_ops->gw.get_best_gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /* if gw.reselect is set to 1 it means that a previous call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * gw.is_eligible() said that we have a new best GW, therefore it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * now be picked from the list and selected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (curr_gw == next_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (next_gw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) router = batadv_orig_router_get(next_gw->orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) BATADV_IF_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (!router) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) batadv_gw_reselect(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) router_ifinfo = batadv_neigh_ifinfo_get(router,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) BATADV_IF_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!router_ifinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) batadv_gw_reselect(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (curr_gw && !next_gw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) "Removing selected gateway - no gateway in range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) } else if (!curr_gw && next_gw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) next_gw->orig_node->orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) next_gw->bandwidth_down / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) next_gw->bandwidth_down % 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) next_gw->bandwidth_up / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) next_gw->bandwidth_up % 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) router_ifinfo->bat_iv.tq_avg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) gw_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) next_gw->orig_node->orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) next_gw->bandwidth_down / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) next_gw->bandwidth_down % 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) next_gw->bandwidth_up / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) next_gw->bandwidth_up % 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) router_ifinfo->bat_iv.tq_avg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) gw_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) batadv_gw_select(bat_priv, next_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (curr_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) batadv_gw_node_put(curr_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (next_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) batadv_gw_node_put(next_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (router)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) batadv_neigh_node_put(router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (router_ifinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) batadv_neigh_ifinfo_put(router_ifinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * batadv_gw_check_election() - Elect orig node as best gateway when eligible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * @orig_node: orig node which is to be checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) void batadv_gw_check_election(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct batadv_orig_node *orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct batadv_orig_node *curr_gw_orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /* abort immediately if the routing algorithm does not support gateway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * election
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!bat_priv->algo_ops->gw.is_eligible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!curr_gw_orig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto reselect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* this node already is the gateway */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (curr_gw_orig == orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) orig_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) reselect:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) batadv_gw_reselect(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (curr_gw_orig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) batadv_orig_node_put(curr_gw_orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * batadv_gw_node_add() - add gateway node to list of available gateways
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * @orig_node: originator announcing gateway capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * @gateway: announced bandwidth information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * Has to be called with the appropriate locks being acquired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * (gw.list_lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static void batadv_gw_node_add(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct batadv_tvlv_gateway_data *gateway)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct batadv_gw_node *gw_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) lockdep_assert_held(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (gateway->bandwidth_down == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) kref_init(&gw_node->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) INIT_HLIST_NODE(&gw_node->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) kref_get(&orig_node->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) gw_node->orig_node = orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) kref_get(&gw_node->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) bat_priv->gw.generation++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) orig_node->orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) ntohl(gateway->bandwidth_down) / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ntohl(gateway->bandwidth_down) % 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) ntohl(gateway->bandwidth_up) / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ntohl(gateway->bandwidth_up) % 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /* don't return reference to new gw_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) batadv_gw_node_put(gw_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * batadv_gw_node_get() - retrieve gateway node from list of available gateways
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * @orig_node: originator announcing gateway capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * Return: gateway node if found or NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct batadv_orig_node *orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (gw_node_tmp->orig_node != orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (!kref_get_unless_zero(&gw_node_tmp->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) gw_node = gw_node_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return gw_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * batadv_gw_node_update() - update list of available gateways with changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * bandwidth information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * @orig_node: originator announcing gateway capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * @gateway: announced bandwidth information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) void batadv_gw_node_update(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct batadv_tvlv_gateway_data *gateway)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct batadv_gw_node *gw_node, *curr_gw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) spin_lock_bh(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) gw_node = batadv_gw_node_get(bat_priv, orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (!gw_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) batadv_gw_node_add(bat_priv, orig_node, gateway);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) spin_unlock_bh(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) spin_unlock_bh(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) orig_node->orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) gw_node->bandwidth_down / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) gw_node->bandwidth_down % 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) gw_node->bandwidth_up / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) gw_node->bandwidth_up % 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ntohl(gateway->bandwidth_down) / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) ntohl(gateway->bandwidth_down) % 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) ntohl(gateway->bandwidth_up) / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) ntohl(gateway->bandwidth_up) % 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (ntohl(gateway->bandwidth_down) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) "Gateway %pM removed from gateway list\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) orig_node->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /* Note: We don't need a NULL check here, since curr_gw never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * gets dereferenced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) spin_lock_bh(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (!hlist_unhashed(&gw_node->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) hlist_del_init_rcu(&gw_node->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) batadv_gw_node_put(gw_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) bat_priv->gw.generation++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) spin_unlock_bh(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) if (gw_node == curr_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) batadv_gw_reselect(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (curr_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) batadv_gw_node_put(curr_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) batadv_gw_node_put(gw_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * batadv_gw_node_delete() - Remove orig_node from gateway list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * @orig_node: orig node which is currently in process of being removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) void batadv_gw_node_delete(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) struct batadv_orig_node *orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct batadv_tvlv_gateway_data gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) gateway.bandwidth_down = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) gateway.bandwidth_up = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) batadv_gw_node_update(bat_priv, orig_node, &gateway);
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * batadv_gw_node_free() - Free gateway information from soft interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) void batadv_gw_node_free(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct batadv_gw_node *gw_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct hlist_node *node_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) spin_lock_bh(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) hlist_for_each_entry_safe(gw_node, node_tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) &bat_priv->gw.gateway_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) hlist_del_init_rcu(&gw_node->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) batadv_gw_node_put(gw_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) bat_priv->gw.generation++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) spin_unlock_bh(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) #ifdef CONFIG_BATMAN_ADV_DEBUGFS
^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) * batadv_gw_client_seq_print_text() - Print the gateway table in a seq file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * @seq: seq file to print on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * @offset: not used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * Return: always 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct net_device *net_dev = (struct net_device *)seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct batadv_priv *bat_priv = netdev_priv(net_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct batadv_hard_iface *primary_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) primary_if = batadv_seq_print_text_primary_if_get(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (!primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) BATADV_SOURCE_VERSION, primary_if->net_dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) primary_if->net_dev->dev_addr, net_dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) bat_priv->algo_ops->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) batadv_hardif_put(primary_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (!bat_priv->algo_ops->gw.print) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) seq_puts(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) "No printing function for this routing protocol\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) bat_priv->algo_ops->gw.print(bat_priv, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * batadv_gw_dump() - Dump gateways into a message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * @msg: Netlink message to dump into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * @cb: Control block containing additional options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * Return: Error code, or length of message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) struct batadv_hard_iface *primary_if = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) struct net *net = sock_net(cb->skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct net_device *soft_iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) struct batadv_priv *bat_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) int ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) ifindex = batadv_netlink_get_ifindex(cb->nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) BATADV_ATTR_MESH_IFINDEX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (!ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) soft_iface = dev_get_by_index(net, ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) bat_priv = netdev_priv(soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) primary_if = batadv_primary_if_get_selected(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (!bat_priv->algo_ops->gw.dump) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) ret = msg->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) batadv_hardif_put(primary_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (soft_iface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) dev_put(soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return ret;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) * @skb: the packet to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) * @header_len: a pointer to the batman-adv header size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * @chaddr: buffer where the client address will be stored. Valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * only if the function returns BATADV_DHCP_TO_CLIENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * This function may re-allocate the data buffer of the skb passed as argument.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * while parsing it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) enum batadv_dhcp_recipient
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) u8 *chaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct iphdr *iphdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) struct ipv6hdr *ipv6hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) struct udphdr *udphdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) struct vlan_ethhdr *vhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) int chaddr_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) __be16 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) u8 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) /* check for ethernet header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) proto = ethhdr->h_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) *header_len += ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) /* check for initial vlan header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (proto == htons(ETH_P_8021Q)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) vhdr = vlan_eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) proto = vhdr->h_vlan_encapsulated_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) *header_len += VLAN_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /* check for ip header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) switch (proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) case htons(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) iphdr = (struct iphdr *)(skb->data + *header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) *header_len += iphdr->ihl * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) /* check for udp header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (iphdr->protocol != IPPROTO_UDP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) case htons(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) *header_len += sizeof(*ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) /* check for udp header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (ipv6hdr->nexthdr != IPPROTO_UDP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) udphdr = (struct udphdr *)(skb->data + *header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) *header_len += sizeof(*udphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /* check for bootp port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) switch (proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) case htons(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (udphdr->dest == htons(67))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) ret = BATADV_DHCP_TO_SERVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) else if (udphdr->source == htons(67))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) ret = BATADV_DHCP_TO_CLIENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) case htons(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (udphdr->dest == htons(547))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) ret = BATADV_DHCP_TO_SERVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) else if (udphdr->source == htons(547))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) ret = BATADV_DHCP_TO_CLIENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) break;
^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) chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) /* store the client address if the message is going to a client */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (ret == BATADV_DHCP_TO_CLIENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (!pskb_may_pull(skb, chaddr_offset + ETH_ALEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /* check if the DHCP packet carries an Ethernet DHCP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (*p != BATADV_DHCP_HTYPE_ETHERNET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) /* check if the DHCP packet carries a valid Ethernet address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (*p != ETH_ALEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return BATADV_DHCP_NO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) ether_addr_copy(chaddr, skb->data + chaddr_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * batadv_gw_out_of_range() - check if the dhcp request destination is the best
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * gateway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * @skb: the outgoing packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * Check if the skb is a DHCP request and if it is sent to the current best GW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * server. Due to topology changes it may be the case that the GW server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * previously selected is not the best one anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * This call might reallocate skb data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * Return: true if the packet destination is unicast and it is not the best gw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) struct batadv_neigh_node *neigh_curr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) struct batadv_neigh_node *neigh_old = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) struct batadv_orig_node *orig_dst_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) struct batadv_gw_node *gw_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) struct batadv_gw_node *curr_gw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) bool out_of_range = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) u8 curr_tq_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) unsigned short vid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) vid = batadv_get_vid(skb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (is_multicast_ether_addr(ethhdr->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) ethhdr->h_dest, vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (!orig_dst_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (!gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) switch (atomic_read(&bat_priv->gw.mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) case BATADV_GW_MODE_SERVER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) /* If we are a GW then we are our best GW. We can artificially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * set the tq towards ourself as the maximum value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) curr_tq_avg = BATADV_TQ_MAX_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) case BATADV_GW_MODE_CLIENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) if (!curr_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) /* packet is going to our gateway */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (curr_gw->orig_node == orig_dst_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) /* If the dhcp packet has been sent to a different gw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) * we have to evaluate whether the old gw is still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) * reliable enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (!neigh_curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) BATADV_IF_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) if (!curr_ifinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) batadv_neigh_ifinfo_put(curr_ifinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) case BATADV_GW_MODE_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) if (!neigh_old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (!old_ifinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) out_of_range = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) batadv_neigh_ifinfo_put(old_ifinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (orig_dst_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) batadv_orig_node_put(orig_dst_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (curr_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) batadv_gw_node_put(curr_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (gw_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) batadv_gw_node_put(gw_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (neigh_old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) batadv_neigh_node_put(neigh_old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (neigh_curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) batadv_neigh_node_put(neigh_curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) return out_of_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }