Commit | Line | Data |
---|---|---|
7db7d9f3 | 1 | // SPDX-License-Identifier: GPL-2.0 |
68e039f9 | 2 | /* Copyright (C) 2009-2020 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
3 | * |
4 | * Marek Lindner | |
c6c8fea2 SE |
5 | */ |
6 | ||
c6c8fea2 | 7 | #include "gateway_common.h" |
1e2c2a4f SE |
8 | #include "main.h" |
9 | ||
10 | #include <linux/atomic.h> | |
11 | #include <linux/byteorder/generic.h> | |
fcafa5e7 | 12 | #include <linux/errno.h> |
1e2c2a4f | 13 | #include <linux/kernel.h> |
e1928752 | 14 | #include <linux/limits.h> |
0b8336f5 | 15 | #include <linux/math64.h> |
1e2c2a4f SE |
16 | #include <linux/netdevice.h> |
17 | #include <linux/stddef.h> | |
18 | #include <linux/string.h> | |
fec149f5 | 19 | #include <uapi/linux/batadv_packet.h> |
e2d0d35b | 20 | #include <uapi/linux/batman_adv.h> |
1e2c2a4f | 21 | |
c6c8fea2 | 22 | #include "gateway_client.h" |
ba412080 | 23 | #include "log.h" |
1f8dce49 | 24 | #include "tvlv.h" |
c6c8fea2 | 25 | |
414254e3 | 26 | /** |
7e9a8c2c SE |
27 | * batadv_parse_throughput() - parse supplied string buffer to extract |
28 | * throughput information | |
414254e3 ML |
29 | * @net_dev: the soft interface net device |
30 | * @buff: string buffer to parse | |
d737ccbe SE |
31 | * @description: text shown when throughput string cannot be parsed |
32 | * @throughput: pointer holding the returned throughput information | |
414254e3 | 33 | * |
62fe710f | 34 | * Return: false on parse error and true otherwise. |
414254e3 | 35 | */ |
0b5ecc68 AQ |
36 | bool batadv_parse_throughput(struct net_device *net_dev, char *buff, |
37 | const char *description, u32 *throughput) | |
c6c8fea2 | 38 | { |
414254e3 | 39 | enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT; |
d737ccbe SE |
40 | u64 lthroughput; |
41 | char *tmp_ptr; | |
414254e3 | 42 | int ret; |
c6c8fea2 | 43 | |
c6c8fea2 SE |
44 | if (strlen(buff) > 4) { |
45 | tmp_ptr = buff + strlen(buff) - 4; | |
46 | ||
b7a8d756 | 47 | if (strncasecmp(tmp_ptr, "mbit", 4) == 0) |
414254e3 | 48 | bw_unit_type = BATADV_BW_UNIT_MBIT; |
c6c8fea2 | 49 | |
825ffe1f SE |
50 | if (strncasecmp(tmp_ptr, "kbit", 4) == 0 || |
51 | bw_unit_type == BATADV_BW_UNIT_MBIT) | |
c6c8fea2 SE |
52 | *tmp_ptr = '\0'; |
53 | } | |
54 | ||
d737ccbe | 55 | ret = kstrtou64(buff, 10, <hroughput); |
c6c8fea2 | 56 | if (ret) { |
3e34819e | 57 | batadv_err(net_dev, |
d737ccbe SE |
58 | "Invalid throughput speed for %s: %s\n", |
59 | description, buff); | |
c6c8fea2 SE |
60 | return false; |
61 | } | |
62 | ||
414254e3 ML |
63 | switch (bw_unit_type) { |
64 | case BATADV_BW_UNIT_MBIT: | |
0b8336f5 | 65 | /* prevent overflow */ |
d737ccbe | 66 | if (U64_MAX / 10 < lthroughput) { |
0b8336f5 | 67 | batadv_err(net_dev, |
d737ccbe SE |
68 | "Throughput speed for %s too large: %s\n", |
69 | description, buff); | |
0b8336f5 SE |
70 | return false; |
71 | } | |
72 | ||
d737ccbe | 73 | lthroughput *= 10; |
414254e3 ML |
74 | break; |
75 | case BATADV_BW_UNIT_KBIT: | |
76 | default: | |
d737ccbe | 77 | lthroughput = div_u64(lthroughput, 100); |
414254e3 ML |
78 | break; |
79 | } | |
c6c8fea2 | 80 | |
d737ccbe | 81 | if (lthroughput > U32_MAX) { |
0b8336f5 | 82 | batadv_err(net_dev, |
d737ccbe SE |
83 | "Throughput speed for %s too large: %s\n", |
84 | description, buff); | |
0b8336f5 SE |
85 | return false; |
86 | } | |
87 | ||
d737ccbe | 88 | *throughput = lthroughput; |
0b8336f5 | 89 | |
d737ccbe SE |
90 | return true; |
91 | } | |
c6c8fea2 | 92 | |
d737ccbe | 93 | /** |
7e9a8c2c SE |
94 | * batadv_parse_gw_bandwidth() - parse supplied string buffer to extract |
95 | * download and upload bandwidth information | |
d737ccbe SE |
96 | * @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 | */ | |
103 | static 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; | |
c6c8fea2 | 108 | |
d737ccbe SE |
109 | slash_ptr = strchr(buff, '/'); |
110 | if (slash_ptr) | |
111 | *slash_ptr = 0; | |
c6c8fea2 | 112 | |
d737ccbe SE |
113 | ret = batadv_parse_throughput(net_dev, buff, "download gateway speed", |
114 | down); | |
115 | if (!ret) | |
116 | return false; | |
0b8336f5 | 117 | |
d737ccbe SE |
118 | /* we also got some upload info */ |
119 | if (slash_ptr) { | |
120 | ret = batadv_parse_throughput(net_dev, slash_ptr + 1, | |
121 | "upload gateway speed", up); | |
122 | if (!ret) | |
0b8336f5 | 123 | return false; |
c6c8fea2 SE |
124 | } |
125 | ||
126 | return true; | |
127 | } | |
128 | ||
414254e3 | 129 | /** |
7e9a8c2c SE |
130 | * batadv_gw_tvlv_container_update() - update the gw tvlv container after |
131 | * gateway setting change | |
414254e3 ML |
132 | * @bat_priv: the bat priv with all the soft interface information |
133 | */ | |
134 | void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv) | |
135 | { | |
136 | struct batadv_tvlv_gateway_data gw; | |
6b5e971a | 137 | u32 down, up; |
414254e3 ML |
138 | char gw_mode; |
139 | ||
3a24a63e | 140 | gw_mode = atomic_read(&bat_priv->gw.mode); |
414254e3 ML |
141 | |
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 | ||
ff15c27c SE |
158 | /** |
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 | */ | |
84d5e5e0 SE |
167 | ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, |
168 | size_t count) | |
c6c8fea2 | 169 | { |
56303d34 | 170 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
4f248cff SE |
171 | u32 down_curr; |
172 | u32 up_curr; | |
173 | u32 down_new = 0; | |
174 | u32 up_new = 0; | |
c6c8fea2 SE |
175 | bool ret; |
176 | ||
414254e3 ML |
177 | 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); | |
c6c8fea2 | 181 | if (!ret) |
77927b7d | 182 | return -EINVAL; |
c6c8fea2 | 183 | |
414254e3 ML |
184 | if (!down_new) |
185 | down_new = 1; | |
c6c8fea2 | 186 | |
414254e3 ML |
187 | if (!up_new) |
188 | up_new = down_new / 5; | |
c6c8fea2 | 189 | |
414254e3 ML |
190 | if (!up_new) |
191 | up_new = 1; | |
c6c8fea2 | 192 | |
825ffe1f | 193 | if (down_curr == down_new && up_curr == up_new) |
dafe94b2 ML |
194 | return count; |
195 | ||
4e820e72 | 196 | batadv_gw_reselect(bat_priv); |
3e34819e | 197 | batadv_info(net_dev, |
414254e3 ML |
198 | "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); | |
c6c8fea2 | 201 | |
414254e3 ML |
202 | 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); | |
c6c8fea2 | 205 | |
c6c8fea2 SE |
206 | return count; |
207 | } | |
414254e3 ML |
208 | |
209 | /** | |
7e9a8c2c | 210 | * batadv_gw_tvlv_ogm_handler_v1() - process incoming gateway tvlv container |
414254e3 ML |
211 | * @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 | */ | |
217 | static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv, | |
218 | struct batadv_orig_node *orig, | |
6b5e971a SE |
219 | u8 flags, |
220 | void *tvlv_value, u16 tvlv_value_len) | |
414254e3 ML |
221 | { |
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 | */ | |
825ffe1f SE |
227 | if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND || |
228 | tvlv_value_len < sizeof(gateway)) { | |
414254e3 ML |
229 | 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; | |
825ffe1f SE |
235 | if (gateway.bandwidth_down == 0 || |
236 | gateway.bandwidth_up == 0) { | |
414254e3 ML |
237 | gateway.bandwidth_down = 0; |
238 | gateway.bandwidth_up = 0; | |
239 | } | |
240 | } | |
241 | ||
242 | batadv_gw_node_update(bat_priv, orig, &gateway); | |
243 | ||
34d99cfe | 244 | /* restart gateway selection */ |
825ffe1f SE |
245 | if (gateway.bandwidth_down != 0 && |
246 | atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT) | |
414254e3 ML |
247 | batadv_gw_check_election(bat_priv, orig); |
248 | } | |
249 | ||
250 | /** | |
7e9a8c2c | 251 | * batadv_gw_init() - initialise the gateway handling internals |
414254e3 ML |
252 | * @bat_priv: the bat priv with all the soft interface information |
253 | */ | |
254 | void batadv_gw_init(struct batadv_priv *bat_priv) | |
255 | { | |
1a9070ec SE |
256 | 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 | ||
414254e3 ML |
261 | 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 | /** | |
7e9a8c2c | 267 | * batadv_gw_free() - free the gateway handling internals |
414254e3 ML |
268 | * @bat_priv: the bat priv with all the soft interface information |
269 | */ | |
270 | void 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 | } |