1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2019 B.A.T.M.A.N. contributors:
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <linux/atomic.h>
23 #include <linux/compiler.h>
24 #include <linux/device.h>
25 #include <linux/errno.h>
26 #include <linux/gfp.h>
28 #include <linux/if_vlan.h>
29 #include <linux/kernel.h>
30 #include <linux/kobject.h>
31 #include <linux/kref.h>
32 #include <linux/netdevice.h>
33 #include <linux/printk.h>
34 #include <linux/rculist.h>
35 #include <linux/rcupdate.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/slab.h>
38 #include <linux/stddef.h>
39 #include <linux/string.h>
40 #include <linux/stringify.h>
41 #include <linux/workqueue.h>
42 #include <uapi/linux/batadv_packet.h>
43 #include <uapi/linux/batman_adv.h>
45 #include "bridge_loop_avoidance.h"
46 #include "distributed-arp-table.h"
47 #include "gateway_client.h"
48 #include "gateway_common.h"
49 #include "hard-interface.h"
52 #include "network-coding.h"
53 #include "soft-interface.h"
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 #define BATADV_UEV_TYPE_VAR "BATTYPE="
118 #define BATADV_UEV_ACTION_VAR "BATACTION="
119 #define BATADV_UEV_DATA_VAR "BATDATA="
121 static char *batadv_uev_action_str[] = {
128 static char *batadv_uev_type_str[] = {
133 /* Use this, if you have customized show and store functions for vlan attrs */
134 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
135 struct batadv_attribute batadv_attr_vlan_##_name = { \
136 .attr = {.name = __stringify(_name), \
142 /* Use this, if you have customized show and store functions */
143 #define BATADV_ATTR(_name, _mode, _show, _store) \
144 struct batadv_attribute batadv_attr_##_name = { \
145 .attr = {.name = __stringify(_name), \
151 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
152 ssize_t batadv_store_##_name(struct kobject *kobj, \
153 struct attribute *attr, char *buff, \
156 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
157 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
160 length = __batadv_store_bool_attr(buff, count, _post_func, attr,\
161 &bat_priv->_name, net_dev); \
163 batadv_netlink_notify_mesh(bat_priv); \
168 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
169 ssize_t batadv_show_##_name(struct kobject *kobj, \
170 struct attribute *attr, char *buff) \
172 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
174 return sprintf(buff, "%s\n", \
175 atomic_read(&bat_priv->_name) == 0 ? \
176 "disabled" : "enabled"); \
179 /* Use this, if you are going to turn a [name] in the soft-interface
180 * (bat_priv) on or off
182 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
183 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
184 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
185 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
186 batadv_store_##_name)
188 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
189 ssize_t batadv_store_##_name(struct kobject *kobj, \
190 struct attribute *attr, char *buff, \
193 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
194 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
197 length = __batadv_store_uint_attr(buff, count, _min, _max, \
199 &bat_priv->_var, net_dev, \
202 batadv_netlink_notify_mesh(bat_priv); \
207 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
208 ssize_t batadv_show_##_name(struct kobject *kobj, \
209 struct attribute *attr, char *buff) \
211 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
213 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
216 /* Use this, if you are going to set [name] in the soft-interface
217 * (bat_priv) to an unsigned integer value
219 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
220 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
221 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
222 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
223 batadv_store_##_name)
225 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
226 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
227 struct attribute *attr, char *buff, \
230 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
231 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
233 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
234 attr, &vlan->_name, \
235 bat_priv->soft_iface); \
238 batadv_netlink_notify_vlan(bat_priv, vlan); \
240 batadv_netlink_notify_mesh(bat_priv); \
242 batadv_softif_vlan_put(vlan); \
246 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
247 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
248 struct attribute *attr, char *buff) \
250 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
251 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
253 size_t res = sprintf(buff, "%s\n", \
254 atomic_read(&vlan->_name) == 0 ? \
255 "disabled" : "enabled"); \
257 batadv_softif_vlan_put(vlan); \
261 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
262 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
263 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
264 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
265 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
266 batadv_store_vlan_##_name)
268 #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
269 ssize_t batadv_store_##_name(struct kobject *kobj, \
270 struct attribute *attr, char *buff, \
273 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
274 struct batadv_hard_iface *hard_iface; \
275 struct batadv_priv *bat_priv; \
278 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
282 length = __batadv_store_uint_attr(buff, count, _min, _max, \
285 hard_iface->soft_iface, \
288 if (hard_iface->soft_iface) { \
289 bat_priv = netdev_priv(hard_iface->soft_iface); \
290 batadv_netlink_notify_hardif(bat_priv, hard_iface); \
293 batadv_hardif_put(hard_iface); \
297 #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
298 ssize_t batadv_show_##_name(struct kobject *kobj, \
299 struct attribute *attr, char *buff) \
301 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
302 struct batadv_hard_iface *hard_iface; \
305 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
309 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
311 batadv_hardif_put(hard_iface); \
315 /* Use this, if you are going to set [name] in hard_iface to an
316 * unsigned integer value
318 #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
319 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
321 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
322 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
323 batadv_store_##_name)
325 static int batadv_store_bool_attr(char *buff, size_t count,
326 struct net_device *net_dev,
327 const char *attr_name, atomic_t *attr,
334 if (buff[count - 1] == '\n')
335 buff[count - 1] = '\0';
337 if ((strncmp(buff, "1", 2) == 0) ||
338 (strncmp(buff, "enable", 7) == 0) ||
339 (strncmp(buff, "enabled", 8) == 0))
342 if ((strncmp(buff, "0", 2) == 0) ||
343 (strncmp(buff, "disable", 8) == 0) ||
344 (strncmp(buff, "disabled", 9) == 0))
348 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
353 if (atomic_read(attr) == enabled)
356 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
357 atomic_read(attr) == 1 ? "enabled" : "disabled",
358 enabled == 1 ? "enabled" : "disabled");
362 atomic_set(attr, (unsigned int)enabled);
366 static inline ssize_t
367 __batadv_store_bool_attr(char *buff, size_t count,
368 void (*post_func)(struct net_device *),
369 struct attribute *attr,
370 atomic_t *attr_store, struct net_device *net_dev)
375 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
376 attr_store, &changed);
377 if (post_func && changed)
383 static int batadv_store_uint_attr(const char *buff, size_t count,
384 struct net_device *net_dev,
385 struct net_device *slave_dev,
386 const char *attr_name,
387 unsigned int min, unsigned int max,
390 char ifname[IFNAMSIZ + 3] = "";
391 unsigned long uint_val;
394 ret = kstrtoul(buff, 10, &uint_val);
396 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
401 if (uint_val < min) {
402 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
403 attr_name, uint_val, min);
407 if (uint_val > max) {
408 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
409 attr_name, uint_val, max);
413 if (atomic_read(attr) == uint_val)
417 snprintf(ifname, sizeof(ifname), "%s: ", slave_dev->name);
419 batadv_info(net_dev, "%s: %sChanging from: %i to: %lu\n",
420 attr_name, ifname, atomic_read(attr), uint_val);
422 atomic_set(attr, uint_val);
426 static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
428 void (*post_func)(struct net_device *),
429 const struct attribute *attr,
430 atomic_t *attr_store,
431 struct net_device *net_dev,
432 struct net_device *slave_dev)
436 ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
437 attr->name, min, max, attr_store);
438 if (post_func && ret)
444 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
445 struct attribute *attr, char *buff)
447 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
449 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
452 static void batadv_post_gw_reselect(struct net_device *net_dev)
454 struct batadv_priv *bat_priv = netdev_priv(net_dev);
456 batadv_gw_reselect(bat_priv);
459 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
462 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
465 /* GW mode is not available if the routing algorithm in use does not
466 * implement the GW API
468 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
469 !bat_priv->algo_ops->gw.is_eligible)
472 switch (atomic_read(&bat_priv->gw.mode)) {
473 case BATADV_GW_MODE_CLIENT:
474 bytes_written = sprintf(buff, "%s\n",
475 BATADV_GW_MODE_CLIENT_NAME);
477 case BATADV_GW_MODE_SERVER:
478 bytes_written = sprintf(buff, "%s\n",
479 BATADV_GW_MODE_SERVER_NAME);
482 bytes_written = sprintf(buff, "%s\n",
483 BATADV_GW_MODE_OFF_NAME);
487 return bytes_written;
490 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
491 struct attribute *attr, char *buff,
494 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
495 struct batadv_priv *bat_priv = netdev_priv(net_dev);
496 char *curr_gw_mode_str;
497 int gw_mode_tmp = -1;
499 /* toggling GW mode is allowed only if the routing algorithm in use
500 * provides the GW API
502 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
503 !bat_priv->algo_ops->gw.is_eligible)
506 if (buff[count - 1] == '\n')
507 buff[count - 1] = '\0';
509 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
510 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
511 gw_mode_tmp = BATADV_GW_MODE_OFF;
513 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
514 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
515 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
517 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
518 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
519 gw_mode_tmp = BATADV_GW_MODE_SERVER;
521 if (gw_mode_tmp < 0) {
523 "Invalid parameter for 'gw mode' setting received: %s\n",
528 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
531 switch (atomic_read(&bat_priv->gw.mode)) {
532 case BATADV_GW_MODE_CLIENT:
533 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
535 case BATADV_GW_MODE_SERVER:
536 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
539 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
543 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
544 curr_gw_mode_str, buff);
546 /* Invoking batadv_gw_reselect() is not enough to really de-select the
547 * current GW. It will only instruct the gateway client code to perform
548 * a re-election the next time that this is needed.
550 * When gw client mode is being switched off the current GW must be
551 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
552 * client mode re-activation. This is operation is performed in
553 * batadv_gw_check_client_stop().
555 batadv_gw_reselect(bat_priv);
556 /* always call batadv_gw_check_client_stop() before changing the gateway
559 batadv_gw_check_client_stop(bat_priv);
560 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
561 batadv_gw_tvlv_container_update(bat_priv);
563 batadv_netlink_notify_mesh(bat_priv);
568 static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
569 struct attribute *attr, char *buff)
571 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
573 /* GW selection class is not available if the routing algorithm in use
574 * does not implement the GW API
576 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
577 !bat_priv->algo_ops->gw.is_eligible)
580 if (bat_priv->algo_ops->gw.show_sel_class)
581 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
583 return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
586 static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
587 struct attribute *attr, char *buff,
590 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
593 /* setting the GW selection class is allowed only if the routing
594 * algorithm in use implements the GW API
596 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
597 !bat_priv->algo_ops->gw.is_eligible)
600 if (buff[count - 1] == '\n')
601 buff[count - 1] = '\0';
603 if (bat_priv->algo_ops->gw.store_sel_class)
604 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
607 length = __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
608 batadv_post_gw_reselect, attr,
609 &bat_priv->gw.sel_class,
610 bat_priv->soft_iface, NULL);
612 batadv_netlink_notify_mesh(bat_priv);
617 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
618 struct attribute *attr, char *buff)
620 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
623 down = atomic_read(&bat_priv->gw.bandwidth_down);
624 up = atomic_read(&bat_priv->gw.bandwidth_up);
626 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
627 down % 10, up / 10, up % 10);
630 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
631 struct attribute *attr, char *buff,
634 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
635 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
638 if (buff[count - 1] == '\n')
639 buff[count - 1] = '\0';
641 length = batadv_gw_bandwidth_set(net_dev, buff, count);
643 batadv_netlink_notify_mesh(bat_priv);
649 * batadv_show_isolation_mark() - print the current isolation mark/mask
650 * @kobj: kobject representing the private mesh sysfs directory
651 * @attr: the batman-adv attribute the user is interacting with
652 * @buff: the buffer that will contain the data to send back to the user
654 * Return: the number of bytes written into 'buff' on success or a negative
655 * error code in case of failure
657 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
658 struct attribute *attr, char *buff)
660 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
662 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
663 bat_priv->isolation_mark_mask);
667 * batadv_store_isolation_mark() - parse and store the isolation mark/mask
668 * entered by the user
669 * @kobj: kobject representing the private mesh sysfs directory
670 * @attr: the batman-adv attribute the user is interacting with
671 * @buff: the buffer containing the user data
672 * @count: number of bytes in the buffer
674 * Return: 'count' on success or a negative error code in case of failure
676 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
677 struct attribute *attr, char *buff,
680 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
681 struct batadv_priv *bat_priv = netdev_priv(net_dev);
685 /* parse the mask if it has been specified, otherwise assume the mask is
686 * the biggest possible
689 mask_ptr = strchr(buff, '/');
694 /* the mask must be entered in hex base as it is going to be a
695 * bitmask and not a prefix length
697 if (kstrtou32(mask_ptr, 16, &mask) < 0)
701 /* the mark can be entered in any base */
702 if (kstrtou32(buff, 0, &mark) < 0)
705 bat_priv->isolation_mark_mask = mask;
706 /* erase bits not covered by the mask */
707 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
710 "New skb mark for extended isolation: %#.8x/%#.8x\n",
711 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
713 batadv_netlink_notify_mesh(bat_priv);
718 BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
719 BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
720 #ifdef CONFIG_BATMAN_ADV_BLA
721 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
723 #ifdef CONFIG_BATMAN_ADV_DAT
724 BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
726 BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
727 static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
728 static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
729 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
731 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
733 static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
734 batadv_store_gw_sel_class);
735 static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
736 batadv_store_gw_bwidth);
737 #ifdef CONFIG_BATMAN_ADV_MCAST
738 BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
740 #ifdef CONFIG_BATMAN_ADV_DEBUG
741 BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
743 #ifdef CONFIG_BATMAN_ADV_NC
744 BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
746 static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
747 batadv_store_isolation_mark);
749 static struct batadv_attribute *batadv_mesh_attrs[] = {
750 &batadv_attr_aggregated_ogms,
751 &batadv_attr_bonding,
752 #ifdef CONFIG_BATMAN_ADV_BLA
753 &batadv_attr_bridge_loop_avoidance,
755 #ifdef CONFIG_BATMAN_ADV_DAT
756 &batadv_attr_distributed_arp_table,
758 #ifdef CONFIG_BATMAN_ADV_MCAST
759 &batadv_attr_multicast_mode,
761 &batadv_attr_fragmentation,
762 &batadv_attr_routing_algo,
763 &batadv_attr_gw_mode,
764 &batadv_attr_orig_interval,
765 &batadv_attr_hop_penalty,
766 &batadv_attr_gw_sel_class,
767 &batadv_attr_gw_bandwidth,
768 #ifdef CONFIG_BATMAN_ADV_DEBUG
769 &batadv_attr_log_level,
771 #ifdef CONFIG_BATMAN_ADV_NC
772 &batadv_attr_network_coding,
774 &batadv_attr_isolation_mark,
778 BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
780 /* array of vlan specific sysfs attributes */
781 static struct batadv_attribute *batadv_vlan_attrs[] = {
782 &batadv_attr_vlan_ap_isolation,
787 * batadv_sysfs_add_meshif() - Add soft interface specific sysfs entries
788 * @dev: netdev struct of the soft interface
790 * Return: 0 on success or negative error number in case of failure
792 int batadv_sysfs_add_meshif(struct net_device *dev)
794 struct kobject *batif_kobject = &dev->dev.kobj;
795 struct batadv_priv *bat_priv = netdev_priv(dev);
796 struct batadv_attribute **bat_attr;
799 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
801 if (!bat_priv->mesh_obj) {
802 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
803 BATADV_SYSFS_IF_MESH_SUBDIR);
807 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
808 err = sysfs_create_file(bat_priv->mesh_obj,
809 &((*bat_attr)->attr));
811 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
812 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
813 ((*bat_attr)->attr).name);
821 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
822 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
824 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
825 kobject_del(bat_priv->mesh_obj);
826 kobject_put(bat_priv->mesh_obj);
827 bat_priv->mesh_obj = NULL;
833 * batadv_sysfs_del_meshif() - Remove soft interface specific sysfs entries
834 * @dev: netdev struct of the soft interface
836 void batadv_sysfs_del_meshif(struct net_device *dev)
838 struct batadv_priv *bat_priv = netdev_priv(dev);
839 struct batadv_attribute **bat_attr;
841 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
842 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
844 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
845 kobject_del(bat_priv->mesh_obj);
846 kobject_put(bat_priv->mesh_obj);
847 bat_priv->mesh_obj = NULL;
851 * batadv_sysfs_add_vlan() - add all the needed sysfs objects for the new vlan
852 * @dev: netdev of the mesh interface
853 * @vlan: private data of the newly added VLAN interface
855 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
857 int batadv_sysfs_add_vlan(struct net_device *dev,
858 struct batadv_softif_vlan *vlan)
860 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
861 struct batadv_priv *bat_priv = netdev_priv(dev);
862 struct batadv_attribute **bat_attr;
865 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
866 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
867 vlan->vid & VLAN_VID_MASK);
869 vlan->kobj = kobject_create_and_add(vlan_subdir,
872 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
873 dev->name, vlan_subdir);
877 /* the untagged LAN uses the root folder to store its "VLAN
878 * specific attributes"
880 vlan->kobj = bat_priv->mesh_obj;
881 kobject_get(bat_priv->mesh_obj);
884 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
885 err = sysfs_create_file(vlan->kobj,
886 &((*bat_attr)->attr));
888 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
889 dev->name, vlan_subdir,
890 ((*bat_attr)->attr).name);
898 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
899 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
901 if (vlan->kobj != bat_priv->mesh_obj) {
902 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
903 kobject_del(vlan->kobj);
905 kobject_put(vlan->kobj);
912 * batadv_sysfs_del_vlan() - remove all the sysfs objects for a given VLAN
913 * @bat_priv: the bat priv with all the soft interface information
914 * @vlan: the private data of the VLAN to destroy
916 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
917 struct batadv_softif_vlan *vlan)
919 struct batadv_attribute **bat_attr;
921 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
922 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
924 if (vlan->kobj != bat_priv->mesh_obj) {
925 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
926 kobject_del(vlan->kobj);
928 kobject_put(vlan->kobj);
932 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
933 struct attribute *attr, char *buff)
935 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
936 struct batadv_hard_iface *hard_iface;
940 hard_iface = batadv_hardif_get_by_netdev(net_dev);
944 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
947 ifname = hard_iface->soft_iface->name;
949 length = sprintf(buff, "%s\n", ifname);
951 batadv_hardif_put(hard_iface);
957 * batadv_store_mesh_iface_finish() - store new hardif mesh_iface state
958 * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
959 * @ifname: name of soft-interface to modify
961 * Changes the parts of the hard+soft interface which can not be modified under
962 * sysfs lock (to prevent deadlock situations).
964 * Return: 0 on success, 0 < on failure
966 static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
967 char ifname[IFNAMSIZ])
969 struct net *net = dev_net(net_dev);
970 struct batadv_hard_iface *hard_iface;
976 hard_iface = batadv_hardif_get_by_netdev(net_dev);
980 if (strncmp(ifname, "none", 4) == 0)
981 status_tmp = BATADV_IF_NOT_IN_USE;
983 status_tmp = BATADV_IF_I_WANT_YOU;
985 if (hard_iface->if_status == status_tmp)
988 if (hard_iface->soft_iface &&
989 strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)
992 if (status_tmp == BATADV_IF_NOT_IN_USE) {
993 batadv_hardif_disable_interface(hard_iface,
994 BATADV_IF_CLEANUP_AUTO);
998 /* if the interface already is in use */
999 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
1000 batadv_hardif_disable_interface(hard_iface,
1001 BATADV_IF_CLEANUP_AUTO);
1003 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
1005 batadv_hardif_put(hard_iface);
1010 * batadv_store_mesh_iface_work() - store new hardif mesh_iface state
1011 * @work: work queue item
1013 * Changes the parts of the hard+soft interface which can not be modified under
1014 * sysfs lock (to prevent deadlock situations).
1016 static void batadv_store_mesh_iface_work(struct work_struct *work)
1018 struct batadv_store_mesh_work *store_work;
1021 store_work = container_of(work, struct batadv_store_mesh_work, work);
1024 ret = batadv_store_mesh_iface_finish(store_work->net_dev,
1025 store_work->soft_iface_name);
1029 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
1030 store_work->soft_iface_name, store_work->net_dev->name,
1033 dev_put(store_work->net_dev);
1037 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
1038 struct attribute *attr, char *buff,
1041 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1042 struct batadv_store_mesh_work *store_work;
1044 if (buff[count - 1] == '\n')
1045 buff[count - 1] = '\0';
1047 if (strlen(buff) >= IFNAMSIZ) {
1048 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
1053 store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
1058 INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1059 store_work->net_dev = net_dev;
1060 strlcpy(store_work->soft_iface_name, buff,
1061 sizeof(store_work->soft_iface_name));
1063 queue_work(batadv_event_workqueue, &store_work->work);
1068 static ssize_t batadv_show_iface_status(struct kobject *kobj,
1069 struct attribute *attr, char *buff)
1071 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1072 struct batadv_hard_iface *hard_iface;
1075 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1079 switch (hard_iface->if_status) {
1080 case BATADV_IF_TO_BE_REMOVED:
1081 length = sprintf(buff, "disabling\n");
1083 case BATADV_IF_INACTIVE:
1084 length = sprintf(buff, "inactive\n");
1086 case BATADV_IF_ACTIVE:
1087 length = sprintf(buff, "active\n");
1089 case BATADV_IF_TO_BE_ACTIVATED:
1090 length = sprintf(buff, "enabling\n");
1092 case BATADV_IF_NOT_IN_USE:
1094 length = sprintf(buff, "not in use\n");
1098 batadv_hardif_put(hard_iface);
1103 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1106 * batadv_store_throughput_override() - parse and store throughput override
1107 * entered by the user
1108 * @kobj: kobject representing the private mesh sysfs directory
1109 * @attr: the batman-adv attribute the user is interacting with
1110 * @buff: the buffer containing the user data
1111 * @count: number of bytes in the buffer
1113 * Return: 'count' on success or a negative error code in case of failure
1115 static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1116 struct attribute *attr,
1117 char *buff, size_t count)
1119 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1120 struct batadv_hard_iface *hard_iface;
1121 struct batadv_priv *bat_priv;
1123 u32 old_tp_override;
1126 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1130 if (buff[count - 1] == '\n')
1131 buff[count - 1] = '\0';
1133 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1138 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1139 if (old_tp_override == tp_override)
1142 batadv_info(hard_iface->soft_iface,
1143 "%s: %s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1144 "throughput_override", net_dev->name,
1145 old_tp_override / 10, old_tp_override % 10,
1146 tp_override / 10, tp_override % 10);
1148 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1150 if (hard_iface->soft_iface) {
1151 bat_priv = netdev_priv(hard_iface->soft_iface);
1152 batadv_netlink_notify_hardif(bat_priv, hard_iface);
1156 batadv_hardif_put(hard_iface);
1160 static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1161 struct attribute *attr,
1164 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1165 struct batadv_hard_iface *hard_iface;
1168 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1172 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1174 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1180 static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
1181 batadv_store_mesh_iface);
1182 static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
1183 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1184 BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
1185 2 * BATADV_JITTER, INT_MAX, NULL);
1186 static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
1187 batadv_store_throughput_override);
1190 static struct batadv_attribute *batadv_batman_attrs[] = {
1191 &batadv_attr_mesh_iface,
1192 &batadv_attr_iface_status,
1193 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1194 &batadv_attr_elp_interval,
1195 &batadv_attr_throughput_override,
1201 * batadv_sysfs_add_hardif() - Add hard interface specific sysfs entries
1202 * @hardif_obj: address where to store the pointer to new sysfs folder
1203 * @dev: netdev struct of the hard interface
1205 * Return: 0 on success or negative error number in case of failure
1207 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
1209 struct kobject *hardif_kobject = &dev->dev.kobj;
1210 struct batadv_attribute **bat_attr;
1213 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1217 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
1218 BATADV_SYSFS_IF_BAT_SUBDIR);
1222 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
1223 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1225 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
1226 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
1227 ((*bat_attr)->attr).name);
1235 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
1236 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1242 * batadv_sysfs_del_hardif() - Remove hard interface specific sysfs entries
1243 * @hardif_obj: address to the pointer to which stores batman-adv sysfs folder
1244 * of the hard interface
1246 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
1248 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1249 kobject_del(*hardif_obj);
1250 kobject_put(*hardif_obj);
1255 * batadv_throw_uevent() - Send an uevent with batman-adv specific env data
1256 * @bat_priv: the bat priv with all the soft interface information
1257 * @type: subsystem type of event. Stored in uevent's BATTYPE
1258 * @action: action type of event. Stored in uevent's BATACTION
1259 * @data: string with additional information to the event (ignored for
1260 * BATADV_UEV_DEL). Stored in uevent's BATDATA
1262 * Return: 0 on success or negative error number in case of failure
1264 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
1265 enum batadv_uev_action action, const char *data)
1268 struct kobject *bat_kobj;
1269 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1271 bat_kobj = &bat_priv->soft_iface->dev.kobj;
1273 uevent_env[0] = kasprintf(GFP_ATOMIC,
1274 "%s%s", BATADV_UEV_TYPE_VAR,
1275 batadv_uev_type_str[type]);
1279 uevent_env[1] = kasprintf(GFP_ATOMIC,
1280 "%s%s", BATADV_UEV_ACTION_VAR,
1281 batadv_uev_action_str[action]);
1285 /* If the event is DEL, ignore the data field */
1286 if (action != BATADV_UEV_DEL) {
1287 uevent_env[2] = kasprintf(GFP_ATOMIC,
1288 "%s%s", BATADV_UEV_DATA_VAR, data);
1293 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1295 kfree(uevent_env[0]);
1296 kfree(uevent_env[1]);
1297 kfree(uevent_env[2]);
1300 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1301 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
1302 batadv_uev_type_str[type],
1303 batadv_uev_action_str[action],
1304 (action == BATADV_UEV_DEL ? "NULL" : data), ret);