10b873931SAntonio Quartulli /* Copyright (C) 2009-2013 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 "main.h" 19b706b13bSSven Eckelmann #include "sysfs.h" 20c6c8fea2SSven Eckelmann #include "gateway_client.h" 21c6c8fea2SSven Eckelmann #include "gateway_common.h" 22c6c8fea2SSven Eckelmann #include "hard-interface.h" 2357f0c07cSLinus Lüssing #include "originator.h" 24be7af5cfSMarek Lindner #include "translation-table.h" 2543676ab5SAntonio Quartulli #include "routing.h" 26c6c8fea2SSven Eckelmann #include <linux/ip.h> 27c6c8fea2SSven Eckelmann #include <linux/ipv6.h> 28c6c8fea2SSven Eckelmann #include <linux/udp.h> 29c6c8fea2SSven Eckelmann #include <linux/if_vlan.h> 30c6c8fea2SSven Eckelmann 316c413b1cSAntonio Quartulli /* These are the offsets of the "hw type" and "hw address length" in the dhcp 326c413b1cSAntonio Quartulli * packet starting at the beginning of the dhcp header 339cfc7bd6SSven Eckelmann */ 346c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_OFFSET 1 356c413b1cSAntonio Quartulli #define BATADV_DHCP_HLEN_OFFSET 2 366c413b1cSAntonio Quartulli /* Value of htype representing Ethernet */ 376c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_ETHERNET 0x01 386c413b1cSAntonio Quartulli /* This is the offset of the "chaddr" field in the dhcp packet starting at the 396c413b1cSAntonio Quartulli * beginning of the dhcp header 406c413b1cSAntonio Quartulli */ 416c413b1cSAntonio Quartulli #define BATADV_DHCP_CHADDR_OFFSET 28 4243676ab5SAntonio Quartulli 4356303d34SSven Eckelmann static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node) 4425b6d3c1SMarek Lindner { 4525b6d3c1SMarek Lindner if (atomic_dec_and_test(&gw_node->refcount)) 46eb340b2fSPaul E. McKenney kfree_rcu(gw_node, rcu); 47c6c8fea2SSven Eckelmann } 48c6c8fea2SSven Eckelmann 4956303d34SSven Eckelmann static struct batadv_gw_node * 5056303d34SSven Eckelmann batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv) 51c6c8fea2SSven Eckelmann { 5256303d34SSven Eckelmann struct batadv_gw_node *gw_node; 53c6c8fea2SSven Eckelmann 545d02b3cdSLinus Lüssing rcu_read_lock(); 55807736f6SSven Eckelmann gw_node = rcu_dereference(bat_priv->gw.curr_gw); 56c4aac1abSMarek Lindner if (!gw_node) 575d02b3cdSLinus Lüssing goto out; 58c6c8fea2SSven Eckelmann 59c4aac1abSMarek Lindner if (!atomic_inc_not_zero(&gw_node->refcount)) 60c4aac1abSMarek Lindner gw_node = NULL; 61c4aac1abSMarek Lindner 62c4aac1abSMarek Lindner out: 63c4aac1abSMarek Lindner rcu_read_unlock(); 64c4aac1abSMarek Lindner return gw_node; 65c4aac1abSMarek Lindner } 66c4aac1abSMarek Lindner 6756303d34SSven Eckelmann struct batadv_orig_node * 6856303d34SSven Eckelmann batadv_gw_get_selected_orig(struct batadv_priv *bat_priv) 69c4aac1abSMarek Lindner { 7056303d34SSven Eckelmann struct batadv_gw_node *gw_node; 7156303d34SSven Eckelmann struct batadv_orig_node *orig_node = NULL; 72c4aac1abSMarek Lindner 731409a834SSven Eckelmann gw_node = batadv_gw_get_selected_gw_node(bat_priv); 74c4aac1abSMarek Lindner if (!gw_node) 757b36e8eeSMarek Lindner goto out; 765d02b3cdSLinus Lüssing 77c4aac1abSMarek Lindner rcu_read_lock(); 78c4aac1abSMarek Lindner orig_node = gw_node->orig_node; 79c4aac1abSMarek Lindner if (!orig_node) 80c4aac1abSMarek Lindner goto unlock; 81c4aac1abSMarek Lindner 827b36e8eeSMarek Lindner if (!atomic_inc_not_zero(&orig_node->refcount)) 837b36e8eeSMarek Lindner orig_node = NULL; 8443c70ad5SLinus Lüssing 85c4aac1abSMarek Lindner unlock: 865d02b3cdSLinus Lüssing rcu_read_unlock(); 87c4aac1abSMarek Lindner out: 88c6c8fea2SSven Eckelmann if (gw_node) 891409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 90c4aac1abSMarek Lindner return orig_node; 91c6c8fea2SSven Eckelmann } 92c6c8fea2SSven Eckelmann 9356303d34SSven Eckelmann static void batadv_gw_select(struct batadv_priv *bat_priv, 9456303d34SSven Eckelmann struct batadv_gw_node *new_gw_node) 95c6c8fea2SSven Eckelmann { 9656303d34SSven Eckelmann struct batadv_gw_node *curr_gw_node; 97c6c8fea2SSven Eckelmann 98807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 99c4aac1abSMarek Lindner 10025b6d3c1SMarek Lindner if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount)) 10125b6d3c1SMarek Lindner new_gw_node = NULL; 102c6c8fea2SSven Eckelmann 103807736f6SSven Eckelmann curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1); 104807736f6SSven Eckelmann rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node); 10525b6d3c1SMarek Lindner 10625b6d3c1SMarek Lindner if (curr_gw_node) 1071409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw_node); 108c4aac1abSMarek Lindner 109807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 110c4aac1abSMarek Lindner } 111c4aac1abSMarek Lindner 1124e820e72SAntonio Quartulli /** 1134e820e72SAntonio Quartulli * batadv_gw_reselect - force a gateway reselection 1144e820e72SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 1154e820e72SAntonio Quartulli * 1164e820e72SAntonio Quartulli * Set a flag to remind the GW component to perform a new gateway reselection. 1174e820e72SAntonio Quartulli * However this function does not ensure that the current gateway is going to be 1184e820e72SAntonio Quartulli * deselected. The reselection mechanism may elect the same gateway once again. 1194e820e72SAntonio Quartulli * 1204e820e72SAntonio Quartulli * This means that invoking batadv_gw_reselect() does not guarantee a gateway 1214e820e72SAntonio Quartulli * change and therefore a uevent is not necessarily expected. 1224e820e72SAntonio Quartulli */ 1234e820e72SAntonio Quartulli void batadv_gw_reselect(struct batadv_priv *bat_priv) 124c4aac1abSMarek Lindner { 125807736f6SSven Eckelmann atomic_set(&bat_priv->gw.reselect, 1); 126c6c8fea2SSven Eckelmann } 127c6c8fea2SSven Eckelmann 12856303d34SSven Eckelmann static struct batadv_gw_node * 12956303d34SSven Eckelmann batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv) 130c6c8fea2SSven Eckelmann { 13156303d34SSven Eckelmann struct batadv_neigh_node *router; 132*89652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo; 13356303d34SSven Eckelmann struct batadv_gw_node *gw_node, *curr_gw = NULL; 134c6c8fea2SSven Eckelmann uint32_t max_gw_factor = 0, tmp_gw_factor = 0; 135c67893d1SSven Eckelmann uint32_t gw_divisor; 1362265c141SAntonio Quartulli uint8_t max_tq = 0; 137c67893d1SSven Eckelmann uint8_t tq_avg; 13856303d34SSven Eckelmann struct batadv_orig_node *orig_node; 139c6c8fea2SSven Eckelmann 140c67893d1SSven Eckelmann gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE; 141c67893d1SSven Eckelmann gw_divisor *= 64; 142c67893d1SSven Eckelmann 143c6c8fea2SSven Eckelmann rcu_read_lock(); 144b67bfe0dSSasha Levin hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 145e1a5382fSLinus Lüssing if (gw_node->deleted) 146c6c8fea2SSven Eckelmann continue; 147c6c8fea2SSven Eckelmann 14884d5e5e0SSven Eckelmann orig_node = gw_node->orig_node; 1497d211efcSSven Eckelmann router = batadv_orig_node_get_router(orig_node); 150e1a5382fSLinus Lüssing if (!router) 151c6c8fea2SSven Eckelmann continue; 152c6c8fea2SSven Eckelmann 153*89652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, 154*89652331SSimon Wunderlich BATADV_IF_DEFAULT); 155*89652331SSimon Wunderlich if (!router_ifinfo) 156*89652331SSimon Wunderlich goto next; 157*89652331SSimon Wunderlich 1582265c141SAntonio Quartulli if (!atomic_inc_not_zero(&gw_node->refcount)) 1592265c141SAntonio Quartulli goto next; 1602265c141SAntonio Quartulli 161*89652331SSimon Wunderlich tq_avg = router_ifinfo->bat_iv.tq_avg; 162c67893d1SSven Eckelmann 163c6c8fea2SSven Eckelmann switch (atomic_read(&bat_priv->gw_sel_class)) { 164c6c8fea2SSven Eckelmann case 1: /* fast connection */ 165414254e3SMarek Lindner tmp_gw_factor = tq_avg * tq_avg; 166414254e3SMarek Lindner tmp_gw_factor *= gw_node->bandwidth_down; 167414254e3SMarek Lindner tmp_gw_factor *= 100 * 100; 168c67893d1SSven Eckelmann tmp_gw_factor /= gw_divisor; 169c6c8fea2SSven Eckelmann 170c6c8fea2SSven Eckelmann if ((tmp_gw_factor > max_gw_factor) || 171c6c8fea2SSven Eckelmann ((tmp_gw_factor == max_gw_factor) && 172c67893d1SSven Eckelmann (tq_avg > max_tq))) { 1732265c141SAntonio Quartulli if (curr_gw) 1741409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 1752265c141SAntonio Quartulli curr_gw = gw_node; 1762265c141SAntonio Quartulli atomic_inc(&curr_gw->refcount); 1772265c141SAntonio Quartulli } 178c6c8fea2SSven Eckelmann break; 179c6c8fea2SSven Eckelmann 1809cfc7bd6SSven Eckelmann default: /* 2: stable connection (use best statistic) 181c6c8fea2SSven Eckelmann * 3: fast-switch (use best statistic but change as 182c6c8fea2SSven Eckelmann * soon as a better gateway appears) 183c6c8fea2SSven Eckelmann * XX: late-switch (use best statistic but change as 184c6c8fea2SSven Eckelmann * soon as a better gateway appears which has 185c6c8fea2SSven Eckelmann * $routing_class more tq points) 1869cfc7bd6SSven Eckelmann */ 187c67893d1SSven Eckelmann if (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 } 195c6c8fea2SSven Eckelmann 196c67893d1SSven Eckelmann if (tq_avg > max_tq) 197c67893d1SSven Eckelmann max_tq = tq_avg; 198c6c8fea2SSven Eckelmann 199c6c8fea2SSven Eckelmann if (tmp_gw_factor > max_gw_factor) 200c6c8fea2SSven Eckelmann max_gw_factor = tmp_gw_factor; 201e1a5382fSLinus Lüssing 2021409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 2032265c141SAntonio Quartulli 2042265c141SAntonio Quartulli next: 2057d211efcSSven Eckelmann batadv_neigh_node_free_ref(router); 206*89652331SSimon Wunderlich if (router_ifinfo) 207*89652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_ifinfo); 208c6c8fea2SSven Eckelmann } 2092265c141SAntonio Quartulli rcu_read_unlock(); 210c6c8fea2SSven Eckelmann 2112265c141SAntonio Quartulli return curr_gw; 2122265c141SAntonio Quartulli } 213e1a5382fSLinus Lüssing 214c6eaa3f0SAntonio Quartulli /** 215c6eaa3f0SAntonio Quartulli * batadv_gw_check_client_stop - check if client mode has been switched off 216c6eaa3f0SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 217c6eaa3f0SAntonio Quartulli * 218c6eaa3f0SAntonio Quartulli * This function assumes the caller has checked that the gw state *is actually 219c6eaa3f0SAntonio Quartulli * changing*. This function is not supposed to be called when there is no state 220c6eaa3f0SAntonio Quartulli * change. 221c6eaa3f0SAntonio Quartulli */ 222c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv) 223c6eaa3f0SAntonio Quartulli { 224c6eaa3f0SAntonio Quartulli struct batadv_gw_node *curr_gw; 225c6eaa3f0SAntonio Quartulli 226c6eaa3f0SAntonio Quartulli if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT) 227c6eaa3f0SAntonio Quartulli return; 228c6eaa3f0SAntonio Quartulli 229c6eaa3f0SAntonio Quartulli curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 230c6eaa3f0SAntonio Quartulli if (!curr_gw) 231c6eaa3f0SAntonio Quartulli return; 232c6eaa3f0SAntonio Quartulli 233f3163181SAntonio Quartulli /* deselect the current gateway so that next time that client mode is 234f3163181SAntonio Quartulli * enabled a proper GW_ADD event can be sent 235f3163181SAntonio Quartulli */ 236f3163181SAntonio Quartulli batadv_gw_select(bat_priv, NULL); 237f3163181SAntonio Quartulli 238c6eaa3f0SAntonio Quartulli /* if batman-adv is switching the gw client mode off and a gateway was 239c6eaa3f0SAntonio Quartulli * already selected, send a DEL uevent 240c6eaa3f0SAntonio Quartulli */ 241c6eaa3f0SAntonio Quartulli batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL); 242c6eaa3f0SAntonio Quartulli 243c6eaa3f0SAntonio Quartulli batadv_gw_node_free_ref(curr_gw); 244c6eaa3f0SAntonio Quartulli } 245c6eaa3f0SAntonio Quartulli 24656303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv) 2472265c141SAntonio Quartulli { 24856303d34SSven Eckelmann struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL; 24956303d34SSven Eckelmann struct batadv_neigh_node *router = NULL; 250*89652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo = NULL; 25119595e05SAntonio Quartulli char gw_addr[18] = { '\0' }; 2522265c141SAntonio Quartulli 253cd646ab1SSven Eckelmann if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT) 2542265c141SAntonio Quartulli goto out; 2552265c141SAntonio Quartulli 2561409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 2572265c141SAntonio Quartulli 258807736f6SSven Eckelmann if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw) 259caa0bf64SMarek Lindner goto out; 260caa0bf64SMarek Lindner 2611409a834SSven Eckelmann next_gw = batadv_gw_get_best_gw_node(bat_priv); 2622265c141SAntonio Quartulli 2632265c141SAntonio Quartulli if (curr_gw == next_gw) 2642265c141SAntonio Quartulli goto out; 2652265c141SAntonio Quartulli 2662265c141SAntonio Quartulli if (next_gw) { 26719595e05SAntonio Quartulli sprintf(gw_addr, "%pM", next_gw->orig_node->orig); 26819595e05SAntonio Quartulli 2697d211efcSSven Eckelmann router = batadv_orig_node_get_router(next_gw->orig_node); 2702265c141SAntonio Quartulli if (!router) { 2714e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 2722265c141SAntonio Quartulli goto out; 2732265c141SAntonio Quartulli } 274*89652331SSimon Wunderlich 275*89652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, 276*89652331SSimon Wunderlich BATADV_IF_DEFAULT); 277*89652331SSimon Wunderlich if (!router_ifinfo) { 278*89652331SSimon Wunderlich batadv_gw_reselect(bat_priv); 279*89652331SSimon Wunderlich goto out; 280*89652331SSimon Wunderlich } 2812265c141SAntonio Quartulli } 2822265c141SAntonio Quartulli 2832265c141SAntonio Quartulli if ((curr_gw) && (!next_gw)) { 28439c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 2852265c141SAntonio Quartulli "Removing selected gateway - no gateway in range\n"); 28639c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, 28739c75a51SSven Eckelmann NULL); 2882265c141SAntonio Quartulli } else if ((!curr_gw) && (next_gw)) { 28939c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 290414254e3SMarek Lindner "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 2911eda58bfSSven Eckelmann next_gw->orig_node->orig, 292414254e3SMarek Lindner next_gw->bandwidth_down / 10, 293414254e3SMarek Lindner next_gw->bandwidth_down % 10, 294414254e3SMarek Lindner next_gw->bandwidth_up / 10, 295*89652331SSimon Wunderlich next_gw->bandwidth_up % 10, 296*89652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg); 29739c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD, 29839c75a51SSven Eckelmann gw_addr); 2992265c141SAntonio Quartulli } else { 30039c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 301414254e3SMarek Lindner "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 3021eda58bfSSven Eckelmann next_gw->orig_node->orig, 303414254e3SMarek Lindner next_gw->bandwidth_down / 10, 304414254e3SMarek Lindner next_gw->bandwidth_down % 10, 305414254e3SMarek Lindner next_gw->bandwidth_up / 10, 306*89652331SSimon Wunderlich next_gw->bandwidth_up % 10, 307*89652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg); 30839c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE, 30939c75a51SSven Eckelmann gw_addr); 310c6c8fea2SSven Eckelmann } 311c6c8fea2SSven Eckelmann 3121409a834SSven Eckelmann batadv_gw_select(bat_priv, next_gw); 3132265c141SAntonio Quartulli 314c4aac1abSMarek Lindner out: 315c4aac1abSMarek Lindner if (curr_gw) 3161409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 3172265c141SAntonio Quartulli if (next_gw) 3181409a834SSven Eckelmann batadv_gw_node_free_ref(next_gw); 3192265c141SAntonio Quartulli if (router) 3207d211efcSSven Eckelmann batadv_neigh_node_free_ref(router); 321*89652331SSimon Wunderlich if (router_ifinfo) 322*89652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_ifinfo); 323c6c8fea2SSven Eckelmann } 324c6c8fea2SSven Eckelmann 32556303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv, 32656303d34SSven Eckelmann struct batadv_orig_node *orig_node) 327c6c8fea2SSven Eckelmann { 328*89652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_orig_tq = NULL; 329*89652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_gw_tq = NULL; 33056303d34SSven Eckelmann struct batadv_orig_node *curr_gw_orig; 33156303d34SSven Eckelmann struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL; 332c6c8fea2SSven Eckelmann uint8_t gw_tq_avg, orig_tq_avg; 333c6c8fea2SSven Eckelmann 3347cf06bc6SSven Eckelmann curr_gw_orig = batadv_gw_get_selected_orig(bat_priv); 33557f0c07cSLinus Lüssing if (!curr_gw_orig) 3364e820e72SAntonio Quartulli goto reselect; 337c6c8fea2SSven Eckelmann 3387d211efcSSven Eckelmann router_gw = batadv_orig_node_get_router(curr_gw_orig); 339e1a5382fSLinus Lüssing if (!router_gw) 3404e820e72SAntonio Quartulli goto reselect; 341c6c8fea2SSven Eckelmann 342*89652331SSimon Wunderlich router_gw_tq = batadv_neigh_ifinfo_get(router_gw, 343*89652331SSimon Wunderlich BATADV_IF_DEFAULT); 344*89652331SSimon Wunderlich if (!router_gw_tq) 345*89652331SSimon Wunderlich goto reselect; 346*89652331SSimon Wunderlich 347c6c8fea2SSven Eckelmann /* this node already is the gateway */ 34857f0c07cSLinus Lüssing if (curr_gw_orig == orig_node) 349e1a5382fSLinus Lüssing goto out; 350c6c8fea2SSven Eckelmann 3517d211efcSSven Eckelmann router_orig = batadv_orig_node_get_router(orig_node); 352e1a5382fSLinus Lüssing if (!router_orig) 353e1a5382fSLinus Lüssing goto out; 354c6c8fea2SSven Eckelmann 355*89652331SSimon Wunderlich router_orig_tq = batadv_neigh_ifinfo_get(router_orig, 356*89652331SSimon Wunderlich BATADV_IF_DEFAULT); 357*89652331SSimon Wunderlich if (!router_orig_tq) 358*89652331SSimon Wunderlich goto out; 359*89652331SSimon Wunderlich 360*89652331SSimon Wunderlich gw_tq_avg = router_gw_tq->bat_iv.tq_avg; 361*89652331SSimon Wunderlich orig_tq_avg = router_orig_tq->bat_iv.tq_avg; 362c6c8fea2SSven Eckelmann 363c6c8fea2SSven Eckelmann /* the TQ value has to be better */ 364c6c8fea2SSven Eckelmann if (orig_tq_avg < gw_tq_avg) 3655d02b3cdSLinus Lüssing goto out; 366c6c8fea2SSven Eckelmann 3679cfc7bd6SSven Eckelmann /* if the routing class is greater than 3 the value tells us how much 368c6c8fea2SSven Eckelmann * greater the TQ value of the new gateway must be 3699cfc7bd6SSven Eckelmann */ 370c6c8fea2SSven Eckelmann if ((atomic_read(&bat_priv->gw_sel_class) > 3) && 371c6c8fea2SSven Eckelmann (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class))) 3725d02b3cdSLinus Lüssing goto out; 373c6c8fea2SSven Eckelmann 37439c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 37586ceb360SSven Eckelmann "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n", 376c6c8fea2SSven Eckelmann gw_tq_avg, orig_tq_avg); 377c6c8fea2SSven Eckelmann 3784e820e72SAntonio Quartulli reselect: 3794e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 3805d02b3cdSLinus Lüssing out: 38157f0c07cSLinus Lüssing if (curr_gw_orig) 3827d211efcSSven Eckelmann batadv_orig_node_free_ref(curr_gw_orig); 383e1a5382fSLinus Lüssing if (router_gw) 3847d211efcSSven Eckelmann batadv_neigh_node_free_ref(router_gw); 385e1a5382fSLinus Lüssing if (router_orig) 3867d211efcSSven Eckelmann batadv_neigh_node_free_ref(router_orig); 387*89652331SSimon Wunderlich if (router_gw_tq) 388*89652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_gw_tq); 389*89652331SSimon Wunderlich if (router_orig_tq) 390*89652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_orig_tq); 39157f0c07cSLinus Lüssing 3925d02b3cdSLinus Lüssing return; 393c6c8fea2SSven Eckelmann } 394c6c8fea2SSven Eckelmann 395414254e3SMarek Lindner /** 396414254e3SMarek Lindner * batadv_gw_node_add - add gateway node to list of available gateways 397414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 398414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 399414254e3SMarek Lindner * @gateway: announced bandwidth information 400414254e3SMarek Lindner */ 40156303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv, 40256303d34SSven Eckelmann struct batadv_orig_node *orig_node, 403414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 404c6c8fea2SSven Eckelmann { 40556303d34SSven Eckelmann struct batadv_gw_node *gw_node; 406414254e3SMarek Lindner 407414254e3SMarek Lindner if (gateway->bandwidth_down == 0) 408414254e3SMarek Lindner return; 409c6c8fea2SSven Eckelmann 410704509b8SSven Eckelmann gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); 411c6c8fea2SSven Eckelmann if (!gw_node) 412c6c8fea2SSven Eckelmann return; 413c6c8fea2SSven Eckelmann 414c6c8fea2SSven Eckelmann INIT_HLIST_NODE(&gw_node->list); 415c6c8fea2SSven Eckelmann gw_node->orig_node = orig_node; 41625b6d3c1SMarek Lindner atomic_set(&gw_node->refcount, 1); 417c6c8fea2SSven Eckelmann 418807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 419807736f6SSven Eckelmann hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list); 420807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 421c6c8fea2SSven Eckelmann 42239c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 423414254e3SMarek Lindner "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", 424414254e3SMarek Lindner orig_node->orig, 425414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 426414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 427414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 428414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 429c6c8fea2SSven Eckelmann } 430c6c8fea2SSven Eckelmann 431414254e3SMarek Lindner /** 432414254e3SMarek Lindner * batadv_gw_node_get - retrieve gateway node from list of available gateways 433414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 434414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 435414254e3SMarek Lindner * 436414254e3SMarek Lindner * Returns gateway node if found or NULL otherwise. 43771e4aa9cSAntonio Quartulli */ 438414254e3SMarek Lindner static struct batadv_gw_node * 439414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv, 440414254e3SMarek Lindner struct batadv_orig_node *orig_node) 441414254e3SMarek Lindner { 442414254e3SMarek Lindner struct batadv_gw_node *gw_node_tmp, *gw_node = NULL; 443c6c8fea2SSven Eckelmann 444c6c8fea2SSven Eckelmann rcu_read_lock(); 445414254e3SMarek Lindner hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) { 446414254e3SMarek Lindner if (gw_node_tmp->orig_node != orig_node) 447c6c8fea2SSven Eckelmann continue; 448c6c8fea2SSven Eckelmann 449414254e3SMarek Lindner if (gw_node_tmp->deleted) 450414254e3SMarek Lindner continue; 451414254e3SMarek Lindner 452414254e3SMarek Lindner if (!atomic_inc_not_zero(&gw_node_tmp->refcount)) 453414254e3SMarek Lindner continue; 454414254e3SMarek Lindner 455414254e3SMarek Lindner gw_node = gw_node_tmp; 456414254e3SMarek Lindner break; 457414254e3SMarek Lindner } 458414254e3SMarek Lindner rcu_read_unlock(); 459414254e3SMarek Lindner 460414254e3SMarek Lindner return gw_node; 461414254e3SMarek Lindner } 462414254e3SMarek Lindner 463414254e3SMarek Lindner /** 464414254e3SMarek Lindner * batadv_gw_node_update - update list of available gateways with changed 465414254e3SMarek Lindner * bandwidth information 466414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 467414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 468414254e3SMarek Lindner * @gateway: announced bandwidth information 469414254e3SMarek Lindner */ 470414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv, 471414254e3SMarek Lindner struct batadv_orig_node *orig_node, 472414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 473414254e3SMarek Lindner { 474414254e3SMarek Lindner struct batadv_gw_node *gw_node, *curr_gw = NULL; 475414254e3SMarek Lindner 476414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_node); 477414254e3SMarek Lindner if (!gw_node) { 478414254e3SMarek Lindner batadv_gw_node_add(bat_priv, orig_node, gateway); 479414254e3SMarek Lindner goto out; 480414254e3SMarek Lindner } 481414254e3SMarek Lindner 482414254e3SMarek Lindner if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) && 483414254e3SMarek Lindner (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))) 484414254e3SMarek Lindner goto out; 485414254e3SMarek Lindner 48639c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 487414254e3SMarek Lindner "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n", 488414254e3SMarek Lindner orig_node->orig, 489414254e3SMarek Lindner gw_node->bandwidth_down / 10, 490414254e3SMarek Lindner gw_node->bandwidth_down % 10, 491414254e3SMarek Lindner gw_node->bandwidth_up / 10, 492414254e3SMarek Lindner gw_node->bandwidth_up % 10, 493414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 494414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 495414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 496414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 497414254e3SMarek Lindner 498414254e3SMarek Lindner gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 499414254e3SMarek Lindner gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 500c6c8fea2SSven Eckelmann 501c6c8fea2SSven Eckelmann gw_node->deleted = 0; 502414254e3SMarek Lindner if (ntohl(gateway->bandwidth_down) == 0) { 503c6c8fea2SSven Eckelmann gw_node->deleted = jiffies; 50439c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 505c6c8fea2SSven Eckelmann "Gateway %pM removed from gateway list\n", 506c6c8fea2SSven Eckelmann orig_node->orig); 507c6c8fea2SSven Eckelmann 508414254e3SMarek Lindner /* Note: We don't need a NULL check here, since curr_gw never 509414254e3SMarek Lindner * gets dereferenced. 510414254e3SMarek Lindner */ 511414254e3SMarek Lindner curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 512c4aac1abSMarek Lindner if (gw_node == curr_gw) 5134e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 514414254e3SMarek Lindner } 51571e4aa9cSAntonio Quartulli 516414254e3SMarek Lindner out: 517c4aac1abSMarek Lindner if (curr_gw) 5181409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 519414254e3SMarek Lindner if (gw_node) 520414254e3SMarek Lindner batadv_gw_node_free_ref(gw_node); 521c6c8fea2SSven Eckelmann } 522c6c8fea2SSven Eckelmann 52356303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv, 52456303d34SSven Eckelmann struct batadv_orig_node *orig_node) 525c6c8fea2SSven Eckelmann { 526414254e3SMarek Lindner struct batadv_tvlv_gateway_data gateway; 527414254e3SMarek Lindner 528414254e3SMarek Lindner gateway.bandwidth_down = 0; 529414254e3SMarek Lindner gateway.bandwidth_up = 0; 530414254e3SMarek Lindner 531414254e3SMarek Lindner batadv_gw_node_update(bat_priv, orig_node, &gateway); 532c6c8fea2SSven Eckelmann } 533c6c8fea2SSven Eckelmann 53456303d34SSven Eckelmann void batadv_gw_node_purge(struct batadv_priv *bat_priv) 535c6c8fea2SSven Eckelmann { 53656303d34SSven Eckelmann struct batadv_gw_node *gw_node, *curr_gw; 537b67bfe0dSSasha Levin struct hlist_node *node_tmp; 53842d0b044SSven Eckelmann unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT); 5394e820e72SAntonio Quartulli int do_reselect = 0; 540c4aac1abSMarek Lindner 5411409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 542c6c8fea2SSven Eckelmann 543807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 544c6c8fea2SSven Eckelmann 545b67bfe0dSSasha Levin hlist_for_each_entry_safe(gw_node, node_tmp, 546807736f6SSven Eckelmann &bat_priv->gw.list, list) { 547c6c8fea2SSven Eckelmann if (((!gw_node->deleted) || 548c6c8fea2SSven Eckelmann (time_before(jiffies, gw_node->deleted + timeout))) && 54939c75a51SSven Eckelmann atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE) 550c6c8fea2SSven Eckelmann continue; 551c6c8fea2SSven Eckelmann 552c4aac1abSMarek Lindner if (curr_gw == gw_node) 5534e820e72SAntonio Quartulli do_reselect = 1; 554c6c8fea2SSven Eckelmann 555c6c8fea2SSven Eckelmann hlist_del_rcu(&gw_node->list); 5561409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 557c6c8fea2SSven Eckelmann } 558c6c8fea2SSven Eckelmann 559807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 560c4aac1abSMarek Lindner 5614e820e72SAntonio Quartulli /* gw_reselect() needs to acquire the gw_list_lock */ 5624e820e72SAntonio Quartulli if (do_reselect) 5634e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 564c4aac1abSMarek Lindner 565c4aac1abSMarek Lindner if (curr_gw) 5661409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 567c6c8fea2SSven Eckelmann } 568c6c8fea2SSven Eckelmann 5699cfc7bd6SSven Eckelmann /* fails if orig_node has no router */ 57056303d34SSven Eckelmann static int batadv_write_buffer_text(struct batadv_priv *bat_priv, 5711409a834SSven Eckelmann struct seq_file *seq, 57256303d34SSven Eckelmann const struct batadv_gw_node *gw_node) 573c6c8fea2SSven Eckelmann { 57456303d34SSven Eckelmann struct batadv_gw_node *curr_gw; 57556303d34SSven Eckelmann struct batadv_neigh_node *router; 576*89652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo = NULL; 577414254e3SMarek Lindner int ret = -1; 578c6c8fea2SSven Eckelmann 5797d211efcSSven Eckelmann router = batadv_orig_node_get_router(gw_node->orig_node); 580e1a5382fSLinus Lüssing if (!router) 581e1a5382fSLinus Lüssing goto out; 582e1a5382fSLinus Lüssing 583*89652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT); 584*89652331SSimon Wunderlich if (!router_ifinfo) 585*89652331SSimon Wunderlich goto out; 586*89652331SSimon Wunderlich 5871409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 5885d02b3cdSLinus Lüssing 589414254e3SMarek Lindner ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n", 5905d02b3cdSLinus Lüssing (curr_gw == gw_node ? "=>" : " "), 591c6c8fea2SSven Eckelmann gw_node->orig_node->orig, 592*89652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg, router->addr, 593e1a5382fSLinus Lüssing router->if_incoming->net_dev->name, 594414254e3SMarek Lindner gw_node->bandwidth_down / 10, 595414254e3SMarek Lindner gw_node->bandwidth_down % 10, 596414254e3SMarek Lindner gw_node->bandwidth_up / 10, 597414254e3SMarek Lindner gw_node->bandwidth_up % 10); 5985d02b3cdSLinus Lüssing 599c4aac1abSMarek Lindner if (curr_gw) 6001409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 601e1a5382fSLinus Lüssing out: 602*89652331SSimon Wunderlich if (router_ifinfo) 603*89652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(router_ifinfo); 604*89652331SSimon Wunderlich if (router) 605*89652331SSimon Wunderlich batadv_neigh_node_free_ref(router); 6065d02b3cdSLinus Lüssing return ret; 607c6c8fea2SSven Eckelmann } 608c6c8fea2SSven Eckelmann 6097cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset) 610c6c8fea2SSven Eckelmann { 611c6c8fea2SSven Eckelmann struct net_device *net_dev = (struct net_device *)seq->private; 61256303d34SSven Eckelmann struct batadv_priv *bat_priv = netdev_priv(net_dev); 61356303d34SSven Eckelmann struct batadv_hard_iface *primary_if; 61456303d34SSven Eckelmann struct batadv_gw_node *gw_node; 61530da63a6SMarek Lindner int gw_count = 0; 616c6c8fea2SSven Eckelmann 61730da63a6SMarek Lindner primary_if = batadv_seq_print_text_primary_if_get(seq); 61830da63a6SMarek Lindner if (!primary_if) 61932ae9b22SMarek Lindner goto out; 620c6c8fea2SSven Eckelmann 62186ceb360SSven Eckelmann seq_printf(seq, 622414254e3SMarek Lindner " %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", 62342d0b044SSven Eckelmann "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF", 62442d0b044SSven Eckelmann BATADV_SOURCE_VERSION, primary_if->net_dev->name, 62532ae9b22SMarek Lindner primary_if->net_dev->dev_addr, net_dev->name); 626c6c8fea2SSven Eckelmann 627c6c8fea2SSven Eckelmann rcu_read_lock(); 628b67bfe0dSSasha Levin hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 629c6c8fea2SSven Eckelmann if (gw_node->deleted) 630c6c8fea2SSven Eckelmann continue; 631c6c8fea2SSven Eckelmann 632e1a5382fSLinus Lüssing /* fails if orig_node has no router */ 6331409a834SSven Eckelmann if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0) 634c6c8fea2SSven Eckelmann continue; 635c6c8fea2SSven Eckelmann 636c6c8fea2SSven Eckelmann gw_count++; 637c6c8fea2SSven Eckelmann } 638c6c8fea2SSven Eckelmann rcu_read_unlock(); 639c6c8fea2SSven Eckelmann 640c6c8fea2SSven Eckelmann if (gw_count == 0) 6410c814653SAntonio Quartulli seq_puts(seq, "No gateways in range ...\n"); 642c6c8fea2SSven Eckelmann 64332ae9b22SMarek Lindner out: 64432ae9b22SMarek Lindner if (primary_if) 645e5d89254SSven Eckelmann batadv_hardif_free_ref(primary_if); 64630da63a6SMarek Lindner return 0; 647c6c8fea2SSven Eckelmann } 648c6c8fea2SSven Eckelmann 6496c413b1cSAntonio Quartulli /** 6506c413b1cSAntonio Quartulli * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message 6516c413b1cSAntonio Quartulli * @skb: the packet to check 6526c413b1cSAntonio Quartulli * @header_len: a pointer to the batman-adv header size 6536c413b1cSAntonio Quartulli * @chaddr: buffer where the client address will be stored. Valid 6546c413b1cSAntonio Quartulli * only if the function returns BATADV_DHCP_TO_CLIENT 6556c413b1cSAntonio Quartulli * 6566c413b1cSAntonio Quartulli * Returns: 6576c413b1cSAntonio Quartulli * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error 6586c413b1cSAntonio Quartulli * while parsing it 6596c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server 6606c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client 6616c413b1cSAntonio Quartulli * 6626c413b1cSAntonio Quartulli * This function may re-allocate the data buffer of the skb passed as argument. 6639cfc7bd6SSven Eckelmann */ 6646c413b1cSAntonio Quartulli enum batadv_dhcp_recipient 6656c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len, 6666c413b1cSAntonio Quartulli uint8_t *chaddr) 667c6c8fea2SSven Eckelmann { 6686c413b1cSAntonio Quartulli enum batadv_dhcp_recipient ret = BATADV_DHCP_NO; 669c6c8fea2SSven Eckelmann struct ethhdr *ethhdr; 670c6c8fea2SSven Eckelmann struct iphdr *iphdr; 671c6c8fea2SSven Eckelmann struct ipv6hdr *ipv6hdr; 672c6c8fea2SSven Eckelmann struct udphdr *udphdr; 673f7f8ed56SAntonio Quartulli struct vlan_ethhdr *vhdr; 6746c413b1cSAntonio Quartulli int chaddr_offset; 675f7f8ed56SAntonio Quartulli __be16 proto; 6766c413b1cSAntonio Quartulli uint8_t *p; 677c6c8fea2SSven Eckelmann 678c6c8fea2SSven Eckelmann /* check for ethernet header */ 679be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) 6806c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 6816c413b1cSAntonio Quartulli 682c6c8fea2SSven Eckelmann ethhdr = (struct ethhdr *)skb->data; 683f7f8ed56SAntonio Quartulli proto = ethhdr->h_proto; 684be7af5cfSMarek Lindner *header_len += ETH_HLEN; 685c6c8fea2SSven Eckelmann 686c6c8fea2SSven Eckelmann /* check for initial vlan header */ 687f7f8ed56SAntonio Quartulli if (proto == htons(ETH_P_8021Q)) { 688be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) 6896c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 690f7f8ed56SAntonio Quartulli 691f7f8ed56SAntonio Quartulli vhdr = (struct vlan_ethhdr *)skb->data; 692f7f8ed56SAntonio Quartulli proto = vhdr->h_vlan_encapsulated_proto; 693be7af5cfSMarek Lindner *header_len += VLAN_HLEN; 694c6c8fea2SSven Eckelmann } 695c6c8fea2SSven Eckelmann 696c6c8fea2SSven Eckelmann /* check for ip header */ 697f7f8ed56SAntonio Quartulli switch (proto) { 698f7f8ed56SAntonio Quartulli case htons(ETH_P_IP): 699be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) 7006c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7016c413b1cSAntonio Quartulli 702be7af5cfSMarek Lindner iphdr = (struct iphdr *)(skb->data + *header_len); 703be7af5cfSMarek Lindner *header_len += iphdr->ihl * 4; 704c6c8fea2SSven Eckelmann 705c6c8fea2SSven Eckelmann /* check for udp header */ 706c6c8fea2SSven Eckelmann if (iphdr->protocol != IPPROTO_UDP) 7076c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 708c6c8fea2SSven Eckelmann 709c6c8fea2SSven Eckelmann break; 710f7f8ed56SAntonio Quartulli case htons(ETH_P_IPV6): 711be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) 7126c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7136c413b1cSAntonio Quartulli 714be7af5cfSMarek Lindner ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); 715be7af5cfSMarek Lindner *header_len += sizeof(*ipv6hdr); 716c6c8fea2SSven Eckelmann 717c6c8fea2SSven Eckelmann /* check for udp header */ 718c6c8fea2SSven Eckelmann if (ipv6hdr->nexthdr != IPPROTO_UDP) 7196c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 720c6c8fea2SSven Eckelmann 721c6c8fea2SSven Eckelmann break; 722c6c8fea2SSven Eckelmann default: 7236c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 724c6c8fea2SSven Eckelmann } 725c6c8fea2SSven Eckelmann 726be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr))) 7276c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7289d2c9488SLinus Lüssing 7299d2c9488SLinus Lüssing /* skb->data might have been reallocated by pskb_may_pull() */ 7309d2c9488SLinus Lüssing ethhdr = (struct ethhdr *)skb->data; 7319d2c9488SLinus Lüssing if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) 7329d2c9488SLinus Lüssing ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN); 7339d2c9488SLinus Lüssing 734be7af5cfSMarek Lindner udphdr = (struct udphdr *)(skb->data + *header_len); 735be7af5cfSMarek Lindner *header_len += sizeof(*udphdr); 736c6c8fea2SSven Eckelmann 737c6c8fea2SSven Eckelmann /* check for bootp port */ 7386c413b1cSAntonio Quartulli switch (proto) { 7396c413b1cSAntonio Quartulli case htons(ETH_P_IP): 7406c413b1cSAntonio Quartulli if (udphdr->dest == htons(67)) 7416c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 7426c413b1cSAntonio Quartulli else if (udphdr->source == htons(67)) 7436c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 7446c413b1cSAntonio Quartulli break; 7456c413b1cSAntonio Quartulli case htons(ETH_P_IPV6): 7466c413b1cSAntonio Quartulli if (udphdr->dest == htons(547)) 7476c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 7486c413b1cSAntonio Quartulli else if (udphdr->source == htons(547)) 7496c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 7506c413b1cSAntonio Quartulli break; 751be7af5cfSMarek Lindner } 752c6c8fea2SSven Eckelmann 7536c413b1cSAntonio Quartulli chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET; 7546c413b1cSAntonio Quartulli /* store the client address if the message is going to a client */ 7556c413b1cSAntonio Quartulli if (ret == BATADV_DHCP_TO_CLIENT && 7566c413b1cSAntonio Quartulli pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) { 7576c413b1cSAntonio Quartulli /* check if the DHCP packet carries an Ethernet DHCP */ 7586c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET; 7596c413b1cSAntonio Quartulli if (*p != BATADV_DHCP_HTYPE_ETHERNET) 7606c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7616c413b1cSAntonio Quartulli 7626c413b1cSAntonio Quartulli /* check if the DHCP packet carries a valid Ethernet address */ 7636c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET; 7646c413b1cSAntonio Quartulli if (*p != ETH_ALEN) 7656c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 7666c413b1cSAntonio Quartulli 7676c413b1cSAntonio Quartulli memcpy(chaddr, skb->data + chaddr_offset, ETH_ALEN); 7686c413b1cSAntonio Quartulli } 7696c413b1cSAntonio Quartulli 7706c413b1cSAntonio Quartulli return ret; 7716c413b1cSAntonio Quartulli } 772bbb877edSAntonio Quartulli /** 773bbb877edSAntonio Quartulli * batadv_gw_out_of_range - check if the dhcp request destination is the best gw 774bbb877edSAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 775bbb877edSAntonio Quartulli * @skb: the outgoing packet 776bbb877edSAntonio Quartulli * 777bbb877edSAntonio Quartulli * Check if the skb is a DHCP request and if it is sent to the current best GW 778bbb877edSAntonio Quartulli * server. Due to topology changes it may be the case that the GW server 779bbb877edSAntonio Quartulli * previously selected is not the best one anymore. 780bbb877edSAntonio Quartulli * 781bbb877edSAntonio Quartulli * Returns true if the packet destination is unicast and it is not the best gw, 782bbb877edSAntonio Quartulli * false otherwise. 783bbb877edSAntonio Quartulli * 784bbb877edSAntonio Quartulli * This call might reallocate skb data. 7856c413b1cSAntonio Quartulli * Must be invoked only when the DHCP packet is going TO a DHCP SERVER. 786bbb877edSAntonio Quartulli */ 78756303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, 7889d2c9488SLinus Lüssing struct sk_buff *skb) 789be7af5cfSMarek Lindner { 79056303d34SSven Eckelmann struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL; 79156303d34SSven Eckelmann struct batadv_orig_node *orig_dst_node = NULL; 792414254e3SMarek Lindner struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL; 793*89652331SSimon Wunderlich struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo; 7946c413b1cSAntonio Quartulli struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 7956c413b1cSAntonio Quartulli bool out_of_range = false; 796be7af5cfSMarek Lindner uint8_t curr_tq_avg; 797bbb877edSAntonio Quartulli unsigned short vid; 798bbb877edSAntonio Quartulli 799bbb877edSAntonio Quartulli vid = batadv_get_vid(skb, 0); 800be7af5cfSMarek Lindner 80108c36d3eSSven Eckelmann orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, 802bbb877edSAntonio Quartulli ethhdr->h_dest, vid); 803be7af5cfSMarek Lindner if (!orig_dst_node) 804be7af5cfSMarek Lindner goto out; 805be7af5cfSMarek Lindner 806414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_dst_node); 807414254e3SMarek Lindner if (!gw_node->bandwidth_down == 0) 808be7af5cfSMarek Lindner goto out; 809be7af5cfSMarek Lindner 810be7af5cfSMarek Lindner switch (atomic_read(&bat_priv->gw_mode)) { 811cd646ab1SSven Eckelmann case BATADV_GW_MODE_SERVER: 812be7af5cfSMarek Lindner /* If we are a GW then we are our best GW. We can artificially 8139cfc7bd6SSven Eckelmann * set the tq towards ourself as the maximum value 8149cfc7bd6SSven Eckelmann */ 81542d0b044SSven Eckelmann curr_tq_avg = BATADV_TQ_MAX_VALUE; 816be7af5cfSMarek Lindner break; 817cd646ab1SSven Eckelmann case BATADV_GW_MODE_CLIENT: 8181409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 819c4aac1abSMarek Lindner if (!curr_gw) 820be7af5cfSMarek Lindner goto out; 821c6c8fea2SSven Eckelmann 822be7af5cfSMarek Lindner /* packet is going to our gateway */ 823be7af5cfSMarek Lindner if (curr_gw->orig_node == orig_dst_node) 824be7af5cfSMarek Lindner goto out; 825be7af5cfSMarek Lindner 82643676ab5SAntonio Quartulli /* If the dhcp packet has been sent to a different gw, 82743676ab5SAntonio Quartulli * we have to evaluate whether the old gw is still 8289cfc7bd6SSven Eckelmann * reliable enough 8299cfc7bd6SSven Eckelmann */ 83030d3c511SSven Eckelmann neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node, 83130d3c511SSven Eckelmann NULL); 832be7af5cfSMarek Lindner if (!neigh_curr) 833be7af5cfSMarek Lindner goto out; 834be7af5cfSMarek Lindner 835*89652331SSimon Wunderlich curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr, 836*89652331SSimon Wunderlich BATADV_IF_DEFAULT); 837*89652331SSimon Wunderlich if (!curr_ifinfo) 838*89652331SSimon Wunderlich goto out; 839*89652331SSimon Wunderlich 840*89652331SSimon Wunderlich curr_tq_avg = curr_ifinfo->bat_iv.tq_avg; 841*89652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(curr_ifinfo); 842*89652331SSimon Wunderlich 843be7af5cfSMarek Lindner break; 844cd646ab1SSven Eckelmann case BATADV_GW_MODE_OFF: 845be7af5cfSMarek Lindner default: 846be7af5cfSMarek Lindner goto out; 84743676ab5SAntonio Quartulli } 848be7af5cfSMarek Lindner 84930d3c511SSven Eckelmann neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL); 8502ef04f47SDan Carpenter if (!neigh_old) 851be7af5cfSMarek Lindner goto out; 852be7af5cfSMarek Lindner 853*89652331SSimon Wunderlich old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT); 854*89652331SSimon Wunderlich if (!old_ifinfo) 855*89652331SSimon Wunderlich goto out; 856*89652331SSimon Wunderlich 857*89652331SSimon Wunderlich if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD) 858be7af5cfSMarek Lindner out_of_range = true; 859*89652331SSimon Wunderlich batadv_neigh_ifinfo_free_ref(old_ifinfo); 860be7af5cfSMarek Lindner 861be7af5cfSMarek Lindner out: 862be7af5cfSMarek Lindner if (orig_dst_node) 8637d211efcSSven Eckelmann batadv_orig_node_free_ref(orig_dst_node); 864be7af5cfSMarek Lindner if (curr_gw) 8651409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 866414254e3SMarek Lindner if (gw_node) 867414254e3SMarek Lindner batadv_gw_node_free_ref(gw_node); 86843676ab5SAntonio Quartulli if (neigh_old) 8697d211efcSSven Eckelmann batadv_neigh_node_free_ref(neigh_old); 87043676ab5SAntonio Quartulli if (neigh_curr) 8717d211efcSSven Eckelmann batadv_neigh_node_free_ref(neigh_curr); 872be7af5cfSMarek Lindner return out_of_range; 873c6c8fea2SSven Eckelmann } 874