Sven Eckelmann | 9f6446c | 2015-04-23 13:16:35 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2009-2015 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 2 | * |
| 3 | * Marek Lindner |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of version 2 of the GNU General Public |
| 7 | * License as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Antonio Quartulli | ebf38fb | 2013-11-03 20:40:48 +0100 | [diff] [blame] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 18 | #include "gateway_common.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 19 | #include "main.h" |
| 20 | |
| 21 | #include <linux/atomic.h> |
Sven Eckelmann | 77927b7 | 2015-06-21 14:42:49 +0200 | [diff] [blame] | 22 | #include <linux/errno.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 23 | #include <linux/byteorder/generic.h> |
| 24 | #include <linux/kernel.h> |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 25 | #include <linux/math64.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/stddef.h> |
| 28 | #include <linux/string.h> |
| 29 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 30 | #include "gateway_client.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 31 | #include "packet.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 32 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 33 | /** |
| 34 | * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download |
| 35 | * and upload bandwidth information |
| 36 | * @net_dev: the soft interface net device |
| 37 | * @buff: string buffer to parse |
| 38 | * @down: pointer holding the returned download bandwidth information |
| 39 | * @up: pointer holding the returned upload bandwidth information |
| 40 | * |
| 41 | * Returns false on parse error and true otherwise. |
| 42 | */ |
Sven Eckelmann | 8e714a5 | 2012-05-12 18:33:56 +0200 | [diff] [blame] | 43 | static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff, |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 44 | u32 *down, u32 *up) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 45 | { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 46 | enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 47 | char *slash_ptr, *tmp_ptr; |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 48 | u64 ldown, lup; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 49 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 50 | |
| 51 | slash_ptr = strchr(buff, '/'); |
| 52 | if (slash_ptr) |
| 53 | *slash_ptr = 0; |
| 54 | |
| 55 | if (strlen(buff) > 4) { |
| 56 | tmp_ptr = buff + strlen(buff) - 4; |
| 57 | |
Rasmus Villemoes | b7a8d75 | 2014-10-13 15:54:42 -0700 | [diff] [blame] | 58 | if (strncasecmp(tmp_ptr, "mbit", 4) == 0) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 59 | bw_unit_type = BATADV_BW_UNIT_MBIT; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 60 | |
Rasmus Villemoes | b7a8d75 | 2014-10-13 15:54:42 -0700 | [diff] [blame] | 61 | if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) || |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 62 | (bw_unit_type == BATADV_BW_UNIT_MBIT)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 63 | *tmp_ptr = '\0'; |
| 64 | } |
| 65 | |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 66 | ret = kstrtou64(buff, 10, &ldown); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 67 | if (ret) { |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 68 | batadv_err(net_dev, |
| 69 | "Download speed of gateway mode invalid: %s\n", |
| 70 | buff); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 74 | switch (bw_unit_type) { |
| 75 | case BATADV_BW_UNIT_MBIT: |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 76 | /* prevent overflow */ |
| 77 | if (U64_MAX / 10 < ldown) { |
| 78 | batadv_err(net_dev, |
| 79 | "Download speed of gateway mode too large: %s\n", |
| 80 | buff); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | ldown *= 10; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 85 | break; |
| 86 | case BATADV_BW_UNIT_KBIT: |
| 87 | default: |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 88 | ldown = div_u64(ldown, 100); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 89 | break; |
| 90 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 91 | |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 92 | if (U32_MAX < ldown) { |
| 93 | batadv_err(net_dev, |
| 94 | "Download speed of gateway mode too large: %s\n", |
| 95 | buff); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | *down = ldown; |
| 100 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 101 | /* we also got some upload info */ |
| 102 | if (slash_ptr) { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 103 | bw_unit_type = BATADV_BW_UNIT_KBIT; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 104 | |
| 105 | if (strlen(slash_ptr + 1) > 4) { |
| 106 | tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1); |
| 107 | |
Rasmus Villemoes | b7a8d75 | 2014-10-13 15:54:42 -0700 | [diff] [blame] | 108 | if (strncasecmp(tmp_ptr, "mbit", 4) == 0) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 109 | bw_unit_type = BATADV_BW_UNIT_MBIT; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 110 | |
Rasmus Villemoes | b7a8d75 | 2014-10-13 15:54:42 -0700 | [diff] [blame] | 111 | if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) || |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 112 | (bw_unit_type == BATADV_BW_UNIT_MBIT)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 113 | *tmp_ptr = '\0'; |
| 114 | } |
| 115 | |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 116 | ret = kstrtou64(slash_ptr + 1, 10, &lup); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 117 | if (ret) { |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 118 | batadv_err(net_dev, |
| 119 | "Upload speed of gateway mode invalid: %s\n", |
| 120 | slash_ptr + 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 124 | switch (bw_unit_type) { |
| 125 | case BATADV_BW_UNIT_MBIT: |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 126 | /* prevent overflow */ |
| 127 | if (U64_MAX / 10 < lup) { |
| 128 | batadv_err(net_dev, |
| 129 | "Upload speed of gateway mode too large: %s\n", |
| 130 | slash_ptr + 1); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | lup *= 10; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 135 | break; |
| 136 | case BATADV_BW_UNIT_KBIT: |
| 137 | default: |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 138 | lup = div_u64(lup, 100); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 139 | break; |
| 140 | } |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 141 | |
| 142 | if (U32_MAX < lup) { |
| 143 | batadv_err(net_dev, |
| 144 | "Upload speed of gateway mode too large: %s\n", |
| 145 | slash_ptr + 1); |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | *up = lup; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 155 | /** |
| 156 | * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway |
| 157 | * setting change |
| 158 | * @bat_priv: the bat priv with all the soft interface information |
| 159 | */ |
| 160 | void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv) |
| 161 | { |
| 162 | struct batadv_tvlv_gateway_data gw; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 163 | u32 down, up; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 164 | char gw_mode; |
| 165 | |
| 166 | gw_mode = atomic_read(&bat_priv->gw_mode); |
| 167 | |
| 168 | switch (gw_mode) { |
| 169 | case BATADV_GW_MODE_OFF: |
| 170 | case BATADV_GW_MODE_CLIENT: |
| 171 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1); |
| 172 | break; |
| 173 | case BATADV_GW_MODE_SERVER: |
| 174 | down = atomic_read(&bat_priv->gw.bandwidth_down); |
| 175 | up = atomic_read(&bat_priv->gw.bandwidth_up); |
| 176 | gw.bandwidth_down = htonl(down); |
| 177 | gw.bandwidth_up = htonl(up); |
| 178 | batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1, |
| 179 | &gw, sizeof(gw)); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
Sven Eckelmann | 84d5e5e | 2012-05-12 02:09:30 +0200 | [diff] [blame] | 184 | ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, |
| 185 | size_t count) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 186 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 187 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
Sven Eckelmann | 4f248cf | 2015-06-09 20:50:49 +0200 | [diff] [blame] | 188 | u32 down_curr; |
| 189 | u32 up_curr; |
| 190 | u32 down_new = 0; |
| 191 | u32 up_new = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 192 | bool ret; |
| 193 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 194 | down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down); |
| 195 | up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up); |
| 196 | |
| 197 | ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 198 | if (!ret) |
Sven Eckelmann | 77927b7 | 2015-06-21 14:42:49 +0200 | [diff] [blame] | 199 | return -EINVAL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 200 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 201 | if (!down_new) |
| 202 | down_new = 1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 203 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 204 | if (!up_new) |
| 205 | up_new = down_new / 5; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 206 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 207 | if (!up_new) |
| 208 | up_new = 1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 209 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 210 | if ((down_curr == down_new) && (up_curr == up_new)) |
Marek Lindner | dafe94b | 2012-05-11 16:10:50 +0800 | [diff] [blame] | 211 | return count; |
| 212 | |
Antonio Quartulli | 4e820e7 | 2013-11-04 20:59:41 +0100 | [diff] [blame] | 213 | batadv_gw_reselect(bat_priv); |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 214 | batadv_info(net_dev, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 215 | "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n", |
| 216 | down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10, |
| 217 | down_new / 10, down_new % 10, up_new / 10, up_new % 10); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 218 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 219 | atomic_set(&bat_priv->gw.bandwidth_down, down_new); |
| 220 | atomic_set(&bat_priv->gw.bandwidth_up, up_new); |
| 221 | batadv_gw_tvlv_container_update(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 222 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 223 | return count; |
| 224 | } |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * batadv_gw_tvlv_ogm_handler_v1 - process incoming gateway tvlv container |
| 228 | * @bat_priv: the bat priv with all the soft interface information |
| 229 | * @orig: the orig_node of the ogm |
| 230 | * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags) |
| 231 | * @tvlv_value: tvlv buffer containing the gateway data |
| 232 | * @tvlv_value_len: tvlv buffer length |
| 233 | */ |
| 234 | static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv, |
| 235 | struct batadv_orig_node *orig, |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 236 | u8 flags, |
| 237 | void *tvlv_value, u16 tvlv_value_len) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 238 | { |
| 239 | struct batadv_tvlv_gateway_data gateway, *gateway_ptr; |
| 240 | |
| 241 | /* only fetch the tvlv value if the handler wasn't called via the |
| 242 | * CIFNOTFND flag and if there is data to fetch |
| 243 | */ |
| 244 | if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) || |
| 245 | (tvlv_value_len < sizeof(gateway))) { |
| 246 | gateway.bandwidth_down = 0; |
| 247 | gateway.bandwidth_up = 0; |
| 248 | } else { |
| 249 | gateway_ptr = tvlv_value; |
| 250 | gateway.bandwidth_down = gateway_ptr->bandwidth_down; |
| 251 | gateway.bandwidth_up = gateway_ptr->bandwidth_up; |
| 252 | if ((gateway.bandwidth_down == 0) || |
| 253 | (gateway.bandwidth_up == 0)) { |
| 254 | gateway.bandwidth_down = 0; |
| 255 | gateway.bandwidth_up = 0; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | batadv_gw_node_update(bat_priv, orig, &gateway); |
| 260 | |
| 261 | /* restart gateway selection if fast or late switching was enabled */ |
| 262 | if ((gateway.bandwidth_down != 0) && |
| 263 | (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) && |
| 264 | (atomic_read(&bat_priv->gw_sel_class) > 2)) |
| 265 | batadv_gw_check_election(bat_priv, orig); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * batadv_gw_init - initialise the gateway handling internals |
| 270 | * @bat_priv: the bat priv with all the soft interface information |
| 271 | */ |
| 272 | void batadv_gw_init(struct batadv_priv *bat_priv) |
| 273 | { |
| 274 | batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1, |
| 275 | NULL, BATADV_TVLV_GW, 1, |
| 276 | BATADV_TVLV_HANDLER_OGM_CIFNOTFND); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * batadv_gw_free - free the gateway handling internals |
| 281 | * @bat_priv: the bat priv with all the soft interface information |
| 282 | */ |
| 283 | void batadv_gw_free(struct batadv_priv *bat_priv) |
| 284 | { |
| 285 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1); |
| 286 | batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1); |
| 287 | } |