xref: /openbmc/linux/net/batman-adv/gateway_client.c (revision 27a4d5efd417b6ef3190e9af357715532d4617a3)
19f6446c7SSven Eckelmann /* Copyright (C) 2009-2015 B.A.T.M.A.N. contributors:
2c6c8fea2SSven Eckelmann  *
3c6c8fea2SSven Eckelmann  * Marek Lindner
4c6c8fea2SSven Eckelmann  *
5c6c8fea2SSven Eckelmann  * This program is free software; you can redistribute it and/or
6c6c8fea2SSven Eckelmann  * modify it under the terms of version 2 of the GNU General Public
7c6c8fea2SSven Eckelmann  * License as published by the Free Software Foundation.
8c6c8fea2SSven Eckelmann  *
9c6c8fea2SSven Eckelmann  * This program is distributed in the hope that it will be useful, but
10c6c8fea2SSven Eckelmann  * WITHOUT ANY WARRANTY; without even the implied warranty of
11c6c8fea2SSven Eckelmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12c6c8fea2SSven Eckelmann  * General Public License for more details.
13c6c8fea2SSven Eckelmann  *
14c6c8fea2SSven Eckelmann  * You should have received a copy of the GNU General Public License
15ebf38fb7SAntonio Quartulli  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16c6c8fea2SSven Eckelmann  */
17c6c8fea2SSven Eckelmann 
18c6c8fea2SSven Eckelmann #include "gateway_client.h"
191e2c2a4fSSven Eckelmann #include "main.h"
201e2c2a4fSSven Eckelmann 
211e2c2a4fSSven Eckelmann #include <linux/atomic.h>
221e2c2a4fSSven Eckelmann #include <linux/byteorder/generic.h>
231e2c2a4fSSven Eckelmann #include <linux/etherdevice.h>
241e2c2a4fSSven Eckelmann #include <linux/fs.h>
251e2c2a4fSSven Eckelmann #include <linux/if_ether.h>
261e2c2a4fSSven Eckelmann #include <linux/if_vlan.h>
271e2c2a4fSSven Eckelmann #include <linux/in.h>
281e2c2a4fSSven Eckelmann #include <linux/ip.h>
291e2c2a4fSSven Eckelmann #include <linux/ipv6.h>
301e2c2a4fSSven Eckelmann #include <linux/jiffies.h>
311e2c2a4fSSven Eckelmann #include <linux/kernel.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"
4557f0c07cSLinus Lüssing #include "originator.h"
461e2c2a4fSSven Eckelmann #include "packet.h"
4743676ab5SAntonio Quartulli #include "routing.h"
481e2c2a4fSSven Eckelmann #include "sysfs.h"
491e2c2a4fSSven Eckelmann #include "translation-table.h"
50c6c8fea2SSven Eckelmann 
516c413b1cSAntonio Quartulli /* These are the offsets of the "hw type" and "hw address length" in the dhcp
526c413b1cSAntonio Quartulli  * packet starting at the beginning of the dhcp header
539cfc7bd6SSven Eckelmann  */
546c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_OFFSET	1
556c413b1cSAntonio Quartulli #define BATADV_DHCP_HLEN_OFFSET		2
566c413b1cSAntonio Quartulli /* Value of htype representing Ethernet */
576c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_ETHERNET	0x01
586c413b1cSAntonio Quartulli /* This is the offset of the "chaddr" field in the dhcp packet starting at the
596c413b1cSAntonio Quartulli  * beginning of the dhcp header
606c413b1cSAntonio Quartulli  */
616c413b1cSAntonio Quartulli #define BATADV_DHCP_CHADDR_OFFSET	28
6243676ab5SAntonio Quartulli 
6356303d34SSven Eckelmann static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node)
6425b6d3c1SMarek Lindner {
65377fe0f9SAntonio Quartulli 	if (atomic_dec_and_test(&gw_node->refcount)) {
66377fe0f9SAntonio Quartulli 		batadv_orig_node_free_ref(gw_node->orig_node);
67eb340b2fSPaul E. McKenney 		kfree_rcu(gw_node, rcu);
68c6c8fea2SSven Eckelmann 	}
69377fe0f9SAntonio Quartulli }
70c6c8fea2SSven Eckelmann 
7156303d34SSven Eckelmann static struct batadv_gw_node *
7256303d34SSven Eckelmann batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
73c6c8fea2SSven Eckelmann {
7456303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
75c6c8fea2SSven Eckelmann 
765d02b3cdSLinus Lüssing 	rcu_read_lock();
77807736f6SSven Eckelmann 	gw_node = rcu_dereference(bat_priv->gw.curr_gw);
78c4aac1abSMarek Lindner 	if (!gw_node)
795d02b3cdSLinus Lüssing 		goto out;
80c6c8fea2SSven Eckelmann 
81c4aac1abSMarek Lindner 	if (!atomic_inc_not_zero(&gw_node->refcount))
82c4aac1abSMarek Lindner 		gw_node = NULL;
83c4aac1abSMarek Lindner 
84c4aac1abSMarek Lindner out:
85c4aac1abSMarek Lindner 	rcu_read_unlock();
86c4aac1abSMarek Lindner 	return gw_node;
87c4aac1abSMarek Lindner }
88c4aac1abSMarek Lindner 
8956303d34SSven Eckelmann struct batadv_orig_node *
9056303d34SSven Eckelmann batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
91c4aac1abSMarek Lindner {
9256303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
9356303d34SSven Eckelmann 	struct batadv_orig_node *orig_node = NULL;
94c4aac1abSMarek Lindner 
951409a834SSven Eckelmann 	gw_node = batadv_gw_get_selected_gw_node(bat_priv);
96c4aac1abSMarek Lindner 	if (!gw_node)
977b36e8eeSMarek Lindner 		goto out;
985d02b3cdSLinus Lüssing 
99c4aac1abSMarek Lindner 	rcu_read_lock();
100c4aac1abSMarek Lindner 	orig_node = gw_node->orig_node;
101c4aac1abSMarek Lindner 	if (!orig_node)
102c4aac1abSMarek Lindner 		goto unlock;
103c4aac1abSMarek Lindner 
1047b36e8eeSMarek Lindner 	if (!atomic_inc_not_zero(&orig_node->refcount))
1057b36e8eeSMarek Lindner 		orig_node = NULL;
10643c70ad5SLinus Lüssing 
107c4aac1abSMarek Lindner unlock:
1085d02b3cdSLinus Lüssing 	rcu_read_unlock();
109c4aac1abSMarek Lindner out:
110c6c8fea2SSven Eckelmann 	if (gw_node)
1111409a834SSven Eckelmann 		batadv_gw_node_free_ref(gw_node);
112c4aac1abSMarek Lindner 	return orig_node;
113c6c8fea2SSven Eckelmann }
114c6c8fea2SSven Eckelmann 
11556303d34SSven Eckelmann static void batadv_gw_select(struct batadv_priv *bat_priv,
11656303d34SSven Eckelmann 			     struct batadv_gw_node *new_gw_node)
117c6c8fea2SSven Eckelmann {
11856303d34SSven Eckelmann 	struct batadv_gw_node *curr_gw_node;
119c6c8fea2SSven Eckelmann 
120807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
121c4aac1abSMarek Lindner 
12225b6d3c1SMarek Lindner 	if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
12325b6d3c1SMarek Lindner 		new_gw_node = NULL;
124c6c8fea2SSven Eckelmann 
125807736f6SSven Eckelmann 	curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
126807736f6SSven Eckelmann 	rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
12725b6d3c1SMarek Lindner 
12825b6d3c1SMarek Lindner 	if (curr_gw_node)
1291409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw_node);
130c4aac1abSMarek Lindner 
131807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
132c4aac1abSMarek Lindner }
133c4aac1abSMarek Lindner 
1344e820e72SAntonio Quartulli /**
1354e820e72SAntonio Quartulli  * batadv_gw_reselect - force a gateway reselection
1364e820e72SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
1374e820e72SAntonio Quartulli  *
1384e820e72SAntonio Quartulli  * Set a flag to remind the GW component to perform a new gateway reselection.
1394e820e72SAntonio Quartulli  * However this function does not ensure that the current gateway is going to be
1404e820e72SAntonio Quartulli  * deselected. The reselection mechanism may elect the same gateway once again.
1414e820e72SAntonio Quartulli  *
1424e820e72SAntonio Quartulli  * This means that invoking batadv_gw_reselect() does not guarantee a gateway
1434e820e72SAntonio Quartulli  * change and therefore a uevent is not necessarily expected.
1444e820e72SAntonio Quartulli  */
1454e820e72SAntonio Quartulli void batadv_gw_reselect(struct batadv_priv *bat_priv)
146c4aac1abSMarek Lindner {
147807736f6SSven Eckelmann 	atomic_set(&bat_priv->gw.reselect, 1);
148c6c8fea2SSven Eckelmann }
149c6c8fea2SSven Eckelmann 
15056303d34SSven Eckelmann static struct batadv_gw_node *
15156303d34SSven Eckelmann batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
152c6c8fea2SSven Eckelmann {
15356303d34SSven Eckelmann 	struct batadv_neigh_node *router;
15489652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo;
15556303d34SSven Eckelmann 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
156c6c8fea2SSven Eckelmann 	uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
157c67893d1SSven Eckelmann 	uint32_t gw_divisor;
1582265c141SAntonio Quartulli 	uint8_t max_tq = 0;
159c67893d1SSven Eckelmann 	uint8_t tq_avg;
16056303d34SSven Eckelmann 	struct batadv_orig_node *orig_node;
161c6c8fea2SSven Eckelmann 
162c67893d1SSven Eckelmann 	gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE;
163c67893d1SSven Eckelmann 	gw_divisor *= 64;
164c67893d1SSven Eckelmann 
165c6c8fea2SSven Eckelmann 	rcu_read_lock();
166b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
167e1a5382fSLinus Lüssing 		if (gw_node->deleted)
168c6c8fea2SSven Eckelmann 			continue;
169c6c8fea2SSven Eckelmann 
17084d5e5e0SSven Eckelmann 		orig_node = gw_node->orig_node;
1717351a482SSimon Wunderlich 		router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
172e1a5382fSLinus Lüssing 		if (!router)
173c6c8fea2SSven Eckelmann 			continue;
174c6c8fea2SSven Eckelmann 
17589652331SSimon Wunderlich 		router_ifinfo = batadv_neigh_ifinfo_get(router,
17689652331SSimon Wunderlich 							BATADV_IF_DEFAULT);
17789652331SSimon Wunderlich 		if (!router_ifinfo)
17889652331SSimon Wunderlich 			goto next;
17989652331SSimon Wunderlich 
1802265c141SAntonio Quartulli 		if (!atomic_inc_not_zero(&gw_node->refcount))
1812265c141SAntonio Quartulli 			goto next;
1822265c141SAntonio Quartulli 
18389652331SSimon Wunderlich 		tq_avg = router_ifinfo->bat_iv.tq_avg;
184c67893d1SSven Eckelmann 
185c6c8fea2SSven Eckelmann 		switch (atomic_read(&bat_priv->gw_sel_class)) {
186c6c8fea2SSven Eckelmann 		case 1: /* fast connection */
187414254e3SMarek Lindner 			tmp_gw_factor = tq_avg * tq_avg;
188414254e3SMarek Lindner 			tmp_gw_factor *= gw_node->bandwidth_down;
189414254e3SMarek Lindner 			tmp_gw_factor *= 100 * 100;
190c67893d1SSven Eckelmann 			tmp_gw_factor /= gw_divisor;
191c6c8fea2SSven Eckelmann 
192c6c8fea2SSven Eckelmann 			if ((tmp_gw_factor > max_gw_factor) ||
193c6c8fea2SSven Eckelmann 			    ((tmp_gw_factor == max_gw_factor) &&
194c67893d1SSven Eckelmann 			     (tq_avg > max_tq))) {
1952265c141SAntonio Quartulli 				if (curr_gw)
1961409a834SSven Eckelmann 					batadv_gw_node_free_ref(curr_gw);
1972265c141SAntonio Quartulli 				curr_gw = gw_node;
1982265c141SAntonio Quartulli 				atomic_inc(&curr_gw->refcount);
1992265c141SAntonio Quartulli 			}
200c6c8fea2SSven Eckelmann 			break;
201c6c8fea2SSven Eckelmann 
2029cfc7bd6SSven Eckelmann 		default: /* 2:  stable connection (use best statistic)
203c6c8fea2SSven Eckelmann 			  * 3:  fast-switch (use best statistic but change as
204c6c8fea2SSven Eckelmann 			  *     soon as a better gateway appears)
205c6c8fea2SSven Eckelmann 			  * XX: late-switch (use best statistic but change as
206c6c8fea2SSven Eckelmann 			  *     soon as a better gateway appears which has
207c6c8fea2SSven Eckelmann 			  *     $routing_class more tq points)
2089cfc7bd6SSven Eckelmann 			  */
209c67893d1SSven Eckelmann 			if (tq_avg > max_tq) {
2102265c141SAntonio Quartulli 				if (curr_gw)
2111409a834SSven Eckelmann 					batadv_gw_node_free_ref(curr_gw);
2122265c141SAntonio Quartulli 				curr_gw = gw_node;
2132265c141SAntonio Quartulli 				atomic_inc(&curr_gw->refcount);
2142265c141SAntonio Quartulli 			}
215c6c8fea2SSven Eckelmann 			break;
216c6c8fea2SSven Eckelmann 		}
217c6c8fea2SSven Eckelmann 
218c67893d1SSven Eckelmann 		if (tq_avg > max_tq)
219c67893d1SSven Eckelmann 			max_tq = tq_avg;
220c6c8fea2SSven Eckelmann 
221c6c8fea2SSven Eckelmann 		if (tmp_gw_factor > max_gw_factor)
222c6c8fea2SSven Eckelmann 			max_gw_factor = tmp_gw_factor;
223e1a5382fSLinus Lüssing 
2241409a834SSven Eckelmann 		batadv_gw_node_free_ref(gw_node);
2252265c141SAntonio Quartulli 
2262265c141SAntonio Quartulli next:
2277d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router);
22889652331SSimon Wunderlich 		if (router_ifinfo)
22989652331SSimon Wunderlich 			batadv_neigh_ifinfo_free_ref(router_ifinfo);
230c6c8fea2SSven Eckelmann 	}
2312265c141SAntonio Quartulli 	rcu_read_unlock();
232c6c8fea2SSven Eckelmann 
2332265c141SAntonio Quartulli 	return curr_gw;
2342265c141SAntonio Quartulli }
235e1a5382fSLinus Lüssing 
236c6eaa3f0SAntonio Quartulli /**
237c6eaa3f0SAntonio Quartulli  * batadv_gw_check_client_stop - check if client mode has been switched off
238c6eaa3f0SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
239c6eaa3f0SAntonio Quartulli  *
240c6eaa3f0SAntonio Quartulli  * This function assumes the caller has checked that the gw state *is actually
241c6eaa3f0SAntonio Quartulli  * changing*. This function is not supposed to be called when there is no state
242c6eaa3f0SAntonio Quartulli  * change.
243c6eaa3f0SAntonio Quartulli  */
244c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
245c6eaa3f0SAntonio Quartulli {
246c6eaa3f0SAntonio Quartulli 	struct batadv_gw_node *curr_gw;
247c6eaa3f0SAntonio Quartulli 
248c6eaa3f0SAntonio Quartulli 	if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
249c6eaa3f0SAntonio Quartulli 		return;
250c6eaa3f0SAntonio Quartulli 
251c6eaa3f0SAntonio Quartulli 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
252c6eaa3f0SAntonio Quartulli 	if (!curr_gw)
253c6eaa3f0SAntonio Quartulli 		return;
254c6eaa3f0SAntonio Quartulli 
255f3163181SAntonio Quartulli 	/* deselect the current gateway so that next time that client mode is
256f3163181SAntonio Quartulli 	 * enabled a proper GW_ADD event can be sent
257f3163181SAntonio Quartulli 	 */
258f3163181SAntonio Quartulli 	batadv_gw_select(bat_priv, NULL);
259f3163181SAntonio Quartulli 
260c6eaa3f0SAntonio Quartulli 	/* if batman-adv is switching the gw client mode off and a gateway was
261c6eaa3f0SAntonio Quartulli 	 * already selected, send a DEL uevent
262c6eaa3f0SAntonio Quartulli 	 */
263c6eaa3f0SAntonio Quartulli 	batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
264c6eaa3f0SAntonio Quartulli 
265c6eaa3f0SAntonio Quartulli 	batadv_gw_node_free_ref(curr_gw);
266c6eaa3f0SAntonio Quartulli }
267c6eaa3f0SAntonio Quartulli 
26856303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv)
2692265c141SAntonio Quartulli {
27056303d34SSven Eckelmann 	struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL;
27156303d34SSven Eckelmann 	struct batadv_neigh_node *router = NULL;
27289652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
27319595e05SAntonio Quartulli 	char gw_addr[18] = { '\0' };
2742265c141SAntonio Quartulli 
275cd646ab1SSven Eckelmann 	if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
2762265c141SAntonio Quartulli 		goto out;
2772265c141SAntonio Quartulli 
2781409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2792265c141SAntonio Quartulli 
280807736f6SSven Eckelmann 	if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
281caa0bf64SMarek Lindner 		goto out;
282caa0bf64SMarek Lindner 
2831409a834SSven Eckelmann 	next_gw = batadv_gw_get_best_gw_node(bat_priv);
2842265c141SAntonio Quartulli 
2852265c141SAntonio Quartulli 	if (curr_gw == next_gw)
2862265c141SAntonio Quartulli 		goto out;
2872265c141SAntonio Quartulli 
2882265c141SAntonio Quartulli 	if (next_gw) {
28919595e05SAntonio Quartulli 		sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
29019595e05SAntonio Quartulli 
2917351a482SSimon Wunderlich 		router = batadv_orig_router_get(next_gw->orig_node,
2927351a482SSimon Wunderlich 						BATADV_IF_DEFAULT);
2932265c141SAntonio Quartulli 		if (!router) {
2944e820e72SAntonio Quartulli 			batadv_gw_reselect(bat_priv);
2952265c141SAntonio Quartulli 			goto out;
2962265c141SAntonio Quartulli 		}
29789652331SSimon Wunderlich 
29889652331SSimon Wunderlich 		router_ifinfo = batadv_neigh_ifinfo_get(router,
29989652331SSimon Wunderlich 							BATADV_IF_DEFAULT);
30089652331SSimon Wunderlich 		if (!router_ifinfo) {
30189652331SSimon Wunderlich 			batadv_gw_reselect(bat_priv);
30289652331SSimon Wunderlich 			goto out;
30389652331SSimon Wunderlich 		}
3042265c141SAntonio Quartulli 	}
3052265c141SAntonio Quartulli 
3062265c141SAntonio Quartulli 	if ((curr_gw) && (!next_gw)) {
30739c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
3082265c141SAntonio Quartulli 			   "Removing selected gateway - no gateway in range\n");
30939c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
31039c75a51SSven Eckelmann 				    NULL);
3112265c141SAntonio Quartulli 	} else if ((!curr_gw) && (next_gw)) {
31239c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
313414254e3SMarek Lindner 			   "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
3141eda58bfSSven Eckelmann 			   next_gw->orig_node->orig,
315414254e3SMarek Lindner 			   next_gw->bandwidth_down / 10,
316414254e3SMarek Lindner 			   next_gw->bandwidth_down % 10,
317414254e3SMarek Lindner 			   next_gw->bandwidth_up / 10,
31889652331SSimon Wunderlich 			   next_gw->bandwidth_up % 10,
31989652331SSimon Wunderlich 			   router_ifinfo->bat_iv.tq_avg);
32039c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
32139c75a51SSven Eckelmann 				    gw_addr);
3222265c141SAntonio Quartulli 	} else {
32339c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
324414254e3SMarek Lindner 			   "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
3251eda58bfSSven Eckelmann 			   next_gw->orig_node->orig,
326414254e3SMarek Lindner 			   next_gw->bandwidth_down / 10,
327414254e3SMarek Lindner 			   next_gw->bandwidth_down % 10,
328414254e3SMarek Lindner 			   next_gw->bandwidth_up / 10,
32989652331SSimon Wunderlich 			   next_gw->bandwidth_up % 10,
33089652331SSimon Wunderlich 			   router_ifinfo->bat_iv.tq_avg);
33139c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
33239c75a51SSven Eckelmann 				    gw_addr);
333c6c8fea2SSven Eckelmann 	}
334c6c8fea2SSven Eckelmann 
3351409a834SSven Eckelmann 	batadv_gw_select(bat_priv, next_gw);
3362265c141SAntonio Quartulli 
337c4aac1abSMarek Lindner out:
338c4aac1abSMarek Lindner 	if (curr_gw)
3391409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
3402265c141SAntonio Quartulli 	if (next_gw)
3411409a834SSven Eckelmann 		batadv_gw_node_free_ref(next_gw);
3422265c141SAntonio Quartulli 	if (router)
3437d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router);
34489652331SSimon Wunderlich 	if (router_ifinfo)
34589652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_ifinfo);
346c6c8fea2SSven Eckelmann }
347c6c8fea2SSven Eckelmann 
34856303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv,
34956303d34SSven Eckelmann 			      struct batadv_orig_node *orig_node)
350c6c8fea2SSven Eckelmann {
35189652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_orig_tq = NULL;
35289652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_gw_tq = NULL;
35356303d34SSven Eckelmann 	struct batadv_orig_node *curr_gw_orig;
35456303d34SSven Eckelmann 	struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL;
355c6c8fea2SSven Eckelmann 	uint8_t gw_tq_avg, orig_tq_avg;
356c6c8fea2SSven Eckelmann 
3577cf06bc6SSven Eckelmann 	curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
35857f0c07cSLinus Lüssing 	if (!curr_gw_orig)
3594e820e72SAntonio Quartulli 		goto reselect;
360c6c8fea2SSven Eckelmann 
3617351a482SSimon Wunderlich 	router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
362e1a5382fSLinus Lüssing 	if (!router_gw)
3634e820e72SAntonio Quartulli 		goto reselect;
364c6c8fea2SSven Eckelmann 
36589652331SSimon Wunderlich 	router_gw_tq = batadv_neigh_ifinfo_get(router_gw,
36689652331SSimon Wunderlich 					       BATADV_IF_DEFAULT);
36789652331SSimon Wunderlich 	if (!router_gw_tq)
36889652331SSimon Wunderlich 		goto reselect;
36989652331SSimon Wunderlich 
370c6c8fea2SSven Eckelmann 	/* this node already is the gateway */
37157f0c07cSLinus Lüssing 	if (curr_gw_orig == orig_node)
372e1a5382fSLinus Lüssing 		goto out;
373c6c8fea2SSven Eckelmann 
3747351a482SSimon Wunderlich 	router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
375e1a5382fSLinus Lüssing 	if (!router_orig)
376e1a5382fSLinus Lüssing 		goto out;
377c6c8fea2SSven Eckelmann 
37889652331SSimon Wunderlich 	router_orig_tq = batadv_neigh_ifinfo_get(router_orig,
37989652331SSimon Wunderlich 						 BATADV_IF_DEFAULT);
38089652331SSimon Wunderlich 	if (!router_orig_tq)
38189652331SSimon Wunderlich 		goto out;
38289652331SSimon Wunderlich 
38389652331SSimon Wunderlich 	gw_tq_avg = router_gw_tq->bat_iv.tq_avg;
38489652331SSimon Wunderlich 	orig_tq_avg = router_orig_tq->bat_iv.tq_avg;
385c6c8fea2SSven Eckelmann 
386c6c8fea2SSven Eckelmann 	/* the TQ value has to be better */
387c6c8fea2SSven Eckelmann 	if (orig_tq_avg < gw_tq_avg)
3885d02b3cdSLinus Lüssing 		goto out;
389c6c8fea2SSven Eckelmann 
3909cfc7bd6SSven Eckelmann 	/* if the routing class is greater than 3 the value tells us how much
391c6c8fea2SSven Eckelmann 	 * greater the TQ value of the new gateway must be
3929cfc7bd6SSven Eckelmann 	 */
393c6c8fea2SSven Eckelmann 	if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
394c6c8fea2SSven Eckelmann 	    (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
3955d02b3cdSLinus Lüssing 		goto out;
396c6c8fea2SSven Eckelmann 
39739c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
39886ceb360SSven Eckelmann 		   "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
399c6c8fea2SSven Eckelmann 		   gw_tq_avg, orig_tq_avg);
400c6c8fea2SSven Eckelmann 
4014e820e72SAntonio Quartulli reselect:
4024e820e72SAntonio Quartulli 	batadv_gw_reselect(bat_priv);
4035d02b3cdSLinus Lüssing out:
40457f0c07cSLinus Lüssing 	if (curr_gw_orig)
4057d211efcSSven Eckelmann 		batadv_orig_node_free_ref(curr_gw_orig);
406e1a5382fSLinus Lüssing 	if (router_gw)
4077d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router_gw);
408e1a5382fSLinus Lüssing 	if (router_orig)
4097d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router_orig);
41089652331SSimon Wunderlich 	if (router_gw_tq)
41189652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_gw_tq);
41289652331SSimon Wunderlich 	if (router_orig_tq)
41389652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_orig_tq);
414c6c8fea2SSven Eckelmann }
415c6c8fea2SSven Eckelmann 
416414254e3SMarek Lindner /**
417414254e3SMarek Lindner  * batadv_gw_node_add - add gateway node to list of available gateways
418414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
419414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
420414254e3SMarek Lindner  * @gateway: announced bandwidth information
421414254e3SMarek Lindner  */
42256303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv,
42356303d34SSven Eckelmann 			       struct batadv_orig_node *orig_node,
424414254e3SMarek Lindner 			       struct batadv_tvlv_gateway_data *gateway)
425c6c8fea2SSven Eckelmann {
42656303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
427414254e3SMarek Lindner 
428414254e3SMarek Lindner 	if (gateway->bandwidth_down == 0)
429414254e3SMarek Lindner 		return;
430c6c8fea2SSven Eckelmann 
431377fe0f9SAntonio Quartulli 	if (!atomic_inc_not_zero(&orig_node->refcount))
432c6c8fea2SSven Eckelmann 		return;
433c6c8fea2SSven Eckelmann 
434377fe0f9SAntonio Quartulli 	gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
435377fe0f9SAntonio Quartulli 	if (!gw_node) {
436377fe0f9SAntonio Quartulli 		batadv_orig_node_free_ref(orig_node);
437377fe0f9SAntonio Quartulli 		return;
438377fe0f9SAntonio Quartulli 	}
439377fe0f9SAntonio Quartulli 
440c6c8fea2SSven Eckelmann 	INIT_HLIST_NODE(&gw_node->list);
441c6c8fea2SSven Eckelmann 	gw_node->orig_node = orig_node;
442*27a4d5efSSimon Wunderlich 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
443*27a4d5efSSimon Wunderlich 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
44425b6d3c1SMarek Lindner 	atomic_set(&gw_node->refcount, 1);
445c6c8fea2SSven Eckelmann 
446807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
447807736f6SSven Eckelmann 	hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
448807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
449c6c8fea2SSven Eckelmann 
45039c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
451414254e3SMarek Lindner 		   "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
452414254e3SMarek Lindner 		   orig_node->orig,
453414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
454414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
455414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
456414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
457c6c8fea2SSven Eckelmann }
458c6c8fea2SSven Eckelmann 
459414254e3SMarek Lindner /**
460414254e3SMarek Lindner  * batadv_gw_node_get - retrieve gateway node from list of available gateways
461414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
462414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
463414254e3SMarek Lindner  *
464414254e3SMarek Lindner  * Returns gateway node if found or NULL otherwise.
46571e4aa9cSAntonio Quartulli  */
466414254e3SMarek Lindner static struct batadv_gw_node *
467414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv,
468414254e3SMarek Lindner 		   struct batadv_orig_node *orig_node)
469414254e3SMarek Lindner {
470414254e3SMarek Lindner 	struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
471c6c8fea2SSven Eckelmann 
472c6c8fea2SSven Eckelmann 	rcu_read_lock();
473414254e3SMarek Lindner 	hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
474414254e3SMarek Lindner 		if (gw_node_tmp->orig_node != orig_node)
475c6c8fea2SSven Eckelmann 			continue;
476c6c8fea2SSven Eckelmann 
477414254e3SMarek Lindner 		if (gw_node_tmp->deleted)
478414254e3SMarek Lindner 			continue;
479414254e3SMarek Lindner 
480414254e3SMarek Lindner 		if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
481414254e3SMarek Lindner 			continue;
482414254e3SMarek Lindner 
483414254e3SMarek Lindner 		gw_node = gw_node_tmp;
484414254e3SMarek Lindner 		break;
485414254e3SMarek Lindner 	}
486414254e3SMarek Lindner 	rcu_read_unlock();
487414254e3SMarek Lindner 
488414254e3SMarek Lindner 	return gw_node;
489414254e3SMarek Lindner }
490414254e3SMarek Lindner 
491414254e3SMarek Lindner /**
492414254e3SMarek Lindner  * batadv_gw_node_update - update list of available gateways with changed
493414254e3SMarek Lindner  *  bandwidth information
494414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
495414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
496414254e3SMarek Lindner  * @gateway: announced bandwidth information
497414254e3SMarek Lindner  */
498414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv,
499414254e3SMarek Lindner 			   struct batadv_orig_node *orig_node,
500414254e3SMarek Lindner 			   struct batadv_tvlv_gateway_data *gateway)
501414254e3SMarek Lindner {
502414254e3SMarek Lindner 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
503414254e3SMarek Lindner 
504414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_node);
505414254e3SMarek Lindner 	if (!gw_node) {
506414254e3SMarek Lindner 		batadv_gw_node_add(bat_priv, orig_node, gateway);
507414254e3SMarek Lindner 		goto out;
508414254e3SMarek Lindner 	}
509414254e3SMarek Lindner 
510414254e3SMarek Lindner 	if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
511414254e3SMarek Lindner 	    (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
512414254e3SMarek Lindner 		goto out;
513414254e3SMarek Lindner 
51439c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
515414254e3SMarek Lindner 		   "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
516414254e3SMarek Lindner 		   orig_node->orig,
517414254e3SMarek Lindner 		   gw_node->bandwidth_down / 10,
518414254e3SMarek Lindner 		   gw_node->bandwidth_down % 10,
519414254e3SMarek Lindner 		   gw_node->bandwidth_up / 10,
520414254e3SMarek Lindner 		   gw_node->bandwidth_up % 10,
521414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
522414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
523414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
524414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
525414254e3SMarek Lindner 
526414254e3SMarek Lindner 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
527414254e3SMarek Lindner 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
528c6c8fea2SSven Eckelmann 
529c6c8fea2SSven Eckelmann 	gw_node->deleted = 0;
530414254e3SMarek Lindner 	if (ntohl(gateway->bandwidth_down) == 0) {
531c6c8fea2SSven Eckelmann 		gw_node->deleted = jiffies;
53239c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
533c6c8fea2SSven Eckelmann 			   "Gateway %pM removed from gateway list\n",
534c6c8fea2SSven Eckelmann 			   orig_node->orig);
535c6c8fea2SSven Eckelmann 
536414254e3SMarek Lindner 		/* Note: We don't need a NULL check here, since curr_gw never
537414254e3SMarek Lindner 		 * gets dereferenced.
538414254e3SMarek Lindner 		 */
539414254e3SMarek Lindner 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
540c4aac1abSMarek Lindner 		if (gw_node == curr_gw)
5414e820e72SAntonio Quartulli 			batadv_gw_reselect(bat_priv);
542414254e3SMarek Lindner 	}
54371e4aa9cSAntonio Quartulli 
544414254e3SMarek Lindner out:
545c4aac1abSMarek Lindner 	if (curr_gw)
5461409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
547414254e3SMarek Lindner 	if (gw_node)
548414254e3SMarek Lindner 		batadv_gw_node_free_ref(gw_node);
549c6c8fea2SSven Eckelmann }
550c6c8fea2SSven Eckelmann 
55156303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv,
55256303d34SSven Eckelmann 			   struct batadv_orig_node *orig_node)
553c6c8fea2SSven Eckelmann {
554414254e3SMarek Lindner 	struct batadv_tvlv_gateway_data gateway;
555414254e3SMarek Lindner 
556414254e3SMarek Lindner 	gateway.bandwidth_down = 0;
557414254e3SMarek Lindner 	gateway.bandwidth_up = 0;
558414254e3SMarek Lindner 
559414254e3SMarek Lindner 	batadv_gw_node_update(bat_priv, orig_node, &gateway);
560c6c8fea2SSven Eckelmann }
561c6c8fea2SSven Eckelmann 
56256303d34SSven Eckelmann void batadv_gw_node_purge(struct batadv_priv *bat_priv)
563c6c8fea2SSven Eckelmann {
56456303d34SSven Eckelmann 	struct batadv_gw_node *gw_node, *curr_gw;
565b67bfe0dSSasha Levin 	struct hlist_node *node_tmp;
56642d0b044SSven Eckelmann 	unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
5674e820e72SAntonio Quartulli 	int do_reselect = 0;
568c4aac1abSMarek Lindner 
5691409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
570c6c8fea2SSven Eckelmann 
571807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
572c6c8fea2SSven Eckelmann 
573b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(gw_node, node_tmp,
574807736f6SSven Eckelmann 				  &bat_priv->gw.list, list) {
575c6c8fea2SSven Eckelmann 		if (((!gw_node->deleted) ||
576c6c8fea2SSven Eckelmann 		     (time_before(jiffies, gw_node->deleted + timeout))) &&
57739c75a51SSven Eckelmann 		    atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
578c6c8fea2SSven Eckelmann 			continue;
579c6c8fea2SSven Eckelmann 
580c4aac1abSMarek Lindner 		if (curr_gw == gw_node)
5814e820e72SAntonio Quartulli 			do_reselect = 1;
582c6c8fea2SSven Eckelmann 
583c6c8fea2SSven Eckelmann 		hlist_del_rcu(&gw_node->list);
5841409a834SSven Eckelmann 		batadv_gw_node_free_ref(gw_node);
585c6c8fea2SSven Eckelmann 	}
586c6c8fea2SSven Eckelmann 
587807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
588c4aac1abSMarek Lindner 
5894e820e72SAntonio Quartulli 	/* gw_reselect() needs to acquire the gw_list_lock */
5904e820e72SAntonio Quartulli 	if (do_reselect)
5914e820e72SAntonio Quartulli 		batadv_gw_reselect(bat_priv);
592c4aac1abSMarek Lindner 
593c4aac1abSMarek Lindner 	if (curr_gw)
5941409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
595c6c8fea2SSven Eckelmann }
596c6c8fea2SSven Eckelmann 
5979cfc7bd6SSven Eckelmann /* fails if orig_node has no router */
59856303d34SSven Eckelmann static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
5991409a834SSven Eckelmann 				    struct seq_file *seq,
60056303d34SSven Eckelmann 				    const struct batadv_gw_node *gw_node)
601c6c8fea2SSven Eckelmann {
60256303d34SSven Eckelmann 	struct batadv_gw_node *curr_gw;
60356303d34SSven Eckelmann 	struct batadv_neigh_node *router;
60489652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
605414254e3SMarek Lindner 	int ret = -1;
606c6c8fea2SSven Eckelmann 
6077351a482SSimon Wunderlich 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
608e1a5382fSLinus Lüssing 	if (!router)
609e1a5382fSLinus Lüssing 		goto out;
610e1a5382fSLinus Lüssing 
61189652331SSimon Wunderlich 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
61289652331SSimon Wunderlich 	if (!router_ifinfo)
61389652331SSimon Wunderlich 		goto out;
61489652331SSimon Wunderlich 
6151409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
6165d02b3cdSLinus Lüssing 
6176d91147dSJoe Perches 	seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
6185d02b3cdSLinus Lüssing 		   (curr_gw == gw_node ? "=>" : "  "),
619c6c8fea2SSven Eckelmann 		   gw_node->orig_node->orig,
62089652331SSimon Wunderlich 		   router_ifinfo->bat_iv.tq_avg, router->addr,
621e1a5382fSLinus Lüssing 		   router->if_incoming->net_dev->name,
622414254e3SMarek Lindner 		   gw_node->bandwidth_down / 10,
623414254e3SMarek Lindner 		   gw_node->bandwidth_down % 10,
624414254e3SMarek Lindner 		   gw_node->bandwidth_up / 10,
625414254e3SMarek Lindner 		   gw_node->bandwidth_up % 10);
62692b83917SJoe Perches 	ret = seq_has_overflowed(seq) ? -1 : 0;
6275d02b3cdSLinus Lüssing 
628c4aac1abSMarek Lindner 	if (curr_gw)
6291409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
630e1a5382fSLinus Lüssing out:
63189652331SSimon Wunderlich 	if (router_ifinfo)
63289652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_ifinfo);
63389652331SSimon Wunderlich 	if (router)
63489652331SSimon Wunderlich 		batadv_neigh_node_free_ref(router);
6355d02b3cdSLinus Lüssing 	return ret;
636c6c8fea2SSven Eckelmann }
637c6c8fea2SSven Eckelmann 
6387cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
639c6c8fea2SSven Eckelmann {
640c6c8fea2SSven Eckelmann 	struct net_device *net_dev = (struct net_device *)seq->private;
64156303d34SSven Eckelmann 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
64256303d34SSven Eckelmann 	struct batadv_hard_iface *primary_if;
64356303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
64430da63a6SMarek Lindner 	int gw_count = 0;
645c6c8fea2SSven Eckelmann 
64630da63a6SMarek Lindner 	primary_if = batadv_seq_print_text_primary_if_get(seq);
64730da63a6SMarek Lindner 	if (!primary_if)
64832ae9b22SMarek Lindner 		goto out;
649c6c8fea2SSven Eckelmann 
65086ceb360SSven Eckelmann 	seq_printf(seq,
651414254e3SMarek Lindner 		   "      %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
65242d0b044SSven Eckelmann 		   "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
65342d0b044SSven Eckelmann 		   BATADV_SOURCE_VERSION, primary_if->net_dev->name,
65432ae9b22SMarek Lindner 		   primary_if->net_dev->dev_addr, net_dev->name);
655c6c8fea2SSven Eckelmann 
656c6c8fea2SSven Eckelmann 	rcu_read_lock();
657b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
658c6c8fea2SSven Eckelmann 		if (gw_node->deleted)
659c6c8fea2SSven Eckelmann 			continue;
660c6c8fea2SSven Eckelmann 
661e1a5382fSLinus Lüssing 		/* fails if orig_node has no router */
6621409a834SSven Eckelmann 		if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
663c6c8fea2SSven Eckelmann 			continue;
664c6c8fea2SSven Eckelmann 
665c6c8fea2SSven Eckelmann 		gw_count++;
666c6c8fea2SSven Eckelmann 	}
667c6c8fea2SSven Eckelmann 	rcu_read_unlock();
668c6c8fea2SSven Eckelmann 
669c6c8fea2SSven Eckelmann 	if (gw_count == 0)
6700c814653SAntonio Quartulli 		seq_puts(seq, "No gateways in range ...\n");
671c6c8fea2SSven Eckelmann 
67232ae9b22SMarek Lindner out:
67332ae9b22SMarek Lindner 	if (primary_if)
674e5d89254SSven Eckelmann 		batadv_hardif_free_ref(primary_if);
67530da63a6SMarek Lindner 	return 0;
676c6c8fea2SSven Eckelmann }
677c6c8fea2SSven Eckelmann 
6786c413b1cSAntonio Quartulli /**
6796c413b1cSAntonio Quartulli  * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
6806c413b1cSAntonio Quartulli  * @skb: the packet to check
6816c413b1cSAntonio Quartulli  * @header_len: a pointer to the batman-adv header size
6826c413b1cSAntonio Quartulli  * @chaddr: buffer where the client address will be stored. Valid
6836c413b1cSAntonio Quartulli  *  only if the function returns BATADV_DHCP_TO_CLIENT
6846c413b1cSAntonio Quartulli  *
6856c413b1cSAntonio Quartulli  * Returns:
6866c413b1cSAntonio Quartulli  * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
6876c413b1cSAntonio Quartulli  *   while parsing it
6886c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
6896c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
6906c413b1cSAntonio Quartulli  *
6916c413b1cSAntonio Quartulli  * This function may re-allocate the data buffer of the skb passed as argument.
6929cfc7bd6SSven Eckelmann  */
6936c413b1cSAntonio Quartulli enum batadv_dhcp_recipient
6946c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
6956c413b1cSAntonio Quartulli 			     uint8_t *chaddr)
696c6c8fea2SSven Eckelmann {
6976c413b1cSAntonio Quartulli 	enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
698c6c8fea2SSven Eckelmann 	struct ethhdr *ethhdr;
699c6c8fea2SSven Eckelmann 	struct iphdr *iphdr;
700c6c8fea2SSven Eckelmann 	struct ipv6hdr *ipv6hdr;
701c6c8fea2SSven Eckelmann 	struct udphdr *udphdr;
702f7f8ed56SAntonio Quartulli 	struct vlan_ethhdr *vhdr;
7036c413b1cSAntonio Quartulli 	int chaddr_offset;
704f7f8ed56SAntonio Quartulli 	__be16 proto;
7056c413b1cSAntonio Quartulli 	uint8_t *p;
706c6c8fea2SSven Eckelmann 
707c6c8fea2SSven Eckelmann 	/* check for ethernet header */
708be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
7096c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
7106c413b1cSAntonio Quartulli 
711927c2ed7SLinus Lüssing 	ethhdr = eth_hdr(skb);
712f7f8ed56SAntonio Quartulli 	proto = ethhdr->h_proto;
713be7af5cfSMarek Lindner 	*header_len += ETH_HLEN;
714c6c8fea2SSven Eckelmann 
715c6c8fea2SSven Eckelmann 	/* check for initial vlan header */
716f7f8ed56SAntonio Quartulli 	if (proto == htons(ETH_P_8021Q)) {
717be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
7186c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
719f7f8ed56SAntonio Quartulli 
720927c2ed7SLinus Lüssing 		vhdr = vlan_eth_hdr(skb);
721f7f8ed56SAntonio Quartulli 		proto = vhdr->h_vlan_encapsulated_proto;
722be7af5cfSMarek Lindner 		*header_len += VLAN_HLEN;
723c6c8fea2SSven Eckelmann 	}
724c6c8fea2SSven Eckelmann 
725c6c8fea2SSven Eckelmann 	/* check for ip header */
726f7f8ed56SAntonio Quartulli 	switch (proto) {
727f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IP):
728be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
7296c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7306c413b1cSAntonio Quartulli 
731be7af5cfSMarek Lindner 		iphdr = (struct iphdr *)(skb->data + *header_len);
732be7af5cfSMarek Lindner 		*header_len += iphdr->ihl * 4;
733c6c8fea2SSven Eckelmann 
734c6c8fea2SSven Eckelmann 		/* check for udp header */
735c6c8fea2SSven Eckelmann 		if (iphdr->protocol != IPPROTO_UDP)
7366c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
737c6c8fea2SSven Eckelmann 
738c6c8fea2SSven Eckelmann 		break;
739f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IPV6):
740be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
7416c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7426c413b1cSAntonio Quartulli 
743be7af5cfSMarek Lindner 		ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
744be7af5cfSMarek Lindner 		*header_len += sizeof(*ipv6hdr);
745c6c8fea2SSven Eckelmann 
746c6c8fea2SSven Eckelmann 		/* check for udp header */
747c6c8fea2SSven Eckelmann 		if (ipv6hdr->nexthdr != IPPROTO_UDP)
7486c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
749c6c8fea2SSven Eckelmann 
750c6c8fea2SSven Eckelmann 		break;
751c6c8fea2SSven Eckelmann 	default:
7526c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
753c6c8fea2SSven Eckelmann 	}
754c6c8fea2SSven Eckelmann 
755be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
7566c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
7579d2c9488SLinus Lüssing 
758be7af5cfSMarek Lindner 	udphdr = (struct udphdr *)(skb->data + *header_len);
759be7af5cfSMarek Lindner 	*header_len += sizeof(*udphdr);
760c6c8fea2SSven Eckelmann 
761c6c8fea2SSven Eckelmann 	/* check for bootp port */
7626c413b1cSAntonio Quartulli 	switch (proto) {
7636c413b1cSAntonio Quartulli 	case htons(ETH_P_IP):
7646c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(67))
7656c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
7666c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(67))
7676c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
7686c413b1cSAntonio Quartulli 		break;
7696c413b1cSAntonio Quartulli 	case htons(ETH_P_IPV6):
7706c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(547))
7716c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
7726c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(547))
7736c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
7746c413b1cSAntonio Quartulli 		break;
775be7af5cfSMarek Lindner 	}
776c6c8fea2SSven Eckelmann 
7776c413b1cSAntonio Quartulli 	chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
7786c413b1cSAntonio Quartulli 	/* store the client address if the message is going to a client */
7796c413b1cSAntonio Quartulli 	if (ret == BATADV_DHCP_TO_CLIENT &&
7806c413b1cSAntonio Quartulli 	    pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
7816c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries an Ethernet DHCP */
7826c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
7836c413b1cSAntonio Quartulli 		if (*p != BATADV_DHCP_HTYPE_ETHERNET)
7846c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7856c413b1cSAntonio Quartulli 
7866c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries a valid Ethernet address */
7876c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
7886c413b1cSAntonio Quartulli 		if (*p != ETH_ALEN)
7896c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7906c413b1cSAntonio Quartulli 
7918fdd0153SAntonio Quartulli 		ether_addr_copy(chaddr, skb->data + chaddr_offset);
7926c413b1cSAntonio Quartulli 	}
7936c413b1cSAntonio Quartulli 
7946c413b1cSAntonio Quartulli 	return ret;
7956c413b1cSAntonio Quartulli }
796aa143d28SAntonio Quartulli 
797bbb877edSAntonio Quartulli /**
798bbb877edSAntonio Quartulli  * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
799bbb877edSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
800bbb877edSAntonio Quartulli  * @skb: the outgoing packet
801bbb877edSAntonio Quartulli  *
802bbb877edSAntonio Quartulli  * Check if the skb is a DHCP request and if it is sent to the current best GW
803bbb877edSAntonio Quartulli  * server. Due to topology changes it may be the case that the GW server
804bbb877edSAntonio Quartulli  * previously selected is not the best one anymore.
805bbb877edSAntonio Quartulli  *
806bbb877edSAntonio Quartulli  * Returns true if the packet destination is unicast and it is not the best gw,
807bbb877edSAntonio Quartulli  * false otherwise.
808bbb877edSAntonio Quartulli  *
809bbb877edSAntonio Quartulli  * This call might reallocate skb data.
8106c413b1cSAntonio Quartulli  * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
811bbb877edSAntonio Quartulli  */
81256303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
8139d2c9488SLinus Lüssing 			    struct sk_buff *skb)
814be7af5cfSMarek Lindner {
81556303d34SSven Eckelmann 	struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
81656303d34SSven Eckelmann 	struct batadv_orig_node *orig_dst_node = NULL;
817414254e3SMarek Lindner 	struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
81889652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
8196c413b1cSAntonio Quartulli 	struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
8206c413b1cSAntonio Quartulli 	bool out_of_range = false;
821be7af5cfSMarek Lindner 	uint8_t curr_tq_avg;
822bbb877edSAntonio Quartulli 	unsigned short vid;
823bbb877edSAntonio Quartulli 
824bbb877edSAntonio Quartulli 	vid = batadv_get_vid(skb, 0);
825be7af5cfSMarek Lindner 
82608c36d3eSSven Eckelmann 	orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
827bbb877edSAntonio Quartulli 						 ethhdr->h_dest, vid);
828be7af5cfSMarek Lindner 	if (!orig_dst_node)
829be7af5cfSMarek Lindner 		goto out;
830be7af5cfSMarek Lindner 
831414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
8320d164491SAntonio Quartulli 	if (!gw_node)
833be7af5cfSMarek Lindner 		goto out;
834be7af5cfSMarek Lindner 
835be7af5cfSMarek Lindner 	switch (atomic_read(&bat_priv->gw_mode)) {
836cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_SERVER:
837be7af5cfSMarek Lindner 		/* If we are a GW then we are our best GW. We can artificially
8389cfc7bd6SSven Eckelmann 		 * set the tq towards ourself as the maximum value
8399cfc7bd6SSven Eckelmann 		 */
84042d0b044SSven Eckelmann 		curr_tq_avg = BATADV_TQ_MAX_VALUE;
841be7af5cfSMarek Lindner 		break;
842cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_CLIENT:
8431409a834SSven Eckelmann 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
844c4aac1abSMarek Lindner 		if (!curr_gw)
845be7af5cfSMarek Lindner 			goto out;
846c6c8fea2SSven Eckelmann 
847be7af5cfSMarek Lindner 		/* packet is going to our gateway */
848be7af5cfSMarek Lindner 		if (curr_gw->orig_node == orig_dst_node)
849be7af5cfSMarek Lindner 			goto out;
850be7af5cfSMarek Lindner 
85143676ab5SAntonio Quartulli 		/* If the dhcp packet has been sent to a different gw,
85243676ab5SAntonio Quartulli 		 * we have to evaluate whether the old gw is still
8539cfc7bd6SSven Eckelmann 		 * reliable enough
8549cfc7bd6SSven Eckelmann 		 */
85530d3c511SSven Eckelmann 		neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
85630d3c511SSven Eckelmann 						NULL);
857be7af5cfSMarek Lindner 		if (!neigh_curr)
858be7af5cfSMarek Lindner 			goto out;
859be7af5cfSMarek Lindner 
86089652331SSimon Wunderlich 		curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
86189652331SSimon Wunderlich 						      BATADV_IF_DEFAULT);
86289652331SSimon Wunderlich 		if (!curr_ifinfo)
86389652331SSimon Wunderlich 			goto out;
86489652331SSimon Wunderlich 
86589652331SSimon Wunderlich 		curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
86689652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(curr_ifinfo);
86789652331SSimon Wunderlich 
868be7af5cfSMarek Lindner 		break;
869cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_OFF:
870be7af5cfSMarek Lindner 	default:
871be7af5cfSMarek Lindner 		goto out;
87243676ab5SAntonio Quartulli 	}
873be7af5cfSMarek Lindner 
87430d3c511SSven Eckelmann 	neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
8752ef04f47SDan Carpenter 	if (!neigh_old)
876be7af5cfSMarek Lindner 		goto out;
877be7af5cfSMarek Lindner 
87889652331SSimon Wunderlich 	old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
87989652331SSimon Wunderlich 	if (!old_ifinfo)
88089652331SSimon Wunderlich 		goto out;
88189652331SSimon Wunderlich 
88289652331SSimon Wunderlich 	if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
883be7af5cfSMarek Lindner 		out_of_range = true;
88489652331SSimon Wunderlich 	batadv_neigh_ifinfo_free_ref(old_ifinfo);
885be7af5cfSMarek Lindner 
886be7af5cfSMarek Lindner out:
887be7af5cfSMarek Lindner 	if (orig_dst_node)
8887d211efcSSven Eckelmann 		batadv_orig_node_free_ref(orig_dst_node);
889be7af5cfSMarek Lindner 	if (curr_gw)
8901409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
891414254e3SMarek Lindner 	if (gw_node)
892414254e3SMarek Lindner 		batadv_gw_node_free_ref(gw_node);
89343676ab5SAntonio Quartulli 	if (neigh_old)
8947d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(neigh_old);
89543676ab5SAntonio Quartulli 	if (neigh_curr)
8967d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(neigh_curr);
897be7af5cfSMarek Lindner 	return out_of_range;
898c6c8fea2SSven Eckelmann }
899