Commit | Line | Data |
---|---|---|
0046b040 | 1 | /* Copyright (C) 2010-2016 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 | ||
b706b13b | 18 | #include "sysfs.h" |
1e2c2a4f SE |
19 | #include "main.h" |
20 | ||
21 | #include <linux/atomic.h> | |
22 | #include <linux/compiler.h> | |
23 | #include <linux/device.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/fs.h> | |
26 | #include <linux/if.h> | |
27 | #include <linux/if_vlan.h> | |
28 | #include <linux/kernel.h> | |
fcafa5e7 | 29 | #include <linux/kref.h> |
1e2c2a4f SE |
30 | #include <linux/netdevice.h> |
31 | #include <linux/printk.h> | |
32 | #include <linux/rculist.h> | |
33 | #include <linux/rcupdate.h> | |
34 | #include <linux/rtnetlink.h> | |
35 | #include <linux/slab.h> | |
36 | #include <linux/stat.h> | |
37 | #include <linux/stddef.h> | |
38 | #include <linux/string.h> | |
39 | #include <linux/stringify.h> | |
77d69d8c | 40 | #include <linux/workqueue.h> |
1e2c2a4f | 41 | |
fcafa5e7 | 42 | #include "bridge_loop_avoidance.h" |
33af49ad | 43 | #include "distributed-arp-table.h" |
1e2c2a4f SE |
44 | #include "gateway_client.h" |
45 | #include "gateway_common.h" | |
c6c8fea2 | 46 | #include "hard-interface.h" |
ba412080 | 47 | #include "log.h" |
1e2c2a4f SE |
48 | #include "network-coding.h" |
49 | #include "packet.h" | |
90f4435d | 50 | #include "soft-interface.h" |
c6c8fea2 | 51 | |
0ff9b86f | 52 | static struct net_device *batadv_kobj_to_netdev(struct kobject *obj) |
3d222bba SE |
53 | { |
54 | struct device *dev = container_of(obj->parent, struct device, kobj); | |
f138694b | 55 | |
3d222bba SE |
56 | return to_net_dev(dev); |
57 | } | |
58 | ||
56303d34 | 59 | static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj) |
3d222bba | 60 | { |
0ff9b86f | 61 | struct net_device *net_dev = batadv_kobj_to_netdev(obj); |
f138694b | 62 | |
3d222bba SE |
63 | return netdev_priv(net_dev); |
64 | } | |
c6c8fea2 | 65 | |
90f4435d AQ |
66 | /** |
67 | * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv | |
68 | * @obj: kobject to covert | |
69 | * | |
62fe710f | 70 | * Return: the associated batadv_priv struct. |
90f4435d AQ |
71 | */ |
72 | static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj) | |
73 | { | |
74 | /* VLAN specific attributes are located in the root sysfs folder if they | |
75 | * refer to the untagged VLAN.. | |
76 | */ | |
77 | if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name)) | |
78 | return batadv_kobj_to_batpriv(obj); | |
79 | ||
80 | /* ..while the attributes for the tagged vlans are located in | |
81 | * the in the corresponding "vlan%VID" subfolder | |
82 | */ | |
83 | return batadv_kobj_to_batpriv(obj->parent); | |
84 | } | |
85 | ||
86 | /** | |
87 | * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct | |
7afcbbef | 88 | * @bat_priv: the bat priv with all the soft interface information |
90f4435d AQ |
89 | * @obj: kobject to covert |
90 | * | |
62fe710f | 91 | * Return: the associated softif_vlan struct if found, NULL otherwise. |
90f4435d AQ |
92 | */ |
93 | static struct batadv_softif_vlan * | |
94 | batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj) | |
95 | { | |
96 | struct batadv_softif_vlan *vlan_tmp, *vlan = NULL; | |
97 | ||
98 | rcu_read_lock(); | |
99 | hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) { | |
100 | if (vlan_tmp->kobj != obj) | |
101 | continue; | |
102 | ||
6be4d30c | 103 | if (!kref_get_unless_zero(&vlan_tmp->refcount)) |
90f4435d AQ |
104 | continue; |
105 | ||
106 | vlan = vlan_tmp; | |
107 | break; | |
108 | } | |
109 | rcu_read_unlock(); | |
110 | ||
111 | return vlan; | |
112 | } | |
113 | ||
347c80f0 SE |
114 | #define BATADV_UEV_TYPE_VAR "BATTYPE=" |
115 | #define BATADV_UEV_ACTION_VAR "BATACTION=" | |
116 | #define BATADV_UEV_DATA_VAR "BATDATA=" | |
c6bda689 | 117 | |
0ff9b86f | 118 | static char *batadv_uev_action_str[] = { |
c6bda689 AQ |
119 | "add", |
120 | "del", | |
cd9c7bfb SW |
121 | "change", |
122 | "loopdetect", | |
c6bda689 AQ |
123 | }; |
124 | ||
0ff9b86f | 125 | static char *batadv_uev_type_str[] = { |
cd9c7bfb SW |
126 | "gw", |
127 | "bla", | |
c6bda689 AQ |
128 | }; |
129 | ||
90f4435d AQ |
130 | /* Use this, if you have customized show and store functions for vlan attrs */ |
131 | #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \ | |
132 | struct batadv_attribute batadv_attr_vlan_##_name = { \ | |
133 | .attr = {.name = __stringify(_name), \ | |
134 | .mode = _mode }, \ | |
135 | .show = _show, \ | |
136 | .store = _store, \ | |
2b64df20 | 137 | } |
90f4435d | 138 | |
c6c8fea2 | 139 | /* Use this, if you have customized show and store functions */ |
347c80f0 | 140 | #define BATADV_ATTR(_name, _mode, _show, _store) \ |
b4d66b87 | 141 | struct batadv_attribute batadv_attr_##_name = { \ |
347c80f0 SE |
142 | .attr = {.name = __stringify(_name), \ |
143 | .mode = _mode }, \ | |
144 | .show = _show, \ | |
145 | .store = _store, \ | |
2b64df20 | 146 | } |
c6c8fea2 | 147 | |
347c80f0 | 148 | #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \ |
0ff9b86f SE |
149 | ssize_t batadv_store_##_name(struct kobject *kobj, \ |
150 | struct attribute *attr, char *buff, \ | |
151 | size_t count) \ | |
c6c8fea2 | 152 | { \ |
0ff9b86f | 153 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \ |
56303d34 | 154 | struct batadv_priv *bat_priv = netdev_priv(net_dev); \ |
f138694b | 155 | \ |
0ff9b86f SE |
156 | return __batadv_store_bool_attr(buff, count, _post_func, attr, \ |
157 | &bat_priv->_name, net_dev); \ | |
c6c8fea2 SE |
158 | } |
159 | ||
347c80f0 | 160 | #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \ |
0ff9b86f SE |
161 | ssize_t batadv_show_##_name(struct kobject *kobj, \ |
162 | struct attribute *attr, char *buff) \ | |
c6c8fea2 | 163 | { \ |
56303d34 | 164 | struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \ |
f138694b | 165 | \ |
c6c8fea2 SE |
166 | return sprintf(buff, "%s\n", \ |
167 | atomic_read(&bat_priv->_name) == 0 ? \ | |
168 | "disabled" : "enabled"); \ | |
169 | } \ | |
170 | ||
f245c38b | 171 | /* Use this, if you are going to turn a [name] in the soft-interface |
9cfc7bd6 SE |
172 | * (bat_priv) on or off |
173 | */ | |
347c80f0 SE |
174 | #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \ |
175 | static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \ | |
176 | static BATADV_ATTR_SIF_SHOW_BOOL(_name) \ | |
177 | static BATADV_ATTR(_name, _mode, batadv_show_##_name, \ | |
178 | batadv_store_##_name) | |
c6c8fea2 | 179 | |
6f70eb75 | 180 | #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \ |
0ff9b86f SE |
181 | ssize_t batadv_store_##_name(struct kobject *kobj, \ |
182 | struct attribute *attr, char *buff, \ | |
183 | size_t count) \ | |
c6c8fea2 | 184 | { \ |
0ff9b86f | 185 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \ |
56303d34 | 186 | struct batadv_priv *bat_priv = netdev_priv(net_dev); \ |
f138694b | 187 | \ |
0ff9b86f SE |
188 | return __batadv_store_uint_attr(buff, count, _min, _max, \ |
189 | _post_func, attr, \ | |
6f70eb75 | 190 | &bat_priv->_var, net_dev); \ |
c6c8fea2 SE |
191 | } |
192 | ||
6f70eb75 | 193 | #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \ |
0ff9b86f SE |
194 | ssize_t batadv_show_##_name(struct kobject *kobj, \ |
195 | struct attribute *attr, char *buff) \ | |
c6c8fea2 | 196 | { \ |
56303d34 | 197 | struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \ |
f138694b | 198 | \ |
6f70eb75 | 199 | return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \ |
c6c8fea2 SE |
200 | } \ |
201 | ||
f245c38b | 202 | /* Use this, if you are going to set [name] in the soft-interface |
9cfc7bd6 SE |
203 | * (bat_priv) to an unsigned integer value |
204 | */ | |
6f70eb75 AQ |
205 | #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\ |
206 | static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\ | |
207 | static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \ | |
347c80f0 SE |
208 | static BATADV_ATTR(_name, _mode, batadv_show_##_name, \ |
209 | batadv_store_##_name) | |
c6c8fea2 | 210 | |
90f4435d AQ |
211 | #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \ |
212 | ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \ | |
213 | struct attribute *attr, char *buff, \ | |
214 | size_t count) \ | |
215 | { \ | |
216 | struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\ | |
217 | struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \ | |
218 | kobj); \ | |
219 | size_t res = __batadv_store_bool_attr(buff, count, _post_func, \ | |
220 | attr, &vlan->_name, \ | |
221 | bat_priv->soft_iface); \ | |
f138694b | 222 | \ |
9c3bf081 | 223 | batadv_softif_vlan_put(vlan); \ |
90f4435d AQ |
224 | return res; \ |
225 | } | |
226 | ||
227 | #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \ | |
228 | ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \ | |
229 | struct attribute *attr, char *buff) \ | |
230 | { \ | |
231 | struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\ | |
232 | struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \ | |
233 | kobj); \ | |
234 | size_t res = sprintf(buff, "%s\n", \ | |
235 | atomic_read(&vlan->_name) == 0 ? \ | |
236 | "disabled" : "enabled"); \ | |
f138694b | 237 | \ |
9c3bf081 | 238 | batadv_softif_vlan_put(vlan); \ |
90f4435d AQ |
239 | return res; \ |
240 | } | |
241 | ||
242 | /* Use this, if you are going to turn a [name] in the vlan struct on or off */ | |
243 | #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \ | |
244 | static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \ | |
245 | static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \ | |
246 | static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \ | |
247 | batadv_store_vlan_##_name) | |
0744ff8f LL |
248 | |
249 | #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \ | |
250 | ssize_t batadv_store_##_name(struct kobject *kobj, \ | |
251 | struct attribute *attr, char *buff, \ | |
252 | size_t count) \ | |
253 | { \ | |
254 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \ | |
255 | struct batadv_hard_iface *hard_iface; \ | |
256 | ssize_t length; \ | |
257 | \ | |
258 | hard_iface = batadv_hardif_get_by_netdev(net_dev); \ | |
259 | if (!hard_iface) \ | |
260 | return 0; \ | |
261 | \ | |
262 | length = __batadv_store_uint_attr(buff, count, _min, _max, \ | |
263 | _post_func, attr, \ | |
264 | &hard_iface->_var, net_dev); \ | |
265 | \ | |
266 | batadv_hardif_put(hard_iface); \ | |
267 | return length; \ | |
268 | } | |
269 | ||
270 | #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \ | |
271 | ssize_t batadv_show_##_name(struct kobject *kobj, \ | |
272 | struct attribute *attr, char *buff) \ | |
273 | { \ | |
274 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \ | |
275 | struct batadv_hard_iface *hard_iface; \ | |
276 | ssize_t length; \ | |
277 | \ | |
278 | hard_iface = batadv_hardif_get_by_netdev(net_dev); \ | |
279 | if (!hard_iface) \ | |
280 | return 0; \ | |
281 | \ | |
282 | length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \ | |
283 | \ | |
284 | batadv_hardif_put(hard_iface); \ | |
285 | return length; \ | |
286 | } | |
287 | ||
288 | /* Use this, if you are going to set [name] in hard_iface to an | |
289 | * unsigned integer value | |
290 | */ | |
291 | #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\ | |
292 | static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \ | |
293 | _max, _post_func) \ | |
294 | static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \ | |
295 | static BATADV_ATTR(_name, _mode, batadv_show_##_name, \ | |
296 | batadv_store_##_name) | |
c6c8fea2 | 297 | |
0ff9b86f SE |
298 | static int batadv_store_bool_attr(char *buff, size_t count, |
299 | struct net_device *net_dev, | |
9e728e84 SW |
300 | const char *attr_name, atomic_t *attr, |
301 | bool *changed) | |
c6c8fea2 SE |
302 | { |
303 | int enabled = -1; | |
304 | ||
9e728e84 SW |
305 | *changed = false; |
306 | ||
c6c8fea2 SE |
307 | if (buff[count - 1] == '\n') |
308 | buff[count - 1] = '\0'; | |
309 | ||
310 | if ((strncmp(buff, "1", 2) == 0) || | |
311 | (strncmp(buff, "enable", 7) == 0) || | |
312 | (strncmp(buff, "enabled", 8) == 0)) | |
313 | enabled = 1; | |
314 | ||
315 | if ((strncmp(buff, "0", 2) == 0) || | |
316 | (strncmp(buff, "disable", 8) == 0) || | |
317 | (strncmp(buff, "disabled", 9) == 0)) | |
318 | enabled = 0; | |
319 | ||
320 | if (enabled < 0) { | |
3e34819e SE |
321 | batadv_info(net_dev, "%s: Invalid parameter received: %s\n", |
322 | attr_name, buff); | |
c6c8fea2 SE |
323 | return -EINVAL; |
324 | } | |
325 | ||
326 | if (atomic_read(attr) == enabled) | |
327 | return count; | |
328 | ||
3e34819e SE |
329 | batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name, |
330 | atomic_read(attr) == 1 ? "enabled" : "disabled", | |
331 | enabled == 1 ? "enabled" : "disabled"); | |
c6c8fea2 | 332 | |
9e728e84 SW |
333 | *changed = true; |
334 | ||
95c96174 | 335 | atomic_set(attr, (unsigned int)enabled); |
c6c8fea2 SE |
336 | return count; |
337 | } | |
338 | ||
0ff9b86f SE |
339 | static inline ssize_t |
340 | __batadv_store_bool_attr(char *buff, size_t count, | |
341 | void (*post_func)(struct net_device *), | |
342 | struct attribute *attr, | |
343 | atomic_t *attr_store, struct net_device *net_dev) | |
c6c8fea2 | 344 | { |
9e728e84 | 345 | bool changed; |
c6c8fea2 SE |
346 | int ret; |
347 | ||
0ff9b86f | 348 | ret = batadv_store_bool_attr(buff, count, net_dev, attr->name, |
9e728e84 SW |
349 | attr_store, &changed); |
350 | if (post_func && changed) | |
c6c8fea2 SE |
351 | post_func(net_dev); |
352 | ||
353 | return ret; | |
354 | } | |
355 | ||
0ff9b86f SE |
356 | static int batadv_store_uint_attr(const char *buff, size_t count, |
357 | struct net_device *net_dev, | |
358 | const char *attr_name, | |
359 | unsigned int min, unsigned int max, | |
360 | atomic_t *attr) | |
c6c8fea2 SE |
361 | { |
362 | unsigned long uint_val; | |
363 | int ret; | |
364 | ||
25a92b13 | 365 | ret = kstrtoul(buff, 10, &uint_val); |
c6c8fea2 | 366 | if (ret) { |
3e34819e SE |
367 | batadv_info(net_dev, "%s: Invalid parameter received: %s\n", |
368 | attr_name, buff); | |
c6c8fea2 SE |
369 | return -EINVAL; |
370 | } | |
371 | ||
372 | if (uint_val < min) { | |
3e34819e SE |
373 | batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n", |
374 | attr_name, uint_val, min); | |
c6c8fea2 SE |
375 | return -EINVAL; |
376 | } | |
377 | ||
378 | if (uint_val > max) { | |
3e34819e SE |
379 | batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n", |
380 | attr_name, uint_val, max); | |
c6c8fea2 SE |
381 | return -EINVAL; |
382 | } | |
383 | ||
384 | if (atomic_read(attr) == uint_val) | |
385 | return count; | |
386 | ||
3e34819e SE |
387 | batadv_info(net_dev, "%s: Changing from: %i to: %lu\n", |
388 | attr_name, atomic_read(attr), uint_val); | |
c6c8fea2 SE |
389 | |
390 | atomic_set(attr, uint_val); | |
391 | return count; | |
392 | } | |
393 | ||
c149ca72 AQ |
394 | static ssize_t __batadv_store_uint_attr(const char *buff, size_t count, |
395 | int min, int max, | |
396 | void (*post_func)(struct net_device *), | |
397 | const struct attribute *attr, | |
398 | atomic_t *attr_store, | |
399 | struct net_device *net_dev) | |
c6c8fea2 SE |
400 | { |
401 | int ret; | |
402 | ||
0ff9b86f SE |
403 | ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max, |
404 | attr_store); | |
c6c8fea2 SE |
405 | if (post_func && ret) |
406 | post_func(net_dev); | |
407 | ||
408 | return ret; | |
409 | } | |
410 | ||
0ff9b86f SE |
411 | static ssize_t batadv_show_bat_algo(struct kobject *kobj, |
412 | struct attribute *attr, char *buff) | |
ea3d2fd1 | 413 | { |
56303d34 | 414 | struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); |
f138694b | 415 | |
29824a55 | 416 | return sprintf(buff, "%s\n", bat_priv->algo_ops->name); |
ea3d2fd1 ML |
417 | } |
418 | ||
4e820e72 | 419 | static void batadv_post_gw_reselect(struct net_device *net_dev) |
c6c8fea2 | 420 | { |
56303d34 | 421 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
f138694b | 422 | |
4e820e72 | 423 | batadv_gw_reselect(bat_priv); |
c6c8fea2 SE |
424 | } |
425 | ||
0ff9b86f SE |
426 | static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr, |
427 | char *buff) | |
c6c8fea2 | 428 | { |
56303d34 | 429 | struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); |
c6c8fea2 SE |
430 | int bytes_written; |
431 | ||
a8d8d1de AQ |
432 | /* GW mode is not available if the routing algorithm in use does not |
433 | * implement the GW API | |
434 | */ | |
435 | if (!bat_priv->algo_ops->gw.get_best_gw_node || | |
436 | !bat_priv->algo_ops->gw.is_eligible) | |
437 | return -ENOENT; | |
438 | ||
3a24a63e | 439 | switch (atomic_read(&bat_priv->gw.mode)) { |
cd646ab1 | 440 | case BATADV_GW_MODE_CLIENT: |
97ea4ba1 SE |
441 | bytes_written = sprintf(buff, "%s\n", |
442 | BATADV_GW_MODE_CLIENT_NAME); | |
c6c8fea2 | 443 | break; |
cd646ab1 | 444 | case BATADV_GW_MODE_SERVER: |
97ea4ba1 SE |
445 | bytes_written = sprintf(buff, "%s\n", |
446 | BATADV_GW_MODE_SERVER_NAME); | |
c6c8fea2 SE |
447 | break; |
448 | default: | |
97ea4ba1 SE |
449 | bytes_written = sprintf(buff, "%s\n", |
450 | BATADV_GW_MODE_OFF_NAME); | |
c6c8fea2 SE |
451 | break; |
452 | } | |
453 | ||
454 | return bytes_written; | |
455 | } | |
456 | ||
0ff9b86f SE |
457 | static ssize_t batadv_store_gw_mode(struct kobject *kobj, |
458 | struct attribute *attr, char *buff, | |
459 | size_t count) | |
c6c8fea2 | 460 | { |
0ff9b86f | 461 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
56303d34 | 462 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
c6c8fea2 SE |
463 | char *curr_gw_mode_str; |
464 | int gw_mode_tmp = -1; | |
465 | ||
a8d8d1de AQ |
466 | /* toggling GW mode is allowed only if the routing algorithm in use |
467 | * provides the GW API | |
468 | */ | |
469 | if (!bat_priv->algo_ops->gw.get_best_gw_node || | |
470 | !bat_priv->algo_ops->gw.is_eligible) | |
471 | return -EINVAL; | |
472 | ||
c6c8fea2 SE |
473 | if (buff[count - 1] == '\n') |
474 | buff[count - 1] = '\0'; | |
475 | ||
97ea4ba1 SE |
476 | if (strncmp(buff, BATADV_GW_MODE_OFF_NAME, |
477 | strlen(BATADV_GW_MODE_OFF_NAME)) == 0) | |
cd646ab1 | 478 | gw_mode_tmp = BATADV_GW_MODE_OFF; |
c6c8fea2 | 479 | |
97ea4ba1 SE |
480 | if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME, |
481 | strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0) | |
cd646ab1 | 482 | gw_mode_tmp = BATADV_GW_MODE_CLIENT; |
c6c8fea2 | 483 | |
97ea4ba1 SE |
484 | if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME, |
485 | strlen(BATADV_GW_MODE_SERVER_NAME)) == 0) | |
cd646ab1 | 486 | gw_mode_tmp = BATADV_GW_MODE_SERVER; |
c6c8fea2 SE |
487 | |
488 | if (gw_mode_tmp < 0) { | |
3e34819e SE |
489 | batadv_info(net_dev, |
490 | "Invalid parameter for 'gw mode' setting received: %s\n", | |
491 | buff); | |
c6c8fea2 SE |
492 | return -EINVAL; |
493 | } | |
494 | ||
3a24a63e | 495 | if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp) |
c6c8fea2 SE |
496 | return count; |
497 | ||
3a24a63e | 498 | switch (atomic_read(&bat_priv->gw.mode)) { |
cd646ab1 | 499 | case BATADV_GW_MODE_CLIENT: |
97ea4ba1 | 500 | curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME; |
c6c8fea2 | 501 | break; |
cd646ab1 | 502 | case BATADV_GW_MODE_SERVER: |
97ea4ba1 | 503 | curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME; |
c6c8fea2 SE |
504 | break; |
505 | default: | |
97ea4ba1 | 506 | curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME; |
c6c8fea2 SE |
507 | break; |
508 | } | |
509 | ||
3e34819e SE |
510 | batadv_info(net_dev, "Changing gw mode from: %s to: %s\n", |
511 | curr_gw_mode_str, buff); | |
c6c8fea2 | 512 | |
f3163181 AQ |
513 | /* Invoking batadv_gw_reselect() is not enough to really de-select the |
514 | * current GW. It will only instruct the gateway client code to perform | |
515 | * a re-election the next time that this is needed. | |
516 | * | |
517 | * When gw client mode is being switched off the current GW must be | |
518 | * de-selected explicitly otherwise no GW_ADD uevent is thrown on | |
519 | * client mode re-activation. This is operation is performed in | |
520 | * batadv_gw_check_client_stop(). | |
521 | */ | |
4e820e72 | 522 | batadv_gw_reselect(bat_priv); |
c6eaa3f0 AQ |
523 | /* always call batadv_gw_check_client_stop() before changing the gateway |
524 | * state | |
525 | */ | |
526 | batadv_gw_check_client_stop(bat_priv); | |
3a24a63e | 527 | atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp); |
414254e3 | 528 | batadv_gw_tvlv_container_update(bat_priv); |
c6c8fea2 SE |
529 | return count; |
530 | } | |
531 | ||
08686943 AQ |
532 | static ssize_t batadv_show_gw_sel_class(struct kobject *kobj, |
533 | struct attribute *attr, char *buff) | |
534 | { | |
535 | struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); | |
536 | ||
a8d8d1de AQ |
537 | /* GW selection class is not available if the routing algorithm in use |
538 | * does not implement the GW API | |
539 | */ | |
540 | if (!bat_priv->algo_ops->gw.get_best_gw_node || | |
541 | !bat_priv->algo_ops->gw.is_eligible) | |
542 | return -ENOENT; | |
543 | ||
08686943 AQ |
544 | if (bat_priv->algo_ops->gw.show_sel_class) |
545 | return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff); | |
546 | ||
547 | return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class)); | |
548 | } | |
549 | ||
550 | static ssize_t batadv_store_gw_sel_class(struct kobject *kobj, | |
551 | struct attribute *attr, char *buff, | |
552 | size_t count) | |
553 | { | |
554 | struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); | |
555 | ||
a8d8d1de AQ |
556 | /* setting the GW selection class is allowed only if the routing |
557 | * algorithm in use implements the GW API | |
558 | */ | |
559 | if (!bat_priv->algo_ops->gw.get_best_gw_node || | |
560 | !bat_priv->algo_ops->gw.is_eligible) | |
561 | return -EINVAL; | |
562 | ||
08686943 AQ |
563 | if (buff[count - 1] == '\n') |
564 | buff[count - 1] = '\0'; | |
565 | ||
566 | if (bat_priv->algo_ops->gw.store_sel_class) | |
567 | return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff, | |
568 | count); | |
569 | ||
570 | return __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE, | |
571 | batadv_post_gw_reselect, attr, | |
572 | &bat_priv->gw.sel_class, | |
573 | bat_priv->soft_iface); | |
574 | } | |
575 | ||
0ff9b86f SE |
576 | static ssize_t batadv_show_gw_bwidth(struct kobject *kobj, |
577 | struct attribute *attr, char *buff) | |
c6c8fea2 | 578 | { |
56303d34 | 579 | struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); |
6b5e971a | 580 | u32 down, up; |
414254e3 ML |
581 | |
582 | down = atomic_read(&bat_priv->gw.bandwidth_down); | |
583 | up = atomic_read(&bat_priv->gw.bandwidth_up); | |
584 | ||
585 | return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10, | |
586 | down % 10, up / 10, up % 10); | |
c6c8fea2 SE |
587 | } |
588 | ||
0ff9b86f SE |
589 | static ssize_t batadv_store_gw_bwidth(struct kobject *kobj, |
590 | struct attribute *attr, char *buff, | |
591 | size_t count) | |
c6c8fea2 | 592 | { |
0ff9b86f | 593 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
c6c8fea2 SE |
594 | |
595 | if (buff[count - 1] == '\n') | |
596 | buff[count - 1] = '\0'; | |
597 | ||
84d5e5e0 | 598 | return batadv_gw_bandwidth_set(net_dev, buff, count); |
c6c8fea2 SE |
599 | } |
600 | ||
c42edfe3 AQ |
601 | /** |
602 | * batadv_show_isolation_mark - print the current isolation mark/mask | |
603 | * @kobj: kobject representing the private mesh sysfs directory | |
604 | * @attr: the batman-adv attribute the user is interacting with | |
605 | * @buff: the buffer that will contain the data to send back to the user | |
606 | * | |
62fe710f | 607 | * Return: the number of bytes written into 'buff' on success or a negative |
c42edfe3 AQ |
608 | * error code in case of failure |
609 | */ | |
610 | static ssize_t batadv_show_isolation_mark(struct kobject *kobj, | |
611 | struct attribute *attr, char *buff) | |
612 | { | |
613 | struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); | |
614 | ||
615 | return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark, | |
616 | bat_priv->isolation_mark_mask); | |
617 | } | |
618 | ||
619 | /** | |
620 | * batadv_store_isolation_mark - parse and store the isolation mark/mask entered | |
621 | * by the user | |
622 | * @kobj: kobject representing the private mesh sysfs directory | |
623 | * @attr: the batman-adv attribute the user is interacting with | |
624 | * @buff: the buffer containing the user data | |
625 | * @count: number of bytes in the buffer | |
626 | * | |
62fe710f | 627 | * Return: 'count' on success or a negative error code in case of failure |
c42edfe3 AQ |
628 | */ |
629 | static ssize_t batadv_store_isolation_mark(struct kobject *kobj, | |
630 | struct attribute *attr, char *buff, | |
631 | size_t count) | |
632 | { | |
633 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); | |
634 | struct batadv_priv *bat_priv = netdev_priv(net_dev); | |
6b5e971a | 635 | u32 mark, mask; |
c42edfe3 AQ |
636 | char *mask_ptr; |
637 | ||
638 | /* parse the mask if it has been specified, otherwise assume the mask is | |
639 | * the biggest possible | |
640 | */ | |
641 | mask = 0xFFFFFFFF; | |
642 | mask_ptr = strchr(buff, '/'); | |
643 | if (mask_ptr) { | |
644 | *mask_ptr = '\0'; | |
645 | mask_ptr++; | |
646 | ||
647 | /* the mask must be entered in hex base as it is going to be a | |
648 | * bitmask and not a prefix length | |
649 | */ | |
650 | if (kstrtou32(mask_ptr, 16, &mask) < 0) | |
651 | return -EINVAL; | |
652 | } | |
653 | ||
654 | /* the mark can be entered in any base */ | |
655 | if (kstrtou32(buff, 0, &mark) < 0) | |
656 | return -EINVAL; | |
657 | ||
658 | bat_priv->isolation_mark_mask = mask; | |
659 | /* erase bits not covered by the mask */ | |
660 | bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask; | |
661 | ||
662 | batadv_info(net_dev, | |
663 | "New skb mark for extended isolation: %#.8x/%#.8x\n", | |
664 | bat_priv->isolation_mark, bat_priv->isolation_mark_mask); | |
665 | ||
666 | return count; | |
667 | } | |
668 | ||
347c80f0 SE |
669 | BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL); |
670 | BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL); | |
7a5cc242 | 671 | #ifdef CONFIG_BATMAN_ADV_BLA |
d68081a2 SW |
672 | BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, |
673 | batadv_bla_status_update); | |
7a5cc242 | 674 | #endif |
33af49ad | 675 | #ifdef CONFIG_BATMAN_ADV_DAT |
17cf0ea4 ML |
676 | BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR, |
677 | batadv_dat_status_update); | |
33af49ad | 678 | #endif |
347c80f0 | 679 | BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu); |
347c80f0 SE |
680 | static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL); |
681 | static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode, | |
682 | batadv_store_gw_mode); | |
6f70eb75 AQ |
683 | BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, S_IRUGO | S_IWUSR, |
684 | 2 * BATADV_JITTER, INT_MAX, NULL); | |
685 | BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, S_IRUGO | S_IWUSR, 0, | |
686 | BATADV_TQ_MAX_VALUE, NULL); | |
08686943 AQ |
687 | static BATADV_ATTR(gw_sel_class, S_IRUGO | S_IWUSR, batadv_show_gw_sel_class, |
688 | batadv_store_gw_sel_class); | |
347c80f0 SE |
689 | static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth, |
690 | batadv_store_gw_bwidth); | |
1d8ab8d3 LL |
691 | #ifdef CONFIG_BATMAN_ADV_MCAST |
692 | BATADV_ATTR_SIF_BOOL(multicast_mode, S_IRUGO | S_IWUSR, NULL); | |
693 | #endif | |
c6c8fea2 | 694 | #ifdef CONFIG_BATMAN_ADV_DEBUG |
6f70eb75 AQ |
695 | BATADV_ATTR_SIF_UINT(log_level, log_level, S_IRUGO | S_IWUSR, 0, |
696 | BATADV_DBG_ALL, NULL); | |
c6c8fea2 | 697 | #endif |
d353d8d4 | 698 | #ifdef CONFIG_BATMAN_ADV_NC |
3f4841ff ML |
699 | BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR, |
700 | batadv_nc_status_update); | |
d353d8d4 | 701 | #endif |
c42edfe3 AQ |
702 | static BATADV_ATTR(isolation_mark, S_IRUGO | S_IWUSR, |
703 | batadv_show_isolation_mark, batadv_store_isolation_mark); | |
c6c8fea2 | 704 | |
b4d66b87 | 705 | static struct batadv_attribute *batadv_mesh_attrs[] = { |
0ff9b86f SE |
706 | &batadv_attr_aggregated_ogms, |
707 | &batadv_attr_bonding, | |
7a5cc242 | 708 | #ifdef CONFIG_BATMAN_ADV_BLA |
0ff9b86f | 709 | &batadv_attr_bridge_loop_avoidance, |
33af49ad AQ |
710 | #endif |
711 | #ifdef CONFIG_BATMAN_ADV_DAT | |
712 | &batadv_attr_distributed_arp_table, | |
1d8ab8d3 LL |
713 | #endif |
714 | #ifdef CONFIG_BATMAN_ADV_MCAST | |
715 | &batadv_attr_multicast_mode, | |
7a5cc242 | 716 | #endif |
0ff9b86f | 717 | &batadv_attr_fragmentation, |
0ff9b86f SE |
718 | &batadv_attr_routing_algo, |
719 | &batadv_attr_gw_mode, | |
720 | &batadv_attr_orig_interval, | |
721 | &batadv_attr_hop_penalty, | |
722 | &batadv_attr_gw_sel_class, | |
723 | &batadv_attr_gw_bandwidth, | |
c6c8fea2 | 724 | #ifdef CONFIG_BATMAN_ADV_DEBUG |
0ff9b86f | 725 | &batadv_attr_log_level, |
d353d8d4 MH |
726 | #endif |
727 | #ifdef CONFIG_BATMAN_ADV_NC | |
728 | &batadv_attr_network_coding, | |
c6c8fea2 | 729 | #endif |
c42edfe3 | 730 | &batadv_attr_isolation_mark, |
c6c8fea2 SE |
731 | NULL, |
732 | }; | |
733 | ||
b8cbd81d AQ |
734 | BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL); |
735 | ||
f34ac9d4 | 736 | /* array of vlan specific sysfs attributes */ |
90f4435d | 737 | static struct batadv_attribute *batadv_vlan_attrs[] = { |
b8cbd81d | 738 | &batadv_attr_vlan_ap_isolation, |
90f4435d AQ |
739 | NULL, |
740 | }; | |
741 | ||
5853e22c | 742 | int batadv_sysfs_add_meshif(struct net_device *dev) |
c6c8fea2 SE |
743 | { |
744 | struct kobject *batif_kobject = &dev->dev.kobj; | |
56303d34 | 745 | struct batadv_priv *bat_priv = netdev_priv(dev); |
b4d66b87 | 746 | struct batadv_attribute **bat_attr; |
c6c8fea2 SE |
747 | int err; |
748 | ||
036cbfeb | 749 | bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR, |
c6c8fea2 SE |
750 | batif_kobject); |
751 | if (!bat_priv->mesh_obj) { | |
3e34819e | 752 | batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name, |
036cbfeb | 753 | BATADV_SYSFS_IF_MESH_SUBDIR); |
c6c8fea2 SE |
754 | goto out; |
755 | } | |
756 | ||
0ff9b86f | 757 | for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) { |
c6c8fea2 SE |
758 | err = sysfs_create_file(bat_priv->mesh_obj, |
759 | &((*bat_attr)->attr)); | |
760 | if (err) { | |
3e34819e | 761 | batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n", |
036cbfeb | 762 | dev->name, BATADV_SYSFS_IF_MESH_SUBDIR, |
3e34819e | 763 | ((*bat_attr)->attr).name); |
c6c8fea2 SE |
764 | goto rem_attr; |
765 | } | |
766 | } | |
767 | ||
768 | return 0; | |
769 | ||
770 | rem_attr: | |
0ff9b86f | 771 | for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) |
c6c8fea2 SE |
772 | sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr)); |
773 | ||
f4acb108 SE |
774 | kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE); |
775 | kobject_del(bat_priv->mesh_obj); | |
c6c8fea2 SE |
776 | kobject_put(bat_priv->mesh_obj); |
777 | bat_priv->mesh_obj = NULL; | |
778 | out: | |
779 | return -ENOMEM; | |
780 | } | |
781 | ||
5853e22c | 782 | void batadv_sysfs_del_meshif(struct net_device *dev) |
c6c8fea2 | 783 | { |
56303d34 | 784 | struct batadv_priv *bat_priv = netdev_priv(dev); |
b4d66b87 | 785 | struct batadv_attribute **bat_attr; |
c6c8fea2 | 786 | |
0ff9b86f | 787 | for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) |
c6c8fea2 SE |
788 | sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr)); |
789 | ||
f4acb108 SE |
790 | kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE); |
791 | kobject_del(bat_priv->mesh_obj); | |
c6c8fea2 SE |
792 | kobject_put(bat_priv->mesh_obj); |
793 | bat_priv->mesh_obj = NULL; | |
794 | } | |
795 | ||
90f4435d AQ |
796 | /** |
797 | * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan | |
798 | * @dev: netdev of the mesh interface | |
799 | * @vlan: private data of the newly added VLAN interface | |
800 | * | |
62fe710f | 801 | * Return: 0 on success and -ENOMEM if any of the structure allocations fails. |
90f4435d AQ |
802 | */ |
803 | int batadv_sysfs_add_vlan(struct net_device *dev, | |
804 | struct batadv_softif_vlan *vlan) | |
805 | { | |
806 | char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5]; | |
807 | struct batadv_priv *bat_priv = netdev_priv(dev); | |
808 | struct batadv_attribute **bat_attr; | |
809 | int err; | |
810 | ||
811 | if (vlan->vid & BATADV_VLAN_HAS_TAG) { | |
812 | sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu", | |
813 | vlan->vid & VLAN_VID_MASK); | |
814 | ||
815 | vlan->kobj = kobject_create_and_add(vlan_subdir, | |
816 | bat_priv->mesh_obj); | |
817 | if (!vlan->kobj) { | |
818 | batadv_err(dev, "Can't add sysfs directory: %s/%s\n", | |
819 | dev->name, vlan_subdir); | |
820 | goto out; | |
821 | } | |
822 | } else { | |
823 | /* the untagged LAN uses the root folder to store its "VLAN | |
824 | * specific attributes" | |
825 | */ | |
826 | vlan->kobj = bat_priv->mesh_obj; | |
827 | kobject_get(bat_priv->mesh_obj); | |
828 | } | |
829 | ||
830 | for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) { | |
831 | err = sysfs_create_file(vlan->kobj, | |
832 | &((*bat_attr)->attr)); | |
833 | if (err) { | |
834 | batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n", | |
835 | dev->name, vlan_subdir, | |
836 | ((*bat_attr)->attr).name); | |
837 | goto rem_attr; | |
838 | } | |
839 | } | |
840 | ||
841 | return 0; | |
842 | ||
843 | rem_attr: | |
844 | for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) | |
845 | sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr)); | |
846 | ||
f4acb108 SE |
847 | if (vlan->kobj != bat_priv->mesh_obj) { |
848 | kobject_uevent(vlan->kobj, KOBJ_REMOVE); | |
849 | kobject_del(vlan->kobj); | |
850 | } | |
90f4435d AQ |
851 | kobject_put(vlan->kobj); |
852 | vlan->kobj = NULL; | |
853 | out: | |
854 | return -ENOMEM; | |
855 | } | |
856 | ||
857 | /** | |
858 | * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN | |
859 | * @bat_priv: the bat priv with all the soft interface information | |
860 | * @vlan: the private data of the VLAN to destroy | |
861 | */ | |
862 | void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv, | |
863 | struct batadv_softif_vlan *vlan) | |
864 | { | |
865 | struct batadv_attribute **bat_attr; | |
866 | ||
867 | for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) | |
868 | sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr)); | |
869 | ||
f4acb108 SE |
870 | if (vlan->kobj != bat_priv->mesh_obj) { |
871 | kobject_uevent(vlan->kobj, KOBJ_REMOVE); | |
872 | kobject_del(vlan->kobj); | |
873 | } | |
90f4435d AQ |
874 | kobject_put(vlan->kobj); |
875 | vlan->kobj = NULL; | |
876 | } | |
877 | ||
0ff9b86f SE |
878 | static ssize_t batadv_show_mesh_iface(struct kobject *kobj, |
879 | struct attribute *attr, char *buff) | |
c6c8fea2 | 880 | { |
0ff9b86f | 881 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
56303d34 | 882 | struct batadv_hard_iface *hard_iface; |
c6c8fea2 | 883 | ssize_t length; |
e9a4f295 | 884 | const char *ifname; |
c6c8fea2 | 885 | |
56303d34 | 886 | hard_iface = batadv_hardif_get_by_netdev(net_dev); |
e6c10f43 | 887 | if (!hard_iface) |
c6c8fea2 SE |
888 | return 0; |
889 | ||
e9a4f295 SE |
890 | if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) |
891 | ifname = "none"; | |
892 | else | |
893 | ifname = hard_iface->soft_iface->name; | |
894 | ||
895 | length = sprintf(buff, "%s\n", ifname); | |
c6c8fea2 | 896 | |
82047ad7 | 897 | batadv_hardif_put(hard_iface); |
c6c8fea2 SE |
898 | |
899 | return length; | |
900 | } | |
901 | ||
77d69d8c SE |
902 | /** |
903 | * batadv_store_mesh_iface_finish - store new hardif mesh_iface state | |
904 | * @net_dev: netdevice to add/remove to/from batman-adv soft-interface | |
905 | * @ifname: name of soft-interface to modify | |
906 | * | |
907 | * Changes the parts of the hard+soft interface which can not be modified under | |
908 | * sysfs lock (to prevent deadlock situations). | |
909 | * | |
910 | * Return: 0 on success, 0 < on failure | |
911 | */ | |
912 | static int batadv_store_mesh_iface_finish(struct net_device *net_dev, | |
913 | char ifname[IFNAMSIZ]) | |
c6c8fea2 | 914 | { |
2cd45a06 | 915 | struct net *net = dev_net(net_dev); |
56303d34 | 916 | struct batadv_hard_iface *hard_iface; |
77d69d8c SE |
917 | int status_tmp; |
918 | int ret = 0; | |
919 | ||
920 | ASSERT_RTNL(); | |
c6c8fea2 | 921 | |
56303d34 | 922 | hard_iface = batadv_hardif_get_by_netdev(net_dev); |
e6c10f43 | 923 | if (!hard_iface) |
77d69d8c | 924 | return 0; |
c6c8fea2 | 925 | |
77d69d8c | 926 | if (strncmp(ifname, "none", 4) == 0) |
e9a4f295 | 927 | status_tmp = BATADV_IF_NOT_IN_USE; |
c6c8fea2 | 928 | else |
e9a4f295 | 929 | status_tmp = BATADV_IF_I_WANT_YOU; |
c6c8fea2 | 930 | |
e6c10f43 ML |
931 | if (hard_iface->if_status == status_tmp) |
932 | goto out; | |
933 | ||
934 | if ((hard_iface->soft_iface) && | |
77d69d8c | 935 | (strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)) |
ed75ccbe | 936 | goto out; |
c6c8fea2 | 937 | |
e9a4f295 | 938 | if (status_tmp == BATADV_IF_NOT_IN_USE) { |
a15fd361 SE |
939 | batadv_hardif_disable_interface(hard_iface, |
940 | BATADV_IF_CLEANUP_AUTO); | |
77d69d8c | 941 | goto out; |
c6c8fea2 SE |
942 | } |
943 | ||
944 | /* if the interface already is in use */ | |
e9a4f295 | 945 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
a15fd361 SE |
946 | batadv_hardif_disable_interface(hard_iface, |
947 | BATADV_IF_CLEANUP_AUTO); | |
c6c8fea2 | 948 | |
77d69d8c | 949 | ret = batadv_hardif_enable_interface(hard_iface, net, ifname); |
ed75ccbe | 950 | out: |
82047ad7 | 951 | batadv_hardif_put(hard_iface); |
c6c8fea2 SE |
952 | return ret; |
953 | } | |
954 | ||
77d69d8c SE |
955 | /** |
956 | * batadv_store_mesh_iface_work - store new hardif mesh_iface state | |
957 | * @work: work queue item | |
958 | * | |
959 | * Changes the parts of the hard+soft interface which can not be modified under | |
960 | * sysfs lock (to prevent deadlock situations). | |
961 | */ | |
962 | static void batadv_store_mesh_iface_work(struct work_struct *work) | |
963 | { | |
964 | struct batadv_store_mesh_work *store_work; | |
965 | int ret; | |
966 | ||
967 | store_work = container_of(work, struct batadv_store_mesh_work, work); | |
968 | ||
969 | rtnl_lock(); | |
970 | ret = batadv_store_mesh_iface_finish(store_work->net_dev, | |
971 | store_work->soft_iface_name); | |
972 | rtnl_unlock(); | |
973 | ||
974 | if (ret < 0) | |
975 | pr_err("Failed to store new mesh_iface state %s for %s: %d\n", | |
976 | store_work->soft_iface_name, store_work->net_dev->name, | |
977 | ret); | |
978 | ||
979 | dev_put(store_work->net_dev); | |
980 | kfree(store_work); | |
981 | } | |
982 | ||
983 | static ssize_t batadv_store_mesh_iface(struct kobject *kobj, | |
984 | struct attribute *attr, char *buff, | |
985 | size_t count) | |
986 | { | |
987 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); | |
988 | struct batadv_store_mesh_work *store_work; | |
989 | ||
990 | if (buff[count - 1] == '\n') | |
991 | buff[count - 1] = '\0'; | |
992 | ||
993 | if (strlen(buff) >= IFNAMSIZ) { | |
994 | pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n", | |
995 | buff); | |
996 | return -EINVAL; | |
997 | } | |
998 | ||
999 | store_work = kmalloc(sizeof(*store_work), GFP_KERNEL); | |
1000 | if (!store_work) | |
1001 | return -ENOMEM; | |
1002 | ||
1003 | dev_hold(net_dev); | |
1004 | INIT_WORK(&store_work->work, batadv_store_mesh_iface_work); | |
1005 | store_work->net_dev = net_dev; | |
1006 | strlcpy(store_work->soft_iface_name, buff, | |
1007 | sizeof(store_work->soft_iface_name)); | |
1008 | ||
1009 | queue_work(batadv_event_workqueue, &store_work->work); | |
1010 | ||
1011 | return count; | |
1012 | } | |
1013 | ||
0ff9b86f SE |
1014 | static ssize_t batadv_show_iface_status(struct kobject *kobj, |
1015 | struct attribute *attr, char *buff) | |
c6c8fea2 | 1016 | { |
0ff9b86f | 1017 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
56303d34 | 1018 | struct batadv_hard_iface *hard_iface; |
c6c8fea2 SE |
1019 | ssize_t length; |
1020 | ||
56303d34 | 1021 | hard_iface = batadv_hardif_get_by_netdev(net_dev); |
e6c10f43 | 1022 | if (!hard_iface) |
c6c8fea2 SE |
1023 | return 0; |
1024 | ||
e6c10f43 | 1025 | switch (hard_iface->if_status) { |
e9a4f295 | 1026 | case BATADV_IF_TO_BE_REMOVED: |
c6c8fea2 SE |
1027 | length = sprintf(buff, "disabling\n"); |
1028 | break; | |
e9a4f295 | 1029 | case BATADV_IF_INACTIVE: |
c6c8fea2 SE |
1030 | length = sprintf(buff, "inactive\n"); |
1031 | break; | |
e9a4f295 | 1032 | case BATADV_IF_ACTIVE: |
c6c8fea2 SE |
1033 | length = sprintf(buff, "active\n"); |
1034 | break; | |
e9a4f295 | 1035 | case BATADV_IF_TO_BE_ACTIVATED: |
c6c8fea2 SE |
1036 | length = sprintf(buff, "enabling\n"); |
1037 | break; | |
e9a4f295 | 1038 | case BATADV_IF_NOT_IN_USE: |
c6c8fea2 SE |
1039 | default: |
1040 | length = sprintf(buff, "not in use\n"); | |
1041 | break; | |
1042 | } | |
1043 | ||
82047ad7 | 1044 | batadv_hardif_put(hard_iface); |
c6c8fea2 SE |
1045 | |
1046 | return length; | |
1047 | } | |
1048 | ||
0b5ecc68 AQ |
1049 | #ifdef CONFIG_BATMAN_ADV_BATMAN_V |
1050 | ||
1051 | /** | |
1052 | * batadv_store_throughput_override - parse and store throughput override | |
1053 | * entered by the user | |
1054 | * @kobj: kobject representing the private mesh sysfs directory | |
1055 | * @attr: the batman-adv attribute the user is interacting with | |
1056 | * @buff: the buffer containing the user data | |
1057 | * @count: number of bytes in the buffer | |
1058 | * | |
1059 | * Return: 'count' on success or a negative error code in case of failure | |
1060 | */ | |
1061 | static ssize_t batadv_store_throughput_override(struct kobject *kobj, | |
1062 | struct attribute *attr, | |
1063 | char *buff, size_t count) | |
1064 | { | |
1065 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); | |
1066 | struct batadv_hard_iface *hard_iface; | |
1067 | u32 tp_override; | |
1068 | u32 old_tp_override; | |
1069 | bool ret; | |
1070 | ||
1071 | hard_iface = batadv_hardif_get_by_netdev(net_dev); | |
1072 | if (!hard_iface) | |
1073 | return -EINVAL; | |
1074 | ||
1075 | if (buff[count - 1] == '\n') | |
1076 | buff[count - 1] = '\0'; | |
1077 | ||
1078 | ret = batadv_parse_throughput(net_dev, buff, "throughput_override", | |
1079 | &tp_override); | |
1080 | if (!ret) | |
1081 | return count; | |
1082 | ||
1083 | old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override); | |
1084 | if (old_tp_override == tp_override) | |
1085 | goto out; | |
1086 | ||
1087 | batadv_info(net_dev, "%s: Changing from: %u.%u MBit to: %u.%u MBit\n", | |
1088 | "throughput_override", | |
1089 | old_tp_override / 10, old_tp_override % 10, | |
1090 | tp_override / 10, tp_override % 10); | |
1091 | ||
1092 | atomic_set(&hard_iface->bat_v.throughput_override, tp_override); | |
1093 | ||
1094 | out: | |
1095 | batadv_hardif_put(hard_iface); | |
1096 | return count; | |
1097 | } | |
1098 | ||
1099 | static ssize_t batadv_show_throughput_override(struct kobject *kobj, | |
1100 | struct attribute *attr, | |
1101 | char *buff) | |
1102 | { | |
1103 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); | |
1104 | struct batadv_hard_iface *hard_iface; | |
1105 | u32 tp_override; | |
1106 | ||
1107 | hard_iface = batadv_hardif_get_by_netdev(net_dev); | |
1108 | if (!hard_iface) | |
1109 | return -EINVAL; | |
1110 | ||
1111 | tp_override = atomic_read(&hard_iface->bat_v.throughput_override); | |
1112 | ||
1113 | return sprintf(buff, "%u.%u MBit\n", tp_override / 10, | |
1114 | tp_override % 10); | |
1115 | } | |
1116 | ||
1117 | #endif | |
1118 | ||
347c80f0 SE |
1119 | static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface, |
1120 | batadv_store_mesh_iface); | |
1121 | static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL); | |
7f136cd4 LL |
1122 | #ifdef CONFIG_BATMAN_ADV_BATMAN_V |
1123 | BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, S_IRUGO | S_IWUSR, | |
1124 | 2 * BATADV_JITTER, INT_MAX, NULL); | |
0b5ecc68 AQ |
1125 | static BATADV_ATTR(throughput_override, S_IRUGO | S_IWUSR, |
1126 | batadv_show_throughput_override, | |
1127 | batadv_store_throughput_override); | |
7f136cd4 | 1128 | #endif |
c6c8fea2 | 1129 | |
b4d66b87 | 1130 | static struct batadv_attribute *batadv_batman_attrs[] = { |
0ff9b86f SE |
1131 | &batadv_attr_mesh_iface, |
1132 | &batadv_attr_iface_status, | |
7f136cd4 LL |
1133 | #ifdef CONFIG_BATMAN_ADV_BATMAN_V |
1134 | &batadv_attr_elp_interval, | |
0b5ecc68 | 1135 | &batadv_attr_throughput_override, |
7f136cd4 | 1136 | #endif |
c6c8fea2 SE |
1137 | NULL, |
1138 | }; | |
1139 | ||
5853e22c | 1140 | int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev) |
c6c8fea2 SE |
1141 | { |
1142 | struct kobject *hardif_kobject = &dev->dev.kobj; | |
b4d66b87 | 1143 | struct batadv_attribute **bat_attr; |
c6c8fea2 SE |
1144 | int err; |
1145 | ||
036cbfeb SE |
1146 | *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR, |
1147 | hardif_kobject); | |
c6c8fea2 SE |
1148 | |
1149 | if (!*hardif_obj) { | |
3e34819e | 1150 | batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name, |
036cbfeb | 1151 | BATADV_SYSFS_IF_BAT_SUBDIR); |
c6c8fea2 SE |
1152 | goto out; |
1153 | } | |
1154 | ||
0ff9b86f | 1155 | for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) { |
c6c8fea2 SE |
1156 | err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr)); |
1157 | if (err) { | |
3e34819e | 1158 | batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n", |
036cbfeb | 1159 | dev->name, BATADV_SYSFS_IF_BAT_SUBDIR, |
3e34819e | 1160 | ((*bat_attr)->attr).name); |
c6c8fea2 SE |
1161 | goto rem_attr; |
1162 | } | |
1163 | } | |
1164 | ||
1165 | return 0; | |
1166 | ||
1167 | rem_attr: | |
0ff9b86f | 1168 | for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) |
c6c8fea2 SE |
1169 | sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr)); |
1170 | out: | |
1171 | return -ENOMEM; | |
1172 | } | |
1173 | ||
5853e22c | 1174 | void batadv_sysfs_del_hardif(struct kobject **hardif_obj) |
c6c8fea2 | 1175 | { |
f4acb108 SE |
1176 | kobject_uevent(*hardif_obj, KOBJ_REMOVE); |
1177 | kobject_del(*hardif_obj); | |
c6c8fea2 SE |
1178 | kobject_put(*hardif_obj); |
1179 | *hardif_obj = NULL; | |
1180 | } | |
c6bda689 | 1181 | |
56303d34 | 1182 | int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type, |
39c75a51 | 1183 | enum batadv_uev_action action, const char *data) |
c6bda689 | 1184 | { |
5346c35e | 1185 | int ret = -ENOMEM; |
c6bda689 AQ |
1186 | struct kobject *bat_kobj; |
1187 | char *uevent_env[4] = { NULL, NULL, NULL, NULL }; | |
1188 | ||
736292c2 | 1189 | bat_kobj = &bat_priv->soft_iface->dev.kobj; |
c6bda689 | 1190 | |
b98fe24c HS |
1191 | uevent_env[0] = kasprintf(GFP_ATOMIC, |
1192 | "%s%s", BATADV_UEV_TYPE_VAR, | |
1193 | batadv_uev_type_str[type]); | |
c6bda689 AQ |
1194 | if (!uevent_env[0]) |
1195 | goto out; | |
1196 | ||
b98fe24c HS |
1197 | uevent_env[1] = kasprintf(GFP_ATOMIC, |
1198 | "%s%s", BATADV_UEV_ACTION_VAR, | |
1199 | batadv_uev_action_str[action]); | |
c6bda689 AQ |
1200 | if (!uevent_env[1]) |
1201 | goto out; | |
1202 | ||
c6bda689 | 1203 | /* If the event is DEL, ignore the data field */ |
39c75a51 | 1204 | if (action != BATADV_UEV_DEL) { |
b98fe24c HS |
1205 | uevent_env[2] = kasprintf(GFP_ATOMIC, |
1206 | "%s%s", BATADV_UEV_DATA_VAR, data); | |
c6bda689 AQ |
1207 | if (!uevent_env[2]) |
1208 | goto out; | |
c6bda689 AQ |
1209 | } |
1210 | ||
1211 | ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env); | |
1212 | out: | |
1213 | kfree(uevent_env[0]); | |
1214 | kfree(uevent_env[1]); | |
1215 | kfree(uevent_env[2]); | |
1216 | ||
c6bda689 | 1217 | if (ret) |
39c75a51 | 1218 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 1219 | "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n", |
0ff9b86f SE |
1220 | batadv_uev_type_str[type], |
1221 | batadv_uev_action_str[action], | |
39c75a51 | 1222 | (action == BATADV_UEV_DEL ? "NULL" : data), ret); |
c6bda689 AQ |
1223 | return ret; |
1224 | } |