Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2010-2019 B.A.T.M.A.N. contributors:
3 *
4 * Marek Lindner
5 */
6
7#include "sysfs.h"
8#include "main.h"
9
10#include <asm/current.h>
11#include <linux/atomic.h>
12#include <linux/compiler.h>
13#include <linux/device.h>
14#include <linux/errno.h>
15#include <linux/gfp.h>
16#include <linux/if.h>
17#include <linux/if_vlan.h>
18#include <linux/kernel.h>
19#include <linux/kobject.h>
20#include <linux/kref.h>
21#include <linux/netdevice.h>
22#include <linux/printk.h>
23#include <linux/rculist.h>
24#include <linux/rcupdate.h>
25#include <linux/rtnetlink.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
28#include <linux/stddef.h>
29#include <linux/string.h>
30#include <linux/stringify.h>
31#include <linux/workqueue.h>
32#include <uapi/linux/batadv_packet.h>
33#include <uapi/linux/batman_adv.h>
34
35#include "bridge_loop_avoidance.h"
36#include "distributed-arp-table.h"
37#include "gateway_client.h"
38#include "gateway_common.h"
39#include "hard-interface.h"
40#include "log.h"
41#include "netlink.h"
42#include "network-coding.h"
43#include "soft-interface.h"
44
45/**
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
55static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
56{
57 struct device *dev = container_of(obj->parent, struct device, kobj);
58
59 return to_net_dev(dev);
60}
61
62static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
63{
64 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
65
66 return netdev_priv(net_dev);
67}
68
69/**
70 * batadv_vlan_kobj_to_batpriv() - convert a vlan kobj in the associated batpriv
71 * @obj: kobject to covert
72 *
73 * Return: the associated batadv_priv struct.
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/**
90 * batadv_kobj_to_vlan() - convert a kobj in the associated softif_vlan struct
91 * @bat_priv: the bat priv with all the soft interface information
92 * @obj: kobject to covert
93 *
94 * Return: the associated softif_vlan struct if found, NULL otherwise.
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
106 if (!kref_get_unless_zero(&vlan_tmp->refcount))
107 continue;
108
109 vlan = vlan_tmp;
110 break;
111 }
112 rcu_read_unlock();
113
114 return vlan;
115}
116
117/* 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, \
124}
125
126/* Use this, if you have customized show and store functions */
127#define BATADV_ATTR(_name, _mode, _show, _store) \
128struct batadv_attribute batadv_attr_##_name = { \
129 .attr = {.name = __stringify(_name), \
130 .mode = _mode }, \
131 .show = _show, \
132 .store = _store, \
133}
134
135#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
136ssize_t batadv_store_##_name(struct kobject *kobj, \
137 struct attribute *attr, char *buff, \
138 size_t count) \
139{ \
140 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
141 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
142 ssize_t length; \
143 \
144 batadv_sysfs_deprecated(attr); \
145 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; \
151}
152
153#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
154ssize_t batadv_show_##_name(struct kobject *kobj, \
155 struct attribute *attr, char *buff) \
156{ \
157 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
158 \
159 batadv_sysfs_deprecated(attr); \
160 return sprintf(buff, "%s\n", \
161 atomic_read(&bat_priv->_name) == 0 ? \
162 "disabled" : "enabled"); \
163} \
164
165/* Use this, if you are going to turn a [name] in the soft-interface
166 * (bat_priv) on or off
167 */
168#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)
173
174#define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
175ssize_t batadv_store_##_name(struct kobject *kobj, \
176 struct attribute *attr, char *buff, \
177 size_t count) \
178{ \
179 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
180 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
181 ssize_t length; \
182 \
183 batadv_sysfs_deprecated(attr); \
184 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; \
192}
193
194#define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
195ssize_t batadv_show_##_name(struct kobject *kobj, \
196 struct attribute *attr, char *buff) \
197{ \
198 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
199 \
200 batadv_sysfs_deprecated(attr); \
201 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
202} \
203
204/* Use this, if you are going to set [name] in the soft-interface
205 * (bat_priv) to an unsigned integer value
206 */
207#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) \
210 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
211 batadv_store_##_name)
212
213#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); \
224 \
225 batadv_sysfs_deprecated(attr); \
226 if (vlan->vid) \
227 batadv_netlink_notify_vlan(bat_priv, vlan); \
228 else \
229 batadv_netlink_notify_mesh(bat_priv); \
230 \
231 batadv_softif_vlan_put(vlan); \
232 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"); \
245 \
246 batadv_sysfs_deprecated(attr); \
247 batadv_softif_vlan_put(vlan); \
248 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)
257
258#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; \
265 struct batadv_priv *bat_priv; \
266 ssize_t length; \
267 \
268 batadv_sysfs_deprecated(attr); \
269 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, \
275 &hard_iface->_var, \
276 hard_iface->soft_iface, \
277 net_dev); \
278 \
279 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 \
284 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 \
296 batadv_sysfs_deprecated(attr); \
297 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
317static int batadv_store_bool_attr(char *buff, size_t count,
318 struct net_device *net_dev,
319 const char *attr_name, atomic_t *attr,
320 bool *changed)
321{
322 int enabled = -1;
323
324 *changed = false;
325
326 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) {
340 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
341 attr_name, buff);
342 return -EINVAL;
343 }
344
345 if (atomic_read(attr) == enabled)
346 return count;
347
348 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");
351
352 *changed = true;
353
354 atomic_set(attr, (unsigned int)enabled);
355 return count;
356}
357
358static 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)
363{
364 bool changed;
365 int ret;
366
367 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
368 attr_store, &changed);
369 if (post_func && changed)
370 post_func(net_dev);
371
372 return ret;
373}
374
375static int batadv_store_uint_attr(const char *buff, size_t count,
376 struct net_device *net_dev,
377 struct net_device *slave_dev,
378 const char *attr_name,
379 unsigned int min, unsigned int max,
380 atomic_t *attr)
381{
382 char ifname[IFNAMSIZ + 3] = "";
383 unsigned long uint_val;
384 int ret;
385
386 ret = kstrtoul(buff, 10, &uint_val);
387 if (ret) {
388 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
389 attr_name, buff);
390 return -EINVAL;
391 }
392
393 if (uint_val < min) {
394 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
395 attr_name, uint_val, min);
396 return -EINVAL;
397 }
398
399 if (uint_val > max) {
400 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
401 attr_name, uint_val, max);
402 return -EINVAL;
403 }
404
405 if (atomic_read(attr) == uint_val)
406 return count;
407
408 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);
413
414 atomic_set(attr, uint_val);
415 return count;
416}
417
418static 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,
423 struct net_device *net_dev,
424 struct net_device *slave_dev)
425{
426 int ret;
427
428 ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
429 attr->name, min, max, attr_store);
430 if (post_func && ret)
431 post_func(net_dev);
432
433 return ret;
434}
435
436static ssize_t batadv_show_bat_algo(struct kobject *kobj,
437 struct attribute *attr, char *buff)
438{
439 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
440
441 batadv_sysfs_deprecated(attr);
442 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
443}
444
445static void batadv_post_gw_reselect(struct net_device *net_dev)
446{
447 struct batadv_priv *bat_priv = netdev_priv(net_dev);
448
449 batadv_gw_reselect(bat_priv);
450}
451
452static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
453 char *buff)
454{
455 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
456 int bytes_written;
457
458 batadv_sysfs_deprecated(attr);
459
460 /* 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
467 switch (atomic_read(&bat_priv->gw.mode)) {
468 case BATADV_GW_MODE_CLIENT:
469 bytes_written = sprintf(buff, "%s\n",
470 BATADV_GW_MODE_CLIENT_NAME);
471 break;
472 case BATADV_GW_MODE_SERVER:
473 bytes_written = sprintf(buff, "%s\n",
474 BATADV_GW_MODE_SERVER_NAME);
475 break;
476 default:
477 bytes_written = sprintf(buff, "%s\n",
478 BATADV_GW_MODE_OFF_NAME);
479 break;
480 }
481
482 return bytes_written;
483}
484
485static ssize_t batadv_store_gw_mode(struct kobject *kobj,
486 struct attribute *attr, char *buff,
487 size_t count)
488{
489 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
490 struct batadv_priv *bat_priv = netdev_priv(net_dev);
491 char *curr_gw_mode_str;
492 int gw_mode_tmp = -1;
493
494 batadv_sysfs_deprecated(attr);
495
496 /* 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
503 if (buff[count - 1] == '\n')
504 buff[count - 1] = '\0';
505
506 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
507 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
508 gw_mode_tmp = BATADV_GW_MODE_OFF;
509
510 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
511 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
512 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
513
514 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
515 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
516 gw_mode_tmp = BATADV_GW_MODE_SERVER;
517
518 if (gw_mode_tmp < 0) {
519 batadv_info(net_dev,
520 "Invalid parameter for 'gw mode' setting received: %s\n",
521 buff);
522 return -EINVAL;
523 }
524
525 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
526 return count;
527
528 switch (atomic_read(&bat_priv->gw.mode)) {
529 case BATADV_GW_MODE_CLIENT:
530 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
531 break;
532 case BATADV_GW_MODE_SERVER:
533 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
534 break;
535 default:
536 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
537 break;
538 }
539
540 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
541 curr_gw_mode_str, buff);
542
543 /* 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 */
552 batadv_gw_reselect(bat_priv);
553 /* always call batadv_gw_check_client_stop() before changing the gateway
554 * state
555 */
556 batadv_gw_check_client_stop(bat_priv);
557 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
558 batadv_gw_tvlv_container_update(bat_priv);
559
560 batadv_netlink_notify_mesh(bat_priv);
561
562 return count;
563}
564
565static 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
570 batadv_sysfs_deprecated(attr);
571
572 /* 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
579 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);
590 ssize_t length;
591
592 batadv_sysfs_deprecated(attr);
593
594 /* 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
601 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
608 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;
616}
617
618static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
619 struct attribute *attr, char *buff)
620{
621 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
622 u32 down, up;
623
624 batadv_sysfs_deprecated(attr);
625
626 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);
631}
632
633static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
634 struct attribute *attr, char *buff,
635 size_t count)
636{
637 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
638 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
639 ssize_t length;
640
641 batadv_sysfs_deprecated(attr);
642
643 if (buff[count - 1] == '\n')
644 buff[count - 1] = '\0';
645
646 length = batadv_gw_bandwidth_set(net_dev, buff, count);
647
648 batadv_netlink_notify_mesh(bat_priv);
649
650 return length;
651}
652
653/**
654 * batadv_show_isolation_mark() - print the current isolation mark/mask
655 * @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 *
659 * Return: the number of bytes written into 'buff' on success or a negative
660 * 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
667 batadv_sysfs_deprecated(attr);
668 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
669 bat_priv->isolation_mark_mask);
670}
671
672/**
673 * batadv_store_isolation_mark() - parse and store the isolation mark/mask
674 * entered by the user
675 * @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 *
680 * Return: 'count' on success or a negative error code in case of failure
681 */
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);
688 u32 mark, mask;
689 char *mask_ptr;
690
691 batadv_sysfs_deprecated(attr);
692
693 /* 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
721 batadv_netlink_notify_mesh(bat_priv);
722
723 return count;
724}
725
726BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
727BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
728#ifdef CONFIG_BATMAN_ADV_BLA
729BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
730#endif
731#ifdef CONFIG_BATMAN_ADV_DAT
732BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
733#endif
734BATADV_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,
742 batadv_store_gw_sel_class);
743static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
744 batadv_store_gw_bwidth);
745#ifdef CONFIG_BATMAN_ADV_MCAST
746BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
747#endif
748#ifdef CONFIG_BATMAN_ADV_DEBUG
749BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
750#endif
751#ifdef CONFIG_BATMAN_ADV_NC
752BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
753#endif
754static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
755 batadv_store_isolation_mark);
756
757static struct batadv_attribute *batadv_mesh_attrs[] = {
758 &batadv_attr_aggregated_ogms,
759 &batadv_attr_bonding,
760#ifdef CONFIG_BATMAN_ADV_BLA
761 &batadv_attr_bridge_loop_avoidance,
762#endif
763#ifdef CONFIG_BATMAN_ADV_DAT
764 &batadv_attr_distributed_arp_table,
765#endif
766#ifdef CONFIG_BATMAN_ADV_MCAST
767 &batadv_attr_multicast_mode,
768#endif
769 &batadv_attr_fragmentation,
770 &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,
776#ifdef CONFIG_BATMAN_ADV_DEBUG
777 &batadv_attr_log_level,
778#endif
779#ifdef CONFIG_BATMAN_ADV_NC
780 &batadv_attr_network_coding,
781#endif
782 &batadv_attr_isolation_mark,
783 NULL,
784};
785
786BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
787
788/* array of vlan specific sysfs attributes */
789static struct batadv_attribute *batadv_vlan_attrs[] = {
790 &batadv_attr_vlan_ap_isolation,
791 NULL,
792};
793
794/**
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 */
800int batadv_sysfs_add_meshif(struct net_device *dev)
801{
802 struct kobject *batif_kobject = &dev->dev.kobj;
803 struct batadv_priv *bat_priv = netdev_priv(dev);
804 struct batadv_attribute **bat_attr;
805 int err;
806
807 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
808 batif_kobject);
809 if (!bat_priv->mesh_obj) {
810 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
811 BATADV_SYSFS_IF_MESH_SUBDIR);
812 goto out;
813 }
814
815 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
816 err = sysfs_create_file(bat_priv->mesh_obj,
817 &((*bat_attr)->attr));
818 if (err) {
819 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
820 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
821 ((*bat_attr)->attr).name);
822 goto rem_attr;
823 }
824 }
825
826 return 0;
827
828rem_attr:
829 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
830 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
831
832 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
833 kobject_del(bat_priv->mesh_obj);
834 kobject_put(bat_priv->mesh_obj);
835 bat_priv->mesh_obj = NULL;
836out:
837 return -ENOMEM;
838}
839
840/**
841 * batadv_sysfs_del_meshif() - Remove soft interface specific sysfs entries
842 * @dev: netdev struct of the soft interface
843 */
844void batadv_sysfs_del_meshif(struct net_device *dev)
845{
846 struct batadv_priv *bat_priv = netdev_priv(dev);
847 struct batadv_attribute **bat_attr;
848
849 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
850 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
851
852 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
853 kobject_del(bat_priv->mesh_obj);
854 kobject_put(bat_priv->mesh_obj);
855 bat_priv->mesh_obj = NULL;
856}
857
858/**
859 * batadv_sysfs_add_vlan() - add all the needed sysfs objects for the new vlan
860 * @dev: netdev of the mesh interface
861 * @vlan: private data of the newly added VLAN interface
862 *
863 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
864 */
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
909 if (vlan->kobj != bat_priv->mesh_obj) {
910 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
911 kobject_del(vlan->kobj);
912 }
913 kobject_put(vlan->kobj);
914 vlan->kobj = NULL;
915out:
916 return -ENOMEM;
917}
918
919/**
920 * batadv_sysfs_del_vlan() - remove all the sysfs objects for a given VLAN
921 * @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
932 if (vlan->kobj != bat_priv->mesh_obj) {
933 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
934 kobject_del(vlan->kobj);
935 }
936 kobject_put(vlan->kobj);
937 vlan->kobj = NULL;
938}
939
940static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
941 struct attribute *attr, char *buff)
942{
943 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
944 struct batadv_hard_iface *hard_iface;
945 ssize_t length;
946 const char *ifname;
947
948 batadv_sysfs_deprecated(attr);
949
950 hard_iface = batadv_hardif_get_by_netdev(net_dev);
951 if (!hard_iface)
952 return 0;
953
954 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);
960
961 batadv_hardif_put(hard_iface);
962
963 return length;
964}
965
966/**
967 * batadv_store_mesh_iface_finish() - store new hardif mesh_iface state
968 * @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])
978{
979 struct net *net = dev_net(net_dev);
980 struct batadv_hard_iface *hard_iface;
981 int status_tmp;
982 int ret = 0;
983
984 ASSERT_RTNL();
985
986 hard_iface = batadv_hardif_get_by_netdev(net_dev);
987 if (!hard_iface)
988 return 0;
989
990 if (strncmp(ifname, "none", 4) == 0)
991 status_tmp = BATADV_IF_NOT_IN_USE;
992 else
993 status_tmp = BATADV_IF_I_WANT_YOU;
994
995 if (hard_iface->if_status == status_tmp)
996 goto out;
997
998 if (hard_iface->soft_iface &&
999 strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)
1000 goto out;
1001
1002 if (status_tmp == BATADV_IF_NOT_IN_USE) {
1003 batadv_hardif_disable_interface(hard_iface,
1004 BATADV_IF_CLEANUP_AUTO);
1005 goto out;
1006 }
1007
1008 /* if the interface already is in use */
1009 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
1010 batadv_hardif_disable_interface(hard_iface,
1011 BATADV_IF_CLEANUP_AUTO);
1012
1013 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
1014out:
1015 batadv_hardif_put(hard_iface);
1016 return ret;
1017}
1018
1019/**
1020 * batadv_store_mesh_iface_work() - store new hardif mesh_iface state
1021 * @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
1054 batadv_sysfs_deprecated(attr);
1055
1056 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
1080static ssize_t batadv_show_iface_status(struct kobject *kobj,
1081 struct attribute *attr, char *buff)
1082{
1083 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1084 struct batadv_hard_iface *hard_iface;
1085 ssize_t length;
1086
1087 batadv_sysfs_deprecated(attr);
1088
1089 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1090 if (!hard_iface)
1091 return 0;
1092
1093 switch (hard_iface->if_status) {
1094 case BATADV_IF_TO_BE_REMOVED:
1095 length = sprintf(buff, "disabling\n");
1096 break;
1097 case BATADV_IF_INACTIVE:
1098 length = sprintf(buff, "inactive\n");
1099 break;
1100 case BATADV_IF_ACTIVE:
1101 length = sprintf(buff, "active\n");
1102 break;
1103 case BATADV_IF_TO_BE_ACTIVATED:
1104 length = sprintf(buff, "enabling\n");
1105 break;
1106 case BATADV_IF_NOT_IN_USE:
1107 default:
1108 length = sprintf(buff, "not in use\n");
1109 break;
1110 }
1111
1112 batadv_hardif_put(hard_iface);
1113
1114 return length;
1115}
1116
1117#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1118
1119/**
1120 * batadv_store_throughput_override() - parse and store throughput override
1121 * 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;
1135 struct batadv_priv *bat_priv;
1136 u32 tp_override;
1137 u32 old_tp_override;
1138 bool ret;
1139
1140 batadv_sysfs_deprecated(attr);
1141
1142 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
1158 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,
1161 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
1166 if (hard_iface->soft_iface) {
1167 bat_priv = netdev_priv(hard_iface->soft_iface);
1168 batadv_netlink_notify_hardif(bat_priv, hard_iface);
1169 }
1170
1171out:
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
1184 batadv_sysfs_deprecated(attr);
1185
1186 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
1198static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
1199 batadv_store_mesh_iface);
1200static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
1201#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1202BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
1203 2 * BATADV_JITTER, INT_MAX, NULL);
1204static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
1205 batadv_store_throughput_override);
1206#endif
1207
1208static struct batadv_attribute *batadv_batman_attrs[] = {
1209 &batadv_attr_mesh_iface,
1210 &batadv_attr_iface_status,
1211#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1212 &batadv_attr_elp_interval,
1213 &batadv_attr_throughput_override,
1214#endif
1215 NULL,
1216};
1217
1218/**
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 */
1225int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
1226{
1227 struct kobject *hardif_kobject = &dev->dev.kobj;
1228 struct batadv_attribute **bat_attr;
1229 int err;
1230
1231 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1232 hardif_kobject);
1233
1234 if (!*hardif_obj) {
1235 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
1236 BATADV_SYSFS_IF_BAT_SUBDIR);
1237 goto out;
1238 }
1239
1240 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
1241 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1242 if (err) {
1243 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
1244 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
1245 ((*bat_attr)->attr).name);
1246 goto rem_attr;
1247 }
1248 }
1249
1250 return 0;
1251
1252rem_attr:
1253 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
1254 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1255out:
1256 return -ENOMEM;
1257}
1258
1259/**
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 */
1264void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
1265{
1266 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1267 kobject_del(*hardif_obj);
1268 kobject_put(*hardif_obj);
1269 *hardif_obj = NULL;
1270}