blob: 6b930a651f382f98164a8d11a8dfe51c7a844155 [file] [log] [blame]
Sven Eckelmann9f6446c2015-04-23 13:16:35 +02001/* Copyright (C) 2009-2015 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
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
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "gateway_common.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
21#include <linux/atomic.h>
Sven Eckelmann77927b72015-06-21 14:42:49 +020022#include <linux/errno.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020023#include <linux/byteorder/generic.h>
24#include <linux/kernel.h>
25#include <linux/netdevice.h>
26#include <linux/stddef.h>
27#include <linux/string.h>
28
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020030#include "packet.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031
Marek Lindner414254e2013-04-23 21:39:58 +080032/**
33 * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
34 * and upload bandwidth information
35 * @net_dev: the soft interface net device
36 * @buff: string buffer to parse
37 * @down: pointer holding the returned download bandwidth information
38 * @up: pointer holding the returned upload bandwidth information
39 *
40 * Returns false on parse error and true otherwise.
41 */
Sven Eckelmann8e714a52012-05-12 18:33:56 +020042static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020043 u32 *down, u32 *up)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044{
Marek Lindner414254e2013-04-23 21:39:58 +080045 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 char *slash_ptr, *tmp_ptr;
Sven Eckelmann37a40652011-05-14 23:14:51 +020047 long ldown, lup;
Marek Lindner414254e2013-04-23 21:39:58 +080048 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049
50 slash_ptr = strchr(buff, '/');
51 if (slash_ptr)
52 *slash_ptr = 0;
53
54 if (strlen(buff) > 4) {
55 tmp_ptr = buff + strlen(buff) - 4;
56
Rasmus Villemoesb7a8d752014-10-13 15:54:42 -070057 if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
Marek Lindner414254e2013-04-23 21:39:58 +080058 bw_unit_type = BATADV_BW_UNIT_MBIT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059
Rasmus Villemoesb7a8d752014-10-13 15:54:42 -070060 if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
Marek Lindner414254e2013-04-23 21:39:58 +080061 (bw_unit_type == BATADV_BW_UNIT_MBIT))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062 *tmp_ptr = '\0';
63 }
64
Sven Eckelmann25a92b12011-09-30 13:32:01 +020065 ret = kstrtol(buff, 10, &ldown);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066 if (ret) {
Sven Eckelmann3e348192012-05-16 20:23:22 +020067 batadv_err(net_dev,
68 "Download speed of gateway mode invalid: %s\n",
69 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070 return false;
71 }
72
Marek Lindner414254e2013-04-23 21:39:58 +080073 switch (bw_unit_type) {
74 case BATADV_BW_UNIT_MBIT:
75 *down = ldown * 10;
76 break;
77 case BATADV_BW_UNIT_KBIT:
78 default:
79 *down = ldown / 100;
80 break;
81 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082
83 /* we also got some upload info */
84 if (slash_ptr) {
Marek Lindner414254e2013-04-23 21:39:58 +080085 bw_unit_type = BATADV_BW_UNIT_KBIT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000086
87 if (strlen(slash_ptr + 1) > 4) {
88 tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
89
Rasmus Villemoesb7a8d752014-10-13 15:54:42 -070090 if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
Marek Lindner414254e2013-04-23 21:39:58 +080091 bw_unit_type = BATADV_BW_UNIT_MBIT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000092
Rasmus Villemoesb7a8d752014-10-13 15:54:42 -070093 if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
Marek Lindner414254e2013-04-23 21:39:58 +080094 (bw_unit_type == BATADV_BW_UNIT_MBIT))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000095 *tmp_ptr = '\0';
96 }
97
Sven Eckelmann25a92b12011-09-30 13:32:01 +020098 ret = kstrtol(slash_ptr + 1, 10, &lup);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099 if (ret) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200100 batadv_err(net_dev,
101 "Upload speed of gateway mode invalid: %s\n",
102 slash_ptr + 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000103 return false;
104 }
105
Marek Lindner414254e2013-04-23 21:39:58 +0800106 switch (bw_unit_type) {
107 case BATADV_BW_UNIT_MBIT:
108 *up = lup * 10;
109 break;
110 case BATADV_BW_UNIT_KBIT:
111 default:
112 *up = lup / 100;
113 break;
114 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000115 }
116
117 return true;
118}
119
Marek Lindner414254e2013-04-23 21:39:58 +0800120/**
121 * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
122 * setting change
123 * @bat_priv: the bat priv with all the soft interface information
124 */
125void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
126{
127 struct batadv_tvlv_gateway_data gw;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200128 u32 down, up;
Marek Lindner414254e2013-04-23 21:39:58 +0800129 char gw_mode;
130
131 gw_mode = atomic_read(&bat_priv->gw_mode);
132
133 switch (gw_mode) {
134 case BATADV_GW_MODE_OFF:
135 case BATADV_GW_MODE_CLIENT:
136 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
137 break;
138 case BATADV_GW_MODE_SERVER:
139 down = atomic_read(&bat_priv->gw.bandwidth_down);
140 up = atomic_read(&bat_priv->gw.bandwidth_up);
141 gw.bandwidth_down = htonl(down);
142 gw.bandwidth_up = htonl(up);
143 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
144 &gw, sizeof(gw));
145 break;
146 }
147}
148
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200149ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
150 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200152 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200153 u32 down_curr;
154 u32 up_curr;
155 u32 down_new = 0;
156 u32 up_new = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157 bool ret;
158
Marek Lindner414254e2013-04-23 21:39:58 +0800159 down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
160 up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
161
162 ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163 if (!ret)
Sven Eckelmann77927b72015-06-21 14:42:49 +0200164 return -EINVAL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165
Marek Lindner414254e2013-04-23 21:39:58 +0800166 if (!down_new)
167 down_new = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168
Marek Lindner414254e2013-04-23 21:39:58 +0800169 if (!up_new)
170 up_new = down_new / 5;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000171
Marek Lindner414254e2013-04-23 21:39:58 +0800172 if (!up_new)
173 up_new = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174
Marek Lindner414254e2013-04-23 21:39:58 +0800175 if ((down_curr == down_new) && (up_curr == up_new))
Marek Lindnerdafe94b2012-05-11 16:10:50 +0800176 return count;
177
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100178 batadv_gw_reselect(bat_priv);
Sven Eckelmann3e348192012-05-16 20:23:22 +0200179 batadv_info(net_dev,
Marek Lindner414254e2013-04-23 21:39:58 +0800180 "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
181 down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
182 down_new / 10, down_new % 10, up_new / 10, up_new % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183
Marek Lindner414254e2013-04-23 21:39:58 +0800184 atomic_set(&bat_priv->gw.bandwidth_down, down_new);
185 atomic_set(&bat_priv->gw.bandwidth_up, up_new);
186 batadv_gw_tvlv_container_update(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188 return count;
189}
Marek Lindner414254e2013-04-23 21:39:58 +0800190
191/**
192 * batadv_gw_tvlv_ogm_handler_v1 - process incoming gateway tvlv container
193 * @bat_priv: the bat priv with all the soft interface information
194 * @orig: the orig_node of the ogm
195 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
196 * @tvlv_value: tvlv buffer containing the gateway data
197 * @tvlv_value_len: tvlv buffer length
198 */
199static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
200 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200201 u8 flags,
202 void *tvlv_value, u16 tvlv_value_len)
Marek Lindner414254e2013-04-23 21:39:58 +0800203{
204 struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
205
206 /* only fetch the tvlv value if the handler wasn't called via the
207 * CIFNOTFND flag and if there is data to fetch
208 */
209 if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) ||
210 (tvlv_value_len < sizeof(gateway))) {
211 gateway.bandwidth_down = 0;
212 gateway.bandwidth_up = 0;
213 } else {
214 gateway_ptr = tvlv_value;
215 gateway.bandwidth_down = gateway_ptr->bandwidth_down;
216 gateway.bandwidth_up = gateway_ptr->bandwidth_up;
217 if ((gateway.bandwidth_down == 0) ||
218 (gateway.bandwidth_up == 0)) {
219 gateway.bandwidth_down = 0;
220 gateway.bandwidth_up = 0;
221 }
222 }
223
224 batadv_gw_node_update(bat_priv, orig, &gateway);
225
226 /* restart gateway selection if fast or late switching was enabled */
227 if ((gateway.bandwidth_down != 0) &&
228 (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
229 (atomic_read(&bat_priv->gw_sel_class) > 2))
230 batadv_gw_check_election(bat_priv, orig);
231}
232
233/**
234 * batadv_gw_init - initialise the gateway handling internals
235 * @bat_priv: the bat priv with all the soft interface information
236 */
237void batadv_gw_init(struct batadv_priv *bat_priv)
238{
239 batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
240 NULL, BATADV_TVLV_GW, 1,
241 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
242}
243
244/**
245 * batadv_gw_free - free the gateway handling internals
246 * @bat_priv: the bat priv with all the soft interface information
247 */
248void batadv_gw_free(struct batadv_priv *bat_priv)
249{
250 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
251 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
252}