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/kref.h>
29 #include <linux/kernel.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>
41 #include "distributed-arp-table.h"
42 #include "gateway_client.h"
43 #include "gateway_common.h"
44 #include "bridge_loop_avoidance.h"
45 #include "hard-interface.h"
46 #include "network-coding.h"
48 #include "soft-interface.h"
50 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
52 struct device *dev = container_of(obj->parent, struct device, kobj);
54 return to_net_dev(dev);
57 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
59 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
61 return netdev_priv(net_dev);
65 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
66 * @obj: kobject to covert
68 * Return: the associated batadv_priv struct.
70 static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
72 /* VLAN specific attributes are located in the root sysfs folder if they
73 * refer to the untagged VLAN..
75 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
76 return batadv_kobj_to_batpriv(obj);
78 /* ..while the attributes for the tagged vlans are located in
79 * the in the corresponding "vlan%VID" subfolder
81 return batadv_kobj_to_batpriv(obj->parent);
85 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
86 * @bat_priv: the bat priv with all the soft interface information
87 * @obj: kobject to covert
89 * Return: the associated softif_vlan struct if found, NULL otherwise.
91 static struct batadv_softif_vlan *
92 batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
94 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
97 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
98 if (vlan_tmp->kobj != obj)
101 if (!kref_get_unless_zero(&vlan_tmp->refcount))
112 #define BATADV_UEV_TYPE_VAR "BATTYPE="
113 #define BATADV_UEV_ACTION_VAR "BATACTION="
114 #define BATADV_UEV_DATA_VAR "BATDATA="
116 static char *batadv_uev_action_str[] = {
123 static char *batadv_uev_type_str[] = {
128 /* Use this, if you have customized show and store functions for vlan attrs */
129 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
130 struct batadv_attribute batadv_attr_vlan_##_name = { \
131 .attr = {.name = __stringify(_name), \
137 /* Use this, if you have customized show and store functions */
138 #define BATADV_ATTR(_name, _mode, _show, _store) \
139 struct batadv_attribute batadv_attr_##_name = { \
140 .attr = {.name = __stringify(_name), \
146 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
147 ssize_t batadv_store_##_name(struct kobject *kobj, \
148 struct attribute *attr, char *buff, \
151 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
152 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
154 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
155 &bat_priv->_name, net_dev); \
158 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
159 ssize_t batadv_show_##_name(struct kobject *kobj, \
160 struct attribute *attr, char *buff) \
162 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
164 return sprintf(buff, "%s\n", \
165 atomic_read(&bat_priv->_name) == 0 ? \
166 "disabled" : "enabled"); \
169 /* Use this, if you are going to turn a [name] in the soft-interface
170 * (bat_priv) on or off
172 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
173 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
174 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
175 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
176 batadv_store_##_name)
178 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
179 ssize_t batadv_store_##_name(struct kobject *kobj, \
180 struct attribute *attr, char *buff, \
183 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
184 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
186 return __batadv_store_uint_attr(buff, count, _min, _max, \
188 &bat_priv->_var, net_dev); \
191 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
192 ssize_t batadv_show_##_name(struct kobject *kobj, \
193 struct attribute *attr, char *buff) \
195 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
197 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
200 /* Use this, if you are going to set [name] in the soft-interface
201 * (bat_priv) to an unsigned integer value
203 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
204 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
205 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
206 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
207 batadv_store_##_name)
209 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
210 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
211 struct attribute *attr, char *buff, \
214 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
215 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
217 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
218 attr, &vlan->_name, \
219 bat_priv->soft_iface); \
221 batadv_softif_vlan_put(vlan); \
225 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
226 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
227 struct attribute *attr, char *buff) \
229 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
230 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
232 size_t res = sprintf(buff, "%s\n", \
233 atomic_read(&vlan->_name) == 0 ? \
234 "disabled" : "enabled"); \
236 batadv_softif_vlan_put(vlan); \
240 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
241 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
242 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
243 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
244 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
245 batadv_store_vlan_##_name)
247 #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
248 ssize_t batadv_store_##_name(struct kobject *kobj, \
249 struct attribute *attr, char *buff, \
252 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
253 struct batadv_hard_iface *hard_iface; \
256 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
260 length = __batadv_store_uint_attr(buff, count, _min, _max, \
262 &hard_iface->_var, net_dev); \
264 batadv_hardif_put(hard_iface); \
268 #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
269 ssize_t batadv_show_##_name(struct kobject *kobj, \
270 struct attribute *attr, char *buff) \
272 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
273 struct batadv_hard_iface *hard_iface; \
276 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
280 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
282 batadv_hardif_put(hard_iface); \
286 /* Use this, if you are going to set [name] in hard_iface to an
287 * unsigned integer value
289 #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
290 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
292 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
293 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
294 batadv_store_##_name)
296 static int batadv_store_bool_attr(char *buff, size_t count,
297 struct net_device *net_dev,
298 const char *attr_name, atomic_t *attr,
305 if (buff[count - 1] == '\n')
306 buff[count - 1] = '\0';
308 if ((strncmp(buff, "1", 2) == 0) ||
309 (strncmp(buff, "enable", 7) == 0) ||
310 (strncmp(buff, "enabled", 8) == 0))
313 if ((strncmp(buff, "0", 2) == 0) ||
314 (strncmp(buff, "disable", 8) == 0) ||
315 (strncmp(buff, "disabled", 9) == 0))
319 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
324 if (atomic_read(attr) == enabled)
327 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
328 atomic_read(attr) == 1 ? "enabled" : "disabled",
329 enabled == 1 ? "enabled" : "disabled");
333 atomic_set(attr, (unsigned int)enabled);
337 static inline ssize_t
338 __batadv_store_bool_attr(char *buff, size_t count,
339 void (*post_func)(struct net_device *),
340 struct attribute *attr,
341 atomic_t *attr_store, struct net_device *net_dev)
346 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
347 attr_store, &changed);
348 if (post_func && changed)
354 static int batadv_store_uint_attr(const char *buff, size_t count,
355 struct net_device *net_dev,
356 const char *attr_name,
357 unsigned int min, unsigned int max,
360 unsigned long uint_val;
363 ret = kstrtoul(buff, 10, &uint_val);
365 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
370 if (uint_val < min) {
371 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
372 attr_name, uint_val, min);
376 if (uint_val > max) {
377 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
378 attr_name, uint_val, max);
382 if (atomic_read(attr) == uint_val)
385 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
386 attr_name, atomic_read(attr), uint_val);
388 atomic_set(attr, uint_val);
392 static inline ssize_t
393 __batadv_store_uint_attr(const char *buff, size_t count,
395 void (*post_func)(struct net_device *),
396 const struct attribute *attr,
397 atomic_t *attr_store, struct net_device *net_dev)
401 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
403 if (post_func && ret)
409 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
410 struct attribute *attr, char *buff)
412 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
414 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
417 static void batadv_post_gw_reselect(struct net_device *net_dev)
419 struct batadv_priv *bat_priv = netdev_priv(net_dev);
421 batadv_gw_reselect(bat_priv);
424 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
427 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
430 switch (atomic_read(&bat_priv->gw_mode)) {
431 case BATADV_GW_MODE_CLIENT:
432 bytes_written = sprintf(buff, "%s\n",
433 BATADV_GW_MODE_CLIENT_NAME);
435 case BATADV_GW_MODE_SERVER:
436 bytes_written = sprintf(buff, "%s\n",
437 BATADV_GW_MODE_SERVER_NAME);
440 bytes_written = sprintf(buff, "%s\n",
441 BATADV_GW_MODE_OFF_NAME);
445 return bytes_written;
448 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
449 struct attribute *attr, char *buff,
452 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
453 struct batadv_priv *bat_priv = netdev_priv(net_dev);
454 char *curr_gw_mode_str;
455 int gw_mode_tmp = -1;
457 if (buff[count - 1] == '\n')
458 buff[count - 1] = '\0';
460 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
461 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
462 gw_mode_tmp = BATADV_GW_MODE_OFF;
464 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
465 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
466 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
468 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
469 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
470 gw_mode_tmp = BATADV_GW_MODE_SERVER;
472 if (gw_mode_tmp < 0) {
474 "Invalid parameter for 'gw mode' setting received: %s\n",
479 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
482 switch (atomic_read(&bat_priv->gw_mode)) {
483 case BATADV_GW_MODE_CLIENT:
484 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
486 case BATADV_GW_MODE_SERVER:
487 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
490 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
494 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
495 curr_gw_mode_str, buff);
497 /* Invoking batadv_gw_reselect() is not enough to really de-select the
498 * current GW. It will only instruct the gateway client code to perform
499 * a re-election the next time that this is needed.
501 * When gw client mode is being switched off the current GW must be
502 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
503 * client mode re-activation. This is operation is performed in
504 * batadv_gw_check_client_stop().
506 batadv_gw_reselect(bat_priv);
507 /* always call batadv_gw_check_client_stop() before changing the gateway
510 batadv_gw_check_client_stop(bat_priv);
511 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
512 batadv_gw_tvlv_container_update(bat_priv);
516 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
517 struct attribute *attr, char *buff)
519 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
522 down = atomic_read(&bat_priv->gw.bandwidth_down);
523 up = atomic_read(&bat_priv->gw.bandwidth_up);
525 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
526 down % 10, up / 10, up % 10);
529 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
530 struct attribute *attr, char *buff,
533 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
535 if (buff[count - 1] == '\n')
536 buff[count - 1] = '\0';
538 return batadv_gw_bandwidth_set(net_dev, buff, count);
542 * batadv_show_isolation_mark - print the current isolation mark/mask
543 * @kobj: kobject representing the private mesh sysfs directory
544 * @attr: the batman-adv attribute the user is interacting with
545 * @buff: the buffer that will contain the data to send back to the user
547 * Return: the number of bytes written into 'buff' on success or a negative
548 * error code in case of failure
550 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
551 struct attribute *attr, char *buff)
553 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
555 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
556 bat_priv->isolation_mark_mask);
560 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
562 * @kobj: kobject representing the private mesh sysfs directory
563 * @attr: the batman-adv attribute the user is interacting with
564 * @buff: the buffer containing the user data
565 * @count: number of bytes in the buffer
567 * Return: 'count' on success or a negative error code in case of failure
569 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
570 struct attribute *attr, char *buff,
573 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
574 struct batadv_priv *bat_priv = netdev_priv(net_dev);
578 /* parse the mask if it has been specified, otherwise assume the mask is
579 * the biggest possible
582 mask_ptr = strchr(buff, '/');
587 /* the mask must be entered in hex base as it is going to be a
588 * bitmask and not a prefix length
590 if (kstrtou32(mask_ptr, 16, &mask) < 0)
594 /* the mark can be entered in any base */
595 if (kstrtou32(buff, 0, &mark) < 0)
598 bat_priv->isolation_mark_mask = mask;
599 /* erase bits not covered by the mask */
600 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
603 "New skb mark for extended isolation: %#.8x/%#.8x\n",
604 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
609 BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
610 BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
611 #ifdef CONFIG_BATMAN_ADV_BLA
612 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR,
613 batadv_bla_status_update);
615 #ifdef CONFIG_BATMAN_ADV_DAT
616 BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
617 batadv_dat_status_update);
619 BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
620 static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
621 static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
622 batadv_store_gw_mode);
623 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, S_IRUGO | S_IWUSR,
624 2 * BATADV_JITTER, INT_MAX, NULL);
625 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, S_IRUGO | S_IWUSR, 0,
626 BATADV_TQ_MAX_VALUE, NULL);
627 BATADV_ATTR_SIF_UINT(gw_sel_class, gw_sel_class, S_IRUGO | S_IWUSR, 1,
628 BATADV_TQ_MAX_VALUE, batadv_post_gw_reselect);
629 static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
630 batadv_store_gw_bwidth);
631 #ifdef CONFIG_BATMAN_ADV_MCAST
632 BATADV_ATTR_SIF_BOOL(multicast_mode, S_IRUGO | S_IWUSR, NULL);
634 #ifdef CONFIG_BATMAN_ADV_DEBUG
635 BATADV_ATTR_SIF_UINT(log_level, log_level, S_IRUGO | S_IWUSR, 0,
636 BATADV_DBG_ALL, NULL);
638 #ifdef CONFIG_BATMAN_ADV_NC
639 BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
640 batadv_nc_status_update);
642 static BATADV_ATTR(isolation_mark, S_IRUGO | S_IWUSR,
643 batadv_show_isolation_mark, batadv_store_isolation_mark);
645 static struct batadv_attribute *batadv_mesh_attrs[] = {
646 &batadv_attr_aggregated_ogms,
647 &batadv_attr_bonding,
648 #ifdef CONFIG_BATMAN_ADV_BLA
649 &batadv_attr_bridge_loop_avoidance,
651 #ifdef CONFIG_BATMAN_ADV_DAT
652 &batadv_attr_distributed_arp_table,
654 #ifdef CONFIG_BATMAN_ADV_MCAST
655 &batadv_attr_multicast_mode,
657 &batadv_attr_fragmentation,
658 &batadv_attr_routing_algo,
659 &batadv_attr_gw_mode,
660 &batadv_attr_orig_interval,
661 &batadv_attr_hop_penalty,
662 &batadv_attr_gw_sel_class,
663 &batadv_attr_gw_bandwidth,
664 #ifdef CONFIG_BATMAN_ADV_DEBUG
665 &batadv_attr_log_level,
667 #ifdef CONFIG_BATMAN_ADV_NC
668 &batadv_attr_network_coding,
670 &batadv_attr_isolation_mark,
674 BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
676 /* array of vlan specific sysfs attributes */
677 static struct batadv_attribute *batadv_vlan_attrs[] = {
678 &batadv_attr_vlan_ap_isolation,
682 int batadv_sysfs_add_meshif(struct net_device *dev)
684 struct kobject *batif_kobject = &dev->dev.kobj;
685 struct batadv_priv *bat_priv = netdev_priv(dev);
686 struct batadv_attribute **bat_attr;
689 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
691 if (!bat_priv->mesh_obj) {
692 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
693 BATADV_SYSFS_IF_MESH_SUBDIR);
697 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
698 err = sysfs_create_file(bat_priv->mesh_obj,
699 &((*bat_attr)->attr));
701 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
702 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
703 ((*bat_attr)->attr).name);
711 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
712 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
714 kobject_put(bat_priv->mesh_obj);
715 bat_priv->mesh_obj = NULL;
720 void batadv_sysfs_del_meshif(struct net_device *dev)
722 struct batadv_priv *bat_priv = netdev_priv(dev);
723 struct batadv_attribute **bat_attr;
725 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
726 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
728 kobject_put(bat_priv->mesh_obj);
729 bat_priv->mesh_obj = NULL;
733 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
734 * @dev: netdev of the mesh interface
735 * @vlan: private data of the newly added VLAN interface
737 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
739 int batadv_sysfs_add_vlan(struct net_device *dev,
740 struct batadv_softif_vlan *vlan)
742 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
743 struct batadv_priv *bat_priv = netdev_priv(dev);
744 struct batadv_attribute **bat_attr;
747 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
748 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
749 vlan->vid & VLAN_VID_MASK);
751 vlan->kobj = kobject_create_and_add(vlan_subdir,
754 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
755 dev->name, vlan_subdir);
759 /* the untagged LAN uses the root folder to store its "VLAN
760 * specific attributes"
762 vlan->kobj = bat_priv->mesh_obj;
763 kobject_get(bat_priv->mesh_obj);
766 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
767 err = sysfs_create_file(vlan->kobj,
768 &((*bat_attr)->attr));
770 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
771 dev->name, vlan_subdir,
772 ((*bat_attr)->attr).name);
780 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
781 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
783 kobject_put(vlan->kobj);
790 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
791 * @bat_priv: the bat priv with all the soft interface information
792 * @vlan: the private data of the VLAN to destroy
794 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
795 struct batadv_softif_vlan *vlan)
797 struct batadv_attribute **bat_attr;
799 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
800 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
802 kobject_put(vlan->kobj);
806 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
807 struct attribute *attr, char *buff)
809 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
810 struct batadv_hard_iface *hard_iface;
814 hard_iface = batadv_hardif_get_by_netdev(net_dev);
818 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
821 ifname = hard_iface->soft_iface->name;
823 length = sprintf(buff, "%s\n", ifname);
825 batadv_hardif_put(hard_iface);
830 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
831 struct attribute *attr, char *buff,
834 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
835 struct net *net = dev_net(net_dev);
836 struct batadv_hard_iface *hard_iface;
840 hard_iface = batadv_hardif_get_by_netdev(net_dev);
844 if (buff[count - 1] == '\n')
845 buff[count - 1] = '\0';
847 if (strlen(buff) >= IFNAMSIZ) {
848 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
850 batadv_hardif_put(hard_iface);
854 if (strncmp(buff, "none", 4) == 0)
855 status_tmp = BATADV_IF_NOT_IN_USE;
857 status_tmp = BATADV_IF_I_WANT_YOU;
859 if (hard_iface->if_status == status_tmp)
862 if ((hard_iface->soft_iface) &&
863 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
868 if (status_tmp == BATADV_IF_NOT_IN_USE) {
869 batadv_hardif_disable_interface(hard_iface,
870 BATADV_IF_CLEANUP_AUTO);
874 /* if the interface already is in use */
875 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
876 batadv_hardif_disable_interface(hard_iface,
877 BATADV_IF_CLEANUP_AUTO);
879 ret = batadv_hardif_enable_interface(hard_iface, net, buff);
884 batadv_hardif_put(hard_iface);
888 static ssize_t batadv_show_iface_status(struct kobject *kobj,
889 struct attribute *attr, char *buff)
891 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
892 struct batadv_hard_iface *hard_iface;
895 hard_iface = batadv_hardif_get_by_netdev(net_dev);
899 switch (hard_iface->if_status) {
900 case BATADV_IF_TO_BE_REMOVED:
901 length = sprintf(buff, "disabling\n");
903 case BATADV_IF_INACTIVE:
904 length = sprintf(buff, "inactive\n");
906 case BATADV_IF_ACTIVE:
907 length = sprintf(buff, "active\n");
909 case BATADV_IF_TO_BE_ACTIVATED:
910 length = sprintf(buff, "enabling\n");
912 case BATADV_IF_NOT_IN_USE:
914 length = sprintf(buff, "not in use\n");
918 batadv_hardif_put(hard_iface);
923 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
926 * batadv_store_throughput_override - parse and store throughput override
927 * entered by the user
928 * @kobj: kobject representing the private mesh sysfs directory
929 * @attr: the batman-adv attribute the user is interacting with
930 * @buff: the buffer containing the user data
931 * @count: number of bytes in the buffer
933 * Return: 'count' on success or a negative error code in case of failure
935 static ssize_t batadv_store_throughput_override(struct kobject *kobj,
936 struct attribute *attr,
937 char *buff, size_t count)
939 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
940 struct batadv_hard_iface *hard_iface;
945 hard_iface = batadv_hardif_get_by_netdev(net_dev);
949 if (buff[count - 1] == '\n')
950 buff[count - 1] = '\0';
952 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
957 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
958 if (old_tp_override == tp_override)
961 batadv_info(net_dev, "%s: Changing from: %u.%u MBit to: %u.%u MBit\n",
962 "throughput_override",
963 old_tp_override / 10, old_tp_override % 10,
964 tp_override / 10, tp_override % 10);
966 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
969 batadv_hardif_put(hard_iface);
973 static ssize_t batadv_show_throughput_override(struct kobject *kobj,
974 struct attribute *attr,
977 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
978 struct batadv_hard_iface *hard_iface;
981 hard_iface = batadv_hardif_get_by_netdev(net_dev);
985 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
987 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
993 static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
994 batadv_store_mesh_iface);
995 static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
996 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
997 BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, S_IRUGO | S_IWUSR,
998 2 * BATADV_JITTER, INT_MAX, NULL);
999 static BATADV_ATTR(throughput_override, S_IRUGO | S_IWUSR,
1000 batadv_show_throughput_override,
1001 batadv_store_throughput_override);
1004 static struct batadv_attribute *batadv_batman_attrs[] = {
1005 &batadv_attr_mesh_iface,
1006 &batadv_attr_iface_status,
1007 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1008 &batadv_attr_elp_interval,
1009 &batadv_attr_throughput_override,
1014 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
1016 struct kobject *hardif_kobject = &dev->dev.kobj;
1017 struct batadv_attribute **bat_attr;
1020 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1024 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
1025 BATADV_SYSFS_IF_BAT_SUBDIR);
1029 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
1030 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1032 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
1033 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
1034 ((*bat_attr)->attr).name);
1042 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
1043 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1048 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
1050 kobject_put(*hardif_obj);
1054 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
1055 enum batadv_uev_action action, const char *data)
1058 struct kobject *bat_kobj;
1059 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1061 bat_kobj = &bat_priv->soft_iface->dev.kobj;
1063 uevent_env[0] = kasprintf(GFP_ATOMIC,
1064 "%s%s", BATADV_UEV_TYPE_VAR,
1065 batadv_uev_type_str[type]);
1069 uevent_env[1] = kasprintf(GFP_ATOMIC,
1070 "%s%s", BATADV_UEV_ACTION_VAR,
1071 batadv_uev_action_str[action]);
1075 /* If the event is DEL, ignore the data field */
1076 if (action != BATADV_UEV_DEL) {
1077 uevent_env[2] = kasprintf(GFP_ATOMIC,
1078 "%s%s", BATADV_UEV_DATA_VAR, data);
1083 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1085 kfree(uevent_env[0]);
1086 kfree(uevent_env[1]);
1087 kfree(uevent_env[2]);
1090 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1091 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
1092 batadv_uev_type_str[type],
1093 batadv_uev_action_str[action],
1094 (action == BATADV_UEV_DEL ? "NULL" : data), ret);