gateway_common.c 7.5 KB
Newer Older
1
/* Copyright (C) 2009-2015 B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14
 *
 * Marek Lindner
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * 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 20 21
#include "main.h"

#include <linux/atomic.h>
22
#include <linux/errno.h>
23 24
#include <linux/byteorder/generic.h>
#include <linux/kernel.h>
25
#include <linux/math64.h>
26 27 28 29
#include <linux/netdevice.h>
#include <linux/stddef.h>
#include <linux/string.h>

30
#include "gateway_client.h"
31
#include "packet.h"
32

33 34 35 36 37 38 39 40 41 42
/**
 * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
 *  and upload bandwidth information
 * @net_dev: the soft interface net device
 * @buff: string buffer to parse
 * @down: pointer holding the returned download bandwidth information
 * @up: pointer holding the returned upload bandwidth information
 *
 * Returns false on parse error and true otherwise.
 */
43
static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
44
				      u32 *down, u32 *up)
45
{
46
	enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
47
	char *slash_ptr, *tmp_ptr;
48
	u64 ldown, lup;
49
	int ret;
50 51 52 53 54 55 56 57

	slash_ptr = strchr(buff, '/');
	if (slash_ptr)
		*slash_ptr = 0;

	if (strlen(buff) > 4) {
		tmp_ptr = buff + strlen(buff) - 4;

58
		if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
59
			bw_unit_type = BATADV_BW_UNIT_MBIT;
60

61
		if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
62
		    (bw_unit_type == BATADV_BW_UNIT_MBIT))
63 64 65
			*tmp_ptr = '\0';
	}

66
	ret = kstrtou64(buff, 10, &ldown);
67
	if (ret) {
68 69 70
		batadv_err(net_dev,
			   "Download speed of gateway mode invalid: %s\n",
			   buff);
71 72 73
		return false;
	}

74 75
	switch (bw_unit_type) {
	case BATADV_BW_UNIT_MBIT:
76 77 78 79 80 81 82 83 84
		/* prevent overflow */
		if (U64_MAX / 10 < ldown) {
			batadv_err(net_dev,
				   "Download speed of gateway mode too large: %s\n",
				   buff);
			return false;
		}

		ldown *= 10;
85 86 87
		break;
	case BATADV_BW_UNIT_KBIT:
	default:
88
		ldown = div_u64(ldown, 100);
89 90
		break;
	}
91

92 93 94 95 96 97 98 99 100
	if (U32_MAX < ldown) {
		batadv_err(net_dev,
			   "Download speed of gateway mode too large: %s\n",
			   buff);
		return false;
	}

	*down = ldown;

101 102
	/* we also got some upload info */
	if (slash_ptr) {
103
		bw_unit_type = BATADV_BW_UNIT_KBIT;
104 105 106 107

		if (strlen(slash_ptr + 1) > 4) {
			tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);

108
			if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
109
				bw_unit_type = BATADV_BW_UNIT_MBIT;
110

111
			if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
112
			    (bw_unit_type == BATADV_BW_UNIT_MBIT))
113 114 115
				*tmp_ptr = '\0';
		}

116
		ret = kstrtou64(slash_ptr + 1, 10, &lup);
117
		if (ret) {
118 119 120
			batadv_err(net_dev,
				   "Upload speed of gateway mode invalid: %s\n",
				   slash_ptr + 1);
121 122 123
			return false;
		}

124 125
		switch (bw_unit_type) {
		case BATADV_BW_UNIT_MBIT:
126 127 128 129 130 131 132 133 134
			/* prevent overflow */
			if (U64_MAX / 10 < lup) {
				batadv_err(net_dev,
					   "Upload speed of gateway mode too large: %s\n",
					   slash_ptr + 1);
				return false;
			}

			lup *= 10;
135 136 137
			break;
		case BATADV_BW_UNIT_KBIT:
		default:
138
			lup = div_u64(lup, 100);
139 140
			break;
		}
141 142 143 144 145 146 147 148 149

		if (U32_MAX < lup) {
			batadv_err(net_dev,
				   "Upload speed of gateway mode too large: %s\n",
				   slash_ptr + 1);
			return false;
		}

		*up = lup;
150 151 152 153 154
	}

	return true;
}

155 156 157 158 159 160 161 162
/**
 * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
 *  setting change
 * @bat_priv: the bat priv with all the soft interface information
 */
void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
{
	struct batadv_tvlv_gateway_data gw;
163
	u32 down, up;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	char gw_mode;

	gw_mode = atomic_read(&bat_priv->gw_mode);

	switch (gw_mode) {
	case BATADV_GW_MODE_OFF:
	case BATADV_GW_MODE_CLIENT:
		batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
		break;
	case BATADV_GW_MODE_SERVER:
		down = atomic_read(&bat_priv->gw.bandwidth_down);
		up = atomic_read(&bat_priv->gw.bandwidth_up);
		gw.bandwidth_down = htonl(down);
		gw.bandwidth_up = htonl(up);
		batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
					       &gw, sizeof(gw));
		break;
	}
}

184 185
ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
				size_t count)
186
{
187
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
188 189 190 191
	u32 down_curr;
	u32 up_curr;
	u32 down_new = 0;
	u32 up_new = 0;
192 193
	bool ret;

194 195 196 197
	down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
	up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);

	ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
198
	if (!ret)
199
		return -EINVAL;
200

201 202
	if (!down_new)
		down_new = 1;
203

204 205
	if (!up_new)
		up_new = down_new / 5;
206

207 208
	if (!up_new)
		up_new = 1;
209

210
	if ((down_curr == down_new) && (up_curr == up_new))
211 212
		return count;

213
	batadv_gw_reselect(bat_priv);
214
	batadv_info(net_dev,
215 216 217
		    "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
		    down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
		    down_new / 10, down_new % 10, up_new / 10, up_new % 10);
218

219 220 221
	atomic_set(&bat_priv->gw.bandwidth_down, down_new);
	atomic_set(&bat_priv->gw.bandwidth_up, up_new);
	batadv_gw_tvlv_container_update(bat_priv);
222 223 224

	return count;
}
225 226 227 228 229 230 231 232 233 234 235

/**
 * batadv_gw_tvlv_ogm_handler_v1 - process incoming gateway tvlv container
 * @bat_priv: the bat priv with all the soft interface information
 * @orig: the orig_node of the ogm
 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
 * @tvlv_value: tvlv buffer containing the gateway data
 * @tvlv_value_len: tvlv buffer length
 */
static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
					  struct batadv_orig_node *orig,
236 237
					  u8 flags,
					  void *tvlv_value, u16 tvlv_value_len)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
{
	struct batadv_tvlv_gateway_data gateway, *gateway_ptr;

	/* only fetch the tvlv value if the handler wasn't called via the
	 * CIFNOTFND flag and if there is data to fetch
	 */
	if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) ||
	    (tvlv_value_len < sizeof(gateway))) {
		gateway.bandwidth_down = 0;
		gateway.bandwidth_up = 0;
	} else {
		gateway_ptr = tvlv_value;
		gateway.bandwidth_down = gateway_ptr->bandwidth_down;
		gateway.bandwidth_up = gateway_ptr->bandwidth_up;
		if ((gateway.bandwidth_down == 0) ||
		    (gateway.bandwidth_up == 0)) {
			gateway.bandwidth_down = 0;
			gateway.bandwidth_up = 0;
		}
	}

	batadv_gw_node_update(bat_priv, orig, &gateway);

	/* restart gateway selection if fast or late switching was enabled */
	if ((gateway.bandwidth_down != 0) &&
	    (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
	    (atomic_read(&bat_priv->gw_sel_class) > 2))
		batadv_gw_check_election(bat_priv, orig);
}

/**
 * batadv_gw_init - initialise the gateway handling internals
 * @bat_priv: the bat priv with all the soft interface information
 */
void batadv_gw_init(struct batadv_priv *bat_priv)
{
	batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
				     NULL, BATADV_TVLV_GW, 1,
				     BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
}

/**
 * batadv_gw_free - free the gateway handling internals
 * @bat_priv: the bat priv with all the soft interface information
 */
void batadv_gw_free(struct batadv_priv *bat_priv)
{
	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
}