blob: 3bd7ed6b6b3e12e01b296e024937e93be3dcd009 [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2009-2016 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>
Sven Eckelmann3c3e5422020-03-16 23:31:01 +010034#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include <linux/netdevice.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020036#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020037#include <linux/rculist.h>
38#include <linux/rcupdate.h>
39#include <linux/seq_file.h>
40#include <linux/skbuff.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/stddef.h>
44#include <linux/udp.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020045#include <net/sock.h>
46#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020047
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048#include "gateway_common.h"
49#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020050#include "log.h"
Sven Eckelmannd7129da2016-07-03 13:31:42 +020051#include "netlink.h"
Linus Lüssing57f0c072011-03-14 22:43:33 +000052#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020053#include "packet.h"
Antonio Quartulli43676ab52011-04-26 21:31:45 +020054#include "routing.h"
Sven Eckelmannd7129da2016-07-03 13:31:42 +020055#include "soft-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020056#include "sysfs.h"
57#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000058
Antonio Quartulli6c413b12013-11-05 19:31:08 +010059/* These are the offsets of the "hw type" and "hw address length" in the dhcp
60 * packet starting at the beginning of the dhcp header
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020061 */
Antonio Quartulli6c413b12013-11-05 19:31:08 +010062#define BATADV_DHCP_HTYPE_OFFSET 1
63#define BATADV_DHCP_HLEN_OFFSET 2
64/* Value of htype representing Ethernet */
65#define BATADV_DHCP_HTYPE_ETHERNET 0x01
66/* This is the offset of the "chaddr" field in the dhcp packet starting at the
67 * beginning of the dhcp header
68 */
69#define BATADV_DHCP_CHADDR_OFFSET 28
Antonio Quartulli43676ab52011-04-26 21:31:45 +020070
Sven Eckelmanne7aed322016-01-16 10:29:41 +010071/**
72 * batadv_gw_node_release - release gw_node from lists and queue for free after
73 * rcu grace period
74 * @ref: kref pointer of the gw_node
75 */
76static void batadv_gw_node_release(struct kref *ref)
77{
78 struct batadv_gw_node *gw_node;
79
80 gw_node = container_of(ref, struct batadv_gw_node, refcount);
81
Sven Eckelmann5d967312016-01-17 11:01:09 +010082 batadv_orig_node_put(gw_node->orig_node);
Sven Eckelmanne7aed322016-01-16 10:29:41 +010083 kfree_rcu(gw_node, rcu);
84}
85
86/**
Sven Eckelmann3a017432016-01-17 11:01:18 +010087 * batadv_gw_node_put - decrement the gw_node refcounter and possibly release it
Sven Eckelmanne7aed322016-01-16 10:29:41 +010088 * @gw_node: gateway node to free
89 */
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020090void batadv_gw_node_put(struct batadv_gw_node *gw_node)
Marek Lindner25b6d3c2011-02-10 14:33:49 +000091{
Sven Eckelmanne7aed322016-01-16 10:29:41 +010092 kref_put(&gw_node->refcount, batadv_gw_node_release);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000093}
94
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020095struct batadv_gw_node *
Sven Eckelmann56303d32012-06-05 22:31:31 +020096batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000097{
Sven Eckelmann56303d32012-06-05 22:31:31 +020098 struct batadv_gw_node *gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000100 rcu_read_lock();
Sven Eckelmann807736f2012-07-15 22:26:51 +0200101 gw_node = rcu_dereference(bat_priv->gw.curr_gw);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100102 if (!gw_node)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000103 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000104
Sven Eckelmanne7aed322016-01-16 10:29:41 +0100105 if (!kref_get_unless_zero(&gw_node->refcount))
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100106 gw_node = NULL;
107
108out:
109 rcu_read_unlock();
110 return gw_node;
111}
112
Sven Eckelmann56303d32012-06-05 22:31:31 +0200113struct batadv_orig_node *
114batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100115{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200116 struct batadv_gw_node *gw_node;
117 struct batadv_orig_node *orig_node = NULL;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100118
Sven Eckelmann1409a832012-05-12 18:33:55 +0200119 gw_node = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100120 if (!gw_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000121 goto out;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000122
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100123 rcu_read_lock();
124 orig_node = gw_node->orig_node;
125 if (!orig_node)
126 goto unlock;
127
Sven Eckelmann7c124392016-01-16 10:29:56 +0100128 if (!kref_get_unless_zero(&orig_node->refcount))
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000129 orig_node = NULL;
Linus Lüssing43c70ad2011-02-13 21:13:04 +0000130
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100131unlock:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000132 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100133out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100135 batadv_gw_node_put(gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100136 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000137}
138
Sven Eckelmann56303d32012-06-05 22:31:31 +0200139static void batadv_gw_select(struct batadv_priv *bat_priv,
140 struct batadv_gw_node *new_gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200142 struct batadv_gw_node *curr_gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000143
Sven Eckelmann807736f2012-07-15 22:26:51 +0200144 spin_lock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100145
Sven Eckelmanna08d4972016-03-05 16:09:22 +0100146 if (new_gw_node)
147 kref_get(&new_gw_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000148
Sven Eckelmann807736f2012-07-15 22:26:51 +0200149 curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
150 rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000151
152 if (curr_gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100153 batadv_gw_node_put(curr_gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100154
Sven Eckelmann807736f2012-07-15 22:26:51 +0200155 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100156}
157
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100158/**
159 * batadv_gw_reselect - force a gateway reselection
160 * @bat_priv: the bat priv with all the soft interface information
161 *
162 * Set a flag to remind the GW component to perform a new gateway reselection.
163 * However this function does not ensure that the current gateway is going to be
164 * deselected. The reselection mechanism may elect the same gateway once again.
165 *
166 * This means that invoking batadv_gw_reselect() does not guarantee a gateway
167 * change and therefore a uevent is not necessarily expected.
168 */
169void batadv_gw_reselect(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100170{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200171 atomic_set(&bat_priv->gw.reselect, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172}
173
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200174/**
175 * batadv_gw_check_client_stop - check if client mode has been switched off
176 * @bat_priv: the bat priv with all the soft interface information
177 *
178 * This function assumes the caller has checked that the gw state *is actually
179 * changing*. This function is not supposed to be called when there is no state
180 * change.
181 */
182void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
183{
184 struct batadv_gw_node *curr_gw;
185
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800186 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200187 return;
188
189 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
190 if (!curr_gw)
191 return;
192
Antonio Quartullif3163182013-11-04 20:59:40 +0100193 /* deselect the current gateway so that next time that client mode is
194 * enabled a proper GW_ADD event can be sent
195 */
196 batadv_gw_select(bat_priv, NULL);
197
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200198 /* if batman-adv is switching the gw client mode off and a gateway was
199 * already selected, send a DEL uevent
200 */
201 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
202
Sven Eckelmann3a017432016-01-17 11:01:18 +0100203 batadv_gw_node_put(curr_gw);
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200204}
205
Sven Eckelmann56303d32012-06-05 22:31:31 +0200206void batadv_gw_election(struct batadv_priv *bat_priv)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200207{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200208 struct batadv_gw_node *curr_gw = NULL;
209 struct batadv_gw_node *next_gw = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200210 struct batadv_neigh_node *router = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100211 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Antonio Quartulli19595e02011-06-12 11:58:58 +0200212 char gw_addr[18] = { '\0' };
Antonio Quartulli2265c142011-04-27 00:22:00 +0200213
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800214 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200215 goto out;
216
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200217 if (!bat_priv->algo_ops->gw.get_best_gw_node)
218 goto out;
219
Sven Eckelmann1409a832012-05-12 18:33:55 +0200220 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200221
Sven Eckelmann807736f2012-07-15 22:26:51 +0200222 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
Marek Lindnercaa0bf62012-08-04 04:13:26 +0000223 goto out;
224
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200225 /* if gw.reselect is set to 1 it means that a previous call to
226 * gw.is_eligible() said that we have a new best GW, therefore it can
227 * now be picked from the list and selected
228 */
229 next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200230
231 if (curr_gw == next_gw)
232 goto out;
233
234 if (next_gw) {
Antonio Quartulli19595e02011-06-12 11:58:58 +0200235 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
236
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100237 router = batadv_orig_router_get(next_gw->orig_node,
238 BATADV_IF_DEFAULT);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200239 if (!router) {
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100240 batadv_gw_reselect(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200241 goto out;
242 }
Simon Wunderlich89652332013-11-13 19:14:46 +0100243
244 router_ifinfo = batadv_neigh_ifinfo_get(router,
245 BATADV_IF_DEFAULT);
246 if (!router_ifinfo) {
247 batadv_gw_reselect(bat_priv);
248 goto out;
249 }
Antonio Quartulli2265c142011-04-27 00:22:00 +0200250 }
251
252 if ((curr_gw) && (!next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200253 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200254 "Removing selected gateway - no gateway in range\n");
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200255 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
256 NULL);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200257 } else if ((!curr_gw) && (next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200258 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800259 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200260 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800261 next_gw->bandwidth_down / 10,
262 next_gw->bandwidth_down % 10,
263 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100264 next_gw->bandwidth_up % 10,
265 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200266 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
267 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200268 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200269 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800270 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200271 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800272 next_gw->bandwidth_down / 10,
273 next_gw->bandwidth_down % 10,
274 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100275 next_gw->bandwidth_up % 10,
276 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200277 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
278 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200279 }
280
Sven Eckelmann1409a832012-05-12 18:33:55 +0200281 batadv_gw_select(bat_priv, next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200282
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100283out:
284 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100285 batadv_gw_node_put(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200286 if (next_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100287 batadv_gw_node_put(next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200288 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100289 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +0100290 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100291 batadv_neigh_ifinfo_put(router_ifinfo);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292}
293
Sven Eckelmann56303d32012-06-05 22:31:31 +0200294void batadv_gw_check_election(struct batadv_priv *bat_priv,
295 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200297 struct batadv_orig_node *curr_gw_orig;
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200298
299 /* abort immediately if the routing algorithm does not support gateway
300 * election
301 */
302 if (!bat_priv->algo_ops->gw.is_eligible)
303 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200305 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000306 if (!curr_gw_orig)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100307 goto reselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000310 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000311 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200313 if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
314 orig_node))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000315 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100317reselect:
318 batadv_gw_reselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000319out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000320 if (curr_gw_orig)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100321 batadv_orig_node_put(curr_gw_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322}
323
Marek Lindner414254e2013-04-23 21:39:58 +0800324/**
325 * batadv_gw_node_add - add gateway node to list of available gateways
326 * @bat_priv: the bat priv with all the soft interface information
327 * @orig_node: originator announcing gateway capabilities
328 * @gateway: announced bandwidth information
Sven Eckelmann3c3e5422020-03-16 23:31:01 +0100329 *
330 * Has to be called with the appropriate locks being acquired
331 * (gw.list_lock).
Marek Lindner414254e2013-04-23 21:39:58 +0800332 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200333static void batadv_gw_node_add(struct batadv_priv *bat_priv,
334 struct batadv_orig_node *orig_node,
Marek Lindner414254e2013-04-23 21:39:58 +0800335 struct batadv_tvlv_gateway_data *gateway)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200337 struct batadv_gw_node *gw_node;
Marek Lindner414254e2013-04-23 21:39:58 +0800338
Sven Eckelmann3c3e5422020-03-16 23:31:01 +0100339 lockdep_assert_held(&bat_priv->gw.list_lock);
340
Marek Lindner414254e2013-04-23 21:39:58 +0800341 if (gateway->bandwidth_down == 0)
342 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200344 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc3ba37a72016-03-05 16:09:23 +0100345 if (!gw_node)
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200346 return;
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200347
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200348 kref_init(&gw_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349 INIT_HLIST_NODE(&gw_node->list);
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200350 kref_get(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351 gw_node->orig_node = orig_node;
Simon Wunderlich27a4d5e2015-06-24 14:50:19 +0200352 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
353 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200355 kref_get(&gw_node->refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200356 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200358 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800359 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
360 orig_node->orig,
361 ntohl(gateway->bandwidth_down) / 10,
362 ntohl(gateway->bandwidth_down) % 10,
363 ntohl(gateway->bandwidth_up) / 10,
364 ntohl(gateway->bandwidth_up) % 10);
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200365
366 /* don't return reference to new gw_node */
367 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368}
369
Marek Lindner414254e2013-04-23 21:39:58 +0800370/**
371 * batadv_gw_node_get - retrieve gateway node from list of available gateways
372 * @bat_priv: the bat priv with all the soft interface information
373 * @orig_node: originator announcing gateway capabilities
374 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200375 * Return: gateway node if found or NULL otherwise.
Marek Lindner414254e2013-04-23 21:39:58 +0800376 */
Antonio Quartulli50164d82016-07-03 12:46:34 +0200377struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
378 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379{
Marek Lindner414254e2013-04-23 21:39:58 +0800380 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381
382 rcu_read_lock();
Marek Lindner414254e2013-04-23 21:39:58 +0800383 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
384 if (gw_node_tmp->orig_node != orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385 continue;
386
Sven Eckelmanne7aed322016-01-16 10:29:41 +0100387 if (!kref_get_unless_zero(&gw_node_tmp->refcount))
Marek Lindner414254e2013-04-23 21:39:58 +0800388 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389
Marek Lindner414254e2013-04-23 21:39:58 +0800390 gw_node = gw_node_tmp;
391 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392 }
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100393 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200394
Marek Lindner414254e2013-04-23 21:39:58 +0800395 return gw_node;
396}
397
398/**
399 * batadv_gw_node_update - update list of available gateways with changed
400 * bandwidth information
401 * @bat_priv: the bat priv with all the soft interface information
402 * @orig_node: originator announcing gateway capabilities
403 * @gateway: announced bandwidth information
404 */
405void batadv_gw_node_update(struct batadv_priv *bat_priv,
406 struct batadv_orig_node *orig_node,
407 struct batadv_tvlv_gateway_data *gateway)
408{
409 struct batadv_gw_node *gw_node, *curr_gw = NULL;
410
Sven Eckelmann3c3e5422020-03-16 23:31:01 +0100411 spin_lock_bh(&bat_priv->gw.list_lock);
Marek Lindner414254e2013-04-23 21:39:58 +0800412 gw_node = batadv_gw_node_get(bat_priv, orig_node);
413 if (!gw_node) {
414 batadv_gw_node_add(bat_priv, orig_node, gateway);
Sven Eckelmann3c3e5422020-03-16 23:31:01 +0100415 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindner414254e2013-04-23 21:39:58 +0800416 goto out;
417 }
Sven Eckelmann3c3e5422020-03-16 23:31:01 +0100418 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindner414254e2013-04-23 21:39:58 +0800419
420 if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
421 (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
422 goto out;
423
424 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
425 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
426 orig_node->orig,
427 gw_node->bandwidth_down / 10,
428 gw_node->bandwidth_down % 10,
429 gw_node->bandwidth_up / 10,
430 gw_node->bandwidth_up % 10,
431 ntohl(gateway->bandwidth_down) / 10,
432 ntohl(gateway->bandwidth_down) % 10,
433 ntohl(gateway->bandwidth_up) / 10,
434 ntohl(gateway->bandwidth_up) % 10);
435
436 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
437 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
438
Marek Lindner414254e2013-04-23 21:39:58 +0800439 if (ntohl(gateway->bandwidth_down) == 0) {
Marek Lindner414254e2013-04-23 21:39:58 +0800440 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
441 "Gateway %pM removed from gateway list\n",
442 orig_node->orig);
443
444 /* Note: We don't need a NULL check here, since curr_gw never
445 * gets dereferenced.
446 */
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200447 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc18bdd02016-01-31 13:27:59 +0100448 if (!hlist_unhashed(&gw_node->list)) {
449 hlist_del_init_rcu(&gw_node->list);
Sven Eckelmann3a017432016-01-17 11:01:18 +0100450 batadv_gw_node_put(gw_node);
Sven Eckelmannc18bdd02016-01-31 13:27:59 +0100451 }
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200452 spin_unlock_bh(&bat_priv->gw.list_lock);
453
Marek Lindner414254e2013-04-23 21:39:58 +0800454 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
455 if (gw_node == curr_gw)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100456 batadv_gw_reselect(bat_priv);
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200457
458 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100459 batadv_gw_node_put(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800460 }
461
462out:
Marek Lindner414254e2013-04-23 21:39:58 +0800463 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100464 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465}
466
Sven Eckelmann56303d32012-06-05 22:31:31 +0200467void batadv_gw_node_delete(struct batadv_priv *bat_priv,
468 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469{
Marek Lindner414254e2013-04-23 21:39:58 +0800470 struct batadv_tvlv_gateway_data gateway;
471
472 gateway.bandwidth_down = 0;
473 gateway.bandwidth_up = 0;
474
475 batadv_gw_node_update(bat_priv, orig_node, &gateway);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476}
477
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200478void batadv_gw_node_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479{
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200480 struct batadv_gw_node *gw_node;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800481 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482
Sven Eckelmann807736f2012-07-15 22:26:51 +0200483 spin_lock_bh(&bat_priv->gw.list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800484 hlist_for_each_entry_safe(gw_node, node_tmp,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200485 &bat_priv->gw.list, list) {
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200486 hlist_del_init_rcu(&gw_node->list);
Sven Eckelmann3a017432016-01-17 11:01:18 +0100487 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200489 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490}
491
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200492#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200493int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494{
495 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200496 struct batadv_priv *bat_priv = netdev_priv(net_dev);
497 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498
Marek Lindner30da63a2012-08-03 17:15:46 +0200499 primary_if = batadv_seq_print_text_primary_if_get(seq);
500 if (!primary_if)
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200501 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200503 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 +0200504 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200505 primary_if->net_dev->dev_addr, net_dev->name,
506 bat_priv->algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200508 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200510 if (!bat_priv->algo_ops->gw.print) {
511 seq_puts(seq,
512 "No printing function for this routing protocol\n");
513 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200516 bat_priv->algo_ops->gw.print(bat_priv, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517
Marek Lindner30da63a2012-08-03 17:15:46 +0200518 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200520#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000521
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100522/**
Sven Eckelmannd7129da2016-07-03 13:31:42 +0200523 * batadv_gw_dump - Dump gateways into a message
524 * @msg: Netlink message to dump into
525 * @cb: Control block containing additional options
526 *
527 * Return: Error code, or length of message
528 */
529int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
530{
531 struct batadv_hard_iface *primary_if = NULL;
532 struct net *net = sock_net(cb->skb->sk);
533 struct net_device *soft_iface;
534 struct batadv_priv *bat_priv;
535 int ifindex;
536 int ret;
537
538 ifindex = batadv_netlink_get_ifindex(cb->nlh,
539 BATADV_ATTR_MESH_IFINDEX);
540 if (!ifindex)
541 return -EINVAL;
542
543 soft_iface = dev_get_by_index(net, ifindex);
544 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
545 ret = -ENODEV;
546 goto out;
547 }
548
549 bat_priv = netdev_priv(soft_iface);
550
551 primary_if = batadv_primary_if_get_selected(bat_priv);
552 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
553 ret = -ENOENT;
554 goto out;
555 }
556
557 if (!bat_priv->algo_ops->gw.dump) {
558 ret = -EOPNOTSUPP;
559 goto out;
560 }
561
562 bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
563
564 ret = msg->len;
565
566out:
567 if (primary_if)
568 batadv_hardif_put(primary_if);
569 if (soft_iface)
570 dev_put(soft_iface);
571
572 return ret;
573}
574
575/**
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100576 * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
577 * @skb: the packet to check
578 * @header_len: a pointer to the batman-adv header size
579 * @chaddr: buffer where the client address will be stored. Valid
580 * only if the function returns BATADV_DHCP_TO_CLIENT
581 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200582 * This function may re-allocate the data buffer of the skb passed as argument.
583 *
584 * Return:
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100585 * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
586 * while parsing it
587 * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
588 * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100589 */
590enum batadv_dhcp_recipient
591batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200592 u8 *chaddr)
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200593{
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100594 enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595 struct ethhdr *ethhdr;
596 struct iphdr *iphdr;
597 struct ipv6hdr *ipv6hdr;
598 struct udphdr *udphdr;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200599 struct vlan_ethhdr *vhdr;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100600 int chaddr_offset;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200601 __be16 proto;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200602 u8 *p;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603
604 /* check for ethernet header */
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200605 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100606 return BATADV_DHCP_NO;
607
Linus Lüssing927c2ed2014-01-19 22:22:45 +0100608 ethhdr = eth_hdr(skb);
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200609 proto = ethhdr->h_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200610 *header_len += ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611
612 /* check for initial vlan header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200613 if (proto == htons(ETH_P_8021Q)) {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200614 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100615 return BATADV_DHCP_NO;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200616
Linus Lüssing927c2ed2014-01-19 22:22:45 +0100617 vhdr = vlan_eth_hdr(skb);
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200618 proto = vhdr->h_vlan_encapsulated_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200619 *header_len += VLAN_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620 }
621
622 /* check for ip header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200623 switch (proto) {
624 case htons(ETH_P_IP):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200625 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100626 return BATADV_DHCP_NO;
627
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200628 iphdr = (struct iphdr *)(skb->data + *header_len);
629 *header_len += iphdr->ihl * 4;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630
631 /* check for udp header */
632 if (iphdr->protocol != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100633 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634
635 break;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200636 case htons(ETH_P_IPV6):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200637 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100638 return BATADV_DHCP_NO;
639
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200640 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
641 *header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642
643 /* check for udp header */
644 if (ipv6hdr->nexthdr != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100645 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000646
647 break;
648 default:
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100649 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650 }
651
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200652 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100653 return BATADV_DHCP_NO;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200654
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200655 udphdr = (struct udphdr *)(skb->data + *header_len);
656 *header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000657
658 /* check for bootp port */
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100659 switch (proto) {
660 case htons(ETH_P_IP):
661 if (udphdr->dest == htons(67))
662 ret = BATADV_DHCP_TO_SERVER;
663 else if (udphdr->source == htons(67))
664 ret = BATADV_DHCP_TO_CLIENT;
665 break;
666 case htons(ETH_P_IPV6):
667 if (udphdr->dest == htons(547))
668 ret = BATADV_DHCP_TO_SERVER;
669 else if (udphdr->source == htons(547))
670 ret = BATADV_DHCP_TO_CLIENT;
671 break;
672 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100674 chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
675 /* store the client address if the message is going to a client */
676 if (ret == BATADV_DHCP_TO_CLIENT &&
677 pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
678 /* check if the DHCP packet carries an Ethernet DHCP */
679 p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
680 if (*p != BATADV_DHCP_HTYPE_ETHERNET)
681 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100683 /* check if the DHCP packet carries a valid Ethernet address */
684 p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
685 if (*p != ETH_ALEN)
686 return BATADV_DHCP_NO;
687
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100688 ether_addr_copy(chaddr, skb->data + chaddr_offset);
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100689 }
690
691 return ret;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200692}
Antonio Quartulliaa143d22014-09-01 14:37:27 +0200693
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200694/**
695 * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
696 * @bat_priv: the bat priv with all the soft interface information
697 * @skb: the outgoing packet
698 *
699 * Check if the skb is a DHCP request and if it is sent to the current best GW
700 * server. Due to topology changes it may be the case that the GW server
701 * previously selected is not the best one anymore.
702 *
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200703 * This call might reallocate skb data.
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100704 * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200705 *
706 * Return: true if the packet destination is unicast and it is not the best gw,
707 * false otherwise.
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200708 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200709bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200710 struct sk_buff *skb)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200711{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200712 struct batadv_neigh_node *neigh_curr = NULL;
713 struct batadv_neigh_node *neigh_old = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200714 struct batadv_orig_node *orig_dst_node = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200715 struct batadv_gw_node *gw_node = NULL;
716 struct batadv_gw_node *curr_gw = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100717 struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100718 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
719 bool out_of_range = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200720 u8 curr_tq_avg;
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200721 unsigned short vid;
722
723 vid = batadv_get_vid(skb, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724
Linus Lüssing511d9452018-03-22 00:21:32 +0100725 if (is_multicast_ether_addr(ethhdr->h_dest))
726 goto out;
727
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200728 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200729 ethhdr->h_dest, vid);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200730 if (!orig_dst_node)
731 goto out;
732
Marek Lindner414254e2013-04-23 21:39:58 +0800733 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
Antonio Quartulli0d164492014-12-20 13:48:57 +0100734 if (!gw_node)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200735 goto out;
736
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800737 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200738 case BATADV_GW_MODE_SERVER:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200739 /* If we are a GW then we are our best GW. We can artificially
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200740 * set the tq towards ourself as the maximum value
741 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200742 curr_tq_avg = BATADV_TQ_MAX_VALUE;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200743 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200744 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann1409a832012-05-12 18:33:55 +0200745 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200746 if (!curr_gw)
747 goto out;
748
749 /* packet is going to our gateway */
750 if (curr_gw->orig_node == orig_dst_node)
751 goto out;
752
753 /* If the dhcp packet has been sent to a different gw,
754 * we have to evaluate whether the old gw is still
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200755 * reliable enough
756 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200757 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
758 NULL);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200759 if (!neigh_curr)
760 goto out;
761
Simon Wunderlich89652332013-11-13 19:14:46 +0100762 curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
763 BATADV_IF_DEFAULT);
764 if (!curr_ifinfo)
765 goto out;
766
767 curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100768 batadv_neigh_ifinfo_put(curr_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100769
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200770 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200771 case BATADV_GW_MODE_OFF:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200772 default:
773 goto out;
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200774 }
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200775
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200776 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
Dan Carpenter2ef04f42011-11-29 09:09:09 +0300777 if (!neigh_old)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200778 goto out;
779
Simon Wunderlich89652332013-11-13 19:14:46 +0100780 old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
781 if (!old_ifinfo)
782 goto out;
783
784 if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200785 out_of_range = true;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100786 batadv_neigh_ifinfo_put(old_ifinfo);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200787
788out:
789 if (orig_dst_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100790 batadv_orig_node_put(orig_dst_node);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200791 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100792 batadv_gw_node_put(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800793 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100794 batadv_gw_node_put(gw_node);
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200795 if (neigh_old)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100796 batadv_neigh_node_put(neigh_old);
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200797 if (neigh_curr)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100798 batadv_neigh_node_put(neigh_curr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200799 return out_of_range;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000800}