blob: 6381864c1ca45ae6f46ca379f24a0c77c73244eb [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;
Antonio Quartulli19595e02011-06-12 11:58:58 +0200182 char gw_addr[18] = { '\0' };
Antonio Quartulli2265c142011-04-27 00:22:00 +0200183
184 /**
185 * The batman daemon checks here if we already passed a full originator
186 * cycle in order to make sure we don't choose the first gateway we
187 * hear about. This check is based on the daemon's uptime which we
188 * don't have.
189 **/
190 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
191 goto out;
192
193 if (!atomic_dec_not_zero(&bat_priv->gw_reselect))
194 goto out;
195
196 curr_gw = gw_get_selected_gw_node(bat_priv);
197
198 next_gw = gw_get_best_gw_node(bat_priv);
199
200 if (curr_gw == next_gw)
201 goto out;
202
203 if (next_gw) {
Antonio Quartulli19595e02011-06-12 11:58:58 +0200204 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
205
Antonio Quartulli2265c142011-04-27 00:22:00 +0200206 router = orig_node_get_router(next_gw->orig_node);
207 if (!router) {
208 gw_deselect(bat_priv);
209 goto out;
210 }
211 }
212
213 if ((curr_gw) && (!next_gw)) {
214 bat_dbg(DBG_BATMAN, bat_priv,
215 "Removing selected gateway - no gateway in range\n");
Antonio Quartulli19595e02011-06-12 11:58:58 +0200216 throw_uevent(bat_priv, UEV_GW, UEV_DEL, NULL);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200217 } else if ((!curr_gw) && (next_gw)) {
218 bat_dbg(DBG_BATMAN, bat_priv,
219 "Adding route to gateway %pM (gw_flags: %i, tq: %i)\n",
220 next_gw->orig_node->orig,
221 next_gw->orig_node->gw_flags,
222 router->tq_avg);
Antonio Quartulli19595e02011-06-12 11:58:58 +0200223 throw_uevent(bat_priv, UEV_GW, UEV_ADD, gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200224 } else {
225 bat_dbg(DBG_BATMAN, bat_priv,
226 "Changing route to gateway %pM "
227 "(gw_flags: %i, tq: %i)\n",
228 next_gw->orig_node->orig,
229 next_gw->orig_node->gw_flags,
230 router->tq_avg);
Antonio Quartulli19595e02011-06-12 11:58:58 +0200231 throw_uevent(bat_priv, UEV_GW, UEV_CHANGE, gw_addr);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200232 }
233
234 gw_select(bat_priv, next_gw);
235
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100236out:
237 if (curr_gw)
238 gw_node_free_ref(curr_gw);
Antonio Quartulli2265c142011-04-27 00:22:00 +0200239 if (next_gw)
240 gw_node_free_ref(next_gw);
241 if (router)
242 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243}
244
245void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
246{
Linus Lüssing57f0c072011-03-14 22:43:33 +0000247 struct orig_node *curr_gw_orig;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000248 struct neigh_node *router_gw = NULL, *router_orig = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249 uint8_t gw_tq_avg, orig_tq_avg;
250
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100251 curr_gw_orig = gw_get_selected_orig(bat_priv);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000252 if (!curr_gw_orig)
253 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000255 router_gw = orig_node_get_router(curr_gw_orig);
256 if (!router_gw)
257 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258
259 /* this node already is the gateway */
Linus Lüssing57f0c072011-03-14 22:43:33 +0000260 if (curr_gw_orig == orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000261 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000263 router_orig = orig_node_get_router(orig_node);
264 if (!router_orig)
265 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000267 gw_tq_avg = router_gw->tq_avg;
268 orig_tq_avg = router_orig->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
270 /* the TQ value has to be better */
271 if (orig_tq_avg < gw_tq_avg)
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000272 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273
274 /**
275 * if the routing class is greater than 3 the value tells us how much
276 * greater the TQ value of the new gateway must be
277 **/
278 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
279 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000280 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281
282 bat_dbg(DBG_BATMAN, bat_priv,
283 "Restarting gateway selection: better gateway found (tq curr: "
284 "%i, tq new: %i)\n",
285 gw_tq_avg, orig_tq_avg);
286
287deselect:
288 gw_deselect(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000289out:
Linus Lüssing57f0c072011-03-14 22:43:33 +0000290 if (curr_gw_orig)
291 orig_node_free_ref(curr_gw_orig);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000292 if (router_gw)
293 neigh_node_free_ref(router_gw);
294 if (router_orig)
295 neigh_node_free_ref(router_orig);
Linus Lüssing57f0c072011-03-14 22:43:33 +0000296
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000297 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298}
299
300static void gw_node_add(struct bat_priv *bat_priv,
301 struct orig_node *orig_node, uint8_t new_gwflags)
302{
303 struct gw_node *gw_node;
304 int down, up;
305
Sven Eckelmann704509b2011-05-14 23:14:54 +0200306 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307 if (!gw_node)
308 return;
309
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310 INIT_HLIST_NODE(&gw_node->list);
311 gw_node->orig_node = orig_node;
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000312 atomic_set(&gw_node->refcount, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
314 spin_lock_bh(&bat_priv->gw_list_lock);
315 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
316 spin_unlock_bh(&bat_priv->gw_list_lock);
317
318 gw_bandwidth_to_kbit(new_gwflags, &down, &up);
319 bat_dbg(DBG_BATMAN, bat_priv,
320 "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n",
321 orig_node->orig, new_gwflags,
322 (down > 2048 ? down / 1024 : down),
323 (down > 2048 ? "MBit" : "KBit"),
324 (up > 2048 ? up / 1024 : up),
325 (up > 2048 ? "MBit" : "KBit"));
326}
327
328void gw_node_update(struct bat_priv *bat_priv,
329 struct orig_node *orig_node, uint8_t new_gwflags)
330{
331 struct hlist_node *node;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100332 struct gw_node *gw_node, *curr_gw;
333
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200334 /**
335 * Note: We don't need a NULL check here, since curr_gw never gets
336 * dereferenced. If curr_gw is NULL we also should not exit as we may
337 * have this gateway in our list (duplication check!) even though we
338 * have no currently selected gateway.
339 */
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100340 curr_gw = gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341
342 rcu_read_lock();
343 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
344 if (gw_node->orig_node != orig_node)
345 continue;
346
347 bat_dbg(DBG_BATMAN, bat_priv,
348 "Gateway class of originator %pM changed from "
349 "%i to %i\n",
350 orig_node->orig, gw_node->orig_node->gw_flags,
351 new_gwflags);
352
353 gw_node->deleted = 0;
354
Marek Lindnerecbd5322011-06-09 17:13:09 +0200355 if (new_gwflags == NO_FLAGS) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356 gw_node->deleted = jiffies;
357 bat_dbg(DBG_BATMAN, bat_priv,
358 "Gateway %pM removed from gateway list\n",
359 orig_node->orig);
360
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100361 if (gw_node == curr_gw)
362 goto deselect;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363 }
364
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100365 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
Marek Lindnerecbd5322011-06-09 17:13:09 +0200368 if (new_gwflags == NO_FLAGS)
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100369 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
371 gw_node_add(bat_priv, orig_node, new_gwflags);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100372 goto unlock;
373
374deselect:
375 gw_deselect(bat_priv);
376unlock:
377 rcu_read_unlock();
Antonio Quartulli71e4aa92011-04-25 22:44:32 +0200378
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100379 if (curr_gw)
380 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381}
382
383void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
384{
Sven Eckelmann6b9aadf2011-06-04 12:40:37 +0200385 gw_node_update(bat_priv, orig_node, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386}
387
388void gw_node_purge(struct bat_priv *bat_priv)
389{
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100390 struct gw_node *gw_node, *curr_gw;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391 struct hlist_node *node, *node_tmp;
392 unsigned long timeout = 2 * PURGE_TIMEOUT * HZ;
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200393 int do_deselect = 0;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100394
395 curr_gw = gw_get_selected_gw_node(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396
397 spin_lock_bh(&bat_priv->gw_list_lock);
398
399 hlist_for_each_entry_safe(gw_node, node, node_tmp,
400 &bat_priv->gw_list, list) {
401 if (((!gw_node->deleted) ||
402 (time_before(jiffies, gw_node->deleted + timeout))) &&
403 atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
404 continue;
405
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100406 if (curr_gw == gw_node)
407 do_deselect = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
409 hlist_del_rcu(&gw_node->list);
Marek Lindner25b6d3c2011-02-10 14:33:49 +0000410 gw_node_free_ref(gw_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411 }
412
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413 spin_unlock_bh(&bat_priv->gw_list_lock);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100414
415 /* gw_deselect() needs to acquire the gw_list_lock */
416 if (do_deselect)
417 gw_deselect(bat_priv);
418
419 if (curr_gw)
420 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421}
422
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000423/**
424 * fails if orig_node has no router
425 */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200426static int _write_buffer_text(struct bat_priv *bat_priv, struct seq_file *seq,
427 const struct gw_node *gw_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428{
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000429 struct gw_node *curr_gw;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000430 struct neigh_node *router;
431 int down, up, ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432
433 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
434
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000435 router = orig_node_get_router(gw_node->orig_node);
436 if (!router)
437 goto out;
438
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100439 curr_gw = gw_get_selected_gw_node(bat_priv);
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000440
441 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100442 (curr_gw == gw_node ? "=>" : " "),
443 gw_node->orig_node->orig,
444 router->tq_avg, router->addr,
445 router->if_incoming->net_dev->name,
446 gw_node->orig_node->gw_flags,
447 (down > 2048 ? down / 1024 : down),
448 (down > 2048 ? "MBit" : "KBit"),
449 (up > 2048 ? up / 1024 : up),
450 (up > 2048 ? "MBit" : "KBit"));
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000451
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000452 neigh_node_free_ref(router);
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100453 if (curr_gw)
454 gw_node_free_ref(curr_gw);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000455out:
Linus Lüssing5d02b3c2011-02-13 21:13:02 +0000456 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457}
458
459int gw_client_seq_print_text(struct seq_file *seq, void *offset)
460{
461 struct net_device *net_dev = (struct net_device *)seq->private;
462 struct bat_priv *bat_priv = netdev_priv(net_dev);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200463 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464 struct gw_node *gw_node;
465 struct hlist_node *node;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200466 int gw_count = 0, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467
Marek Lindner32ae9b22011-04-20 15:40:58 +0200468 primary_if = primary_if_get_selected(bat_priv);
469 if (!primary_if) {
470 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
471 "specify interfaces to enable it\n",
472 net_dev->name);
473 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474 }
475
Marek Lindner32ae9b22011-04-20 15:40:58 +0200476 if (primary_if->if_status != IF_ACTIVE) {
477 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
478 "primary interface not active\n",
479 net_dev->name);
480 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000481 }
482
483 seq_printf(seq, " %-12s (%s/%i) %17s [%10s]: gw_class ... "
484 "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
485 "Gateway", "#", TQ_MAX_VALUE, "Nexthop",
486 "outgoingIF", SOURCE_VERSION, REVISION_VERSION_STR,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200487 primary_if->net_dev->name,
488 primary_if->net_dev->dev_addr, net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489
490 rcu_read_lock();
491 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
492 if (gw_node->deleted)
493 continue;
494
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000495 /* fails if orig_node has no router */
496 if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497 continue;
498
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499 gw_count++;
500 }
501 rcu_read_unlock();
502
503 if (gw_count == 0)
504 seq_printf(seq, "No gateways in range ...\n");
505
Marek Lindner32ae9b22011-04-20 15:40:58 +0200506out:
507 if (primary_if)
508 hardif_free_ref(primary_if);
509 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510}
511
512int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
513{
514 struct ethhdr *ethhdr;
515 struct iphdr *iphdr;
516 struct ipv6hdr *ipv6hdr;
517 struct udphdr *udphdr;
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100518 struct gw_node *curr_gw;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519 unsigned int header_len = 0;
520
521 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
522 return 0;
523
524 /* check for ethernet header */
525 if (!pskb_may_pull(skb, header_len + ETH_HLEN))
526 return 0;
527 ethhdr = (struct ethhdr *)skb->data;
528 header_len += ETH_HLEN;
529
530 /* check for initial vlan header */
531 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
532 if (!pskb_may_pull(skb, header_len + VLAN_HLEN))
533 return 0;
534 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
535 header_len += VLAN_HLEN;
536 }
537
538 /* check for ip header */
539 switch (ntohs(ethhdr->h_proto)) {
540 case ETH_P_IP:
Sven Eckelmann704509b2011-05-14 23:14:54 +0200541 if (!pskb_may_pull(skb, header_len + sizeof(*iphdr)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542 return 0;
543 iphdr = (struct iphdr *)(skb->data + header_len);
544 header_len += iphdr->ihl * 4;
545
546 /* check for udp header */
547 if (iphdr->protocol != IPPROTO_UDP)
548 return 0;
549
550 break;
551 case ETH_P_IPV6:
Sven Eckelmann704509b2011-05-14 23:14:54 +0200552 if (!pskb_may_pull(skb, header_len + sizeof(*ipv6hdr)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553 return 0;
554 ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200555 header_len += sizeof(*ipv6hdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556
557 /* check for udp header */
558 if (ipv6hdr->nexthdr != IPPROTO_UDP)
559 return 0;
560
561 break;
562 default:
563 return 0;
564 }
565
Sven Eckelmann704509b2011-05-14 23:14:54 +0200566 if (!pskb_may_pull(skb, header_len + sizeof(*udphdr)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567 return 0;
568 udphdr = (struct udphdr *)(skb->data + header_len);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200569 header_len += sizeof(*udphdr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570
571 /* check for bootp port */
572 if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
573 (ntohs(udphdr->dest) != 67))
574 return 0;
575
576 if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
577 (ntohs(udphdr->dest) != 547))
578 return 0;
579
580 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
581 return -1;
582
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100583 curr_gw = gw_get_selected_gw_node(bat_priv);
584 if (!curr_gw)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586
Marek Lindnerc4aac1a2011-03-23 11:24:34 +0100587 if (curr_gw)
588 gw_node_free_ref(curr_gw);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000589 return 1;
590}