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" 4557f0c07cSLinus Lüssing #include "originator.h" 461e2c2a4fSSven Eckelmann #include "packet.h" 4743676ab5SAntonio Quartulli #include "routing.h" 481e2c2a4fSSven Eckelmann #include "sysfs.h" 491e2c2a4fSSven Eckelmann #include "translation-table.h" 50c6c8fea2SSven Eckelmann 516c413b1cSAntonio Quartulli /* These are the offsets of the "hw type" and "hw address length" in the dhcp 526c413b1cSAntonio Quartulli * packet starting at the beginning of the dhcp header 539cfc7bd6SSven Eckelmann */ 546c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_OFFSET 1 556c413b1cSAntonio Quartulli #define BATADV_DHCP_HLEN_OFFSET 2 566c413b1cSAntonio Quartulli /* Value of htype representing Ethernet */ 576c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_ETHERNET 0x01 586c413b1cSAntonio Quartulli /* This is the offset of the "chaddr" field in the dhcp packet starting at the 596c413b1cSAntonio Quartulli * beginning of the dhcp header 606c413b1cSAntonio Quartulli */ 616c413b1cSAntonio Quartulli #define BATADV_DHCP_CHADDR_OFFSET 28 6243676ab5SAntonio Quartulli 63e7aed321SSven Eckelmann /** 64e7aed321SSven Eckelmann * batadv_gw_node_release - release gw_node from lists and queue for free after 65e7aed321SSven Eckelmann * rcu grace period 66e7aed321SSven Eckelmann * @ref: kref pointer of the gw_node 67e7aed321SSven Eckelmann */ 68e7aed321SSven Eckelmann static void batadv_gw_node_release(struct kref *ref) 6925b6d3c1SMarek Lindner { 70e7aed321SSven Eckelmann struct batadv_gw_node *gw_node; 71e7aed321SSven Eckelmann 72e7aed321SSven Eckelmann gw_node = container_of(ref, struct batadv_gw_node, refcount); 73e7aed321SSven Eckelmann 745d967310SSven Eckelmann batadv_orig_node_put(gw_node->orig_node); 75eb340b2fSPaul E. McKenney kfree_rcu(gw_node, rcu); 76c6c8fea2SSven Eckelmann } 77e7aed321SSven Eckelmann 78e7aed321SSven Eckelmann /** 79e7aed321SSven Eckelmann * batadv_gw_node_free_ref - decrement the gw_node refcounter and possibly 80e7aed321SSven Eckelmann * release it 81e7aed321SSven Eckelmann * @gw_node: gateway node to free 82e7aed321SSven Eckelmann */ 83e7aed321SSven Eckelmann static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node) 84e7aed321SSven Eckelmann { 85e7aed321SSven Eckelmann kref_put(&gw_node->refcount, batadv_gw_node_release); 86377fe0f9SAntonio Quartulli } 87c6c8fea2SSven Eckelmann 8856303d34SSven Eckelmann static 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) 1281409a834SSven Eckelmann batadv_gw_node_free_ref(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 139e7aed321SSven Eckelmann if (new_gw_node && !kref_get_unless_zero(&new_gw_node->refcount)) 14025b6d3c1SMarek Lindner new_gw_node = NULL; 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) 1461409a834SSven Eckelmann batadv_gw_node_free_ref(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 16756303d34SSven Eckelmann static struct batadv_gw_node * 16856303d34SSven Eckelmann batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv) 169c6c8fea2SSven Eckelmann { 17056303d34SSven Eckelmann struct batadv_neigh_node *router; 17189652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo; 17256303d34SSven Eckelmann struct batadv_gw_node *gw_node, *curr_gw = NULL; 1734f248cffSSven Eckelmann u64 max_gw_factor = 0; 1744f248cffSSven Eckelmann u64 tmp_gw_factor = 0; 1756b5e971aSSven Eckelmann u8 max_tq = 0; 1766b5e971aSSven Eckelmann u8 tq_avg; 17756303d34SSven Eckelmann struct batadv_orig_node *orig_node; 178c6c8fea2SSven Eckelmann 179c6c8fea2SSven Eckelmann rcu_read_lock(); 180b67bfe0dSSasha Levin hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 18184d5e5e0SSven Eckelmann orig_node = gw_node->orig_node; 1827351a482SSimon Wunderlich router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT); 183e1a5382fSLinus Lüssing if (!router) 184c6c8fea2SSven Eckelmann continue; 185c6c8fea2SSven Eckelmann 18689652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, 18789652331SSimon Wunderlich BATADV_IF_DEFAULT); 18889652331SSimon Wunderlich if (!router_ifinfo) 18989652331SSimon Wunderlich goto next; 19089652331SSimon Wunderlich 191e7aed321SSven Eckelmann if (!kref_get_unless_zero(&gw_node->refcount)) 1922265c141SAntonio Quartulli goto next; 1932265c141SAntonio Quartulli 19489652331SSimon Wunderlich tq_avg = router_ifinfo->bat_iv.tq_avg; 195c67893d1SSven Eckelmann 196c6c8fea2SSven Eckelmann switch (atomic_read(&bat_priv->gw_sel_class)) { 197c6c8fea2SSven Eckelmann case 1: /* fast connection */ 198414254e3SMarek Lindner tmp_gw_factor = tq_avg * tq_avg; 199414254e3SMarek Lindner tmp_gw_factor *= gw_node->bandwidth_down; 200414254e3SMarek Lindner tmp_gw_factor *= 100 * 100; 201e071d93eSSven Eckelmann tmp_gw_factor >>= 18; 202c6c8fea2SSven Eckelmann 203c6c8fea2SSven Eckelmann if ((tmp_gw_factor > max_gw_factor) || 204c6c8fea2SSven Eckelmann ((tmp_gw_factor == max_gw_factor) && 205c67893d1SSven Eckelmann (tq_avg > max_tq))) { 2062265c141SAntonio Quartulli if (curr_gw) 2071409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 2082265c141SAntonio Quartulli curr_gw = gw_node; 209e7aed321SSven Eckelmann kref_get(&curr_gw->refcount); 2102265c141SAntonio Quartulli } 211c6c8fea2SSven Eckelmann break; 212c6c8fea2SSven Eckelmann 2139cfc7bd6SSven Eckelmann default: /* 2: stable connection (use best statistic) 214c6c8fea2SSven Eckelmann * 3: fast-switch (use best statistic but change as 215c6c8fea2SSven Eckelmann * soon as a better gateway appears) 216c6c8fea2SSven Eckelmann * XX: late-switch (use best statistic but change as 217c6c8fea2SSven Eckelmann * soon as a better gateway appears which has 218c6c8fea2SSven Eckelmann * $routing_class more tq points) 2199cfc7bd6SSven Eckelmann */ 220c67893d1SSven Eckelmann if (tq_avg > max_tq) { 2212265c141SAntonio Quartulli if (curr_gw) 2221409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 2232265c141SAntonio Quartulli curr_gw = gw_node; 224e7aed321SSven Eckelmann kref_get(&curr_gw->refcount); 2252265c141SAntonio Quartulli } 226c6c8fea2SSven Eckelmann break; 227c6c8fea2SSven Eckelmann } 228c6c8fea2SSven Eckelmann 229c67893d1SSven Eckelmann if (tq_avg > max_tq) 230c67893d1SSven Eckelmann max_tq = tq_avg; 231c6c8fea2SSven Eckelmann 232c6c8fea2SSven Eckelmann if (tmp_gw_factor > max_gw_factor) 233c6c8fea2SSven Eckelmann max_gw_factor = tmp_gw_factor; 234e1a5382fSLinus Lüssing 2351409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 2362265c141SAntonio Quartulli 2372265c141SAntonio Quartulli next: 238*25bb2509SSven Eckelmann batadv_neigh_node_put(router); 23989652331SSimon Wunderlich if (router_ifinfo) 24089652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_ifinfo); 241c6c8fea2SSven Eckelmann } 2422265c141SAntonio Quartulli rcu_read_unlock(); 243c6c8fea2SSven Eckelmann 2442265c141SAntonio Quartulli return curr_gw; 2452265c141SAntonio Quartulli } 246e1a5382fSLinus Lüssing 247c6eaa3f0SAntonio Quartulli /** 248c6eaa3f0SAntonio Quartulli * batadv_gw_check_client_stop - check if client mode has been switched off 249c6eaa3f0SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 250c6eaa3f0SAntonio Quartulli * 251c6eaa3f0SAntonio Quartulli * This function assumes the caller has checked that the gw state *is actually 252c6eaa3f0SAntonio Quartulli * changing*. This function is not supposed to be called when there is no state 253c6eaa3f0SAntonio Quartulli * change. 254c6eaa3f0SAntonio Quartulli */ 255c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv) 256c6eaa3f0SAntonio Quartulli { 257c6eaa3f0SAntonio Quartulli struct batadv_gw_node *curr_gw; 258c6eaa3f0SAntonio Quartulli 259c6eaa3f0SAntonio Quartulli if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT) 260c6eaa3f0SAntonio Quartulli return; 261c6eaa3f0SAntonio Quartulli 262c6eaa3f0SAntonio Quartulli curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 263c6eaa3f0SAntonio Quartulli if (!curr_gw) 264c6eaa3f0SAntonio Quartulli return; 265c6eaa3f0SAntonio Quartulli 266f3163181SAntonio Quartulli /* deselect the current gateway so that next time that client mode is 267f3163181SAntonio Quartulli * enabled a proper GW_ADD event can be sent 268f3163181SAntonio Quartulli */ 269f3163181SAntonio Quartulli batadv_gw_select(bat_priv, NULL); 270f3163181SAntonio Quartulli 271c6eaa3f0SAntonio Quartulli /* if batman-adv is switching the gw client mode off and a gateway was 272c6eaa3f0SAntonio Quartulli * already selected, send a DEL uevent 273c6eaa3f0SAntonio Quartulli */ 274c6eaa3f0SAntonio Quartulli batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL); 275c6eaa3f0SAntonio Quartulli 276c6eaa3f0SAntonio Quartulli batadv_gw_node_free_ref(curr_gw); 277c6eaa3f0SAntonio Quartulli } 278c6eaa3f0SAntonio Quartulli 27956303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv) 2802265c141SAntonio Quartulli { 2814f248cffSSven Eckelmann struct batadv_gw_node *curr_gw = NULL; 2824f248cffSSven Eckelmann struct batadv_gw_node *next_gw = NULL; 28356303d34SSven Eckelmann struct batadv_neigh_node *router = NULL; 28489652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo = NULL; 28519595e05SAntonio Quartulli char gw_addr[18] = { '\0' }; 2862265c141SAntonio Quartulli 287cd646ab1SSven Eckelmann if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT) 2882265c141SAntonio Quartulli goto out; 2892265c141SAntonio Quartulli 2901409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 2912265c141SAntonio Quartulli 292807736f6SSven Eckelmann if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw) 293caa0bf64SMarek Lindner goto out; 294caa0bf64SMarek Lindner 2951409a834SSven Eckelmann next_gw = batadv_gw_get_best_gw_node(bat_priv); 2962265c141SAntonio Quartulli 2972265c141SAntonio Quartulli if (curr_gw == next_gw) 2982265c141SAntonio Quartulli goto out; 2992265c141SAntonio Quartulli 3002265c141SAntonio Quartulli if (next_gw) { 30119595e05SAntonio Quartulli sprintf(gw_addr, "%pM", next_gw->orig_node->orig); 30219595e05SAntonio Quartulli 3037351a482SSimon Wunderlich router = batadv_orig_router_get(next_gw->orig_node, 3047351a482SSimon Wunderlich BATADV_IF_DEFAULT); 3052265c141SAntonio Quartulli if (!router) { 3064e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 3072265c141SAntonio Quartulli goto out; 3082265c141SAntonio Quartulli } 30989652331SSimon Wunderlich 31089652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, 31189652331SSimon Wunderlich BATADV_IF_DEFAULT); 31289652331SSimon Wunderlich if (!router_ifinfo) { 31389652331SSimon Wunderlich batadv_gw_reselect(bat_priv); 31489652331SSimon Wunderlich goto out; 31589652331SSimon Wunderlich } 3162265c141SAntonio Quartulli } 3172265c141SAntonio Quartulli 3182265c141SAntonio Quartulli if ((curr_gw) && (!next_gw)) { 31939c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 3202265c141SAntonio Quartulli "Removing selected gateway - no gateway in range\n"); 32139c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, 32239c75a51SSven Eckelmann NULL); 3232265c141SAntonio Quartulli } else if ((!curr_gw) && (next_gw)) { 32439c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 325414254e3SMarek Lindner "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 3261eda58bfSSven Eckelmann next_gw->orig_node->orig, 327414254e3SMarek Lindner next_gw->bandwidth_down / 10, 328414254e3SMarek Lindner next_gw->bandwidth_down % 10, 329414254e3SMarek Lindner next_gw->bandwidth_up / 10, 33089652331SSimon Wunderlich next_gw->bandwidth_up % 10, 33189652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg); 33239c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD, 33339c75a51SSven Eckelmann gw_addr); 3342265c141SAntonio Quartulli } else { 33539c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 336414254e3SMarek Lindner "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 3371eda58bfSSven Eckelmann next_gw->orig_node->orig, 338414254e3SMarek Lindner next_gw->bandwidth_down / 10, 339414254e3SMarek Lindner next_gw->bandwidth_down % 10, 340414254e3SMarek Lindner next_gw->bandwidth_up / 10, 34189652331SSimon Wunderlich next_gw->bandwidth_up % 10, 34289652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg); 34339c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE, 34439c75a51SSven Eckelmann gw_addr); 345c6c8fea2SSven Eckelmann } 346c6c8fea2SSven Eckelmann 3471409a834SSven Eckelmann batadv_gw_select(bat_priv, next_gw); 3482265c141SAntonio Quartulli 349c4aac1abSMarek Lindner out: 350c4aac1abSMarek Lindner if (curr_gw) 3511409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 3522265c141SAntonio Quartulli if (next_gw) 3531409a834SSven Eckelmann batadv_gw_node_free_ref(next_gw); 3542265c141SAntonio Quartulli if (router) 355*25bb2509SSven Eckelmann batadv_neigh_node_put(router); 35689652331SSimon Wunderlich if (router_ifinfo) 35789652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_ifinfo); 358c6c8fea2SSven Eckelmann } 359c6c8fea2SSven Eckelmann 36056303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv, 36156303d34SSven Eckelmann struct batadv_orig_node *orig_node) 362c6c8fea2SSven Eckelmann { 36389652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_orig_tq = NULL; 36489652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_gw_tq = NULL; 36556303d34SSven Eckelmann struct batadv_orig_node *curr_gw_orig; 3664f248cffSSven Eckelmann struct batadv_neigh_node *router_gw = NULL; 3674f248cffSSven Eckelmann struct batadv_neigh_node *router_orig = NULL; 3686b5e971aSSven Eckelmann u8 gw_tq_avg, orig_tq_avg; 369c6c8fea2SSven Eckelmann 3707cf06bc6SSven Eckelmann curr_gw_orig = batadv_gw_get_selected_orig(bat_priv); 37157f0c07cSLinus Lüssing if (!curr_gw_orig) 3724e820e72SAntonio Quartulli goto reselect; 373c6c8fea2SSven Eckelmann 3747351a482SSimon Wunderlich router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT); 375e1a5382fSLinus Lüssing if (!router_gw) 3764e820e72SAntonio Quartulli goto reselect; 377c6c8fea2SSven Eckelmann 37889652331SSimon Wunderlich router_gw_tq = batadv_neigh_ifinfo_get(router_gw, 37989652331SSimon Wunderlich BATADV_IF_DEFAULT); 38089652331SSimon Wunderlich if (!router_gw_tq) 38189652331SSimon Wunderlich goto reselect; 38289652331SSimon Wunderlich 383c6c8fea2SSven Eckelmann /* this node already is the gateway */ 38457f0c07cSLinus Lüssing if (curr_gw_orig == orig_node) 385e1a5382fSLinus Lüssing goto out; 386c6c8fea2SSven Eckelmann 3877351a482SSimon Wunderlich router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT); 388e1a5382fSLinus Lüssing if (!router_orig) 389e1a5382fSLinus Lüssing goto out; 390c6c8fea2SSven Eckelmann 39189652331SSimon Wunderlich router_orig_tq = batadv_neigh_ifinfo_get(router_orig, 39289652331SSimon Wunderlich BATADV_IF_DEFAULT); 39389652331SSimon Wunderlich if (!router_orig_tq) 39489652331SSimon Wunderlich goto out; 39589652331SSimon Wunderlich 39689652331SSimon Wunderlich gw_tq_avg = router_gw_tq->bat_iv.tq_avg; 39789652331SSimon Wunderlich orig_tq_avg = router_orig_tq->bat_iv.tq_avg; 398c6c8fea2SSven Eckelmann 399c6c8fea2SSven Eckelmann /* the TQ value has to be better */ 400c6c8fea2SSven Eckelmann if (orig_tq_avg < gw_tq_avg) 4015d02b3cdSLinus Lüssing goto out; 402c6c8fea2SSven Eckelmann 4039cfc7bd6SSven Eckelmann /* if the routing class is greater than 3 the value tells us how much 404c6c8fea2SSven Eckelmann * greater the TQ value of the new gateway must be 4059cfc7bd6SSven Eckelmann */ 406c6c8fea2SSven Eckelmann if ((atomic_read(&bat_priv->gw_sel_class) > 3) && 407c6c8fea2SSven Eckelmann (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class))) 4085d02b3cdSLinus Lüssing goto out; 409c6c8fea2SSven Eckelmann 41039c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 41186ceb360SSven Eckelmann "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n", 412c6c8fea2SSven Eckelmann gw_tq_avg, orig_tq_avg); 413c6c8fea2SSven Eckelmann 4144e820e72SAntonio Quartulli reselect: 4154e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 4165d02b3cdSLinus Lüssing out: 41757f0c07cSLinus Lüssing if (curr_gw_orig) 4185d967310SSven Eckelmann batadv_orig_node_put(curr_gw_orig); 419e1a5382fSLinus Lüssing if (router_gw) 420*25bb2509SSven Eckelmann batadv_neigh_node_put(router_gw); 421e1a5382fSLinus Lüssing if (router_orig) 422*25bb2509SSven Eckelmann batadv_neigh_node_put(router_orig); 42389652331SSimon Wunderlich if (router_gw_tq) 42489652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_gw_tq); 42589652331SSimon Wunderlich if (router_orig_tq) 42689652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_orig_tq); 427c6c8fea2SSven Eckelmann } 428c6c8fea2SSven Eckelmann 429414254e3SMarek Lindner /** 430414254e3SMarek Lindner * batadv_gw_node_add - add gateway node to list of available gateways 431414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 432414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 433414254e3SMarek Lindner * @gateway: announced bandwidth information 434414254e3SMarek Lindner */ 43556303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv, 43656303d34SSven Eckelmann struct batadv_orig_node *orig_node, 437414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 438c6c8fea2SSven Eckelmann { 43956303d34SSven Eckelmann struct batadv_gw_node *gw_node; 440414254e3SMarek Lindner 441414254e3SMarek Lindner if (gateway->bandwidth_down == 0) 442414254e3SMarek Lindner return; 443c6c8fea2SSven Eckelmann 4447c124391SSven Eckelmann if (!kref_get_unless_zero(&orig_node->refcount)) 445c6c8fea2SSven Eckelmann return; 446c6c8fea2SSven Eckelmann 447377fe0f9SAntonio Quartulli gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); 448377fe0f9SAntonio Quartulli if (!gw_node) { 4495d967310SSven Eckelmann batadv_orig_node_put(orig_node); 450377fe0f9SAntonio Quartulli return; 451377fe0f9SAntonio Quartulli } 452377fe0f9SAntonio Quartulli 453c6c8fea2SSven Eckelmann INIT_HLIST_NODE(&gw_node->list); 454c6c8fea2SSven Eckelmann gw_node->orig_node = orig_node; 45527a4d5efSSimon Wunderlich gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 45627a4d5efSSimon Wunderlich gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 457e7aed321SSven Eckelmann kref_init(&gw_node->refcount); 458c6c8fea2SSven Eckelmann 459807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 460807736f6SSven Eckelmann hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list); 461807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 462c6c8fea2SSven Eckelmann 46339c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 464414254e3SMarek Lindner "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", 465414254e3SMarek Lindner orig_node->orig, 466414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 467414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 468414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 469414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 470c6c8fea2SSven Eckelmann } 471c6c8fea2SSven Eckelmann 472414254e3SMarek Lindner /** 473414254e3SMarek Lindner * batadv_gw_node_get - retrieve gateway node from list of available gateways 474414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 475414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 476414254e3SMarek Lindner * 47762fe710fSSven Eckelmann * Return: gateway node if found or NULL otherwise. 47871e4aa9cSAntonio Quartulli */ 479414254e3SMarek Lindner static struct batadv_gw_node * 480414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv, 481414254e3SMarek Lindner struct batadv_orig_node *orig_node) 482414254e3SMarek Lindner { 483414254e3SMarek Lindner struct batadv_gw_node *gw_node_tmp, *gw_node = NULL; 484c6c8fea2SSven Eckelmann 485c6c8fea2SSven Eckelmann rcu_read_lock(); 486414254e3SMarek Lindner hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) { 487414254e3SMarek Lindner if (gw_node_tmp->orig_node != orig_node) 488c6c8fea2SSven Eckelmann continue; 489c6c8fea2SSven Eckelmann 490e7aed321SSven Eckelmann if (!kref_get_unless_zero(&gw_node_tmp->refcount)) 491414254e3SMarek Lindner continue; 492414254e3SMarek Lindner 493414254e3SMarek Lindner gw_node = gw_node_tmp; 494414254e3SMarek Lindner break; 495414254e3SMarek Lindner } 496414254e3SMarek Lindner rcu_read_unlock(); 497414254e3SMarek Lindner 498414254e3SMarek Lindner return gw_node; 499414254e3SMarek Lindner } 500414254e3SMarek Lindner 501414254e3SMarek Lindner /** 502414254e3SMarek Lindner * batadv_gw_node_update - update list of available gateways with changed 503414254e3SMarek Lindner * bandwidth information 504414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 505414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 506414254e3SMarek Lindner * @gateway: announced bandwidth information 507414254e3SMarek Lindner */ 508414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv, 509414254e3SMarek Lindner struct batadv_orig_node *orig_node, 510414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 511414254e3SMarek Lindner { 512414254e3SMarek Lindner struct batadv_gw_node *gw_node, *curr_gw = NULL; 513414254e3SMarek Lindner 514414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_node); 515414254e3SMarek Lindner if (!gw_node) { 516414254e3SMarek Lindner batadv_gw_node_add(bat_priv, orig_node, gateway); 517414254e3SMarek Lindner goto out; 518414254e3SMarek Lindner } 519414254e3SMarek Lindner 520414254e3SMarek Lindner if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) && 521414254e3SMarek Lindner (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))) 522414254e3SMarek Lindner goto out; 523414254e3SMarek Lindner 52439c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 525414254e3SMarek Lindner "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n", 526414254e3SMarek Lindner orig_node->orig, 527414254e3SMarek Lindner gw_node->bandwidth_down / 10, 528414254e3SMarek Lindner gw_node->bandwidth_down % 10, 529414254e3SMarek Lindner gw_node->bandwidth_up / 10, 530414254e3SMarek Lindner gw_node->bandwidth_up % 10, 531414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 532414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 533414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 534414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 535414254e3SMarek Lindner 536414254e3SMarek Lindner gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 537414254e3SMarek Lindner gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 538c6c8fea2SSven Eckelmann 539414254e3SMarek Lindner if (ntohl(gateway->bandwidth_down) == 0) { 54039c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 541c6c8fea2SSven Eckelmann "Gateway %pM removed from gateway list\n", 542c6c8fea2SSven Eckelmann orig_node->orig); 543c6c8fea2SSven Eckelmann 544414254e3SMarek Lindner /* Note: We don't need a NULL check here, since curr_gw never 545414254e3SMarek Lindner * gets dereferenced. 546414254e3SMarek Lindner */ 547bd3524c1SSimon Wunderlich spin_lock_bh(&bat_priv->gw.list_lock); 548c18bdd01SSven Eckelmann if (!hlist_unhashed(&gw_node->list)) { 549bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list); 550bd3524c1SSimon Wunderlich batadv_gw_node_free_ref(gw_node); 551c18bdd01SSven Eckelmann } 552c18bdd01SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 553bd3524c1SSimon Wunderlich 554414254e3SMarek Lindner curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 555c4aac1abSMarek Lindner if (gw_node == curr_gw) 5564e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 557bd3524c1SSimon Wunderlich 558bd3524c1SSimon Wunderlich if (curr_gw) 559bd3524c1SSimon Wunderlich batadv_gw_node_free_ref(curr_gw); 560414254e3SMarek Lindner } 56171e4aa9cSAntonio Quartulli 562414254e3SMarek Lindner out: 563414254e3SMarek Lindner if (gw_node) 564414254e3SMarek Lindner batadv_gw_node_free_ref(gw_node); 565c6c8fea2SSven Eckelmann } 566c6c8fea2SSven Eckelmann 56756303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv, 56856303d34SSven Eckelmann struct batadv_orig_node *orig_node) 569c6c8fea2SSven Eckelmann { 570414254e3SMarek Lindner struct batadv_tvlv_gateway_data gateway; 571414254e3SMarek Lindner 572414254e3SMarek Lindner gateway.bandwidth_down = 0; 573414254e3SMarek Lindner gateway.bandwidth_up = 0; 574414254e3SMarek Lindner 575414254e3SMarek Lindner batadv_gw_node_update(bat_priv, orig_node, &gateway); 576c6c8fea2SSven Eckelmann } 577c6c8fea2SSven Eckelmann 578bd3524c1SSimon Wunderlich void batadv_gw_node_free(struct batadv_priv *bat_priv) 579c6c8fea2SSven Eckelmann { 580bd3524c1SSimon Wunderlich struct batadv_gw_node *gw_node; 581b67bfe0dSSasha Levin struct hlist_node *node_tmp; 582c6c8fea2SSven Eckelmann 583807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 584b67bfe0dSSasha Levin hlist_for_each_entry_safe(gw_node, node_tmp, 585807736f6SSven Eckelmann &bat_priv->gw.list, list) { 586bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list); 5871409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 588c6c8fea2SSven Eckelmann } 589807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 590c6c8fea2SSven Eckelmann } 591c6c8fea2SSven Eckelmann 5929cfc7bd6SSven Eckelmann /* fails if orig_node has no router */ 59356303d34SSven Eckelmann static int batadv_write_buffer_text(struct batadv_priv *bat_priv, 5941409a834SSven Eckelmann struct seq_file *seq, 59556303d34SSven Eckelmann const struct batadv_gw_node *gw_node) 596c6c8fea2SSven Eckelmann { 59756303d34SSven Eckelmann struct batadv_gw_node *curr_gw; 59856303d34SSven Eckelmann struct batadv_neigh_node *router; 59989652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo = NULL; 600414254e3SMarek Lindner int ret = -1; 601c6c8fea2SSven Eckelmann 6027351a482SSimon Wunderlich router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT); 603e1a5382fSLinus Lüssing if (!router) 604e1a5382fSLinus Lüssing goto out; 605e1a5382fSLinus Lüssing 60689652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT); 60789652331SSimon Wunderlich if (!router_ifinfo) 60889652331SSimon Wunderlich goto out; 60989652331SSimon Wunderlich 6101409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 6115d02b3cdSLinus Lüssing 6126d91147dSJoe Perches seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n", 6135d02b3cdSLinus Lüssing (curr_gw == gw_node ? "=>" : " "), 614c6c8fea2SSven Eckelmann gw_node->orig_node->orig, 61589652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg, router->addr, 616e1a5382fSLinus Lüssing router->if_incoming->net_dev->name, 617414254e3SMarek Lindner gw_node->bandwidth_down / 10, 618414254e3SMarek Lindner gw_node->bandwidth_down % 10, 619414254e3SMarek Lindner gw_node->bandwidth_up / 10, 620414254e3SMarek Lindner gw_node->bandwidth_up % 10); 62192b83917SJoe Perches ret = seq_has_overflowed(seq) ? -1 : 0; 6225d02b3cdSLinus Lüssing 623c4aac1abSMarek Lindner if (curr_gw) 6241409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 625e1a5382fSLinus Lüssing out: 62689652331SSimon Wunderlich if (router_ifinfo) 62789652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_ifinfo); 62889652331SSimon Wunderlich if (router) 629*25bb2509SSven Eckelmann batadv_neigh_node_put(router); 6305d02b3cdSLinus Lüssing return ret; 631c6c8fea2SSven Eckelmann } 632c6c8fea2SSven Eckelmann 6337cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset) 634c6c8fea2SSven Eckelmann { 635c6c8fea2SSven Eckelmann struct net_device *net_dev = (struct net_device *)seq->private; 63656303d34SSven Eckelmann struct batadv_priv *bat_priv = netdev_priv(net_dev); 63756303d34SSven Eckelmann struct batadv_hard_iface *primary_if; 63856303d34SSven Eckelmann struct batadv_gw_node *gw_node; 63930da63a6SMarek Lindner int gw_count = 0; 640c6c8fea2SSven Eckelmann 64130da63a6SMarek Lindner primary_if = batadv_seq_print_text_primary_if_get(seq); 64230da63a6SMarek Lindner if (!primary_if) 64332ae9b22SMarek Lindner goto out; 644c6c8fea2SSven Eckelmann 64586ceb360SSven Eckelmann seq_printf(seq, 646414254e3SMarek Lindner " %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", 64742d0b044SSven Eckelmann "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF", 64842d0b044SSven Eckelmann BATADV_SOURCE_VERSION, primary_if->net_dev->name, 64932ae9b22SMarek Lindner primary_if->net_dev->dev_addr, net_dev->name); 650c6c8fea2SSven Eckelmann 651c6c8fea2SSven Eckelmann rcu_read_lock(); 652b67bfe0dSSasha Levin hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 653e1a5382fSLinus Lüssing /* fails if orig_node has no router */ 6541409a834SSven Eckelmann if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0) 655c6c8fea2SSven Eckelmann continue; 656c6c8fea2SSven Eckelmann 657c6c8fea2SSven Eckelmann gw_count++; 658c6c8fea2SSven Eckelmann } 659c6c8fea2SSven Eckelmann rcu_read_unlock(); 660c6c8fea2SSven Eckelmann 661c6c8fea2SSven Eckelmann if (gw_count == 0) 6620c814653SAntonio Quartulli seq_puts(seq, "No gateways in range ...\n"); 663c6c8fea2SSven Eckelmann 66432ae9b22SMarek Lindner out: 66532ae9b22SMarek Lindner if (primary_if) 66682047ad7SSven Eckelmann batadv_hardif_put(primary_if); 66730da63a6SMarek Lindner return 0; 668c6c8fea2SSven Eckelmann } 669c6c8fea2SSven Eckelmann 6706c413b1cSAntonio Quartulli /** 6716c413b1cSAntonio Quartulli * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message 6726c413b1cSAntonio Quartulli * @skb: the packet to check 6736c413b1cSAntonio Quartulli * @header_len: a pointer to the batman-adv header size 6746c413b1cSAntonio Quartulli * @chaddr: buffer where the client address will be stored. Valid 6756c413b1cSAntonio Quartulli * only if the function returns BATADV_DHCP_TO_CLIENT 6766c413b1cSAntonio Quartulli * 67762fe710fSSven Eckelmann * This function may re-allocate the data buffer of the skb passed as argument. 67862fe710fSSven Eckelmann * 67962fe710fSSven Eckelmann * Return: 6806c413b1cSAntonio Quartulli * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error 6816c413b1cSAntonio Quartulli * while parsing it 6826c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server 6836c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client 6849cfc7bd6SSven Eckelmann */ 6856c413b1cSAntonio Quartulli enum batadv_dhcp_recipient 6866c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len, 6876b5e971aSSven Eckelmann u8 *chaddr) 688c6c8fea2SSven Eckelmann { 6896c413b1cSAntonio Quartulli enum batadv_dhcp_recipient ret = BATADV_DHCP_NO; 690c6c8fea2SSven Eckelmann struct ethhdr *ethhdr; 691c6c8fea2SSven Eckelmann struct iphdr *iphdr; 692c6c8fea2SSven Eckelmann struct ipv6hdr *ipv6hdr; 693c6c8fea2SSven Eckelmann struct udphdr *udphdr; 694f7f8ed56SAntonio Quartulli struct vlan_ethhdr *vhdr; 6956c413b1cSAntonio Quartulli int chaddr_offset; 696f7f8ed56SAntonio Quartulli __be16 proto; 6976b5e971aSSven Eckelmann u8 *p; 698c6c8fea2SSven Eckelmann 699c6c8fea2SSven Eckelmann /* check for ethernet header */ 700be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) 7016c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7026c413b1cSAntonio Quartulli 703927c2ed7SLinus Lüssing ethhdr = eth_hdr(skb); 704f7f8ed56SAntonio Quartulli proto = ethhdr->h_proto; 705be7af5cfSMarek Lindner *header_len += ETH_HLEN; 706c6c8fea2SSven Eckelmann 707c6c8fea2SSven Eckelmann /* check for initial vlan header */ 708f7f8ed56SAntonio Quartulli if (proto == htons(ETH_P_8021Q)) { 709be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) 7106c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 711f7f8ed56SAntonio Quartulli 712927c2ed7SLinus Lüssing vhdr = vlan_eth_hdr(skb); 713f7f8ed56SAntonio Quartulli proto = vhdr->h_vlan_encapsulated_proto; 714be7af5cfSMarek Lindner *header_len += VLAN_HLEN; 715c6c8fea2SSven Eckelmann } 716c6c8fea2SSven Eckelmann 717c6c8fea2SSven Eckelmann /* check for ip header */ 718f7f8ed56SAntonio Quartulli switch (proto) { 719f7f8ed56SAntonio Quartulli case htons(ETH_P_IP): 720be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) 7216c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7226c413b1cSAntonio Quartulli 723be7af5cfSMarek Lindner iphdr = (struct iphdr *)(skb->data + *header_len); 724be7af5cfSMarek Lindner *header_len += iphdr->ihl * 4; 725c6c8fea2SSven Eckelmann 726c6c8fea2SSven Eckelmann /* check for udp header */ 727c6c8fea2SSven Eckelmann if (iphdr->protocol != IPPROTO_UDP) 7286c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 729c6c8fea2SSven Eckelmann 730c6c8fea2SSven Eckelmann break; 731f7f8ed56SAntonio Quartulli case htons(ETH_P_IPV6): 732be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) 7336c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7346c413b1cSAntonio Quartulli 735be7af5cfSMarek Lindner ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); 736be7af5cfSMarek Lindner *header_len += sizeof(*ipv6hdr); 737c6c8fea2SSven Eckelmann 738c6c8fea2SSven Eckelmann /* check for udp header */ 739c6c8fea2SSven Eckelmann if (ipv6hdr->nexthdr != IPPROTO_UDP) 7406c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 741c6c8fea2SSven Eckelmann 742c6c8fea2SSven Eckelmann break; 743c6c8fea2SSven Eckelmann default: 7446c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 745c6c8fea2SSven Eckelmann } 746c6c8fea2SSven Eckelmann 747be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr))) 7486c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7499d2c9488SLinus Lüssing 750be7af5cfSMarek Lindner udphdr = (struct udphdr *)(skb->data + *header_len); 751be7af5cfSMarek Lindner *header_len += sizeof(*udphdr); 752c6c8fea2SSven Eckelmann 753c6c8fea2SSven Eckelmann /* check for bootp port */ 7546c413b1cSAntonio Quartulli switch (proto) { 7556c413b1cSAntonio Quartulli case htons(ETH_P_IP): 7566c413b1cSAntonio Quartulli if (udphdr->dest == htons(67)) 7576c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 7586c413b1cSAntonio Quartulli else if (udphdr->source == htons(67)) 7596c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 7606c413b1cSAntonio Quartulli break; 7616c413b1cSAntonio Quartulli case htons(ETH_P_IPV6): 7626c413b1cSAntonio Quartulli if (udphdr->dest == htons(547)) 7636c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 7646c413b1cSAntonio Quartulli else if (udphdr->source == htons(547)) 7656c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 7666c413b1cSAntonio Quartulli break; 767be7af5cfSMarek Lindner } 768c6c8fea2SSven Eckelmann 7696c413b1cSAntonio Quartulli chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET; 7706c413b1cSAntonio Quartulli /* store the client address if the message is going to a client */ 7716c413b1cSAntonio Quartulli if (ret == BATADV_DHCP_TO_CLIENT && 7726c413b1cSAntonio Quartulli pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) { 7736c413b1cSAntonio Quartulli /* check if the DHCP packet carries an Ethernet DHCP */ 7746c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET; 7756c413b1cSAntonio Quartulli if (*p != BATADV_DHCP_HTYPE_ETHERNET) 7766c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7776c413b1cSAntonio Quartulli 7786c413b1cSAntonio Quartulli /* check if the DHCP packet carries a valid Ethernet address */ 7796c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET; 7806c413b1cSAntonio Quartulli if (*p != ETH_ALEN) 7816c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7826c413b1cSAntonio Quartulli 7838fdd0153SAntonio Quartulli ether_addr_copy(chaddr, skb->data + chaddr_offset); 7846c413b1cSAntonio Quartulli } 7856c413b1cSAntonio Quartulli 7866c413b1cSAntonio Quartulli return ret; 7876c413b1cSAntonio Quartulli } 788aa143d28SAntonio Quartulli 789bbb877edSAntonio Quartulli /** 790bbb877edSAntonio Quartulli * batadv_gw_out_of_range - check if the dhcp request destination is the best gw 791bbb877edSAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 792bbb877edSAntonio Quartulli * @skb: the outgoing packet 793bbb877edSAntonio Quartulli * 794bbb877edSAntonio Quartulli * Check if the skb is a DHCP request and if it is sent to the current best GW 795bbb877edSAntonio Quartulli * server. Due to topology changes it may be the case that the GW server 796bbb877edSAntonio Quartulli * previously selected is not the best one anymore. 797bbb877edSAntonio Quartulli * 798bbb877edSAntonio Quartulli * This call might reallocate skb data. 7996c413b1cSAntonio Quartulli * Must be invoked only when the DHCP packet is going TO a DHCP SERVER. 80062fe710fSSven Eckelmann * 80162fe710fSSven Eckelmann * Return: true if the packet destination is unicast and it is not the best gw, 80262fe710fSSven Eckelmann * false otherwise. 803bbb877edSAntonio Quartulli */ 80456303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, 8059d2c9488SLinus Lüssing struct sk_buff *skb) 806be7af5cfSMarek Lindner { 8074f248cffSSven Eckelmann struct batadv_neigh_node *neigh_curr = NULL; 8084f248cffSSven Eckelmann struct batadv_neigh_node *neigh_old = NULL; 80956303d34SSven Eckelmann struct batadv_orig_node *orig_dst_node = NULL; 8104f248cffSSven Eckelmann struct batadv_gw_node *gw_node = NULL; 8114f248cffSSven Eckelmann struct batadv_gw_node *curr_gw = NULL; 81289652331SSimon Wunderlich struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo; 8136c413b1cSAntonio Quartulli struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 8146c413b1cSAntonio Quartulli bool out_of_range = false; 8156b5e971aSSven Eckelmann u8 curr_tq_avg; 816bbb877edSAntonio Quartulli unsigned short vid; 817bbb877edSAntonio Quartulli 818bbb877edSAntonio Quartulli vid = batadv_get_vid(skb, 0); 819be7af5cfSMarek Lindner 82008c36d3eSSven Eckelmann orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, 821bbb877edSAntonio Quartulli ethhdr->h_dest, vid); 822be7af5cfSMarek Lindner if (!orig_dst_node) 823be7af5cfSMarek Lindner goto out; 824be7af5cfSMarek Lindner 825414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_dst_node); 8260d164491SAntonio Quartulli if (!gw_node) 827be7af5cfSMarek Lindner goto out; 828be7af5cfSMarek Lindner 829be7af5cfSMarek Lindner switch (atomic_read(&bat_priv->gw_mode)) { 830cd646ab1SSven Eckelmann case BATADV_GW_MODE_SERVER: 831be7af5cfSMarek Lindner /* If we are a GW then we are our best GW. We can artificially 8329cfc7bd6SSven Eckelmann * set the tq towards ourself as the maximum value 8339cfc7bd6SSven Eckelmann */ 83442d0b044SSven Eckelmann curr_tq_avg = BATADV_TQ_MAX_VALUE; 835be7af5cfSMarek Lindner break; 836cd646ab1SSven Eckelmann case BATADV_GW_MODE_CLIENT: 8371409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 838c4aac1abSMarek Lindner if (!curr_gw) 839be7af5cfSMarek Lindner goto out; 840c6c8fea2SSven Eckelmann 841be7af5cfSMarek Lindner /* packet is going to our gateway */ 842be7af5cfSMarek Lindner if (curr_gw->orig_node == orig_dst_node) 843be7af5cfSMarek Lindner goto out; 844be7af5cfSMarek Lindner 84543676ab5SAntonio Quartulli /* If the dhcp packet has been sent to a different gw, 84643676ab5SAntonio Quartulli * we have to evaluate whether the old gw is still 8479cfc7bd6SSven Eckelmann * reliable enough 8489cfc7bd6SSven Eckelmann */ 84930d3c511SSven Eckelmann neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node, 85030d3c511SSven Eckelmann NULL); 851be7af5cfSMarek Lindner if (!neigh_curr) 852be7af5cfSMarek Lindner goto out; 853be7af5cfSMarek Lindner 85489652331SSimon Wunderlich curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr, 85589652331SSimon Wunderlich BATADV_IF_DEFAULT); 85689652331SSimon Wunderlich if (!curr_ifinfo) 85789652331SSimon Wunderlich goto out; 85889652331SSimon Wunderlich 85989652331SSimon Wunderlich curr_tq_avg = curr_ifinfo->bat_iv.tq_avg; 86089652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(curr_ifinfo); 86189652331SSimon Wunderlich 862be7af5cfSMarek Lindner break; 863cd646ab1SSven Eckelmann case BATADV_GW_MODE_OFF: 864be7af5cfSMarek Lindner default: 865be7af5cfSMarek Lindner goto out; 86643676ab5SAntonio Quartulli } 867be7af5cfSMarek Lindner 86830d3c511SSven Eckelmann neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL); 8692ef04f47SDan Carpenter if (!neigh_old) 870be7af5cfSMarek Lindner goto out; 871be7af5cfSMarek Lindner 87289652331SSimon Wunderlich old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT); 87389652331SSimon Wunderlich if (!old_ifinfo) 87489652331SSimon Wunderlich goto out; 87589652331SSimon Wunderlich 87689652331SSimon Wunderlich if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD) 877be7af5cfSMarek Lindner out_of_range = true; 87889652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(old_ifinfo); 879be7af5cfSMarek Lindner 880be7af5cfSMarek Lindner out: 881be7af5cfSMarek Lindner if (orig_dst_node) 8825d967310SSven Eckelmann batadv_orig_node_put(orig_dst_node); 883be7af5cfSMarek Lindner if (curr_gw) 8841409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 885414254e3SMarek Lindner if (gw_node) 886414254e3SMarek Lindner batadv_gw_node_free_ref(gw_node); 88743676ab5SAntonio Quartulli if (neigh_old) 888*25bb2509SSven Eckelmann batadv_neigh_node_put(neigh_old); 88943676ab5SAntonio Quartulli if (neigh_curr) 890*25bb2509SSven Eckelmann batadv_neigh_node_put(neigh_curr); 891be7af5cfSMarek Lindner return out_of_range; 892c6c8fea2SSven Eckelmann } 893