blob: 47df4c678988613f7410acb96889558eabdf396d [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann7a79d712018-12-31 23:59:59 +01002/* Copyright (C) 2009-2019 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00007#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
9
10#include <linux/atomic.h>
11#include <linux/byteorder/generic.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020012#include <linux/errno.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020013#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010014#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020015#include <linux/if_ether.h>
16#include <linux/if_vlan.h>
17#include <linux/in.h>
18#include <linux/ip.h>
19#include <linux/ipv6.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#include <linux/kernel.h>
Sven Eckelmanne7aed322016-01-16 10:29:41 +010021#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020022#include <linux/list.h>
Sven Eckelmanndff9bc42018-08-12 21:04:41 +020023#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020024#include <linux/netdevice.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020025#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020026#include <linux/rculist.h>
27#include <linux/rcupdate.h>
28#include <linux/seq_file.h>
29#include <linux/skbuff.h>
30#include <linux/slab.h>
31#include <linux/spinlock.h>
32#include <linux/stddef.h>
33#include <linux/udp.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020034#include <net/sock.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010035#include <uapi/linux/batadv_packet.h>
Sven Eckelmannd7129da2016-07-03 13:31:42 +020036#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020037
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020039#include "log.h"
Sven Eckelmannd7129da2016-07-03 13:31:42 +020040#include "netlink.h"
Linus Lüssing57f0c072011-03-14 22:43:33 +000041#include "originator.h"
Antonio Quartulli43676ab2011-04-26 21:31:45 +020042#include "routing.h"
Sven Eckelmannd7129da2016-07-03 13:31:42 +020043#include "soft-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020044#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045
Antonio Quartulli6c413b12013-11-05 19:31:08 +010046/* These are the offsets of the "hw type" and "hw address length" in the dhcp
47 * packet starting at the beginning of the dhcp header
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020048 */
Antonio Quartulli6c413b12013-11-05 19:31:08 +010049#define BATADV_DHCP_HTYPE_OFFSET 1
50#define BATADV_DHCP_HLEN_OFFSET 2
51/* Value of htype representing Ethernet */
52#define BATADV_DHCP_HTYPE_ETHERNET 0x01
53/* This is the offset of the "chaddr" field in the dhcp packet starting at the
54 * beginning of the dhcp header
55 */
56#define BATADV_DHCP_CHADDR_OFFSET 28
Antonio Quartulli43676ab2011-04-26 21:31:45 +020057
Sven Eckelmanne7aed322016-01-16 10:29:41 +010058/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010059 * batadv_gw_node_release() - release gw_node from lists and queue for free
60 * after rcu grace period
Sven Eckelmanne7aed322016-01-16 10:29:41 +010061 * @ref: kref pointer of the gw_node
62 */
63static void batadv_gw_node_release(struct kref *ref)
64{
65 struct batadv_gw_node *gw_node;
66
67 gw_node = container_of(ref, struct batadv_gw_node, refcount);
68
Sven Eckelmann5d967312016-01-17 11:01:09 +010069 batadv_orig_node_put(gw_node->orig_node);
Sven Eckelmanne7aed322016-01-16 10:29:41 +010070 kfree_rcu(gw_node, rcu);
71}
72
73/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010074 * batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
75 * it
Sven Eckelmanne7aed322016-01-16 10:29:41 +010076 * @gw_node: gateway node to free
77 */
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020078void batadv_gw_node_put(struct batadv_gw_node *gw_node)
Marek Lindner25b6d3c2011-02-10 14:33:49 +000079{
Sven Eckelmanne7aed322016-01-16 10:29:41 +010080 kref_put(&gw_node->refcount, batadv_gw_node_release);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081}
82
Sven Eckelmannff15c272017-12-02 19:51:53 +010083/**
84 * batadv_gw_get_selected_gw_node() - Get currently selected gateway
85 * @bat_priv: the bat priv with all the soft interface information
86 *
87 * Return: selected gateway (with increased refcnt), NULL on errors
88 */
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020089struct batadv_gw_node *
Sven Eckelmann56303d32012-06-05 22:31:31 +020090batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091{
Sven Eckelmann56303d32012-06-05 22:31:31 +020092 struct batadv_gw_node *gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000093
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000094 rcu_read_lock();
Sven Eckelmann807736f2012-07-15 22:26:51 +020095 gw_node = rcu_dereference(bat_priv->gw.curr_gw);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010096 if (!gw_node)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000097 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098
Sven Eckelmanne7aed322016-01-16 10:29:41 +010099 if (!kref_get_unless_zero(&gw_node->refcount))
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100100 gw_node = NULL;
101
102out:
103 rcu_read_unlock();
104 return gw_node;
105}
106
Sven Eckelmannff15c272017-12-02 19:51:53 +0100107/**
108 * batadv_gw_get_selected_orig() - Get originator of currently selected gateway
109 * @bat_priv: the bat priv with all the soft interface information
110 *
111 * Return: orig_node of selected gateway (with increased refcnt), NULL on errors
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/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100159 * batadv_gw_reselect() - force a gateway reselection
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100160 * @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/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100175 * batadv_gw_check_client_stop() - check if client mode has been switched off
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200176 * @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 Eckelmannff15c272017-12-02 19:51:53 +0100206/**
207 * batadv_gw_election() - Elect the best gateway
208 * @bat_priv: the bat priv with all the soft interface information
209 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200210void batadv_gw_election(struct batadv_priv *bat_priv)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200211{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200212 struct batadv_gw_node *curr_gw = NULL;
213 struct batadv_gw_node *next_gw = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200214 struct batadv_neigh_node *router = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100215 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Antonio Quartulli19595e02011-06-12 11:58:58 +0200216 char gw_addr[18] = { '\0' };
Antonio Quartulli2265c142011-04-27 00:22:00 +0200217
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800218 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200219 goto out;
220
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200221 if (!bat_priv->algo_ops->gw.get_best_gw_node)
222 goto out;
223
Sven Eckelmann1409a832012-05-12 18:33:55 +0200224 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200225
Sven Eckelmann807736f2012-07-15 22:26:51 +0200226 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
Marek Lindnercaa0bf62012-08-04 04:13:26 +0000227 goto out;
228
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200229 /* if gw.reselect is set to 1 it means that a previous call to
230 * gw.is_eligible() said that we have a new best GW, therefore it can
231 * now be picked from the list and selected
232 */
233 next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200234
235 if (curr_gw == next_gw)
236 goto out;
237
238 if (next_gw) {
Antonio Quartulli19595e02011-06-12 11:58:58 +0200239 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
240
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100241 router = batadv_orig_router_get(next_gw->orig_node,
242 BATADV_IF_DEFAULT);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200243 if (!router) {
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100244 batadv_gw_reselect(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200245 goto out;
246 }
Simon Wunderlich89652332013-11-13 19:14:46 +0100247
248 router_ifinfo = batadv_neigh_ifinfo_get(router,
249 BATADV_IF_DEFAULT);
250 if (!router_ifinfo) {
251 batadv_gw_reselect(bat_priv);
252 goto out;
253 }
Antonio Quartulli2265c142011-04-27 00:22:00 +0200254 }
255
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200256 if (curr_gw && !next_gw) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200257 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200258 "Removing selected gateway - no gateway in range\n");
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200259 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
260 NULL);
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200261 } else if (!curr_gw && next_gw) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200262 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800263 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200264 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800265 next_gw->bandwidth_down / 10,
266 next_gw->bandwidth_down % 10,
267 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100268 next_gw->bandwidth_up % 10,
269 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200270 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
271 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200272 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200273 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800274 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200275 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800276 next_gw->bandwidth_down / 10,
277 next_gw->bandwidth_down % 10,
278 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100279 next_gw->bandwidth_up % 10,
280 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200281 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
282 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200283 }
284
Sven Eckelmann1409a832012-05-12 18:33:55 +0200285 batadv_gw_select(bat_priv, next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200286
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100287out:
288 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100289 batadv_gw_node_put(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200290 if (next_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100291 batadv_gw_node_put(next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200292 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100293 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +0100294 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100295 batadv_neigh_ifinfo_put(router_ifinfo);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296}
297
Sven Eckelmannff15c272017-12-02 19:51:53 +0100298/**
299 * batadv_gw_check_election() - Elect orig node as best gateway when eligible
300 * @bat_priv: the bat priv with all the soft interface information
301 * @orig_node: orig node which is to be checked
302 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200303void batadv_gw_check_election(struct batadv_priv *bat_priv,
304 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200306 struct batadv_orig_node *curr_gw_orig;
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200307
308 /* abort immediately if the routing algorithm does not support gateway
309 * election
310 */
311 if (!bat_priv->algo_ops->gw.is_eligible)
312 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200314 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000315 if (!curr_gw_orig)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100316 goto reselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000319 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000320 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200322 if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
323 orig_node))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000324 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100326reselect:
327 batadv_gw_reselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000328out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000329 if (curr_gw_orig)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100330 batadv_orig_node_put(curr_gw_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331}
332
Marek Lindner414254e2013-04-23 21:39:58 +0800333/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100334 * batadv_gw_node_add() - add gateway node to list of available gateways
Marek Lindner414254e2013-04-23 21:39:58 +0800335 * @bat_priv: the bat priv with all the soft interface information
336 * @orig_node: originator announcing gateway capabilities
337 * @gateway: announced bandwidth information
Sven Eckelmanndff9bc42018-08-12 21:04:41 +0200338 *
339 * Has to be called with the appropriate locks being acquired
340 * (gw.list_lock).
Marek Lindner414254e2013-04-23 21:39:58 +0800341 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200342static void batadv_gw_node_add(struct batadv_priv *bat_priv,
343 struct batadv_orig_node *orig_node,
Marek Lindner414254e2013-04-23 21:39:58 +0800344 struct batadv_tvlv_gateway_data *gateway)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200346 struct batadv_gw_node *gw_node;
Marek Lindner414254e2013-04-23 21:39:58 +0800347
Sven Eckelmanndff9bc42018-08-12 21:04:41 +0200348 lockdep_assert_held(&bat_priv->gw.list_lock);
349
Marek Lindner414254e2013-04-23 21:39:58 +0800350 if (gateway->bandwidth_down == 0)
351 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200353 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc3ba37a72016-03-05 16:09:23 +0100354 if (!gw_node)
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200355 return;
Antonio Quartulli377fe0f2014-05-02 01:35:13 +0200356
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200357 kref_init(&gw_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 INIT_HLIST_NODE(&gw_node->list);
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200359 kref_get(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360 gw_node->orig_node = orig_node;
Simon Wunderlich27a4d5e2015-06-24 14:50:19 +0200361 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
362 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200364 kref_get(&gw_node->refcount);
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +0200365 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
Sven Eckelmann9264c852018-10-30 22:01:23 +0100366 bat_priv->gw.generation++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200368 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800369 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
370 orig_node->orig,
371 ntohl(gateway->bandwidth_down) / 10,
372 ntohl(gateway->bandwidth_down) % 10,
373 ntohl(gateway->bandwidth_up) / 10,
374 ntohl(gateway->bandwidth_up) % 10);
Sven Eckelmannf665fa72016-07-15 17:39:27 +0200375
376 /* don't return reference to new gw_node */
377 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378}
379
Marek Lindner414254e2013-04-23 21:39:58 +0800380/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100381 * batadv_gw_node_get() - retrieve gateway node from list of available gateways
Marek Lindner414254e2013-04-23 21:39:58 +0800382 * @bat_priv: the bat priv with all the soft interface information
383 * @orig_node: originator announcing gateway capabilities
384 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200385 * Return: gateway node if found or NULL otherwise.
Marek Lindner414254e2013-04-23 21:39:58 +0800386 */
Antonio Quartulli50164d82016-07-03 12:46:34 +0200387struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
388 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389{
Marek Lindner414254e2013-04-23 21:39:58 +0800390 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391
392 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +0200393 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list,
394 list) {
Marek Lindner414254e2013-04-23 21:39:58 +0800395 if (gw_node_tmp->orig_node != orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396 continue;
397
Sven Eckelmanne7aed322016-01-16 10:29:41 +0100398 if (!kref_get_unless_zero(&gw_node_tmp->refcount))
Marek Lindner414254e2013-04-23 21:39:58 +0800399 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400
Marek Lindner414254e2013-04-23 21:39:58 +0800401 gw_node = gw_node_tmp;
402 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403 }
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100404 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200405
Marek Lindner414254e2013-04-23 21:39:58 +0800406 return gw_node;
407}
408
409/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100410 * batadv_gw_node_update() - update list of available gateways with changed
Marek Lindner414254e2013-04-23 21:39:58 +0800411 * bandwidth information
412 * @bat_priv: the bat priv with all the soft interface information
413 * @orig_node: originator announcing gateway capabilities
414 * @gateway: announced bandwidth information
415 */
416void batadv_gw_node_update(struct batadv_priv *bat_priv,
417 struct batadv_orig_node *orig_node,
418 struct batadv_tvlv_gateway_data *gateway)
419{
420 struct batadv_gw_node *gw_node, *curr_gw = NULL;
421
Sven Eckelmanndff9bc42018-08-12 21:04:41 +0200422 spin_lock_bh(&bat_priv->gw.list_lock);
Marek Lindner414254e2013-04-23 21:39:58 +0800423 gw_node = batadv_gw_node_get(bat_priv, orig_node);
424 if (!gw_node) {
425 batadv_gw_node_add(bat_priv, orig_node, gateway);
Sven Eckelmanndff9bc42018-08-12 21:04:41 +0200426 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindner414254e2013-04-23 21:39:58 +0800427 goto out;
428 }
Sven Eckelmanndff9bc42018-08-12 21:04:41 +0200429 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindner414254e2013-04-23 21:39:58 +0800430
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200431 if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) &&
432 gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))
Marek Lindner414254e2013-04-23 21:39:58 +0800433 goto out;
434
435 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
436 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
437 orig_node->orig,
438 gw_node->bandwidth_down / 10,
439 gw_node->bandwidth_down % 10,
440 gw_node->bandwidth_up / 10,
441 gw_node->bandwidth_up % 10,
442 ntohl(gateway->bandwidth_down) / 10,
443 ntohl(gateway->bandwidth_down) % 10,
444 ntohl(gateway->bandwidth_up) / 10,
445 ntohl(gateway->bandwidth_up) % 10);
446
447 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
448 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
449
Marek Lindner414254e2013-04-23 21:39:58 +0800450 if (ntohl(gateway->bandwidth_down) == 0) {
Marek Lindner414254e2013-04-23 21:39:58 +0800451 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
452 "Gateway %pM removed from gateway list\n",
453 orig_node->orig);
454
455 /* Note: We don't need a NULL check here, since curr_gw never
456 * gets dereferenced.
457 */
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200458 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc18bdd02016-01-31 13:27:59 +0100459 if (!hlist_unhashed(&gw_node->list)) {
460 hlist_del_init_rcu(&gw_node->list);
Sven Eckelmann3a017432016-01-17 11:01:18 +0100461 batadv_gw_node_put(gw_node);
Sven Eckelmann9264c852018-10-30 22:01:23 +0100462 bat_priv->gw.generation++;
Sven Eckelmannc18bdd02016-01-31 13:27:59 +0100463 }
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200464 spin_unlock_bh(&bat_priv->gw.list_lock);
465
Marek Lindner414254e2013-04-23 21:39:58 +0800466 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
467 if (gw_node == curr_gw)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100468 batadv_gw_reselect(bat_priv);
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200469
470 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100471 batadv_gw_node_put(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800472 }
473
474out:
Marek Lindner414254e2013-04-23 21:39:58 +0800475 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100476 batadv_gw_node_put(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477}
478
Sven Eckelmannff15c272017-12-02 19:51:53 +0100479/**
480 * batadv_gw_node_delete() - Remove orig_node from gateway list
481 * @bat_priv: the bat priv with all the soft interface information
482 * @orig_node: orig node which is currently in process of being removed
483 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200484void batadv_gw_node_delete(struct batadv_priv *bat_priv,
485 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486{
Marek Lindner414254e2013-04-23 21:39:58 +0800487 struct batadv_tvlv_gateway_data gateway;
488
489 gateway.bandwidth_down = 0;
490 gateway.bandwidth_up = 0;
491
492 batadv_gw_node_update(bat_priv, orig_node, &gateway);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493}
494
Sven Eckelmannff15c272017-12-02 19:51:53 +0100495/**
496 * batadv_gw_node_free() - Free gateway information from soft interface
497 * @bat_priv: the bat priv with all the soft interface information
498 */
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200499void batadv_gw_node_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500{
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200501 struct batadv_gw_node *gw_node;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800502 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503
Sven Eckelmann807736f2012-07-15 22:26:51 +0200504 spin_lock_bh(&bat_priv->gw.list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800505 hlist_for_each_entry_safe(gw_node, node_tmp,
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +0200506 &bat_priv->gw.gateway_list, list) {
Simon Wunderlichbd3524c2015-08-03 19:13:58 +0200507 hlist_del_init_rcu(&gw_node->list);
Sven Eckelmann3a017432016-01-17 11:01:18 +0100508 batadv_gw_node_put(gw_node);
Sven Eckelmann9264c852018-10-30 22:01:23 +0100509 bat_priv->gw.generation++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200511 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512}
513
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200514#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmannff15c272017-12-02 19:51:53 +0100515
516/**
517 * batadv_gw_client_seq_print_text() - Print the gateway table in a seq file
518 * @seq: seq file to print on
519 * @offset: not used
520 *
521 * Return: always 0
522 */
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200523int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524{
525 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200526 struct batadv_priv *bat_priv = netdev_priv(net_dev);
527 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528
Marek Lindner30da63a2012-08-03 17:15:46 +0200529 primary_if = batadv_seq_print_text_primary_if_get(seq);
530 if (!primary_if)
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200531 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200533 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 +0200534 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200535 primary_if->net_dev->dev_addr, net_dev->name,
536 bat_priv->algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200538 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000539
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200540 if (!bat_priv->algo_ops->gw.print) {
541 seq_puts(seq,
542 "No printing function for this routing protocol\n");
543 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200546 bat_priv->algo_ops->gw.print(bat_priv, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547
Marek Lindner30da63a2012-08-03 17:15:46 +0200548 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200550#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100552/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100553 * batadv_gw_dump() - Dump gateways into a message
Sven Eckelmannd7129da2016-07-03 13:31:42 +0200554 * @msg: Netlink message to dump into
555 * @cb: Control block containing additional options
556 *
557 * Return: Error code, or length of message
558 */
559int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
560{
561 struct batadv_hard_iface *primary_if = NULL;
562 struct net *net = sock_net(cb->skb->sk);
563 struct net_device *soft_iface;
564 struct batadv_priv *bat_priv;
565 int ifindex;
566 int ret;
567
568 ifindex = batadv_netlink_get_ifindex(cb->nlh,
569 BATADV_ATTR_MESH_IFINDEX);
570 if (!ifindex)
571 return -EINVAL;
572
573 soft_iface = dev_get_by_index(net, ifindex);
574 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
575 ret = -ENODEV;
576 goto out;
577 }
578
579 bat_priv = netdev_priv(soft_iface);
580
581 primary_if = batadv_primary_if_get_selected(bat_priv);
582 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
583 ret = -ENOENT;
584 goto out;
585 }
586
587 if (!bat_priv->algo_ops->gw.dump) {
588 ret = -EOPNOTSUPP;
589 goto out;
590 }
591
592 bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
593
594 ret = msg->len;
595
596out:
597 if (primary_if)
598 batadv_hardif_put(primary_if);
599 if (soft_iface)
600 dev_put(soft_iface);
601
602 return ret;
603}
604
605/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100606 * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100607 * @skb: the packet to check
608 * @header_len: a pointer to the batman-adv header size
609 * @chaddr: buffer where the client address will be stored. Valid
610 * only if the function returns BATADV_DHCP_TO_CLIENT
611 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200612 * This function may re-allocate the data buffer of the skb passed as argument.
613 *
614 * Return:
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100615 * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
616 * while parsing it
617 * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
618 * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100619 */
620enum batadv_dhcp_recipient
621batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200622 u8 *chaddr)
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200623{
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100624 enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 struct ethhdr *ethhdr;
626 struct iphdr *iphdr;
627 struct ipv6hdr *ipv6hdr;
628 struct udphdr *udphdr;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200629 struct vlan_ethhdr *vhdr;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100630 int chaddr_offset;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200631 __be16 proto;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200632 u8 *p;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633
634 /* check for ethernet header */
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200635 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100636 return BATADV_DHCP_NO;
637
Linus Lüssing927c2ed2014-01-19 22:22:45 +0100638 ethhdr = eth_hdr(skb);
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200639 proto = ethhdr->h_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200640 *header_len += ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641
642 /* check for initial vlan header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200643 if (proto == htons(ETH_P_8021Q)) {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200644 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100645 return BATADV_DHCP_NO;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200646
Linus Lüssing927c2ed2014-01-19 22:22:45 +0100647 vhdr = vlan_eth_hdr(skb);
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200648 proto = vhdr->h_vlan_encapsulated_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200649 *header_len += VLAN_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650 }
651
652 /* check for ip header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200653 switch (proto) {
654 case htons(ETH_P_IP):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200655 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100656 return BATADV_DHCP_NO;
657
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200658 iphdr = (struct iphdr *)(skb->data + *header_len);
659 *header_len += iphdr->ihl * 4;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000660
661 /* check for udp header */
662 if (iphdr->protocol != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100663 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664
665 break;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200666 case htons(ETH_P_IPV6):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200667 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100668 return BATADV_DHCP_NO;
669
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200670 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
671 *header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000672
673 /* check for udp header */
674 if (ipv6hdr->nexthdr != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100675 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676
677 break;
678 default:
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100679 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680 }
681
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200682 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100683 return BATADV_DHCP_NO;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200684
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200685 udphdr = (struct udphdr *)(skb->data + *header_len);
686 *header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687
688 /* check for bootp port */
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100689 switch (proto) {
690 case htons(ETH_P_IP):
691 if (udphdr->dest == htons(67))
692 ret = BATADV_DHCP_TO_SERVER;
693 else if (udphdr->source == htons(67))
694 ret = BATADV_DHCP_TO_CLIENT;
695 break;
696 case htons(ETH_P_IPV6):
697 if (udphdr->dest == htons(547))
698 ret = BATADV_DHCP_TO_SERVER;
699 else if (udphdr->source == htons(547))
700 ret = BATADV_DHCP_TO_CLIENT;
701 break;
702 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000703
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100704 chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
705 /* store the client address if the message is going to a client */
706 if (ret == BATADV_DHCP_TO_CLIENT &&
707 pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
708 /* check if the DHCP packet carries an Ethernet DHCP */
709 p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
710 if (*p != BATADV_DHCP_HTYPE_ETHERNET)
711 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000712
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100713 /* check if the DHCP packet carries a valid Ethernet address */
714 p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
715 if (*p != ETH_ALEN)
716 return BATADV_DHCP_NO;
717
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100718 ether_addr_copy(chaddr, skb->data + chaddr_offset);
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100719 }
720
721 return ret;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200722}
Antonio Quartulliaa143d22014-09-01 14:37:27 +0200723
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200724/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100725 * batadv_gw_out_of_range() - check if the dhcp request destination is the best
726 * gateway
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200727 * @bat_priv: the bat priv with all the soft interface information
728 * @skb: the outgoing packet
729 *
730 * Check if the skb is a DHCP request and if it is sent to the current best GW
731 * server. Due to topology changes it may be the case that the GW server
732 * previously selected is not the best one anymore.
733 *
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200734 * This call might reallocate skb data.
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100735 * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200736 *
737 * Return: true if the packet destination is unicast and it is not the best gw,
738 * false otherwise.
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200739 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200740bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200741 struct sk_buff *skb)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200742{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200743 struct batadv_neigh_node *neigh_curr = NULL;
744 struct batadv_neigh_node *neigh_old = NULL;
Linus Lüssinga752c0a2018-03-22 00:21:32 +0100745 struct batadv_orig_node *orig_dst_node = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200746 struct batadv_gw_node *gw_node = NULL;
747 struct batadv_gw_node *curr_gw = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100748 struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100749 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
750 bool out_of_range = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200751 u8 curr_tq_avg;
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200752 unsigned short vid;
753
754 vid = batadv_get_vid(skb, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755
Linus Lüssinga752c0a2018-03-22 00:21:32 +0100756 if (is_multicast_ether_addr(ethhdr->h_dest))
757 goto out;
758
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200759 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200760 ethhdr->h_dest, vid);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200761 if (!orig_dst_node)
762 goto out;
763
Marek Lindner414254e2013-04-23 21:39:58 +0800764 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
Antonio Quartulli0d164492014-12-20 13:48:57 +0100765 if (!gw_node)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200766 goto out;
767
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800768 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200769 case BATADV_GW_MODE_SERVER:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200770 /* If we are a GW then we are our best GW. We can artificially
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200771 * set the tq towards ourself as the maximum value
772 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200773 curr_tq_avg = BATADV_TQ_MAX_VALUE;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200774 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200775 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann1409a832012-05-12 18:33:55 +0200776 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200777 if (!curr_gw)
778 goto out;
779
780 /* packet is going to our gateway */
781 if (curr_gw->orig_node == orig_dst_node)
782 goto out;
783
784 /* If the dhcp packet has been sent to a different gw,
785 * we have to evaluate whether the old gw is still
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200786 * reliable enough
787 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200788 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
789 NULL);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200790 if (!neigh_curr)
791 goto out;
792
Simon Wunderlich89652332013-11-13 19:14:46 +0100793 curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
794 BATADV_IF_DEFAULT);
795 if (!curr_ifinfo)
796 goto out;
797
798 curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100799 batadv_neigh_ifinfo_put(curr_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100800
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200801 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200802 case BATADV_GW_MODE_OFF:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200803 default:
804 goto out;
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200805 }
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200806
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200807 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
Dan Carpenter2ef04f42011-11-29 09:09:09 +0300808 if (!neigh_old)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200809 goto out;
810
Simon Wunderlich89652332013-11-13 19:14:46 +0100811 old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
812 if (!old_ifinfo)
813 goto out;
814
815 if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200816 out_of_range = true;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100817 batadv_neigh_ifinfo_put(old_ifinfo);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200818
819out:
820 if (orig_dst_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100821 batadv_orig_node_put(orig_dst_node);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200822 if (curr_gw)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100823 batadv_gw_node_put(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800824 if (gw_node)
Sven Eckelmann3a017432016-01-17 11:01:18 +0100825 batadv_gw_node_put(gw_node);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200826 if (neigh_old)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100827 batadv_neigh_node_put(neigh_old);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200828 if (neigh_curr)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100829 batadv_neigh_node_put(neigh_curr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200830 return out_of_range;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000831}