2017-11-19 15:05:11 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2021-01-01 00:00:01 +01:00
|
|
|
/* Copyright (C) B.A.T.M.A.N. contributors:
|
2010-12-13 11:19:28 +00:00
|
|
|
*
|
|
|
|
* Marek Lindner
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gateway_common.h"
|
2015-04-17 19:40:28 +02:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include <linux/atomic.h>
|
|
|
|
#include <linux/byteorder/generic.h>
|
|
|
|
#include <linux/stddef.h>
|
2023-08-02 10:39:28 +02:00
|
|
|
#include <linux/types.h>
|
2017-12-21 10:17:41 +01:00
|
|
|
#include <uapi/linux/batadv_packet.h>
|
2018-11-23 13:15:00 +01:00
|
|
|
#include <uapi/linux/batman_adv.h>
|
2015-04-17 19:40:28 +02:00
|
|
|
|
2010-12-13 11:19:28 +00:00
|
|
|
#include "gateway_client.h"
|
2016-05-15 11:07:43 +02:00
|
|
|
#include "tvlv.h"
|
2010-12-13 11:19:28 +00:00
|
|
|
|
2013-04-23 21:39:58 +08:00
|
|
|
/**
|
2017-12-02 19:51:47 +01: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-26 18:34:26 +02: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-02 19:51:47 +01: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-26 18:34:26 +02: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-23 21:52:13 +02: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-23 21:52:13 +02: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 12:46:33 +02:00
|
|
|
/* restart gateway selection */
|
2017-08-23 21:52:13 +02: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-02 19:51:47 +01: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 15:48:50 +01: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,
|
2022-12-27 20:34:06 +01:00
|
|
|
NULL, NULL, BATADV_TVLV_GW, 1,
|
2013-04-23 21:39:58 +08:00
|
|
|
BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-02 19:51:47 +01: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);
|
|
|
|
}
|