batman-adv: Protect global TQ window with a spinlock
[deliverable/linux.git] / net / batman-adv / gateway_client.c
CommitLineData
c6c8fea2 1/*
64afe353 2 * Copyright (C) 2009-2011 B.A.T.M.A.N. contributors:
c6c8fea2
SE
3 *
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"
57f0c07c 26#include "originator.h"
c6c8fea2
SE
27#include <linux/ip.h>
28#include <linux/ipv6.h>
29#include <linux/udp.h>
30#include <linux/if_vlan.h>
31
25b6d3c1 32static void gw_node_free_rcu(struct rcu_head *rcu)
c6c8fea2
SE
33{
34 struct gw_node *gw_node;
35
25b6d3c1 36 gw_node = container_of(rcu, struct gw_node, rcu);
c6c8fea2
SE
37 kfree(gw_node);
38}
39
25b6d3c1 40static void gw_node_free_ref(struct gw_node *gw_node)
c6c8fea2 41{
25b6d3c1
ML
42 if (atomic_dec_and_test(&gw_node->refcount))
43 call_rcu(&gw_node->rcu, gw_node_free_rcu);
c6c8fea2
SE
44}
45
4c804850 46struct orig_node *gw_get_selected(struct bat_priv *bat_priv)
c6c8fea2 47{
5d02b3cd
LL
48 struct gw_node *curr_gateway_tmp;
49 struct orig_node *orig_node = NULL;
c6c8fea2 50
5d02b3cd
LL
51 rcu_read_lock();
52 curr_gateway_tmp = rcu_dereference(bat_priv->curr_gw);
c6c8fea2 53 if (!curr_gateway_tmp)
5d02b3cd
LL
54 goto out;
55
56 orig_node = curr_gateway_tmp->orig_node;
7b36e8ee
ML
57 if (!orig_node)
58 goto out;
c6c8fea2 59
7b36e8ee
ML
60 if (!atomic_inc_not_zero(&orig_node->refcount))
61 orig_node = NULL;
43c70ad5 62
5d02b3cd
LL
63out:
64 rcu_read_unlock();
65 return orig_node;
c6c8fea2
SE
66}
67
68void gw_deselect(struct bat_priv *bat_priv)
69{
5d02b3cd 70 struct gw_node *gw_node;
c6c8fea2 71
5d02b3cd
LL
72 spin_lock_bh(&bat_priv->gw_list_lock);
73 gw_node = rcu_dereference(bat_priv->curr_gw);
74 rcu_assign_pointer(bat_priv->curr_gw, NULL);
75 spin_unlock_bh(&bat_priv->gw_list_lock);
c6c8fea2
SE
76
77 if (gw_node)
25b6d3c1 78 gw_node_free_ref(gw_node);
c6c8fea2
SE
79}
80
25b6d3c1 81static void gw_select(struct bat_priv *bat_priv, struct gw_node *new_gw_node)
c6c8fea2 82{
5d02b3cd 83 struct gw_node *curr_gw_node;
c6c8fea2 84
25b6d3c1
ML
85 if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
86 new_gw_node = NULL;
c6c8fea2 87
5d02b3cd
LL
88 spin_lock_bh(&bat_priv->gw_list_lock);
89 curr_gw_node = rcu_dereference(bat_priv->curr_gw);
90 rcu_assign_pointer(bat_priv->curr_gw, new_gw_node);
91 spin_unlock_bh(&bat_priv->gw_list_lock);
25b6d3c1
ML
92
93 if (curr_gw_node)
94 gw_node_free_ref(curr_gw_node);
c6c8fea2
SE
95}
96
97void gw_election(struct bat_priv *bat_priv)
98{
99 struct hlist_node *node;
5d02b3cd 100 struct gw_node *gw_node, *curr_gw, *curr_gw_tmp = NULL;
e1a5382f 101 struct neigh_node *router;
c6c8fea2
SE
102 uint8_t max_tq = 0;
103 uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
104 int down, up;
105
106 /**
107 * The batman daemon checks here if we already passed a full originator
108 * cycle in order to make sure we don't choose the first gateway we
109 * hear about. This check is based on the daemon's uptime which we
110 * don't have.
111 **/
112 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
113 return;
114
5d02b3cd
LL
115 rcu_read_lock();
116 curr_gw = rcu_dereference(bat_priv->curr_gw);
117 if (curr_gw) {
118 rcu_read_unlock();
c6c8fea2 119 return;
5d02b3cd 120 }
c6c8fea2 121
c6c8fea2 122 if (hlist_empty(&bat_priv->gw_list)) {
c6c8fea2 123
5d02b3cd
LL
124 if (curr_gw) {
125 rcu_read_unlock();
c6c8fea2
SE
126 bat_dbg(DBG_BATMAN, bat_priv,
127 "Removing selected gateway - "
128 "no gateway in range\n");
129 gw_deselect(bat_priv);
5d02b3cd
LL
130 } else
131 rcu_read_unlock();
c6c8fea2
SE
132
133 return;
134 }
135
136 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
e1a5382f 137 if (gw_node->deleted)
c6c8fea2
SE
138 continue;
139
e1a5382f
LL
140 router = orig_node_get_router(gw_node->orig_node);
141 if (!router)
c6c8fea2
SE
142 continue;
143
144 switch (atomic_read(&bat_priv->gw_sel_class)) {
145 case 1: /* fast connection */
146 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags,
147 &down, &up);
148
e1a5382f 149 tmp_gw_factor = (router->tq_avg * router->tq_avg *
c6c8fea2
SE
150 down * 100 * 100) /
151 (TQ_LOCAL_WINDOW_SIZE *
152 TQ_LOCAL_WINDOW_SIZE * 64);
153
154 if ((tmp_gw_factor > max_gw_factor) ||
155 ((tmp_gw_factor == max_gw_factor) &&
e1a5382f 156 (router->tq_avg > max_tq)))
c6c8fea2
SE
157 curr_gw_tmp = gw_node;
158 break;
159
160 default: /**
161 * 2: stable connection (use best statistic)
162 * 3: fast-switch (use best statistic but change as
163 * soon as a better gateway appears)
164 * XX: late-switch (use best statistic but change as
165 * soon as a better gateway appears which has
166 * $routing_class more tq points)
167 **/
e1a5382f 168 if (router->tq_avg > max_tq)
c6c8fea2
SE
169 curr_gw_tmp = gw_node;
170 break;
171 }
172
e1a5382f
LL
173 if (router->tq_avg > max_tq)
174 max_tq = router->tq_avg;
c6c8fea2
SE
175
176 if (tmp_gw_factor > max_gw_factor)
177 max_gw_factor = tmp_gw_factor;
e1a5382f
LL
178
179 neigh_node_free_ref(router);
c6c8fea2
SE
180 }
181
5d02b3cd 182 if (curr_gw != curr_gw_tmp) {
e1a5382f
LL
183 router = orig_node_get_router(curr_gw_tmp->orig_node);
184 if (!router)
185 goto out;
186
5d02b3cd 187 if ((curr_gw) && (!curr_gw_tmp))
c6c8fea2
SE
188 bat_dbg(DBG_BATMAN, bat_priv,
189 "Removing selected gateway - "
190 "no gateway in range\n");
5d02b3cd 191 else if ((!curr_gw) && (curr_gw_tmp))
c6c8fea2
SE
192 bat_dbg(DBG_BATMAN, bat_priv,
193 "Adding route to gateway %pM "
194 "(gw_flags: %i, tq: %i)\n",
195 curr_gw_tmp->orig_node->orig,
196 curr_gw_tmp->orig_node->gw_flags,
e1a5382f 197 router->tq_avg);
c6c8fea2
SE
198 else
199 bat_dbg(DBG_BATMAN, bat_priv,
200 "Changing route to gateway %pM "
201 "(gw_flags: %i, tq: %i)\n",
202 curr_gw_tmp->orig_node->orig,
203 curr_gw_tmp->orig_node->gw_flags,
e1a5382f 204 router->tq_avg);
c6c8fea2 205
e1a5382f 206 neigh_node_free_ref(router);
25b6d3c1 207 gw_select(bat_priv, curr_gw_tmp);
c6c8fea2
SE
208 }
209
e1a5382f 210out:
c6c8fea2 211 rcu_read_unlock();
c6c8fea2
SE
212}
213
214void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
215{
57f0c07c 216 struct orig_node *curr_gw_orig;
e1a5382f 217 struct neigh_node *router_gw = NULL, *router_orig = NULL;
c6c8fea2
SE
218 uint8_t gw_tq_avg, orig_tq_avg;
219
57f0c07c
LL
220 curr_gw_orig = gw_get_selected(bat_priv);
221 if (!curr_gw_orig)
222 goto deselect;
c6c8fea2 223
e1a5382f
LL
224 router_gw = orig_node_get_router(curr_gw_orig);
225 if (!router_gw)
226 goto deselect;
c6c8fea2
SE
227
228 /* this node already is the gateway */
57f0c07c 229 if (curr_gw_orig == orig_node)
e1a5382f 230 goto out;
c6c8fea2 231
e1a5382f
LL
232 router_orig = orig_node_get_router(orig_node);
233 if (!router_orig)
234 goto out;
5d02b3cd 235
e1a5382f
LL
236 gw_tq_avg = router_gw->tq_avg;
237 orig_tq_avg = router_orig->tq_avg;
c6c8fea2
SE
238
239 /* the TQ value has to be better */
240 if (orig_tq_avg < gw_tq_avg)
5d02b3cd 241 goto out;
c6c8fea2
SE
242
243 /**
244 * if the routing class is greater than 3 the value tells us how much
245 * greater the TQ value of the new gateway must be
246 **/
247 if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
248 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
5d02b3cd 249 goto out;
c6c8fea2
SE
250
251 bat_dbg(DBG_BATMAN, bat_priv,
252 "Restarting gateway selection: better gateway found (tq curr: "
253 "%i, tq new: %i)\n",
254 gw_tq_avg, orig_tq_avg);
255
256deselect:
257 gw_deselect(bat_priv);
5d02b3cd 258out:
57f0c07c
LL
259 if (curr_gw_orig)
260 orig_node_free_ref(curr_gw_orig);
e1a5382f
LL
261 if (router_gw)
262 neigh_node_free_ref(router_gw);
263 if (router_orig)
264 neigh_node_free_ref(router_orig);
57f0c07c 265
5d02b3cd 266 return;
c6c8fea2
SE
267}
268
269static void gw_node_add(struct bat_priv *bat_priv,
270 struct orig_node *orig_node, uint8_t new_gwflags)
271{
272 struct gw_node *gw_node;
273 int down, up;
274
275 gw_node = kmalloc(sizeof(struct gw_node), GFP_ATOMIC);
276 if (!gw_node)
277 return;
278
279 memset(gw_node, 0, sizeof(struct gw_node));
280 INIT_HLIST_NODE(&gw_node->list);
281 gw_node->orig_node = orig_node;
25b6d3c1 282 atomic_set(&gw_node->refcount, 1);
c6c8fea2
SE
283
284 spin_lock_bh(&bat_priv->gw_list_lock);
285 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
286 spin_unlock_bh(&bat_priv->gw_list_lock);
287
288 gw_bandwidth_to_kbit(new_gwflags, &down, &up);
289 bat_dbg(DBG_BATMAN, bat_priv,
290 "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n",
291 orig_node->orig, new_gwflags,
292 (down > 2048 ? down / 1024 : down),
293 (down > 2048 ? "MBit" : "KBit"),
294 (up > 2048 ? up / 1024 : up),
295 (up > 2048 ? "MBit" : "KBit"));
296}
297
298void gw_node_update(struct bat_priv *bat_priv,
299 struct orig_node *orig_node, uint8_t new_gwflags)
300{
301 struct hlist_node *node;
302 struct gw_node *gw_node;
303
304 rcu_read_lock();
305 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
306 if (gw_node->orig_node != orig_node)
307 continue;
308
309 bat_dbg(DBG_BATMAN, bat_priv,
310 "Gateway class of originator %pM changed from "
311 "%i to %i\n",
312 orig_node->orig, gw_node->orig_node->gw_flags,
313 new_gwflags);
314
315 gw_node->deleted = 0;
316
317 if (new_gwflags == 0) {
318 gw_node->deleted = jiffies;
319 bat_dbg(DBG_BATMAN, bat_priv,
320 "Gateway %pM removed from gateway list\n",
321 orig_node->orig);
322
5d02b3cd 323 if (gw_node == rcu_dereference(bat_priv->curr_gw)) {
c6c8fea2
SE
324 rcu_read_unlock();
325 gw_deselect(bat_priv);
326 return;
327 }
328 }
329
330 rcu_read_unlock();
331 return;
332 }
333 rcu_read_unlock();
334
335 if (new_gwflags == 0)
336 return;
337
338 gw_node_add(bat_priv, orig_node, new_gwflags);
339}
340
341void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
342{
343 return gw_node_update(bat_priv, orig_node, 0);
344}
345
346void gw_node_purge(struct bat_priv *bat_priv)
347{
348 struct gw_node *gw_node;
349 struct hlist_node *node, *node_tmp;
350 unsigned long timeout = 2 * PURGE_TIMEOUT * HZ;
351
352 spin_lock_bh(&bat_priv->gw_list_lock);
353
354 hlist_for_each_entry_safe(gw_node, node, node_tmp,
355 &bat_priv->gw_list, list) {
356 if (((!gw_node->deleted) ||
357 (time_before(jiffies, gw_node->deleted + timeout))) &&
358 atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
359 continue;
360
5d02b3cd 361 if (rcu_dereference(bat_priv->curr_gw) == gw_node)
c6c8fea2
SE
362 gw_deselect(bat_priv);
363
364 hlist_del_rcu(&gw_node->list);
25b6d3c1 365 gw_node_free_ref(gw_node);
c6c8fea2
SE
366 }
367
368
369 spin_unlock_bh(&bat_priv->gw_list_lock);
370}
371
e1a5382f
LL
372/**
373 * fails if orig_node has no router
374 */
c6c8fea2
SE
375static int _write_buffer_text(struct bat_priv *bat_priv,
376 struct seq_file *seq, struct gw_node *gw_node)
377{
5d02b3cd 378 struct gw_node *curr_gw;
e1a5382f
LL
379 struct neigh_node *router;
380 int down, up, ret = -1;
c6c8fea2
SE
381
382 gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
383
e1a5382f
LL
384 router = orig_node_get_router(gw_node->orig_node);
385 if (!router)
386 goto out;
387
5d02b3cd
LL
388 rcu_read_lock();
389 curr_gw = rcu_dereference(bat_priv->curr_gw);
390
391 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
392 (curr_gw == gw_node ? "=>" : " "),
c6c8fea2 393 gw_node->orig_node->orig,
e1a5382f
LL
394 router->tq_avg, router->addr,
395 router->if_incoming->net_dev->name,
c6c8fea2
SE
396 gw_node->orig_node->gw_flags,
397 (down > 2048 ? down / 1024 : down),
398 (down > 2048 ? "MBit" : "KBit"),
399 (up > 2048 ? up / 1024 : up),
400 (up > 2048 ? "MBit" : "KBit"));
5d02b3cd
LL
401
402 rcu_read_unlock();
e1a5382f
LL
403 neigh_node_free_ref(router);
404out:
5d02b3cd 405 return ret;
c6c8fea2
SE
406}
407
408int gw_client_seq_print_text(struct seq_file *seq, void *offset)
409{
410 struct net_device *net_dev = (struct net_device *)seq->private;
411 struct bat_priv *bat_priv = netdev_priv(net_dev);
412 struct gw_node *gw_node;
413 struct hlist_node *node;
414 int gw_count = 0;
415
416 if (!bat_priv->primary_if) {
417
418 return seq_printf(seq, "BATMAN mesh %s disabled - please "
419 "specify interfaces to enable it\n",
420 net_dev->name);
421 }
422
423 if (bat_priv->primary_if->if_status != IF_ACTIVE) {
424
425 return seq_printf(seq, "BATMAN mesh %s disabled - "
426 "primary interface not active\n",
427 net_dev->name);
428 }
429
430 seq_printf(seq, " %-12s (%s/%i) %17s [%10s]: gw_class ... "
431 "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
432 "Gateway", "#", TQ_MAX_VALUE, "Nexthop",
433 "outgoingIF", SOURCE_VERSION, REVISION_VERSION_STR,
434 bat_priv->primary_if->net_dev->name,
435 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
436
437 rcu_read_lock();
438 hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
439 if (gw_node->deleted)
440 continue;
441
e1a5382f
LL
442 /* fails if orig_node has no router */
443 if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
c6c8fea2
SE
444 continue;
445
c6c8fea2
SE
446 gw_count++;
447 }
448 rcu_read_unlock();
449
450 if (gw_count == 0)
451 seq_printf(seq, "No gateways in range ...\n");
452
453 return 0;
454}
455
456int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
457{
458 struct ethhdr *ethhdr;
459 struct iphdr *iphdr;
460 struct ipv6hdr *ipv6hdr;
461 struct udphdr *udphdr;
462 unsigned int header_len = 0;
463
464 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
465 return 0;
466
467 /* check for ethernet header */
468 if (!pskb_may_pull(skb, header_len + ETH_HLEN))
469 return 0;
470 ethhdr = (struct ethhdr *)skb->data;
471 header_len += ETH_HLEN;
472
473 /* check for initial vlan header */
474 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
475 if (!pskb_may_pull(skb, header_len + VLAN_HLEN))
476 return 0;
477 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
478 header_len += VLAN_HLEN;
479 }
480
481 /* check for ip header */
482 switch (ntohs(ethhdr->h_proto)) {
483 case ETH_P_IP:
484 if (!pskb_may_pull(skb, header_len + sizeof(struct iphdr)))
485 return 0;
486 iphdr = (struct iphdr *)(skb->data + header_len);
487 header_len += iphdr->ihl * 4;
488
489 /* check for udp header */
490 if (iphdr->protocol != IPPROTO_UDP)
491 return 0;
492
493 break;
494 case ETH_P_IPV6:
495 if (!pskb_may_pull(skb, header_len + sizeof(struct ipv6hdr)))
496 return 0;
497 ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
498 header_len += sizeof(struct ipv6hdr);
499
500 /* check for udp header */
501 if (ipv6hdr->nexthdr != IPPROTO_UDP)
502 return 0;
503
504 break;
505 default:
506 return 0;
507 }
508
509 if (!pskb_may_pull(skb, header_len + sizeof(struct udphdr)))
510 return 0;
511 udphdr = (struct udphdr *)(skb->data + header_len);
512 header_len += sizeof(struct udphdr);
513
514 /* check for bootp port */
515 if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
516 (ntohs(udphdr->dest) != 67))
517 return 0;
518
519 if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
520 (ntohs(udphdr->dest) != 547))
521 return 0;
522
523 if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
524 return -1;
525
5d02b3cd
LL
526 rcu_read_lock();
527 if (!rcu_dereference(bat_priv->curr_gw)) {
528 rcu_read_unlock();
c6c8fea2 529 return 0;
5d02b3cd
LL
530 }
531 rcu_read_unlock();
c6c8fea2
SE
532
533 return 1;
534}
This page took 0.108227 seconds and 5 git commands to generate.