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 31*6c413b1cSAntonio Quartulli /* These are the offsets of the "hw type" and "hw address length" in the dhcp 32*6c413b1cSAntonio Quartulli * packet starting at the beginning of the dhcp header 339cfc7bd6SSven Eckelmann */ 34*6c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_OFFSET 1 35*6c413b1cSAntonio Quartulli #define BATADV_DHCP_HLEN_OFFSET 2 36*6c413b1cSAntonio Quartulli /* Value of htype representing Ethernet */ 37*6c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_ETHERNET 0x01 38*6c413b1cSAntonio Quartulli /* This is the offset of the "chaddr" field in the dhcp packet starting at the 39*6c413b1cSAntonio Quartulli * beginning of the dhcp header 40*6c413b1cSAntonio Quartulli */ 41*6c413b1cSAntonio 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; 13256303d34SSven Eckelmann struct batadv_gw_node *gw_node, *curr_gw = NULL; 133c6c8fea2SSven Eckelmann uint32_t max_gw_factor = 0, tmp_gw_factor = 0; 134c67893d1SSven Eckelmann uint32_t gw_divisor; 1352265c141SAntonio Quartulli uint8_t max_tq = 0; 136c67893d1SSven Eckelmann uint8_t tq_avg; 13756303d34SSven Eckelmann struct batadv_orig_node *orig_node; 138c6c8fea2SSven Eckelmann 139c67893d1SSven Eckelmann gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE; 140c67893d1SSven Eckelmann gw_divisor *= 64; 141c67893d1SSven Eckelmann 142c6c8fea2SSven Eckelmann rcu_read_lock(); 143b67bfe0dSSasha Levin hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 144e1a5382fSLinus Lüssing if (gw_node->deleted) 145c6c8fea2SSven Eckelmann continue; 146c6c8fea2SSven Eckelmann 14784d5e5e0SSven Eckelmann orig_node = gw_node->orig_node; 1487d211efcSSven Eckelmann router = batadv_orig_node_get_router(orig_node); 149e1a5382fSLinus Lüssing if (!router) 150c6c8fea2SSven Eckelmann continue; 151c6c8fea2SSven Eckelmann 1522265c141SAntonio Quartulli if (!atomic_inc_not_zero(&gw_node->refcount)) 1532265c141SAntonio Quartulli goto next; 1542265c141SAntonio Quartulli 1550538f759SAntonio Quartulli tq_avg = router->bat_iv.tq_avg; 156c67893d1SSven Eckelmann 157c6c8fea2SSven Eckelmann switch (atomic_read(&bat_priv->gw_sel_class)) { 158c6c8fea2SSven Eckelmann case 1: /* fast connection */ 159414254e3SMarek Lindner tmp_gw_factor = tq_avg * tq_avg; 160414254e3SMarek Lindner tmp_gw_factor *= gw_node->bandwidth_down; 161414254e3SMarek Lindner tmp_gw_factor *= 100 * 100; 162c67893d1SSven Eckelmann tmp_gw_factor /= gw_divisor; 163c6c8fea2SSven Eckelmann 164c6c8fea2SSven Eckelmann if ((tmp_gw_factor > max_gw_factor) || 165c6c8fea2SSven Eckelmann ((tmp_gw_factor == max_gw_factor) && 166c67893d1SSven Eckelmann (tq_avg > max_tq))) { 1672265c141SAntonio Quartulli if (curr_gw) 1681409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 1692265c141SAntonio Quartulli curr_gw = gw_node; 1702265c141SAntonio Quartulli atomic_inc(&curr_gw->refcount); 1712265c141SAntonio Quartulli } 172c6c8fea2SSven Eckelmann break; 173c6c8fea2SSven Eckelmann 1749cfc7bd6SSven Eckelmann default: /* 2: stable connection (use best statistic) 175c6c8fea2SSven Eckelmann * 3: fast-switch (use best statistic but change as 176c6c8fea2SSven Eckelmann * soon as a better gateway appears) 177c6c8fea2SSven Eckelmann * XX: late-switch (use best statistic but change as 178c6c8fea2SSven Eckelmann * soon as a better gateway appears which has 179c6c8fea2SSven Eckelmann * $routing_class more tq points) 1809cfc7bd6SSven Eckelmann */ 181c67893d1SSven Eckelmann if (tq_avg > max_tq) { 1822265c141SAntonio Quartulli if (curr_gw) 1831409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 1842265c141SAntonio Quartulli curr_gw = gw_node; 1852265c141SAntonio Quartulli atomic_inc(&curr_gw->refcount); 1862265c141SAntonio Quartulli } 187c6c8fea2SSven Eckelmann break; 188c6c8fea2SSven Eckelmann } 189c6c8fea2SSven Eckelmann 190c67893d1SSven Eckelmann if (tq_avg > max_tq) 191c67893d1SSven Eckelmann max_tq = tq_avg; 192c6c8fea2SSven Eckelmann 193c6c8fea2SSven Eckelmann if (tmp_gw_factor > max_gw_factor) 194c6c8fea2SSven Eckelmann max_gw_factor = tmp_gw_factor; 195e1a5382fSLinus Lüssing 1961409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 1972265c141SAntonio Quartulli 1982265c141SAntonio Quartulli next: 1997d211efcSSven Eckelmann batadv_neigh_node_free_ref(router); 200c6c8fea2SSven Eckelmann } 2012265c141SAntonio Quartulli rcu_read_unlock(); 202c6c8fea2SSven Eckelmann 2032265c141SAntonio Quartulli return curr_gw; 2042265c141SAntonio Quartulli } 205e1a5382fSLinus Lüssing 206c6eaa3f0SAntonio Quartulli /** 207c6eaa3f0SAntonio Quartulli * batadv_gw_check_client_stop - check if client mode has been switched off 208c6eaa3f0SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 209c6eaa3f0SAntonio Quartulli * 210c6eaa3f0SAntonio Quartulli * This function assumes the caller has checked that the gw state *is actually 211c6eaa3f0SAntonio Quartulli * changing*. This function is not supposed to be called when there is no state 212c6eaa3f0SAntonio Quartulli * change. 213c6eaa3f0SAntonio Quartulli */ 214c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv) 215c6eaa3f0SAntonio Quartulli { 216c6eaa3f0SAntonio Quartulli struct batadv_gw_node *curr_gw; 217c6eaa3f0SAntonio Quartulli 218c6eaa3f0SAntonio Quartulli if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT) 219c6eaa3f0SAntonio Quartulli return; 220c6eaa3f0SAntonio Quartulli 221c6eaa3f0SAntonio Quartulli curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 222c6eaa3f0SAntonio Quartulli if (!curr_gw) 223c6eaa3f0SAntonio Quartulli return; 224c6eaa3f0SAntonio Quartulli 225f3163181SAntonio Quartulli /* deselect the current gateway so that next time that client mode is 226f3163181SAntonio Quartulli * enabled a proper GW_ADD event can be sent 227f3163181SAntonio Quartulli */ 228f3163181SAntonio Quartulli batadv_gw_select(bat_priv, NULL); 229f3163181SAntonio Quartulli 230c6eaa3f0SAntonio Quartulli /* if batman-adv is switching the gw client mode off and a gateway was 231c6eaa3f0SAntonio Quartulli * already selected, send a DEL uevent 232c6eaa3f0SAntonio Quartulli */ 233c6eaa3f0SAntonio Quartulli batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL); 234c6eaa3f0SAntonio Quartulli 235c6eaa3f0SAntonio Quartulli batadv_gw_node_free_ref(curr_gw); 236c6eaa3f0SAntonio Quartulli } 237c6eaa3f0SAntonio Quartulli 23856303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv) 2392265c141SAntonio Quartulli { 24056303d34SSven Eckelmann struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL; 24156303d34SSven Eckelmann struct batadv_neigh_node *router = NULL; 24219595e05SAntonio Quartulli char gw_addr[18] = { '\0' }; 2432265c141SAntonio Quartulli 244cd646ab1SSven Eckelmann if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT) 2452265c141SAntonio Quartulli goto out; 2462265c141SAntonio Quartulli 2471409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 2482265c141SAntonio Quartulli 249807736f6SSven Eckelmann if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw) 250caa0bf64SMarek Lindner goto out; 251caa0bf64SMarek Lindner 2521409a834SSven Eckelmann next_gw = batadv_gw_get_best_gw_node(bat_priv); 2532265c141SAntonio Quartulli 2542265c141SAntonio Quartulli if (curr_gw == next_gw) 2552265c141SAntonio Quartulli goto out; 2562265c141SAntonio Quartulli 2572265c141SAntonio Quartulli if (next_gw) { 25819595e05SAntonio Quartulli sprintf(gw_addr, "%pM", next_gw->orig_node->orig); 25919595e05SAntonio Quartulli 2607d211efcSSven Eckelmann router = batadv_orig_node_get_router(next_gw->orig_node); 2612265c141SAntonio Quartulli if (!router) { 2624e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 2632265c141SAntonio Quartulli goto out; 2642265c141SAntonio Quartulli } 2652265c141SAntonio Quartulli } 2662265c141SAntonio Quartulli 2672265c141SAntonio Quartulli if ((curr_gw) && (!next_gw)) { 26839c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 2692265c141SAntonio Quartulli "Removing selected gateway - no gateway in range\n"); 27039c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, 27139c75a51SSven Eckelmann NULL); 2722265c141SAntonio Quartulli } else if ((!curr_gw) && (next_gw)) { 27339c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 274414254e3SMarek Lindner "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 2751eda58bfSSven Eckelmann next_gw->orig_node->orig, 276414254e3SMarek Lindner next_gw->bandwidth_down / 10, 277414254e3SMarek Lindner next_gw->bandwidth_down % 10, 278414254e3SMarek Lindner next_gw->bandwidth_up / 10, 2790538f759SAntonio Quartulli next_gw->bandwidth_up % 10, router->bat_iv.tq_avg); 28039c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD, 28139c75a51SSven Eckelmann gw_addr); 2822265c141SAntonio Quartulli } else { 28339c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 284414254e3SMarek Lindner "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 2851eda58bfSSven Eckelmann next_gw->orig_node->orig, 286414254e3SMarek Lindner next_gw->bandwidth_down / 10, 287414254e3SMarek Lindner next_gw->bandwidth_down % 10, 288414254e3SMarek Lindner next_gw->bandwidth_up / 10, 2890538f759SAntonio Quartulli next_gw->bandwidth_up % 10, router->bat_iv.tq_avg); 29039c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE, 29139c75a51SSven Eckelmann gw_addr); 292c6c8fea2SSven Eckelmann } 293c6c8fea2SSven Eckelmann 2941409a834SSven Eckelmann batadv_gw_select(bat_priv, next_gw); 2952265c141SAntonio Quartulli 296c4aac1abSMarek Lindner out: 297c4aac1abSMarek Lindner if (curr_gw) 2981409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 2992265c141SAntonio Quartulli if (next_gw) 3001409a834SSven Eckelmann batadv_gw_node_free_ref(next_gw); 3012265c141SAntonio Quartulli if (router) 3027d211efcSSven Eckelmann batadv_neigh_node_free_ref(router); 303c6c8fea2SSven Eckelmann } 304c6c8fea2SSven Eckelmann 30556303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv, 30656303d34SSven Eckelmann struct batadv_orig_node *orig_node) 307c6c8fea2SSven Eckelmann { 30856303d34SSven Eckelmann struct batadv_orig_node *curr_gw_orig; 30956303d34SSven Eckelmann struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL; 310c6c8fea2SSven Eckelmann uint8_t gw_tq_avg, orig_tq_avg; 311c6c8fea2SSven Eckelmann 3127cf06bc6SSven Eckelmann curr_gw_orig = batadv_gw_get_selected_orig(bat_priv); 31357f0c07cSLinus Lüssing if (!curr_gw_orig) 3144e820e72SAntonio Quartulli goto reselect; 315c6c8fea2SSven Eckelmann 3167d211efcSSven Eckelmann router_gw = batadv_orig_node_get_router(curr_gw_orig); 317e1a5382fSLinus Lüssing if (!router_gw) 3184e820e72SAntonio Quartulli goto reselect; 319c6c8fea2SSven Eckelmann 320c6c8fea2SSven Eckelmann /* this node already is the gateway */ 32157f0c07cSLinus Lüssing if (curr_gw_orig == orig_node) 322e1a5382fSLinus Lüssing goto out; 323c6c8fea2SSven Eckelmann 3247d211efcSSven Eckelmann router_orig = batadv_orig_node_get_router(orig_node); 325e1a5382fSLinus Lüssing if (!router_orig) 326e1a5382fSLinus Lüssing goto out; 327c6c8fea2SSven Eckelmann 3280538f759SAntonio Quartulli gw_tq_avg = router_gw->bat_iv.tq_avg; 3290538f759SAntonio Quartulli orig_tq_avg = router_orig->bat_iv.tq_avg; 330c6c8fea2SSven Eckelmann 331c6c8fea2SSven Eckelmann /* the TQ value has to be better */ 332c6c8fea2SSven Eckelmann if (orig_tq_avg < gw_tq_avg) 3335d02b3cdSLinus Lüssing goto out; 334c6c8fea2SSven Eckelmann 3359cfc7bd6SSven Eckelmann /* if the routing class is greater than 3 the value tells us how much 336c6c8fea2SSven Eckelmann * greater the TQ value of the new gateway must be 3379cfc7bd6SSven Eckelmann */ 338c6c8fea2SSven Eckelmann if ((atomic_read(&bat_priv->gw_sel_class) > 3) && 339c6c8fea2SSven Eckelmann (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class))) 3405d02b3cdSLinus Lüssing goto out; 341c6c8fea2SSven Eckelmann 34239c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 34386ceb360SSven Eckelmann "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n", 344c6c8fea2SSven Eckelmann gw_tq_avg, orig_tq_avg); 345c6c8fea2SSven Eckelmann 3464e820e72SAntonio Quartulli reselect: 3474e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 3485d02b3cdSLinus Lüssing out: 34957f0c07cSLinus Lüssing if (curr_gw_orig) 3507d211efcSSven Eckelmann batadv_orig_node_free_ref(curr_gw_orig); 351e1a5382fSLinus Lüssing if (router_gw) 3527d211efcSSven Eckelmann batadv_neigh_node_free_ref(router_gw); 353e1a5382fSLinus Lüssing if (router_orig) 3547d211efcSSven Eckelmann batadv_neigh_node_free_ref(router_orig); 35557f0c07cSLinus Lüssing 3565d02b3cdSLinus Lüssing return; 357c6c8fea2SSven Eckelmann } 358c6c8fea2SSven Eckelmann 359414254e3SMarek Lindner /** 360414254e3SMarek Lindner * batadv_gw_node_add - add gateway node to list of available gateways 361414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 362414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 363414254e3SMarek Lindner * @gateway: announced bandwidth information 364414254e3SMarek Lindner */ 36556303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv, 36656303d34SSven Eckelmann struct batadv_orig_node *orig_node, 367414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 368c6c8fea2SSven Eckelmann { 36956303d34SSven Eckelmann struct batadv_gw_node *gw_node; 370414254e3SMarek Lindner 371414254e3SMarek Lindner if (gateway->bandwidth_down == 0) 372414254e3SMarek Lindner return; 373c6c8fea2SSven Eckelmann 374704509b8SSven Eckelmann gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); 375c6c8fea2SSven Eckelmann if (!gw_node) 376c6c8fea2SSven Eckelmann return; 377c6c8fea2SSven Eckelmann 378c6c8fea2SSven Eckelmann INIT_HLIST_NODE(&gw_node->list); 379c6c8fea2SSven Eckelmann gw_node->orig_node = orig_node; 38025b6d3c1SMarek Lindner atomic_set(&gw_node->refcount, 1); 381c6c8fea2SSven Eckelmann 382807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 383807736f6SSven Eckelmann hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list); 384807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 385c6c8fea2SSven Eckelmann 38639c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 387414254e3SMarek Lindner "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", 388414254e3SMarek Lindner orig_node->orig, 389414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 390414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 391414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 392414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 393c6c8fea2SSven Eckelmann } 394c6c8fea2SSven Eckelmann 395414254e3SMarek Lindner /** 396414254e3SMarek Lindner * batadv_gw_node_get - retrieve gateway node from 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 * 400414254e3SMarek Lindner * Returns gateway node if found or NULL otherwise. 40171e4aa9cSAntonio Quartulli */ 402414254e3SMarek Lindner static struct batadv_gw_node * 403414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv, 404414254e3SMarek Lindner struct batadv_orig_node *orig_node) 405414254e3SMarek Lindner { 406414254e3SMarek Lindner struct batadv_gw_node *gw_node_tmp, *gw_node = NULL; 407c6c8fea2SSven Eckelmann 408c6c8fea2SSven Eckelmann rcu_read_lock(); 409414254e3SMarek Lindner hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) { 410414254e3SMarek Lindner if (gw_node_tmp->orig_node != orig_node) 411c6c8fea2SSven Eckelmann continue; 412c6c8fea2SSven Eckelmann 413414254e3SMarek Lindner if (gw_node_tmp->deleted) 414414254e3SMarek Lindner continue; 415414254e3SMarek Lindner 416414254e3SMarek Lindner if (!atomic_inc_not_zero(&gw_node_tmp->refcount)) 417414254e3SMarek Lindner continue; 418414254e3SMarek Lindner 419414254e3SMarek Lindner gw_node = gw_node_tmp; 420414254e3SMarek Lindner break; 421414254e3SMarek Lindner } 422414254e3SMarek Lindner rcu_read_unlock(); 423414254e3SMarek Lindner 424414254e3SMarek Lindner return gw_node; 425414254e3SMarek Lindner } 426414254e3SMarek Lindner 427414254e3SMarek Lindner /** 428414254e3SMarek Lindner * batadv_gw_node_update - update list of available gateways with changed 429414254e3SMarek Lindner * bandwidth information 430414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information 431414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities 432414254e3SMarek Lindner * @gateway: announced bandwidth information 433414254e3SMarek Lindner */ 434414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv, 435414254e3SMarek Lindner struct batadv_orig_node *orig_node, 436414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway) 437414254e3SMarek Lindner { 438414254e3SMarek Lindner struct batadv_gw_node *gw_node, *curr_gw = NULL; 439414254e3SMarek Lindner 440414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_node); 441414254e3SMarek Lindner if (!gw_node) { 442414254e3SMarek Lindner batadv_gw_node_add(bat_priv, orig_node, gateway); 443414254e3SMarek Lindner goto out; 444414254e3SMarek Lindner } 445414254e3SMarek Lindner 446414254e3SMarek Lindner if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) && 447414254e3SMarek Lindner (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))) 448414254e3SMarek Lindner goto out; 449414254e3SMarek Lindner 45039c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 451414254e3SMarek Lindner "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n", 452414254e3SMarek Lindner orig_node->orig, 453414254e3SMarek Lindner gw_node->bandwidth_down / 10, 454414254e3SMarek Lindner gw_node->bandwidth_down % 10, 455414254e3SMarek Lindner gw_node->bandwidth_up / 10, 456414254e3SMarek Lindner gw_node->bandwidth_up % 10, 457414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10, 458414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10, 459414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10, 460414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10); 461414254e3SMarek Lindner 462414254e3SMarek Lindner gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 463414254e3SMarek Lindner gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 464c6c8fea2SSven Eckelmann 465c6c8fea2SSven Eckelmann gw_node->deleted = 0; 466414254e3SMarek Lindner if (ntohl(gateway->bandwidth_down) == 0) { 467c6c8fea2SSven Eckelmann gw_node->deleted = jiffies; 46839c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 469c6c8fea2SSven Eckelmann "Gateway %pM removed from gateway list\n", 470c6c8fea2SSven Eckelmann orig_node->orig); 471c6c8fea2SSven Eckelmann 472414254e3SMarek Lindner /* Note: We don't need a NULL check here, since curr_gw never 473414254e3SMarek Lindner * gets dereferenced. 474414254e3SMarek Lindner */ 475414254e3SMarek Lindner curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 476c4aac1abSMarek Lindner if (gw_node == curr_gw) 4774e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 478414254e3SMarek Lindner } 47971e4aa9cSAntonio Quartulli 480414254e3SMarek Lindner out: 481c4aac1abSMarek Lindner if (curr_gw) 4821409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 483414254e3SMarek Lindner if (gw_node) 484414254e3SMarek Lindner batadv_gw_node_free_ref(gw_node); 485c6c8fea2SSven Eckelmann } 486c6c8fea2SSven Eckelmann 48756303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv, 48856303d34SSven Eckelmann struct batadv_orig_node *orig_node) 489c6c8fea2SSven Eckelmann { 490414254e3SMarek Lindner struct batadv_tvlv_gateway_data gateway; 491414254e3SMarek Lindner 492414254e3SMarek Lindner gateway.bandwidth_down = 0; 493414254e3SMarek Lindner gateway.bandwidth_up = 0; 494414254e3SMarek Lindner 495414254e3SMarek Lindner batadv_gw_node_update(bat_priv, orig_node, &gateway); 496c6c8fea2SSven Eckelmann } 497c6c8fea2SSven Eckelmann 49856303d34SSven Eckelmann void batadv_gw_node_purge(struct batadv_priv *bat_priv) 499c6c8fea2SSven Eckelmann { 50056303d34SSven Eckelmann struct batadv_gw_node *gw_node, *curr_gw; 501b67bfe0dSSasha Levin struct hlist_node *node_tmp; 50242d0b044SSven Eckelmann unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT); 5034e820e72SAntonio Quartulli int do_reselect = 0; 504c4aac1abSMarek Lindner 5051409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 506c6c8fea2SSven Eckelmann 507807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock); 508c6c8fea2SSven Eckelmann 509b67bfe0dSSasha Levin hlist_for_each_entry_safe(gw_node, node_tmp, 510807736f6SSven Eckelmann &bat_priv->gw.list, list) { 511c6c8fea2SSven Eckelmann if (((!gw_node->deleted) || 512c6c8fea2SSven Eckelmann (time_before(jiffies, gw_node->deleted + timeout))) && 51339c75a51SSven Eckelmann atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE) 514c6c8fea2SSven Eckelmann continue; 515c6c8fea2SSven Eckelmann 516c4aac1abSMarek Lindner if (curr_gw == gw_node) 5174e820e72SAntonio Quartulli do_reselect = 1; 518c6c8fea2SSven Eckelmann 519c6c8fea2SSven Eckelmann hlist_del_rcu(&gw_node->list); 5201409a834SSven Eckelmann batadv_gw_node_free_ref(gw_node); 521c6c8fea2SSven Eckelmann } 522c6c8fea2SSven Eckelmann 523807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock); 524c4aac1abSMarek Lindner 5254e820e72SAntonio Quartulli /* gw_reselect() needs to acquire the gw_list_lock */ 5264e820e72SAntonio Quartulli if (do_reselect) 5274e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv); 528c4aac1abSMarek Lindner 529c4aac1abSMarek Lindner if (curr_gw) 5301409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 531c6c8fea2SSven Eckelmann } 532c6c8fea2SSven Eckelmann 5339cfc7bd6SSven Eckelmann /* fails if orig_node has no router */ 53456303d34SSven Eckelmann static int batadv_write_buffer_text(struct batadv_priv *bat_priv, 5351409a834SSven Eckelmann struct seq_file *seq, 53656303d34SSven Eckelmann const struct batadv_gw_node *gw_node) 537c6c8fea2SSven Eckelmann { 53856303d34SSven Eckelmann struct batadv_gw_node *curr_gw; 53956303d34SSven Eckelmann struct batadv_neigh_node *router; 540414254e3SMarek Lindner int ret = -1; 541c6c8fea2SSven Eckelmann 5427d211efcSSven Eckelmann router = batadv_orig_node_get_router(gw_node->orig_node); 543e1a5382fSLinus Lüssing if (!router) 544e1a5382fSLinus Lüssing goto out; 545e1a5382fSLinus Lüssing 5461409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 5475d02b3cdSLinus Lüssing 548414254e3SMarek Lindner ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n", 5495d02b3cdSLinus Lüssing (curr_gw == gw_node ? "=>" : " "), 550c6c8fea2SSven Eckelmann gw_node->orig_node->orig, 5510538f759SAntonio Quartulli router->bat_iv.tq_avg, router->addr, 552e1a5382fSLinus Lüssing router->if_incoming->net_dev->name, 553414254e3SMarek Lindner gw_node->bandwidth_down / 10, 554414254e3SMarek Lindner gw_node->bandwidth_down % 10, 555414254e3SMarek Lindner gw_node->bandwidth_up / 10, 556414254e3SMarek Lindner gw_node->bandwidth_up % 10); 5575d02b3cdSLinus Lüssing 5587d211efcSSven Eckelmann batadv_neigh_node_free_ref(router); 559c4aac1abSMarek Lindner if (curr_gw) 5601409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 561e1a5382fSLinus Lüssing out: 5625d02b3cdSLinus Lüssing return ret; 563c6c8fea2SSven Eckelmann } 564c6c8fea2SSven Eckelmann 5657cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset) 566c6c8fea2SSven Eckelmann { 567c6c8fea2SSven Eckelmann struct net_device *net_dev = (struct net_device *)seq->private; 56856303d34SSven Eckelmann struct batadv_priv *bat_priv = netdev_priv(net_dev); 56956303d34SSven Eckelmann struct batadv_hard_iface *primary_if; 57056303d34SSven Eckelmann struct batadv_gw_node *gw_node; 57130da63a6SMarek Lindner int gw_count = 0; 572c6c8fea2SSven Eckelmann 57330da63a6SMarek Lindner primary_if = batadv_seq_print_text_primary_if_get(seq); 57430da63a6SMarek Lindner if (!primary_if) 57532ae9b22SMarek Lindner goto out; 576c6c8fea2SSven Eckelmann 57786ceb360SSven Eckelmann seq_printf(seq, 578414254e3SMarek Lindner " %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", 57942d0b044SSven Eckelmann "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF", 58042d0b044SSven Eckelmann BATADV_SOURCE_VERSION, primary_if->net_dev->name, 58132ae9b22SMarek Lindner primary_if->net_dev->dev_addr, net_dev->name); 582c6c8fea2SSven Eckelmann 583c6c8fea2SSven Eckelmann rcu_read_lock(); 584b67bfe0dSSasha Levin hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 585c6c8fea2SSven Eckelmann if (gw_node->deleted) 586c6c8fea2SSven Eckelmann continue; 587c6c8fea2SSven Eckelmann 588e1a5382fSLinus Lüssing /* fails if orig_node has no router */ 5891409a834SSven Eckelmann if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0) 590c6c8fea2SSven Eckelmann continue; 591c6c8fea2SSven Eckelmann 592c6c8fea2SSven Eckelmann gw_count++; 593c6c8fea2SSven Eckelmann } 594c6c8fea2SSven Eckelmann rcu_read_unlock(); 595c6c8fea2SSven Eckelmann 596c6c8fea2SSven Eckelmann if (gw_count == 0) 5970c814653SAntonio Quartulli seq_puts(seq, "No gateways in range ...\n"); 598c6c8fea2SSven Eckelmann 59932ae9b22SMarek Lindner out: 60032ae9b22SMarek Lindner if (primary_if) 601e5d89254SSven Eckelmann batadv_hardif_free_ref(primary_if); 60230da63a6SMarek Lindner return 0; 603c6c8fea2SSven Eckelmann } 604c6c8fea2SSven Eckelmann 605*6c413b1cSAntonio Quartulli /** 606*6c413b1cSAntonio Quartulli * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message 607*6c413b1cSAntonio Quartulli * @skb: the packet to check 608*6c413b1cSAntonio Quartulli * @header_len: a pointer to the batman-adv header size 609*6c413b1cSAntonio Quartulli * @chaddr: buffer where the client address will be stored. Valid 610*6c413b1cSAntonio Quartulli * only if the function returns BATADV_DHCP_TO_CLIENT 611*6c413b1cSAntonio Quartulli * 612*6c413b1cSAntonio Quartulli * Returns: 613*6c413b1cSAntonio Quartulli * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error 614*6c413b1cSAntonio Quartulli * while parsing it 615*6c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server 616*6c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client 617*6c413b1cSAntonio Quartulli * 618*6c413b1cSAntonio Quartulli * This function may re-allocate the data buffer of the skb passed as argument. 6199cfc7bd6SSven Eckelmann */ 620*6c413b1cSAntonio Quartulli enum batadv_dhcp_recipient 621*6c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len, 622*6c413b1cSAntonio Quartulli uint8_t *chaddr) 623c6c8fea2SSven Eckelmann { 624*6c413b1cSAntonio Quartulli enum batadv_dhcp_recipient ret = BATADV_DHCP_NO; 625c6c8fea2SSven Eckelmann struct ethhdr *ethhdr; 626c6c8fea2SSven Eckelmann struct iphdr *iphdr; 627c6c8fea2SSven Eckelmann struct ipv6hdr *ipv6hdr; 628c6c8fea2SSven Eckelmann struct udphdr *udphdr; 629f7f8ed56SAntonio Quartulli struct vlan_ethhdr *vhdr; 630*6c413b1cSAntonio Quartulli int chaddr_offset; 631f7f8ed56SAntonio Quartulli __be16 proto; 632*6c413b1cSAntonio Quartulli uint8_t *p; 633c6c8fea2SSven Eckelmann 634c6c8fea2SSven Eckelmann /* check for ethernet header */ 635be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) 636*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 637*6c413b1cSAntonio Quartulli 638c6c8fea2SSven Eckelmann ethhdr = (struct ethhdr *)skb->data; 639f7f8ed56SAntonio Quartulli proto = ethhdr->h_proto; 640be7af5cfSMarek Lindner *header_len += ETH_HLEN; 641c6c8fea2SSven Eckelmann 642c6c8fea2SSven Eckelmann /* check for initial vlan header */ 643f7f8ed56SAntonio Quartulli if (proto == htons(ETH_P_8021Q)) { 644be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) 645*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 646f7f8ed56SAntonio Quartulli 647f7f8ed56SAntonio Quartulli vhdr = (struct vlan_ethhdr *)skb->data; 648f7f8ed56SAntonio Quartulli proto = vhdr->h_vlan_encapsulated_proto; 649be7af5cfSMarek Lindner *header_len += VLAN_HLEN; 650c6c8fea2SSven Eckelmann } 651c6c8fea2SSven Eckelmann 652c6c8fea2SSven Eckelmann /* check for ip header */ 653f7f8ed56SAntonio Quartulli switch (proto) { 654f7f8ed56SAntonio Quartulli case htons(ETH_P_IP): 655be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) 656*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 657*6c413b1cSAntonio Quartulli 658be7af5cfSMarek Lindner iphdr = (struct iphdr *)(skb->data + *header_len); 659be7af5cfSMarek Lindner *header_len += iphdr->ihl * 4; 660c6c8fea2SSven Eckelmann 661c6c8fea2SSven Eckelmann /* check for udp header */ 662c6c8fea2SSven Eckelmann if (iphdr->protocol != IPPROTO_UDP) 663*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 664c6c8fea2SSven Eckelmann 665c6c8fea2SSven Eckelmann break; 666f7f8ed56SAntonio Quartulli case htons(ETH_P_IPV6): 667be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) 668*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 669*6c413b1cSAntonio Quartulli 670be7af5cfSMarek Lindner ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); 671be7af5cfSMarek Lindner *header_len += sizeof(*ipv6hdr); 672c6c8fea2SSven Eckelmann 673c6c8fea2SSven Eckelmann /* check for udp header */ 674c6c8fea2SSven Eckelmann if (ipv6hdr->nexthdr != IPPROTO_UDP) 675*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 676c6c8fea2SSven Eckelmann 677c6c8fea2SSven Eckelmann break; 678c6c8fea2SSven Eckelmann default: 679*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 680c6c8fea2SSven Eckelmann } 681c6c8fea2SSven Eckelmann 682be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr))) 683*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 6849d2c9488SLinus Lüssing 6859d2c9488SLinus Lüssing /* skb->data might have been reallocated by pskb_may_pull() */ 6869d2c9488SLinus Lüssing ethhdr = (struct ethhdr *)skb->data; 6879d2c9488SLinus Lüssing if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) 6889d2c9488SLinus Lüssing ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN); 6899d2c9488SLinus Lüssing 690be7af5cfSMarek Lindner udphdr = (struct udphdr *)(skb->data + *header_len); 691be7af5cfSMarek Lindner *header_len += sizeof(*udphdr); 692c6c8fea2SSven Eckelmann 693c6c8fea2SSven Eckelmann /* check for bootp port */ 694*6c413b1cSAntonio Quartulli switch (proto) { 695*6c413b1cSAntonio Quartulli case htons(ETH_P_IP): 696*6c413b1cSAntonio Quartulli if (udphdr->dest == htons(67)) 697*6c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 698*6c413b1cSAntonio Quartulli else if (udphdr->source == htons(67)) 699*6c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 700*6c413b1cSAntonio Quartulli break; 701*6c413b1cSAntonio Quartulli case htons(ETH_P_IPV6): 702*6c413b1cSAntonio Quartulli if (udphdr->dest == htons(547)) 703*6c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER; 704*6c413b1cSAntonio Quartulli else if (udphdr->source == htons(547)) 705*6c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT; 706*6c413b1cSAntonio Quartulli break; 707be7af5cfSMarek Lindner } 708c6c8fea2SSven Eckelmann 709*6c413b1cSAntonio Quartulli chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET; 710*6c413b1cSAntonio Quartulli /* store the client address if the message is going to a client */ 711*6c413b1cSAntonio Quartulli if (ret == BATADV_DHCP_TO_CLIENT && 712*6c413b1cSAntonio Quartulli pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) { 713*6c413b1cSAntonio Quartulli /* check if the DHCP packet carries an Ethernet DHCP */ 714*6c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET; 715*6c413b1cSAntonio Quartulli if (*p != BATADV_DHCP_HTYPE_ETHERNET) 716*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 717*6c413b1cSAntonio Quartulli 718*6c413b1cSAntonio Quartulli /* check if the DHCP packet carries a valid Ethernet address */ 719*6c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET; 720*6c413b1cSAntonio Quartulli if (*p != ETH_ALEN) 721*6c413b1cSAntonio Quartulli return BATADV_DHCP_NO; 722*6c413b1cSAntonio Quartulli 723*6c413b1cSAntonio Quartulli memcpy(chaddr, skb->data + chaddr_offset, ETH_ALEN); 724*6c413b1cSAntonio Quartulli } 725*6c413b1cSAntonio Quartulli 726*6c413b1cSAntonio Quartulli return ret; 727*6c413b1cSAntonio Quartulli } 728bbb877edSAntonio Quartulli /** 729bbb877edSAntonio Quartulli * batadv_gw_out_of_range - check if the dhcp request destination is the best gw 730bbb877edSAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information 731bbb877edSAntonio Quartulli * @skb: the outgoing packet 732bbb877edSAntonio Quartulli * 733bbb877edSAntonio Quartulli * Check if the skb is a DHCP request and if it is sent to the current best GW 734bbb877edSAntonio Quartulli * server. Due to topology changes it may be the case that the GW server 735bbb877edSAntonio Quartulli * previously selected is not the best one anymore. 736bbb877edSAntonio Quartulli * 737bbb877edSAntonio Quartulli * Returns true if the packet destination is unicast and it is not the best gw, 738bbb877edSAntonio Quartulli * false otherwise. 739bbb877edSAntonio Quartulli * 740bbb877edSAntonio Quartulli * This call might reallocate skb data. 741*6c413b1cSAntonio Quartulli * Must be invoked only when the DHCP packet is going TO a DHCP SERVER. 742bbb877edSAntonio Quartulli */ 74356303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, 7449d2c9488SLinus Lüssing struct sk_buff *skb) 745be7af5cfSMarek Lindner { 74656303d34SSven Eckelmann struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL; 74756303d34SSven Eckelmann struct batadv_orig_node *orig_dst_node = NULL; 748414254e3SMarek Lindner struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL; 749*6c413b1cSAntonio Quartulli struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 750*6c413b1cSAntonio Quartulli bool out_of_range = false; 751be7af5cfSMarek Lindner uint8_t curr_tq_avg; 752bbb877edSAntonio Quartulli unsigned short vid; 753bbb877edSAntonio Quartulli 754bbb877edSAntonio Quartulli vid = batadv_get_vid(skb, 0); 755be7af5cfSMarek Lindner 75608c36d3eSSven Eckelmann orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, 757bbb877edSAntonio Quartulli ethhdr->h_dest, vid); 758be7af5cfSMarek Lindner if (!orig_dst_node) 759be7af5cfSMarek Lindner goto out; 760be7af5cfSMarek Lindner 761414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_dst_node); 762414254e3SMarek Lindner if (!gw_node->bandwidth_down == 0) 763be7af5cfSMarek Lindner goto out; 764be7af5cfSMarek Lindner 765be7af5cfSMarek Lindner switch (atomic_read(&bat_priv->gw_mode)) { 766cd646ab1SSven Eckelmann case BATADV_GW_MODE_SERVER: 767be7af5cfSMarek Lindner /* If we are a GW then we are our best GW. We can artificially 7689cfc7bd6SSven Eckelmann * set the tq towards ourself as the maximum value 7699cfc7bd6SSven Eckelmann */ 77042d0b044SSven Eckelmann curr_tq_avg = BATADV_TQ_MAX_VALUE; 771be7af5cfSMarek Lindner break; 772cd646ab1SSven Eckelmann case BATADV_GW_MODE_CLIENT: 7731409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 774c4aac1abSMarek Lindner if (!curr_gw) 775be7af5cfSMarek Lindner goto out; 776c6c8fea2SSven Eckelmann 777be7af5cfSMarek Lindner /* packet is going to our gateway */ 778be7af5cfSMarek Lindner if (curr_gw->orig_node == orig_dst_node) 779be7af5cfSMarek Lindner goto out; 780be7af5cfSMarek Lindner 78143676ab5SAntonio Quartulli /* If the dhcp packet has been sent to a different gw, 78243676ab5SAntonio Quartulli * we have to evaluate whether the old gw is still 7839cfc7bd6SSven Eckelmann * reliable enough 7849cfc7bd6SSven Eckelmann */ 78530d3c511SSven Eckelmann neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node, 78630d3c511SSven Eckelmann NULL); 787be7af5cfSMarek Lindner if (!neigh_curr) 788be7af5cfSMarek Lindner goto out; 789be7af5cfSMarek Lindner 7900538f759SAntonio Quartulli curr_tq_avg = neigh_curr->bat_iv.tq_avg; 791be7af5cfSMarek Lindner break; 792cd646ab1SSven Eckelmann case BATADV_GW_MODE_OFF: 793be7af5cfSMarek Lindner default: 794be7af5cfSMarek Lindner goto out; 79543676ab5SAntonio Quartulli } 796be7af5cfSMarek Lindner 79730d3c511SSven Eckelmann neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL); 7982ef04f47SDan Carpenter if (!neigh_old) 799be7af5cfSMarek Lindner goto out; 800be7af5cfSMarek Lindner 8010538f759SAntonio Quartulli if (curr_tq_avg - neigh_old->bat_iv.tq_avg > BATADV_GW_THRESHOLD) 802be7af5cfSMarek Lindner out_of_range = true; 803be7af5cfSMarek Lindner 804be7af5cfSMarek Lindner out: 805be7af5cfSMarek Lindner if (orig_dst_node) 8067d211efcSSven Eckelmann batadv_orig_node_free_ref(orig_dst_node); 807be7af5cfSMarek Lindner if (curr_gw) 8081409a834SSven Eckelmann batadv_gw_node_free_ref(curr_gw); 809414254e3SMarek Lindner if (gw_node) 810414254e3SMarek Lindner batadv_gw_node_free_ref(gw_node); 81143676ab5SAntonio Quartulli if (neigh_old) 8127d211efcSSven Eckelmann batadv_neigh_node_free_ref(neigh_old); 81343676ab5SAntonio Quartulli if (neigh_curr) 8147d211efcSSven Eckelmann batadv_neigh_node_free_ref(neigh_curr); 815be7af5cfSMarek Lindner return out_of_range; 816c6c8fea2SSven Eckelmann } 817