10046b040SSven Eckelmann /* Copyright (C) 2009-2016 B.A.T.M.A.N. contributors: 2c6c8fea2SSven Eckelmann * 3c6c8fea2SSven Eckelmann * Marek Lindner 4c6c8fea2SSven Eckelmann * 5c6c8fea2SSven Eckelmann * This program is free software; you can redistribute it and/or 6c6c8fea2SSven Eckelmann * modify it under the terms of version 2 of the GNU General Public 7c6c8fea2SSven Eckelmann * License as published by the Free Software Foundation. 8c6c8fea2SSven Eckelmann * 9c6c8fea2SSven Eckelmann * This program is distributed in the hope that it will be useful, but 10c6c8fea2SSven Eckelmann * WITHOUT ANY WARRANTY; without even the implied warranty of 11c6c8fea2SSven Eckelmann * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12c6c8fea2SSven Eckelmann * General Public License for more details. 13c6c8fea2SSven Eckelmann * 14c6c8fea2SSven Eckelmann * You should have received a copy of the GNU General Public License 15ebf38fb7SAntonio Quartulli * along with this program; if not, see <http://www.gnu.org/licenses/>. 16c6c8fea2SSven Eckelmann */ 17c6c8fea2SSven Eckelmann 18c6c8fea2SSven Eckelmann #include "gateway_client.h" 191e2c2a4fSSven Eckelmann #include "main.h" 201e2c2a4fSSven Eckelmann 211e2c2a4fSSven Eckelmann #include <linux/atomic.h> 221e2c2a4fSSven Eckelmann #include <linux/byteorder/generic.h> 231e2c2a4fSSven Eckelmann #include <linux/etherdevice.h> 241e2c2a4fSSven Eckelmann #include <linux/fs.h> 251e2c2a4fSSven Eckelmann #include <linux/if_ether.h> 261e2c2a4fSSven Eckelmann #include <linux/if_vlan.h> 271e2c2a4fSSven Eckelmann #include <linux/in.h> 281e2c2a4fSSven Eckelmann #include <linux/ip.h> 291e2c2a4fSSven Eckelmann #include <linux/ipv6.h> 301e2c2a4fSSven Eckelmann #include <linux/kernel.h> 31e7aed321SSven Eckelmann #include <linux/kref.h> 321e2c2a4fSSven Eckelmann #include <linux/list.h> 331e2c2a4fSSven Eckelmann #include <linux/netdevice.h> 341e2c2a4fSSven Eckelmann #include <linux/rculist.h> 351e2c2a4fSSven Eckelmann #include <linux/rcupdate.h> 361e2c2a4fSSven Eckelmann #include <linux/seq_file.h> 371e2c2a4fSSven Eckelmann #include <linux/skbuff.h> 381e2c2a4fSSven Eckelmann #include <linux/slab.h> 391e2c2a4fSSven Eckelmann #include <linux/spinlock.h> 401e2c2a4fSSven Eckelmann #include <linux/stddef.h> 411e2c2a4fSSven Eckelmann #include <linux/udp.h> 421e2c2a4fSSven Eckelmann 43c6c8fea2SSven Eckelmann #include "gateway_common.h" 44c6c8fea2SSven Eckelmann #include "hard-interface.h" 45ba412080SSven Eckelmann #include "log.h" 4657f0c07cSLinus Lüssing #include "originator.h" 471e2c2a4fSSven Eckelmann #include "packet.h" 4843676ab5SAntonio Quartulli #include "routing.h" 491e2c2a4fSSven Eckelmann #include "sysfs.h" 501e2c2a4fSSven Eckelmann #include "translation-table.h" 51c6c8fea2SSven Eckelmann 526c413b1cSAntonio Quartulli /* These are the offsets of the "hw type" and "hw address length" in the dhcp 536c413b1cSAntonio Quartulli * packet starting at the beginning of the dhcp header 549cfc7bd6SSven Eckelmann */ 556c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_OFFSET 1 566c413b1cSAntonio Quartulli #define BATADV_DHCP_HLEN_OFFSET 2 576c413b1cSAntonio Quartulli /* Value of htype representing Ethernet */ 586c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_ETHERNET 0x01 596c413b1cSAntonio Quartulli /* This is the offset of the "chaddr" field in the dhcp packet starting at the 606c413b1cSAntonio Quartulli * beginning of the dhcp header 616c413b1cSAntonio Quartulli */ 626c413b1cSAntonio Quartulli #define BATADV_DHCP_CHADDR_OFFSET 28 6343676ab5SAntonio Quartulli 64e7aed321SSven Eckelmann /** 65e7aed321SSven Eckelmann * batadv_gw_node_release - release gw_node from lists and queue for free after 66e7aed321SSven Eckelmann * rcu grace period 67e7aed321SSven Eckelmann * @ref: kref pointer of the gw_node 68e7aed321SSven Eckelmann */ 69e7aed321SSven Eckelmann static void batadv_gw_node_release(struct kref *ref) 7025b6d3c1SMarek Lindner { 71e7aed321SSven Eckelmann struct batadv_gw_node *gw_node; 72e7aed321SSven Eckelmann 73e7aed321SSven Eckelmann gw_node = container_of(ref, struct batadv_gw_node, refcount); 74e7aed321SSven Eckelmann 755d967310SSven Eckelmann batadv_orig_node_put(gw_node->orig_node); 76eb340b2fSPaul E. McKenney kfree_rcu(gw_node, rcu); 77c6c8fea2SSven Eckelmann } 78e7aed321SSven Eckelmann 79e7aed321SSven Eckelmann /** 803a01743dSSven Eckelmann * batadv_gw_node_put - decrement the gw_node refcounter and possibly release it 81e7aed321SSven Eckelmann * @gw_node: gateway node to free 82e7aed321SSven Eckelmann */ 83*34d99cfeSAntonio Quartulli void batadv_gw_node_put(struct batadv_gw_node *gw_node) 84e7aed321SSven Eckelmann { 85e7aed321SSven Eckelmann kref_put(&gw_node->refcount, batadv_gw_node_release); 86377fe0f9SAntonio Quartulli } 87c6c8fea2SSven Eckelmann 88*34d99cfeSAntonio Quartulli struct batadv_gw_node * 8956303d34SSven Eckelmann batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv) 90c6c8fea2SSven Eckelmann { 9156303d34SSven Eckelmann struct batadv_gw_node *gw_node; 92c6c8fea2SSven Eckelmann 935d02b3cdSLinus Lüssing rcu_read_lock(); 94807736f6SSven Eckelmann gw_node = rcu_dereference(bat_priv->gw.curr_gw); 95c4aac1abSMarek Lindner if (!gw_node) 965d02b3cdSLinus Lüssing goto out; 97c6c8fea2SSven Eckelmann 98e7aed321SSven Eckelmann if (!kref_get_unless_zero(&gw_node->refcount)) 99c4aac1abSMarek Lindner gw_node = NULL; 100c4aac1abSMarek Lindner 101c4aac1abSMarek Lindner out: 102c4aac1abSMarek Lindner rcu_read_unlock(); 103c4aac1abSMarek Lindner return gw_node; 104c4aac1abSMarek Lindner } 105c4aac1abSMarek Lindner 10656303d34SSven Eckelmann struct batadv_orig_node * 10756303d34SSven Eckelmann batadv_gw_get_selected_orig(struct batadv_priv *bat_priv) 108c4aac1abSMarek Lindner { 10956303d34SSven Eckelmann struct batadv_gw_node *gw_node; 11056303d34SSven Eckelmann struct batadv_orig_node *orig_node = NULL; 111c4aac1abSMarek Lindner 1121409a834SSven Eckelmann gw_node = batadv_gw_get_selected_gw_node(bat_priv); 113c4aac1abSMarek Lindner if (!gw_node) 1147b36e8eeSMarek Lindner goto out; 1155d02b3cdSLinus Lüssing 116c4aac1abSMarek Lindner rcu_read_lock(); 117c4aac1abSMarek Lindner orig_node = gw_node->orig_node; 118c4aac1abSMarek Lindner if (!orig_node) 119c4aac1abSMarek Lindner goto unlock; 120c4aac1abSMarek Lindner 1217c124391SSven Eckelmann if (!kref_get_unless_zero(&orig_node->refcount)) 1227b36e8eeSMarek Lindner orig_node = NULL; 12343c70ad5SLinus Lüssing 124c4aac1abSMarek Lindner unlock: 1255d02b3cdSLinus Lüssing rcu_read_unlock(); 126c4aac1abSMarek Lindner out: 127c6c8fea2SSven Eckelmann if (gw_node) 1283a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 129c4aac1abSMarek Lindner return orig_node; 130c6c8fea2SSven Eckelmann } 131c6c8fea2SSven Eckelmann 13256303d34SSven Eckelmann static void batadv_gw_select(struct batadv_priv *bat_priv, 13356303d34SSven Eckelmann struct batadv_gw_node *new_gw_node) 134c6c8fea2SSven Eckelmann { 13556303d34SSven Eckelmann struct batadv_gw_node *curr_gw_node; 136c6c8fea2SSven Eckelmann 137807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 138c4aac1abSMarek Lindner 139a08d497dSSven Eckelmann if (new_gw_node) 140a08d497dSSven Eckelmann kref_get(&new_gw_node->refcount); 141c6c8fea2SSven Eckelmann 142807736f6SSven Eckelmann curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1); 143807736f6SSven Eckelmann rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node); 14425b6d3c1SMarek Lindner 14525b6d3c1SMarek Lindner if (curr_gw_node) 1463a01743dSSven Eckelmann batadv_gw_node_put(curr_gw_node); 147c4aac1abSMarek Lindner 148807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 149c4aac1abSMarek Lindner } 150c4aac1abSMarek Lindner 1514e820e72SAntonio Quartulli /** 1524e820e72SAntonio Quartulli * batadv_gw_reselect - force a gateway reselection 1534e820e72SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 1544e820e72SAntonio Quartulli * 1554e820e72SAntonio Quartulli * Set a flag to remind the GW component to perform a new gateway reselection. 1564e820e72SAntonio Quartulli * However this function does not ensure that the current gateway is going to be 1574e820e72SAntonio Quartulli * deselected. The reselection mechanism may elect the same gateway once again. 1584e820e72SAntonio Quartulli * 1594e820e72SAntonio Quartulli * This means that invoking batadv_gw_reselect() does not guarantee a gateway 1604e820e72SAntonio Quartulli * change and therefore a uevent is not necessarily expected. 1614e820e72SAntonio Quartulli */ 1624e820e72SAntonio Quartulli void batadv_gw_reselect(struct batadv_priv *bat_priv) 163c4aac1abSMarek Lindner { 164807736f6SSven Eckelmann atomic_set(&bat_priv->gw.reselect, 1); 165c6c8fea2SSven Eckelmann } 166c6c8fea2SSven Eckelmann 167c6eaa3f0SAntonio Quartulli /** 168c6eaa3f0SAntonio Quartulli * batadv_gw_check_client_stop - check if client mode has been switched off 169c6eaa3f0SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 170c6eaa3f0SAntonio Quartulli * 171c6eaa3f0SAntonio Quartulli * This function assumes the caller has checked that the gw state *is actually 172c6eaa3f0SAntonio Quartulli * changing*. This function is not supposed to be called when there is no state 173c6eaa3f0SAntonio Quartulli * change. 174c6eaa3f0SAntonio Quartulli */ 175c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv) 176c6eaa3f0SAntonio Quartulli { 177c6eaa3f0SAntonio Quartulli struct batadv_gw_node *curr_gw; 178c6eaa3f0SAntonio Quartulli 1793a24a63eSAntonio Quartulli if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT) 180c6eaa3f0SAntonio Quartulli return; 181c6eaa3f0SAntonio Quartulli 182c6eaa3f0SAntonio Quartulli curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 183c6eaa3f0SAntonio Quartulli if (!curr_gw) 184c6eaa3f0SAntonio Quartulli return; 185c6eaa3f0SAntonio Quartulli 186f3163181SAntonio Quartulli /* deselect the current gateway so that next time that client mode is 187f3163181SAntonio Quartulli * enabled a proper GW_ADD event can be sent 188f3163181SAntonio Quartulli */ 189f3163181SAntonio Quartulli batadv_gw_select(bat_priv, NULL); 190f3163181SAntonio Quartulli 191c6eaa3f0SAntonio Quartulli /* if batman-adv is switching the gw client mode off and a gateway was 192c6eaa3f0SAntonio Quartulli * already selected, send a DEL uevent 193c6eaa3f0SAntonio Quartulli */ 194c6eaa3f0SAntonio Quartulli batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL); 195c6eaa3f0SAntonio Quartulli 1963a01743dSSven Eckelmann batadv_gw_node_put(curr_gw); 197c6eaa3f0SAntonio Quartulli } 198c6eaa3f0SAntonio Quartulli 19956303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv) 2002265c141SAntonio Quartulli { 2014f248cffSSven Eckelmann struct batadv_gw_node *curr_gw = NULL; 2024f248cffSSven Eckelmann struct batadv_gw_node *next_gw = NULL; 20356303d34SSven Eckelmann struct batadv_neigh_node *router = NULL; 20489652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo = NULL; 20519595e05SAntonio Quartulli char gw_addr[18] = { '\0' }; 2062265c141SAntonio Quartulli 2073a24a63eSAntonio Quartulli if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT) 2082265c141SAntonio Quartulli goto out; 2092265c141SAntonio Quartulli 210*34d99cfeSAntonio Quartulli if (!bat_priv->algo_ops->gw.get_best_gw_node) 211*34d99cfeSAntonio Quartulli goto out; 212*34d99cfeSAntonio Quartulli 2131409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 2142265c141SAntonio Quartulli 215807736f6SSven Eckelmann if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw) 216caa0bf64SMarek Lindner goto out; 217caa0bf64SMarek Lindner 218*34d99cfeSAntonio Quartulli /* if gw.reselect is set to 1 it means that a previous call to 219*34d99cfeSAntonio Quartulli * gw.is_eligible() said that we have a new best GW, therefore it can 220*34d99cfeSAntonio Quartulli * now be picked from the list and selected 221*34d99cfeSAntonio Quartulli */ 222*34d99cfeSAntonio Quartulli next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv); 2232265c141SAntonio Quartulli 2242265c141SAntonio Quartulli if (curr_gw == next_gw) 2252265c141SAntonio Quartulli goto out; 2262265c141SAntonio Quartulli 2272265c141SAntonio Quartulli if (next_gw) { 22819595e05SAntonio Quartulli sprintf(gw_addr, "%pM", next_gw->orig_node->orig); 22919595e05SAntonio Quartulli 2307351a482SSimon Wunderlich router = batadv_orig_router_get(next_gw->orig_node, 2317351a482SSimon Wunderlich BATADV_IF_DEFAULT); 2322265c141SAntonio Quartulli if (!router) { 2334e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 2342265c141SAntonio Quartulli goto out; 2352265c141SAntonio Quartulli } 23689652331SSimon Wunderlich 23789652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, 23889652331SSimon Wunderlich BATADV_IF_DEFAULT); 23989652331SSimon Wunderlich if (!router_ifinfo) { 24089652331SSimon Wunderlich batadv_gw_reselect(bat_priv); 24189652331SSimon Wunderlich goto out; 24289652331SSimon Wunderlich } 2432265c141SAntonio Quartulli } 2442265c141SAntonio Quartulli 2452265c141SAntonio Quartulli if ((curr_gw) && (!next_gw)) { 24639c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 2472265c141SAntonio Quartulli "Removing selected gateway - no gateway in range\n"); 24839c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, 24939c75a51SSven Eckelmann NULL); 2502265c141SAntonio Quartulli } else if ((!curr_gw) && (next_gw)) { 25139c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 252414254e3SMarek Lindner "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 2531eda58bfSSven Eckelmann next_gw->orig_node->orig, 254414254e3SMarek Lindner next_gw->bandwidth_down / 10, 255414254e3SMarek Lindner next_gw->bandwidth_down % 10, 256414254e3SMarek Lindner next_gw->bandwidth_up / 10, 25789652331SSimon Wunderlich next_gw->bandwidth_up % 10, 25889652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg); 25939c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD, 26039c75a51SSven Eckelmann gw_addr); 2612265c141SAntonio Quartulli } else { 26239c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 263414254e3SMarek Lindner "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 2641eda58bfSSven Eckelmann next_gw->orig_node->orig, 265414254e3SMarek Lindner next_gw->bandwidth_down / 10, 266414254e3SMarek Lindner next_gw->bandwidth_down % 10, 267414254e3SMarek Lindner next_gw->bandwidth_up / 10, 26889652331SSimon Wunderlich next_gw->bandwidth_up % 10, 26989652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg); 27039c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE, 27139c75a51SSven Eckelmann gw_addr); 272c6c8fea2SSven Eckelmann } 273c6c8fea2SSven Eckelmann 2741409a834SSven Eckelmann batadv_gw_select(bat_priv, next_gw); 2752265c141SAntonio Quartulli 276c4aac1abSMarek Lindner out: 277c4aac1abSMarek Lindner if (curr_gw) 2783a01743dSSven Eckelmann batadv_gw_node_put(curr_gw); 2792265c141SAntonio Quartulli if (next_gw) 2803a01743dSSven Eckelmann batadv_gw_node_put(next_gw); 2812265c141SAntonio Quartulli if (router) 28225bb2509SSven Eckelmann batadv_neigh_node_put(router); 28389652331SSimon Wunderlich if (router_ifinfo) 284044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(router_ifinfo); 285c6c8fea2SSven Eckelmann } 286c6c8fea2SSven Eckelmann 28756303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv, 28856303d34SSven Eckelmann struct batadv_orig_node *orig_node) 289c6c8fea2SSven Eckelmann { 29056303d34SSven Eckelmann struct batadv_orig_node *curr_gw_orig; 291*34d99cfeSAntonio Quartulli 292*34d99cfeSAntonio Quartulli /* abort immediately if the routing algorithm does not support gateway 293*34d99cfeSAntonio Quartulli * election 294*34d99cfeSAntonio Quartulli */ 295*34d99cfeSAntonio Quartulli if (!bat_priv->algo_ops->gw.is_eligible) 296*34d99cfeSAntonio Quartulli return; 297c6c8fea2SSven Eckelmann 2987cf06bc6SSven Eckelmann curr_gw_orig = batadv_gw_get_selected_orig(bat_priv); 29957f0c07cSLinus Lüssing if (!curr_gw_orig) 3004e820e72SAntonio Quartulli goto reselect; 301c6c8fea2SSven Eckelmann 302c6c8fea2SSven Eckelmann /* this node already is the gateway */ 30357f0c07cSLinus Lüssing if (curr_gw_orig == orig_node) 304e1a5382fSLinus Lüssing goto out; 305c6c8fea2SSven Eckelmann 306*34d99cfeSAntonio Quartulli if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig, 307*34d99cfeSAntonio Quartulli orig_node)) 308e1a5382fSLinus Lüssing goto out; 309c6c8fea2SSven Eckelmann 3104e820e72SAntonio Quartulli reselect: 3114e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 3125d02b3cdSLinus Lüssing out: 31357f0c07cSLinus Lüssing if (curr_gw_orig) 3145d967310SSven Eckelmann batadv_orig_node_put(curr_gw_orig); 315c6c8fea2SSven Eckelmann } 316c6c8fea2SSven Eckelmann 317414254e3SMarek Lindner /** 318414254e3SMarek Lindner * batadv_gw_node_add - add gateway node to list of available gateways 319414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 320414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 321414254e3SMarek Lindner * @gateway: announced bandwidth information 322414254e3SMarek Lindner */ 32356303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv, 32456303d34SSven Eckelmann struct batadv_orig_node *orig_node, 325414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 326c6c8fea2SSven Eckelmann { 32756303d34SSven Eckelmann struct batadv_gw_node *gw_node; 328414254e3SMarek Lindner 329414254e3SMarek Lindner if (gateway->bandwidth_down == 0) 330414254e3SMarek Lindner return; 331c6c8fea2SSven Eckelmann 332377fe0f9SAntonio Quartulli gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); 333c3ba37a7SSven Eckelmann if (!gw_node) 334377fe0f9SAntonio Quartulli return; 335377fe0f9SAntonio Quartulli 336c3ba37a7SSven Eckelmann kref_get(&orig_node->refcount); 337c6c8fea2SSven Eckelmann INIT_HLIST_NODE(&gw_node->list); 338c6c8fea2SSven Eckelmann gw_node->orig_node = orig_node; 33927a4d5efSSimon Wunderlich gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 34027a4d5efSSimon Wunderlich gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 341e7aed321SSven Eckelmann kref_init(&gw_node->refcount); 342c6c8fea2SSven Eckelmann 343807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 344807736f6SSven Eckelmann hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list); 345807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 346c6c8fea2SSven Eckelmann 34739c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 348414254e3SMarek Lindner "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", 349414254e3SMarek Lindner orig_node->orig, 350414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 351414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 352414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 353414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 354c6c8fea2SSven Eckelmann } 355c6c8fea2SSven Eckelmann 356414254e3SMarek Lindner /** 357414254e3SMarek Lindner * batadv_gw_node_get - retrieve gateway node from list of available gateways 358414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 359414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 360414254e3SMarek Lindner * 36162fe710fSSven Eckelmann * Return: gateway node if found or NULL otherwise. 36271e4aa9cSAntonio Quartulli */ 363414254e3SMarek Lindner static struct batadv_gw_node * 364414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv, 365414254e3SMarek Lindner struct batadv_orig_node *orig_node) 366414254e3SMarek Lindner { 367414254e3SMarek Lindner struct batadv_gw_node *gw_node_tmp, *gw_node = NULL; 368c6c8fea2SSven Eckelmann 369c6c8fea2SSven Eckelmann rcu_read_lock(); 370414254e3SMarek Lindner hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) { 371414254e3SMarek Lindner if (gw_node_tmp->orig_node != orig_node) 372c6c8fea2SSven Eckelmann continue; 373c6c8fea2SSven Eckelmann 374e7aed321SSven Eckelmann if (!kref_get_unless_zero(&gw_node_tmp->refcount)) 375414254e3SMarek Lindner continue; 376414254e3SMarek Lindner 377414254e3SMarek Lindner gw_node = gw_node_tmp; 378414254e3SMarek Lindner break; 379414254e3SMarek Lindner } 380414254e3SMarek Lindner rcu_read_unlock(); 381414254e3SMarek Lindner 382414254e3SMarek Lindner return gw_node; 383414254e3SMarek Lindner } 384414254e3SMarek Lindner 385414254e3SMarek Lindner /** 386414254e3SMarek Lindner * batadv_gw_node_update - update list of available gateways with changed 387414254e3SMarek Lindner * bandwidth information 388414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 389414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 390414254e3SMarek Lindner * @gateway: announced bandwidth information 391414254e3SMarek Lindner */ 392414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv, 393414254e3SMarek Lindner struct batadv_orig_node *orig_node, 394414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 395414254e3SMarek Lindner { 396414254e3SMarek Lindner struct batadv_gw_node *gw_node, *curr_gw = NULL; 397414254e3SMarek Lindner 398414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_node); 399414254e3SMarek Lindner if (!gw_node) { 400414254e3SMarek Lindner batadv_gw_node_add(bat_priv, orig_node, gateway); 401414254e3SMarek Lindner goto out; 402414254e3SMarek Lindner } 403414254e3SMarek Lindner 404414254e3SMarek Lindner if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) && 405414254e3SMarek Lindner (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))) 406414254e3SMarek Lindner goto out; 407414254e3SMarek Lindner 40839c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 409414254e3SMarek Lindner "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n", 410414254e3SMarek Lindner orig_node->orig, 411414254e3SMarek Lindner gw_node->bandwidth_down / 10, 412414254e3SMarek Lindner gw_node->bandwidth_down % 10, 413414254e3SMarek Lindner gw_node->bandwidth_up / 10, 414414254e3SMarek Lindner gw_node->bandwidth_up % 10, 415414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 416414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 417414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 418414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 419414254e3SMarek Lindner 420414254e3SMarek Lindner gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 421414254e3SMarek Lindner gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 422c6c8fea2SSven Eckelmann 423414254e3SMarek Lindner if (ntohl(gateway->bandwidth_down) == 0) { 42439c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 425c6c8fea2SSven Eckelmann "Gateway %pM removed from gateway list\n", 426c6c8fea2SSven Eckelmann orig_node->orig); 427c6c8fea2SSven Eckelmann 428414254e3SMarek Lindner /* Note: We don't need a NULL check here, since curr_gw never 429414254e3SMarek Lindner * gets dereferenced. 430414254e3SMarek Lindner */ 431bd3524c1SSimon Wunderlich spin_lock_bh(&bat_priv->gw.list_lock); 432c18bdd01SSven Eckelmann if (!hlist_unhashed(&gw_node->list)) { 433bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list); 4343a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 435c18bdd01SSven Eckelmann } 436c18bdd01SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 437bd3524c1SSimon Wunderlich 438414254e3SMarek Lindner curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 439c4aac1abSMarek Lindner if (gw_node == curr_gw) 4404e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 441bd3524c1SSimon Wunderlich 442bd3524c1SSimon Wunderlich if (curr_gw) 4433a01743dSSven Eckelmann batadv_gw_node_put(curr_gw); 444414254e3SMarek Lindner } 44571e4aa9cSAntonio Quartulli 446414254e3SMarek Lindner out: 447414254e3SMarek Lindner if (gw_node) 4483a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 449c6c8fea2SSven Eckelmann } 450c6c8fea2SSven Eckelmann 45156303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv, 45256303d34SSven Eckelmann struct batadv_orig_node *orig_node) 453c6c8fea2SSven Eckelmann { 454414254e3SMarek Lindner struct batadv_tvlv_gateway_data gateway; 455414254e3SMarek Lindner 456414254e3SMarek Lindner gateway.bandwidth_down = 0; 457414254e3SMarek Lindner gateway.bandwidth_up = 0; 458414254e3SMarek Lindner 459414254e3SMarek Lindner batadv_gw_node_update(bat_priv, orig_node, &gateway); 460c6c8fea2SSven Eckelmann } 461c6c8fea2SSven Eckelmann 462bd3524c1SSimon Wunderlich void batadv_gw_node_free(struct batadv_priv *bat_priv) 463c6c8fea2SSven Eckelmann { 464bd3524c1SSimon Wunderlich struct batadv_gw_node *gw_node; 465b67bfe0dSSasha Levin struct hlist_node *node_tmp; 466c6c8fea2SSven Eckelmann 467807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 468b67bfe0dSSasha Levin hlist_for_each_entry_safe(gw_node, node_tmp, 469807736f6SSven Eckelmann &bat_priv->gw.list, list) { 470bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list); 4713a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 472c6c8fea2SSven Eckelmann } 473807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 474c6c8fea2SSven Eckelmann } 475c6c8fea2SSven Eckelmann 4767cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset) 477c6c8fea2SSven Eckelmann { 478c6c8fea2SSven Eckelmann struct net_device *net_dev = (struct net_device *)seq->private; 47956303d34SSven Eckelmann struct batadv_priv *bat_priv = netdev_priv(net_dev); 48056303d34SSven Eckelmann struct batadv_hard_iface *primary_if; 481c6c8fea2SSven Eckelmann 48230da63a6SMarek Lindner primary_if = batadv_seq_print_text_primary_if_get(seq); 48330da63a6SMarek Lindner if (!primary_if) 484*34d99cfeSAntonio Quartulli return 0; 485c6c8fea2SSven Eckelmann 486*34d99cfeSAntonio Quartulli seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n", 48742d0b044SSven Eckelmann BATADV_SOURCE_VERSION, primary_if->net_dev->name, 488*34d99cfeSAntonio Quartulli primary_if->net_dev->dev_addr, net_dev->name, 489*34d99cfeSAntonio Quartulli bat_priv->algo_ops->name); 490c6c8fea2SSven Eckelmann 49182047ad7SSven Eckelmann batadv_hardif_put(primary_if); 492*34d99cfeSAntonio Quartulli 493*34d99cfeSAntonio Quartulli if (!bat_priv->algo_ops->gw.print) { 494*34d99cfeSAntonio Quartulli seq_puts(seq, 495*34d99cfeSAntonio Quartulli "No printing function for this routing protocol\n"); 496*34d99cfeSAntonio Quartulli return 0; 497*34d99cfeSAntonio Quartulli } 498*34d99cfeSAntonio Quartulli 499*34d99cfeSAntonio Quartulli bat_priv->algo_ops->gw.print(bat_priv, seq); 500*34d99cfeSAntonio Quartulli 50130da63a6SMarek Lindner return 0; 502c6c8fea2SSven Eckelmann } 503c6c8fea2SSven Eckelmann 5046c413b1cSAntonio Quartulli /** 5056c413b1cSAntonio Quartulli * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message 5066c413b1cSAntonio Quartulli * @skb: the packet to check 5076c413b1cSAntonio Quartulli * @header_len: a pointer to the batman-adv header size 5086c413b1cSAntonio Quartulli * @chaddr: buffer where the client address will be stored. Valid 5096c413b1cSAntonio Quartulli * only if the function returns BATADV_DHCP_TO_CLIENT 5106c413b1cSAntonio Quartulli * 51162fe710fSSven Eckelmann * This function may re-allocate the data buffer of the skb passed as argument. 51262fe710fSSven Eckelmann * 51362fe710fSSven Eckelmann * Return: 5146c413b1cSAntonio Quartulli * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error 5156c413b1cSAntonio Quartulli * while parsing it 5166c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server 5176c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client 5189cfc7bd6SSven Eckelmann */ 5196c413b1cSAntonio Quartulli enum batadv_dhcp_recipient 5206c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len, 5216b5e971aSSven Eckelmann u8 *chaddr) 522c6c8fea2SSven Eckelmann { 5236c413b1cSAntonio Quartulli enum batadv_dhcp_recipient ret = BATADV_DHCP_NO; 524c6c8fea2SSven Eckelmann struct ethhdr *ethhdr; 525c6c8fea2SSven Eckelmann struct iphdr *iphdr; 526c6c8fea2SSven Eckelmann struct ipv6hdr *ipv6hdr; 527c6c8fea2SSven Eckelmann struct udphdr *udphdr; 528f7f8ed56SAntonio Quartulli struct vlan_ethhdr *vhdr; 5296c413b1cSAntonio Quartulli int chaddr_offset; 530f7f8ed56SAntonio Quartulli __be16 proto; 5316b5e971aSSven Eckelmann u8 *p; 532c6c8fea2SSven Eckelmann 533c6c8fea2SSven Eckelmann /* check for ethernet header */ 534be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) 5356c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 5366c413b1cSAntonio Quartulli 537927c2ed7SLinus Lüssing ethhdr = eth_hdr(skb); 538f7f8ed56SAntonio Quartulli proto = ethhdr->h_proto; 539be7af5cfSMarek Lindner *header_len += ETH_HLEN; 540c6c8fea2SSven Eckelmann 541c6c8fea2SSven Eckelmann /* check for initial vlan header */ 542f7f8ed56SAntonio Quartulli if (proto == htons(ETH_P_8021Q)) { 543be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) 5446c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 545f7f8ed56SAntonio Quartulli 546927c2ed7SLinus Lüssing vhdr = vlan_eth_hdr(skb); 547f7f8ed56SAntonio Quartulli proto = vhdr->h_vlan_encapsulated_proto; 548be7af5cfSMarek Lindner *header_len += VLAN_HLEN; 549c6c8fea2SSven Eckelmann } 550c6c8fea2SSven Eckelmann 551c6c8fea2SSven Eckelmann /* check for ip header */ 552f7f8ed56SAntonio Quartulli switch (proto) { 553f7f8ed56SAntonio Quartulli case htons(ETH_P_IP): 554be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) 5556c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 5566c413b1cSAntonio Quartulli 557be7af5cfSMarek Lindner iphdr = (struct iphdr *)(skb->data + *header_len); 558be7af5cfSMarek Lindner *header_len += iphdr->ihl * 4; 559c6c8fea2SSven Eckelmann 560c6c8fea2SSven Eckelmann /* check for udp header */ 561c6c8fea2SSven Eckelmann if (iphdr->protocol != IPPROTO_UDP) 5626c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 563c6c8fea2SSven Eckelmann 564c6c8fea2SSven Eckelmann break; 565f7f8ed56SAntonio Quartulli case htons(ETH_P_IPV6): 566be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) 5676c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 5686c413b1cSAntonio Quartulli 569be7af5cfSMarek Lindner ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); 570be7af5cfSMarek Lindner *header_len += sizeof(*ipv6hdr); 571c6c8fea2SSven Eckelmann 572c6c8fea2SSven Eckelmann /* check for udp header */ 573c6c8fea2SSven Eckelmann if (ipv6hdr->nexthdr != IPPROTO_UDP) 5746c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 575c6c8fea2SSven Eckelmann 576c6c8fea2SSven Eckelmann break; 577c6c8fea2SSven Eckelmann default: 5786c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 579c6c8fea2SSven Eckelmann } 580c6c8fea2SSven Eckelmann 581be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr))) 5826c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 5839d2c9488SLinus Lüssing 584be7af5cfSMarek Lindner udphdr = (struct udphdr *)(skb->data + *header_len); 585be7af5cfSMarek Lindner *header_len += sizeof(*udphdr); 586c6c8fea2SSven Eckelmann 587c6c8fea2SSven Eckelmann /* check for bootp port */ 5886c413b1cSAntonio Quartulli switch (proto) { 5896c413b1cSAntonio Quartulli case htons(ETH_P_IP): 5906c413b1cSAntonio Quartulli if (udphdr->dest == htons(67)) 5916c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 5926c413b1cSAntonio Quartulli else if (udphdr->source == htons(67)) 5936c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 5946c413b1cSAntonio Quartulli break; 5956c413b1cSAntonio Quartulli case htons(ETH_P_IPV6): 5966c413b1cSAntonio Quartulli if (udphdr->dest == htons(547)) 5976c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 5986c413b1cSAntonio Quartulli else if (udphdr->source == htons(547)) 5996c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 6006c413b1cSAntonio Quartulli break; 601be7af5cfSMarek Lindner } 602c6c8fea2SSven Eckelmann 6036c413b1cSAntonio Quartulli chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET; 6046c413b1cSAntonio Quartulli /* store the client address if the message is going to a client */ 6056c413b1cSAntonio Quartulli if (ret == BATADV_DHCP_TO_CLIENT && 6066c413b1cSAntonio Quartulli pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) { 6076c413b1cSAntonio Quartulli /* check if the DHCP packet carries an Ethernet DHCP */ 6086c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET; 6096c413b1cSAntonio Quartulli if (*p != BATADV_DHCP_HTYPE_ETHERNET) 6106c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 6116c413b1cSAntonio Quartulli 6126c413b1cSAntonio Quartulli /* check if the DHCP packet carries a valid Ethernet address */ 6136c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET; 6146c413b1cSAntonio Quartulli if (*p != ETH_ALEN) 6156c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 6166c413b1cSAntonio Quartulli 6178fdd0153SAntonio Quartulli ether_addr_copy(chaddr, skb->data + chaddr_offset); 6186c413b1cSAntonio Quartulli } 6196c413b1cSAntonio Quartulli 6206c413b1cSAntonio Quartulli return ret; 6216c413b1cSAntonio Quartulli } 622aa143d28SAntonio Quartulli 623bbb877edSAntonio Quartulli /** 624bbb877edSAntonio Quartulli * batadv_gw_out_of_range - check if the dhcp request destination is the best gw 625bbb877edSAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 626bbb877edSAntonio Quartulli * @skb: the outgoing packet 627bbb877edSAntonio Quartulli * 628bbb877edSAntonio Quartulli * Check if the skb is a DHCP request and if it is sent to the current best GW 629bbb877edSAntonio Quartulli * server. Due to topology changes it may be the case that the GW server 630bbb877edSAntonio Quartulli * previously selected is not the best one anymore. 631bbb877edSAntonio Quartulli * 632bbb877edSAntonio Quartulli * This call might reallocate skb data. 6336c413b1cSAntonio Quartulli * Must be invoked only when the DHCP packet is going TO a DHCP SERVER. 63462fe710fSSven Eckelmann * 63562fe710fSSven Eckelmann * Return: true if the packet destination is unicast and it is not the best gw, 63662fe710fSSven Eckelmann * false otherwise. 637bbb877edSAntonio Quartulli */ 63856303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, 6399d2c9488SLinus Lüssing struct sk_buff *skb) 640be7af5cfSMarek Lindner { 6414f248cffSSven Eckelmann struct batadv_neigh_node *neigh_curr = NULL; 6424f248cffSSven Eckelmann struct batadv_neigh_node *neigh_old = NULL; 64356303d34SSven Eckelmann struct batadv_orig_node *orig_dst_node = NULL; 6444f248cffSSven Eckelmann struct batadv_gw_node *gw_node = NULL; 6454f248cffSSven Eckelmann struct batadv_gw_node *curr_gw = NULL; 64689652331SSimon Wunderlich struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo; 6476c413b1cSAntonio Quartulli struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 6486c413b1cSAntonio Quartulli bool out_of_range = false; 6496b5e971aSSven Eckelmann u8 curr_tq_avg; 650bbb877edSAntonio Quartulli unsigned short vid; 651bbb877edSAntonio Quartulli 652bbb877edSAntonio Quartulli vid = batadv_get_vid(skb, 0); 653be7af5cfSMarek Lindner 65408c36d3eSSven Eckelmann orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, 655bbb877edSAntonio Quartulli ethhdr->h_dest, vid); 656be7af5cfSMarek Lindner if (!orig_dst_node) 657be7af5cfSMarek Lindner goto out; 658be7af5cfSMarek Lindner 659414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_dst_node); 6600d164491SAntonio Quartulli if (!gw_node) 661be7af5cfSMarek Lindner goto out; 662be7af5cfSMarek Lindner 6633a24a63eSAntonio Quartulli switch (atomic_read(&bat_priv->gw.mode)) { 664cd646ab1SSven Eckelmann case BATADV_GW_MODE_SERVER: 665be7af5cfSMarek Lindner /* If we are a GW then we are our best GW. We can artificially 6669cfc7bd6SSven Eckelmann * set the tq towards ourself as the maximum value 6679cfc7bd6SSven Eckelmann */ 66842d0b044SSven Eckelmann curr_tq_avg = BATADV_TQ_MAX_VALUE; 669be7af5cfSMarek Lindner break; 670cd646ab1SSven Eckelmann case BATADV_GW_MODE_CLIENT: 6711409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 672c4aac1abSMarek Lindner if (!curr_gw) 673be7af5cfSMarek Lindner goto out; 674c6c8fea2SSven Eckelmann 675be7af5cfSMarek Lindner /* packet is going to our gateway */ 676be7af5cfSMarek Lindner if (curr_gw->orig_node == orig_dst_node) 677be7af5cfSMarek Lindner goto out; 678be7af5cfSMarek Lindner 67943676ab5SAntonio Quartulli /* If the dhcp packet has been sent to a different gw, 68043676ab5SAntonio Quartulli * we have to evaluate whether the old gw is still 6819cfc7bd6SSven Eckelmann * reliable enough 6829cfc7bd6SSven Eckelmann */ 68330d3c511SSven Eckelmann neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node, 68430d3c511SSven Eckelmann NULL); 685be7af5cfSMarek Lindner if (!neigh_curr) 686be7af5cfSMarek Lindner goto out; 687be7af5cfSMarek Lindner 68889652331SSimon Wunderlich curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr, 68989652331SSimon Wunderlich BATADV_IF_DEFAULT); 69089652331SSimon Wunderlich if (!curr_ifinfo) 69189652331SSimon Wunderlich goto out; 69289652331SSimon Wunderlich 69389652331SSimon Wunderlich curr_tq_avg = curr_ifinfo->bat_iv.tq_avg; 694044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(curr_ifinfo); 69589652331SSimon Wunderlich 696be7af5cfSMarek Lindner break; 697cd646ab1SSven Eckelmann case BATADV_GW_MODE_OFF: 698be7af5cfSMarek Lindner default: 699be7af5cfSMarek Lindner goto out; 70043676ab5SAntonio Quartulli } 701be7af5cfSMarek Lindner 70230d3c511SSven Eckelmann neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL); 7032ef04f47SDan Carpenter if (!neigh_old) 704be7af5cfSMarek Lindner goto out; 705be7af5cfSMarek Lindner 70689652331SSimon Wunderlich old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT); 70789652331SSimon Wunderlich if (!old_ifinfo) 70889652331SSimon Wunderlich goto out; 70989652331SSimon Wunderlich 71089652331SSimon Wunderlich if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD) 711be7af5cfSMarek Lindner out_of_range = true; 712044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(old_ifinfo); 713be7af5cfSMarek Lindner 714be7af5cfSMarek Lindner out: 715be7af5cfSMarek Lindner if (orig_dst_node) 7165d967310SSven Eckelmann batadv_orig_node_put(orig_dst_node); 717be7af5cfSMarek Lindner if (curr_gw) 7183a01743dSSven Eckelmann batadv_gw_node_put(curr_gw); 719414254e3SMarek Lindner if (gw_node) 7203a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 72143676ab5SAntonio Quartulli if (neigh_old) 72225bb2509SSven Eckelmann batadv_neigh_node_put(neigh_old); 72343676ab5SAntonio Quartulli if (neigh_curr) 72425bb2509SSven Eckelmann batadv_neigh_node_put(neigh_curr); 725be7af5cfSMarek Lindner return out_of_range; 726c6c8fea2SSven Eckelmann } 727