xref: /openbmc/linux/net/batman-adv/gateway_client.c (revision 50164d8f500f1cd211178f7b3d062987d68fe013)
10046b040SSven Eckelmann /* Copyright (C) 2009-2016  B.A.T.M.A.N. contributors:
2c6c8fea2SSven Eckelmann  *
3c6c8fea2SSven Eckelmann  * Marek Lindner
4c6c8fea2SSven Eckelmann  *
5c6c8fea2SSven Eckelmann  * This program is free software; you can redistribute it and/or
6c6c8fea2SSven Eckelmann  * modify it under the terms of version 2 of the GNU General Public
7c6c8fea2SSven Eckelmann  * License as published by the Free Software Foundation.
8c6c8fea2SSven Eckelmann  *
9c6c8fea2SSven Eckelmann  * This program is distributed in the hope that it will be useful, but
10c6c8fea2SSven Eckelmann  * WITHOUT ANY WARRANTY; without even the implied warranty of
11c6c8fea2SSven Eckelmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12c6c8fea2SSven Eckelmann  * General Public License for more details.
13c6c8fea2SSven Eckelmann  *
14c6c8fea2SSven Eckelmann  * You should have received a copy of the GNU General Public License
15ebf38fb7SAntonio Quartulli  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16c6c8fea2SSven Eckelmann  */
17c6c8fea2SSven Eckelmann 
18c6c8fea2SSven Eckelmann #include "gateway_client.h"
191e2c2a4fSSven Eckelmann #include "main.h"
201e2c2a4fSSven Eckelmann 
211e2c2a4fSSven Eckelmann #include <linux/atomic.h>
221e2c2a4fSSven Eckelmann #include <linux/byteorder/generic.h>
231e2c2a4fSSven Eckelmann #include <linux/etherdevice.h>
241e2c2a4fSSven Eckelmann #include <linux/fs.h>
251e2c2a4fSSven Eckelmann #include <linux/if_ether.h>
261e2c2a4fSSven Eckelmann #include <linux/if_vlan.h>
271e2c2a4fSSven Eckelmann #include <linux/in.h>
281e2c2a4fSSven Eckelmann #include <linux/ip.h>
291e2c2a4fSSven Eckelmann #include <linux/ipv6.h>
301e2c2a4fSSven Eckelmann #include <linux/kernel.h>
31e7aed321SSven Eckelmann #include <linux/kref.h>
321e2c2a4fSSven Eckelmann #include <linux/list.h>
331e2c2a4fSSven Eckelmann #include <linux/netdevice.h>
341e2c2a4fSSven Eckelmann #include <linux/rculist.h>
351e2c2a4fSSven Eckelmann #include <linux/rcupdate.h>
361e2c2a4fSSven Eckelmann #include <linux/seq_file.h>
371e2c2a4fSSven Eckelmann #include <linux/skbuff.h>
381e2c2a4fSSven Eckelmann #include <linux/slab.h>
391e2c2a4fSSven Eckelmann #include <linux/spinlock.h>
401e2c2a4fSSven Eckelmann #include <linux/stddef.h>
411e2c2a4fSSven Eckelmann #include <linux/udp.h>
421e2c2a4fSSven Eckelmann 
43c6c8fea2SSven Eckelmann #include "gateway_common.h"
44c6c8fea2SSven Eckelmann #include "hard-interface.h"
45ba412080SSven Eckelmann #include "log.h"
4657f0c07cSLinus Lüssing #include "originator.h"
471e2c2a4fSSven Eckelmann #include "packet.h"
4843676ab5SAntonio Quartulli #include "routing.h"
491e2c2a4fSSven Eckelmann #include "sysfs.h"
501e2c2a4fSSven Eckelmann #include "translation-table.h"
51c6c8fea2SSven Eckelmann 
526c413b1cSAntonio Quartulli /* These are the offsets of the "hw type" and "hw address length" in the dhcp
536c413b1cSAntonio Quartulli  * packet starting at the beginning of the dhcp header
549cfc7bd6SSven Eckelmann  */
556c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_OFFSET	1
566c413b1cSAntonio Quartulli #define BATADV_DHCP_HLEN_OFFSET		2
576c413b1cSAntonio Quartulli /* Value of htype representing Ethernet */
586c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_ETHERNET	0x01
596c413b1cSAntonio Quartulli /* This is the offset of the "chaddr" field in the dhcp packet starting at the
606c413b1cSAntonio Quartulli  * beginning of the dhcp header
616c413b1cSAntonio Quartulli  */
626c413b1cSAntonio Quartulli #define BATADV_DHCP_CHADDR_OFFSET	28
6343676ab5SAntonio Quartulli 
64e7aed321SSven Eckelmann /**
65e7aed321SSven Eckelmann  * batadv_gw_node_release - release gw_node from lists and queue for free after
66e7aed321SSven Eckelmann  *  rcu grace period
67e7aed321SSven Eckelmann  * @ref: kref pointer of the gw_node
68e7aed321SSven Eckelmann  */
69e7aed321SSven Eckelmann static void batadv_gw_node_release(struct kref *ref)
7025b6d3c1SMarek Lindner {
71e7aed321SSven Eckelmann 	struct batadv_gw_node *gw_node;
72e7aed321SSven Eckelmann 
73e7aed321SSven Eckelmann 	gw_node = container_of(ref, struct batadv_gw_node, refcount);
74e7aed321SSven Eckelmann 
755d967310SSven Eckelmann 	batadv_orig_node_put(gw_node->orig_node);
76eb340b2fSPaul E. McKenney 	kfree_rcu(gw_node, rcu);
77c6c8fea2SSven Eckelmann }
78e7aed321SSven Eckelmann 
79e7aed321SSven Eckelmann /**
803a01743dSSven Eckelmann  * batadv_gw_node_put - decrement the gw_node refcounter and possibly release it
81e7aed321SSven Eckelmann  * @gw_node: gateway node to free
82e7aed321SSven Eckelmann  */
8334d99cfeSAntonio Quartulli void batadv_gw_node_put(struct batadv_gw_node *gw_node)
84e7aed321SSven Eckelmann {
85e7aed321SSven Eckelmann 	kref_put(&gw_node->refcount, batadv_gw_node_release);
86377fe0f9SAntonio Quartulli }
87c6c8fea2SSven Eckelmann 
8834d99cfeSAntonio Quartulli struct batadv_gw_node *
8956303d34SSven Eckelmann batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
90c6c8fea2SSven Eckelmann {
9156303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
92c6c8fea2SSven Eckelmann 
935d02b3cdSLinus Lüssing 	rcu_read_lock();
94807736f6SSven Eckelmann 	gw_node = rcu_dereference(bat_priv->gw.curr_gw);
95c4aac1abSMarek Lindner 	if (!gw_node)
965d02b3cdSLinus Lüssing 		goto out;
97c6c8fea2SSven Eckelmann 
98e7aed321SSven Eckelmann 	if (!kref_get_unless_zero(&gw_node->refcount))
99c4aac1abSMarek Lindner 		gw_node = NULL;
100c4aac1abSMarek Lindner 
101c4aac1abSMarek Lindner out:
102c4aac1abSMarek Lindner 	rcu_read_unlock();
103c4aac1abSMarek Lindner 	return gw_node;
104c4aac1abSMarek Lindner }
105c4aac1abSMarek Lindner 
10656303d34SSven Eckelmann struct batadv_orig_node *
10756303d34SSven Eckelmann batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
108c4aac1abSMarek Lindner {
10956303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
11056303d34SSven Eckelmann 	struct batadv_orig_node *orig_node = NULL;
111c4aac1abSMarek Lindner 
1121409a834SSven Eckelmann 	gw_node = batadv_gw_get_selected_gw_node(bat_priv);
113c4aac1abSMarek Lindner 	if (!gw_node)
1147b36e8eeSMarek Lindner 		goto out;
1155d02b3cdSLinus Lüssing 
116c4aac1abSMarek Lindner 	rcu_read_lock();
117c4aac1abSMarek Lindner 	orig_node = gw_node->orig_node;
118c4aac1abSMarek Lindner 	if (!orig_node)
119c4aac1abSMarek Lindner 		goto unlock;
120c4aac1abSMarek Lindner 
1217c124391SSven Eckelmann 	if (!kref_get_unless_zero(&orig_node->refcount))
1227b36e8eeSMarek Lindner 		orig_node = NULL;
12343c70ad5SLinus Lüssing 
124c4aac1abSMarek Lindner unlock:
1255d02b3cdSLinus Lüssing 	rcu_read_unlock();
126c4aac1abSMarek Lindner out:
127c6c8fea2SSven Eckelmann 	if (gw_node)
1283a01743dSSven Eckelmann 		batadv_gw_node_put(gw_node);
129c4aac1abSMarek Lindner 	return orig_node;
130c6c8fea2SSven Eckelmann }
131c6c8fea2SSven Eckelmann 
13256303d34SSven Eckelmann static void batadv_gw_select(struct batadv_priv *bat_priv,
13356303d34SSven Eckelmann 			     struct batadv_gw_node *new_gw_node)
134c6c8fea2SSven Eckelmann {
13556303d34SSven Eckelmann 	struct batadv_gw_node *curr_gw_node;
136c6c8fea2SSven Eckelmann 
137807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
138c4aac1abSMarek Lindner 
139a08d497dSSven Eckelmann 	if (new_gw_node)
140a08d497dSSven Eckelmann 		kref_get(&new_gw_node->refcount);
141c6c8fea2SSven Eckelmann 
142807736f6SSven Eckelmann 	curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
143807736f6SSven Eckelmann 	rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
14425b6d3c1SMarek Lindner 
14525b6d3c1SMarek Lindner 	if (curr_gw_node)
1463a01743dSSven Eckelmann 		batadv_gw_node_put(curr_gw_node);
147c4aac1abSMarek Lindner 
148807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
149c4aac1abSMarek Lindner }
150c4aac1abSMarek Lindner 
1514e820e72SAntonio Quartulli /**
1524e820e72SAntonio Quartulli  * batadv_gw_reselect - force a gateway reselection
1534e820e72SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
1544e820e72SAntonio Quartulli  *
1554e820e72SAntonio Quartulli  * Set a flag to remind the GW component to perform a new gateway reselection.
1564e820e72SAntonio Quartulli  * However this function does not ensure that the current gateway is going to be
1574e820e72SAntonio Quartulli  * deselected. The reselection mechanism may elect the same gateway once again.
1584e820e72SAntonio Quartulli  *
1594e820e72SAntonio Quartulli  * This means that invoking batadv_gw_reselect() does not guarantee a gateway
1604e820e72SAntonio Quartulli  * change and therefore a uevent is not necessarily expected.
1614e820e72SAntonio Quartulli  */
1624e820e72SAntonio Quartulli void batadv_gw_reselect(struct batadv_priv *bat_priv)
163c4aac1abSMarek Lindner {
164807736f6SSven Eckelmann 	atomic_set(&bat_priv->gw.reselect, 1);
165c6c8fea2SSven Eckelmann }
166c6c8fea2SSven Eckelmann 
167c6eaa3f0SAntonio Quartulli /**
168c6eaa3f0SAntonio Quartulli  * batadv_gw_check_client_stop - check if client mode has been switched off
169c6eaa3f0SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
170c6eaa3f0SAntonio Quartulli  *
171c6eaa3f0SAntonio Quartulli  * This function assumes the caller has checked that the gw state *is actually
172c6eaa3f0SAntonio Quartulli  * changing*. This function is not supposed to be called when there is no state
173c6eaa3f0SAntonio Quartulli  * change.
174c6eaa3f0SAntonio Quartulli  */
175c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
176c6eaa3f0SAntonio Quartulli {
177c6eaa3f0SAntonio Quartulli 	struct batadv_gw_node *curr_gw;
178c6eaa3f0SAntonio Quartulli 
1793a24a63eSAntonio Quartulli 	if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
180c6eaa3f0SAntonio Quartulli 		return;
181c6eaa3f0SAntonio Quartulli 
182c6eaa3f0SAntonio Quartulli 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
183c6eaa3f0SAntonio Quartulli 	if (!curr_gw)
184c6eaa3f0SAntonio Quartulli 		return;
185c6eaa3f0SAntonio Quartulli 
186f3163181SAntonio Quartulli 	/* deselect the current gateway so that next time that client mode is
187f3163181SAntonio Quartulli 	 * enabled a proper GW_ADD event can be sent
188f3163181SAntonio Quartulli 	 */
189f3163181SAntonio Quartulli 	batadv_gw_select(bat_priv, NULL);
190f3163181SAntonio Quartulli 
191c6eaa3f0SAntonio Quartulli 	/* if batman-adv is switching the gw client mode off and a gateway was
192c6eaa3f0SAntonio Quartulli 	 * already selected, send a DEL uevent
193c6eaa3f0SAntonio Quartulli 	 */
194c6eaa3f0SAntonio Quartulli 	batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
195c6eaa3f0SAntonio Quartulli 
1963a01743dSSven Eckelmann 	batadv_gw_node_put(curr_gw);
197c6eaa3f0SAntonio Quartulli }
198c6eaa3f0SAntonio Quartulli 
19956303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv)
2002265c141SAntonio Quartulli {
2014f248cffSSven Eckelmann 	struct batadv_gw_node *curr_gw = NULL;
2024f248cffSSven Eckelmann 	struct batadv_gw_node *next_gw = NULL;
20356303d34SSven Eckelmann 	struct batadv_neigh_node *router = NULL;
20489652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
20519595e05SAntonio Quartulli 	char gw_addr[18] = { '\0' };
2062265c141SAntonio Quartulli 
2073a24a63eSAntonio Quartulli 	if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
2082265c141SAntonio Quartulli 		goto out;
2092265c141SAntonio Quartulli 
21034d99cfeSAntonio Quartulli 	if (!bat_priv->algo_ops->gw.get_best_gw_node)
21134d99cfeSAntonio Quartulli 		goto out;
21234d99cfeSAntonio Quartulli 
2131409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2142265c141SAntonio Quartulli 
215807736f6SSven Eckelmann 	if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
216caa0bf64SMarek Lindner 		goto out;
217caa0bf64SMarek Lindner 
21834d99cfeSAntonio Quartulli 	/* if gw.reselect is set to 1 it means that a previous call to
21934d99cfeSAntonio Quartulli 	 * gw.is_eligible() said that we have a new best GW, therefore it can
22034d99cfeSAntonio Quartulli 	 * now be picked from the list and selected
22134d99cfeSAntonio Quartulli 	 */
22234d99cfeSAntonio Quartulli 	next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
2232265c141SAntonio Quartulli 
2242265c141SAntonio Quartulli 	if (curr_gw == next_gw)
2252265c141SAntonio Quartulli 		goto out;
2262265c141SAntonio Quartulli 
2272265c141SAntonio Quartulli 	if (next_gw) {
22819595e05SAntonio Quartulli 		sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
22919595e05SAntonio Quartulli 
2307351a482SSimon Wunderlich 		router = batadv_orig_router_get(next_gw->orig_node,
2317351a482SSimon Wunderlich 						BATADV_IF_DEFAULT);
2322265c141SAntonio Quartulli 		if (!router) {
2334e820e72SAntonio Quartulli 			batadv_gw_reselect(bat_priv);
2342265c141SAntonio Quartulli 			goto out;
2352265c141SAntonio Quartulli 		}
23689652331SSimon Wunderlich 
23789652331SSimon Wunderlich 		router_ifinfo = batadv_neigh_ifinfo_get(router,
23889652331SSimon Wunderlich 							BATADV_IF_DEFAULT);
23989652331SSimon Wunderlich 		if (!router_ifinfo) {
24089652331SSimon Wunderlich 			batadv_gw_reselect(bat_priv);
24189652331SSimon Wunderlich 			goto out;
24289652331SSimon Wunderlich 		}
2432265c141SAntonio Quartulli 	}
2442265c141SAntonio Quartulli 
2452265c141SAntonio Quartulli 	if ((curr_gw) && (!next_gw)) {
24639c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2472265c141SAntonio Quartulli 			   "Removing selected gateway - no gateway in range\n");
24839c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
24939c75a51SSven Eckelmann 				    NULL);
2502265c141SAntonio Quartulli 	} else if ((!curr_gw) && (next_gw)) {
25139c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
252414254e3SMarek Lindner 			   "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
2531eda58bfSSven Eckelmann 			   next_gw->orig_node->orig,
254414254e3SMarek Lindner 			   next_gw->bandwidth_down / 10,
255414254e3SMarek Lindner 			   next_gw->bandwidth_down % 10,
256414254e3SMarek Lindner 			   next_gw->bandwidth_up / 10,
25789652331SSimon Wunderlich 			   next_gw->bandwidth_up % 10,
25889652331SSimon Wunderlich 			   router_ifinfo->bat_iv.tq_avg);
25939c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
26039c75a51SSven Eckelmann 				    gw_addr);
2612265c141SAntonio Quartulli 	} else {
26239c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
263414254e3SMarek Lindner 			   "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
2641eda58bfSSven Eckelmann 			   next_gw->orig_node->orig,
265414254e3SMarek Lindner 			   next_gw->bandwidth_down / 10,
266414254e3SMarek Lindner 			   next_gw->bandwidth_down % 10,
267414254e3SMarek Lindner 			   next_gw->bandwidth_up / 10,
26889652331SSimon Wunderlich 			   next_gw->bandwidth_up % 10,
26989652331SSimon Wunderlich 			   router_ifinfo->bat_iv.tq_avg);
27039c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
27139c75a51SSven Eckelmann 				    gw_addr);
272c6c8fea2SSven Eckelmann 	}
273c6c8fea2SSven Eckelmann 
2741409a834SSven Eckelmann 	batadv_gw_select(bat_priv, next_gw);
2752265c141SAntonio Quartulli 
276c4aac1abSMarek Lindner out:
277c4aac1abSMarek Lindner 	if (curr_gw)
2783a01743dSSven Eckelmann 		batadv_gw_node_put(curr_gw);
2792265c141SAntonio Quartulli 	if (next_gw)
2803a01743dSSven Eckelmann 		batadv_gw_node_put(next_gw);
2812265c141SAntonio Quartulli 	if (router)
28225bb2509SSven Eckelmann 		batadv_neigh_node_put(router);
28389652331SSimon Wunderlich 	if (router_ifinfo)
284044fa3aeSSven Eckelmann 		batadv_neigh_ifinfo_put(router_ifinfo);
285c6c8fea2SSven Eckelmann }
286c6c8fea2SSven Eckelmann 
28756303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv,
28856303d34SSven Eckelmann 			      struct batadv_orig_node *orig_node)
289c6c8fea2SSven Eckelmann {
29056303d34SSven Eckelmann 	struct batadv_orig_node *curr_gw_orig;
29134d99cfeSAntonio Quartulli 
29234d99cfeSAntonio Quartulli 	/* abort immediately if the routing algorithm does not support gateway
29334d99cfeSAntonio Quartulli 	 * election
29434d99cfeSAntonio Quartulli 	 */
29534d99cfeSAntonio Quartulli 	if (!bat_priv->algo_ops->gw.is_eligible)
29634d99cfeSAntonio Quartulli 		return;
297c6c8fea2SSven Eckelmann 
2987cf06bc6SSven Eckelmann 	curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
29957f0c07cSLinus Lüssing 	if (!curr_gw_orig)
3004e820e72SAntonio Quartulli 		goto reselect;
301c6c8fea2SSven Eckelmann 
302c6c8fea2SSven Eckelmann 	/* this node already is the gateway */
30357f0c07cSLinus Lüssing 	if (curr_gw_orig == orig_node)
304e1a5382fSLinus Lüssing 		goto out;
305c6c8fea2SSven Eckelmann 
30634d99cfeSAntonio Quartulli 	if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
30734d99cfeSAntonio Quartulli 						orig_node))
308e1a5382fSLinus Lüssing 		goto out;
309c6c8fea2SSven Eckelmann 
3104e820e72SAntonio Quartulli reselect:
3114e820e72SAntonio Quartulli 	batadv_gw_reselect(bat_priv);
3125d02b3cdSLinus Lüssing out:
31357f0c07cSLinus Lüssing 	if (curr_gw_orig)
3145d967310SSven Eckelmann 		batadv_orig_node_put(curr_gw_orig);
315c6c8fea2SSven Eckelmann }
316c6c8fea2SSven Eckelmann 
317414254e3SMarek Lindner /**
318414254e3SMarek Lindner  * batadv_gw_node_add - add gateway node to list of available gateways
319414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
320414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
321414254e3SMarek Lindner  * @gateway: announced bandwidth information
322414254e3SMarek Lindner  */
32356303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv,
32456303d34SSven Eckelmann 			       struct batadv_orig_node *orig_node,
325414254e3SMarek Lindner 			       struct batadv_tvlv_gateway_data *gateway)
326c6c8fea2SSven Eckelmann {
32756303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
328414254e3SMarek Lindner 
329414254e3SMarek Lindner 	if (gateway->bandwidth_down == 0)
330414254e3SMarek Lindner 		return;
331c6c8fea2SSven Eckelmann 
332377fe0f9SAntonio Quartulli 	gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
333c3ba37a7SSven Eckelmann 	if (!gw_node)
334377fe0f9SAntonio Quartulli 		return;
335377fe0f9SAntonio Quartulli 
336c3ba37a7SSven Eckelmann 	kref_get(&orig_node->refcount);
337c6c8fea2SSven Eckelmann 	INIT_HLIST_NODE(&gw_node->list);
338c6c8fea2SSven Eckelmann 	gw_node->orig_node = orig_node;
33927a4d5efSSimon Wunderlich 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
34027a4d5efSSimon Wunderlich 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
341e7aed321SSven Eckelmann 	kref_init(&gw_node->refcount);
342c6c8fea2SSven Eckelmann 
343807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
344807736f6SSven Eckelmann 	hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
345807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
346c6c8fea2SSven Eckelmann 
34739c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
348414254e3SMarek Lindner 		   "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
349414254e3SMarek Lindner 		   orig_node->orig,
350414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
351414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
352414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
353414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
354c6c8fea2SSven Eckelmann }
355c6c8fea2SSven Eckelmann 
356414254e3SMarek Lindner /**
357414254e3SMarek Lindner  * batadv_gw_node_get - retrieve gateway node from list of available gateways
358414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
359414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
360414254e3SMarek Lindner  *
36162fe710fSSven Eckelmann  * Return: gateway node if found or NULL otherwise.
36271e4aa9cSAntonio Quartulli  */
363*50164d8fSAntonio Quartulli struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
364414254e3SMarek Lindner 					  struct batadv_orig_node *orig_node)
365414254e3SMarek Lindner {
366414254e3SMarek Lindner 	struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
367c6c8fea2SSven Eckelmann 
368c6c8fea2SSven Eckelmann 	rcu_read_lock();
369414254e3SMarek Lindner 	hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
370414254e3SMarek Lindner 		if (gw_node_tmp->orig_node != orig_node)
371c6c8fea2SSven Eckelmann 			continue;
372c6c8fea2SSven Eckelmann 
373e7aed321SSven Eckelmann 		if (!kref_get_unless_zero(&gw_node_tmp->refcount))
374414254e3SMarek Lindner 			continue;
375414254e3SMarek Lindner 
376414254e3SMarek Lindner 		gw_node = gw_node_tmp;
377414254e3SMarek Lindner 		break;
378414254e3SMarek Lindner 	}
379414254e3SMarek Lindner 	rcu_read_unlock();
380414254e3SMarek Lindner 
381414254e3SMarek Lindner 	return gw_node;
382414254e3SMarek Lindner }
383414254e3SMarek Lindner 
384414254e3SMarek Lindner /**
385414254e3SMarek Lindner  * batadv_gw_node_update - update list of available gateways with changed
386414254e3SMarek Lindner  *  bandwidth information
387414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
388414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
389414254e3SMarek Lindner  * @gateway: announced bandwidth information
390414254e3SMarek Lindner  */
391414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv,
392414254e3SMarek Lindner 			   struct batadv_orig_node *orig_node,
393414254e3SMarek Lindner 			   struct batadv_tvlv_gateway_data *gateway)
394414254e3SMarek Lindner {
395414254e3SMarek Lindner 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
396414254e3SMarek Lindner 
397414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_node);
398414254e3SMarek Lindner 	if (!gw_node) {
399414254e3SMarek Lindner 		batadv_gw_node_add(bat_priv, orig_node, gateway);
400414254e3SMarek Lindner 		goto out;
401414254e3SMarek Lindner 	}
402414254e3SMarek Lindner 
403414254e3SMarek Lindner 	if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
404414254e3SMarek Lindner 	    (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
405414254e3SMarek Lindner 		goto out;
406414254e3SMarek Lindner 
40739c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
408414254e3SMarek Lindner 		   "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
409414254e3SMarek Lindner 		   orig_node->orig,
410414254e3SMarek Lindner 		   gw_node->bandwidth_down / 10,
411414254e3SMarek Lindner 		   gw_node->bandwidth_down % 10,
412414254e3SMarek Lindner 		   gw_node->bandwidth_up / 10,
413414254e3SMarek Lindner 		   gw_node->bandwidth_up % 10,
414414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
415414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
416414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
417414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
418414254e3SMarek Lindner 
419414254e3SMarek Lindner 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
420414254e3SMarek Lindner 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
421c6c8fea2SSven Eckelmann 
422414254e3SMarek Lindner 	if (ntohl(gateway->bandwidth_down) == 0) {
42339c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
424c6c8fea2SSven Eckelmann 			   "Gateway %pM removed from gateway list\n",
425c6c8fea2SSven Eckelmann 			   orig_node->orig);
426c6c8fea2SSven Eckelmann 
427414254e3SMarek Lindner 		/* Note: We don't need a NULL check here, since curr_gw never
428414254e3SMarek Lindner 		 * gets dereferenced.
429414254e3SMarek Lindner 		 */
430bd3524c1SSimon Wunderlich 		spin_lock_bh(&bat_priv->gw.list_lock);
431c18bdd01SSven Eckelmann 		if (!hlist_unhashed(&gw_node->list)) {
432bd3524c1SSimon Wunderlich 			hlist_del_init_rcu(&gw_node->list);
4333a01743dSSven Eckelmann 			batadv_gw_node_put(gw_node);
434c18bdd01SSven Eckelmann 		}
435c18bdd01SSven Eckelmann 		spin_unlock_bh(&bat_priv->gw.list_lock);
436bd3524c1SSimon Wunderlich 
437414254e3SMarek Lindner 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
438c4aac1abSMarek Lindner 		if (gw_node == curr_gw)
4394e820e72SAntonio Quartulli 			batadv_gw_reselect(bat_priv);
440bd3524c1SSimon Wunderlich 
441bd3524c1SSimon Wunderlich 		if (curr_gw)
4423a01743dSSven Eckelmann 			batadv_gw_node_put(curr_gw);
443414254e3SMarek Lindner 	}
44471e4aa9cSAntonio Quartulli 
445414254e3SMarek Lindner out:
446414254e3SMarek Lindner 	if (gw_node)
4473a01743dSSven Eckelmann 		batadv_gw_node_put(gw_node);
448c6c8fea2SSven Eckelmann }
449c6c8fea2SSven Eckelmann 
45056303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv,
45156303d34SSven Eckelmann 			   struct batadv_orig_node *orig_node)
452c6c8fea2SSven Eckelmann {
453414254e3SMarek Lindner 	struct batadv_tvlv_gateway_data gateway;
454414254e3SMarek Lindner 
455414254e3SMarek Lindner 	gateway.bandwidth_down = 0;
456414254e3SMarek Lindner 	gateway.bandwidth_up = 0;
457414254e3SMarek Lindner 
458414254e3SMarek Lindner 	batadv_gw_node_update(bat_priv, orig_node, &gateway);
459c6c8fea2SSven Eckelmann }
460c6c8fea2SSven Eckelmann 
461bd3524c1SSimon Wunderlich void batadv_gw_node_free(struct batadv_priv *bat_priv)
462c6c8fea2SSven Eckelmann {
463bd3524c1SSimon Wunderlich 	struct batadv_gw_node *gw_node;
464b67bfe0dSSasha Levin 	struct hlist_node *node_tmp;
465c6c8fea2SSven Eckelmann 
466807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
467b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(gw_node, node_tmp,
468807736f6SSven Eckelmann 				  &bat_priv->gw.list, list) {
469bd3524c1SSimon Wunderlich 		hlist_del_init_rcu(&gw_node->list);
4703a01743dSSven Eckelmann 		batadv_gw_node_put(gw_node);
471c6c8fea2SSven Eckelmann 	}
472807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
473c6c8fea2SSven Eckelmann }
474c6c8fea2SSven Eckelmann 
4757cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
476c6c8fea2SSven Eckelmann {
477c6c8fea2SSven Eckelmann 	struct net_device *net_dev = (struct net_device *)seq->private;
47856303d34SSven Eckelmann 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
47956303d34SSven Eckelmann 	struct batadv_hard_iface *primary_if;
480c6c8fea2SSven Eckelmann 
48130da63a6SMarek Lindner 	primary_if = batadv_seq_print_text_primary_if_get(seq);
48230da63a6SMarek Lindner 	if (!primary_if)
48334d99cfeSAntonio Quartulli 		return 0;
484c6c8fea2SSven Eckelmann 
48534d99cfeSAntonio Quartulli 	seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
48642d0b044SSven Eckelmann 		   BATADV_SOURCE_VERSION, primary_if->net_dev->name,
48734d99cfeSAntonio Quartulli 		   primary_if->net_dev->dev_addr, net_dev->name,
48834d99cfeSAntonio Quartulli 		   bat_priv->algo_ops->name);
489c6c8fea2SSven Eckelmann 
49082047ad7SSven Eckelmann 	batadv_hardif_put(primary_if);
49134d99cfeSAntonio Quartulli 
49234d99cfeSAntonio Quartulli 	if (!bat_priv->algo_ops->gw.print) {
49334d99cfeSAntonio Quartulli 		seq_puts(seq,
49434d99cfeSAntonio Quartulli 			 "No printing function for this routing protocol\n");
49534d99cfeSAntonio Quartulli 		return 0;
49634d99cfeSAntonio Quartulli 	}
49734d99cfeSAntonio Quartulli 
49834d99cfeSAntonio Quartulli 	bat_priv->algo_ops->gw.print(bat_priv, seq);
49934d99cfeSAntonio Quartulli 
50030da63a6SMarek Lindner 	return 0;
501c6c8fea2SSven Eckelmann }
502c6c8fea2SSven Eckelmann 
5036c413b1cSAntonio Quartulli /**
5046c413b1cSAntonio Quartulli  * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
5056c413b1cSAntonio Quartulli  * @skb: the packet to check
5066c413b1cSAntonio Quartulli  * @header_len: a pointer to the batman-adv header size
5076c413b1cSAntonio Quartulli  * @chaddr: buffer where the client address will be stored. Valid
5086c413b1cSAntonio Quartulli  *  only if the function returns BATADV_DHCP_TO_CLIENT
5096c413b1cSAntonio Quartulli  *
51062fe710fSSven Eckelmann  * This function may re-allocate the data buffer of the skb passed as argument.
51162fe710fSSven Eckelmann  *
51262fe710fSSven Eckelmann  * Return:
5136c413b1cSAntonio Quartulli  * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
5146c413b1cSAntonio Quartulli  *   while parsing it
5156c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
5166c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
5179cfc7bd6SSven Eckelmann  */
5186c413b1cSAntonio Quartulli enum batadv_dhcp_recipient
5196c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
5206b5e971aSSven Eckelmann 			     u8 *chaddr)
521c6c8fea2SSven Eckelmann {
5226c413b1cSAntonio Quartulli 	enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
523c6c8fea2SSven Eckelmann 	struct ethhdr *ethhdr;
524c6c8fea2SSven Eckelmann 	struct iphdr *iphdr;
525c6c8fea2SSven Eckelmann 	struct ipv6hdr *ipv6hdr;
526c6c8fea2SSven Eckelmann 	struct udphdr *udphdr;
527f7f8ed56SAntonio Quartulli 	struct vlan_ethhdr *vhdr;
5286c413b1cSAntonio Quartulli 	int chaddr_offset;
529f7f8ed56SAntonio Quartulli 	__be16 proto;
5306b5e971aSSven Eckelmann 	u8 *p;
531c6c8fea2SSven Eckelmann 
532c6c8fea2SSven Eckelmann 	/* check for ethernet header */
533be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
5346c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
5356c413b1cSAntonio Quartulli 
536927c2ed7SLinus Lüssing 	ethhdr = eth_hdr(skb);
537f7f8ed56SAntonio Quartulli 	proto = ethhdr->h_proto;
538be7af5cfSMarek Lindner 	*header_len += ETH_HLEN;
539c6c8fea2SSven Eckelmann 
540c6c8fea2SSven Eckelmann 	/* check for initial vlan header */
541f7f8ed56SAntonio Quartulli 	if (proto == htons(ETH_P_8021Q)) {
542be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
5436c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
544f7f8ed56SAntonio Quartulli 
545927c2ed7SLinus Lüssing 		vhdr = vlan_eth_hdr(skb);
546f7f8ed56SAntonio Quartulli 		proto = vhdr->h_vlan_encapsulated_proto;
547be7af5cfSMarek Lindner 		*header_len += VLAN_HLEN;
548c6c8fea2SSven Eckelmann 	}
549c6c8fea2SSven Eckelmann 
550c6c8fea2SSven Eckelmann 	/* check for ip header */
551f7f8ed56SAntonio Quartulli 	switch (proto) {
552f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IP):
553be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
5546c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
5556c413b1cSAntonio Quartulli 
556be7af5cfSMarek Lindner 		iphdr = (struct iphdr *)(skb->data + *header_len);
557be7af5cfSMarek Lindner 		*header_len += iphdr->ihl * 4;
558c6c8fea2SSven Eckelmann 
559c6c8fea2SSven Eckelmann 		/* check for udp header */
560c6c8fea2SSven Eckelmann 		if (iphdr->protocol != IPPROTO_UDP)
5616c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
562c6c8fea2SSven Eckelmann 
563c6c8fea2SSven Eckelmann 		break;
564f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IPV6):
565be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
5666c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
5676c413b1cSAntonio Quartulli 
568be7af5cfSMarek Lindner 		ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
569be7af5cfSMarek Lindner 		*header_len += sizeof(*ipv6hdr);
570c6c8fea2SSven Eckelmann 
571c6c8fea2SSven Eckelmann 		/* check for udp header */
572c6c8fea2SSven Eckelmann 		if (ipv6hdr->nexthdr != IPPROTO_UDP)
5736c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
574c6c8fea2SSven Eckelmann 
575c6c8fea2SSven Eckelmann 		break;
576c6c8fea2SSven Eckelmann 	default:
5776c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
578c6c8fea2SSven Eckelmann 	}
579c6c8fea2SSven Eckelmann 
580be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
5816c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
5829d2c9488SLinus Lüssing 
583be7af5cfSMarek Lindner 	udphdr = (struct udphdr *)(skb->data + *header_len);
584be7af5cfSMarek Lindner 	*header_len += sizeof(*udphdr);
585c6c8fea2SSven Eckelmann 
586c6c8fea2SSven Eckelmann 	/* check for bootp port */
5876c413b1cSAntonio Quartulli 	switch (proto) {
5886c413b1cSAntonio Quartulli 	case htons(ETH_P_IP):
5896c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(67))
5906c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
5916c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(67))
5926c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
5936c413b1cSAntonio Quartulli 		break;
5946c413b1cSAntonio Quartulli 	case htons(ETH_P_IPV6):
5956c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(547))
5966c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
5976c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(547))
5986c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
5996c413b1cSAntonio Quartulli 		break;
600be7af5cfSMarek Lindner 	}
601c6c8fea2SSven Eckelmann 
6026c413b1cSAntonio Quartulli 	chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
6036c413b1cSAntonio Quartulli 	/* store the client address if the message is going to a client */
6046c413b1cSAntonio Quartulli 	if (ret == BATADV_DHCP_TO_CLIENT &&
6056c413b1cSAntonio Quartulli 	    pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
6066c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries an Ethernet DHCP */
6076c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
6086c413b1cSAntonio Quartulli 		if (*p != BATADV_DHCP_HTYPE_ETHERNET)
6096c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
6106c413b1cSAntonio Quartulli 
6116c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries a valid Ethernet address */
6126c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
6136c413b1cSAntonio Quartulli 		if (*p != ETH_ALEN)
6146c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
6156c413b1cSAntonio Quartulli 
6168fdd0153SAntonio Quartulli 		ether_addr_copy(chaddr, skb->data + chaddr_offset);
6176c413b1cSAntonio Quartulli 	}
6186c413b1cSAntonio Quartulli 
6196c413b1cSAntonio Quartulli 	return ret;
6206c413b1cSAntonio Quartulli }
621aa143d28SAntonio Quartulli 
622bbb877edSAntonio Quartulli /**
623bbb877edSAntonio Quartulli  * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
624bbb877edSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
625bbb877edSAntonio Quartulli  * @skb: the outgoing packet
626bbb877edSAntonio Quartulli  *
627bbb877edSAntonio Quartulli  * Check if the skb is a DHCP request and if it is sent to the current best GW
628bbb877edSAntonio Quartulli  * server. Due to topology changes it may be the case that the GW server
629bbb877edSAntonio Quartulli  * previously selected is not the best one anymore.
630bbb877edSAntonio Quartulli  *
631bbb877edSAntonio Quartulli  * This call might reallocate skb data.
6326c413b1cSAntonio Quartulli  * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
63362fe710fSSven Eckelmann  *
63462fe710fSSven Eckelmann  * Return: true if the packet destination is unicast and it is not the best gw,
63562fe710fSSven Eckelmann  * false otherwise.
636bbb877edSAntonio Quartulli  */
63756303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
6389d2c9488SLinus Lüssing 			    struct sk_buff *skb)
639be7af5cfSMarek Lindner {
6404f248cffSSven Eckelmann 	struct batadv_neigh_node *neigh_curr = NULL;
6414f248cffSSven Eckelmann 	struct batadv_neigh_node *neigh_old = NULL;
64256303d34SSven Eckelmann 	struct batadv_orig_node *orig_dst_node = NULL;
6434f248cffSSven Eckelmann 	struct batadv_gw_node *gw_node = NULL;
6444f248cffSSven Eckelmann 	struct batadv_gw_node *curr_gw = NULL;
64589652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
6466c413b1cSAntonio Quartulli 	struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
6476c413b1cSAntonio Quartulli 	bool out_of_range = false;
6486b5e971aSSven Eckelmann 	u8 curr_tq_avg;
649bbb877edSAntonio Quartulli 	unsigned short vid;
650bbb877edSAntonio Quartulli 
651bbb877edSAntonio Quartulli 	vid = batadv_get_vid(skb, 0);
652be7af5cfSMarek Lindner 
65308c36d3eSSven Eckelmann 	orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
654bbb877edSAntonio Quartulli 						 ethhdr->h_dest, vid);
655be7af5cfSMarek Lindner 	if (!orig_dst_node)
656be7af5cfSMarek Lindner 		goto out;
657be7af5cfSMarek Lindner 
658414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
6590d164491SAntonio Quartulli 	if (!gw_node)
660be7af5cfSMarek Lindner 		goto out;
661be7af5cfSMarek Lindner 
6623a24a63eSAntonio Quartulli 	switch (atomic_read(&bat_priv->gw.mode)) {
663cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_SERVER:
664be7af5cfSMarek Lindner 		/* If we are a GW then we are our best GW. We can artificially
6659cfc7bd6SSven Eckelmann 		 * set the tq towards ourself as the maximum value
6669cfc7bd6SSven Eckelmann 		 */
66742d0b044SSven Eckelmann 		curr_tq_avg = BATADV_TQ_MAX_VALUE;
668be7af5cfSMarek Lindner 		break;
669cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_CLIENT:
6701409a834SSven Eckelmann 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
671c4aac1abSMarek Lindner 		if (!curr_gw)
672be7af5cfSMarek Lindner 			goto out;
673c6c8fea2SSven Eckelmann 
674be7af5cfSMarek Lindner 		/* packet is going to our gateway */
675be7af5cfSMarek Lindner 		if (curr_gw->orig_node == orig_dst_node)
676be7af5cfSMarek Lindner 			goto out;
677be7af5cfSMarek Lindner 
67843676ab5SAntonio Quartulli 		/* If the dhcp packet has been sent to a different gw,
67943676ab5SAntonio Quartulli 		 * we have to evaluate whether the old gw is still
6809cfc7bd6SSven Eckelmann 		 * reliable enough
6819cfc7bd6SSven Eckelmann 		 */
68230d3c511SSven Eckelmann 		neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
68330d3c511SSven Eckelmann 						NULL);
684be7af5cfSMarek Lindner 		if (!neigh_curr)
685be7af5cfSMarek Lindner 			goto out;
686be7af5cfSMarek Lindner 
68789652331SSimon Wunderlich 		curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
68889652331SSimon Wunderlich 						      BATADV_IF_DEFAULT);
68989652331SSimon Wunderlich 		if (!curr_ifinfo)
69089652331SSimon Wunderlich 			goto out;
69189652331SSimon Wunderlich 
69289652331SSimon Wunderlich 		curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
693044fa3aeSSven Eckelmann 		batadv_neigh_ifinfo_put(curr_ifinfo);
69489652331SSimon Wunderlich 
695be7af5cfSMarek Lindner 		break;
696cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_OFF:
697be7af5cfSMarek Lindner 	default:
698be7af5cfSMarek Lindner 		goto out;
69943676ab5SAntonio Quartulli 	}
700be7af5cfSMarek Lindner 
70130d3c511SSven Eckelmann 	neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
7022ef04f47SDan Carpenter 	if (!neigh_old)
703be7af5cfSMarek Lindner 		goto out;
704be7af5cfSMarek Lindner 
70589652331SSimon Wunderlich 	old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
70689652331SSimon Wunderlich 	if (!old_ifinfo)
70789652331SSimon Wunderlich 		goto out;
70889652331SSimon Wunderlich 
70989652331SSimon Wunderlich 	if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
710be7af5cfSMarek Lindner 		out_of_range = true;
711044fa3aeSSven Eckelmann 	batadv_neigh_ifinfo_put(old_ifinfo);
712be7af5cfSMarek Lindner 
713be7af5cfSMarek Lindner out:
714be7af5cfSMarek Lindner 	if (orig_dst_node)
7155d967310SSven Eckelmann 		batadv_orig_node_put(orig_dst_node);
716be7af5cfSMarek Lindner 	if (curr_gw)
7173a01743dSSven Eckelmann 		batadv_gw_node_put(curr_gw);
718414254e3SMarek Lindner 	if (gw_node)
7193a01743dSSven Eckelmann 		batadv_gw_node_put(gw_node);
72043676ab5SAntonio Quartulli 	if (neigh_old)
72125bb2509SSven Eckelmann 		batadv_neigh_node_put(neigh_old);
72243676ab5SAntonio Quartulli 	if (neigh_curr)
72325bb2509SSven Eckelmann 		batadv_neigh_node_put(neigh_curr);
724be7af5cfSMarek Lindner 	return out_of_range;
725c6c8fea2SSven Eckelmann }
726