1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2019 B.A.T.M.A.N. contributors:
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>
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"
42 #include "network-coding.h"
43 #include "soft-interface.h"
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
);
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
);
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
;
102 hlist_for_each_entry_rcu(vlan_tmp
, &bat_priv
->softif_vlan_list
, list
) {
103 if (vlan_tmp
->kobj
!= obj
)
106 if (!kref_get_unless_zero(&vlan_tmp
->refcount
))
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), \
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), \
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, \
140 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
141 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
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); \
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, \
179 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
180 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
183 batadv_sysfs_deprecated(attr); \
184 length = __batadv_store_uint_attr(buff, count, _min, _max, \
186 &bat_priv->_var, net_dev, \
189 batadv_netlink_notify_mesh(bat_priv); \
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, \
218 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
219 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
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); \
227 batadv_netlink_notify_vlan(bat_priv, vlan); \
229 batadv_netlink_notify_mesh(bat_priv); \
231 batadv_softif_vlan_put(vlan); \
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, \
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); \
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, \
263 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
264 struct batadv_hard_iface *hard_iface; \
265 struct batadv_priv *bat_priv; \
268 batadv_sysfs_deprecated(attr); \
269 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
273 length = __batadv_store_uint_attr(buff, count, _min, _max, \
276 hard_iface->soft_iface, \
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); \
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; \
296 batadv_sysfs_deprecated(attr); \
297 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
301 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
303 batadv_hardif_put(hard_iface); \
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, \
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
,
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))
334 if ((strncmp(buff
, "0", 2) == 0) ||
335 (strncmp(buff
, "disable", 8) == 0) ||
336 (strncmp(buff
, "disabled", 9) == 0))
340 batadv_info(net_dev
, "%s: Invalid parameter received: %s\n",
345 if (atomic_read(attr
) == enabled
)
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");
354 atomic_set(attr
, (unsigned int)enabled
);
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
)
367 ret
= batadv_store_bool_attr(buff
, count
, net_dev
, attr
->name
,
368 attr_store
, &changed
);
369 if (post_func
&& changed
)
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
,
382 char ifname
[IFNAMSIZ
+ 3] = "";
383 unsigned long uint_val
;
386 ret
= kstrtoul(buff
, 10, &uint_val
);
388 batadv_info(net_dev
, "%s: Invalid parameter received: %s\n",
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
);
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
);
405 if (atomic_read(attr
) == uint_val
)
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
);
418 static ssize_t
__batadv_store_uint_attr(const char *buff
, size_t count
,
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
)
428 ret
= batadv_store_uint_attr(buff
, count
, net_dev
, slave_dev
,
429 attr
->name
, min
, max
, attr_store
);
430 if (post_func
&& 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
,
455 struct batadv_priv
*bat_priv
= batadv_kobj_to_batpriv(kobj
);
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
)
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
);
472 case BATADV_GW_MODE_SERVER
:
473 bytes_written
= sprintf(buff
, "%s\n",
474 BATADV_GW_MODE_SERVER_NAME
);
477 bytes_written
= sprintf(buff
, "%s\n",
478 BATADV_GW_MODE_OFF_NAME
);
482 return bytes_written
;
485 static ssize_t
batadv_store_gw_mode(struct kobject
*kobj
,
486 struct attribute
*attr
, char *buff
,
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
)
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) {
520 "Invalid parameter for 'gw mode' setting received: %s\n",
525 if (atomic_read(&bat_priv
->gw
.mode
) == gw_mode_tmp
)
528 switch (atomic_read(&bat_priv
->gw
.mode
)) {
529 case BATADV_GW_MODE_CLIENT
:
530 curr_gw_mode_str
= BATADV_GW_MODE_CLIENT_NAME
;
532 case BATADV_GW_MODE_SERVER
:
533 curr_gw_mode_str
= BATADV_GW_MODE_SERVER_NAME
;
536 curr_gw_mode_str
= BATADV_GW_MODE_OFF_NAME
;
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
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
);
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
)
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
,
589 struct batadv_priv
*bat_priv
= batadv_kobj_to_batpriv(kobj
);
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
)
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
,
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
);
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
);
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
,
637 struct batadv_priv
*bat_priv
= batadv_kobj_to_batpriv(kobj
);
638 struct net_device
*net_dev
= batadv_kobj_to_netdev(kobj
);
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
);
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
,
686 struct net_device
*net_dev
= batadv_kobj_to_netdev(kobj
);
687 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
691 batadv_sysfs_deprecated(attr
);
693 /* parse the mask if it has been specified, otherwise assume the mask is
694 * the biggest possible
697 mask_ptr
= strchr(buff
, '/');
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)
709 /* the mark can be entered in any base */
710 if (kstrtou32(buff
, 0, &mark
) < 0)
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
;
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
);
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
);
731 #ifdef CONFIG_BATMAN_ADV_DAT
732 BATADV_ATTR_SIF_BOOL(distributed_arp_table
, 0644, batadv_dat_status_update
);
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
,
739 BATADV_ATTR_SIF_UINT(hop_penalty
, hop_penalty
, 0644, 0, BATADV_TQ_MAX_VALUE
,
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
);
748 #ifdef CONFIG_BATMAN_ADV_DEBUG
749 BATADV_ATTR_SIF_UINT(log_level
, log_level
, 0644, 0, BATADV_DBG_ALL
, NULL
);
751 #ifdef CONFIG_BATMAN_ADV_NC
752 BATADV_ATTR_SIF_BOOL(network_coding
, 0644, batadv_nc_status_update
);
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
,
763 #ifdef CONFIG_BATMAN_ADV_DAT
764 &batadv_attr_distributed_arp_table
,
766 #ifdef CONFIG_BATMAN_ADV_MCAST
767 &batadv_attr_multicast_mode
,
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
,
779 #ifdef CONFIG_BATMAN_ADV_NC
780 &batadv_attr_network_coding
,
782 &batadv_attr_isolation_mark
,
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
,
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
;
807 bat_priv
->mesh_obj
= kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR
,
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
);
815 for (bat_attr
= batadv_mesh_attrs
; *bat_attr
; ++bat_attr
) {
816 err
= sysfs_create_file(bat_priv
->mesh_obj
,
817 &((*bat_attr
)->attr
));
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
);
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
;
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
;
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
,
880 batadv_err(dev
, "Can't add sysfs directory: %s/%s\n",
881 dev
->name
, vlan_subdir
);
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
));
896 batadv_err(dev
, "Can't add sysfs file: %s/%s/%s\n",
897 dev
->name
, vlan_subdir
,
898 ((*bat_attr
)->attr
).name
);
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
);
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
);
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
;
948 batadv_sysfs_deprecated(attr
);
950 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
954 if (hard_iface
->if_status
== BATADV_IF_NOT_IN_USE
)
957 ifname
= hard_iface
->soft_iface
->name
;
959 length
= sprintf(buff
, "%s\n", ifname
);
961 batadv_hardif_put(hard_iface
);
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
;
986 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
990 if (strncmp(ifname
, "none", 4) == 0)
991 status_tmp
= BATADV_IF_NOT_IN_USE
;
993 status_tmp
= BATADV_IF_I_WANT_YOU
;
995 if (hard_iface
->if_status
== status_tmp
)
998 if (hard_iface
->soft_iface
&&
999 strncmp(hard_iface
->soft_iface
->name
, ifname
, IFNAMSIZ
) == 0)
1002 if (status_tmp
== BATADV_IF_NOT_IN_USE
) {
1003 batadv_hardif_disable_interface(hard_iface
,
1004 BATADV_IF_CLEANUP_AUTO
);
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
);
1015 batadv_hardif_put(hard_iface
);
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
;
1031 store_work
= container_of(work
, struct batadv_store_mesh_work
, work
);
1034 ret
= batadv_store_mesh_iface_finish(store_work
->net_dev
,
1035 store_work
->soft_iface_name
);
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
,
1043 dev_put(store_work
->net_dev
);
1047 static ssize_t
batadv_store_mesh_iface(struct kobject
*kobj
,
1048 struct attribute
*attr
, char *buff
,
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",
1065 store_work
= kmalloc(sizeof(*store_work
), GFP_KERNEL
);
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
);
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
;
1087 batadv_sysfs_deprecated(attr
);
1089 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
1093 switch (hard_iface
->if_status
) {
1094 case BATADV_IF_TO_BE_REMOVED
:
1095 length
= sprintf(buff
, "disabling\n");
1097 case BATADV_IF_INACTIVE
:
1098 length
= sprintf(buff
, "inactive\n");
1100 case BATADV_IF_ACTIVE
:
1101 length
= sprintf(buff
, "active\n");
1103 case BATADV_IF_TO_BE_ACTIVATED
:
1104 length
= sprintf(buff
, "enabling\n");
1106 case BATADV_IF_NOT_IN_USE
:
1108 length
= sprintf(buff
, "not in use\n");
1112 batadv_hardif_put(hard_iface
);
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
;
1137 u32 old_tp_override
;
1140 batadv_sysfs_deprecated(attr
);
1142 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
1146 if (buff
[count
- 1] == '\n')
1147 buff
[count
- 1] = '\0';
1149 ret
= batadv_parse_throughput(net_dev
, buff
, "throughput_override",
1154 old_tp_override
= atomic_read(&hard_iface
->bat_v
.throughput_override
);
1155 if (old_tp_override
== tp_override
)
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
);
1172 batadv_hardif_put(hard_iface
);
1176 static ssize_t
batadv_show_throughput_override(struct kobject
*kobj
,
1177 struct attribute
*attr
,
1180 struct net_device
*net_dev
= batadv_kobj_to_netdev(kobj
);
1181 struct batadv_hard_iface
*hard_iface
;
1184 batadv_sysfs_deprecated(attr
);
1186 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
1190 tp_override
= atomic_read(&hard_iface
->bat_v
.throughput_override
);
1192 return sprintf(buff
, "%u.%u MBit\n", tp_override
/ 10,
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
);
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
,
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
;
1231 *hardif_obj
= kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR
,
1235 batadv_err(dev
, "Can't add sysfs directory: %s/%s\n", dev
->name
,
1236 BATADV_SYSFS_IF_BAT_SUBDIR
);
1240 for (bat_attr
= batadv_batman_attrs
; *bat_attr
; ++bat_attr
) {
1241 err
= sysfs_create_file(*hardif_obj
, &((*bat_attr
)->attr
));
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
);
1253 for (bat_attr
= batadv_batman_attrs
; *bat_attr
; ++bat_attr
)
1254 sysfs_remove_file(*hardif_obj
, &((*bat_attr
)->attr
));
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
);