1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) B.A.T.M.A.N. contributors: !! 2 /* Copyright (C) 2009-2019 B.A.T.M.A.N. contributors: 3 * 3 * 4 * Marek Lindner 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, see <http://www.gnu.org/licenses/>. 5 */ 17 */ 6 18 7 #include "gateway_common.h" 19 #include "gateway_common.h" 8 #include "main.h" 20 #include "main.h" 9 21 10 #include <linux/atomic.h> 22 #include <linux/atomic.h> 11 #include <linux/byteorder/generic.h> 23 #include <linux/byteorder/generic.h> >> 24 #include <linux/errno.h> >> 25 #include <linux/kernel.h> >> 26 #include <linux/math64.h> >> 27 #include <linux/netdevice.h> 12 #include <linux/stddef.h> 28 #include <linux/stddef.h> 13 #include <linux/types.h> !! 29 #include <linux/string.h> 14 #include <uapi/linux/batadv_packet.h> 30 #include <uapi/linux/batadv_packet.h> 15 #include <uapi/linux/batman_adv.h> 31 #include <uapi/linux/batman_adv.h> 16 32 17 #include "gateway_client.h" 33 #include "gateway_client.h" >> 34 #include "log.h" 18 #include "tvlv.h" 35 #include "tvlv.h" 19 36 20 /** 37 /** >> 38 * batadv_parse_throughput() - parse supplied string buffer to extract >> 39 * throughput information >> 40 * @net_dev: the soft interface net device >> 41 * @buff: string buffer to parse >> 42 * @description: text shown when throughput string cannot be parsed >> 43 * @throughput: pointer holding the returned throughput information >> 44 * >> 45 * Return: false on parse error and true otherwise. >> 46 */ >> 47 bool batadv_parse_throughput(struct net_device *net_dev, char *buff, >> 48 const char *description, u32 *throughput) >> 49 { >> 50 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT; >> 51 u64 lthroughput; >> 52 char *tmp_ptr; >> 53 int ret; >> 54 >> 55 if (strlen(buff) > 4) { >> 56 tmp_ptr = buff + strlen(buff) - 4; >> 57 >> 58 if (strncasecmp(tmp_ptr, "mbit", 4) == 0) >> 59 bw_unit_type = BATADV_BW_UNIT_MBIT; >> 60 >> 61 if (strncasecmp(tmp_ptr, "kbit", 4) == 0 || >> 62 bw_unit_type == BATADV_BW_UNIT_MBIT) >> 63 *tmp_ptr = '\0'; >> 64 } >> 65 >> 66 ret = kstrtou64(buff, 10, <hroughput); >> 67 if (ret) { >> 68 batadv_err(net_dev, >> 69 "Invalid throughput speed for %s: %s\n", >> 70 description, buff); >> 71 return false; >> 72 } >> 73 >> 74 switch (bw_unit_type) { >> 75 case BATADV_BW_UNIT_MBIT: >> 76 /* prevent overflow */ >> 77 if (U64_MAX / 10 < lthroughput) { >> 78 batadv_err(net_dev, >> 79 "Throughput speed for %s too large: %s\n", >> 80 description, buff); >> 81 return false; >> 82 } >> 83 >> 84 lthroughput *= 10; >> 85 break; >> 86 case BATADV_BW_UNIT_KBIT: >> 87 default: >> 88 lthroughput = div_u64(lthroughput, 100); >> 89 break; >> 90 } >> 91 >> 92 if (lthroughput > U32_MAX) { >> 93 batadv_err(net_dev, >> 94 "Throughput speed for %s too large: %s\n", >> 95 description, buff); >> 96 return false; >> 97 } >> 98 >> 99 *throughput = lthroughput; >> 100 >> 101 return true; >> 102 } >> 103 >> 104 /** >> 105 * batadv_parse_gw_bandwidth() - parse supplied string buffer to extract >> 106 * download and upload bandwidth information >> 107 * @net_dev: the soft interface net device >> 108 * @buff: string buffer to parse >> 109 * @down: pointer holding the returned download bandwidth information >> 110 * @up: pointer holding the returned upload bandwidth information >> 111 * >> 112 * Return: false on parse error and true otherwise. >> 113 */ >> 114 static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff, >> 115 u32 *down, u32 *up) >> 116 { >> 117 char *slash_ptr; >> 118 bool ret; >> 119 >> 120 slash_ptr = strchr(buff, '/'); >> 121 if (slash_ptr) >> 122 *slash_ptr = 0; >> 123 >> 124 ret = batadv_parse_throughput(net_dev, buff, "download gateway speed", >> 125 down); >> 126 if (!ret) >> 127 return false; >> 128 >> 129 /* we also got some upload info */ >> 130 if (slash_ptr) { >> 131 ret = batadv_parse_throughput(net_dev, slash_ptr + 1, >> 132 "upload gateway speed", up); >> 133 if (!ret) >> 134 return false; >> 135 } >> 136 >> 137 return true; >> 138 } >> 139 >> 140 /** 21 * batadv_gw_tvlv_container_update() - update 141 * batadv_gw_tvlv_container_update() - update the gw tvlv container after 22 * gateway setting change 142 * gateway setting change 23 * @bat_priv: the bat priv with all the soft i 143 * @bat_priv: the bat priv with all the soft interface information 24 */ 144 */ 25 void batadv_gw_tvlv_container_update(struct ba 145 void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv) 26 { 146 { 27 struct batadv_tvlv_gateway_data gw; 147 struct batadv_tvlv_gateway_data gw; 28 u32 down, up; 148 u32 down, up; 29 char gw_mode; 149 char gw_mode; 30 150 31 gw_mode = atomic_read(&bat_priv->gw.mo 151 gw_mode = atomic_read(&bat_priv->gw.mode); 32 152 33 switch (gw_mode) { 153 switch (gw_mode) { 34 case BATADV_GW_MODE_OFF: 154 case BATADV_GW_MODE_OFF: 35 case BATADV_GW_MODE_CLIENT: 155 case BATADV_GW_MODE_CLIENT: 36 batadv_tvlv_container_unregist 156 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1); 37 break; 157 break; 38 case BATADV_GW_MODE_SERVER: 158 case BATADV_GW_MODE_SERVER: 39 down = atomic_read(&bat_priv-> 159 down = atomic_read(&bat_priv->gw.bandwidth_down); 40 up = atomic_read(&bat_priv->gw 160 up = atomic_read(&bat_priv->gw.bandwidth_up); 41 gw.bandwidth_down = htonl(down 161 gw.bandwidth_down = htonl(down); 42 gw.bandwidth_up = htonl(up); 162 gw.bandwidth_up = htonl(up); 43 batadv_tvlv_container_register 163 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1, 44 164 &gw, sizeof(gw)); 45 break; 165 break; 46 } 166 } 47 } 167 } 48 168 49 /** 169 /** >> 170 * batadv_gw_bandwidth_set() - Parse and set download/upload gateway bandwidth >> 171 * from supplied string buffer >> 172 * @net_dev: netdev struct of the soft interface >> 173 * @buff: the buffer containing the user data >> 174 * @count: number of bytes in the buffer >> 175 * >> 176 * Return: 'count' on success or a negative error code in case of failure >> 177 */ >> 178 ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, >> 179 size_t count) >> 180 { >> 181 struct batadv_priv *bat_priv = netdev_priv(net_dev); >> 182 u32 down_curr; >> 183 u32 up_curr; >> 184 u32 down_new = 0; >> 185 u32 up_new = 0; >> 186 bool ret; >> 187 >> 188 down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down); >> 189 up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up); >> 190 >> 191 ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new); >> 192 if (!ret) >> 193 return -EINVAL; >> 194 >> 195 if (!down_new) >> 196 down_new = 1; >> 197 >> 198 if (!up_new) >> 199 up_new = down_new / 5; >> 200 >> 201 if (!up_new) >> 202 up_new = 1; >> 203 >> 204 if (down_curr == down_new && up_curr == up_new) >> 205 return count; >> 206 >> 207 batadv_gw_reselect(bat_priv); >> 208 batadv_info(net_dev, >> 209 "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n", >> 210 down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10, >> 211 down_new / 10, down_new % 10, up_new / 10, up_new % 10); >> 212 >> 213 atomic_set(&bat_priv->gw.bandwidth_down, down_new); >> 214 atomic_set(&bat_priv->gw.bandwidth_up, up_new); >> 215 batadv_gw_tvlv_container_update(bat_priv); >> 216 >> 217 return count; >> 218 } >> 219 >> 220 /** 50 * batadv_gw_tvlv_ogm_handler_v1() - process i 221 * batadv_gw_tvlv_ogm_handler_v1() - process incoming gateway tvlv container 51 * @bat_priv: the bat priv with all the soft i 222 * @bat_priv: the bat priv with all the soft interface information 52 * @orig: the orig_node of the ogm 223 * @orig: the orig_node of the ogm 53 * @flags: flags indicating the tvlv state (se 224 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags) 54 * @tvlv_value: tvlv buffer containing the gat 225 * @tvlv_value: tvlv buffer containing the gateway data 55 * @tvlv_value_len: tvlv buffer length 226 * @tvlv_value_len: tvlv buffer length 56 */ 227 */ 57 static void batadv_gw_tvlv_ogm_handler_v1(stru 228 static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv, 58 stru 229 struct batadv_orig_node *orig, 59 u8 f 230 u8 flags, 60 void 231 void *tvlv_value, u16 tvlv_value_len) 61 { 232 { 62 struct batadv_tvlv_gateway_data gatewa 233 struct batadv_tvlv_gateway_data gateway, *gateway_ptr; 63 234 64 /* only fetch the tvlv value if the ha 235 /* only fetch the tvlv value if the handler wasn't called via the 65 * CIFNOTFND flag and if there is data 236 * CIFNOTFND flag and if there is data to fetch 66 */ 237 */ 67 if (flags & BATADV_TVLV_HANDLER_OGM_CI 238 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND || 68 tvlv_value_len < sizeof(gateway)) 239 tvlv_value_len < sizeof(gateway)) { 69 gateway.bandwidth_down = 0; 240 gateway.bandwidth_down = 0; 70 gateway.bandwidth_up = 0; 241 gateway.bandwidth_up = 0; 71 } else { 242 } else { 72 gateway_ptr = tvlv_value; 243 gateway_ptr = tvlv_value; 73 gateway.bandwidth_down = gatew 244 gateway.bandwidth_down = gateway_ptr->bandwidth_down; 74 gateway.bandwidth_up = gateway 245 gateway.bandwidth_up = gateway_ptr->bandwidth_up; 75 if (gateway.bandwidth_down == 246 if (gateway.bandwidth_down == 0 || 76 gateway.bandwidth_up == 0) 247 gateway.bandwidth_up == 0) { 77 gateway.bandwidth_down 248 gateway.bandwidth_down = 0; 78 gateway.bandwidth_up = 249 gateway.bandwidth_up = 0; 79 } 250 } 80 } 251 } 81 252 82 batadv_gw_node_update(bat_priv, orig, 253 batadv_gw_node_update(bat_priv, orig, &gateway); 83 254 84 /* restart gateway selection */ 255 /* restart gateway selection */ 85 if (gateway.bandwidth_down != 0 && 256 if (gateway.bandwidth_down != 0 && 86 atomic_read(&bat_priv->gw.mode) == 257 atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT) 87 batadv_gw_check_election(bat_p 258 batadv_gw_check_election(bat_priv, orig); 88 } 259 } 89 260 90 /** 261 /** 91 * batadv_gw_init() - initialise the gateway h 262 * batadv_gw_init() - initialise the gateway handling internals 92 * @bat_priv: the bat priv with all the soft i 263 * @bat_priv: the bat priv with all the soft interface information 93 */ 264 */ 94 void batadv_gw_init(struct batadv_priv *bat_pr 265 void batadv_gw_init(struct batadv_priv *bat_priv) 95 { 266 { 96 if (bat_priv->algo_ops->gw.init_sel_cl 267 if (bat_priv->algo_ops->gw.init_sel_class) 97 bat_priv->algo_ops->gw.init_se 268 bat_priv->algo_ops->gw.init_sel_class(bat_priv); 98 else 269 else 99 atomic_set(&bat_priv->gw.sel_c 270 atomic_set(&bat_priv->gw.sel_class, 1); 100 271 101 batadv_tvlv_handler_register(bat_priv, 272 batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1, 102 NULL, NUL !! 273 NULL, BATADV_TVLV_GW, 1, 103 BATADV_TV 274 BATADV_TVLV_HANDLER_OGM_CIFNOTFND); 104 } 275 } 105 276 106 /** 277 /** 107 * batadv_gw_free() - free the gateway handlin 278 * batadv_gw_free() - free the gateway handling internals 108 * @bat_priv: the bat priv with all the soft i 279 * @bat_priv: the bat priv with all the soft interface information 109 */ 280 */ 110 void batadv_gw_free(struct batadv_priv *bat_pr 281 void batadv_gw_free(struct batadv_priv *bat_priv) 111 { 282 { 112 batadv_tvlv_container_unregister(bat_p 283 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1); 113 batadv_tvlv_handler_unregister(bat_pri 284 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1); 114 } 285 } 115 286
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.