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