1 /* Copyright (C) 2010-2016 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/stat.h>
37 #include <linux/stddef.h>
38 #include <linux/string.h>
39 #include <linux/stringify.h>
40 #include <linux/workqueue.h>
42 #include "bridge_loop_avoidance.h"
43 #include "distributed-arp-table.h"
44 #include "gateway_client.h"
45 #include "gateway_common.h"
46 #include "hard-interface.h"
48 #include "network-coding.h"
50 #include "soft-interface.h"
52 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
54 struct device *dev = container_of(obj->parent, struct device, kobj);
56 return to_net_dev(dev);
59 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
61 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
63 return netdev_priv(net_dev);
67 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
68 * @obj: kobject to covert
70 * Return: the associated batadv_priv struct.
72 static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
74 /* VLAN specific attributes are located in the root sysfs folder if they
75 * refer to the untagged VLAN..
77 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
78 return batadv_kobj_to_batpriv(obj);
80 /* ..while the attributes for the tagged vlans are located in
81 * the in the corresponding "vlan%VID" subfolder
83 return batadv_kobj_to_batpriv(obj->parent);
87 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
88 * @bat_priv: the bat priv with all the soft interface information
89 * @obj: kobject to covert
91 * Return: the associated softif_vlan struct if found, NULL otherwise.
93 static struct batadv_softif_vlan *
94 batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
96 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
99 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
100 if (vlan_tmp->kobj != obj)
103 if (!kref_get_unless_zero(&vlan_tmp->refcount))
114 #define BATADV_UEV_TYPE_VAR "BATTYPE="
115 #define BATADV_UEV_ACTION_VAR "BATACTION="
116 #define BATADV_UEV_DATA_VAR "BATDATA="
118 static char *batadv_uev_action_str[] = {
125 static char *batadv_uev_type_str[] = {
130 /* Use this, if you have customized show and store functions for vlan attrs */
131 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
132 struct batadv_attribute batadv_attr_vlan_##_name = { \
133 .attr = {.name = __stringify(_name), \
139 /* Use this, if you have customized show and store functions */
140 #define BATADV_ATTR(_name, _mode, _show, _store) \
141 struct batadv_attribute batadv_attr_##_name = { \
142 .attr = {.name = __stringify(_name), \
148 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
149 ssize_t batadv_store_##_name(struct kobject *kobj, \
150 struct attribute *attr, char *buff, \
153 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
154 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
156 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
157 &bat_priv->_name, net_dev); \
160 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
161 ssize_t batadv_show_##_name(struct kobject *kobj, \
162 struct attribute *attr, char *buff) \
164 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
166 return sprintf(buff, "%s\n", \
167 atomic_read(&bat_priv->_name) == 0 ? \
168 "disabled" : "enabled"); \
171 /* Use this, if you are going to turn a [name] in the soft-interface
172 * (bat_priv) on or off
174 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
175 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
176 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
177 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
178 batadv_store_##_name)
180 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
181 ssize_t batadv_store_##_name(struct kobject *kobj, \
182 struct attribute *attr, char *buff, \
185 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
186 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
188 return __batadv_store_uint_attr(buff, count, _min, _max, \
190 &bat_priv->_var, net_dev, \
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 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
203 /* Use this, if you are going to set [name] in the soft-interface
204 * (bat_priv) to an unsigned integer value
206 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
207 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
208 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
209 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
210 batadv_store_##_name)
212 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
213 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
214 struct attribute *attr, char *buff, \
217 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
218 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
220 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
221 attr, &vlan->_name, \
222 bat_priv->soft_iface); \
224 batadv_softif_vlan_put(vlan); \
228 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
229 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
230 struct attribute *attr, char *buff) \
232 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
233 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
235 size_t res = sprintf(buff, "%s\n", \
236 atomic_read(&vlan->_name) == 0 ? \
237 "disabled" : "enabled"); \
239 batadv_softif_vlan_put(vlan); \
243 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
244 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
245 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
246 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
247 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
248 batadv_store_vlan_##_name)
250 #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
251 ssize_t batadv_store_##_name(struct kobject *kobj, \
252 struct attribute *attr, char *buff, \
255 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
256 struct batadv_hard_iface *hard_iface; \
259 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
263 length = __batadv_store_uint_attr(buff, count, _min, _max, \
266 hard_iface->soft_iface, \
269 batadv_hardif_put(hard_iface); \
273 #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
274 ssize_t batadv_show_##_name(struct kobject *kobj, \
275 struct attribute *attr, char *buff) \
277 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
278 struct batadv_hard_iface *hard_iface; \
281 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
285 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
287 batadv_hardif_put(hard_iface); \
291 /* Use this, if you are going to set [name] in hard_iface to an
292 * unsigned integer value
294 #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
295 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
297 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
298 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
299 batadv_store_##_name)
301 static int batadv_store_bool_attr(char *buff, size_t count,
302 struct net_device *net_dev,
303 const char *attr_name, atomic_t *attr,
310 if (buff[count - 1] == '\n')
311 buff[count - 1] = '\0';
313 if ((strncmp(buff, "1", 2) == 0) ||
314 (strncmp(buff, "enable", 7) == 0) ||
315 (strncmp(buff, "enabled", 8) == 0))
318 if ((strncmp(buff, "0", 2) == 0) ||
319 (strncmp(buff, "disable", 8) == 0) ||
320 (strncmp(buff, "disabled", 9) == 0))
324 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
329 if (atomic_read(attr) == enabled)
332 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
333 atomic_read(attr) == 1 ? "enabled" : "disabled",
334 enabled == 1 ? "enabled" : "disabled");
338 atomic_set(attr, (unsigned int)enabled);
342 static inline ssize_t
343 __batadv_store_bool_attr(char *buff, size_t count,
344 void (*post_func)(struct net_device *),
345 struct attribute *attr,
346 atomic_t *attr_store, struct net_device *net_dev)
351 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
352 attr_store, &changed);
353 if (post_func && changed)
359 static int batadv_store_uint_attr(const char *buff, size_t count,
360 struct net_device *net_dev,
361 struct net_device *slave_dev,
362 const char *attr_name,
363 unsigned int min, unsigned int max,
366 char ifname[IFNAMSIZ + 3] = "";
367 unsigned long uint_val;
370 ret = kstrtoul(buff, 10, &uint_val);
372 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
377 if (uint_val < min) {
378 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
379 attr_name, uint_val, min);
383 if (uint_val > max) {
384 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
385 attr_name, uint_val, max);
389 if (atomic_read(attr) == uint_val)
393 snprintf(ifname, sizeof(ifname), "%s: ", slave_dev->name);
395 batadv_info(net_dev, "%s: %sChanging from: %i to: %lu\n",
396 attr_name, ifname, atomic_read(attr), uint_val);
398 atomic_set(attr, uint_val);
402 static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
404 void (*post_func)(struct net_device *),
405 const struct attribute *attr,
406 atomic_t *attr_store,
407 struct net_device *net_dev,
408 struct net_device *slave_dev)
412 ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
413 attr->name, min, max, attr_store);
414 if (post_func && ret)
420 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
421 struct attribute *attr, char *buff)
423 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
425 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
428 static void batadv_post_gw_reselect(struct net_device *net_dev)
430 struct batadv_priv *bat_priv = netdev_priv(net_dev);
432 batadv_gw_reselect(bat_priv);
435 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
438 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
441 /* GW mode is not available if the routing algorithm in use does not
442 * implement the GW API
444 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
445 !bat_priv->algo_ops->gw.is_eligible)
448 switch (atomic_read(&bat_priv->gw.mode)) {
449 case BATADV_GW_MODE_CLIENT:
450 bytes_written = sprintf(buff, "%s\n",
451 BATADV_GW_MODE_CLIENT_NAME);
453 case BATADV_GW_MODE_SERVER:
454 bytes_written = sprintf(buff, "%s\n",
455 BATADV_GW_MODE_SERVER_NAME);
458 bytes_written = sprintf(buff, "%s\n",
459 BATADV_GW_MODE_OFF_NAME);
463 return bytes_written;
466 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
467 struct attribute *attr, char *buff,
470 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
471 struct batadv_priv *bat_priv = netdev_priv(net_dev);
472 char *curr_gw_mode_str;
473 int gw_mode_tmp = -1;
475 /* toggling GW mode is allowed only if the routing algorithm in use
476 * provides the GW API
478 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
479 !bat_priv->algo_ops->gw.is_eligible)
482 if (buff[count - 1] == '\n')
483 buff[count - 1] = '\0';
485 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
486 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
487 gw_mode_tmp = BATADV_GW_MODE_OFF;
489 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
490 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
491 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
493 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
494 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
495 gw_mode_tmp = BATADV_GW_MODE_SERVER;
497 if (gw_mode_tmp < 0) {
499 "Invalid parameter for 'gw mode' setting received: %s\n",
504 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
507 switch (atomic_read(&bat_priv->gw.mode)) {
508 case BATADV_GW_MODE_CLIENT:
509 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
511 case BATADV_GW_MODE_SERVER:
512 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
515 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
519 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
520 curr_gw_mode_str, buff);
522 /* Invoking batadv_gw_reselect() is not enough to really de-select the
523 * current GW. It will only instruct the gateway client code to perform
524 * a re-election the next time that this is needed.
526 * When gw client mode is being switched off the current GW must be
527 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
528 * client mode re-activation. This is operation is performed in
529 * batadv_gw_check_client_stop().
531 batadv_gw_reselect(bat_priv);
532 /* always call batadv_gw_check_client_stop() before changing the gateway
535 batadv_gw_check_client_stop(bat_priv);
536 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
537 batadv_gw_tvlv_container_update(bat_priv);
541 static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
542 struct attribute *attr, char *buff)
544 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
546 /* GW selection class is not available if the routing algorithm in use
547 * does not implement the GW API
549 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
550 !bat_priv->algo_ops->gw.is_eligible)
553 if (bat_priv->algo_ops->gw.show_sel_class)
554 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
556 return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
559 static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
560 struct attribute *attr, char *buff,
563 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
565 /* setting the GW selection class is allowed only if the routing
566 * algorithm in use implements the GW API
568 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
569 !bat_priv->algo_ops->gw.is_eligible)
572 if (buff[count - 1] == '\n')
573 buff[count - 1] = '\0';
575 if (bat_priv->algo_ops->gw.store_sel_class)
576 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
579 return __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
580 batadv_post_gw_reselect, attr,
581 &bat_priv->gw.sel_class,
582 bat_priv->soft_iface, NULL);
585 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
586 struct attribute *attr, char *buff)
588 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
591 down = atomic_read(&bat_priv->gw.bandwidth_down);
592 up = atomic_read(&bat_priv->gw.bandwidth_up);
594 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
595 down % 10, up / 10, up % 10);
598 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
599 struct attribute *attr, char *buff,
602 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
604 if (buff[count - 1] == '\n')
605 buff[count - 1] = '\0';
607 return batadv_gw_bandwidth_set(net_dev, buff, count);
611 * batadv_show_isolation_mark - print the current isolation mark/mask
612 * @kobj: kobject representing the private mesh sysfs directory
613 * @attr: the batman-adv attribute the user is interacting with
614 * @buff: the buffer that will contain the data to send back to the user
616 * Return: the number of bytes written into 'buff' on success or a negative
617 * error code in case of failure
619 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
620 struct attribute *attr, char *buff)
622 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
624 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
625 bat_priv->isolation_mark_mask);
629 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
631 * @kobj: kobject representing the private mesh sysfs directory
632 * @attr: the batman-adv attribute the user is interacting with
633 * @buff: the buffer containing the user data
634 * @count: number of bytes in the buffer
636 * Return: 'count' on success or a negative error code in case of failure
638 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
639 struct attribute *attr, char *buff,
642 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
643 struct batadv_priv *bat_priv = netdev_priv(net_dev);
647 /* parse the mask if it has been specified, otherwise assume the mask is
648 * the biggest possible
651 mask_ptr = strchr(buff, '/');
656 /* the mask must be entered in hex base as it is going to be a
657 * bitmask and not a prefix length
659 if (kstrtou32(mask_ptr, 16, &mask) < 0)
663 /* the mark can be entered in any base */
664 if (kstrtou32(buff, 0, &mark) < 0)
667 bat_priv->isolation_mark_mask = mask;
668 /* erase bits not covered by the mask */
669 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
672 "New skb mark for extended isolation: %#.8x/%#.8x\n",
673 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
678 BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
679 BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
680 #ifdef CONFIG_BATMAN_ADV_BLA
681 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR,
682 batadv_bla_status_update);
684 #ifdef CONFIG_BATMAN_ADV_DAT
685 BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
686 batadv_dat_status_update);
688 BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
689 static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
690 static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
691 batadv_store_gw_mode);
692 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, S_IRUGO | S_IWUSR,
693 2 * BATADV_JITTER, INT_MAX, NULL);
694 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, S_IRUGO | S_IWUSR, 0,
695 BATADV_TQ_MAX_VALUE, NULL);
696 static BATADV_ATTR(gw_sel_class, S_IRUGO | S_IWUSR, batadv_show_gw_sel_class,
697 batadv_store_gw_sel_class);
698 static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
699 batadv_store_gw_bwidth);
700 #ifdef CONFIG_BATMAN_ADV_MCAST
701 BATADV_ATTR_SIF_BOOL(multicast_mode, S_IRUGO | S_IWUSR, NULL);
703 #ifdef CONFIG_BATMAN_ADV_DEBUG
704 BATADV_ATTR_SIF_UINT(log_level, log_level, S_IRUGO | S_IWUSR, 0,
705 BATADV_DBG_ALL, NULL);
707 #ifdef CONFIG_BATMAN_ADV_NC
708 BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
709 batadv_nc_status_update);
711 static BATADV_ATTR(isolation_mark, S_IRUGO | S_IWUSR,
712 batadv_show_isolation_mark, batadv_store_isolation_mark);
714 static struct batadv_attribute *batadv_mesh_attrs[] = {
715 &batadv_attr_aggregated_ogms,
716 &batadv_attr_bonding,
717 #ifdef CONFIG_BATMAN_ADV_BLA
718 &batadv_attr_bridge_loop_avoidance,
720 #ifdef CONFIG_BATMAN_ADV_DAT
721 &batadv_attr_distributed_arp_table,
723 #ifdef CONFIG_BATMAN_ADV_MCAST
724 &batadv_attr_multicast_mode,
726 &batadv_attr_fragmentation,
727 &batadv_attr_routing_algo,
728 &batadv_attr_gw_mode,
729 &batadv_attr_orig_interval,
730 &batadv_attr_hop_penalty,
731 &batadv_attr_gw_sel_class,
732 &batadv_attr_gw_bandwidth,
733 #ifdef CONFIG_BATMAN_ADV_DEBUG
734 &batadv_attr_log_level,
736 #ifdef CONFIG_BATMAN_ADV_NC
737 &batadv_attr_network_coding,
739 &batadv_attr_isolation_mark,
743 BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
745 /* array of vlan specific sysfs attributes */
746 static struct batadv_attribute *batadv_vlan_attrs[] = {
747 &batadv_attr_vlan_ap_isolation,
751 int batadv_sysfs_add_meshif(struct net_device *dev)
753 struct kobject *batif_kobject = &dev->dev.kobj;
754 struct batadv_priv *bat_priv = netdev_priv(dev);
755 struct batadv_attribute **bat_attr;
758 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
760 if (!bat_priv->mesh_obj) {
761 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
762 BATADV_SYSFS_IF_MESH_SUBDIR);
766 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
767 err = sysfs_create_file(bat_priv->mesh_obj,
768 &((*bat_attr)->attr));
770 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
771 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
772 ((*bat_attr)->attr).name);
780 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
781 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
783 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
784 kobject_del(bat_priv->mesh_obj);
785 kobject_put(bat_priv->mesh_obj);
786 bat_priv->mesh_obj = NULL;
791 void batadv_sysfs_del_meshif(struct net_device *dev)
793 struct batadv_priv *bat_priv = netdev_priv(dev);
794 struct batadv_attribute **bat_attr;
796 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
797 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
799 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
800 kobject_del(bat_priv->mesh_obj);
801 kobject_put(bat_priv->mesh_obj);
802 bat_priv->mesh_obj = NULL;
806 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
807 * @dev: netdev of the mesh interface
808 * @vlan: private data of the newly added VLAN interface
810 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
812 int batadv_sysfs_add_vlan(struct net_device *dev,
813 struct batadv_softif_vlan *vlan)
815 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
816 struct batadv_priv *bat_priv = netdev_priv(dev);
817 struct batadv_attribute **bat_attr;
820 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
821 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
822 vlan->vid & VLAN_VID_MASK);
824 vlan->kobj = kobject_create_and_add(vlan_subdir,
827 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
828 dev->name, vlan_subdir);
832 /* the untagged LAN uses the root folder to store its "VLAN
833 * specific attributes"
835 vlan->kobj = bat_priv->mesh_obj;
836 kobject_get(bat_priv->mesh_obj);
839 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
840 err = sysfs_create_file(vlan->kobj,
841 &((*bat_attr)->attr));
843 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
844 dev->name, vlan_subdir,
845 ((*bat_attr)->attr).name);
853 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
854 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
856 if (vlan->kobj != bat_priv->mesh_obj) {
857 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
858 kobject_del(vlan->kobj);
860 kobject_put(vlan->kobj);
867 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
868 * @bat_priv: the bat priv with all the soft interface information
869 * @vlan: the private data of the VLAN to destroy
871 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
872 struct batadv_softif_vlan *vlan)
874 struct batadv_attribute **bat_attr;
876 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
877 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
879 if (vlan->kobj != bat_priv->mesh_obj) {
880 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
881 kobject_del(vlan->kobj);
883 kobject_put(vlan->kobj);
887 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
888 struct attribute *attr, char *buff)
890 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
891 struct batadv_hard_iface *hard_iface;
895 hard_iface = batadv_hardif_get_by_netdev(net_dev);
899 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
902 ifname = hard_iface->soft_iface->name;
904 length = sprintf(buff, "%s\n", ifname);
906 batadv_hardif_put(hard_iface);
912 * batadv_store_mesh_iface_finish - store new hardif mesh_iface state
913 * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
914 * @ifname: name of soft-interface to modify
916 * Changes the parts of the hard+soft interface which can not be modified under
917 * sysfs lock (to prevent deadlock situations).
919 * Return: 0 on success, 0 < on failure
921 static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
922 char ifname[IFNAMSIZ])
924 struct net *net = dev_net(net_dev);
925 struct batadv_hard_iface *hard_iface;
931 hard_iface = batadv_hardif_get_by_netdev(net_dev);
935 if (strncmp(ifname, "none", 4) == 0)
936 status_tmp = BATADV_IF_NOT_IN_USE;
938 status_tmp = BATADV_IF_I_WANT_YOU;
940 if (hard_iface->if_status == status_tmp)
943 if ((hard_iface->soft_iface) &&
944 (strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0))
947 if (status_tmp == BATADV_IF_NOT_IN_USE) {
948 batadv_hardif_disable_interface(hard_iface,
949 BATADV_IF_CLEANUP_AUTO);
953 /* if the interface already is in use */
954 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
955 batadv_hardif_disable_interface(hard_iface,
956 BATADV_IF_CLEANUP_AUTO);
958 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
960 batadv_hardif_put(hard_iface);
965 * batadv_store_mesh_iface_work - store new hardif mesh_iface state
966 * @work: work queue item
968 * Changes the parts of the hard+soft interface which can not be modified under
969 * sysfs lock (to prevent deadlock situations).
971 static void batadv_store_mesh_iface_work(struct work_struct *work)
973 struct batadv_store_mesh_work *store_work;
976 store_work = container_of(work, struct batadv_store_mesh_work, work);
979 ret = batadv_store_mesh_iface_finish(store_work->net_dev,
980 store_work->soft_iface_name);
984 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
985 store_work->soft_iface_name, store_work->net_dev->name,
988 dev_put(store_work->net_dev);
992 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
993 struct attribute *attr, char *buff,
996 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
997 struct batadv_store_mesh_work *store_work;
999 if (buff[count - 1] == '\n')
1000 buff[count - 1] = '\0';
1002 if (strlen(buff) >= IFNAMSIZ) {
1003 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
1008 store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
1013 INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1014 store_work->net_dev = net_dev;
1015 strlcpy(store_work->soft_iface_name, buff,
1016 sizeof(store_work->soft_iface_name));
1018 queue_work(batadv_event_workqueue, &store_work->work);
1023 static ssize_t batadv_show_iface_status(struct kobject *kobj,
1024 struct attribute *attr, char *buff)
1026 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1027 struct batadv_hard_iface *hard_iface;
1030 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1034 switch (hard_iface->if_status) {
1035 case BATADV_IF_TO_BE_REMOVED:
1036 length = sprintf(buff, "disabling\n");
1038 case BATADV_IF_INACTIVE:
1039 length = sprintf(buff, "inactive\n");
1041 case BATADV_IF_ACTIVE:
1042 length = sprintf(buff, "active\n");
1044 case BATADV_IF_TO_BE_ACTIVATED:
1045 length = sprintf(buff, "enabling\n");
1047 case BATADV_IF_NOT_IN_USE:
1049 length = sprintf(buff, "not in use\n");
1053 batadv_hardif_put(hard_iface);
1058 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1061 * batadv_store_throughput_override - parse and store throughput override
1062 * entered by the user
1063 * @kobj: kobject representing the private mesh sysfs directory
1064 * @attr: the batman-adv attribute the user is interacting with
1065 * @buff: the buffer containing the user data
1066 * @count: number of bytes in the buffer
1068 * Return: 'count' on success or a negative error code in case of failure
1070 static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1071 struct attribute *attr,
1072 char *buff, size_t count)
1074 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1075 struct batadv_hard_iface *hard_iface;
1077 u32 old_tp_override;
1080 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1084 if (buff[count - 1] == '\n')
1085 buff[count - 1] = '\0';
1087 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1092 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1093 if (old_tp_override == tp_override)
1096 batadv_info(hard_iface->soft_iface,
1097 "%s: %s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1098 "throughput_override", net_dev->name,
1099 old_tp_override / 10, old_tp_override % 10,
1100 tp_override / 10, tp_override % 10);
1102 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1105 batadv_hardif_put(hard_iface);
1109 static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1110 struct attribute *attr,
1113 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1114 struct batadv_hard_iface *hard_iface;
1117 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1121 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1123 batadv_hardif_put(hard_iface);
1124 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1130 static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
1131 batadv_store_mesh_iface);
1132 static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
1133 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1134 BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, S_IRUGO | S_IWUSR,
1135 2 * BATADV_JITTER, INT_MAX, NULL);
1136 static BATADV_ATTR(throughput_override, S_IRUGO | S_IWUSR,
1137 batadv_show_throughput_override,
1138 batadv_store_throughput_override);
1141 static struct batadv_attribute *batadv_batman_attrs[] = {
1142 &batadv_attr_mesh_iface,
1143 &batadv_attr_iface_status,
1144 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1145 &batadv_attr_elp_interval,
1146 &batadv_attr_throughput_override,
1151 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
1153 struct kobject *hardif_kobject = &dev->dev.kobj;
1154 struct batadv_attribute **bat_attr;
1157 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1161 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
1162 BATADV_SYSFS_IF_BAT_SUBDIR);
1166 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
1167 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1169 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
1170 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
1171 ((*bat_attr)->attr).name);
1179 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
1180 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1185 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
1187 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1188 kobject_del(*hardif_obj);
1189 kobject_put(*hardif_obj);
1193 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
1194 enum batadv_uev_action action, const char *data)
1197 struct kobject *bat_kobj;
1198 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1200 bat_kobj = &bat_priv->soft_iface->dev.kobj;
1202 uevent_env[0] = kasprintf(GFP_ATOMIC,
1203 "%s%s", BATADV_UEV_TYPE_VAR,
1204 batadv_uev_type_str[type]);
1208 uevent_env[1] = kasprintf(GFP_ATOMIC,
1209 "%s%s", BATADV_UEV_ACTION_VAR,
1210 batadv_uev_action_str[action]);
1214 /* If the event is DEL, ignore the data field */
1215 if (action != BATADV_UEV_DEL) {
1216 uevent_env[2] = kasprintf(GFP_ATOMIC,
1217 "%s%s", BATADV_UEV_DATA_VAR, data);
1222 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1224 kfree(uevent_env[0]);
1225 kfree(uevent_env[1]);
1226 kfree(uevent_env[2]);
1229 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1230 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
1231 batadv_uev_type_str[type],
1232 batadv_uev_action_str[action],
1233 (action == BATADV_UEV_DEL ? "NULL" : data), ret);