1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "gateway_common.h"
20#include "main.h"
21
22#include <linux/atomic.h>
23#include <linux/byteorder/generic.h>
24#include <linux/errno.h>
25#include <linux/kernel.h>
26#include <linux/math64.h>
27#include <linux/netdevice.h>
28#include <linux/stddef.h>
29#include <linux/string.h>
30#include <uapi/linux/batadv_packet.h>
31
32#include "gateway_client.h"
33#include "log.h"
34#include "tvlv.h"
35
36
37
38
39
40
41
42
43
44
45
46bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
47 const char *description, u32 *throughput)
48{
49 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
50 u64 lthroughput;
51 char *tmp_ptr;
52 int ret;
53
54 if (strlen(buff) > 4) {
55 tmp_ptr = buff + strlen(buff) - 4;
56
57 if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
58 bw_unit_type = BATADV_BW_UNIT_MBIT;
59
60 if (strncasecmp(tmp_ptr, "kbit", 4) == 0 ||
61 bw_unit_type == BATADV_BW_UNIT_MBIT)
62 *tmp_ptr = '\0';
63 }
64
65 ret = kstrtou64(buff, 10, <hroughput);
66 if (ret) {
67 batadv_err(net_dev,
68 "Invalid throughput speed for %s: %s\n",
69 description, buff);
70 return false;
71 }
72
73 switch (bw_unit_type) {
74 case BATADV_BW_UNIT_MBIT:
75
76 if (U64_MAX / 10 < lthroughput) {
77 batadv_err(net_dev,
78 "Throughput speed for %s too large: %s\n",
79 description, buff);
80 return false;
81 }
82
83 lthroughput *= 10;
84 break;
85 case BATADV_BW_UNIT_KBIT:
86 default:
87 lthroughput = div_u64(lthroughput, 100);
88 break;
89 }
90
91 if (lthroughput > U32_MAX) {
92 batadv_err(net_dev,
93 "Throughput speed for %s too large: %s\n",
94 description, buff);
95 return false;
96 }
97
98 *throughput = lthroughput;
99
100 return true;
101}
102
103
104
105
106
107
108
109
110
111
112
113static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
114 u32 *down, u32 *up)
115{
116 char *slash_ptr;
117 bool ret;
118
119 slash_ptr = strchr(buff, '/');
120 if (slash_ptr)
121 *slash_ptr = 0;
122
123 ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
124 down);
125 if (!ret)
126 return false;
127
128
129 if (slash_ptr) {
130 ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
131 "upload gateway speed", up);
132 if (!ret)
133 return false;
134 }
135
136 return true;
137}
138
139
140
141
142
143
144void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
145{
146 struct batadv_tvlv_gateway_data gw;
147 u32 down, up;
148 char gw_mode;
149
150 gw_mode = atomic_read(&bat_priv->gw.mode);
151
152 switch (gw_mode) {
153 case BATADV_GW_MODE_OFF:
154 case BATADV_GW_MODE_CLIENT:
155 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
156 break;
157 case BATADV_GW_MODE_SERVER:
158 down = atomic_read(&bat_priv->gw.bandwidth_down);
159 up = atomic_read(&bat_priv->gw.bandwidth_up);
160 gw.bandwidth_down = htonl(down);
161 gw.bandwidth_up = htonl(up);
162 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
163 &gw, sizeof(gw));
164 break;
165 }
166}
167
168
169
170
171
172
173
174
175
176
177ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
178 size_t count)
179{
180 struct batadv_priv *bat_priv = netdev_priv(net_dev);
181 u32 down_curr;
182 u32 up_curr;
183 u32 down_new = 0;
184 u32 up_new = 0;
185 bool ret;
186
187 down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
188 up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
189
190 ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
191 if (!ret)
192 return -EINVAL;
193
194 if (!down_new)
195 down_new = 1;
196
197 if (!up_new)
198 up_new = down_new / 5;
199
200 if (!up_new)
201 up_new = 1;
202
203 if (down_curr == down_new && up_curr == up_new)
204 return count;
205
206 batadv_gw_reselect(bat_priv);
207 batadv_info(net_dev,
208 "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
209 down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
210 down_new / 10, down_new % 10, up_new / 10, up_new % 10);
211
212 atomic_set(&bat_priv->gw.bandwidth_down, down_new);
213 atomic_set(&bat_priv->gw.bandwidth_up, up_new);
214 batadv_gw_tvlv_container_update(bat_priv);
215
216 return count;
217}
218
219
220
221
222
223
224
225
226
227static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
228 struct batadv_orig_node *orig,
229 u8 flags,
230 void *tvlv_value, u16 tvlv_value_len)
231{
232 struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
233
234
235
236
237 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND ||
238 tvlv_value_len < sizeof(gateway)) {
239 gateway.bandwidth_down = 0;
240 gateway.bandwidth_up = 0;
241 } else {
242 gateway_ptr = tvlv_value;
243 gateway.bandwidth_down = gateway_ptr->bandwidth_down;
244 gateway.bandwidth_up = gateway_ptr->bandwidth_up;
245 if (gateway.bandwidth_down == 0 ||
246 gateway.bandwidth_up == 0) {
247 gateway.bandwidth_down = 0;
248 gateway.bandwidth_up = 0;
249 }
250 }
251
252 batadv_gw_node_update(bat_priv, orig, &gateway);
253
254
255 if (gateway.bandwidth_down != 0 &&
256 atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT)
257 batadv_gw_check_election(bat_priv, orig);
258}
259
260
261
262
263
264void batadv_gw_init(struct batadv_priv *bat_priv)
265{
266 if (bat_priv->algo_ops->gw.init_sel_class)
267 bat_priv->algo_ops->gw.init_sel_class(bat_priv);
268 else
269 atomic_set(&bat_priv->gw.sel_class, 1);
270
271 batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
272 NULL, BATADV_TVLV_GW, 1,
273 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
274}
275
276
277
278
279
280void batadv_gw_free(struct batadv_priv *bat_priv)
281{
282 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
283 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
284}
285