blob: 0b4b3fb778a61708978438bcaf7adfd05b997668 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann7a79d712018-12-31 23:59:59 +01002/* Copyright (C) 2010-2019 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 Eckelmanne2d0d352018-11-23 13:15:00 +010043#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020044
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020045#include "bridge_loop_avoidance.h"
Antonio Quartulli33af49a2012-08-08 18:50:57 +020046#include "distributed-arp-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020048#include "gateway_common.h"
49#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020050#include "log.h"
Sven Eckelmann7e6f4612018-11-23 16:07:12 +010051#include "netlink.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020052#include "network-coding.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020053#include "soft-interface.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020055static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020056{
57 struct device *dev = container_of(obj->parent, struct device, kobj);
Antonio Quartullif1386942014-05-10 18:56:37 +020058
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020059 return to_net_dev(dev);
60}
61
Sven Eckelmann56303d32012-06-05 22:31:31 +020062static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020063{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020064 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
Antonio Quartullif1386942014-05-10 18:56:37 +020065
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020066 return netdev_priv(net_dev);
67}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000068
Antonio Quartulli90f44352013-07-02 11:04:35 +020069/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010070 * batadv_vlan_kobj_to_batpriv() - convert a vlan kobj in the associated batpriv
Antonio Quartulli90f44352013-07-02 11:04:35 +020071 * @obj: kobject to covert
72 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020073 * Return: the associated batadv_priv struct.
Antonio Quartulli90f44352013-07-02 11:04:35 +020074 */
75static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
76{
77 /* VLAN specific attributes are located in the root sysfs folder if they
78 * refer to the untagged VLAN..
79 */
80 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
81 return batadv_kobj_to_batpriv(obj);
82
83 /* ..while the attributes for the tagged vlans are located in
84 * the in the corresponding "vlan%VID" subfolder
85 */
86 return batadv_kobj_to_batpriv(obj->parent);
87}
88
89/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010090 * batadv_kobj_to_vlan() - convert a kobj in the associated softif_vlan struct
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010091 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli90f44352013-07-02 11:04:35 +020092 * @obj: kobject to covert
93 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020094 * Return: the associated softif_vlan struct if found, NULL otherwise.
Antonio Quartulli90f44352013-07-02 11:04:35 +020095 */
96static struct batadv_softif_vlan *
97batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
98{
99 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
100
101 rcu_read_lock();
102 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
103 if (vlan_tmp->kobj != obj)
104 continue;
105
Sven Eckelmann6be4d302016-01-16 10:29:42 +0100106 if (!kref_get_unless_zero(&vlan_tmp->refcount))
Antonio Quartulli90f44352013-07-02 11:04:35 +0200107 continue;
108
109 vlan = vlan_tmp;
110 break;
111 }
112 rcu_read_unlock();
113
114 return vlan;
115}
116
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200117#define BATADV_UEV_TYPE_VAR "BATTYPE="
118#define BATADV_UEV_ACTION_VAR "BATACTION="
119#define BATADV_UEV_DATA_VAR "BATDATA="
Antonio Quartullic6bda682011-04-26 18:26:01 +0200120
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200121static char *batadv_uev_action_str[] = {
Antonio Quartullic6bda682011-04-26 18:26:01 +0200122 "add",
123 "del",
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100124 "change",
125 "loopdetect",
Antonio Quartullic6bda682011-04-26 18:26:01 +0200126};
127
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200128static char *batadv_uev_type_str[] = {
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100129 "gw",
130 "bla",
Antonio Quartullic6bda682011-04-26 18:26:01 +0200131};
132
Antonio Quartulli90f44352013-07-02 11:04:35 +0200133/* Use this, if you have customized show and store functions for vlan attrs */
134#define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
135struct batadv_attribute batadv_attr_vlan_##_name = { \
136 .attr = {.name = __stringify(_name), \
137 .mode = _mode }, \
138 .show = _show, \
139 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200140}
Antonio Quartulli90f44352013-07-02 11:04:35 +0200141
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000142/* Use this, if you have customized show and store functions */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200143#define BATADV_ATTR(_name, _mode, _show, _store) \
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200144struct batadv_attribute batadv_attr_##_name = { \
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200145 .attr = {.name = __stringify(_name), \
146 .mode = _mode }, \
147 .show = _show, \
148 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200149}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200151#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200152ssize_t batadv_store_##_name(struct kobject *kobj, \
153 struct attribute *attr, char *buff, \
154 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200156 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200157 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100158 ssize_t length; \
Antonio Quartullif1386942014-05-10 18:56:37 +0200159 \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100160 length = __batadv_store_bool_attr(buff, count, _post_func, attr,\
161 &bat_priv->_name, net_dev); \
162 \
163 batadv_netlink_notify_mesh(bat_priv); \
164 \
165 return length; \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166}
167
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200168#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200169ssize_t batadv_show_##_name(struct kobject *kobj, \
170 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000171{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200172 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200173 \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174 return sprintf(buff, "%s\n", \
175 atomic_read(&bat_priv->_name) == 0 ? \
176 "disabled" : "enabled"); \
177} \
178
Marek Lindnerf245c382012-03-11 06:17:51 +0800179/* Use this, if you are going to turn a [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200180 * (bat_priv) on or off
181 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200182#define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
183 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
184 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
185 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
186 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800188#define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200189ssize_t batadv_store_##_name(struct kobject *kobj, \
190 struct attribute *attr, char *buff, \
191 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200193 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200194 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100195 ssize_t length; \
Antonio Quartullif1386942014-05-10 18:56:37 +0200196 \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100197 length = __batadv_store_uint_attr(buff, count, _min, _max, \
198 _post_func, attr, \
199 &bat_priv->_var, net_dev, \
200 NULL); \
201 \
202 batadv_netlink_notify_mesh(bat_priv); \
203 \
204 return length; \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205}
206
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800207#define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200208ssize_t batadv_show_##_name(struct kobject *kobj, \
209 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200211 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200212 \
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800213 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214} \
215
Marek Lindnerf245c382012-03-11 06:17:51 +0800216/* Use this, if you are going to set [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200217 * (bat_priv) to an unsigned integer value
218 */
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800219#define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
220 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
221 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200222 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
223 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224
Antonio Quartulli90f44352013-07-02 11:04:35 +0200225#define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
226ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
227 struct attribute *attr, char *buff, \
228 size_t count) \
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 = __batadv_store_bool_attr(buff, count, _post_func, \
234 attr, &vlan->_name, \
235 bat_priv->soft_iface); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200236 \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100237 if (vlan->vid) \
238 batadv_netlink_notify_vlan(bat_priv, vlan); \
239 else \
240 batadv_netlink_notify_mesh(bat_priv); \
241 \
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100242 batadv_softif_vlan_put(vlan); \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200243 return res; \
244}
245
246#define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
247ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
248 struct attribute *attr, char *buff) \
249{ \
250 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
251 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
252 kobj); \
253 size_t res = sprintf(buff, "%s\n", \
254 atomic_read(&vlan->_name) == 0 ? \
255 "disabled" : "enabled"); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200256 \
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100257 batadv_softif_vlan_put(vlan); \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200258 return res; \
259}
260
261/* Use this, if you are going to turn a [name] in the vlan struct on or off */
262#define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
263 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
264 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
265 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
266 batadv_store_vlan_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
Linus Luessing0744ff82016-01-16 16:40:08 +0800268#define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
269ssize_t batadv_store_##_name(struct kobject *kobj, \
270 struct attribute *attr, char *buff, \
271 size_t count) \
272{ \
273 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
274 struct batadv_hard_iface *hard_iface; \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100275 struct batadv_priv *bat_priv; \
Linus Luessing0744ff82016-01-16 16:40:08 +0800276 ssize_t length; \
277 \
278 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
279 if (!hard_iface) \
280 return 0; \
281 \
282 length = __batadv_store_uint_attr(buff, count, _min, _max, \
283 _post_func, attr, \
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200284 &hard_iface->_var, \
285 hard_iface->soft_iface, \
286 net_dev); \
Linus Luessing0744ff82016-01-16 16:40:08 +0800287 \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100288 if (hard_iface->soft_iface) { \
289 bat_priv = netdev_priv(hard_iface->soft_iface); \
290 batadv_netlink_notify_hardif(bat_priv, hard_iface); \
291 } \
292 \
Linus Luessing0744ff82016-01-16 16:40:08 +0800293 batadv_hardif_put(hard_iface); \
294 return length; \
295}
296
297#define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
298ssize_t batadv_show_##_name(struct kobject *kobj, \
299 struct attribute *attr, char *buff) \
300{ \
301 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
302 struct batadv_hard_iface *hard_iface; \
303 ssize_t length; \
304 \
305 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
306 if (!hard_iface) \
307 return 0; \
308 \
309 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
310 \
311 batadv_hardif_put(hard_iface); \
312 return length; \
313}
314
315/* Use this, if you are going to set [name] in hard_iface to an
316 * unsigned integer value
317 */
318#define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
319 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
320 _max, _post_func) \
321 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
322 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
323 batadv_store_##_name)
324
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200325static int batadv_store_bool_attr(char *buff, size_t count,
326 struct net_device *net_dev,
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100327 const char *attr_name, atomic_t *attr,
328 bool *changed)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329{
330 int enabled = -1;
331
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100332 *changed = false;
333
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 if (buff[count - 1] == '\n')
335 buff[count - 1] = '\0';
336
337 if ((strncmp(buff, "1", 2) == 0) ||
338 (strncmp(buff, "enable", 7) == 0) ||
339 (strncmp(buff, "enabled", 8) == 0))
340 enabled = 1;
341
342 if ((strncmp(buff, "0", 2) == 0) ||
343 (strncmp(buff, "disable", 8) == 0) ||
344 (strncmp(buff, "disabled", 9) == 0))
345 enabled = 0;
346
347 if (enabled < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200348 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
349 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350 return -EINVAL;
351 }
352
353 if (atomic_read(attr) == enabled)
354 return count;
355
Sven Eckelmann3e348192012-05-16 20:23:22 +0200356 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
357 atomic_read(attr) == 1 ? "enabled" : "disabled",
358 enabled == 1 ? "enabled" : "disabled");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100360 *changed = true;
361
Eric Dumazet95c96172012-04-15 05:58:06 +0000362 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363 return count;
364}
365
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200366static inline ssize_t
367__batadv_store_bool_attr(char *buff, size_t count,
368 void (*post_func)(struct net_device *),
369 struct attribute *attr,
370 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371{
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100372 bool changed;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373 int ret;
374
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200375 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100376 attr_store, &changed);
377 if (post_func && changed)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378 post_func(net_dev);
379
380 return ret;
381}
382
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200383static int batadv_store_uint_attr(const char *buff, size_t count,
384 struct net_device *net_dev,
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200385 struct net_device *slave_dev,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200386 const char *attr_name,
387 unsigned int min, unsigned int max,
388 atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389{
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200390 char ifname[IFNAMSIZ + 3] = "";
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391 unsigned long uint_val;
392 int ret;
393
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200394 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395 if (ret) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200396 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
397 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398 return -EINVAL;
399 }
400
401 if (uint_val < min) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200402 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
403 attr_name, uint_val, min);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404 return -EINVAL;
405 }
406
407 if (uint_val > max) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200408 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
409 attr_name, uint_val, max);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410 return -EINVAL;
411 }
412
413 if (atomic_read(attr) == uint_val)
414 return count;
415
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200416 if (slave_dev)
417 snprintf(ifname, sizeof(ifname), "%s: ", slave_dev->name);
418
419 batadv_info(net_dev, "%s: %sChanging from: %i to: %lu\n",
420 attr_name, ifname, atomic_read(attr), uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421
422 atomic_set(attr, uint_val);
423 return count;
424}
425
Antonio Quartullic149ca72016-05-06 02:46:37 +0800426static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
427 int min, int max,
428 void (*post_func)(struct net_device *),
429 const struct attribute *attr,
430 atomic_t *attr_store,
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200431 struct net_device *net_dev,
432 struct net_device *slave_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433{
434 int ret;
435
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200436 ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
437 attr->name, min, max, attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438 if (post_func && ret)
439 post_func(net_dev);
440
441 return ret;
442}
443
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200444static ssize_t batadv_show_bat_algo(struct kobject *kobj,
445 struct attribute *attr, char *buff)
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800446{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200447 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Antonio Quartullif1386942014-05-10 18:56:37 +0200448
Antonio Quartulli29824a52016-05-25 23:27:31 +0800449 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800450}
451
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100452static void batadv_post_gw_reselect(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200454 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartullif1386942014-05-10 18:56:37 +0200455
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100456 batadv_gw_reselect(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457}
458
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200459static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
460 char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200462 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463 int bytes_written;
464
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200465 /* GW mode is not available if the routing algorithm in use does not
466 * implement 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 -ENOENT;
471
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800472 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200473 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200474 bytes_written = sprintf(buff, "%s\n",
475 BATADV_GW_MODE_CLIENT_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200477 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200478 bytes_written = sprintf(buff, "%s\n",
479 BATADV_GW_MODE_SERVER_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480 break;
481 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200482 bytes_written = sprintf(buff, "%s\n",
483 BATADV_GW_MODE_OFF_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484 break;
485 }
486
487 return bytes_written;
488}
489
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200490static ssize_t batadv_store_gw_mode(struct kobject *kobj,
491 struct attribute *attr, char *buff,
492 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200494 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200495 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496 char *curr_gw_mode_str;
497 int gw_mode_tmp = -1;
498
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200499 /* toggling GW mode is allowed only if the routing algorithm in use
500 * provides the GW API
501 */
502 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
503 !bat_priv->algo_ops->gw.is_eligible)
504 return -EINVAL;
505
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506 if (buff[count - 1] == '\n')
507 buff[count - 1] = '\0';
508
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200509 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
510 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200511 gw_mode_tmp = BATADV_GW_MODE_OFF;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200513 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
514 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200515 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000516
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200517 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
518 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200519 gw_mode_tmp = BATADV_GW_MODE_SERVER;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520
521 if (gw_mode_tmp < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200522 batadv_info(net_dev,
523 "Invalid parameter for 'gw mode' setting received: %s\n",
524 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525 return -EINVAL;
526 }
527
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800528 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529 return count;
530
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800531 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200532 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200533 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200535 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200536 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537 break;
538 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200539 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000540 break;
541 }
542
Sven Eckelmann3e348192012-05-16 20:23:22 +0200543 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
544 curr_gw_mode_str, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545
Antonio Quartullif3163182013-11-04 20:59:40 +0100546 /* Invoking batadv_gw_reselect() is not enough to really de-select the
547 * current GW. It will only instruct the gateway client code to perform
548 * a re-election the next time that this is needed.
549 *
550 * When gw client mode is being switched off the current GW must be
551 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
552 * client mode re-activation. This is operation is performed in
553 * batadv_gw_check_client_stop().
554 */
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100555 batadv_gw_reselect(bat_priv);
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200556 /* always call batadv_gw_check_client_stop() before changing the gateway
557 * state
558 */
559 batadv_gw_check_client_stop(bat_priv);
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800560 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
Marek Lindner414254e2013-04-23 21:39:58 +0800561 batadv_gw_tvlv_container_update(bat_priv);
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100562
563 batadv_netlink_notify_mesh(bat_priv);
564
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565 return count;
566}
567
Antonio Quartulli08686942016-07-03 12:46:32 +0200568static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
569 struct attribute *attr, char *buff)
570{
571 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
572
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200573 /* GW selection class is not available if the routing algorithm in use
574 * does not implement the GW API
575 */
576 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
577 !bat_priv->algo_ops->gw.is_eligible)
578 return -ENOENT;
579
Antonio Quartulli08686942016-07-03 12:46:32 +0200580 if (bat_priv->algo_ops->gw.show_sel_class)
581 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
582
583 return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
584}
585
586static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
587 struct attribute *attr, char *buff,
588 size_t count)
589{
590 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100591 ssize_t length;
Antonio Quartulli08686942016-07-03 12:46:32 +0200592
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200593 /* setting the GW selection class is allowed only if the routing
594 * algorithm in use implements the GW API
595 */
596 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
597 !bat_priv->algo_ops->gw.is_eligible)
598 return -EINVAL;
599
Antonio Quartulli08686942016-07-03 12:46:32 +0200600 if (buff[count - 1] == '\n')
601 buff[count - 1] = '\0';
602
603 if (bat_priv->algo_ops->gw.store_sel_class)
604 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
605 count);
606
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100607 length = __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
608 batadv_post_gw_reselect, attr,
609 &bat_priv->gw.sel_class,
610 bat_priv->soft_iface, NULL);
611
612 batadv_netlink_notify_mesh(bat_priv);
613
614 return length;
Antonio Quartulli08686942016-07-03 12:46:32 +0200615}
616
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200617static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
618 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200620 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200621 u32 down, up;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622
Marek Lindner414254e2013-04-23 21:39:58 +0800623 down = atomic_read(&bat_priv->gw.bandwidth_down);
624 up = atomic_read(&bat_priv->gw.bandwidth_up);
625
626 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
627 down % 10, up / 10, up % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628}
629
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200630static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
631 struct attribute *attr, char *buff,
632 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633{
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100634 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200635 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100636 ssize_t length;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637
638 if (buff[count - 1] == '\n')
639 buff[count - 1] = '\0';
640
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100641 length = batadv_gw_bandwidth_set(net_dev, buff, count);
642
643 batadv_netlink_notify_mesh(bat_priv);
644
645 return length;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000646}
647
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100648/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100649 * batadv_show_isolation_mark() - print the current isolation mark/mask
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100650 * @kobj: kobject representing the private mesh sysfs directory
651 * @attr: the batman-adv attribute the user is interacting with
652 * @buff: the buffer that will contain the data to send back to the user
653 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200654 * Return: the number of bytes written into 'buff' on success or a negative
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100655 * error code in case of failure
656 */
657static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
658 struct attribute *attr, char *buff)
659{
660 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
661
662 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
663 bat_priv->isolation_mark_mask);
664}
665
666/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100667 * batadv_store_isolation_mark() - parse and store the isolation mark/mask
668 * entered by the user
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100669 * @kobj: kobject representing the private mesh sysfs directory
670 * @attr: the batman-adv attribute the user is interacting with
671 * @buff: the buffer containing the user data
672 * @count: number of bytes in the buffer
673 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200674 * Return: 'count' on success or a negative error code in case of failure
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100675 */
676static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
677 struct attribute *attr, char *buff,
678 size_t count)
679{
680 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
681 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200682 u32 mark, mask;
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100683 char *mask_ptr;
684
685 /* parse the mask if it has been specified, otherwise assume the mask is
686 * the biggest possible
687 */
688 mask = 0xFFFFFFFF;
689 mask_ptr = strchr(buff, '/');
690 if (mask_ptr) {
691 *mask_ptr = '\0';
692 mask_ptr++;
693
694 /* the mask must be entered in hex base as it is going to be a
695 * bitmask and not a prefix length
696 */
697 if (kstrtou32(mask_ptr, 16, &mask) < 0)
698 return -EINVAL;
699 }
700
701 /* the mark can be entered in any base */
702 if (kstrtou32(buff, 0, &mark) < 0)
703 return -EINVAL;
704
705 bat_priv->isolation_mark_mask = mask;
706 /* erase bits not covered by the mask */
707 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
708
709 batadv_info(net_dev,
710 "New skb mark for extended isolation: %#.8x/%#.8x\n",
711 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
712
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100713 batadv_netlink_notify_mesh(bat_priv);
714
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100715 return count;
716}
717
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200718BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
719BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100720#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200721BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100722#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200723#ifdef CONFIG_BATMAN_ADV_DAT
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200724BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200725#endif
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200726BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
727static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
728static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
729BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
730 INT_MAX, NULL);
731BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
732 NULL);
733static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
Antonio Quartulli08686942016-07-03 12:46:32 +0200734 batadv_store_gw_sel_class);
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200735static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200736 batadv_store_gw_bwidth);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100737#ifdef CONFIG_BATMAN_ADV_MCAST
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200738BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100739#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200741BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000742#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100743#ifdef CONFIG_BATMAN_ADV_NC
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200744BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100745#endif
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200746static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
747 batadv_store_isolation_mark);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000748
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200749static struct batadv_attribute *batadv_mesh_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200750 &batadv_attr_aggregated_ogms,
751 &batadv_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100752#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200753 &batadv_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100754#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200755#ifdef CONFIG_BATMAN_ADV_DAT
756 &batadv_attr_distributed_arp_table,
757#endif
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100758#ifdef CONFIG_BATMAN_ADV_MCAST
759 &batadv_attr_multicast_mode,
760#endif
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200761 &batadv_attr_fragmentation,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200762 &batadv_attr_routing_algo,
763 &batadv_attr_gw_mode,
764 &batadv_attr_orig_interval,
765 &batadv_attr_hop_penalty,
766 &batadv_attr_gw_sel_class,
767 &batadv_attr_gw_bandwidth,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000768#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200769 &batadv_attr_log_level,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000770#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100771#ifdef CONFIG_BATMAN_ADV_NC
772 &batadv_attr_network_coding,
773#endif
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100774 &batadv_attr_isolation_mark,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775 NULL,
776};
777
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200778BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200779
Sven Eckelmannf34ac9d2015-09-06 21:38:53 +0200780/* array of vlan specific sysfs attributes */
Antonio Quartulli90f44352013-07-02 11:04:35 +0200781static struct batadv_attribute *batadv_vlan_attrs[] = {
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200782 &batadv_attr_vlan_ap_isolation,
Antonio Quartulli90f44352013-07-02 11:04:35 +0200783 NULL,
784};
785
Sven Eckelmannff15c272017-12-02 19:51:53 +0100786/**
787 * batadv_sysfs_add_meshif() - Add soft interface specific sysfs entries
788 * @dev: netdev struct of the soft interface
789 *
790 * Return: 0 on success or negative error number in case of failure
791 */
Sven Eckelmann5853e222012-05-12 02:09:24 +0200792int batadv_sysfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793{
794 struct kobject *batif_kobject = &dev->dev.kobj;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200795 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200796 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000797 int err;
798
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200799 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000800 batif_kobject);
801 if (!bat_priv->mesh_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200802 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200803 BATADV_SYSFS_IF_MESH_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804 goto out;
805 }
806
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200807 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808 err = sysfs_create_file(bat_priv->mesh_obj,
809 &((*bat_attr)->attr));
810 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200811 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200812 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +0200813 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000814 goto rem_attr;
815 }
816 }
817
818 return 0;
819
820rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200821 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000822 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
823
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200824 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
825 kobject_del(bat_priv->mesh_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000826 kobject_put(bat_priv->mesh_obj);
827 bat_priv->mesh_obj = NULL;
828out:
829 return -ENOMEM;
830}
831
Sven Eckelmannff15c272017-12-02 19:51:53 +0100832/**
833 * batadv_sysfs_del_meshif() - Remove soft interface specific sysfs entries
834 * @dev: netdev struct of the soft interface
835 */
Sven Eckelmann5853e222012-05-12 02:09:24 +0200836void batadv_sysfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200838 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200839 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200841 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
843
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200844 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
845 kobject_del(bat_priv->mesh_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846 kobject_put(bat_priv->mesh_obj);
847 bat_priv->mesh_obj = NULL;
848}
849
Antonio Quartulli90f44352013-07-02 11:04:35 +0200850/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100851 * batadv_sysfs_add_vlan() - add all the needed sysfs objects for the new vlan
Antonio Quartulli90f44352013-07-02 11:04:35 +0200852 * @dev: netdev of the mesh interface
853 * @vlan: private data of the newly added VLAN interface
854 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200855 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
Antonio Quartulli90f44352013-07-02 11:04:35 +0200856 */
857int batadv_sysfs_add_vlan(struct net_device *dev,
858 struct batadv_softif_vlan *vlan)
859{
860 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
861 struct batadv_priv *bat_priv = netdev_priv(dev);
862 struct batadv_attribute **bat_attr;
863 int err;
864
865 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
866 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
867 vlan->vid & VLAN_VID_MASK);
868
869 vlan->kobj = kobject_create_and_add(vlan_subdir,
870 bat_priv->mesh_obj);
871 if (!vlan->kobj) {
872 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
873 dev->name, vlan_subdir);
874 goto out;
875 }
876 } else {
877 /* the untagged LAN uses the root folder to store its "VLAN
878 * specific attributes"
879 */
880 vlan->kobj = bat_priv->mesh_obj;
881 kobject_get(bat_priv->mesh_obj);
882 }
883
884 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
885 err = sysfs_create_file(vlan->kobj,
886 &((*bat_attr)->attr));
887 if (err) {
888 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
889 dev->name, vlan_subdir,
890 ((*bat_attr)->attr).name);
891 goto rem_attr;
892 }
893 }
894
895 return 0;
896
897rem_attr:
898 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
899 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
900
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200901 if (vlan->kobj != bat_priv->mesh_obj) {
902 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
903 kobject_del(vlan->kobj);
904 }
Antonio Quartulli90f44352013-07-02 11:04:35 +0200905 kobject_put(vlan->kobj);
906 vlan->kobj = NULL;
907out:
908 return -ENOMEM;
909}
910
911/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100912 * batadv_sysfs_del_vlan() - remove all the sysfs objects for a given VLAN
Antonio Quartulli90f44352013-07-02 11:04:35 +0200913 * @bat_priv: the bat priv with all the soft interface information
914 * @vlan: the private data of the VLAN to destroy
915 */
916void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
917 struct batadv_softif_vlan *vlan)
918{
919 struct batadv_attribute **bat_attr;
920
921 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
922 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
923
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200924 if (vlan->kobj != bat_priv->mesh_obj) {
925 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
926 kobject_del(vlan->kobj);
927 }
Antonio Quartulli90f44352013-07-02 11:04:35 +0200928 kobject_put(vlan->kobj);
929 vlan->kobj = NULL;
930}
931
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200932static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
933 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000934{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200935 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200936 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000937 ssize_t length;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200938 const char *ifname;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000939
Sven Eckelmann56303d32012-06-05 22:31:31 +0200940 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000941 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942 return 0;
943
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200944 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
945 ifname = "none";
946 else
947 ifname = hard_iface->soft_iface->name;
948
949 length = sprintf(buff, "%s\n", ifname);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100951 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000952
953 return length;
954}
955
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200956/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100957 * batadv_store_mesh_iface_finish() - store new hardif mesh_iface state
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200958 * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
959 * @ifname: name of soft-interface to modify
960 *
961 * Changes the parts of the hard+soft interface which can not be modified under
962 * sysfs lock (to prevent deadlock situations).
963 *
964 * Return: 0 on success, 0 < on failure
965 */
966static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
967 char ifname[IFNAMSIZ])
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000968{
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200969 struct net *net = dev_net(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200970 struct batadv_hard_iface *hard_iface;
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200971 int status_tmp;
972 int ret = 0;
973
974 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000975
Sven Eckelmann56303d32012-06-05 22:31:31 +0200976 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000977 if (!hard_iface)
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200978 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000979
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200980 if (strncmp(ifname, "none", 4) == 0)
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200981 status_tmp = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000982 else
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200983 status_tmp = BATADV_IF_I_WANT_YOU;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000984
Marek Lindnere6c10f42011-02-18 12:33:20 +0000985 if (hard_iface->if_status == status_tmp)
986 goto out;
987
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200988 if (hard_iface->soft_iface &&
989 strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)
Marek Lindnered75ccb2011-02-10 14:33:51 +0000990 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000991
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200992 if (status_tmp == BATADV_IF_NOT_IN_USE) {
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800993 batadv_hardif_disable_interface(hard_iface,
994 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200995 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000996 }
997
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200998 /* if the interface already is in use */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200999 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +08001000 batadv_hardif_disable_interface(hard_iface,
1001 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001002
Sven Eckelmann77d69d82016-06-13 07:41:30 +02001003 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
Marek Lindnered75ccb2011-02-10 14:33:51 +00001004out:
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001005 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001006 return ret;
1007}
1008
Sven Eckelmann77d69d82016-06-13 07:41:30 +02001009/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001010 * batadv_store_mesh_iface_work() - store new hardif mesh_iface state
Sven Eckelmann77d69d82016-06-13 07:41:30 +02001011 * @work: work queue item
1012 *
1013 * Changes the parts of the hard+soft interface which can not be modified under
1014 * sysfs lock (to prevent deadlock situations).
1015 */
1016static void batadv_store_mesh_iface_work(struct work_struct *work)
1017{
1018 struct batadv_store_mesh_work *store_work;
1019 int ret;
1020
1021 store_work = container_of(work, struct batadv_store_mesh_work, work);
1022
1023 rtnl_lock();
1024 ret = batadv_store_mesh_iface_finish(store_work->net_dev,
1025 store_work->soft_iface_name);
1026 rtnl_unlock();
1027
1028 if (ret < 0)
1029 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
1030 store_work->soft_iface_name, store_work->net_dev->name,
1031 ret);
1032
1033 dev_put(store_work->net_dev);
1034 kfree(store_work);
1035}
1036
1037static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
1038 struct attribute *attr, char *buff,
1039 size_t count)
1040{
1041 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1042 struct batadv_store_mesh_work *store_work;
1043
1044 if (buff[count - 1] == '\n')
1045 buff[count - 1] = '\0';
1046
1047 if (strlen(buff) >= IFNAMSIZ) {
1048 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
1049 buff);
1050 return -EINVAL;
1051 }
1052
1053 store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
1054 if (!store_work)
1055 return -ENOMEM;
1056
1057 dev_hold(net_dev);
1058 INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1059 store_work->net_dev = net_dev;
1060 strlcpy(store_work->soft_iface_name, buff,
1061 sizeof(store_work->soft_iface_name));
1062
1063 queue_work(batadv_event_workqueue, &store_work->work);
1064
1065 return count;
1066}
1067
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001068static ssize_t batadv_show_iface_status(struct kobject *kobj,
1069 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001070{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001071 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001072 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001073 ssize_t length;
1074
Sven Eckelmann56303d32012-06-05 22:31:31 +02001075 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +00001076 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001077 return 0;
1078
Marek Lindnere6c10f42011-02-18 12:33:20 +00001079 switch (hard_iface->if_status) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001080 case BATADV_IF_TO_BE_REMOVED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001081 length = sprintf(buff, "disabling\n");
1082 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001083 case BATADV_IF_INACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001084 length = sprintf(buff, "inactive\n");
1085 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001086 case BATADV_IF_ACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001087 length = sprintf(buff, "active\n");
1088 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001089 case BATADV_IF_TO_BE_ACTIVATED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001090 length = sprintf(buff, "enabling\n");
1091 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001092 case BATADV_IF_NOT_IN_USE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001093 default:
1094 length = sprintf(buff, "not in use\n");
1095 break;
1096 }
1097
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001098 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001099
1100 return length;
1101}
1102
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001103#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1104
1105/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001106 * batadv_store_throughput_override() - parse and store throughput override
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001107 * entered by the user
1108 * @kobj: kobject representing the private mesh sysfs directory
1109 * @attr: the batman-adv attribute the user is interacting with
1110 * @buff: the buffer containing the user data
1111 * @count: number of bytes in the buffer
1112 *
1113 * Return: 'count' on success or a negative error code in case of failure
1114 */
1115static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1116 struct attribute *attr,
1117 char *buff, size_t count)
1118{
Sven Eckelmann7e6f4612018-11-23 16:07:12 +01001119 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001120 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1121 struct batadv_hard_iface *hard_iface;
1122 u32 tp_override;
1123 u32 old_tp_override;
1124 bool ret;
1125
1126 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1127 if (!hard_iface)
1128 return -EINVAL;
1129
1130 if (buff[count - 1] == '\n')
1131 buff[count - 1] = '\0';
1132
1133 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1134 &tp_override);
1135 if (!ret)
1136 return count;
1137
1138 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1139 if (old_tp_override == tp_override)
1140 goto out;
1141
Sven Eckelmannb9fd14c2018-08-31 16:46:47 +02001142 batadv_info(hard_iface->soft_iface,
1143 "%s: %s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1144 "throughput_override", net_dev->name,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001145 old_tp_override / 10, old_tp_override % 10,
1146 tp_override / 10, tp_override % 10);
1147
1148 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1149
Sven Eckelmann7e6f4612018-11-23 16:07:12 +01001150 batadv_netlink_notify_hardif(bat_priv, hard_iface);
1151
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001152out:
1153 batadv_hardif_put(hard_iface);
1154 return count;
1155}
1156
1157static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1158 struct attribute *attr,
1159 char *buff)
1160{
1161 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1162 struct batadv_hard_iface *hard_iface;
1163 u32 tp_override;
1164
1165 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1166 if (!hard_iface)
1167 return -EINVAL;
1168
1169 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1170
1171 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1172 tp_override % 10);
1173}
1174
1175#endif
1176
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001177static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
Sven Eckelmann347c80f2012-06-03 22:19:07 +02001178 batadv_store_mesh_iface);
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001179static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
Linus Luessing7f136cd2016-01-16 16:40:11 +08001180#ifdef CONFIG_BATMAN_ADV_BATMAN_V
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001181BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001182 2 * BATADV_JITTER, INT_MAX, NULL);
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001183static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001184 batadv_store_throughput_override);
Linus Luessing7f136cd2016-01-16 16:40:11 +08001185#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001186
Sven Eckelmannb4d66b82012-06-05 22:31:29 +02001187static struct batadv_attribute *batadv_batman_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001188 &batadv_attr_mesh_iface,
1189 &batadv_attr_iface_status,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001190#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1191 &batadv_attr_elp_interval,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001192 &batadv_attr_throughput_override,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001193#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001194 NULL,
1195};
1196
Sven Eckelmannff15c272017-12-02 19:51:53 +01001197/**
1198 * batadv_sysfs_add_hardif() - Add hard interface specific sysfs entries
1199 * @hardif_obj: address where to store the pointer to new sysfs folder
1200 * @dev: netdev struct of the hard interface
1201 *
1202 * Return: 0 on success or negative error number in case of failure
1203 */
Sven Eckelmann5853e222012-05-12 02:09:24 +02001204int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001205{
1206 struct kobject *hardif_kobject = &dev->dev.kobj;
Sven Eckelmannb4d66b82012-06-05 22:31:29 +02001207 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001208 int err;
1209
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001210 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1211 hardif_kobject);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001212
1213 if (!*hardif_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +02001214 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001215 BATADV_SYSFS_IF_BAT_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001216 goto out;
1217 }
1218
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001219 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001220 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1221 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +02001222 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001223 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +02001224 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001225 goto rem_attr;
1226 }
1227 }
1228
1229 return 0;
1230
1231rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001232 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001233 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1234out:
1235 return -ENOMEM;
1236}
1237
Sven Eckelmannff15c272017-12-02 19:51:53 +01001238/**
1239 * batadv_sysfs_del_hardif() - Remove hard interface specific sysfs entries
1240 * @hardif_obj: address to the pointer to which stores batman-adv sysfs folder
1241 * of the hard interface
1242 */
Sven Eckelmann5853e222012-05-12 02:09:24 +02001243void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001244{
Sven Eckelmannf4acb102016-06-13 07:41:32 +02001245 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1246 kobject_del(*hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001247 kobject_put(*hardif_obj);
1248 *hardif_obj = NULL;
1249}
Antonio Quartullic6bda682011-04-26 18:26:01 +02001250
Sven Eckelmannff15c272017-12-02 19:51:53 +01001251/**
1252 * batadv_throw_uevent() - Send an uevent with batman-adv specific env data
1253 * @bat_priv: the bat priv with all the soft interface information
1254 * @type: subsystem type of event. Stored in uevent's BATTYPE
1255 * @action: action type of event. Stored in uevent's BATACTION
1256 * @data: string with additional information to the event (ignored for
1257 * BATADV_UEV_DEL). Stored in uevent's BATDATA
1258 *
1259 * Return: 0 on success or negative error number in case of failure
1260 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001261int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001262 enum batadv_uev_action action, const char *data)
Antonio Quartullic6bda682011-04-26 18:26:01 +02001263{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001264 int ret = -ENOMEM;
Antonio Quartullic6bda682011-04-26 18:26:01 +02001265 struct kobject *bat_kobj;
1266 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1267
Marek Lindner736292c2013-01-12 19:19:06 +08001268 bat_kobj = &bat_priv->soft_iface->dev.kobj;
Antonio Quartullic6bda682011-04-26 18:26:01 +02001269
Himangi Saraogib98fe242014-06-29 00:06:29 +05301270 uevent_env[0] = kasprintf(GFP_ATOMIC,
1271 "%s%s", BATADV_UEV_TYPE_VAR,
1272 batadv_uev_type_str[type]);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001273 if (!uevent_env[0])
1274 goto out;
1275
Himangi Saraogib98fe242014-06-29 00:06:29 +05301276 uevent_env[1] = kasprintf(GFP_ATOMIC,
1277 "%s%s", BATADV_UEV_ACTION_VAR,
1278 batadv_uev_action_str[action]);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001279 if (!uevent_env[1])
1280 goto out;
1281
Antonio Quartullic6bda682011-04-26 18:26:01 +02001282 /* If the event is DEL, ignore the data field */
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001283 if (action != BATADV_UEV_DEL) {
Himangi Saraogib98fe242014-06-29 00:06:29 +05301284 uevent_env[2] = kasprintf(GFP_ATOMIC,
1285 "%s%s", BATADV_UEV_DATA_VAR, data);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001286 if (!uevent_env[2])
1287 goto out;
Antonio Quartullic6bda682011-04-26 18:26:01 +02001288 }
1289
1290 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1291out:
1292 kfree(uevent_env[0]);
1293 kfree(uevent_env[1]);
1294 kfree(uevent_env[2]);
1295
Antonio Quartullic6bda682011-04-26 18:26:01 +02001296 if (ret)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001297 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001298 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001299 batadv_uev_type_str[type],
1300 batadv_uev_action_str[action],
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001301 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
Antonio Quartullic6bda682011-04-26 18:26:01 +02001302 return ret;
1303}