batman-adv: split tvlv into a separate file
[deliverable/linux.git] / net / batman-adv / gateway_common.c
1 /* Copyright (C) 2009-2016 B.A.T.M.A.N. contributors:
2 *
3 * Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "gateway_common.h"
19 #include "main.h"
20
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/errno.h>
24 #include <linux/kernel.h>
25 #include <linux/math64.h>
26 #include <linux/netdevice.h>
27 #include <linux/stddef.h>
28 #include <linux/string.h>
29
30 #include "gateway_client.h"
31 #include "packet.h"
32 #include "tvlv.h"
33
34 /**
35 * batadv_parse_throughput - parse supplied string buffer to extract throughput
36 * information
37 * @net_dev: the soft interface net device
38 * @buff: string buffer to parse
39 * @description: text shown when throughput string cannot be parsed
40 * @throughput: pointer holding the returned throughput information
41 *
42 * Return: false on parse error and true otherwise.
43 */
44 bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
45 const char *description, u32 *throughput)
46 {
47 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
48 u64 lthroughput;
49 char *tmp_ptr;
50 int ret;
51
52 if (strlen(buff) > 4) {
53 tmp_ptr = buff + strlen(buff) - 4;
54
55 if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
56 bw_unit_type = BATADV_BW_UNIT_MBIT;
57
58 if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
59 (bw_unit_type == BATADV_BW_UNIT_MBIT))
60 *tmp_ptr = '\0';
61 }
62
63 ret = kstrtou64(buff, 10, &lthroughput);
64 if (ret) {
65 batadv_err(net_dev,
66 "Invalid throughput speed for %s: %s\n",
67 description, buff);
68 return false;
69 }
70
71 switch (bw_unit_type) {
72 case BATADV_BW_UNIT_MBIT:
73 /* prevent overflow */
74 if (U64_MAX / 10 < lthroughput) {
75 batadv_err(net_dev,
76 "Throughput speed for %s too large: %s\n",
77 description, buff);
78 return false;
79 }
80
81 lthroughput *= 10;
82 break;
83 case BATADV_BW_UNIT_KBIT:
84 default:
85 lthroughput = div_u64(lthroughput, 100);
86 break;
87 }
88
89 if (lthroughput > U32_MAX) {
90 batadv_err(net_dev,
91 "Throughput speed for %s too large: %s\n",
92 description, buff);
93 return false;
94 }
95
96 *throughput = lthroughput;
97
98 return true;
99 }
100
101 /**
102 * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
103 * and upload bandwidth information
104 * @net_dev: the soft interface net device
105 * @buff: string buffer to parse
106 * @down: pointer holding the returned download bandwidth information
107 * @up: pointer holding the returned upload bandwidth information
108 *
109 * Return: false on parse error and true otherwise.
110 */
111 static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
112 u32 *down, u32 *up)
113 {
114 char *slash_ptr;
115 bool ret;
116
117 slash_ptr = strchr(buff, '/');
118 if (slash_ptr)
119 *slash_ptr = 0;
120
121 ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
122 down);
123 if (!ret)
124 return false;
125
126 /* we also got some upload info */
127 if (slash_ptr) {
128 ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
129 "upload gateway speed", up);
130 if (!ret)
131 return false;
132 }
133
134 return true;
135 }
136
137 /**
138 * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
139 * setting change
140 * @bat_priv: the bat priv with all the soft interface information
141 */
142 void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
143 {
144 struct batadv_tvlv_gateway_data gw;
145 u32 down, up;
146 char gw_mode;
147
148 gw_mode = atomic_read(&bat_priv->gw.mode);
149
150 switch (gw_mode) {
151 case BATADV_GW_MODE_OFF:
152 case BATADV_GW_MODE_CLIENT:
153 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
154 break;
155 case BATADV_GW_MODE_SERVER:
156 down = atomic_read(&bat_priv->gw.bandwidth_down);
157 up = atomic_read(&bat_priv->gw.bandwidth_up);
158 gw.bandwidth_down = htonl(down);
159 gw.bandwidth_up = htonl(up);
160 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
161 &gw, sizeof(gw));
162 break;
163 }
164 }
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 if fast or late switching was enabled */
244 if ((gateway.bandwidth_down != 0) &&
245 (atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT) &&
246 (atomic_read(&bat_priv->gw.sel_class) > 2))
247 batadv_gw_check_election(bat_priv, orig);
248 }
249
250 /**
251 * batadv_gw_init - initialise the gateway handling internals
252 * @bat_priv: the bat priv with all the soft interface information
253 */
254 void batadv_gw_init(struct batadv_priv *bat_priv)
255 {
256 batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
257 NULL, BATADV_TVLV_GW, 1,
258 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
259 }
260
261 /**
262 * batadv_gw_free - free the gateway handling internals
263 * @bat_priv: the bat priv with all the soft interface information
264 */
265 void batadv_gw_free(struct batadv_priv *bat_priv)
266 {
267 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
268 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
269 }
This page took 0.052486 seconds and 5 git commands to generate.