2017-11-19 22:05:11 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2020-01-01 07:00:01 +08:00
|
|
|
/* Copyright (C) 2009-2020 B.A.T.M.A.N. contributors:
|
2010-12-13 19:19:28 +08:00
|
|
|
*
|
|
|
|
* Marek Lindner
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gateway_common.h"
|
2015-04-18 01:40:28 +08:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include <linux/atomic.h>
|
|
|
|
#include <linux/byteorder/generic.h>
|
2016-05-15 17:07:42 +08:00
|
|
|
#include <linux/errno.h>
|
2015-04-18 01:40:28 +08:00
|
|
|
#include <linux/kernel.h>
|
2019-05-24 22:28:50 +08:00
|
|
|
#include <linux/limits.h>
|
2015-06-22 01:40:09 +08:00
|
|
|
#include <linux/math64.h>
|
2015-04-18 01:40:28 +08:00
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/string.h>
|
2017-12-21 17:17:41 +08:00
|
|
|
#include <uapi/linux/batadv_packet.h>
|
2018-11-23 20:15:00 +08:00
|
|
|
#include <uapi/linux/batman_adv.h>
|
2015-04-18 01:40:28 +08:00
|
|
|
|
2010-12-13 19:19:28 +08:00
|
|
|
#include "gateway_client.h"
|
2016-05-16 05:48:31 +08:00
|
|
|
#include "log.h"
|
2016-05-15 17:07:43 +08:00
|
|
|
#include "tvlv.h"
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
/**
|
2017-12-03 02:51:47 +08:00
|
|
|
* batadv_parse_throughput() - parse supplied string buffer to extract
|
|
|
|
* throughput information
|
2013-04-23 21:39:58 +08:00
|
|
|
* @net_dev: the soft interface net device
|
|
|
|
* @buff: string buffer to parse
|
2015-09-13 15:44:45 +08:00
|
|
|
* @description: text shown when throughput string cannot be parsed
|
|
|
|
* @throughput: pointer holding the returned throughput information
|
2013-04-23 21:39:58 +08:00
|
|
|
*
|
2015-09-16 01:00:48 +08:00
|
|
|
* Return: false on parse error and true otherwise.
|
2013-04-23 21:39:58 +08:00
|
|
|
*/
|
2016-01-16 16:40:14 +08:00
|
|
|
bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
|
|
|
|
const char *description, u32 *throughput)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
2013-04-23 21:39:58 +08:00
|
|
|
enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
|
2015-09-13 15:44:45 +08:00
|
|
|
u64 lthroughput;
|
|
|
|
char *tmp_ptr;
|
2013-04-23 21:39:58 +08:00
|
|
|
int ret;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
if (strlen(buff) > 4) {
|
|
|
|
tmp_ptr = buff + strlen(buff) - 4;
|
|
|
|
|
2014-10-14 06:54:42 +08:00
|
|
|
if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
|
2013-04-23 21:39:58 +08:00
|
|
|
bw_unit_type = BATADV_BW_UNIT_MBIT;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2017-08-24 03:52:13 +08:00
|
|
|
if (strncasecmp(tmp_ptr, "kbit", 4) == 0 ||
|
|
|
|
bw_unit_type == BATADV_BW_UNIT_MBIT)
|
2010-12-13 19:19:28 +08:00
|
|
|
*tmp_ptr = '\0';
|
|
|
|
}
|
|
|
|
|
2015-09-13 15:44:45 +08:00
|
|
|
ret = kstrtou64(buff, 10, <hroughput);
|
2010-12-13 19:19:28 +08:00
|
|
|
if (ret) {
|
2012-05-17 02:23:22 +08:00
|
|
|
batadv_err(net_dev,
|
2015-09-13 15:44:45 +08:00
|
|
|
"Invalid throughput speed for %s: %s\n",
|
|
|
|
description, buff);
|
2010-12-13 19:19:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
switch (bw_unit_type) {
|
|
|
|
case BATADV_BW_UNIT_MBIT:
|
2015-06-22 01:40:09 +08:00
|
|
|
/* prevent overflow */
|
2015-09-13 15:44:45 +08:00
|
|
|
if (U64_MAX / 10 < lthroughput) {
|
2015-06-22 01:40:09 +08:00
|
|
|
batadv_err(net_dev,
|
2015-09-13 15:44:45 +08:00
|
|
|
"Throughput speed for %s too large: %s\n",
|
|
|
|
description, buff);
|
2015-06-22 01:40:09 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-13 15:44:45 +08:00
|
|
|
lthroughput *= 10;
|
2013-04-23 21:39:58 +08:00
|
|
|
break;
|
|
|
|
case BATADV_BW_UNIT_KBIT:
|
|
|
|
default:
|
2015-09-13 15:44:45 +08:00
|
|
|
lthroughput = div_u64(lthroughput, 100);
|
2013-04-23 21:39:58 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2015-09-13 15:44:45 +08:00
|
|
|
if (lthroughput > U32_MAX) {
|
2015-06-22 01:40:09 +08:00
|
|
|
batadv_err(net_dev,
|
2015-09-13 15:44:45 +08:00
|
|
|
"Throughput speed for %s too large: %s\n",
|
|
|
|
description, buff);
|
2015-06-22 01:40:09 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-13 15:44:45 +08:00
|
|
|
*throughput = lthroughput;
|
2015-06-22 01:40:09 +08:00
|
|
|
|
2015-09-13 15:44:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2015-09-13 15:44:45 +08:00
|
|
|
/**
|
2017-12-03 02:51:47 +08:00
|
|
|
* batadv_parse_gw_bandwidth() - parse supplied string buffer to extract
|
|
|
|
* download and upload bandwidth information
|
2015-09-13 15:44:45 +08:00
|
|
|
* @net_dev: the soft interface net device
|
|
|
|
* @buff: string buffer to parse
|
|
|
|
* @down: pointer holding the returned download bandwidth information
|
|
|
|
* @up: pointer holding the returned upload bandwidth information
|
|
|
|
*
|
|
|
|
* Return: false on parse error and true otherwise.
|
|
|
|
*/
|
|
|
|
static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
|
|
|
|
u32 *down, u32 *up)
|
|
|
|
{
|
|
|
|
char *slash_ptr;
|
|
|
|
bool ret;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2015-09-13 15:44:45 +08:00
|
|
|
slash_ptr = strchr(buff, '/');
|
|
|
|
if (slash_ptr)
|
|
|
|
*slash_ptr = 0;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2015-09-13 15:44:45 +08:00
|
|
|
ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
|
|
|
|
down);
|
|
|
|
if (!ret)
|
|
|
|
return false;
|
2015-06-22 01:40:09 +08:00
|
|
|
|
2015-09-13 15:44:45 +08:00
|
|
|
/* we also got some upload info */
|
|
|
|
if (slash_ptr) {
|
|
|
|
ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
|
|
|
|
"upload gateway speed", up);
|
|
|
|
if (!ret)
|
2015-06-22 01:40:09 +08:00
|
|
|
return false;
|
2010-12-13 19:19:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
/**
|
2017-12-03 02:51:47 +08:00
|
|
|
* batadv_gw_tvlv_container_update() - update the gw tvlv container after
|
|
|
|
* gateway setting change
|
2013-04-23 21:39:58 +08:00
|
|
|
* @bat_priv: the bat priv with all the soft interface information
|
|
|
|
*/
|
|
|
|
void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
|
|
|
|
{
|
|
|
|
struct batadv_tvlv_gateway_data gw;
|
2015-05-27 00:34:26 +08:00
|
|
|
u32 down, up;
|
2013-04-23 21:39:58 +08:00
|
|
|
char gw_mode;
|
|
|
|
|
2016-05-06 02:46:38 +08:00
|
|
|
gw_mode = atomic_read(&bat_priv->gw.mode);
|
2013-04-23 21:39:58 +08:00
|
|
|
|
|
|
|
switch (gw_mode) {
|
|
|
|
case BATADV_GW_MODE_OFF:
|
|
|
|
case BATADV_GW_MODE_CLIENT:
|
|
|
|
batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
|
|
|
|
break;
|
|
|
|
case BATADV_GW_MODE_SERVER:
|
|
|
|
down = atomic_read(&bat_priv->gw.bandwidth_down);
|
|
|
|
up = atomic_read(&bat_priv->gw.bandwidth_up);
|
|
|
|
gw.bandwidth_down = htonl(down);
|
|
|
|
gw.bandwidth_up = htonl(up);
|
|
|
|
batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
|
|
|
|
&gw, sizeof(gw));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 02:51:53 +08:00
|
|
|
/**
|
|
|
|
* batadv_gw_bandwidth_set() - Parse and set download/upload gateway bandwidth
|
|
|
|
* from supplied string buffer
|
|
|
|
* @net_dev: netdev struct of the soft interface
|
|
|
|
* @buff: the buffer containing the user data
|
|
|
|
* @count: number of bytes in the buffer
|
|
|
|
*
|
|
|
|
* Return: 'count' on success or a negative error code in case of failure
|
|
|
|
*/
|
2012-05-12 08:09:30 +08:00
|
|
|
ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
|
|
|
|
size_t count)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_priv *bat_priv = netdev_priv(net_dev);
|
2015-06-10 02:50:49 +08:00
|
|
|
u32 down_curr;
|
|
|
|
u32 up_curr;
|
|
|
|
u32 down_new = 0;
|
|
|
|
u32 up_new = 0;
|
2010-12-13 19:19:28 +08:00
|
|
|
bool ret;
|
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
|
|
|
|
up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
|
|
|
|
|
|
|
|
ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
|
2010-12-13 19:19:28 +08:00
|
|
|
if (!ret)
|
2015-06-21 20:42:49 +08:00
|
|
|
return -EINVAL;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
if (!down_new)
|
|
|
|
down_new = 1;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
if (!up_new)
|
|
|
|
up_new = down_new / 5;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
if (!up_new)
|
|
|
|
up_new = 1;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2017-08-24 03:52:13 +08:00
|
|
|
if (down_curr == down_new && up_curr == up_new)
|
2012-05-11 16:10:50 +08:00
|
|
|
return count;
|
|
|
|
|
2013-11-05 03:59:41 +08:00
|
|
|
batadv_gw_reselect(bat_priv);
|
2012-05-17 02:23:22 +08:00
|
|
|
batadv_info(net_dev,
|
2013-04-23 21:39:58 +08:00
|
|
|
"Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
|
|
|
|
down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
|
|
|
|
down_new / 10, down_new % 10, up_new / 10, up_new % 10);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
atomic_set(&bat_priv->gw.bandwidth_down, down_new);
|
|
|
|
atomic_set(&bat_priv->gw.bandwidth_up, up_new);
|
|
|
|
batadv_gw_tvlv_container_update(bat_priv);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2013-04-23 21:39:58 +08:00
|
|
|
|
|
|
|
/**
|
2017-12-03 02:51:47 +08:00
|
|
|
* batadv_gw_tvlv_ogm_handler_v1() - process incoming gateway tvlv container
|
2013-04-23 21:39:58 +08:00
|
|
|
* @bat_priv: the bat priv with all the soft interface information
|
|
|
|
* @orig: the orig_node of the ogm
|
|
|
|
* @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
|
|
|
|
* @tvlv_value: tvlv buffer containing the gateway data
|
|
|
|
* @tvlv_value_len: tvlv buffer length
|
|
|
|
*/
|
|
|
|
static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
|
|
|
|
struct batadv_orig_node *orig,
|
2015-05-27 00:34:26 +08:00
|
|
|
u8 flags,
|
|
|
|
void *tvlv_value, u16 tvlv_value_len)
|
2013-04-23 21:39:58 +08:00
|
|
|
{
|
|
|
|
struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
|
|
|
|
|
|
|
|
/* only fetch the tvlv value if the handler wasn't called via the
|
|
|
|
* CIFNOTFND flag and if there is data to fetch
|
|
|
|
*/
|
2017-08-24 03:52:13 +08:00
|
|
|
if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND ||
|
|
|
|
tvlv_value_len < sizeof(gateway)) {
|
2013-04-23 21:39:58 +08:00
|
|
|
gateway.bandwidth_down = 0;
|
|
|
|
gateway.bandwidth_up = 0;
|
|
|
|
} else {
|
|
|
|
gateway_ptr = tvlv_value;
|
|
|
|
gateway.bandwidth_down = gateway_ptr->bandwidth_down;
|
|
|
|
gateway.bandwidth_up = gateway_ptr->bandwidth_up;
|
2017-08-24 03:52:13 +08:00
|
|
|
if (gateway.bandwidth_down == 0 ||
|
|
|
|
gateway.bandwidth_up == 0) {
|
2013-04-23 21:39:58 +08:00
|
|
|
gateway.bandwidth_down = 0;
|
|
|
|
gateway.bandwidth_up = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
batadv_gw_node_update(bat_priv, orig, &gateway);
|
|
|
|
|
2016-07-03 18:46:33 +08:00
|
|
|
/* restart gateway selection */
|
2017-08-24 03:52:13 +08:00
|
|
|
if (gateway.bandwidth_down != 0 &&
|
|
|
|
atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT)
|
2013-04-23 21:39:58 +08:00
|
|
|
batadv_gw_check_election(bat_priv, orig);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-03 02:51:47 +08:00
|
|
|
* batadv_gw_init() - initialise the gateway handling internals
|
2013-04-23 21:39:58 +08:00
|
|
|
* @bat_priv: the bat priv with all the soft interface information
|
|
|
|
*/
|
|
|
|
void batadv_gw_init(struct batadv_priv *bat_priv)
|
|
|
|
{
|
2017-03-04 22:48:50 +08:00
|
|
|
if (bat_priv->algo_ops->gw.init_sel_class)
|
|
|
|
bat_priv->algo_ops->gw.init_sel_class(bat_priv);
|
|
|
|
else
|
|
|
|
atomic_set(&bat_priv->gw.sel_class, 1);
|
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
|
|
|
|
NULL, BATADV_TVLV_GW, 1,
|
|
|
|
BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-03 02:51:47 +08:00
|
|
|
* batadv_gw_free() - free the gateway handling internals
|
2013-04-23 21:39:58 +08:00
|
|
|
* @bat_priv: the bat priv with all the soft interface information
|
|
|
|
*/
|
|
|
|
void batadv_gw_free(struct batadv_priv *bat_priv)
|
|
|
|
{
|
|
|
|
batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
|
|
|
|
batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
|
|
|
|
}
|