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/netdevice.h>
30 #include <linux/printk.h>
31 #include <linux/rculist.h>
32 #include <linux/rcupdate.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/slab.h>
35 #include <linux/stat.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/stringify.h>
40 #include "distributed-arp-table.h"
41 #include "gateway_client.h"
42 #include "gateway_common.h"
43 #include "bridge_loop_avoidance.h"
44 #include "hard-interface.h"
45 #include "network-coding.h"
47 #include "soft-interface.h"
49 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
51 struct device *dev = container_of(obj->parent, struct device, kobj);
53 return to_net_dev(dev);
56 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
58 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
60 return netdev_priv(net_dev);
64 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
65 * @obj: kobject to covert
67 * Return: the associated batadv_priv struct.
69 static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
71 /* VLAN specific attributes are located in the root sysfs folder if they
72 * refer to the untagged VLAN..
74 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
75 return batadv_kobj_to_batpriv(obj);
77 /* ..while the attributes for the tagged vlans are located in
78 * the in the corresponding "vlan%VID" subfolder
80 return batadv_kobj_to_batpriv(obj->parent);
84 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
85 * @bat_priv: the bat priv with all the soft interface information
86 * @obj: kobject to covert
88 * Return: the associated softif_vlan struct if found, NULL otherwise.
90 static struct batadv_softif_vlan *
91 batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
93 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
96 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
97 if (vlan_tmp->kobj != obj)
100 if (!atomic_inc_not_zero(&vlan_tmp->refcount))
111 #define BATADV_UEV_TYPE_VAR "BATTYPE="
112 #define BATADV_UEV_ACTION_VAR "BATACTION="
113 #define BATADV_UEV_DATA_VAR "BATDATA="
115 static char *batadv_uev_action_str[] = {
121 static char *batadv_uev_type_str[] = {
125 /* Use this, if you have customized show and store functions for vlan attrs */
126 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
127 struct batadv_attribute batadv_attr_vlan_##_name = { \
128 .attr = {.name = __stringify(_name), \
134 /* Use this, if you have customized show and store functions */
135 #define BATADV_ATTR(_name, _mode, _show, _store) \
136 struct batadv_attribute batadv_attr_##_name = { \
137 .attr = {.name = __stringify(_name), \
143 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
144 ssize_t batadv_store_##_name(struct kobject *kobj, \
145 struct attribute *attr, char *buff, \
148 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
149 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
151 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
152 &bat_priv->_name, net_dev); \
155 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
156 ssize_t batadv_show_##_name(struct kobject *kobj, \
157 struct attribute *attr, char *buff) \
159 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
161 return sprintf(buff, "%s\n", \
162 atomic_read(&bat_priv->_name) == 0 ? \
163 "disabled" : "enabled"); \
166 /* Use this, if you are going to turn a [name] in the soft-interface
167 * (bat_priv) on or off
169 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
170 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
171 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
172 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
173 batadv_store_##_name)
175 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
176 ssize_t batadv_store_##_name(struct kobject *kobj, \
177 struct attribute *attr, char *buff, \
180 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
181 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
183 return __batadv_store_uint_attr(buff, count, _min, _max, \
185 &bat_priv->_var, net_dev); \
188 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
189 ssize_t batadv_show_##_name(struct kobject *kobj, \
190 struct attribute *attr, char *buff) \
192 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
194 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
197 /* Use this, if you are going to set [name] in the soft-interface
198 * (bat_priv) to an unsigned integer value
200 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
201 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
202 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
203 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
204 batadv_store_##_name)
206 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
207 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
208 struct attribute *attr, char *buff, \
211 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
212 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
214 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
215 attr, &vlan->_name, \
216 bat_priv->soft_iface); \
218 batadv_softif_vlan_free_ref(vlan); \
222 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
223 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
224 struct attribute *attr, char *buff) \
226 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
227 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
229 size_t res = sprintf(buff, "%s\n", \
230 atomic_read(&vlan->_name) == 0 ? \
231 "disabled" : "enabled"); \
233 batadv_softif_vlan_free_ref(vlan); \
237 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
238 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
239 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
240 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
241 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
242 batadv_store_vlan_##_name)
244 static int batadv_store_bool_attr(char *buff, size_t count,
245 struct net_device *net_dev,
246 const char *attr_name, atomic_t *attr,
253 if (buff[count - 1] == '\n')
254 buff[count - 1] = '\0';
256 if ((strncmp(buff, "1", 2) == 0) ||
257 (strncmp(buff, "enable", 7) == 0) ||
258 (strncmp(buff, "enabled", 8) == 0))
261 if ((strncmp(buff, "0", 2) == 0) ||
262 (strncmp(buff, "disable", 8) == 0) ||
263 (strncmp(buff, "disabled", 9) == 0))
267 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
272 if (atomic_read(attr) == enabled)
275 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
276 atomic_read(attr) == 1 ? "enabled" : "disabled",
277 enabled == 1 ? "enabled" : "disabled");
281 atomic_set(attr, (unsigned int)enabled);
285 static inline ssize_t
286 __batadv_store_bool_attr(char *buff, size_t count,
287 void (*post_func)(struct net_device *),
288 struct attribute *attr,
289 atomic_t *attr_store, struct net_device *net_dev)
294 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
295 attr_store, &changed);
296 if (post_func && changed)
302 static int batadv_store_uint_attr(const char *buff, size_t count,
303 struct net_device *net_dev,
304 const char *attr_name,
305 unsigned int min, unsigned int max,
308 unsigned long uint_val;
311 ret = kstrtoul(buff, 10, &uint_val);
313 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
318 if (uint_val < min) {
319 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
320 attr_name, uint_val, min);
324 if (uint_val > max) {
325 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
326 attr_name, uint_val, max);
330 if (atomic_read(attr) == uint_val)
333 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
334 attr_name, atomic_read(attr), uint_val);
336 atomic_set(attr, uint_val);
340 static inline ssize_t
341 __batadv_store_uint_attr(const char *buff, size_t count,
343 void (*post_func)(struct net_device *),
344 const struct attribute *attr,
345 atomic_t *attr_store, struct net_device *net_dev)
349 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
351 if (post_func && ret)
357 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
358 struct attribute *attr, char *buff)
360 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
362 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
365 static void batadv_post_gw_reselect(struct net_device *net_dev)
367 struct batadv_priv *bat_priv = netdev_priv(net_dev);
369 batadv_gw_reselect(bat_priv);
372 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
375 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
378 switch (atomic_read(&bat_priv->gw_mode)) {
379 case BATADV_GW_MODE_CLIENT:
380 bytes_written = sprintf(buff, "%s\n",
381 BATADV_GW_MODE_CLIENT_NAME);
383 case BATADV_GW_MODE_SERVER:
384 bytes_written = sprintf(buff, "%s\n",
385 BATADV_GW_MODE_SERVER_NAME);
388 bytes_written = sprintf(buff, "%s\n",
389 BATADV_GW_MODE_OFF_NAME);
393 return bytes_written;
396 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
397 struct attribute *attr, char *buff,
400 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
401 struct batadv_priv *bat_priv = netdev_priv(net_dev);
402 char *curr_gw_mode_str;
403 int gw_mode_tmp = -1;
405 if (buff[count - 1] == '\n')
406 buff[count - 1] = '\0';
408 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
409 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
410 gw_mode_tmp = BATADV_GW_MODE_OFF;
412 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
413 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
414 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
416 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
417 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
418 gw_mode_tmp = BATADV_GW_MODE_SERVER;
420 if (gw_mode_tmp < 0) {
422 "Invalid parameter for 'gw mode' setting received: %s\n",
427 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
430 switch (atomic_read(&bat_priv->gw_mode)) {
431 case BATADV_GW_MODE_CLIENT:
432 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
434 case BATADV_GW_MODE_SERVER:
435 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
438 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
442 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
443 curr_gw_mode_str, buff);
445 /* Invoking batadv_gw_reselect() is not enough to really de-select the
446 * current GW. It will only instruct the gateway client code to perform
447 * a re-election the next time that this is needed.
449 * When gw client mode is being switched off the current GW must be
450 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
451 * client mode re-activation. This is operation is performed in
452 * batadv_gw_check_client_stop().
454 batadv_gw_reselect(bat_priv);
455 /* always call batadv_gw_check_client_stop() before changing the gateway
458 batadv_gw_check_client_stop(bat_priv);
459 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
460 batadv_gw_tvlv_container_update(bat_priv);
464 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
465 struct attribute *attr, char *buff)
467 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
470 down = atomic_read(&bat_priv->gw.bandwidth_down);
471 up = atomic_read(&bat_priv->gw.bandwidth_up);
473 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
474 down % 10, up / 10, up % 10);
477 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
478 struct attribute *attr, char *buff,
481 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
483 if (buff[count - 1] == '\n')
484 buff[count - 1] = '\0';
486 return batadv_gw_bandwidth_set(net_dev, buff, count);
490 * batadv_show_isolation_mark - print the current isolation mark/mask
491 * @kobj: kobject representing the private mesh sysfs directory
492 * @attr: the batman-adv attribute the user is interacting with
493 * @buff: the buffer that will contain the data to send back to the user
495 * Return: the number of bytes written into 'buff' on success or a negative
496 * error code in case of failure
498 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
499 struct attribute *attr, char *buff)
501 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
503 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
504 bat_priv->isolation_mark_mask);
508 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
510 * @kobj: kobject representing the private mesh sysfs directory
511 * @attr: the batman-adv attribute the user is interacting with
512 * @buff: the buffer containing the user data
513 * @count: number of bytes in the buffer
515 * Return: 'count' on success or a negative error code in case of failure
517 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
518 struct attribute *attr, char *buff,
521 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
522 struct batadv_priv *bat_priv = netdev_priv(net_dev);
526 /* parse the mask if it has been specified, otherwise assume the mask is
527 * the biggest possible
530 mask_ptr = strchr(buff, '/');
535 /* the mask must be entered in hex base as it is going to be a
536 * bitmask and not a prefix length
538 if (kstrtou32(mask_ptr, 16, &mask) < 0)
542 /* the mark can be entered in any base */
543 if (kstrtou32(buff, 0, &mark) < 0)
546 bat_priv->isolation_mark_mask = mask;
547 /* erase bits not covered by the mask */
548 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
551 "New skb mark for extended isolation: %#.8x/%#.8x\n",
552 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
557 BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
558 BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
559 #ifdef CONFIG_BATMAN_ADV_BLA
560 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR,
561 batadv_bla_status_update);
563 #ifdef CONFIG_BATMAN_ADV_DAT
564 BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
565 batadv_dat_status_update);
567 BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
568 static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
569 static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
570 batadv_store_gw_mode);
571 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, S_IRUGO | S_IWUSR,
572 2 * BATADV_JITTER, INT_MAX, NULL);
573 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, S_IRUGO | S_IWUSR, 0,
574 BATADV_TQ_MAX_VALUE, NULL);
575 BATADV_ATTR_SIF_UINT(gw_sel_class, gw_sel_class, S_IRUGO | S_IWUSR, 1,
576 BATADV_TQ_MAX_VALUE, batadv_post_gw_reselect);
577 static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
578 batadv_store_gw_bwidth);
579 #ifdef CONFIG_BATMAN_ADV_MCAST
580 BATADV_ATTR_SIF_BOOL(multicast_mode, S_IRUGO | S_IWUSR, NULL);
582 #ifdef CONFIG_BATMAN_ADV_DEBUG
583 BATADV_ATTR_SIF_UINT(log_level, log_level, S_IRUGO | S_IWUSR, 0,
584 BATADV_DBG_ALL, NULL);
586 #ifdef CONFIG_BATMAN_ADV_NC
587 BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
588 batadv_nc_status_update);
590 static BATADV_ATTR(isolation_mark, S_IRUGO | S_IWUSR,
591 batadv_show_isolation_mark, batadv_store_isolation_mark);
593 static struct batadv_attribute *batadv_mesh_attrs[] = {
594 &batadv_attr_aggregated_ogms,
595 &batadv_attr_bonding,
596 #ifdef CONFIG_BATMAN_ADV_BLA
597 &batadv_attr_bridge_loop_avoidance,
599 #ifdef CONFIG_BATMAN_ADV_DAT
600 &batadv_attr_distributed_arp_table,
602 #ifdef CONFIG_BATMAN_ADV_MCAST
603 &batadv_attr_multicast_mode,
605 &batadv_attr_fragmentation,
606 &batadv_attr_routing_algo,
607 &batadv_attr_gw_mode,
608 &batadv_attr_orig_interval,
609 &batadv_attr_hop_penalty,
610 &batadv_attr_gw_sel_class,
611 &batadv_attr_gw_bandwidth,
612 #ifdef CONFIG_BATMAN_ADV_DEBUG
613 &batadv_attr_log_level,
615 #ifdef CONFIG_BATMAN_ADV_NC
616 &batadv_attr_network_coding,
618 &batadv_attr_isolation_mark,
622 BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
624 /* array of vlan specific sysfs attributes */
625 static struct batadv_attribute *batadv_vlan_attrs[] = {
626 &batadv_attr_vlan_ap_isolation,
630 int batadv_sysfs_add_meshif(struct net_device *dev)
632 struct kobject *batif_kobject = &dev->dev.kobj;
633 struct batadv_priv *bat_priv = netdev_priv(dev);
634 struct batadv_attribute **bat_attr;
637 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
639 if (!bat_priv->mesh_obj) {
640 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
641 BATADV_SYSFS_IF_MESH_SUBDIR);
645 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
646 err = sysfs_create_file(bat_priv->mesh_obj,
647 &((*bat_attr)->attr));
649 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
650 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
651 ((*bat_attr)->attr).name);
659 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
660 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
662 kobject_put(bat_priv->mesh_obj);
663 bat_priv->mesh_obj = NULL;
668 void batadv_sysfs_del_meshif(struct net_device *dev)
670 struct batadv_priv *bat_priv = netdev_priv(dev);
671 struct batadv_attribute **bat_attr;
673 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
674 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
676 kobject_put(bat_priv->mesh_obj);
677 bat_priv->mesh_obj = NULL;
681 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
682 * @dev: netdev of the mesh interface
683 * @vlan: private data of the newly added VLAN interface
685 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
687 int batadv_sysfs_add_vlan(struct net_device *dev,
688 struct batadv_softif_vlan *vlan)
690 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
691 struct batadv_priv *bat_priv = netdev_priv(dev);
692 struct batadv_attribute **bat_attr;
695 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
696 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
697 vlan->vid & VLAN_VID_MASK);
699 vlan->kobj = kobject_create_and_add(vlan_subdir,
702 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
703 dev->name, vlan_subdir);
707 /* the untagged LAN uses the root folder to store its "VLAN
708 * specific attributes"
710 vlan->kobj = bat_priv->mesh_obj;
711 kobject_get(bat_priv->mesh_obj);
714 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
715 err = sysfs_create_file(vlan->kobj,
716 &((*bat_attr)->attr));
718 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
719 dev->name, vlan_subdir,
720 ((*bat_attr)->attr).name);
728 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
729 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
731 kobject_put(vlan->kobj);
738 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
739 * @bat_priv: the bat priv with all the soft interface information
740 * @vlan: the private data of the VLAN to destroy
742 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
743 struct batadv_softif_vlan *vlan)
745 struct batadv_attribute **bat_attr;
747 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
748 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
750 kobject_put(vlan->kobj);
754 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
755 struct attribute *attr, char *buff)
757 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
758 struct batadv_hard_iface *hard_iface;
762 hard_iface = batadv_hardif_get_by_netdev(net_dev);
766 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
769 ifname = hard_iface->soft_iface->name;
771 length = sprintf(buff, "%s\n", ifname);
773 batadv_hardif_free_ref(hard_iface);
778 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
779 struct attribute *attr, char *buff,
782 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
783 struct batadv_hard_iface *hard_iface;
787 hard_iface = batadv_hardif_get_by_netdev(net_dev);
791 if (buff[count - 1] == '\n')
792 buff[count - 1] = '\0';
794 if (strlen(buff) >= IFNAMSIZ) {
795 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
797 batadv_hardif_free_ref(hard_iface);
801 if (strncmp(buff, "none", 4) == 0)
802 status_tmp = BATADV_IF_NOT_IN_USE;
804 status_tmp = BATADV_IF_I_WANT_YOU;
806 if (hard_iface->if_status == status_tmp)
809 if ((hard_iface->soft_iface) &&
810 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
815 if (status_tmp == BATADV_IF_NOT_IN_USE) {
816 batadv_hardif_disable_interface(hard_iface,
817 BATADV_IF_CLEANUP_AUTO);
821 /* if the interface already is in use */
822 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
823 batadv_hardif_disable_interface(hard_iface,
824 BATADV_IF_CLEANUP_AUTO);
826 ret = batadv_hardif_enable_interface(hard_iface, buff);
831 batadv_hardif_free_ref(hard_iface);
835 static ssize_t batadv_show_iface_status(struct kobject *kobj,
836 struct attribute *attr, char *buff)
838 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
839 struct batadv_hard_iface *hard_iface;
842 hard_iface = batadv_hardif_get_by_netdev(net_dev);
846 switch (hard_iface->if_status) {
847 case BATADV_IF_TO_BE_REMOVED:
848 length = sprintf(buff, "disabling\n");
850 case BATADV_IF_INACTIVE:
851 length = sprintf(buff, "inactive\n");
853 case BATADV_IF_ACTIVE:
854 length = sprintf(buff, "active\n");
856 case BATADV_IF_TO_BE_ACTIVATED:
857 length = sprintf(buff, "enabling\n");
859 case BATADV_IF_NOT_IN_USE:
861 length = sprintf(buff, "not in use\n");
865 batadv_hardif_free_ref(hard_iface);
870 static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
871 batadv_store_mesh_iface);
872 static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
874 static struct batadv_attribute *batadv_batman_attrs[] = {
875 &batadv_attr_mesh_iface,
876 &batadv_attr_iface_status,
880 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
882 struct kobject *hardif_kobject = &dev->dev.kobj;
883 struct batadv_attribute **bat_attr;
886 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
890 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
891 BATADV_SYSFS_IF_BAT_SUBDIR);
895 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
896 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
898 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
899 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
900 ((*bat_attr)->attr).name);
908 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
909 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
914 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
916 kobject_put(*hardif_obj);
920 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
921 enum batadv_uev_action action, const char *data)
924 struct kobject *bat_kobj;
925 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
927 bat_kobj = &bat_priv->soft_iface->dev.kobj;
929 uevent_env[0] = kasprintf(GFP_ATOMIC,
930 "%s%s", BATADV_UEV_TYPE_VAR,
931 batadv_uev_type_str[type]);
935 uevent_env[1] = kasprintf(GFP_ATOMIC,
936 "%s%s", BATADV_UEV_ACTION_VAR,
937 batadv_uev_action_str[action]);
941 /* If the event is DEL, ignore the data field */
942 if (action != BATADV_UEV_DEL) {
943 uevent_env[2] = kasprintf(GFP_ATOMIC,
944 "%s%s", BATADV_UEV_DATA_VAR, data);
949 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
951 kfree(uevent_env[0]);
952 kfree(uevent_env[1]);
953 kfree(uevent_env[2]);
956 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
957 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
958 batadv_uev_type_str[type],
959 batadv_uev_action_str[action],
960 (action == BATADV_UEV_DEL ? "NULL" : data), ret);