xref: /openbmc/linux/net/batman-adv/gateway_client.c (revision 927c2ed7e5879a96759aadda94404d516ce9fb42)
1e19f9759SSimon Wunderlich /* Copyright (C) 2009-2014 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;
1497351a482SSimon 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 
2697351a482SSimon Wunderlich 		router = batadv_orig_router_get(next_gw->orig_node,
2707351a482SSimon 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 
3397351a482SSimon 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 
3527351a482SSimon 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);
392c6c8fea2SSven Eckelmann }
393c6c8fea2SSven Eckelmann 
394414254e3SMarek Lindner /**
395414254e3SMarek Lindner  * batadv_gw_node_add - add gateway node to list of available gateways
396414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
397414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
398414254e3SMarek Lindner  * @gateway: announced bandwidth information
399414254e3SMarek Lindner  */
40056303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv,
40156303d34SSven Eckelmann 			       struct batadv_orig_node *orig_node,
402414254e3SMarek Lindner 			       struct batadv_tvlv_gateway_data *gateway)
403c6c8fea2SSven Eckelmann {
40456303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
405414254e3SMarek Lindner 
406414254e3SMarek Lindner 	if (gateway->bandwidth_down == 0)
407414254e3SMarek Lindner 		return;
408c6c8fea2SSven Eckelmann 
409704509b8SSven Eckelmann 	gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
410c6c8fea2SSven Eckelmann 	if (!gw_node)
411c6c8fea2SSven Eckelmann 		return;
412c6c8fea2SSven Eckelmann 
413c6c8fea2SSven Eckelmann 	INIT_HLIST_NODE(&gw_node->list);
414c6c8fea2SSven Eckelmann 	gw_node->orig_node = orig_node;
41525b6d3c1SMarek Lindner 	atomic_set(&gw_node->refcount, 1);
416c6c8fea2SSven Eckelmann 
417807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
418807736f6SSven Eckelmann 	hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
419807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
420c6c8fea2SSven Eckelmann 
42139c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
422414254e3SMarek Lindner 		   "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
423414254e3SMarek Lindner 		   orig_node->orig,
424414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
425414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
426414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
427414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
428c6c8fea2SSven Eckelmann }
429c6c8fea2SSven Eckelmann 
430414254e3SMarek Lindner /**
431414254e3SMarek Lindner  * batadv_gw_node_get - retrieve gateway node from list of available gateways
432414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
433414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
434414254e3SMarek Lindner  *
435414254e3SMarek Lindner  * Returns gateway node if found or NULL otherwise.
43671e4aa9cSAntonio Quartulli  */
437414254e3SMarek Lindner static struct batadv_gw_node *
438414254e3SMarek Lindner batadv_gw_node_get(struct batadv_priv *bat_priv,
439414254e3SMarek Lindner 		   struct batadv_orig_node *orig_node)
440414254e3SMarek Lindner {
441414254e3SMarek Lindner 	struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
442c6c8fea2SSven Eckelmann 
443c6c8fea2SSven Eckelmann 	rcu_read_lock();
444414254e3SMarek Lindner 	hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
445414254e3SMarek Lindner 		if (gw_node_tmp->orig_node != orig_node)
446c6c8fea2SSven Eckelmann 			continue;
447c6c8fea2SSven Eckelmann 
448414254e3SMarek Lindner 		if (gw_node_tmp->deleted)
449414254e3SMarek Lindner 			continue;
450414254e3SMarek Lindner 
451414254e3SMarek Lindner 		if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
452414254e3SMarek Lindner 			continue;
453414254e3SMarek Lindner 
454414254e3SMarek Lindner 		gw_node = gw_node_tmp;
455414254e3SMarek Lindner 		break;
456414254e3SMarek Lindner 	}
457414254e3SMarek Lindner 	rcu_read_unlock();
458414254e3SMarek Lindner 
459414254e3SMarek Lindner 	return gw_node;
460414254e3SMarek Lindner }
461414254e3SMarek Lindner 
462414254e3SMarek Lindner /**
463414254e3SMarek Lindner  * batadv_gw_node_update - update list of available gateways with changed
464414254e3SMarek Lindner  *  bandwidth information
465414254e3SMarek Lindner  * @bat_priv: the bat priv with all the soft interface information
466414254e3SMarek Lindner  * @orig_node: originator announcing gateway capabilities
467414254e3SMarek Lindner  * @gateway: announced bandwidth information
468414254e3SMarek Lindner  */
469414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv,
470414254e3SMarek Lindner 			   struct batadv_orig_node *orig_node,
471414254e3SMarek Lindner 			   struct batadv_tvlv_gateway_data *gateway)
472414254e3SMarek Lindner {
473414254e3SMarek Lindner 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
474414254e3SMarek Lindner 
475414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_node);
476414254e3SMarek Lindner 	if (!gw_node) {
477414254e3SMarek Lindner 		batadv_gw_node_add(bat_priv, orig_node, gateway);
478414254e3SMarek Lindner 		goto out;
479414254e3SMarek Lindner 	}
480414254e3SMarek Lindner 
481414254e3SMarek Lindner 	if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
482414254e3SMarek Lindner 	    (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
483414254e3SMarek Lindner 		goto out;
484414254e3SMarek Lindner 
48539c75a51SSven Eckelmann 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
486414254e3SMarek Lindner 		   "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
487414254e3SMarek Lindner 		   orig_node->orig,
488414254e3SMarek Lindner 		   gw_node->bandwidth_down / 10,
489414254e3SMarek Lindner 		   gw_node->bandwidth_down % 10,
490414254e3SMarek Lindner 		   gw_node->bandwidth_up / 10,
491414254e3SMarek Lindner 		   gw_node->bandwidth_up % 10,
492414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) / 10,
493414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_down) % 10,
494414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) / 10,
495414254e3SMarek Lindner 		   ntohl(gateway->bandwidth_up) % 10);
496414254e3SMarek Lindner 
497414254e3SMarek Lindner 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
498414254e3SMarek Lindner 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
499c6c8fea2SSven Eckelmann 
500c6c8fea2SSven Eckelmann 	gw_node->deleted = 0;
501414254e3SMarek Lindner 	if (ntohl(gateway->bandwidth_down) == 0) {
502c6c8fea2SSven Eckelmann 		gw_node->deleted = jiffies;
50339c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
504c6c8fea2SSven Eckelmann 			   "Gateway %pM removed from gateway list\n",
505c6c8fea2SSven Eckelmann 			   orig_node->orig);
506c6c8fea2SSven Eckelmann 
507414254e3SMarek Lindner 		/* Note: We don't need a NULL check here, since curr_gw never
508414254e3SMarek Lindner 		 * gets dereferenced.
509414254e3SMarek Lindner 		 */
510414254e3SMarek Lindner 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
511c4aac1abSMarek Lindner 		if (gw_node == curr_gw)
5124e820e72SAntonio Quartulli 			batadv_gw_reselect(bat_priv);
513414254e3SMarek Lindner 	}
51471e4aa9cSAntonio Quartulli 
515414254e3SMarek Lindner out:
516c4aac1abSMarek Lindner 	if (curr_gw)
5171409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
518414254e3SMarek Lindner 	if (gw_node)
519414254e3SMarek Lindner 		batadv_gw_node_free_ref(gw_node);
520c6c8fea2SSven Eckelmann }
521c6c8fea2SSven Eckelmann 
52256303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv,
52356303d34SSven Eckelmann 			   struct batadv_orig_node *orig_node)
524c6c8fea2SSven Eckelmann {
525414254e3SMarek Lindner 	struct batadv_tvlv_gateway_data gateway;
526414254e3SMarek Lindner 
527414254e3SMarek Lindner 	gateway.bandwidth_down = 0;
528414254e3SMarek Lindner 	gateway.bandwidth_up = 0;
529414254e3SMarek Lindner 
530414254e3SMarek Lindner 	batadv_gw_node_update(bat_priv, orig_node, &gateway);
531c6c8fea2SSven Eckelmann }
532c6c8fea2SSven Eckelmann 
53356303d34SSven Eckelmann void batadv_gw_node_purge(struct batadv_priv *bat_priv)
534c6c8fea2SSven Eckelmann {
53556303d34SSven Eckelmann 	struct batadv_gw_node *gw_node, *curr_gw;
536b67bfe0dSSasha Levin 	struct hlist_node *node_tmp;
53742d0b044SSven Eckelmann 	unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
5384e820e72SAntonio Quartulli 	int do_reselect = 0;
539c4aac1abSMarek Lindner 
5401409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
541c6c8fea2SSven Eckelmann 
542807736f6SSven Eckelmann 	spin_lock_bh(&bat_priv->gw.list_lock);
543c6c8fea2SSven Eckelmann 
544b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(gw_node, node_tmp,
545807736f6SSven Eckelmann 				  &bat_priv->gw.list, list) {
546c6c8fea2SSven Eckelmann 		if (((!gw_node->deleted) ||
547c6c8fea2SSven Eckelmann 		     (time_before(jiffies, gw_node->deleted + timeout))) &&
54839c75a51SSven Eckelmann 		    atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
549c6c8fea2SSven Eckelmann 			continue;
550c6c8fea2SSven Eckelmann 
551c4aac1abSMarek Lindner 		if (curr_gw == gw_node)
5524e820e72SAntonio Quartulli 			do_reselect = 1;
553c6c8fea2SSven Eckelmann 
554c6c8fea2SSven Eckelmann 		hlist_del_rcu(&gw_node->list);
5551409a834SSven Eckelmann 		batadv_gw_node_free_ref(gw_node);
556c6c8fea2SSven Eckelmann 	}
557c6c8fea2SSven Eckelmann 
558807736f6SSven Eckelmann 	spin_unlock_bh(&bat_priv->gw.list_lock);
559c4aac1abSMarek Lindner 
5604e820e72SAntonio Quartulli 	/* gw_reselect() needs to acquire the gw_list_lock */
5614e820e72SAntonio Quartulli 	if (do_reselect)
5624e820e72SAntonio Quartulli 		batadv_gw_reselect(bat_priv);
563c4aac1abSMarek Lindner 
564c4aac1abSMarek Lindner 	if (curr_gw)
5651409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
566c6c8fea2SSven Eckelmann }
567c6c8fea2SSven Eckelmann 
5689cfc7bd6SSven Eckelmann /* fails if orig_node has no router */
56956303d34SSven Eckelmann static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
5701409a834SSven Eckelmann 				    struct seq_file *seq,
57156303d34SSven Eckelmann 				    const struct batadv_gw_node *gw_node)
572c6c8fea2SSven Eckelmann {
57356303d34SSven Eckelmann 	struct batadv_gw_node *curr_gw;
57456303d34SSven Eckelmann 	struct batadv_neigh_node *router;
57589652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
576414254e3SMarek Lindner 	int ret = -1;
577c6c8fea2SSven Eckelmann 
5787351a482SSimon Wunderlich 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
579e1a5382fSLinus Lüssing 	if (!router)
580e1a5382fSLinus Lüssing 		goto out;
581e1a5382fSLinus Lüssing 
58289652331SSimon Wunderlich 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
58389652331SSimon Wunderlich 	if (!router_ifinfo)
58489652331SSimon Wunderlich 		goto out;
58589652331SSimon Wunderlich 
5861409a834SSven Eckelmann 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
5875d02b3cdSLinus Lüssing 
588414254e3SMarek Lindner 	ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
5895d02b3cdSLinus Lüssing 			 (curr_gw == gw_node ? "=>" : "  "),
590c6c8fea2SSven Eckelmann 			 gw_node->orig_node->orig,
59189652331SSimon Wunderlich 			 router_ifinfo->bat_iv.tq_avg, router->addr,
592e1a5382fSLinus Lüssing 			 router->if_incoming->net_dev->name,
593414254e3SMarek Lindner 			 gw_node->bandwidth_down / 10,
594414254e3SMarek Lindner 			 gw_node->bandwidth_down % 10,
595414254e3SMarek Lindner 			 gw_node->bandwidth_up / 10,
596414254e3SMarek Lindner 			 gw_node->bandwidth_up % 10);
5975d02b3cdSLinus Lüssing 
598c4aac1abSMarek Lindner 	if (curr_gw)
5991409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
600e1a5382fSLinus Lüssing out:
60189652331SSimon Wunderlich 	if (router_ifinfo)
60289652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(router_ifinfo);
60389652331SSimon Wunderlich 	if (router)
60489652331SSimon Wunderlich 		batadv_neigh_node_free_ref(router);
6055d02b3cdSLinus Lüssing 	return ret;
606c6c8fea2SSven Eckelmann }
607c6c8fea2SSven Eckelmann 
6087cf06bc6SSven Eckelmann int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
609c6c8fea2SSven Eckelmann {
610c6c8fea2SSven Eckelmann 	struct net_device *net_dev = (struct net_device *)seq->private;
61156303d34SSven Eckelmann 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
61256303d34SSven Eckelmann 	struct batadv_hard_iface *primary_if;
61356303d34SSven Eckelmann 	struct batadv_gw_node *gw_node;
61430da63a6SMarek Lindner 	int gw_count = 0;
615c6c8fea2SSven Eckelmann 
61630da63a6SMarek Lindner 	primary_if = batadv_seq_print_text_primary_if_get(seq);
61730da63a6SMarek Lindner 	if (!primary_if)
61832ae9b22SMarek Lindner 		goto out;
619c6c8fea2SSven Eckelmann 
62086ceb360SSven Eckelmann 	seq_printf(seq,
621414254e3SMarek Lindner 		   "      %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
62242d0b044SSven Eckelmann 		   "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
62342d0b044SSven Eckelmann 		   BATADV_SOURCE_VERSION, primary_if->net_dev->name,
62432ae9b22SMarek Lindner 		   primary_if->net_dev->dev_addr, net_dev->name);
625c6c8fea2SSven Eckelmann 
626c6c8fea2SSven Eckelmann 	rcu_read_lock();
627b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
628c6c8fea2SSven Eckelmann 		if (gw_node->deleted)
629c6c8fea2SSven Eckelmann 			continue;
630c6c8fea2SSven Eckelmann 
631e1a5382fSLinus Lüssing 		/* fails if orig_node has no router */
6321409a834SSven Eckelmann 		if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
633c6c8fea2SSven Eckelmann 			continue;
634c6c8fea2SSven Eckelmann 
635c6c8fea2SSven Eckelmann 		gw_count++;
636c6c8fea2SSven Eckelmann 	}
637c6c8fea2SSven Eckelmann 	rcu_read_unlock();
638c6c8fea2SSven Eckelmann 
639c6c8fea2SSven Eckelmann 	if (gw_count == 0)
6400c814653SAntonio Quartulli 		seq_puts(seq, "No gateways in range ...\n");
641c6c8fea2SSven Eckelmann 
64232ae9b22SMarek Lindner out:
64332ae9b22SMarek Lindner 	if (primary_if)
644e5d89254SSven Eckelmann 		batadv_hardif_free_ref(primary_if);
64530da63a6SMarek Lindner 	return 0;
646c6c8fea2SSven Eckelmann }
647c6c8fea2SSven Eckelmann 
6486c413b1cSAntonio Quartulli /**
6496c413b1cSAntonio Quartulli  * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
6506c413b1cSAntonio Quartulli  * @skb: the packet to check
6516c413b1cSAntonio Quartulli  * @header_len: a pointer to the batman-adv header size
6526c413b1cSAntonio Quartulli  * @chaddr: buffer where the client address will be stored. Valid
6536c413b1cSAntonio Quartulli  *  only if the function returns BATADV_DHCP_TO_CLIENT
6546c413b1cSAntonio Quartulli  *
6556c413b1cSAntonio Quartulli  * Returns:
6566c413b1cSAntonio Quartulli  * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
6576c413b1cSAntonio Quartulli  *   while parsing it
6586c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
6596c413b1cSAntonio Quartulli  * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
6606c413b1cSAntonio Quartulli  *
6616c413b1cSAntonio Quartulli  * This function may re-allocate the data buffer of the skb passed as argument.
6629cfc7bd6SSven Eckelmann  */
6636c413b1cSAntonio Quartulli enum batadv_dhcp_recipient
6646c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
6656c413b1cSAntonio Quartulli 			     uint8_t *chaddr)
666c6c8fea2SSven Eckelmann {
6676c413b1cSAntonio Quartulli 	enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
668c6c8fea2SSven Eckelmann 	struct ethhdr *ethhdr;
669c6c8fea2SSven Eckelmann 	struct iphdr *iphdr;
670c6c8fea2SSven Eckelmann 	struct ipv6hdr *ipv6hdr;
671c6c8fea2SSven Eckelmann 	struct udphdr *udphdr;
672f7f8ed56SAntonio Quartulli 	struct vlan_ethhdr *vhdr;
6736c413b1cSAntonio Quartulli 	int chaddr_offset;
674f7f8ed56SAntonio Quartulli 	__be16 proto;
6756c413b1cSAntonio Quartulli 	uint8_t *p;
676c6c8fea2SSven Eckelmann 
677c6c8fea2SSven Eckelmann 	/* check for ethernet header */
678be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
6796c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
6806c413b1cSAntonio Quartulli 
681*927c2ed7SLinus Lüssing 	ethhdr = eth_hdr(skb);
682f7f8ed56SAntonio Quartulli 	proto = ethhdr->h_proto;
683be7af5cfSMarek Lindner 	*header_len += ETH_HLEN;
684c6c8fea2SSven Eckelmann 
685c6c8fea2SSven Eckelmann 	/* check for initial vlan header */
686f7f8ed56SAntonio Quartulli 	if (proto == htons(ETH_P_8021Q)) {
687be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
6886c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
689f7f8ed56SAntonio Quartulli 
690*927c2ed7SLinus Lüssing 		vhdr = vlan_eth_hdr(skb);
691f7f8ed56SAntonio Quartulli 		proto = vhdr->h_vlan_encapsulated_proto;
692be7af5cfSMarek Lindner 		*header_len += VLAN_HLEN;
693c6c8fea2SSven Eckelmann 	}
694c6c8fea2SSven Eckelmann 
695c6c8fea2SSven Eckelmann 	/* check for ip header */
696f7f8ed56SAntonio Quartulli 	switch (proto) {
697f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IP):
698be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
6996c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7006c413b1cSAntonio Quartulli 
701be7af5cfSMarek Lindner 		iphdr = (struct iphdr *)(skb->data + *header_len);
702be7af5cfSMarek Lindner 		*header_len += iphdr->ihl * 4;
703c6c8fea2SSven Eckelmann 
704c6c8fea2SSven Eckelmann 		/* check for udp header */
705c6c8fea2SSven Eckelmann 		if (iphdr->protocol != IPPROTO_UDP)
7066c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
707c6c8fea2SSven Eckelmann 
708c6c8fea2SSven Eckelmann 		break;
709f7f8ed56SAntonio Quartulli 	case htons(ETH_P_IPV6):
710be7af5cfSMarek Lindner 		if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
7116c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7126c413b1cSAntonio Quartulli 
713be7af5cfSMarek Lindner 		ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
714be7af5cfSMarek Lindner 		*header_len += sizeof(*ipv6hdr);
715c6c8fea2SSven Eckelmann 
716c6c8fea2SSven Eckelmann 		/* check for udp header */
717c6c8fea2SSven Eckelmann 		if (ipv6hdr->nexthdr != IPPROTO_UDP)
7186c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
719c6c8fea2SSven Eckelmann 
720c6c8fea2SSven Eckelmann 		break;
721c6c8fea2SSven Eckelmann 	default:
7226c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
723c6c8fea2SSven Eckelmann 	}
724c6c8fea2SSven Eckelmann 
725be7af5cfSMarek Lindner 	if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
7266c413b1cSAntonio Quartulli 		return BATADV_DHCP_NO;
7279d2c9488SLinus Lüssing 
7289d2c9488SLinus Lüssing 	/* skb->data might have been reallocated by pskb_may_pull() */
729*927c2ed7SLinus Lüssing 	ethhdr = eth_hdr(skb);
7309d2c9488SLinus Lüssing 	if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
7319d2c9488SLinus Lüssing 		ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
7329d2c9488SLinus Lüssing 
733be7af5cfSMarek Lindner 	udphdr = (struct udphdr *)(skb->data + *header_len);
734be7af5cfSMarek Lindner 	*header_len += sizeof(*udphdr);
735c6c8fea2SSven Eckelmann 
736c6c8fea2SSven Eckelmann 	/* check for bootp port */
7376c413b1cSAntonio Quartulli 	switch (proto) {
7386c413b1cSAntonio Quartulli 	case htons(ETH_P_IP):
7396c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(67))
7406c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
7416c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(67))
7426c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
7436c413b1cSAntonio Quartulli 		break;
7446c413b1cSAntonio Quartulli 	case htons(ETH_P_IPV6):
7456c413b1cSAntonio Quartulli 		if (udphdr->dest == htons(547))
7466c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_SERVER;
7476c413b1cSAntonio Quartulli 		else if (udphdr->source == htons(547))
7486c413b1cSAntonio Quartulli 			ret = BATADV_DHCP_TO_CLIENT;
7496c413b1cSAntonio Quartulli 		break;
750be7af5cfSMarek Lindner 	}
751c6c8fea2SSven Eckelmann 
7526c413b1cSAntonio Quartulli 	chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
7536c413b1cSAntonio Quartulli 	/* store the client address if the message is going to a client */
7546c413b1cSAntonio Quartulli 	if (ret == BATADV_DHCP_TO_CLIENT &&
7556c413b1cSAntonio Quartulli 	    pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
7566c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries an Ethernet DHCP */
7576c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
7586c413b1cSAntonio Quartulli 		if (*p != BATADV_DHCP_HTYPE_ETHERNET)
7596c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7606c413b1cSAntonio Quartulli 
7616c413b1cSAntonio Quartulli 		/* check if the DHCP packet carries a valid Ethernet address */
7626c413b1cSAntonio Quartulli 		p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
7636c413b1cSAntonio Quartulli 		if (*p != ETH_ALEN)
7646c413b1cSAntonio Quartulli 			return BATADV_DHCP_NO;
7656c413b1cSAntonio Quartulli 
7666c413b1cSAntonio Quartulli 		memcpy(chaddr, skb->data + chaddr_offset, ETH_ALEN);
7676c413b1cSAntonio Quartulli 	}
7686c413b1cSAntonio Quartulli 
7696c413b1cSAntonio Quartulli 	return ret;
7706c413b1cSAntonio Quartulli }
771bbb877edSAntonio Quartulli /**
772bbb877edSAntonio Quartulli  * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
773bbb877edSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
774bbb877edSAntonio Quartulli  * @skb: the outgoing packet
775bbb877edSAntonio Quartulli  *
776bbb877edSAntonio Quartulli  * Check if the skb is a DHCP request and if it is sent to the current best GW
777bbb877edSAntonio Quartulli  * server. Due to topology changes it may be the case that the GW server
778bbb877edSAntonio Quartulli  * previously selected is not the best one anymore.
779bbb877edSAntonio Quartulli  *
780bbb877edSAntonio Quartulli  * Returns true if the packet destination is unicast and it is not the best gw,
781bbb877edSAntonio Quartulli  * false otherwise.
782bbb877edSAntonio Quartulli  *
783bbb877edSAntonio Quartulli  * This call might reallocate skb data.
7846c413b1cSAntonio Quartulli  * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
785bbb877edSAntonio Quartulli  */
78656303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
7879d2c9488SLinus Lüssing 			    struct sk_buff *skb)
788be7af5cfSMarek Lindner {
78956303d34SSven Eckelmann 	struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
79056303d34SSven Eckelmann 	struct batadv_orig_node *orig_dst_node = NULL;
791414254e3SMarek Lindner 	struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
79289652331SSimon Wunderlich 	struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
7936c413b1cSAntonio Quartulli 	struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
7946c413b1cSAntonio Quartulli 	bool out_of_range = false;
795be7af5cfSMarek Lindner 	uint8_t curr_tq_avg;
796bbb877edSAntonio Quartulli 	unsigned short vid;
797bbb877edSAntonio Quartulli 
798bbb877edSAntonio Quartulli 	vid = batadv_get_vid(skb, 0);
799be7af5cfSMarek Lindner 
80008c36d3eSSven Eckelmann 	orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
801bbb877edSAntonio Quartulli 						 ethhdr->h_dest, vid);
802be7af5cfSMarek Lindner 	if (!orig_dst_node)
803be7af5cfSMarek Lindner 		goto out;
804be7af5cfSMarek Lindner 
805414254e3SMarek Lindner 	gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
806414254e3SMarek Lindner 	if (!gw_node->bandwidth_down == 0)
807be7af5cfSMarek Lindner 		goto out;
808be7af5cfSMarek Lindner 
809be7af5cfSMarek Lindner 	switch (atomic_read(&bat_priv->gw_mode)) {
810cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_SERVER:
811be7af5cfSMarek Lindner 		/* If we are a GW then we are our best GW. We can artificially
8129cfc7bd6SSven Eckelmann 		 * set the tq towards ourself as the maximum value
8139cfc7bd6SSven Eckelmann 		 */
81442d0b044SSven Eckelmann 		curr_tq_avg = BATADV_TQ_MAX_VALUE;
815be7af5cfSMarek Lindner 		break;
816cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_CLIENT:
8171409a834SSven Eckelmann 		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
818c4aac1abSMarek Lindner 		if (!curr_gw)
819be7af5cfSMarek Lindner 			goto out;
820c6c8fea2SSven Eckelmann 
821be7af5cfSMarek Lindner 		/* packet is going to our gateway */
822be7af5cfSMarek Lindner 		if (curr_gw->orig_node == orig_dst_node)
823be7af5cfSMarek Lindner 			goto out;
824be7af5cfSMarek Lindner 
82543676ab5SAntonio Quartulli 		/* If the dhcp packet has been sent to a different gw,
82643676ab5SAntonio Quartulli 		 * we have to evaluate whether the old gw is still
8279cfc7bd6SSven Eckelmann 		 * reliable enough
8289cfc7bd6SSven Eckelmann 		 */
82930d3c511SSven Eckelmann 		neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
83030d3c511SSven Eckelmann 						NULL);
831be7af5cfSMarek Lindner 		if (!neigh_curr)
832be7af5cfSMarek Lindner 			goto out;
833be7af5cfSMarek Lindner 
83489652331SSimon Wunderlich 		curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
83589652331SSimon Wunderlich 						      BATADV_IF_DEFAULT);
83689652331SSimon Wunderlich 		if (!curr_ifinfo)
83789652331SSimon Wunderlich 			goto out;
83889652331SSimon Wunderlich 
83989652331SSimon Wunderlich 		curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
84089652331SSimon Wunderlich 		batadv_neigh_ifinfo_free_ref(curr_ifinfo);
84189652331SSimon Wunderlich 
842be7af5cfSMarek Lindner 		break;
843cd646ab1SSven Eckelmann 	case BATADV_GW_MODE_OFF:
844be7af5cfSMarek Lindner 	default:
845be7af5cfSMarek Lindner 		goto out;
84643676ab5SAntonio Quartulli 	}
847be7af5cfSMarek Lindner 
84830d3c511SSven Eckelmann 	neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
8492ef04f47SDan Carpenter 	if (!neigh_old)
850be7af5cfSMarek Lindner 		goto out;
851be7af5cfSMarek Lindner 
85289652331SSimon Wunderlich 	old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
85389652331SSimon Wunderlich 	if (!old_ifinfo)
85489652331SSimon Wunderlich 		goto out;
85589652331SSimon Wunderlich 
85689652331SSimon Wunderlich 	if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
857be7af5cfSMarek Lindner 		out_of_range = true;
85889652331SSimon Wunderlich 	batadv_neigh_ifinfo_free_ref(old_ifinfo);
859be7af5cfSMarek Lindner 
860be7af5cfSMarek Lindner out:
861be7af5cfSMarek Lindner 	if (orig_dst_node)
8627d211efcSSven Eckelmann 		batadv_orig_node_free_ref(orig_dst_node);
863be7af5cfSMarek Lindner 	if (curr_gw)
8641409a834SSven Eckelmann 		batadv_gw_node_free_ref(curr_gw);
865414254e3SMarek Lindner 	if (gw_node)
866414254e3SMarek Lindner 		batadv_gw_node_free_ref(gw_node);
86743676ab5SAntonio Quartulli 	if (neigh_old)
8687d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(neigh_old);
86943676ab5SAntonio Quartulli 	if (neigh_curr)
8707d211efcSSven Eckelmann 		batadv_neigh_node_free_ref(neigh_curr);
871be7af5cfSMarek Lindner 	return out_of_range;
872c6c8fea2SSven Eckelmann }
873