blob: 808d2dd4a8394556744b694f3684745d876e1001 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01002/* Copyright (C) 2009-2017 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000017 */
18
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000019#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#include "main.h"
21
22#include <linux/atomic.h>
23#include <linux/byteorder/generic.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020024#include <linux/errno.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010026#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/if_ether.h>
28#include <linux/if_vlan.h>
29#include <linux/in.h>
30#include <linux/ip.h>
31#include <linux/ipv6.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020032#include <linux/kernel.h>
Sven Eckelmanne7aed322016-01-16 10:29:41 +010033#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020034#include <linux/list.h>
35#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>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010046#include <uapi/linux/batadv_packet.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020047#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020048
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049#include "gateway_common.h"
50#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020051#include "log.h"
Sven Eckelmannd7129da2016-07-03 13:31:42 +020052#include "netlink.h"
Linus Lüssing57f0c072011-03-14 22:43:33 +000053#include "originator.h"
Antonio Quartulli43676ab2011-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 Quartulli43676ab2011-04-26 21:31:45 +020070
Sven Eckelmanne7aed322016-01-16 10:29:41 +010071/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010072 * batadv_gw_node_release() - release gw_node from lists and queue for free
73 * after rcu grace period
Sven Eckelmanne7aed322016-01-16 10:29:41 +010074 * @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 Eckelmann7e9a8c22017-12-02 19:51:47 +010087 * batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
88 * it
Sven Eckelmanne7aed322016-01-16 10:29:41 +010089 * @gw_node: gateway node to free
90 */
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020091void batadv_gw_node_put(struct batadv_gw_node *gw_node)
Marek Lindner25b6d3c2011-02-10 14:33:49 +000092{
Sven Eckelmanne7aed322016-01-16 10:29:41 +010093 kref_put(&gw_node->refcount, batadv_gw_node_release);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000094}
95
Sven Eckelmannff15c272017-12-02 19:51:53 +010096/**
97 * batadv_gw_get_selected_gw_node() - Get currently selected gateway
98 * @bat_priv: the bat priv with all the soft interface information
99 *
100 * Return: selected gateway (with increased refcnt), NULL on errors
101 */
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200102struct batadv_gw_node *
Sven Eckelmann56303d32012-06-05 22:31:31 +0200103batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000104{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200105 struct batadv_gw_node *gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000106
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000107 rcu_read_lock();
Sven Eckelmann807736f2012-07-15 22:26:51 +0200108 gw_node = rcu_dereference(bat_priv->gw.curr_gw);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100109 if (!gw_node)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000110 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111
Sven Eckelmanne7aed322016-01-16 10:29:41 +0100112 if (!kref_get_unless_zero(&gw_node->refcount))
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100113 gw_node = NULL;
114
115out:
116 rcu_read_unlock();
117 return gw_node;
118}
119
Sven Eckelmannff15c272017-12-02 19:51:53 +0100120/**
121 * batadv_gw_get_selected_orig() - Get originator of currently selected gateway
122 * @bat_priv: the bat priv with all the soft interface information
123 *
124 * Return: orig_node of selected gateway (with increased refcnt), NULL on errors
125 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200126struct batadv_orig_node *
127batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100128{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200129 struct batadv_gw_node *gw_node;
130 struct batadv_orig_node *orig_node = NULL;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100131
Sven Eckelmann1409a832012-05-12 18:33:55 +0200132 gw_node = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100133 if (!gw_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000134 goto out;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000135
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100136 rcu_read_lock();
137 orig_node = gw_node->orig_node;
138 if (!orig_node)
139 goto unlock;
140
Sven Eckelmann7c124392016-01-16 10:29:56 +0100141 if (!kref_get_unless_zero(&orig_node->refcount))
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000142 orig_node = NULL;
Linus Lüssing43c70ad2011-02-13 21:13:04 +0000143
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100144unlock:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000145 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100146out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000147 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100148 batadv_gw_node_put(gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100149 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150}
151
Sven Eckelmann56303d32012-06-05 22:31:31 +0200152static void batadv_gw_select(struct batadv_priv *bat_priv,
153 struct batadv_gw_node *new_gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200155 struct batadv_gw_node *curr_gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000156
Sven Eckelmann807736f2012-07-15 22:26:51 +0200157 spin_lock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100158
Sven Eckelmanna08d4972016-03-05 16:09:22 +0100159 if (new_gw_node)
160 kref_get(&new_gw_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161
Sven Eckelmann807736f2012-07-15 22:26:51 +0200162 curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
163 rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000164
165 if (curr_gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100166 batadv_gw_node_put(curr_gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100167
Sven Eckelmann807736f2012-07-15 22:26:51 +0200168 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100169}
170
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100171/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100172 * batadv_gw_reselect() - force a gateway reselection
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100173 * @bat_priv: the bat priv with all the soft interface information
174 *
175 * Set a flag to remind the GW component to perform a new gateway reselection.
176 * However this function does not ensure that the current gateway is going to be
177 * deselected. The reselection mechanism may elect the same gateway once again.
178 *
179 * This means that invoking batadv_gw_reselect() does not guarantee a gateway
180 * change and therefore a uevent is not necessarily expected.
181 */
182void batadv_gw_reselect(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100183{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200184 atomic_set(&bat_priv->gw.reselect, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185}
186
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200187/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100188 * batadv_gw_check_client_stop() - check if client mode has been switched off
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200189 * @bat_priv: the bat priv with all the soft interface information
190 *
191 * This function assumes the caller has checked that the gw state *is actually
192 * changing*. This function is not supposed to be called when there is no state
193 * change.
194 */
195void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
196{
197 struct batadv_gw_node *curr_gw;
198
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800199 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200200 return;
201
202 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
203 if (!curr_gw)
204 return;
205
Antonio Quartullif3163182013-11-04 20:59:40 +0100206 /* deselect the current gateway so that next time that client mode is
207 * enabled a proper GW_ADD event can be sent
208 */
209 batadv_gw_select(bat_priv, NULL);
210
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200211 /* if batman-adv is switching the gw client mode off and a gateway was
212 * already selected, send a DEL uevent
213 */
214 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
215
Sven Eckelmann3a017432016-01-17 11:01:18 +0100216 batadv_gw_node_put(curr_gw);
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200217}
218
Sven Eckelmannff15c272017-12-02 19:51:53 +0100219/**
220 * batadv_gw_election() - Elect the best gateway
221 * @bat_priv: the bat priv with all the soft interface information
222 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200223void batadv_gw_election(struct batadv_priv *bat_priv)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200224{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200225 struct batadv_gw_node *curr_gw = NULL;
226 struct batadv_gw_node *next_gw = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200227 struct batadv_neigh_node *router = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100228 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Antonio Quartulli19595e02011-06-12 11:58:58 +0200229 char gw_addr[18] = { '\0' };
Antonio Quartulli2265c142011-04-27 00:22:00 +0200230
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800231 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200232 goto out;
233
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200234 if (!bat_priv->algo_ops->gw.get_best_gw_node)
235 goto out;
236
Sven Eckelmann1409a832012-05-12 18:33:55 +0200237 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200238
Sven Eckelmann807736f2012-07-15 22:26:51 +0200239 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
Marek Lindnercaa0bf62012-08-04 04:13:26 +0000240 goto out;
241
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200242 /* if gw.reselect is set to 1 it means that a previous call to
243 * gw.is_eligible() said that we have a new best GW, therefore it can
244 * now be picked from the list and selected
245 */
246 next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200247
248 if (curr_gw == next_gw)
249 goto out;
250
251 if (next_gw) {
Antonio Quartulli19595e02011-06-12 11:58:58 +0200252 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
253
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100254 router = batadv_orig_router_get(next_gw->orig_node,
255 BATADV_IF_DEFAULT);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200256 if (!router) {
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100257 batadv_gw_reselect(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200258 goto out;
259 }
Simon Wunderlich89652332013-11-13 19:14:46 +0100260
261 router_ifinfo = batadv_neigh_ifinfo_get(router,
262 BATADV_IF_DEFAULT);
263 if (!router_ifinfo) {
264 batadv_gw_reselect(bat_priv);
265 goto out;
266 }
Antonio Quartulli2265c142011-04-27 00:22:00 +0200267 }
268
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200269 if (curr_gw && !next_gw) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200270 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200271 "Removing selected gateway - no gateway in range\n");
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200272 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
273 NULL);
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200274 } else if (!curr_gw && next_gw) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200275 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800276 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200277 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800278 next_gw->bandwidth_down / 10,
279 next_gw->bandwidth_down % 10,
280 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100281 next_gw->bandwidth_up % 10,
282 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200283 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
284 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200285 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200286 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800287 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200288 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800289 next_gw->bandwidth_down / 10,
290 next_gw->bandwidth_down % 10,
291 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100292 next_gw->bandwidth_up % 10,
293 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200294 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
295 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200296 }
297
Sven Eckelmann1409a832012-05-12 18:33:55 +0200298 batadv_gw_select(bat_priv, next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200299
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100300out:
301 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100302 batadv_gw_node_put(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200303 if (next_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100304 batadv_gw_node_put(next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200305 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100306 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +0100307 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100308 batadv_neigh_ifinfo_put(router_ifinfo);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309}
310
Sven Eckelmannff15c272017-12-02 19:51:53 +0100311/**
312 * batadv_gw_check_election() - Elect orig node as best gateway when eligible
313 * @bat_priv: the bat priv with all the soft interface information
314 * @orig_node: orig node which is to be checked
315 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200316void batadv_gw_check_election(struct batadv_priv *bat_priv,
317 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200319 struct batadv_orig_node *curr_gw_orig;
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200320
321 /* abort immediately if the routing algorithm does not support gateway
322 * election
323 */
324 if (!bat_priv->algo_ops->gw.is_eligible)
325 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200327 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000328 if (!curr_gw_orig)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100329 goto reselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000332 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000333 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200335 if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
336 orig_node))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000337 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100339reselect:
340 batadv_gw_reselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000341out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000342 if (curr_gw_orig)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100343 batadv_orig_node_put(curr_gw_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344}
345
Marek Lindner414254e2013-04-23 21:39:58 +0800346/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100347 * batadv_gw_node_add() - add gateway node to list of available gateways
Marek Lindner414254e2013-04-23 21:39:58 +0800348 * @bat_priv: the bat priv with all the soft interface information
349 * @orig_node: originator announcing gateway capabilities
350 * @gateway: announced bandwidth information
351 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200352static void batadv_gw_node_add(struct batadv_priv *bat_priv,
353 struct batadv_orig_node *orig_node,
Marek Lindner414254e2013-04-23 21:39:58 +0800354 struct batadv_tvlv_gateway_data *gateway)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200356 struct batadv_gw_node *gw_node;
Marek Lindner414254e2013-04-23 21:39:58 +0800357
358 if (gateway->bandwidth_down == 0)
359 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200361 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc3ba37a72016-03-05 16:09:23 +0100362 if (!gw_node)
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200363 return;
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200364
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200365 kref_init(&gw_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366 INIT_HLIST_NODE(&gw_node->list);
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200367 kref_get(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368 gw_node->orig_node = orig_node;
Simon Wunderlich27a4d5e2015-06-24 14:50:19 +0200369 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
370 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371
Sven Eckelmann807736f2012-07-15 22:26:51 +0200372 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200373 kref_get(&gw_node->refcount);
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +0200374 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200375 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200377 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800378 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
379 orig_node->orig,
380 ntohl(gateway->bandwidth_down) / 10,
381 ntohl(gateway->bandwidth_down) % 10,
382 ntohl(gateway->bandwidth_up) / 10,
383 ntohl(gateway->bandwidth_up) % 10);
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200384
385 /* don't return reference to new gw_node */
386 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387}
388
Marek Lindner414254e2013-04-23 21:39:58 +0800389/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100390 * batadv_gw_node_get() - retrieve gateway node from list of available gateways
Marek Lindner414254e2013-04-23 21:39:58 +0800391 * @bat_priv: the bat priv with all the soft interface information
392 * @orig_node: originator announcing gateway capabilities
393 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200394 * Return: gateway node if found or NULL otherwise.
Marek Lindner414254e2013-04-23 21:39:58 +0800395 */
Antonio Quartulli50164d82016-07-03 12:46:34 +0200396struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
397 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398{
Marek Lindner414254e2013-04-23 21:39:58 +0800399 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400
401 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +0200402 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list,
403 list) {
Marek Lindner414254e2013-04-23 21:39:58 +0800404 if (gw_node_tmp->orig_node != orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405 continue;
406
Sven Eckelmanne7aed322016-01-16 10:29:41 +0100407 if (!kref_get_unless_zero(&gw_node_tmp->refcount))
Marek Lindner414254e2013-04-23 21:39:58 +0800408 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
Marek Lindner414254e2013-04-23 21:39:58 +0800410 gw_node = gw_node_tmp;
411 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412 }
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100413 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200414
Marek Lindner414254e2013-04-23 21:39:58 +0800415 return gw_node;
416}
417
418/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100419 * batadv_gw_node_update() - update list of available gateways with changed
Marek Lindner414254e2013-04-23 21:39:58 +0800420 * bandwidth information
421 * @bat_priv: the bat priv with all the soft interface information
422 * @orig_node: originator announcing gateway capabilities
423 * @gateway: announced bandwidth information
424 */
425void batadv_gw_node_update(struct batadv_priv *bat_priv,
426 struct batadv_orig_node *orig_node,
427 struct batadv_tvlv_gateway_data *gateway)
428{
429 struct batadv_gw_node *gw_node, *curr_gw = NULL;
430
431 gw_node = batadv_gw_node_get(bat_priv, orig_node);
432 if (!gw_node) {
433 batadv_gw_node_add(bat_priv, orig_node, gateway);
434 goto out;
435 }
436
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200437 if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) &&
438 gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))
Marek Lindner414254e2013-04-23 21:39:58 +0800439 goto out;
440
441 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
442 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
443 orig_node->orig,
444 gw_node->bandwidth_down / 10,
445 gw_node->bandwidth_down % 10,
446 gw_node->bandwidth_up / 10,
447 gw_node->bandwidth_up % 10,
448 ntohl(gateway->bandwidth_down) / 10,
449 ntohl(gateway->bandwidth_down) % 10,
450 ntohl(gateway->bandwidth_up) / 10,
451 ntohl(gateway->bandwidth_up) % 10);
452
453 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
454 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
455
Marek Lindner414254e2013-04-23 21:39:58 +0800456 if (ntohl(gateway->bandwidth_down) == 0) {
Marek Lindner414254e2013-04-23 21:39:58 +0800457 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
458 "Gateway %pM removed from gateway list\n",
459 orig_node->orig);
460
461 /* Note: We don't need a NULL check here, since curr_gw never
462 * gets dereferenced.
463 */
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200464 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc18bdd02016-01-31 13:27:59 +0100465 if (!hlist_unhashed(&gw_node->list)) {
466 hlist_del_init_rcu(&gw_node->list);
Sven Eckelmann3a017432016-01-17 11:01:18 +0100467 batadv_gw_node_put(gw_node);
Sven Eckelmannc18bdd02016-01-31 13:27:59 +0100468 }
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200469 spin_unlock_bh(&bat_priv->gw.list_lock);
470
Marek Lindner414254e2013-04-23 21:39:58 +0800471 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
472 if (gw_node == curr_gw)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100473 batadv_gw_reselect(bat_priv);
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200474
475 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100476 batadv_gw_node_put(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800477 }
478
479out:
Marek Lindner414254e2013-04-23 21:39:58 +0800480 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100481 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482}
483
Sven Eckelmannff15c272017-12-02 19:51:53 +0100484/**
485 * batadv_gw_node_delete() - Remove orig_node from gateway list
486 * @bat_priv: the bat priv with all the soft interface information
487 * @orig_node: orig node which is currently in process of being removed
488 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200489void batadv_gw_node_delete(struct batadv_priv *bat_priv,
490 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491{
Marek Lindner414254e2013-04-23 21:39:58 +0800492 struct batadv_tvlv_gateway_data gateway;
493
494 gateway.bandwidth_down = 0;
495 gateway.bandwidth_up = 0;
496
497 batadv_gw_node_update(bat_priv, orig_node, &gateway);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498}
499
Sven Eckelmannff15c272017-12-02 19:51:53 +0100500/**
501 * batadv_gw_node_free() - Free gateway information from soft interface
502 * @bat_priv: the bat priv with all the soft interface information
503 */
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200504void batadv_gw_node_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505{
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200506 struct batadv_gw_node *gw_node;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800507 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508
Sven Eckelmann807736f2012-07-15 22:26:51 +0200509 spin_lock_bh(&bat_priv->gw.list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800510 hlist_for_each_entry_safe(gw_node, node_tmp,
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +0200511 &bat_priv->gw.gateway_list, list) {
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200512 hlist_del_init_rcu(&gw_node->list);
Sven Eckelmann3a017432016-01-17 11:01:18 +0100513 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200515 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000516}
517
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200518#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmannff15c272017-12-02 19:51:53 +0100519
520/**
521 * batadv_gw_client_seq_print_text() - Print the gateway table in a seq file
522 * @seq: seq file to print on
523 * @offset: not used
524 *
525 * Return: always 0
526 */
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200527int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528{
529 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200530 struct batadv_priv *bat_priv = netdev_priv(net_dev);
531 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532
Marek Lindner30da63a2012-08-03 17:15:46 +0200533 primary_if = batadv_seq_print_text_primary_if_get(seq);
534 if (!primary_if)
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200535 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200537 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 +0200538 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200539 primary_if->net_dev->dev_addr, net_dev->name,
540 bat_priv->algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200542 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000543
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200544 if (!bat_priv->algo_ops->gw.print) {
545 seq_puts(seq,
546 "No printing function for this routing protocol\n");
547 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200550 bat_priv->algo_ops->gw.print(bat_priv, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551
Marek Lindner30da63a2012-08-03 17:15:46 +0200552 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200554#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100556/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100557 * batadv_gw_dump() - Dump gateways into a message
Sven Eckelmannd7129da2016-07-03 13:31:42 +0200558 * @msg: Netlink message to dump into
559 * @cb: Control block containing additional options
560 *
561 * Return: Error code, or length of message
562 */
563int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
564{
565 struct batadv_hard_iface *primary_if = NULL;
566 struct net *net = sock_net(cb->skb->sk);
567 struct net_device *soft_iface;
568 struct batadv_priv *bat_priv;
569 int ifindex;
570 int ret;
571
572 ifindex = batadv_netlink_get_ifindex(cb->nlh,
573 BATADV_ATTR_MESH_IFINDEX);
574 if (!ifindex)
575 return -EINVAL;
576
577 soft_iface = dev_get_by_index(net, ifindex);
578 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
579 ret = -ENODEV;
580 goto out;
581 }
582
583 bat_priv = netdev_priv(soft_iface);
584
585 primary_if = batadv_primary_if_get_selected(bat_priv);
586 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
587 ret = -ENOENT;
588 goto out;
589 }
590
591 if (!bat_priv->algo_ops->gw.dump) {
592 ret = -EOPNOTSUPP;
593 goto out;
594 }
595
596 bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
597
598 ret = msg->len;
599
600out:
601 if (primary_if)
602 batadv_hardif_put(primary_if);
603 if (soft_iface)
604 dev_put(soft_iface);
605
606 return ret;
607}
608
609/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100610 * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100611 * @skb: the packet to check
612 * @header_len: a pointer to the batman-adv header size
613 * @chaddr: buffer where the client address will be stored. Valid
614 * only if the function returns BATADV_DHCP_TO_CLIENT
615 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200616 * This function may re-allocate the data buffer of the skb passed as argument.
617 *
618 * Return:
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100619 * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
620 * while parsing it
621 * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
622 * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100623 */
624enum batadv_dhcp_recipient
625batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200626 u8 *chaddr)
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200627{
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100628 enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 struct ethhdr *ethhdr;
630 struct iphdr *iphdr;
631 struct ipv6hdr *ipv6hdr;
632 struct udphdr *udphdr;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200633 struct vlan_ethhdr *vhdr;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100634 int chaddr_offset;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200635 __be16 proto;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200636 u8 *p;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637
638 /* check for ethernet header */
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200639 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100640 return BATADV_DHCP_NO;
641
Linus Lüssing927c2ed2014-01-19 22:22:45 +0100642 ethhdr = eth_hdr(skb);
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200643 proto = ethhdr->h_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200644 *header_len += ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645
646 /* check for initial vlan header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200647 if (proto == htons(ETH_P_8021Q)) {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200648 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100649 return BATADV_DHCP_NO;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200650
Linus Lüssing927c2ed2014-01-19 22:22:45 +0100651 vhdr = vlan_eth_hdr(skb);
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200652 proto = vhdr->h_vlan_encapsulated_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200653 *header_len += VLAN_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000654 }
655
656 /* check for ip header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200657 switch (proto) {
658 case htons(ETH_P_IP):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200659 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100660 return BATADV_DHCP_NO;
661
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200662 iphdr = (struct iphdr *)(skb->data + *header_len);
663 *header_len += iphdr->ihl * 4;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664
665 /* check for udp header */
666 if (iphdr->protocol != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100667 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668
669 break;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200670 case htons(ETH_P_IPV6):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200671 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100672 return BATADV_DHCP_NO;
673
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200674 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
675 *header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676
677 /* check for udp header */
678 if (ipv6hdr->nexthdr != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100679 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680
681 break;
682 default:
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100683 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684 }
685
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200686 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100687 return BATADV_DHCP_NO;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200688
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200689 udphdr = (struct udphdr *)(skb->data + *header_len);
690 *header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000691
692 /* check for bootp port */
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100693 switch (proto) {
694 case htons(ETH_P_IP):
695 if (udphdr->dest == htons(67))
696 ret = BATADV_DHCP_TO_SERVER;
697 else if (udphdr->source == htons(67))
698 ret = BATADV_DHCP_TO_CLIENT;
699 break;
700 case htons(ETH_P_IPV6):
701 if (udphdr->dest == htons(547))
702 ret = BATADV_DHCP_TO_SERVER;
703 else if (udphdr->source == htons(547))
704 ret = BATADV_DHCP_TO_CLIENT;
705 break;
706 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100708 chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
709 /* store the client address if the message is going to a client */
710 if (ret == BATADV_DHCP_TO_CLIENT &&
711 pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
712 /* check if the DHCP packet carries an Ethernet DHCP */
713 p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
714 if (*p != BATADV_DHCP_HTYPE_ETHERNET)
715 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100717 /* check if the DHCP packet carries a valid Ethernet address */
718 p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
719 if (*p != ETH_ALEN)
720 return BATADV_DHCP_NO;
721
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100722 ether_addr_copy(chaddr, skb->data + chaddr_offset);
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100723 }
724
725 return ret;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200726}
Antonio Quartulliaa143d22014-09-01 14:37:27 +0200727
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200728/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100729 * batadv_gw_out_of_range() - check if the dhcp request destination is the best
730 * gateway
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200731 * @bat_priv: the bat priv with all the soft interface information
732 * @skb: the outgoing packet
733 *
734 * Check if the skb is a DHCP request and if it is sent to the current best GW
735 * server. Due to topology changes it may be the case that the GW server
736 * previously selected is not the best one anymore.
737 *
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200738 * This call might reallocate skb data.
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100739 * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200740 *
741 * Return: true if the packet destination is unicast and it is not the best gw,
742 * false otherwise.
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200743 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200744bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200745 struct sk_buff *skb)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200746{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200747 struct batadv_neigh_node *neigh_curr = NULL;
748 struct batadv_neigh_node *neigh_old = NULL;
Linus Lüssinga752c0a2018-03-22 00:21:32 +0100749 struct batadv_orig_node *orig_dst_node = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200750 struct batadv_gw_node *gw_node = NULL;
751 struct batadv_gw_node *curr_gw = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100752 struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100753 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
754 bool out_of_range = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200755 u8 curr_tq_avg;
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200756 unsigned short vid;
757
758 vid = batadv_get_vid(skb, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000759
Linus Lüssinga752c0a2018-03-22 00:21:32 +0100760 if (is_multicast_ether_addr(ethhdr->h_dest))
761 goto out;
762
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200763 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200764 ethhdr->h_dest, vid);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200765 if (!orig_dst_node)
766 goto out;
767
Marek Lindner414254e2013-04-23 21:39:58 +0800768 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
Antonio Quartulli0d164492014-12-20 13:48:57 +0100769 if (!gw_node)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200770 goto out;
771
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800772 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200773 case BATADV_GW_MODE_SERVER:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200774 /* If we are a GW then we are our best GW. We can artificially
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200775 * set the tq towards ourself as the maximum value
776 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200777 curr_tq_avg = BATADV_TQ_MAX_VALUE;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200778 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200779 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann1409a832012-05-12 18:33:55 +0200780 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200781 if (!curr_gw)
782 goto out;
783
784 /* packet is going to our gateway */
785 if (curr_gw->orig_node == orig_dst_node)
786 goto out;
787
788 /* If the dhcp packet has been sent to a different gw,
789 * we have to evaluate whether the old gw is still
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200790 * reliable enough
791 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200792 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
793 NULL);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200794 if (!neigh_curr)
795 goto out;
796
Simon Wunderlich89652332013-11-13 19:14:46 +0100797 curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
798 BATADV_IF_DEFAULT);
799 if (!curr_ifinfo)
800 goto out;
801
802 curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100803 batadv_neigh_ifinfo_put(curr_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100804
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200805 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200806 case BATADV_GW_MODE_OFF:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200807 default:
808 goto out;
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200809 }
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200810
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200811 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
Dan Carpenter2ef04f42011-11-29 09:09:09 +0300812 if (!neigh_old)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200813 goto out;
814
Simon Wunderlich89652332013-11-13 19:14:46 +0100815 old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
816 if (!old_ifinfo)
817 goto out;
818
819 if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200820 out_of_range = true;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100821 batadv_neigh_ifinfo_put(old_ifinfo);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200822
823out:
824 if (orig_dst_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100825 batadv_orig_node_put(orig_dst_node);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200826 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100827 batadv_gw_node_put(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800828 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100829 batadv_gw_node_put(gw_node);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200830 if (neigh_old)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100831 batadv_neigh_node_put(neigh_old);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200832 if (neigh_curr)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100833 batadv_neigh_node_put(neigh_curr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200834 return out_of_range;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000835}