blob: 80fc3253c3368e3cc356176c5ba961542de0a8c9 [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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
Sven Eckelmannb706b132012-06-10 23:58:51 +02007#include "sysfs.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
9
Sven Eckelmann1392f552019-03-03 18:02:59 +010010#include <asm/current.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020011#include <linux/atomic.h>
12#include <linux/compiler.h>
13#include <linux/device.h>
14#include <linux/errno.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010015#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020016#include <linux/if.h>
17#include <linux/if_vlan.h>
18#include <linux/kernel.h>
Sven Eckelmann8cfba952017-11-19 17:12:05 +010019#include <linux/kobject.h>
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020020#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/netdevice.h>
22#include <linux/printk.h>
23#include <linux/rculist.h>
24#include <linux/rcupdate.h>
25#include <linux/rtnetlink.h>
Sven Eckelmann1392f552019-03-03 18:02:59 +010026#include <linux/sched.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/slab.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020028#include <linux/stddef.h>
29#include <linux/string.h>
30#include <linux/stringify.h>
Sven Eckelmann77d69d82016-06-13 07:41:30 +020031#include <linux/workqueue.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010032#include <uapi/linux/batadv_packet.h>
Sven Eckelmanne2d0d352018-11-23 13:15:00 +010033#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020034
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020035#include "bridge_loop_avoidance.h"
Antonio Quartulli33af49a2012-08-08 18:50:57 +020036#include "distributed-arp-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020038#include "gateway_common.h"
39#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020040#include "log.h"
Sven Eckelmann7e6f4612018-11-23 16:07:12 +010041#include "netlink.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020042#include "network-coding.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020043#include "soft-interface.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044
Sven Eckelmann1392f552019-03-03 18:02:59 +010045/**
46 * batadv_sysfs_deprecated() - Log use of deprecated batadv sysfs access
47 * @attr: attribute which was accessed
48 */
49static void batadv_sysfs_deprecated(struct attribute *attr)
50{
51 pr_warn_ratelimited(DEPRECATED "%s (pid %d) Use of sysfs file \"%s\".\nUse batadv genl family instead",
52 current->comm, task_pid_nr(current), attr->name);
53}
54
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
Antonio Quartulli90f44352013-07-02 11:04:35 +0200117/* Use this, if you have customized show and store functions for vlan attrs */
118#define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
119struct batadv_attribute batadv_attr_vlan_##_name = { \
120 .attr = {.name = __stringify(_name), \
121 .mode = _mode }, \
122 .show = _show, \
123 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200124}
Antonio Quartulli90f44352013-07-02 11:04:35 +0200125
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126/* Use this, if you have customized show and store functions */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200127#define BATADV_ATTR(_name, _mode, _show, _store) \
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200128struct batadv_attribute batadv_attr_##_name = { \
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200129 .attr = {.name = __stringify(_name), \
130 .mode = _mode }, \
131 .show = _show, \
132 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200133}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200135#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200136ssize_t batadv_store_##_name(struct kobject *kobj, \
137 struct attribute *attr, char *buff, \
138 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000139{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200140 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200141 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100142 ssize_t length; \
Antonio Quartullif1386942014-05-10 18:56:37 +0200143 \
Sven Eckelmann1392f552019-03-03 18:02:59 +0100144 batadv_sysfs_deprecated(attr); \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100145 length = __batadv_store_bool_attr(buff, count, _post_func, attr,\
146 &bat_priv->_name, net_dev); \
147 \
148 batadv_netlink_notify_mesh(bat_priv); \
149 \
150 return length; \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151}
152
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200153#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200154ssize_t batadv_show_##_name(struct kobject *kobj, \
155 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000156{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200157 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200158 \
Sven Eckelmann1392f552019-03-03 18:02:59 +0100159 batadv_sysfs_deprecated(attr); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160 return sprintf(buff, "%s\n", \
161 atomic_read(&bat_priv->_name) == 0 ? \
162 "disabled" : "enabled"); \
163} \
164
Marek Lindnerf245c382012-03-11 06:17:51 +0800165/* Use this, if you are going to turn a [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200166 * (bat_priv) on or off
167 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200168#define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
169 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
170 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
171 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
172 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800174#define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200175ssize_t batadv_store_##_name(struct kobject *kobj, \
176 struct attribute *attr, char *buff, \
177 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200179 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200180 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100181 ssize_t length; \
Antonio Quartullif1386942014-05-10 18:56:37 +0200182 \
Sven Eckelmann1392f552019-03-03 18:02:59 +0100183 batadv_sysfs_deprecated(attr); \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100184 length = __batadv_store_uint_attr(buff, count, _min, _max, \
185 _post_func, attr, \
186 &bat_priv->_var, net_dev, \
187 NULL); \
188 \
189 batadv_netlink_notify_mesh(bat_priv); \
190 \
191 return length; \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192}
193
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800194#define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200195ssize_t batadv_show_##_name(struct kobject *kobj, \
196 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200198 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200199 \
Sven Eckelmann1392f552019-03-03 18:02:59 +0100200 batadv_sysfs_deprecated(attr); \
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800201 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202} \
203
Marek Lindnerf245c382012-03-11 06:17:51 +0800204/* Use this, if you are going to set [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200205 * (bat_priv) to an unsigned integer value
206 */
Antonio Quartulli6f70eb72015-03-01 00:50:16 +0800207#define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
208 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
209 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200210 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
211 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212
Antonio Quartulli90f44352013-07-02 11:04:35 +0200213#define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
214ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
215 struct attribute *attr, char *buff, \
216 size_t count) \
217{ \
218 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
219 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
220 kobj); \
221 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
222 attr, &vlan->_name, \
223 bat_priv->soft_iface); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200224 \
Sven Eckelmann1392f552019-03-03 18:02:59 +0100225 batadv_sysfs_deprecated(attr); \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100226 if (vlan->vid) \
227 batadv_netlink_notify_vlan(bat_priv, vlan); \
228 else \
229 batadv_netlink_notify_mesh(bat_priv); \
230 \
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100231 batadv_softif_vlan_put(vlan); \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200232 return res; \
233}
234
235#define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
236ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
237 struct attribute *attr, char *buff) \
238{ \
239 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
240 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
241 kobj); \
242 size_t res = sprintf(buff, "%s\n", \
243 atomic_read(&vlan->_name) == 0 ? \
244 "disabled" : "enabled"); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200245 \
Sven Eckelmann1392f552019-03-03 18:02:59 +0100246 batadv_sysfs_deprecated(attr); \
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100247 batadv_softif_vlan_put(vlan); \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200248 return res; \
249}
250
251/* Use this, if you are going to turn a [name] in the vlan struct on or off */
252#define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
253 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
254 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
255 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
256 batadv_store_vlan_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257
Linus Luessing0744ff82016-01-16 16:40:08 +0800258#define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
259ssize_t batadv_store_##_name(struct kobject *kobj, \
260 struct attribute *attr, char *buff, \
261 size_t count) \
262{ \
263 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
264 struct batadv_hard_iface *hard_iface; \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100265 struct batadv_priv *bat_priv; \
Linus Luessing0744ff82016-01-16 16:40:08 +0800266 ssize_t length; \
267 \
Sven Eckelmann1392f552019-03-03 18:02:59 +0100268 batadv_sysfs_deprecated(attr); \
Linus Luessing0744ff82016-01-16 16:40:08 +0800269 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
270 if (!hard_iface) \
271 return 0; \
272 \
273 length = __batadv_store_uint_attr(buff, count, _min, _max, \
274 _post_func, attr, \
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200275 &hard_iface->_var, \
276 hard_iface->soft_iface, \
277 net_dev); \
Linus Luessing0744ff82016-01-16 16:40:08 +0800278 \
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100279 if (hard_iface->soft_iface) { \
280 bat_priv = netdev_priv(hard_iface->soft_iface); \
281 batadv_netlink_notify_hardif(bat_priv, hard_iface); \
282 } \
283 \
Linus Luessing0744ff82016-01-16 16:40:08 +0800284 batadv_hardif_put(hard_iface); \
285 return length; \
286}
287
288#define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
289ssize_t batadv_show_##_name(struct kobject *kobj, \
290 struct attribute *attr, char *buff) \
291{ \
292 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
293 struct batadv_hard_iface *hard_iface; \
294 ssize_t length; \
295 \
Sven Eckelmann1392f552019-03-03 18:02:59 +0100296 batadv_sysfs_deprecated(attr); \
Linus Luessing0744ff82016-01-16 16:40:08 +0800297 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
298 if (!hard_iface) \
299 return 0; \
300 \
301 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
302 \
303 batadv_hardif_put(hard_iface); \
304 return length; \
305}
306
307/* Use this, if you are going to set [name] in hard_iface to an
308 * unsigned integer value
309 */
310#define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
311 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
312 _max, _post_func) \
313 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
314 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
315 batadv_store_##_name)
316
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200317static int batadv_store_bool_attr(char *buff, size_t count,
318 struct net_device *net_dev,
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100319 const char *attr_name, atomic_t *attr,
320 bool *changed)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321{
322 int enabled = -1;
323
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100324 *changed = false;
325
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326 if (buff[count - 1] == '\n')
327 buff[count - 1] = '\0';
328
329 if ((strncmp(buff, "1", 2) == 0) ||
330 (strncmp(buff, "enable", 7) == 0) ||
331 (strncmp(buff, "enabled", 8) == 0))
332 enabled = 1;
333
334 if ((strncmp(buff, "0", 2) == 0) ||
335 (strncmp(buff, "disable", 8) == 0) ||
336 (strncmp(buff, "disabled", 9) == 0))
337 enabled = 0;
338
339 if (enabled < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200340 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
341 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342 return -EINVAL;
343 }
344
345 if (atomic_read(attr) == enabled)
346 return count;
347
Sven Eckelmann3e348192012-05-16 20:23:22 +0200348 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
349 atomic_read(attr) == 1 ? "enabled" : "disabled",
350 enabled == 1 ? "enabled" : "disabled");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100352 *changed = true;
353
Eric Dumazet95c96172012-04-15 05:58:06 +0000354 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355 return count;
356}
357
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200358static inline ssize_t
359__batadv_store_bool_attr(char *buff, size_t count,
360 void (*post_func)(struct net_device *),
361 struct attribute *attr,
362 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363{
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100364 bool changed;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000365 int ret;
366
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200367 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
Simon Wunderlich9e728e82015-11-17 14:11:26 +0100368 attr_store, &changed);
369 if (post_func && changed)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370 post_func(net_dev);
371
372 return ret;
373}
374
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200375static int batadv_store_uint_attr(const char *buff, size_t count,
376 struct net_device *net_dev,
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200377 struct net_device *slave_dev,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200378 const char *attr_name,
379 unsigned int min, unsigned int max,
380 atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381{
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200382 char ifname[IFNAMSIZ + 3] = "";
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383 unsigned long uint_val;
384 int ret;
385
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200386 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387 if (ret) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200388 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
389 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390 return -EINVAL;
391 }
392
393 if (uint_val < min) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200394 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
395 attr_name, uint_val, min);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396 return -EINVAL;
397 }
398
399 if (uint_val > max) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200400 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
401 attr_name, uint_val, max);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402 return -EINVAL;
403 }
404
405 if (atomic_read(attr) == uint_val)
406 return count;
407
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200408 if (slave_dev)
409 snprintf(ifname, sizeof(ifname), "%s: ", slave_dev->name);
410
411 batadv_info(net_dev, "%s: %sChanging from: %i to: %lu\n",
412 attr_name, ifname, atomic_read(attr), uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
414 atomic_set(attr, uint_val);
415 return count;
416}
417
Antonio Quartullic149ca72016-05-06 02:46:37 +0800418static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
419 int min, int max,
420 void (*post_func)(struct net_device *),
421 const struct attribute *attr,
422 atomic_t *attr_store,
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200423 struct net_device *net_dev,
424 struct net_device *slave_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425{
426 int ret;
427
Sven Eckelmanna25bab92018-08-31 16:56:29 +0200428 ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
429 attr->name, min, max, attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430 if (post_func && ret)
431 post_func(net_dev);
432
433 return ret;
434}
435
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200436static ssize_t batadv_show_bat_algo(struct kobject *kobj,
437 struct attribute *attr, char *buff)
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800438{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200439 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Antonio Quartullif1386942014-05-10 18:56:37 +0200440
Sven Eckelmann1392f552019-03-03 18:02:59 +0100441 batadv_sysfs_deprecated(attr);
Antonio Quartulli29824a52016-05-25 23:27:31 +0800442 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800443}
444
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100445static void batadv_post_gw_reselect(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200447 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartullif1386942014-05-10 18:56:37 +0200448
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100449 batadv_gw_reselect(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450}
451
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200452static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
453 char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200455 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456 int bytes_written;
457
Sven Eckelmann1392f552019-03-03 18:02:59 +0100458 batadv_sysfs_deprecated(attr);
459
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200460 /* GW mode is not available if the routing algorithm in use does not
461 * implement the GW API
462 */
463 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
464 !bat_priv->algo_ops->gw.is_eligible)
465 return -ENOENT;
466
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800467 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200468 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200469 bytes_written = sprintf(buff, "%s\n",
470 BATADV_GW_MODE_CLIENT_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200472 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200473 bytes_written = sprintf(buff, "%s\n",
474 BATADV_GW_MODE_SERVER_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475 break;
476 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200477 bytes_written = sprintf(buff, "%s\n",
478 BATADV_GW_MODE_OFF_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479 break;
480 }
481
482 return bytes_written;
483}
484
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200485static ssize_t batadv_store_gw_mode(struct kobject *kobj,
486 struct attribute *attr, char *buff,
487 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200489 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200490 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491 char *curr_gw_mode_str;
492 int gw_mode_tmp = -1;
493
Sven Eckelmann1392f552019-03-03 18:02:59 +0100494 batadv_sysfs_deprecated(attr);
495
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200496 /* toggling GW mode is allowed only if the routing algorithm in use
497 * provides the GW API
498 */
499 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
500 !bat_priv->algo_ops->gw.is_eligible)
501 return -EINVAL;
502
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503 if (buff[count - 1] == '\n')
504 buff[count - 1] = '\0';
505
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200506 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
507 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200508 gw_mode_tmp = BATADV_GW_MODE_OFF;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200510 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
511 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200512 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200514 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
515 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200516 gw_mode_tmp = BATADV_GW_MODE_SERVER;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517
518 if (gw_mode_tmp < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200519 batadv_info(net_dev,
520 "Invalid parameter for 'gw mode' setting received: %s\n",
521 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522 return -EINVAL;
523 }
524
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800525 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526 return count;
527
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800528 switch (atomic_read(&bat_priv->gw.mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200529 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200530 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200532 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200533 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534 break;
535 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200536 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537 break;
538 }
539
Sven Eckelmann3e348192012-05-16 20:23:22 +0200540 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
541 curr_gw_mode_str, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542
Antonio Quartullif3163182013-11-04 20:59:40 +0100543 /* Invoking batadv_gw_reselect() is not enough to really de-select the
544 * current GW. It will only instruct the gateway client code to perform
545 * a re-election the next time that this is needed.
546 *
547 * When gw client mode is being switched off the current GW must be
548 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
549 * client mode re-activation. This is operation is performed in
550 * batadv_gw_check_client_stop().
551 */
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100552 batadv_gw_reselect(bat_priv);
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200553 /* always call batadv_gw_check_client_stop() before changing the gateway
554 * state
555 */
556 batadv_gw_check_client_stop(bat_priv);
Antonio Quartulli3a24a632016-05-06 02:46:38 +0800557 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
Marek Lindner414254e2013-04-23 21:39:58 +0800558 batadv_gw_tvlv_container_update(bat_priv);
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100559
560 batadv_netlink_notify_mesh(bat_priv);
561
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562 return count;
563}
564
Antonio Quartulli08686942016-07-03 12:46:32 +0200565static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
566 struct attribute *attr, char *buff)
567{
568 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
569
Sven Eckelmann1392f552019-03-03 18:02:59 +0100570 batadv_sysfs_deprecated(attr);
571
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200572 /* GW selection class is not available if the routing algorithm in use
573 * does not implement the GW API
574 */
575 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
576 !bat_priv->algo_ops->gw.is_eligible)
577 return -ENOENT;
578
Antonio Quartulli08686942016-07-03 12:46:32 +0200579 if (bat_priv->algo_ops->gw.show_sel_class)
580 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
581
582 return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
583}
584
585static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
586 struct attribute *attr, char *buff,
587 size_t count)
588{
589 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100590 ssize_t length;
Antonio Quartulli08686942016-07-03 12:46:32 +0200591
Sven Eckelmann1392f552019-03-03 18:02:59 +0100592 batadv_sysfs_deprecated(attr);
593
Antonio Quartullia8d8d1d2016-07-03 12:46:35 +0200594 /* setting the GW selection class is allowed only if the routing
595 * algorithm in use implements the GW API
596 */
597 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
598 !bat_priv->algo_ops->gw.is_eligible)
599 return -EINVAL;
600
Antonio Quartulli08686942016-07-03 12:46:32 +0200601 if (buff[count - 1] == '\n')
602 buff[count - 1] = '\0';
603
604 if (bat_priv->algo_ops->gw.store_sel_class)
605 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
606 count);
607
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100608 length = __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
609 batadv_post_gw_reselect, attr,
610 &bat_priv->gw.sel_class,
611 bat_priv->soft_iface, NULL);
612
613 batadv_netlink_notify_mesh(bat_priv);
614
615 return length;
Antonio Quartulli08686942016-07-03 12:46:32 +0200616}
617
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200618static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
619 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200621 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200622 u32 down, up;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623
Sven Eckelmann1392f552019-03-03 18:02:59 +0100624 batadv_sysfs_deprecated(attr);
625
Marek Lindner414254e2013-04-23 21:39:58 +0800626 down = atomic_read(&bat_priv->gw.bandwidth_down);
627 up = atomic_read(&bat_priv->gw.bandwidth_up);
628
629 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
630 down % 10, up / 10, up % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631}
632
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200633static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
634 struct attribute *attr, char *buff,
635 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636{
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100637 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200638 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100639 ssize_t length;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640
Sven Eckelmann1392f552019-03-03 18:02:59 +0100641 batadv_sysfs_deprecated(attr);
642
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643 if (buff[count - 1] == '\n')
644 buff[count - 1] = '\0';
645
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100646 length = batadv_gw_bandwidth_set(net_dev, buff, count);
647
648 batadv_netlink_notify_mesh(bat_priv);
649
650 return length;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651}
652
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100653/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100654 * batadv_show_isolation_mark() - print the current isolation mark/mask
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100655 * @kobj: kobject representing the private mesh sysfs directory
656 * @attr: the batman-adv attribute the user is interacting with
657 * @buff: the buffer that will contain the data to send back to the user
658 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200659 * Return: the number of bytes written into 'buff' on success or a negative
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100660 * error code in case of failure
661 */
662static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
663 struct attribute *attr, char *buff)
664{
665 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
666
Sven Eckelmann1392f552019-03-03 18:02:59 +0100667 batadv_sysfs_deprecated(attr);
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100668 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
669 bat_priv->isolation_mark_mask);
670}
671
672/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100673 * batadv_store_isolation_mark() - parse and store the isolation mark/mask
674 * entered by the user
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100675 * @kobj: kobject representing the private mesh sysfs directory
676 * @attr: the batman-adv attribute the user is interacting with
677 * @buff: the buffer containing the user data
678 * @count: number of bytes in the buffer
679 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200680 * Return: 'count' on success or a negative error code in case of failure
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100681 */
682static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
683 struct attribute *attr, char *buff,
684 size_t count)
685{
686 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
687 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200688 u32 mark, mask;
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100689 char *mask_ptr;
690
Sven Eckelmann1392f552019-03-03 18:02:59 +0100691 batadv_sysfs_deprecated(attr);
692
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100693 /* parse the mask if it has been specified, otherwise assume the mask is
694 * the biggest possible
695 */
696 mask = 0xFFFFFFFF;
697 mask_ptr = strchr(buff, '/');
698 if (mask_ptr) {
699 *mask_ptr = '\0';
700 mask_ptr++;
701
702 /* the mask must be entered in hex base as it is going to be a
703 * bitmask and not a prefix length
704 */
705 if (kstrtou32(mask_ptr, 16, &mask) < 0)
706 return -EINVAL;
707 }
708
709 /* the mark can be entered in any base */
710 if (kstrtou32(buff, 0, &mark) < 0)
711 return -EINVAL;
712
713 bat_priv->isolation_mark_mask = mask;
714 /* erase bits not covered by the mask */
715 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
716
717 batadv_info(net_dev,
718 "New skb mark for extended isolation: %#.8x/%#.8x\n",
719 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
720
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100721 batadv_netlink_notify_mesh(bat_priv);
722
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100723 return count;
724}
725
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200726BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
727BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100728#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200729BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100730#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200731#ifdef CONFIG_BATMAN_ADV_DAT
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200732BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200733#endif
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200734BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
735static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
736static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
737BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
738 INT_MAX, NULL);
739BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
740 NULL);
741static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
Antonio Quartulli08686942016-07-03 12:46:32 +0200742 batadv_store_gw_sel_class);
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200743static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200744 batadv_store_gw_bwidth);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100745#ifdef CONFIG_BATMAN_ADV_MCAST
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200746BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100747#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000748#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200749BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100751#ifdef CONFIG_BATMAN_ADV_NC
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200752BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100753#endif
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200754static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
755 batadv_store_isolation_mark);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000756
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200757static struct batadv_attribute *batadv_mesh_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200758 &batadv_attr_aggregated_ogms,
759 &batadv_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100760#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200761 &batadv_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100762#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200763#ifdef CONFIG_BATMAN_ADV_DAT
764 &batadv_attr_distributed_arp_table,
765#endif
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100766#ifdef CONFIG_BATMAN_ADV_MCAST
767 &batadv_attr_multicast_mode,
768#endif
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200769 &batadv_attr_fragmentation,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200770 &batadv_attr_routing_algo,
771 &batadv_attr_gw_mode,
772 &batadv_attr_orig_interval,
773 &batadv_attr_hop_penalty,
774 &batadv_attr_gw_sel_class,
775 &batadv_attr_gw_bandwidth,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200777 &batadv_attr_log_level,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100779#ifdef CONFIG_BATMAN_ADV_NC
780 &batadv_attr_network_coding,
781#endif
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100782 &batadv_attr_isolation_mark,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783 NULL,
784};
785
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200786BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200787
Sven Eckelmannf34ac9d2015-09-06 21:38:53 +0200788/* array of vlan specific sysfs attributes */
Antonio Quartulli90f44352013-07-02 11:04:35 +0200789static struct batadv_attribute *batadv_vlan_attrs[] = {
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200790 &batadv_attr_vlan_ap_isolation,
Antonio Quartulli90f44352013-07-02 11:04:35 +0200791 NULL,
792};
793
Sven Eckelmannff15c272017-12-02 19:51:53 +0100794/**
795 * batadv_sysfs_add_meshif() - Add soft interface specific sysfs entries
796 * @dev: netdev struct of the soft interface
797 *
798 * Return: 0 on success or negative error number in case of failure
799 */
Sven Eckelmann5853e222012-05-12 02:09:24 +0200800int batadv_sysfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000801{
802 struct kobject *batif_kobject = &dev->dev.kobj;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200803 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200804 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000805 int err;
806
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200807 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808 batif_kobject);
809 if (!bat_priv->mesh_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200810 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200811 BATADV_SYSFS_IF_MESH_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812 goto out;
813 }
814
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200815 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816 err = sysfs_create_file(bat_priv->mesh_obj,
817 &((*bat_attr)->attr));
818 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200819 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200820 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +0200821 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000822 goto rem_attr;
823 }
824 }
825
826 return 0;
827
828rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200829 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000830 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
831
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200832 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
833 kobject_del(bat_priv->mesh_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000834 kobject_put(bat_priv->mesh_obj);
835 bat_priv->mesh_obj = NULL;
836out:
837 return -ENOMEM;
838}
839
Sven Eckelmannff15c272017-12-02 19:51:53 +0100840/**
841 * batadv_sysfs_del_meshif() - Remove soft interface specific sysfs entries
842 * @dev: netdev struct of the soft interface
843 */
Sven Eckelmann5853e222012-05-12 02:09:24 +0200844void batadv_sysfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000845{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200846 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200847 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000848
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200849 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
851
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200852 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
853 kobject_del(bat_priv->mesh_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000854 kobject_put(bat_priv->mesh_obj);
855 bat_priv->mesh_obj = NULL;
856}
857
Antonio Quartulli90f44352013-07-02 11:04:35 +0200858/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100859 * batadv_sysfs_add_vlan() - add all the needed sysfs objects for the new vlan
Antonio Quartulli90f44352013-07-02 11:04:35 +0200860 * @dev: netdev of the mesh interface
861 * @vlan: private data of the newly added VLAN interface
862 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200863 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
Antonio Quartulli90f44352013-07-02 11:04:35 +0200864 */
865int batadv_sysfs_add_vlan(struct net_device *dev,
866 struct batadv_softif_vlan *vlan)
867{
868 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
869 struct batadv_priv *bat_priv = netdev_priv(dev);
870 struct batadv_attribute **bat_attr;
871 int err;
872
873 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
874 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
875 vlan->vid & VLAN_VID_MASK);
876
877 vlan->kobj = kobject_create_and_add(vlan_subdir,
878 bat_priv->mesh_obj);
879 if (!vlan->kobj) {
880 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
881 dev->name, vlan_subdir);
882 goto out;
883 }
884 } else {
885 /* the untagged LAN uses the root folder to store its "VLAN
886 * specific attributes"
887 */
888 vlan->kobj = bat_priv->mesh_obj;
889 kobject_get(bat_priv->mesh_obj);
890 }
891
892 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
893 err = sysfs_create_file(vlan->kobj,
894 &((*bat_attr)->attr));
895 if (err) {
896 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
897 dev->name, vlan_subdir,
898 ((*bat_attr)->attr).name);
899 goto rem_attr;
900 }
901 }
902
903 return 0;
904
905rem_attr:
906 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
907 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
908
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200909 if (vlan->kobj != bat_priv->mesh_obj) {
910 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
911 kobject_del(vlan->kobj);
912 }
Antonio Quartulli90f44352013-07-02 11:04:35 +0200913 kobject_put(vlan->kobj);
914 vlan->kobj = NULL;
915out:
916 return -ENOMEM;
917}
918
919/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100920 * batadv_sysfs_del_vlan() - remove all the sysfs objects for a given VLAN
Antonio Quartulli90f44352013-07-02 11:04:35 +0200921 * @bat_priv: the bat priv with all the soft interface information
922 * @vlan: the private data of the VLAN to destroy
923 */
924void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
925 struct batadv_softif_vlan *vlan)
926{
927 struct batadv_attribute **bat_attr;
928
929 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
930 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
931
Sven Eckelmannf4acb102016-06-13 07:41:32 +0200932 if (vlan->kobj != bat_priv->mesh_obj) {
933 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
934 kobject_del(vlan->kobj);
935 }
Antonio Quartulli90f44352013-07-02 11:04:35 +0200936 kobject_put(vlan->kobj);
937 vlan->kobj = NULL;
938}
939
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200940static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
941 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200943 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200944 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000945 ssize_t length;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200946 const char *ifname;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000947
Sven Eckelmann1392f552019-03-03 18:02:59 +0100948 batadv_sysfs_deprecated(attr);
949
Sven Eckelmann56303d32012-06-05 22:31:31 +0200950 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000951 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000952 return 0;
953
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200954 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
955 ifname = "none";
956 else
957 ifname = hard_iface->soft_iface->name;
958
959 length = sprintf(buff, "%s\n", ifname);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000960
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100961 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000962
963 return length;
964}
965
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200966/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100967 * batadv_store_mesh_iface_finish() - store new hardif mesh_iface state
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200968 * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
969 * @ifname: name of soft-interface to modify
970 *
971 * Changes the parts of the hard+soft interface which can not be modified under
972 * sysfs lock (to prevent deadlock situations).
973 *
974 * Return: 0 on success, 0 < on failure
975 */
976static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
977 char ifname[IFNAMSIZ])
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000978{
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200979 struct net *net = dev_net(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200980 struct batadv_hard_iface *hard_iface;
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200981 int status_tmp;
982 int ret = 0;
983
984 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000985
Sven Eckelmann56303d32012-06-05 22:31:31 +0200986 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000987 if (!hard_iface)
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200988 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000989
Sven Eckelmann77d69d82016-06-13 07:41:30 +0200990 if (strncmp(ifname, "none", 4) == 0)
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200991 status_tmp = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000992 else
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200993 status_tmp = BATADV_IF_I_WANT_YOU;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000994
Marek Lindnere6c10f42011-02-18 12:33:20 +0000995 if (hard_iface->if_status == status_tmp)
996 goto out;
997
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200998 if (hard_iface->soft_iface &&
999 strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)
Marek Lindnered75ccb2011-02-10 14:33:51 +00001000 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001001
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001002 if (status_tmp == BATADV_IF_NOT_IN_USE) {
Sven Eckelmanna15fd362013-02-11 17:10:24 +08001003 batadv_hardif_disable_interface(hard_iface,
1004 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmann77d69d82016-06-13 07:41:30 +02001005 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001006 }
1007
Sven Eckelmann3a4375a2011-05-03 13:10:06 +02001008 /* if the interface already is in use */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001009 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +08001010 batadv_hardif_disable_interface(hard_iface,
1011 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001012
Sven Eckelmann77d69d82016-06-13 07:41:30 +02001013 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
Marek Lindnered75ccb2011-02-10 14:33:51 +00001014out:
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001015 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016 return ret;
1017}
1018
Sven Eckelmann77d69d82016-06-13 07:41:30 +02001019/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001020 * batadv_store_mesh_iface_work() - store new hardif mesh_iface state
Sven Eckelmann77d69d82016-06-13 07:41:30 +02001021 * @work: work queue item
1022 *
1023 * Changes the parts of the hard+soft interface which can not be modified under
1024 * sysfs lock (to prevent deadlock situations).
1025 */
1026static void batadv_store_mesh_iface_work(struct work_struct *work)
1027{
1028 struct batadv_store_mesh_work *store_work;
1029 int ret;
1030
1031 store_work = container_of(work, struct batadv_store_mesh_work, work);
1032
1033 rtnl_lock();
1034 ret = batadv_store_mesh_iface_finish(store_work->net_dev,
1035 store_work->soft_iface_name);
1036 rtnl_unlock();
1037
1038 if (ret < 0)
1039 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
1040 store_work->soft_iface_name, store_work->net_dev->name,
1041 ret);
1042
1043 dev_put(store_work->net_dev);
1044 kfree(store_work);
1045}
1046
1047static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
1048 struct attribute *attr, char *buff,
1049 size_t count)
1050{
1051 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1052 struct batadv_store_mesh_work *store_work;
1053
Sven Eckelmann1392f552019-03-03 18:02:59 +01001054 batadv_sysfs_deprecated(attr);
1055
Sven Eckelmann77d69d82016-06-13 07:41:30 +02001056 if (buff[count - 1] == '\n')
1057 buff[count - 1] = '\0';
1058
1059 if (strlen(buff) >= IFNAMSIZ) {
1060 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
1061 buff);
1062 return -EINVAL;
1063 }
1064
1065 store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
1066 if (!store_work)
1067 return -ENOMEM;
1068
1069 dev_hold(net_dev);
1070 INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1071 store_work->net_dev = net_dev;
1072 strlcpy(store_work->soft_iface_name, buff,
1073 sizeof(store_work->soft_iface_name));
1074
1075 queue_work(batadv_event_workqueue, &store_work->work);
1076
1077 return count;
1078}
1079
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001080static ssize_t batadv_show_iface_status(struct kobject *kobj,
1081 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001082{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001083 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001084 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001085 ssize_t length;
1086
Sven Eckelmann1392f552019-03-03 18:02:59 +01001087 batadv_sysfs_deprecated(attr);
1088
Sven Eckelmann56303d32012-06-05 22:31:31 +02001089 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +00001090 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001091 return 0;
1092
Marek Lindnere6c10f42011-02-18 12:33:20 +00001093 switch (hard_iface->if_status) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001094 case BATADV_IF_TO_BE_REMOVED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001095 length = sprintf(buff, "disabling\n");
1096 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001097 case BATADV_IF_INACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001098 length = sprintf(buff, "inactive\n");
1099 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001100 case BATADV_IF_ACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001101 length = sprintf(buff, "active\n");
1102 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001103 case BATADV_IF_TO_BE_ACTIVATED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001104 length = sprintf(buff, "enabling\n");
1105 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001106 case BATADV_IF_NOT_IN_USE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001107 default:
1108 length = sprintf(buff, "not in use\n");
1109 break;
1110 }
1111
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001112 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001113
1114 return length;
1115}
1116
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001117#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1118
1119/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001120 * batadv_store_throughput_override() - parse and store throughput override
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001121 * entered by the user
1122 * @kobj: kobject representing the private mesh sysfs directory
1123 * @attr: the batman-adv attribute the user is interacting with
1124 * @buff: the buffer containing the user data
1125 * @count: number of bytes in the buffer
1126 *
1127 * Return: 'count' on success or a negative error code in case of failure
1128 */
1129static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1130 struct attribute *attr,
1131 char *buff, size_t count)
1132{
1133 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1134 struct batadv_hard_iface *hard_iface;
Sven Eckelmann438b3d32019-03-03 19:25:26 +01001135 struct batadv_priv *bat_priv;
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001136 u32 tp_override;
1137 u32 old_tp_override;
1138 bool ret;
1139
Sven Eckelmann1392f552019-03-03 18:02:59 +01001140 batadv_sysfs_deprecated(attr);
1141
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001142 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1143 if (!hard_iface)
1144 return -EINVAL;
1145
1146 if (buff[count - 1] == '\n')
1147 buff[count - 1] = '\0';
1148
1149 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1150 &tp_override);
1151 if (!ret)
1152 return count;
1153
1154 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1155 if (old_tp_override == tp_override)
1156 goto out;
1157
Sven Eckelmannb9fd14c2018-08-31 16:46:47 +02001158 batadv_info(hard_iface->soft_iface,
1159 "%s: %s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1160 "throughput_override", net_dev->name,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001161 old_tp_override / 10, old_tp_override % 10,
1162 tp_override / 10, tp_override % 10);
1163
1164 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1165
Sven Eckelmann438b3d32019-03-03 19:25:26 +01001166 if (hard_iface->soft_iface) {
1167 bat_priv = netdev_priv(hard_iface->soft_iface);
1168 batadv_netlink_notify_hardif(bat_priv, hard_iface);
1169 }
Sven Eckelmann7e6f4612018-11-23 16:07:12 +01001170
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001171out:
1172 batadv_hardif_put(hard_iface);
1173 return count;
1174}
1175
1176static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1177 struct attribute *attr,
1178 char *buff)
1179{
1180 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1181 struct batadv_hard_iface *hard_iface;
1182 u32 tp_override;
1183
Sven Eckelmann1392f552019-03-03 18:02:59 +01001184 batadv_sysfs_deprecated(attr);
1185
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001186 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1187 if (!hard_iface)
1188 return -EINVAL;
1189
1190 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1191
1192 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1193 tp_override % 10);
1194}
1195
1196#endif
1197
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001198static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
Sven Eckelmann347c80f2012-06-03 22:19:07 +02001199 batadv_store_mesh_iface);
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001200static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
Linus Luessing7f136cd2016-01-16 16:40:11 +08001201#ifdef CONFIG_BATMAN_ADV_BATMAN_V
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001202BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001203 2 * BATADV_JITTER, INT_MAX, NULL);
Sven Eckelmann507b37c2016-09-01 10:25:12 +02001204static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001205 batadv_store_throughput_override);
Linus Luessing7f136cd2016-01-16 16:40:11 +08001206#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001207
Sven Eckelmannb4d66b82012-06-05 22:31:29 +02001208static struct batadv_attribute *batadv_batman_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001209 &batadv_attr_mesh_iface,
1210 &batadv_attr_iface_status,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001211#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1212 &batadv_attr_elp_interval,
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +08001213 &batadv_attr_throughput_override,
Linus Luessing7f136cd2016-01-16 16:40:11 +08001214#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001215 NULL,
1216};
1217
Sven Eckelmannff15c272017-12-02 19:51:53 +01001218/**
1219 * batadv_sysfs_add_hardif() - Add hard interface specific sysfs entries
1220 * @hardif_obj: address where to store the pointer to new sysfs folder
1221 * @dev: netdev struct of the hard interface
1222 *
1223 * Return: 0 on success or negative error number in case of failure
1224 */
Sven Eckelmann5853e222012-05-12 02:09:24 +02001225int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001226{
1227 struct kobject *hardif_kobject = &dev->dev.kobj;
Sven Eckelmannb4d66b82012-06-05 22:31:29 +02001228 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001229 int err;
1230
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001231 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1232 hardif_kobject);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001233
1234 if (!*hardif_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +02001235 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001236 BATADV_SYSFS_IF_BAT_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001237 goto out;
1238 }
1239
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001240 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001241 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1242 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +02001243 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +02001244 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +02001245 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001246 goto rem_attr;
1247 }
1248 }
1249
1250 return 0;
1251
1252rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +02001253 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001254 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1255out:
1256 return -ENOMEM;
1257}
1258
Sven Eckelmannff15c272017-12-02 19:51:53 +01001259/**
1260 * batadv_sysfs_del_hardif() - Remove hard interface specific sysfs entries
1261 * @hardif_obj: address to the pointer to which stores batman-adv sysfs folder
1262 * of the hard interface
1263 */
Sven Eckelmann5853e222012-05-12 02:09:24 +02001264void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001265{
Sven Eckelmannf4acb102016-06-13 07:41:32 +02001266 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1267 kobject_del(*hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001268 kobject_put(*hardif_obj);
1269 *hardif_obj = NULL;
1270}