blob: 16cd9450ceb14aeff43801091bc64e7be40e6f0c [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann68e039f2020-01-01 00:00:01 +01002/* Copyright (C) 2009-2020 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00007#include "gateway_common.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
9
10#include <linux/atomic.h>
11#include <linux/byteorder/generic.h>
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020012#include <linux/errno.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020013#include <linux/kernel.h>
Sven Eckelmanne1928752019-05-24 16:28:50 +020014#include <linux/limits.h>
Sven Eckelmann0b8336f2015-06-21 19:40:09 +020015#include <linux/math64.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020016#include <linux/netdevice.h>
17#include <linux/stddef.h>
18#include <linux/string.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010019#include <uapi/linux/batadv_packet.h>
Sven Eckelmanne2d0d352018-11-23 13:15:00 +010020#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000022#include "gateway_client.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020023#include "log.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020024#include "tvlv.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025
Marek Lindner414254e2013-04-23 21:39:58 +080026/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010027 * batadv_parse_throughput() - parse supplied string buffer to extract
28 * throughput information
Marek Lindner414254e2013-04-23 21:39:58 +080029 * @net_dev: the soft interface net device
30 * @buff: string buffer to parse
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020031 * @description: text shown when throughput string cannot be parsed
32 * @throughput: pointer holding the returned throughput information
Marek Lindner414254e2013-04-23 21:39:58 +080033 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020034 * Return: false on parse error and true otherwise.
Marek Lindner414254e2013-04-23 21:39:58 +080035 */
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +080036bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
37 const char *description, u32 *throughput)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038{
Marek Lindner414254e2013-04-23 21:39:58 +080039 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020040 u64 lthroughput;
41 char *tmp_ptr;
Marek Lindner414254e2013-04-23 21:39:58 +080042 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044 if (strlen(buff) > 4) {
45 tmp_ptr = buff + strlen(buff) - 4;
46
Rasmus Villemoesb7a8d7562014-10-13 15:54:42 -070047 if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
Marek Lindner414254e2013-04-23 21:39:58 +080048 bw_unit_type = BATADV_BW_UNIT_MBIT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049
Sven Eckelmann825ffe12017-08-23 21:52:13 +020050 if (strncasecmp(tmp_ptr, "kbit", 4) == 0 ||
51 bw_unit_type == BATADV_BW_UNIT_MBIT)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052 *tmp_ptr = '\0';
53 }
54
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020055 ret = kstrtou64(buff, 10, &lthroughput);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056 if (ret) {
Sven Eckelmann3e348192012-05-16 20:23:22 +020057 batadv_err(net_dev,
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020058 "Invalid throughput speed for %s: %s\n",
59 description, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060 return false;
61 }
62
Marek Lindner414254e2013-04-23 21:39:58 +080063 switch (bw_unit_type) {
64 case BATADV_BW_UNIT_MBIT:
Sven Eckelmann0b8336f2015-06-21 19:40:09 +020065 /* prevent overflow */
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020066 if (U64_MAX / 10 < lthroughput) {
Sven Eckelmann0b8336f2015-06-21 19:40:09 +020067 batadv_err(net_dev,
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020068 "Throughput speed for %s too large: %s\n",
69 description, buff);
Sven Eckelmann0b8336f2015-06-21 19:40:09 +020070 return false;
71 }
72
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020073 lthroughput *= 10;
Marek Lindner414254e2013-04-23 21:39:58 +080074 break;
75 case BATADV_BW_UNIT_KBIT:
76 default:
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020077 lthroughput = div_u64(lthroughput, 100);
Marek Lindner414254e2013-04-23 21:39:58 +080078 break;
79 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020081 if (lthroughput > U32_MAX) {
Sven Eckelmann0b8336f2015-06-21 19:40:09 +020082 batadv_err(net_dev,
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020083 "Throughput speed for %s too large: %s\n",
84 description, buff);
Sven Eckelmann0b8336f2015-06-21 19:40:09 +020085 return false;
86 }
87
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020088 *throughput = lthroughput;
89
90 return true;
91}
92
93/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010094 * batadv_parse_gw_bandwidth() - parse supplied string buffer to extract
95 * download and upload bandwidth information
Sven Eckelmannd737ccb2015-09-13 09:44:45 +020096 * @net_dev: the soft interface net device
97 * @buff: string buffer to parse
98 * @down: pointer holding the returned download bandwidth information
99 * @up: pointer holding the returned upload bandwidth information
100 *
101 * Return: false on parse error and true otherwise.
102 */
103static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
104 u32 *down, u32 *up)
105{
106 char *slash_ptr;
107 bool ret;
108
109 slash_ptr = strchr(buff, '/');
110 if (slash_ptr)
111 *slash_ptr = 0;
112
113 ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
114 down);
115 if (!ret)
116 return false;
Sven Eckelmann0b8336f2015-06-21 19:40:09 +0200117
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118 /* we also got some upload info */
119 if (slash_ptr) {
Sven Eckelmannd737ccb2015-09-13 09:44:45 +0200120 ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
121 "upload gateway speed", up);
122 if (!ret)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000124 }
125
126 return true;
127}
128
Marek Lindner414254e2013-04-23 21:39:58 +0800129/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100130 * batadv_gw_tvlv_container_update() - update the gw tvlv container after
131 * gateway setting change
Marek Lindner414254e2013-04-23 21:39:58 +0800132 * @bat_priv: the bat priv with all the soft interface information
133 */
134void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
135{
136 struct batadv_tvlv_gateway_data gw;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200137 u32 down, up;
Marek Lindner414254e2013-04-23 21:39:58 +0800138 char gw_mode;
139
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800140 gw_mode = atomic_read(&bat_priv->gw.mode);
Marek Lindner414254e2013-04-23 21:39:58 +0800141
142 switch (gw_mode) {
143 case BATADV_GW_MODE_OFF:
144 case BATADV_GW_MODE_CLIENT:
145 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
146 break;
147 case BATADV_GW_MODE_SERVER:
148 down = atomic_read(&bat_priv->gw.bandwidth_down);
149 up = atomic_read(&bat_priv->gw.bandwidth_up);
150 gw.bandwidth_down = htonl(down);
151 gw.bandwidth_up = htonl(up);
152 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
153 &gw, sizeof(gw));
154 break;
155 }
156}
157
Sven Eckelmannff15c272017-12-02 19:51:53 +0100158/**
159 * batadv_gw_bandwidth_set() - Parse and set download/upload gateway bandwidth
160 * from supplied string buffer
161 * @net_dev: netdev struct of the soft interface
162 * @buff: the buffer containing the user data
163 * @count: number of bytes in the buffer
164 *
165 * Return: 'count' on success or a negative error code in case of failure
166 */
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200167ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
168 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200170 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200171 u32 down_curr;
172 u32 up_curr;
173 u32 down_new = 0;
174 u32 up_new = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175 bool ret;
176
Marek Lindner414254e2013-04-23 21:39:58 +0800177 down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
178 up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
179
180 ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181 if (!ret)
Sven Eckelmann77927b72015-06-21 14:42:49 +0200182 return -EINVAL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183
Marek Lindner414254e2013-04-23 21:39:58 +0800184 if (!down_new)
185 down_new = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186
Marek Lindner414254e2013-04-23 21:39:58 +0800187 if (!up_new)
188 up_new = down_new / 5;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189
Marek Lindner414254e2013-04-23 21:39:58 +0800190 if (!up_new)
191 up_new = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200193 if (down_curr == down_new && up_curr == up_new)
Marek Lindnerdafe94b2012-05-11 16:10:50 +0800194 return count;
195
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100196 batadv_gw_reselect(bat_priv);
Sven Eckelmann3e348192012-05-16 20:23:22 +0200197 batadv_info(net_dev,
Marek Lindner414254e2013-04-23 21:39:58 +0800198 "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
199 down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
200 down_new / 10, down_new % 10, up_new / 10, up_new % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201
Marek Lindner414254e2013-04-23 21:39:58 +0800202 atomic_set(&bat_priv->gw.bandwidth_down, down_new);
203 atomic_set(&bat_priv->gw.bandwidth_up, up_new);
204 batadv_gw_tvlv_container_update(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206 return count;
207}
Marek Lindner414254e2013-04-23 21:39:58 +0800208
209/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100210 * batadv_gw_tvlv_ogm_handler_v1() - process incoming gateway tvlv container
Marek Lindner414254e2013-04-23 21:39:58 +0800211 * @bat_priv: the bat priv with all the soft interface information
212 * @orig: the orig_node of the ogm
213 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
214 * @tvlv_value: tvlv buffer containing the gateway data
215 * @tvlv_value_len: tvlv buffer length
216 */
217static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
218 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200219 u8 flags,
220 void *tvlv_value, u16 tvlv_value_len)
Marek Lindner414254e2013-04-23 21:39:58 +0800221{
222 struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
223
224 /* only fetch the tvlv value if the handler wasn't called via the
225 * CIFNOTFND flag and if there is data to fetch
226 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200227 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND ||
228 tvlv_value_len < sizeof(gateway)) {
Marek Lindner414254e2013-04-23 21:39:58 +0800229 gateway.bandwidth_down = 0;
230 gateway.bandwidth_up = 0;
231 } else {
232 gateway_ptr = tvlv_value;
233 gateway.bandwidth_down = gateway_ptr->bandwidth_down;
234 gateway.bandwidth_up = gateway_ptr->bandwidth_up;
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200235 if (gateway.bandwidth_down == 0 ||
236 gateway.bandwidth_up == 0) {
Marek Lindner414254e2013-04-23 21:39:58 +0800237 gateway.bandwidth_down = 0;
238 gateway.bandwidth_up = 0;
239 }
240 }
241
242 batadv_gw_node_update(bat_priv, orig, &gateway);
243
Antonio Quartulli34d99cf2016-07-03 12:46:33 +0200244 /* restart gateway selection */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200245 if (gateway.bandwidth_down != 0 &&
246 atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT)
Marek Lindner414254e2013-04-23 21:39:58 +0800247 batadv_gw_check_election(bat_priv, orig);
248}
249
250/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100251 * batadv_gw_init() - initialise the gateway handling internals
Marek Lindner414254e2013-04-23 21:39:58 +0800252 * @bat_priv: the bat priv with all the soft interface information
253 */
254void batadv_gw_init(struct batadv_priv *bat_priv)
255{
Sven Eckelmann1a9070e2017-03-04 15:48:50 +0100256 if (bat_priv->algo_ops->gw.init_sel_class)
257 bat_priv->algo_ops->gw.init_sel_class(bat_priv);
258 else
259 atomic_set(&bat_priv->gw.sel_class, 1);
260
Marek Lindner414254e2013-04-23 21:39:58 +0800261 batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
262 NULL, BATADV_TVLV_GW, 1,
263 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
264}
265
266/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100267 * batadv_gw_free() - free the gateway handling internals
Marek Lindner414254e2013-04-23 21:39:58 +0800268 * @bat_priv: the bat priv with all the soft interface information
269 */
270void batadv_gw_free(struct batadv_priv *bat_priv)
271{
272 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
273 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
274}