]>
Commit | Line | Data |
---|---|---|
9f6446c7 | 1 | /* Copyright (C) 2009-2015 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
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 | |
ebf38fb7 | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
c6c8fea2 SE |
16 | */ |
17 | ||
c6c8fea2 | 18 | #include "gateway_common.h" |
1e2c2a4f SE |
19 | #include "main.h" |
20 | ||
21 | #include <linux/atomic.h> | |
22 | #include <linux/byteorder/generic.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/netdevice.h> | |
25 | #include <linux/stddef.h> | |
26 | #include <linux/string.h> | |
27 | ||
c6c8fea2 | 28 | #include "gateway_client.h" |
1e2c2a4f | 29 | #include "packet.h" |
c6c8fea2 | 30 | |
414254e3 ML |
31 | /** |
32 | * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download | |
33 | * and upload bandwidth information | |
34 | * @net_dev: the soft interface net device | |
35 | * @buff: string buffer to parse | |
36 | * @down: pointer holding the returned download bandwidth information | |
37 | * @up: pointer holding the returned upload bandwidth information | |
38 | * | |
39 | * Returns false on parse error and true otherwise. | |
40 | */ | |
8e714a5d | 41 | static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff, |
6b5e971a | 42 | u32 *down, u32 *up) |
c6c8fea2 | 43 | { |
414254e3 | 44 | enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT; |
c6c8fea2 | 45 | char *slash_ptr, *tmp_ptr; |
37a4065e | 46 | long ldown, lup; |
414254e3 | 47 | int ret; |
c6c8fea2 SE |
48 | |
49 | slash_ptr = strchr(buff, '/'); | |
50 | if (slash_ptr) | |
51 | *slash_ptr = 0; | |
52 | ||
53 | if (strlen(buff) > 4) { | |
54 | tmp_ptr = buff + strlen(buff) - 4; | |
55 | ||
b7a8d756 | 56 | if (strncasecmp(tmp_ptr, "mbit", 4) == 0) |
414254e3 | 57 | bw_unit_type = BATADV_BW_UNIT_MBIT; |
c6c8fea2 | 58 | |
b7a8d756 | 59 | if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) || |
414254e3 | 60 | (bw_unit_type == BATADV_BW_UNIT_MBIT)) |
c6c8fea2 SE |
61 | *tmp_ptr = '\0'; |
62 | } | |
63 | ||
25a92b13 | 64 | ret = kstrtol(buff, 10, &ldown); |
c6c8fea2 | 65 | if (ret) { |
3e34819e SE |
66 | batadv_err(net_dev, |
67 | "Download speed of gateway mode invalid: %s\n", | |
68 | buff); | |
c6c8fea2 SE |
69 | return false; |
70 | } | |
71 | ||
414254e3 ML |
72 | switch (bw_unit_type) { |
73 | case BATADV_BW_UNIT_MBIT: | |
74 | *down = ldown * 10; | |
75 | break; | |
76 | case BATADV_BW_UNIT_KBIT: | |
77 | default: | |
78 | *down = ldown / 100; | |
79 | break; | |
80 | } | |
c6c8fea2 SE |
81 | |
82 | /* we also got some upload info */ | |
83 | if (slash_ptr) { | |
414254e3 | 84 | bw_unit_type = BATADV_BW_UNIT_KBIT; |
c6c8fea2 SE |
85 | |
86 | if (strlen(slash_ptr + 1) > 4) { | |
87 | tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1); | |
88 | ||
b7a8d756 | 89 | if (strncasecmp(tmp_ptr, "mbit", 4) == 0) |
414254e3 | 90 | bw_unit_type = BATADV_BW_UNIT_MBIT; |
c6c8fea2 | 91 | |
b7a8d756 | 92 | if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) || |
414254e3 | 93 | (bw_unit_type == BATADV_BW_UNIT_MBIT)) |
c6c8fea2 SE |
94 | *tmp_ptr = '\0'; |
95 | } | |
96 | ||
25a92b13 | 97 | ret = kstrtol(slash_ptr + 1, 10, &lup); |
c6c8fea2 | 98 | if (ret) { |
3e34819e SE |
99 | batadv_err(net_dev, |
100 | "Upload speed of gateway mode invalid: %s\n", | |
101 | slash_ptr + 1); | |
c6c8fea2 SE |
102 | return false; |
103 | } | |
104 | ||
414254e3 ML |
105 | switch (bw_unit_type) { |
106 | case BATADV_BW_UNIT_MBIT: | |
107 | *up = lup * 10; | |
108 | break; | |
109 | case BATADV_BW_UNIT_KBIT: | |
110 | default: | |
111 | *up = lup / 100; | |
112 | break; | |
113 | } | |
c6c8fea2 SE |
114 | } |
115 | ||
116 | return true; | |
117 | } | |
118 | ||
414254e3 ML |
119 | /** |
120 | * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway | |
121 | * setting change | |
122 | * @bat_priv: the bat priv with all the soft interface information | |
123 | */ | |
124 | void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv) | |
125 | { | |
126 | struct batadv_tvlv_gateway_data gw; | |
6b5e971a | 127 | u32 down, up; |
414254e3 ML |
128 | char gw_mode; |
129 | ||
130 | gw_mode = atomic_read(&bat_priv->gw_mode); | |
131 | ||
132 | switch (gw_mode) { | |
133 | case BATADV_GW_MODE_OFF: | |
134 | case BATADV_GW_MODE_CLIENT: | |
135 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1); | |
136 | break; | |
137 | case BATADV_GW_MODE_SERVER: | |
138 | down = atomic_read(&bat_priv->gw.bandwidth_down); | |
139 | up = atomic_read(&bat_priv->gw.bandwidth_up); | |
140 | gw.bandwidth_down = htonl(down); | |
141 | gw.bandwidth_up = htonl(up); | |
142 | batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1, | |
143 | &gw, sizeof(gw)); | |
144 | break; | |
145 | } | |
146 | } | |
147 | ||
84d5e5e0 SE |
148 | ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, |
149 | size_t count) | |
c6c8fea2 | 150 | { |
56303d34 | 151 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
4f248cff SE |
152 | u32 down_curr; |
153 | u32 up_curr; | |
154 | u32 down_new = 0; | |
155 | u32 up_new = 0; | |
c6c8fea2 SE |
156 | bool ret; |
157 | ||
414254e3 ML |
158 | down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down); |
159 | up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up); | |
160 | ||
161 | ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new); | |
c6c8fea2 SE |
162 | if (!ret) |
163 | goto end; | |
164 | ||
414254e3 ML |
165 | if (!down_new) |
166 | down_new = 1; | |
c6c8fea2 | 167 | |
414254e3 ML |
168 | if (!up_new) |
169 | up_new = down_new / 5; | |
c6c8fea2 | 170 | |
414254e3 ML |
171 | if (!up_new) |
172 | up_new = 1; | |
c6c8fea2 | 173 | |
414254e3 | 174 | if ((down_curr == down_new) && (up_curr == up_new)) |
dafe94b2 ML |
175 | return count; |
176 | ||
4e820e72 | 177 | batadv_gw_reselect(bat_priv); |
3e34819e | 178 | batadv_info(net_dev, |
414254e3 ML |
179 | "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n", |
180 | down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10, | |
181 | down_new / 10, down_new % 10, up_new / 10, up_new % 10); | |
c6c8fea2 | 182 | |
414254e3 ML |
183 | atomic_set(&bat_priv->gw.bandwidth_down, down_new); |
184 | atomic_set(&bat_priv->gw.bandwidth_up, up_new); | |
185 | batadv_gw_tvlv_container_update(bat_priv); | |
c6c8fea2 SE |
186 | |
187 | end: | |
188 | return count; | |
189 | } | |
414254e3 ML |
190 | |
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 | */ | |
199 | static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv, | |
200 | struct batadv_orig_node *orig, | |
6b5e971a SE |
201 | u8 flags, |
202 | void *tvlv_value, u16 tvlv_value_len) | |
414254e3 ML |
203 | { |
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 | */ | |
237 | void 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 | */ | |
248 | void 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 | } |