blob: 0ae8b30e4eaaf4e3513e9a552de2ae51791d2554 [file] [log] [blame]
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01001/* Copyright (C) 2010-2017 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner
4 *
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.
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannb706b132012-06-10 23:58:51 +020018#include "sysfs.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/compiler.h>
23#include <linux/device.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/if.h>
27#include <linux/if_vlan.h>
28#include <linux/kernel.h>
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020029#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020030#include <linux/netdevice.h>
31#include <linux/printk.h>
32#include <linux/rculist.h>
33#include <linux/rcupdate.h>
34#include <linux/rtnetlink.h>
35#include <linux/slab.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020036#include <linux/stddef.h>
37#include <linux/string.h>
38#include <linux/stringify.h>
Sven Eckelmann77d69d82016-06-13 07:41:30 +020039#include <linux/workqueue.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020040
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020041#include "bridge_loop_avoidance.h"
Antonio Quartulli33af49a2012-08-08 18:50:57 +020042#include "distributed-arp-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020044#include "gateway_common.h"
45#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020046#include "log.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020047#include "network-coding.h"
48#include "packet.h"
49#include "soft-interface.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020051static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020052{
53 struct device *dev = container_of(obj->parent, struct device, kobj);
Antonio Quartullif1386942014-05-10 18:56:37 +020054
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020055 return to_net_dev(dev);
56}
57
Sven Eckelmann56303d32012-06-05 22:31:31 +020058static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020059{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020060 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
Antonio Quartullif1386942014-05-10 18:56:37 +020061
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020062 return netdev_priv(net_dev);
63}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064
Antonio Quartulli90f44352013-07-02 11:04:35 +020065/**
66 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
67 * @obj: kobject to covert
68 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020069 * Return: the associated batadv_priv struct.
Antonio Quartulli90f44352013-07-02 11:04:35 +020070 */
71static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
72{
73 /* VLAN specific attributes are located in the root sysfs folder if they
74 * refer to the untagged VLAN..
75 */
76 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
77 return batadv_kobj_to_batpriv(obj);
78
79 /* ..while the attributes for the tagged vlans are located in
80 * the in the corresponding "vlan%VID" subfolder
81 */
82 return batadv_kobj_to_batpriv(obj->parent);
83}
84
85/**
86 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010087 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli90f44352013-07-02 11:04:35 +020088 * @obj: kobject to covert
89 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020090 * Return: the associated softif_vlan struct if found, NULL otherwise.
Antonio Quartulli90f44352013-07-02 11:04:35 +020091 */
92static struct batadv_softif_vlan *
93batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
94{
95 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
96
97 rcu_read_lock();
98 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
99 if (vlan_tmp->kobj != obj)
100 continue;
101
Sven Eckelmann6be4d302016-01-16 10:29:42 +0100102 if (!kref_get_unless_zero(&vlan_tmp->refcount))
Antonio Quartulli90f44352013-07-02 11:04:35 +0200103 continue;
104
105 vlan = vlan_tmp;
106 break;
107 }
108 rcu_read_unlock();
109
110 return vlan;
111}
112
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200113#define BATADV_UEV_TYPE_VAR "BATTYPE="
114#define BATADV_UEV_ACTION_VAR "BATACTION="
115#define BATADV_UEV_DATA_VAR "BATDATA="
Antonio Quartullic6bda682011-04-26 18:26:01 +0200116
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200117static char *batadv_uev_action_str[] = {
Antonio Quartullic6bda682011-04-26 18:26:01 +0200118 "add",
119 "del",
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100120 "change",
121 "loopdetect",
Antonio Quartullic6bda682011-04-26 18:26:01 +0200122};
123
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200124static char *batadv_uev_type_str[] = {
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100125 "gw",
126 "bla",
Antonio Quartullic6bda682011-04-26 18:26:01 +0200127};
128
Antonio Quartulli90f44352013-07-02 11:04:35 +0200129/* Use this, if you have customized show and store functions for vlan attrs */
130#define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
131struct batadv_attribute batadv_attr_vlan_##_name = { \
132 .attr = {.name = __stringify(_name), \
133 .mode = _mode }, \
134 .show = _show, \
135 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200136}
Antonio Quartulli90f44352013-07-02 11:04:35 +0200137
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000138/* Use this, if you have customized show and store functions */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200139#define BATADV_ATTR(_name, _mode, _show, _store) \
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200140struct batadv_attribute batadv_attr_##_name = { \
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200141 .attr = {.name = __stringify(_name), \
142 .mode = _mode }, \
143 .show = _show, \
144 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200145}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200147#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200148ssize_t batadv_store_##_name(struct kobject *kobj, \
149 struct attribute *attr, char *buff, \
150 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200152 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200153 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200154 \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200155 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
156 &bat_priv->_name, net_dev); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157}
158
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200159#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200160ssize_t batadv_show_##_name(struct kobject *kobj, \
161 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000162{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200163 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200164 \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165 return sprintf(buff, "%s\n", \
166 atomic_read(&bat_priv->_name) == 0 ? \
167 "disabled" : "enabled"); \
168} \
169
Marek Lindnerf245c382012-03-11 06:17:51 +0800170/* Use this, if you are going to turn a [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200171 * (bat_priv) on or off
172 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200173#define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
174 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
175 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
176 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
177 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800179#define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200180ssize_t batadv_store_##_name(struct kobject *kobj, \
181 struct attribute *attr, char *buff, \
182 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200184 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200185 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200186 \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200187 return __batadv_store_uint_attr(buff, count, _min, _max, \
188 _post_func, attr, \
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800189 &bat_priv->_var, net_dev); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190}
191
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800192#define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200193ssize_t batadv_show_##_name(struct kobject *kobj, \
194 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200196 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200197 \
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800198 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199} \
200
Marek Lindnerf245c382012-03-11 06:17:51 +0800201/* Use this, if you are going to set [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200202 * (bat_priv) to an unsigned integer value
203 */
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800204#define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
205 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
206 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200207 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
208 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209
Antonio Quartulli90f44352013-07-02 11:04:35 +0200210#define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
211ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
212 struct attribute *attr, char *buff, \
213 size_t count) \
214{ \
215 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
216 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
217 kobj); \
218 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
219 attr, &vlan->_name, \
220 bat_priv->soft_iface); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200221 \
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100222 batadv_softif_vlan_put(vlan); \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200223 return res; \
224}
225
226#define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
227ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
228 struct attribute *attr, char *buff) \
229{ \
230 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
231 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
232 kobj); \
233 size_t res = sprintf(buff, "%s\n", \
234 atomic_read(&vlan->_name) == 0 ? \
235 "disabled" : "enabled"); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200236 \
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100237 batadv_softif_vlan_put(vlan); \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200238 return res; \
239}
240
241/* Use this, if you are going to turn a [name] in the vlan struct on or off */
242#define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
243 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
244 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
245 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
246 batadv_store_vlan_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247
Linus Luessing0744ff82016-01-16 16:40:08 +0800248#define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
249ssize_t batadv_store_##_name(struct kobject *kobj, \
250 struct attribute *attr, char *buff, \
251 size_t count) \
252{ \
253 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
254 struct batadv_hard_iface *hard_iface; \
255 ssize_t length; \
256 \
257 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
258 if (!hard_iface) \
259 return 0; \
260 \
261 length = __batadv_store_uint_attr(buff, count, _min, _max, \
262 _post_func, attr, \
263 &hard_iface->_var, net_dev); \
264 \
265 batadv_hardif_put(hard_iface); \
266 return length; \
267}
268
269#define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
270ssize_t batadv_show_##_name(struct kobject *kobj, \
271 struct attribute *attr, char *buff) \
272{ \
273 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
274 struct batadv_hard_iface *hard_iface; \
275 ssize_t length; \
276 \
277 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
278 if (!hard_iface) \
279 return 0; \
280 \
281 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
282 \
283 batadv_hardif_put(hard_iface); \
284 return length; \
285}
286
287/* Use this, if you are going to set [name] in hard_iface to an
288 * unsigned integer value
289 */
290#define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
291 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
292 _max, _post_func) \
293 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
294 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
295 batadv_store_##_name)
296
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200297static int batadv_store_bool_attr(char *buff, size_t count,
298 struct net_device *net_dev,
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100299 const char *attr_name, atomic_t *attr,
300 bool *changed)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301{
302 int enabled = -1;
303
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100304 *changed = false;
305
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306 if (buff[count - 1] == '\n')
307 buff[count - 1] = '\0';
308
309 if ((strncmp(buff, "1", 2) == 0) ||
310 (strncmp(buff, "enable", 7) == 0) ||
311 (strncmp(buff, "enabled", 8) == 0))
312 enabled = 1;
313
314 if ((strncmp(buff, "0", 2) == 0) ||
315 (strncmp(buff, "disable", 8) == 0) ||
316 (strncmp(buff, "disabled", 9) == 0))
317 enabled = 0;
318
319 if (enabled < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200320 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
321 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322 return -EINVAL;
323 }
324
325 if (atomic_read(attr) == enabled)
326 return count;
327
Sven Eckelmann3e348192012-05-16 20:23:22 +0200328 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
329 atomic_read(attr) == 1 ? "enabled" : "disabled",
330 enabled == 1 ? "enabled" : "disabled");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100332 *changed = true;
333
Eric Dumazet95c96172012-04-15 05:58:06 +0000334 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335 return count;
336}
337
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200338static inline ssize_t
339__batadv_store_bool_attr(char *buff, size_t count,
340 void (*post_func)(struct net_device *),
341 struct attribute *attr,
342 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343{
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100344 bool changed;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345 int ret;
346
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200347 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100348 attr_store, &changed);
349 if (post_func && changed)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350 post_func(net_dev);
351
352 return ret;
353}
354
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200355static int batadv_store_uint_attr(const char *buff, size_t count,
356 struct net_device *net_dev,
357 const char *attr_name,
358 unsigned int min, unsigned int max,
359 atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360{
361 unsigned long uint_val;
362 int ret;
363
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200364 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000365 if (ret) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200366 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
367 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368 return -EINVAL;
369 }
370
371 if (uint_val < min) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200372 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
373 attr_name, uint_val, min);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374 return -EINVAL;
375 }
376
377 if (uint_val > max) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200378 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
379 attr_name, uint_val, max);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380 return -EINVAL;
381 }
382
383 if (atomic_read(attr) == uint_val)
384 return count;
385
Sven Eckelmann3e348192012-05-16 20:23:22 +0200386 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
387 attr_name, atomic_read(attr), uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388
389 atomic_set(attr, uint_val);
390 return count;
391}
392
Antonio Quartullic149ca72016-05-06 02:46:37 +0800393static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
394 int min, int max,
395 void (*post_func)(struct net_device *),
396 const struct attribute *attr,
397 atomic_t *attr_store,
398 struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399{
400 int ret;
401
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200402 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
403 attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404 if (post_func && ret)
405 post_func(net_dev);
406
407 return ret;
408}
409
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200410static ssize_t batadv_show_bat_algo(struct kobject *kobj,
411 struct attribute *attr, char *buff)
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800412{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200413 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Antonio Quartullif1386942014-05-10 18:56:37 +0200414
Antonio Quartulli29824a52016-05-25 23:27:31 +0800415 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800416}
417
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100418static void batadv_post_gw_reselect(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000419{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200420 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartullif1386942014-05-10 18:56:37 +0200421
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100422 batadv_gw_reselect(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423}
424
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200425static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
426 char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200428 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 int bytes_written;
430
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200431 /* GW mode is not available if the routing algorithm in use does not
432 * implement the GW API
433 */
434 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
435 !bat_priv->algo_ops->gw.is_eligible)
436 return -ENOENT;
437
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800438 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200439 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200440 bytes_written = sprintf(buff, "%s\n",
441 BATADV_GW_MODE_CLIENT_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200443 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200444 bytes_written = sprintf(buff, "%s\n",
445 BATADV_GW_MODE_SERVER_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446 break;
447 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200448 bytes_written = sprintf(buff, "%s\n",
449 BATADV_GW_MODE_OFF_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450 break;
451 }
452
453 return bytes_written;
454}
455
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200456static ssize_t batadv_store_gw_mode(struct kobject *kobj,
457 struct attribute *attr, char *buff,
458 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200460 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200461 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462 char *curr_gw_mode_str;
463 int gw_mode_tmp = -1;
464
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200465 /* toggling GW mode is allowed only if the routing algorithm in use
466 * provides the GW API
467 */
468 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
469 !bat_priv->algo_ops->gw.is_eligible)
470 return -EINVAL;
471
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472 if (buff[count - 1] == '\n')
473 buff[count - 1] = '\0';
474
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200475 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
476 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200477 gw_mode_tmp = BATADV_GW_MODE_OFF;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200479 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
480 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200481 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200483 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
484 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200485 gw_mode_tmp = BATADV_GW_MODE_SERVER;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486
487 if (gw_mode_tmp < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200488 batadv_info(net_dev,
489 "Invalid parameter for 'gw mode' setting received: %s\n",
490 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491 return -EINVAL;
492 }
493
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800494 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495 return count;
496
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800497 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200498 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200499 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200501 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200502 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503 break;
504 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200505 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506 break;
507 }
508
Sven Eckelmann3e348192012-05-16 20:23:22 +0200509 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
510 curr_gw_mode_str, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511
Antonio Quartullif3163182013-11-04 20:59:40 +0100512 /* Invoking batadv_gw_reselect() is not enough to really de-select the
513 * current GW. It will only instruct the gateway client code to perform
514 * a re-election the next time that this is needed.
515 *
516 * When gw client mode is being switched off the current GW must be
517 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
518 * client mode re-activation. This is operation is performed in
519 * batadv_gw_check_client_stop().
520 */
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100521 batadv_gw_reselect(bat_priv);
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200522 /* always call batadv_gw_check_client_stop() before changing the gateway
523 * state
524 */
525 batadv_gw_check_client_stop(bat_priv);
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800526 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
Marek Lindner414254e2013-04-23 21:39:58 +0800527 batadv_gw_tvlv_container_update(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528 return count;
529}
530
Antonio Quartulli08686942016-07-03 12:46:32 +0200531static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
532 struct attribute *attr, char *buff)
533{
534 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
535
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200536 /* GW selection class is not available if the routing algorithm in use
537 * does not implement the GW API
538 */
539 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
540 !bat_priv->algo_ops->gw.is_eligible)
541 return -ENOENT;
542
Antonio Quartulli08686942016-07-03 12:46:32 +0200543 if (bat_priv->algo_ops->gw.show_sel_class)
544 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
545
546 return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
547}
548
549static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
550 struct attribute *attr, char *buff,
551 size_t count)
552{
553 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
554
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200555 /* setting the GW selection class is allowed only if the routing
556 * algorithm in use implements the GW API
557 */
558 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
559 !bat_priv->algo_ops->gw.is_eligible)
560 return -EINVAL;
561
Antonio Quartulli08686942016-07-03 12:46:32 +0200562 if (buff[count - 1] == '\n')
563 buff[count - 1] = '\0';
564
565 if (bat_priv->algo_ops->gw.store_sel_class)
566 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
567 count);
568
569 return __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
570 batadv_post_gw_reselect, attr,
571 &bat_priv->gw.sel_class,
572 bat_priv->soft_iface);
573}
574
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200575static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
576 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000577{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200578 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200579 u32 down, up;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580
Marek Lindner414254e2013-04-23 21:39:58 +0800581 down = atomic_read(&bat_priv->gw.bandwidth_down);
582 up = atomic_read(&bat_priv->gw.bandwidth_up);
583
584 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
585 down % 10, up / 10, up % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586}
587
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200588static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
589 struct attribute *attr, char *buff,
590 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200592 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593
594 if (buff[count - 1] == '\n')
595 buff[count - 1] = '\0';
596
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200597 return batadv_gw_bandwidth_set(net_dev, buff, count);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598}
599
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100600/**
601 * batadv_show_isolation_mark - print the current isolation mark/mask
602 * @kobj: kobject representing the private mesh sysfs directory
603 * @attr: the batman-adv attribute the user is interacting with
604 * @buff: the buffer that will contain the data to send back to the user
605 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200606 * Return: the number of bytes written into 'buff' on success or a negative
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100607 * error code in case of failure
608 */
609static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
610 struct attribute *attr, char *buff)
611{
612 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
613
614 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
615 bat_priv->isolation_mark_mask);
616}
617
618/**
619 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
620 * by the user
621 * @kobj: kobject representing the private mesh sysfs directory
622 * @attr: the batman-adv attribute the user is interacting with
623 * @buff: the buffer containing the user data
624 * @count: number of bytes in the buffer
625 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200626 * Return: 'count' on success or a negative error code in case of failure
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100627 */
628static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
629 struct attribute *attr, char *buff,
630 size_t count)
631{
632 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
633 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200634 u32 mark, mask;
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100635 char *mask_ptr;
636
637 /* parse the mask if it has been specified, otherwise assume the mask is
638 * the biggest possible
639 */
640 mask = 0xFFFFFFFF;
641 mask_ptr = strchr(buff, '/');
642 if (mask_ptr) {
643 *mask_ptr = '\0';
644 mask_ptr++;
645
646 /* the mask must be entered in hex base as it is going to be a
647 * bitmask and not a prefix length
648 */
649 if (kstrtou32(mask_ptr, 16, &mask) < 0)
650 return -EINVAL;
651 }
652
653 /* the mark can be entered in any base */
654 if (kstrtou32(buff, 0, &mark) < 0)
655 return -EINVAL;
656
657 bat_priv->isolation_mark_mask = mask;
658 /* erase bits not covered by the mask */
659 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
660
661 batadv_info(net_dev,
662 "New skb mark for extended isolation: %#.8x/%#.8x\n",
663 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
664
665 return count;
666}
667
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200668BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
669BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100670#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200671BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100672#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200673#ifdef CONFIG_BATMAN_ADV_DAT
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200674BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200675#endif
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200676BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
677static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
678static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
679BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
680 INT_MAX, NULL);
681BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
682 NULL);
683static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
Antonio Quartulli08686942016-07-03 12:46:32 +0200684 batadv_store_gw_sel_class);
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200685static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200686 batadv_store_gw_bwidth);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100687#ifdef CONFIG_BATMAN_ADV_MCAST
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200688BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100689#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000690#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200691BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100693#ifdef CONFIG_BATMAN_ADV_NC
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200694BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100695#endif
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200696static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
697 batadv_store_isolation_mark);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200699static struct batadv_attribute *batadv_mesh_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200700 &batadv_attr_aggregated_ogms,
701 &batadv_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100702#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200703 &batadv_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100704#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200705#ifdef CONFIG_BATMAN_ADV_DAT
706 &batadv_attr_distributed_arp_table,
707#endif
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100708#ifdef CONFIG_BATMAN_ADV_MCAST
709 &batadv_attr_multicast_mode,
710#endif
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200711 &batadv_attr_fragmentation,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200712 &batadv_attr_routing_algo,
713 &batadv_attr_gw_mode,
714 &batadv_attr_orig_interval,
715 &batadv_attr_hop_penalty,
716 &batadv_attr_gw_sel_class,
717 &batadv_attr_gw_bandwidth,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200719 &batadv_attr_log_level,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100721#ifdef CONFIG_BATMAN_ADV_NC
722 &batadv_attr_network_coding,
723#endif
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100724 &batadv_attr_isolation_mark,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725 NULL,
726};
727
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200728BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200729
Sven Eckelmannf34ac9d2015-09-06 21:38:53 +0200730/* array of vlan specific sysfs attributes */
Antonio Quartulli90f44352013-07-02 11:04:35 +0200731static struct batadv_attribute *batadv_vlan_attrs[] = {
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200732 &batadv_attr_vlan_ap_isolation,
Antonio Quartulli90f44352013-07-02 11:04:35 +0200733 NULL,
734};
735
Sven Eckelmann5853e222012-05-12 02:09:24 +0200736int batadv_sysfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000737{
738 struct kobject *batif_kobject = &dev->dev.kobj;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200739 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200740 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741 int err;
742
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200743 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744 batif_kobject);
745 if (!bat_priv->mesh_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200746 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200747 BATADV_SYSFS_IF_MESH_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000748 goto out;
749 }
750
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200751 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752 err = sysfs_create_file(bat_priv->mesh_obj,
753 &((*bat_attr)->attr));
754 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200755 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200756 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +0200757 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000758 goto rem_attr;
759 }
760 }
761
762 return 0;
763
764rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200765 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000766 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
767
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200768 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
769 kobject_del(bat_priv->mesh_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000770 kobject_put(bat_priv->mesh_obj);
771 bat_priv->mesh_obj = NULL;
772out:
773 return -ENOMEM;
774}
775
Sven Eckelmann5853e222012-05-12 02:09:24 +0200776void batadv_sysfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000777{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200778 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200779 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200781 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
783
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200784 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
785 kobject_del(bat_priv->mesh_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000786 kobject_put(bat_priv->mesh_obj);
787 bat_priv->mesh_obj = NULL;
788}
789
Antonio Quartulli90f44352013-07-02 11:04:35 +0200790/**
791 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
792 * @dev: netdev of the mesh interface
793 * @vlan: private data of the newly added VLAN interface
794 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200795 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
Antonio Quartulli90f44352013-07-02 11:04:35 +0200796 */
797int batadv_sysfs_add_vlan(struct net_device *dev,
798 struct batadv_softif_vlan *vlan)
799{
800 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
801 struct batadv_priv *bat_priv = netdev_priv(dev);
802 struct batadv_attribute **bat_attr;
803 int err;
804
805 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
806 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
807 vlan->vid & VLAN_VID_MASK);
808
809 vlan->kobj = kobject_create_and_add(vlan_subdir,
810 bat_priv->mesh_obj);
811 if (!vlan->kobj) {
812 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
813 dev->name, vlan_subdir);
814 goto out;
815 }
816 } else {
817 /* the untagged LAN uses the root folder to store its "VLAN
818 * specific attributes"
819 */
820 vlan->kobj = bat_priv->mesh_obj;
821 kobject_get(bat_priv->mesh_obj);
822 }
823
824 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
825 err = sysfs_create_file(vlan->kobj,
826 &((*bat_attr)->attr));
827 if (err) {
828 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
829 dev->name, vlan_subdir,
830 ((*bat_attr)->attr).name);
831 goto rem_attr;
832 }
833 }
834
835 return 0;
836
837rem_attr:
838 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
839 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
840
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200841 if (vlan->kobj != bat_priv->mesh_obj) {
842 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
843 kobject_del(vlan->kobj);
844 }
Antonio Quartulli90f44352013-07-02 11:04:35 +0200845 kobject_put(vlan->kobj);
846 vlan->kobj = NULL;
847out:
848 return -ENOMEM;
849}
850
851/**
852 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
853 * @bat_priv: the bat priv with all the soft interface information
854 * @vlan: the private data of the VLAN to destroy
855 */
856void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
857 struct batadv_softif_vlan *vlan)
858{
859 struct batadv_attribute **bat_attr;
860
861 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
862 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
863
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200864 if (vlan->kobj != bat_priv->mesh_obj) {
865 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
866 kobject_del(vlan->kobj);
867 }
Antonio Quartulli90f44352013-07-02 11:04:35 +0200868 kobject_put(vlan->kobj);
869 vlan->kobj = NULL;
870}
871
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200872static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
873 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200875 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200876 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000877 ssize_t length;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200878 const char *ifname;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879
Sven Eckelmann56303d32012-06-05 22:31:31 +0200880 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000881 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000882 return 0;
883
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200884 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
885 ifname = "none";
886 else
887 ifname = hard_iface->soft_iface->name;
888
889 length = sprintf(buff, "%s\n", ifname);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100891 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892
893 return length;
894}
895
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200896/**
897 * batadv_store_mesh_iface_finish - store new hardif mesh_iface state
898 * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
899 * @ifname: name of soft-interface to modify
900 *
901 * Changes the parts of the hard+soft interface which can not be modified under
902 * sysfs lock (to prevent deadlock situations).
903 *
904 * Return: 0 on success, 0 < on failure
905 */
906static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
907 char ifname[IFNAMSIZ])
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908{
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200909 struct net *net = dev_net(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200910 struct batadv_hard_iface *hard_iface;
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200911 int status_tmp;
912 int ret = 0;
913
914 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915
Sven Eckelmann56303d32012-06-05 22:31:31 +0200916 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000917 if (!hard_iface)
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200918 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000919
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200920 if (strncmp(ifname, "none", 4) == 0)
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200921 status_tmp = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000922 else
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200923 status_tmp = BATADV_IF_I_WANT_YOU;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000924
Marek Lindnere6c10f42011-02-18 12:33:20 +0000925 if (hard_iface->if_status == status_tmp)
926 goto out;
927
928 if ((hard_iface->soft_iface) &&
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200929 (strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000930 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200932 if (status_tmp == BATADV_IF_NOT_IN_USE) {
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800933 batadv_hardif_disable_interface(hard_iface,
934 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200935 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000936 }
937
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200938 /* if the interface already is in use */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200939 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800940 batadv_hardif_disable_interface(hard_iface,
941 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200943 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000944out:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100945 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000946 return ret;
947}
948
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200949/**
950 * batadv_store_mesh_iface_work - store new hardif mesh_iface state
951 * @work: work queue item
952 *
953 * Changes the parts of the hard+soft interface which can not be modified under
954 * sysfs lock (to prevent deadlock situations).
955 */
956static void batadv_store_mesh_iface_work(struct work_struct *work)
957{
958 struct batadv_store_mesh_work *store_work;
959 int ret;
960
961 store_work = container_of(work, struct batadv_store_mesh_work, work);
962
963 rtnl_lock();
964 ret = batadv_store_mesh_iface_finish(store_work->net_dev,
965 store_work->soft_iface_name);
966 rtnl_unlock();
967
968 if (ret < 0)
969 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
970 store_work->soft_iface_name, store_work->net_dev->name,
971 ret);
972
973 dev_put(store_work->net_dev);
974 kfree(store_work);
975}
976
977static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
978 struct attribute *attr, char *buff,
979 size_t count)
980{
981 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
982 struct batadv_store_mesh_work *store_work;
983
984 if (buff[count - 1] == '\n')
985 buff[count - 1] = '\0';
986
987 if (strlen(buff) >= IFNAMSIZ) {
988 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
989 buff);
990 return -EINVAL;
991 }
992
993 store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
994 if (!store_work)
995 return -ENOMEM;
996
997 dev_hold(net_dev);
998 INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
999 store_work->net_dev = net_dev;
1000 strlcpy(store_work->soft_iface_name, buff,
1001 sizeof(store_work->soft_iface_name));
1002
1003 queue_work(batadv_event_workqueue, &store_work->work);
1004
1005 return count;
1006}
1007
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001008static ssize_t batadv_show_iface_status(struct kobject *kobj,
1009 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001010{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001011 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001012 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001013 ssize_t length;
1014
Sven Eckelmann56303d32012-06-05 22:31:31 +02001015 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +00001016 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017 return 0;
1018
Marek Lindnere6c10f42011-02-18 12:33:20 +00001019 switch (hard_iface->if_status) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001020 case BATADV_IF_TO_BE_REMOVED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001021 length = sprintf(buff, "disabling\n");
1022 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001023 case BATADV_IF_INACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024 length = sprintf(buff, "inactive\n");
1025 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001026 case BATADV_IF_ACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001027 length = sprintf(buff, "active\n");
1028 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001029 case BATADV_IF_TO_BE_ACTIVATED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030 length = sprintf(buff, "enabling\n");
1031 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001032 case BATADV_IF_NOT_IN_USE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001033 default:
1034 length = sprintf(buff, "not in use\n");
1035 break;
1036 }
1037
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001038 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001039
1040 return length;
1041}
1042
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001043#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1044
1045/**
1046 * batadv_store_throughput_override - parse and store throughput override
1047 * entered by the user
1048 * @kobj: kobject representing the private mesh sysfs directory
1049 * @attr: the batman-adv attribute the user is interacting with
1050 * @buff: the buffer containing the user data
1051 * @count: number of bytes in the buffer
1052 *
1053 * Return: 'count' on success or a negative error code in case of failure
1054 */
1055static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1056 struct attribute *attr,
1057 char *buff, size_t count)
1058{
1059 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1060 struct batadv_hard_iface *hard_iface;
1061 u32 tp_override;
1062 u32 old_tp_override;
1063 bool ret;
1064
1065 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1066 if (!hard_iface)
1067 return -EINVAL;
1068
1069 if (buff[count - 1] == '\n')
1070 buff[count - 1] = '\0';
1071
1072 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1073 &tp_override);
1074 if (!ret)
1075 return count;
1076
1077 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1078 if (old_tp_override == tp_override)
1079 goto out;
1080
1081 batadv_info(net_dev, "%s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1082 "throughput_override",
1083 old_tp_override / 10, old_tp_override % 10,
1084 tp_override / 10, tp_override % 10);
1085
1086 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1087
1088out:
1089 batadv_hardif_put(hard_iface);
1090 return count;
1091}
1092
1093static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1094 struct attribute *attr,
1095 char *buff)
1096{
1097 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1098 struct batadv_hard_iface *hard_iface;
1099 u32 tp_override;
1100
1101 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1102 if (!hard_iface)
1103 return -EINVAL;
1104
1105 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1106
1107 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1108 tp_override % 10);
1109}
1110
1111#endif
1112
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001113static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
Sven Eckelmann347c80f2012-06-03 22:19:07 +02001114 batadv_store_mesh_iface);
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001115static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
Linus Luessing7f136cd2016-01-16 16:40:11 +08001116#ifdef CONFIG_BATMAN_ADV_BATMAN_V
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001117BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001118 2 * BATADV_JITTER, INT_MAX, NULL);
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001119static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001120 batadv_store_throughput_override);
Linus Luessing7f136cd2016-01-16 16:40:11 +08001121#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001122
Sven Eckelmannb4d66b82012-06-05 22:31:29 +02001123static struct batadv_attribute *batadv_batman_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001124 &batadv_attr_mesh_iface,
1125 &batadv_attr_iface_status,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001126#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1127 &batadv_attr_elp_interval,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001128 &batadv_attr_throughput_override,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001129#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001130 NULL,
1131};
1132
Sven Eckelmann5853e222012-05-12 02:09:24 +02001133int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001134{
1135 struct kobject *hardif_kobject = &dev->dev.kobj;
Sven Eckelmannb4d66b82012-06-05 22:31:29 +02001136 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001137 int err;
1138
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001139 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1140 hardif_kobject);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001141
1142 if (!*hardif_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +02001143 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001144 BATADV_SYSFS_IF_BAT_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001145 goto out;
1146 }
1147
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001148 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001149 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1150 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +02001151 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001152 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +02001153 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001154 goto rem_attr;
1155 }
1156 }
1157
1158 return 0;
1159
1160rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001161 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001162 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1163out:
1164 return -ENOMEM;
1165}
1166
Sven Eckelmann5853e222012-05-12 02:09:24 +02001167void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001168{
Sven Eckelmannf4acb102016-06-13 07:41:32 +02001169 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1170 kobject_del(*hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001171 kobject_put(*hardif_obj);
1172 *hardif_obj = NULL;
1173}
Antonio Quartullic6bda682011-04-26 18:26:01 +02001174
Sven Eckelmann56303d32012-06-05 22:31:31 +02001175int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001176 enum batadv_uev_action action, const char *data)
Antonio Quartullic6bda682011-04-26 18:26:01 +02001177{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001178 int ret = -ENOMEM;
Antonio Quartullic6bda682011-04-26 18:26:01 +02001179 struct kobject *bat_kobj;
1180 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1181
Marek Lindner736292c2013-01-12 19:19:06 +08001182 bat_kobj = &bat_priv->soft_iface->dev.kobj;
Antonio Quartullic6bda682011-04-26 18:26:01 +02001183
Himangi Saraogib98fe242014-06-29 00:06:29 +05301184 uevent_env[0] = kasprintf(GFP_ATOMIC,
1185 "%s%s", BATADV_UEV_TYPE_VAR,
1186 batadv_uev_type_str[type]);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001187 if (!uevent_env[0])
1188 goto out;
1189
Himangi Saraogib98fe242014-06-29 00:06:29 +05301190 uevent_env[1] = kasprintf(GFP_ATOMIC,
1191 "%s%s", BATADV_UEV_ACTION_VAR,
1192 batadv_uev_action_str[action]);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001193 if (!uevent_env[1])
1194 goto out;
1195
Antonio Quartullic6bda682011-04-26 18:26:01 +02001196 /* If the event is DEL, ignore the data field */
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001197 if (action != BATADV_UEV_DEL) {
Himangi Saraogib98fe242014-06-29 00:06:29 +05301198 uevent_env[2] = kasprintf(GFP_ATOMIC,
1199 "%s%s", BATADV_UEV_DATA_VAR, data);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001200 if (!uevent_env[2])
1201 goto out;
Antonio Quartullic6bda682011-04-26 18:26:01 +02001202 }
1203
1204 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1205out:
1206 kfree(uevent_env[0]);
1207 kfree(uevent_env[1]);
1208 kfree(uevent_env[2]);
1209
Antonio Quartullic6bda682011-04-26 18:26:01 +02001210 if (ret)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001211 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001212 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001213 batadv_uev_type_str[type],
1214 batadv_uev_action_str[action],
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001215 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001216 return ret;
1217}