Blame view
net/batman-adv/gateway_common.c
7.25 KB
7db7d9f36
|
1 |
// SPDX-License-Identifier: GPL-2.0 |
7a79d717e
|
2 |
/* Copyright (C) 2009-2019 B.A.T.M.A.N. contributors: |
c6c8fea29
|
3 4 |
* * Marek Lindner |
c6c8fea29
|
5 |
*/ |
c6c8fea29
|
6 |
#include "gateway_common.h" |
1e2c2a4fe
|
7 8 9 10 |
#include "main.h" #include <linux/atomic.h> #include <linux/byteorder/generic.h> |
fcafa5e74
|
11 |
#include <linux/errno.h> |
1e2c2a4fe
|
12 |
#include <linux/kernel.h> |
e19287529
|
13 |
#include <linux/limits.h> |
0b8336f5f
|
14 |
#include <linux/math64.h> |
1e2c2a4fe
|
15 16 17 |
#include <linux/netdevice.h> #include <linux/stddef.h> #include <linux/string.h> |
fec149f5d
|
18 |
#include <uapi/linux/batadv_packet.h> |
e2d0d35b5
|
19 |
#include <uapi/linux/batman_adv.h> |
1e2c2a4fe
|
20 |
|
c6c8fea29
|
21 |
#include "gateway_client.h" |
ba412080f
|
22 |
#include "log.h" |
1f8dce499
|
23 |
#include "tvlv.h" |
c6c8fea29
|
24 |
|
414254e34
|
25 |
/** |
7e9a8c2ce
|
26 27 |
* batadv_parse_throughput() - parse supplied string buffer to extract * throughput information |
414254e34
|
28 29 |
* @net_dev: the soft interface net device * @buff: string buffer to parse |
d737ccbed
|
30 31 |
* @description: text shown when throughput string cannot be parsed * @throughput: pointer holding the returned throughput information |
414254e34
|
32 |
* |
62fe710f6
|
33 |
* Return: false on parse error and true otherwise. |
414254e34
|
34 |
*/ |
0b5ecc681
|
35 36 |
bool batadv_parse_throughput(struct net_device *net_dev, char *buff, const char *description, u32 *throughput) |
c6c8fea29
|
37 |
{ |
414254e34
|
38 |
enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT; |
d737ccbed
|
39 40 |
u64 lthroughput; char *tmp_ptr; |
414254e34
|
41 |
int ret; |
c6c8fea29
|
42 |
|
c6c8fea29
|
43 44 |
if (strlen(buff) > 4) { tmp_ptr = buff + strlen(buff) - 4; |
b7a8d756f
|
45 |
if (strncasecmp(tmp_ptr, "mbit", 4) == 0) |
414254e34
|
46 |
bw_unit_type = BATADV_BW_UNIT_MBIT; |
c6c8fea29
|
47 |
|
825ffe1f7
|
48 49 |
if (strncasecmp(tmp_ptr, "kbit", 4) == 0 || bw_unit_type == BATADV_BW_UNIT_MBIT) |
c6c8fea29
|
50 51 |
*tmp_ptr = '\0'; } |
d737ccbed
|
52 |
ret = kstrtou64(buff, 10, <hroughput); |
c6c8fea29
|
53 |
if (ret) { |
3e34819e0
|
54 |
batadv_err(net_dev, |
d737ccbed
|
55 56 57 |
"Invalid throughput speed for %s: %s ", description, buff); |
c6c8fea29
|
58 59 |
return false; } |
414254e34
|
60 61 |
switch (bw_unit_type) { case BATADV_BW_UNIT_MBIT: |
0b8336f5f
|
62 |
/* prevent overflow */ |
d737ccbed
|
63 |
if (U64_MAX / 10 < lthroughput) { |
0b8336f5f
|
64 |
batadv_err(net_dev, |
d737ccbed
|
65 66 67 |
"Throughput speed for %s too large: %s ", description, buff); |
0b8336f5f
|
68 69 |
return false; } |
d737ccbed
|
70 |
lthroughput *= 10; |
414254e34
|
71 72 73 |
break; case BATADV_BW_UNIT_KBIT: default: |
d737ccbed
|
74 |
lthroughput = div_u64(lthroughput, 100); |
414254e34
|
75 76 |
break; } |
c6c8fea29
|
77 |
|
d737ccbed
|
78 |
if (lthroughput > U32_MAX) { |
0b8336f5f
|
79 |
batadv_err(net_dev, |
d737ccbed
|
80 81 82 |
"Throughput speed for %s too large: %s ", description, buff); |
0b8336f5f
|
83 84 |
return false; } |
d737ccbed
|
85 |
*throughput = lthroughput; |
0b8336f5f
|
86 |
|
d737ccbed
|
87 88 |
return true; } |
c6c8fea29
|
89 |
|
d737ccbed
|
90 |
/** |
7e9a8c2ce
|
91 92 |
* batadv_parse_gw_bandwidth() - parse supplied string buffer to extract * download and upload bandwidth information |
d737ccbed
|
93 94 95 96 97 98 99 100 101 102 103 104 |
* @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; |
c6c8fea29
|
105 |
|
d737ccbed
|
106 107 108 |
slash_ptr = strchr(buff, '/'); if (slash_ptr) *slash_ptr = 0; |
c6c8fea29
|
109 |
|
d737ccbed
|
110 111 112 113 |
ret = batadv_parse_throughput(net_dev, buff, "download gateway speed", down); if (!ret) return false; |
0b8336f5f
|
114 |
|
d737ccbed
|
115 116 117 118 119 |
/* we also got some upload info */ if (slash_ptr) { ret = batadv_parse_throughput(net_dev, slash_ptr + 1, "upload gateway speed", up); if (!ret) |
0b8336f5f
|
120 |
return false; |
c6c8fea29
|
121 122 123 124 |
} return true; } |
414254e34
|
125 |
/** |
7e9a8c2ce
|
126 127 |
* batadv_gw_tvlv_container_update() - update the gw tvlv container after * gateway setting change |
414254e34
|
128 129 130 131 132 |
* @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; |
6b5e971a2
|
133 |
u32 down, up; |
414254e34
|
134 |
char gw_mode; |
3a24a63e7
|
135 |
gw_mode = atomic_read(&bat_priv->gw.mode); |
414254e34
|
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
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; } } |
ff15c27c9
|
152 153 154 155 156 157 158 159 160 |
/** * 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 */ |
84d5e5e00
|
161 162 |
ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, size_t count) |
c6c8fea29
|
163 |
{ |
56303d34a
|
164 |
struct batadv_priv *bat_priv = netdev_priv(net_dev); |
4f248cff9
|
165 166 167 168 |
u32 down_curr; u32 up_curr; u32 down_new = 0; u32 up_new = 0; |
c6c8fea29
|
169 |
bool ret; |
414254e34
|
170 171 172 173 |
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); |
c6c8fea29
|
174 |
if (!ret) |
77927b7d9
|
175 |
return -EINVAL; |
c6c8fea29
|
176 |
|
414254e34
|
177 178 |
if (!down_new) down_new = 1; |
c6c8fea29
|
179 |
|
414254e34
|
180 181 |
if (!up_new) up_new = down_new / 5; |
c6c8fea29
|
182 |
|
414254e34
|
183 184 |
if (!up_new) up_new = 1; |
c6c8fea29
|
185 |
|
825ffe1f7
|
186 |
if (down_curr == down_new && up_curr == up_new) |
dafe94b27
|
187 |
return count; |
4e820e72d
|
188 |
batadv_gw_reselect(bat_priv); |
3e34819e0
|
189 |
batadv_info(net_dev, |
414254e34
|
190 191 192 193 |
"Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit' ", down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10, down_new / 10, down_new % 10, up_new / 10, up_new % 10); |
c6c8fea29
|
194 |
|
414254e34
|
195 196 197 |
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); |
c6c8fea29
|
198 |
|
c6c8fea29
|
199 200 |
return count; } |
414254e34
|
201 202 |
/** |
7e9a8c2ce
|
203 |
* batadv_gw_tvlv_ogm_handler_v1() - process incoming gateway tvlv container |
414254e34
|
204 205 206 207 208 209 210 211 |
* @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, |
6b5e971a2
|
212 213 |
u8 flags, void *tvlv_value, u16 tvlv_value_len) |
414254e34
|
214 215 216 217 218 219 |
{ 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 */ |
825ffe1f7
|
220 221 |
if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND || tvlv_value_len < sizeof(gateway)) { |
414254e34
|
222 223 224 225 226 227 |
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; |
825ffe1f7
|
228 229 |
if (gateway.bandwidth_down == 0 || gateway.bandwidth_up == 0) { |
414254e34
|
230 231 232 233 234 235 |
gateway.bandwidth_down = 0; gateway.bandwidth_up = 0; } } batadv_gw_node_update(bat_priv, orig, &gateway); |
34d99cfef
|
236 |
/* restart gateway selection */ |
825ffe1f7
|
237 238 |
if (gateway.bandwidth_down != 0 && atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT) |
414254e34
|
239 240 241 242 |
batadv_gw_check_election(bat_priv, orig); } /** |
7e9a8c2ce
|
243 |
* batadv_gw_init() - initialise the gateway handling internals |
414254e34
|
244 245 246 247 |
* @bat_priv: the bat priv with all the soft interface information */ void batadv_gw_init(struct batadv_priv *bat_priv) { |
1a9070ec9
|
248 249 250 251 |
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); |
414254e34
|
252 253 254 255 256 257 |
batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1, NULL, BATADV_TVLV_GW, 1, BATADV_TVLV_HANDLER_OGM_CIFNOTFND); } /** |
7e9a8c2ce
|
258 |
* batadv_gw_free() - free the gateway handling internals |
414254e34
|
259 260 261 262 263 264 265 |
* @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); } |