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[] = {
122 static char *batadv_uev_type_str[] = {
126 /* Use this, if you have customized show and store functions for vlan attrs */
127 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
128 struct batadv_attribute batadv_attr_vlan_##_name = { \
129 .attr = {.name = __stringify(_name), \
135 /* Use this, if you have customized show and store functions */
136 #define BATADV_ATTR(_name, _mode, _show, _store) \
137 struct batadv_attribute batadv_attr_##_name = { \
138 .attr = {.name = __stringify(_name), \
144 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
145 ssize_t batadv_store_##_name(struct kobject *kobj, \
146 struct attribute *attr, char *buff, \
149 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
150 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
152 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
153 &bat_priv->_name, net_dev); \
156 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
157 ssize_t batadv_show_##_name(struct kobject *kobj, \
158 struct attribute *attr, char *buff) \
160 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
162 return sprintf(buff, "%s\n", \
163 atomic_read(&bat_priv->_name) == 0 ? \
164 "disabled" : "enabled"); \
167 /* Use this, if you are going to turn a [name] in the soft-interface
168 * (bat_priv) on or off
170 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
171 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
172 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
173 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
174 batadv_store_##_name)
176 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
177 ssize_t batadv_store_##_name(struct kobject *kobj, \
178 struct attribute *attr, char *buff, \
181 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
182 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
184 return __batadv_store_uint_attr(buff, count, _min, _max, \
186 &bat_priv->_var, net_dev); \
189 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
190 ssize_t batadv_show_##_name(struct kobject *kobj, \
191 struct attribute *attr, char *buff) \
193 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
195 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
198 /* Use this, if you are going to set [name] in the soft-interface
199 * (bat_priv) to an unsigned integer value
201 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
202 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
203 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
204 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
205 batadv_store_##_name)
207 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
208 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
209 struct attribute *attr, char *buff, \
212 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
213 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
215 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
216 attr, &vlan->_name, \
217 bat_priv->soft_iface); \
219 batadv_softif_vlan_free_ref(vlan); \
223 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
224 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
225 struct attribute *attr, char *buff) \
227 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
228 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
230 size_t res = sprintf(buff, "%s\n", \
231 atomic_read(&vlan->_name) == 0 ? \
232 "disabled" : "enabled"); \
234 batadv_softif_vlan_free_ref(vlan); \
238 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
239 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
240 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
241 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
242 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
243 batadv_store_vlan_##_name)
245 static int batadv_store_bool_attr(char *buff, size_t count,
246 struct net_device *net_dev,
247 const char *attr_name, atomic_t *attr,
254 if (buff[count - 1] == '\n')
255 buff[count - 1] = '\0';
257 if ((strncmp(buff, "1", 2) == 0) ||
258 (strncmp(buff, "enable", 7) == 0) ||
259 (strncmp(buff, "enabled", 8) == 0))
262 if ((strncmp(buff, "0", 2) == 0) ||
263 (strncmp(buff, "disable", 8) == 0) ||
264 (strncmp(buff, "disabled", 9) == 0))
268 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
273 if (atomic_read(attr) == enabled)
276 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
277 atomic_read(attr) == 1 ? "enabled" : "disabled",
278 enabled == 1 ? "enabled" : "disabled");
282 atomic_set(attr, (unsigned int)enabled);
286 static inline ssize_t
287 __batadv_store_bool_attr(char *buff, size_t count,
288 void (*post_func)(struct net_device *),
289 struct attribute *attr,
290 atomic_t *attr_store, struct net_device *net_dev)
295 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
296 attr_store, &changed);
297 if (post_func && changed)
303 static int batadv_store_uint_attr(const char *buff, size_t count,
304 struct net_device *net_dev,
305 const char *attr_name,
306 unsigned int min, unsigned int max,
309 unsigned long uint_val;
312 ret = kstrtoul(buff, 10, &uint_val);
314 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
319 if (uint_val < min) {
320 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
321 attr_name, uint_val, min);
325 if (uint_val > max) {
326 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
327 attr_name, uint_val, max);
331 if (atomic_read(attr) == uint_val)
334 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
335 attr_name, atomic_read(attr), uint_val);
337 atomic_set(attr, uint_val);
341 static inline ssize_t
342 __batadv_store_uint_attr(const char *buff, size_t count,
344 void (*post_func)(struct net_device *),
345 const struct attribute *attr,
346 atomic_t *attr_store, struct net_device *net_dev)
350 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
352 if (post_func && ret)
358 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
359 struct attribute *attr, char *buff)
361 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
363 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
366 static void batadv_post_gw_reselect(struct net_device *net_dev)
368 struct batadv_priv *bat_priv = netdev_priv(net_dev);
370 batadv_gw_reselect(bat_priv);
373 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
376 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
379 switch (atomic_read(&bat_priv->gw_mode)) {
380 case BATADV_GW_MODE_CLIENT:
381 bytes_written = sprintf(buff, "%s\n",
382 BATADV_GW_MODE_CLIENT_NAME);
384 case BATADV_GW_MODE_SERVER:
385 bytes_written = sprintf(buff, "%s\n",
386 BATADV_GW_MODE_SERVER_NAME);
389 bytes_written = sprintf(buff, "%s\n",
390 BATADV_GW_MODE_OFF_NAME);
394 return bytes_written;
397 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
398 struct attribute *attr, char *buff,
401 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
402 struct batadv_priv *bat_priv = netdev_priv(net_dev);
403 char *curr_gw_mode_str;
404 int gw_mode_tmp = -1;
406 if (buff[count - 1] == '\n')
407 buff[count - 1] = '\0';
409 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
410 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
411 gw_mode_tmp = BATADV_GW_MODE_OFF;
413 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
414 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
415 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
417 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
418 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
419 gw_mode_tmp = BATADV_GW_MODE_SERVER;
421 if (gw_mode_tmp < 0) {
423 "Invalid parameter for 'gw mode' setting received: %s\n",
428 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
431 switch (atomic_read(&bat_priv->gw_mode)) {
432 case BATADV_GW_MODE_CLIENT:
433 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
435 case BATADV_GW_MODE_SERVER:
436 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
439 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
443 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
444 curr_gw_mode_str, buff);
446 /* Invoking batadv_gw_reselect() is not enough to really de-select the
447 * current GW. It will only instruct the gateway client code to perform
448 * a re-election the next time that this is needed.
450 * When gw client mode is being switched off the current GW must be
451 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
452 * client mode re-activation. This is operation is performed in
453 * batadv_gw_check_client_stop().
455 batadv_gw_reselect(bat_priv);
456 /* always call batadv_gw_check_client_stop() before changing the gateway
459 batadv_gw_check_client_stop(bat_priv);
460 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
461 batadv_gw_tvlv_container_update(bat_priv);
465 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
466 struct attribute *attr, char *buff)
468 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
471 down = atomic_read(&bat_priv->gw.bandwidth_down);
472 up = atomic_read(&bat_priv->gw.bandwidth_up);
474 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
475 down % 10, up / 10, up % 10);
478 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
479 struct attribute *attr, char *buff,
482 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
484 if (buff[count - 1] == '\n')
485 buff[count - 1] = '\0';
487 return batadv_gw_bandwidth_set(net_dev, buff, count);
491 * batadv_show_isolation_mark - print the current isolation mark/mask
492 * @kobj: kobject representing the private mesh sysfs directory
493 * @attr: the batman-adv attribute the user is interacting with
494 * @buff: the buffer that will contain the data to send back to the user
496 * Return: the number of bytes written into 'buff' on success or a negative
497 * error code in case of failure
499 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
500 struct attribute *attr, char *buff)
502 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
504 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
505 bat_priv->isolation_mark_mask);
509 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
511 * @kobj: kobject representing the private mesh sysfs directory
512 * @attr: the batman-adv attribute the user is interacting with
513 * @buff: the buffer containing the user data
514 * @count: number of bytes in the buffer
516 * Return: 'count' on success or a negative error code in case of failure
518 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
519 struct attribute *attr, char *buff,
522 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
523 struct batadv_priv *bat_priv = netdev_priv(net_dev);
527 /* parse the mask if it has been specified, otherwise assume the mask is
528 * the biggest possible
531 mask_ptr = strchr(buff, '/');
536 /* the mask must be entered in hex base as it is going to be a
537 * bitmask and not a prefix length
539 if (kstrtou32(mask_ptr, 16, &mask) < 0)
543 /* the mark can be entered in any base */
544 if (kstrtou32(buff, 0, &mark) < 0)
547 bat_priv->isolation_mark_mask = mask;
548 /* erase bits not covered by the mask */
549 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
552 "New skb mark for extended isolation: %#.8x/%#.8x\n",
553 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
558 BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
559 BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
560 #ifdef CONFIG_BATMAN_ADV_BLA
561 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR,
562 batadv_bla_status_update);
564 #ifdef CONFIG_BATMAN_ADV_DAT
565 BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
566 batadv_dat_status_update);
568 BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
569 static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
570 static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
571 batadv_store_gw_mode);
572 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, S_IRUGO | S_IWUSR,
573 2 * BATADV_JITTER, INT_MAX, NULL);
574 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, S_IRUGO | S_IWUSR, 0,
575 BATADV_TQ_MAX_VALUE, NULL);
576 BATADV_ATTR_SIF_UINT(gw_sel_class, gw_sel_class, S_IRUGO | S_IWUSR, 1,
577 BATADV_TQ_MAX_VALUE, batadv_post_gw_reselect);
578 static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
579 batadv_store_gw_bwidth);
580 #ifdef CONFIG_BATMAN_ADV_MCAST
581 BATADV_ATTR_SIF_BOOL(multicast_mode, S_IRUGO | S_IWUSR, NULL);
583 #ifdef CONFIG_BATMAN_ADV_DEBUG
584 BATADV_ATTR_SIF_UINT(log_level, log_level, S_IRUGO | S_IWUSR, 0,
585 BATADV_DBG_ALL, NULL);
587 #ifdef CONFIG_BATMAN_ADV_NC
588 BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
589 batadv_nc_status_update);
591 static BATADV_ATTR(isolation_mark, S_IRUGO | S_IWUSR,
592 batadv_show_isolation_mark, batadv_store_isolation_mark);
594 static struct batadv_attribute *batadv_mesh_attrs[] = {
595 &batadv_attr_aggregated_ogms,
596 &batadv_attr_bonding,
597 #ifdef CONFIG_BATMAN_ADV_BLA
598 &batadv_attr_bridge_loop_avoidance,
600 #ifdef CONFIG_BATMAN_ADV_DAT
601 &batadv_attr_distributed_arp_table,
603 #ifdef CONFIG_BATMAN_ADV_MCAST
604 &batadv_attr_multicast_mode,
606 &batadv_attr_fragmentation,
607 &batadv_attr_routing_algo,
608 &batadv_attr_gw_mode,
609 &batadv_attr_orig_interval,
610 &batadv_attr_hop_penalty,
611 &batadv_attr_gw_sel_class,
612 &batadv_attr_gw_bandwidth,
613 #ifdef CONFIG_BATMAN_ADV_DEBUG
614 &batadv_attr_log_level,
616 #ifdef CONFIG_BATMAN_ADV_NC
617 &batadv_attr_network_coding,
619 &batadv_attr_isolation_mark,
623 BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
625 /* array of vlan specific sysfs attributes */
626 static struct batadv_attribute *batadv_vlan_attrs[] = {
627 &batadv_attr_vlan_ap_isolation,
631 int batadv_sysfs_add_meshif(struct net_device *dev)
633 struct kobject *batif_kobject = &dev->dev.kobj;
634 struct batadv_priv *bat_priv = netdev_priv(dev);
635 struct batadv_attribute **bat_attr;
638 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
640 if (!bat_priv->mesh_obj) {
641 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
642 BATADV_SYSFS_IF_MESH_SUBDIR);
646 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
647 err = sysfs_create_file(bat_priv->mesh_obj,
648 &((*bat_attr)->attr));
650 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
651 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
652 ((*bat_attr)->attr).name);
660 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
661 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
663 kobject_put(bat_priv->mesh_obj);
664 bat_priv->mesh_obj = NULL;
669 void batadv_sysfs_del_meshif(struct net_device *dev)
671 struct batadv_priv *bat_priv = netdev_priv(dev);
672 struct batadv_attribute **bat_attr;
674 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
675 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
677 kobject_put(bat_priv->mesh_obj);
678 bat_priv->mesh_obj = NULL;
682 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
683 * @dev: netdev of the mesh interface
684 * @vlan: private data of the newly added VLAN interface
686 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
688 int batadv_sysfs_add_vlan(struct net_device *dev,
689 struct batadv_softif_vlan *vlan)
691 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
692 struct batadv_priv *bat_priv = netdev_priv(dev);
693 struct batadv_attribute **bat_attr;
696 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
697 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
698 vlan->vid & VLAN_VID_MASK);
700 vlan->kobj = kobject_create_and_add(vlan_subdir,
703 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
704 dev->name, vlan_subdir);
708 /* the untagged LAN uses the root folder to store its "VLAN
709 * specific attributes"
711 vlan->kobj = bat_priv->mesh_obj;
712 kobject_get(bat_priv->mesh_obj);
715 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
716 err = sysfs_create_file(vlan->kobj,
717 &((*bat_attr)->attr));
719 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
720 dev->name, vlan_subdir,
721 ((*bat_attr)->attr).name);
729 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
730 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
732 kobject_put(vlan->kobj);
739 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
740 * @bat_priv: the bat priv with all the soft interface information
741 * @vlan: the private data of the VLAN to destroy
743 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
744 struct batadv_softif_vlan *vlan)
746 struct batadv_attribute **bat_attr;
748 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
749 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
751 kobject_put(vlan->kobj);
755 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
756 struct attribute *attr, char *buff)
758 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
759 struct batadv_hard_iface *hard_iface;
763 hard_iface = batadv_hardif_get_by_netdev(net_dev);
767 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
770 ifname = hard_iface->soft_iface->name;
772 length = sprintf(buff, "%s\n", ifname);
774 batadv_hardif_free_ref(hard_iface);
779 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
780 struct attribute *attr, char *buff,
783 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
784 struct batadv_hard_iface *hard_iface;
788 hard_iface = batadv_hardif_get_by_netdev(net_dev);
792 if (buff[count - 1] == '\n')
793 buff[count - 1] = '\0';
795 if (strlen(buff) >= IFNAMSIZ) {
796 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
798 batadv_hardif_free_ref(hard_iface);
802 if (strncmp(buff, "none", 4) == 0)
803 status_tmp = BATADV_IF_NOT_IN_USE;
805 status_tmp = BATADV_IF_I_WANT_YOU;
807 if (hard_iface->if_status == status_tmp)
810 if ((hard_iface->soft_iface) &&
811 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
816 if (status_tmp == BATADV_IF_NOT_IN_USE) {
817 batadv_hardif_disable_interface(hard_iface,
818 BATADV_IF_CLEANUP_AUTO);
822 /* if the interface already is in use */
823 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
824 batadv_hardif_disable_interface(hard_iface,
825 BATADV_IF_CLEANUP_AUTO);
827 ret = batadv_hardif_enable_interface(hard_iface, buff);
832 batadv_hardif_free_ref(hard_iface);
836 static ssize_t batadv_show_iface_status(struct kobject *kobj,
837 struct attribute *attr, char *buff)
839 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
840 struct batadv_hard_iface *hard_iface;
843 hard_iface = batadv_hardif_get_by_netdev(net_dev);
847 switch (hard_iface->if_status) {
848 case BATADV_IF_TO_BE_REMOVED:
849 length = sprintf(buff, "disabling\n");
851 case BATADV_IF_INACTIVE:
852 length = sprintf(buff, "inactive\n");
854 case BATADV_IF_ACTIVE:
855 length = sprintf(buff, "active\n");
857 case BATADV_IF_TO_BE_ACTIVATED:
858 length = sprintf(buff, "enabling\n");
860 case BATADV_IF_NOT_IN_USE:
862 length = sprintf(buff, "not in use\n");
866 batadv_hardif_free_ref(hard_iface);
871 static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
872 batadv_store_mesh_iface);
873 static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
875 static struct batadv_attribute *batadv_batman_attrs[] = {
876 &batadv_attr_mesh_iface,
877 &batadv_attr_iface_status,
881 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
883 struct kobject *hardif_kobject = &dev->dev.kobj;
884 struct batadv_attribute **bat_attr;
887 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
891 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
892 BATADV_SYSFS_IF_BAT_SUBDIR);
896 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
897 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
899 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
900 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
901 ((*bat_attr)->attr).name);
909 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
910 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
915 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
917 kobject_put(*hardif_obj);
921 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
922 enum batadv_uev_action action, const char *data)
925 struct kobject *bat_kobj;
926 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
928 bat_kobj = &bat_priv->soft_iface->dev.kobj;
930 uevent_env[0] = kasprintf(GFP_ATOMIC,
931 "%s%s", BATADV_UEV_TYPE_VAR,
932 batadv_uev_type_str[type]);
936 uevent_env[1] = kasprintf(GFP_ATOMIC,
937 "%s%s", BATADV_UEV_ACTION_VAR,
938 batadv_uev_action_str[action]);
942 /* If the event is DEL, ignore the data field */
943 if (action != BATADV_UEV_DEL) {
944 uevent_env[2] = kasprintf(GFP_ATOMIC,
945 "%s%s", BATADV_UEV_DATA_VAR, data);
950 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
952 kfree(uevent_env[0]);
953 kfree(uevent_env[1]);
954 kfree(uevent_env[2]);
957 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
958 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
959 batadv_uev_type_str[type],
960 batadv_uev_action_str[action],
961 (action == BATADV_UEV_DEL ? "NULL" : data), ret);