17db7d9f3SSven Eckelmann // SPDX-License-Identifier: GPL-2.0
2cfa55c6dSSven Eckelmann /* Copyright (C) B.A.T.M.A.N. contributors:
3c6c8fea2SSven Eckelmann *
4c6c8fea2SSven Eckelmann * Marek Lindner
5c6c8fea2SSven Eckelmann */
6c6c8fea2SSven Eckelmann
7c6c8fea2SSven Eckelmann #include "gateway_client.h"
81e2c2a4fSSven Eckelmann #include "main.h"
91e2c2a4fSSven Eckelmann
101e2c2a4fSSven Eckelmann #include <linux/atomic.h>
111e2c2a4fSSven Eckelmann #include <linux/byteorder/generic.h>
12*eb7da4f1SSven Eckelmann #include <linux/container_of.h>
13d7129dafSSven Eckelmann #include <linux/errno.h>
141e2c2a4fSSven Eckelmann #include <linux/etherdevice.h>
15b92b94acSSven Eckelmann #include <linux/gfp.h>
161e2c2a4fSSven Eckelmann #include <linux/if_ether.h>
171e2c2a4fSSven Eckelmann #include <linux/if_vlan.h>
181e2c2a4fSSven Eckelmann #include <linux/in.h>
191e2c2a4fSSven Eckelmann #include <linux/ip.h>
201e2c2a4fSSven Eckelmann #include <linux/ipv6.h>
211e2c2a4fSSven Eckelmann #include <linux/kernel.h>
22e7aed321SSven Eckelmann #include <linux/kref.h>
231e2c2a4fSSven Eckelmann #include <linux/list.h>
24dff9bc42SSven Eckelmann #include <linux/lockdep.h>
251e2c2a4fSSven Eckelmann #include <linux/netdevice.h>
26d7129dafSSven Eckelmann #include <linux/netlink.h>
271e2c2a4fSSven Eckelmann #include <linux/rculist.h>
281e2c2a4fSSven Eckelmann #include <linux/rcupdate.h>
291e2c2a4fSSven Eckelmann #include <linux/skbuff.h>
301e2c2a4fSSven Eckelmann #include <linux/slab.h>
311e2c2a4fSSven Eckelmann #include <linux/spinlock.h>
321e2c2a4fSSven Eckelmann #include <linux/stddef.h>
331e2c2a4fSSven Eckelmann #include <linux/udp.h>
34d7129dafSSven Eckelmann #include <net/sock.h>
35fec149f5SSven Eckelmann #include <uapi/linux/batadv_packet.h>
36d7129dafSSven Eckelmann #include <uapi/linux/batman_adv.h>
371e2c2a4fSSven Eckelmann
38c6c8fea2SSven Eckelmann #include "hard-interface.h"
39ba412080SSven Eckelmann #include "log.h"
40d7129dafSSven Eckelmann #include "netlink.h"
4157f0c07cSLinus Lüssing #include "originator.h"
4243676ab5SAntonio Quartulli #include "routing.h"
43d7129dafSSven Eckelmann #include "soft-interface.h"
441e2c2a4fSSven Eckelmann #include "translation-table.h"
45c6c8fea2SSven Eckelmann
466c413b1cSAntonio Quartulli /* These are the offsets of the "hw type" and "hw address length" in the dhcp
476c413b1cSAntonio Quartulli * packet starting at the beginning of the dhcp header
489cfc7bd6SSven Eckelmann */
496c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_OFFSET 1
506c413b1cSAntonio Quartulli #define BATADV_DHCP_HLEN_OFFSET 2
516c413b1cSAntonio Quartulli /* Value of htype representing Ethernet */
526c413b1cSAntonio Quartulli #define BATADV_DHCP_HTYPE_ETHERNET 0x01
536c413b1cSAntonio Quartulli /* This is the offset of the "chaddr" field in the dhcp packet starting at the
546c413b1cSAntonio Quartulli * beginning of the dhcp header
556c413b1cSAntonio Quartulli */
566c413b1cSAntonio Quartulli #define BATADV_DHCP_CHADDR_OFFSET 28
5743676ab5SAntonio Quartulli
58e7aed321SSven Eckelmann /**
597e9a8c2cSSven Eckelmann * batadv_gw_node_release() - release gw_node from lists and queue for free
607e9a8c2cSSven Eckelmann * after rcu grace period
61e7aed321SSven Eckelmann * @ref: kref pointer of the gw_node
62e7aed321SSven Eckelmann */
batadv_gw_node_release(struct kref * ref)636340dcbdSSven Eckelmann void batadv_gw_node_release(struct kref *ref)
6425b6d3c1SMarek Lindner {
65e7aed321SSven Eckelmann struct batadv_gw_node *gw_node;
66e7aed321SSven Eckelmann
67e7aed321SSven Eckelmann gw_node = container_of(ref, struct batadv_gw_node, refcount);
68e7aed321SSven Eckelmann
695d967310SSven Eckelmann batadv_orig_node_put(gw_node->orig_node);
70eb340b2fSPaul E. McKenney kfree_rcu(gw_node, rcu);
71c6c8fea2SSven Eckelmann }
72e7aed321SSven Eckelmann
73e7aed321SSven Eckelmann /**
74ff15c27cSSven Eckelmann * batadv_gw_get_selected_gw_node() - Get currently selected gateway
75ff15c27cSSven Eckelmann * @bat_priv: the bat priv with all the soft interface information
76ff15c27cSSven Eckelmann *
77ff15c27cSSven Eckelmann * Return: selected gateway (with increased refcnt), NULL on errors
78ff15c27cSSven Eckelmann */
7934d99cfeSAntonio Quartulli struct batadv_gw_node *
batadv_gw_get_selected_gw_node(struct batadv_priv * bat_priv)8056303d34SSven Eckelmann batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
81c6c8fea2SSven Eckelmann {
8256303d34SSven Eckelmann struct batadv_gw_node *gw_node;
83c6c8fea2SSven Eckelmann
845d02b3cdSLinus Lüssing rcu_read_lock();
85807736f6SSven Eckelmann gw_node = rcu_dereference(bat_priv->gw.curr_gw);
86c4aac1abSMarek Lindner if (!gw_node)
875d02b3cdSLinus Lüssing goto out;
88c6c8fea2SSven Eckelmann
89e7aed321SSven Eckelmann if (!kref_get_unless_zero(&gw_node->refcount))
90c4aac1abSMarek Lindner gw_node = NULL;
91c4aac1abSMarek Lindner
92c4aac1abSMarek Lindner out:
93c4aac1abSMarek Lindner rcu_read_unlock();
94c4aac1abSMarek Lindner return gw_node;
95c4aac1abSMarek Lindner }
96c4aac1abSMarek Lindner
97ff15c27cSSven Eckelmann /**
98ff15c27cSSven Eckelmann * batadv_gw_get_selected_orig() - Get originator of currently selected gateway
99ff15c27cSSven Eckelmann * @bat_priv: the bat priv with all the soft interface information
100ff15c27cSSven Eckelmann *
101ff15c27cSSven Eckelmann * Return: orig_node of selected gateway (with increased refcnt), NULL on errors
102ff15c27cSSven Eckelmann */
10356303d34SSven Eckelmann struct batadv_orig_node *
batadv_gw_get_selected_orig(struct batadv_priv * bat_priv)10456303d34SSven Eckelmann batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
105c4aac1abSMarek Lindner {
10656303d34SSven Eckelmann struct batadv_gw_node *gw_node;
10756303d34SSven Eckelmann struct batadv_orig_node *orig_node = NULL;
108c4aac1abSMarek Lindner
1091409a834SSven Eckelmann gw_node = batadv_gw_get_selected_gw_node(bat_priv);
110c4aac1abSMarek Lindner if (!gw_node)
1117b36e8eeSMarek Lindner goto out;
1125d02b3cdSLinus Lüssing
113c4aac1abSMarek Lindner rcu_read_lock();
114c4aac1abSMarek Lindner orig_node = gw_node->orig_node;
115c4aac1abSMarek Lindner if (!orig_node)
116c4aac1abSMarek Lindner goto unlock;
117c4aac1abSMarek Lindner
1187c124391SSven Eckelmann if (!kref_get_unless_zero(&orig_node->refcount))
1197b36e8eeSMarek Lindner orig_node = NULL;
12043c70ad5SLinus Lüssing
121c4aac1abSMarek Lindner unlock:
1225d02b3cdSLinus Lüssing rcu_read_unlock();
123c4aac1abSMarek Lindner out:
1243a01743dSSven Eckelmann batadv_gw_node_put(gw_node);
125c4aac1abSMarek Lindner return orig_node;
126c6c8fea2SSven Eckelmann }
127c6c8fea2SSven Eckelmann
batadv_gw_select(struct batadv_priv * bat_priv,struct batadv_gw_node * new_gw_node)12856303d34SSven Eckelmann static void batadv_gw_select(struct batadv_priv *bat_priv,
12956303d34SSven Eckelmann struct batadv_gw_node *new_gw_node)
130c6c8fea2SSven Eckelmann {
13156303d34SSven Eckelmann struct batadv_gw_node *curr_gw_node;
132c6c8fea2SSven Eckelmann
133807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock);
134c4aac1abSMarek Lindner
135a08d497dSSven Eckelmann if (new_gw_node)
136a08d497dSSven Eckelmann kref_get(&new_gw_node->refcount);
137c6c8fea2SSven Eckelmann
138cf78bb0bSAntonio Quartulli curr_gw_node = rcu_replace_pointer(bat_priv->gw.curr_gw, new_gw_node,
139cf78bb0bSAntonio Quartulli true);
14025b6d3c1SMarek Lindner
1413a01743dSSven Eckelmann batadv_gw_node_put(curr_gw_node);
142c4aac1abSMarek Lindner
143807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock);
144c4aac1abSMarek Lindner }
145c4aac1abSMarek Lindner
1464e820e72SAntonio Quartulli /**
1477e9a8c2cSSven Eckelmann * batadv_gw_reselect() - force a gateway reselection
1484e820e72SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information
1494e820e72SAntonio Quartulli *
1504e820e72SAntonio Quartulli * Set a flag to remind the GW component to perform a new gateway reselection.
1514e820e72SAntonio Quartulli * However this function does not ensure that the current gateway is going to be
1524e820e72SAntonio Quartulli * deselected. The reselection mechanism may elect the same gateway once again.
1534e820e72SAntonio Quartulli *
1544e820e72SAntonio Quartulli * This means that invoking batadv_gw_reselect() does not guarantee a gateway
1554e820e72SAntonio Quartulli * change and therefore a uevent is not necessarily expected.
1564e820e72SAntonio Quartulli */
batadv_gw_reselect(struct batadv_priv * bat_priv)1574e820e72SAntonio Quartulli void batadv_gw_reselect(struct batadv_priv *bat_priv)
158c4aac1abSMarek Lindner {
159807736f6SSven Eckelmann atomic_set(&bat_priv->gw.reselect, 1);
160c6c8fea2SSven Eckelmann }
161c6c8fea2SSven Eckelmann
162c6eaa3f0SAntonio Quartulli /**
1637e9a8c2cSSven Eckelmann * batadv_gw_check_client_stop() - check if client mode has been switched off
164c6eaa3f0SAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information
165c6eaa3f0SAntonio Quartulli *
166c6eaa3f0SAntonio Quartulli * This function assumes the caller has checked that the gw state *is actually
167c6eaa3f0SAntonio Quartulli * changing*. This function is not supposed to be called when there is no state
168c6eaa3f0SAntonio Quartulli * change.
169c6eaa3f0SAntonio Quartulli */
batadv_gw_check_client_stop(struct batadv_priv * bat_priv)170c6eaa3f0SAntonio Quartulli void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
171c6eaa3f0SAntonio Quartulli {
172c6eaa3f0SAntonio Quartulli struct batadv_gw_node *curr_gw;
173c6eaa3f0SAntonio Quartulli
1743a24a63eSAntonio Quartulli if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
175c6eaa3f0SAntonio Quartulli return;
176c6eaa3f0SAntonio Quartulli
177c6eaa3f0SAntonio Quartulli curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
178c6eaa3f0SAntonio Quartulli if (!curr_gw)
179c6eaa3f0SAntonio Quartulli return;
180c6eaa3f0SAntonio Quartulli
181f3163181SAntonio Quartulli /* deselect the current gateway so that next time that client mode is
182f3163181SAntonio Quartulli * enabled a proper GW_ADD event can be sent
183f3163181SAntonio Quartulli */
184f3163181SAntonio Quartulli batadv_gw_select(bat_priv, NULL);
185f3163181SAntonio Quartulli
186c6eaa3f0SAntonio Quartulli /* if batman-adv is switching the gw client mode off and a gateway was
187c6eaa3f0SAntonio Quartulli * already selected, send a DEL uevent
188c6eaa3f0SAntonio Quartulli */
189c6eaa3f0SAntonio Quartulli batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
190c6eaa3f0SAntonio Quartulli
1913a01743dSSven Eckelmann batadv_gw_node_put(curr_gw);
192c6eaa3f0SAntonio Quartulli }
193c6eaa3f0SAntonio Quartulli
194ff15c27cSSven Eckelmann /**
195ff15c27cSSven Eckelmann * batadv_gw_election() - Elect the best gateway
196ff15c27cSSven Eckelmann * @bat_priv: the bat priv with all the soft interface information
197ff15c27cSSven Eckelmann */
batadv_gw_election(struct batadv_priv * bat_priv)19856303d34SSven Eckelmann void batadv_gw_election(struct batadv_priv *bat_priv)
1992265c141SAntonio Quartulli {
2004f248cffSSven Eckelmann struct batadv_gw_node *curr_gw = NULL;
2014f248cffSSven Eckelmann struct batadv_gw_node *next_gw = NULL;
20256303d34SSven Eckelmann struct batadv_neigh_node *router = NULL;
20389652331SSimon Wunderlich struct batadv_neigh_ifinfo *router_ifinfo = NULL;
20419595e05SAntonio Quartulli char gw_addr[18] = { '\0' };
2052265c141SAntonio Quartulli
2063a24a63eSAntonio Quartulli if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
2072265c141SAntonio Quartulli goto out;
2082265c141SAntonio Quartulli
20934d99cfeSAntonio Quartulli if (!bat_priv->algo_ops->gw.get_best_gw_node)
21034d99cfeSAntonio Quartulli goto out;
21134d99cfeSAntonio Quartulli
2121409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2132265c141SAntonio Quartulli
214807736f6SSven Eckelmann if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
215caa0bf64SMarek Lindner goto out;
216caa0bf64SMarek Lindner
21734d99cfeSAntonio Quartulli /* if gw.reselect is set to 1 it means that a previous call to
21834d99cfeSAntonio Quartulli * gw.is_eligible() said that we have a new best GW, therefore it can
21934d99cfeSAntonio Quartulli * now be picked from the list and selected
22034d99cfeSAntonio Quartulli */
22134d99cfeSAntonio Quartulli next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
2222265c141SAntonio Quartulli
2232265c141SAntonio Quartulli if (curr_gw == next_gw)
2242265c141SAntonio Quartulli goto out;
2252265c141SAntonio Quartulli
2262265c141SAntonio Quartulli if (next_gw) {
22719595e05SAntonio Quartulli sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
22819595e05SAntonio Quartulli
2297351a482SSimon Wunderlich router = batadv_orig_router_get(next_gw->orig_node,
2307351a482SSimon Wunderlich BATADV_IF_DEFAULT);
2312265c141SAntonio Quartulli if (!router) {
2324e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv);
2332265c141SAntonio Quartulli goto out;
2342265c141SAntonio Quartulli }
23589652331SSimon Wunderlich
23689652331SSimon Wunderlich router_ifinfo = batadv_neigh_ifinfo_get(router,
23789652331SSimon Wunderlich BATADV_IF_DEFAULT);
23889652331SSimon Wunderlich if (!router_ifinfo) {
23989652331SSimon Wunderlich batadv_gw_reselect(bat_priv);
24089652331SSimon Wunderlich goto out;
24189652331SSimon Wunderlich }
2422265c141SAntonio Quartulli }
2432265c141SAntonio Quartulli
244825ffe1fSSven Eckelmann if (curr_gw && !next_gw) {
24539c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2462265c141SAntonio Quartulli "Removing selected gateway - no gateway in range\n");
24739c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
24839c75a51SSven Eckelmann NULL);
249825ffe1fSSven Eckelmann } else if (!curr_gw && next_gw) {
25039c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
251414254e3SMarek Lindner "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
2521eda58bfSSven Eckelmann next_gw->orig_node->orig,
253414254e3SMarek Lindner next_gw->bandwidth_down / 10,
254414254e3SMarek Lindner next_gw->bandwidth_down % 10,
255414254e3SMarek Lindner next_gw->bandwidth_up / 10,
25689652331SSimon Wunderlich next_gw->bandwidth_up % 10,
25789652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg);
25839c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
25939c75a51SSven Eckelmann gw_addr);
2602265c141SAntonio Quartulli } else {
26139c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
262414254e3SMarek Lindner "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
2631eda58bfSSven Eckelmann next_gw->orig_node->orig,
264414254e3SMarek Lindner next_gw->bandwidth_down / 10,
265414254e3SMarek Lindner next_gw->bandwidth_down % 10,
266414254e3SMarek Lindner next_gw->bandwidth_up / 10,
26789652331SSimon Wunderlich next_gw->bandwidth_up % 10,
26889652331SSimon Wunderlich router_ifinfo->bat_iv.tq_avg);
26939c75a51SSven Eckelmann batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
27039c75a51SSven Eckelmann gw_addr);
271c6c8fea2SSven Eckelmann }
272c6c8fea2SSven Eckelmann
2731409a834SSven Eckelmann batadv_gw_select(bat_priv, next_gw);
2742265c141SAntonio Quartulli
275c4aac1abSMarek Lindner out:
2763a01743dSSven Eckelmann batadv_gw_node_put(curr_gw);
2773a01743dSSven Eckelmann batadv_gw_node_put(next_gw);
27825bb2509SSven Eckelmann batadv_neigh_node_put(router);
279044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(router_ifinfo);
280c6c8fea2SSven Eckelmann }
281c6c8fea2SSven Eckelmann
282ff15c27cSSven Eckelmann /**
283ff15c27cSSven Eckelmann * batadv_gw_check_election() - Elect orig node as best gateway when eligible
284ff15c27cSSven Eckelmann * @bat_priv: the bat priv with all the soft interface information
285ff15c27cSSven Eckelmann * @orig_node: orig node which is to be checked
286ff15c27cSSven Eckelmann */
batadv_gw_check_election(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node)28756303d34SSven Eckelmann void batadv_gw_check_election(struct batadv_priv *bat_priv,
28856303d34SSven Eckelmann struct batadv_orig_node *orig_node)
289c6c8fea2SSven Eckelmann {
29056303d34SSven Eckelmann struct batadv_orig_node *curr_gw_orig;
29134d99cfeSAntonio Quartulli
29234d99cfeSAntonio Quartulli /* abort immediately if the routing algorithm does not support gateway
29334d99cfeSAntonio Quartulli * election
29434d99cfeSAntonio Quartulli */
29534d99cfeSAntonio Quartulli if (!bat_priv->algo_ops->gw.is_eligible)
29634d99cfeSAntonio Quartulli return;
297c6c8fea2SSven Eckelmann
2987cf06bc6SSven Eckelmann curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
29957f0c07cSLinus Lüssing if (!curr_gw_orig)
3004e820e72SAntonio Quartulli goto reselect;
301c6c8fea2SSven Eckelmann
302c6c8fea2SSven Eckelmann /* this node already is the gateway */
30357f0c07cSLinus Lüssing if (curr_gw_orig == orig_node)
304e1a5382fSLinus Lüssing goto out;
305c6c8fea2SSven Eckelmann
30634d99cfeSAntonio Quartulli if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
30734d99cfeSAntonio Quartulli orig_node))
308e1a5382fSLinus Lüssing goto out;
309c6c8fea2SSven Eckelmann
3104e820e72SAntonio Quartulli reselect:
3114e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv);
3125d02b3cdSLinus Lüssing out:
3135d967310SSven Eckelmann batadv_orig_node_put(curr_gw_orig);
314c6c8fea2SSven Eckelmann }
315c6c8fea2SSven Eckelmann
316414254e3SMarek Lindner /**
3177e9a8c2cSSven Eckelmann * batadv_gw_node_add() - add gateway node to list of available gateways
318414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information
319414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities
320414254e3SMarek Lindner * @gateway: announced bandwidth information
321dff9bc42SSven Eckelmann *
322dff9bc42SSven Eckelmann * Has to be called with the appropriate locks being acquired
323dff9bc42SSven Eckelmann * (gw.list_lock).
324414254e3SMarek Lindner */
batadv_gw_node_add(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,struct batadv_tvlv_gateway_data * gateway)32556303d34SSven Eckelmann static void batadv_gw_node_add(struct batadv_priv *bat_priv,
32656303d34SSven Eckelmann struct batadv_orig_node *orig_node,
327414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway)
328c6c8fea2SSven Eckelmann {
32956303d34SSven Eckelmann struct batadv_gw_node *gw_node;
330414254e3SMarek Lindner
331dff9bc42SSven Eckelmann lockdep_assert_held(&bat_priv->gw.list_lock);
332dff9bc42SSven Eckelmann
333414254e3SMarek Lindner if (gateway->bandwidth_down == 0)
334414254e3SMarek Lindner return;
335c6c8fea2SSven Eckelmann
336377fe0f9SAntonio Quartulli gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
337c3ba37a7SSven Eckelmann if (!gw_node)
338377fe0f9SAntonio Quartulli return;
339377fe0f9SAntonio Quartulli
340f665fa7eSSven Eckelmann kref_init(&gw_node->refcount);
341c6c8fea2SSven Eckelmann INIT_HLIST_NODE(&gw_node->list);
34255db2d59SSven Eckelmann kref_get(&orig_node->refcount);
343c6c8fea2SSven Eckelmann gw_node->orig_node = orig_node;
34427a4d5efSSimon Wunderlich gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
34527a4d5efSSimon Wunderlich gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
346c6c8fea2SSven Eckelmann
347f665fa7eSSven Eckelmann kref_get(&gw_node->refcount);
34870ea5ceeSSven Eckelmann hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
3499264c85cSSven Eckelmann bat_priv->gw.generation++;
350c6c8fea2SSven Eckelmann
35139c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
352414254e3SMarek Lindner "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
353414254e3SMarek Lindner orig_node->orig,
354414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10,
355414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10,
356414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10,
357414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10);
358f665fa7eSSven Eckelmann
359f665fa7eSSven Eckelmann /* don't return reference to new gw_node */
360f665fa7eSSven Eckelmann batadv_gw_node_put(gw_node);
361c6c8fea2SSven Eckelmann }
362c6c8fea2SSven Eckelmann
363414254e3SMarek Lindner /**
3647e9a8c2cSSven Eckelmann * batadv_gw_node_get() - retrieve gateway node from list of available gateways
365414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information
366414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities
367414254e3SMarek Lindner *
36862fe710fSSven Eckelmann * Return: gateway node if found or NULL otherwise.
36971e4aa9cSAntonio Quartulli */
batadv_gw_node_get(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node)37050164d8fSAntonio Quartulli struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
371414254e3SMarek Lindner struct batadv_orig_node *orig_node)
372414254e3SMarek Lindner {
373414254e3SMarek Lindner struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
374c6c8fea2SSven Eckelmann
375c6c8fea2SSven Eckelmann rcu_read_lock();
37670ea5ceeSSven Eckelmann hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list,
37770ea5ceeSSven Eckelmann list) {
378414254e3SMarek Lindner if (gw_node_tmp->orig_node != orig_node)
379c6c8fea2SSven Eckelmann continue;
380c6c8fea2SSven Eckelmann
381e7aed321SSven Eckelmann if (!kref_get_unless_zero(&gw_node_tmp->refcount))
382414254e3SMarek Lindner continue;
383414254e3SMarek Lindner
384414254e3SMarek Lindner gw_node = gw_node_tmp;
385414254e3SMarek Lindner break;
386414254e3SMarek Lindner }
387414254e3SMarek Lindner rcu_read_unlock();
388414254e3SMarek Lindner
389414254e3SMarek Lindner return gw_node;
390414254e3SMarek Lindner }
391414254e3SMarek Lindner
392414254e3SMarek Lindner /**
3937e9a8c2cSSven Eckelmann * batadv_gw_node_update() - update list of available gateways with changed
394414254e3SMarek Lindner * bandwidth information
395414254e3SMarek Lindner * @bat_priv: the bat priv with all the soft interface information
396414254e3SMarek Lindner * @orig_node: originator announcing gateway capabilities
397414254e3SMarek Lindner * @gateway: announced bandwidth information
398414254e3SMarek Lindner */
batadv_gw_node_update(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,struct batadv_tvlv_gateway_data * gateway)399414254e3SMarek Lindner void batadv_gw_node_update(struct batadv_priv *bat_priv,
400414254e3SMarek Lindner struct batadv_orig_node *orig_node,
401414254e3SMarek Lindner struct batadv_tvlv_gateway_data *gateway)
402414254e3SMarek Lindner {
403414254e3SMarek Lindner struct batadv_gw_node *gw_node, *curr_gw = NULL;
404414254e3SMarek Lindner
405dff9bc42SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock);
406414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_node);
407414254e3SMarek Lindner if (!gw_node) {
408414254e3SMarek Lindner batadv_gw_node_add(bat_priv, orig_node, gateway);
409dff9bc42SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock);
410414254e3SMarek Lindner goto out;
411414254e3SMarek Lindner }
412dff9bc42SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock);
413414254e3SMarek Lindner
414825ffe1fSSven Eckelmann if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) &&
415825ffe1fSSven Eckelmann gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))
416414254e3SMarek Lindner goto out;
417414254e3SMarek Lindner
41839c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
419414254e3SMarek Lindner "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
420414254e3SMarek Lindner orig_node->orig,
421414254e3SMarek Lindner gw_node->bandwidth_down / 10,
422414254e3SMarek Lindner gw_node->bandwidth_down % 10,
423414254e3SMarek Lindner gw_node->bandwidth_up / 10,
424414254e3SMarek Lindner gw_node->bandwidth_up % 10,
425414254e3SMarek Lindner ntohl(gateway->bandwidth_down) / 10,
426414254e3SMarek Lindner ntohl(gateway->bandwidth_down) % 10,
427414254e3SMarek Lindner ntohl(gateway->bandwidth_up) / 10,
428414254e3SMarek Lindner ntohl(gateway->bandwidth_up) % 10);
429414254e3SMarek Lindner
430414254e3SMarek Lindner gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
431414254e3SMarek Lindner gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
432c6c8fea2SSven Eckelmann
433414254e3SMarek Lindner if (ntohl(gateway->bandwidth_down) == 0) {
43439c75a51SSven Eckelmann batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
435c6c8fea2SSven Eckelmann "Gateway %pM removed from gateway list\n",
436c6c8fea2SSven Eckelmann orig_node->orig);
437c6c8fea2SSven Eckelmann
438414254e3SMarek Lindner /* Note: We don't need a NULL check here, since curr_gw never
439414254e3SMarek Lindner * gets dereferenced.
440414254e3SMarek Lindner */
441bd3524c1SSimon Wunderlich spin_lock_bh(&bat_priv->gw.list_lock);
442c18bdd01SSven Eckelmann if (!hlist_unhashed(&gw_node->list)) {
443bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list);
4443a01743dSSven Eckelmann batadv_gw_node_put(gw_node);
4459264c85cSSven Eckelmann bat_priv->gw.generation++;
446c18bdd01SSven Eckelmann }
447c18bdd01SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock);
448bd3524c1SSimon Wunderlich
449414254e3SMarek Lindner curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
450c4aac1abSMarek Lindner if (gw_node == curr_gw)
4514e820e72SAntonio Quartulli batadv_gw_reselect(bat_priv);
452bd3524c1SSimon Wunderlich
4533a01743dSSven Eckelmann batadv_gw_node_put(curr_gw);
454414254e3SMarek Lindner }
45571e4aa9cSAntonio Quartulli
456414254e3SMarek Lindner out:
4573a01743dSSven Eckelmann batadv_gw_node_put(gw_node);
458c6c8fea2SSven Eckelmann }
459c6c8fea2SSven Eckelmann
460ff15c27cSSven Eckelmann /**
461ff15c27cSSven Eckelmann * batadv_gw_node_delete() - Remove orig_node from gateway list
462ff15c27cSSven Eckelmann * @bat_priv: the bat priv with all the soft interface information
463ff15c27cSSven Eckelmann * @orig_node: orig node which is currently in process of being removed
464ff15c27cSSven Eckelmann */
batadv_gw_node_delete(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node)46556303d34SSven Eckelmann void batadv_gw_node_delete(struct batadv_priv *bat_priv,
46656303d34SSven Eckelmann struct batadv_orig_node *orig_node)
467c6c8fea2SSven Eckelmann {
468414254e3SMarek Lindner struct batadv_tvlv_gateway_data gateway;
469414254e3SMarek Lindner
470414254e3SMarek Lindner gateway.bandwidth_down = 0;
471414254e3SMarek Lindner gateway.bandwidth_up = 0;
472414254e3SMarek Lindner
473414254e3SMarek Lindner batadv_gw_node_update(bat_priv, orig_node, &gateway);
474c6c8fea2SSven Eckelmann }
475c6c8fea2SSven Eckelmann
476ff15c27cSSven Eckelmann /**
477ff15c27cSSven Eckelmann * batadv_gw_node_free() - Free gateway information from soft interface
478ff15c27cSSven Eckelmann * @bat_priv: the bat priv with all the soft interface information
479ff15c27cSSven Eckelmann */
batadv_gw_node_free(struct batadv_priv * bat_priv)480bd3524c1SSimon Wunderlich void batadv_gw_node_free(struct batadv_priv *bat_priv)
481c6c8fea2SSven Eckelmann {
482bd3524c1SSimon Wunderlich struct batadv_gw_node *gw_node;
483b67bfe0dSSasha Levin struct hlist_node *node_tmp;
484c6c8fea2SSven Eckelmann
485807736f6SSven Eckelmann spin_lock_bh(&bat_priv->gw.list_lock);
486b67bfe0dSSasha Levin hlist_for_each_entry_safe(gw_node, node_tmp,
48770ea5ceeSSven Eckelmann &bat_priv->gw.gateway_list, list) {
488bd3524c1SSimon Wunderlich hlist_del_init_rcu(&gw_node->list);
4893a01743dSSven Eckelmann batadv_gw_node_put(gw_node);
4909264c85cSSven Eckelmann bat_priv->gw.generation++;
491c6c8fea2SSven Eckelmann }
492807736f6SSven Eckelmann spin_unlock_bh(&bat_priv->gw.list_lock);
493c6c8fea2SSven Eckelmann }
494c6c8fea2SSven Eckelmann
4956c413b1cSAntonio Quartulli /**
4967e9a8c2cSSven Eckelmann * batadv_gw_dump() - Dump gateways into a message
497d7129dafSSven Eckelmann * @msg: Netlink message to dump into
498d7129dafSSven Eckelmann * @cb: Control block containing additional options
499d7129dafSSven Eckelmann *
500d7129dafSSven Eckelmann * Return: Error code, or length of message
501d7129dafSSven Eckelmann */
batadv_gw_dump(struct sk_buff * msg,struct netlink_callback * cb)502d7129dafSSven Eckelmann int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
503d7129dafSSven Eckelmann {
504d7129dafSSven Eckelmann struct batadv_hard_iface *primary_if = NULL;
505d7129dafSSven Eckelmann struct net *net = sock_net(cb->skb->sk);
506d7129dafSSven Eckelmann struct net_device *soft_iface;
507d7129dafSSven Eckelmann struct batadv_priv *bat_priv;
508d7129dafSSven Eckelmann int ifindex;
509d7129dafSSven Eckelmann int ret;
510d7129dafSSven Eckelmann
511d7129dafSSven Eckelmann ifindex = batadv_netlink_get_ifindex(cb->nlh,
512d7129dafSSven Eckelmann BATADV_ATTR_MESH_IFINDEX);
513d7129dafSSven Eckelmann if (!ifindex)
514d7129dafSSven Eckelmann return -EINVAL;
515d7129dafSSven Eckelmann
516d7129dafSSven Eckelmann soft_iface = dev_get_by_index(net, ifindex);
517d7129dafSSven Eckelmann if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
518d7129dafSSven Eckelmann ret = -ENODEV;
519d7129dafSSven Eckelmann goto out;
520d7129dafSSven Eckelmann }
521d7129dafSSven Eckelmann
522d7129dafSSven Eckelmann bat_priv = netdev_priv(soft_iface);
523d7129dafSSven Eckelmann
524d7129dafSSven Eckelmann primary_if = batadv_primary_if_get_selected(bat_priv);
525d7129dafSSven Eckelmann if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
526d7129dafSSven Eckelmann ret = -ENOENT;
527d7129dafSSven Eckelmann goto out;
528d7129dafSSven Eckelmann }
529d7129dafSSven Eckelmann
530d7129dafSSven Eckelmann if (!bat_priv->algo_ops->gw.dump) {
531d7129dafSSven Eckelmann ret = -EOPNOTSUPP;
532d7129dafSSven Eckelmann goto out;
533d7129dafSSven Eckelmann }
534d7129dafSSven Eckelmann
535d7129dafSSven Eckelmann bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
536d7129dafSSven Eckelmann
537d7129dafSSven Eckelmann ret = msg->len;
538d7129dafSSven Eckelmann
539d7129dafSSven Eckelmann out:
540d7129dafSSven Eckelmann batadv_hardif_put(primary_if);
541d7129dafSSven Eckelmann dev_put(soft_iface);
542d7129dafSSven Eckelmann
543d7129dafSSven Eckelmann return ret;
544d7129dafSSven Eckelmann }
545d7129dafSSven Eckelmann
546d7129dafSSven Eckelmann /**
5477e9a8c2cSSven Eckelmann * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message
5486c413b1cSAntonio Quartulli * @skb: the packet to check
5496c413b1cSAntonio Quartulli * @header_len: a pointer to the batman-adv header size
5506c413b1cSAntonio Quartulli * @chaddr: buffer where the client address will be stored. Valid
5516c413b1cSAntonio Quartulli * only if the function returns BATADV_DHCP_TO_CLIENT
5526c413b1cSAntonio Quartulli *
55362fe710fSSven Eckelmann * This function may re-allocate the data buffer of the skb passed as argument.
55462fe710fSSven Eckelmann *
55562fe710fSSven Eckelmann * Return:
5566c413b1cSAntonio Quartulli * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
5576c413b1cSAntonio Quartulli * while parsing it
5586c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
5596c413b1cSAntonio Quartulli * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
5609cfc7bd6SSven Eckelmann */
5616c413b1cSAntonio Quartulli enum batadv_dhcp_recipient
batadv_gw_dhcp_recipient_get(struct sk_buff * skb,unsigned int * header_len,u8 * chaddr)5626c413b1cSAntonio Quartulli batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
5636b5e971aSSven Eckelmann u8 *chaddr)
564c6c8fea2SSven Eckelmann {
5656c413b1cSAntonio Quartulli enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
566c6c8fea2SSven Eckelmann struct ethhdr *ethhdr;
567c6c8fea2SSven Eckelmann struct iphdr *iphdr;
568c6c8fea2SSven Eckelmann struct ipv6hdr *ipv6hdr;
569c6c8fea2SSven Eckelmann struct udphdr *udphdr;
570f7f8ed56SAntonio Quartulli struct vlan_ethhdr *vhdr;
5716c413b1cSAntonio Quartulli int chaddr_offset;
572f7f8ed56SAntonio Quartulli __be16 proto;
5736b5e971aSSven Eckelmann u8 *p;
574c6c8fea2SSven Eckelmann
575c6c8fea2SSven Eckelmann /* check for ethernet header */
576be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
5776c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
5786c413b1cSAntonio Quartulli
579927c2ed7SLinus Lüssing ethhdr = eth_hdr(skb);
580f7f8ed56SAntonio Quartulli proto = ethhdr->h_proto;
581be7af5cfSMarek Lindner *header_len += ETH_HLEN;
582c6c8fea2SSven Eckelmann
583c6c8fea2SSven Eckelmann /* check for initial vlan header */
584f7f8ed56SAntonio Quartulli if (proto == htons(ETH_P_8021Q)) {
585be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
5866c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
587f7f8ed56SAntonio Quartulli
588927c2ed7SLinus Lüssing vhdr = vlan_eth_hdr(skb);
589f7f8ed56SAntonio Quartulli proto = vhdr->h_vlan_encapsulated_proto;
590be7af5cfSMarek Lindner *header_len += VLAN_HLEN;
591c6c8fea2SSven Eckelmann }
592c6c8fea2SSven Eckelmann
593c6c8fea2SSven Eckelmann /* check for ip header */
594f7f8ed56SAntonio Quartulli switch (proto) {
595f7f8ed56SAntonio Quartulli case htons(ETH_P_IP):
596be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
5976c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
5986c413b1cSAntonio Quartulli
599be7af5cfSMarek Lindner iphdr = (struct iphdr *)(skb->data + *header_len);
600be7af5cfSMarek Lindner *header_len += iphdr->ihl * 4;
601c6c8fea2SSven Eckelmann
602c6c8fea2SSven Eckelmann /* check for udp header */
603c6c8fea2SSven Eckelmann if (iphdr->protocol != IPPROTO_UDP)
6046c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
605c6c8fea2SSven Eckelmann
606c6c8fea2SSven Eckelmann break;
607f7f8ed56SAntonio Quartulli case htons(ETH_P_IPV6):
608be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
6096c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
6106c413b1cSAntonio Quartulli
611be7af5cfSMarek Lindner ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
612be7af5cfSMarek Lindner *header_len += sizeof(*ipv6hdr);
613c6c8fea2SSven Eckelmann
614c6c8fea2SSven Eckelmann /* check for udp header */
615c6c8fea2SSven Eckelmann if (ipv6hdr->nexthdr != IPPROTO_UDP)
6166c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
617c6c8fea2SSven Eckelmann
618c6c8fea2SSven Eckelmann break;
619c6c8fea2SSven Eckelmann default:
6206c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
621c6c8fea2SSven Eckelmann }
622c6c8fea2SSven Eckelmann
623be7af5cfSMarek Lindner if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
6246c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
6259d2c9488SLinus Lüssing
626be7af5cfSMarek Lindner udphdr = (struct udphdr *)(skb->data + *header_len);
627be7af5cfSMarek Lindner *header_len += sizeof(*udphdr);
628c6c8fea2SSven Eckelmann
629c6c8fea2SSven Eckelmann /* check for bootp port */
6306c413b1cSAntonio Quartulli switch (proto) {
6316c413b1cSAntonio Quartulli case htons(ETH_P_IP):
6326c413b1cSAntonio Quartulli if (udphdr->dest == htons(67))
6336c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER;
6346c413b1cSAntonio Quartulli else if (udphdr->source == htons(67))
6356c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT;
6366c413b1cSAntonio Quartulli break;
6376c413b1cSAntonio Quartulli case htons(ETH_P_IPV6):
6386c413b1cSAntonio Quartulli if (udphdr->dest == htons(547))
6396c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_SERVER;
6406c413b1cSAntonio Quartulli else if (udphdr->source == htons(547))
6416c413b1cSAntonio Quartulli ret = BATADV_DHCP_TO_CLIENT;
6426c413b1cSAntonio Quartulli break;
643be7af5cfSMarek Lindner }
644c6c8fea2SSven Eckelmann
6456c413b1cSAntonio Quartulli chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
6466c413b1cSAntonio Quartulli /* store the client address if the message is going to a client */
647303216e7SSven Eckelmann if (ret == BATADV_DHCP_TO_CLIENT) {
648303216e7SSven Eckelmann if (!pskb_may_pull(skb, chaddr_offset + ETH_ALEN))
649303216e7SSven Eckelmann return BATADV_DHCP_NO;
650303216e7SSven Eckelmann
6516c413b1cSAntonio Quartulli /* check if the DHCP packet carries an Ethernet DHCP */
6526c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
6536c413b1cSAntonio Quartulli if (*p != BATADV_DHCP_HTYPE_ETHERNET)
6546c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
6556c413b1cSAntonio Quartulli
6566c413b1cSAntonio Quartulli /* check if the DHCP packet carries a valid Ethernet address */
6576c413b1cSAntonio Quartulli p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
6586c413b1cSAntonio Quartulli if (*p != ETH_ALEN)
6596c413b1cSAntonio Quartulli return BATADV_DHCP_NO;
6606c413b1cSAntonio Quartulli
6618fdd0153SAntonio Quartulli ether_addr_copy(chaddr, skb->data + chaddr_offset);
6626c413b1cSAntonio Quartulli }
6636c413b1cSAntonio Quartulli
6646c413b1cSAntonio Quartulli return ret;
6656c413b1cSAntonio Quartulli }
666aa143d28SAntonio Quartulli
667bbb877edSAntonio Quartulli /**
6687e9a8c2cSSven Eckelmann * batadv_gw_out_of_range() - check if the dhcp request destination is the best
6697e9a8c2cSSven Eckelmann * gateway
670bbb877edSAntonio Quartulli * @bat_priv: the bat priv with all the soft interface information
671bbb877edSAntonio Quartulli * @skb: the outgoing packet
672bbb877edSAntonio Quartulli *
673bbb877edSAntonio Quartulli * Check if the skb is a DHCP request and if it is sent to the current best GW
674bbb877edSAntonio Quartulli * server. Due to topology changes it may be the case that the GW server
675bbb877edSAntonio Quartulli * previously selected is not the best one anymore.
676bbb877edSAntonio Quartulli *
677bbb877edSAntonio Quartulli * This call might reallocate skb data.
6786c413b1cSAntonio Quartulli * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
67962fe710fSSven Eckelmann *
68062fe710fSSven Eckelmann * Return: true if the packet destination is unicast and it is not the best gw,
68162fe710fSSven Eckelmann * false otherwise.
682bbb877edSAntonio Quartulli */
batadv_gw_out_of_range(struct batadv_priv * bat_priv,struct sk_buff * skb)68356303d34SSven Eckelmann bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
6849d2c9488SLinus Lüssing struct sk_buff *skb)
685be7af5cfSMarek Lindner {
6864f248cffSSven Eckelmann struct batadv_neigh_node *neigh_curr = NULL;
6874f248cffSSven Eckelmann struct batadv_neigh_node *neigh_old = NULL;
688a752c0a4SLinus Lüssing struct batadv_orig_node *orig_dst_node = NULL;
6894f248cffSSven Eckelmann struct batadv_gw_node *gw_node = NULL;
6904f248cffSSven Eckelmann struct batadv_gw_node *curr_gw = NULL;
69189652331SSimon Wunderlich struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
6926c413b1cSAntonio Quartulli struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
6936c413b1cSAntonio Quartulli bool out_of_range = false;
6946b5e971aSSven Eckelmann u8 curr_tq_avg;
695bbb877edSAntonio Quartulli unsigned short vid;
696bbb877edSAntonio Quartulli
697bbb877edSAntonio Quartulli vid = batadv_get_vid(skb, 0);
698be7af5cfSMarek Lindner
699a752c0a4SLinus Lüssing if (is_multicast_ether_addr(ethhdr->h_dest))
700a752c0a4SLinus Lüssing goto out;
701a752c0a4SLinus Lüssing
70208c36d3eSSven Eckelmann orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
703bbb877edSAntonio Quartulli ethhdr->h_dest, vid);
704be7af5cfSMarek Lindner if (!orig_dst_node)
705be7af5cfSMarek Lindner goto out;
706be7af5cfSMarek Lindner
707414254e3SMarek Lindner gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
7080d164491SAntonio Quartulli if (!gw_node)
709be7af5cfSMarek Lindner goto out;
710be7af5cfSMarek Lindner
7113a24a63eSAntonio Quartulli switch (atomic_read(&bat_priv->gw.mode)) {
712cd646ab1SSven Eckelmann case BATADV_GW_MODE_SERVER:
713be7af5cfSMarek Lindner /* If we are a GW then we are our best GW. We can artificially
7149cfc7bd6SSven Eckelmann * set the tq towards ourself as the maximum value
7159cfc7bd6SSven Eckelmann */
71642d0b044SSven Eckelmann curr_tq_avg = BATADV_TQ_MAX_VALUE;
717be7af5cfSMarek Lindner break;
718cd646ab1SSven Eckelmann case BATADV_GW_MODE_CLIENT:
7191409a834SSven Eckelmann curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
720c4aac1abSMarek Lindner if (!curr_gw)
721be7af5cfSMarek Lindner goto out;
722c6c8fea2SSven Eckelmann
723be7af5cfSMarek Lindner /* packet is going to our gateway */
724be7af5cfSMarek Lindner if (curr_gw->orig_node == orig_dst_node)
725be7af5cfSMarek Lindner goto out;
726be7af5cfSMarek Lindner
72743676ab5SAntonio Quartulli /* If the dhcp packet has been sent to a different gw,
72843676ab5SAntonio Quartulli * we have to evaluate whether the old gw is still
7299cfc7bd6SSven Eckelmann * reliable enough
7309cfc7bd6SSven Eckelmann */
73130d3c511SSven Eckelmann neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
73230d3c511SSven Eckelmann NULL);
733be7af5cfSMarek Lindner if (!neigh_curr)
734be7af5cfSMarek Lindner goto out;
735be7af5cfSMarek Lindner
73689652331SSimon Wunderlich curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
73789652331SSimon Wunderlich BATADV_IF_DEFAULT);
73889652331SSimon Wunderlich if (!curr_ifinfo)
73989652331SSimon Wunderlich goto out;
74089652331SSimon Wunderlich
74189652331SSimon Wunderlich curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
742044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(curr_ifinfo);
74389652331SSimon Wunderlich
744be7af5cfSMarek Lindner break;
745cd646ab1SSven Eckelmann case BATADV_GW_MODE_OFF:
746be7af5cfSMarek Lindner default:
747be7af5cfSMarek Lindner goto out;
74843676ab5SAntonio Quartulli }
749be7af5cfSMarek Lindner
75030d3c511SSven Eckelmann neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
7512ef04f47SDan Carpenter if (!neigh_old)
752be7af5cfSMarek Lindner goto out;
753be7af5cfSMarek Lindner
75489652331SSimon Wunderlich old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
75589652331SSimon Wunderlich if (!old_ifinfo)
75689652331SSimon Wunderlich goto out;
75789652331SSimon Wunderlich
75889652331SSimon Wunderlich if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
759be7af5cfSMarek Lindner out_of_range = true;
760044fa3aeSSven Eckelmann batadv_neigh_ifinfo_put(old_ifinfo);
761be7af5cfSMarek Lindner
762be7af5cfSMarek Lindner out:
7635d967310SSven Eckelmann batadv_orig_node_put(orig_dst_node);
7643a01743dSSven Eckelmann batadv_gw_node_put(curr_gw);
7653a01743dSSven Eckelmann batadv_gw_node_put(gw_node);
76625bb2509SSven Eckelmann batadv_neigh_node_put(neigh_old);
76725bb2509SSven Eckelmann batadv_neigh_node_put(neigh_curr);
768be7af5cfSMarek Lindner return out_of_range;
769c6c8fea2SSven Eckelmann }
770