blob: 356417379e960d923f39620f4d234f97b1436d0f [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
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
208 /* if batman-adv is switching the gw client mode off and a gateway was
209 * already selected, send a DEL uevent
210 */
211 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
212
213 batadv_gw_node_free_ref(curr_gw);
214}
215
Sven Eckelmann56303d32012-06-05 22:31:31 +0200216void batadv_gw_election(struct batadv_priv *bat_priv)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200217{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200218 struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL;
219 struct batadv_neigh_node *router = NULL;
Antonio Quartulli19595e02011-06-12 11:58:58 +0200220 char gw_addr[18] = { '\0' };
Antonio Quartulli2265c142011-04-27 00:22:00 +0200221
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200222 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
Antonio Quartulli2265c142011-04-27 00:22:00 +0200223 goto out;
224
Sven Eckelmann1409a832012-05-12 18:33:55 +0200225 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200226
Sven Eckelmann807736f2012-07-15 22:26:51 +0200227 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
Marek Lindnercaa0bf62012-08-04 04:13:26 +0000228 goto out;
229
Sven Eckelmann1409a832012-05-12 18:33:55 +0200230 next_gw = batadv_gw_get_best_gw_node(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200231
232 if (curr_gw == next_gw)
233 goto out;
234
235 if (next_gw) {
Antonio Quartulli19595e02011-06-12 11:58:58 +0200236 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
237
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200238 router = batadv_orig_node_get_router(next_gw->orig_node);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200239 if (!router) {
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200240 batadv_gw_deselect(bat_priv);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200241 goto out;
242 }
243 }
244
245 if ((curr_gw) && (!next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200246 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200247 "Removing selected gateway - no gateway in range\n");
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200248 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
249 NULL);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200250 } else if ((!curr_gw) && (next_gw)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200251 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800252 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200253 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800254 next_gw->bandwidth_down / 10,
255 next_gw->bandwidth_down % 10,
256 next_gw->bandwidth_up / 10,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200257 next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200258 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
259 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200260 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200261 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800262 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200263 next_gw->orig_node->orig,
Marek Lindner414254e2013-04-23 21:39:58 +0800264 next_gw->bandwidth_down / 10,
265 next_gw->bandwidth_down % 10,
266 next_gw->bandwidth_up / 10,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200267 next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200268 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
269 gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200270 }
271
Sven Eckelmann1409a832012-05-12 18:33:55 +0200272 batadv_gw_select(bat_priv, next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200273
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100274out:
275 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200276 batadv_gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200277 if (next_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200278 batadv_gw_node_free_ref(next_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200279 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200280 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281}
282
Sven Eckelmann56303d32012-06-05 22:31:31 +0200283void batadv_gw_check_election(struct batadv_priv *bat_priv,
284 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200286 struct batadv_orig_node *curr_gw_orig;
287 struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288 uint8_t gw_tq_avg, orig_tq_avg;
289
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200290 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000291 if (!curr_gw_orig)
292 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200294 router_gw = batadv_orig_node_get_router(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000295 if (!router_gw)
296 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297
298 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000299 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000300 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200302 router_orig = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000303 if (!router_orig)
304 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305
Antonio Quartulli0538f752013-09-02 12:15:01 +0200306 gw_tq_avg = router_gw->bat_iv.tq_avg;
307 orig_tq_avg = router_orig->bat_iv.tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308
309 /* the TQ value has to be better */
310 if (orig_tq_avg < gw_tq_avg)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000311 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200313 /* if the routing class is greater than 3 the value tells us how much
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314 * greater the TQ value of the new gateway must be
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200315 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
317 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000318 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200320 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200321 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
322 gw_tq_avg, orig_tq_avg);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323
324deselect:
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200325 batadv_gw_deselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000326out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000327 if (curr_gw_orig)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200328 batadv_orig_node_free_ref(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000329 if (router_gw)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200330 batadv_neigh_node_free_ref(router_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000331 if (router_orig)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200332 batadv_neigh_node_free_ref(router_orig);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000333
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000334 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335}
336
Marek Lindner414254e2013-04-23 21:39:58 +0800337/**
338 * batadv_gw_node_add - add gateway node to list of available gateways
339 * @bat_priv: the bat priv with all the soft interface information
340 * @orig_node: originator announcing gateway capabilities
341 * @gateway: announced bandwidth information
342 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200343static void batadv_gw_node_add(struct batadv_priv *bat_priv,
344 struct batadv_orig_node *orig_node,
Marek Lindner414254e2013-04-23 21:39:58 +0800345 struct batadv_tvlv_gateway_data *gateway)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200347 struct batadv_gw_node *gw_node;
Marek Lindner414254e2013-04-23 21:39:58 +0800348
349 if (gateway->bandwidth_down == 0)
350 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351
Sven Eckelmann704509b2011-05-14 23:14:54 +0200352 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353 if (!gw_node)
354 return;
355
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356 INIT_HLIST_NODE(&gw_node->list);
357 gw_node->orig_node = orig_node;
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000358 atomic_set(&gw_node->refcount, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
Sven Eckelmann807736f2012-07-15 22:26:51 +0200360 spin_lock_bh(&bat_priv->gw.list_lock);
361 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
362 spin_unlock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200364 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindner414254e2013-04-23 21:39:58 +0800365 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
366 orig_node->orig,
367 ntohl(gateway->bandwidth_down) / 10,
368 ntohl(gateway->bandwidth_down) % 10,
369 ntohl(gateway->bandwidth_up) / 10,
370 ntohl(gateway->bandwidth_up) % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371}
372
Marek Lindner414254e2013-04-23 21:39:58 +0800373/**
374 * batadv_gw_node_get - retrieve gateway node from list of available gateways
375 * @bat_priv: the bat priv with all the soft interface information
376 * @orig_node: originator announcing gateway capabilities
377 *
378 * Returns gateway node if found or NULL otherwise.
379 */
380static struct batadv_gw_node *
381batadv_gw_node_get(struct batadv_priv *bat_priv,
382 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383{
Marek Lindner414254e2013-04-23 21:39:58 +0800384 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385
386 rcu_read_lock();
Marek Lindner414254e2013-04-23 21:39:58 +0800387 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
388 if (gw_node_tmp->orig_node != orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389 continue;
390
Marek Lindner414254e2013-04-23 21:39:58 +0800391 if (gw_node_tmp->deleted)
392 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393
Marek Lindner414254e2013-04-23 21:39:58 +0800394 if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
395 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396
Marek Lindner414254e2013-04-23 21:39:58 +0800397 gw_node = gw_node_tmp;
398 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399 }
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100400 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200401
Marek Lindner414254e2013-04-23 21:39:58 +0800402 return gw_node;
403}
404
405/**
406 * batadv_gw_node_update - update list of available gateways with changed
407 * bandwidth information
408 * @bat_priv: the bat priv with all the soft interface information
409 * @orig_node: originator announcing gateway capabilities
410 * @gateway: announced bandwidth information
411 */
412void batadv_gw_node_update(struct batadv_priv *bat_priv,
413 struct batadv_orig_node *orig_node,
414 struct batadv_tvlv_gateway_data *gateway)
415{
416 struct batadv_gw_node *gw_node, *curr_gw = NULL;
417
418 gw_node = batadv_gw_node_get(bat_priv, orig_node);
419 if (!gw_node) {
420 batadv_gw_node_add(bat_priv, orig_node, gateway);
421 goto out;
422 }
423
424 if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
425 (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
426 goto out;
427
428 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
429 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
430 orig_node->orig,
431 gw_node->bandwidth_down / 10,
432 gw_node->bandwidth_down % 10,
433 gw_node->bandwidth_up / 10,
434 gw_node->bandwidth_up % 10,
435 ntohl(gateway->bandwidth_down) / 10,
436 ntohl(gateway->bandwidth_down) % 10,
437 ntohl(gateway->bandwidth_up) / 10,
438 ntohl(gateway->bandwidth_up) % 10);
439
440 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
441 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
442
443 gw_node->deleted = 0;
444 if (ntohl(gateway->bandwidth_down) == 0) {
445 gw_node->deleted = jiffies;
446 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
447 "Gateway %pM removed from gateway list\n",
448 orig_node->orig);
449
450 /* Note: We don't need a NULL check here, since curr_gw never
451 * gets dereferenced.
452 */
453 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
454 if (gw_node == curr_gw)
455 batadv_gw_deselect(bat_priv);
456 }
457
458out:
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100459 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200460 batadv_gw_node_free_ref(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800461 if (gw_node)
462 batadv_gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463}
464
Sven Eckelmann56303d32012-06-05 22:31:31 +0200465void batadv_gw_node_delete(struct batadv_priv *bat_priv,
466 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467{
Marek Lindner414254e2013-04-23 21:39:58 +0800468 struct batadv_tvlv_gateway_data gateway;
469
470 gateway.bandwidth_down = 0;
471 gateway.bandwidth_up = 0;
472
473 batadv_gw_node_update(bat_priv, orig_node, &gateway);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474}
475
Sven Eckelmann56303d32012-06-05 22:31:31 +0200476void batadv_gw_node_purge(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200478 struct batadv_gw_node *gw_node, *curr_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800479 struct hlist_node *node_tmp;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200480 unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200481 int do_deselect = 0;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100482
Sven Eckelmann1409a832012-05-12 18:33:55 +0200483 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484
Sven Eckelmann807736f2012-07-15 22:26:51 +0200485 spin_lock_bh(&bat_priv->gw.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486
Sasha Levinb67bfe02013-02-27 17:06:00 -0800487 hlist_for_each_entry_safe(gw_node, node_tmp,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200488 &bat_priv->gw.list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489 if (((!gw_node->deleted) ||
490 (time_before(jiffies, gw_node->deleted + timeout))) &&
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200491 atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492 continue;
493
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100494 if (curr_gw == gw_node)
495 do_deselect = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
497 hlist_del_rcu(&gw_node->list);
Sven Eckelmann1409a832012-05-12 18:33:55 +0200498 batadv_gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499 }
500
Sven Eckelmann807736f2012-07-15 22:26:51 +0200501 spin_unlock_bh(&bat_priv->gw.list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100502
503 /* gw_deselect() needs to acquire the gw_list_lock */
504 if (do_deselect)
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200505 batadv_gw_deselect(bat_priv);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100506
507 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200508 batadv_gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509}
510
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200511/* fails if orig_node has no router */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200512static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
513 struct seq_file *seq,
514 const struct batadv_gw_node *gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200516 struct batadv_gw_node *curr_gw;
517 struct batadv_neigh_node *router;
Marek Lindner414254e2013-04-23 21:39:58 +0800518 int ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200520 router = batadv_orig_node_get_router(gw_node->orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000521 if (!router)
522 goto out;
523
Sven Eckelmann1409a832012-05-12 18:33:55 +0200524 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000525
Marek Lindner414254e2013-04-23 21:39:58 +0800526 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100527 (curr_gw == gw_node ? "=>" : " "),
528 gw_node->orig_node->orig,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200529 router->bat_iv.tq_avg, router->addr,
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100530 router->if_incoming->net_dev->name,
Marek Lindner414254e2013-04-23 21:39:58 +0800531 gw_node->bandwidth_down / 10,
532 gw_node->bandwidth_down % 10,
533 gw_node->bandwidth_up / 10,
534 gw_node->bandwidth_up % 10);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000535
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200536 batadv_neigh_node_free_ref(router);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100537 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200538 batadv_gw_node_free_ref(curr_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000539out:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000540 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541}
542
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200543int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544{
545 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200546 struct batadv_priv *bat_priv = netdev_priv(net_dev);
547 struct batadv_hard_iface *primary_if;
548 struct batadv_gw_node *gw_node;
Marek Lindner30da63a2012-08-03 17:15:46 +0200549 int gw_count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
Marek Lindner30da63a2012-08-03 17:15:46 +0200551 primary_if = batadv_seq_print_text_primary_if_get(seq);
552 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200553 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100555 seq_printf(seq,
Marek Lindner414254e2013-04-23 21:39:58 +0800556 " %-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 +0200557 "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
558 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200559 primary_if->net_dev->dev_addr, net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560
561 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800562 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563 if (gw_node->deleted)
564 continue;
565
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000566 /* fails if orig_node has no router */
Sven Eckelmann1409a832012-05-12 18:33:55 +0200567 if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568 continue;
569
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570 gw_count++;
571 }
572 rcu_read_unlock();
573
574 if (gw_count == 0)
Antonio Quartulli0c814652013-03-21 09:23:29 +0100575 seq_puts(seq, "No gateways in range ...\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576
Marek Lindner32ae9b22011-04-20 15:40:58 +0200577out:
578 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200579 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200580 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581}
582
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200583/* this call might reallocate skb data */
Sven Eckelmann1409a832012-05-12 18:33:55 +0200584static bool batadv_is_type_dhcprequest(struct sk_buff *skb, int header_len)
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200585{
586 int ret = false;
587 unsigned char *p;
588 int pkt_len;
589
590 if (skb_linearize(skb) < 0)
591 goto out;
592
593 pkt_len = skb_headlen(skb);
594
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200595 if (pkt_len < header_len + BATADV_DHCP_OPTIONS_OFFSET + 1)
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200596 goto out;
597
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200598 p = skb->data + header_len + BATADV_DHCP_OPTIONS_OFFSET;
599 pkt_len -= header_len + BATADV_DHCP_OPTIONS_OFFSET + 1;
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200600
601 /* Access the dhcp option lists. Each entry is made up by:
Antonio Quartulli015758d2011-07-09 17:52:13 +0200602 * - octet 1: option type
603 * - octet 2: option data len (only if type != 255 and 0)
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200604 * - octet 3: option data
605 */
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200606 while (*p != 255 && !ret) {
Antonio Quartulli015758d2011-07-09 17:52:13 +0200607 /* p now points to the first octet: option type */
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200608 if (*p == 53) {
609 /* type 53 is the message type option.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200610 * Jump the len octet and go to the data octet
611 */
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200612 if (pkt_len < 2)
613 goto out;
614 p += 2;
615
616 /* check if the message type is what we need */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200617 if (*p == BATADV_DHCP_REQUEST)
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200618 ret = true;
619 break;
620 } else if (*p == 0) {
621 /* option type 0 (padding), just go forward */
622 if (pkt_len < 1)
623 goto out;
624 pkt_len--;
625 p++;
626 } else {
627 /* This is any other option. So we get the length... */
628 if (pkt_len < 1)
629 goto out;
630 pkt_len--;
631 p++;
632
633 /* ...and then we jump over the data */
Antonio Quartulli9205cc52012-02-27 11:29:53 +0100634 if (pkt_len < 1 + (*p))
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200635 goto out;
Antonio Quartulli9205cc52012-02-27 11:29:53 +0100636 pkt_len -= 1 + (*p);
637 p += 1 + (*p);
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200638 }
639 }
640out:
641 return ret;
642}
643
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200644/* this call might reallocate skb data */
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200645bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000646{
647 struct ethhdr *ethhdr;
648 struct iphdr *iphdr;
649 struct ipv6hdr *ipv6hdr;
650 struct udphdr *udphdr;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200651 struct vlan_ethhdr *vhdr;
652 __be16 proto;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653
654 /* check for ethernet header */
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200655 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
656 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000657 ethhdr = (struct ethhdr *)skb->data;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200658 proto = ethhdr->h_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200659 *header_len += ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000660
661 /* check for initial vlan header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200662 if (proto == htons(ETH_P_8021Q)) {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200663 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
664 return false;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200665
666 vhdr = (struct vlan_ethhdr *)skb->data;
667 proto = vhdr->h_vlan_encapsulated_proto;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200668 *header_len += VLAN_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669 }
670
671 /* check for ip header */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200672 switch (proto) {
673 case htons(ETH_P_IP):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200674 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
675 return false;
676 iphdr = (struct iphdr *)(skb->data + *header_len);
677 *header_len += iphdr->ihl * 4;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678
679 /* check for udp header */
680 if (iphdr->protocol != IPPROTO_UDP)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200681 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682
683 break;
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200684 case htons(ETH_P_IPV6):
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200685 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
686 return false;
687 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
688 *header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689
690 /* check for udp header */
691 if (ipv6hdr->nexthdr != IPPROTO_UDP)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200692 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000693
694 break;
695 default:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200696 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697 }
698
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200699 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
700 return false;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200701
702 /* skb->data might have been reallocated by pskb_may_pull() */
703 ethhdr = (struct ethhdr *)skb->data;
704 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
705 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
706
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200707 udphdr = (struct udphdr *)(skb->data + *header_len);
708 *header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709
710 /* check for bootp port */
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200711 if ((proto == htons(ETH_P_IP)) &&
Antonio Quartulli293e9332013-05-19 12:55:16 +0200712 (udphdr->dest != htons(67)))
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200713 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714
Antonio Quartullif7f8ed52013-05-12 21:51:15 +0200715 if ((proto == htons(ETH_P_IPV6)) &&
Antonio Quartulli293e9332013-05-19 12:55:16 +0200716 (udphdr->dest != htons(547)))
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200717 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200719 return true;
720}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000721
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200722/**
723 * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
724 * @bat_priv: the bat priv with all the soft interface information
725 * @skb: the outgoing packet
726 *
727 * Check if the skb is a DHCP request and if it is sent to the current best GW
728 * server. Due to topology changes it may be the case that the GW server
729 * previously selected is not the best one anymore.
730 *
731 * Returns true if the packet destination is unicast and it is not the best gw,
732 * false otherwise.
733 *
734 * This call might reallocate skb data.
735 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200736bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200737 struct sk_buff *skb)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200738{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200739 struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
740 struct batadv_orig_node *orig_dst_node = NULL;
Marek Lindner414254e2013-04-23 21:39:58 +0800741 struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200742 struct ethhdr *ethhdr;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200743 bool ret, out_of_range = false;
744 unsigned int header_len = 0;
745 uint8_t curr_tq_avg;
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200746 unsigned short vid;
747
748 vid = batadv_get_vid(skb, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200750 ret = batadv_gw_is_dhcp_target(skb, &header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200751 if (!ret)
752 goto out;
753
Linus Lüssing9d2c9482013-08-06 20:21:15 +0200754 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200755 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
Antonio Quartullibbb877e2013-06-04 12:11:42 +0200756 ethhdr->h_dest, vid);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200757 if (!orig_dst_node)
758 goto out;
759
Marek Lindner414254e2013-04-23 21:39:58 +0800760 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
761 if (!gw_node->bandwidth_down == 0)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200762 goto out;
763
Sven Eckelmann1409a832012-05-12 18:33:55 +0200764 ret = batadv_is_type_dhcprequest(skb, header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200765 if (!ret)
766 goto out;
767
768 switch (atomic_read(&bat_priv->gw_mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200769 case BATADV_GW_MODE_SERVER:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200770 /* If we are a GW then we are our best GW. We can artificially
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200771 * set the tq towards ourself as the maximum value
772 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200773 curr_tq_avg = BATADV_TQ_MAX_VALUE;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200774 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200775 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann1409a832012-05-12 18:33:55 +0200776 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200777 if (!curr_gw)
778 goto out;
779
780 /* packet is going to our gateway */
781 if (curr_gw->orig_node == orig_dst_node)
782 goto out;
783
784 /* If the dhcp packet has been sent to a different gw,
785 * we have to evaluate whether the old gw is still
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200786 * reliable enough
787 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200788 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
789 NULL);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200790 if (!neigh_curr)
791 goto out;
792
Antonio Quartulli0538f752013-09-02 12:15:01 +0200793 curr_tq_avg = neigh_curr->bat_iv.tq_avg;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200794 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200795 case BATADV_GW_MODE_OFF:
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200796 default:
797 goto out;
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200798 }
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200799
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200800 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
Dan Carpenter2ef04f42011-11-29 09:09:09 +0300801 if (!neigh_old)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200802 goto out;
803
Antonio Quartulli0538f752013-09-02 12:15:01 +0200804 if (curr_tq_avg - neigh_old->bat_iv.tq_avg > BATADV_GW_THRESHOLD)
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200805 out_of_range = true;
806
807out:
808 if (orig_dst_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200809 batadv_orig_node_free_ref(orig_dst_node);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200810 if (curr_gw)
Sven Eckelmann1409a832012-05-12 18:33:55 +0200811 batadv_gw_node_free_ref(curr_gw);
Marek Lindner414254e2013-04-23 21:39:58 +0800812 if (gw_node)
813 batadv_gw_node_free_ref(gw_node);
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200814 if (neigh_old)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200815 batadv_neigh_node_free_ref(neigh_old);
Antonio Quartulli43676ab52011-04-26 21:31:45 +0200816 if (neigh_curr)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200817 batadv_neigh_node_free_ref(neigh_curr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200818 return out_of_range;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819}