19f6446c7SSven Eckelmann /* Copyright (C) 2009-2015 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> 311e2c2a4fSSven Eckelmann #include <linux/list.h> 321e2c2a4fSSven Eckelmann #include <linux/netdevice.h> 331e2c2a4fSSven Eckelmann #include <linux/rculist.h> 341e2c2a4fSSven Eckelmann #include <linux/rcupdate.h> 351e2c2a4fSSven Eckelmann #include <linux/seq_file.h> 361e2c2a4fSSven Eckelmann #include <linux/skbuff.h> 371e2c2a4fSSven Eckelmann #include <linux/slab.h> 381e2c2a4fSSven Eckelmann #include <linux/spinlock.h> 391e2c2a4fSSven Eckelmann #include <linux/stddef.h> 401e2c2a4fSSven Eckelmann #include <linux/udp.h> 411e2c2a4fSSven Eckelmann 42c6c8fea2SSven Eckelmann #include "gateway_common.h" 43c6c8fea2SSven Eckelmann #include "hard-interface.h" 4457f0c07cSLinus Lüssing #include "originator.h" 451e2c2a4fSSven Eckelmann #include "packet.h" 4643676ab5SAntonio Quartulli #include "routing.h" 471e2c2a4fSSven Eckelmann #include "sysfs.h" 481e2c2a4fSSven Eckelmann #include "translation-table.h" 49c6c8fea2SSven Eckelmann 506c413b1cSAntonio Quartulli /* These are the offsets of the "hw type" and "hw address length" in the dhcp 516c413b1cSAntonio Quartulli * packet starting at the beginning of the dhcp header 529cfc7bd6SSven Eckelmann */ 536c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_OFFSET 1 546c413b1cSAntonio Quartulli #define BATADV_DHCP_HLEN_OFFSET 2 556c413b1cSAntonio Quartulli /* Value of htype representing Ethernet */ 566c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_ETHERNET 0x01 576c413b1cSAntonio Quartulli /* This is the offset of the "chaddr" field in the dhcp packet starting at the 586c413b1cSAntonio Quartulli * beginning of the dhcp header 596c413b1cSAntonio Quartulli */ 606c413b1cSAntonio Quartulli #define BATADV_DHCP_CHADDR_OFFSET 28 6143676ab5SAntonio Quartulli 6256303d34SSven Eckelmann static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node) 6325b6d3c1SMarek Lindner { 64377fe0f9SAntonio Quartulli if (atomic_dec_and_test(&gw_node->refcount)) { 65377fe0f9SAntonio Quartulli batadv_orig_node_free_ref(gw_node->orig_node); 66eb340b2fSPaul E. McKenney kfree_rcu(gw_node, rcu); 67c6c8fea2SSven Eckelmann } 68377fe0f9SAntonio Quartulli } 69c6c8fea2SSven Eckelmann 7056303d34SSven Eckelmann static struct batadv_gw_node * 7156303d34SSven Eckelmann batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv) 72c6c8fea2SSven Eckelmann { 7356303d34SSven Eckelmann struct batadv_gw_node *gw_node; 74c6c8fea2SSven Eckelmann 755d02b3cdSLinus Lüssing rcu_read_lock(); 76807736f6SSven Eckelmann gw_node = rcu_dereference(bat_priv->gw.curr_gw); 77c4aac1abSMarek Lindner if (!gw_node) 785d02b3cdSLinus Lüssing goto out; 79c6c8fea2SSven Eckelmann 80c4aac1abSMarek Lindner if (!atomic_inc_not_zero(&gw_node->refcount)) 81c4aac1abSMarek Lindner gw_node = NULL; 82c4aac1abSMarek Lindner 83c4aac1abSMarek Lindner out: 84c4aac1abSMarek Lindner rcu_read_unlock(); 85c4aac1abSMarek Lindner return gw_node; 86c4aac1abSMarek Lindner } 87c4aac1abSMarek Lindner 8856303d34SSven Eckelmann struct batadv_orig_node * 8956303d34SSven Eckelmann batadv_gw_get_selected_orig(struct batadv_priv *bat_priv) 90c4aac1abSMarek Lindner { 9156303d34SSven Eckelmann struct batadv_gw_node *gw_node; 9256303d34SSven Eckelmann struct batadv_orig_node *orig_node = NULL; 93c4aac1abSMarek Lindner 941409a834SSven Eckelmann gw_node = batadv_gw_get_selected_gw_node(bat_priv); 95c4aac1abSMarek Lindner if (!gw_node) 967b36e8eeSMarek Lindner goto out; 975d02b3cdSLinus Lüssing 98c4aac1abSMarek Lindner rcu_read_lock(); 99c4aac1abSMarek Lindner orig_node = gw_node->orig_node; 100c4aac1abSMarek Lindner if (!orig_node) 101c4aac1abSMarek Lindner goto unlock; 102c4aac1abSMarek Lindner 1037b36e8eeSMarek Lindner if (!atomic_inc_not_zero(&orig_node->refcount)) 1047b36e8eeSMarek Lindner orig_node = NULL; 10543c70ad5SLinus Lüssing 106c4aac1abSMarek Lindner unlock: 1075d02b3cdSLinus Lüssing rcu_read_unlock(); 108c4aac1abSMarek Lindner out: 109c6c8fea2SSven Eckelmann if (gw_node) 1101409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 111c4aac1abSMarek Lindner return orig_node; 112c6c8fea2SSven Eckelmann } 113c6c8fea2SSven Eckelmann 11456303d34SSven Eckelmann static void batadv_gw_select(struct batadv_priv *bat_priv, 11556303d34SSven Eckelmann struct batadv_gw_node *new_gw_node) 116c6c8fea2SSven Eckelmann { 11756303d34SSven Eckelmann struct batadv_gw_node *curr_gw_node; 118c6c8fea2SSven Eckelmann 119807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 120c4aac1abSMarek Lindner 12125b6d3c1SMarek Lindner if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount)) 12225b6d3c1SMarek Lindner new_gw_node = NULL; 123c6c8fea2SSven Eckelmann 124807736f6SSven Eckelmann curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1); 125807736f6SSven Eckelmann rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node); 12625b6d3c1SMarek Lindner 12725b6d3c1SMarek Lindner if (curr_gw_node) 1281409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw_node); 129c4aac1abSMarek Lindner 130807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 131c4aac1abSMarek Lindner } 132c4aac1abSMarek Lindner 1334e820e72SAntonio Quartulli /** 1344e820e72SAntonio Quartulli * batadv_gw_reselect - force a gateway reselection 1354e820e72SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 1364e820e72SAntonio Quartulli * 1374e820e72SAntonio Quartulli * Set a flag to remind the GW component to perform a new gateway reselection. 1384e820e72SAntonio Quartulli * However this function does not ensure that the current gateway is going to be 1394e820e72SAntonio Quartulli * deselected. The reselection mechanism may elect the same gateway once again. 1404e820e72SAntonio Quartulli * 1414e820e72SAntonio Quartulli * This means that invoking batadv_gw_reselect() does not guarantee a gateway 1424e820e72SAntonio Quartulli * change and therefore a uevent is not necessarily expected. 1434e820e72SAntonio Quartulli */ 1444e820e72SAntonio Quartulli void batadv_gw_reselect(struct batadv_priv *bat_priv) 145c4aac1abSMarek Lindner { 146807736f6SSven Eckelmann atomic_set(&bat_priv->gw.reselect, 1); 147c6c8fea2SSven Eckelmann } 148c6c8fea2SSven Eckelmann 14956303d34SSven Eckelmann static struct batadv_gw_node * 15056303d34SSven Eckelmann batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv) 151c6c8fea2SSven Eckelmann { 15256303d34SSven Eckelmann struct batadv_neigh_node *router; 15389652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo; 15456303d34SSven Eckelmann struct batadv_gw_node *gw_node, *curr_gw = NULL; 1554f248cffSSven Eckelmann u64 max_gw_factor = 0; 1564f248cffSSven Eckelmann u64 tmp_gw_factor = 0; 1576b5e971aSSven Eckelmann u8 max_tq = 0; 1586b5e971aSSven Eckelmann u8 tq_avg; 15956303d34SSven Eckelmann struct batadv_orig_node *orig_node; 160c6c8fea2SSven Eckelmann 161c6c8fea2SSven Eckelmann rcu_read_lock(); 162b67bfe0dSSasha Levin hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 16384d5e5e0SSven Eckelmann orig_node = gw_node->orig_node; 1647351a482SSimon Wunderlich router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT); 165e1a5382fSLinus Lüssing if (!router) 166c6c8fea2SSven Eckelmann continue; 167c6c8fea2SSven Eckelmann 16889652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, 16989652331SSimon Wunderlich BATADV_IF_DEFAULT); 17089652331SSimon Wunderlich if (!router_ifinfo) 17189652331SSimon Wunderlich goto next; 17289652331SSimon Wunderlich 1732265c141SAntonio Quartulli if (!atomic_inc_not_zero(&gw_node->refcount)) 1742265c141SAntonio Quartulli goto next; 1752265c141SAntonio Quartulli 17689652331SSimon Wunderlich tq_avg = router_ifinfo->bat_iv.tq_avg; 177c67893d1SSven Eckelmann 178c6c8fea2SSven Eckelmann switch (atomic_read(&bat_priv->gw_sel_class)) { 179c6c8fea2SSven Eckelmann case 1: /* fast connection */ 180414254e3SMarek Lindner tmp_gw_factor = tq_avg * tq_avg; 181414254e3SMarek Lindner tmp_gw_factor *= gw_node->bandwidth_down; 182414254e3SMarek Lindner tmp_gw_factor *= 100 * 100; 183e071d93eSSven Eckelmann tmp_gw_factor >>= 18; 184c6c8fea2SSven Eckelmann 185c6c8fea2SSven Eckelmann if ((tmp_gw_factor > max_gw_factor) || 186c6c8fea2SSven Eckelmann ((tmp_gw_factor == max_gw_factor) && 187c67893d1SSven Eckelmann (tq_avg > max_tq))) { 1882265c141SAntonio Quartulli if (curr_gw) 1891409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 1902265c141SAntonio Quartulli curr_gw = gw_node; 1912265c141SAntonio Quartulli atomic_inc(&curr_gw->refcount); 1922265c141SAntonio Quartulli } 193c6c8fea2SSven Eckelmann break; 194c6c8fea2SSven Eckelmann 1959cfc7bd6SSven Eckelmann default: /* 2: stable connection (use best statistic) 196c6c8fea2SSven Eckelmann * 3: fast-switch (use best statistic but change as 197c6c8fea2SSven Eckelmann * soon as a better gateway appears) 198c6c8fea2SSven Eckelmann * XX: late-switch (use best statistic but change as 199c6c8fea2SSven Eckelmann * soon as a better gateway appears which has 200c6c8fea2SSven Eckelmann * $routing_class more tq points) 2019cfc7bd6SSven Eckelmann */ 202c67893d1SSven Eckelmann if (tq_avg > max_tq) { 2032265c141SAntonio Quartulli if (curr_gw) 2041409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 2052265c141SAntonio Quartulli curr_gw = gw_node; 2062265c141SAntonio Quartulli atomic_inc(&curr_gw->refcount); 2072265c141SAntonio Quartulli } 208c6c8fea2SSven Eckelmann break; 209c6c8fea2SSven Eckelmann } 210c6c8fea2SSven Eckelmann 211c67893d1SSven Eckelmann if (tq_avg > max_tq) 212c67893d1SSven Eckelmann max_tq = tq_avg; 213c6c8fea2SSven Eckelmann 214c6c8fea2SSven Eckelmann if (tmp_gw_factor > max_gw_factor) 215c6c8fea2SSven Eckelmann max_gw_factor = tmp_gw_factor; 216e1a5382fSLinus Lüssing 2171409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 2182265c141SAntonio Quartulli 2192265c141SAntonio Quartulli next: 2207d211efcSSven Eckelmann batadv_neigh_node_free_ref(router); 22189652331SSimon Wunderlich if (router_ifinfo) 22289652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_ifinfo); 223c6c8fea2SSven Eckelmann } 2242265c141SAntonio Quartulli rcu_read_unlock(); 225c6c8fea2SSven Eckelmann 2262265c141SAntonio Quartulli return curr_gw; 2272265c141SAntonio Quartulli } 228e1a5382fSLinus Lüssing 229c6eaa3f0SAntonio Quartulli /** 230c6eaa3f0SAntonio Quartulli * batadv_gw_check_client_stop - check if client mode has been switched off 231c6eaa3f0SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 232c6eaa3f0SAntonio Quartulli * 233c6eaa3f0SAntonio Quartulli * This function assumes the caller has checked that the gw state *is actually 234c6eaa3f0SAntonio Quartulli * changing*. This function is not supposed to be called when there is no state 235c6eaa3f0SAntonio Quartulli * change. 236c6eaa3f0SAntonio Quartulli */ 237c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv) 238c6eaa3f0SAntonio Quartulli { 239c6eaa3f0SAntonio Quartulli struct batadv_gw_node *curr_gw; 240c6eaa3f0SAntonio Quartulli 241c6eaa3f0SAntonio Quartulli if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT) 242c6eaa3f0SAntonio Quartulli return; 243c6eaa3f0SAntonio Quartulli 244c6eaa3f0SAntonio Quartulli curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 245c6eaa3f0SAntonio Quartulli if (!curr_gw) 246c6eaa3f0SAntonio Quartulli return; 247c6eaa3f0SAntonio Quartulli 248f3163181SAntonio Quartulli /* deselect the current gateway so that next time that client mode is 249f3163181SAntonio Quartulli * enabled a proper GW_ADD event can be sent 250f3163181SAntonio Quartulli */ 251f3163181SAntonio Quartulli batadv_gw_select(bat_priv, NULL); 252f3163181SAntonio Quartulli 253c6eaa3f0SAntonio Quartulli /* if batman-adv is switching the gw client mode off and a gateway was 254c6eaa3f0SAntonio Quartulli * already selected, send a DEL uevent 255c6eaa3f0SAntonio Quartulli */ 256c6eaa3f0SAntonio Quartulli batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL); 257c6eaa3f0SAntonio Quartulli 258c6eaa3f0SAntonio Quartulli batadv_gw_node_free_ref(curr_gw); 259c6eaa3f0SAntonio Quartulli } 260c6eaa3f0SAntonio Quartulli 26156303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv) 2622265c141SAntonio Quartulli { 2634f248cffSSven Eckelmann struct batadv_gw_node *curr_gw = NULL; 2644f248cffSSven Eckelmann struct batadv_gw_node *next_gw = NULL; 26556303d34SSven Eckelmann struct batadv_neigh_node *router = NULL; 26689652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo = NULL; 26719595e05SAntonio Quartulli char gw_addr[18] = { '\0' }; 2682265c141SAntonio Quartulli 269cd646ab1SSven Eckelmann if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT) 2702265c141SAntonio Quartulli goto out; 2712265c141SAntonio Quartulli 2721409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 2732265c141SAntonio Quartulli 274807736f6SSven Eckelmann if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw) 275caa0bf64SMarek Lindner goto out; 276caa0bf64SMarek Lindner 2771409a834SSven Eckelmann next_gw = batadv_gw_get_best_gw_node(bat_priv); 2782265c141SAntonio Quartulli 2792265c141SAntonio Quartulli if (curr_gw == next_gw) 2802265c141SAntonio Quartulli goto out; 2812265c141SAntonio Quartulli 2822265c141SAntonio Quartulli if (next_gw) { 28319595e05SAntonio Quartulli sprintf(gw_addr, "%pM", next_gw->orig_node->orig); 28419595e05SAntonio Quartulli 2857351a482SSimon Wunderlich router = batadv_orig_router_get(next_gw->orig_node, 2867351a482SSimon Wunderlich BATADV_IF_DEFAULT); 2872265c141SAntonio Quartulli if (!router) { 2884e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 2892265c141SAntonio Quartulli goto out; 2902265c141SAntonio Quartulli } 29189652331SSimon Wunderlich 29289652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, 29389652331SSimon Wunderlich BATADV_IF_DEFAULT); 29489652331SSimon Wunderlich if (!router_ifinfo) { 29589652331SSimon Wunderlich batadv_gw_reselect(bat_priv); 29689652331SSimon Wunderlich goto out; 29789652331SSimon Wunderlich } 2982265c141SAntonio Quartulli } 2992265c141SAntonio Quartulli 3002265c141SAntonio Quartulli if ((curr_gw) && (!next_gw)) { 30139c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 3022265c141SAntonio Quartulli "Removing selected gateway - no gateway in range\n"); 30339c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, 30439c75a51SSven Eckelmann NULL); 3052265c141SAntonio Quartulli } else if ((!curr_gw) && (next_gw)) { 30639c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 307414254e3SMarek Lindner "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 3081eda58bfSSven Eckelmann next_gw->orig_node->orig, 309414254e3SMarek Lindner next_gw->bandwidth_down / 10, 310414254e3SMarek Lindner next_gw->bandwidth_down % 10, 311414254e3SMarek Lindner next_gw->bandwidth_up / 10, 31289652331SSimon Wunderlich next_gw->bandwidth_up % 10, 31389652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg); 31439c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD, 31539c75a51SSven Eckelmann gw_addr); 3162265c141SAntonio Quartulli } else { 31739c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 318414254e3SMarek Lindner "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 3191eda58bfSSven Eckelmann next_gw->orig_node->orig, 320414254e3SMarek Lindner next_gw->bandwidth_down / 10, 321414254e3SMarek Lindner next_gw->bandwidth_down % 10, 322414254e3SMarek Lindner next_gw->bandwidth_up / 10, 32389652331SSimon Wunderlich next_gw->bandwidth_up % 10, 32489652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg); 32539c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE, 32639c75a51SSven Eckelmann gw_addr); 327c6c8fea2SSven Eckelmann } 328c6c8fea2SSven Eckelmann 3291409a834SSven Eckelmann batadv_gw_select(bat_priv, next_gw); 3302265c141SAntonio Quartulli 331c4aac1abSMarek Lindner out: 332c4aac1abSMarek Lindner if (curr_gw) 3331409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 3342265c141SAntonio Quartulli if (next_gw) 3351409a834SSven Eckelmann batadv_gw_node_free_ref(next_gw); 3362265c141SAntonio Quartulli if (router) 3377d211efcSSven Eckelmann batadv_neigh_node_free_ref(router); 33889652331SSimon Wunderlich if (router_ifinfo) 33989652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_ifinfo); 340c6c8fea2SSven Eckelmann } 341c6c8fea2SSven Eckelmann 34256303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv, 34356303d34SSven Eckelmann struct batadv_orig_node *orig_node) 344c6c8fea2SSven Eckelmann { 34589652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_orig_tq = NULL; 34689652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_gw_tq = NULL; 34756303d34SSven Eckelmann struct batadv_orig_node *curr_gw_orig; 3484f248cffSSven Eckelmann struct batadv_neigh_node *router_gw = NULL; 3494f248cffSSven Eckelmann struct batadv_neigh_node *router_orig = NULL; 3506b5e971aSSven Eckelmann u8 gw_tq_avg, orig_tq_avg; 351c6c8fea2SSven Eckelmann 3527cf06bc6SSven Eckelmann curr_gw_orig = batadv_gw_get_selected_orig(bat_priv); 35357f0c07cSLinus Lüssing if (!curr_gw_orig) 3544e820e72SAntonio Quartulli goto reselect; 355c6c8fea2SSven Eckelmann 3567351a482SSimon Wunderlich router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT); 357e1a5382fSLinus Lüssing if (!router_gw) 3584e820e72SAntonio Quartulli goto reselect; 359c6c8fea2SSven Eckelmann 36089652331SSimon Wunderlich router_gw_tq = batadv_neigh_ifinfo_get(router_gw, 36189652331SSimon Wunderlich BATADV_IF_DEFAULT); 36289652331SSimon Wunderlich if (!router_gw_tq) 36389652331SSimon Wunderlich goto reselect; 36489652331SSimon Wunderlich 365c6c8fea2SSven Eckelmann /* this node already is the gateway */ 36657f0c07cSLinus Lüssing if (curr_gw_orig == orig_node) 367e1a5382fSLinus Lüssing goto out; 368c6c8fea2SSven Eckelmann 3697351a482SSimon Wunderlich router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT); 370e1a5382fSLinus Lüssing if (!router_orig) 371e1a5382fSLinus Lüssing goto out; 372c6c8fea2SSven Eckelmann 37389652331SSimon Wunderlich router_orig_tq = batadv_neigh_ifinfo_get(router_orig, 37489652331SSimon Wunderlich BATADV_IF_DEFAULT); 37589652331SSimon Wunderlich if (!router_orig_tq) 37689652331SSimon Wunderlich goto out; 37789652331SSimon Wunderlich 37889652331SSimon Wunderlich gw_tq_avg = router_gw_tq->bat_iv.tq_avg; 37989652331SSimon Wunderlich orig_tq_avg = router_orig_tq->bat_iv.tq_avg; 380c6c8fea2SSven Eckelmann 381c6c8fea2SSven Eckelmann /* the TQ value has to be better */ 382c6c8fea2SSven Eckelmann if (orig_tq_avg < gw_tq_avg) 3835d02b3cdSLinus Lüssing goto out; 384c6c8fea2SSven Eckelmann 3859cfc7bd6SSven Eckelmann /* if the routing class is greater than 3 the value tells us how much 386c6c8fea2SSven Eckelmann * greater the TQ value of the new gateway must be 3879cfc7bd6SSven Eckelmann */ 388c6c8fea2SSven Eckelmann if ((atomic_read(&bat_priv->gw_sel_class) > 3) && 389c6c8fea2SSven Eckelmann (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class))) 3905d02b3cdSLinus Lüssing goto out; 391c6c8fea2SSven Eckelmann 39239c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 39386ceb360SSven Eckelmann "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n", 394c6c8fea2SSven Eckelmann gw_tq_avg, orig_tq_avg); 395c6c8fea2SSven Eckelmann 3964e820e72SAntonio Quartulli reselect: 3974e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 3985d02b3cdSLinus Lüssing out: 39957f0c07cSLinus Lüssing if (curr_gw_orig) 4007d211efcSSven Eckelmann batadv_orig_node_free_ref(curr_gw_orig); 401e1a5382fSLinus Lüssing if (router_gw) 4027d211efcSSven Eckelmann batadv_neigh_node_free_ref(router_gw); 403e1a5382fSLinus Lüssing if (router_orig) 4047d211efcSSven Eckelmann batadv_neigh_node_free_ref(router_orig); 40589652331SSimon Wunderlich if (router_gw_tq) 40689652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_gw_tq); 40789652331SSimon Wunderlich if (router_orig_tq) 40889652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_orig_tq); 409c6c8fea2SSven Eckelmann } 410c6c8fea2SSven Eckelmann 411414254e3SMarek Lindner /** 412414254e3SMarek Lindner * batadv_gw_node_add - add gateway node to list of available gateways 413414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 414414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 415414254e3SMarek Lindner * @gateway: announced bandwidth information 416414254e3SMarek Lindner */ 41756303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv, 41856303d34SSven Eckelmann struct batadv_orig_node *orig_node, 419414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 420c6c8fea2SSven Eckelmann { 42156303d34SSven Eckelmann struct batadv_gw_node *gw_node; 422414254e3SMarek Lindner 423414254e3SMarek Lindner if (gateway->bandwidth_down == 0) 424414254e3SMarek Lindner return; 425c6c8fea2SSven Eckelmann 426377fe0f9SAntonio Quartulli if (!atomic_inc_not_zero(&orig_node->refcount)) 427c6c8fea2SSven Eckelmann return; 428c6c8fea2SSven Eckelmann 429377fe0f9SAntonio Quartulli gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); 430377fe0f9SAntonio Quartulli if (!gw_node) { 431377fe0f9SAntonio Quartulli batadv_orig_node_free_ref(orig_node); 432377fe0f9SAntonio Quartulli return; 433377fe0f9SAntonio Quartulli } 434377fe0f9SAntonio Quartulli 435c6c8fea2SSven Eckelmann INIT_HLIST_NODE(&gw_node->list); 436c6c8fea2SSven Eckelmann gw_node->orig_node = orig_node; 43727a4d5efSSimon Wunderlich gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 43827a4d5efSSimon Wunderlich gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 43925b6d3c1SMarek Lindner atomic_set(&gw_node->refcount, 1); 440c6c8fea2SSven Eckelmann 441807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 442807736f6SSven Eckelmann hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list); 443807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 444c6c8fea2SSven Eckelmann 44539c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 446414254e3SMarek Lindner "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", 447414254e3SMarek Lindner orig_node->orig, 448414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 449414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 450414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 451414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 452c6c8fea2SSven Eckelmann } 453c6c8fea2SSven Eckelmann 454414254e3SMarek Lindner /** 455414254e3SMarek Lindner * batadv_gw_node_get - retrieve gateway node from list of available gateways 456414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 457414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 458414254e3SMarek Lindner * 459414254e3SMarek Lindner * Returns gateway node if found or NULL otherwise. 46071e4aa9cSAntonio Quartulli */ 461414254e3SMarek Lindner static struct batadv_gw_node * 462414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv, 463414254e3SMarek Lindner struct batadv_orig_node *orig_node) 464414254e3SMarek Lindner { 465414254e3SMarek Lindner struct batadv_gw_node *gw_node_tmp, *gw_node = NULL; 466c6c8fea2SSven Eckelmann 467c6c8fea2SSven Eckelmann rcu_read_lock(); 468414254e3SMarek Lindner hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) { 469414254e3SMarek Lindner if (gw_node_tmp->orig_node != orig_node) 470c6c8fea2SSven Eckelmann continue; 471c6c8fea2SSven Eckelmann 472414254e3SMarek Lindner if (!atomic_inc_not_zero(&gw_node_tmp->refcount)) 473414254e3SMarek Lindner continue; 474414254e3SMarek Lindner 475414254e3SMarek Lindner gw_node = gw_node_tmp; 476414254e3SMarek Lindner break; 477414254e3SMarek Lindner } 478414254e3SMarek Lindner rcu_read_unlock(); 479414254e3SMarek Lindner 480414254e3SMarek Lindner return gw_node; 481414254e3SMarek Lindner } 482414254e3SMarek Lindner 483414254e3SMarek Lindner /** 484414254e3SMarek Lindner * batadv_gw_node_update - update list of available gateways with changed 485414254e3SMarek Lindner * bandwidth information 486414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 487414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 488414254e3SMarek Lindner * @gateway: announced bandwidth information 489414254e3SMarek Lindner */ 490414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv, 491414254e3SMarek Lindner struct batadv_orig_node *orig_node, 492414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 493414254e3SMarek Lindner { 494414254e3SMarek Lindner struct batadv_gw_node *gw_node, *curr_gw = NULL; 495414254e3SMarek Lindner 496414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_node); 497414254e3SMarek Lindner if (!gw_node) { 498414254e3SMarek Lindner batadv_gw_node_add(bat_priv, orig_node, gateway); 499414254e3SMarek Lindner goto out; 500414254e3SMarek Lindner } 501414254e3SMarek Lindner 502414254e3SMarek Lindner if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) && 503414254e3SMarek Lindner (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))) 504414254e3SMarek Lindner goto out; 505414254e3SMarek Lindner 50639c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 507414254e3SMarek Lindner "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n", 508414254e3SMarek Lindner orig_node->orig, 509414254e3SMarek Lindner gw_node->bandwidth_down / 10, 510414254e3SMarek Lindner gw_node->bandwidth_down % 10, 511414254e3SMarek Lindner gw_node->bandwidth_up / 10, 512414254e3SMarek Lindner gw_node->bandwidth_up % 10, 513414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 514414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 515414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 516414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 517414254e3SMarek Lindner 518414254e3SMarek Lindner gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 519414254e3SMarek Lindner gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 520c6c8fea2SSven Eckelmann 521414254e3SMarek Lindner if (ntohl(gateway->bandwidth_down) == 0) { 52239c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 523c6c8fea2SSven Eckelmann "Gateway %pM removed from gateway list\n", 524c6c8fea2SSven Eckelmann orig_node->orig); 525c6c8fea2SSven Eckelmann 526414254e3SMarek Lindner /* Note: We don't need a NULL check here, since curr_gw never 527414254e3SMarek Lindner * gets dereferenced. 528414254e3SMarek Lindner */ 529bd3524c1SSimon Wunderlich spin_lock_bh(&bat_priv->gw.list_lock); 530*c18bdd01SSven Eckelmann if (!hlist_unhashed(&gw_node->list)) { 531bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list); 532bd3524c1SSimon Wunderlich batadv_gw_node_free_ref(gw_node); 533*c18bdd01SSven Eckelmann } 534*c18bdd01SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 535bd3524c1SSimon Wunderlich 536414254e3SMarek Lindner curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 537c4aac1abSMarek Lindner if (gw_node == curr_gw) 5384e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 539bd3524c1SSimon Wunderlich 540bd3524c1SSimon Wunderlich if (curr_gw) 541bd3524c1SSimon Wunderlich batadv_gw_node_free_ref(curr_gw); 542414254e3SMarek Lindner } 54371e4aa9cSAntonio Quartulli 544414254e3SMarek Lindner out: 545414254e3SMarek Lindner if (gw_node) 546414254e3SMarek Lindner batadv_gw_node_free_ref(gw_node); 547c6c8fea2SSven Eckelmann } 548c6c8fea2SSven Eckelmann 54956303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv, 55056303d34SSven Eckelmann struct batadv_orig_node *orig_node) 551c6c8fea2SSven Eckelmann { 552414254e3SMarek Lindner struct batadv_tvlv_gateway_data gateway; 553414254e3SMarek Lindner 554414254e3SMarek Lindner gateway.bandwidth_down = 0; 555414254e3SMarek Lindner gateway.bandwidth_up = 0; 556414254e3SMarek Lindner 557414254e3SMarek Lindner batadv_gw_node_update(bat_priv, orig_node, &gateway); 558c6c8fea2SSven Eckelmann } 559c6c8fea2SSven Eckelmann 560bd3524c1SSimon Wunderlich void batadv_gw_node_free(struct batadv_priv *bat_priv) 561c6c8fea2SSven Eckelmann { 562bd3524c1SSimon Wunderlich struct batadv_gw_node *gw_node; 563b67bfe0dSSasha Levin struct hlist_node *node_tmp; 564c6c8fea2SSven Eckelmann 565807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 566b67bfe0dSSasha Levin hlist_for_each_entry_safe(gw_node, node_tmp, 567807736f6SSven Eckelmann &bat_priv->gw.list, list) { 568bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list); 5691409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 570c6c8fea2SSven Eckelmann } 571807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 572c6c8fea2SSven Eckelmann } 573c6c8fea2SSven Eckelmann 5749cfc7bd6SSven Eckelmann /* fails if orig_node has no router */ 57556303d34SSven Eckelmann static int batadv_write_buffer_text(struct batadv_priv *bat_priv, 5761409a834SSven Eckelmann struct seq_file *seq, 57756303d34SSven Eckelmann const struct batadv_gw_node *gw_node) 578c6c8fea2SSven Eckelmann { 57956303d34SSven Eckelmann struct batadv_gw_node *curr_gw; 58056303d34SSven Eckelmann struct batadv_neigh_node *router; 58189652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo = NULL; 582414254e3SMarek Lindner int ret = -1; 583c6c8fea2SSven Eckelmann 5847351a482SSimon Wunderlich router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT); 585e1a5382fSLinus Lüssing if (!router) 586e1a5382fSLinus Lüssing goto out; 587e1a5382fSLinus Lüssing 58889652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT); 58989652331SSimon Wunderlich if (!router_ifinfo) 59089652331SSimon Wunderlich goto out; 59189652331SSimon Wunderlich 5921409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 5935d02b3cdSLinus Lüssing 5946d91147dSJoe Perches seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n", 5955d02b3cdSLinus Lüssing (curr_gw == gw_node ? "=>" : " "), 596c6c8fea2SSven Eckelmann gw_node->orig_node->orig, 59789652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg, router->addr, 598e1a5382fSLinus Lüssing router->if_incoming->net_dev->name, 599414254e3SMarek Lindner gw_node->bandwidth_down / 10, 600414254e3SMarek Lindner gw_node->bandwidth_down % 10, 601414254e3SMarek Lindner gw_node->bandwidth_up / 10, 602414254e3SMarek Lindner gw_node->bandwidth_up % 10); 60392b83917SJoe Perches ret = seq_has_overflowed(seq) ? -1 : 0; 6045d02b3cdSLinus Lüssing 605c4aac1abSMarek Lindner if (curr_gw) 6061409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 607e1a5382fSLinus Lüssing out: 60889652331SSimon Wunderlich if (router_ifinfo) 60989652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_ifinfo); 61089652331SSimon Wunderlich if (router) 61189652331SSimon Wunderlich batadv_neigh_node_free_ref(router); 6125d02b3cdSLinus Lüssing return ret; 613c6c8fea2SSven Eckelmann } 614c6c8fea2SSven Eckelmann 6157cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset) 616c6c8fea2SSven Eckelmann { 617c6c8fea2SSven Eckelmann struct net_device *net_dev = (struct net_device *)seq->private; 61856303d34SSven Eckelmann struct batadv_priv *bat_priv = netdev_priv(net_dev); 61956303d34SSven Eckelmann struct batadv_hard_iface *primary_if; 62056303d34SSven Eckelmann struct batadv_gw_node *gw_node; 62130da63a6SMarek Lindner int gw_count = 0; 622c6c8fea2SSven Eckelmann 62330da63a6SMarek Lindner primary_if = batadv_seq_print_text_primary_if_get(seq); 62430da63a6SMarek Lindner if (!primary_if) 62532ae9b22SMarek Lindner goto out; 626c6c8fea2SSven Eckelmann 62786ceb360SSven Eckelmann seq_printf(seq, 628414254e3SMarek Lindner " %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", 62942d0b044SSven Eckelmann "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF", 63042d0b044SSven Eckelmann BATADV_SOURCE_VERSION, primary_if->net_dev->name, 63132ae9b22SMarek Lindner primary_if->net_dev->dev_addr, net_dev->name); 632c6c8fea2SSven Eckelmann 633c6c8fea2SSven Eckelmann rcu_read_lock(); 634b67bfe0dSSasha Levin hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 635e1a5382fSLinus Lüssing /* fails if orig_node has no router */ 6361409a834SSven Eckelmann if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0) 637c6c8fea2SSven Eckelmann continue; 638c6c8fea2SSven Eckelmann 639c6c8fea2SSven Eckelmann gw_count++; 640c6c8fea2SSven Eckelmann } 641c6c8fea2SSven Eckelmann rcu_read_unlock(); 642c6c8fea2SSven Eckelmann 643c6c8fea2SSven Eckelmann if (gw_count == 0) 6440c814653SAntonio Quartulli seq_puts(seq, "No gateways in range ...\n"); 645c6c8fea2SSven Eckelmann 64632ae9b22SMarek Lindner out: 64732ae9b22SMarek Lindner if (primary_if) 648e5d89254SSven Eckelmann batadv_hardif_free_ref(primary_if); 64930da63a6SMarek Lindner return 0; 650c6c8fea2SSven Eckelmann } 651c6c8fea2SSven Eckelmann 6526c413b1cSAntonio Quartulli /** 6536c413b1cSAntonio Quartulli * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message 6546c413b1cSAntonio Quartulli * @skb: the packet to check 6556c413b1cSAntonio Quartulli * @header_len: a pointer to the batman-adv header size 6566c413b1cSAntonio Quartulli * @chaddr: buffer where the client address will be stored. Valid 6576c413b1cSAntonio Quartulli * only if the function returns BATADV_DHCP_TO_CLIENT 6586c413b1cSAntonio Quartulli * 6596c413b1cSAntonio Quartulli * Returns: 6606c413b1cSAntonio Quartulli * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error 6616c413b1cSAntonio Quartulli * while parsing it 6626c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server 6636c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client 6646c413b1cSAntonio Quartulli * 6656c413b1cSAntonio Quartulli * This function may re-allocate the data buffer of the skb passed as argument. 6669cfc7bd6SSven Eckelmann */ 6676c413b1cSAntonio Quartulli enum batadv_dhcp_recipient 6686c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len, 6696b5e971aSSven Eckelmann u8 *chaddr) 670c6c8fea2SSven Eckelmann { 6716c413b1cSAntonio Quartulli enum batadv_dhcp_recipient ret = BATADV_DHCP_NO; 672c6c8fea2SSven Eckelmann struct ethhdr *ethhdr; 673c6c8fea2SSven Eckelmann struct iphdr *iphdr; 674c6c8fea2SSven Eckelmann struct ipv6hdr *ipv6hdr; 675c6c8fea2SSven Eckelmann struct udphdr *udphdr; 676f7f8ed56SAntonio Quartulli struct vlan_ethhdr *vhdr; 6776c413b1cSAntonio Quartulli int chaddr_offset; 678f7f8ed56SAntonio Quartulli __be16 proto; 6796b5e971aSSven Eckelmann u8 *p; 680c6c8fea2SSven Eckelmann 681c6c8fea2SSven Eckelmann /* check for ethernet header */ 682be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) 6836c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 6846c413b1cSAntonio Quartulli 685927c2ed7SLinus Lüssing ethhdr = eth_hdr(skb); 686f7f8ed56SAntonio Quartulli proto = ethhdr->h_proto; 687be7af5cfSMarek Lindner *header_len += ETH_HLEN; 688c6c8fea2SSven Eckelmann 689c6c8fea2SSven Eckelmann /* check for initial vlan header */ 690f7f8ed56SAntonio Quartulli if (proto == htons(ETH_P_8021Q)) { 691be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) 6926c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 693f7f8ed56SAntonio Quartulli 694927c2ed7SLinus Lüssing vhdr = vlan_eth_hdr(skb); 695f7f8ed56SAntonio Quartulli proto = vhdr->h_vlan_encapsulated_proto; 696be7af5cfSMarek Lindner *header_len += VLAN_HLEN; 697c6c8fea2SSven Eckelmann } 698c6c8fea2SSven Eckelmann 699c6c8fea2SSven Eckelmann /* check for ip header */ 700f7f8ed56SAntonio Quartulli switch (proto) { 701f7f8ed56SAntonio Quartulli case htons(ETH_P_IP): 702be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) 7036c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7046c413b1cSAntonio Quartulli 705be7af5cfSMarek Lindner iphdr = (struct iphdr *)(skb->data + *header_len); 706be7af5cfSMarek Lindner *header_len += iphdr->ihl * 4; 707c6c8fea2SSven Eckelmann 708c6c8fea2SSven Eckelmann /* check for udp header */ 709c6c8fea2SSven Eckelmann if (iphdr->protocol != IPPROTO_UDP) 7106c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 711c6c8fea2SSven Eckelmann 712c6c8fea2SSven Eckelmann break; 713f7f8ed56SAntonio Quartulli case htons(ETH_P_IPV6): 714be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) 7156c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7166c413b1cSAntonio Quartulli 717be7af5cfSMarek Lindner ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); 718be7af5cfSMarek Lindner *header_len += sizeof(*ipv6hdr); 719c6c8fea2SSven Eckelmann 720c6c8fea2SSven Eckelmann /* check for udp header */ 721c6c8fea2SSven Eckelmann if (ipv6hdr->nexthdr != IPPROTO_UDP) 7226c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 723c6c8fea2SSven Eckelmann 724c6c8fea2SSven Eckelmann break; 725c6c8fea2SSven Eckelmann default: 7266c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 727c6c8fea2SSven Eckelmann } 728c6c8fea2SSven Eckelmann 729be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr))) 7306c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7319d2c9488SLinus Lüssing 732be7af5cfSMarek Lindner udphdr = (struct udphdr *)(skb->data + *header_len); 733be7af5cfSMarek Lindner *header_len += sizeof(*udphdr); 734c6c8fea2SSven Eckelmann 735c6c8fea2SSven Eckelmann /* check for bootp port */ 7366c413b1cSAntonio Quartulli switch (proto) { 7376c413b1cSAntonio Quartulli case htons(ETH_P_IP): 7386c413b1cSAntonio Quartulli if (udphdr->dest == htons(67)) 7396c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 7406c413b1cSAntonio Quartulli else if (udphdr->source == htons(67)) 7416c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 7426c413b1cSAntonio Quartulli break; 7436c413b1cSAntonio Quartulli case htons(ETH_P_IPV6): 7446c413b1cSAntonio Quartulli if (udphdr->dest == htons(547)) 7456c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 7466c413b1cSAntonio Quartulli else if (udphdr->source == htons(547)) 7476c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 7486c413b1cSAntonio Quartulli break; 749be7af5cfSMarek Lindner } 750c6c8fea2SSven Eckelmann 7516c413b1cSAntonio Quartulli chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET; 7526c413b1cSAntonio Quartulli /* store the client address if the message is going to a client */ 7536c413b1cSAntonio Quartulli if (ret == BATADV_DHCP_TO_CLIENT && 7546c413b1cSAntonio Quartulli pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) { 7556c413b1cSAntonio Quartulli /* check if the DHCP packet carries an Ethernet DHCP */ 7566c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET; 7576c413b1cSAntonio Quartulli if (*p != BATADV_DHCP_HTYPE_ETHERNET) 7586c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7596c413b1cSAntonio Quartulli 7606c413b1cSAntonio Quartulli /* check if the DHCP packet carries a valid Ethernet address */ 7616c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET; 7626c413b1cSAntonio Quartulli if (*p != ETH_ALEN) 7636c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7646c413b1cSAntonio Quartulli 7658fdd0153SAntonio Quartulli ether_addr_copy(chaddr, skb->data + chaddr_offset); 7666c413b1cSAntonio Quartulli } 7676c413b1cSAntonio Quartulli 7686c413b1cSAntonio Quartulli return ret; 7696c413b1cSAntonio Quartulli } 770aa143d28SAntonio Quartulli 771bbb877edSAntonio Quartulli /** 772bbb877edSAntonio Quartulli * batadv_gw_out_of_range - check if the dhcp request destination is the best gw 773bbb877edSAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 774bbb877edSAntonio Quartulli * @skb: the outgoing packet 775bbb877edSAntonio Quartulli * 776bbb877edSAntonio Quartulli * Check if the skb is a DHCP request and if it is sent to the current best GW 777bbb877edSAntonio Quartulli * server. Due to topology changes it may be the case that the GW server 778bbb877edSAntonio Quartulli * previously selected is not the best one anymore. 779bbb877edSAntonio Quartulli * 780bbb877edSAntonio Quartulli * Returns true if the packet destination is unicast and it is not the best gw, 781bbb877edSAntonio Quartulli * false otherwise. 782bbb877edSAntonio Quartulli * 783bbb877edSAntonio Quartulli * This call might reallocate skb data. 7846c413b1cSAntonio Quartulli * Must be invoked only when the DHCP packet is going TO a DHCP SERVER. 785bbb877edSAntonio Quartulli */ 78656303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, 7879d2c9488SLinus Lüssing struct sk_buff *skb) 788be7af5cfSMarek Lindner { 7894f248cffSSven Eckelmann struct batadv_neigh_node *neigh_curr = NULL; 7904f248cffSSven Eckelmann struct batadv_neigh_node *neigh_old = NULL; 79156303d34SSven Eckelmann struct batadv_orig_node *orig_dst_node = NULL; 7924f248cffSSven Eckelmann struct batadv_gw_node *gw_node = NULL; 7934f248cffSSven Eckelmann struct batadv_gw_node *curr_gw = NULL; 79489652331SSimon Wunderlich struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo; 7956c413b1cSAntonio Quartulli struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 7966c413b1cSAntonio Quartulli bool out_of_range = false; 7976b5e971aSSven Eckelmann u8 curr_tq_avg; 798bbb877edSAntonio Quartulli unsigned short vid; 799bbb877edSAntonio Quartulli 800bbb877edSAntonio Quartulli vid = batadv_get_vid(skb, 0); 801be7af5cfSMarek Lindner 80208c36d3eSSven Eckelmann orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, 803bbb877edSAntonio Quartulli ethhdr->h_dest, vid); 804be7af5cfSMarek Lindner if (!orig_dst_node) 805be7af5cfSMarek Lindner goto out; 806be7af5cfSMarek Lindner 807414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_dst_node); 8080d164491SAntonio Quartulli if (!gw_node) 809be7af5cfSMarek Lindner goto out; 810be7af5cfSMarek Lindner 811be7af5cfSMarek Lindner switch (atomic_read(&bat_priv->gw_mode)) { 812cd646ab1SSven Eckelmann case BATADV_GW_MODE_SERVER: 813be7af5cfSMarek Lindner /* If we are a GW then we are our best GW. We can artificially 8149cfc7bd6SSven Eckelmann * set the tq towards ourself as the maximum value 8159cfc7bd6SSven Eckelmann */ 81642d0b044SSven Eckelmann curr_tq_avg = BATADV_TQ_MAX_VALUE; 817be7af5cfSMarek Lindner break; 818cd646ab1SSven Eckelmann case BATADV_GW_MODE_CLIENT: 8191409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 820c4aac1abSMarek Lindner if (!curr_gw) 821be7af5cfSMarek Lindner goto out; 822c6c8fea2SSven Eckelmann 823be7af5cfSMarek Lindner /* packet is going to our gateway */ 824be7af5cfSMarek Lindner if (curr_gw->orig_node == orig_dst_node) 825be7af5cfSMarek Lindner goto out; 826be7af5cfSMarek Lindner 82743676ab5SAntonio Quartulli /* If the dhcp packet has been sent to a different gw, 82843676ab5SAntonio Quartulli * we have to evaluate whether the old gw is still 8299cfc7bd6SSven Eckelmann * reliable enough 8309cfc7bd6SSven Eckelmann */ 83130d3c511SSven Eckelmann neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node, 83230d3c511SSven Eckelmann NULL); 833be7af5cfSMarek Lindner if (!neigh_curr) 834be7af5cfSMarek Lindner goto out; 835be7af5cfSMarek Lindner 83689652331SSimon Wunderlich curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr, 83789652331SSimon Wunderlich BATADV_IF_DEFAULT); 83889652331SSimon Wunderlich if (!curr_ifinfo) 83989652331SSimon Wunderlich goto out; 84089652331SSimon Wunderlich 84189652331SSimon Wunderlich curr_tq_avg = curr_ifinfo->bat_iv.tq_avg; 84289652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(curr_ifinfo); 84389652331SSimon Wunderlich 844be7af5cfSMarek Lindner break; 845cd646ab1SSven Eckelmann case BATADV_GW_MODE_OFF: 846be7af5cfSMarek Lindner default: 847be7af5cfSMarek Lindner goto out; 84843676ab5SAntonio Quartulli } 849be7af5cfSMarek Lindner 85030d3c511SSven Eckelmann neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL); 8512ef04f47SDan Carpenter if (!neigh_old) 852be7af5cfSMarek Lindner goto out; 853be7af5cfSMarek Lindner 85489652331SSimon Wunderlich old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT); 85589652331SSimon Wunderlich if (!old_ifinfo) 85689652331SSimon Wunderlich goto out; 85789652331SSimon Wunderlich 85889652331SSimon Wunderlich if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD) 859be7af5cfSMarek Lindner out_of_range = true; 86089652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(old_ifinfo); 861be7af5cfSMarek Lindner 862be7af5cfSMarek Lindner out: 863be7af5cfSMarek Lindner if (orig_dst_node) 8647d211efcSSven Eckelmann batadv_orig_node_free_ref(orig_dst_node); 865be7af5cfSMarek Lindner if (curr_gw) 8661409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 867414254e3SMarek Lindner if (gw_node) 868414254e3SMarek Lindner batadv_gw_node_free_ref(gw_node); 86943676ab5SAntonio Quartulli if (neigh_old) 8707d211efcSSven Eckelmann batadv_neigh_node_free_ref(neigh_old); 87143676ab5SAntonio Quartulli if (neigh_curr) 8727d211efcSSven Eckelmann batadv_neigh_node_free_ref(neigh_curr); 873be7af5cfSMarek Lindner return out_of_range; 874c6c8fea2SSven Eckelmann } 875