blob: a5602ef0f262acd87d6cb3021cc03e7eebbfce07 [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2009-2013 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
18#include "main.h"
Sven Eckelmannb706b132012-06-10 23:58:51 +020019#include "sysfs.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020#include "gateway_client.h"
21#include "gateway_common.h"
22#include "hard-interface.h"
Linus Lüssing57f0c072011-03-14 22:43:33 +000023#include "originator.h"
Marek Lindnerbe7af5c2011-09-08 13:12:53 +020024#include "translation-table.h"
Antonio Quartulli43676ab52011-04-26 21:31:45 +020025#include "routing.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000026#include <linux/ip.h>
27#include <linux/ipv6.h>
28#include <linux/udp.h>
29#include <linux/if_vlan.h>
30
Antonio Quartulli43676ab52011-04-26 21:31:45 +020031/* This is the offset of the options field in a dhcp packet starting at
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020032 * the beginning of the dhcp header
33 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +020034#define BATADV_DHCP_OPTIONS_OFFSET 240
35#define BATADV_DHCP_REQUEST 3
Antonio Quartulli43676ab52011-04-26 21:31:45 +020036
Sven Eckelmann56303d32012-06-05 22:31:31 +020037static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node)
Marek Lindner25b6d3c2011-02-10 14:33:49 +000038{
39 if (atomic_dec_and_test(&gw_node->refcount))
Paul E. McKenneyeb340b22011-05-01 23:25:02 -070040 kfree_rcu(gw_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041}
42
Sven Eckelmann56303d32012-06-05 22:31:31 +020043static struct batadv_gw_node *
44batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045{
Sven Eckelmann56303d32012-06-05 22:31:31 +020046 struct batadv_gw_node *gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000048 rcu_read_lock();
Sven Eckelmann807736f2012-07-15 22:26:51 +020049 gw_node = rcu_dereference(bat_priv->gw.curr_gw);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010050 if (!gw_node)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000051 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010053 if (!atomic_inc_not_zero(&gw_node->refcount))
54 gw_node = NULL;
55
56out:
57 rcu_read_unlock();
58 return gw_node;
59}
60
Sven Eckelmann56303d32012-06-05 22:31:31 +020061struct batadv_orig_node *
62batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010063{
Sven Eckelmann56303d32012-06-05 22:31:31 +020064 struct batadv_gw_node *gw_node;
65 struct batadv_orig_node *orig_node = NULL;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010066
Sven Eckelmann1409a832012-05-12 18:33:55 +020067 gw_node = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010068 if (!gw_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +000069 goto out;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000070
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010071 rcu_read_lock();
72 orig_node = gw_node->orig_node;
73 if (!orig_node)
74 goto unlock;
75
Marek Lindner7b36e8e2011-02-18 12:28:10 +000076 if (!atomic_inc_not_zero(&orig_node->refcount))
77 orig_node = NULL;
Linus Lüssing43c70ad2011-02-13 21:13:04 +000078
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010079unlock:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000080 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010081out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082 if (gw_node)
Sven Eckelmann1409a832012-05-12 18:33:55 +020083 batadv_gw_node_free_ref(gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010084 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000085}
86
Sven Eckelmann56303d32012-06-05 22:31:31 +020087static void batadv_gw_select(struct batadv_priv *bat_priv,
88 struct batadv_gw_node *new_gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089{
Sven Eckelmann56303d32012-06-05 22:31:31 +020090 struct batadv_gw_node *curr_gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091
Sven Eckelmann807736f2012-07-15 22:26:51 +020092 spin_lock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010093
Marek Lindner25b6d3c2011-02-10 14:33:49 +000094 if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
95 new_gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000096
Sven Eckelmann807736f2012-07-15 22:26:51 +020097 curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
98 rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
Marek Lindner25b6d3c2011-02-10 14:33:49 +000099
100 if (curr_gw_node)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200101 batadv_gw_node_free_ref(curr_gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100102
Sven Eckelmann807736f2012-07-15 22:26:51 +0200103 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100104}
105
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100106/**
107 * batadv_gw_reselect - force a gateway reselection
108 * @bat_priv: the bat priv with all the soft interface information
109 *
110 * Set a flag to remind the GW component to perform a new gateway reselection.
111 * However this function does not ensure that the current gateway is going to be
112 * deselected. The reselection mechanism may elect the same gateway once again.
113 *
114 * This means that invoking batadv_gw_reselect() does not guarantee a gateway
115 * change and therefore a uevent is not necessarily expected.
116 */
117void batadv_gw_reselect(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100118{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200119 atomic_set(&bat_priv->gw.reselect, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120}
121
Sven Eckelmann56303d32012-06-05 22:31:31 +0200122static struct batadv_gw_node *
123batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000124{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200125 struct batadv_neigh_node *router;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200126 struct batadv_gw_node *gw_node, *curr_gw = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000127 uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200128 uint32_t gw_divisor;
Antonio Quartulli2265c142011-04-27 00:22:00 +0200129 uint8_t max_tq = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200130 uint8_t tq_avg;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200131 struct batadv_orig_node *orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200133 gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE;
134 gw_divisor *= 64;
135
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800137 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000138 if (gw_node->deleted)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000139 continue;
140
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200141 orig_node = gw_node->orig_node;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200142 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000143 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144 continue;
145
Antonio Quartulli2265c142011-04-27 00:22:00 +0200146 if (!atomic_inc_not_zero(&gw_node->refcount))
147 goto next;
148
Antonio Quartulli0538f752013-09-02 12:15:01 +0200149 tq_avg = router->bat_iv.tq_avg;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200150
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151 switch (atomic_read(&bat_priv->gw_sel_class)) {
152 case 1: /* fast connection */
Marek Lindner414254e2013-04-23 21:39:58 +0800153 tmp_gw_factor = tq_avg * tq_avg;
154 tmp_gw_factor *= gw_node->bandwidth_down;
155 tmp_gw_factor *= 100 * 100;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200156 tmp_gw_factor /= gw_divisor;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157
158 if ((tmp_gw_factor > max_gw_factor) ||
159 ((tmp_gw_factor == max_gw_factor) &&
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200160 (tq_avg > max_tq))) {
Antonio Quartulli2265c142011-04-27 00:22:00 +0200161 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200162 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200163 curr_gw = gw_node;
164 atomic_inc(&curr_gw->refcount);
165 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166 break;
167
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200168 default: /* 2: stable connection (use best statistic)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169 * 3: fast-switch (use best statistic but change as
170 * soon as a better gateway appears)
171 * XX: late-switch (use best statistic but change as
172 * soon as a better gateway appears which has
173 * $routing_class more tq points)
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200174 */
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200175 if (tq_avg > max_tq) {
Antonio Quartulli2265c142011-04-27 00:22:00 +0200176 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200177 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200178 curr_gw = gw_node;
179 atomic_inc(&curr_gw->refcount);
180 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181 break;
182 }
183
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200184 if (tq_avg > max_tq)
185 max_tq = tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186
187 if (tmp_gw_factor > max_gw_factor)
188 max_gw_factor = tmp_gw_factor;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000189
Sven Eckelmann1409a832012-05-12 18:33:55 +0200190 batadv_gw_node_free_ref(gw_node);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200191
192next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200193 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195 rcu_read_unlock();
Antonio Quartulli2265c142011-04-27 00:22:00 +0200196
197 return curr_gw;
198}
199
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200200/**
201 * batadv_gw_check_client_stop - check if client mode has been switched off
202 * @bat_priv: the bat priv with all the soft interface information
203 *
204 * This function assumes the caller has checked that the gw state *is actually
205 * changing*. This function is not supposed to be called when there is no state
206 * change.
207 */
208void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
209{
210 struct batadv_gw_node *curr_gw;
211
212 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
213 return;
214
215 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
216 if (!curr_gw)
217 return;
218
Antonio Quartullif3163182013-11-04 20:59:40 +0100219 /* deselect the current gateway so that next time that client mode is
220 * enabled a proper GW_ADD event can be sent
221 */
222 batadv_gw_select(bat_priv, NULL);
223
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200224 /* if batman-adv is switching the gw client mode off and a gateway was
225 * already selected, send a DEL uevent
226 */
227 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
228
229 batadv_gw_node_free_ref(curr_gw);
230}
231
Sven Eckelmann56303d32012-06-05 22:31:31 +0200232void batadv_gw_election(struct batadv_priv *bat_priv)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200233{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200234 struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL;
235 struct batadv_neigh_node *router = NULL;
Antonio Quartulli19595e02011-06-12 11:58:58 +0200236 char gw_addr[18] = { '\0' };
Antonio Quartulli2265c142011-04-27 00:22:00 +0200237
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200238 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200239 goto out;
240
Sven Eckelmann1409a832012-05-12 18:33:55 +0200241 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200242
Sven Eckelmann807736f2012-07-15 22:26:51 +0200243 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
Marek Lindnercaa0bf62012-08-04 04:13:26 +0000244 goto out;
245
Sven Eckelmann1409a832012-05-12 18:33:55 +0200246 next_gw = batadv_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
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200254 router = batadv_orig_node_get_router(next_gw->orig_node);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200255 if (!router) {
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100256 batadv_gw_reselect(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200257 goto out;
258 }
259 }
260
261 if ((curr_gw) && (!next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200262 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200263 "Removing selected gateway - no gateway in range\n");
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200264 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
265 NULL);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200266 } else if ((!curr_gw) && (next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200267 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800268 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200269 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800270 next_gw->bandwidth_down / 10,
271 next_gw->bandwidth_down % 10,
272 next_gw->bandwidth_up / 10,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200273 next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200274 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
275 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200276 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200277 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800278 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200279 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800280 next_gw->bandwidth_down / 10,
281 next_gw->bandwidth_down % 10,
282 next_gw->bandwidth_up / 10,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200283 next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200284 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
285 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200286 }
287
Sven Eckelmann1409a832012-05-12 18:33:55 +0200288 batadv_gw_select(bat_priv, next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200289
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100290out:
291 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200292 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200293 if (next_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200294 batadv_gw_node_free_ref(next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200295 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200296 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297}
298
Sven Eckelmann56303d32012-06-05 22:31:31 +0200299void batadv_gw_check_election(struct batadv_priv *bat_priv,
300 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200302 struct batadv_orig_node *curr_gw_orig;
303 struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304 uint8_t gw_tq_avg, orig_tq_avg;
305
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200306 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000307 if (!curr_gw_orig)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100308 goto reselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200310 router_gw = batadv_orig_node_get_router(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000311 if (!router_gw)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100312 goto reselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
314 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000315 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000316 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200318 router_orig = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000319 if (!router_orig)
320 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321
Antonio Quartulli0538f752013-09-02 12:15:01 +0200322 gw_tq_avg = router_gw->bat_iv.tq_avg;
323 orig_tq_avg = router_orig->bat_iv.tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324
325 /* the TQ value has to be better */
326 if (orig_tq_avg < gw_tq_avg)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000327 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200329 /* if the routing class is greater than 3 the value tells us how much
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330 * greater the TQ value of the new gateway must be
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200331 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
333 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000334 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200336 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200337 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
338 gw_tq_avg, orig_tq_avg);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100340reselect:
341 batadv_gw_reselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000342out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000343 if (curr_gw_orig)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200344 batadv_orig_node_free_ref(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000345 if (router_gw)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200346 batadv_neigh_node_free_ref(router_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000347 if (router_orig)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200348 batadv_neigh_node_free_ref(router_orig);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000349
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000350 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351}
352
Marek Lindner414254e2013-04-23 21:39:58 +0800353/**
354 * batadv_gw_node_add - add gateway node to list of available gateways
355 * @bat_priv: the bat priv with all the soft interface information
356 * @orig_node: originator announcing gateway capabilities
357 * @gateway: announced bandwidth information
358 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200359static void batadv_gw_node_add(struct batadv_priv *bat_priv,
360 struct batadv_orig_node *orig_node,
Marek Lindner414254e2013-04-23 21:39:58 +0800361 struct batadv_tvlv_gateway_data *gateway)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200363 struct batadv_gw_node *gw_node;
Marek Lindner414254e2013-04-23 21:39:58 +0800364
365 if (gateway->bandwidth_down == 0)
366 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
Sven Eckelmann704509b2011-05-14 23:14:54 +0200368 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369 if (!gw_node)
370 return;
371
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372 INIT_HLIST_NODE(&gw_node->list);
373 gw_node->orig_node = orig_node;
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000374 atomic_set(&gw_node->refcount, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375
Sven Eckelmann807736f2012-07-15 22:26:51 +0200376 spin_lock_bh(&bat_priv->gw.list_lock);
377 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
378 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200380 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800381 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
382 orig_node->orig,
383 ntohl(gateway->bandwidth_down) / 10,
384 ntohl(gateway->bandwidth_down) % 10,
385 ntohl(gateway->bandwidth_up) / 10,
386 ntohl(gateway->bandwidth_up) % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387}
388
Marek Lindner414254e2013-04-23 21:39:58 +0800389/**
390 * batadv_gw_node_get - retrieve gateway node from list of available gateways
391 * @bat_priv: the bat priv with all the soft interface information
392 * @orig_node: originator announcing gateway capabilities
393 *
394 * Returns gateway node if found or NULL otherwise.
395 */
396static struct batadv_gw_node *
397batadv_gw_node_get(struct batadv_priv *bat_priv,
398 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399{
Marek Lindner414254e2013-04-23 21:39:58 +0800400 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401
402 rcu_read_lock();
Marek Lindner414254e2013-04-23 21:39:58 +0800403 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
404 if (gw_node_tmp->orig_node != orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405 continue;
406
Marek Lindner414254e2013-04-23 21:39:58 +0800407 if (gw_node_tmp->deleted)
408 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
Marek Lindner414254e2013-04-23 21:39:58 +0800410 if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
411 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412
Marek Lindner414254e2013-04-23 21:39:58 +0800413 gw_node = gw_node_tmp;
414 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415 }
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100416 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200417
Marek Lindner414254e2013-04-23 21:39:58 +0800418 return gw_node;
419}
420
421/**
422 * batadv_gw_node_update - update list of available gateways with changed
423 * bandwidth information
424 * @bat_priv: the bat priv with all the soft interface information
425 * @orig_node: originator announcing gateway capabilities
426 * @gateway: announced bandwidth information
427 */
428void batadv_gw_node_update(struct batadv_priv *bat_priv,
429 struct batadv_orig_node *orig_node,
430 struct batadv_tvlv_gateway_data *gateway)
431{
432 struct batadv_gw_node *gw_node, *curr_gw = NULL;
433
434 gw_node = batadv_gw_node_get(bat_priv, orig_node);
435 if (!gw_node) {
436 batadv_gw_node_add(bat_priv, orig_node, gateway);
437 goto out;
438 }
439
440 if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
441 (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
442 goto out;
443
444 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
445 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
446 orig_node->orig,
447 gw_node->bandwidth_down / 10,
448 gw_node->bandwidth_down % 10,
449 gw_node->bandwidth_up / 10,
450 gw_node->bandwidth_up % 10,
451 ntohl(gateway->bandwidth_down) / 10,
452 ntohl(gateway->bandwidth_down) % 10,
453 ntohl(gateway->bandwidth_up) / 10,
454 ntohl(gateway->bandwidth_up) % 10);
455
456 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
457 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
458
459 gw_node->deleted = 0;
460 if (ntohl(gateway->bandwidth_down) == 0) {
461 gw_node->deleted = jiffies;
462 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
463 "Gateway %pM removed from gateway list\n",
464 orig_node->orig);
465
466 /* Note: We don't need a NULL check here, since curr_gw never
467 * gets dereferenced.
468 */
469 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
470 if (gw_node == curr_gw)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100471 batadv_gw_reselect(bat_priv);
Marek Lindner414254e2013-04-23 21:39:58 +0800472 }
473
474out:
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100475 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200476 batadv_gw_node_free_ref(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800477 if (gw_node)
478 batadv_gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479}
480
Sven Eckelmann56303d32012-06-05 22:31:31 +0200481void batadv_gw_node_delete(struct batadv_priv *bat_priv,
482 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483{
Marek Lindner414254e2013-04-23 21:39:58 +0800484 struct batadv_tvlv_gateway_data gateway;
485
486 gateway.bandwidth_down = 0;
487 gateway.bandwidth_up = 0;
488
489 batadv_gw_node_update(bat_priv, orig_node, &gateway);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490}
491
Sven Eckelmann56303d32012-06-05 22:31:31 +0200492void batadv_gw_node_purge(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200494 struct batadv_gw_node *gw_node, *curr_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800495 struct hlist_node *node_tmp;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200496 unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100497 int do_reselect = 0;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100498
Sven Eckelmann1409a832012-05-12 18:33:55 +0200499 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500
Sven Eckelmann807736f2012-07-15 22:26:51 +0200501 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502
Sasha Levinb67bfe02013-02-27 17:06:00 -0800503 hlist_for_each_entry_safe(gw_node, node_tmp,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200504 &bat_priv->gw.list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505 if (((!gw_node->deleted) ||
506 (time_before(jiffies, gw_node->deleted + timeout))) &&
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200507 atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 continue;
509
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100510 if (curr_gw == gw_node)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100511 do_reselect = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512
513 hlist_del_rcu(&gw_node->list);
Sven Eckelmann1409a832012-05-12 18:33:55 +0200514 batadv_gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515 }
516
Sven Eckelmann807736f2012-07-15 22:26:51 +0200517 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100518
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100519 /* gw_reselect() needs to acquire the gw_list_lock */
520 if (do_reselect)
521 batadv_gw_reselect(bat_priv);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100522
523 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200524 batadv_gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525}
526
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200527/* fails if orig_node has no router */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200528static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
529 struct seq_file *seq,
530 const struct batadv_gw_node *gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200532 struct batadv_gw_node *curr_gw;
533 struct batadv_neigh_node *router;
Marek Lindner414254e2013-04-23 21:39:58 +0800534 int ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000535
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200536 router = batadv_orig_node_get_router(gw_node->orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000537 if (!router)
538 goto out;
539
Sven Eckelmann1409a832012-05-12 18:33:55 +0200540 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000541
Marek Lindner414254e2013-04-23 21:39:58 +0800542 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100543 (curr_gw == gw_node ? "=>" : " "),
544 gw_node->orig_node->orig,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200545 router->bat_iv.tq_avg, router->addr,
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100546 router->if_incoming->net_dev->name,
Marek Lindner414254e2013-04-23 21:39:58 +0800547 gw_node->bandwidth_down / 10,
548 gw_node->bandwidth_down % 10,
549 gw_node->bandwidth_up / 10,
550 gw_node->bandwidth_up % 10);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000551
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200552 batadv_neigh_node_free_ref(router);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100553 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200554 batadv_gw_node_free_ref(curr_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000555out:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000556 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557}
558
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200559int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560{
561 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200562 struct batadv_priv *bat_priv = netdev_priv(net_dev);
563 struct batadv_hard_iface *primary_if;
564 struct batadv_gw_node *gw_node;
Marek Lindner30da63a2012-08-03 17:15:46 +0200565 int gw_count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566
Marek Lindner30da63a2012-08-03 17:15:46 +0200567 primary_if = batadv_seq_print_text_primary_if_get(seq);
568 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200569 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100571 seq_printf(seq,
Marek Lindner414254e2013-04-23 21:39:58 +0800572 " %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200573 "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
574 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200575 primary_if->net_dev->dev_addr, net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576
577 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800578 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579 if (gw_node->deleted)
580 continue;
581
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000582 /* fails if orig_node has no router */
Sven Eckelmann1409a832012-05-12 18:33:55 +0200583 if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584 continue;
585
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586 gw_count++;
587 }
588 rcu_read_unlock();
589
590 if (gw_count == 0)
Antonio Quartulli0c814652013-03-21 09:23:29 +0100591 seq_puts(seq, "No gateways in range ...\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592
Marek Lindner32ae9b22011-04-20 15:40:58 +0200593out:
594 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200595 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200596 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597}
598
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200599/* this call might reallocate skb data */
Sven Eckelmann1409a832012-05-12 18:33:55 +0200600static bool batadv_is_type_dhcprequest(struct sk_buff *skb, int header_len)
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200601{
602 int ret = false;
603 unsigned char *p;
604 int pkt_len;
605
606 if (skb_linearize(skb) < 0)
607 goto out;
608
609 pkt_len = skb_headlen(skb);
610
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200611 if (pkt_len < header_len + BATADV_DHCP_OPTIONS_OFFSET + 1)
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200612 goto out;
613
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200614 p = skb->data + header_len + BATADV_DHCP_OPTIONS_OFFSET;
615 pkt_len -= header_len + BATADV_DHCP_OPTIONS_OFFSET + 1;
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200616
617 /* Access the dhcp option lists. Each entry is made up by:
Antonio Quartulli015758d2011-07-09 17:52:13 +0200618 * - octet 1: option type
619 * - octet 2: option data len (only if type != 255 and 0)
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200620 * - octet 3: option data
621 */
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200622 while (*p != 255 && !ret) {
Antonio Quartulli015758d2011-07-09 17:52:13 +0200623 /* p now points to the first octet: option type */
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200624 if (*p == 53) {
625 /* type 53 is the message type option.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200626 * Jump the len octet and go to the data octet
627 */
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200628 if (pkt_len < 2)
629 goto out;
630 p += 2;
631
632 /* check if the message type is what we need */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200633 if (*p == BATADV_DHCP_REQUEST)
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200634 ret = true;
635 break;
636 } else if (*p == 0) {
637 /* option type 0 (padding), just go forward */
638 if (pkt_len < 1)
639 goto out;
640 pkt_len--;
641 p++;
642 } else {
643 /* This is any other option. So we get the length... */
644 if (pkt_len < 1)
645 goto out;
646 pkt_len--;
647 p++;
648
649 /* ...and then we jump over the data */
Antonio Quartulli9205cc52012-02-27 11:29:53 +0100650 if (pkt_len < 1 + (*p))
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200651 goto out;
Antonio Quartulli9205cc52012-02-27 11:29:53 +0100652 pkt_len -= 1 + (*p);
653 p += 1 + (*p);
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200654 }
655 }
656out:
657 return ret;
658}
659
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200660/* this call might reallocate skb data */
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200661bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662{
663 struct ethhdr *ethhdr;
664 struct iphdr *iphdr;
665 struct ipv6hdr *ipv6hdr;
666 struct udphdr *udphdr;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200667 struct vlan_ethhdr *vhdr;
668 __be16 proto;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669
670 /* check for ethernet header */
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200671 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
672 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673 ethhdr = (struct ethhdr *)skb->data;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200674 proto = ethhdr->h_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200675 *header_len += ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676
677 /* check for initial vlan header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200678 if (proto == htons(ETH_P_8021Q)) {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200679 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
680 return false;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200681
682 vhdr = (struct vlan_ethhdr *)skb->data;
683 proto = vhdr->h_vlan_encapsulated_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200684 *header_len += VLAN_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685 }
686
687 /* check for ip header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200688 switch (proto) {
689 case htons(ETH_P_IP):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200690 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
691 return false;
692 iphdr = (struct iphdr *)(skb->data + *header_len);
693 *header_len += iphdr->ihl * 4;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694
695 /* check for udp header */
696 if (iphdr->protocol != IPPROTO_UDP)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200697 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698
699 break;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200700 case htons(ETH_P_IPV6):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200701 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
702 return false;
703 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
704 *header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000705
706 /* check for udp header */
707 if (ipv6hdr->nexthdr != IPPROTO_UDP)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200708 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709
710 break;
711 default:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200712 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713 }
714
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200715 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
716 return false;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200717
718 /* skb->data might have been reallocated by pskb_may_pull() */
719 ethhdr = (struct ethhdr *)skb->data;
720 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
721 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
722
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200723 udphdr = (struct udphdr *)(skb->data + *header_len);
724 *header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725
726 /* check for bootp port */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200727 if ((proto == htons(ETH_P_IP)) &&
Antonio Quartulli293e9332013-05-19 12:55:16 +0200728 (udphdr->dest != htons(67)))
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200729 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200731 if ((proto == htons(ETH_P_IPV6)) &&
Antonio Quartulli293e9332013-05-19 12:55:16 +0200732 (udphdr->dest != htons(547)))
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200733 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200735 return true;
736}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000737
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200738/**
739 * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
740 * @bat_priv: the bat priv with all the soft interface information
741 * @skb: the outgoing packet
742 *
743 * Check if the skb is a DHCP request and if it is sent to the current best GW
744 * server. Due to topology changes it may be the case that the GW server
745 * previously selected is not the best one anymore.
746 *
747 * Returns true if the packet destination is unicast and it is not the best gw,
748 * false otherwise.
749 *
750 * This call might reallocate skb data.
751 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200752bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200753 struct sk_buff *skb)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200754{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200755 struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
756 struct batadv_orig_node *orig_dst_node = NULL;
Marek Lindner414254e2013-04-23 21:39:58 +0800757 struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200758 struct ethhdr *ethhdr;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200759 bool ret, out_of_range = false;
760 unsigned int header_len = 0;
761 uint8_t curr_tq_avg;
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200762 unsigned short vid;
763
764 vid = batadv_get_vid(skb, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000765
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200766 ret = batadv_gw_is_dhcp_target(skb, &header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200767 if (!ret)
768 goto out;
769
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200770 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200771 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200772 ethhdr->h_dest, vid);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200773 if (!orig_dst_node)
774 goto out;
775
Marek Lindner414254e2013-04-23 21:39:58 +0800776 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
777 if (!gw_node->bandwidth_down == 0)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200778 goto out;
779
Sven Eckelmann1409a832012-05-12 18:33:55 +0200780 ret = batadv_is_type_dhcprequest(skb, header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200781 if (!ret)
782 goto out;
783
784 switch (atomic_read(&bat_priv->gw_mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200785 case BATADV_GW_MODE_SERVER:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200786 /* If we are a GW then we are our best GW. We can artificially
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200787 * set the tq towards ourself as the maximum value
788 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200789 curr_tq_avg = BATADV_TQ_MAX_VALUE;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200790 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200791 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann1409a832012-05-12 18:33:55 +0200792 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200793 if (!curr_gw)
794 goto out;
795
796 /* packet is going to our gateway */
797 if (curr_gw->orig_node == orig_dst_node)
798 goto out;
799
800 /* If the dhcp packet has been sent to a different gw,
801 * we have to evaluate whether the old gw is still
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200802 * reliable enough
803 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200804 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
805 NULL);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200806 if (!neigh_curr)
807 goto out;
808
Antonio Quartulli0538f752013-09-02 12:15:01 +0200809 curr_tq_avg = neigh_curr->bat_iv.tq_avg;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200810 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200811 case BATADV_GW_MODE_OFF:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200812 default:
813 goto out;
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200814 }
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200815
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200816 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
Dan Carpenter2ef04f42011-11-29 09:09:09 +0300817 if (!neigh_old)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200818 goto out;
819
Antonio Quartulli0538f752013-09-02 12:15:01 +0200820 if (curr_tq_avg - neigh_old->bat_iv.tq_avg > BATADV_GW_THRESHOLD)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200821 out_of_range = true;
822
823out:
824 if (orig_dst_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200825 batadv_orig_node_free_ref(orig_dst_node);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200826 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200827 batadv_gw_node_free_ref(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800828 if (gw_node)
829 batadv_gw_node_free_ref(gw_node);
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200830 if (neigh_old)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200831 batadv_neigh_node_free_ref(neigh_old);
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200832 if (neigh_curr)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200833 batadv_neigh_node_free_ref(neigh_curr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200834 return out_of_range;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000835}