blob: 65f39530799de908f5f5a1021e3e37db152fb64c [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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032static void gw_node_free_rcu(struct rcu_head *rcu)
33{
34 struct gw_node *gw_node;
35
36 gw_node = container_of(rcu, struct gw_node, rcu);
Marek Lindner25b6d3c2011-02-10 14:33:49 +000037 kfree(gw_node);
38}
39
40static void gw_node_free_ref(struct gw_node *gw_node)
41{
42 if (atomic_dec_and_test(&gw_node->refcount))
43 call_rcu(&gw_node->rcu, gw_node_free_rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044}
45
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010046static struct gw_node *gw_get_selected_gw_node(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047{
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010048 struct gw_node *gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000050 rcu_read_lock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010051 gw_node = rcu_dereference(bat_priv->curr_gw);
52 if (!gw_node)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000053 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010055 if (!atomic_inc_not_zero(&gw_node->refcount))
56 gw_node = NULL;
57
58out:
59 rcu_read_unlock();
60 return gw_node;
61}
62
63struct orig_node *gw_get_selected_orig(struct bat_priv *bat_priv)
64{
65 struct gw_node *gw_node;
66 struct orig_node *orig_node = NULL;
67
68 gw_node = gw_get_selected_gw_node(bat_priv);
69 if (!gw_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +000070 goto out;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000071
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010072 rcu_read_lock();
73 orig_node = gw_node->orig_node;
74 if (!orig_node)
75 goto unlock;
76
Marek Lindner7b36e8e2011-02-18 12:28:10 +000077 if (!atomic_inc_not_zero(&orig_node->refcount))
78 orig_node = NULL;
Linus Lüssing43c70ad2011-02-13 21:13:04 +000079
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010080unlock:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000081 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010082out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083 if (gw_node)
Marek Lindner25b6d3c2011-02-10 14:33:49 +000084 gw_node_free_ref(gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010085 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000086}
87
Marek Lindner25b6d3c2011-02-10 14:33:49 +000088static void gw_select(struct bat_priv *bat_priv, struct gw_node *new_gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089{
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000090 struct gw_node *curr_gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010092 spin_lock_bh(&bat_priv->gw_list_lock);
93
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
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010097 curr_gw_node = bat_priv->curr_gw;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000098 rcu_assign_pointer(bat_priv->curr_gw, new_gw_node);
Marek Lindner25b6d3c2011-02-10 14:33:49 +000099
100 if (curr_gw_node)
101 gw_node_free_ref(curr_gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100102
103 spin_unlock_bh(&bat_priv->gw_list_lock);
104}
105
106void gw_deselect(struct bat_priv *bat_priv)
107{
108 gw_select(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000109}
110
111void gw_election(struct bat_priv *bat_priv)
112{
113 struct hlist_node *node;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100114 struct gw_node *gw_node, *curr_gw = NULL, *curr_gw_tmp = NULL;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000115 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116 uint8_t max_tq = 0;
117 uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
118 int down, up;
119
120 /**
121 * The batman daemon checks here if we already passed a full originator
122 * cycle in order to make sure we don't choose the first gateway we
123 * hear about. This check is based on the daemon's uptime which we
124 * don't have.
125 **/
126 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
127 return;
128
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100129 curr_gw = gw_get_selected_gw_node(bat_priv);
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200130 if (curr_gw)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100131 goto out;
132
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133 rcu_read_lock();
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000134 if (hlist_empty(&bat_priv->gw_list)) {
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100135 bat_dbg(DBG_BATMAN, bat_priv,
136 "Removing selected gateway - "
137 "no gateway in range\n");
138 gw_deselect(bat_priv);
139 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140 }
141
142 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
Linus Lüssinge1a53822011-03-14 22:43:37 +0000143 if (gw_node->deleted)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144 continue;
145
Linus Lüssinge1a53822011-03-14 22:43:37 +0000146 router = orig_node_get_router(gw_node->orig_node);
147 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000148 continue;
149
150 switch (atomic_read(&bat_priv->gw_sel_class)) {
151 case 1: /* fast connection */
152 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags,
153 &down, &up);
154
Linus Lüssinge1a53822011-03-14 22:43:37 +0000155 tmp_gw_factor = (router->tq_avg * router->tq_avg *
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000156 down * 100 * 100) /
157 (TQ_LOCAL_WINDOW_SIZE *
158 TQ_LOCAL_WINDOW_SIZE * 64);
159
160 if ((tmp_gw_factor > max_gw_factor) ||
161 ((tmp_gw_factor == max_gw_factor) &&
Linus Lüssinge1a53822011-03-14 22:43:37 +0000162 (router->tq_avg > max_tq)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163 curr_gw_tmp = gw_node;
164 break;
165
166 default: /**
167 * 2: stable connection (use best statistic)
168 * 3: fast-switch (use best statistic but change as
169 * soon as a better gateway appears)
170 * XX: late-switch (use best statistic but change as
171 * soon as a better gateway appears which has
172 * $routing_class more tq points)
173 **/
Linus Lüssinge1a53822011-03-14 22:43:37 +0000174 if (router->tq_avg > max_tq)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175 curr_gw_tmp = gw_node;
176 break;
177 }
178
Linus Lüssinge1a53822011-03-14 22:43:37 +0000179 if (router->tq_avg > max_tq)
180 max_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181
182 if (tmp_gw_factor > max_gw_factor)
183 max_gw_factor = tmp_gw_factor;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000184
185 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186 }
187
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000188 if (curr_gw != curr_gw_tmp) {
Linus Lüssinge1a53822011-03-14 22:43:37 +0000189 router = orig_node_get_router(curr_gw_tmp->orig_node);
190 if (!router)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100191 goto unlock;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000192
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000193 if ((curr_gw) && (!curr_gw_tmp))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194 bat_dbg(DBG_BATMAN, bat_priv,
195 "Removing selected gateway - "
196 "no gateway in range\n");
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000197 else if ((!curr_gw) && (curr_gw_tmp))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198 bat_dbg(DBG_BATMAN, bat_priv,
199 "Adding route to gateway %pM "
200 "(gw_flags: %i, tq: %i)\n",
201 curr_gw_tmp->orig_node->orig,
202 curr_gw_tmp->orig_node->gw_flags,
Linus Lüssinge1a53822011-03-14 22:43:37 +0000203 router->tq_avg);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204 else
205 bat_dbg(DBG_BATMAN, bat_priv,
206 "Changing route to gateway %pM "
207 "(gw_flags: %i, tq: %i)\n",
208 curr_gw_tmp->orig_node->orig,
209 curr_gw_tmp->orig_node->gw_flags,
Linus Lüssinge1a53822011-03-14 22:43:37 +0000210 router->tq_avg);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211
Linus Lüssinge1a53822011-03-14 22:43:37 +0000212 neigh_node_free_ref(router);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000213 gw_select(bat_priv, curr_gw_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214 }
215
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100216unlock:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000217 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100218out:
219 if (curr_gw)
220 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221}
222
223void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
224{
Linus Lüssing57f0c072011-03-14 22:43:33 +0000225 struct orig_node *curr_gw_orig;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000226 struct neigh_node *router_gw = NULL, *router_orig = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227 uint8_t gw_tq_avg, orig_tq_avg;
228
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100229 curr_gw_orig = gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000230 if (!curr_gw_orig)
231 goto deselect;
232
Linus Lüssinge1a53822011-03-14 22:43:37 +0000233 router_gw = orig_node_get_router(curr_gw_orig);
234 if (!router_gw)
235 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236
237 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000238 if (curr_gw_orig == orig_node)
Linus Lüssinge1a53822011-03-14 22:43:37 +0000239 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240
Linus Lüssinge1a53822011-03-14 22:43:37 +0000241 router_orig = orig_node_get_router(orig_node);
242 if (!router_orig)
243 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
Linus Lüssinge1a53822011-03-14 22:43:37 +0000245 gw_tq_avg = router_gw->tq_avg;
246 orig_tq_avg = router_orig->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247
248 /* the TQ value has to be better */
249 if (orig_tq_avg < gw_tq_avg)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000250 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
252 /**
253 * if the routing class is greater than 3 the value tells us how much
254 * greater the TQ value of the new gateway must be
255 **/
256 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
257 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000258 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
260 bat_dbg(DBG_BATMAN, bat_priv,
261 "Restarting gateway selection: better gateway found (tq curr: "
262 "%i, tq new: %i)\n",
263 gw_tq_avg, orig_tq_avg);
264
265deselect:
266 gw_deselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000267out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000268 if (curr_gw_orig)
269 orig_node_free_ref(curr_gw_orig);
Linus Lüssinge1a53822011-03-14 22:43:37 +0000270 if (router_gw)
271 neigh_node_free_ref(router_gw);
272 if (router_orig)
273 neigh_node_free_ref(router_orig);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000274
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000275 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276}
277
278static void gw_node_add(struct bat_priv *bat_priv,
279 struct orig_node *orig_node, uint8_t new_gwflags)
280{
281 struct gw_node *gw_node;
282 int down, up;
283
284 gw_node = kmalloc(sizeof(struct gw_node), GFP_ATOMIC);
285 if (!gw_node)
286 return;
287
288 memset(gw_node, 0, sizeof(struct gw_node));
289 INIT_HLIST_NODE(&gw_node->list);
290 gw_node->orig_node = orig_node;
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000291 atomic_set(&gw_node->refcount, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292
293 spin_lock_bh(&bat_priv->gw_list_lock);
294 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
295 spin_unlock_bh(&bat_priv->gw_list_lock);
296
297 gw_bandwidth_to_kbit(new_gwflags, &down, &up);
298 bat_dbg(DBG_BATMAN, bat_priv,
299 "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n",
300 orig_node->orig, new_gwflags,
301 (down > 2048 ? down / 1024 : down),
302 (down > 2048 ? "MBit" : "KBit"),
303 (up > 2048 ? up / 1024 : up),
304 (up > 2048 ? "MBit" : "KBit"));
305}
306
307void gw_node_update(struct bat_priv *bat_priv,
308 struct orig_node *orig_node, uint8_t new_gwflags)
309{
310 struct hlist_node *node;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100311 struct gw_node *gw_node, *curr_gw;
312
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200313 /**
314 * Note: We don't need a NULL check here, since curr_gw never gets
315 * dereferenced. If curr_gw is NULL we also should not exit as we may
316 * have this gateway in our list (duplication check!) even though we
317 * have no currently selected gateway.
318 */
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100319 curr_gw = gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320
321 rcu_read_lock();
322 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
323 if (gw_node->orig_node != orig_node)
324 continue;
325
326 bat_dbg(DBG_BATMAN, bat_priv,
327 "Gateway class of originator %pM changed from "
328 "%i to %i\n",
329 orig_node->orig, gw_node->orig_node->gw_flags,
330 new_gwflags);
331
332 gw_node->deleted = 0;
333
334 if (new_gwflags == 0) {
335 gw_node->deleted = jiffies;
336 bat_dbg(DBG_BATMAN, bat_priv,
337 "Gateway %pM removed from gateway list\n",
338 orig_node->orig);
339
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100340 if (gw_node == curr_gw)
341 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342 }
343
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100344 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346
347 if (new_gwflags == 0)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100348 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349
350 gw_node_add(bat_priv, orig_node, new_gwflags);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100351 goto unlock;
352
353deselect:
354 gw_deselect(bat_priv);
355unlock:
356 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200357
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100358 if (curr_gw)
359 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360}
361
362void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
363{
364 return gw_node_update(bat_priv, orig_node, 0);
365}
366
367void gw_node_purge(struct bat_priv *bat_priv)
368{
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100369 struct gw_node *gw_node, *curr_gw;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370 struct hlist_node *node, *node_tmp;
371 unsigned long timeout = 2 * PURGE_TIMEOUT * HZ;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100372 char do_deselect = 0;
373
374 curr_gw = gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375
376 spin_lock_bh(&bat_priv->gw_list_lock);
377
378 hlist_for_each_entry_safe(gw_node, node, node_tmp,
379 &bat_priv->gw_list, list) {
380 if (((!gw_node->deleted) ||
381 (time_before(jiffies, gw_node->deleted + timeout))) &&
382 atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
383 continue;
384
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100385 if (curr_gw == gw_node)
386 do_deselect = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387
388 hlist_del_rcu(&gw_node->list);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000389 gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390 }
391
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392 spin_unlock_bh(&bat_priv->gw_list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100393
394 /* gw_deselect() needs to acquire the gw_list_lock */
395 if (do_deselect)
396 gw_deselect(bat_priv);
397
398 if (curr_gw)
399 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400}
401
Linus Lüssinge1a53822011-03-14 22:43:37 +0000402/**
403 * fails if orig_node has no router
404 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405static int _write_buffer_text(struct bat_priv *bat_priv,
406 struct seq_file *seq, struct gw_node *gw_node)
407{
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000408 struct gw_node *curr_gw;
Linus Lüssinge1a53822011-03-14 22:43:37 +0000409 struct neigh_node *router;
410 int down, up, ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411
412 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
413
Linus Lüssinge1a53822011-03-14 22:43:37 +0000414 router = orig_node_get_router(gw_node->orig_node);
415 if (!router)
416 goto out;
417
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100418 curr_gw = gw_get_selected_gw_node(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000419
420 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100421 (curr_gw == gw_node ? "=>" : " "),
422 gw_node->orig_node->orig,
423 router->tq_avg, router->addr,
424 router->if_incoming->net_dev->name,
425 gw_node->orig_node->gw_flags,
426 (down > 2048 ? down / 1024 : down),
427 (down > 2048 ? "MBit" : "KBit"),
428 (up > 2048 ? up / 1024 : up),
429 (up > 2048 ? "MBit" : "KBit"));
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000430
Linus Lüssinge1a53822011-03-14 22:43:37 +0000431 neigh_node_free_ref(router);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100432 if (curr_gw)
433 gw_node_free_ref(curr_gw);
Linus Lüssinge1a53822011-03-14 22:43:37 +0000434out:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000435 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436}
437
438int gw_client_seq_print_text(struct seq_file *seq, void *offset)
439{
440 struct net_device *net_dev = (struct net_device *)seq->private;
441 struct bat_priv *bat_priv = netdev_priv(net_dev);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200442 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443 struct gw_node *gw_node;
444 struct hlist_node *node;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200445 int gw_count = 0, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446
Marek Lindner32ae9b22011-04-20 15:40:58 +0200447 primary_if = primary_if_get_selected(bat_priv);
448 if (!primary_if) {
449 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
450 "specify interfaces to enable it\n",
451 net_dev->name);
452 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453 }
454
Marek Lindner32ae9b22011-04-20 15:40:58 +0200455 if (primary_if->if_status != IF_ACTIVE) {
456 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
457 "primary interface not active\n",
458 net_dev->name);
459 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460 }
461
462 seq_printf(seq, " %-12s (%s/%i) %17s [%10s]: gw_class ... "
463 "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
464 "Gateway", "#", TQ_MAX_VALUE, "Nexthop",
465 "outgoingIF", SOURCE_VERSION, REVISION_VERSION_STR,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200466 primary_if->net_dev->name,
467 primary_if->net_dev->dev_addr, net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468
469 rcu_read_lock();
470 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
471 if (gw_node->deleted)
472 continue;
473
Linus Lüssinge1a53822011-03-14 22:43:37 +0000474 /* fails if orig_node has no router */
475 if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476 continue;
477
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478 gw_count++;
479 }
480 rcu_read_unlock();
481
482 if (gw_count == 0)
483 seq_printf(seq, "No gateways in range ...\n");
484
Marek Lindner32ae9b22011-04-20 15:40:58 +0200485out:
486 if (primary_if)
487 hardif_free_ref(primary_if);
488 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489}
490
491int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
492{
493 struct ethhdr *ethhdr;
494 struct iphdr *iphdr;
495 struct ipv6hdr *ipv6hdr;
496 struct udphdr *udphdr;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100497 struct gw_node *curr_gw;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498 unsigned int header_len = 0;
499
500 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
501 return 0;
502
503 /* check for ethernet header */
504 if (!pskb_may_pull(skb, header_len + ETH_HLEN))
505 return 0;
506 ethhdr = (struct ethhdr *)skb->data;
507 header_len += ETH_HLEN;
508
509 /* check for initial vlan header */
510 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
511 if (!pskb_may_pull(skb, header_len + VLAN_HLEN))
512 return 0;
513 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
514 header_len += VLAN_HLEN;
515 }
516
517 /* check for ip header */
518 switch (ntohs(ethhdr->h_proto)) {
519 case ETH_P_IP:
520 if (!pskb_may_pull(skb, header_len + sizeof(struct iphdr)))
521 return 0;
522 iphdr = (struct iphdr *)(skb->data + header_len);
523 header_len += iphdr->ihl * 4;
524
525 /* check for udp header */
526 if (iphdr->protocol != IPPROTO_UDP)
527 return 0;
528
529 break;
530 case ETH_P_IPV6:
531 if (!pskb_may_pull(skb, header_len + sizeof(struct ipv6hdr)))
532 return 0;
533 ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
534 header_len += sizeof(struct ipv6hdr);
535
536 /* check for udp header */
537 if (ipv6hdr->nexthdr != IPPROTO_UDP)
538 return 0;
539
540 break;
541 default:
542 return 0;
543 }
544
545 if (!pskb_may_pull(skb, header_len + sizeof(struct udphdr)))
546 return 0;
547 udphdr = (struct udphdr *)(skb->data + header_len);
548 header_len += sizeof(struct udphdr);
549
550 /* check for bootp port */
551 if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
552 (ntohs(udphdr->dest) != 67))
553 return 0;
554
555 if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
556 (ntohs(udphdr->dest) != 547))
557 return 0;
558
559 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
560 return -1;
561
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100562 curr_gw = gw_get_selected_gw_node(bat_priv);
563 if (!curr_gw)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100566 if (curr_gw)
567 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568 return 1;
569}