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