blob: 61605a0f3f39464b8e87a7512c97caf23de59504 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2009-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "gateway_client.h"
24#include "gateway_common.h"
25#include "hard-interface.h"
Linus Lüssing57f0c072011-03-14 22:43:33 +000026#include "originator.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include <linux/ip.h>
28#include <linux/ipv6.h>
29#include <linux/udp.h>
30#include <linux/if_vlan.h>
31
Marek Lindner25b6d3c2011-02-10 14:33:49 +000032static void gw_node_free_ref(struct gw_node *gw_node)
33{
34 if (atomic_dec_and_test(&gw_node->refcount))
Paul E. McKenneyeb340b22011-05-01 23:25:02 -070035 kfree_rcu(gw_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036}
37
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010038static struct gw_node *gw_get_selected_gw_node(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000039{
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010040 struct gw_node *gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000042 rcu_read_lock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010043 gw_node = rcu_dereference(bat_priv->curr_gw);
44 if (!gw_node)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000045 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010047 if (!atomic_inc_not_zero(&gw_node->refcount))
48 gw_node = NULL;
49
50out:
51 rcu_read_unlock();
52 return gw_node;
53}
54
55struct orig_node *gw_get_selected_orig(struct bat_priv *bat_priv)
56{
57 struct gw_node *gw_node;
58 struct orig_node *orig_node = NULL;
59
60 gw_node = gw_get_selected_gw_node(bat_priv);
61 if (!gw_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +000062 goto out;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000063
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010064 rcu_read_lock();
65 orig_node = gw_node->orig_node;
66 if (!orig_node)
67 goto unlock;
68
Marek Lindner7b36e8e2011-02-18 12:28:10 +000069 if (!atomic_inc_not_zero(&orig_node->refcount))
70 orig_node = NULL;
Linus Lüssing43c70ad2011-02-13 21:13:04 +000071
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010072unlock:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000073 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010074out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075 if (gw_node)
Marek Lindner25b6d3c2011-02-10 14:33:49 +000076 gw_node_free_ref(gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010077 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000078}
79
Marek Lindner25b6d3c2011-02-10 14:33:49 +000080static void gw_select(struct bat_priv *bat_priv, struct gw_node *new_gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081{
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000082 struct gw_node *curr_gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010084 spin_lock_bh(&bat_priv->gw_list_lock);
85
Marek Lindner25b6d3c2011-02-10 14:33:49 +000086 if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
87 new_gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000088
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010089 curr_gw_node = bat_priv->curr_gw;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000090 rcu_assign_pointer(bat_priv->curr_gw, new_gw_node);
Marek Lindner25b6d3c2011-02-10 14:33:49 +000091
92 if (curr_gw_node)
93 gw_node_free_ref(curr_gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010094
95 spin_unlock_bh(&bat_priv->gw_list_lock);
96}
97
98void gw_deselect(struct bat_priv *bat_priv)
99{
100 gw_select(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000101}
102
103void gw_election(struct bat_priv *bat_priv)
104{
105 struct hlist_node *node;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100106 struct gw_node *gw_node, *curr_gw = NULL, *curr_gw_tmp = NULL;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000107 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108 uint8_t max_tq = 0;
109 uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
110 int down, up;
111
112 /**
113 * The batman daemon checks here if we already passed a full originator
114 * cycle in order to make sure we don't choose the first gateway we
115 * hear about. This check is based on the daemon's uptime which we
116 * don't have.
117 **/
118 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
119 return;
120
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100121 curr_gw = gw_get_selected_gw_node(bat_priv);
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200122 if (curr_gw)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100123 goto out;
124
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125 rcu_read_lock();
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000126 if (hlist_empty(&bat_priv->gw_list)) {
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100127 bat_dbg(DBG_BATMAN, bat_priv,
128 "Removing selected gateway - "
129 "no gateway in range\n");
130 gw_deselect(bat_priv);
131 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132 }
133
134 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
Linus Lüssinge1a53822011-03-14 22:43:37 +0000135 if (gw_node->deleted)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136 continue;
137
Linus Lüssinge1a53822011-03-14 22:43:37 +0000138 router = orig_node_get_router(gw_node->orig_node);
139 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140 continue;
141
142 switch (atomic_read(&bat_priv->gw_sel_class)) {
143 case 1: /* fast connection */
144 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags,
145 &down, &up);
146
Linus Lüssinge1a53822011-03-14 22:43:37 +0000147 tmp_gw_factor = (router->tq_avg * router->tq_avg *
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000148 down * 100 * 100) /
149 (TQ_LOCAL_WINDOW_SIZE *
150 TQ_LOCAL_WINDOW_SIZE * 64);
151
152 if ((tmp_gw_factor > max_gw_factor) ||
153 ((tmp_gw_factor == max_gw_factor) &&
Linus Lüssinge1a53822011-03-14 22:43:37 +0000154 (router->tq_avg > max_tq)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 curr_gw_tmp = gw_node;
156 break;
157
158 default: /**
159 * 2: stable connection (use best statistic)
160 * 3: fast-switch (use best statistic but change as
161 * soon as a better gateway appears)
162 * XX: late-switch (use best statistic but change as
163 * soon as a better gateway appears which has
164 * $routing_class more tq points)
165 **/
Linus Lüssinge1a53822011-03-14 22:43:37 +0000166 if (router->tq_avg > max_tq)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167 curr_gw_tmp = gw_node;
168 break;
169 }
170
Linus Lüssinge1a53822011-03-14 22:43:37 +0000171 if (router->tq_avg > max_tq)
172 max_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173
174 if (tmp_gw_factor > max_gw_factor)
175 max_gw_factor = tmp_gw_factor;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000176
177 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178 }
179
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000180 if (curr_gw != curr_gw_tmp) {
Linus Lüssinge1a53822011-03-14 22:43:37 +0000181 router = orig_node_get_router(curr_gw_tmp->orig_node);
182 if (!router)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100183 goto unlock;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000184
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000185 if ((curr_gw) && (!curr_gw_tmp))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186 bat_dbg(DBG_BATMAN, bat_priv,
187 "Removing selected gateway - "
188 "no gateway in range\n");
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000189 else if ((!curr_gw) && (curr_gw_tmp))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190 bat_dbg(DBG_BATMAN, bat_priv,
191 "Adding route to gateway %pM "
192 "(gw_flags: %i, tq: %i)\n",
193 curr_gw_tmp->orig_node->orig,
194 curr_gw_tmp->orig_node->gw_flags,
Linus Lüssinge1a53822011-03-14 22:43:37 +0000195 router->tq_avg);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196 else
197 bat_dbg(DBG_BATMAN, bat_priv,
198 "Changing route to gateway %pM "
199 "(gw_flags: %i, tq: %i)\n",
200 curr_gw_tmp->orig_node->orig,
201 curr_gw_tmp->orig_node->gw_flags,
Linus Lüssinge1a53822011-03-14 22:43:37 +0000202 router->tq_avg);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000203
Linus Lüssinge1a53822011-03-14 22:43:37 +0000204 neigh_node_free_ref(router);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000205 gw_select(bat_priv, curr_gw_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206 }
207
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100208unlock:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100210out:
211 if (curr_gw)
212 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213}
214
215void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
216{
Linus Lüssing57f0c072011-03-14 22:43:33 +0000217 struct orig_node *curr_gw_orig;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000218 struct neigh_node *router_gw = NULL, *router_orig = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219 uint8_t gw_tq_avg, orig_tq_avg;
220
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100221 curr_gw_orig = gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000222 if (!curr_gw_orig)
223 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224
Linus Lüssinge1a53822011-03-14 22:43:37 +0000225 router_gw = orig_node_get_router(curr_gw_orig);
226 if (!router_gw)
227 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
229 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000230 if (curr_gw_orig == orig_node)
Linus Lüssinge1a53822011-03-14 22:43:37 +0000231 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Linus Lüssinge1a53822011-03-14 22:43:37 +0000233 router_orig = orig_node_get_router(orig_node);
234 if (!router_orig)
235 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236
Linus Lüssinge1a53822011-03-14 22:43:37 +0000237 gw_tq_avg = router_gw->tq_avg;
238 orig_tq_avg = router_orig->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239
240 /* the TQ value has to be better */
241 if (orig_tq_avg < gw_tq_avg)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000242 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243
244 /**
245 * if the routing class is greater than 3 the value tells us how much
246 * greater the TQ value of the new gateway must be
247 **/
248 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
249 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000250 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
252 bat_dbg(DBG_BATMAN, bat_priv,
253 "Restarting gateway selection: better gateway found (tq curr: "
254 "%i, tq new: %i)\n",
255 gw_tq_avg, orig_tq_avg);
256
257deselect:
258 gw_deselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000259out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000260 if (curr_gw_orig)
261 orig_node_free_ref(curr_gw_orig);
Linus Lüssinge1a53822011-03-14 22:43:37 +0000262 if (router_gw)
263 neigh_node_free_ref(router_gw);
264 if (router_orig)
265 neigh_node_free_ref(router_orig);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000266
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000267 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268}
269
270static void gw_node_add(struct bat_priv *bat_priv,
271 struct orig_node *orig_node, uint8_t new_gwflags)
272{
273 struct gw_node *gw_node;
274 int down, up;
275
276 gw_node = kmalloc(sizeof(struct gw_node), GFP_ATOMIC);
277 if (!gw_node)
278 return;
279
280 memset(gw_node, 0, sizeof(struct gw_node));
281 INIT_HLIST_NODE(&gw_node->list);
282 gw_node->orig_node = orig_node;
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000283 atomic_set(&gw_node->refcount, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284
285 spin_lock_bh(&bat_priv->gw_list_lock);
286 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
287 spin_unlock_bh(&bat_priv->gw_list_lock);
288
289 gw_bandwidth_to_kbit(new_gwflags, &down, &up);
290 bat_dbg(DBG_BATMAN, bat_priv,
291 "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n",
292 orig_node->orig, new_gwflags,
293 (down > 2048 ? down / 1024 : down),
294 (down > 2048 ? "MBit" : "KBit"),
295 (up > 2048 ? up / 1024 : up),
296 (up > 2048 ? "MBit" : "KBit"));
297}
298
299void gw_node_update(struct bat_priv *bat_priv,
300 struct orig_node *orig_node, uint8_t new_gwflags)
301{
302 struct hlist_node *node;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100303 struct gw_node *gw_node, *curr_gw;
304
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200305 /**
306 * Note: We don't need a NULL check here, since curr_gw never gets
307 * dereferenced. If curr_gw is NULL we also should not exit as we may
308 * have this gateway in our list (duplication check!) even though we
309 * have no currently selected gateway.
310 */
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100311 curr_gw = gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312
313 rcu_read_lock();
314 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
315 if (gw_node->orig_node != orig_node)
316 continue;
317
318 bat_dbg(DBG_BATMAN, bat_priv,
319 "Gateway class of originator %pM changed from "
320 "%i to %i\n",
321 orig_node->orig, gw_node->orig_node->gw_flags,
322 new_gwflags);
323
324 gw_node->deleted = 0;
325
326 if (new_gwflags == 0) {
327 gw_node->deleted = jiffies;
328 bat_dbg(DBG_BATMAN, bat_priv,
329 "Gateway %pM removed from gateway list\n",
330 orig_node->orig);
331
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100332 if (gw_node == curr_gw)
333 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 }
335
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100336 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338
339 if (new_gwflags == 0)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100340 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341
342 gw_node_add(bat_priv, orig_node, new_gwflags);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100343 goto unlock;
344
345deselect:
346 gw_deselect(bat_priv);
347unlock:
348 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200349
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100350 if (curr_gw)
351 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352}
353
354void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
355{
356 return gw_node_update(bat_priv, orig_node, 0);
357}
358
359void gw_node_purge(struct bat_priv *bat_priv)
360{
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100361 struct gw_node *gw_node, *curr_gw;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362 struct hlist_node *node, *node_tmp;
363 unsigned long timeout = 2 * PURGE_TIMEOUT * HZ;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100364 char do_deselect = 0;
365
366 curr_gw = gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
368 spin_lock_bh(&bat_priv->gw_list_lock);
369
370 hlist_for_each_entry_safe(gw_node, node, node_tmp,
371 &bat_priv->gw_list, list) {
372 if (((!gw_node->deleted) ||
373 (time_before(jiffies, gw_node->deleted + timeout))) &&
374 atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
375 continue;
376
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100377 if (curr_gw == gw_node)
378 do_deselect = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379
380 hlist_del_rcu(&gw_node->list);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000381 gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382 }
383
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384 spin_unlock_bh(&bat_priv->gw_list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100385
386 /* gw_deselect() needs to acquire the gw_list_lock */
387 if (do_deselect)
388 gw_deselect(bat_priv);
389
390 if (curr_gw)
391 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392}
393
Linus Lüssinge1a53822011-03-14 22:43:37 +0000394/**
395 * fails if orig_node has no router
396 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397static int _write_buffer_text(struct bat_priv *bat_priv,
398 struct seq_file *seq, struct gw_node *gw_node)
399{
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000400 struct gw_node *curr_gw;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000401 struct neigh_node *router;
402 int down, up, ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403
404 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
405
Linus Lüssinge1a53822011-03-14 22:43:37 +0000406 router = orig_node_get_router(gw_node->orig_node);
407 if (!router)
408 goto out;
409
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100410 curr_gw = gw_get_selected_gw_node(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000411
412 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100413 (curr_gw == gw_node ? "=>" : " "),
414 gw_node->orig_node->orig,
415 router->tq_avg, router->addr,
416 router->if_incoming->net_dev->name,
417 gw_node->orig_node->gw_flags,
418 (down > 2048 ? down / 1024 : down),
419 (down > 2048 ? "MBit" : "KBit"),
420 (up > 2048 ? up / 1024 : up),
421 (up > 2048 ? "MBit" : "KBit"));
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000422
Linus Lüssinge1a53822011-03-14 22:43:37 +0000423 neigh_node_free_ref(router);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100424 if (curr_gw)
425 gw_node_free_ref(curr_gw);
Linus Lüssinge1a53822011-03-14 22:43:37 +0000426out:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000427 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428}
429
430int gw_client_seq_print_text(struct seq_file *seq, void *offset)
431{
432 struct net_device *net_dev = (struct net_device *)seq->private;
433 struct bat_priv *bat_priv = netdev_priv(net_dev);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200434 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435 struct gw_node *gw_node;
436 struct hlist_node *node;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200437 int gw_count = 0, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438
Marek Lindner32ae9b22011-04-20 15:40:58 +0200439 primary_if = primary_if_get_selected(bat_priv);
440 if (!primary_if) {
441 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
442 "specify interfaces to enable it\n",
443 net_dev->name);
444 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445 }
446
Marek Lindner32ae9b22011-04-20 15:40:58 +0200447 if (primary_if->if_status != IF_ACTIVE) {
448 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
449 "primary interface not active\n",
450 net_dev->name);
451 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 }
453
454 seq_printf(seq, " %-12s (%s/%i) %17s [%10s]: gw_class ... "
455 "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
456 "Gateway", "#", TQ_MAX_VALUE, "Nexthop",
457 "outgoingIF", SOURCE_VERSION, REVISION_VERSION_STR,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200458 primary_if->net_dev->name,
459 primary_if->net_dev->dev_addr, net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460
461 rcu_read_lock();
462 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
463 if (gw_node->deleted)
464 continue;
465
Linus Lüssinge1a53822011-03-14 22:43:37 +0000466 /* fails if orig_node has no router */
467 if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468 continue;
469
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470 gw_count++;
471 }
472 rcu_read_unlock();
473
474 if (gw_count == 0)
475 seq_printf(seq, "No gateways in range ...\n");
476
Marek Lindner32ae9b22011-04-20 15:40:58 +0200477out:
478 if (primary_if)
479 hardif_free_ref(primary_if);
480 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000481}
482
483int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
484{
485 struct ethhdr *ethhdr;
486 struct iphdr *iphdr;
487 struct ipv6hdr *ipv6hdr;
488 struct udphdr *udphdr;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100489 struct gw_node *curr_gw;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490 unsigned int header_len = 0;
491
492 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
493 return 0;
494
495 /* check for ethernet header */
496 if (!pskb_may_pull(skb, header_len + ETH_HLEN))
497 return 0;
498 ethhdr = (struct ethhdr *)skb->data;
499 header_len += ETH_HLEN;
500
501 /* check for initial vlan header */
502 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
503 if (!pskb_may_pull(skb, header_len + VLAN_HLEN))
504 return 0;
505 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
506 header_len += VLAN_HLEN;
507 }
508
509 /* check for ip header */
510 switch (ntohs(ethhdr->h_proto)) {
511 case ETH_P_IP:
512 if (!pskb_may_pull(skb, header_len + sizeof(struct iphdr)))
513 return 0;
514 iphdr = (struct iphdr *)(skb->data + header_len);
515 header_len += iphdr->ihl * 4;
516
517 /* check for udp header */
518 if (iphdr->protocol != IPPROTO_UDP)
519 return 0;
520
521 break;
522 case ETH_P_IPV6:
523 if (!pskb_may_pull(skb, header_len + sizeof(struct ipv6hdr)))
524 return 0;
525 ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
526 header_len += sizeof(struct ipv6hdr);
527
528 /* check for udp header */
529 if (ipv6hdr->nexthdr != IPPROTO_UDP)
530 return 0;
531
532 break;
533 default:
534 return 0;
535 }
536
537 if (!pskb_may_pull(skb, header_len + sizeof(struct udphdr)))
538 return 0;
539 udphdr = (struct udphdr *)(skb->data + header_len);
540 header_len += sizeof(struct udphdr);
541
542 /* check for bootp port */
543 if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
544 (ntohs(udphdr->dest) != 67))
545 return 0;
546
547 if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
548 (ntohs(udphdr->dest) != 547))
549 return 0;
550
551 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
552 return -1;
553
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100554 curr_gw = gw_get_selected_gw_node(bat_priv);
555 if (!curr_gw)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100558 if (curr_gw)
559 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560 return 1;
561}