Merge tag 'mips_fixes_5.2_2' of git://git.kernel.org/pub/scm/linux/kernel/git/mips...
[linux-2.6/linux-2.6-arm.git] / net / batman-adv / sysfs.c
blob80fc3253c3368e3cc356176c5ba961542de0a8c9
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2019 B.A.T.M.A.N. contributors:
4 * Marek Lindner
5 */
7 #include "sysfs.h"
8 #include "main.h"
10 #include <asm/current.h>
11 #include <linux/atomic.h>
12 #include <linux/compiler.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/gfp.h>
16 #include <linux/if.h>
17 #include <linux/if_vlan.h>
18 #include <linux/kernel.h>
19 #include <linux/kobject.h>
20 #include <linux/kref.h>
21 #include <linux/netdevice.h>
22 #include <linux/printk.h>
23 #include <linux/rculist.h>
24 #include <linux/rcupdate.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/stddef.h>
29 #include <linux/string.h>
30 #include <linux/stringify.h>
31 #include <linux/workqueue.h>
32 #include <uapi/linux/batadv_packet.h>
33 #include <uapi/linux/batman_adv.h>
35 #include "bridge_loop_avoidance.h"
36 #include "distributed-arp-table.h"
37 #include "gateway_client.h"
38 #include "gateway_common.h"
39 #include "hard-interface.h"
40 #include "log.h"
41 #include "netlink.h"
42 #include "network-coding.h"
43 #include "soft-interface.h"
45 /**
46 * batadv_sysfs_deprecated() - Log use of deprecated batadv sysfs access
47 * @attr: attribute which was accessed
49 static void batadv_sysfs_deprecated(struct attribute *attr)
51 pr_warn_ratelimited(DEPRECATED "%s (pid %d) Use of sysfs file \"%s\".\nUse batadv genl family instead",
52 current->comm, task_pid_nr(current), attr->name);
55 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
57 struct device *dev = container_of(obj->parent, struct device, kobj);
59 return to_net_dev(dev);
62 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
64 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
66 return netdev_priv(net_dev);
69 /**
70 * batadv_vlan_kobj_to_batpriv() - convert a vlan kobj in the associated batpriv
71 * @obj: kobject to covert
73 * Return: the associated batadv_priv struct.
75 static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
77 /* VLAN specific attributes are located in the root sysfs folder if they
78 * refer to the untagged VLAN..
80 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
81 return batadv_kobj_to_batpriv(obj);
83 /* ..while the attributes for the tagged vlans are located in
84 * the in the corresponding "vlan%VID" subfolder
86 return batadv_kobj_to_batpriv(obj->parent);
89 /**
90 * batadv_kobj_to_vlan() - convert a kobj in the associated softif_vlan struct
91 * @bat_priv: the bat priv with all the soft interface information
92 * @obj: kobject to covert
94 * Return: the associated softif_vlan struct if found, NULL otherwise.
96 static struct batadv_softif_vlan *
97 batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
99 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
101 rcu_read_lock();
102 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
103 if (vlan_tmp->kobj != obj)
104 continue;
106 if (!kref_get_unless_zero(&vlan_tmp->refcount))
107 continue;
109 vlan = vlan_tmp;
110 break;
112 rcu_read_unlock();
114 return vlan;
117 /* Use this, if you have customized show and store functions for vlan attrs */
118 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
119 struct batadv_attribute batadv_attr_vlan_##_name = { \
120 .attr = {.name = __stringify(_name), \
121 .mode = _mode }, \
122 .show = _show, \
123 .store = _store, \
126 /* Use this, if you have customized show and store functions */
127 #define BATADV_ATTR(_name, _mode, _show, _store) \
128 struct batadv_attribute batadv_attr_##_name = { \
129 .attr = {.name = __stringify(_name), \
130 .mode = _mode }, \
131 .show = _show, \
132 .store = _store, \
135 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
136 ssize_t batadv_store_##_name(struct kobject *kobj, \
137 struct attribute *attr, char *buff, \
138 size_t count) \
140 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
141 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
142 ssize_t length; \
144 batadv_sysfs_deprecated(attr); \
145 length = __batadv_store_bool_attr(buff, count, _post_func, attr,\
146 &bat_priv->_name, net_dev); \
148 batadv_netlink_notify_mesh(bat_priv); \
150 return length; \
153 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
154 ssize_t batadv_show_##_name(struct kobject *kobj, \
155 struct attribute *attr, char *buff) \
157 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
159 batadv_sysfs_deprecated(attr); \
160 return sprintf(buff, "%s\n", \
161 atomic_read(&bat_priv->_name) == 0 ? \
162 "disabled" : "enabled"); \
165 /* Use this, if you are going to turn a [name] in the soft-interface
166 * (bat_priv) on or off
168 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
169 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
170 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
171 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
172 batadv_store_##_name)
174 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
175 ssize_t batadv_store_##_name(struct kobject *kobj, \
176 struct attribute *attr, char *buff, \
177 size_t count) \
179 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
180 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
181 ssize_t length; \
183 batadv_sysfs_deprecated(attr); \
184 length = __batadv_store_uint_attr(buff, count, _min, _max, \
185 _post_func, attr, \
186 &bat_priv->_var, net_dev, \
187 NULL); \
189 batadv_netlink_notify_mesh(bat_priv); \
191 return length; \
194 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
195 ssize_t batadv_show_##_name(struct kobject *kobj, \
196 struct attribute *attr, char *buff) \
198 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
200 batadv_sysfs_deprecated(attr); \
201 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
204 /* Use this, if you are going to set [name] in the soft-interface
205 * (bat_priv) to an unsigned integer value
207 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
208 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
209 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
210 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
211 batadv_store_##_name)
213 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
214 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
215 struct attribute *attr, char *buff, \
216 size_t count) \
218 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
219 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
220 kobj); \
221 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
222 attr, &vlan->_name, \
223 bat_priv->soft_iface); \
225 batadv_sysfs_deprecated(attr); \
226 if (vlan->vid) \
227 batadv_netlink_notify_vlan(bat_priv, vlan); \
228 else \
229 batadv_netlink_notify_mesh(bat_priv); \
231 batadv_softif_vlan_put(vlan); \
232 return res; \
235 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
236 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
237 struct attribute *attr, char *buff) \
239 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
240 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
241 kobj); \
242 size_t res = sprintf(buff, "%s\n", \
243 atomic_read(&vlan->_name) == 0 ? \
244 "disabled" : "enabled"); \
246 batadv_sysfs_deprecated(attr); \
247 batadv_softif_vlan_put(vlan); \
248 return res; \
251 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
252 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
253 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
254 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
255 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
256 batadv_store_vlan_##_name)
258 #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
259 ssize_t batadv_store_##_name(struct kobject *kobj, \
260 struct attribute *attr, char *buff, \
261 size_t count) \
263 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
264 struct batadv_hard_iface *hard_iface; \
265 struct batadv_priv *bat_priv; \
266 ssize_t length; \
268 batadv_sysfs_deprecated(attr); \
269 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
270 if (!hard_iface) \
271 return 0; \
273 length = __batadv_store_uint_attr(buff, count, _min, _max, \
274 _post_func, attr, \
275 &hard_iface->_var, \
276 hard_iface->soft_iface, \
277 net_dev); \
279 if (hard_iface->soft_iface) { \
280 bat_priv = netdev_priv(hard_iface->soft_iface); \
281 batadv_netlink_notify_hardif(bat_priv, hard_iface); \
284 batadv_hardif_put(hard_iface); \
285 return length; \
288 #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
289 ssize_t batadv_show_##_name(struct kobject *kobj, \
290 struct attribute *attr, char *buff) \
292 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
293 struct batadv_hard_iface *hard_iface; \
294 ssize_t length; \
296 batadv_sysfs_deprecated(attr); \
297 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
298 if (!hard_iface) \
299 return 0; \
301 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
303 batadv_hardif_put(hard_iface); \
304 return length; \
307 /* Use this, if you are going to set [name] in hard_iface to an
308 * unsigned integer value
310 #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
311 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
312 _max, _post_func) \
313 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
314 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
315 batadv_store_##_name)
317 static int batadv_store_bool_attr(char *buff, size_t count,
318 struct net_device *net_dev,
319 const char *attr_name, atomic_t *attr,
320 bool *changed)
322 int enabled = -1;
324 *changed = false;
326 if (buff[count - 1] == '\n')
327 buff[count - 1] = '\0';
329 if ((strncmp(buff, "1", 2) == 0) ||
330 (strncmp(buff, "enable", 7) == 0) ||
331 (strncmp(buff, "enabled", 8) == 0))
332 enabled = 1;
334 if ((strncmp(buff, "0", 2) == 0) ||
335 (strncmp(buff, "disable", 8) == 0) ||
336 (strncmp(buff, "disabled", 9) == 0))
337 enabled = 0;
339 if (enabled < 0) {
340 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
341 attr_name, buff);
342 return -EINVAL;
345 if (atomic_read(attr) == enabled)
346 return count;
348 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
349 atomic_read(attr) == 1 ? "enabled" : "disabled",
350 enabled == 1 ? "enabled" : "disabled");
352 *changed = true;
354 atomic_set(attr, (unsigned int)enabled);
355 return count;
358 static inline ssize_t
359 __batadv_store_bool_attr(char *buff, size_t count,
360 void (*post_func)(struct net_device *),
361 struct attribute *attr,
362 atomic_t *attr_store, struct net_device *net_dev)
364 bool changed;
365 int ret;
367 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
368 attr_store, &changed);
369 if (post_func && changed)
370 post_func(net_dev);
372 return ret;
375 static int batadv_store_uint_attr(const char *buff, size_t count,
376 struct net_device *net_dev,
377 struct net_device *slave_dev,
378 const char *attr_name,
379 unsigned int min, unsigned int max,
380 atomic_t *attr)
382 char ifname[IFNAMSIZ + 3] = "";
383 unsigned long uint_val;
384 int ret;
386 ret = kstrtoul(buff, 10, &uint_val);
387 if (ret) {
388 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
389 attr_name, buff);
390 return -EINVAL;
393 if (uint_val < min) {
394 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
395 attr_name, uint_val, min);
396 return -EINVAL;
399 if (uint_val > max) {
400 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
401 attr_name, uint_val, max);
402 return -EINVAL;
405 if (atomic_read(attr) == uint_val)
406 return count;
408 if (slave_dev)
409 snprintf(ifname, sizeof(ifname), "%s: ", slave_dev->name);
411 batadv_info(net_dev, "%s: %sChanging from: %i to: %lu\n",
412 attr_name, ifname, atomic_read(attr), uint_val);
414 atomic_set(attr, uint_val);
415 return count;
418 static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
419 int min, int max,
420 void (*post_func)(struct net_device *),
421 const struct attribute *attr,
422 atomic_t *attr_store,
423 struct net_device *net_dev,
424 struct net_device *slave_dev)
426 int ret;
428 ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
429 attr->name, min, max, attr_store);
430 if (post_func && ret)
431 post_func(net_dev);
433 return ret;
436 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
437 struct attribute *attr, char *buff)
439 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
441 batadv_sysfs_deprecated(attr);
442 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
445 static void batadv_post_gw_reselect(struct net_device *net_dev)
447 struct batadv_priv *bat_priv = netdev_priv(net_dev);
449 batadv_gw_reselect(bat_priv);
452 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
453 char *buff)
455 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
456 int bytes_written;
458 batadv_sysfs_deprecated(attr);
460 /* GW mode is not available if the routing algorithm in use does not
461 * implement the GW API
463 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
464 !bat_priv->algo_ops->gw.is_eligible)
465 return -ENOENT;
467 switch (atomic_read(&bat_priv->gw.mode)) {
468 case BATADV_GW_MODE_CLIENT:
469 bytes_written = sprintf(buff, "%s\n",
470 BATADV_GW_MODE_CLIENT_NAME);
471 break;
472 case BATADV_GW_MODE_SERVER:
473 bytes_written = sprintf(buff, "%s\n",
474 BATADV_GW_MODE_SERVER_NAME);
475 break;
476 default:
477 bytes_written = sprintf(buff, "%s\n",
478 BATADV_GW_MODE_OFF_NAME);
479 break;
482 return bytes_written;
485 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
486 struct attribute *attr, char *buff,
487 size_t count)
489 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
490 struct batadv_priv *bat_priv = netdev_priv(net_dev);
491 char *curr_gw_mode_str;
492 int gw_mode_tmp = -1;
494 batadv_sysfs_deprecated(attr);
496 /* toggling GW mode is allowed only if the routing algorithm in use
497 * provides the GW API
499 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
500 !bat_priv->algo_ops->gw.is_eligible)
501 return -EINVAL;
503 if (buff[count - 1] == '\n')
504 buff[count - 1] = '\0';
506 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
507 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
508 gw_mode_tmp = BATADV_GW_MODE_OFF;
510 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
511 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
512 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
514 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
515 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
516 gw_mode_tmp = BATADV_GW_MODE_SERVER;
518 if (gw_mode_tmp < 0) {
519 batadv_info(net_dev,
520 "Invalid parameter for 'gw mode' setting received: %s\n",
521 buff);
522 return -EINVAL;
525 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
526 return count;
528 switch (atomic_read(&bat_priv->gw.mode)) {
529 case BATADV_GW_MODE_CLIENT:
530 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
531 break;
532 case BATADV_GW_MODE_SERVER:
533 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
534 break;
535 default:
536 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
537 break;
540 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
541 curr_gw_mode_str, buff);
543 /* Invoking batadv_gw_reselect() is not enough to really de-select the
544 * current GW. It will only instruct the gateway client code to perform
545 * a re-election the next time that this is needed.
547 * When gw client mode is being switched off the current GW must be
548 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
549 * client mode re-activation. This is operation is performed in
550 * batadv_gw_check_client_stop().
552 batadv_gw_reselect(bat_priv);
553 /* always call batadv_gw_check_client_stop() before changing the gateway
554 * state
556 batadv_gw_check_client_stop(bat_priv);
557 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
558 batadv_gw_tvlv_container_update(bat_priv);
560 batadv_netlink_notify_mesh(bat_priv);
562 return count;
565 static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
566 struct attribute *attr, char *buff)
568 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
570 batadv_sysfs_deprecated(attr);
572 /* GW selection class is not available if the routing algorithm in use
573 * does not implement the GW API
575 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
576 !bat_priv->algo_ops->gw.is_eligible)
577 return -ENOENT;
579 if (bat_priv->algo_ops->gw.show_sel_class)
580 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
582 return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
585 static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
586 struct attribute *attr, char *buff,
587 size_t count)
589 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
590 ssize_t length;
592 batadv_sysfs_deprecated(attr);
594 /* setting the GW selection class is allowed only if the routing
595 * algorithm in use implements the GW API
597 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
598 !bat_priv->algo_ops->gw.is_eligible)
599 return -EINVAL;
601 if (buff[count - 1] == '\n')
602 buff[count - 1] = '\0';
604 if (bat_priv->algo_ops->gw.store_sel_class)
605 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
606 count);
608 length = __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
609 batadv_post_gw_reselect, attr,
610 &bat_priv->gw.sel_class,
611 bat_priv->soft_iface, NULL);
613 batadv_netlink_notify_mesh(bat_priv);
615 return length;
618 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
619 struct attribute *attr, char *buff)
621 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
622 u32 down, up;
624 batadv_sysfs_deprecated(attr);
626 down = atomic_read(&bat_priv->gw.bandwidth_down);
627 up = atomic_read(&bat_priv->gw.bandwidth_up);
629 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
630 down % 10, up / 10, up % 10);
633 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
634 struct attribute *attr, char *buff,
635 size_t count)
637 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
638 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
639 ssize_t length;
641 batadv_sysfs_deprecated(attr);
643 if (buff[count - 1] == '\n')
644 buff[count - 1] = '\0';
646 length = batadv_gw_bandwidth_set(net_dev, buff, count);
648 batadv_netlink_notify_mesh(bat_priv);
650 return length;
654 * batadv_show_isolation_mark() - print the current isolation mark/mask
655 * @kobj: kobject representing the private mesh sysfs directory
656 * @attr: the batman-adv attribute the user is interacting with
657 * @buff: the buffer that will contain the data to send back to the user
659 * Return: the number of bytes written into 'buff' on success or a negative
660 * error code in case of failure
662 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
663 struct attribute *attr, char *buff)
665 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
667 batadv_sysfs_deprecated(attr);
668 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
669 bat_priv->isolation_mark_mask);
673 * batadv_store_isolation_mark() - parse and store the isolation mark/mask
674 * entered by the user
675 * @kobj: kobject representing the private mesh sysfs directory
676 * @attr: the batman-adv attribute the user is interacting with
677 * @buff: the buffer containing the user data
678 * @count: number of bytes in the buffer
680 * Return: 'count' on success or a negative error code in case of failure
682 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
683 struct attribute *attr, char *buff,
684 size_t count)
686 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
687 struct batadv_priv *bat_priv = netdev_priv(net_dev);
688 u32 mark, mask;
689 char *mask_ptr;
691 batadv_sysfs_deprecated(attr);
693 /* parse the mask if it has been specified, otherwise assume the mask is
694 * the biggest possible
696 mask = 0xFFFFFFFF;
697 mask_ptr = strchr(buff, '/');
698 if (mask_ptr) {
699 *mask_ptr = '\0';
700 mask_ptr++;
702 /* the mask must be entered in hex base as it is going to be a
703 * bitmask and not a prefix length
705 if (kstrtou32(mask_ptr, 16, &mask) < 0)
706 return -EINVAL;
709 /* the mark can be entered in any base */
710 if (kstrtou32(buff, 0, &mark) < 0)
711 return -EINVAL;
713 bat_priv->isolation_mark_mask = mask;
714 /* erase bits not covered by the mask */
715 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
717 batadv_info(net_dev,
718 "New skb mark for extended isolation: %#.8x/%#.8x\n",
719 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
721 batadv_netlink_notify_mesh(bat_priv);
723 return count;
726 BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
727 BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
728 #ifdef CONFIG_BATMAN_ADV_BLA
729 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
730 #endif
731 #ifdef CONFIG_BATMAN_ADV_DAT
732 BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
733 #endif
734 BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
735 static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
736 static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
737 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
738 INT_MAX, NULL);
739 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
740 NULL);
741 static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
742 batadv_store_gw_sel_class);
743 static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
744 batadv_store_gw_bwidth);
745 #ifdef CONFIG_BATMAN_ADV_MCAST
746 BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
747 #endif
748 #ifdef CONFIG_BATMAN_ADV_DEBUG
749 BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
750 #endif
751 #ifdef CONFIG_BATMAN_ADV_NC
752 BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
753 #endif
754 static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
755 batadv_store_isolation_mark);
757 static struct batadv_attribute *batadv_mesh_attrs[] = {
758 &batadv_attr_aggregated_ogms,
759 &batadv_attr_bonding,
760 #ifdef CONFIG_BATMAN_ADV_BLA
761 &batadv_attr_bridge_loop_avoidance,
762 #endif
763 #ifdef CONFIG_BATMAN_ADV_DAT
764 &batadv_attr_distributed_arp_table,
765 #endif
766 #ifdef CONFIG_BATMAN_ADV_MCAST
767 &batadv_attr_multicast_mode,
768 #endif
769 &batadv_attr_fragmentation,
770 &batadv_attr_routing_algo,
771 &batadv_attr_gw_mode,
772 &batadv_attr_orig_interval,
773 &batadv_attr_hop_penalty,
774 &batadv_attr_gw_sel_class,
775 &batadv_attr_gw_bandwidth,
776 #ifdef CONFIG_BATMAN_ADV_DEBUG
777 &batadv_attr_log_level,
778 #endif
779 #ifdef CONFIG_BATMAN_ADV_NC
780 &batadv_attr_network_coding,
781 #endif
782 &batadv_attr_isolation_mark,
783 NULL,
786 BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
788 /* array of vlan specific sysfs attributes */
789 static struct batadv_attribute *batadv_vlan_attrs[] = {
790 &batadv_attr_vlan_ap_isolation,
791 NULL,
795 * batadv_sysfs_add_meshif() - Add soft interface specific sysfs entries
796 * @dev: netdev struct of the soft interface
798 * Return: 0 on success or negative error number in case of failure
800 int batadv_sysfs_add_meshif(struct net_device *dev)
802 struct kobject *batif_kobject = &dev->dev.kobj;
803 struct batadv_priv *bat_priv = netdev_priv(dev);
804 struct batadv_attribute **bat_attr;
805 int err;
807 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
808 batif_kobject);
809 if (!bat_priv->mesh_obj) {
810 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
811 BATADV_SYSFS_IF_MESH_SUBDIR);
812 goto out;
815 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
816 err = sysfs_create_file(bat_priv->mesh_obj,
817 &((*bat_attr)->attr));
818 if (err) {
819 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
820 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
821 ((*bat_attr)->attr).name);
822 goto rem_attr;
826 return 0;
828 rem_attr:
829 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
830 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
832 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
833 kobject_del(bat_priv->mesh_obj);
834 kobject_put(bat_priv->mesh_obj);
835 bat_priv->mesh_obj = NULL;
836 out:
837 return -ENOMEM;
841 * batadv_sysfs_del_meshif() - Remove soft interface specific sysfs entries
842 * @dev: netdev struct of the soft interface
844 void batadv_sysfs_del_meshif(struct net_device *dev)
846 struct batadv_priv *bat_priv = netdev_priv(dev);
847 struct batadv_attribute **bat_attr;
849 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
850 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
852 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
853 kobject_del(bat_priv->mesh_obj);
854 kobject_put(bat_priv->mesh_obj);
855 bat_priv->mesh_obj = NULL;
859 * batadv_sysfs_add_vlan() - add all the needed sysfs objects for the new vlan
860 * @dev: netdev of the mesh interface
861 * @vlan: private data of the newly added VLAN interface
863 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
865 int batadv_sysfs_add_vlan(struct net_device *dev,
866 struct batadv_softif_vlan *vlan)
868 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
869 struct batadv_priv *bat_priv = netdev_priv(dev);
870 struct batadv_attribute **bat_attr;
871 int err;
873 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
874 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
875 vlan->vid & VLAN_VID_MASK);
877 vlan->kobj = kobject_create_and_add(vlan_subdir,
878 bat_priv->mesh_obj);
879 if (!vlan->kobj) {
880 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
881 dev->name, vlan_subdir);
882 goto out;
884 } else {
885 /* the untagged LAN uses the root folder to store its "VLAN
886 * specific attributes"
888 vlan->kobj = bat_priv->mesh_obj;
889 kobject_get(bat_priv->mesh_obj);
892 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
893 err = sysfs_create_file(vlan->kobj,
894 &((*bat_attr)->attr));
895 if (err) {
896 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
897 dev->name, vlan_subdir,
898 ((*bat_attr)->attr).name);
899 goto rem_attr;
903 return 0;
905 rem_attr:
906 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
907 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
909 if (vlan->kobj != bat_priv->mesh_obj) {
910 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
911 kobject_del(vlan->kobj);
913 kobject_put(vlan->kobj);
914 vlan->kobj = NULL;
915 out:
916 return -ENOMEM;
920 * batadv_sysfs_del_vlan() - remove all the sysfs objects for a given VLAN
921 * @bat_priv: the bat priv with all the soft interface information
922 * @vlan: the private data of the VLAN to destroy
924 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
925 struct batadv_softif_vlan *vlan)
927 struct batadv_attribute **bat_attr;
929 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
930 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
932 if (vlan->kobj != bat_priv->mesh_obj) {
933 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
934 kobject_del(vlan->kobj);
936 kobject_put(vlan->kobj);
937 vlan->kobj = NULL;
940 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
941 struct attribute *attr, char *buff)
943 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
944 struct batadv_hard_iface *hard_iface;
945 ssize_t length;
946 const char *ifname;
948 batadv_sysfs_deprecated(attr);
950 hard_iface = batadv_hardif_get_by_netdev(net_dev);
951 if (!hard_iface)
952 return 0;
954 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
955 ifname = "none";
956 else
957 ifname = hard_iface->soft_iface->name;
959 length = sprintf(buff, "%s\n", ifname);
961 batadv_hardif_put(hard_iface);
963 return length;
967 * batadv_store_mesh_iface_finish() - store new hardif mesh_iface state
968 * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
969 * @ifname: name of soft-interface to modify
971 * Changes the parts of the hard+soft interface which can not be modified under
972 * sysfs lock (to prevent deadlock situations).
974 * Return: 0 on success, 0 < on failure
976 static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
977 char ifname[IFNAMSIZ])
979 struct net *net = dev_net(net_dev);
980 struct batadv_hard_iface *hard_iface;
981 int status_tmp;
982 int ret = 0;
984 ASSERT_RTNL();
986 hard_iface = batadv_hardif_get_by_netdev(net_dev);
987 if (!hard_iface)
988 return 0;
990 if (strncmp(ifname, "none", 4) == 0)
991 status_tmp = BATADV_IF_NOT_IN_USE;
992 else
993 status_tmp = BATADV_IF_I_WANT_YOU;
995 if (hard_iface->if_status == status_tmp)
996 goto out;
998 if (hard_iface->soft_iface &&
999 strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)
1000 goto out;
1002 if (status_tmp == BATADV_IF_NOT_IN_USE) {
1003 batadv_hardif_disable_interface(hard_iface,
1004 BATADV_IF_CLEANUP_AUTO);
1005 goto out;
1008 /* if the interface already is in use */
1009 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
1010 batadv_hardif_disable_interface(hard_iface,
1011 BATADV_IF_CLEANUP_AUTO);
1013 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
1014 out:
1015 batadv_hardif_put(hard_iface);
1016 return ret;
1020 * batadv_store_mesh_iface_work() - store new hardif mesh_iface state
1021 * @work: work queue item
1023 * Changes the parts of the hard+soft interface which can not be modified under
1024 * sysfs lock (to prevent deadlock situations).
1026 static void batadv_store_mesh_iface_work(struct work_struct *work)
1028 struct batadv_store_mesh_work *store_work;
1029 int ret;
1031 store_work = container_of(work, struct batadv_store_mesh_work, work);
1033 rtnl_lock();
1034 ret = batadv_store_mesh_iface_finish(store_work->net_dev,
1035 store_work->soft_iface_name);
1036 rtnl_unlock();
1038 if (ret < 0)
1039 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
1040 store_work->soft_iface_name, store_work->net_dev->name,
1041 ret);
1043 dev_put(store_work->net_dev);
1044 kfree(store_work);
1047 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
1048 struct attribute *attr, char *buff,
1049 size_t count)
1051 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1052 struct batadv_store_mesh_work *store_work;
1054 batadv_sysfs_deprecated(attr);
1056 if (buff[count - 1] == '\n')
1057 buff[count - 1] = '\0';
1059 if (strlen(buff) >= IFNAMSIZ) {
1060 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
1061 buff);
1062 return -EINVAL;
1065 store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
1066 if (!store_work)
1067 return -ENOMEM;
1069 dev_hold(net_dev);
1070 INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1071 store_work->net_dev = net_dev;
1072 strlcpy(store_work->soft_iface_name, buff,
1073 sizeof(store_work->soft_iface_name));
1075 queue_work(batadv_event_workqueue, &store_work->work);
1077 return count;
1080 static ssize_t batadv_show_iface_status(struct kobject *kobj,
1081 struct attribute *attr, char *buff)
1083 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1084 struct batadv_hard_iface *hard_iface;
1085 ssize_t length;
1087 batadv_sysfs_deprecated(attr);
1089 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1090 if (!hard_iface)
1091 return 0;
1093 switch (hard_iface->if_status) {
1094 case BATADV_IF_TO_BE_REMOVED:
1095 length = sprintf(buff, "disabling\n");
1096 break;
1097 case BATADV_IF_INACTIVE:
1098 length = sprintf(buff, "inactive\n");
1099 break;
1100 case BATADV_IF_ACTIVE:
1101 length = sprintf(buff, "active\n");
1102 break;
1103 case BATADV_IF_TO_BE_ACTIVATED:
1104 length = sprintf(buff, "enabling\n");
1105 break;
1106 case BATADV_IF_NOT_IN_USE:
1107 default:
1108 length = sprintf(buff, "not in use\n");
1109 break;
1112 batadv_hardif_put(hard_iface);
1114 return length;
1117 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1120 * batadv_store_throughput_override() - parse and store throughput override
1121 * entered by the user
1122 * @kobj: kobject representing the private mesh sysfs directory
1123 * @attr: the batman-adv attribute the user is interacting with
1124 * @buff: the buffer containing the user data
1125 * @count: number of bytes in the buffer
1127 * Return: 'count' on success or a negative error code in case of failure
1129 static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1130 struct attribute *attr,
1131 char *buff, size_t count)
1133 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1134 struct batadv_hard_iface *hard_iface;
1135 struct batadv_priv *bat_priv;
1136 u32 tp_override;
1137 u32 old_tp_override;
1138 bool ret;
1140 batadv_sysfs_deprecated(attr);
1142 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1143 if (!hard_iface)
1144 return -EINVAL;
1146 if (buff[count - 1] == '\n')
1147 buff[count - 1] = '\0';
1149 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1150 &tp_override);
1151 if (!ret)
1152 return count;
1154 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1155 if (old_tp_override == tp_override)
1156 goto out;
1158 batadv_info(hard_iface->soft_iface,
1159 "%s: %s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1160 "throughput_override", net_dev->name,
1161 old_tp_override / 10, old_tp_override % 10,
1162 tp_override / 10, tp_override % 10);
1164 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1166 if (hard_iface->soft_iface) {
1167 bat_priv = netdev_priv(hard_iface->soft_iface);
1168 batadv_netlink_notify_hardif(bat_priv, hard_iface);
1171 out:
1172 batadv_hardif_put(hard_iface);
1173 return count;
1176 static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1177 struct attribute *attr,
1178 char *buff)
1180 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1181 struct batadv_hard_iface *hard_iface;
1182 u32 tp_override;
1184 batadv_sysfs_deprecated(attr);
1186 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1187 if (!hard_iface)
1188 return -EINVAL;
1190 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1192 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1193 tp_override % 10);
1196 #endif
1198 static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
1199 batadv_store_mesh_iface);
1200 static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
1201 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1202 BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
1203 2 * BATADV_JITTER, INT_MAX, NULL);
1204 static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
1205 batadv_store_throughput_override);
1206 #endif
1208 static struct batadv_attribute *batadv_batman_attrs[] = {
1209 &batadv_attr_mesh_iface,
1210 &batadv_attr_iface_status,
1211 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1212 &batadv_attr_elp_interval,
1213 &batadv_attr_throughput_override,
1214 #endif
1215 NULL,
1219 * batadv_sysfs_add_hardif() - Add hard interface specific sysfs entries
1220 * @hardif_obj: address where to store the pointer to new sysfs folder
1221 * @dev: netdev struct of the hard interface
1223 * Return: 0 on success or negative error number in case of failure
1225 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
1227 struct kobject *hardif_kobject = &dev->dev.kobj;
1228 struct batadv_attribute **bat_attr;
1229 int err;
1231 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1232 hardif_kobject);
1234 if (!*hardif_obj) {
1235 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
1236 BATADV_SYSFS_IF_BAT_SUBDIR);
1237 goto out;
1240 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
1241 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1242 if (err) {
1243 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
1244 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
1245 ((*bat_attr)->attr).name);
1246 goto rem_attr;
1250 return 0;
1252 rem_attr:
1253 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
1254 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1255 out:
1256 return -ENOMEM;
1260 * batadv_sysfs_del_hardif() - Remove hard interface specific sysfs entries
1261 * @hardif_obj: address to the pointer to which stores batman-adv sysfs folder
1262 * of the hard interface
1264 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
1266 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1267 kobject_del(*hardif_obj);
1268 kobject_put(*hardif_obj);
1269 *hardif_obj = NULL;