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" 45*ba412080SSven 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 */ 833a01743dSSven Eckelmann static 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 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) 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 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 1963a24a63eSAntonio Quartulli 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) 2073a01743dSSven Eckelmann batadv_gw_node_put(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) 2223a01743dSSven Eckelmann batadv_gw_node_put(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 2353a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 2362265c141SAntonio Quartulli 2372265c141SAntonio Quartulli next: 23825bb2509SSven Eckelmann batadv_neigh_node_put(router); 23989652331SSimon Wunderlich if (router_ifinfo) 240044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(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 2593a24a63eSAntonio 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 2763a01743dSSven Eckelmann batadv_gw_node_put(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 2873a24a63eSAntonio Quartulli 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) 3513a01743dSSven Eckelmann batadv_gw_node_put(curr_gw); 3522265c141SAntonio Quartulli if (next_gw) 3533a01743dSSven Eckelmann batadv_gw_node_put(next_gw); 3542265c141SAntonio Quartulli if (router) 35525bb2509SSven Eckelmann batadv_neigh_node_put(router); 35689652331SSimon Wunderlich if (router_ifinfo) 357044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(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 */ 4063a24a63eSAntonio Quartulli if ((atomic_read(&bat_priv->gw.sel_class) > 3) && 4073a24a63eSAntonio Quartulli (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) 42025bb2509SSven Eckelmann batadv_neigh_node_put(router_gw); 421e1a5382fSLinus Lüssing if (router_orig) 42225bb2509SSven Eckelmann batadv_neigh_node_put(router_orig); 42389652331SSimon Wunderlich if (router_gw_tq) 424044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(router_gw_tq); 42589652331SSimon Wunderlich if (router_orig_tq) 426044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(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 444377fe0f9SAntonio Quartulli gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); 445c3ba37a7SSven Eckelmann if (!gw_node) 446377fe0f9SAntonio Quartulli return; 447377fe0f9SAntonio Quartulli 448c3ba37a7SSven Eckelmann kref_get(&orig_node->refcount); 449c6c8fea2SSven Eckelmann INIT_HLIST_NODE(&gw_node->list); 450c6c8fea2SSven Eckelmann gw_node->orig_node = orig_node; 45127a4d5efSSimon Wunderlich gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 45227a4d5efSSimon Wunderlich gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 453e7aed321SSven Eckelmann kref_init(&gw_node->refcount); 454c6c8fea2SSven Eckelmann 455807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 456807736f6SSven Eckelmann hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list); 457807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 458c6c8fea2SSven Eckelmann 45939c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 460414254e3SMarek Lindner "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", 461414254e3SMarek Lindner orig_node->orig, 462414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 463414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 464414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 465414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 466c6c8fea2SSven Eckelmann } 467c6c8fea2SSven Eckelmann 468414254e3SMarek Lindner /** 469414254e3SMarek Lindner * batadv_gw_node_get - retrieve gateway node from list of available gateways 470414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 471414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 472414254e3SMarek Lindner * 47362fe710fSSven Eckelmann * Return: gateway node if found or NULL otherwise. 47471e4aa9cSAntonio Quartulli */ 475414254e3SMarek Lindner static struct batadv_gw_node * 476414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv, 477414254e3SMarek Lindner struct batadv_orig_node *orig_node) 478414254e3SMarek Lindner { 479414254e3SMarek Lindner struct batadv_gw_node *gw_node_tmp, *gw_node = NULL; 480c6c8fea2SSven Eckelmann 481c6c8fea2SSven Eckelmann rcu_read_lock(); 482414254e3SMarek Lindner hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) { 483414254e3SMarek Lindner if (gw_node_tmp->orig_node != orig_node) 484c6c8fea2SSven Eckelmann continue; 485c6c8fea2SSven Eckelmann 486e7aed321SSven Eckelmann if (!kref_get_unless_zero(&gw_node_tmp->refcount)) 487414254e3SMarek Lindner continue; 488414254e3SMarek Lindner 489414254e3SMarek Lindner gw_node = gw_node_tmp; 490414254e3SMarek Lindner break; 491414254e3SMarek Lindner } 492414254e3SMarek Lindner rcu_read_unlock(); 493414254e3SMarek Lindner 494414254e3SMarek Lindner return gw_node; 495414254e3SMarek Lindner } 496414254e3SMarek Lindner 497414254e3SMarek Lindner /** 498414254e3SMarek Lindner * batadv_gw_node_update - update list of available gateways with changed 499414254e3SMarek Lindner * bandwidth information 500414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 501414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 502414254e3SMarek Lindner * @gateway: announced bandwidth information 503414254e3SMarek Lindner */ 504414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv, 505414254e3SMarek Lindner struct batadv_orig_node *orig_node, 506414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 507414254e3SMarek Lindner { 508414254e3SMarek Lindner struct batadv_gw_node *gw_node, *curr_gw = NULL; 509414254e3SMarek Lindner 510414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_node); 511414254e3SMarek Lindner if (!gw_node) { 512414254e3SMarek Lindner batadv_gw_node_add(bat_priv, orig_node, gateway); 513414254e3SMarek Lindner goto out; 514414254e3SMarek Lindner } 515414254e3SMarek Lindner 516414254e3SMarek Lindner if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) && 517414254e3SMarek Lindner (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))) 518414254e3SMarek Lindner goto out; 519414254e3SMarek Lindner 52039c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 521414254e3SMarek Lindner "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n", 522414254e3SMarek Lindner orig_node->orig, 523414254e3SMarek Lindner gw_node->bandwidth_down / 10, 524414254e3SMarek Lindner gw_node->bandwidth_down % 10, 525414254e3SMarek Lindner gw_node->bandwidth_up / 10, 526414254e3SMarek Lindner gw_node->bandwidth_up % 10, 527414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 528414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 529414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 530414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 531414254e3SMarek Lindner 532414254e3SMarek Lindner gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 533414254e3SMarek Lindner gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 534c6c8fea2SSven Eckelmann 535414254e3SMarek Lindner if (ntohl(gateway->bandwidth_down) == 0) { 53639c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 537c6c8fea2SSven Eckelmann "Gateway %pM removed from gateway list\n", 538c6c8fea2SSven Eckelmann orig_node->orig); 539c6c8fea2SSven Eckelmann 540414254e3SMarek Lindner /* Note: We don't need a NULL check here, since curr_gw never 541414254e3SMarek Lindner * gets dereferenced. 542414254e3SMarek Lindner */ 543bd3524c1SSimon Wunderlich spin_lock_bh(&bat_priv->gw.list_lock); 544c18bdd01SSven Eckelmann if (!hlist_unhashed(&gw_node->list)) { 545bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list); 5463a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 547c18bdd01SSven Eckelmann } 548c18bdd01SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 549bd3524c1SSimon Wunderlich 550414254e3SMarek Lindner curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 551c4aac1abSMarek Lindner if (gw_node == curr_gw) 5524e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 553bd3524c1SSimon Wunderlich 554bd3524c1SSimon Wunderlich if (curr_gw) 5553a01743dSSven Eckelmann batadv_gw_node_put(curr_gw); 556414254e3SMarek Lindner } 55771e4aa9cSAntonio Quartulli 558414254e3SMarek Lindner out: 559414254e3SMarek Lindner if (gw_node) 5603a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 561c6c8fea2SSven Eckelmann } 562c6c8fea2SSven Eckelmann 56356303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv, 56456303d34SSven Eckelmann struct batadv_orig_node *orig_node) 565c6c8fea2SSven Eckelmann { 566414254e3SMarek Lindner struct batadv_tvlv_gateway_data gateway; 567414254e3SMarek Lindner 568414254e3SMarek Lindner gateway.bandwidth_down = 0; 569414254e3SMarek Lindner gateway.bandwidth_up = 0; 570414254e3SMarek Lindner 571414254e3SMarek Lindner batadv_gw_node_update(bat_priv, orig_node, &gateway); 572c6c8fea2SSven Eckelmann } 573c6c8fea2SSven Eckelmann 574bd3524c1SSimon Wunderlich void batadv_gw_node_free(struct batadv_priv *bat_priv) 575c6c8fea2SSven Eckelmann { 576bd3524c1SSimon Wunderlich struct batadv_gw_node *gw_node; 577b67bfe0dSSasha Levin struct hlist_node *node_tmp; 578c6c8fea2SSven Eckelmann 579807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 580b67bfe0dSSasha Levin hlist_for_each_entry_safe(gw_node, node_tmp, 581807736f6SSven Eckelmann &bat_priv->gw.list, list) { 582bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list); 5833a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 584c6c8fea2SSven Eckelmann } 585807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 586c6c8fea2SSven Eckelmann } 587c6c8fea2SSven Eckelmann 5889cfc7bd6SSven Eckelmann /* fails if orig_node has no router */ 58956303d34SSven Eckelmann static int batadv_write_buffer_text(struct batadv_priv *bat_priv, 5901409a834SSven Eckelmann struct seq_file *seq, 59156303d34SSven Eckelmann const struct batadv_gw_node *gw_node) 592c6c8fea2SSven Eckelmann { 59356303d34SSven Eckelmann struct batadv_gw_node *curr_gw; 59456303d34SSven Eckelmann struct batadv_neigh_node *router; 59589652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo = NULL; 596414254e3SMarek Lindner int ret = -1; 597c6c8fea2SSven Eckelmann 5987351a482SSimon Wunderlich router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT); 599e1a5382fSLinus Lüssing if (!router) 600e1a5382fSLinus Lüssing goto out; 601e1a5382fSLinus Lüssing 60289652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT); 60389652331SSimon Wunderlich if (!router_ifinfo) 60489652331SSimon Wunderlich goto out; 60589652331SSimon Wunderlich 6061409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 6075d02b3cdSLinus Lüssing 6086d91147dSJoe Perches seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n", 6095d02b3cdSLinus Lüssing (curr_gw == gw_node ? "=>" : " "), 610c6c8fea2SSven Eckelmann gw_node->orig_node->orig, 61189652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg, router->addr, 612e1a5382fSLinus Lüssing router->if_incoming->net_dev->name, 613414254e3SMarek Lindner gw_node->bandwidth_down / 10, 614414254e3SMarek Lindner gw_node->bandwidth_down % 10, 615414254e3SMarek Lindner gw_node->bandwidth_up / 10, 616414254e3SMarek Lindner gw_node->bandwidth_up % 10); 61792b83917SJoe Perches ret = seq_has_overflowed(seq) ? -1 : 0; 6185d02b3cdSLinus Lüssing 619c4aac1abSMarek Lindner if (curr_gw) 6203a01743dSSven Eckelmann batadv_gw_node_put(curr_gw); 621e1a5382fSLinus Lüssing out: 62289652331SSimon Wunderlich if (router_ifinfo) 623044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(router_ifinfo); 62489652331SSimon Wunderlich if (router) 62525bb2509SSven Eckelmann batadv_neigh_node_put(router); 6265d02b3cdSLinus Lüssing return ret; 627c6c8fea2SSven Eckelmann } 628c6c8fea2SSven Eckelmann 6297cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset) 630c6c8fea2SSven Eckelmann { 631c6c8fea2SSven Eckelmann struct net_device *net_dev = (struct net_device *)seq->private; 63256303d34SSven Eckelmann struct batadv_priv *bat_priv = netdev_priv(net_dev); 63356303d34SSven Eckelmann struct batadv_hard_iface *primary_if; 63456303d34SSven Eckelmann struct batadv_gw_node *gw_node; 63530da63a6SMarek Lindner int gw_count = 0; 636c6c8fea2SSven Eckelmann 63730da63a6SMarek Lindner primary_if = batadv_seq_print_text_primary_if_get(seq); 63830da63a6SMarek Lindner if (!primary_if) 63932ae9b22SMarek Lindner goto out; 640c6c8fea2SSven Eckelmann 64186ceb360SSven Eckelmann seq_printf(seq, 64292d2b1a5SAntonio Quartulli " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", 64342d0b044SSven Eckelmann BATADV_SOURCE_VERSION, primary_if->net_dev->name, 64432ae9b22SMarek Lindner primary_if->net_dev->dev_addr, net_dev->name); 645c6c8fea2SSven Eckelmann 646c6c8fea2SSven Eckelmann rcu_read_lock(); 647b67bfe0dSSasha Levin hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 648e1a5382fSLinus Lüssing /* fails if orig_node has no router */ 6491409a834SSven Eckelmann if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0) 650c6c8fea2SSven Eckelmann continue; 651c6c8fea2SSven Eckelmann 652c6c8fea2SSven Eckelmann gw_count++; 653c6c8fea2SSven Eckelmann } 654c6c8fea2SSven Eckelmann rcu_read_unlock(); 655c6c8fea2SSven Eckelmann 656c6c8fea2SSven Eckelmann if (gw_count == 0) 6570c814653SAntonio Quartulli seq_puts(seq, "No gateways in range ...\n"); 658c6c8fea2SSven Eckelmann 65932ae9b22SMarek Lindner out: 66032ae9b22SMarek Lindner if (primary_if) 66182047ad7SSven Eckelmann batadv_hardif_put(primary_if); 66230da63a6SMarek Lindner return 0; 663c6c8fea2SSven Eckelmann } 664c6c8fea2SSven Eckelmann 6656c413b1cSAntonio Quartulli /** 6666c413b1cSAntonio Quartulli * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message 6676c413b1cSAntonio Quartulli * @skb: the packet to check 6686c413b1cSAntonio Quartulli * @header_len: a pointer to the batman-adv header size 6696c413b1cSAntonio Quartulli * @chaddr: buffer where the client address will be stored. Valid 6706c413b1cSAntonio Quartulli * only if the function returns BATADV_DHCP_TO_CLIENT 6716c413b1cSAntonio Quartulli * 67262fe710fSSven Eckelmann * This function may re-allocate the data buffer of the skb passed as argument. 67362fe710fSSven Eckelmann * 67462fe710fSSven Eckelmann * Return: 6756c413b1cSAntonio Quartulli * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error 6766c413b1cSAntonio Quartulli * while parsing it 6776c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server 6786c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client 6799cfc7bd6SSven Eckelmann */ 6806c413b1cSAntonio Quartulli enum batadv_dhcp_recipient 6816c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len, 6826b5e971aSSven Eckelmann u8 *chaddr) 683c6c8fea2SSven Eckelmann { 6846c413b1cSAntonio Quartulli enum batadv_dhcp_recipient ret = BATADV_DHCP_NO; 685c6c8fea2SSven Eckelmann struct ethhdr *ethhdr; 686c6c8fea2SSven Eckelmann struct iphdr *iphdr; 687c6c8fea2SSven Eckelmann struct ipv6hdr *ipv6hdr; 688c6c8fea2SSven Eckelmann struct udphdr *udphdr; 689f7f8ed56SAntonio Quartulli struct vlan_ethhdr *vhdr; 6906c413b1cSAntonio Quartulli int chaddr_offset; 691f7f8ed56SAntonio Quartulli __be16 proto; 6926b5e971aSSven Eckelmann u8 *p; 693c6c8fea2SSven Eckelmann 694c6c8fea2SSven Eckelmann /* check for ethernet header */ 695be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) 6966c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 6976c413b1cSAntonio Quartulli 698927c2ed7SLinus Lüssing ethhdr = eth_hdr(skb); 699f7f8ed56SAntonio Quartulli proto = ethhdr->h_proto; 700be7af5cfSMarek Lindner *header_len += ETH_HLEN; 701c6c8fea2SSven Eckelmann 702c6c8fea2SSven Eckelmann /* check for initial vlan header */ 703f7f8ed56SAntonio Quartulli if (proto == htons(ETH_P_8021Q)) { 704be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) 7056c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 706f7f8ed56SAntonio Quartulli 707927c2ed7SLinus Lüssing vhdr = vlan_eth_hdr(skb); 708f7f8ed56SAntonio Quartulli proto = vhdr->h_vlan_encapsulated_proto; 709be7af5cfSMarek Lindner *header_len += VLAN_HLEN; 710c6c8fea2SSven Eckelmann } 711c6c8fea2SSven Eckelmann 712c6c8fea2SSven Eckelmann /* check for ip header */ 713f7f8ed56SAntonio Quartulli switch (proto) { 714f7f8ed56SAntonio Quartulli case htons(ETH_P_IP): 715be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) 7166c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7176c413b1cSAntonio Quartulli 718be7af5cfSMarek Lindner iphdr = (struct iphdr *)(skb->data + *header_len); 719be7af5cfSMarek Lindner *header_len += iphdr->ihl * 4; 720c6c8fea2SSven Eckelmann 721c6c8fea2SSven Eckelmann /* check for udp header */ 722c6c8fea2SSven Eckelmann if (iphdr->protocol != IPPROTO_UDP) 7236c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 724c6c8fea2SSven Eckelmann 725c6c8fea2SSven Eckelmann break; 726f7f8ed56SAntonio Quartulli case htons(ETH_P_IPV6): 727be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) 7286c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7296c413b1cSAntonio Quartulli 730be7af5cfSMarek Lindner ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); 731be7af5cfSMarek Lindner *header_len += sizeof(*ipv6hdr); 732c6c8fea2SSven Eckelmann 733c6c8fea2SSven Eckelmann /* check for udp header */ 734c6c8fea2SSven Eckelmann if (ipv6hdr->nexthdr != IPPROTO_UDP) 7356c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 736c6c8fea2SSven Eckelmann 737c6c8fea2SSven Eckelmann break; 738c6c8fea2SSven Eckelmann default: 7396c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 740c6c8fea2SSven Eckelmann } 741c6c8fea2SSven Eckelmann 742be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr))) 7436c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7449d2c9488SLinus Lüssing 745be7af5cfSMarek Lindner udphdr = (struct udphdr *)(skb->data + *header_len); 746be7af5cfSMarek Lindner *header_len += sizeof(*udphdr); 747c6c8fea2SSven Eckelmann 748c6c8fea2SSven Eckelmann /* check for bootp port */ 7496c413b1cSAntonio Quartulli switch (proto) { 7506c413b1cSAntonio Quartulli case htons(ETH_P_IP): 7516c413b1cSAntonio Quartulli if (udphdr->dest == htons(67)) 7526c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 7536c413b1cSAntonio Quartulli else if (udphdr->source == htons(67)) 7546c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 7556c413b1cSAntonio Quartulli break; 7566c413b1cSAntonio Quartulli case htons(ETH_P_IPV6): 7576c413b1cSAntonio Quartulli if (udphdr->dest == htons(547)) 7586c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 7596c413b1cSAntonio Quartulli else if (udphdr->source == htons(547)) 7606c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 7616c413b1cSAntonio Quartulli break; 762be7af5cfSMarek Lindner } 763c6c8fea2SSven Eckelmann 7646c413b1cSAntonio Quartulli chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET; 7656c413b1cSAntonio Quartulli /* store the client address if the message is going to a client */ 7666c413b1cSAntonio Quartulli if (ret == BATADV_DHCP_TO_CLIENT && 7676c413b1cSAntonio Quartulli pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) { 7686c413b1cSAntonio Quartulli /* check if the DHCP packet carries an Ethernet DHCP */ 7696c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET; 7706c413b1cSAntonio Quartulli if (*p != BATADV_DHCP_HTYPE_ETHERNET) 7716c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7726c413b1cSAntonio Quartulli 7736c413b1cSAntonio Quartulli /* check if the DHCP packet carries a valid Ethernet address */ 7746c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET; 7756c413b1cSAntonio Quartulli if (*p != ETH_ALEN) 7766c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7776c413b1cSAntonio Quartulli 7788fdd0153SAntonio Quartulli ether_addr_copy(chaddr, skb->data + chaddr_offset); 7796c413b1cSAntonio Quartulli } 7806c413b1cSAntonio Quartulli 7816c413b1cSAntonio Quartulli return ret; 7826c413b1cSAntonio Quartulli } 783aa143d28SAntonio Quartulli 784bbb877edSAntonio Quartulli /** 785bbb877edSAntonio Quartulli * batadv_gw_out_of_range - check if the dhcp request destination is the best gw 786bbb877edSAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 787bbb877edSAntonio Quartulli * @skb: the outgoing packet 788bbb877edSAntonio Quartulli * 789bbb877edSAntonio Quartulli * Check if the skb is a DHCP request and if it is sent to the current best GW 790bbb877edSAntonio Quartulli * server. Due to topology changes it may be the case that the GW server 791bbb877edSAntonio Quartulli * previously selected is not the best one anymore. 792bbb877edSAntonio Quartulli * 793bbb877edSAntonio Quartulli * This call might reallocate skb data. 7946c413b1cSAntonio Quartulli * Must be invoked only when the DHCP packet is going TO a DHCP SERVER. 79562fe710fSSven Eckelmann * 79662fe710fSSven Eckelmann * Return: true if the packet destination is unicast and it is not the best gw, 79762fe710fSSven Eckelmann * false otherwise. 798bbb877edSAntonio Quartulli */ 79956303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, 8009d2c9488SLinus Lüssing struct sk_buff *skb) 801be7af5cfSMarek Lindner { 8024f248cffSSven Eckelmann struct batadv_neigh_node *neigh_curr = NULL; 8034f248cffSSven Eckelmann struct batadv_neigh_node *neigh_old = NULL; 80456303d34SSven Eckelmann struct batadv_orig_node *orig_dst_node = NULL; 8054f248cffSSven Eckelmann struct batadv_gw_node *gw_node = NULL; 8064f248cffSSven Eckelmann struct batadv_gw_node *curr_gw = NULL; 80789652331SSimon Wunderlich struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo; 8086c413b1cSAntonio Quartulli struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 8096c413b1cSAntonio Quartulli bool out_of_range = false; 8106b5e971aSSven Eckelmann u8 curr_tq_avg; 811bbb877edSAntonio Quartulli unsigned short vid; 812bbb877edSAntonio Quartulli 813bbb877edSAntonio Quartulli vid = batadv_get_vid(skb, 0); 814be7af5cfSMarek Lindner 81508c36d3eSSven Eckelmann orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, 816bbb877edSAntonio Quartulli ethhdr->h_dest, vid); 817be7af5cfSMarek Lindner if (!orig_dst_node) 818be7af5cfSMarek Lindner goto out; 819be7af5cfSMarek Lindner 820414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_dst_node); 8210d164491SAntonio Quartulli if (!gw_node) 822be7af5cfSMarek Lindner goto out; 823be7af5cfSMarek Lindner 8243a24a63eSAntonio Quartulli switch (atomic_read(&bat_priv->gw.mode)) { 825cd646ab1SSven Eckelmann case BATADV_GW_MODE_SERVER: 826be7af5cfSMarek Lindner /* If we are a GW then we are our best GW. We can artificially 8279cfc7bd6SSven Eckelmann * set the tq towards ourself as the maximum value 8289cfc7bd6SSven Eckelmann */ 82942d0b044SSven Eckelmann curr_tq_avg = BATADV_TQ_MAX_VALUE; 830be7af5cfSMarek Lindner break; 831cd646ab1SSven Eckelmann case BATADV_GW_MODE_CLIENT: 8321409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 833c4aac1abSMarek Lindner if (!curr_gw) 834be7af5cfSMarek Lindner goto out; 835c6c8fea2SSven Eckelmann 836be7af5cfSMarek Lindner /* packet is going to our gateway */ 837be7af5cfSMarek Lindner if (curr_gw->orig_node == orig_dst_node) 838be7af5cfSMarek Lindner goto out; 839be7af5cfSMarek Lindner 84043676ab5SAntonio Quartulli /* If the dhcp packet has been sent to a different gw, 84143676ab5SAntonio Quartulli * we have to evaluate whether the old gw is still 8429cfc7bd6SSven Eckelmann * reliable enough 8439cfc7bd6SSven Eckelmann */ 84430d3c511SSven Eckelmann neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node, 84530d3c511SSven Eckelmann NULL); 846be7af5cfSMarek Lindner if (!neigh_curr) 847be7af5cfSMarek Lindner goto out; 848be7af5cfSMarek Lindner 84989652331SSimon Wunderlich curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr, 85089652331SSimon Wunderlich BATADV_IF_DEFAULT); 85189652331SSimon Wunderlich if (!curr_ifinfo) 85289652331SSimon Wunderlich goto out; 85389652331SSimon Wunderlich 85489652331SSimon Wunderlich curr_tq_avg = curr_ifinfo->bat_iv.tq_avg; 855044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(curr_ifinfo); 85689652331SSimon Wunderlich 857be7af5cfSMarek Lindner break; 858cd646ab1SSven Eckelmann case BATADV_GW_MODE_OFF: 859be7af5cfSMarek Lindner default: 860be7af5cfSMarek Lindner goto out; 86143676ab5SAntonio Quartulli } 862be7af5cfSMarek Lindner 86330d3c511SSven Eckelmann neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL); 8642ef04f47SDan Carpenter if (!neigh_old) 865be7af5cfSMarek Lindner goto out; 866be7af5cfSMarek Lindner 86789652331SSimon Wunderlich old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT); 86889652331SSimon Wunderlich if (!old_ifinfo) 86989652331SSimon Wunderlich goto out; 87089652331SSimon Wunderlich 87189652331SSimon Wunderlich if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD) 872be7af5cfSMarek Lindner out_of_range = true; 873044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(old_ifinfo); 874be7af5cfSMarek Lindner 875be7af5cfSMarek Lindner out: 876be7af5cfSMarek Lindner if (orig_dst_node) 8775d967310SSven Eckelmann batadv_orig_node_put(orig_dst_node); 878be7af5cfSMarek Lindner if (curr_gw) 8793a01743dSSven Eckelmann batadv_gw_node_put(curr_gw); 880414254e3SMarek Lindner if (gw_node) 8813a01743dSSven Eckelmann batadv_gw_node_put(gw_node); 88243676ab5SAntonio Quartulli if (neigh_old) 88325bb2509SSven Eckelmann batadv_neigh_node_put(neigh_old); 88443676ab5SAntonio Quartulli if (neigh_curr) 88525bb2509SSven Eckelmann batadv_neigh_node_put(neigh_curr); 886be7af5cfSMarek Lindner return out_of_range; 887c6c8fea2SSven Eckelmann } 888