1 /* Copyright (C) 2010-2017 B.A.T.M.A.N. contributors:
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <linux/atomic.h>
22 #include <linux/compiler.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
27 #include <linux/if_vlan.h>
28 #include <linux/kernel.h>
29 #include <linux/kref.h>
30 #include <linux/netdevice.h>
31 #include <linux/printk.h>
32 #include <linux/rculist.h>
33 #include <linux/rcupdate.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/slab.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/stringify.h>
39 #include <linux/workqueue.h>
41 #include "bridge_loop_avoidance.h"
42 #include "distributed-arp-table.h"
43 #include "gateway_client.h"
44 #include "gateway_common.h"
45 #include "hard-interface.h"
47 #include "network-coding.h"
49 #include "soft-interface.h"
51 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
53 struct device *dev = container_of(obj->parent, struct device, kobj);
55 return to_net_dev(dev);
58 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
60 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
62 return netdev_priv(net_dev);
66 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
67 * @obj: kobject to covert
69 * Return: the associated batadv_priv struct.
71 static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
73 /* VLAN specific attributes are located in the root sysfs folder if they
74 * refer to the untagged VLAN..
76 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
77 return batadv_kobj_to_batpriv(obj);
79 /* ..while the attributes for the tagged vlans are located in
80 * the in the corresponding "vlan%VID" subfolder
82 return batadv_kobj_to_batpriv(obj->parent);
86 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
87 * @bat_priv: the bat priv with all the soft interface information
88 * @obj: kobject to covert
90 * Return: the associated softif_vlan struct if found, NULL otherwise.
92 static struct batadv_softif_vlan *
93 batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
95 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
98 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
99 if (vlan_tmp->kobj != obj)
102 if (!kref_get_unless_zero(&vlan_tmp->refcount))
113 #define BATADV_UEV_TYPE_VAR "BATTYPE="
114 #define BATADV_UEV_ACTION_VAR "BATACTION="
115 #define BATADV_UEV_DATA_VAR "BATDATA="
117 static char *batadv_uev_action_str[] = {
124 static char *batadv_uev_type_str[] = {
129 /* Use this, if you have customized show and store functions for vlan attrs */
130 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
131 struct batadv_attribute batadv_attr_vlan_##_name = { \
132 .attr = {.name = __stringify(_name), \
138 /* Use this, if you have customized show and store functions */
139 #define BATADV_ATTR(_name, _mode, _show, _store) \
140 struct batadv_attribute batadv_attr_##_name = { \
141 .attr = {.name = __stringify(_name), \
147 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
148 ssize_t batadv_store_##_name(struct kobject *kobj, \
149 struct attribute *attr, char *buff, \
152 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
153 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
155 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
156 &bat_priv->_name, net_dev); \
159 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
160 ssize_t batadv_show_##_name(struct kobject *kobj, \
161 struct attribute *attr, char *buff) \
163 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
165 return sprintf(buff, "%s\n", \
166 atomic_read(&bat_priv->_name) == 0 ? \
167 "disabled" : "enabled"); \
170 /* Use this, if you are going to turn a [name] in the soft-interface
171 * (bat_priv) on or off
173 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
174 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
175 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
176 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
177 batadv_store_##_name)
179 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
180 ssize_t batadv_store_##_name(struct kobject *kobj, \
181 struct attribute *attr, char *buff, \
184 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
185 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
187 return __batadv_store_uint_attr(buff, count, _min, _max, \
189 &bat_priv->_var, net_dev, \
193 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
194 ssize_t batadv_show_##_name(struct kobject *kobj, \
195 struct attribute *attr, char *buff) \
197 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
199 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
202 /* Use this, if you are going to set [name] in the soft-interface
203 * (bat_priv) to an unsigned integer value
205 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
206 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
207 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
208 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
209 batadv_store_##_name)
211 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
212 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
213 struct attribute *attr, char *buff, \
216 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
217 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
219 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
220 attr, &vlan->_name, \
221 bat_priv->soft_iface); \
223 batadv_softif_vlan_put(vlan); \
227 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
228 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
229 struct attribute *attr, char *buff) \
231 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
232 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
234 size_t res = sprintf(buff, "%s\n", \
235 atomic_read(&vlan->_name) == 0 ? \
236 "disabled" : "enabled"); \
238 batadv_softif_vlan_put(vlan); \
242 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
243 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
244 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
245 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
246 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
247 batadv_store_vlan_##_name)
249 #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
250 ssize_t batadv_store_##_name(struct kobject *kobj, \
251 struct attribute *attr, char *buff, \
254 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
255 struct batadv_hard_iface *hard_iface; \
258 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
262 length = __batadv_store_uint_attr(buff, count, _min, _max, \
265 hard_iface->soft_iface, \
268 batadv_hardif_put(hard_iface); \
272 #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
273 ssize_t batadv_show_##_name(struct kobject *kobj, \
274 struct attribute *attr, char *buff) \
276 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
277 struct batadv_hard_iface *hard_iface; \
280 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
284 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
286 batadv_hardif_put(hard_iface); \
290 /* Use this, if you are going to set [name] in hard_iface to an
291 * unsigned integer value
293 #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
294 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
296 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
297 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
298 batadv_store_##_name)
300 static int batadv_store_bool_attr(char *buff, size_t count,
301 struct net_device *net_dev,
302 const char *attr_name, atomic_t *attr,
309 if (buff[count - 1] == '\n')
310 buff[count - 1] = '\0';
312 if ((strncmp(buff, "1", 2) == 0) ||
313 (strncmp(buff, "enable", 7) == 0) ||
314 (strncmp(buff, "enabled", 8) == 0))
317 if ((strncmp(buff, "0", 2) == 0) ||
318 (strncmp(buff, "disable", 8) == 0) ||
319 (strncmp(buff, "disabled", 9) == 0))
323 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
328 if (atomic_read(attr) == enabled)
331 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
332 atomic_read(attr) == 1 ? "enabled" : "disabled",
333 enabled == 1 ? "enabled" : "disabled");
337 atomic_set(attr, (unsigned int)enabled);
341 static inline ssize_t
342 __batadv_store_bool_attr(char *buff, size_t count,
343 void (*post_func)(struct net_device *),
344 struct attribute *attr,
345 atomic_t *attr_store, struct net_device *net_dev)
350 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
351 attr_store, &changed);
352 if (post_func && changed)
358 static int batadv_store_uint_attr(const char *buff, size_t count,
359 struct net_device *net_dev,
360 struct net_device *slave_dev,
361 const char *attr_name,
362 unsigned int min, unsigned int max,
365 char ifname[IFNAMSIZ + 3] = "";
366 unsigned long uint_val;
369 ret = kstrtoul(buff, 10, &uint_val);
371 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
376 if (uint_val < min) {
377 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
378 attr_name, uint_val, min);
382 if (uint_val > max) {
383 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
384 attr_name, uint_val, max);
388 if (atomic_read(attr) == uint_val)
392 snprintf(ifname, sizeof(ifname), "%s: ", slave_dev->name);
394 batadv_info(net_dev, "%s: %sChanging from: %i to: %lu\n",
395 attr_name, ifname, atomic_read(attr), uint_val);
397 atomic_set(attr, uint_val);
401 static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
403 void (*post_func)(struct net_device *),
404 const struct attribute *attr,
405 atomic_t *attr_store,
406 struct net_device *net_dev,
407 struct net_device *slave_dev)
411 ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
412 attr->name, min, max, attr_store);
413 if (post_func && ret)
419 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
420 struct attribute *attr, char *buff)
422 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
424 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
427 static void batadv_post_gw_reselect(struct net_device *net_dev)
429 struct batadv_priv *bat_priv = netdev_priv(net_dev);
431 batadv_gw_reselect(bat_priv);
434 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
437 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
440 /* GW mode is not available if the routing algorithm in use does not
441 * implement the GW API
443 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
444 !bat_priv->algo_ops->gw.is_eligible)
447 switch (atomic_read(&bat_priv->gw.mode)) {
448 case BATADV_GW_MODE_CLIENT:
449 bytes_written = sprintf(buff, "%s\n",
450 BATADV_GW_MODE_CLIENT_NAME);
452 case BATADV_GW_MODE_SERVER:
453 bytes_written = sprintf(buff, "%s\n",
454 BATADV_GW_MODE_SERVER_NAME);
457 bytes_written = sprintf(buff, "%s\n",
458 BATADV_GW_MODE_OFF_NAME);
462 return bytes_written;
465 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
466 struct attribute *attr, char *buff,
469 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
470 struct batadv_priv *bat_priv = netdev_priv(net_dev);
471 char *curr_gw_mode_str;
472 int gw_mode_tmp = -1;
474 /* toggling GW mode is allowed only if the routing algorithm in use
475 * provides the GW API
477 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
478 !bat_priv->algo_ops->gw.is_eligible)
481 if (buff[count - 1] == '\n')
482 buff[count - 1] = '\0';
484 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
485 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
486 gw_mode_tmp = BATADV_GW_MODE_OFF;
488 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
489 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
490 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
492 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
493 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
494 gw_mode_tmp = BATADV_GW_MODE_SERVER;
496 if (gw_mode_tmp < 0) {
498 "Invalid parameter for 'gw mode' setting received: %s\n",
503 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
506 switch (atomic_read(&bat_priv->gw.mode)) {
507 case BATADV_GW_MODE_CLIENT:
508 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
510 case BATADV_GW_MODE_SERVER:
511 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
514 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
518 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
519 curr_gw_mode_str, buff);
521 /* Invoking batadv_gw_reselect() is not enough to really de-select the
522 * current GW. It will only instruct the gateway client code to perform
523 * a re-election the next time that this is needed.
525 * When gw client mode is being switched off the current GW must be
526 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
527 * client mode re-activation. This is operation is performed in
528 * batadv_gw_check_client_stop().
530 batadv_gw_reselect(bat_priv);
531 /* always call batadv_gw_check_client_stop() before changing the gateway
534 batadv_gw_check_client_stop(bat_priv);
535 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
536 batadv_gw_tvlv_container_update(bat_priv);
540 static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
541 struct attribute *attr, char *buff)
543 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
545 /* GW selection class is not available if the routing algorithm in use
546 * does not implement the GW API
548 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
549 !bat_priv->algo_ops->gw.is_eligible)
552 if (bat_priv->algo_ops->gw.show_sel_class)
553 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
555 return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
558 static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
559 struct attribute *attr, char *buff,
562 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
564 /* setting the GW selection class is allowed only if the routing
565 * algorithm in use implements the GW API
567 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
568 !bat_priv->algo_ops->gw.is_eligible)
571 if (buff[count - 1] == '\n')
572 buff[count - 1] = '\0';
574 if (bat_priv->algo_ops->gw.store_sel_class)
575 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
578 return __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
579 batadv_post_gw_reselect, attr,
580 &bat_priv->gw.sel_class,
581 bat_priv->soft_iface, NULL);
584 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
585 struct attribute *attr, char *buff)
587 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
590 down = atomic_read(&bat_priv->gw.bandwidth_down);
591 up = atomic_read(&bat_priv->gw.bandwidth_up);
593 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
594 down % 10, up / 10, up % 10);
597 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
598 struct attribute *attr, char *buff,
601 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
603 if (buff[count - 1] == '\n')
604 buff[count - 1] = '\0';
606 return batadv_gw_bandwidth_set(net_dev, buff, count);
610 * batadv_show_isolation_mark - print the current isolation mark/mask
611 * @kobj: kobject representing the private mesh sysfs directory
612 * @attr: the batman-adv attribute the user is interacting with
613 * @buff: the buffer that will contain the data to send back to the user
615 * Return: the number of bytes written into 'buff' on success or a negative
616 * error code in case of failure
618 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
619 struct attribute *attr, char *buff)
621 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
623 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
624 bat_priv->isolation_mark_mask);
628 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
630 * @kobj: kobject representing the private mesh sysfs directory
631 * @attr: the batman-adv attribute the user is interacting with
632 * @buff: the buffer containing the user data
633 * @count: number of bytes in the buffer
635 * Return: 'count' on success or a negative error code in case of failure
637 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
638 struct attribute *attr, char *buff,
641 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
642 struct batadv_priv *bat_priv = netdev_priv(net_dev);
646 /* parse the mask if it has been specified, otherwise assume the mask is
647 * the biggest possible
650 mask_ptr = strchr(buff, '/');
655 /* the mask must be entered in hex base as it is going to be a
656 * bitmask and not a prefix length
658 if (kstrtou32(mask_ptr, 16, &mask) < 0)
662 /* the mark can be entered in any base */
663 if (kstrtou32(buff, 0, &mark) < 0)
666 bat_priv->isolation_mark_mask = mask;
667 /* erase bits not covered by the mask */
668 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
671 "New skb mark for extended isolation: %#.8x/%#.8x\n",
672 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
677 BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
678 BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
679 #ifdef CONFIG_BATMAN_ADV_BLA
680 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
682 #ifdef CONFIG_BATMAN_ADV_DAT
683 BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
685 BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
686 static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
687 static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
688 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
690 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
692 static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
693 batadv_store_gw_sel_class);
694 static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
695 batadv_store_gw_bwidth);
696 #ifdef CONFIG_BATMAN_ADV_MCAST
697 BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
699 #ifdef CONFIG_BATMAN_ADV_DEBUG
700 BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
702 #ifdef CONFIG_BATMAN_ADV_NC
703 BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
705 static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
706 batadv_store_isolation_mark);
708 static struct batadv_attribute *batadv_mesh_attrs[] = {
709 &batadv_attr_aggregated_ogms,
710 &batadv_attr_bonding,
711 #ifdef CONFIG_BATMAN_ADV_BLA
712 &batadv_attr_bridge_loop_avoidance,
714 #ifdef CONFIG_BATMAN_ADV_DAT
715 &batadv_attr_distributed_arp_table,
717 #ifdef CONFIG_BATMAN_ADV_MCAST
718 &batadv_attr_multicast_mode,
720 &batadv_attr_fragmentation,
721 &batadv_attr_routing_algo,
722 &batadv_attr_gw_mode,
723 &batadv_attr_orig_interval,
724 &batadv_attr_hop_penalty,
725 &batadv_attr_gw_sel_class,
726 &batadv_attr_gw_bandwidth,
727 #ifdef CONFIG_BATMAN_ADV_DEBUG
728 &batadv_attr_log_level,
730 #ifdef CONFIG_BATMAN_ADV_NC
731 &batadv_attr_network_coding,
733 &batadv_attr_isolation_mark,
737 BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
739 /* array of vlan specific sysfs attributes */
740 static struct batadv_attribute *batadv_vlan_attrs[] = {
741 &batadv_attr_vlan_ap_isolation,
745 int batadv_sysfs_add_meshif(struct net_device *dev)
747 struct kobject *batif_kobject = &dev->dev.kobj;
748 struct batadv_priv *bat_priv = netdev_priv(dev);
749 struct batadv_attribute **bat_attr;
752 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
754 if (!bat_priv->mesh_obj) {
755 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
756 BATADV_SYSFS_IF_MESH_SUBDIR);
760 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
761 err = sysfs_create_file(bat_priv->mesh_obj,
762 &((*bat_attr)->attr));
764 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
765 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
766 ((*bat_attr)->attr).name);
774 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
775 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
777 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
778 kobject_del(bat_priv->mesh_obj);
779 kobject_put(bat_priv->mesh_obj);
780 bat_priv->mesh_obj = NULL;
785 void batadv_sysfs_del_meshif(struct net_device *dev)
787 struct batadv_priv *bat_priv = netdev_priv(dev);
788 struct batadv_attribute **bat_attr;
790 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
791 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
793 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
794 kobject_del(bat_priv->mesh_obj);
795 kobject_put(bat_priv->mesh_obj);
796 bat_priv->mesh_obj = NULL;
800 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
801 * @dev: netdev of the mesh interface
802 * @vlan: private data of the newly added VLAN interface
804 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
806 int batadv_sysfs_add_vlan(struct net_device *dev,
807 struct batadv_softif_vlan *vlan)
809 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
810 struct batadv_priv *bat_priv = netdev_priv(dev);
811 struct batadv_attribute **bat_attr;
814 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
815 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
816 vlan->vid & VLAN_VID_MASK);
818 vlan->kobj = kobject_create_and_add(vlan_subdir,
821 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
822 dev->name, vlan_subdir);
826 /* the untagged LAN uses the root folder to store its "VLAN
827 * specific attributes"
829 vlan->kobj = bat_priv->mesh_obj;
830 kobject_get(bat_priv->mesh_obj);
833 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
834 err = sysfs_create_file(vlan->kobj,
835 &((*bat_attr)->attr));
837 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
838 dev->name, vlan_subdir,
839 ((*bat_attr)->attr).name);
847 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
848 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
850 if (vlan->kobj != bat_priv->mesh_obj) {
851 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
852 kobject_del(vlan->kobj);
854 kobject_put(vlan->kobj);
861 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
862 * @bat_priv: the bat priv with all the soft interface information
863 * @vlan: the private data of the VLAN to destroy
865 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
866 struct batadv_softif_vlan *vlan)
868 struct batadv_attribute **bat_attr;
870 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
871 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
873 if (vlan->kobj != bat_priv->mesh_obj) {
874 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
875 kobject_del(vlan->kobj);
877 kobject_put(vlan->kobj);
881 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
882 struct attribute *attr, char *buff)
884 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
885 struct batadv_hard_iface *hard_iface;
889 hard_iface = batadv_hardif_get_by_netdev(net_dev);
893 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
896 ifname = hard_iface->soft_iface->name;
898 length = sprintf(buff, "%s\n", ifname);
900 batadv_hardif_put(hard_iface);
906 * batadv_store_mesh_iface_finish - store new hardif mesh_iface state
907 * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
908 * @ifname: name of soft-interface to modify
910 * Changes the parts of the hard+soft interface which can not be modified under
911 * sysfs lock (to prevent deadlock situations).
913 * Return: 0 on success, 0 < on failure
915 static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
916 char ifname[IFNAMSIZ])
918 struct net *net = dev_net(net_dev);
919 struct batadv_hard_iface *hard_iface;
925 hard_iface = batadv_hardif_get_by_netdev(net_dev);
929 if (strncmp(ifname, "none", 4) == 0)
930 status_tmp = BATADV_IF_NOT_IN_USE;
932 status_tmp = BATADV_IF_I_WANT_YOU;
934 if (hard_iface->if_status == status_tmp)
937 if ((hard_iface->soft_iface) &&
938 (strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0))
941 if (status_tmp == BATADV_IF_NOT_IN_USE) {
942 batadv_hardif_disable_interface(hard_iface,
943 BATADV_IF_CLEANUP_AUTO);
947 /* if the interface already is in use */
948 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
949 batadv_hardif_disable_interface(hard_iface,
950 BATADV_IF_CLEANUP_AUTO);
952 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
954 batadv_hardif_put(hard_iface);
959 * batadv_store_mesh_iface_work - store new hardif mesh_iface state
960 * @work: work queue item
962 * Changes the parts of the hard+soft interface which can not be modified under
963 * sysfs lock (to prevent deadlock situations).
965 static void batadv_store_mesh_iface_work(struct work_struct *work)
967 struct batadv_store_mesh_work *store_work;
970 store_work = container_of(work, struct batadv_store_mesh_work, work);
973 ret = batadv_store_mesh_iface_finish(store_work->net_dev,
974 store_work->soft_iface_name);
978 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
979 store_work->soft_iface_name, store_work->net_dev->name,
982 dev_put(store_work->net_dev);
986 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
987 struct attribute *attr, char *buff,
990 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
991 struct batadv_store_mesh_work *store_work;
993 if (buff[count - 1] == '\n')
994 buff[count - 1] = '\0';
996 if (strlen(buff) >= IFNAMSIZ) {
997 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
1002 store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
1007 INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1008 store_work->net_dev = net_dev;
1009 strlcpy(store_work->soft_iface_name, buff,
1010 sizeof(store_work->soft_iface_name));
1012 queue_work(batadv_event_workqueue, &store_work->work);
1017 static ssize_t batadv_show_iface_status(struct kobject *kobj,
1018 struct attribute *attr, char *buff)
1020 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1021 struct batadv_hard_iface *hard_iface;
1024 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1028 switch (hard_iface->if_status) {
1029 case BATADV_IF_TO_BE_REMOVED:
1030 length = sprintf(buff, "disabling\n");
1032 case BATADV_IF_INACTIVE:
1033 length = sprintf(buff, "inactive\n");
1035 case BATADV_IF_ACTIVE:
1036 length = sprintf(buff, "active\n");
1038 case BATADV_IF_TO_BE_ACTIVATED:
1039 length = sprintf(buff, "enabling\n");
1041 case BATADV_IF_NOT_IN_USE:
1043 length = sprintf(buff, "not in use\n");
1047 batadv_hardif_put(hard_iface);
1052 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1055 * batadv_store_throughput_override - parse and store throughput override
1056 * entered by the user
1057 * @kobj: kobject representing the private mesh sysfs directory
1058 * @attr: the batman-adv attribute the user is interacting with
1059 * @buff: the buffer containing the user data
1060 * @count: number of bytes in the buffer
1062 * Return: 'count' on success or a negative error code in case of failure
1064 static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1065 struct attribute *attr,
1066 char *buff, size_t count)
1068 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1069 struct batadv_hard_iface *hard_iface;
1071 u32 old_tp_override;
1074 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1078 if (buff[count - 1] == '\n')
1079 buff[count - 1] = '\0';
1081 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1086 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1087 if (old_tp_override == tp_override)
1090 batadv_info(hard_iface->soft_iface,
1091 "%s: %s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1092 "throughput_override", net_dev->name,
1093 old_tp_override / 10, old_tp_override % 10,
1094 tp_override / 10, tp_override % 10);
1096 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1099 batadv_hardif_put(hard_iface);
1103 static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1104 struct attribute *attr,
1107 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1108 struct batadv_hard_iface *hard_iface;
1111 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1115 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1117 batadv_hardif_put(hard_iface);
1118 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1124 static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
1125 batadv_store_mesh_iface);
1126 static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
1127 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1128 BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
1129 2 * BATADV_JITTER, INT_MAX, NULL);
1130 static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
1131 batadv_store_throughput_override);
1134 static struct batadv_attribute *batadv_batman_attrs[] = {
1135 &batadv_attr_mesh_iface,
1136 &batadv_attr_iface_status,
1137 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1138 &batadv_attr_elp_interval,
1139 &batadv_attr_throughput_override,
1144 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
1146 struct kobject *hardif_kobject = &dev->dev.kobj;
1147 struct batadv_attribute **bat_attr;
1150 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1154 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
1155 BATADV_SYSFS_IF_BAT_SUBDIR);
1159 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
1160 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1162 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
1163 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
1164 ((*bat_attr)->attr).name);
1172 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
1173 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1178 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
1180 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1181 kobject_del(*hardif_obj);
1182 kobject_put(*hardif_obj);
1186 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
1187 enum batadv_uev_action action, const char *data)
1190 struct kobject *bat_kobj;
1191 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1193 bat_kobj = &bat_priv->soft_iface->dev.kobj;
1195 uevent_env[0] = kasprintf(GFP_ATOMIC,
1196 "%s%s", BATADV_UEV_TYPE_VAR,
1197 batadv_uev_type_str[type]);
1201 uevent_env[1] = kasprintf(GFP_ATOMIC,
1202 "%s%s", BATADV_UEV_ACTION_VAR,
1203 batadv_uev_action_str[action]);
1207 /* If the event is DEL, ignore the data field */
1208 if (action != BATADV_UEV_DEL) {
1209 uevent_env[2] = kasprintf(GFP_ATOMIC,
1210 "%s%s", BATADV_UEV_DATA_VAR, data);
1215 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1217 kfree(uevent_env[0]);
1218 kfree(uevent_env[1]);
1219 kfree(uevent_env[2]);
1222 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1223 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
1224 batadv_uev_type_str[type],
1225 batadv_uev_action_str[action],
1226 (action == BATADV_UEV_DEL ? "NULL" : data), ret);