1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2009-2019 B.A.T.M.A.N. contributors:
7 #include "gateway_common.h"
10 #include <linux/atomic.h>
11 #include <linux/byteorder/generic.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/math64.h>
15 #include <linux/netdevice.h>
16 #include <linux/stddef.h>
17 #include <linux/string.h>
18 #include <uapi/linux/batadv_packet.h>
19 #include <uapi/linux/batman_adv.h>
21 #include "gateway_client.h"
26 * batadv_parse_throughput() - parse supplied string buffer to extract
27 * throughput information
28 * @net_dev: the soft interface net device
29 * @buff: string buffer to parse
30 * @description: text shown when throughput string cannot be parsed
31 * @throughput: pointer holding the returned throughput information
33 * Return: false on parse error and true otherwise.
35 bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
36 const char *description, u32 *throughput)
38 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
43 if (strlen(buff) > 4) {
44 tmp_ptr = buff + strlen(buff) - 4;
46 if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
47 bw_unit_type = BATADV_BW_UNIT_MBIT;
49 if (strncasecmp(tmp_ptr, "kbit", 4) == 0 ||
50 bw_unit_type == BATADV_BW_UNIT_MBIT)
54 ret = kstrtou64(buff, 10, <hroughput);
57 "Invalid throughput speed for %s: %s\n",
62 switch (bw_unit_type) {
63 case BATADV_BW_UNIT_MBIT:
64 /* prevent overflow */
65 if (U64_MAX / 10 < lthroughput) {
67 "Throughput speed for %s too large: %s\n",
74 case BATADV_BW_UNIT_KBIT:
76 lthroughput = div_u64(lthroughput, 100);
80 if (lthroughput > U32_MAX) {
82 "Throughput speed for %s too large: %s\n",
87 *throughput = lthroughput;
93 * batadv_parse_gw_bandwidth() - parse supplied string buffer to extract
94 * download and upload bandwidth information
95 * @net_dev: the soft interface net device
96 * @buff: string buffer to parse
97 * @down: pointer holding the returned download bandwidth information
98 * @up: pointer holding the returned upload bandwidth information
100 * Return: false on parse error and true otherwise.
102 static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
108 slash_ptr = strchr(buff, '/');
112 ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
117 /* we also got some upload info */
119 ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
120 "upload gateway speed", up);
129 * batadv_gw_tvlv_container_update() - update the gw tvlv container after
130 * gateway setting change
131 * @bat_priv: the bat priv with all the soft interface information
133 void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
135 struct batadv_tvlv_gateway_data gw;
139 gw_mode = atomic_read(&bat_priv->gw.mode);
142 case BATADV_GW_MODE_OFF:
143 case BATADV_GW_MODE_CLIENT:
144 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
146 case BATADV_GW_MODE_SERVER:
147 down = atomic_read(&bat_priv->gw.bandwidth_down);
148 up = atomic_read(&bat_priv->gw.bandwidth_up);
149 gw.bandwidth_down = htonl(down);
150 gw.bandwidth_up = htonl(up);
151 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
158 * batadv_gw_bandwidth_set() - Parse and set download/upload gateway bandwidth
159 * from supplied string buffer
160 * @net_dev: netdev struct of the soft interface
161 * @buff: the buffer containing the user data
162 * @count: number of bytes in the buffer
164 * Return: 'count' on success or a negative error code in case of failure
166 ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
169 struct batadv_priv *bat_priv = netdev_priv(net_dev);
176 down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
177 up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
179 ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
187 up_new = down_new / 5;
192 if (down_curr == down_new && up_curr == up_new)
195 batadv_gw_reselect(bat_priv);
197 "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
198 down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
199 down_new / 10, down_new % 10, up_new / 10, up_new % 10);
201 atomic_set(&bat_priv->gw.bandwidth_down, down_new);
202 atomic_set(&bat_priv->gw.bandwidth_up, up_new);
203 batadv_gw_tvlv_container_update(bat_priv);
209 * batadv_gw_tvlv_ogm_handler_v1() - process incoming gateway tvlv container
210 * @bat_priv: the bat priv with all the soft interface information
211 * @orig: the orig_node of the ogm
212 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
213 * @tvlv_value: tvlv buffer containing the gateway data
214 * @tvlv_value_len: tvlv buffer length
216 static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
217 struct batadv_orig_node *orig,
219 void *tvlv_value, u16 tvlv_value_len)
221 struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
223 /* only fetch the tvlv value if the handler wasn't called via the
224 * CIFNOTFND flag and if there is data to fetch
226 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND ||
227 tvlv_value_len < sizeof(gateway)) {
228 gateway.bandwidth_down = 0;
229 gateway.bandwidth_up = 0;
231 gateway_ptr = tvlv_value;
232 gateway.bandwidth_down = gateway_ptr->bandwidth_down;
233 gateway.bandwidth_up = gateway_ptr->bandwidth_up;
234 if (gateway.bandwidth_down == 0 ||
235 gateway.bandwidth_up == 0) {
236 gateway.bandwidth_down = 0;
237 gateway.bandwidth_up = 0;
241 batadv_gw_node_update(bat_priv, orig, &gateway);
243 /* restart gateway selection */
244 if (gateway.bandwidth_down != 0 &&
245 atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT)
246 batadv_gw_check_election(bat_priv, orig);
250 * batadv_gw_init() - initialise the gateway handling internals
251 * @bat_priv: the bat priv with all the soft interface information
253 void batadv_gw_init(struct batadv_priv *bat_priv)
255 if (bat_priv->algo_ops->gw.init_sel_class)
256 bat_priv->algo_ops->gw.init_sel_class(bat_priv);
258 atomic_set(&bat_priv->gw.sel_class, 1);
260 batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
261 NULL, BATADV_TVLV_GW, 1,
262 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
266 * batadv_gw_free() - free the gateway handling internals
267 * @bat_priv: the bat priv with all the soft interface information
269 void batadv_gw_free(struct batadv_priv *bat_priv)
271 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
272 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);