1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2009-2019 B.A.T.M.A.N. contributors:
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.
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.
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/>.
19 #include "gateway_common.h"
22 #include <linux/atomic.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>
28 #include <linux/stddef.h>
29 #include <linux/string.h>
30 #include <uapi/linux/batadv_packet.h>
31 #include <uapi/linux/batman_adv.h>
33 #include "gateway_client.h"
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
45 * Return: false on parse error and true otherwise.
47 bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
48 const char *description, u32 *throughput)
50 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
55 if (strlen(buff) > 4) {
56 tmp_ptr = buff + strlen(buff) - 4;
58 if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
59 bw_unit_type = BATADV_BW_UNIT_MBIT;
61 if (strncasecmp(tmp_ptr, "kbit", 4) == 0 ||
62 bw_unit_type == BATADV_BW_UNIT_MBIT)
66 ret = kstrtou64(buff, 10, <hroughput);
69 "Invalid throughput speed for %s: %s\n",
74 switch (bw_unit_type) {
75 case BATADV_BW_UNIT_MBIT:
76 /* prevent overflow */
77 if (U64_MAX / 10 < lthroughput) {
79 "Throughput speed for %s too large: %s\n",
86 case BATADV_BW_UNIT_KBIT:
88 lthroughput = div_u64(lthroughput, 100);
92 if (lthroughput > U32_MAX) {
94 "Throughput speed for %s too large: %s\n",
99 *throughput = lthroughput;
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
112 * Return: false on parse error and true otherwise.
114 static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
120 slash_ptr = strchr(buff, '/');
124 ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
129 /* we also got some upload info */
131 ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
132 "upload gateway speed", up);
141 * batadv_gw_tvlv_container_update() - update the gw tvlv container after
142 * gateway setting change
143 * @bat_priv: the bat priv with all the soft interface information
145 void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
147 struct batadv_tvlv_gateway_data gw;
151 gw_mode = atomic_read(&bat_priv->gw.mode);
154 case BATADV_GW_MODE_OFF:
155 case BATADV_GW_MODE_CLIENT:
156 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
158 case BATADV_GW_MODE_SERVER:
159 down = atomic_read(&bat_priv->gw.bandwidth_down);
160 up = atomic_read(&bat_priv->gw.bandwidth_up);
161 gw.bandwidth_down = htonl(down);
162 gw.bandwidth_up = htonl(up);
163 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
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
176 * Return: 'count' on success or a negative error code in case of failure
178 ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
181 struct batadv_priv *bat_priv = netdev_priv(net_dev);
188 down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
189 up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
191 ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
199 up_new = down_new / 5;
204 if (down_curr == down_new && up_curr == up_new)
207 batadv_gw_reselect(bat_priv);
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);
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);
221 * batadv_gw_tvlv_ogm_handler_v1() - process incoming gateway tvlv container
222 * @bat_priv: the bat priv with all the soft interface information
223 * @orig: the orig_node of the ogm
224 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
225 * @tvlv_value: tvlv buffer containing the gateway data
226 * @tvlv_value_len: tvlv buffer length
228 static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
229 struct batadv_orig_node *orig,
231 void *tvlv_value, u16 tvlv_value_len)
233 struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
235 /* only fetch the tvlv value if the handler wasn't called via the
236 * CIFNOTFND flag and if there is data to fetch
238 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND ||
239 tvlv_value_len < sizeof(gateway)) {
240 gateway.bandwidth_down = 0;
241 gateway.bandwidth_up = 0;
243 gateway_ptr = tvlv_value;
244 gateway.bandwidth_down = gateway_ptr->bandwidth_down;
245 gateway.bandwidth_up = gateway_ptr->bandwidth_up;
246 if (gateway.bandwidth_down == 0 ||
247 gateway.bandwidth_up == 0) {
248 gateway.bandwidth_down = 0;
249 gateway.bandwidth_up = 0;
253 batadv_gw_node_update(bat_priv, orig, &gateway);
255 /* restart gateway selection */
256 if (gateway.bandwidth_down != 0 &&
257 atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT)
258 batadv_gw_check_election(bat_priv, orig);
262 * batadv_gw_init() - initialise the gateway handling internals
263 * @bat_priv: the bat priv with all the soft interface information
265 void batadv_gw_init(struct batadv_priv *bat_priv)
267 if (bat_priv->algo_ops->gw.init_sel_class)
268 bat_priv->algo_ops->gw.init_sel_class(bat_priv);
270 atomic_set(&bat_priv->gw.sel_class, 1);
272 batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
273 NULL, BATADV_TVLV_GW, 1,
274 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
278 * batadv_gw_free() - free the gateway handling internals
279 * @bat_priv: the bat priv with all the soft interface information
281 void batadv_gw_free(struct batadv_priv *bat_priv)
283 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
284 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);