1 /* Copyright (C) 2009-2015 B.A.T.M.A.N. contributors:
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.
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.
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/>.
18 #include "gateway_common.h"
21 #include <linux/atomic.h>
22 #include <linux/errno.h>
23 #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>
29 #include "gateway_client.h"
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
40 * Returns false on parse error and true otherwise.
42 static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
45 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
46 char *slash_ptr, *tmp_ptr;
50 slash_ptr = strchr(buff, '/');
54 if (strlen(buff) > 4) {
55 tmp_ptr = buff + strlen(buff) - 4;
57 if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
58 bw_unit_type = BATADV_BW_UNIT_MBIT;
60 if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
61 (bw_unit_type == BATADV_BW_UNIT_MBIT))
65 ret = kstrtol(buff, 10, &ldown);
68 "Download speed of gateway mode invalid: %s\n",
73 switch (bw_unit_type) {
74 case BATADV_BW_UNIT_MBIT:
77 case BATADV_BW_UNIT_KBIT:
83 /* we also got some upload info */
85 bw_unit_type = BATADV_BW_UNIT_KBIT;
87 if (strlen(slash_ptr + 1) > 4) {
88 tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
90 if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
91 bw_unit_type = BATADV_BW_UNIT_MBIT;
93 if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
94 (bw_unit_type == BATADV_BW_UNIT_MBIT))
98 ret = kstrtol(slash_ptr + 1, 10, &lup);
101 "Upload speed of gateway mode invalid: %s\n",
106 switch (bw_unit_type) {
107 case BATADV_BW_UNIT_MBIT:
110 case BATADV_BW_UNIT_KBIT:
121 * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
123 * @bat_priv: the bat priv with all the soft interface information
125 void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
127 struct batadv_tvlv_gateway_data gw;
131 gw_mode = atomic_read(&bat_priv->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);
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,
149 ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
152 struct batadv_priv *bat_priv = netdev_priv(net_dev);
159 down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
160 up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
162 ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
170 up_new = down_new / 5;
175 if ((down_curr == down_new) && (up_curr == up_new))
178 batadv_gw_reselect(bat_priv);
180 "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);
184 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);
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
199 static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
200 struct batadv_orig_node *orig,
202 void *tvlv_value, u16 tvlv_value_len)
204 struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
206 /* only fetch the tvlv value if the handler wasn't called via the
207 * CIFNOTFND flag and if there is data to fetch
209 if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) ||
210 (tvlv_value_len < sizeof(gateway))) {
211 gateway.bandwidth_down = 0;
212 gateway.bandwidth_up = 0;
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;
224 batadv_gw_node_update(bat_priv, orig, &gateway);
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);
234 * batadv_gw_init - initialise the gateway handling internals
235 * @bat_priv: the bat priv with all the soft interface information
237 void batadv_gw_init(struct batadv_priv *bat_priv)
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);
245 * batadv_gw_free - free the gateway handling internals
246 * @bat_priv: the bat priv with all the soft interface information
248 void batadv_gw_free(struct batadv_priv *bat_priv)
250 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
251 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);