1 /* Copyright (C) 2010-2015 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 "hard-interface.h"
44 #include "network-coding.h"
46 #include "soft-interface.h"
48 static struct net_device
*batadv_kobj_to_netdev(struct kobject
*obj
)
50 struct device
*dev
= container_of(obj
->parent
, struct device
, kobj
);
52 return to_net_dev(dev
);
55 static struct batadv_priv
*batadv_kobj_to_batpriv(struct kobject
*obj
)
57 struct net_device
*net_dev
= batadv_kobj_to_netdev(obj
);
59 return netdev_priv(net_dev
);
63 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
64 * @obj: kobject to covert
66 * Returns the associated batadv_priv struct.
68 static struct batadv_priv
*batadv_vlan_kobj_to_batpriv(struct kobject
*obj
)
70 /* VLAN specific attributes are located in the root sysfs folder if they
71 * refer to the untagged VLAN..
73 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR
, obj
->name
))
74 return batadv_kobj_to_batpriv(obj
);
76 /* ..while the attributes for the tagged vlans are located in
77 * the in the corresponding "vlan%VID" subfolder
79 return batadv_kobj_to_batpriv(obj
->parent
);
83 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
84 * @obj: kobject to covert
86 * Returns the associated softif_vlan struct if found, NULL otherwise.
88 static struct batadv_softif_vlan
*
89 batadv_kobj_to_vlan(struct batadv_priv
*bat_priv
, struct kobject
*obj
)
91 struct batadv_softif_vlan
*vlan_tmp
, *vlan
= NULL
;
94 hlist_for_each_entry_rcu(vlan_tmp
, &bat_priv
->softif_vlan_list
, list
) {
95 if (vlan_tmp
->kobj
!= obj
)
98 if (!atomic_inc_not_zero(&vlan_tmp
->refcount
))
109 #define BATADV_UEV_TYPE_VAR "BATTYPE="
110 #define BATADV_UEV_ACTION_VAR "BATACTION="
111 #define BATADV_UEV_DATA_VAR "BATDATA="
113 static char *batadv_uev_action_str
[] = {
119 static char *batadv_uev_type_str
[] = {
123 /* Use this, if you have customized show and store functions for vlan attrs */
124 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
125 struct batadv_attribute batadv_attr_vlan_##_name = { \
126 .attr = {.name = __stringify(_name), \
132 /* Use this, if you have customized show and store functions */
133 #define BATADV_ATTR(_name, _mode, _show, _store) \
134 struct batadv_attribute batadv_attr_##_name = { \
135 .attr = {.name = __stringify(_name), \
141 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
142 ssize_t batadv_store_##_name(struct kobject *kobj, \
143 struct attribute *attr, char *buff, \
146 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
147 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
149 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
150 &bat_priv->_name, net_dev); \
153 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
154 ssize_t batadv_show_##_name(struct kobject *kobj, \
155 struct attribute *attr, char *buff) \
157 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
159 return sprintf(buff, "%s\n", \
160 atomic_read(&bat_priv->_name) == 0 ? \
161 "disabled" : "enabled"); \
164 /* Use this, if you are going to turn a [name] in the soft-interface
165 * (bat_priv) on or off
167 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
168 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
169 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
170 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
171 batadv_store_##_name)
173 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
174 ssize_t batadv_store_##_name(struct kobject *kobj, \
175 struct attribute *attr, char *buff, \
178 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
179 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
181 return __batadv_store_uint_attr(buff, count, _min, _max, \
183 &bat_priv->_var, net_dev); \
186 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
187 ssize_t batadv_show_##_name(struct kobject *kobj, \
188 struct attribute *attr, char *buff) \
190 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
192 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
195 /* Use this, if you are going to set [name] in the soft-interface
196 * (bat_priv) to an unsigned integer value
198 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
199 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
200 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
201 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
202 batadv_store_##_name)
204 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
205 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
206 struct attribute *attr, char *buff, \
209 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
210 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
212 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
213 attr, &vlan->_name, \
214 bat_priv->soft_iface); \
216 batadv_softif_vlan_free_ref(vlan); \
220 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
221 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
222 struct attribute *attr, char *buff) \
224 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
225 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
227 size_t res = sprintf(buff, "%s\n", \
228 atomic_read(&vlan->_name) == 0 ? \
229 "disabled" : "enabled"); \
231 batadv_softif_vlan_free_ref(vlan); \
235 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
236 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
237 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
238 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
239 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
240 batadv_store_vlan_##_name)
242 static int batadv_store_bool_attr(char *buff
, size_t count
,
243 struct net_device
*net_dev
,
244 const char *attr_name
, atomic_t
*attr
)
248 if (buff
[count
- 1] == '\n')
249 buff
[count
- 1] = '\0';
251 if ((strncmp(buff
, "1", 2) == 0) ||
252 (strncmp(buff
, "enable", 7) == 0) ||
253 (strncmp(buff
, "enabled", 8) == 0))
256 if ((strncmp(buff
, "0", 2) == 0) ||
257 (strncmp(buff
, "disable", 8) == 0) ||
258 (strncmp(buff
, "disabled", 9) == 0))
262 batadv_info(net_dev
, "%s: Invalid parameter received: %s\n",
267 if (atomic_read(attr
) == enabled
)
270 batadv_info(net_dev
, "%s: Changing from: %s to: %s\n", attr_name
,
271 atomic_read(attr
) == 1 ? "enabled" : "disabled",
272 enabled
== 1 ? "enabled" : "disabled");
274 atomic_set(attr
, (unsigned int)enabled
);
278 static inline ssize_t
279 __batadv_store_bool_attr(char *buff
, size_t count
,
280 void (*post_func
)(struct net_device
*),
281 struct attribute
*attr
,
282 atomic_t
*attr_store
, struct net_device
*net_dev
)
286 ret
= batadv_store_bool_attr(buff
, count
, net_dev
, attr
->name
,
288 if (post_func
&& ret
)
294 static int batadv_store_uint_attr(const char *buff
, size_t count
,
295 struct net_device
*net_dev
,
296 const char *attr_name
,
297 unsigned int min
, unsigned int max
,
300 unsigned long uint_val
;
303 ret
= kstrtoul(buff
, 10, &uint_val
);
305 batadv_info(net_dev
, "%s: Invalid parameter received: %s\n",
310 if (uint_val
< min
) {
311 batadv_info(net_dev
, "%s: Value is too small: %lu min: %u\n",
312 attr_name
, uint_val
, min
);
316 if (uint_val
> max
) {
317 batadv_info(net_dev
, "%s: Value is too big: %lu max: %u\n",
318 attr_name
, uint_val
, max
);
322 if (atomic_read(attr
) == uint_val
)
325 batadv_info(net_dev
, "%s: Changing from: %i to: %lu\n",
326 attr_name
, atomic_read(attr
), uint_val
);
328 atomic_set(attr
, uint_val
);
332 static inline ssize_t
333 __batadv_store_uint_attr(const char *buff
, size_t count
,
335 void (*post_func
)(struct net_device
*),
336 const struct attribute
*attr
,
337 atomic_t
*attr_store
, struct net_device
*net_dev
)
341 ret
= batadv_store_uint_attr(buff
, count
, net_dev
, attr
->name
, min
, max
,
343 if (post_func
&& ret
)
349 static ssize_t
batadv_show_bat_algo(struct kobject
*kobj
,
350 struct attribute
*attr
, char *buff
)
352 struct batadv_priv
*bat_priv
= batadv_kobj_to_batpriv(kobj
);
354 return sprintf(buff
, "%s\n", bat_priv
->bat_algo_ops
->name
);
357 static void batadv_post_gw_reselect(struct net_device
*net_dev
)
359 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
361 batadv_gw_reselect(bat_priv
);
364 static ssize_t
batadv_show_gw_mode(struct kobject
*kobj
, struct attribute
*attr
,
367 struct batadv_priv
*bat_priv
= batadv_kobj_to_batpriv(kobj
);
370 switch (atomic_read(&bat_priv
->gw_mode
)) {
371 case BATADV_GW_MODE_CLIENT
:
372 bytes_written
= sprintf(buff
, "%s\n",
373 BATADV_GW_MODE_CLIENT_NAME
);
375 case BATADV_GW_MODE_SERVER
:
376 bytes_written
= sprintf(buff
, "%s\n",
377 BATADV_GW_MODE_SERVER_NAME
);
380 bytes_written
= sprintf(buff
, "%s\n",
381 BATADV_GW_MODE_OFF_NAME
);
385 return bytes_written
;
388 static ssize_t
batadv_store_gw_mode(struct kobject
*kobj
,
389 struct attribute
*attr
, char *buff
,
392 struct net_device
*net_dev
= batadv_kobj_to_netdev(kobj
);
393 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
394 char *curr_gw_mode_str
;
395 int gw_mode_tmp
= -1;
397 if (buff
[count
- 1] == '\n')
398 buff
[count
- 1] = '\0';
400 if (strncmp(buff
, BATADV_GW_MODE_OFF_NAME
,
401 strlen(BATADV_GW_MODE_OFF_NAME
)) == 0)
402 gw_mode_tmp
= BATADV_GW_MODE_OFF
;
404 if (strncmp(buff
, BATADV_GW_MODE_CLIENT_NAME
,
405 strlen(BATADV_GW_MODE_CLIENT_NAME
)) == 0)
406 gw_mode_tmp
= BATADV_GW_MODE_CLIENT
;
408 if (strncmp(buff
, BATADV_GW_MODE_SERVER_NAME
,
409 strlen(BATADV_GW_MODE_SERVER_NAME
)) == 0)
410 gw_mode_tmp
= BATADV_GW_MODE_SERVER
;
412 if (gw_mode_tmp
< 0) {
414 "Invalid parameter for 'gw mode' setting received: %s\n",
419 if (atomic_read(&bat_priv
->gw_mode
) == gw_mode_tmp
)
422 switch (atomic_read(&bat_priv
->gw_mode
)) {
423 case BATADV_GW_MODE_CLIENT
:
424 curr_gw_mode_str
= BATADV_GW_MODE_CLIENT_NAME
;
426 case BATADV_GW_MODE_SERVER
:
427 curr_gw_mode_str
= BATADV_GW_MODE_SERVER_NAME
;
430 curr_gw_mode_str
= BATADV_GW_MODE_OFF_NAME
;
434 batadv_info(net_dev
, "Changing gw mode from: %s to: %s\n",
435 curr_gw_mode_str
, buff
);
437 /* Invoking batadv_gw_reselect() is not enough to really de-select the
438 * current GW. It will only instruct the gateway client code to perform
439 * a re-election the next time that this is needed.
441 * When gw client mode is being switched off the current GW must be
442 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
443 * client mode re-activation. This is operation is performed in
444 * batadv_gw_check_client_stop().
446 batadv_gw_reselect(bat_priv
);
447 /* always call batadv_gw_check_client_stop() before changing the gateway
450 batadv_gw_check_client_stop(bat_priv
);
451 atomic_set(&bat_priv
->gw_mode
, (unsigned int)gw_mode_tmp
);
452 batadv_gw_tvlv_container_update(bat_priv
);
456 static ssize_t
batadv_show_gw_bwidth(struct kobject
*kobj
,
457 struct attribute
*attr
, char *buff
)
459 struct batadv_priv
*bat_priv
= batadv_kobj_to_batpriv(kobj
);
462 down
= atomic_read(&bat_priv
->gw
.bandwidth_down
);
463 up
= atomic_read(&bat_priv
->gw
.bandwidth_up
);
465 return sprintf(buff
, "%u.%u/%u.%u MBit\n", down
/ 10,
466 down
% 10, up
/ 10, up
% 10);
469 static ssize_t
batadv_store_gw_bwidth(struct kobject
*kobj
,
470 struct attribute
*attr
, char *buff
,
473 struct net_device
*net_dev
= batadv_kobj_to_netdev(kobj
);
475 if (buff
[count
- 1] == '\n')
476 buff
[count
- 1] = '\0';
478 return batadv_gw_bandwidth_set(net_dev
, buff
, count
);
482 * batadv_show_isolation_mark - print the current isolation mark/mask
483 * @kobj: kobject representing the private mesh sysfs directory
484 * @attr: the batman-adv attribute the user is interacting with
485 * @buff: the buffer that will contain the data to send back to the user
487 * Returns the number of bytes written into 'buff' on success or a negative
488 * error code in case of failure
490 static ssize_t
batadv_show_isolation_mark(struct kobject
*kobj
,
491 struct attribute
*attr
, char *buff
)
493 struct batadv_priv
*bat_priv
= batadv_kobj_to_batpriv(kobj
);
495 return sprintf(buff
, "%#.8x/%#.8x\n", bat_priv
->isolation_mark
,
496 bat_priv
->isolation_mark_mask
);
500 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
502 * @kobj: kobject representing the private mesh sysfs directory
503 * @attr: the batman-adv attribute the user is interacting with
504 * @buff: the buffer containing the user data
505 * @count: number of bytes in the buffer
507 * Returns 'count' on success or a negative error code in case of failure
509 static ssize_t
batadv_store_isolation_mark(struct kobject
*kobj
,
510 struct attribute
*attr
, char *buff
,
513 struct net_device
*net_dev
= batadv_kobj_to_netdev(kobj
);
514 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
518 /* parse the mask if it has been specified, otherwise assume the mask is
519 * the biggest possible
522 mask_ptr
= strchr(buff
, '/');
527 /* the mask must be entered in hex base as it is going to be a
528 * bitmask and not a prefix length
530 if (kstrtou32(mask_ptr
, 16, &mask
) < 0)
534 /* the mark can be entered in any base */
535 if (kstrtou32(buff
, 0, &mark
) < 0)
538 bat_priv
->isolation_mark_mask
= mask
;
539 /* erase bits not covered by the mask */
540 bat_priv
->isolation_mark
= mark
& bat_priv
->isolation_mark_mask
;
543 "New skb mark for extended isolation: %#.8x/%#.8x\n",
544 bat_priv
->isolation_mark
, bat_priv
->isolation_mark_mask
);
549 BATADV_ATTR_SIF_BOOL(aggregated_ogms
, S_IRUGO
| S_IWUSR
, NULL
);
550 BATADV_ATTR_SIF_BOOL(bonding
, S_IRUGO
| S_IWUSR
, NULL
);
551 #ifdef CONFIG_BATMAN_ADV_BLA
552 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance
, S_IRUGO
| S_IWUSR
, NULL
);
554 #ifdef CONFIG_BATMAN_ADV_DAT
555 BATADV_ATTR_SIF_BOOL(distributed_arp_table
, S_IRUGO
| S_IWUSR
,
556 batadv_dat_status_update
);
558 BATADV_ATTR_SIF_BOOL(fragmentation
, S_IRUGO
| S_IWUSR
, batadv_update_min_mtu
);
559 static BATADV_ATTR(routing_algo
, S_IRUGO
, batadv_show_bat_algo
, NULL
);
560 static BATADV_ATTR(gw_mode
, S_IRUGO
| S_IWUSR
, batadv_show_gw_mode
,
561 batadv_store_gw_mode
);
562 BATADV_ATTR_SIF_UINT(orig_interval
, orig_interval
, S_IRUGO
| S_IWUSR
,
563 2 * BATADV_JITTER
, INT_MAX
, NULL
);
564 BATADV_ATTR_SIF_UINT(hop_penalty
, hop_penalty
, S_IRUGO
| S_IWUSR
, 0,
565 BATADV_TQ_MAX_VALUE
, NULL
);
566 BATADV_ATTR_SIF_UINT(gw_sel_class
, gw_sel_class
, S_IRUGO
| S_IWUSR
, 1,
567 BATADV_TQ_MAX_VALUE
, batadv_post_gw_reselect
);
568 static BATADV_ATTR(gw_bandwidth
, S_IRUGO
| S_IWUSR
, batadv_show_gw_bwidth
,
569 batadv_store_gw_bwidth
);
570 #ifdef CONFIG_BATMAN_ADV_MCAST
571 BATADV_ATTR_SIF_BOOL(multicast_mode
, S_IRUGO
| S_IWUSR
, NULL
);
573 #ifdef CONFIG_BATMAN_ADV_DEBUG
574 BATADV_ATTR_SIF_UINT(log_level
, log_level
, S_IRUGO
| S_IWUSR
, 0,
575 BATADV_DBG_ALL
, NULL
);
577 #ifdef CONFIG_BATMAN_ADV_NC
578 BATADV_ATTR_SIF_BOOL(network_coding
, S_IRUGO
| S_IWUSR
,
579 batadv_nc_status_update
);
581 static BATADV_ATTR(isolation_mark
, S_IRUGO
| S_IWUSR
,
582 batadv_show_isolation_mark
, batadv_store_isolation_mark
);
584 static struct batadv_attribute
*batadv_mesh_attrs
[] = {
585 &batadv_attr_aggregated_ogms
,
586 &batadv_attr_bonding
,
587 #ifdef CONFIG_BATMAN_ADV_BLA
588 &batadv_attr_bridge_loop_avoidance
,
590 #ifdef CONFIG_BATMAN_ADV_DAT
591 &batadv_attr_distributed_arp_table
,
593 #ifdef CONFIG_BATMAN_ADV_MCAST
594 &batadv_attr_multicast_mode
,
596 &batadv_attr_fragmentation
,
597 &batadv_attr_routing_algo
,
598 &batadv_attr_gw_mode
,
599 &batadv_attr_orig_interval
,
600 &batadv_attr_hop_penalty
,
601 &batadv_attr_gw_sel_class
,
602 &batadv_attr_gw_bandwidth
,
603 #ifdef CONFIG_BATMAN_ADV_DEBUG
604 &batadv_attr_log_level
,
606 #ifdef CONFIG_BATMAN_ADV_NC
607 &batadv_attr_network_coding
,
609 &batadv_attr_isolation_mark
,
613 BATADV_ATTR_VLAN_BOOL(ap_isolation
, S_IRUGO
| S_IWUSR
, NULL
);
616 * batadv_vlan_attrs - array of vlan specific sysfs attributes
618 static struct batadv_attribute
*batadv_vlan_attrs
[] = {
619 &batadv_attr_vlan_ap_isolation
,
623 int batadv_sysfs_add_meshif(struct net_device
*dev
)
625 struct kobject
*batif_kobject
= &dev
->dev
.kobj
;
626 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
627 struct batadv_attribute
**bat_attr
;
630 bat_priv
->mesh_obj
= kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR
,
632 if (!bat_priv
->mesh_obj
) {
633 batadv_err(dev
, "Can't add sysfs directory: %s/%s\n", dev
->name
,
634 BATADV_SYSFS_IF_MESH_SUBDIR
);
638 for (bat_attr
= batadv_mesh_attrs
; *bat_attr
; ++bat_attr
) {
639 err
= sysfs_create_file(bat_priv
->mesh_obj
,
640 &((*bat_attr
)->attr
));
642 batadv_err(dev
, "Can't add sysfs file: %s/%s/%s\n",
643 dev
->name
, BATADV_SYSFS_IF_MESH_SUBDIR
,
644 ((*bat_attr
)->attr
).name
);
652 for (bat_attr
= batadv_mesh_attrs
; *bat_attr
; ++bat_attr
)
653 sysfs_remove_file(bat_priv
->mesh_obj
, &((*bat_attr
)->attr
));
655 kobject_put(bat_priv
->mesh_obj
);
656 bat_priv
->mesh_obj
= NULL
;
661 void batadv_sysfs_del_meshif(struct net_device
*dev
)
663 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
664 struct batadv_attribute
**bat_attr
;
666 for (bat_attr
= batadv_mesh_attrs
; *bat_attr
; ++bat_attr
)
667 sysfs_remove_file(bat_priv
->mesh_obj
, &((*bat_attr
)->attr
));
669 kobject_put(bat_priv
->mesh_obj
);
670 bat_priv
->mesh_obj
= NULL
;
674 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
675 * @dev: netdev of the mesh interface
676 * @vlan: private data of the newly added VLAN interface
678 * Returns 0 on success and -ENOMEM if any of the structure allocations fails.
680 int batadv_sysfs_add_vlan(struct net_device
*dev
,
681 struct batadv_softif_vlan
*vlan
)
683 char vlan_subdir
[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX
) + 5];
684 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
685 struct batadv_attribute
**bat_attr
;
688 if (vlan
->vid
& BATADV_VLAN_HAS_TAG
) {
689 sprintf(vlan_subdir
, BATADV_SYSFS_VLAN_SUBDIR_PREFIX
"%hu",
690 vlan
->vid
& VLAN_VID_MASK
);
692 vlan
->kobj
= kobject_create_and_add(vlan_subdir
,
695 batadv_err(dev
, "Can't add sysfs directory: %s/%s\n",
696 dev
->name
, vlan_subdir
);
700 /* the untagged LAN uses the root folder to store its "VLAN
701 * specific attributes"
703 vlan
->kobj
= bat_priv
->mesh_obj
;
704 kobject_get(bat_priv
->mesh_obj
);
707 for (bat_attr
= batadv_vlan_attrs
; *bat_attr
; ++bat_attr
) {
708 err
= sysfs_create_file(vlan
->kobj
,
709 &((*bat_attr
)->attr
));
711 batadv_err(dev
, "Can't add sysfs file: %s/%s/%s\n",
712 dev
->name
, vlan_subdir
,
713 ((*bat_attr
)->attr
).name
);
721 for (bat_attr
= batadv_vlan_attrs
; *bat_attr
; ++bat_attr
)
722 sysfs_remove_file(vlan
->kobj
, &((*bat_attr
)->attr
));
724 kobject_put(vlan
->kobj
);
731 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
732 * @bat_priv: the bat priv with all the soft interface information
733 * @vlan: the private data of the VLAN to destroy
735 void batadv_sysfs_del_vlan(struct batadv_priv
*bat_priv
,
736 struct batadv_softif_vlan
*vlan
)
738 struct batadv_attribute
**bat_attr
;
740 for (bat_attr
= batadv_vlan_attrs
; *bat_attr
; ++bat_attr
)
741 sysfs_remove_file(vlan
->kobj
, &((*bat_attr
)->attr
));
743 kobject_put(vlan
->kobj
);
747 static ssize_t
batadv_show_mesh_iface(struct kobject
*kobj
,
748 struct attribute
*attr
, char *buff
)
750 struct net_device
*net_dev
= batadv_kobj_to_netdev(kobj
);
751 struct batadv_hard_iface
*hard_iface
;
755 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
759 if (hard_iface
->if_status
== BATADV_IF_NOT_IN_USE
)
762 ifname
= hard_iface
->soft_iface
->name
;
764 length
= sprintf(buff
, "%s\n", ifname
);
766 batadv_hardif_free_ref(hard_iface
);
771 static ssize_t
batadv_store_mesh_iface(struct kobject
*kobj
,
772 struct attribute
*attr
, char *buff
,
775 struct net_device
*net_dev
= batadv_kobj_to_netdev(kobj
);
776 struct batadv_hard_iface
*hard_iface
;
780 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
784 if (buff
[count
- 1] == '\n')
785 buff
[count
- 1] = '\0';
787 if (strlen(buff
) >= IFNAMSIZ
) {
788 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
790 batadv_hardif_free_ref(hard_iface
);
794 if (strncmp(buff
, "none", 4) == 0)
795 status_tmp
= BATADV_IF_NOT_IN_USE
;
797 status_tmp
= BATADV_IF_I_WANT_YOU
;
799 if (hard_iface
->if_status
== status_tmp
)
802 if ((hard_iface
->soft_iface
) &&
803 (strncmp(hard_iface
->soft_iface
->name
, buff
, IFNAMSIZ
) == 0))
808 if (status_tmp
== BATADV_IF_NOT_IN_USE
) {
809 batadv_hardif_disable_interface(hard_iface
,
810 BATADV_IF_CLEANUP_AUTO
);
814 /* if the interface already is in use */
815 if (hard_iface
->if_status
!= BATADV_IF_NOT_IN_USE
)
816 batadv_hardif_disable_interface(hard_iface
,
817 BATADV_IF_CLEANUP_AUTO
);
819 ret
= batadv_hardif_enable_interface(hard_iface
, buff
);
824 batadv_hardif_free_ref(hard_iface
);
828 static ssize_t
batadv_show_iface_status(struct kobject
*kobj
,
829 struct attribute
*attr
, char *buff
)
831 struct net_device
*net_dev
= batadv_kobj_to_netdev(kobj
);
832 struct batadv_hard_iface
*hard_iface
;
835 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
839 switch (hard_iface
->if_status
) {
840 case BATADV_IF_TO_BE_REMOVED
:
841 length
= sprintf(buff
, "disabling\n");
843 case BATADV_IF_INACTIVE
:
844 length
= sprintf(buff
, "inactive\n");
846 case BATADV_IF_ACTIVE
:
847 length
= sprintf(buff
, "active\n");
849 case BATADV_IF_TO_BE_ACTIVATED
:
850 length
= sprintf(buff
, "enabling\n");
852 case BATADV_IF_NOT_IN_USE
:
854 length
= sprintf(buff
, "not in use\n");
858 batadv_hardif_free_ref(hard_iface
);
863 static BATADV_ATTR(mesh_iface
, S_IRUGO
| S_IWUSR
, batadv_show_mesh_iface
,
864 batadv_store_mesh_iface
);
865 static BATADV_ATTR(iface_status
, S_IRUGO
, batadv_show_iface_status
, NULL
);
867 static struct batadv_attribute
*batadv_batman_attrs
[] = {
868 &batadv_attr_mesh_iface
,
869 &batadv_attr_iface_status
,
873 int batadv_sysfs_add_hardif(struct kobject
**hardif_obj
, struct net_device
*dev
)
875 struct kobject
*hardif_kobject
= &dev
->dev
.kobj
;
876 struct batadv_attribute
**bat_attr
;
879 *hardif_obj
= kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR
,
883 batadv_err(dev
, "Can't add sysfs directory: %s/%s\n", dev
->name
,
884 BATADV_SYSFS_IF_BAT_SUBDIR
);
888 for (bat_attr
= batadv_batman_attrs
; *bat_attr
; ++bat_attr
) {
889 err
= sysfs_create_file(*hardif_obj
, &((*bat_attr
)->attr
));
891 batadv_err(dev
, "Can't add sysfs file: %s/%s/%s\n",
892 dev
->name
, BATADV_SYSFS_IF_BAT_SUBDIR
,
893 ((*bat_attr
)->attr
).name
);
901 for (bat_attr
= batadv_batman_attrs
; *bat_attr
; ++bat_attr
)
902 sysfs_remove_file(*hardif_obj
, &((*bat_attr
)->attr
));
907 void batadv_sysfs_del_hardif(struct kobject
**hardif_obj
)
909 kobject_put(*hardif_obj
);
913 int batadv_throw_uevent(struct batadv_priv
*bat_priv
, enum batadv_uev_type type
,
914 enum batadv_uev_action action
, const char *data
)
917 struct kobject
*bat_kobj
;
918 char *uevent_env
[4] = { NULL
, NULL
, NULL
, NULL
};
920 bat_kobj
= &bat_priv
->soft_iface
->dev
.kobj
;
922 uevent_env
[0] = kasprintf(GFP_ATOMIC
,
923 "%s%s", BATADV_UEV_TYPE_VAR
,
924 batadv_uev_type_str
[type
]);
928 uevent_env
[1] = kasprintf(GFP_ATOMIC
,
929 "%s%s", BATADV_UEV_ACTION_VAR
,
930 batadv_uev_action_str
[action
]);
934 /* If the event is DEL, ignore the data field */
935 if (action
!= BATADV_UEV_DEL
) {
936 uevent_env
[2] = kasprintf(GFP_ATOMIC
,
937 "%s%s", BATADV_UEV_DATA_VAR
, data
);
942 ret
= kobject_uevent_env(bat_kobj
, KOBJ_CHANGE
, uevent_env
);
944 kfree(uevent_env
[0]);
945 kfree(uevent_env
[1]);
946 kfree(uevent_env
[2]);
949 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
950 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
951 batadv_uev_type_str
[type
],
952 batadv_uev_action_str
[action
],
953 (action
== BATADV_UEV_DEL
? "NULL" : data
), ret
);