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