blob: c1578fa0b952ab2e997d62e4f95f2111df063882 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01002/* Copyright (C) 2010-2017 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000017 */
18
Sven Eckelmannb706b132012-06-10 23:58:51 +020019#include "sysfs.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#include "main.h"
21
22#include <linux/atomic.h>
23#include <linux/compiler.h>
24#include <linux/device.h>
25#include <linux/errno.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010026#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/if.h>
28#include <linux/if_vlan.h>
29#include <linux/kernel.h>
Sven Eckelmann8cfba952017-11-19 17:12:05 +010030#include <linux/kobject.h>
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020031#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020032#include <linux/netdevice.h>
33#include <linux/printk.h>
34#include <linux/rculist.h>
35#include <linux/rcupdate.h>
36#include <linux/rtnetlink.h>
37#include <linux/slab.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020038#include <linux/stddef.h>
39#include <linux/string.h>
40#include <linux/stringify.h>
Sven Eckelmann77d69d82016-06-13 07:41:30 +020041#include <linux/workqueue.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010042#include <uapi/linux/batadv_packet.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020043
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020044#include "bridge_loop_avoidance.h"
Antonio Quartulli33af49a2012-08-08 18:50:57 +020045#include "distributed-arp-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020047#include "gateway_common.h"
48#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020049#include "log.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020050#include "network-coding.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020051#include "soft-interface.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020053static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020054{
55 struct device *dev = container_of(obj->parent, struct device, kobj);
Antonio Quartullif1386942014-05-10 18:56:37 +020056
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020057 return to_net_dev(dev);
58}
59
Sven Eckelmann56303d32012-06-05 22:31:31 +020060static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020061{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020062 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
Antonio Quartullif1386942014-05-10 18:56:37 +020063
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020064 return netdev_priv(net_dev);
65}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066
Antonio Quartulli90f44352013-07-02 11:04:35 +020067/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010068 * batadv_vlan_kobj_to_batpriv() - convert a vlan kobj in the associated batpriv
Antonio Quartulli90f44352013-07-02 11:04:35 +020069 * @obj: kobject to covert
70 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020071 * Return: the associated batadv_priv struct.
Antonio Quartulli90f44352013-07-02 11:04:35 +020072 */
73static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
74{
75 /* VLAN specific attributes are located in the root sysfs folder if they
76 * refer to the untagged VLAN..
77 */
78 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
79 return batadv_kobj_to_batpriv(obj);
80
81 /* ..while the attributes for the tagged vlans are located in
82 * the in the corresponding "vlan%VID" subfolder
83 */
84 return batadv_kobj_to_batpriv(obj->parent);
85}
86
87/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010088 * batadv_kobj_to_vlan() - convert a kobj in the associated softif_vlan struct
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010089 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli90f44352013-07-02 11:04:35 +020090 * @obj: kobject to covert
91 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020092 * Return: the associated softif_vlan struct if found, NULL otherwise.
Antonio Quartulli90f44352013-07-02 11:04:35 +020093 */
94static struct batadv_softif_vlan *
95batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
96{
97 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
98
99 rcu_read_lock();
100 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
101 if (vlan_tmp->kobj != obj)
102 continue;
103
Sven Eckelmann6be4d302016-01-16 10:29:42 +0100104 if (!kref_get_unless_zero(&vlan_tmp->refcount))
Antonio Quartulli90f44352013-07-02 11:04:35 +0200105 continue;
106
107 vlan = vlan_tmp;
108 break;
109 }
110 rcu_read_unlock();
111
112 return vlan;
113}
114
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200115#define BATADV_UEV_TYPE_VAR "BATTYPE="
116#define BATADV_UEV_ACTION_VAR "BATACTION="
117#define BATADV_UEV_DATA_VAR "BATDATA="
Antonio Quartullic6bda682011-04-26 18:26:01 +0200118
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200119static char *batadv_uev_action_str[] = {
Antonio Quartullic6bda682011-04-26 18:26:01 +0200120 "add",
121 "del",
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100122 "change",
123 "loopdetect",
Antonio Quartullic6bda682011-04-26 18:26:01 +0200124};
125
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200126static char *batadv_uev_type_str[] = {
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100127 "gw",
128 "bla",
Antonio Quartullic6bda682011-04-26 18:26:01 +0200129};
130
Antonio Quartulli90f44352013-07-02 11:04:35 +0200131/* Use this, if you have customized show and store functions for vlan attrs */
132#define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
133struct batadv_attribute batadv_attr_vlan_##_name = { \
134 .attr = {.name = __stringify(_name), \
135 .mode = _mode }, \
136 .show = _show, \
137 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200138}
Antonio Quartulli90f44352013-07-02 11:04:35 +0200139
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140/* Use this, if you have customized show and store functions */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200141#define BATADV_ATTR(_name, _mode, _show, _store) \
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200142struct batadv_attribute batadv_attr_##_name = { \
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200143 .attr = {.name = __stringify(_name), \
144 .mode = _mode }, \
145 .show = _show, \
146 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200147}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000148
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200149#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200150ssize_t batadv_store_##_name(struct kobject *kobj, \
151 struct attribute *attr, char *buff, \
152 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200154 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200155 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200156 \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200157 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
158 &bat_priv->_name, net_dev); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000159}
160
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200161#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200162ssize_t batadv_show_##_name(struct kobject *kobj, \
163 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200165 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200166 \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167 return sprintf(buff, "%s\n", \
168 atomic_read(&bat_priv->_name) == 0 ? \
169 "disabled" : "enabled"); \
170} \
171
Marek Lindnerf245c382012-03-11 06:17:51 +0800172/* Use this, if you are going to turn a [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200173 * (bat_priv) on or off
174 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200175#define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
176 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
177 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
178 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
179 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800181#define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200182ssize_t batadv_store_##_name(struct kobject *kobj, \
183 struct attribute *attr, char *buff, \
184 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200186 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200187 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200188 \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200189 return __batadv_store_uint_attr(buff, count, _min, _max, \
190 _post_func, attr, \
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800191 &bat_priv->_var, net_dev); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192}
193
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800194#define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200195ssize_t batadv_show_##_name(struct kobject *kobj, \
196 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200198 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200199 \
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800200 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201} \
202
Marek Lindnerf245c382012-03-11 06:17:51 +0800203/* Use this, if you are going to set [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200204 * (bat_priv) to an unsigned integer value
205 */
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800206#define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
207 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
208 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200209 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
210 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211
Antonio Quartulli90f44352013-07-02 11:04:35 +0200212#define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
213ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
214 struct attribute *attr, char *buff, \
215 size_t count) \
216{ \
217 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
218 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
219 kobj); \
220 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
221 attr, &vlan->_name, \
222 bat_priv->soft_iface); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200223 \
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100224 batadv_softif_vlan_put(vlan); \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200225 return res; \
226}
227
228#define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
229ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
230 struct attribute *attr, char *buff) \
231{ \
232 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
233 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
234 kobj); \
235 size_t res = sprintf(buff, "%s\n", \
236 atomic_read(&vlan->_name) == 0 ? \
237 "disabled" : "enabled"); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200238 \
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100239 batadv_softif_vlan_put(vlan); \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200240 return res; \
241}
242
243/* Use this, if you are going to turn a [name] in the vlan struct on or off */
244#define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
245 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
246 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
247 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
248 batadv_store_vlan_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249
Linus Luessing0744ff82016-01-16 16:40:08 +0800250#define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
251ssize_t batadv_store_##_name(struct kobject *kobj, \
252 struct attribute *attr, char *buff, \
253 size_t count) \
254{ \
255 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
256 struct batadv_hard_iface *hard_iface; \
257 ssize_t length; \
258 \
259 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
260 if (!hard_iface) \
261 return 0; \
262 \
263 length = __batadv_store_uint_attr(buff, count, _min, _max, \
264 _post_func, attr, \
265 &hard_iface->_var, net_dev); \
266 \
267 batadv_hardif_put(hard_iface); \
268 return length; \
269}
270
271#define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
272ssize_t batadv_show_##_name(struct kobject *kobj, \
273 struct attribute *attr, char *buff) \
274{ \
275 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
276 struct batadv_hard_iface *hard_iface; \
277 ssize_t length; \
278 \
279 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
280 if (!hard_iface) \
281 return 0; \
282 \
283 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
284 \
285 batadv_hardif_put(hard_iface); \
286 return length; \
287}
288
289/* Use this, if you are going to set [name] in hard_iface to an
290 * unsigned integer value
291 */
292#define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
293 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
294 _max, _post_func) \
295 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
296 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
297 batadv_store_##_name)
298
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200299static int batadv_store_bool_attr(char *buff, size_t count,
300 struct net_device *net_dev,
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100301 const char *attr_name, atomic_t *attr,
302 bool *changed)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303{
304 int enabled = -1;
305
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100306 *changed = false;
307
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308 if (buff[count - 1] == '\n')
309 buff[count - 1] = '\0';
310
311 if ((strncmp(buff, "1", 2) == 0) ||
312 (strncmp(buff, "enable", 7) == 0) ||
313 (strncmp(buff, "enabled", 8) == 0))
314 enabled = 1;
315
316 if ((strncmp(buff, "0", 2) == 0) ||
317 (strncmp(buff, "disable", 8) == 0) ||
318 (strncmp(buff, "disabled", 9) == 0))
319 enabled = 0;
320
321 if (enabled < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200322 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
323 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324 return -EINVAL;
325 }
326
327 if (atomic_read(attr) == enabled)
328 return count;
329
Sven Eckelmann3e348192012-05-16 20:23:22 +0200330 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
331 atomic_read(attr) == 1 ? "enabled" : "disabled",
332 enabled == 1 ? "enabled" : "disabled");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100334 *changed = true;
335
Eric Dumazet95c96172012-04-15 05:58:06 +0000336 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337 return count;
338}
339
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200340static inline ssize_t
341__batadv_store_bool_attr(char *buff, size_t count,
342 void (*post_func)(struct net_device *),
343 struct attribute *attr,
344 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345{
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100346 bool changed;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 int ret;
348
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200349 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100350 attr_store, &changed);
351 if (post_func && changed)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352 post_func(net_dev);
353
354 return ret;
355}
356
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200357static int batadv_store_uint_attr(const char *buff, size_t count,
358 struct net_device *net_dev,
359 const char *attr_name,
360 unsigned int min, unsigned int max,
361 atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362{
363 unsigned long uint_val;
364 int ret;
365
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200366 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367 if (ret) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200368 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
369 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370 return -EINVAL;
371 }
372
373 if (uint_val < min) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200374 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
375 attr_name, uint_val, min);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376 return -EINVAL;
377 }
378
379 if (uint_val > max) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200380 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
381 attr_name, uint_val, max);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382 return -EINVAL;
383 }
384
385 if (atomic_read(attr) == uint_val)
386 return count;
387
Sven Eckelmann3e348192012-05-16 20:23:22 +0200388 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
389 attr_name, atomic_read(attr), uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390
391 atomic_set(attr, uint_val);
392 return count;
393}
394
Antonio Quartullic149ca72016-05-06 02:46:37 +0800395static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
396 int min, int max,
397 void (*post_func)(struct net_device *),
398 const struct attribute *attr,
399 atomic_t *attr_store,
400 struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401{
402 int ret;
403
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200404 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
405 attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406 if (post_func && ret)
407 post_func(net_dev);
408
409 return ret;
410}
411
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200412static ssize_t batadv_show_bat_algo(struct kobject *kobj,
413 struct attribute *attr, char *buff)
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800414{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200415 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Antonio Quartullif1386942014-05-10 18:56:37 +0200416
Antonio Quartulli29824a52016-05-25 23:27:31 +0800417 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800418}
419
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100420static void batadv_post_gw_reselect(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200422 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartullif1386942014-05-10 18:56:37 +0200423
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100424 batadv_gw_reselect(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425}
426
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200427static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
428 char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200430 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 int bytes_written;
432
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200433 /* GW mode is not available if the routing algorithm in use does not
434 * implement the GW API
435 */
436 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
437 !bat_priv->algo_ops->gw.is_eligible)
438 return -ENOENT;
439
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800440 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200441 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200442 bytes_written = sprintf(buff, "%s\n",
443 BATADV_GW_MODE_CLIENT_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200445 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200446 bytes_written = sprintf(buff, "%s\n",
447 BATADV_GW_MODE_SERVER_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 break;
449 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200450 bytes_written = sprintf(buff, "%s\n",
451 BATADV_GW_MODE_OFF_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 break;
453 }
454
455 return bytes_written;
456}
457
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200458static ssize_t batadv_store_gw_mode(struct kobject *kobj,
459 struct attribute *attr, char *buff,
460 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200462 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200463 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464 char *curr_gw_mode_str;
465 int gw_mode_tmp = -1;
466
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200467 /* toggling GW mode is allowed only if the routing algorithm in use
468 * provides the GW API
469 */
470 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
471 !bat_priv->algo_ops->gw.is_eligible)
472 return -EINVAL;
473
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474 if (buff[count - 1] == '\n')
475 buff[count - 1] = '\0';
476
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200477 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
478 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200479 gw_mode_tmp = BATADV_GW_MODE_OFF;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200481 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
482 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200483 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200485 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
486 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200487 gw_mode_tmp = BATADV_GW_MODE_SERVER;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488
489 if (gw_mode_tmp < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200490 batadv_info(net_dev,
491 "Invalid parameter for 'gw mode' setting received: %s\n",
492 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493 return -EINVAL;
494 }
495
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800496 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497 return count;
498
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800499 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200500 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200501 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200503 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200504 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505 break;
506 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200507 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 break;
509 }
510
Sven Eckelmann3e348192012-05-16 20:23:22 +0200511 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
512 curr_gw_mode_str, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513
Antonio Quartullif3163182013-11-04 20:59:40 +0100514 /* Invoking batadv_gw_reselect() is not enough to really de-select the
515 * current GW. It will only instruct the gateway client code to perform
516 * a re-election the next time that this is needed.
517 *
518 * When gw client mode is being switched off the current GW must be
519 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
520 * client mode re-activation. This is operation is performed in
521 * batadv_gw_check_client_stop().
522 */
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100523 batadv_gw_reselect(bat_priv);
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200524 /* always call batadv_gw_check_client_stop() before changing the gateway
525 * state
526 */
527 batadv_gw_check_client_stop(bat_priv);
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800528 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
Marek Lindner414254e2013-04-23 21:39:58 +0800529 batadv_gw_tvlv_container_update(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530 return count;
531}
532
Antonio Quartulli08686942016-07-03 12:46:32 +0200533static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
534 struct attribute *attr, char *buff)
535{
536 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
537
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200538 /* GW selection class is not available if the routing algorithm in use
539 * does not implement the GW API
540 */
541 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
542 !bat_priv->algo_ops->gw.is_eligible)
543 return -ENOENT;
544
Antonio Quartulli08686942016-07-03 12:46:32 +0200545 if (bat_priv->algo_ops->gw.show_sel_class)
546 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
547
548 return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
549}
550
551static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
552 struct attribute *attr, char *buff,
553 size_t count)
554{
555 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
556
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200557 /* setting the GW selection class is allowed only if the routing
558 * algorithm in use implements the GW API
559 */
560 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
561 !bat_priv->algo_ops->gw.is_eligible)
562 return -EINVAL;
563
Antonio Quartulli08686942016-07-03 12:46:32 +0200564 if (buff[count - 1] == '\n')
565 buff[count - 1] = '\0';
566
567 if (bat_priv->algo_ops->gw.store_sel_class)
568 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
569 count);
570
571 return __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
572 batadv_post_gw_reselect, attr,
573 &bat_priv->gw.sel_class,
574 bat_priv->soft_iface);
575}
576
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200577static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
578 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200580 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200581 u32 down, up;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582
Marek Lindner414254e2013-04-23 21:39:58 +0800583 down = atomic_read(&bat_priv->gw.bandwidth_down);
584 up = atomic_read(&bat_priv->gw.bandwidth_up);
585
586 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
587 down % 10, up / 10, up % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588}
589
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200590static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
591 struct attribute *attr, char *buff,
592 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200594 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595
596 if (buff[count - 1] == '\n')
597 buff[count - 1] = '\0';
598
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200599 return batadv_gw_bandwidth_set(net_dev, buff, count);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000600}
601
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100602/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100603 * batadv_show_isolation_mark() - print the current isolation mark/mask
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100604 * @kobj: kobject representing the private mesh sysfs directory
605 * @attr: the batman-adv attribute the user is interacting with
606 * @buff: the buffer that will contain the data to send back to the user
607 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200608 * Return: the number of bytes written into 'buff' on success or a negative
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100609 * error code in case of failure
610 */
611static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
612 struct attribute *attr, char *buff)
613{
614 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
615
616 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
617 bat_priv->isolation_mark_mask);
618}
619
620/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100621 * batadv_store_isolation_mark() - parse and store the isolation mark/mask
622 * entered by the user
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100623 * @kobj: kobject representing the private mesh sysfs directory
624 * @attr: the batman-adv attribute the user is interacting with
625 * @buff: the buffer containing the user data
626 * @count: number of bytes in the buffer
627 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200628 * Return: 'count' on success or a negative error code in case of failure
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100629 */
630static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
631 struct attribute *attr, char *buff,
632 size_t count)
633{
634 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
635 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200636 u32 mark, mask;
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100637 char *mask_ptr;
638
639 /* parse the mask if it has been specified, otherwise assume the mask is
640 * the biggest possible
641 */
642 mask = 0xFFFFFFFF;
643 mask_ptr = strchr(buff, '/');
644 if (mask_ptr) {
645 *mask_ptr = '\0';
646 mask_ptr++;
647
648 /* the mask must be entered in hex base as it is going to be a
649 * bitmask and not a prefix length
650 */
651 if (kstrtou32(mask_ptr, 16, &mask) < 0)
652 return -EINVAL;
653 }
654
655 /* the mark can be entered in any base */
656 if (kstrtou32(buff, 0, &mark) < 0)
657 return -EINVAL;
658
659 bat_priv->isolation_mark_mask = mask;
660 /* erase bits not covered by the mask */
661 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
662
663 batadv_info(net_dev,
664 "New skb mark for extended isolation: %#.8x/%#.8x\n",
665 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
666
667 return count;
668}
669
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200670BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
671BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100672#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200673BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100674#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200675#ifdef CONFIG_BATMAN_ADV_DAT
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200676BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200677#endif
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200678BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
679static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
680static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
681BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
682 INT_MAX, NULL);
683BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
684 NULL);
685static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
Antonio Quartulli08686942016-07-03 12:46:32 +0200686 batadv_store_gw_sel_class);
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200687static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200688 batadv_store_gw_bwidth);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100689#ifdef CONFIG_BATMAN_ADV_MCAST
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200690BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100691#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200693BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100695#ifdef CONFIG_BATMAN_ADV_NC
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200696BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100697#endif
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200698static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
699 batadv_store_isolation_mark);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200701static struct batadv_attribute *batadv_mesh_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200702 &batadv_attr_aggregated_ogms,
703 &batadv_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100704#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200705 &batadv_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100706#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200707#ifdef CONFIG_BATMAN_ADV_DAT
708 &batadv_attr_distributed_arp_table,
709#endif
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100710#ifdef CONFIG_BATMAN_ADV_MCAST
711 &batadv_attr_multicast_mode,
712#endif
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200713 &batadv_attr_fragmentation,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200714 &batadv_attr_routing_algo,
715 &batadv_attr_gw_mode,
716 &batadv_attr_orig_interval,
717 &batadv_attr_hop_penalty,
718 &batadv_attr_gw_sel_class,
719 &batadv_attr_gw_bandwidth,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200721 &batadv_attr_log_level,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000722#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100723#ifdef CONFIG_BATMAN_ADV_NC
724 &batadv_attr_network_coding,
725#endif
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100726 &batadv_attr_isolation_mark,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000727 NULL,
728};
729
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200730BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200731
Sven Eckelmannf34ac9d2015-09-06 21:38:53 +0200732/* array of vlan specific sysfs attributes */
Antonio Quartulli90f44352013-07-02 11:04:35 +0200733static struct batadv_attribute *batadv_vlan_attrs[] = {
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200734 &batadv_attr_vlan_ap_isolation,
Antonio Quartulli90f44352013-07-02 11:04:35 +0200735 NULL,
736};
737
Sven Eckelmannff15c272017-12-02 19:51:53 +0100738/**
739 * batadv_sysfs_add_meshif() - Add soft interface specific sysfs entries
740 * @dev: netdev struct of the soft interface
741 *
742 * Return: 0 on success or negative error number in case of failure
743 */
Sven Eckelmann5853e222012-05-12 02:09:24 +0200744int batadv_sysfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000745{
746 struct kobject *batif_kobject = &dev->dev.kobj;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200747 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200748 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749 int err;
750
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200751 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752 batif_kobject);
753 if (!bat_priv->mesh_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200754 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200755 BATADV_SYSFS_IF_MESH_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000756 goto out;
757 }
758
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200759 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760 err = sysfs_create_file(bat_priv->mesh_obj,
761 &((*bat_attr)->attr));
762 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200763 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200764 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +0200765 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000766 goto rem_attr;
767 }
768 }
769
770 return 0;
771
772rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200773 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
775
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200776 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
777 kobject_del(bat_priv->mesh_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778 kobject_put(bat_priv->mesh_obj);
779 bat_priv->mesh_obj = NULL;
780out:
781 return -ENOMEM;
782}
783
Sven Eckelmannff15c272017-12-02 19:51:53 +0100784/**
785 * batadv_sysfs_del_meshif() - Remove soft interface specific sysfs entries
786 * @dev: netdev struct of the soft interface
787 */
Sven Eckelmann5853e222012-05-12 02:09:24 +0200788void batadv_sysfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000789{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200790 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200791 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000792
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200793 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
795
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200796 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
797 kobject_del(bat_priv->mesh_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798 kobject_put(bat_priv->mesh_obj);
799 bat_priv->mesh_obj = NULL;
800}
801
Antonio Quartulli90f44352013-07-02 11:04:35 +0200802/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100803 * batadv_sysfs_add_vlan() - add all the needed sysfs objects for the new vlan
Antonio Quartulli90f44352013-07-02 11:04:35 +0200804 * @dev: netdev of the mesh interface
805 * @vlan: private data of the newly added VLAN interface
806 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200807 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
Antonio Quartulli90f44352013-07-02 11:04:35 +0200808 */
809int batadv_sysfs_add_vlan(struct net_device *dev,
810 struct batadv_softif_vlan *vlan)
811{
812 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
813 struct batadv_priv *bat_priv = netdev_priv(dev);
814 struct batadv_attribute **bat_attr;
815 int err;
816
817 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
818 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
819 vlan->vid & VLAN_VID_MASK);
820
821 vlan->kobj = kobject_create_and_add(vlan_subdir,
822 bat_priv->mesh_obj);
823 if (!vlan->kobj) {
824 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
825 dev->name, vlan_subdir);
826 goto out;
827 }
828 } else {
829 /* the untagged LAN uses the root folder to store its "VLAN
830 * specific attributes"
831 */
832 vlan->kobj = bat_priv->mesh_obj;
833 kobject_get(bat_priv->mesh_obj);
834 }
835
836 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
837 err = sysfs_create_file(vlan->kobj,
838 &((*bat_attr)->attr));
839 if (err) {
840 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
841 dev->name, vlan_subdir,
842 ((*bat_attr)->attr).name);
843 goto rem_attr;
844 }
845 }
846
847 return 0;
848
849rem_attr:
850 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
851 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
852
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200853 if (vlan->kobj != bat_priv->mesh_obj) {
854 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
855 kobject_del(vlan->kobj);
856 }
Antonio Quartulli90f44352013-07-02 11:04:35 +0200857 kobject_put(vlan->kobj);
858 vlan->kobj = NULL;
859out:
860 return -ENOMEM;
861}
862
863/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100864 * batadv_sysfs_del_vlan() - remove all the sysfs objects for a given VLAN
Antonio Quartulli90f44352013-07-02 11:04:35 +0200865 * @bat_priv: the bat priv with all the soft interface information
866 * @vlan: the private data of the VLAN to destroy
867 */
868void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
869 struct batadv_softif_vlan *vlan)
870{
871 struct batadv_attribute **bat_attr;
872
873 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
874 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
875
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200876 if (vlan->kobj != bat_priv->mesh_obj) {
877 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
878 kobject_del(vlan->kobj);
879 }
Antonio Quartulli90f44352013-07-02 11:04:35 +0200880 kobject_put(vlan->kobj);
881 vlan->kobj = NULL;
882}
883
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200884static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
885 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000886{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200887 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200888 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889 ssize_t length;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200890 const char *ifname;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891
Sven Eckelmann56303d32012-06-05 22:31:31 +0200892 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000893 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894 return 0;
895
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200896 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
897 ifname = "none";
898 else
899 ifname = hard_iface->soft_iface->name;
900
901 length = sprintf(buff, "%s\n", ifname);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000902
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100903 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000904
905 return length;
906}
907
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200908/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100909 * batadv_store_mesh_iface_finish() - store new hardif mesh_iface state
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200910 * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
911 * @ifname: name of soft-interface to modify
912 *
913 * Changes the parts of the hard+soft interface which can not be modified under
914 * sysfs lock (to prevent deadlock situations).
915 *
916 * Return: 0 on success, 0 < on failure
917 */
918static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
919 char ifname[IFNAMSIZ])
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000920{
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200921 struct net *net = dev_net(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200922 struct batadv_hard_iface *hard_iface;
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200923 int status_tmp;
924 int ret = 0;
925
926 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000927
Sven Eckelmann56303d32012-06-05 22:31:31 +0200928 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000929 if (!hard_iface)
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200930 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200932 if (strncmp(ifname, "none", 4) == 0)
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200933 status_tmp = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000934 else
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200935 status_tmp = BATADV_IF_I_WANT_YOU;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000936
Marek Lindnere6c10f42011-02-18 12:33:20 +0000937 if (hard_iface->if_status == status_tmp)
938 goto out;
939
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200940 if (hard_iface->soft_iface &&
941 strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)
Marek Lindnered75ccb2011-02-10 14:33:51 +0000942 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000943
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200944 if (status_tmp == BATADV_IF_NOT_IN_USE) {
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800945 batadv_hardif_disable_interface(hard_iface,
946 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200947 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000948 }
949
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200950 /* if the interface already is in use */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200951 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800952 batadv_hardif_disable_interface(hard_iface,
953 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000954
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200955 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000956out:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100957 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000958 return ret;
959}
960
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200961/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100962 * batadv_store_mesh_iface_work() - store new hardif mesh_iface state
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200963 * @work: work queue item
964 *
965 * Changes the parts of the hard+soft interface which can not be modified under
966 * sysfs lock (to prevent deadlock situations).
967 */
968static void batadv_store_mesh_iface_work(struct work_struct *work)
969{
970 struct batadv_store_mesh_work *store_work;
971 int ret;
972
973 store_work = container_of(work, struct batadv_store_mesh_work, work);
974
975 rtnl_lock();
976 ret = batadv_store_mesh_iface_finish(store_work->net_dev,
977 store_work->soft_iface_name);
978 rtnl_unlock();
979
980 if (ret < 0)
981 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
982 store_work->soft_iface_name, store_work->net_dev->name,
983 ret);
984
985 dev_put(store_work->net_dev);
986 kfree(store_work);
987}
988
989static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
990 struct attribute *attr, char *buff,
991 size_t count)
992{
993 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
994 struct batadv_store_mesh_work *store_work;
995
996 if (buff[count - 1] == '\n')
997 buff[count - 1] = '\0';
998
999 if (strlen(buff) >= IFNAMSIZ) {
1000 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
1001 buff);
1002 return -EINVAL;
1003 }
1004
1005 store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
1006 if (!store_work)
1007 return -ENOMEM;
1008
1009 dev_hold(net_dev);
1010 INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1011 store_work->net_dev = net_dev;
1012 strlcpy(store_work->soft_iface_name, buff,
1013 sizeof(store_work->soft_iface_name));
1014
1015 queue_work(batadv_event_workqueue, &store_work->work);
1016
1017 return count;
1018}
1019
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001020static ssize_t batadv_show_iface_status(struct kobject *kobj,
1021 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001022{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001023 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001024 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001025 ssize_t length;
1026
Sven Eckelmann56303d32012-06-05 22:31:31 +02001027 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +00001028 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001029 return 0;
1030
Marek Lindnere6c10f42011-02-18 12:33:20 +00001031 switch (hard_iface->if_status) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001032 case BATADV_IF_TO_BE_REMOVED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001033 length = sprintf(buff, "disabling\n");
1034 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001035 case BATADV_IF_INACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001036 length = sprintf(buff, "inactive\n");
1037 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001038 case BATADV_IF_ACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001039 length = sprintf(buff, "active\n");
1040 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001041 case BATADV_IF_TO_BE_ACTIVATED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001042 length = sprintf(buff, "enabling\n");
1043 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001044 case BATADV_IF_NOT_IN_USE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001045 default:
1046 length = sprintf(buff, "not in use\n");
1047 break;
1048 }
1049
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001050 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001051
1052 return length;
1053}
1054
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001055#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1056
1057/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001058 * batadv_store_throughput_override() - parse and store throughput override
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001059 * entered by the user
1060 * @kobj: kobject representing the private mesh sysfs directory
1061 * @attr: the batman-adv attribute the user is interacting with
1062 * @buff: the buffer containing the user data
1063 * @count: number of bytes in the buffer
1064 *
1065 * Return: 'count' on success or a negative error code in case of failure
1066 */
1067static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1068 struct attribute *attr,
1069 char *buff, size_t count)
1070{
1071 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1072 struct batadv_hard_iface *hard_iface;
1073 u32 tp_override;
1074 u32 old_tp_override;
1075 bool ret;
1076
1077 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1078 if (!hard_iface)
1079 return -EINVAL;
1080
1081 if (buff[count - 1] == '\n')
1082 buff[count - 1] = '\0';
1083
1084 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1085 &tp_override);
1086 if (!ret)
1087 return count;
1088
1089 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1090 if (old_tp_override == tp_override)
1091 goto out;
1092
1093 batadv_info(net_dev, "%s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1094 "throughput_override",
1095 old_tp_override / 10, old_tp_override % 10,
1096 tp_override / 10, tp_override % 10);
1097
1098 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1099
1100out:
1101 batadv_hardif_put(hard_iface);
1102 return count;
1103}
1104
1105static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1106 struct attribute *attr,
1107 char *buff)
1108{
1109 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1110 struct batadv_hard_iface *hard_iface;
1111 u32 tp_override;
1112
1113 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1114 if (!hard_iface)
1115 return -EINVAL;
1116
1117 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1118
1119 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1120 tp_override % 10);
1121}
1122
1123#endif
1124
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001125static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
Sven Eckelmann347c80f2012-06-03 22:19:07 +02001126 batadv_store_mesh_iface);
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001127static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
Linus Luessing7f136cd2016-01-16 16:40:11 +08001128#ifdef CONFIG_BATMAN_ADV_BATMAN_V
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001129BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001130 2 * BATADV_JITTER, INT_MAX, NULL);
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001131static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001132 batadv_store_throughput_override);
Linus Luessing7f136cd2016-01-16 16:40:11 +08001133#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001134
Sven Eckelmannb4d66b82012-06-05 22:31:29 +02001135static struct batadv_attribute *batadv_batman_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001136 &batadv_attr_mesh_iface,
1137 &batadv_attr_iface_status,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001138#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1139 &batadv_attr_elp_interval,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001140 &batadv_attr_throughput_override,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001141#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001142 NULL,
1143};
1144
Sven Eckelmannff15c272017-12-02 19:51:53 +01001145/**
1146 * batadv_sysfs_add_hardif() - Add hard interface specific sysfs entries
1147 * @hardif_obj: address where to store the pointer to new sysfs folder
1148 * @dev: netdev struct of the hard interface
1149 *
1150 * Return: 0 on success or negative error number in case of failure
1151 */
Sven Eckelmann5853e222012-05-12 02:09:24 +02001152int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001153{
1154 struct kobject *hardif_kobject = &dev->dev.kobj;
Sven Eckelmannb4d66b82012-06-05 22:31:29 +02001155 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001156 int err;
1157
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001158 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1159 hardif_kobject);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001160
1161 if (!*hardif_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +02001162 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001163 BATADV_SYSFS_IF_BAT_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001164 goto out;
1165 }
1166
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001167 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001168 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1169 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +02001170 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001171 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +02001172 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001173 goto rem_attr;
1174 }
1175 }
1176
1177 return 0;
1178
1179rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001180 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001181 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1182out:
1183 return -ENOMEM;
1184}
1185
Sven Eckelmannff15c272017-12-02 19:51:53 +01001186/**
1187 * batadv_sysfs_del_hardif() - Remove hard interface specific sysfs entries
1188 * @hardif_obj: address to the pointer to which stores batman-adv sysfs folder
1189 * of the hard interface
1190 */
Sven Eckelmann5853e222012-05-12 02:09:24 +02001191void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001192{
Sven Eckelmannf4acb102016-06-13 07:41:32 +02001193 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1194 kobject_del(*hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001195 kobject_put(*hardif_obj);
1196 *hardif_obj = NULL;
1197}
Antonio Quartullic6bda682011-04-26 18:26:01 +02001198
Sven Eckelmannff15c272017-12-02 19:51:53 +01001199/**
1200 * batadv_throw_uevent() - Send an uevent with batman-adv specific env data
1201 * @bat_priv: the bat priv with all the soft interface information
1202 * @type: subsystem type of event. Stored in uevent's BATTYPE
1203 * @action: action type of event. Stored in uevent's BATACTION
1204 * @data: string with additional information to the event (ignored for
1205 * BATADV_UEV_DEL). Stored in uevent's BATDATA
1206 *
1207 * Return: 0 on success or negative error number in case of failure
1208 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001209int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001210 enum batadv_uev_action action, const char *data)
Antonio Quartullic6bda682011-04-26 18:26:01 +02001211{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001212 int ret = -ENOMEM;
Antonio Quartullic6bda682011-04-26 18:26:01 +02001213 struct kobject *bat_kobj;
1214 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1215
Marek Lindner736292c2013-01-12 19:19:06 +08001216 bat_kobj = &bat_priv->soft_iface->dev.kobj;
Antonio Quartullic6bda682011-04-26 18:26:01 +02001217
Himangi Saraogib98fe242014-06-29 00:06:29 +05301218 uevent_env[0] = kasprintf(GFP_ATOMIC,
1219 "%s%s", BATADV_UEV_TYPE_VAR,
1220 batadv_uev_type_str[type]);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001221 if (!uevent_env[0])
1222 goto out;
1223
Himangi Saraogib98fe242014-06-29 00:06:29 +05301224 uevent_env[1] = kasprintf(GFP_ATOMIC,
1225 "%s%s", BATADV_UEV_ACTION_VAR,
1226 batadv_uev_action_str[action]);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001227 if (!uevent_env[1])
1228 goto out;
1229
Antonio Quartullic6bda682011-04-26 18:26:01 +02001230 /* If the event is DEL, ignore the data field */
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001231 if (action != BATADV_UEV_DEL) {
Himangi Saraogib98fe242014-06-29 00:06:29 +05301232 uevent_env[2] = kasprintf(GFP_ATOMIC,
1233 "%s%s", BATADV_UEV_DATA_VAR, data);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001234 if (!uevent_env[2])
1235 goto out;
Antonio Quartullic6bda682011-04-26 18:26:01 +02001236 }
1237
1238 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1239out:
1240 kfree(uevent_env[0]);
1241 kfree(uevent_env[1]);
1242 kfree(uevent_env[2]);
1243
Antonio Quartullic6bda682011-04-26 18:26:01 +02001244 if (ret)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001245 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001246 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001247 batadv_uev_type_str[type],
1248 batadv_uev_action_str[action],
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001249 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001250 return ret;
1251}