blob: 0350c0cd690e17c739379842cda74290369f4418 [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"
Antonio Quartulli2265c142011-04-27 00:22:00 +020023#include "bat_sysfs.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000024#include "gateway_client.h"
25#include "gateway_common.h"
26#include "hard-interface.h"
Linus Lüssing57f0c072011-03-14 22:43:33 +000027#include "originator.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028#include <linux/ip.h>
29#include <linux/ipv6.h>
30#include <linux/udp.h>
31#include <linux/if_vlan.h>
32
Marek Lindner25b6d3c2011-02-10 14:33:49 +000033static void gw_node_free_ref(struct gw_node *gw_node)
34{
35 if (atomic_dec_and_test(&gw_node->refcount))
Paul E. McKenneyeb340b22011-05-01 23:25:02 -070036 kfree_rcu(gw_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037}
38
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010039static struct gw_node *gw_get_selected_gw_node(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040{
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010041 struct gw_node *gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000043 rcu_read_lock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010044 gw_node = rcu_dereference(bat_priv->curr_gw);
45 if (!gw_node)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000046 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010048 if (!atomic_inc_not_zero(&gw_node->refcount))
49 gw_node = NULL;
50
51out:
52 rcu_read_unlock();
53 return gw_node;
54}
55
56struct orig_node *gw_get_selected_orig(struct bat_priv *bat_priv)
57{
58 struct gw_node *gw_node;
59 struct orig_node *orig_node = NULL;
60
61 gw_node = gw_get_selected_gw_node(bat_priv);
62 if (!gw_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +000063 goto out;
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000064
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010065 rcu_read_lock();
66 orig_node = gw_node->orig_node;
67 if (!orig_node)
68 goto unlock;
69
Marek Lindner7b36e8e2011-02-18 12:28:10 +000070 if (!atomic_inc_not_zero(&orig_node->refcount))
71 orig_node = NULL;
Linus Lüssing43c70ad2011-02-13 21:13:04 +000072
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010073unlock:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000074 rcu_read_unlock();
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010075out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076 if (gw_node)
Marek Lindner25b6d3c2011-02-10 14:33:49 +000077 gw_node_free_ref(gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010078 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079}
80
Marek Lindner25b6d3c2011-02-10 14:33:49 +000081static void gw_select(struct bat_priv *bat_priv, struct gw_node *new_gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082{
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000083 struct gw_node *curr_gw_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000084
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010085 spin_lock_bh(&bat_priv->gw_list_lock);
86
Marek Lindner25b6d3c2011-02-10 14:33:49 +000087 if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
88 new_gw_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089
Sven Eckelmann728cbc62011-05-15 00:50:21 +020090 curr_gw_node = rcu_dereference_protected(bat_priv->curr_gw, 1);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +000091 rcu_assign_pointer(bat_priv->curr_gw, new_gw_node);
Marek Lindner25b6d3c2011-02-10 14:33:49 +000092
93 if (curr_gw_node)
94 gw_node_free_ref(curr_gw_node);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +010095
96 spin_unlock_bh(&bat_priv->gw_list_lock);
97}
98
99void gw_deselect(struct bat_priv *bat_priv)
100{
Antonio Quartulli2265c142011-04-27 00:22:00 +0200101 atomic_set(&bat_priv->gw_reselect, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102}
103
Antonio Quartulli2265c142011-04-27 00:22:00 +0200104static struct gw_node *gw_get_best_gw_node(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000105{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000106 struct neigh_node *router;
Antonio Quartulli2265c142011-04-27 00:22:00 +0200107 struct hlist_node *node;
108 struct gw_node *gw_node, *curr_gw = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000109 uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
Antonio Quartulli2265c142011-04-27 00:22:00 +0200110 uint8_t max_tq = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111 int down, up;
112
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113 rcu_read_lock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000115 if (gw_node->deleted)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116 continue;
117
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000118 router = orig_node_get_router(gw_node->orig_node);
119 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120 continue;
121
Antonio Quartulli2265c142011-04-27 00:22:00 +0200122 if (!atomic_inc_not_zero(&gw_node->refcount))
123 goto next;
124
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125 switch (atomic_read(&bat_priv->gw_sel_class)) {
126 case 1: /* fast connection */
127 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags,
128 &down, &up);
129
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000130 tmp_gw_factor = (router->tq_avg * router->tq_avg *
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131 down * 100 * 100) /
132 (TQ_LOCAL_WINDOW_SIZE *
133 TQ_LOCAL_WINDOW_SIZE * 64);
134
135 if ((tmp_gw_factor > max_gw_factor) ||
136 ((tmp_gw_factor == max_gw_factor) &&
Antonio Quartulli2265c142011-04-27 00:22:00 +0200137 (router->tq_avg > max_tq))) {
138 if (curr_gw)
139 gw_node_free_ref(curr_gw);
140 curr_gw = gw_node;
141 atomic_inc(&curr_gw->refcount);
142 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000143 break;
144
145 default: /**
146 * 2: stable connection (use best statistic)
147 * 3: fast-switch (use best statistic but change as
148 * soon as a better gateway appears)
149 * XX: late-switch (use best statistic but change as
150 * soon as a better gateway appears which has
151 * $routing_class more tq points)
152 **/
Antonio Quartulli2265c142011-04-27 00:22:00 +0200153 if (router->tq_avg > max_tq) {
154 if (curr_gw)
155 gw_node_free_ref(curr_gw);
156 curr_gw = gw_node;
157 atomic_inc(&curr_gw->refcount);
158 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000159 break;
160 }
161
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000162 if (router->tq_avg > max_tq)
163 max_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164
165 if (tmp_gw_factor > max_gw_factor)
166 max_gw_factor = tmp_gw_factor;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000167
Antonio Quartulli2265c142011-04-27 00:22:00 +0200168 gw_node_free_ref(gw_node);
169
170next:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000171 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173 rcu_read_unlock();
Antonio Quartulli2265c142011-04-27 00:22:00 +0200174
175 return curr_gw;
176}
177
178void gw_election(struct bat_priv *bat_priv)
179{
180 struct gw_node *curr_gw = NULL, *next_gw = NULL;
181 struct neigh_node *router = NULL;
182
183 /**
184 * The batman daemon checks here if we already passed a full originator
185 * cycle in order to make sure we don't choose the first gateway we
186 * hear about. This check is based on the daemon's uptime which we
187 * don't have.
188 **/
189 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
190 goto out;
191
192 if (!atomic_dec_not_zero(&bat_priv->gw_reselect))
193 goto out;
194
195 curr_gw = gw_get_selected_gw_node(bat_priv);
196
197 next_gw = gw_get_best_gw_node(bat_priv);
198
199 if (curr_gw == next_gw)
200 goto out;
201
202 if (next_gw) {
203 router = orig_node_get_router(next_gw->orig_node);
204 if (!router) {
205 gw_deselect(bat_priv);
206 goto out;
207 }
208 }
209
210 if ((curr_gw) && (!next_gw)) {
211 bat_dbg(DBG_BATMAN, bat_priv,
212 "Removing selected gateway - no gateway in range\n");
213 } else if ((!curr_gw) && (next_gw)) {
214 bat_dbg(DBG_BATMAN, bat_priv,
215 "Adding route to gateway %pM (gw_flags: %i, tq: %i)\n",
216 next_gw->orig_node->orig,
217 next_gw->orig_node->gw_flags,
218 router->tq_avg);
219 } else {
220 bat_dbg(DBG_BATMAN, bat_priv,
221 "Changing route to gateway %pM "
222 "(gw_flags: %i, tq: %i)\n",
223 next_gw->orig_node->orig,
224 next_gw->orig_node->gw_flags,
225 router->tq_avg);
226 }
227
228 gw_select(bat_priv, next_gw);
229
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100230out:
231 if (curr_gw)
232 gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200233 if (next_gw)
234 gw_node_free_ref(next_gw);
235 if (router)
236 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237}
238
239void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
240{
Linus Lüssing57f0c072011-03-14 22:43:33 +0000241 struct orig_node *curr_gw_orig;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000242 struct neigh_node *router_gw = NULL, *router_orig = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243 uint8_t gw_tq_avg, orig_tq_avg;
244
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100245 curr_gw_orig = gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000246 if (!curr_gw_orig)
247 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000249 router_gw = orig_node_get_router(curr_gw_orig);
250 if (!router_gw)
251 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252
253 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000254 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000255 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000257 router_orig = orig_node_get_router(orig_node);
258 if (!router_orig)
259 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000261 gw_tq_avg = router_gw->tq_avg;
262 orig_tq_avg = router_orig->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263
264 /* the TQ value has to be better */
265 if (orig_tq_avg < gw_tq_avg)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000266 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
268 /**
269 * if the routing class is greater than 3 the value tells us how much
270 * greater the TQ value of the new gateway must be
271 **/
272 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
273 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000274 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275
276 bat_dbg(DBG_BATMAN, bat_priv,
277 "Restarting gateway selection: better gateway found (tq curr: "
278 "%i, tq new: %i)\n",
279 gw_tq_avg, orig_tq_avg);
280
281deselect:
282 gw_deselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000283out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000284 if (curr_gw_orig)
285 orig_node_free_ref(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000286 if (router_gw)
287 neigh_node_free_ref(router_gw);
288 if (router_orig)
289 neigh_node_free_ref(router_orig);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000290
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000291 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292}
293
294static void gw_node_add(struct bat_priv *bat_priv,
295 struct orig_node *orig_node, uint8_t new_gwflags)
296{
297 struct gw_node *gw_node;
298 int down, up;
299
Sven Eckelmann704509b2011-05-14 23:14:54 +0200300 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301 if (!gw_node)
302 return;
303
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304 INIT_HLIST_NODE(&gw_node->list);
305 gw_node->orig_node = orig_node;
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000306 atomic_set(&gw_node->refcount, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
308 spin_lock_bh(&bat_priv->gw_list_lock);
309 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
310 spin_unlock_bh(&bat_priv->gw_list_lock);
311
312 gw_bandwidth_to_kbit(new_gwflags, &down, &up);
313 bat_dbg(DBG_BATMAN, bat_priv,
314 "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n",
315 orig_node->orig, new_gwflags,
316 (down > 2048 ? down / 1024 : down),
317 (down > 2048 ? "MBit" : "KBit"),
318 (up > 2048 ? up / 1024 : up),
319 (up > 2048 ? "MBit" : "KBit"));
320}
321
322void gw_node_update(struct bat_priv *bat_priv,
323 struct orig_node *orig_node, uint8_t new_gwflags)
324{
325 struct hlist_node *node;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100326 struct gw_node *gw_node, *curr_gw;
327
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200328 /**
329 * Note: We don't need a NULL check here, since curr_gw never gets
330 * dereferenced. If curr_gw is NULL we also should not exit as we may
331 * have this gateway in our list (duplication check!) even though we
332 * have no currently selected gateway.
333 */
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100334 curr_gw = gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335
336 rcu_read_lock();
337 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
338 if (gw_node->orig_node != orig_node)
339 continue;
340
341 bat_dbg(DBG_BATMAN, bat_priv,
342 "Gateway class of originator %pM changed from "
343 "%i to %i\n",
344 orig_node->orig, gw_node->orig_node->gw_flags,
345 new_gwflags);
346
347 gw_node->deleted = 0;
348
Marek Lindnerecbd5322011-06-09 17:13:09 +0200349 if (new_gwflags == NO_FLAGS) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350 gw_node->deleted = jiffies;
351 bat_dbg(DBG_BATMAN, bat_priv,
352 "Gateway %pM removed from gateway list\n",
353 orig_node->orig);
354
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100355 if (gw_node == curr_gw)
356 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357 }
358
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100359 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361
Marek Lindnerecbd5322011-06-09 17:13:09 +0200362 if (new_gwflags == NO_FLAGS)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100363 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
365 gw_node_add(bat_priv, orig_node, new_gwflags);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100366 goto unlock;
367
368deselect:
369 gw_deselect(bat_priv);
370unlock:
371 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200372
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100373 if (curr_gw)
374 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375}
376
377void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
378{
Sven Eckelmann6b9aadf2011-06-04 12:40:37 +0200379 gw_node_update(bat_priv, orig_node, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380}
381
382void gw_node_purge(struct bat_priv *bat_priv)
383{
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100384 struct gw_node *gw_node, *curr_gw;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385 struct hlist_node *node, *node_tmp;
386 unsigned long timeout = 2 * PURGE_TIMEOUT * HZ;
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200387 int do_deselect = 0;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100388
389 curr_gw = gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390
391 spin_lock_bh(&bat_priv->gw_list_lock);
392
393 hlist_for_each_entry_safe(gw_node, node, node_tmp,
394 &bat_priv->gw_list, list) {
395 if (((!gw_node->deleted) ||
396 (time_before(jiffies, gw_node->deleted + timeout))) &&
397 atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
398 continue;
399
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100400 if (curr_gw == gw_node)
401 do_deselect = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402
403 hlist_del_rcu(&gw_node->list);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000404 gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405 }
406
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407 spin_unlock_bh(&bat_priv->gw_list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100408
409 /* gw_deselect() needs to acquire the gw_list_lock */
410 if (do_deselect)
411 gw_deselect(bat_priv);
412
413 if (curr_gw)
414 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415}
416
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000417/**
418 * fails if orig_node has no router
419 */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200420static int _write_buffer_text(struct bat_priv *bat_priv, struct seq_file *seq,
421 const struct gw_node *gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422{
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000423 struct gw_node *curr_gw;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000424 struct neigh_node *router;
425 int down, up, ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426
427 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
428
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000429 router = orig_node_get_router(gw_node->orig_node);
430 if (!router)
431 goto out;
432
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100433 curr_gw = gw_get_selected_gw_node(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000434
435 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100436 (curr_gw == gw_node ? "=>" : " "),
437 gw_node->orig_node->orig,
438 router->tq_avg, router->addr,
439 router->if_incoming->net_dev->name,
440 gw_node->orig_node->gw_flags,
441 (down > 2048 ? down / 1024 : down),
442 (down > 2048 ? "MBit" : "KBit"),
443 (up > 2048 ? up / 1024 : up),
444 (up > 2048 ? "MBit" : "KBit"));
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000445
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000446 neigh_node_free_ref(router);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100447 if (curr_gw)
448 gw_node_free_ref(curr_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000449out:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000450 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000451}
452
453int gw_client_seq_print_text(struct seq_file *seq, void *offset)
454{
455 struct net_device *net_dev = (struct net_device *)seq->private;
456 struct bat_priv *bat_priv = netdev_priv(net_dev);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200457 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458 struct gw_node *gw_node;
459 struct hlist_node *node;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200460 int gw_count = 0, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461
Marek Lindner32ae9b22011-04-20 15:40:58 +0200462 primary_if = primary_if_get_selected(bat_priv);
463 if (!primary_if) {
464 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
465 "specify interfaces to enable it\n",
466 net_dev->name);
467 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468 }
469
Marek Lindner32ae9b22011-04-20 15:40:58 +0200470 if (primary_if->if_status != IF_ACTIVE) {
471 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
472 "primary interface not active\n",
473 net_dev->name);
474 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475 }
476
477 seq_printf(seq, " %-12s (%s/%i) %17s [%10s]: gw_class ... "
478 "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
479 "Gateway", "#", TQ_MAX_VALUE, "Nexthop",
480 "outgoingIF", SOURCE_VERSION, REVISION_VERSION_STR,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200481 primary_if->net_dev->name,
482 primary_if->net_dev->dev_addr, net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483
484 rcu_read_lock();
485 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
486 if (gw_node->deleted)
487 continue;
488
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000489 /* fails if orig_node has no router */
490 if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491 continue;
492
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493 gw_count++;
494 }
495 rcu_read_unlock();
496
497 if (gw_count == 0)
498 seq_printf(seq, "No gateways in range ...\n");
499
Marek Lindner32ae9b22011-04-20 15:40:58 +0200500out:
501 if (primary_if)
502 hardif_free_ref(primary_if);
503 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504}
505
506int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
507{
508 struct ethhdr *ethhdr;
509 struct iphdr *iphdr;
510 struct ipv6hdr *ipv6hdr;
511 struct udphdr *udphdr;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100512 struct gw_node *curr_gw;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513 unsigned int header_len = 0;
514
515 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
516 return 0;
517
518 /* check for ethernet header */
519 if (!pskb_may_pull(skb, header_len + ETH_HLEN))
520 return 0;
521 ethhdr = (struct ethhdr *)skb->data;
522 header_len += ETH_HLEN;
523
524 /* check for initial vlan header */
525 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
526 if (!pskb_may_pull(skb, header_len + VLAN_HLEN))
527 return 0;
528 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
529 header_len += VLAN_HLEN;
530 }
531
532 /* check for ip header */
533 switch (ntohs(ethhdr->h_proto)) {
534 case ETH_P_IP:
Sven Eckelmann704509b2011-05-14 23:14:54 +0200535 if (!pskb_may_pull(skb, header_len + sizeof(*iphdr)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536 return 0;
537 iphdr = (struct iphdr *)(skb->data + header_len);
538 header_len += iphdr->ihl * 4;
539
540 /* check for udp header */
541 if (iphdr->protocol != IPPROTO_UDP)
542 return 0;
543
544 break;
545 case ETH_P_IPV6:
Sven Eckelmann704509b2011-05-14 23:14:54 +0200546 if (!pskb_may_pull(skb, header_len + sizeof(*ipv6hdr)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547 return 0;
548 ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200549 header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
551 /* check for udp header */
552 if (ipv6hdr->nexthdr != IPPROTO_UDP)
553 return 0;
554
555 break;
556 default:
557 return 0;
558 }
559
Sven Eckelmann704509b2011-05-14 23:14:54 +0200560 if (!pskb_may_pull(skb, header_len + sizeof(*udphdr)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561 return 0;
562 udphdr = (struct udphdr *)(skb->data + header_len);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200563 header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564
565 /* check for bootp port */
566 if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
567 (ntohs(udphdr->dest) != 67))
568 return 0;
569
570 if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
571 (ntohs(udphdr->dest) != 547))
572 return 0;
573
574 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
575 return -1;
576
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100577 curr_gw = gw_get_selected_gw_node(bat_priv);
578 if (!curr_gw)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100581 if (curr_gw)
582 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583 return 1;
584}