blob: 4e8f5b1eedfc2a1ce913b2a5ad5ed5e203198b1a [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 Quartulli6c413b12013-11-05 19:31:08 +010031/* These are the offsets of the "hw type" and "hw address length" in the dhcp
32 * packet starting at the beginning of the dhcp header
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020033 */
Antonio Quartulli6c413b12013-11-05 19:31:08 +010034#define BATADV_DHCP_HTYPE_OFFSET 1
35#define BATADV_DHCP_HLEN_OFFSET 2
36/* Value of htype representing Ethernet */
37#define BATADV_DHCP_HTYPE_ETHERNET 0x01
38/* This is the offset of the "chaddr" field in the dhcp packet starting at the
39 * beginning of the dhcp header
40 */
41#define BATADV_DHCP_CHADDR_OFFSET 28
Antonio Quartulli43676ab2011-04-26 21:31:45 +020042
Sven Eckelmann56303d32012-06-05 22:31:31 +020043static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node)
Marek Lindner25b6d3c2011-02-10 14:33:49 +000044{
45 if (atomic_dec_and_test(&gw_node->refcount))
Paul E. McKenneyeb340b22011-05-01 23:25:02 -070046 kfree_rcu(gw_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047}
48
Sven Eckelmann56303d32012-06-05 22:31:31 +020049static struct batadv_gw_node *
50batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000051{
Sven Eckelmann56303d32012-06-05 22:31:31 +020052 struct batadv_gw_node *gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000054 rcu_read_lock();
Sven Eckelmann807736f2012-07-15 22:26:51 +020055 gw_node = rcu_dereference(bat_priv->gw.curr_gw);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010056 if (!gw_node)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000057 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000058
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010059 if (!atomic_inc_not_zero(&gw_node->refcount))
60 gw_node = NULL;
61
62out:
63 rcu_read_unlock();
64 return gw_node;
65}
66
Sven Eckelmann56303d32012-06-05 22:31:31 +020067struct batadv_orig_node *
68batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010069{
Sven Eckelmann56303d32012-06-05 22:31:31 +020070 struct batadv_gw_node *gw_node;
71 struct batadv_orig_node *orig_node = NULL;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010072
Sven Eckelmann1409a832012-05-12 18:33:55 +020073 gw_node = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010074 if (!gw_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +000075 goto out;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000076
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010077 rcu_read_lock();
78 orig_node = gw_node->orig_node;
79 if (!orig_node)
80 goto unlock;
81
Marek Lindner7b36e8e2011-02-18 12:28:10 +000082 if (!atomic_inc_not_zero(&orig_node->refcount))
83 orig_node = NULL;
Linus Lüssing43c70ad2011-02-13 21:13:04 +000084
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010085unlock:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000086 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010087out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000088 if (gw_node)
Sven Eckelmann1409a832012-05-12 18:33:55 +020089 batadv_gw_node_free_ref(gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010090 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091}
92
Sven Eckelmann56303d32012-06-05 22:31:31 +020093static void batadv_gw_select(struct batadv_priv *bat_priv,
94 struct batadv_gw_node *new_gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000095{
Sven Eckelmann56303d32012-06-05 22:31:31 +020096 struct batadv_gw_node *curr_gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000097
Sven Eckelmann807736f2012-07-15 22:26:51 +020098 spin_lock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010099
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000100 if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
101 new_gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102
Sven Eckelmann807736f2012-07-15 22:26:51 +0200103 curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
104 rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000105
106 if (curr_gw_node)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200107 batadv_gw_node_free_ref(curr_gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100108
Sven Eckelmann807736f2012-07-15 22:26:51 +0200109 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100110}
111
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100112/**
113 * batadv_gw_reselect - force a gateway reselection
114 * @bat_priv: the bat priv with all the soft interface information
115 *
116 * Set a flag to remind the GW component to perform a new gateway reselection.
117 * However this function does not ensure that the current gateway is going to be
118 * deselected. The reselection mechanism may elect the same gateway once again.
119 *
120 * This means that invoking batadv_gw_reselect() does not guarantee a gateway
121 * change and therefore a uevent is not necessarily expected.
122 */
123void batadv_gw_reselect(struct batadv_priv *bat_priv)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100124{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200125 atomic_set(&bat_priv->gw.reselect, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126}
127
Sven Eckelmann56303d32012-06-05 22:31:31 +0200128static struct batadv_gw_node *
129batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000130{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200131 struct batadv_neigh_node *router;
Simon Wunderlich89652332013-11-13 19:14:46 +0100132 struct batadv_neigh_ifinfo *router_ifinfo;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200133 struct batadv_gw_node *gw_node, *curr_gw = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134 uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200135 uint32_t gw_divisor;
Antonio Quartulli2265c142011-04-27 00:22:00 +0200136 uint8_t max_tq = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200137 uint8_t tq_avg;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138 struct batadv_orig_node *orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000139
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200140 gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE;
141 gw_divisor *= 64;
142
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000143 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800144 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000145 if (gw_node->deleted)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146 continue;
147
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200148 orig_node = gw_node->orig_node;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200149 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000150 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151 continue;
152
Simon Wunderlich89652332013-11-13 19:14:46 +0100153 router_ifinfo = batadv_neigh_ifinfo_get(router,
154 BATADV_IF_DEFAULT);
155 if (!router_ifinfo)
156 goto next;
157
Antonio Quartulli2265c142011-04-27 00:22:00 +0200158 if (!atomic_inc_not_zero(&gw_node->refcount))
159 goto next;
160
Simon Wunderlich89652332013-11-13 19:14:46 +0100161 tq_avg = router_ifinfo->bat_iv.tq_avg;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200162
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163 switch (atomic_read(&bat_priv->gw_sel_class)) {
164 case 1: /* fast connection */
Marek Lindner414254e2013-04-23 21:39:58 +0800165 tmp_gw_factor = tq_avg * tq_avg;
166 tmp_gw_factor *= gw_node->bandwidth_down;
167 tmp_gw_factor *= 100 * 100;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200168 tmp_gw_factor /= gw_divisor;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169
170 if ((tmp_gw_factor > max_gw_factor) ||
171 ((tmp_gw_factor == max_gw_factor) &&
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200172 (tq_avg > max_tq))) {
Antonio Quartulli2265c142011-04-27 00:22:00 +0200173 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200174 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200175 curr_gw = gw_node;
176 atomic_inc(&curr_gw->refcount);
177 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178 break;
179
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200180 default: /* 2: stable connection (use best statistic)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181 * 3: fast-switch (use best statistic but change as
182 * soon as a better gateway appears)
183 * XX: late-switch (use best statistic but change as
184 * soon as a better gateway appears which has
185 * $routing_class more tq points)
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200186 */
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200187 if (tq_avg > max_tq) {
Antonio Quartulli2265c142011-04-27 00:22:00 +0200188 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200189 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200190 curr_gw = gw_node;
191 atomic_inc(&curr_gw->refcount);
192 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193 break;
194 }
195
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200196 if (tq_avg > max_tq)
197 max_tq = tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198
199 if (tmp_gw_factor > max_gw_factor)
200 max_gw_factor = tmp_gw_factor;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000201
Sven Eckelmann1409a832012-05-12 18:33:55 +0200202 batadv_gw_node_free_ref(gw_node);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200203
204next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200205 batadv_neigh_node_free_ref(router);
Simon Wunderlich89652332013-11-13 19:14:46 +0100206 if (router_ifinfo)
207 batadv_neigh_ifinfo_free_ref(router_ifinfo);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209 rcu_read_unlock();
Antonio Quartulli2265c142011-04-27 00:22:00 +0200210
211 return curr_gw;
212}
213
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200214/**
215 * batadv_gw_check_client_stop - check if client mode has been switched off
216 * @bat_priv: the bat priv with all the soft interface information
217 *
218 * This function assumes the caller has checked that the gw state *is actually
219 * changing*. This function is not supposed to be called when there is no state
220 * change.
221 */
222void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
223{
224 struct batadv_gw_node *curr_gw;
225
226 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
227 return;
228
229 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
230 if (!curr_gw)
231 return;
232
Antonio Quartullif3163182013-11-04 20:59:40 +0100233 /* deselect the current gateway so that next time that client mode is
234 * enabled a proper GW_ADD event can be sent
235 */
236 batadv_gw_select(bat_priv, NULL);
237
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200238 /* if batman-adv is switching the gw client mode off and a gateway was
239 * already selected, send a DEL uevent
240 */
241 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
242
243 batadv_gw_node_free_ref(curr_gw);
244}
245
Sven Eckelmann56303d32012-06-05 22:31:31 +0200246void batadv_gw_election(struct batadv_priv *bat_priv)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200247{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200248 struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL;
249 struct batadv_neigh_node *router = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100250 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Antonio Quartulli19595e02011-06-12 11:58:58 +0200251 char gw_addr[18] = { '\0' };
Antonio Quartulli2265c142011-04-27 00:22:00 +0200252
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200253 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200254 goto out;
255
Sven Eckelmann1409a832012-05-12 18:33:55 +0200256 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200257
Sven Eckelmann807736f2012-07-15 22:26:51 +0200258 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
Marek Lindnercaa0bf62012-08-04 04:13:26 +0000259 goto out;
260
Sven Eckelmann1409a832012-05-12 18:33:55 +0200261 next_gw = batadv_gw_get_best_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200262
263 if (curr_gw == next_gw)
264 goto out;
265
266 if (next_gw) {
Antonio Quartulli19595e02011-06-12 11:58:58 +0200267 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
268
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200269 router = batadv_orig_node_get_router(next_gw->orig_node);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200270 if (!router) {
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100271 batadv_gw_reselect(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200272 goto out;
273 }
Simon Wunderlich89652332013-11-13 19:14:46 +0100274
275 router_ifinfo = batadv_neigh_ifinfo_get(router,
276 BATADV_IF_DEFAULT);
277 if (!router_ifinfo) {
278 batadv_gw_reselect(bat_priv);
279 goto out;
280 }
Antonio Quartulli2265c142011-04-27 00:22:00 +0200281 }
282
283 if ((curr_gw) && (!next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200284 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200285 "Removing selected gateway - no gateway in range\n");
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200286 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
287 NULL);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200288 } else if ((!curr_gw) && (next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200289 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800290 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200291 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800292 next_gw->bandwidth_down / 10,
293 next_gw->bandwidth_down % 10,
294 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100295 next_gw->bandwidth_up % 10,
296 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200297 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
298 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200299 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200300 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800301 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200302 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800303 next_gw->bandwidth_down / 10,
304 next_gw->bandwidth_down % 10,
305 next_gw->bandwidth_up / 10,
Simon Wunderlich89652332013-11-13 19:14:46 +0100306 next_gw->bandwidth_up % 10,
307 router_ifinfo->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200308 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
309 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200310 }
311
Sven Eckelmann1409a832012-05-12 18:33:55 +0200312 batadv_gw_select(bat_priv, next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200313
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100314out:
315 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200316 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200317 if (next_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200318 batadv_gw_node_free_ref(next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200319 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200320 batadv_neigh_node_free_ref(router);
Simon Wunderlich89652332013-11-13 19:14:46 +0100321 if (router_ifinfo)
322 batadv_neigh_ifinfo_free_ref(router_ifinfo);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323}
324
Sven Eckelmann56303d32012-06-05 22:31:31 +0200325void batadv_gw_check_election(struct batadv_priv *bat_priv,
326 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327{
Simon Wunderlich89652332013-11-13 19:14:46 +0100328 struct batadv_neigh_ifinfo *router_orig_tq = NULL;
329 struct batadv_neigh_ifinfo *router_gw_tq = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200330 struct batadv_orig_node *curr_gw_orig;
331 struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332 uint8_t gw_tq_avg, orig_tq_avg;
333
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200334 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000335 if (!curr_gw_orig)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100336 goto reselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200338 router_gw = batadv_orig_node_get_router(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000339 if (!router_gw)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100340 goto reselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341
Simon Wunderlich89652332013-11-13 19:14:46 +0100342 router_gw_tq = batadv_neigh_ifinfo_get(router_gw,
343 BATADV_IF_DEFAULT);
344 if (!router_gw_tq)
345 goto reselect;
346
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000348 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000349 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200351 router_orig = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000352 if (!router_orig)
353 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354
Simon Wunderlich89652332013-11-13 19:14:46 +0100355 router_orig_tq = batadv_neigh_ifinfo_get(router_orig,
356 BATADV_IF_DEFAULT);
357 if (!router_orig_tq)
358 goto out;
359
360 gw_tq_avg = router_gw_tq->bat_iv.tq_avg;
361 orig_tq_avg = router_orig_tq->bat_iv.tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362
363 /* the TQ value has to be better */
364 if (orig_tq_avg < gw_tq_avg)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000365 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200367 /* if the routing class is greater than 3 the value tells us how much
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368 * greater the TQ value of the new gateway must be
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200369 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
371 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000372 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200374 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200375 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
376 gw_tq_avg, orig_tq_avg);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100378reselect:
379 batadv_gw_reselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000380out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000381 if (curr_gw_orig)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200382 batadv_orig_node_free_ref(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000383 if (router_gw)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200384 batadv_neigh_node_free_ref(router_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000385 if (router_orig)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200386 batadv_neigh_node_free_ref(router_orig);
Simon Wunderlich89652332013-11-13 19:14:46 +0100387 if (router_gw_tq)
388 batadv_neigh_ifinfo_free_ref(router_gw_tq);
389 if (router_orig_tq)
390 batadv_neigh_ifinfo_free_ref(router_orig_tq);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000391
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000392 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393}
394
Marek Lindner414254e2013-04-23 21:39:58 +0800395/**
396 * batadv_gw_node_add - add gateway node to list of available gateways
397 * @bat_priv: the bat priv with all the soft interface information
398 * @orig_node: originator announcing gateway capabilities
399 * @gateway: announced bandwidth information
400 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200401static void batadv_gw_node_add(struct batadv_priv *bat_priv,
402 struct batadv_orig_node *orig_node,
Marek Lindner414254e2013-04-23 21:39:58 +0800403 struct batadv_tvlv_gateway_data *gateway)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200405 struct batadv_gw_node *gw_node;
Marek Lindner414254e2013-04-23 21:39:58 +0800406
407 if (gateway->bandwidth_down == 0)
408 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
Sven Eckelmann704509b2011-05-14 23:14:54 +0200410 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411 if (!gw_node)
412 return;
413
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414 INIT_HLIST_NODE(&gw_node->list);
415 gw_node->orig_node = orig_node;
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000416 atomic_set(&gw_node->refcount, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417
Sven Eckelmann807736f2012-07-15 22:26:51 +0200418 spin_lock_bh(&bat_priv->gw.list_lock);
419 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
420 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200422 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800423 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
424 orig_node->orig,
425 ntohl(gateway->bandwidth_down) / 10,
426 ntohl(gateway->bandwidth_down) % 10,
427 ntohl(gateway->bandwidth_up) / 10,
428 ntohl(gateway->bandwidth_up) % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429}
430
Marek Lindner414254e2013-04-23 21:39:58 +0800431/**
432 * batadv_gw_node_get - retrieve gateway node from list of available gateways
433 * @bat_priv: the bat priv with all the soft interface information
434 * @orig_node: originator announcing gateway capabilities
435 *
436 * Returns gateway node if found or NULL otherwise.
437 */
438static struct batadv_gw_node *
439batadv_gw_node_get(struct batadv_priv *bat_priv,
440 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441{
Marek Lindner414254e2013-04-23 21:39:58 +0800442 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443
444 rcu_read_lock();
Marek Lindner414254e2013-04-23 21:39:58 +0800445 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
446 if (gw_node_tmp->orig_node != orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447 continue;
448
Marek Lindner414254e2013-04-23 21:39:58 +0800449 if (gw_node_tmp->deleted)
450 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000451
Marek Lindner414254e2013-04-23 21:39:58 +0800452 if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
453 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454
Marek Lindner414254e2013-04-23 21:39:58 +0800455 gw_node = gw_node_tmp;
456 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457 }
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100458 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200459
Marek Lindner414254e2013-04-23 21:39:58 +0800460 return gw_node;
461}
462
463/**
464 * batadv_gw_node_update - update list of available gateways with changed
465 * bandwidth information
466 * @bat_priv: the bat priv with all the soft interface information
467 * @orig_node: originator announcing gateway capabilities
468 * @gateway: announced bandwidth information
469 */
470void batadv_gw_node_update(struct batadv_priv *bat_priv,
471 struct batadv_orig_node *orig_node,
472 struct batadv_tvlv_gateway_data *gateway)
473{
474 struct batadv_gw_node *gw_node, *curr_gw = NULL;
475
476 gw_node = batadv_gw_node_get(bat_priv, orig_node);
477 if (!gw_node) {
478 batadv_gw_node_add(bat_priv, orig_node, gateway);
479 goto out;
480 }
481
482 if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
483 (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
484 goto out;
485
486 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
487 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
488 orig_node->orig,
489 gw_node->bandwidth_down / 10,
490 gw_node->bandwidth_down % 10,
491 gw_node->bandwidth_up / 10,
492 gw_node->bandwidth_up % 10,
493 ntohl(gateway->bandwidth_down) / 10,
494 ntohl(gateway->bandwidth_down) % 10,
495 ntohl(gateway->bandwidth_up) / 10,
496 ntohl(gateway->bandwidth_up) % 10);
497
498 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
499 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
500
501 gw_node->deleted = 0;
502 if (ntohl(gateway->bandwidth_down) == 0) {
503 gw_node->deleted = jiffies;
504 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
505 "Gateway %pM removed from gateway list\n",
506 orig_node->orig);
507
508 /* Note: We don't need a NULL check here, since curr_gw never
509 * gets dereferenced.
510 */
511 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
512 if (gw_node == curr_gw)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100513 batadv_gw_reselect(bat_priv);
Marek Lindner414254e2013-04-23 21:39:58 +0800514 }
515
516out:
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100517 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200518 batadv_gw_node_free_ref(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800519 if (gw_node)
520 batadv_gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000521}
522
Sven Eckelmann56303d32012-06-05 22:31:31 +0200523void batadv_gw_node_delete(struct batadv_priv *bat_priv,
524 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525{
Marek Lindner414254e2013-04-23 21:39:58 +0800526 struct batadv_tvlv_gateway_data gateway;
527
528 gateway.bandwidth_down = 0;
529 gateway.bandwidth_up = 0;
530
531 batadv_gw_node_update(bat_priv, orig_node, &gateway);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532}
533
Sven Eckelmann56303d32012-06-05 22:31:31 +0200534void batadv_gw_node_purge(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000535{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200536 struct batadv_gw_node *gw_node, *curr_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800537 struct hlist_node *node_tmp;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200538 unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100539 int do_reselect = 0;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100540
Sven Eckelmann1409a832012-05-12 18:33:55 +0200541 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542
Sven Eckelmann807736f2012-07-15 22:26:51 +0200543 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544
Sasha Levinb67bfe02013-02-27 17:06:00 -0800545 hlist_for_each_entry_safe(gw_node, node_tmp,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200546 &bat_priv->gw.list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547 if (((!gw_node->deleted) ||
548 (time_before(jiffies, gw_node->deleted + timeout))) &&
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200549 atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550 continue;
551
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100552 if (curr_gw == gw_node)
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100553 do_reselect = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
555 hlist_del_rcu(&gw_node->list);
Sven Eckelmann1409a832012-05-12 18:33:55 +0200556 batadv_gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557 }
558
Sven Eckelmann807736f2012-07-15 22:26:51 +0200559 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100560
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100561 /* gw_reselect() needs to acquire the gw_list_lock */
562 if (do_reselect)
563 batadv_gw_reselect(bat_priv);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100564
565 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200566 batadv_gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567}
568
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200569/* fails if orig_node has no router */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200570static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
571 struct seq_file *seq,
572 const struct batadv_gw_node *gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000573{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200574 struct batadv_gw_node *curr_gw;
575 struct batadv_neigh_node *router;
Simon Wunderlich89652332013-11-13 19:14:46 +0100576 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Marek Lindner414254e2013-04-23 21:39:58 +0800577 int ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000578
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200579 router = batadv_orig_node_get_router(gw_node->orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000580 if (!router)
581 goto out;
582
Simon Wunderlich89652332013-11-13 19:14:46 +0100583 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
584 if (!router_ifinfo)
585 goto out;
586
Sven Eckelmann1409a832012-05-12 18:33:55 +0200587 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000588
Marek Lindner414254e2013-04-23 21:39:58 +0800589 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100590 (curr_gw == gw_node ? "=>" : " "),
591 gw_node->orig_node->orig,
Simon Wunderlich89652332013-11-13 19:14:46 +0100592 router_ifinfo->bat_iv.tq_avg, router->addr,
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100593 router->if_incoming->net_dev->name,
Marek Lindner414254e2013-04-23 21:39:58 +0800594 gw_node->bandwidth_down / 10,
595 gw_node->bandwidth_down % 10,
596 gw_node->bandwidth_up / 10,
597 gw_node->bandwidth_up % 10);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000598
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100599 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200600 batadv_gw_node_free_ref(curr_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000601out:
Simon Wunderlich89652332013-11-13 19:14:46 +0100602 if (router_ifinfo)
603 batadv_neigh_ifinfo_free_ref(router_ifinfo);
604 if (router)
605 batadv_neigh_node_free_ref(router);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000606 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607}
608
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200609int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610{
611 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200612 struct batadv_priv *bat_priv = netdev_priv(net_dev);
613 struct batadv_hard_iface *primary_if;
614 struct batadv_gw_node *gw_node;
Marek Lindner30da63a2012-08-03 17:15:46 +0200615 int gw_count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000616
Marek Lindner30da63a2012-08-03 17:15:46 +0200617 primary_if = batadv_seq_print_text_primary_if_get(seq);
618 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200619 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100621 seq_printf(seq,
Marek Lindner414254e2013-04-23 21:39:58 +0800622 " %-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 +0200623 "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
624 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200625 primary_if->net_dev->dev_addr, net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626
627 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800628 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 if (gw_node->deleted)
630 continue;
631
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000632 /* fails if orig_node has no router */
Sven Eckelmann1409a832012-05-12 18:33:55 +0200633 if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634 continue;
635
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636 gw_count++;
637 }
638 rcu_read_unlock();
639
640 if (gw_count == 0)
Antonio Quartulli0c814652013-03-21 09:23:29 +0100641 seq_puts(seq, "No gateways in range ...\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642
Marek Lindner32ae9b22011-04-20 15:40:58 +0200643out:
644 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200645 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200646 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000647}
648
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100649/**
650 * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
651 * @skb: the packet to check
652 * @header_len: a pointer to the batman-adv header size
653 * @chaddr: buffer where the client address will be stored. Valid
654 * only if the function returns BATADV_DHCP_TO_CLIENT
655 *
656 * Returns:
657 * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
658 * while parsing it
659 * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
660 * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
661 *
662 * This function may re-allocate the data buffer of the skb passed as argument.
663 */
664enum batadv_dhcp_recipient
665batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
666 uint8_t *chaddr)
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200667{
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100668 enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669 struct ethhdr *ethhdr;
670 struct iphdr *iphdr;
671 struct ipv6hdr *ipv6hdr;
672 struct udphdr *udphdr;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200673 struct vlan_ethhdr *vhdr;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100674 int chaddr_offset;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200675 __be16 proto;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100676 uint8_t *p;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000677
678 /* check for ethernet header */
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200679 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100680 return BATADV_DHCP_NO;
681
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682 ethhdr = (struct ethhdr *)skb->data;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200683 proto = ethhdr->h_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200684 *header_len += ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685
686 /* check for initial vlan header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200687 if (proto == htons(ETH_P_8021Q)) {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200688 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100689 return BATADV_DHCP_NO;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200690
691 vhdr = (struct vlan_ethhdr *)skb->data;
692 proto = vhdr->h_vlan_encapsulated_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200693 *header_len += VLAN_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694 }
695
696 /* check for ip header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200697 switch (proto) {
698 case htons(ETH_P_IP):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200699 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100700 return BATADV_DHCP_NO;
701
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200702 iphdr = (struct iphdr *)(skb->data + *header_len);
703 *header_len += iphdr->ihl * 4;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000704
705 /* check for udp header */
706 if (iphdr->protocol != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100707 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000708
709 break;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200710 case htons(ETH_P_IPV6):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200711 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100712 return BATADV_DHCP_NO;
713
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200714 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
715 *header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716
717 /* check for udp header */
718 if (ipv6hdr->nexthdr != IPPROTO_UDP)
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100719 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720
721 break;
722 default:
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100723 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724 }
725
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200726 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100727 return BATADV_DHCP_NO;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200728
729 /* skb->data might have been reallocated by pskb_may_pull() */
730 ethhdr = (struct ethhdr *)skb->data;
731 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
732 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
733
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200734 udphdr = (struct udphdr *)(skb->data + *header_len);
735 *header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000736
737 /* check for bootp port */
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100738 switch (proto) {
739 case htons(ETH_P_IP):
740 if (udphdr->dest == htons(67))
741 ret = BATADV_DHCP_TO_SERVER;
742 else if (udphdr->source == htons(67))
743 ret = BATADV_DHCP_TO_CLIENT;
744 break;
745 case htons(ETH_P_IPV6):
746 if (udphdr->dest == htons(547))
747 ret = BATADV_DHCP_TO_SERVER;
748 else if (udphdr->source == htons(547))
749 ret = BATADV_DHCP_TO_CLIENT;
750 break;
751 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100753 chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
754 /* store the client address if the message is going to a client */
755 if (ret == BATADV_DHCP_TO_CLIENT &&
756 pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
757 /* check if the DHCP packet carries an Ethernet DHCP */
758 p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
759 if (*p != BATADV_DHCP_HTYPE_ETHERNET)
760 return BATADV_DHCP_NO;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100762 /* check if the DHCP packet carries a valid Ethernet address */
763 p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
764 if (*p != ETH_ALEN)
765 return BATADV_DHCP_NO;
766
767 memcpy(chaddr, skb->data + chaddr_offset, ETH_ALEN);
768 }
769
770 return ret;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200771}
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200772/**
773 * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
774 * @bat_priv: the bat priv with all the soft interface information
775 * @skb: the outgoing packet
776 *
777 * Check if the skb is a DHCP request and if it is sent to the current best GW
778 * server. Due to topology changes it may be the case that the GW server
779 * previously selected is not the best one anymore.
780 *
781 * Returns true if the packet destination is unicast and it is not the best gw,
782 * false otherwise.
783 *
784 * This call might reallocate skb data.
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100785 * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200786 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200787bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200788 struct sk_buff *skb)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200789{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200790 struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
791 struct batadv_orig_node *orig_dst_node = NULL;
Marek Lindner414254e2013-04-23 21:39:58 +0800792 struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +0100793 struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
Antonio Quartulli6c413b12013-11-05 19:31:08 +0100794 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
795 bool out_of_range = false;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200796 uint8_t curr_tq_avg;
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200797 unsigned short vid;
798
799 vid = batadv_get_vid(skb, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000800
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200801 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200802 ethhdr->h_dest, vid);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200803 if (!orig_dst_node)
804 goto out;
805
Marek Lindner414254e2013-04-23 21:39:58 +0800806 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
807 if (!gw_node->bandwidth_down == 0)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200808 goto out;
809
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200810 switch (atomic_read(&bat_priv->gw_mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200811 case BATADV_GW_MODE_SERVER:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200812 /* If we are a GW then we are our best GW. We can artificially
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200813 * set the tq towards ourself as the maximum value
814 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200815 curr_tq_avg = BATADV_TQ_MAX_VALUE;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200816 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200817 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann1409a832012-05-12 18:33:55 +0200818 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200819 if (!curr_gw)
820 goto out;
821
822 /* packet is going to our gateway */
823 if (curr_gw->orig_node == orig_dst_node)
824 goto out;
825
826 /* If the dhcp packet has been sent to a different gw,
827 * we have to evaluate whether the old gw is still
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200828 * reliable enough
829 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200830 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
831 NULL);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200832 if (!neigh_curr)
833 goto out;
834
Simon Wunderlich89652332013-11-13 19:14:46 +0100835 curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
836 BATADV_IF_DEFAULT);
837 if (!curr_ifinfo)
838 goto out;
839
840 curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
841 batadv_neigh_ifinfo_free_ref(curr_ifinfo);
842
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200843 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200844 case BATADV_GW_MODE_OFF:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200845 default:
846 goto out;
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200847 }
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200848
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200849 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
Dan Carpenter2ef04f42011-11-29 09:09:09 +0300850 if (!neigh_old)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200851 goto out;
852
Simon Wunderlich89652332013-11-13 19:14:46 +0100853 old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
854 if (!old_ifinfo)
855 goto out;
856
857 if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200858 out_of_range = true;
Simon Wunderlich89652332013-11-13 19:14:46 +0100859 batadv_neigh_ifinfo_free_ref(old_ifinfo);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200860
861out:
862 if (orig_dst_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200863 batadv_orig_node_free_ref(orig_dst_node);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200864 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200865 batadv_gw_node_free_ref(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800866 if (gw_node)
867 batadv_gw_node_free_ref(gw_node);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200868 if (neigh_old)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200869 batadv_neigh_node_free_ref(neigh_old);
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200870 if (neigh_curr)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200871 batadv_neigh_node_free_ref(neigh_curr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200872 return out_of_range;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000873}