Merge branch 'akpm' (patches from Andrew)
[linux-2.6-block.git] / net / batman-adv / sysfs.c
CommitLineData
7db7d9f3 1// SPDX-License-Identifier: GPL-2.0
7a79d717 2/* Copyright (C) 2010-2019 B.A.T.M.A.N. contributors:
c6c8fea2
SE
3 *
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
ebf38fb7 16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
17 */
18
b706b13b 19#include "sysfs.h"
1e2c2a4f
SE
20#include "main.h"
21
22#include <linux/atomic.h>
23#include <linux/compiler.h>
24#include <linux/device.h>
25#include <linux/errno.h>
b92b94ac 26#include <linux/gfp.h>
1e2c2a4f
SE
27#include <linux/if.h>
28#include <linux/if_vlan.h>
29#include <linux/kernel.h>
8cfba951 30#include <linux/kobject.h>
fcafa5e7 31#include <linux/kref.h>
1e2c2a4f
SE
32#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>
1e2c2a4f
SE
38#include <linux/stddef.h>
39#include <linux/string.h>
40#include <linux/stringify.h>
77d69d8c 41#include <linux/workqueue.h>
fec149f5 42#include <uapi/linux/batadv_packet.h>
e2d0d35b 43#include <uapi/linux/batman_adv.h>
1e2c2a4f 44
fcafa5e7 45#include "bridge_loop_avoidance.h"
33af49ad 46#include "distributed-arp-table.h"
1e2c2a4f
SE
47#include "gateway_client.h"
48#include "gateway_common.h"
c6c8fea2 49#include "hard-interface.h"
ba412080 50#include "log.h"
7e6f461e 51#include "netlink.h"
1e2c2a4f 52#include "network-coding.h"
90f4435d 53#include "soft-interface.h"
c6c8fea2 54
0ff9b86f 55static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
3d222bba
SE
56{
57 struct device *dev = container_of(obj->parent, struct device, kobj);
f138694b 58
3d222bba
SE
59 return to_net_dev(dev);
60}
61
56303d34 62static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
3d222bba 63{
0ff9b86f 64 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
f138694b 65
3d222bba
SE
66 return netdev_priv(net_dev);
67}
c6c8fea2 68
90f4435d 69/**
7e9a8c2c 70 * batadv_vlan_kobj_to_batpriv() - convert a vlan kobj in the associated batpriv
90f4435d
AQ
71 * @obj: kobject to covert
72 *
62fe710f 73 * Return: the associated batadv_priv struct.
90f4435d
AQ
74 */
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/**
7e9a8c2c 90 * batadv_kobj_to_vlan() - convert a kobj in the associated softif_vlan struct
7afcbbef 91 * @bat_priv: the bat priv with all the soft interface information
90f4435d
AQ
92 * @obj: kobject to covert
93 *
62fe710f 94 * Return: the associated softif_vlan struct if found, NULL otherwise.
90f4435d
AQ
95 */
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
6be4d30c 106 if (!kref_get_unless_zero(&vlan_tmp->refcount))
90f4435d
AQ
107 continue;
108
109 vlan = vlan_tmp;
110 break;
111 }
112 rcu_read_unlock();
113
114 return vlan;
115}
116
347c80f0
SE
117#define BATADV_UEV_TYPE_VAR "BATTYPE="
118#define BATADV_UEV_ACTION_VAR "BATACTION="
119#define BATADV_UEV_DATA_VAR "BATDATA="
c6bda689 120
0ff9b86f 121static char *batadv_uev_action_str[] = {
c6bda689
AQ
122 "add",
123 "del",
cd9c7bfb
SW
124 "change",
125 "loopdetect",
c6bda689
AQ
126};
127
0ff9b86f 128static char *batadv_uev_type_str[] = {
cd9c7bfb
SW
129 "gw",
130 "bla",
c6bda689
AQ
131};
132
90f4435d
AQ
133/* 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, \
2b64df20 140}
90f4435d 141
c6c8fea2 142/* Use this, if you have customized show and store functions */
347c80f0 143#define BATADV_ATTR(_name, _mode, _show, _store) \
b4d66b87 144struct batadv_attribute batadv_attr_##_name = { \
347c80f0
SE
145 .attr = {.name = __stringify(_name), \
146 .mode = _mode }, \
147 .show = _show, \
148 .store = _store, \
2b64df20 149}
c6c8fea2 150
347c80f0 151#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
0ff9b86f
SE
152ssize_t batadv_store_##_name(struct kobject *kobj, \
153 struct attribute *attr, char *buff, \
154 size_t count) \
c6c8fea2 155{ \
0ff9b86f 156 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
56303d34 157 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
7e6f461e
SE
158 ssize_t length; \
159 \
160 length = __batadv_store_bool_attr(buff, count, _post_func, attr,\
161 &bat_priv->_name, net_dev); \
f138694b 162 \
7e6f461e
SE
163 batadv_netlink_notify_mesh(bat_priv); \
164 \
165 return length; \
c6c8fea2
SE
166}
167
347c80f0 168#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
0ff9b86f
SE
169ssize_t batadv_show_##_name(struct kobject *kobj, \
170 struct attribute *attr, char *buff) \
c6c8fea2 171{ \
56303d34 172 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
f138694b 173 \
c6c8fea2
SE
174 return sprintf(buff, "%s\n", \
175 atomic_read(&bat_priv->_name) == 0 ? \
176 "disabled" : "enabled"); \
177} \
178
f245c38b 179/* Use this, if you are going to turn a [name] in the soft-interface
9cfc7bd6
SE
180 * (bat_priv) on or off
181 */
347c80f0
SE
182#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)
c6c8fea2 187
6f70eb75 188#define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
0ff9b86f
SE
189ssize_t batadv_store_##_name(struct kobject *kobj, \
190 struct attribute *attr, char *buff, \
191 size_t count) \
c6c8fea2 192{ \
0ff9b86f 193 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
56303d34 194 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
7e6f461e 195 ssize_t length; \
f138694b 196 \
7e6f461e
SE
197 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; \
c6c8fea2
SE
205}
206
6f70eb75 207#define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
0ff9b86f
SE
208ssize_t batadv_show_##_name(struct kobject *kobj, \
209 struct attribute *attr, char *buff) \
c6c8fea2 210{ \
56303d34 211 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
f138694b 212 \
6f70eb75 213 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
c6c8fea2
SE
214} \
215
f245c38b 216/* Use this, if you are going to set [name] in the soft-interface
9cfc7bd6
SE
217 * (bat_priv) to an unsigned integer value
218 */
6f70eb75
AQ
219#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) \
347c80f0
SE
222 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
223 batadv_store_##_name)
c6c8fea2 224
90f4435d
AQ
225#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); \
f138694b 236 \
7e6f461e
SE
237 if (vlan->vid) \
238 batadv_netlink_notify_vlan(bat_priv, vlan); \
239 else \
240 batadv_netlink_notify_mesh(bat_priv); \
241 \
9c3bf081 242 batadv_softif_vlan_put(vlan); \
90f4435d
AQ
243 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"); \
f138694b 256 \
9c3bf081 257 batadv_softif_vlan_put(vlan); \
90f4435d
AQ
258 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)
0744ff8f
LL
267
268#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; \
7e6f461e 275 struct batadv_priv *bat_priv; \
0744ff8f
LL
276 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, \
a25bab9d
SE
284 &hard_iface->_var, \
285 hard_iface->soft_iface, \
286 net_dev); \
0744ff8f 287 \
7e6f461e
SE
288 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 \
0744ff8f
LL
293 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)
c6c8fea2 324
0ff9b86f
SE
325static int batadv_store_bool_attr(char *buff, size_t count,
326 struct net_device *net_dev,
9e728e84
SW
327 const char *attr_name, atomic_t *attr,
328 bool *changed)
c6c8fea2
SE
329{
330 int enabled = -1;
331
9e728e84
SW
332 *changed = false;
333
c6c8fea2
SE
334 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) {
3e34819e
SE
348 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
349 attr_name, buff);
c6c8fea2
SE
350 return -EINVAL;
351 }
352
353 if (atomic_read(attr) == enabled)
354 return count;
355
3e34819e
SE
356 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");
c6c8fea2 359
9e728e84
SW
360 *changed = true;
361
95c96174 362 atomic_set(attr, (unsigned int)enabled);
c6c8fea2
SE
363 return count;
364}
365
0ff9b86f
SE
366static 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)
c6c8fea2 371{
9e728e84 372 bool changed;
c6c8fea2
SE
373 int ret;
374
0ff9b86f 375 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
9e728e84
SW
376 attr_store, &changed);
377 if (post_func && changed)
c6c8fea2
SE
378 post_func(net_dev);
379
380 return ret;
381}
382
0ff9b86f
SE
383static int batadv_store_uint_attr(const char *buff, size_t count,
384 struct net_device *net_dev,
a25bab9d 385 struct net_device *slave_dev,
0ff9b86f
SE
386 const char *attr_name,
387 unsigned int min, unsigned int max,
388 atomic_t *attr)
c6c8fea2 389{
a25bab9d 390 char ifname[IFNAMSIZ + 3] = "";
c6c8fea2
SE
391 unsigned long uint_val;
392 int ret;
393
25a92b13 394 ret = kstrtoul(buff, 10, &uint_val);
c6c8fea2 395 if (ret) {
3e34819e
SE
396 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
397 attr_name, buff);
c6c8fea2
SE
398 return -EINVAL;
399 }
400
401 if (uint_val < min) {
3e34819e
SE
402 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
403 attr_name, uint_val, min);
c6c8fea2
SE
404 return -EINVAL;
405 }
406
407 if (uint_val > max) {
3e34819e
SE
408 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
409 attr_name, uint_val, max);
c6c8fea2
SE
410 return -EINVAL;
411 }
412
413 if (atomic_read(attr) == uint_val)
414 return count;
415
a25bab9d
SE
416 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);
c6c8fea2
SE
421
422 atomic_set(attr, uint_val);
423 return count;
424}
425
c149ca72
AQ
426static 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,
a25bab9d
SE
431 struct net_device *net_dev,
432 struct net_device *slave_dev)
c6c8fea2
SE
433{
434 int ret;
435
a25bab9d
SE
436 ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
437 attr->name, min, max, attr_store);
c6c8fea2
SE
438 if (post_func && ret)
439 post_func(net_dev);
440
441 return ret;
442}
443
0ff9b86f
SE
444static ssize_t batadv_show_bat_algo(struct kobject *kobj,
445 struct attribute *attr, char *buff)
ea3d2fd1 446{
56303d34 447 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
f138694b 448
29824a55 449 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
ea3d2fd1
ML
450}
451
4e820e72 452static void batadv_post_gw_reselect(struct net_device *net_dev)
c6c8fea2 453{
56303d34 454 struct batadv_priv *bat_priv = netdev_priv(net_dev);
f138694b 455
4e820e72 456 batadv_gw_reselect(bat_priv);
c6c8fea2
SE
457}
458
0ff9b86f
SE
459static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
460 char *buff)
c6c8fea2 461{
56303d34 462 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
c6c8fea2
SE
463 int bytes_written;
464
a8d8d1de
AQ
465 /* 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
3a24a63e 472 switch (atomic_read(&bat_priv->gw.mode)) {
cd646ab1 473 case BATADV_GW_MODE_CLIENT:
97ea4ba1
SE
474 bytes_written = sprintf(buff, "%s\n",
475 BATADV_GW_MODE_CLIENT_NAME);
c6c8fea2 476 break;
cd646ab1 477 case BATADV_GW_MODE_SERVER:
97ea4ba1
SE
478 bytes_written = sprintf(buff, "%s\n",
479 BATADV_GW_MODE_SERVER_NAME);
c6c8fea2
SE
480 break;
481 default:
97ea4ba1
SE
482 bytes_written = sprintf(buff, "%s\n",
483 BATADV_GW_MODE_OFF_NAME);
c6c8fea2
SE
484 break;
485 }
486
487 return bytes_written;
488}
489
0ff9b86f
SE
490static ssize_t batadv_store_gw_mode(struct kobject *kobj,
491 struct attribute *attr, char *buff,
492 size_t count)
c6c8fea2 493{
0ff9b86f 494 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 495 struct batadv_priv *bat_priv = netdev_priv(net_dev);
c6c8fea2
SE
496 char *curr_gw_mode_str;
497 int gw_mode_tmp = -1;
498
a8d8d1de
AQ
499 /* 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
c6c8fea2
SE
506 if (buff[count - 1] == '\n')
507 buff[count - 1] = '\0';
508
97ea4ba1
SE
509 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
510 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
cd646ab1 511 gw_mode_tmp = BATADV_GW_MODE_OFF;
c6c8fea2 512
97ea4ba1
SE
513 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
514 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
cd646ab1 515 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
c6c8fea2 516
97ea4ba1
SE
517 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
518 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
cd646ab1 519 gw_mode_tmp = BATADV_GW_MODE_SERVER;
c6c8fea2
SE
520
521 if (gw_mode_tmp < 0) {
3e34819e
SE
522 batadv_info(net_dev,
523 "Invalid parameter for 'gw mode' setting received: %s\n",
524 buff);
c6c8fea2
SE
525 return -EINVAL;
526 }
527
3a24a63e 528 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
c6c8fea2
SE
529 return count;
530
3a24a63e 531 switch (atomic_read(&bat_priv->gw.mode)) {
cd646ab1 532 case BATADV_GW_MODE_CLIENT:
97ea4ba1 533 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
c6c8fea2 534 break;
cd646ab1 535 case BATADV_GW_MODE_SERVER:
97ea4ba1 536 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
c6c8fea2
SE
537 break;
538 default:
97ea4ba1 539 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
c6c8fea2
SE
540 break;
541 }
542
3e34819e
SE
543 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
544 curr_gw_mode_str, buff);
c6c8fea2 545
f3163181
AQ
546 /* 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 */
4e820e72 555 batadv_gw_reselect(bat_priv);
c6eaa3f0
AQ
556 /* always call batadv_gw_check_client_stop() before changing the gateway
557 * state
558 */
559 batadv_gw_check_client_stop(bat_priv);
3a24a63e 560 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
414254e3 561 batadv_gw_tvlv_container_update(bat_priv);
7e6f461e
SE
562
563 batadv_netlink_notify_mesh(bat_priv);
564
c6c8fea2
SE
565 return count;
566}
567
08686943
AQ
568static 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
a8d8d1de
AQ
573 /* 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
08686943
AQ
580 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);
7e6f461e 591 ssize_t length;
08686943 592
a8d8d1de
AQ
593 /* 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
08686943
AQ
600 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
7e6f461e
SE
607 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;
08686943
AQ
615}
616
0ff9b86f
SE
617static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
618 struct attribute *attr, char *buff)
c6c8fea2 619{
56303d34 620 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
6b5e971a 621 u32 down, up;
414254e3
ML
622
623 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);
c6c8fea2
SE
628}
629
0ff9b86f
SE
630static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
631 struct attribute *attr, char *buff,
632 size_t count)
c6c8fea2 633{
7e6f461e 634 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
0ff9b86f 635 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
7e6f461e 636 ssize_t length;
c6c8fea2
SE
637
638 if (buff[count - 1] == '\n')
639 buff[count - 1] = '\0';
640
7e6f461e
SE
641 length = batadv_gw_bandwidth_set(net_dev, buff, count);
642
643 batadv_netlink_notify_mesh(bat_priv);
644
645 return length;
c6c8fea2
SE
646}
647
c42edfe3 648/**
7e9a8c2c 649 * batadv_show_isolation_mark() - print the current isolation mark/mask
c42edfe3
AQ
650 * @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 *
62fe710f 654 * Return: the number of bytes written into 'buff' on success or a negative
c42edfe3
AQ
655 * 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/**
7e9a8c2c
SE
667 * batadv_store_isolation_mark() - parse and store the isolation mark/mask
668 * entered by the user
c42edfe3
AQ
669 * @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 *
62fe710f 674 * Return: 'count' on success or a negative error code in case of failure
c42edfe3
AQ
675 */
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);
6b5e971a 682 u32 mark, mask;
c42edfe3
AQ
683 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
7e6f461e
SE
713 batadv_netlink_notify_mesh(bat_priv);
714
c42edfe3
AQ
715 return count;
716}
717
507b37cf
SE
718BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
719BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
7a5cc242 720#ifdef CONFIG_BATMAN_ADV_BLA
507b37cf 721BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
7a5cc242 722#endif
33af49ad 723#ifdef CONFIG_BATMAN_ADV_DAT
507b37cf 724BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
33af49ad 725#endif
507b37cf
SE
726BATADV_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,
08686943 734 batadv_store_gw_sel_class);
507b37cf 735static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
347c80f0 736 batadv_store_gw_bwidth);
1d8ab8d3 737#ifdef CONFIG_BATMAN_ADV_MCAST
507b37cf 738BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
1d8ab8d3 739#endif
c6c8fea2 740#ifdef CONFIG_BATMAN_ADV_DEBUG
507b37cf 741BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
c6c8fea2 742#endif
d353d8d4 743#ifdef CONFIG_BATMAN_ADV_NC
507b37cf 744BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
d353d8d4 745#endif
507b37cf
SE
746static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
747 batadv_store_isolation_mark);
c6c8fea2 748
b4d66b87 749static struct batadv_attribute *batadv_mesh_attrs[] = {
0ff9b86f
SE
750 &batadv_attr_aggregated_ogms,
751 &batadv_attr_bonding,
7a5cc242 752#ifdef CONFIG_BATMAN_ADV_BLA
0ff9b86f 753 &batadv_attr_bridge_loop_avoidance,
33af49ad
AQ
754#endif
755#ifdef CONFIG_BATMAN_ADV_DAT
756 &batadv_attr_distributed_arp_table,
1d8ab8d3
LL
757#endif
758#ifdef CONFIG_BATMAN_ADV_MCAST
759 &batadv_attr_multicast_mode,
7a5cc242 760#endif
0ff9b86f 761 &batadv_attr_fragmentation,
0ff9b86f
SE
762 &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,
c6c8fea2 768#ifdef CONFIG_BATMAN_ADV_DEBUG
0ff9b86f 769 &batadv_attr_log_level,
d353d8d4
MH
770#endif
771#ifdef CONFIG_BATMAN_ADV_NC
772 &batadv_attr_network_coding,
c6c8fea2 773#endif
c42edfe3 774 &batadv_attr_isolation_mark,
c6c8fea2
SE
775 NULL,
776};
777
507b37cf 778BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
b8cbd81d 779
f34ac9d4 780/* array of vlan specific sysfs attributes */
90f4435d 781static struct batadv_attribute *batadv_vlan_attrs[] = {
b8cbd81d 782 &batadv_attr_vlan_ap_isolation,
90f4435d
AQ
783 NULL,
784};
785
ff15c27c
SE
786/**
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 */
5853e22c 792int batadv_sysfs_add_meshif(struct net_device *dev)
c6c8fea2
SE
793{
794 struct kobject *batif_kobject = &dev->dev.kobj;
56303d34 795 struct batadv_priv *bat_priv = netdev_priv(dev);
b4d66b87 796 struct batadv_attribute **bat_attr;
c6c8fea2
SE
797 int err;
798
036cbfeb 799 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
c6c8fea2
SE
800 batif_kobject);
801 if (!bat_priv->mesh_obj) {
3e34819e 802 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
036cbfeb 803 BATADV_SYSFS_IF_MESH_SUBDIR);
c6c8fea2
SE
804 goto out;
805 }
806
0ff9b86f 807 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
c6c8fea2
SE
808 err = sysfs_create_file(bat_priv->mesh_obj,
809 &((*bat_attr)->attr));
810 if (err) {
3e34819e 811 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
036cbfeb 812 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
3e34819e 813 ((*bat_attr)->attr).name);
c6c8fea2
SE
814 goto rem_attr;
815 }
816 }
817
818 return 0;
819
820rem_attr:
0ff9b86f 821 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
c6c8fea2
SE
822 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
823
f4acb108
SE
824 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
825 kobject_del(bat_priv->mesh_obj);
c6c8fea2
SE
826 kobject_put(bat_priv->mesh_obj);
827 bat_priv->mesh_obj = NULL;
828out:
829 return -ENOMEM;
830}
831
ff15c27c
SE
832/**
833 * batadv_sysfs_del_meshif() - Remove soft interface specific sysfs entries
834 * @dev: netdev struct of the soft interface
835 */
5853e22c 836void batadv_sysfs_del_meshif(struct net_device *dev)
c6c8fea2 837{
56303d34 838 struct batadv_priv *bat_priv = netdev_priv(dev);
b4d66b87 839 struct batadv_attribute **bat_attr;
c6c8fea2 840
0ff9b86f 841 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
c6c8fea2
SE
842 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
843
f4acb108
SE
844 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
845 kobject_del(bat_priv->mesh_obj);
c6c8fea2
SE
846 kobject_put(bat_priv->mesh_obj);
847 bat_priv->mesh_obj = NULL;
848}
849
90f4435d 850/**
7e9a8c2c 851 * batadv_sysfs_add_vlan() - add all the needed sysfs objects for the new vlan
90f4435d
AQ
852 * @dev: netdev of the mesh interface
853 * @vlan: private data of the newly added VLAN interface
854 *
62fe710f 855 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
90f4435d
AQ
856 */
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
f4acb108
SE
901 if (vlan->kobj != bat_priv->mesh_obj) {
902 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
903 kobject_del(vlan->kobj);
904 }
90f4435d
AQ
905 kobject_put(vlan->kobj);
906 vlan->kobj = NULL;
907out:
908 return -ENOMEM;
909}
910
911/**
7e9a8c2c 912 * batadv_sysfs_del_vlan() - remove all the sysfs objects for a given VLAN
90f4435d
AQ
913 * @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
f4acb108
SE
924 if (vlan->kobj != bat_priv->mesh_obj) {
925 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
926 kobject_del(vlan->kobj);
927 }
90f4435d
AQ
928 kobject_put(vlan->kobj);
929 vlan->kobj = NULL;
930}
931
0ff9b86f
SE
932static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
933 struct attribute *attr, char *buff)
c6c8fea2 934{
0ff9b86f 935 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 936 struct batadv_hard_iface *hard_iface;
c6c8fea2 937 ssize_t length;
e9a4f295 938 const char *ifname;
c6c8fea2 939
56303d34 940 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 941 if (!hard_iface)
c6c8fea2
SE
942 return 0;
943
e9a4f295
SE
944 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);
c6c8fea2 950
82047ad7 951 batadv_hardif_put(hard_iface);
c6c8fea2
SE
952
953 return length;
954}
955
77d69d8c 956/**
7e9a8c2c 957 * batadv_store_mesh_iface_finish() - store new hardif mesh_iface state
77d69d8c
SE
958 * @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])
c6c8fea2 968{
2cd45a06 969 struct net *net = dev_net(net_dev);
56303d34 970 struct batadv_hard_iface *hard_iface;
77d69d8c
SE
971 int status_tmp;
972 int ret = 0;
973
974 ASSERT_RTNL();
c6c8fea2 975
56303d34 976 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 977 if (!hard_iface)
77d69d8c 978 return 0;
c6c8fea2 979
77d69d8c 980 if (strncmp(ifname, "none", 4) == 0)
e9a4f295 981 status_tmp = BATADV_IF_NOT_IN_USE;
c6c8fea2 982 else
e9a4f295 983 status_tmp = BATADV_IF_I_WANT_YOU;
c6c8fea2 984
e6c10f43
ML
985 if (hard_iface->if_status == status_tmp)
986 goto out;
987
825ffe1f
SE
988 if (hard_iface->soft_iface &&
989 strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)
ed75ccbe 990 goto out;
c6c8fea2 991
e9a4f295 992 if (status_tmp == BATADV_IF_NOT_IN_USE) {
a15fd361
SE
993 batadv_hardif_disable_interface(hard_iface,
994 BATADV_IF_CLEANUP_AUTO);
77d69d8c 995 goto out;
c6c8fea2
SE
996 }
997
998 /* if the interface already is in use */
e9a4f295 999 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
a15fd361
SE
1000 batadv_hardif_disable_interface(hard_iface,
1001 BATADV_IF_CLEANUP_AUTO);
c6c8fea2 1002
77d69d8c 1003 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
ed75ccbe 1004out:
82047ad7 1005 batadv_hardif_put(hard_iface);
c6c8fea2
SE
1006 return ret;
1007}
1008
77d69d8c 1009/**
7e9a8c2c 1010 * batadv_store_mesh_iface_work() - store new hardif mesh_iface state
77d69d8c
SE
1011 * @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
0ff9b86f
SE
1068static ssize_t batadv_show_iface_status(struct kobject *kobj,
1069 struct attribute *attr, char *buff)
c6c8fea2 1070{
0ff9b86f 1071 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 1072 struct batadv_hard_iface *hard_iface;
c6c8fea2
SE
1073 ssize_t length;
1074
56303d34 1075 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 1076 if (!hard_iface)
c6c8fea2
SE
1077 return 0;
1078
e6c10f43 1079 switch (hard_iface->if_status) {
e9a4f295 1080 case BATADV_IF_TO_BE_REMOVED:
c6c8fea2
SE
1081 length = sprintf(buff, "disabling\n");
1082 break;
e9a4f295 1083 case BATADV_IF_INACTIVE:
c6c8fea2
SE
1084 length = sprintf(buff, "inactive\n");
1085 break;
e9a4f295 1086 case BATADV_IF_ACTIVE:
c6c8fea2
SE
1087 length = sprintf(buff, "active\n");
1088 break;
e9a4f295 1089 case BATADV_IF_TO_BE_ACTIVATED:
c6c8fea2
SE
1090 length = sprintf(buff, "enabling\n");
1091 break;
e9a4f295 1092 case BATADV_IF_NOT_IN_USE:
c6c8fea2
SE
1093 default:
1094 length = sprintf(buff, "not in use\n");
1095 break;
1096 }
1097
82047ad7 1098 batadv_hardif_put(hard_iface);
c6c8fea2
SE
1099
1100 return length;
1101}
1102
0b5ecc68
AQ
1103#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1104
1105/**
7e9a8c2c 1106 * batadv_store_throughput_override() - parse and store throughput override
0b5ecc68
AQ
1107 * 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{
7e6f461e 1119 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
0b5ecc68
AQ
1120 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
b9fd14c2
SE
1142 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,
0b5ecc68
AQ
1145 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
7e6f461e
SE
1150 batadv_netlink_notify_hardif(bat_priv, hard_iface);
1151
0b5ecc68
AQ
1152out:
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
507b37cf 1177static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
347c80f0 1178 batadv_store_mesh_iface);
507b37cf 1179static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
7f136cd4 1180#ifdef CONFIG_BATMAN_ADV_BATMAN_V
507b37cf 1181BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
7f136cd4 1182 2 * BATADV_JITTER, INT_MAX, NULL);
507b37cf 1183static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
0b5ecc68 1184 batadv_store_throughput_override);
7f136cd4 1185#endif
c6c8fea2 1186
b4d66b87 1187static struct batadv_attribute *batadv_batman_attrs[] = {
0ff9b86f
SE
1188 &batadv_attr_mesh_iface,
1189 &batadv_attr_iface_status,
7f136cd4
LL
1190#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1191 &batadv_attr_elp_interval,
0b5ecc68 1192 &batadv_attr_throughput_override,
7f136cd4 1193#endif
c6c8fea2
SE
1194 NULL,
1195};
1196
ff15c27c
SE
1197/**
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 */
5853e22c 1204int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
c6c8fea2
SE
1205{
1206 struct kobject *hardif_kobject = &dev->dev.kobj;
b4d66b87 1207 struct batadv_attribute **bat_attr;
c6c8fea2
SE
1208 int err;
1209
036cbfeb
SE
1210 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1211 hardif_kobject);
c6c8fea2
SE
1212
1213 if (!*hardif_obj) {
3e34819e 1214 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
036cbfeb 1215 BATADV_SYSFS_IF_BAT_SUBDIR);
c6c8fea2
SE
1216 goto out;
1217 }
1218
0ff9b86f 1219 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
c6c8fea2
SE
1220 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1221 if (err) {
3e34819e 1222 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
036cbfeb 1223 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
3e34819e 1224 ((*bat_attr)->attr).name);
c6c8fea2
SE
1225 goto rem_attr;
1226 }
1227 }
1228
1229 return 0;
1230
1231rem_attr:
0ff9b86f 1232 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
c6c8fea2
SE
1233 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1234out:
1235 return -ENOMEM;
1236}
1237
ff15c27c
SE
1238/**
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 */
5853e22c 1243void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
c6c8fea2 1244{
f4acb108
SE
1245 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1246 kobject_del(*hardif_obj);
c6c8fea2
SE
1247 kobject_put(*hardif_obj);
1248 *hardif_obj = NULL;
1249}
c6bda689 1250
ff15c27c
SE
1251/**
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 */
56303d34 1261int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
39c75a51 1262 enum batadv_uev_action action, const char *data)
c6bda689 1263{
5346c35e 1264 int ret = -ENOMEM;
c6bda689
AQ
1265 struct kobject *bat_kobj;
1266 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1267
736292c2 1268 bat_kobj = &bat_priv->soft_iface->dev.kobj;
c6bda689 1269
b98fe24c
HS
1270 uevent_env[0] = kasprintf(GFP_ATOMIC,
1271 "%s%s", BATADV_UEV_TYPE_VAR,
1272 batadv_uev_type_str[type]);
c6bda689
AQ
1273 if (!uevent_env[0])
1274 goto out;
1275
b98fe24c
HS
1276 uevent_env[1] = kasprintf(GFP_ATOMIC,
1277 "%s%s", BATADV_UEV_ACTION_VAR,
1278 batadv_uev_action_str[action]);
c6bda689
AQ
1279 if (!uevent_env[1])
1280 goto out;
1281
c6bda689 1282 /* If the event is DEL, ignore the data field */
39c75a51 1283 if (action != BATADV_UEV_DEL) {
b98fe24c
HS
1284 uevent_env[2] = kasprintf(GFP_ATOMIC,
1285 "%s%s", BATADV_UEV_DATA_VAR, data);
c6bda689
AQ
1286 if (!uevent_env[2])
1287 goto out;
c6bda689
AQ
1288 }
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
c6bda689 1296 if (ret)
39c75a51 1297 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1298 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
0ff9b86f
SE
1299 batadv_uev_type_str[type],
1300 batadv_uev_action_str[action],
39c75a51 1301 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
c6bda689
AQ
1302 return ret;
1303}