blob: 53a900952282097d56c404adabe7c3c7691d5b7d [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 Quartulli43676ab2011-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 Quartulli43676ab2011-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 Quartulli43676ab2011-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
Sven Eckelmann56303d32012-06-05 22:31:31 +0200106void batadv_gw_deselect(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100107{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200108 atomic_set(&bat_priv->gw.reselect, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000109}
110
Sven Eckelmann56303d32012-06-05 22:31:31 +0200111static struct batadv_gw_node *
112batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200114 struct batadv_neigh_node *router;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200115 struct batadv_gw_node *gw_node, *curr_gw = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116 uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200117 uint32_t gw_divisor;
Antonio Quartulli2265c142011-04-27 00:22:00 +0200118 uint8_t max_tq = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200119 uint8_t tq_avg;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200120 struct batadv_orig_node *orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000121
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200122 gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE;
123 gw_divisor *= 64;
124
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800126 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000127 if (gw_node->deleted)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128 continue;
129
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200130 orig_node = gw_node->orig_node;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200131 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000132 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133 continue;
134
Antonio Quartulli2265c142011-04-27 00:22:00 +0200135 if (!atomic_inc_not_zero(&gw_node->refcount))
136 goto next;
137
Antonio Quartulli0538f752013-09-02 12:15:01 +0200138 tq_avg = router->bat_iv.tq_avg;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200139
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140 switch (atomic_read(&bat_priv->gw_sel_class)) {
141 case 1: /* fast connection */
Marek Lindner414254e2013-04-23 21:39:58 +0800142 tmp_gw_factor = tq_avg * tq_avg;
143 tmp_gw_factor *= gw_node->bandwidth_down;
144 tmp_gw_factor *= 100 * 100;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200145 tmp_gw_factor /= gw_divisor;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146
147 if ((tmp_gw_factor > max_gw_factor) ||
148 ((tmp_gw_factor == max_gw_factor) &&
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200149 (tq_avg > max_tq))) {
Antonio Quartulli2265c142011-04-27 00:22:00 +0200150 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200151 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200152 curr_gw = gw_node;
153 atomic_inc(&curr_gw->refcount);
154 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 break;
156
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200157 default: /* 2: stable connection (use best statistic)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158 * 3: fast-switch (use best statistic but change as
159 * soon as a better gateway appears)
160 * XX: late-switch (use best statistic but change as
161 * soon as a better gateway appears which has
162 * $routing_class more tq points)
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200163 */
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200164 if (tq_avg > max_tq) {
Antonio Quartulli2265c142011-04-27 00:22:00 +0200165 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200166 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200167 curr_gw = gw_node;
168 atomic_inc(&curr_gw->refcount);
169 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170 break;
171 }
172
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200173 if (tq_avg > max_tq)
174 max_tq = tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175
176 if (tmp_gw_factor > max_gw_factor)
177 max_gw_factor = tmp_gw_factor;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000178
Sven Eckelmann1409a832012-05-12 18:33:55 +0200179 batadv_gw_node_free_ref(gw_node);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200180
181next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200182 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184 rcu_read_unlock();
Antonio Quartulli2265c142011-04-27 00:22:00 +0200185
186 return curr_gw;
187}
188
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200189/**
190 * batadv_gw_check_client_stop - check if client mode has been switched off
191 * @bat_priv: the bat priv with all the soft interface information
192 *
193 * This function assumes the caller has checked that the gw state *is actually
194 * changing*. This function is not supposed to be called when there is no state
195 * change.
196 */
197void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
198{
199 struct batadv_gw_node *curr_gw;
200
201 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
202 return;
203
204 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
205 if (!curr_gw)
206 return;
207
Antonio Quartullif3163182013-11-04 20:59:40 +0100208 /* deselect the current gateway so that next time that client mode is
209 * enabled a proper GW_ADD event can be sent
210 */
211 batadv_gw_select(bat_priv, NULL);
212
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200213 /* if batman-adv is switching the gw client mode off and a gateway was
214 * already selected, send a DEL uevent
215 */
216 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
217
218 batadv_gw_node_free_ref(curr_gw);
219}
220
Sven Eckelmann56303d32012-06-05 22:31:31 +0200221void batadv_gw_election(struct batadv_priv *bat_priv)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200222{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200223 struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL;
224 struct batadv_neigh_node *router = NULL;
Antonio Quartulli19595e02011-06-12 11:58:58 +0200225 char gw_addr[18] = { '\0' };
Antonio Quartulli2265c142011-04-27 00:22:00 +0200226
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200227 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200228 goto out;
229
Sven Eckelmann1409a832012-05-12 18:33:55 +0200230 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200231
Sven Eckelmann807736f2012-07-15 22:26:51 +0200232 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
Marek Lindnercaa0bf62012-08-04 04:13:26 +0000233 goto out;
234
Sven Eckelmann1409a832012-05-12 18:33:55 +0200235 next_gw = batadv_gw_get_best_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200236
237 if (curr_gw == next_gw)
238 goto out;
239
240 if (next_gw) {
Antonio Quartulli19595e02011-06-12 11:58:58 +0200241 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
242
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200243 router = batadv_orig_node_get_router(next_gw->orig_node);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200244 if (!router) {
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200245 batadv_gw_deselect(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200246 goto out;
247 }
248 }
249
250 if ((curr_gw) && (!next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200251 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200252 "Removing selected gateway - no gateway in range\n");
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200253 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
254 NULL);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200255 } else if ((!curr_gw) && (next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200256 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800257 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200258 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800259 next_gw->bandwidth_down / 10,
260 next_gw->bandwidth_down % 10,
261 next_gw->bandwidth_up / 10,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200262 next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200263 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
264 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200265 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200266 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800267 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200268 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800269 next_gw->bandwidth_down / 10,
270 next_gw->bandwidth_down % 10,
271 next_gw->bandwidth_up / 10,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200272 next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200273 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
274 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200275 }
276
Sven Eckelmann1409a832012-05-12 18:33:55 +0200277 batadv_gw_select(bat_priv, next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200278
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100279out:
280 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200281 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200282 if (next_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200283 batadv_gw_node_free_ref(next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200284 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200285 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286}
287
Sven Eckelmann56303d32012-06-05 22:31:31 +0200288void batadv_gw_check_election(struct batadv_priv *bat_priv,
289 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000290{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200291 struct batadv_orig_node *curr_gw_orig;
292 struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293 uint8_t gw_tq_avg, orig_tq_avg;
294
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200295 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000296 if (!curr_gw_orig)
297 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200299 router_gw = batadv_orig_node_get_router(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000300 if (!router_gw)
301 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
303 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000304 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000305 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200307 router_orig = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000308 if (!router_orig)
309 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310
Antonio Quartulli0538f752013-09-02 12:15:01 +0200311 gw_tq_avg = router_gw->bat_iv.tq_avg;
312 orig_tq_avg = router_orig->bat_iv.tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
314 /* the TQ value has to be better */
315 if (orig_tq_avg < gw_tq_avg)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000316 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200318 /* if the routing class is greater than 3 the value tells us how much
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319 * greater the TQ value of the new gateway must be
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200320 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
322 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000323 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200325 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200326 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
327 gw_tq_avg, orig_tq_avg);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328
329deselect:
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200330 batadv_gw_deselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000331out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000332 if (curr_gw_orig)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200333 batadv_orig_node_free_ref(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000334 if (router_gw)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200335 batadv_neigh_node_free_ref(router_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000336 if (router_orig)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200337 batadv_neigh_node_free_ref(router_orig);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000338
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000339 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340}
341
Marek Lindner414254e2013-04-23 21:39:58 +0800342/**
343 * batadv_gw_node_add - add gateway node to list of available gateways
344 * @bat_priv: the bat priv with all the soft interface information
345 * @orig_node: originator announcing gateway capabilities
346 * @gateway: announced bandwidth information
347 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200348static void batadv_gw_node_add(struct batadv_priv *bat_priv,
349 struct batadv_orig_node *orig_node,
Marek Lindner414254e2013-04-23 21:39:58 +0800350 struct batadv_tvlv_gateway_data *gateway)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200352 struct batadv_gw_node *gw_node;
Marek Lindner414254e2013-04-23 21:39:58 +0800353
354 if (gateway->bandwidth_down == 0)
355 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356
Sven Eckelmann704509b2011-05-14 23:14:54 +0200357 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 if (!gw_node)
359 return;
360
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361 INIT_HLIST_NODE(&gw_node->list);
362 gw_node->orig_node = orig_node;
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000363 atomic_set(&gw_node->refcount, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
Sven Eckelmann807736f2012-07-15 22:26:51 +0200365 spin_lock_bh(&bat_priv->gw.list_lock);
366 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
367 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200369 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800370 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
371 orig_node->orig,
372 ntohl(gateway->bandwidth_down) / 10,
373 ntohl(gateway->bandwidth_down) % 10,
374 ntohl(gateway->bandwidth_up) / 10,
375 ntohl(gateway->bandwidth_up) % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376}
377
Marek Lindner414254e2013-04-23 21:39:58 +0800378/**
379 * batadv_gw_node_get - retrieve gateway node from list of available gateways
380 * @bat_priv: the bat priv with all the soft interface information
381 * @orig_node: originator announcing gateway capabilities
382 *
383 * Returns gateway node if found or NULL otherwise.
384 */
385static struct batadv_gw_node *
386batadv_gw_node_get(struct batadv_priv *bat_priv,
387 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388{
Marek Lindner414254e2013-04-23 21:39:58 +0800389 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390
391 rcu_read_lock();
Marek Lindner414254e2013-04-23 21:39:58 +0800392 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
393 if (gw_node_tmp->orig_node != orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394 continue;
395
Marek Lindner414254e2013-04-23 21:39:58 +0800396 if (gw_node_tmp->deleted)
397 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398
Marek Lindner414254e2013-04-23 21:39:58 +0800399 if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
400 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401
Marek Lindner414254e2013-04-23 21:39:58 +0800402 gw_node = gw_node_tmp;
403 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404 }
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100405 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200406
Marek Lindner414254e2013-04-23 21:39:58 +0800407 return gw_node;
408}
409
410/**
411 * batadv_gw_node_update - update list of available gateways with changed
412 * bandwidth information
413 * @bat_priv: the bat priv with all the soft interface information
414 * @orig_node: originator announcing gateway capabilities
415 * @gateway: announced bandwidth information
416 */
417void batadv_gw_node_update(struct batadv_priv *bat_priv,
418 struct batadv_orig_node *orig_node,
419 struct batadv_tvlv_gateway_data *gateway)
420{
421 struct batadv_gw_node *gw_node, *curr_gw = NULL;
422
423 gw_node = batadv_gw_node_get(bat_priv, orig_node);
424 if (!gw_node) {
425 batadv_gw_node_add(bat_priv, orig_node, gateway);
426 goto out;
427 }
428
429 if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
430 (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
431 goto out;
432
433 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
434 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
435 orig_node->orig,
436 gw_node->bandwidth_down / 10,
437 gw_node->bandwidth_down % 10,
438 gw_node->bandwidth_up / 10,
439 gw_node->bandwidth_up % 10,
440 ntohl(gateway->bandwidth_down) / 10,
441 ntohl(gateway->bandwidth_down) % 10,
442 ntohl(gateway->bandwidth_up) / 10,
443 ntohl(gateway->bandwidth_up) % 10);
444
445 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
446 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
447
448 gw_node->deleted = 0;
449 if (ntohl(gateway->bandwidth_down) == 0) {
450 gw_node->deleted = jiffies;
451 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 */
458 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
459 if (gw_node == curr_gw)
460 batadv_gw_deselect(bat_priv);
461 }
462
463out:
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100464 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200465 batadv_gw_node_free_ref(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800466 if (gw_node)
467 batadv_gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468}
469
Sven Eckelmann56303d32012-06-05 22:31:31 +0200470void batadv_gw_node_delete(struct batadv_priv *bat_priv,
471 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472{
Marek Lindner414254e2013-04-23 21:39:58 +0800473 struct batadv_tvlv_gateway_data gateway;
474
475 gateway.bandwidth_down = 0;
476 gateway.bandwidth_up = 0;
477
478 batadv_gw_node_update(bat_priv, orig_node, &gateway);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479}
480
Sven Eckelmann56303d32012-06-05 22:31:31 +0200481void batadv_gw_node_purge(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200483 struct batadv_gw_node *gw_node, *curr_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800484 struct hlist_node *node_tmp;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200485 unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200486 int do_deselect = 0;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100487
Sven Eckelmann1409a832012-05-12 18:33:55 +0200488 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489
Sven Eckelmann807736f2012-07-15 22:26:51 +0200490 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491
Sasha Levinb67bfe02013-02-27 17:06:00 -0800492 hlist_for_each_entry_safe(gw_node, node_tmp,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200493 &bat_priv->gw.list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494 if (((!gw_node->deleted) ||
495 (time_before(jiffies, gw_node->deleted + timeout))) &&
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200496 atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497 continue;
498
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100499 if (curr_gw == gw_node)
500 do_deselect = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501
502 hlist_del_rcu(&gw_node->list);
Sven Eckelmann1409a832012-05-12 18:33:55 +0200503 batadv_gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504 }
505
Sven Eckelmann807736f2012-07-15 22:26:51 +0200506 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100507
508 /* gw_deselect() needs to acquire the gw_list_lock */
509 if (do_deselect)
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200510 batadv_gw_deselect(bat_priv);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100511
512 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200513 batadv_gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514}
515
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200516/* fails if orig_node has no router */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200517static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
518 struct seq_file *seq,
519 const struct batadv_gw_node *gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200521 struct batadv_gw_node *curr_gw;
522 struct batadv_neigh_node *router;
Marek Lindner414254e2013-04-23 21:39:58 +0800523 int ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200525 router = batadv_orig_node_get_router(gw_node->orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000526 if (!router)
527 goto out;
528
Sven Eckelmann1409a832012-05-12 18:33:55 +0200529 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000530
Marek Lindner414254e2013-04-23 21:39:58 +0800531 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100532 (curr_gw == gw_node ? "=>" : " "),
533 gw_node->orig_node->orig,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200534 router->bat_iv.tq_avg, router->addr,
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100535 router->if_incoming->net_dev->name,
Marek Lindner414254e2013-04-23 21:39:58 +0800536 gw_node->bandwidth_down / 10,
537 gw_node->bandwidth_down % 10,
538 gw_node->bandwidth_up / 10,
539 gw_node->bandwidth_up % 10);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000540
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200541 batadv_neigh_node_free_ref(router);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100542 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200543 batadv_gw_node_free_ref(curr_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000544out:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000545 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546}
547
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200548int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549{
550 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200551 struct batadv_priv *bat_priv = netdev_priv(net_dev);
552 struct batadv_hard_iface *primary_if;
553 struct batadv_gw_node *gw_node;
Marek Lindner30da63a2012-08-03 17:15:46 +0200554 int gw_count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555
Marek Lindner30da63a2012-08-03 17:15:46 +0200556 primary_if = batadv_seq_print_text_primary_if_get(seq);
557 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200558 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100560 seq_printf(seq,
Marek Lindner414254e2013-04-23 21:39:58 +0800561 " %-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 +0200562 "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
563 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200564 primary_if->net_dev->dev_addr, net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565
566 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800567 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568 if (gw_node->deleted)
569 continue;
570
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000571 /* fails if orig_node has no router */
Sven Eckelmann1409a832012-05-12 18:33:55 +0200572 if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000573 continue;
574
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575 gw_count++;
576 }
577 rcu_read_unlock();
578
579 if (gw_count == 0)
Antonio Quartulli0c814652013-03-21 09:23:29 +0100580 seq_puts(seq, "No gateways in range ...\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581
Marek Lindner32ae9b22011-04-20 15:40:58 +0200582out:
583 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200584 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200585 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586}
587
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200588/* this call might reallocate skb data */
Sven Eckelmann1409a832012-05-12 18:33:55 +0200589static bool batadv_is_type_dhcprequest(struct sk_buff *skb, int header_len)
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200590{
591 int ret = false;
592 unsigned char *p;
593 int pkt_len;
594
595 if (skb_linearize(skb) < 0)
596 goto out;
597
598 pkt_len = skb_headlen(skb);
599
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200600 if (pkt_len < header_len + BATADV_DHCP_OPTIONS_OFFSET + 1)
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200601 goto out;
602
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200603 p = skb->data + header_len + BATADV_DHCP_OPTIONS_OFFSET;
604 pkt_len -= header_len + BATADV_DHCP_OPTIONS_OFFSET + 1;
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200605
606 /* Access the dhcp option lists. Each entry is made up by:
Antonio Quartulli015758d2011-07-09 17:52:13 +0200607 * - octet 1: option type
608 * - octet 2: option data len (only if type != 255 and 0)
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200609 * - octet 3: option data
610 */
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200611 while (*p != 255 && !ret) {
Antonio Quartulli015758d2011-07-09 17:52:13 +0200612 /* p now points to the first octet: option type */
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200613 if (*p == 53) {
614 /* type 53 is the message type option.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200615 * Jump the len octet and go to the data octet
616 */
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200617 if (pkt_len < 2)
618 goto out;
619 p += 2;
620
621 /* check if the message type is what we need */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200622 if (*p == BATADV_DHCP_REQUEST)
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200623 ret = true;
624 break;
625 } else if (*p == 0) {
626 /* option type 0 (padding), just go forward */
627 if (pkt_len < 1)
628 goto out;
629 pkt_len--;
630 p++;
631 } else {
632 /* This is any other option. So we get the length... */
633 if (pkt_len < 1)
634 goto out;
635 pkt_len--;
636 p++;
637
638 /* ...and then we jump over the data */
Antonio Quartulli9205cc52012-02-27 11:29:53 +0100639 if (pkt_len < 1 + (*p))
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200640 goto out;
Antonio Quartulli9205cc52012-02-27 11:29:53 +0100641 pkt_len -= 1 + (*p);
642 p += 1 + (*p);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200643 }
644 }
645out:
646 return ret;
647}
648
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200649/* this call might reallocate skb data */
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200650bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651{
652 struct ethhdr *ethhdr;
653 struct iphdr *iphdr;
654 struct ipv6hdr *ipv6hdr;
655 struct udphdr *udphdr;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200656 struct vlan_ethhdr *vhdr;
657 __be16 proto;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658
659 /* check for ethernet header */
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200660 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
661 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662 ethhdr = (struct ethhdr *)skb->data;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200663 proto = ethhdr->h_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200664 *header_len += ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000665
666 /* check for initial vlan header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200667 if (proto == htons(ETH_P_8021Q)) {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200668 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
669 return false;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200670
671 vhdr = (struct vlan_ethhdr *)skb->data;
672 proto = vhdr->h_vlan_encapsulated_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200673 *header_len += VLAN_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000674 }
675
676 /* check for ip header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200677 switch (proto) {
678 case htons(ETH_P_IP):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200679 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
680 return false;
681 iphdr = (struct iphdr *)(skb->data + *header_len);
682 *header_len += iphdr->ihl * 4;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683
684 /* check for udp header */
685 if (iphdr->protocol != IPPROTO_UDP)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200686 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687
688 break;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200689 case htons(ETH_P_IPV6):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200690 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
691 return false;
692 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
693 *header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694
695 /* check for udp header */
696 if (ipv6hdr->nexthdr != IPPROTO_UDP)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200697 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698
699 break;
700 default:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200701 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702 }
703
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200704 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
705 return false;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200706
707 /* skb->data might have been reallocated by pskb_may_pull() */
708 ethhdr = (struct ethhdr *)skb->data;
709 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
710 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
711
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200712 udphdr = (struct udphdr *)(skb->data + *header_len);
713 *header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714
715 /* check for bootp port */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200716 if ((proto == htons(ETH_P_IP)) &&
Antonio Quartulli293e9332013-05-19 12:55:16 +0200717 (udphdr->dest != htons(67)))
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200718 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200720 if ((proto == htons(ETH_P_IPV6)) &&
Antonio Quartulli293e9332013-05-19 12:55:16 +0200721 (udphdr->dest != htons(547)))
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200722 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000723
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200724 return true;
725}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200727/**
728 * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
729 * @bat_priv: the bat priv with all the soft interface information
730 * @skb: the outgoing packet
731 *
732 * Check if the skb is a DHCP request and if it is sent to the current best GW
733 * server. Due to topology changes it may be the case that the GW server
734 * previously selected is not the best one anymore.
735 *
736 * Returns true if the packet destination is unicast and it is not the best gw,
737 * false otherwise.
738 *
739 * This call might reallocate skb data.
740 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200741bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200742 struct sk_buff *skb)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200743{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200744 struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
745 struct batadv_orig_node *orig_dst_node = NULL;
Marek Lindner414254e2013-04-23 21:39:58 +0800746 struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200747 struct ethhdr *ethhdr;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200748 bool ret, out_of_range = false;
749 unsigned int header_len = 0;
750 uint8_t curr_tq_avg;
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200751 unsigned short vid;
752
753 vid = batadv_get_vid(skb, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000754
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200755 ret = batadv_gw_is_dhcp_target(skb, &header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200756 if (!ret)
757 goto out;
758
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200759 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200760 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200761 ethhdr->h_dest, vid);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200762 if (!orig_dst_node)
763 goto out;
764
Marek Lindner414254e2013-04-23 21:39:58 +0800765 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
766 if (!gw_node->bandwidth_down == 0)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200767 goto out;
768
Sven Eckelmann1409a832012-05-12 18:33:55 +0200769 ret = batadv_is_type_dhcprequest(skb, header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200770 if (!ret)
771 goto out;
772
773 switch (atomic_read(&bat_priv->gw_mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200774 case BATADV_GW_MODE_SERVER:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200775 /* If we are a GW then we are our best GW. We can artificially
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200776 * set the tq towards ourself as the maximum value
777 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200778 curr_tq_avg = BATADV_TQ_MAX_VALUE;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200779 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200780 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann1409a832012-05-12 18:33:55 +0200781 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200782 if (!curr_gw)
783 goto out;
784
785 /* packet is going to our gateway */
786 if (curr_gw->orig_node == orig_dst_node)
787 goto out;
788
789 /* If the dhcp packet has been sent to a different gw,
790 * we have to evaluate whether the old gw is still
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200791 * reliable enough
792 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200793 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
794 NULL);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200795 if (!neigh_curr)
796 goto out;
797
Antonio Quartulli0538f752013-09-02 12:15:01 +0200798 curr_tq_avg = neigh_curr->bat_iv.tq_avg;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200799 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200800 case BATADV_GW_MODE_OFF:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200801 default:
802 goto out;
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200803 }
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200804
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200805 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
Dan Carpenter2ef04f42011-11-29 09:09:09 +0300806 if (!neigh_old)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200807 goto out;
808
Antonio Quartulli0538f752013-09-02 12:15:01 +0200809 if (curr_tq_avg - neigh_old->bat_iv.tq_avg > BATADV_GW_THRESHOLD)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200810 out_of_range = true;
811
812out:
813 if (orig_dst_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200814 batadv_orig_node_free_ref(orig_dst_node);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200815 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200816 batadv_gw_node_free_ref(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800817 if (gw_node)
818 batadv_gw_node_free_ref(gw_node);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200819 if (neigh_old)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200820 batadv_neigh_node_free_ref(neigh_old);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200821 if (neigh_curr)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200822 batadv_neigh_node_free_ref(neigh_curr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200823 return out_of_range;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824}