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" |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 26 | #include "originator.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 27 | #include <linux/ip.h> |
| 28 | #include <linux/ipv6.h> |
| 29 | #include <linux/udp.h> |
| 30 | #include <linux/if_vlan.h> |
| 31 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 32 | static void gw_node_free_rcu(struct rcu_head *rcu) |
| 33 | { |
| 34 | struct gw_node *gw_node; |
| 35 | |
| 36 | gw_node = container_of(rcu, struct gw_node, rcu); |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 37 | kfree(gw_node); |
| 38 | } |
| 39 | |
| 40 | static void gw_node_free_ref(struct gw_node *gw_node) |
| 41 | { |
| 42 | if (atomic_dec_and_test(&gw_node->refcount)) |
| 43 | call_rcu(&gw_node->rcu, gw_node_free_rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 46 | static struct gw_node *gw_get_selected_gw_node(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 47 | { |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 48 | struct gw_node *gw_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 49 | |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 50 | rcu_read_lock(); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 51 | gw_node = rcu_dereference(bat_priv->curr_gw); |
| 52 | if (!gw_node) |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 53 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 54 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 55 | if (!atomic_inc_not_zero(&gw_node->refcount)) |
| 56 | gw_node = NULL; |
| 57 | |
| 58 | out: |
| 59 | rcu_read_unlock(); |
| 60 | return gw_node; |
| 61 | } |
| 62 | |
| 63 | struct orig_node *gw_get_selected_orig(struct bat_priv *bat_priv) |
| 64 | { |
| 65 | struct gw_node *gw_node; |
| 66 | struct orig_node *orig_node = NULL; |
| 67 | |
| 68 | gw_node = gw_get_selected_gw_node(bat_priv); |
| 69 | if (!gw_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 70 | goto out; |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 71 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 72 | rcu_read_lock(); |
| 73 | orig_node = gw_node->orig_node; |
| 74 | if (!orig_node) |
| 75 | goto unlock; |
| 76 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 77 | if (!atomic_inc_not_zero(&orig_node->refcount)) |
| 78 | orig_node = NULL; |
Linus Lüssing | 43c70ad | 2011-02-13 21:13:04 +0000 | [diff] [blame] | 79 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 80 | unlock: |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 81 | rcu_read_unlock(); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 82 | out: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 83 | if (gw_node) |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 84 | gw_node_free_ref(gw_node); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 85 | return orig_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 88 | 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] | 89 | { |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 90 | struct gw_node *curr_gw_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 91 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 92 | spin_lock_bh(&bat_priv->gw_list_lock); |
| 93 | |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 94 | if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount)) |
| 95 | new_gw_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 96 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 97 | curr_gw_node = bat_priv->curr_gw; |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 98 | rcu_assign_pointer(bat_priv->curr_gw, new_gw_node); |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 99 | |
| 100 | if (curr_gw_node) |
| 101 | gw_node_free_ref(curr_gw_node); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 102 | |
| 103 | spin_unlock_bh(&bat_priv->gw_list_lock); |
| 104 | } |
| 105 | |
| 106 | void gw_deselect(struct bat_priv *bat_priv) |
| 107 | { |
| 108 | gw_select(bat_priv, NULL); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void gw_election(struct bat_priv *bat_priv) |
| 112 | { |
| 113 | struct hlist_node *node; |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 114 | struct gw_node *gw_node, *curr_gw = NULL, *curr_gw_tmp = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 115 | struct neigh_node *router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 116 | uint8_t max_tq = 0; |
| 117 | uint32_t max_gw_factor = 0, tmp_gw_factor = 0; |
| 118 | int down, up; |
| 119 | |
| 120 | /** |
| 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 |
| 124 | * don't have. |
| 125 | **/ |
| 126 | if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT) |
| 127 | return; |
| 128 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 129 | curr_gw = gw_get_selected_gw_node(bat_priv); |
| 130 | if (!curr_gw) |
| 131 | goto out; |
| 132 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 133 | rcu_read_lock(); |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 134 | if (hlist_empty(&bat_priv->gw_list)) { |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 135 | bat_dbg(DBG_BATMAN, bat_priv, |
| 136 | "Removing selected gateway - " |
| 137 | "no gateway in range\n"); |
| 138 | gw_deselect(bat_priv); |
| 139 | goto unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) { |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 143 | if (gw_node->deleted) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 144 | continue; |
| 145 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 146 | router = orig_node_get_router(gw_node->orig_node); |
| 147 | if (!router) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 148 | continue; |
| 149 | |
| 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, |
| 153 | &down, &up); |
| 154 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 155 | tmp_gw_factor = (router->tq_avg * router->tq_avg * |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 156 | down * 100 * 100) / |
| 157 | (TQ_LOCAL_WINDOW_SIZE * |
| 158 | TQ_LOCAL_WINDOW_SIZE * 64); |
| 159 | |
| 160 | if ((tmp_gw_factor > max_gw_factor) || |
| 161 | ((tmp_gw_factor == max_gw_factor) && |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 162 | (router->tq_avg > max_tq))) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 163 | curr_gw_tmp = gw_node; |
| 164 | break; |
| 165 | |
| 166 | default: /** |
| 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) |
| 173 | **/ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 174 | if (router->tq_avg > max_tq) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 175 | curr_gw_tmp = gw_node; |
| 176 | break; |
| 177 | } |
| 178 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 179 | if (router->tq_avg > max_tq) |
| 180 | max_tq = router->tq_avg; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 181 | |
| 182 | if (tmp_gw_factor > max_gw_factor) |
| 183 | max_gw_factor = tmp_gw_factor; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 184 | |
| 185 | neigh_node_free_ref(router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 188 | if (curr_gw != curr_gw_tmp) { |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 189 | router = orig_node_get_router(curr_gw_tmp->orig_node); |
| 190 | if (!router) |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 191 | goto unlock; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 192 | |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 193 | if ((curr_gw) && (!curr_gw_tmp)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 194 | bat_dbg(DBG_BATMAN, bat_priv, |
| 195 | "Removing selected gateway - " |
| 196 | "no gateway in range\n"); |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 197 | else if ((!curr_gw) && (curr_gw_tmp)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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, |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 203 | router->tq_avg); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 204 | else |
| 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, |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 210 | router->tq_avg); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 211 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 212 | neigh_node_free_ref(router); |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 213 | gw_select(bat_priv, curr_gw_tmp); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 216 | unlock: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 217 | rcu_read_unlock(); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 218 | out: |
| 219 | if (curr_gw) |
| 220 | gw_node_free_ref(curr_gw); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node) |
| 224 | { |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 225 | struct orig_node *curr_gw_orig; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 226 | struct neigh_node *router_gw = NULL, *router_orig = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 227 | uint8_t gw_tq_avg, orig_tq_avg; |
| 228 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 229 | curr_gw_orig = gw_get_selected_orig(bat_priv); |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 230 | if (!curr_gw_orig) |
| 231 | goto deselect; |
| 232 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 233 | router_gw = orig_node_get_router(curr_gw_orig); |
| 234 | if (!router_gw) |
| 235 | goto deselect; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 236 | |
| 237 | /* this node already is the gateway */ |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 238 | if (curr_gw_orig == orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 239 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 240 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 241 | router_orig = orig_node_get_router(orig_node); |
| 242 | if (!router_orig) |
| 243 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 244 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 245 | gw_tq_avg = router_gw->tq_avg; |
| 246 | orig_tq_avg = router_orig->tq_avg; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 247 | |
| 248 | /* the TQ value has to be better */ |
| 249 | if (orig_tq_avg < gw_tq_avg) |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 250 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 251 | |
| 252 | /** |
| 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 |
| 255 | **/ |
| 256 | if ((atomic_read(&bat_priv->gw_sel_class) > 3) && |
| 257 | (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class))) |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 258 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 259 | |
| 260 | bat_dbg(DBG_BATMAN, bat_priv, |
| 261 | "Restarting gateway selection: better gateway found (tq curr: " |
| 262 | "%i, tq new: %i)\n", |
| 263 | gw_tq_avg, orig_tq_avg); |
| 264 | |
| 265 | deselect: |
| 266 | gw_deselect(bat_priv); |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 267 | out: |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 268 | if (curr_gw_orig) |
| 269 | orig_node_free_ref(curr_gw_orig); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 270 | if (router_gw) |
| 271 | neigh_node_free_ref(router_gw); |
| 272 | if (router_orig) |
| 273 | neigh_node_free_ref(router_orig); |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 274 | |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 275 | return; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static void gw_node_add(struct bat_priv *bat_priv, |
| 279 | struct orig_node *orig_node, uint8_t new_gwflags) |
| 280 | { |
| 281 | struct gw_node *gw_node; |
| 282 | int down, up; |
| 283 | |
| 284 | gw_node = kmalloc(sizeof(struct gw_node), GFP_ATOMIC); |
| 285 | if (!gw_node) |
| 286 | return; |
| 287 | |
| 288 | memset(gw_node, 0, sizeof(struct gw_node)); |
| 289 | INIT_HLIST_NODE(&gw_node->list); |
| 290 | gw_node->orig_node = orig_node; |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 291 | atomic_set(&gw_node->refcount, 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 292 | |
| 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); |
| 296 | |
| 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")); |
| 305 | } |
| 306 | |
| 307 | void gw_node_update(struct bat_priv *bat_priv, |
| 308 | struct orig_node *orig_node, uint8_t new_gwflags) |
| 309 | { |
| 310 | struct hlist_node *node; |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 311 | struct gw_node *gw_node, *curr_gw; |
| 312 | |
| 313 | curr_gw = gw_get_selected_gw_node(bat_priv); |
| 314 | if (!curr_gw) |
| 315 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 316 | |
| 317 | rcu_read_lock(); |
| 318 | hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) { |
| 319 | if (gw_node->orig_node != orig_node) |
| 320 | continue; |
| 321 | |
| 322 | bat_dbg(DBG_BATMAN, bat_priv, |
| 323 | "Gateway class of originator %pM changed from " |
| 324 | "%i to %i\n", |
| 325 | orig_node->orig, gw_node->orig_node->gw_flags, |
| 326 | new_gwflags); |
| 327 | |
| 328 | gw_node->deleted = 0; |
| 329 | |
| 330 | if (new_gwflags == 0) { |
| 331 | gw_node->deleted = jiffies; |
| 332 | bat_dbg(DBG_BATMAN, bat_priv, |
| 333 | "Gateway %pM removed from gateway list\n", |
| 334 | orig_node->orig); |
| 335 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 336 | if (gw_node == curr_gw) |
| 337 | goto deselect; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 340 | goto unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 341 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 342 | |
| 343 | if (new_gwflags == 0) |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 344 | goto unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 345 | |
| 346 | gw_node_add(bat_priv, orig_node, new_gwflags); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 347 | goto unlock; |
| 348 | |
| 349 | deselect: |
| 350 | gw_deselect(bat_priv); |
| 351 | unlock: |
| 352 | rcu_read_unlock(); |
| 353 | out: |
| 354 | if (curr_gw) |
| 355 | gw_node_free_ref(curr_gw); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node) |
| 359 | { |
| 360 | return gw_node_update(bat_priv, orig_node, 0); |
| 361 | } |
| 362 | |
| 363 | void gw_node_purge(struct bat_priv *bat_priv) |
| 364 | { |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 365 | struct gw_node *gw_node, *curr_gw; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 366 | struct hlist_node *node, *node_tmp; |
| 367 | unsigned long timeout = 2 * PURGE_TIMEOUT * HZ; |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 368 | char do_deselect = 0; |
| 369 | |
| 370 | curr_gw = gw_get_selected_gw_node(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 371 | |
| 372 | spin_lock_bh(&bat_priv->gw_list_lock); |
| 373 | |
| 374 | hlist_for_each_entry_safe(gw_node, node, node_tmp, |
| 375 | &bat_priv->gw_list, list) { |
| 376 | if (((!gw_node->deleted) || |
| 377 | (time_before(jiffies, gw_node->deleted + timeout))) && |
| 378 | atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) |
| 379 | continue; |
| 380 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 381 | if (curr_gw == gw_node) |
| 382 | do_deselect = 1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 383 | |
| 384 | hlist_del_rcu(&gw_node->list); |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 385 | gw_node_free_ref(gw_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 388 | spin_unlock_bh(&bat_priv->gw_list_lock); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 389 | |
| 390 | /* gw_deselect() needs to acquire the gw_list_lock */ |
| 391 | if (do_deselect) |
| 392 | gw_deselect(bat_priv); |
| 393 | |
| 394 | if (curr_gw) |
| 395 | gw_node_free_ref(curr_gw); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 398 | /** |
| 399 | * fails if orig_node has no router |
| 400 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 401 | static int _write_buffer_text(struct bat_priv *bat_priv, |
| 402 | struct seq_file *seq, struct gw_node *gw_node) |
| 403 | { |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 404 | struct gw_node *curr_gw; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 405 | struct neigh_node *router; |
| 406 | int down, up, ret = -1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 407 | |
| 408 | gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up); |
| 409 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 410 | router = orig_node_get_router(gw_node->orig_node); |
| 411 | if (!router) |
| 412 | goto out; |
| 413 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 414 | curr_gw = gw_get_selected_gw_node(bat_priv); |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 415 | |
| 416 | ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n", |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 417 | (curr_gw == gw_node ? "=>" : " "), |
| 418 | gw_node->orig_node->orig, |
| 419 | router->tq_avg, router->addr, |
| 420 | router->if_incoming->net_dev->name, |
| 421 | gw_node->orig_node->gw_flags, |
| 422 | (down > 2048 ? down / 1024 : down), |
| 423 | (down > 2048 ? "MBit" : "KBit"), |
| 424 | (up > 2048 ? up / 1024 : up), |
| 425 | (up > 2048 ? "MBit" : "KBit")); |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 426 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 427 | neigh_node_free_ref(router); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 428 | if (curr_gw) |
| 429 | gw_node_free_ref(curr_gw); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 430 | out: |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 431 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | int gw_client_seq_print_text(struct seq_file *seq, void *offset) |
| 435 | { |
| 436 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 437 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
| 438 | struct gw_node *gw_node; |
| 439 | struct hlist_node *node; |
| 440 | int gw_count = 0; |
| 441 | |
| 442 | if (!bat_priv->primary_if) { |
| 443 | |
| 444 | return seq_printf(seq, "BATMAN mesh %s disabled - please " |
| 445 | "specify interfaces to enable it\n", |
| 446 | net_dev->name); |
| 447 | } |
| 448 | |
| 449 | if (bat_priv->primary_if->if_status != IF_ACTIVE) { |
| 450 | |
| 451 | return seq_printf(seq, "BATMAN mesh %s disabled - " |
| 452 | "primary interface not active\n", |
| 453 | net_dev->name); |
| 454 | } |
| 455 | |
| 456 | seq_printf(seq, " %-12s (%s/%i) %17s [%10s]: gw_class ... " |
| 457 | "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n", |
| 458 | "Gateway", "#", TQ_MAX_VALUE, "Nexthop", |
| 459 | "outgoingIF", SOURCE_VERSION, REVISION_VERSION_STR, |
| 460 | bat_priv->primary_if->net_dev->name, |
| 461 | bat_priv->primary_if->net_dev->dev_addr, net_dev->name); |
| 462 | |
| 463 | rcu_read_lock(); |
| 464 | hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) { |
| 465 | if (gw_node->deleted) |
| 466 | continue; |
| 467 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 468 | /* fails if orig_node has no router */ |
| 469 | if (_write_buffer_text(bat_priv, seq, gw_node) < 0) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 470 | continue; |
| 471 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 472 | gw_count++; |
| 473 | } |
| 474 | rcu_read_unlock(); |
| 475 | |
| 476 | if (gw_count == 0) |
| 477 | seq_printf(seq, "No gateways in range ...\n"); |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb) |
| 483 | { |
| 484 | struct ethhdr *ethhdr; |
| 485 | struct iphdr *iphdr; |
| 486 | struct ipv6hdr *ipv6hdr; |
| 487 | struct udphdr *udphdr; |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 488 | struct gw_node *curr_gw; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 489 | unsigned int header_len = 0; |
| 490 | |
| 491 | if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF) |
| 492 | return 0; |
| 493 | |
| 494 | /* check for ethernet header */ |
| 495 | if (!pskb_may_pull(skb, header_len + ETH_HLEN)) |
| 496 | return 0; |
| 497 | ethhdr = (struct ethhdr *)skb->data; |
| 498 | header_len += ETH_HLEN; |
| 499 | |
| 500 | /* check for initial vlan header */ |
| 501 | if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) { |
| 502 | if (!pskb_may_pull(skb, header_len + VLAN_HLEN)) |
| 503 | return 0; |
| 504 | ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN); |
| 505 | header_len += VLAN_HLEN; |
| 506 | } |
| 507 | |
| 508 | /* check for ip header */ |
| 509 | switch (ntohs(ethhdr->h_proto)) { |
| 510 | case ETH_P_IP: |
| 511 | if (!pskb_may_pull(skb, header_len + sizeof(struct iphdr))) |
| 512 | return 0; |
| 513 | iphdr = (struct iphdr *)(skb->data + header_len); |
| 514 | header_len += iphdr->ihl * 4; |
| 515 | |
| 516 | /* check for udp header */ |
| 517 | if (iphdr->protocol != IPPROTO_UDP) |
| 518 | return 0; |
| 519 | |
| 520 | break; |
| 521 | case ETH_P_IPV6: |
| 522 | if (!pskb_may_pull(skb, header_len + sizeof(struct ipv6hdr))) |
| 523 | return 0; |
| 524 | ipv6hdr = (struct ipv6hdr *)(skb->data + header_len); |
| 525 | header_len += sizeof(struct ipv6hdr); |
| 526 | |
| 527 | /* check for udp header */ |
| 528 | if (ipv6hdr->nexthdr != IPPROTO_UDP) |
| 529 | return 0; |
| 530 | |
| 531 | break; |
| 532 | default: |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | if (!pskb_may_pull(skb, header_len + sizeof(struct udphdr))) |
| 537 | return 0; |
| 538 | udphdr = (struct udphdr *)(skb->data + header_len); |
| 539 | header_len += sizeof(struct udphdr); |
| 540 | |
| 541 | /* check for bootp port */ |
| 542 | if ((ntohs(ethhdr->h_proto) == ETH_P_IP) && |
| 543 | (ntohs(udphdr->dest) != 67)) |
| 544 | return 0; |
| 545 | |
| 546 | if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) && |
| 547 | (ntohs(udphdr->dest) != 547)) |
| 548 | return 0; |
| 549 | |
| 550 | if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER) |
| 551 | return -1; |
| 552 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 553 | curr_gw = gw_get_selected_gw_node(bat_priv); |
| 554 | if (!curr_gw) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 555 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 556 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame^] | 557 | if (curr_gw) |
| 558 | gw_node_free_ref(curr_gw); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 559 | return 1; |
| 560 | } |