xref: /openbmc/linux/net/batman-adv/gateway_client.c (revision e7aed321b8c07d2019b003c3f51a067f2b4f78ab)
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>
31*e7aed321SSven 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"
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 
63*e7aed321SSven Eckelmann /**
64*e7aed321SSven Eckelmann  * batadv_gw_node_release - release gw_node from lists and queue for free after
65*e7aed321SSven Eckelmann  *  rcu grace period
66*e7aed321SSven Eckelmann  * @ref: kref pointer of the gw_node
67*e7aed321SSven Eckelmann  */
68*e7aed321SSven Eckelmann static void batadv_gw_node_release(struct kref *ref)
6925b6d3c1SMarek Lindner {
70*e7aed321SSven Eckelmann 	struct batadv_gw_node *gw_node;
71*e7aed321SSven Eckelmann 
72*e7aed321SSven Eckelmann 	gw_node = container_of(ref, struct batadv_gw_node, refcount);
73*e7aed321SSven Eckelmann 
74377fe0f9SAntonio Quartulli 	batadv_orig_node_free_ref(gw_node->orig_node);
75eb340b2fSPaul E. McKenney 	kfree_rcu(gw_node, rcu);
76c6c8fea2SSven Eckelmann }
77*e7aed321SSven Eckelmann 
78*e7aed321SSven Eckelmann /**
79*e7aed321SSven Eckelmann  * batadv_gw_node_free_ref - decrement the gw_node refcounter and possibly
80*e7aed321SSven Eckelmann  *  release it
81*e7aed321SSven Eckelmann  * @gw_node: gateway node to free
82*e7aed321SSven Eckelmann  */
83*e7aed321SSven Eckelmann static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node)
84*e7aed321SSven Eckelmann {
85*e7aed321SSven Eckelmann 	kref_put(&gw_node->refcount, batadv_gw_node_release);
86377fe0f9SAntonio Quartulli }
87c6c8fea2SSven Eckelmann 
8856303d34SSven Eckelmann static 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 
98*e7aed321SSven 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 
1217b36e8eeSMarek Lindner 	if (!atomic_inc_not_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)
1281409a834SSven Eckelmann 		batadv_gw_node_free_ref(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 
139*e7aed321SSven Eckelmann 	if (new_gw_node && !kref_get_unless_zero(&new_gw_node->refcount))
14025b6d3c1SMarek Lindner 		new_gw_node = NULL;
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)
1461409a834SSven Eckelmann 		batadv_gw_node_free_ref(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 
16756303d34SSven Eckelmann static struct batadv_gw_node *
16856303d34SSven Eckelmann batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
169c6c8fea2SSven Eckelmann {
17056303d34SSven Eckelmann 	struct batadv_neigh_node *router;
17189652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo;
17256303d34SSven Eckelmann 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
1734f248cffSSven Eckelmann 	u64 max_gw_factor = 0;
1744f248cffSSven Eckelmann 	u64 tmp_gw_factor = 0;
1756b5e971aSSven Eckelmann 	u8 max_tq = 0;
1766b5e971aSSven Eckelmann 	u8 tq_avg;
17756303d34SSven Eckelmann 	struct batadv_orig_node *orig_node;
178c6c8fea2SSven Eckelmann 
179c6c8fea2SSven Eckelmann 	rcu_read_lock();
180b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
18184d5e5e0SSven Eckelmann 		orig_node = gw_node->orig_node;
1827351a482SSimon Wunderlich 		router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
183e1a5382fSLinus Lüssing 		if (!router)
184c6c8fea2SSven Eckelmann 			continue;
185c6c8fea2SSven Eckelmann 
18689652331SSimon Wunderlich 		router_ifinfo = batadv_neigh_ifinfo_get(router,
18789652331SSimon Wunderlich 							BATADV_IF_DEFAULT);
18889652331SSimon Wunderlich 		if (!router_ifinfo)
18989652331SSimon Wunderlich 			goto next;
19089652331SSimon Wunderlich 
191*e7aed321SSven Eckelmann 		if (!kref_get_unless_zero(&gw_node->refcount))
1922265c141SAntonio Quartulli 			goto next;
1932265c141SAntonio Quartulli 
19489652331SSimon Wunderlich 		tq_avg = router_ifinfo->bat_iv.tq_avg;
195c67893d1SSven Eckelmann 
196c6c8fea2SSven Eckelmann 		switch (atomic_read(&bat_priv->gw_sel_class)) {
197c6c8fea2SSven Eckelmann 		case 1: /* fast connection */
198414254e3SMarek Lindner 			tmp_gw_factor = tq_avg * tq_avg;
199414254e3SMarek Lindner 			tmp_gw_factor *= gw_node->bandwidth_down;
200414254e3SMarek Lindner 			tmp_gw_factor *= 100 * 100;
201e071d93eSSven Eckelmann 			tmp_gw_factor >>= 18;
202c6c8fea2SSven Eckelmann 
203c6c8fea2SSven Eckelmann 			if ((tmp_gw_factor > max_gw_factor) ||
204c6c8fea2SSven Eckelmann 			    ((tmp_gw_factor == max_gw_factor) &&
205c67893d1SSven Eckelmann 			     (tq_avg > max_tq))) {
2062265c141SAntonio Quartulli 				if (curr_gw)
2071409a834SSven Eckelmann 					batadv_gw_node_free_ref(curr_gw);
2082265c141SAntonio Quartulli 				curr_gw = gw_node;
209*e7aed321SSven Eckelmann 				kref_get(&curr_gw->refcount);
2102265c141SAntonio Quartulli 			}
211c6c8fea2SSven Eckelmann 			break;
212c6c8fea2SSven Eckelmann 
2139cfc7bd6SSven Eckelmann 		default: /* 2:  stable connection (use best statistic)
214c6c8fea2SSven Eckelmann 			  * 3:  fast-switch (use best statistic but change as
215c6c8fea2SSven Eckelmann 			  *     soon as a better gateway appears)
216c6c8fea2SSven Eckelmann 			  * XX: late-switch (use best statistic but change as
217c6c8fea2SSven Eckelmann 			  *     soon as a better gateway appears which has
218c6c8fea2SSven Eckelmann 			  *     $routing_class more tq points)
2199cfc7bd6SSven Eckelmann 			  */
220c67893d1SSven Eckelmann 			if (tq_avg > max_tq) {
2212265c141SAntonio Quartulli 				if (curr_gw)
2221409a834SSven Eckelmann 					batadv_gw_node_free_ref(curr_gw);
2232265c141SAntonio Quartulli 				curr_gw = gw_node;
224*e7aed321SSven Eckelmann 				kref_get(&curr_gw->refcount);
2252265c141SAntonio Quartulli 			}
226c6c8fea2SSven Eckelmann 			break;
227c6c8fea2SSven Eckelmann 		}
228c6c8fea2SSven Eckelmann 
229c67893d1SSven Eckelmann 		if (tq_avg > max_tq)
230c67893d1SSven Eckelmann 			max_tq = tq_avg;
231c6c8fea2SSven Eckelmann 
232c6c8fea2SSven Eckelmann 		if (tmp_gw_factor > max_gw_factor)
233c6c8fea2SSven Eckelmann 			max_gw_factor = tmp_gw_factor;
234e1a5382fSLinus Lüssing 
2351409a834SSven Eckelmann 		batadv_gw_node_free_ref(gw_node);
2362265c141SAntonio Quartulli 
2372265c141SAntonio Quartulli next:
2387d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router);
23989652331SSimon Wunderlich 		if (router_ifinfo)
24089652331SSimon Wunderlich 			batadv_neigh_ifinfo_free_ref(router_ifinfo);
241c6c8fea2SSven Eckelmann 	}
2422265c141SAntonio Quartulli 	rcu_read_unlock();
243c6c8fea2SSven Eckelmann 
2442265c141SAntonio Quartulli 	return curr_gw;
2452265c141SAntonio Quartulli }
246e1a5382fSLinus Lüssing 
247c6eaa3f0SAntonio Quartulli /**
248c6eaa3f0SAntonio Quartulli  * batadv_gw_check_client_stop - check if client mode has been switched off
249c6eaa3f0SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
250c6eaa3f0SAntonio Quartulli  *
251c6eaa3f0SAntonio Quartulli  * This function assumes the caller has checked that the gw state *is actually
252c6eaa3f0SAntonio Quartulli  * changing*. This function is not supposed to be called when there is no state
253c6eaa3f0SAntonio Quartulli  * change.
254c6eaa3f0SAntonio Quartulli  */
255c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
256c6eaa3f0SAntonio Quartulli {
257c6eaa3f0SAntonio Quartulli 	struct batadv_gw_node *curr_gw;
258c6eaa3f0SAntonio Quartulli 
259c6eaa3f0SAntonio Quartulli 	if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
260c6eaa3f0SAntonio Quartulli 		return;
261c6eaa3f0SAntonio Quartulli 
262c6eaa3f0SAntonio Quartulli 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
263c6eaa3f0SAntonio Quartulli 	if (!curr_gw)
264c6eaa3f0SAntonio Quartulli 		return;
265c6eaa3f0SAntonio Quartulli 
266f3163181SAntonio Quartulli 	/* deselect the current gateway so that next time that client mode is
267f3163181SAntonio Quartulli 	 * enabled a proper GW_ADD event can be sent
268f3163181SAntonio Quartulli 	 */
269f3163181SAntonio Quartulli 	batadv_gw_select(bat_priv, NULL);
270f3163181SAntonio Quartulli 
271c6eaa3f0SAntonio Quartulli 	/* if batman-adv is switching the gw client mode off and a gateway was
272c6eaa3f0SAntonio Quartulli 	 * already selected, send a DEL uevent
273c6eaa3f0SAntonio Quartulli 	 */
274c6eaa3f0SAntonio Quartulli 	batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
275c6eaa3f0SAntonio Quartulli 
276c6eaa3f0SAntonio Quartulli 	batadv_gw_node_free_ref(curr_gw);
277c6eaa3f0SAntonio Quartulli }
278c6eaa3f0SAntonio Quartulli 
27956303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv)
2802265c141SAntonio Quartulli {
2814f248cffSSven Eckelmann 	struct batadv_gw_node *curr_gw = NULL;
2824f248cffSSven Eckelmann 	struct batadv_gw_node *next_gw = NULL;
28356303d34SSven Eckelmann 	struct batadv_neigh_node *router = NULL;
28489652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
28519595e05SAntonio Quartulli 	char gw_addr[18] = { '\0' };
2862265c141SAntonio Quartulli 
287cd646ab1SSven Eckelmann 	if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
2882265c141SAntonio Quartulli 		goto out;
2892265c141SAntonio Quartulli 
2901409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2912265c141SAntonio Quartulli 
292807736f6SSven Eckelmann 	if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
293caa0bf64SMarek Lindner 		goto out;
294caa0bf64SMarek Lindner 
2951409a834SSven Eckelmann 	next_gw = batadv_gw_get_best_gw_node(bat_priv);
2962265c141SAntonio Quartulli 
2972265c141SAntonio Quartulli 	if (curr_gw == next_gw)
2982265c141SAntonio Quartulli 		goto out;
2992265c141SAntonio Quartulli 
3002265c141SAntonio Quartulli 	if (next_gw) {
30119595e05SAntonio Quartulli 		sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
30219595e05SAntonio Quartulli 
3037351a482SSimon Wunderlich 		router = batadv_orig_router_get(next_gw->orig_node,
3047351a482SSimon Wunderlich 						BATADV_IF_DEFAULT);
3052265c141SAntonio Quartulli 		if (!router) {
3064e820e72SAntonio Quartulli 			batadv_gw_reselect(bat_priv);
3072265c141SAntonio Quartulli 			goto out;
3082265c141SAntonio Quartulli 		}
30989652331SSimon Wunderlich 
31089652331SSimon Wunderlich 		router_ifinfo = batadv_neigh_ifinfo_get(router,
31189652331SSimon Wunderlich 							BATADV_IF_DEFAULT);
31289652331SSimon Wunderlich 		if (!router_ifinfo) {
31389652331SSimon Wunderlich 			batadv_gw_reselect(bat_priv);
31489652331SSimon Wunderlich 			goto out;
31589652331SSimon Wunderlich 		}
3162265c141SAntonio Quartulli 	}
3172265c141SAntonio Quartulli 
3182265c141SAntonio Quartulli 	if ((curr_gw) && (!next_gw)) {
31939c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
3202265c141SAntonio Quartulli 			   "Removing selected gateway - no gateway in range\n");
32139c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
32239c75a51SSven Eckelmann 				    NULL);
3232265c141SAntonio Quartulli 	} else if ((!curr_gw) && (next_gw)) {
32439c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
325414254e3SMarek Lindner 			   "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
3261eda58bfSSven Eckelmann 			   next_gw->orig_node->orig,
327414254e3SMarek Lindner 			   next_gw->bandwidth_down / 10,
328414254e3SMarek Lindner 			   next_gw->bandwidth_down % 10,
329414254e3SMarek Lindner 			   next_gw->bandwidth_up / 10,
33089652331SSimon Wunderlich 			   next_gw->bandwidth_up % 10,
33189652331SSimon Wunderlich 			   router_ifinfo->bat_iv.tq_avg);
33239c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
33339c75a51SSven Eckelmann 				    gw_addr);
3342265c141SAntonio Quartulli 	} else {
33539c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
336414254e3SMarek Lindner 			   "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
3371eda58bfSSven Eckelmann 			   next_gw->orig_node->orig,
338414254e3SMarek Lindner 			   next_gw->bandwidth_down / 10,
339414254e3SMarek Lindner 			   next_gw->bandwidth_down % 10,
340414254e3SMarek Lindner 			   next_gw->bandwidth_up / 10,
34189652331SSimon Wunderlich 			   next_gw->bandwidth_up % 10,
34289652331SSimon Wunderlich 			   router_ifinfo->bat_iv.tq_avg);
34339c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
34439c75a51SSven Eckelmann 				    gw_addr);
345c6c8fea2SSven Eckelmann 	}
346c6c8fea2SSven Eckelmann 
3471409a834SSven Eckelmann 	batadv_gw_select(bat_priv, next_gw);
3482265c141SAntonio Quartulli 
349c4aac1abSMarek Lindner out:
350c4aac1abSMarek Lindner 	if (curr_gw)
3511409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
3522265c141SAntonio Quartulli 	if (next_gw)
3531409a834SSven Eckelmann 		batadv_gw_node_free_ref(next_gw);
3542265c141SAntonio Quartulli 	if (router)
3557d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router);
35689652331SSimon Wunderlich 	if (router_ifinfo)
35789652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_ifinfo);
358c6c8fea2SSven Eckelmann }
359c6c8fea2SSven Eckelmann 
36056303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv,
36156303d34SSven Eckelmann 			      struct batadv_orig_node *orig_node)
362c6c8fea2SSven Eckelmann {
36389652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_orig_tq = NULL;
36489652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_gw_tq = NULL;
36556303d34SSven Eckelmann 	struct batadv_orig_node *curr_gw_orig;
3664f248cffSSven Eckelmann 	struct batadv_neigh_node *router_gw = NULL;
3674f248cffSSven Eckelmann 	struct batadv_neigh_node *router_orig = NULL;
3686b5e971aSSven Eckelmann 	u8 gw_tq_avg, orig_tq_avg;
369c6c8fea2SSven Eckelmann 
3707cf06bc6SSven Eckelmann 	curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
37157f0c07cSLinus Lüssing 	if (!curr_gw_orig)
3724e820e72SAntonio Quartulli 		goto reselect;
373c6c8fea2SSven Eckelmann 
3747351a482SSimon Wunderlich 	router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
375e1a5382fSLinus Lüssing 	if (!router_gw)
3764e820e72SAntonio Quartulli 		goto reselect;
377c6c8fea2SSven Eckelmann 
37889652331SSimon Wunderlich 	router_gw_tq = batadv_neigh_ifinfo_get(router_gw,
37989652331SSimon Wunderlich 					       BATADV_IF_DEFAULT);
38089652331SSimon Wunderlich 	if (!router_gw_tq)
38189652331SSimon Wunderlich 		goto reselect;
38289652331SSimon Wunderlich 
383c6c8fea2SSven Eckelmann 	/* this node already is the gateway */
38457f0c07cSLinus Lüssing 	if (curr_gw_orig == orig_node)
385e1a5382fSLinus Lüssing 		goto out;
386c6c8fea2SSven Eckelmann 
3877351a482SSimon Wunderlich 	router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
388e1a5382fSLinus Lüssing 	if (!router_orig)
389e1a5382fSLinus Lüssing 		goto out;
390c6c8fea2SSven Eckelmann 
39189652331SSimon Wunderlich 	router_orig_tq = batadv_neigh_ifinfo_get(router_orig,
39289652331SSimon Wunderlich 						 BATADV_IF_DEFAULT);
39389652331SSimon Wunderlich 	if (!router_orig_tq)
39489652331SSimon Wunderlich 		goto out;
39589652331SSimon Wunderlich 
39689652331SSimon Wunderlich 	gw_tq_avg = router_gw_tq->bat_iv.tq_avg;
39789652331SSimon Wunderlich 	orig_tq_avg = router_orig_tq->bat_iv.tq_avg;
398c6c8fea2SSven Eckelmann 
399c6c8fea2SSven Eckelmann 	/* the TQ value has to be better */
400c6c8fea2SSven Eckelmann 	if (orig_tq_avg < gw_tq_avg)
4015d02b3cdSLinus Lüssing 		goto out;
402c6c8fea2SSven Eckelmann 
4039cfc7bd6SSven Eckelmann 	/* if the routing class is greater than 3 the value tells us how much
404c6c8fea2SSven Eckelmann 	 * greater the TQ value of the new gateway must be
4059cfc7bd6SSven Eckelmann 	 */
406c6c8fea2SSven Eckelmann 	if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
407c6c8fea2SSven Eckelmann 	    (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
4085d02b3cdSLinus Lüssing 		goto out;
409c6c8fea2SSven Eckelmann 
41039c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
41186ceb360SSven Eckelmann 		   "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
412c6c8fea2SSven Eckelmann 		   gw_tq_avg, orig_tq_avg);
413c6c8fea2SSven Eckelmann 
4144e820e72SAntonio Quartulli reselect:
4154e820e72SAntonio Quartulli 	batadv_gw_reselect(bat_priv);
4165d02b3cdSLinus Lüssing out:
41757f0c07cSLinus Lüssing 	if (curr_gw_orig)
4187d211efcSSven Eckelmann 		batadv_orig_node_free_ref(curr_gw_orig);
419e1a5382fSLinus Lüssing 	if (router_gw)
4207d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router_gw);
421e1a5382fSLinus Lüssing 	if (router_orig)
4227d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router_orig);
42389652331SSimon Wunderlich 	if (router_gw_tq)
42489652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_gw_tq);
42589652331SSimon Wunderlich 	if (router_orig_tq)
42689652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_orig_tq);
427c6c8fea2SSven Eckelmann }
428c6c8fea2SSven Eckelmann 
429414254e3SMarek Lindner /**
430414254e3SMarek Lindner  * batadv_gw_node_add - add gateway node to list of available gateways
431414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
432414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
433414254e3SMarek Lindner  * @gateway: announced bandwidth information
434414254e3SMarek Lindner  */
43556303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv,
43656303d34SSven Eckelmann 			       struct batadv_orig_node *orig_node,
437414254e3SMarek Lindner 			       struct batadv_tvlv_gateway_data *gateway)
438c6c8fea2SSven Eckelmann {
43956303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
440414254e3SMarek Lindner 
441414254e3SMarek Lindner 	if (gateway->bandwidth_down == 0)
442414254e3SMarek Lindner 		return;
443c6c8fea2SSven Eckelmann 
444377fe0f9SAntonio Quartulli 	if (!atomic_inc_not_zero(&orig_node->refcount))
445c6c8fea2SSven Eckelmann 		return;
446c6c8fea2SSven Eckelmann 
447377fe0f9SAntonio Quartulli 	gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
448377fe0f9SAntonio Quartulli 	if (!gw_node) {
449377fe0f9SAntonio Quartulli 		batadv_orig_node_free_ref(orig_node);
450377fe0f9SAntonio Quartulli 		return;
451377fe0f9SAntonio Quartulli 	}
452377fe0f9SAntonio Quartulli 
453c6c8fea2SSven Eckelmann 	INIT_HLIST_NODE(&gw_node->list);
454c6c8fea2SSven Eckelmann 	gw_node->orig_node = orig_node;
45527a4d5efSSimon Wunderlich 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
45627a4d5efSSimon Wunderlich 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
457*e7aed321SSven Eckelmann 	kref_init(&gw_node->refcount);
458c6c8fea2SSven Eckelmann 
459807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
460807736f6SSven Eckelmann 	hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
461807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
462c6c8fea2SSven Eckelmann 
46339c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
464414254e3SMarek Lindner 		   "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
465414254e3SMarek Lindner 		   orig_node->orig,
466414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
467414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
468414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
469414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
470c6c8fea2SSven Eckelmann }
471c6c8fea2SSven Eckelmann 
472414254e3SMarek Lindner /**
473414254e3SMarek Lindner  * batadv_gw_node_get - retrieve gateway node from list of available gateways
474414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
475414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
476414254e3SMarek Lindner  *
47762fe710fSSven Eckelmann  * Return: gateway node if found or NULL otherwise.
47871e4aa9cSAntonio Quartulli  */
479414254e3SMarek Lindner static struct batadv_gw_node *
480414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv,
481414254e3SMarek Lindner 		   struct batadv_orig_node *orig_node)
482414254e3SMarek Lindner {
483414254e3SMarek Lindner 	struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
484c6c8fea2SSven Eckelmann 
485c6c8fea2SSven Eckelmann 	rcu_read_lock();
486414254e3SMarek Lindner 	hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
487414254e3SMarek Lindner 		if (gw_node_tmp->orig_node != orig_node)
488c6c8fea2SSven Eckelmann 			continue;
489c6c8fea2SSven Eckelmann 
490*e7aed321SSven Eckelmann 		if (!kref_get_unless_zero(&gw_node_tmp->refcount))
491414254e3SMarek Lindner 			continue;
492414254e3SMarek Lindner 
493414254e3SMarek Lindner 		gw_node = gw_node_tmp;
494414254e3SMarek Lindner 		break;
495414254e3SMarek Lindner 	}
496414254e3SMarek Lindner 	rcu_read_unlock();
497414254e3SMarek Lindner 
498414254e3SMarek Lindner 	return gw_node;
499414254e3SMarek Lindner }
500414254e3SMarek Lindner 
501414254e3SMarek Lindner /**
502414254e3SMarek Lindner  * batadv_gw_node_update - update list of available gateways with changed
503414254e3SMarek Lindner  *  bandwidth information
504414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
505414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
506414254e3SMarek Lindner  * @gateway: announced bandwidth information
507414254e3SMarek Lindner  */
508414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv,
509414254e3SMarek Lindner 			   struct batadv_orig_node *orig_node,
510414254e3SMarek Lindner 			   struct batadv_tvlv_gateway_data *gateway)
511414254e3SMarek Lindner {
512414254e3SMarek Lindner 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
513414254e3SMarek Lindner 
514414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_node);
515414254e3SMarek Lindner 	if (!gw_node) {
516414254e3SMarek Lindner 		batadv_gw_node_add(bat_priv, orig_node, gateway);
517414254e3SMarek Lindner 		goto out;
518414254e3SMarek Lindner 	}
519414254e3SMarek Lindner 
520414254e3SMarek Lindner 	if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
521414254e3SMarek Lindner 	    (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
522414254e3SMarek Lindner 		goto out;
523414254e3SMarek Lindner 
52439c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
525414254e3SMarek Lindner 		   "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
526414254e3SMarek Lindner 		   orig_node->orig,
527414254e3SMarek Lindner 		   gw_node->bandwidth_down / 10,
528414254e3SMarek Lindner 		   gw_node->bandwidth_down % 10,
529414254e3SMarek Lindner 		   gw_node->bandwidth_up / 10,
530414254e3SMarek Lindner 		   gw_node->bandwidth_up % 10,
531414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
532414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
533414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
534414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
535414254e3SMarek Lindner 
536414254e3SMarek Lindner 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
537414254e3SMarek Lindner 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
538c6c8fea2SSven Eckelmann 
539414254e3SMarek Lindner 	if (ntohl(gateway->bandwidth_down) == 0) {
54039c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
541c6c8fea2SSven Eckelmann 			   "Gateway %pM removed from gateway list\n",
542c6c8fea2SSven Eckelmann 			   orig_node->orig);
543c6c8fea2SSven Eckelmann 
544414254e3SMarek Lindner 		/* Note: We don't need a NULL check here, since curr_gw never
545414254e3SMarek Lindner 		 * gets dereferenced.
546414254e3SMarek Lindner 		 */
547bd3524c1SSimon Wunderlich 		spin_lock_bh(&bat_priv->gw.list_lock);
548bd3524c1SSimon Wunderlich 		hlist_del_init_rcu(&gw_node->list);
549bd3524c1SSimon Wunderlich 		spin_unlock_bh(&bat_priv->gw.list_lock);
550bd3524c1SSimon Wunderlich 
551bd3524c1SSimon Wunderlich 		batadv_gw_node_free_ref(gw_node);
552bd3524c1SSimon Wunderlich 
553414254e3SMarek Lindner 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
554c4aac1abSMarek Lindner 		if (gw_node == curr_gw)
5554e820e72SAntonio Quartulli 			batadv_gw_reselect(bat_priv);
556bd3524c1SSimon Wunderlich 
557bd3524c1SSimon Wunderlich 		if (curr_gw)
558bd3524c1SSimon Wunderlich 			batadv_gw_node_free_ref(curr_gw);
559414254e3SMarek Lindner 	}
56071e4aa9cSAntonio Quartulli 
561414254e3SMarek Lindner out:
562414254e3SMarek Lindner 	if (gw_node)
563414254e3SMarek Lindner 		batadv_gw_node_free_ref(gw_node);
564c6c8fea2SSven Eckelmann }
565c6c8fea2SSven Eckelmann 
56656303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv,
56756303d34SSven Eckelmann 			   struct batadv_orig_node *orig_node)
568c6c8fea2SSven Eckelmann {
569414254e3SMarek Lindner 	struct batadv_tvlv_gateway_data gateway;
570414254e3SMarek Lindner 
571414254e3SMarek Lindner 	gateway.bandwidth_down = 0;
572414254e3SMarek Lindner 	gateway.bandwidth_up = 0;
573414254e3SMarek Lindner 
574414254e3SMarek Lindner 	batadv_gw_node_update(bat_priv, orig_node, &gateway);
575c6c8fea2SSven Eckelmann }
576c6c8fea2SSven Eckelmann 
577bd3524c1SSimon Wunderlich void batadv_gw_node_free(struct batadv_priv *bat_priv)
578c6c8fea2SSven Eckelmann {
579bd3524c1SSimon Wunderlich 	struct batadv_gw_node *gw_node;
580b67bfe0dSSasha Levin 	struct hlist_node *node_tmp;
581c6c8fea2SSven Eckelmann 
582807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
583b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(gw_node, node_tmp,
584807736f6SSven Eckelmann 				  &bat_priv->gw.list, list) {
585bd3524c1SSimon Wunderlich 		hlist_del_init_rcu(&gw_node->list);
5861409a834SSven Eckelmann 		batadv_gw_node_free_ref(gw_node);
587c6c8fea2SSven Eckelmann 	}
588807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
589c6c8fea2SSven Eckelmann }
590c6c8fea2SSven Eckelmann 
5919cfc7bd6SSven Eckelmann /* fails if orig_node has no router */
59256303d34SSven Eckelmann static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
5931409a834SSven Eckelmann 				    struct seq_file *seq,
59456303d34SSven Eckelmann 				    const struct batadv_gw_node *gw_node)
595c6c8fea2SSven Eckelmann {
59656303d34SSven Eckelmann 	struct batadv_gw_node *curr_gw;
59756303d34SSven Eckelmann 	struct batadv_neigh_node *router;
59889652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
599414254e3SMarek Lindner 	int ret = -1;
600c6c8fea2SSven Eckelmann 
6017351a482SSimon Wunderlich 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
602e1a5382fSLinus Lüssing 	if (!router)
603e1a5382fSLinus Lüssing 		goto out;
604e1a5382fSLinus Lüssing 
60589652331SSimon Wunderlich 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
60689652331SSimon Wunderlich 	if (!router_ifinfo)
60789652331SSimon Wunderlich 		goto out;
60889652331SSimon Wunderlich 
6091409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
6105d02b3cdSLinus Lüssing 
6116d91147dSJoe Perches 	seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
6125d02b3cdSLinus Lüssing 		   (curr_gw == gw_node ? "=>" : "  "),
613c6c8fea2SSven Eckelmann 		   gw_node->orig_node->orig,
61489652331SSimon Wunderlich 		   router_ifinfo->bat_iv.tq_avg, router->addr,
615e1a5382fSLinus Lüssing 		   router->if_incoming->net_dev->name,
616414254e3SMarek Lindner 		   gw_node->bandwidth_down / 10,
617414254e3SMarek Lindner 		   gw_node->bandwidth_down % 10,
618414254e3SMarek Lindner 		   gw_node->bandwidth_up / 10,
619414254e3SMarek Lindner 		   gw_node->bandwidth_up % 10);
62092b83917SJoe Perches 	ret = seq_has_overflowed(seq) ? -1 : 0;
6215d02b3cdSLinus Lüssing 
622c4aac1abSMarek Lindner 	if (curr_gw)
6231409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
624e1a5382fSLinus Lüssing out:
62589652331SSimon Wunderlich 	if (router_ifinfo)
62689652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_ifinfo);
62789652331SSimon Wunderlich 	if (router)
62889652331SSimon Wunderlich 		batadv_neigh_node_free_ref(router);
6295d02b3cdSLinus Lüssing 	return ret;
630c6c8fea2SSven Eckelmann }
631c6c8fea2SSven Eckelmann 
6327cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
633c6c8fea2SSven Eckelmann {
634c6c8fea2SSven Eckelmann 	struct net_device *net_dev = (struct net_device *)seq->private;
63556303d34SSven Eckelmann 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
63656303d34SSven Eckelmann 	struct batadv_hard_iface *primary_if;
63756303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
63830da63a6SMarek Lindner 	int gw_count = 0;
639c6c8fea2SSven Eckelmann 
64030da63a6SMarek Lindner 	primary_if = batadv_seq_print_text_primary_if_get(seq);
64130da63a6SMarek Lindner 	if (!primary_if)
64232ae9b22SMarek Lindner 		goto out;
643c6c8fea2SSven Eckelmann 
64486ceb360SSven Eckelmann 	seq_printf(seq,
645414254e3SMarek Lindner 		   "      %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
64642d0b044SSven Eckelmann 		   "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
64742d0b044SSven Eckelmann 		   BATADV_SOURCE_VERSION, primary_if->net_dev->name,
64832ae9b22SMarek Lindner 		   primary_if->net_dev->dev_addr, net_dev->name);
649c6c8fea2SSven Eckelmann 
650c6c8fea2SSven Eckelmann 	rcu_read_lock();
651b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
652e1a5382fSLinus Lüssing 		/* fails if orig_node has no router */
6531409a834SSven Eckelmann 		if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
654c6c8fea2SSven Eckelmann 			continue;
655c6c8fea2SSven Eckelmann 
656c6c8fea2SSven Eckelmann 		gw_count++;
657c6c8fea2SSven Eckelmann 	}
658c6c8fea2SSven Eckelmann 	rcu_read_unlock();
659c6c8fea2SSven Eckelmann 
660c6c8fea2SSven Eckelmann 	if (gw_count == 0)
6610c814653SAntonio Quartulli 		seq_puts(seq, "No gateways in range ...\n");
662c6c8fea2SSven Eckelmann 
66332ae9b22SMarek Lindner out:
66432ae9b22SMarek Lindner 	if (primary_if)
665e5d89254SSven Eckelmann 		batadv_hardif_free_ref(primary_if);
66630da63a6SMarek Lindner 	return 0;
667c6c8fea2SSven Eckelmann }
668c6c8fea2SSven Eckelmann 
6696c413b1cSAntonio Quartulli /**
6706c413b1cSAntonio Quartulli  * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
6716c413b1cSAntonio Quartulli  * @skb: the packet to check
6726c413b1cSAntonio Quartulli  * @header_len: a pointer to the batman-adv header size
6736c413b1cSAntonio Quartulli  * @chaddr: buffer where the client address will be stored. Valid
6746c413b1cSAntonio Quartulli  *  only if the function returns BATADV_DHCP_TO_CLIENT
6756c413b1cSAntonio Quartulli  *
67662fe710fSSven Eckelmann  * This function may re-allocate the data buffer of the skb passed as argument.
67762fe710fSSven Eckelmann  *
67862fe710fSSven Eckelmann  * Return:
6796c413b1cSAntonio Quartulli  * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
6806c413b1cSAntonio Quartulli  *   while parsing it
6816c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
6826c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
6839cfc7bd6SSven Eckelmann  */
6846c413b1cSAntonio Quartulli enum batadv_dhcp_recipient
6856c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
6866b5e971aSSven Eckelmann 			     u8 *chaddr)
687c6c8fea2SSven Eckelmann {
6886c413b1cSAntonio Quartulli 	enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
689c6c8fea2SSven Eckelmann 	struct ethhdr *ethhdr;
690c6c8fea2SSven Eckelmann 	struct iphdr *iphdr;
691c6c8fea2SSven Eckelmann 	struct ipv6hdr *ipv6hdr;
692c6c8fea2SSven Eckelmann 	struct udphdr *udphdr;
693f7f8ed56SAntonio Quartulli 	struct vlan_ethhdr *vhdr;
6946c413b1cSAntonio Quartulli 	int chaddr_offset;
695f7f8ed56SAntonio Quartulli 	__be16 proto;
6966b5e971aSSven Eckelmann 	u8 *p;
697c6c8fea2SSven Eckelmann 
698c6c8fea2SSven Eckelmann 	/* check for ethernet header */
699be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
7006c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
7016c413b1cSAntonio Quartulli 
702927c2ed7SLinus Lüssing 	ethhdr = eth_hdr(skb);
703f7f8ed56SAntonio Quartulli 	proto = ethhdr->h_proto;
704be7af5cfSMarek Lindner 	*header_len += ETH_HLEN;
705c6c8fea2SSven Eckelmann 
706c6c8fea2SSven Eckelmann 	/* check for initial vlan header */
707f7f8ed56SAntonio Quartulli 	if (proto == htons(ETH_P_8021Q)) {
708be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
7096c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
710f7f8ed56SAntonio Quartulli 
711927c2ed7SLinus Lüssing 		vhdr = vlan_eth_hdr(skb);
712f7f8ed56SAntonio Quartulli 		proto = vhdr->h_vlan_encapsulated_proto;
713be7af5cfSMarek Lindner 		*header_len += VLAN_HLEN;
714c6c8fea2SSven Eckelmann 	}
715c6c8fea2SSven Eckelmann 
716c6c8fea2SSven Eckelmann 	/* check for ip header */
717f7f8ed56SAntonio Quartulli 	switch (proto) {
718f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IP):
719be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
7206c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7216c413b1cSAntonio Quartulli 
722be7af5cfSMarek Lindner 		iphdr = (struct iphdr *)(skb->data + *header_len);
723be7af5cfSMarek Lindner 		*header_len += iphdr->ihl * 4;
724c6c8fea2SSven Eckelmann 
725c6c8fea2SSven Eckelmann 		/* check for udp header */
726c6c8fea2SSven Eckelmann 		if (iphdr->protocol != IPPROTO_UDP)
7276c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
728c6c8fea2SSven Eckelmann 
729c6c8fea2SSven Eckelmann 		break;
730f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IPV6):
731be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
7326c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7336c413b1cSAntonio Quartulli 
734be7af5cfSMarek Lindner 		ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
735be7af5cfSMarek Lindner 		*header_len += sizeof(*ipv6hdr);
736c6c8fea2SSven Eckelmann 
737c6c8fea2SSven Eckelmann 		/* check for udp header */
738c6c8fea2SSven Eckelmann 		if (ipv6hdr->nexthdr != IPPROTO_UDP)
7396c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
740c6c8fea2SSven Eckelmann 
741c6c8fea2SSven Eckelmann 		break;
742c6c8fea2SSven Eckelmann 	default:
7436c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
744c6c8fea2SSven Eckelmann 	}
745c6c8fea2SSven Eckelmann 
746be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
7476c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
7489d2c9488SLinus Lüssing 
749be7af5cfSMarek Lindner 	udphdr = (struct udphdr *)(skb->data + *header_len);
750be7af5cfSMarek Lindner 	*header_len += sizeof(*udphdr);
751c6c8fea2SSven Eckelmann 
752c6c8fea2SSven Eckelmann 	/* check for bootp port */
7536c413b1cSAntonio Quartulli 	switch (proto) {
7546c413b1cSAntonio Quartulli 	case htons(ETH_P_IP):
7556c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(67))
7566c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
7576c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(67))
7586c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
7596c413b1cSAntonio Quartulli 		break;
7606c413b1cSAntonio Quartulli 	case htons(ETH_P_IPV6):
7616c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(547))
7626c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
7636c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(547))
7646c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
7656c413b1cSAntonio Quartulli 		break;
766be7af5cfSMarek Lindner 	}
767c6c8fea2SSven Eckelmann 
7686c413b1cSAntonio Quartulli 	chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
7696c413b1cSAntonio Quartulli 	/* store the client address if the message is going to a client */
7706c413b1cSAntonio Quartulli 	if (ret == BATADV_DHCP_TO_CLIENT &&
7716c413b1cSAntonio Quartulli 	    pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
7726c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries an Ethernet DHCP */
7736c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
7746c413b1cSAntonio Quartulli 		if (*p != BATADV_DHCP_HTYPE_ETHERNET)
7756c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7766c413b1cSAntonio Quartulli 
7776c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries a valid Ethernet address */
7786c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
7796c413b1cSAntonio Quartulli 		if (*p != ETH_ALEN)
7806c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7816c413b1cSAntonio Quartulli 
7828fdd0153SAntonio Quartulli 		ether_addr_copy(chaddr, skb->data + chaddr_offset);
7836c413b1cSAntonio Quartulli 	}
7846c413b1cSAntonio Quartulli 
7856c413b1cSAntonio Quartulli 	return ret;
7866c413b1cSAntonio Quartulli }
787aa143d28SAntonio Quartulli 
788bbb877edSAntonio Quartulli /**
789bbb877edSAntonio Quartulli  * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
790bbb877edSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
791bbb877edSAntonio Quartulli  * @skb: the outgoing packet
792bbb877edSAntonio Quartulli  *
793bbb877edSAntonio Quartulli  * Check if the skb is a DHCP request and if it is sent to the current best GW
794bbb877edSAntonio Quartulli  * server. Due to topology changes it may be the case that the GW server
795bbb877edSAntonio Quartulli  * previously selected is not the best one anymore.
796bbb877edSAntonio Quartulli  *
797bbb877edSAntonio Quartulli  * This call might reallocate skb data.
7986c413b1cSAntonio Quartulli  * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
79962fe710fSSven Eckelmann  *
80062fe710fSSven Eckelmann  * Return: true if the packet destination is unicast and it is not the best gw,
80162fe710fSSven Eckelmann  * false otherwise.
802bbb877edSAntonio Quartulli  */
80356303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
8049d2c9488SLinus Lüssing 			    struct sk_buff *skb)
805be7af5cfSMarek Lindner {
8064f248cffSSven Eckelmann 	struct batadv_neigh_node *neigh_curr = NULL;
8074f248cffSSven Eckelmann 	struct batadv_neigh_node *neigh_old = NULL;
80856303d34SSven Eckelmann 	struct batadv_orig_node *orig_dst_node = NULL;
8094f248cffSSven Eckelmann 	struct batadv_gw_node *gw_node = NULL;
8104f248cffSSven Eckelmann 	struct batadv_gw_node *curr_gw = NULL;
81189652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
8126c413b1cSAntonio Quartulli 	struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
8136c413b1cSAntonio Quartulli 	bool out_of_range = false;
8146b5e971aSSven Eckelmann 	u8 curr_tq_avg;
815bbb877edSAntonio Quartulli 	unsigned short vid;
816bbb877edSAntonio Quartulli 
817bbb877edSAntonio Quartulli 	vid = batadv_get_vid(skb, 0);
818be7af5cfSMarek Lindner 
81908c36d3eSSven Eckelmann 	orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
820bbb877edSAntonio Quartulli 						 ethhdr->h_dest, vid);
821be7af5cfSMarek Lindner 	if (!orig_dst_node)
822be7af5cfSMarek Lindner 		goto out;
823be7af5cfSMarek Lindner 
824414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
8250d164491SAntonio Quartulli 	if (!gw_node)
826be7af5cfSMarek Lindner 		goto out;
827be7af5cfSMarek Lindner 
828be7af5cfSMarek Lindner 	switch (atomic_read(&bat_priv->gw_mode)) {
829cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_SERVER:
830be7af5cfSMarek Lindner 		/* If we are a GW then we are our best GW. We can artificially
8319cfc7bd6SSven Eckelmann 		 * set the tq towards ourself as the maximum value
8329cfc7bd6SSven Eckelmann 		 */
83342d0b044SSven Eckelmann 		curr_tq_avg = BATADV_TQ_MAX_VALUE;
834be7af5cfSMarek Lindner 		break;
835cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_CLIENT:
8361409a834SSven Eckelmann 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
837c4aac1abSMarek Lindner 		if (!curr_gw)
838be7af5cfSMarek Lindner 			goto out;
839c6c8fea2SSven Eckelmann 
840be7af5cfSMarek Lindner 		/* packet is going to our gateway */
841be7af5cfSMarek Lindner 		if (curr_gw->orig_node == orig_dst_node)
842be7af5cfSMarek Lindner 			goto out;
843be7af5cfSMarek Lindner 
84443676ab5SAntonio Quartulli 		/* If the dhcp packet has been sent to a different gw,
84543676ab5SAntonio Quartulli 		 * we have to evaluate whether the old gw is still
8469cfc7bd6SSven Eckelmann 		 * reliable enough
8479cfc7bd6SSven Eckelmann 		 */
84830d3c511SSven Eckelmann 		neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
84930d3c511SSven Eckelmann 						NULL);
850be7af5cfSMarek Lindner 		if (!neigh_curr)
851be7af5cfSMarek Lindner 			goto out;
852be7af5cfSMarek Lindner 
85389652331SSimon Wunderlich 		curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
85489652331SSimon Wunderlich 						      BATADV_IF_DEFAULT);
85589652331SSimon Wunderlich 		if (!curr_ifinfo)
85689652331SSimon Wunderlich 			goto out;
85789652331SSimon Wunderlich 
85889652331SSimon Wunderlich 		curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
85989652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(curr_ifinfo);
86089652331SSimon Wunderlich 
861be7af5cfSMarek Lindner 		break;
862cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_OFF:
863be7af5cfSMarek Lindner 	default:
864be7af5cfSMarek Lindner 		goto out;
86543676ab5SAntonio Quartulli 	}
866be7af5cfSMarek Lindner 
86730d3c511SSven Eckelmann 	neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
8682ef04f47SDan Carpenter 	if (!neigh_old)
869be7af5cfSMarek Lindner 		goto out;
870be7af5cfSMarek Lindner 
87189652331SSimon Wunderlich 	old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
87289652331SSimon Wunderlich 	if (!old_ifinfo)
87389652331SSimon Wunderlich 		goto out;
87489652331SSimon Wunderlich 
87589652331SSimon Wunderlich 	if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
876be7af5cfSMarek Lindner 		out_of_range = true;
87789652331SSimon Wunderlich 	batadv_neigh_ifinfo_free_ref(old_ifinfo);
878be7af5cfSMarek Lindner 
879be7af5cfSMarek Lindner out:
880be7af5cfSMarek Lindner 	if (orig_dst_node)
8817d211efcSSven Eckelmann 		batadv_orig_node_free_ref(orig_dst_node);
882be7af5cfSMarek Lindner 	if (curr_gw)
8831409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
884414254e3SMarek Lindner 	if (gw_node)
885414254e3SMarek Lindner 		batadv_gw_node_free_ref(gw_node);
88643676ab5SAntonio Quartulli 	if (neigh_old)
8877d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(neigh_old);
88843676ab5SAntonio Quartulli 	if (neigh_curr)
8897d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(neigh_curr);
890be7af5cfSMarek Lindner 	return out_of_range;
891c6c8fea2SSven Eckelmann }
892