]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - net/batman-adv/sysfs.c
ab0b95f15b36607e1d1422c98b1c1c97410ffee0
[mirror_ubuntu-hirsute-kernel.git] / net / batman-adv / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2017 B.A.T.M.A.N. contributors:
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
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "sysfs.h"
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>
26 #include <linux/fs.h>
27 #include <linux/if.h>
28 #include <linux/if_vlan.h>
29 #include <linux/kernel.h>
30 #include <linux/kref.h>
31 #include <linux/netdevice.h>
32 #include <linux/printk.h>
33 #include <linux/rculist.h>
34 #include <linux/rcupdate.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/slab.h>
37 #include <linux/stddef.h>
38 #include <linux/string.h>
39 #include <linux/stringify.h>
40 #include <linux/workqueue.h>
41
42 #include "bridge_loop_avoidance.h"
43 #include "distributed-arp-table.h"
44 #include "gateway_client.h"
45 #include "gateway_common.h"
46 #include "hard-interface.h"
47 #include "log.h"
48 #include "network-coding.h"
49 #include "packet.h"
50 #include "soft-interface.h"
51
52 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
53 {
54 struct device *dev = container_of(obj->parent, struct device, kobj);
55
56 return to_net_dev(dev);
57 }
58
59 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
60 {
61 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
62
63 return netdev_priv(net_dev);
64 }
65
66 /**
67 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
68 * @obj: kobject to covert
69 *
70 * Return: the associated batadv_priv struct.
71 */
72 static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
73 {
74 /* VLAN specific attributes are located in the root sysfs folder if they
75 * refer to the untagged VLAN..
76 */
77 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
78 return batadv_kobj_to_batpriv(obj);
79
80 /* ..while the attributes for the tagged vlans are located in
81 * the in the corresponding "vlan%VID" subfolder
82 */
83 return batadv_kobj_to_batpriv(obj->parent);
84 }
85
86 /**
87 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
88 * @bat_priv: the bat priv with all the soft interface information
89 * @obj: kobject to covert
90 *
91 * Return: the associated softif_vlan struct if found, NULL otherwise.
92 */
93 static struct batadv_softif_vlan *
94 batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
95 {
96 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
97
98 rcu_read_lock();
99 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
100 if (vlan_tmp->kobj != obj)
101 continue;
102
103 if (!kref_get_unless_zero(&vlan_tmp->refcount))
104 continue;
105
106 vlan = vlan_tmp;
107 break;
108 }
109 rcu_read_unlock();
110
111 return vlan;
112 }
113
114 #define BATADV_UEV_TYPE_VAR "BATTYPE="
115 #define BATADV_UEV_ACTION_VAR "BATACTION="
116 #define BATADV_UEV_DATA_VAR "BATDATA="
117
118 static char *batadv_uev_action_str[] = {
119 "add",
120 "del",
121 "change",
122 "loopdetect",
123 };
124
125 static char *batadv_uev_type_str[] = {
126 "gw",
127 "bla",
128 };
129
130 /* Use this, if you have customized show and store functions for vlan attrs */
131 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
132 struct batadv_attribute batadv_attr_vlan_##_name = { \
133 .attr = {.name = __stringify(_name), \
134 .mode = _mode }, \
135 .show = _show, \
136 .store = _store, \
137 }
138
139 /* Use this, if you have customized show and store functions */
140 #define BATADV_ATTR(_name, _mode, _show, _store) \
141 struct batadv_attribute batadv_attr_##_name = { \
142 .attr = {.name = __stringify(_name), \
143 .mode = _mode }, \
144 .show = _show, \
145 .store = _store, \
146 }
147
148 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
149 ssize_t batadv_store_##_name(struct kobject *kobj, \
150 struct attribute *attr, char *buff, \
151 size_t count) \
152 { \
153 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
154 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
155 \
156 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
157 &bat_priv->_name, net_dev); \
158 }
159
160 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
161 ssize_t batadv_show_##_name(struct kobject *kobj, \
162 struct attribute *attr, char *buff) \
163 { \
164 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
165 \
166 return sprintf(buff, "%s\n", \
167 atomic_read(&bat_priv->_name) == 0 ? \
168 "disabled" : "enabled"); \
169 } \
170
171 /* Use this, if you are going to turn a [name] in the soft-interface
172 * (bat_priv) on or off
173 */
174 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
175 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
176 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
177 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
178 batadv_store_##_name)
179
180 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
181 ssize_t batadv_store_##_name(struct kobject *kobj, \
182 struct attribute *attr, char *buff, \
183 size_t count) \
184 { \
185 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
186 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
187 \
188 return __batadv_store_uint_attr(buff, count, _min, _max, \
189 _post_func, attr, \
190 &bat_priv->_var, net_dev); \
191 }
192
193 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
194 ssize_t batadv_show_##_name(struct kobject *kobj, \
195 struct attribute *attr, char *buff) \
196 { \
197 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
198 \
199 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
200 } \
201
202 /* Use this, if you are going to set [name] in the soft-interface
203 * (bat_priv) to an unsigned integer value
204 */
205 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
206 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
207 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
208 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
209 batadv_store_##_name)
210
211 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
212 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
213 struct attribute *attr, char *buff, \
214 size_t count) \
215 { \
216 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
217 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
218 kobj); \
219 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
220 attr, &vlan->_name, \
221 bat_priv->soft_iface); \
222 \
223 batadv_softif_vlan_put(vlan); \
224 return res; \
225 }
226
227 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
228 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
229 struct attribute *attr, char *buff) \
230 { \
231 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
232 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
233 kobj); \
234 size_t res = sprintf(buff, "%s\n", \
235 atomic_read(&vlan->_name) == 0 ? \
236 "disabled" : "enabled"); \
237 \
238 batadv_softif_vlan_put(vlan); \
239 return res; \
240 }
241
242 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
243 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
244 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
245 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
246 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
247 batadv_store_vlan_##_name)
248
249 #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
250 ssize_t batadv_store_##_name(struct kobject *kobj, \
251 struct attribute *attr, char *buff, \
252 size_t count) \
253 { \
254 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
255 struct batadv_hard_iface *hard_iface; \
256 ssize_t length; \
257 \
258 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
259 if (!hard_iface) \
260 return 0; \
261 \
262 length = __batadv_store_uint_attr(buff, count, _min, _max, \
263 _post_func, attr, \
264 &hard_iface->_var, net_dev); \
265 \
266 batadv_hardif_put(hard_iface); \
267 return length; \
268 }
269
270 #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
271 ssize_t batadv_show_##_name(struct kobject *kobj, \
272 struct attribute *attr, char *buff) \
273 { \
274 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
275 struct batadv_hard_iface *hard_iface; \
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 = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
283 \
284 batadv_hardif_put(hard_iface); \
285 return length; \
286 }
287
288 /* Use this, if you are going to set [name] in hard_iface to an
289 * unsigned integer value
290 */
291 #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
292 static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
293 _max, _post_func) \
294 static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
295 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
296 batadv_store_##_name)
297
298 static int batadv_store_bool_attr(char *buff, size_t count,
299 struct net_device *net_dev,
300 const char *attr_name, atomic_t *attr,
301 bool *changed)
302 {
303 int enabled = -1;
304
305 *changed = false;
306
307 if (buff[count - 1] == '\n')
308 buff[count - 1] = '\0';
309
310 if ((strncmp(buff, "1", 2) == 0) ||
311 (strncmp(buff, "enable", 7) == 0) ||
312 (strncmp(buff, "enabled", 8) == 0))
313 enabled = 1;
314
315 if ((strncmp(buff, "0", 2) == 0) ||
316 (strncmp(buff, "disable", 8) == 0) ||
317 (strncmp(buff, "disabled", 9) == 0))
318 enabled = 0;
319
320 if (enabled < 0) {
321 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
322 attr_name, buff);
323 return -EINVAL;
324 }
325
326 if (atomic_read(attr) == enabled)
327 return count;
328
329 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
330 atomic_read(attr) == 1 ? "enabled" : "disabled",
331 enabled == 1 ? "enabled" : "disabled");
332
333 *changed = true;
334
335 atomic_set(attr, (unsigned int)enabled);
336 return count;
337 }
338
339 static inline ssize_t
340 __batadv_store_bool_attr(char *buff, size_t count,
341 void (*post_func)(struct net_device *),
342 struct attribute *attr,
343 atomic_t *attr_store, struct net_device *net_dev)
344 {
345 bool changed;
346 int ret;
347
348 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
349 attr_store, &changed);
350 if (post_func && changed)
351 post_func(net_dev);
352
353 return ret;
354 }
355
356 static int batadv_store_uint_attr(const char *buff, size_t count,
357 struct net_device *net_dev,
358 const char *attr_name,
359 unsigned int min, unsigned int max,
360 atomic_t *attr)
361 {
362 unsigned long uint_val;
363 int ret;
364
365 ret = kstrtoul(buff, 10, &uint_val);
366 if (ret) {
367 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
368 attr_name, buff);
369 return -EINVAL;
370 }
371
372 if (uint_val < min) {
373 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
374 attr_name, uint_val, min);
375 return -EINVAL;
376 }
377
378 if (uint_val > max) {
379 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
380 attr_name, uint_val, max);
381 return -EINVAL;
382 }
383
384 if (atomic_read(attr) == uint_val)
385 return count;
386
387 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
388 attr_name, atomic_read(attr), uint_val);
389
390 atomic_set(attr, uint_val);
391 return count;
392 }
393
394 static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
395 int min, int max,
396 void (*post_func)(struct net_device *),
397 const struct attribute *attr,
398 atomic_t *attr_store,
399 struct net_device *net_dev)
400 {
401 int ret;
402
403 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
404 attr_store);
405 if (post_func && ret)
406 post_func(net_dev);
407
408 return ret;
409 }
410
411 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
412 struct attribute *attr, char *buff)
413 {
414 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
415
416 return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
417 }
418
419 static void batadv_post_gw_reselect(struct net_device *net_dev)
420 {
421 struct batadv_priv *bat_priv = netdev_priv(net_dev);
422
423 batadv_gw_reselect(bat_priv);
424 }
425
426 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
427 char *buff)
428 {
429 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
430 int bytes_written;
431
432 /* GW mode is not available if the routing algorithm in use does not
433 * implement the GW API
434 */
435 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
436 !bat_priv->algo_ops->gw.is_eligible)
437 return -ENOENT;
438
439 switch (atomic_read(&bat_priv->gw.mode)) {
440 case BATADV_GW_MODE_CLIENT:
441 bytes_written = sprintf(buff, "%s\n",
442 BATADV_GW_MODE_CLIENT_NAME);
443 break;
444 case BATADV_GW_MODE_SERVER:
445 bytes_written = sprintf(buff, "%s\n",
446 BATADV_GW_MODE_SERVER_NAME);
447 break;
448 default:
449 bytes_written = sprintf(buff, "%s\n",
450 BATADV_GW_MODE_OFF_NAME);
451 break;
452 }
453
454 return bytes_written;
455 }
456
457 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
458 struct attribute *attr, char *buff,
459 size_t count)
460 {
461 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
462 struct batadv_priv *bat_priv = netdev_priv(net_dev);
463 char *curr_gw_mode_str;
464 int gw_mode_tmp = -1;
465
466 /* toggling GW mode is allowed only if the routing algorithm in use
467 * provides the GW API
468 */
469 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
470 !bat_priv->algo_ops->gw.is_eligible)
471 return -EINVAL;
472
473 if (buff[count - 1] == '\n')
474 buff[count - 1] = '\0';
475
476 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
477 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
478 gw_mode_tmp = BATADV_GW_MODE_OFF;
479
480 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
481 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
482 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
483
484 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
485 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
486 gw_mode_tmp = BATADV_GW_MODE_SERVER;
487
488 if (gw_mode_tmp < 0) {
489 batadv_info(net_dev,
490 "Invalid parameter for 'gw mode' setting received: %s\n",
491 buff);
492 return -EINVAL;
493 }
494
495 if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
496 return count;
497
498 switch (atomic_read(&bat_priv->gw.mode)) {
499 case BATADV_GW_MODE_CLIENT:
500 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
501 break;
502 case BATADV_GW_MODE_SERVER:
503 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
504 break;
505 default:
506 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
507 break;
508 }
509
510 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
511 curr_gw_mode_str, buff);
512
513 /* Invoking batadv_gw_reselect() is not enough to really de-select the
514 * current GW. It will only instruct the gateway client code to perform
515 * a re-election the next time that this is needed.
516 *
517 * When gw client mode is being switched off the current GW must be
518 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
519 * client mode re-activation. This is operation is performed in
520 * batadv_gw_check_client_stop().
521 */
522 batadv_gw_reselect(bat_priv);
523 /* always call batadv_gw_check_client_stop() before changing the gateway
524 * state
525 */
526 batadv_gw_check_client_stop(bat_priv);
527 atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
528 batadv_gw_tvlv_container_update(bat_priv);
529 return count;
530 }
531
532 static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
533 struct attribute *attr, char *buff)
534 {
535 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
536
537 /* GW selection class is not available if the routing algorithm in use
538 * does not implement the GW API
539 */
540 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
541 !bat_priv->algo_ops->gw.is_eligible)
542 return -ENOENT;
543
544 if (bat_priv->algo_ops->gw.show_sel_class)
545 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
546
547 return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
548 }
549
550 static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
551 struct attribute *attr, char *buff,
552 size_t count)
553 {
554 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
555
556 /* setting the GW selection class is allowed only if the routing
557 * algorithm in use implements the GW API
558 */
559 if (!bat_priv->algo_ops->gw.get_best_gw_node ||
560 !bat_priv->algo_ops->gw.is_eligible)
561 return -EINVAL;
562
563 if (buff[count - 1] == '\n')
564 buff[count - 1] = '\0';
565
566 if (bat_priv->algo_ops->gw.store_sel_class)
567 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
568 count);
569
570 return __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
571 batadv_post_gw_reselect, attr,
572 &bat_priv->gw.sel_class,
573 bat_priv->soft_iface);
574 }
575
576 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
577 struct attribute *attr, char *buff)
578 {
579 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
580 u32 down, up;
581
582 down = atomic_read(&bat_priv->gw.bandwidth_down);
583 up = atomic_read(&bat_priv->gw.bandwidth_up);
584
585 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
586 down % 10, up / 10, up % 10);
587 }
588
589 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
590 struct attribute *attr, char *buff,
591 size_t count)
592 {
593 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
594
595 if (buff[count - 1] == '\n')
596 buff[count - 1] = '\0';
597
598 return batadv_gw_bandwidth_set(net_dev, buff, count);
599 }
600
601 /**
602 * batadv_show_isolation_mark - print the current isolation mark/mask
603 * @kobj: kobject representing the private mesh sysfs directory
604 * @attr: the batman-adv attribute the user is interacting with
605 * @buff: the buffer that will contain the data to send back to the user
606 *
607 * Return: the number of bytes written into 'buff' on success or a negative
608 * error code in case of failure
609 */
610 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
611 struct attribute *attr, char *buff)
612 {
613 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
614
615 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
616 bat_priv->isolation_mark_mask);
617 }
618
619 /**
620 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
621 * by the user
622 * @kobj: kobject representing the private mesh sysfs directory
623 * @attr: the batman-adv attribute the user is interacting with
624 * @buff: the buffer containing the user data
625 * @count: number of bytes in the buffer
626 *
627 * Return: 'count' on success or a negative error code in case of failure
628 */
629 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
630 struct attribute *attr, char *buff,
631 size_t count)
632 {
633 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
634 struct batadv_priv *bat_priv = netdev_priv(net_dev);
635 u32 mark, mask;
636 char *mask_ptr;
637
638 /* parse the mask if it has been specified, otherwise assume the mask is
639 * the biggest possible
640 */
641 mask = 0xFFFFFFFF;
642 mask_ptr = strchr(buff, '/');
643 if (mask_ptr) {
644 *mask_ptr = '\0';
645 mask_ptr++;
646
647 /* the mask must be entered in hex base as it is going to be a
648 * bitmask and not a prefix length
649 */
650 if (kstrtou32(mask_ptr, 16, &mask) < 0)
651 return -EINVAL;
652 }
653
654 /* the mark can be entered in any base */
655 if (kstrtou32(buff, 0, &mark) < 0)
656 return -EINVAL;
657
658 bat_priv->isolation_mark_mask = mask;
659 /* erase bits not covered by the mask */
660 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
661
662 batadv_info(net_dev,
663 "New skb mark for extended isolation: %#.8x/%#.8x\n",
664 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
665
666 return count;
667 }
668
669 BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
670 BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
671 #ifdef CONFIG_BATMAN_ADV_BLA
672 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
673 #endif
674 #ifdef CONFIG_BATMAN_ADV_DAT
675 BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
676 #endif
677 BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
678 static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
679 static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
680 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
681 INT_MAX, NULL);
682 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
683 NULL);
684 static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
685 batadv_store_gw_sel_class);
686 static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
687 batadv_store_gw_bwidth);
688 #ifdef CONFIG_BATMAN_ADV_MCAST
689 BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
690 #endif
691 #ifdef CONFIG_BATMAN_ADV_DEBUG
692 BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
693 #endif
694 #ifdef CONFIG_BATMAN_ADV_NC
695 BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
696 #endif
697 static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
698 batadv_store_isolation_mark);
699
700 static struct batadv_attribute *batadv_mesh_attrs[] = {
701 &batadv_attr_aggregated_ogms,
702 &batadv_attr_bonding,
703 #ifdef CONFIG_BATMAN_ADV_BLA
704 &batadv_attr_bridge_loop_avoidance,
705 #endif
706 #ifdef CONFIG_BATMAN_ADV_DAT
707 &batadv_attr_distributed_arp_table,
708 #endif
709 #ifdef CONFIG_BATMAN_ADV_MCAST
710 &batadv_attr_multicast_mode,
711 #endif
712 &batadv_attr_fragmentation,
713 &batadv_attr_routing_algo,
714 &batadv_attr_gw_mode,
715 &batadv_attr_orig_interval,
716 &batadv_attr_hop_penalty,
717 &batadv_attr_gw_sel_class,
718 &batadv_attr_gw_bandwidth,
719 #ifdef CONFIG_BATMAN_ADV_DEBUG
720 &batadv_attr_log_level,
721 #endif
722 #ifdef CONFIG_BATMAN_ADV_NC
723 &batadv_attr_network_coding,
724 #endif
725 &batadv_attr_isolation_mark,
726 NULL,
727 };
728
729 BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
730
731 /* array of vlan specific sysfs attributes */
732 static struct batadv_attribute *batadv_vlan_attrs[] = {
733 &batadv_attr_vlan_ap_isolation,
734 NULL,
735 };
736
737 int batadv_sysfs_add_meshif(struct net_device *dev)
738 {
739 struct kobject *batif_kobject = &dev->dev.kobj;
740 struct batadv_priv *bat_priv = netdev_priv(dev);
741 struct batadv_attribute **bat_attr;
742 int err;
743
744 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
745 batif_kobject);
746 if (!bat_priv->mesh_obj) {
747 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
748 BATADV_SYSFS_IF_MESH_SUBDIR);
749 goto out;
750 }
751
752 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
753 err = sysfs_create_file(bat_priv->mesh_obj,
754 &((*bat_attr)->attr));
755 if (err) {
756 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
757 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
758 ((*bat_attr)->attr).name);
759 goto rem_attr;
760 }
761 }
762
763 return 0;
764
765 rem_attr:
766 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
767 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
768
769 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
770 kobject_del(bat_priv->mesh_obj);
771 kobject_put(bat_priv->mesh_obj);
772 bat_priv->mesh_obj = NULL;
773 out:
774 return -ENOMEM;
775 }
776
777 void batadv_sysfs_del_meshif(struct net_device *dev)
778 {
779 struct batadv_priv *bat_priv = netdev_priv(dev);
780 struct batadv_attribute **bat_attr;
781
782 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
783 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
784
785 kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
786 kobject_del(bat_priv->mesh_obj);
787 kobject_put(bat_priv->mesh_obj);
788 bat_priv->mesh_obj = NULL;
789 }
790
791 /**
792 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
793 * @dev: netdev of the mesh interface
794 * @vlan: private data of the newly added VLAN interface
795 *
796 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
797 */
798 int batadv_sysfs_add_vlan(struct net_device *dev,
799 struct batadv_softif_vlan *vlan)
800 {
801 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
802 struct batadv_priv *bat_priv = netdev_priv(dev);
803 struct batadv_attribute **bat_attr;
804 int err;
805
806 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
807 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
808 vlan->vid & VLAN_VID_MASK);
809
810 vlan->kobj = kobject_create_and_add(vlan_subdir,
811 bat_priv->mesh_obj);
812 if (!vlan->kobj) {
813 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
814 dev->name, vlan_subdir);
815 goto out;
816 }
817 } else {
818 /* the untagged LAN uses the root folder to store its "VLAN
819 * specific attributes"
820 */
821 vlan->kobj = bat_priv->mesh_obj;
822 kobject_get(bat_priv->mesh_obj);
823 }
824
825 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
826 err = sysfs_create_file(vlan->kobj,
827 &((*bat_attr)->attr));
828 if (err) {
829 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
830 dev->name, vlan_subdir,
831 ((*bat_attr)->attr).name);
832 goto rem_attr;
833 }
834 }
835
836 return 0;
837
838 rem_attr:
839 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
840 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
841
842 if (vlan->kobj != bat_priv->mesh_obj) {
843 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
844 kobject_del(vlan->kobj);
845 }
846 kobject_put(vlan->kobj);
847 vlan->kobj = NULL;
848 out:
849 return -ENOMEM;
850 }
851
852 /**
853 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
854 * @bat_priv: the bat priv with all the soft interface information
855 * @vlan: the private data of the VLAN to destroy
856 */
857 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
858 struct batadv_softif_vlan *vlan)
859 {
860 struct batadv_attribute **bat_attr;
861
862 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
863 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
864
865 if (vlan->kobj != bat_priv->mesh_obj) {
866 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
867 kobject_del(vlan->kobj);
868 }
869 kobject_put(vlan->kobj);
870 vlan->kobj = NULL;
871 }
872
873 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
874 struct attribute *attr, char *buff)
875 {
876 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
877 struct batadv_hard_iface *hard_iface;
878 ssize_t length;
879 const char *ifname;
880
881 hard_iface = batadv_hardif_get_by_netdev(net_dev);
882 if (!hard_iface)
883 return 0;
884
885 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
886 ifname = "none";
887 else
888 ifname = hard_iface->soft_iface->name;
889
890 length = sprintf(buff, "%s\n", ifname);
891
892 batadv_hardif_put(hard_iface);
893
894 return length;
895 }
896
897 /**
898 * batadv_store_mesh_iface_finish - store new hardif mesh_iface state
899 * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
900 * @ifname: name of soft-interface to modify
901 *
902 * Changes the parts of the hard+soft interface which can not be modified under
903 * sysfs lock (to prevent deadlock situations).
904 *
905 * Return: 0 on success, 0 < on failure
906 */
907 static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
908 char ifname[IFNAMSIZ])
909 {
910 struct net *net = dev_net(net_dev);
911 struct batadv_hard_iface *hard_iface;
912 int status_tmp;
913 int ret = 0;
914
915 ASSERT_RTNL();
916
917 hard_iface = batadv_hardif_get_by_netdev(net_dev);
918 if (!hard_iface)
919 return 0;
920
921 if (strncmp(ifname, "none", 4) == 0)
922 status_tmp = BATADV_IF_NOT_IN_USE;
923 else
924 status_tmp = BATADV_IF_I_WANT_YOU;
925
926 if (hard_iface->if_status == status_tmp)
927 goto out;
928
929 if (hard_iface->soft_iface &&
930 strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)
931 goto out;
932
933 if (status_tmp == BATADV_IF_NOT_IN_USE) {
934 batadv_hardif_disable_interface(hard_iface,
935 BATADV_IF_CLEANUP_AUTO);
936 goto out;
937 }
938
939 /* if the interface already is in use */
940 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
941 batadv_hardif_disable_interface(hard_iface,
942 BATADV_IF_CLEANUP_AUTO);
943
944 ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
945 out:
946 batadv_hardif_put(hard_iface);
947 return ret;
948 }
949
950 /**
951 * batadv_store_mesh_iface_work - store new hardif mesh_iface state
952 * @work: work queue item
953 *
954 * Changes the parts of the hard+soft interface which can not be modified under
955 * sysfs lock (to prevent deadlock situations).
956 */
957 static void batadv_store_mesh_iface_work(struct work_struct *work)
958 {
959 struct batadv_store_mesh_work *store_work;
960 int ret;
961
962 store_work = container_of(work, struct batadv_store_mesh_work, work);
963
964 rtnl_lock();
965 ret = batadv_store_mesh_iface_finish(store_work->net_dev,
966 store_work->soft_iface_name);
967 rtnl_unlock();
968
969 if (ret < 0)
970 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
971 store_work->soft_iface_name, store_work->net_dev->name,
972 ret);
973
974 dev_put(store_work->net_dev);
975 kfree(store_work);
976 }
977
978 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
979 struct attribute *attr, char *buff,
980 size_t count)
981 {
982 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
983 struct batadv_store_mesh_work *store_work;
984
985 if (buff[count - 1] == '\n')
986 buff[count - 1] = '\0';
987
988 if (strlen(buff) >= IFNAMSIZ) {
989 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
990 buff);
991 return -EINVAL;
992 }
993
994 store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
995 if (!store_work)
996 return -ENOMEM;
997
998 dev_hold(net_dev);
999 INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1000 store_work->net_dev = net_dev;
1001 strlcpy(store_work->soft_iface_name, buff,
1002 sizeof(store_work->soft_iface_name));
1003
1004 queue_work(batadv_event_workqueue, &store_work->work);
1005
1006 return count;
1007 }
1008
1009 static ssize_t batadv_show_iface_status(struct kobject *kobj,
1010 struct attribute *attr, char *buff)
1011 {
1012 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1013 struct batadv_hard_iface *hard_iface;
1014 ssize_t length;
1015
1016 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1017 if (!hard_iface)
1018 return 0;
1019
1020 switch (hard_iface->if_status) {
1021 case BATADV_IF_TO_BE_REMOVED:
1022 length = sprintf(buff, "disabling\n");
1023 break;
1024 case BATADV_IF_INACTIVE:
1025 length = sprintf(buff, "inactive\n");
1026 break;
1027 case BATADV_IF_ACTIVE:
1028 length = sprintf(buff, "active\n");
1029 break;
1030 case BATADV_IF_TO_BE_ACTIVATED:
1031 length = sprintf(buff, "enabling\n");
1032 break;
1033 case BATADV_IF_NOT_IN_USE:
1034 default:
1035 length = sprintf(buff, "not in use\n");
1036 break;
1037 }
1038
1039 batadv_hardif_put(hard_iface);
1040
1041 return length;
1042 }
1043
1044 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1045
1046 /**
1047 * batadv_store_throughput_override - parse and store throughput override
1048 * entered by the user
1049 * @kobj: kobject representing the private mesh sysfs directory
1050 * @attr: the batman-adv attribute the user is interacting with
1051 * @buff: the buffer containing the user data
1052 * @count: number of bytes in the buffer
1053 *
1054 * Return: 'count' on success or a negative error code in case of failure
1055 */
1056 static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1057 struct attribute *attr,
1058 char *buff, size_t count)
1059 {
1060 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1061 struct batadv_hard_iface *hard_iface;
1062 u32 tp_override;
1063 u32 old_tp_override;
1064 bool ret;
1065
1066 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1067 if (!hard_iface)
1068 return -EINVAL;
1069
1070 if (buff[count - 1] == '\n')
1071 buff[count - 1] = '\0';
1072
1073 ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1074 &tp_override);
1075 if (!ret)
1076 return count;
1077
1078 old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1079 if (old_tp_override == tp_override)
1080 goto out;
1081
1082 batadv_info(net_dev, "%s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1083 "throughput_override",
1084 old_tp_override / 10, old_tp_override % 10,
1085 tp_override / 10, tp_override % 10);
1086
1087 atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1088
1089 out:
1090 batadv_hardif_put(hard_iface);
1091 return count;
1092 }
1093
1094 static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1095 struct attribute *attr,
1096 char *buff)
1097 {
1098 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1099 struct batadv_hard_iface *hard_iface;
1100 u32 tp_override;
1101
1102 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1103 if (!hard_iface)
1104 return -EINVAL;
1105
1106 tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1107
1108 return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1109 tp_override % 10);
1110 }
1111
1112 #endif
1113
1114 static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
1115 batadv_store_mesh_iface);
1116 static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
1117 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1118 BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
1119 2 * BATADV_JITTER, INT_MAX, NULL);
1120 static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
1121 batadv_store_throughput_override);
1122 #endif
1123
1124 static struct batadv_attribute *batadv_batman_attrs[] = {
1125 &batadv_attr_mesh_iface,
1126 &batadv_attr_iface_status,
1127 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1128 &batadv_attr_elp_interval,
1129 &batadv_attr_throughput_override,
1130 #endif
1131 NULL,
1132 };
1133
1134 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
1135 {
1136 struct kobject *hardif_kobject = &dev->dev.kobj;
1137 struct batadv_attribute **bat_attr;
1138 int err;
1139
1140 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1141 hardif_kobject);
1142
1143 if (!*hardif_obj) {
1144 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
1145 BATADV_SYSFS_IF_BAT_SUBDIR);
1146 goto out;
1147 }
1148
1149 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
1150 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1151 if (err) {
1152 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
1153 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
1154 ((*bat_attr)->attr).name);
1155 goto rem_attr;
1156 }
1157 }
1158
1159 return 0;
1160
1161 rem_attr:
1162 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
1163 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1164 out:
1165 return -ENOMEM;
1166 }
1167
1168 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
1169 {
1170 kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1171 kobject_del(*hardif_obj);
1172 kobject_put(*hardif_obj);
1173 *hardif_obj = NULL;
1174 }
1175
1176 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
1177 enum batadv_uev_action action, const char *data)
1178 {
1179 int ret = -ENOMEM;
1180 struct kobject *bat_kobj;
1181 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1182
1183 bat_kobj = &bat_priv->soft_iface->dev.kobj;
1184
1185 uevent_env[0] = kasprintf(GFP_ATOMIC,
1186 "%s%s", BATADV_UEV_TYPE_VAR,
1187 batadv_uev_type_str[type]);
1188 if (!uevent_env[0])
1189 goto out;
1190
1191 uevent_env[1] = kasprintf(GFP_ATOMIC,
1192 "%s%s", BATADV_UEV_ACTION_VAR,
1193 batadv_uev_action_str[action]);
1194 if (!uevent_env[1])
1195 goto out;
1196
1197 /* If the event is DEL, ignore the data field */
1198 if (action != BATADV_UEV_DEL) {
1199 uevent_env[2] = kasprintf(GFP_ATOMIC,
1200 "%s%s", BATADV_UEV_DATA_VAR, data);
1201 if (!uevent_env[2])
1202 goto out;
1203 }
1204
1205 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1206 out:
1207 kfree(uevent_env[0]);
1208 kfree(uevent_env[1]);
1209 kfree(uevent_env[2]);
1210
1211 if (ret)
1212 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1213 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
1214 batadv_uev_type_str[type],
1215 batadv_uev_action_str[action],
1216 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
1217 return ret;
1218 }