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