2 * Copyright (C) 2009-2011 B.A.T.M.A.N. contributors:
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.
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.
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
23 #include "gateway_client.h"
24 #include "gateway_common.h"
25 #include "hard-interface.h"
26 #include "originator.h"
28 #include <linux/ipv6.h>
29 #include <linux/udp.h>
30 #include <linux/if_vlan.h>
32 static void gw_node_free_rcu(struct rcu_head *rcu)
34 struct gw_node *gw_node;
36 gw_node = container_of(rcu, struct gw_node, rcu);
40 static void gw_node_free_ref(struct gw_node *gw_node)
42 if (atomic_dec_and_test(&gw_node->refcount))
43 call_rcu(&gw_node->rcu, gw_node_free_rcu);
46 static struct gw_node *gw_get_selected_gw_node(struct bat_priv *bat_priv)
48 struct gw_node *gw_node;
51 gw_node = rcu_dereference(bat_priv->curr_gw);
55 if (!atomic_inc_not_zero(&gw_node->refcount))
63 struct orig_node *gw_get_selected_orig(struct bat_priv *bat_priv)
65 struct gw_node *gw_node;
66 struct orig_node *orig_node = NULL;
68 gw_node = gw_get_selected_gw_node(bat_priv);
73 orig_node = gw_node->orig_node;
77 if (!atomic_inc_not_zero(&orig_node->refcount))
84 gw_node_free_ref(gw_node);
88 static void gw_select(struct bat_priv *bat_priv, struct gw_node *new_gw_node)
90 struct gw_node *curr_gw_node;
92 spin_lock_bh(&bat_priv->gw_list_lock);
94 if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
97 curr_gw_node = bat_priv->curr_gw;
98 rcu_assign_pointer(bat_priv->curr_gw, new_gw_node);
101 gw_node_free_ref(curr_gw_node);
103 spin_unlock_bh(&bat_priv->gw_list_lock);
106 void gw_deselect(struct bat_priv *bat_priv)
108 gw_select(bat_priv, NULL);
111 void gw_election(struct bat_priv *bat_priv)
113 struct hlist_node *node;
114 struct gw_node *gw_node, *curr_gw = NULL, *curr_gw_tmp = NULL;
115 struct neigh_node *router;
117 uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
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
126 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
129 curr_gw = gw_get_selected_gw_node(bat_priv);
134 if (hlist_empty(&bat_priv->gw_list)) {
135 bat_dbg(DBG_BATMAN, bat_priv,
136 "Removing selected gateway - "
137 "no gateway in range\n");
138 gw_deselect(bat_priv);
142 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
143 if (gw_node->deleted)
146 router = orig_node_get_router(gw_node->orig_node);
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,
155 tmp_gw_factor = (router->tq_avg * router->tq_avg *
157 (TQ_LOCAL_WINDOW_SIZE *
158 TQ_LOCAL_WINDOW_SIZE * 64);
160 if ((tmp_gw_factor > max_gw_factor) ||
161 ((tmp_gw_factor == max_gw_factor) &&
162 (router->tq_avg > max_tq)))
163 curr_gw_tmp = gw_node;
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)
174 if (router->tq_avg > max_tq)
175 curr_gw_tmp = gw_node;
179 if (router->tq_avg > max_tq)
180 max_tq = router->tq_avg;
182 if (tmp_gw_factor > max_gw_factor)
183 max_gw_factor = tmp_gw_factor;
185 neigh_node_free_ref(router);
188 if (curr_gw != curr_gw_tmp) {
189 router = orig_node_get_router(curr_gw_tmp->orig_node);
193 if ((curr_gw) && (!curr_gw_tmp))
194 bat_dbg(DBG_BATMAN, bat_priv,
195 "Removing selected gateway - "
196 "no gateway in range\n");
197 else if ((!curr_gw) && (curr_gw_tmp))
198 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,
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,
212 neigh_node_free_ref(router);
213 gw_select(bat_priv, curr_gw_tmp);
220 gw_node_free_ref(curr_gw);
223 void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
225 struct orig_node *curr_gw_orig;
226 struct neigh_node *router_gw = NULL, *router_orig = NULL;
227 uint8_t gw_tq_avg, orig_tq_avg;
229 curr_gw_orig = gw_get_selected_orig(bat_priv);
233 router_gw = orig_node_get_router(curr_gw_orig);
237 /* this node already is the gateway */
238 if (curr_gw_orig == orig_node)
241 router_orig = orig_node_get_router(orig_node);
245 gw_tq_avg = router_gw->tq_avg;
246 orig_tq_avg = router_orig->tq_avg;
248 /* the TQ value has to be better */
249 if (orig_tq_avg < gw_tq_avg)
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
256 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
257 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
260 bat_dbg(DBG_BATMAN, bat_priv,
261 "Restarting gateway selection: better gateway found (tq curr: "
263 gw_tq_avg, orig_tq_avg);
266 gw_deselect(bat_priv);
269 orig_node_free_ref(curr_gw_orig);
271 neigh_node_free_ref(router_gw);
273 neigh_node_free_ref(router_orig);
278 static void gw_node_add(struct bat_priv *bat_priv,
279 struct orig_node *orig_node, uint8_t new_gwflags)
281 struct gw_node *gw_node;
284 gw_node = kmalloc(sizeof(struct gw_node), GFP_ATOMIC);
288 memset(gw_node, 0, sizeof(struct gw_node));
289 INIT_HLIST_NODE(&gw_node->list);
290 gw_node->orig_node = orig_node;
291 atomic_set(&gw_node->refcount, 1);
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);
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"));
307 void gw_node_update(struct bat_priv *bat_priv,
308 struct orig_node *orig_node, uint8_t new_gwflags)
310 struct hlist_node *node;
311 struct gw_node *gw_node, *curr_gw;
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.
319 curr_gw = gw_get_selected_gw_node(bat_priv);
322 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
323 if (gw_node->orig_node != orig_node)
326 bat_dbg(DBG_BATMAN, bat_priv,
327 "Gateway class of originator %pM changed from "
329 orig_node->orig, gw_node->orig_node->gw_flags,
332 gw_node->deleted = 0;
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",
340 if (gw_node == curr_gw)
347 if (new_gwflags == 0)
350 gw_node_add(bat_priv, orig_node, new_gwflags);
354 gw_deselect(bat_priv);
359 gw_node_free_ref(curr_gw);
362 void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
364 return gw_node_update(bat_priv, orig_node, 0);
367 void gw_node_purge(struct bat_priv *bat_priv)
369 struct gw_node *gw_node, *curr_gw;
370 struct hlist_node *node, *node_tmp;
371 unsigned long timeout = 2 * PURGE_TIMEOUT * HZ;
372 char do_deselect = 0;
374 curr_gw = gw_get_selected_gw_node(bat_priv);
376 spin_lock_bh(&bat_priv->gw_list_lock);
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)
385 if (curr_gw == gw_node)
388 hlist_del_rcu(&gw_node->list);
389 gw_node_free_ref(gw_node);
392 spin_unlock_bh(&bat_priv->gw_list_lock);
394 /* gw_deselect() needs to acquire the gw_list_lock */
396 gw_deselect(bat_priv);
399 gw_node_free_ref(curr_gw);
403 * fails if orig_node has no router
405 static int _write_buffer_text(struct bat_priv *bat_priv,
406 struct seq_file *seq, struct gw_node *gw_node)
408 struct gw_node *curr_gw;
409 struct neigh_node *router;
410 int down, up, ret = -1;
412 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
414 router = orig_node_get_router(gw_node->orig_node);
418 curr_gw = gw_get_selected_gw_node(bat_priv);
420 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
421 (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"));
431 neigh_node_free_ref(router);
433 gw_node_free_ref(curr_gw);
438 int gw_client_seq_print_text(struct seq_file *seq, void *offset)
440 struct net_device *net_dev = (struct net_device *)seq->private;
441 struct bat_priv *bat_priv = netdev_priv(net_dev);
442 struct hard_iface *primary_if;
443 struct gw_node *gw_node;
444 struct hlist_node *node;
445 int gw_count = 0, ret = 0;
447 primary_if = primary_if_get_selected(bat_priv);
449 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
450 "specify interfaces to enable it\n",
455 if (primary_if->if_status != IF_ACTIVE) {
456 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
457 "primary interface not active\n",
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,
466 primary_if->net_dev->name,
467 primary_if->net_dev->dev_addr, net_dev->name);
470 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
471 if (gw_node->deleted)
474 /* fails if orig_node has no router */
475 if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
483 seq_printf(seq, "No gateways in range ...\n");
487 hardif_free_ref(primary_if);
491 int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
493 struct ethhdr *ethhdr;
495 struct ipv6hdr *ipv6hdr;
496 struct udphdr *udphdr;
497 struct gw_node *curr_gw;
498 unsigned int header_len = 0;
500 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
503 /* check for ethernet header */
504 if (!pskb_may_pull(skb, header_len + ETH_HLEN))
506 ethhdr = (struct ethhdr *)skb->data;
507 header_len += ETH_HLEN;
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))
513 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
514 header_len += VLAN_HLEN;
517 /* check for ip header */
518 switch (ntohs(ethhdr->h_proto)) {
520 if (!pskb_may_pull(skb, header_len + sizeof(struct iphdr)))
522 iphdr = (struct iphdr *)(skb->data + header_len);
523 header_len += iphdr->ihl * 4;
525 /* check for udp header */
526 if (iphdr->protocol != IPPROTO_UDP)
531 if (!pskb_may_pull(skb, header_len + sizeof(struct ipv6hdr)))
533 ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
534 header_len += sizeof(struct ipv6hdr);
536 /* check for udp header */
537 if (ipv6hdr->nexthdr != IPPROTO_UDP)
545 if (!pskb_may_pull(skb, header_len + sizeof(struct udphdr)))
547 udphdr = (struct udphdr *)(skb->data + header_len);
548 header_len += sizeof(struct udphdr);
550 /* check for bootp port */
551 if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
552 (ntohs(udphdr->dest) != 67))
555 if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
556 (ntohs(udphdr->dest) != 547))
559 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
562 curr_gw = gw_get_selected_gw_node(bat_priv);
567 gw_node_free_ref(curr_gw);