blob: 10d521f0b17f6faaaeae50cfb2a624cb86be93dd [file] [log] [blame]
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01001/* Copyright (C) 2009-2017 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/byteorder/generic.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020023#include <linux/errno.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020024#include <linux/etherdevice.h>
25#include <linux/fs.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
28#include <linux/in.h>
29#include <linux/ip.h>
30#include <linux/ipv6.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/kernel.h>
Sven Eckelmanne7aed322016-01-16 10:29:41 +010032#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020033#include <linux/list.h>
34#include <linux/netdevice.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020035#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020036#include <linux/rculist.h>
37#include <linux/rcupdate.h>
38#include <linux/seq_file.h>
39#include <linux/skbuff.h>
40#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/stddef.h>
43#include <linux/udp.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020044#include <net/sock.h>
45#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020046
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047#include "gateway_common.h"
48#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020049#include "log.h"
Sven Eckelmannd7129da2016-07-03 13:31:42 +020050#include "netlink.h"
Linus Lüssing57f0c072011-03-14 22:43:33 +000051#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020052#include "packet.h"
Antonio Quartulli43676ab2011-04-26 21:31:45 +020053#include "routing.h"
Sven Eckelmannd7129da2016-07-03 13:31:42 +020054#include "soft-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020055#include "sysfs.h"
56#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057
Antonio Quartulli6c413b12013-11-05 19:31:08 +010058/* These are the offsets of the "hw type" and "hw address length" in the dhcp
59 * packet starting at the beginning of the dhcp header
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020060 */
Antonio Quartulli6c413b12013-11-05 19:31:08 +010061#define BATADV_DHCP_HTYPE_OFFSET 1
62#define BATADV_DHCP_HLEN_OFFSET 2
63/* Value of htype representing Ethernet */
64#define BATADV_DHCP_HTYPE_ETHERNET 0x01
65/* This is the offset of the "chaddr" field in the dhcp packet starting at the
66 * beginning of the dhcp header
67 */
68#define BATADV_DHCP_CHADDR_OFFSET 28
Antonio Quartulli43676ab2011-04-26 21:31:45 +020069
Sven Eckelmanne7aed322016-01-16 10:29:41 +010070/**
71 * batadv_gw_node_release - release gw_node from lists and queue for free after
72 * rcu grace period
73 * @ref: kref pointer of the gw_node
74 */
75static void batadv_gw_node_release(struct kref *ref)
76{
77 struct batadv_gw_node *gw_node;
78
79 gw_node = container_of(ref, struct batadv_gw_node, refcount);
80
Sven Eckelmann5d967312016-01-17 11:01:09 +010081 batadv_orig_node_put(gw_node->orig_node);
Sven Eckelmanne7aed322016-01-16 10:29:41 +010082 kfree_rcu(gw_node, rcu);
83}
84
85/**
Sven Eckelmann3a017432016-01-17 11:01:18 +010086 * batadv_gw_node_put - decrement the gw_node refcounter and possibly release it
Sven Eckelmanne7aed322016-01-16 10:29:41 +010087 * @gw_node: gateway node to free
88 */
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020089void batadv_gw_node_put(struct batadv_gw_node *gw_node)
Marek Lindner25b6d3c2011-02-10 14:33:49 +000090{
Sven Eckelmanne7aed322016-01-16 10:29:41 +010091 kref_put(&gw_node->refcount, batadv_gw_node_release);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000092}
93
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020094struct batadv_gw_node *
Sven Eckelmann56303d32012-06-05 22:31:31 +020095batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000096{
Sven Eckelmann56303d32012-06-05 22:31:31 +020097 struct batadv_gw_node *gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000099 rcu_read_lock();
Sven Eckelmann807736f2012-07-15 22:26:51 +0200100 gw_node = rcu_dereference(bat_priv->gw.curr_gw);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100101 if (!gw_node)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000102 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000103
Sven Eckelmanne7aed322016-01-16 10:29:41 +0100104 if (!kref_get_unless_zero(&gw_node->refcount))
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100105 gw_node = NULL;
106
107out:
108 rcu_read_unlock();
109 return gw_node;
110}
111
Sven Eckelmann56303d32012-06-05 22:31:31 +0200112struct batadv_orig_node *
113batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100114{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200115 struct batadv_gw_node *gw_node;
116 struct batadv_orig_node *orig_node = NULL;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100117
Sven Eckelmann1409a832012-05-12 18:33:55 +0200118 gw_node = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100119 if (!gw_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000120 goto out;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000121
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100122 rcu_read_lock();
123 orig_node = gw_node->orig_node;
124 if (!orig_node)
125 goto unlock;
126
Sven Eckelmann7c124392016-01-16 10:29:56 +0100127 if (!kref_get_unless_zero(&orig_node->refcount))
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000128 orig_node = NULL;
Linus Lüssing43c70ad2011-02-13 21:13:04 +0000129
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100130unlock:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000131 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100132out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100134 batadv_gw_node_put(gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100135 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136}
137
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138static void batadv_gw_select(struct batadv_priv *bat_priv,
139 struct batadv_gw_node *new_gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200141 struct batadv_gw_node *curr_gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000142
Sven Eckelmann807736f2012-07-15 22:26:51 +0200143 spin_lock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100144
Sven Eckelmanna08d4972016-03-05 16:09:22 +0100145 if (new_gw_node)
146 kref_get(&new_gw_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000147
Sven Eckelmann807736f2012-07-15 22:26:51 +0200148 curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
149 rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000150
151 if (curr_gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100152 batadv_gw_node_put(curr_gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100153
Sven Eckelmann807736f2012-07-15 22:26:51 +0200154 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100155}
156
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100157/**
158 * batadv_gw_reselect - force a gateway reselection
159 * @bat_priv: the bat priv with all the soft interface information
160 *
161 * Set a flag to remind the GW component to perform a new gateway reselection.
162 * However this function does not ensure that the current gateway is going to be
163 * deselected. The reselection mechanism may elect the same gateway once again.
164 *
165 * This means that invoking batadv_gw_reselect() does not guarantee a gateway
166 * change and therefore a uevent is not necessarily expected.
167 */
168void batadv_gw_reselect(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100169{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200170 atomic_set(&bat_priv->gw.reselect, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000171}
172
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200173/**
174 * batadv_gw_check_client_stop - check if client mode has been switched off
175 * @bat_priv: the bat priv with all the soft interface information
176 *
177 * This function assumes the caller has checked that the gw state *is actually
178 * changing*. This function is not supposed to be called when there is no state
179 * change.
180 */
181void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
182{
183 struct batadv_gw_node *curr_gw;
184
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800185 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200186 return;
187
188 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
189 if (!curr_gw)
190 return;
191
Antonio Quartullif3163182013-11-04 20:59:40 +0100192 /* deselect the current gateway so that next time that client mode is
193 * enabled a proper GW_ADD event can be sent
194 */
195 batadv_gw_select(bat_priv, NULL);
196
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200197 /* if batman-adv is switching the gw client mode off and a gateway was
198 * already selected, send a DEL uevent
199 */
200 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
201
Sven Eckelmann3a017432016-01-17 11:01:18 +0100202 batadv_gw_node_put(curr_gw);
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200203}
204
Sven Eckelmann56303d32012-06-05 22:31:31 +0200205void batadv_gw_election(struct batadv_priv *bat_priv)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200206{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200207 struct batadv_gw_node *curr_gw = NULL;
208 struct batadv_gw_node *next_gw = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200209 struct batadv_neigh_node *router = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100210 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Antonio Quartulli19595e02011-06-12 11:58:58 +0200211 char gw_addr[18] = { '\0' };
Antonio Quartulli2265c142011-04-27 00:22:00 +0200212
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800213 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200214 goto out;
215
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200216 if (!bat_priv->algo_ops->gw.get_best_gw_node)
217 goto out;
218
Sven Eckelmann1409a832012-05-12 18:33:55 +0200219 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200220
Sven Eckelmann807736f2012-07-15 22:26:51 +0200221 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
Marek Lindnercaa0bf62012-08-04 04:13:26 +0000222 goto out;
223
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200224 /* if gw.reselect is set to 1 it means that a previous call to
225 * gw.is_eligible() said that we have a new best GW, therefore it can
226 * now be picked from the list and selected
227 */
228 next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200229
230 if (curr_gw == next_gw)
231 goto out;
232
233 if (next_gw) {
Antonio Quartulli19595e02011-06-12 11:58:58 +0200234 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
235
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100236 router = batadv_orig_router_get(next_gw->orig_node,
237 BATADV_IF_DEFAULT);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200238 if (!router) {
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100239 batadv_gw_reselect(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200240 goto out;
241 }
Simon Wunderlich89652332013-11-13 19:14:46 +0100242
243 router_ifinfo = batadv_neigh_ifinfo_get(router,
244 BATADV_IF_DEFAULT);
245 if (!router_ifinfo) {
246 batadv_gw_reselect(bat_priv);
247 goto out;
248 }
Antonio Quartulli2265c142011-04-27 00:22:00 +0200249 }
250
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200251 if (curr_gw && !next_gw) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200252 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200253 "Removing selected gateway - no gateway in range\n");
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200254 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
255 NULL);
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200256 } else if (!curr_gw && next_gw) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200257 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800258 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200259 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800260 next_gw->bandwidth_down / 10,
261 next_gw->bandwidth_down % 10,
262 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100263 next_gw->bandwidth_up % 10,
264 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200265 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
266 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200267 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200268 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800269 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200270 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800271 next_gw->bandwidth_down / 10,
272 next_gw->bandwidth_down % 10,
273 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100274 next_gw->bandwidth_up % 10,
275 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200276 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
277 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200278 }
279
Sven Eckelmann1409a832012-05-12 18:33:55 +0200280 batadv_gw_select(bat_priv, next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200281
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100282out:
283 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100284 batadv_gw_node_put(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200285 if (next_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100286 batadv_gw_node_put(next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200287 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100288 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +0100289 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100290 batadv_neigh_ifinfo_put(router_ifinfo);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291}
292
Sven Eckelmann56303d32012-06-05 22:31:31 +0200293void batadv_gw_check_election(struct batadv_priv *bat_priv,
294 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200296 struct batadv_orig_node *curr_gw_orig;
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200297
298 /* abort immediately if the routing algorithm does not support gateway
299 * election
300 */
301 if (!bat_priv->algo_ops->gw.is_eligible)
302 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200304 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000305 if (!curr_gw_orig)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100306 goto reselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000309 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000310 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200312 if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
313 orig_node))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000314 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100316reselect:
317 batadv_gw_reselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000318out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000319 if (curr_gw_orig)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100320 batadv_orig_node_put(curr_gw_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321}
322
Marek Lindner414254e2013-04-23 21:39:58 +0800323/**
324 * batadv_gw_node_add - add gateway node to list of available gateways
325 * @bat_priv: the bat priv with all the soft interface information
326 * @orig_node: originator announcing gateway capabilities
327 * @gateway: announced bandwidth information
328 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200329static void batadv_gw_node_add(struct batadv_priv *bat_priv,
330 struct batadv_orig_node *orig_node,
Marek Lindner414254e2013-04-23 21:39:58 +0800331 struct batadv_tvlv_gateway_data *gateway)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200333 struct batadv_gw_node *gw_node;
Marek Lindner414254e2013-04-23 21:39:58 +0800334
335 if (gateway->bandwidth_down == 0)
336 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200338 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc3ba37a72016-03-05 16:09:23 +0100339 if (!gw_node)
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200340 return;
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200341
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200342 kref_init(&gw_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343 INIT_HLIST_NODE(&gw_node->list);
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200344 kref_get(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345 gw_node->orig_node = orig_node;
Simon Wunderlich27a4d5e2015-06-24 14:50:19 +0200346 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
347 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348
Sven Eckelmann807736f2012-07-15 22:26:51 +0200349 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200350 kref_get(&gw_node->refcount);
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +0200351 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200352 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200354 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800355 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
356 orig_node->orig,
357 ntohl(gateway->bandwidth_down) / 10,
358 ntohl(gateway->bandwidth_down) % 10,
359 ntohl(gateway->bandwidth_up) / 10,
360 ntohl(gateway->bandwidth_up) % 10);
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200361
362 /* don't return reference to new gw_node */
363 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364}
365
Marek Lindner414254e2013-04-23 21:39:58 +0800366/**
367 * batadv_gw_node_get - retrieve gateway node from list of available gateways
368 * @bat_priv: the bat priv with all the soft interface information
369 * @orig_node: originator announcing gateway capabilities
370 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200371 * Return: gateway node if found or NULL otherwise.
Marek Lindner414254e2013-04-23 21:39:58 +0800372 */
Antonio Quartulli50164d82016-07-03 12:46:34 +0200373struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
374 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375{
Marek Lindner414254e2013-04-23 21:39:58 +0800376 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
378 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +0200379 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list,
380 list) {
Marek Lindner414254e2013-04-23 21:39:58 +0800381 if (gw_node_tmp->orig_node != orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382 continue;
383
Sven Eckelmanne7aed322016-01-16 10:29:41 +0100384 if (!kref_get_unless_zero(&gw_node_tmp->refcount))
Marek Lindner414254e2013-04-23 21:39:58 +0800385 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386
Marek Lindner414254e2013-04-23 21:39:58 +0800387 gw_node = gw_node_tmp;
388 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389 }
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100390 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200391
Marek Lindner414254e2013-04-23 21:39:58 +0800392 return gw_node;
393}
394
395/**
396 * batadv_gw_node_update - update list of available gateways with changed
397 * bandwidth information
398 * @bat_priv: the bat priv with all the soft interface information
399 * @orig_node: originator announcing gateway capabilities
400 * @gateway: announced bandwidth information
401 */
402void batadv_gw_node_update(struct batadv_priv *bat_priv,
403 struct batadv_orig_node *orig_node,
404 struct batadv_tvlv_gateway_data *gateway)
405{
406 struct batadv_gw_node *gw_node, *curr_gw = NULL;
407
408 gw_node = batadv_gw_node_get(bat_priv, orig_node);
409 if (!gw_node) {
410 batadv_gw_node_add(bat_priv, orig_node, gateway);
411 goto out;
412 }
413
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200414 if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) &&
415 gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))
Marek Lindner414254e2013-04-23 21:39:58 +0800416 goto out;
417
418 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
419 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
420 orig_node->orig,
421 gw_node->bandwidth_down / 10,
422 gw_node->bandwidth_down % 10,
423 gw_node->bandwidth_up / 10,
424 gw_node->bandwidth_up % 10,
425 ntohl(gateway->bandwidth_down) / 10,
426 ntohl(gateway->bandwidth_down) % 10,
427 ntohl(gateway->bandwidth_up) / 10,
428 ntohl(gateway->bandwidth_up) % 10);
429
430 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
431 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
432
Marek Lindner414254e2013-04-23 21:39:58 +0800433 if (ntohl(gateway->bandwidth_down) == 0) {
Marek Lindner414254e2013-04-23 21:39:58 +0800434 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
435 "Gateway %pM removed from gateway list\n",
436 orig_node->orig);
437
438 /* Note: We don't need a NULL check here, since curr_gw never
439 * gets dereferenced.
440 */
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200441 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc18bdd02016-01-31 13:27:59 +0100442 if (!hlist_unhashed(&gw_node->list)) {
443 hlist_del_init_rcu(&gw_node->list);
Sven Eckelmann3a017432016-01-17 11:01:18 +0100444 batadv_gw_node_put(gw_node);
Sven Eckelmannc18bdd02016-01-31 13:27:59 +0100445 }
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200446 spin_unlock_bh(&bat_priv->gw.list_lock);
447
Marek Lindner414254e2013-04-23 21:39:58 +0800448 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
449 if (gw_node == curr_gw)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100450 batadv_gw_reselect(bat_priv);
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200451
452 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100453 batadv_gw_node_put(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800454 }
455
456out:
Marek Lindner414254e2013-04-23 21:39:58 +0800457 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100458 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459}
460
Sven Eckelmann56303d32012-06-05 22:31:31 +0200461void batadv_gw_node_delete(struct batadv_priv *bat_priv,
462 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463{
Marek Lindner414254e2013-04-23 21:39:58 +0800464 struct batadv_tvlv_gateway_data gateway;
465
466 gateway.bandwidth_down = 0;
467 gateway.bandwidth_up = 0;
468
469 batadv_gw_node_update(bat_priv, orig_node, &gateway);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470}
471
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200472void batadv_gw_node_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473{
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200474 struct batadv_gw_node *gw_node;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800475 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476
Sven Eckelmann807736f2012-07-15 22:26:51 +0200477 spin_lock_bh(&bat_priv->gw.list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800478 hlist_for_each_entry_safe(gw_node, node_tmp,
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +0200479 &bat_priv->gw.gateway_list, list) {
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200480 hlist_del_init_rcu(&gw_node->list);
Sven Eckelmann3a017432016-01-17 11:01:18 +0100481 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200483 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484}
485
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200486#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200487int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488{
489 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200490 struct batadv_priv *bat_priv = netdev_priv(net_dev);
491 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492
Marek Lindner30da63a2012-08-03 17:15:46 +0200493 primary_if = batadv_seq_print_text_primary_if_get(seq);
494 if (!primary_if)
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200495 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200497 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200498 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200499 primary_if->net_dev->dev_addr, net_dev->name,
500 bat_priv->algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200502 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200504 if (!bat_priv->algo_ops->gw.print) {
505 seq_puts(seq,
506 "No printing function for this routing protocol\n");
507 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200510 bat_priv->algo_ops->gw.print(bat_priv, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511
Marek Lindner30da63a2012-08-03 17:15:46 +0200512 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200514#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100516/**
Sven Eckelmannd7129da2016-07-03 13:31:42 +0200517 * batadv_gw_dump - Dump gateways into a message
518 * @msg: Netlink message to dump into
519 * @cb: Control block containing additional options
520 *
521 * Return: Error code, or length of message
522 */
523int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
524{
525 struct batadv_hard_iface *primary_if = NULL;
526 struct net *net = sock_net(cb->skb->sk);
527 struct net_device *soft_iface;
528 struct batadv_priv *bat_priv;
529 int ifindex;
530 int ret;
531
532 ifindex = batadv_netlink_get_ifindex(cb->nlh,
533 BATADV_ATTR_MESH_IFINDEX);
534 if (!ifindex)
535 return -EINVAL;
536
537 soft_iface = dev_get_by_index(net, ifindex);
538 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
539 ret = -ENODEV;
540 goto out;
541 }
542
543 bat_priv = netdev_priv(soft_iface);
544
545 primary_if = batadv_primary_if_get_selected(bat_priv);
546 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
547 ret = -ENOENT;
548 goto out;
549 }
550
551 if (!bat_priv->algo_ops->gw.dump) {
552 ret = -EOPNOTSUPP;
553 goto out;
554 }
555
556 bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
557
558 ret = msg->len;
559
560out:
561 if (primary_if)
562 batadv_hardif_put(primary_if);
563 if (soft_iface)
564 dev_put(soft_iface);
565
566 return ret;
567}
568
569/**
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100570 * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
571 * @skb: the packet to check
572 * @header_len: a pointer to the batman-adv header size
573 * @chaddr: buffer where the client address will be stored. Valid
574 * only if the function returns BATADV_DHCP_TO_CLIENT
575 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200576 * This function may re-allocate the data buffer of the skb passed as argument.
577 *
578 * Return:
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100579 * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
580 * while parsing it
581 * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
582 * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100583 */
584enum batadv_dhcp_recipient
585batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200586 u8 *chaddr)
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200587{
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100588 enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000589 struct ethhdr *ethhdr;
590 struct iphdr *iphdr;
591 struct ipv6hdr *ipv6hdr;
592 struct udphdr *udphdr;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200593 struct vlan_ethhdr *vhdr;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100594 int chaddr_offset;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200595 __be16 proto;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200596 u8 *p;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597
598 /* check for ethernet header */
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200599 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100600 return BATADV_DHCP_NO;
601
Linus Lüssing927c2ed2014-01-19 22:22:45 +0100602 ethhdr = eth_hdr(skb);
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200603 proto = ethhdr->h_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200604 *header_len += ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605
606 /* check for initial vlan header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200607 if (proto == htons(ETH_P_8021Q)) {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200608 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100609 return BATADV_DHCP_NO;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200610
Linus Lüssing927c2ed2014-01-19 22:22:45 +0100611 vhdr = vlan_eth_hdr(skb);
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200612 proto = vhdr->h_vlan_encapsulated_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200613 *header_len += VLAN_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614 }
615
616 /* check for ip header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200617 switch (proto) {
618 case htons(ETH_P_IP):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200619 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100620 return BATADV_DHCP_NO;
621
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200622 iphdr = (struct iphdr *)(skb->data + *header_len);
623 *header_len += iphdr->ihl * 4;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624
625 /* check for udp header */
626 if (iphdr->protocol != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100627 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628
629 break;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200630 case htons(ETH_P_IPV6):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200631 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100632 return BATADV_DHCP_NO;
633
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200634 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
635 *header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636
637 /* check for udp header */
638 if (ipv6hdr->nexthdr != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100639 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640
641 break;
642 default:
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100643 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644 }
645
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200646 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100647 return BATADV_DHCP_NO;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200648
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200649 udphdr = (struct udphdr *)(skb->data + *header_len);
650 *header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651
652 /* check for bootp port */
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100653 switch (proto) {
654 case htons(ETH_P_IP):
655 if (udphdr->dest == htons(67))
656 ret = BATADV_DHCP_TO_SERVER;
657 else if (udphdr->source == htons(67))
658 ret = BATADV_DHCP_TO_CLIENT;
659 break;
660 case htons(ETH_P_IPV6):
661 if (udphdr->dest == htons(547))
662 ret = BATADV_DHCP_TO_SERVER;
663 else if (udphdr->source == htons(547))
664 ret = BATADV_DHCP_TO_CLIENT;
665 break;
666 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100668 chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
669 /* store the client address if the message is going to a client */
670 if (ret == BATADV_DHCP_TO_CLIENT &&
671 pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
672 /* check if the DHCP packet carries an Ethernet DHCP */
673 p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
674 if (*p != BATADV_DHCP_HTYPE_ETHERNET)
675 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100677 /* check if the DHCP packet carries a valid Ethernet address */
678 p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
679 if (*p != ETH_ALEN)
680 return BATADV_DHCP_NO;
681
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100682 ether_addr_copy(chaddr, skb->data + chaddr_offset);
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100683 }
684
685 return ret;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200686}
Antonio Quartulliaa143d22014-09-01 14:37:27 +0200687
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200688/**
689 * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
690 * @bat_priv: the bat priv with all the soft interface information
691 * @skb: the outgoing packet
692 *
693 * Check if the skb is a DHCP request and if it is sent to the current best GW
694 * server. Due to topology changes it may be the case that the GW server
695 * previously selected is not the best one anymore.
696 *
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200697 * This call might reallocate skb data.
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100698 * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200699 *
700 * Return: true if the packet destination is unicast and it is not the best gw,
701 * false otherwise.
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200702 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200703bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200704 struct sk_buff *skb)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200705{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200706 struct batadv_neigh_node *neigh_curr = NULL;
707 struct batadv_neigh_node *neigh_old = NULL;
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200708 struct batadv_orig_node *orig_dst_node;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200709 struct batadv_gw_node *gw_node = NULL;
710 struct batadv_gw_node *curr_gw = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100711 struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100712 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
713 bool out_of_range = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200714 u8 curr_tq_avg;
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200715 unsigned short vid;
716
717 vid = batadv_get_vid(skb, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200719 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200720 ethhdr->h_dest, vid);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200721 if (!orig_dst_node)
722 goto out;
723
Marek Lindner414254e2013-04-23 21:39:58 +0800724 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
Antonio Quartulli0d164492014-12-20 13:48:57 +0100725 if (!gw_node)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200726 goto out;
727
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800728 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200729 case BATADV_GW_MODE_SERVER:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200730 /* If we are a GW then we are our best GW. We can artificially
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200731 * set the tq towards ourself as the maximum value
732 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200733 curr_tq_avg = BATADV_TQ_MAX_VALUE;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200734 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200735 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann1409a832012-05-12 18:33:55 +0200736 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200737 if (!curr_gw)
738 goto out;
739
740 /* packet is going to our gateway */
741 if (curr_gw->orig_node == orig_dst_node)
742 goto out;
743
744 /* If the dhcp packet has been sent to a different gw,
745 * we have to evaluate whether the old gw is still
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200746 * reliable enough
747 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200748 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
749 NULL);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200750 if (!neigh_curr)
751 goto out;
752
Simon Wunderlich89652332013-11-13 19:14:46 +0100753 curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
754 BATADV_IF_DEFAULT);
755 if (!curr_ifinfo)
756 goto out;
757
758 curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100759 batadv_neigh_ifinfo_put(curr_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100760
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200761 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200762 case BATADV_GW_MODE_OFF:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200763 default:
764 goto out;
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200765 }
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200766
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200767 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
Dan Carpenter2ef04f42011-11-29 09:09:09 +0300768 if (!neigh_old)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200769 goto out;
770
Simon Wunderlich89652332013-11-13 19:14:46 +0100771 old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
772 if (!old_ifinfo)
773 goto out;
774
775 if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200776 out_of_range = true;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100777 batadv_neigh_ifinfo_put(old_ifinfo);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200778
779out:
780 if (orig_dst_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100781 batadv_orig_node_put(orig_dst_node);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200782 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100783 batadv_gw_node_put(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800784 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100785 batadv_gw_node_put(gw_node);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200786 if (neigh_old)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100787 batadv_neigh_node_put(neigh_old);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200788 if (neigh_curr)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100789 batadv_neigh_node_put(neigh_curr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200790 return out_of_range;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791}