xref: /openbmc/linux/net/batman-adv/gateway_client.c (revision 7351a4822d42827ba0110677c0cbad88a3d52585)
10b873931SAntonio Quartulli /* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors:
2c6c8fea2SSven Eckelmann  *
3c6c8fea2SSven Eckelmann  * Marek Lindner
4c6c8fea2SSven Eckelmann  *
5c6c8fea2SSven Eckelmann  * This program is free software; you can redistribute it and/or
6c6c8fea2SSven Eckelmann  * modify it under the terms of version 2 of the GNU General Public
7c6c8fea2SSven Eckelmann  * License as published by the Free Software Foundation.
8c6c8fea2SSven Eckelmann  *
9c6c8fea2SSven Eckelmann  * This program is distributed in the hope that it will be useful, but
10c6c8fea2SSven Eckelmann  * WITHOUT ANY WARRANTY; without even the implied warranty of
11c6c8fea2SSven Eckelmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12c6c8fea2SSven Eckelmann  * General Public License for more details.
13c6c8fea2SSven Eckelmann  *
14c6c8fea2SSven Eckelmann  * You should have received a copy of the GNU General Public License
15ebf38fb7SAntonio Quartulli  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16c6c8fea2SSven Eckelmann  */
17c6c8fea2SSven Eckelmann 
18c6c8fea2SSven Eckelmann #include "main.h"
19b706b13bSSven Eckelmann #include "sysfs.h"
20c6c8fea2SSven Eckelmann #include "gateway_client.h"
21c6c8fea2SSven Eckelmann #include "gateway_common.h"
22c6c8fea2SSven Eckelmann #include "hard-interface.h"
2357f0c07cSLinus Lüssing #include "originator.h"
24be7af5cfSMarek Lindner #include "translation-table.h"
2543676ab5SAntonio Quartulli #include "routing.h"
26c6c8fea2SSven Eckelmann #include <linux/ip.h>
27c6c8fea2SSven Eckelmann #include <linux/ipv6.h>
28c6c8fea2SSven Eckelmann #include <linux/udp.h>
29c6c8fea2SSven Eckelmann #include <linux/if_vlan.h>
30c6c8fea2SSven Eckelmann 
316c413b1cSAntonio Quartulli /* These are the offsets of the "hw type" and "hw address length" in the dhcp
326c413b1cSAntonio Quartulli  * packet starting at the beginning of the dhcp header
339cfc7bd6SSven Eckelmann  */
346c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_OFFSET	1
356c413b1cSAntonio Quartulli #define BATADV_DHCP_HLEN_OFFSET		2
366c413b1cSAntonio Quartulli /* Value of htype representing Ethernet */
376c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_ETHERNET	0x01
386c413b1cSAntonio Quartulli /* This is the offset of the "chaddr" field in the dhcp packet starting at the
396c413b1cSAntonio Quartulli  * beginning of the dhcp header
406c413b1cSAntonio Quartulli  */
416c413b1cSAntonio Quartulli #define BATADV_DHCP_CHADDR_OFFSET	28
4243676ab5SAntonio Quartulli 
4356303d34SSven Eckelmann static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node)
4425b6d3c1SMarek Lindner {
4525b6d3c1SMarek Lindner 	if (atomic_dec_and_test(&gw_node->refcount))
46eb340b2fSPaul E. McKenney 		kfree_rcu(gw_node, rcu);
47c6c8fea2SSven Eckelmann }
48c6c8fea2SSven Eckelmann 
4956303d34SSven Eckelmann static struct batadv_gw_node *
5056303d34SSven Eckelmann batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
51c6c8fea2SSven Eckelmann {
5256303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
53c6c8fea2SSven Eckelmann 
545d02b3cdSLinus Lüssing 	rcu_read_lock();
55807736f6SSven Eckelmann 	gw_node = rcu_dereference(bat_priv->gw.curr_gw);
56c4aac1abSMarek Lindner 	if (!gw_node)
575d02b3cdSLinus Lüssing 		goto out;
58c6c8fea2SSven Eckelmann 
59c4aac1abSMarek Lindner 	if (!atomic_inc_not_zero(&gw_node->refcount))
60c4aac1abSMarek Lindner 		gw_node = NULL;
61c4aac1abSMarek Lindner 
62c4aac1abSMarek Lindner out:
63c4aac1abSMarek Lindner 	rcu_read_unlock();
64c4aac1abSMarek Lindner 	return gw_node;
65c4aac1abSMarek Lindner }
66c4aac1abSMarek Lindner 
6756303d34SSven Eckelmann struct batadv_orig_node *
6856303d34SSven Eckelmann batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
69c4aac1abSMarek Lindner {
7056303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
7156303d34SSven Eckelmann 	struct batadv_orig_node *orig_node = NULL;
72c4aac1abSMarek Lindner 
731409a834SSven Eckelmann 	gw_node = batadv_gw_get_selected_gw_node(bat_priv);
74c4aac1abSMarek Lindner 	if (!gw_node)
757b36e8eeSMarek Lindner 		goto out;
765d02b3cdSLinus Lüssing 
77c4aac1abSMarek Lindner 	rcu_read_lock();
78c4aac1abSMarek Lindner 	orig_node = gw_node->orig_node;
79c4aac1abSMarek Lindner 	if (!orig_node)
80c4aac1abSMarek Lindner 		goto unlock;
81c4aac1abSMarek Lindner 
827b36e8eeSMarek Lindner 	if (!atomic_inc_not_zero(&orig_node->refcount))
837b36e8eeSMarek Lindner 		orig_node = NULL;
8443c70ad5SLinus Lüssing 
85c4aac1abSMarek Lindner unlock:
865d02b3cdSLinus Lüssing 	rcu_read_unlock();
87c4aac1abSMarek Lindner out:
88c6c8fea2SSven Eckelmann 	if (gw_node)
891409a834SSven Eckelmann 		batadv_gw_node_free_ref(gw_node);
90c4aac1abSMarek Lindner 	return orig_node;
91c6c8fea2SSven Eckelmann }
92c6c8fea2SSven Eckelmann 
9356303d34SSven Eckelmann static void batadv_gw_select(struct batadv_priv *bat_priv,
9456303d34SSven Eckelmann 			     struct batadv_gw_node *new_gw_node)
95c6c8fea2SSven Eckelmann {
9656303d34SSven Eckelmann 	struct batadv_gw_node *curr_gw_node;
97c6c8fea2SSven Eckelmann 
98807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
99c4aac1abSMarek Lindner 
10025b6d3c1SMarek Lindner 	if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
10125b6d3c1SMarek Lindner 		new_gw_node = NULL;
102c6c8fea2SSven Eckelmann 
103807736f6SSven Eckelmann 	curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
104807736f6SSven Eckelmann 	rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
10525b6d3c1SMarek Lindner 
10625b6d3c1SMarek Lindner 	if (curr_gw_node)
1071409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw_node);
108c4aac1abSMarek Lindner 
109807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
110c4aac1abSMarek Lindner }
111c4aac1abSMarek Lindner 
1124e820e72SAntonio Quartulli /**
1134e820e72SAntonio Quartulli  * batadv_gw_reselect - force a gateway reselection
1144e820e72SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
1154e820e72SAntonio Quartulli  *
1164e820e72SAntonio Quartulli  * Set a flag to remind the GW component to perform a new gateway reselection.
1174e820e72SAntonio Quartulli  * However this function does not ensure that the current gateway is going to be
1184e820e72SAntonio Quartulli  * deselected. The reselection mechanism may elect the same gateway once again.
1194e820e72SAntonio Quartulli  *
1204e820e72SAntonio Quartulli  * This means that invoking batadv_gw_reselect() does not guarantee a gateway
1214e820e72SAntonio Quartulli  * change and therefore a uevent is not necessarily expected.
1224e820e72SAntonio Quartulli  */
1234e820e72SAntonio Quartulli void batadv_gw_reselect(struct batadv_priv *bat_priv)
124c4aac1abSMarek Lindner {
125807736f6SSven Eckelmann 	atomic_set(&bat_priv->gw.reselect, 1);
126c6c8fea2SSven Eckelmann }
127c6c8fea2SSven Eckelmann 
12856303d34SSven Eckelmann static struct batadv_gw_node *
12956303d34SSven Eckelmann batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
130c6c8fea2SSven Eckelmann {
13156303d34SSven Eckelmann 	struct batadv_neigh_node *router;
13289652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo;
13356303d34SSven Eckelmann 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
134c6c8fea2SSven Eckelmann 	uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
135c67893d1SSven Eckelmann 	uint32_t gw_divisor;
1362265c141SAntonio Quartulli 	uint8_t max_tq = 0;
137c67893d1SSven Eckelmann 	uint8_t tq_avg;
13856303d34SSven Eckelmann 	struct batadv_orig_node *orig_node;
139c6c8fea2SSven Eckelmann 
140c67893d1SSven Eckelmann 	gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE;
141c67893d1SSven Eckelmann 	gw_divisor *= 64;
142c67893d1SSven Eckelmann 
143c6c8fea2SSven Eckelmann 	rcu_read_lock();
144b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
145e1a5382fSLinus Lüssing 		if (gw_node->deleted)
146c6c8fea2SSven Eckelmann 			continue;
147c6c8fea2SSven Eckelmann 
14884d5e5e0SSven Eckelmann 		orig_node = gw_node->orig_node;
149*7351a482SSimon Wunderlich 		router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
150e1a5382fSLinus Lüssing 		if (!router)
151c6c8fea2SSven Eckelmann 			continue;
152c6c8fea2SSven Eckelmann 
15389652331SSimon Wunderlich 		router_ifinfo = batadv_neigh_ifinfo_get(router,
15489652331SSimon Wunderlich 							BATADV_IF_DEFAULT);
15589652331SSimon Wunderlich 		if (!router_ifinfo)
15689652331SSimon Wunderlich 			goto next;
15789652331SSimon Wunderlich 
1582265c141SAntonio Quartulli 		if (!atomic_inc_not_zero(&gw_node->refcount))
1592265c141SAntonio Quartulli 			goto next;
1602265c141SAntonio Quartulli 
16189652331SSimon Wunderlich 		tq_avg = router_ifinfo->bat_iv.tq_avg;
162c67893d1SSven Eckelmann 
163c6c8fea2SSven Eckelmann 		switch (atomic_read(&bat_priv->gw_sel_class)) {
164c6c8fea2SSven Eckelmann 		case 1: /* fast connection */
165414254e3SMarek Lindner 			tmp_gw_factor = tq_avg * tq_avg;
166414254e3SMarek Lindner 			tmp_gw_factor *= gw_node->bandwidth_down;
167414254e3SMarek Lindner 			tmp_gw_factor *= 100 * 100;
168c67893d1SSven Eckelmann 			tmp_gw_factor /= gw_divisor;
169c6c8fea2SSven Eckelmann 
170c6c8fea2SSven Eckelmann 			if ((tmp_gw_factor > max_gw_factor) ||
171c6c8fea2SSven Eckelmann 			    ((tmp_gw_factor == max_gw_factor) &&
172c67893d1SSven Eckelmann 			     (tq_avg > max_tq))) {
1732265c141SAntonio Quartulli 				if (curr_gw)
1741409a834SSven Eckelmann 					batadv_gw_node_free_ref(curr_gw);
1752265c141SAntonio Quartulli 				curr_gw = gw_node;
1762265c141SAntonio Quartulli 				atomic_inc(&curr_gw->refcount);
1772265c141SAntonio Quartulli 			}
178c6c8fea2SSven Eckelmann 			break;
179c6c8fea2SSven Eckelmann 
1809cfc7bd6SSven Eckelmann 		default: /* 2:  stable connection (use best statistic)
181c6c8fea2SSven Eckelmann 			  * 3:  fast-switch (use best statistic but change as
182c6c8fea2SSven Eckelmann 			  *     soon as a better gateway appears)
183c6c8fea2SSven Eckelmann 			  * XX: late-switch (use best statistic but change as
184c6c8fea2SSven Eckelmann 			  *     soon as a better gateway appears which has
185c6c8fea2SSven Eckelmann 			  *     $routing_class more tq points)
1869cfc7bd6SSven Eckelmann 			  */
187c67893d1SSven Eckelmann 			if (tq_avg > max_tq) {
1882265c141SAntonio Quartulli 				if (curr_gw)
1891409a834SSven Eckelmann 					batadv_gw_node_free_ref(curr_gw);
1902265c141SAntonio Quartulli 				curr_gw = gw_node;
1912265c141SAntonio Quartulli 				atomic_inc(&curr_gw->refcount);
1922265c141SAntonio Quartulli 			}
193c6c8fea2SSven Eckelmann 			break;
194c6c8fea2SSven Eckelmann 		}
195c6c8fea2SSven Eckelmann 
196c67893d1SSven Eckelmann 		if (tq_avg > max_tq)
197c67893d1SSven Eckelmann 			max_tq = tq_avg;
198c6c8fea2SSven Eckelmann 
199c6c8fea2SSven Eckelmann 		if (tmp_gw_factor > max_gw_factor)
200c6c8fea2SSven Eckelmann 			max_gw_factor = tmp_gw_factor;
201e1a5382fSLinus Lüssing 
2021409a834SSven Eckelmann 		batadv_gw_node_free_ref(gw_node);
2032265c141SAntonio Quartulli 
2042265c141SAntonio Quartulli next:
2057d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router);
20689652331SSimon Wunderlich 		if (router_ifinfo)
20789652331SSimon Wunderlich 			batadv_neigh_ifinfo_free_ref(router_ifinfo);
208c6c8fea2SSven Eckelmann 	}
2092265c141SAntonio Quartulli 	rcu_read_unlock();
210c6c8fea2SSven Eckelmann 
2112265c141SAntonio Quartulli 	return curr_gw;
2122265c141SAntonio Quartulli }
213e1a5382fSLinus Lüssing 
214c6eaa3f0SAntonio Quartulli /**
215c6eaa3f0SAntonio Quartulli  * batadv_gw_check_client_stop - check if client mode has been switched off
216c6eaa3f0SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
217c6eaa3f0SAntonio Quartulli  *
218c6eaa3f0SAntonio Quartulli  * This function assumes the caller has checked that the gw state *is actually
219c6eaa3f0SAntonio Quartulli  * changing*. This function is not supposed to be called when there is no state
220c6eaa3f0SAntonio Quartulli  * change.
221c6eaa3f0SAntonio Quartulli  */
222c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
223c6eaa3f0SAntonio Quartulli {
224c6eaa3f0SAntonio Quartulli 	struct batadv_gw_node *curr_gw;
225c6eaa3f0SAntonio Quartulli 
226c6eaa3f0SAntonio Quartulli 	if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
227c6eaa3f0SAntonio Quartulli 		return;
228c6eaa3f0SAntonio Quartulli 
229c6eaa3f0SAntonio Quartulli 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
230c6eaa3f0SAntonio Quartulli 	if (!curr_gw)
231c6eaa3f0SAntonio Quartulli 		return;
232c6eaa3f0SAntonio Quartulli 
233f3163181SAntonio Quartulli 	/* deselect the current gateway so that next time that client mode is
234f3163181SAntonio Quartulli 	 * enabled a proper GW_ADD event can be sent
235f3163181SAntonio Quartulli 	 */
236f3163181SAntonio Quartulli 	batadv_gw_select(bat_priv, NULL);
237f3163181SAntonio Quartulli 
238c6eaa3f0SAntonio Quartulli 	/* if batman-adv is switching the gw client mode off and a gateway was
239c6eaa3f0SAntonio Quartulli 	 * already selected, send a DEL uevent
240c6eaa3f0SAntonio Quartulli 	 */
241c6eaa3f0SAntonio Quartulli 	batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
242c6eaa3f0SAntonio Quartulli 
243c6eaa3f0SAntonio Quartulli 	batadv_gw_node_free_ref(curr_gw);
244c6eaa3f0SAntonio Quartulli }
245c6eaa3f0SAntonio Quartulli 
24656303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv)
2472265c141SAntonio Quartulli {
24856303d34SSven Eckelmann 	struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL;
24956303d34SSven Eckelmann 	struct batadv_neigh_node *router = NULL;
25089652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
25119595e05SAntonio Quartulli 	char gw_addr[18] = { '\0' };
2522265c141SAntonio Quartulli 
253cd646ab1SSven Eckelmann 	if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
2542265c141SAntonio Quartulli 		goto out;
2552265c141SAntonio Quartulli 
2561409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2572265c141SAntonio Quartulli 
258807736f6SSven Eckelmann 	if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
259caa0bf64SMarek Lindner 		goto out;
260caa0bf64SMarek Lindner 
2611409a834SSven Eckelmann 	next_gw = batadv_gw_get_best_gw_node(bat_priv);
2622265c141SAntonio Quartulli 
2632265c141SAntonio Quartulli 	if (curr_gw == next_gw)
2642265c141SAntonio Quartulli 		goto out;
2652265c141SAntonio Quartulli 
2662265c141SAntonio Quartulli 	if (next_gw) {
26719595e05SAntonio Quartulli 		sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
26819595e05SAntonio Quartulli 
269*7351a482SSimon Wunderlich 		router = batadv_orig_router_get(next_gw->orig_node,
270*7351a482SSimon Wunderlich 						BATADV_IF_DEFAULT);
2712265c141SAntonio Quartulli 		if (!router) {
2724e820e72SAntonio Quartulli 			batadv_gw_reselect(bat_priv);
2732265c141SAntonio Quartulli 			goto out;
2742265c141SAntonio Quartulli 		}
27589652331SSimon Wunderlich 
27689652331SSimon Wunderlich 		router_ifinfo = batadv_neigh_ifinfo_get(router,
27789652331SSimon Wunderlich 							BATADV_IF_DEFAULT);
27889652331SSimon Wunderlich 		if (!router_ifinfo) {
27989652331SSimon Wunderlich 			batadv_gw_reselect(bat_priv);
28089652331SSimon Wunderlich 			goto out;
28189652331SSimon Wunderlich 		}
2822265c141SAntonio Quartulli 	}
2832265c141SAntonio Quartulli 
2842265c141SAntonio Quartulli 	if ((curr_gw) && (!next_gw)) {
28539c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2862265c141SAntonio Quartulli 			   "Removing selected gateway - no gateway in range\n");
28739c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
28839c75a51SSven Eckelmann 				    NULL);
2892265c141SAntonio Quartulli 	} else if ((!curr_gw) && (next_gw)) {
29039c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
291414254e3SMarek Lindner 			   "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
2921eda58bfSSven Eckelmann 			   next_gw->orig_node->orig,
293414254e3SMarek Lindner 			   next_gw->bandwidth_down / 10,
294414254e3SMarek Lindner 			   next_gw->bandwidth_down % 10,
295414254e3SMarek Lindner 			   next_gw->bandwidth_up / 10,
29689652331SSimon Wunderlich 			   next_gw->bandwidth_up % 10,
29789652331SSimon Wunderlich 			   router_ifinfo->bat_iv.tq_avg);
29839c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
29939c75a51SSven Eckelmann 				    gw_addr);
3002265c141SAntonio Quartulli 	} else {
30139c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
302414254e3SMarek Lindner 			   "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
3031eda58bfSSven Eckelmann 			   next_gw->orig_node->orig,
304414254e3SMarek Lindner 			   next_gw->bandwidth_down / 10,
305414254e3SMarek Lindner 			   next_gw->bandwidth_down % 10,
306414254e3SMarek Lindner 			   next_gw->bandwidth_up / 10,
30789652331SSimon Wunderlich 			   next_gw->bandwidth_up % 10,
30889652331SSimon Wunderlich 			   router_ifinfo->bat_iv.tq_avg);
30939c75a51SSven Eckelmann 		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
31039c75a51SSven Eckelmann 				    gw_addr);
311c6c8fea2SSven Eckelmann 	}
312c6c8fea2SSven Eckelmann 
3131409a834SSven Eckelmann 	batadv_gw_select(bat_priv, next_gw);
3142265c141SAntonio Quartulli 
315c4aac1abSMarek Lindner out:
316c4aac1abSMarek Lindner 	if (curr_gw)
3171409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
3182265c141SAntonio Quartulli 	if (next_gw)
3191409a834SSven Eckelmann 		batadv_gw_node_free_ref(next_gw);
3202265c141SAntonio Quartulli 	if (router)
3217d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router);
32289652331SSimon Wunderlich 	if (router_ifinfo)
32389652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_ifinfo);
324c6c8fea2SSven Eckelmann }
325c6c8fea2SSven Eckelmann 
32656303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv,
32756303d34SSven Eckelmann 			      struct batadv_orig_node *orig_node)
328c6c8fea2SSven Eckelmann {
32989652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_orig_tq = NULL;
33089652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_gw_tq = NULL;
33156303d34SSven Eckelmann 	struct batadv_orig_node *curr_gw_orig;
33256303d34SSven Eckelmann 	struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL;
333c6c8fea2SSven Eckelmann 	uint8_t gw_tq_avg, orig_tq_avg;
334c6c8fea2SSven Eckelmann 
3357cf06bc6SSven Eckelmann 	curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
33657f0c07cSLinus Lüssing 	if (!curr_gw_orig)
3374e820e72SAntonio Quartulli 		goto reselect;
338c6c8fea2SSven Eckelmann 
339*7351a482SSimon Wunderlich 	router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
340e1a5382fSLinus Lüssing 	if (!router_gw)
3414e820e72SAntonio Quartulli 		goto reselect;
342c6c8fea2SSven Eckelmann 
34389652331SSimon Wunderlich 	router_gw_tq = batadv_neigh_ifinfo_get(router_gw,
34489652331SSimon Wunderlich 					       BATADV_IF_DEFAULT);
34589652331SSimon Wunderlich 	if (!router_gw_tq)
34689652331SSimon Wunderlich 		goto reselect;
34789652331SSimon Wunderlich 
348c6c8fea2SSven Eckelmann 	/* this node already is the gateway */
34957f0c07cSLinus Lüssing 	if (curr_gw_orig == orig_node)
350e1a5382fSLinus Lüssing 		goto out;
351c6c8fea2SSven Eckelmann 
352*7351a482SSimon Wunderlich 	router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
353e1a5382fSLinus Lüssing 	if (!router_orig)
354e1a5382fSLinus Lüssing 		goto out;
355c6c8fea2SSven Eckelmann 
35689652331SSimon Wunderlich 	router_orig_tq = batadv_neigh_ifinfo_get(router_orig,
35789652331SSimon Wunderlich 						 BATADV_IF_DEFAULT);
35889652331SSimon Wunderlich 	if (!router_orig_tq)
35989652331SSimon Wunderlich 		goto out;
36089652331SSimon Wunderlich 
36189652331SSimon Wunderlich 	gw_tq_avg = router_gw_tq->bat_iv.tq_avg;
36289652331SSimon Wunderlich 	orig_tq_avg = router_orig_tq->bat_iv.tq_avg;
363c6c8fea2SSven Eckelmann 
364c6c8fea2SSven Eckelmann 	/* the TQ value has to be better */
365c6c8fea2SSven Eckelmann 	if (orig_tq_avg < gw_tq_avg)
3665d02b3cdSLinus Lüssing 		goto out;
367c6c8fea2SSven Eckelmann 
3689cfc7bd6SSven Eckelmann 	/* if the routing class is greater than 3 the value tells us how much
369c6c8fea2SSven Eckelmann 	 * greater the TQ value of the new gateway must be
3709cfc7bd6SSven Eckelmann 	 */
371c6c8fea2SSven Eckelmann 	if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
372c6c8fea2SSven Eckelmann 	    (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
3735d02b3cdSLinus Lüssing 		goto out;
374c6c8fea2SSven Eckelmann 
37539c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
37686ceb360SSven Eckelmann 		   "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
377c6c8fea2SSven Eckelmann 		   gw_tq_avg, orig_tq_avg);
378c6c8fea2SSven Eckelmann 
3794e820e72SAntonio Quartulli reselect:
3804e820e72SAntonio Quartulli 	batadv_gw_reselect(bat_priv);
3815d02b3cdSLinus Lüssing out:
38257f0c07cSLinus Lüssing 	if (curr_gw_orig)
3837d211efcSSven Eckelmann 		batadv_orig_node_free_ref(curr_gw_orig);
384e1a5382fSLinus Lüssing 	if (router_gw)
3857d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router_gw);
386e1a5382fSLinus Lüssing 	if (router_orig)
3877d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(router_orig);
38889652331SSimon Wunderlich 	if (router_gw_tq)
38989652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_gw_tq);
39089652331SSimon Wunderlich 	if (router_orig_tq)
39189652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_orig_tq);
39257f0c07cSLinus Lüssing 
3935d02b3cdSLinus Lüssing 	return;
394c6c8fea2SSven Eckelmann }
395c6c8fea2SSven Eckelmann 
396414254e3SMarek Lindner /**
397414254e3SMarek Lindner  * batadv_gw_node_add - add gateway node to list of available gateways
398414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
399414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
400414254e3SMarek Lindner  * @gateway: announced bandwidth information
401414254e3SMarek Lindner  */
40256303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv,
40356303d34SSven Eckelmann 			       struct batadv_orig_node *orig_node,
404414254e3SMarek Lindner 			       struct batadv_tvlv_gateway_data *gateway)
405c6c8fea2SSven Eckelmann {
40656303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
407414254e3SMarek Lindner 
408414254e3SMarek Lindner 	if (gateway->bandwidth_down == 0)
409414254e3SMarek Lindner 		return;
410c6c8fea2SSven Eckelmann 
411704509b8SSven Eckelmann 	gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
412c6c8fea2SSven Eckelmann 	if (!gw_node)
413c6c8fea2SSven Eckelmann 		return;
414c6c8fea2SSven Eckelmann 
415c6c8fea2SSven Eckelmann 	INIT_HLIST_NODE(&gw_node->list);
416c6c8fea2SSven Eckelmann 	gw_node->orig_node = orig_node;
41725b6d3c1SMarek Lindner 	atomic_set(&gw_node->refcount, 1);
418c6c8fea2SSven Eckelmann 
419807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
420807736f6SSven Eckelmann 	hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
421807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
422c6c8fea2SSven Eckelmann 
42339c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
424414254e3SMarek Lindner 		   "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
425414254e3SMarek Lindner 		   orig_node->orig,
426414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
427414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
428414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
429414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
430c6c8fea2SSven Eckelmann }
431c6c8fea2SSven Eckelmann 
432414254e3SMarek Lindner /**
433414254e3SMarek Lindner  * batadv_gw_node_get - retrieve gateway node from list of available gateways
434414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
435414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
436414254e3SMarek Lindner  *
437414254e3SMarek Lindner  * Returns gateway node if found or NULL otherwise.
43871e4aa9cSAntonio Quartulli  */
439414254e3SMarek Lindner static struct batadv_gw_node *
440414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv,
441414254e3SMarek Lindner 		   struct batadv_orig_node *orig_node)
442414254e3SMarek Lindner {
443414254e3SMarek Lindner 	struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
444c6c8fea2SSven Eckelmann 
445c6c8fea2SSven Eckelmann 	rcu_read_lock();
446414254e3SMarek Lindner 	hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
447414254e3SMarek Lindner 		if (gw_node_tmp->orig_node != orig_node)
448c6c8fea2SSven Eckelmann 			continue;
449c6c8fea2SSven Eckelmann 
450414254e3SMarek Lindner 		if (gw_node_tmp->deleted)
451414254e3SMarek Lindner 			continue;
452414254e3SMarek Lindner 
453414254e3SMarek Lindner 		if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
454414254e3SMarek Lindner 			continue;
455414254e3SMarek Lindner 
456414254e3SMarek Lindner 		gw_node = gw_node_tmp;
457414254e3SMarek Lindner 		break;
458414254e3SMarek Lindner 	}
459414254e3SMarek Lindner 	rcu_read_unlock();
460414254e3SMarek Lindner 
461414254e3SMarek Lindner 	return gw_node;
462414254e3SMarek Lindner }
463414254e3SMarek Lindner 
464414254e3SMarek Lindner /**
465414254e3SMarek Lindner  * batadv_gw_node_update - update list of available gateways with changed
466414254e3SMarek Lindner  *  bandwidth information
467414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
468414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
469414254e3SMarek Lindner  * @gateway: announced bandwidth information
470414254e3SMarek Lindner  */
471414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv,
472414254e3SMarek Lindner 			   struct batadv_orig_node *orig_node,
473414254e3SMarek Lindner 			   struct batadv_tvlv_gateway_data *gateway)
474414254e3SMarek Lindner {
475414254e3SMarek Lindner 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
476414254e3SMarek Lindner 
477414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_node);
478414254e3SMarek Lindner 	if (!gw_node) {
479414254e3SMarek Lindner 		batadv_gw_node_add(bat_priv, orig_node, gateway);
480414254e3SMarek Lindner 		goto out;
481414254e3SMarek Lindner 	}
482414254e3SMarek Lindner 
483414254e3SMarek Lindner 	if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
484414254e3SMarek Lindner 	    (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
485414254e3SMarek Lindner 		goto out;
486414254e3SMarek Lindner 
48739c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
488414254e3SMarek Lindner 		   "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
489414254e3SMarek Lindner 		   orig_node->orig,
490414254e3SMarek Lindner 		   gw_node->bandwidth_down / 10,
491414254e3SMarek Lindner 		   gw_node->bandwidth_down % 10,
492414254e3SMarek Lindner 		   gw_node->bandwidth_up / 10,
493414254e3SMarek Lindner 		   gw_node->bandwidth_up % 10,
494414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
495414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
496414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
497414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
498414254e3SMarek Lindner 
499414254e3SMarek Lindner 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
500414254e3SMarek Lindner 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
501c6c8fea2SSven Eckelmann 
502c6c8fea2SSven Eckelmann 	gw_node->deleted = 0;
503414254e3SMarek Lindner 	if (ntohl(gateway->bandwidth_down) == 0) {
504c6c8fea2SSven Eckelmann 		gw_node->deleted = jiffies;
50539c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
506c6c8fea2SSven Eckelmann 			   "Gateway %pM removed from gateway list\n",
507c6c8fea2SSven Eckelmann 			   orig_node->orig);
508c6c8fea2SSven Eckelmann 
509414254e3SMarek Lindner 		/* Note: We don't need a NULL check here, since curr_gw never
510414254e3SMarek Lindner 		 * gets dereferenced.
511414254e3SMarek Lindner 		 */
512414254e3SMarek Lindner 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
513c4aac1abSMarek Lindner 		if (gw_node == curr_gw)
5144e820e72SAntonio Quartulli 			batadv_gw_reselect(bat_priv);
515414254e3SMarek Lindner 	}
51671e4aa9cSAntonio Quartulli 
517414254e3SMarek Lindner out:
518c4aac1abSMarek Lindner 	if (curr_gw)
5191409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
520414254e3SMarek Lindner 	if (gw_node)
521414254e3SMarek Lindner 		batadv_gw_node_free_ref(gw_node);
522c6c8fea2SSven Eckelmann }
523c6c8fea2SSven Eckelmann 
52456303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv,
52556303d34SSven Eckelmann 			   struct batadv_orig_node *orig_node)
526c6c8fea2SSven Eckelmann {
527414254e3SMarek Lindner 	struct batadv_tvlv_gateway_data gateway;
528414254e3SMarek Lindner 
529414254e3SMarek Lindner 	gateway.bandwidth_down = 0;
530414254e3SMarek Lindner 	gateway.bandwidth_up = 0;
531414254e3SMarek Lindner 
532414254e3SMarek Lindner 	batadv_gw_node_update(bat_priv, orig_node, &gateway);
533c6c8fea2SSven Eckelmann }
534c6c8fea2SSven Eckelmann 
53556303d34SSven Eckelmann void batadv_gw_node_purge(struct batadv_priv *bat_priv)
536c6c8fea2SSven Eckelmann {
53756303d34SSven Eckelmann 	struct batadv_gw_node *gw_node, *curr_gw;
538b67bfe0dSSasha Levin 	struct hlist_node *node_tmp;
53942d0b044SSven Eckelmann 	unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
5404e820e72SAntonio Quartulli 	int do_reselect = 0;
541c4aac1abSMarek Lindner 
5421409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
543c6c8fea2SSven Eckelmann 
544807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
545c6c8fea2SSven Eckelmann 
546b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(gw_node, node_tmp,
547807736f6SSven Eckelmann 				  &bat_priv->gw.list, list) {
548c6c8fea2SSven Eckelmann 		if (((!gw_node->deleted) ||
549c6c8fea2SSven Eckelmann 		     (time_before(jiffies, gw_node->deleted + timeout))) &&
55039c75a51SSven Eckelmann 		    atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
551c6c8fea2SSven Eckelmann 			continue;
552c6c8fea2SSven Eckelmann 
553c4aac1abSMarek Lindner 		if (curr_gw == gw_node)
5544e820e72SAntonio Quartulli 			do_reselect = 1;
555c6c8fea2SSven Eckelmann 
556c6c8fea2SSven Eckelmann 		hlist_del_rcu(&gw_node->list);
5571409a834SSven Eckelmann 		batadv_gw_node_free_ref(gw_node);
558c6c8fea2SSven Eckelmann 	}
559c6c8fea2SSven Eckelmann 
560807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
561c4aac1abSMarek Lindner 
5624e820e72SAntonio Quartulli 	/* gw_reselect() needs to acquire the gw_list_lock */
5634e820e72SAntonio Quartulli 	if (do_reselect)
5644e820e72SAntonio Quartulli 		batadv_gw_reselect(bat_priv);
565c4aac1abSMarek Lindner 
566c4aac1abSMarek Lindner 	if (curr_gw)
5671409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
568c6c8fea2SSven Eckelmann }
569c6c8fea2SSven Eckelmann 
5709cfc7bd6SSven Eckelmann /* fails if orig_node has no router */
57156303d34SSven Eckelmann static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
5721409a834SSven Eckelmann 				    struct seq_file *seq,
57356303d34SSven Eckelmann 				    const struct batadv_gw_node *gw_node)
574c6c8fea2SSven Eckelmann {
57556303d34SSven Eckelmann 	struct batadv_gw_node *curr_gw;
57656303d34SSven Eckelmann 	struct batadv_neigh_node *router;
57789652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
578414254e3SMarek Lindner 	int ret = -1;
579c6c8fea2SSven Eckelmann 
580*7351a482SSimon Wunderlich 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
581e1a5382fSLinus Lüssing 	if (!router)
582e1a5382fSLinus Lüssing 		goto out;
583e1a5382fSLinus Lüssing 
58489652331SSimon Wunderlich 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
58589652331SSimon Wunderlich 	if (!router_ifinfo)
58689652331SSimon Wunderlich 		goto out;
58789652331SSimon Wunderlich 
5881409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
5895d02b3cdSLinus Lüssing 
590414254e3SMarek Lindner 	ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
5915d02b3cdSLinus Lüssing 			 (curr_gw == gw_node ? "=>" : "  "),
592c6c8fea2SSven Eckelmann 			 gw_node->orig_node->orig,
59389652331SSimon Wunderlich 			 router_ifinfo->bat_iv.tq_avg, router->addr,
594e1a5382fSLinus Lüssing 			 router->if_incoming->net_dev->name,
595414254e3SMarek Lindner 			 gw_node->bandwidth_down / 10,
596414254e3SMarek Lindner 			 gw_node->bandwidth_down % 10,
597414254e3SMarek Lindner 			 gw_node->bandwidth_up / 10,
598414254e3SMarek Lindner 			 gw_node->bandwidth_up % 10);
5995d02b3cdSLinus Lüssing 
600c4aac1abSMarek Lindner 	if (curr_gw)
6011409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
602e1a5382fSLinus Lüssing out:
60389652331SSimon Wunderlich 	if (router_ifinfo)
60489652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_ifinfo);
60589652331SSimon Wunderlich 	if (router)
60689652331SSimon Wunderlich 		batadv_neigh_node_free_ref(router);
6075d02b3cdSLinus Lüssing 	return ret;
608c6c8fea2SSven Eckelmann }
609c6c8fea2SSven Eckelmann 
6107cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
611c6c8fea2SSven Eckelmann {
612c6c8fea2SSven Eckelmann 	struct net_device *net_dev = (struct net_device *)seq->private;
61356303d34SSven Eckelmann 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
61456303d34SSven Eckelmann 	struct batadv_hard_iface *primary_if;
61556303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
61630da63a6SMarek Lindner 	int gw_count = 0;
617c6c8fea2SSven Eckelmann 
61830da63a6SMarek Lindner 	primary_if = batadv_seq_print_text_primary_if_get(seq);
61930da63a6SMarek Lindner 	if (!primary_if)
62032ae9b22SMarek Lindner 		goto out;
621c6c8fea2SSven Eckelmann 
62286ceb360SSven Eckelmann 	seq_printf(seq,
623414254e3SMarek Lindner 		   "      %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
62442d0b044SSven Eckelmann 		   "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
62542d0b044SSven Eckelmann 		   BATADV_SOURCE_VERSION, primary_if->net_dev->name,
62632ae9b22SMarek Lindner 		   primary_if->net_dev->dev_addr, net_dev->name);
627c6c8fea2SSven Eckelmann 
628c6c8fea2SSven Eckelmann 	rcu_read_lock();
629b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
630c6c8fea2SSven Eckelmann 		if (gw_node->deleted)
631c6c8fea2SSven Eckelmann 			continue;
632c6c8fea2SSven Eckelmann 
633e1a5382fSLinus Lüssing 		/* fails if orig_node has no router */
6341409a834SSven Eckelmann 		if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
635c6c8fea2SSven Eckelmann 			continue;
636c6c8fea2SSven Eckelmann 
637c6c8fea2SSven Eckelmann 		gw_count++;
638c6c8fea2SSven Eckelmann 	}
639c6c8fea2SSven Eckelmann 	rcu_read_unlock();
640c6c8fea2SSven Eckelmann 
641c6c8fea2SSven Eckelmann 	if (gw_count == 0)
6420c814653SAntonio Quartulli 		seq_puts(seq, "No gateways in range ...\n");
643c6c8fea2SSven Eckelmann 
64432ae9b22SMarek Lindner out:
64532ae9b22SMarek Lindner 	if (primary_if)
646e5d89254SSven Eckelmann 		batadv_hardif_free_ref(primary_if);
64730da63a6SMarek Lindner 	return 0;
648c6c8fea2SSven Eckelmann }
649c6c8fea2SSven Eckelmann 
6506c413b1cSAntonio Quartulli /**
6516c413b1cSAntonio Quartulli  * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
6526c413b1cSAntonio Quartulli  * @skb: the packet to check
6536c413b1cSAntonio Quartulli  * @header_len: a pointer to the batman-adv header size
6546c413b1cSAntonio Quartulli  * @chaddr: buffer where the client address will be stored. Valid
6556c413b1cSAntonio Quartulli  *  only if the function returns BATADV_DHCP_TO_CLIENT
6566c413b1cSAntonio Quartulli  *
6576c413b1cSAntonio Quartulli  * Returns:
6586c413b1cSAntonio Quartulli  * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
6596c413b1cSAntonio Quartulli  *   while parsing it
6606c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
6616c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
6626c413b1cSAntonio Quartulli  *
6636c413b1cSAntonio Quartulli  * This function may re-allocate the data buffer of the skb passed as argument.
6649cfc7bd6SSven Eckelmann  */
6656c413b1cSAntonio Quartulli enum batadv_dhcp_recipient
6666c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
6676c413b1cSAntonio Quartulli 			     uint8_t *chaddr)
668c6c8fea2SSven Eckelmann {
6696c413b1cSAntonio Quartulli 	enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
670c6c8fea2SSven Eckelmann 	struct ethhdr *ethhdr;
671c6c8fea2SSven Eckelmann 	struct iphdr *iphdr;
672c6c8fea2SSven Eckelmann 	struct ipv6hdr *ipv6hdr;
673c6c8fea2SSven Eckelmann 	struct udphdr *udphdr;
674f7f8ed56SAntonio Quartulli 	struct vlan_ethhdr *vhdr;
6756c413b1cSAntonio Quartulli 	int chaddr_offset;
676f7f8ed56SAntonio Quartulli 	__be16 proto;
6776c413b1cSAntonio Quartulli 	uint8_t *p;
678c6c8fea2SSven Eckelmann 
679c6c8fea2SSven Eckelmann 	/* check for ethernet header */
680be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
6816c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
6826c413b1cSAntonio Quartulli 
683c6c8fea2SSven Eckelmann 	ethhdr = (struct ethhdr *)skb->data;
684f7f8ed56SAntonio Quartulli 	proto = ethhdr->h_proto;
685be7af5cfSMarek Lindner 	*header_len += ETH_HLEN;
686c6c8fea2SSven Eckelmann 
687c6c8fea2SSven Eckelmann 	/* check for initial vlan header */
688f7f8ed56SAntonio Quartulli 	if (proto == htons(ETH_P_8021Q)) {
689be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
6906c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
691f7f8ed56SAntonio Quartulli 
692f7f8ed56SAntonio Quartulli 		vhdr = (struct vlan_ethhdr *)skb->data;
693f7f8ed56SAntonio Quartulli 		proto = vhdr->h_vlan_encapsulated_proto;
694be7af5cfSMarek Lindner 		*header_len += VLAN_HLEN;
695c6c8fea2SSven Eckelmann 	}
696c6c8fea2SSven Eckelmann 
697c6c8fea2SSven Eckelmann 	/* check for ip header */
698f7f8ed56SAntonio Quartulli 	switch (proto) {
699f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IP):
700be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
7016c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7026c413b1cSAntonio Quartulli 
703be7af5cfSMarek Lindner 		iphdr = (struct iphdr *)(skb->data + *header_len);
704be7af5cfSMarek Lindner 		*header_len += iphdr->ihl * 4;
705c6c8fea2SSven Eckelmann 
706c6c8fea2SSven Eckelmann 		/* check for udp header */
707c6c8fea2SSven Eckelmann 		if (iphdr->protocol != IPPROTO_UDP)
7086c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
709c6c8fea2SSven Eckelmann 
710c6c8fea2SSven Eckelmann 		break;
711f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IPV6):
712be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
7136c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7146c413b1cSAntonio Quartulli 
715be7af5cfSMarek Lindner 		ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
716be7af5cfSMarek Lindner 		*header_len += sizeof(*ipv6hdr);
717c6c8fea2SSven Eckelmann 
718c6c8fea2SSven Eckelmann 		/* check for udp header */
719c6c8fea2SSven Eckelmann 		if (ipv6hdr->nexthdr != IPPROTO_UDP)
7206c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
721c6c8fea2SSven Eckelmann 
722c6c8fea2SSven Eckelmann 		break;
723c6c8fea2SSven Eckelmann 	default:
7246c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
725c6c8fea2SSven Eckelmann 	}
726c6c8fea2SSven Eckelmann 
727be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
7286c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
7299d2c9488SLinus Lüssing 
7309d2c9488SLinus Lüssing 	/* skb->data might have been reallocated by pskb_may_pull() */
7319d2c9488SLinus Lüssing 	ethhdr = (struct ethhdr *)skb->data;
7329d2c9488SLinus Lüssing 	if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
7339d2c9488SLinus Lüssing 		ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
7349d2c9488SLinus Lüssing 
735be7af5cfSMarek Lindner 	udphdr = (struct udphdr *)(skb->data + *header_len);
736be7af5cfSMarek Lindner 	*header_len += sizeof(*udphdr);
737c6c8fea2SSven Eckelmann 
738c6c8fea2SSven Eckelmann 	/* check for bootp port */
7396c413b1cSAntonio Quartulli 	switch (proto) {
7406c413b1cSAntonio Quartulli 	case htons(ETH_P_IP):
7416c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(67))
7426c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
7436c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(67))
7446c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
7456c413b1cSAntonio Quartulli 		break;
7466c413b1cSAntonio Quartulli 	case htons(ETH_P_IPV6):
7476c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(547))
7486c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
7496c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(547))
7506c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
7516c413b1cSAntonio Quartulli 		break;
752be7af5cfSMarek Lindner 	}
753c6c8fea2SSven Eckelmann 
7546c413b1cSAntonio Quartulli 	chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
7556c413b1cSAntonio Quartulli 	/* store the client address if the message is going to a client */
7566c413b1cSAntonio Quartulli 	if (ret == BATADV_DHCP_TO_CLIENT &&
7576c413b1cSAntonio Quartulli 	    pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
7586c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries an Ethernet DHCP */
7596c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
7606c413b1cSAntonio Quartulli 		if (*p != BATADV_DHCP_HTYPE_ETHERNET)
7616c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7626c413b1cSAntonio Quartulli 
7636c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries a valid Ethernet address */
7646c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
7656c413b1cSAntonio Quartulli 		if (*p != ETH_ALEN)
7666c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7676c413b1cSAntonio Quartulli 
7686c413b1cSAntonio Quartulli 		memcpy(chaddr, skb->data + chaddr_offset, ETH_ALEN);
7696c413b1cSAntonio Quartulli 	}
7706c413b1cSAntonio Quartulli 
7716c413b1cSAntonio Quartulli 	return ret;
7726c413b1cSAntonio Quartulli }
773bbb877edSAntonio Quartulli /**
774bbb877edSAntonio Quartulli  * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
775bbb877edSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
776bbb877edSAntonio Quartulli  * @skb: the outgoing packet
777bbb877edSAntonio Quartulli  *
778bbb877edSAntonio Quartulli  * Check if the skb is a DHCP request and if it is sent to the current best GW
779bbb877edSAntonio Quartulli  * server. Due to topology changes it may be the case that the GW server
780bbb877edSAntonio Quartulli  * previously selected is not the best one anymore.
781bbb877edSAntonio Quartulli  *
782bbb877edSAntonio Quartulli  * Returns true if the packet destination is unicast and it is not the best gw,
783bbb877edSAntonio Quartulli  * false otherwise.
784bbb877edSAntonio Quartulli  *
785bbb877edSAntonio Quartulli  * This call might reallocate skb data.
7866c413b1cSAntonio Quartulli  * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
787bbb877edSAntonio Quartulli  */
78856303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
7899d2c9488SLinus Lüssing 			    struct sk_buff *skb)
790be7af5cfSMarek Lindner {
79156303d34SSven Eckelmann 	struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
79256303d34SSven Eckelmann 	struct batadv_orig_node *orig_dst_node = NULL;
793414254e3SMarek Lindner 	struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
79489652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
7956c413b1cSAntonio Quartulli 	struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
7966c413b1cSAntonio Quartulli 	bool out_of_range = false;
797be7af5cfSMarek Lindner 	uint8_t curr_tq_avg;
798bbb877edSAntonio Quartulli 	unsigned short vid;
799bbb877edSAntonio Quartulli 
800bbb877edSAntonio Quartulli 	vid = batadv_get_vid(skb, 0);
801be7af5cfSMarek Lindner 
80208c36d3eSSven Eckelmann 	orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
803bbb877edSAntonio Quartulli 						 ethhdr->h_dest, vid);
804be7af5cfSMarek Lindner 	if (!orig_dst_node)
805be7af5cfSMarek Lindner 		goto out;
806be7af5cfSMarek Lindner 
807414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
808414254e3SMarek Lindner 	if (!gw_node->bandwidth_down == 0)
809be7af5cfSMarek Lindner 		goto out;
810be7af5cfSMarek Lindner 
811be7af5cfSMarek Lindner 	switch (atomic_read(&bat_priv->gw_mode)) {
812cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_SERVER:
813be7af5cfSMarek Lindner 		/* If we are a GW then we are our best GW. We can artificially
8149cfc7bd6SSven Eckelmann 		 * set the tq towards ourself as the maximum value
8159cfc7bd6SSven Eckelmann 		 */
81642d0b044SSven Eckelmann 		curr_tq_avg = BATADV_TQ_MAX_VALUE;
817be7af5cfSMarek Lindner 		break;
818cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_CLIENT:
8191409a834SSven Eckelmann 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
820c4aac1abSMarek Lindner 		if (!curr_gw)
821be7af5cfSMarek Lindner 			goto out;
822c6c8fea2SSven Eckelmann 
823be7af5cfSMarek Lindner 		/* packet is going to our gateway */
824be7af5cfSMarek Lindner 		if (curr_gw->orig_node == orig_dst_node)
825be7af5cfSMarek Lindner 			goto out;
826be7af5cfSMarek Lindner 
82743676ab5SAntonio Quartulli 		/* If the dhcp packet has been sent to a different gw,
82843676ab5SAntonio Quartulli 		 * we have to evaluate whether the old gw is still
8299cfc7bd6SSven Eckelmann 		 * reliable enough
8309cfc7bd6SSven Eckelmann 		 */
83130d3c511SSven Eckelmann 		neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
83230d3c511SSven Eckelmann 						NULL);
833be7af5cfSMarek Lindner 		if (!neigh_curr)
834be7af5cfSMarek Lindner 			goto out;
835be7af5cfSMarek Lindner 
83689652331SSimon Wunderlich 		curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
83789652331SSimon Wunderlich 						      BATADV_IF_DEFAULT);
83889652331SSimon Wunderlich 		if (!curr_ifinfo)
83989652331SSimon Wunderlich 			goto out;
84089652331SSimon Wunderlich 
84189652331SSimon Wunderlich 		curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
84289652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(curr_ifinfo);
84389652331SSimon Wunderlich 
844be7af5cfSMarek Lindner 		break;
845cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_OFF:
846be7af5cfSMarek Lindner 	default:
847be7af5cfSMarek Lindner 		goto out;
84843676ab5SAntonio Quartulli 	}
849be7af5cfSMarek Lindner 
85030d3c511SSven Eckelmann 	neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
8512ef04f47SDan Carpenter 	if (!neigh_old)
852be7af5cfSMarek Lindner 		goto out;
853be7af5cfSMarek Lindner 
85489652331SSimon Wunderlich 	old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
85589652331SSimon Wunderlich 	if (!old_ifinfo)
85689652331SSimon Wunderlich 		goto out;
85789652331SSimon Wunderlich 
85889652331SSimon Wunderlich 	if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
859be7af5cfSMarek Lindner 		out_of_range = true;
86089652331SSimon Wunderlich 	batadv_neigh_ifinfo_free_ref(old_ifinfo);
861be7af5cfSMarek Lindner 
862be7af5cfSMarek Lindner out:
863be7af5cfSMarek Lindner 	if (orig_dst_node)
8647d211efcSSven Eckelmann 		batadv_orig_node_free_ref(orig_dst_node);
865be7af5cfSMarek Lindner 	if (curr_gw)
8661409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
867414254e3SMarek Lindner 	if (gw_node)
868414254e3SMarek Lindner 		batadv_gw_node_free_ref(gw_node);
86943676ab5SAntonio Quartulli 	if (neigh_old)
8707d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(neigh_old);
87143676ab5SAntonio Quartulli 	if (neigh_curr)
8727d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(neigh_curr);
873be7af5cfSMarek Lindner 	return out_of_range;
874c6c8fea2SSven Eckelmann }
875