Sven Eckelmann | ac79cbb | 2017-01-01 00:00:00 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2009-2017 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> |
| 22 | #include <linux/byteorder/generic.h> |
Sven Eckelmann | fcafa5e | 2016-05-15 11:07:42 +0200 | [diff] [blame] | 23 | #include <linux/errno.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 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 | ba41208 | 2016-05-15 23:48:31 +0200 | [diff] [blame] | 31 | #include "log.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 32 | #include "packet.h" |
Markus Pargmann | 1f8dce4 | 2016-05-15 11:07:43 +0200 | [diff] [blame] | 33 | #include "tvlv.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 34 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 35 | /** |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 36 | * batadv_parse_throughput - parse supplied string buffer to extract throughput |
| 37 | * information |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 38 | * @net_dev: the soft interface net device |
| 39 | * @buff: string buffer to parse |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 40 | * @description: text shown when throughput string cannot be parsed |
| 41 | * @throughput: pointer holding the returned throughput information |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 42 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 43 | * Return: false on parse error and true otherwise. |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 44 | */ |
Antonio Quartulli | 0b5ecc6 | 2016-01-16 16:40:14 +0800 | [diff] [blame] | 45 | bool batadv_parse_throughput(struct net_device *net_dev, char *buff, |
| 46 | const char *description, u32 *throughput) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 47 | { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 48 | enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT; |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 49 | u64 lthroughput; |
| 50 | char *tmp_ptr; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 51 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 52 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 53 | if (strlen(buff) > 4) { |
| 54 | tmp_ptr = buff + strlen(buff) - 4; |
| 55 | |
Rasmus Villemoes | b7a8d756 | 2014-10-13 15:54:42 -0700 | [diff] [blame] | 56 | if (strncasecmp(tmp_ptr, "mbit", 4) == 0) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 57 | bw_unit_type = BATADV_BW_UNIT_MBIT; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 58 | |
Rasmus Villemoes | b7a8d756 | 2014-10-13 15:54:42 -0700 | [diff] [blame] | 59 | if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) || |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 60 | (bw_unit_type == BATADV_BW_UNIT_MBIT)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 61 | *tmp_ptr = '\0'; |
| 62 | } |
| 63 | |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 64 | ret = kstrtou64(buff, 10, <hroughput); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 65 | if (ret) { |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 66 | batadv_err(net_dev, |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 67 | "Invalid throughput speed for %s: %s\n", |
| 68 | description, buff); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 72 | switch (bw_unit_type) { |
| 73 | case BATADV_BW_UNIT_MBIT: |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 74 | /* prevent overflow */ |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 75 | if (U64_MAX / 10 < lthroughput) { |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 76 | batadv_err(net_dev, |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 77 | "Throughput speed for %s too large: %s\n", |
| 78 | description, buff); |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 82 | lthroughput *= 10; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 83 | break; |
| 84 | case BATADV_BW_UNIT_KBIT: |
| 85 | default: |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 86 | lthroughput = div_u64(lthroughput, 100); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 87 | break; |
| 88 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 89 | |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 90 | if (lthroughput > U32_MAX) { |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 91 | batadv_err(net_dev, |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 92 | "Throughput speed for %s too large: %s\n", |
| 93 | description, buff); |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 97 | *throughput = lthroughput; |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download |
| 104 | * and upload bandwidth information |
| 105 | * @net_dev: the soft interface net device |
| 106 | * @buff: string buffer to parse |
| 107 | * @down: pointer holding the returned download bandwidth information |
| 108 | * @up: pointer holding the returned upload bandwidth information |
| 109 | * |
| 110 | * Return: false on parse error and true otherwise. |
| 111 | */ |
| 112 | static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff, |
| 113 | u32 *down, u32 *up) |
| 114 | { |
| 115 | char *slash_ptr; |
| 116 | bool ret; |
| 117 | |
| 118 | slash_ptr = strchr(buff, '/'); |
| 119 | if (slash_ptr) |
| 120 | *slash_ptr = 0; |
| 121 | |
| 122 | ret = batadv_parse_throughput(net_dev, buff, "download gateway speed", |
| 123 | down); |
| 124 | if (!ret) |
| 125 | return false; |
Sven Eckelmann | 0b8336f | 2015-06-21 19:40:09 +0200 | [diff] [blame] | 126 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 127 | /* we also got some upload info */ |
| 128 | if (slash_ptr) { |
Sven Eckelmann | d737ccb | 2015-09-13 09:44:45 +0200 | [diff] [blame] | 129 | ret = batadv_parse_throughput(net_dev, slash_ptr + 1, |
| 130 | "upload gateway speed", up); |
| 131 | if (!ret) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 132 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 138 | /** |
| 139 | * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway |
| 140 | * setting change |
| 141 | * @bat_priv: the bat priv with all the soft interface information |
| 142 | */ |
| 143 | void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv) |
| 144 | { |
| 145 | struct batadv_tvlv_gateway_data gw; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 146 | u32 down, up; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 147 | char gw_mode; |
| 148 | |
Antonio Quartulli | 3a24a63 | 2016-05-06 02:46:38 +0800 | [diff] [blame] | 149 | gw_mode = atomic_read(&bat_priv->gw.mode); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 150 | |
| 151 | switch (gw_mode) { |
| 152 | case BATADV_GW_MODE_OFF: |
| 153 | case BATADV_GW_MODE_CLIENT: |
| 154 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1); |
| 155 | break; |
| 156 | case BATADV_GW_MODE_SERVER: |
| 157 | down = atomic_read(&bat_priv->gw.bandwidth_down); |
| 158 | up = atomic_read(&bat_priv->gw.bandwidth_up); |
| 159 | gw.bandwidth_down = htonl(down); |
| 160 | gw.bandwidth_up = htonl(up); |
| 161 | batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1, |
| 162 | &gw, sizeof(gw)); |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
Sven Eckelmann | 84d5e5e | 2012-05-12 02:09:30 +0200 | [diff] [blame] | 167 | ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, |
| 168 | size_t count) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 169 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 170 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
Sven Eckelmann | 4f248cf | 2015-06-09 20:50:49 +0200 | [diff] [blame] | 171 | u32 down_curr; |
| 172 | u32 up_curr; |
| 173 | u32 down_new = 0; |
| 174 | u32 up_new = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 175 | bool ret; |
| 176 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 177 | down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down); |
| 178 | up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up); |
| 179 | |
| 180 | ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 181 | if (!ret) |
Sven Eckelmann | 77927b7 | 2015-06-21 14:42:49 +0200 | [diff] [blame] | 182 | return -EINVAL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 183 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 184 | if (!down_new) |
| 185 | down_new = 1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 186 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 187 | if (!up_new) |
| 188 | up_new = down_new / 5; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 189 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 190 | if (!up_new) |
| 191 | up_new = 1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 192 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 193 | if ((down_curr == down_new) && (up_curr == up_new)) |
Marek Lindner | dafe94b | 2012-05-11 16:10:50 +0800 | [diff] [blame] | 194 | return count; |
| 195 | |
Antonio Quartulli | 4e820e7 | 2013-11-04 20:59:41 +0100 | [diff] [blame] | 196 | batadv_gw_reselect(bat_priv); |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 197 | batadv_info(net_dev, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 198 | "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n", |
| 199 | down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10, |
| 200 | down_new / 10, down_new % 10, up_new / 10, up_new % 10); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 201 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 202 | atomic_set(&bat_priv->gw.bandwidth_down, down_new); |
| 203 | atomic_set(&bat_priv->gw.bandwidth_up, up_new); |
| 204 | batadv_gw_tvlv_container_update(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 205 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 206 | return count; |
| 207 | } |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * batadv_gw_tvlv_ogm_handler_v1 - process incoming gateway tvlv container |
| 211 | * @bat_priv: the bat priv with all the soft interface information |
| 212 | * @orig: the orig_node of the ogm |
| 213 | * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags) |
| 214 | * @tvlv_value: tvlv buffer containing the gateway data |
| 215 | * @tvlv_value_len: tvlv buffer length |
| 216 | */ |
| 217 | static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv, |
| 218 | struct batadv_orig_node *orig, |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 219 | u8 flags, |
| 220 | void *tvlv_value, u16 tvlv_value_len) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 221 | { |
| 222 | struct batadv_tvlv_gateway_data gateway, *gateway_ptr; |
| 223 | |
| 224 | /* only fetch the tvlv value if the handler wasn't called via the |
| 225 | * CIFNOTFND flag and if there is data to fetch |
| 226 | */ |
| 227 | if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) || |
| 228 | (tvlv_value_len < sizeof(gateway))) { |
| 229 | gateway.bandwidth_down = 0; |
| 230 | gateway.bandwidth_up = 0; |
| 231 | } else { |
| 232 | gateway_ptr = tvlv_value; |
| 233 | gateway.bandwidth_down = gateway_ptr->bandwidth_down; |
| 234 | gateway.bandwidth_up = gateway_ptr->bandwidth_up; |
| 235 | if ((gateway.bandwidth_down == 0) || |
| 236 | (gateway.bandwidth_up == 0)) { |
| 237 | gateway.bandwidth_down = 0; |
| 238 | gateway.bandwidth_up = 0; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | batadv_gw_node_update(bat_priv, orig, &gateway); |
| 243 | |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 244 | /* restart gateway selection */ |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 245 | if ((gateway.bandwidth_down != 0) && |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 246 | (atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT)) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 247 | batadv_gw_check_election(bat_priv, orig); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * batadv_gw_init - initialise the gateway handling internals |
| 252 | * @bat_priv: the bat priv with all the soft interface information |
| 253 | */ |
| 254 | void batadv_gw_init(struct batadv_priv *bat_priv) |
| 255 | { |
Sven Eckelmann | 1a9070e | 2017-03-04 15:48:50 +0100 | [diff] [blame] | 256 | if (bat_priv->algo_ops->gw.init_sel_class) |
| 257 | bat_priv->algo_ops->gw.init_sel_class(bat_priv); |
| 258 | else |
| 259 | atomic_set(&bat_priv->gw.sel_class, 1); |
| 260 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 261 | batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1, |
| 262 | NULL, BATADV_TVLV_GW, 1, |
| 263 | BATADV_TVLV_HANDLER_OGM_CIFNOTFND); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * batadv_gw_free - free the gateway handling internals |
| 268 | * @bat_priv: the bat priv with all the soft interface information |
| 269 | */ |
| 270 | void batadv_gw_free(struct batadv_priv *bat_priv) |
| 271 | { |
| 272 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1); |
| 273 | batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1); |
| 274 | } |