batman-adv: Convert batadv_softif_vlan to kref
[deliverable/linux.git] / net / batman-adv / sysfs.c
CommitLineData
0046b040 1/* Copyright (C) 2010-2016 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
3 * Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
ebf38fb7 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
16 */
17
b706b13b 18#include "sysfs.h"
1e2c2a4f
SE
19#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/compiler.h>
23#include <linux/device.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/if.h>
27#include <linux/if_vlan.h>
6be4d30c 28#include <linux/kref.h>
1e2c2a4f
SE
29#include <linux/kernel.h>
30#include <linux/netdevice.h>
31#include <linux/printk.h>
32#include <linux/rculist.h>
33#include <linux/rcupdate.h>
34#include <linux/rtnetlink.h>
35#include <linux/slab.h>
36#include <linux/stat.h>
37#include <linux/stddef.h>
38#include <linux/string.h>
39#include <linux/stringify.h>
40
33af49ad 41#include "distributed-arp-table.h"
1e2c2a4f
SE
42#include "gateway_client.h"
43#include "gateway_common.h"
d68081a2 44#include "bridge_loop_avoidance.h"
c6c8fea2 45#include "hard-interface.h"
1e2c2a4f
SE
46#include "network-coding.h"
47#include "packet.h"
90f4435d 48#include "soft-interface.h"
c6c8fea2 49
0ff9b86f 50static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
3d222bba
SE
51{
52 struct device *dev = container_of(obj->parent, struct device, kobj);
f138694b 53
3d222bba
SE
54 return to_net_dev(dev);
55}
56
56303d34 57static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
3d222bba 58{
0ff9b86f 59 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
f138694b 60
3d222bba
SE
61 return netdev_priv(net_dev);
62}
c6c8fea2 63
90f4435d
AQ
64/**
65 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
66 * @obj: kobject to covert
67 *
62fe710f 68 * Return: the associated batadv_priv struct.
90f4435d
AQ
69 */
70static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
71{
72 /* VLAN specific attributes are located in the root sysfs folder if they
73 * refer to the untagged VLAN..
74 */
75 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
76 return batadv_kobj_to_batpriv(obj);
77
78 /* ..while the attributes for the tagged vlans are located in
79 * the in the corresponding "vlan%VID" subfolder
80 */
81 return batadv_kobj_to_batpriv(obj->parent);
82}
83
84/**
85 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
7afcbbef 86 * @bat_priv: the bat priv with all the soft interface information
90f4435d
AQ
87 * @obj: kobject to covert
88 *
62fe710f 89 * Return: the associated softif_vlan struct if found, NULL otherwise.
90f4435d
AQ
90 */
91static struct batadv_softif_vlan *
92batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
93{
94 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
95
96 rcu_read_lock();
97 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
98 if (vlan_tmp->kobj != obj)
99 continue;
100
6be4d30c 101 if (!kref_get_unless_zero(&vlan_tmp->refcount))
90f4435d
AQ
102 continue;
103
104 vlan = vlan_tmp;
105 break;
106 }
107 rcu_read_unlock();
108
109 return vlan;
110}
111
347c80f0
SE
112#define BATADV_UEV_TYPE_VAR "BATTYPE="
113#define BATADV_UEV_ACTION_VAR "BATACTION="
114#define BATADV_UEV_DATA_VAR "BATDATA="
c6bda689 115
0ff9b86f 116static char *batadv_uev_action_str[] = {
c6bda689
AQ
117 "add",
118 "del",
119 "change"
120};
121
0ff9b86f 122static char *batadv_uev_type_str[] = {
c6bda689
AQ
123 "gw"
124};
125
90f4435d
AQ
126/* Use this, if you have customized show and store functions for vlan attrs */
127#define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
128struct batadv_attribute batadv_attr_vlan_##_name = { \
129 .attr = {.name = __stringify(_name), \
130 .mode = _mode }, \
131 .show = _show, \
132 .store = _store, \
2b64df20 133}
90f4435d 134
c6c8fea2 135/* Use this, if you have customized show and store functions */
347c80f0 136#define BATADV_ATTR(_name, _mode, _show, _store) \
b4d66b87 137struct batadv_attribute batadv_attr_##_name = { \
347c80f0
SE
138 .attr = {.name = __stringify(_name), \
139 .mode = _mode }, \
140 .show = _show, \
141 .store = _store, \
2b64df20 142}
c6c8fea2 143
347c80f0 144#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
0ff9b86f
SE
145ssize_t batadv_store_##_name(struct kobject *kobj, \
146 struct attribute *attr, char *buff, \
147 size_t count) \
c6c8fea2 148{ \
0ff9b86f 149 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
56303d34 150 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
f138694b 151 \
0ff9b86f
SE
152 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
153 &bat_priv->_name, net_dev); \
c6c8fea2
SE
154}
155
347c80f0 156#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
0ff9b86f
SE
157ssize_t batadv_show_##_name(struct kobject *kobj, \
158 struct attribute *attr, char *buff) \
c6c8fea2 159{ \
56303d34 160 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
f138694b 161 \
c6c8fea2
SE
162 return sprintf(buff, "%s\n", \
163 atomic_read(&bat_priv->_name) == 0 ? \
164 "disabled" : "enabled"); \
165} \
166
f245c38b 167/* Use this, if you are going to turn a [name] in the soft-interface
9cfc7bd6
SE
168 * (bat_priv) on or off
169 */
347c80f0
SE
170#define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
171 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
172 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
173 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
174 batadv_store_##_name)
c6c8fea2 175
6f70eb75 176#define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
0ff9b86f
SE
177ssize_t batadv_store_##_name(struct kobject *kobj, \
178 struct attribute *attr, char *buff, \
179 size_t count) \
c6c8fea2 180{ \
0ff9b86f 181 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
56303d34 182 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
f138694b 183 \
0ff9b86f
SE
184 return __batadv_store_uint_attr(buff, count, _min, _max, \
185 _post_func, attr, \
6f70eb75 186 &bat_priv->_var, net_dev); \
c6c8fea2
SE
187}
188
6f70eb75 189#define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
0ff9b86f
SE
190ssize_t batadv_show_##_name(struct kobject *kobj, \
191 struct attribute *attr, char *buff) \
c6c8fea2 192{ \
56303d34 193 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
f138694b 194 \
6f70eb75 195 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
c6c8fea2
SE
196} \
197
f245c38b 198/* Use this, if you are going to set [name] in the soft-interface
9cfc7bd6
SE
199 * (bat_priv) to an unsigned integer value
200 */
6f70eb75
AQ
201#define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
202 static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
203 static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
347c80f0
SE
204 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
205 batadv_store_##_name)
c6c8fea2 206
90f4435d
AQ
207#define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
208ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
209 struct attribute *attr, char *buff, \
210 size_t count) \
211{ \
212 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
213 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
214 kobj); \
215 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
216 attr, &vlan->_name, \
217 bat_priv->soft_iface); \
f138694b 218 \
90f4435d
AQ
219 batadv_softif_vlan_free_ref(vlan); \
220 return res; \
221}
222
223#define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
224ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
225 struct attribute *attr, char *buff) \
226{ \
227 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
228 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
229 kobj); \
230 size_t res = sprintf(buff, "%s\n", \
231 atomic_read(&vlan->_name) == 0 ? \
232 "disabled" : "enabled"); \
f138694b 233 \
90f4435d
AQ
234 batadv_softif_vlan_free_ref(vlan); \
235 return res; \
236}
237
238/* Use this, if you are going to turn a [name] in the vlan struct on or off */
239#define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
240 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
241 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
242 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
243 batadv_store_vlan_##_name)
c6c8fea2 244
0ff9b86f
SE
245static int batadv_store_bool_attr(char *buff, size_t count,
246 struct net_device *net_dev,
9e728e84
SW
247 const char *attr_name, atomic_t *attr,
248 bool *changed)
c6c8fea2
SE
249{
250 int enabled = -1;
251
9e728e84
SW
252 *changed = false;
253
c6c8fea2
SE
254 if (buff[count - 1] == '\n')
255 buff[count - 1] = '\0';
256
257 if ((strncmp(buff, "1", 2) == 0) ||
258 (strncmp(buff, "enable", 7) == 0) ||
259 (strncmp(buff, "enabled", 8) == 0))
260 enabled = 1;
261
262 if ((strncmp(buff, "0", 2) == 0) ||
263 (strncmp(buff, "disable", 8) == 0) ||
264 (strncmp(buff, "disabled", 9) == 0))
265 enabled = 0;
266
267 if (enabled < 0) {
3e34819e
SE
268 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
269 attr_name, buff);
c6c8fea2
SE
270 return -EINVAL;
271 }
272
273 if (atomic_read(attr) == enabled)
274 return count;
275
3e34819e
SE
276 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
277 atomic_read(attr) == 1 ? "enabled" : "disabled",
278 enabled == 1 ? "enabled" : "disabled");
c6c8fea2 279
9e728e84
SW
280 *changed = true;
281
95c96174 282 atomic_set(attr, (unsigned int)enabled);
c6c8fea2
SE
283 return count;
284}
285
0ff9b86f
SE
286static inline ssize_t
287__batadv_store_bool_attr(char *buff, size_t count,
288 void (*post_func)(struct net_device *),
289 struct attribute *attr,
290 atomic_t *attr_store, struct net_device *net_dev)
c6c8fea2 291{
9e728e84 292 bool changed;
c6c8fea2
SE
293 int ret;
294
0ff9b86f 295 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
9e728e84
SW
296 attr_store, &changed);
297 if (post_func && changed)
c6c8fea2
SE
298 post_func(net_dev);
299
300 return ret;
301}
302
0ff9b86f
SE
303static int batadv_store_uint_attr(const char *buff, size_t count,
304 struct net_device *net_dev,
305 const char *attr_name,
306 unsigned int min, unsigned int max,
307 atomic_t *attr)
c6c8fea2
SE
308{
309 unsigned long uint_val;
310 int ret;
311
25a92b13 312 ret = kstrtoul(buff, 10, &uint_val);
c6c8fea2 313 if (ret) {
3e34819e
SE
314 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
315 attr_name, buff);
c6c8fea2
SE
316 return -EINVAL;
317 }
318
319 if (uint_val < min) {
3e34819e
SE
320 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
321 attr_name, uint_val, min);
c6c8fea2
SE
322 return -EINVAL;
323 }
324
325 if (uint_val > max) {
3e34819e
SE
326 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
327 attr_name, uint_val, max);
c6c8fea2
SE
328 return -EINVAL;
329 }
330
331 if (atomic_read(attr) == uint_val)
332 return count;
333
3e34819e
SE
334 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
335 attr_name, atomic_read(attr), uint_val);
c6c8fea2
SE
336
337 atomic_set(attr, uint_val);
338 return count;
339}
340
0ff9b86f
SE
341static inline ssize_t
342__batadv_store_uint_attr(const char *buff, size_t count,
343 int min, int max,
344 void (*post_func)(struct net_device *),
345 const struct attribute *attr,
346 atomic_t *attr_store, struct net_device *net_dev)
c6c8fea2
SE
347{
348 int ret;
349
0ff9b86f
SE
350 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
351 attr_store);
c6c8fea2
SE
352 if (post_func && ret)
353 post_func(net_dev);
354
355 return ret;
356}
357
0ff9b86f
SE
358static ssize_t batadv_show_bat_algo(struct kobject *kobj,
359 struct attribute *attr, char *buff)
ea3d2fd1 360{
56303d34 361 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
f138694b 362
ea3d2fd1
ML
363 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
364}
365
4e820e72 366static void batadv_post_gw_reselect(struct net_device *net_dev)
c6c8fea2 367{
56303d34 368 struct batadv_priv *bat_priv = netdev_priv(net_dev);
f138694b 369
4e820e72 370 batadv_gw_reselect(bat_priv);
c6c8fea2
SE
371}
372
0ff9b86f
SE
373static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
374 char *buff)
c6c8fea2 375{
56303d34 376 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
c6c8fea2
SE
377 int bytes_written;
378
379 switch (atomic_read(&bat_priv->gw_mode)) {
cd646ab1 380 case BATADV_GW_MODE_CLIENT:
97ea4ba1
SE
381 bytes_written = sprintf(buff, "%s\n",
382 BATADV_GW_MODE_CLIENT_NAME);
c6c8fea2 383 break;
cd646ab1 384 case BATADV_GW_MODE_SERVER:
97ea4ba1
SE
385 bytes_written = sprintf(buff, "%s\n",
386 BATADV_GW_MODE_SERVER_NAME);
c6c8fea2
SE
387 break;
388 default:
97ea4ba1
SE
389 bytes_written = sprintf(buff, "%s\n",
390 BATADV_GW_MODE_OFF_NAME);
c6c8fea2
SE
391 break;
392 }
393
394 return bytes_written;
395}
396
0ff9b86f
SE
397static ssize_t batadv_store_gw_mode(struct kobject *kobj,
398 struct attribute *attr, char *buff,
399 size_t count)
c6c8fea2 400{
0ff9b86f 401 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 402 struct batadv_priv *bat_priv = netdev_priv(net_dev);
c6c8fea2
SE
403 char *curr_gw_mode_str;
404 int gw_mode_tmp = -1;
405
406 if (buff[count - 1] == '\n')
407 buff[count - 1] = '\0';
408
97ea4ba1
SE
409 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
410 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
cd646ab1 411 gw_mode_tmp = BATADV_GW_MODE_OFF;
c6c8fea2 412
97ea4ba1
SE
413 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
414 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
cd646ab1 415 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
c6c8fea2 416
97ea4ba1
SE
417 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
418 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
cd646ab1 419 gw_mode_tmp = BATADV_GW_MODE_SERVER;
c6c8fea2
SE
420
421 if (gw_mode_tmp < 0) {
3e34819e
SE
422 batadv_info(net_dev,
423 "Invalid parameter for 'gw mode' setting received: %s\n",
424 buff);
c6c8fea2
SE
425 return -EINVAL;
426 }
427
428 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
429 return count;
430
431 switch (atomic_read(&bat_priv->gw_mode)) {
cd646ab1 432 case BATADV_GW_MODE_CLIENT:
97ea4ba1 433 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
c6c8fea2 434 break;
cd646ab1 435 case BATADV_GW_MODE_SERVER:
97ea4ba1 436 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
c6c8fea2
SE
437 break;
438 default:
97ea4ba1 439 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
c6c8fea2
SE
440 break;
441 }
442
3e34819e
SE
443 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
444 curr_gw_mode_str, buff);
c6c8fea2 445
f3163181
AQ
446 /* Invoking batadv_gw_reselect() is not enough to really de-select the
447 * current GW. It will only instruct the gateway client code to perform
448 * a re-election the next time that this is needed.
449 *
450 * When gw client mode is being switched off the current GW must be
451 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
452 * client mode re-activation. This is operation is performed in
453 * batadv_gw_check_client_stop().
454 */
4e820e72 455 batadv_gw_reselect(bat_priv);
c6eaa3f0
AQ
456 /* always call batadv_gw_check_client_stop() before changing the gateway
457 * state
458 */
459 batadv_gw_check_client_stop(bat_priv);
95c96174 460 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
414254e3 461 batadv_gw_tvlv_container_update(bat_priv);
c6c8fea2
SE
462 return count;
463}
464
0ff9b86f
SE
465static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
466 struct attribute *attr, char *buff)
c6c8fea2 467{
56303d34 468 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
6b5e971a 469 u32 down, up;
414254e3
ML
470
471 down = atomic_read(&bat_priv->gw.bandwidth_down);
472 up = atomic_read(&bat_priv->gw.bandwidth_up);
473
474 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
475 down % 10, up / 10, up % 10);
c6c8fea2
SE
476}
477
0ff9b86f
SE
478static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
479 struct attribute *attr, char *buff,
480 size_t count)
c6c8fea2 481{
0ff9b86f 482 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
c6c8fea2
SE
483
484 if (buff[count - 1] == '\n')
485 buff[count - 1] = '\0';
486
84d5e5e0 487 return batadv_gw_bandwidth_set(net_dev, buff, count);
c6c8fea2
SE
488}
489
c42edfe3
AQ
490/**
491 * batadv_show_isolation_mark - print the current isolation mark/mask
492 * @kobj: kobject representing the private mesh sysfs directory
493 * @attr: the batman-adv attribute the user is interacting with
494 * @buff: the buffer that will contain the data to send back to the user
495 *
62fe710f 496 * Return: the number of bytes written into 'buff' on success or a negative
c42edfe3
AQ
497 * error code in case of failure
498 */
499static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
500 struct attribute *attr, char *buff)
501{
502 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
503
504 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
505 bat_priv->isolation_mark_mask);
506}
507
508/**
509 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
510 * by the user
511 * @kobj: kobject representing the private mesh sysfs directory
512 * @attr: the batman-adv attribute the user is interacting with
513 * @buff: the buffer containing the user data
514 * @count: number of bytes in the buffer
515 *
62fe710f 516 * Return: 'count' on success or a negative error code in case of failure
c42edfe3
AQ
517 */
518static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
519 struct attribute *attr, char *buff,
520 size_t count)
521{
522 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
523 struct batadv_priv *bat_priv = netdev_priv(net_dev);
6b5e971a 524 u32 mark, mask;
c42edfe3
AQ
525 char *mask_ptr;
526
527 /* parse the mask if it has been specified, otherwise assume the mask is
528 * the biggest possible
529 */
530 mask = 0xFFFFFFFF;
531 mask_ptr = strchr(buff, '/');
532 if (mask_ptr) {
533 *mask_ptr = '\0';
534 mask_ptr++;
535
536 /* the mask must be entered in hex base as it is going to be a
537 * bitmask and not a prefix length
538 */
539 if (kstrtou32(mask_ptr, 16, &mask) < 0)
540 return -EINVAL;
541 }
542
543 /* the mark can be entered in any base */
544 if (kstrtou32(buff, 0, &mark) < 0)
545 return -EINVAL;
546
547 bat_priv->isolation_mark_mask = mask;
548 /* erase bits not covered by the mask */
549 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
550
551 batadv_info(net_dev,
552 "New skb mark for extended isolation: %#.8x/%#.8x\n",
553 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
554
555 return count;
556}
557
347c80f0
SE
558BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
559BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
7a5cc242 560#ifdef CONFIG_BATMAN_ADV_BLA
d68081a2
SW
561BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR,
562 batadv_bla_status_update);
7a5cc242 563#endif
33af49ad 564#ifdef CONFIG_BATMAN_ADV_DAT
17cf0ea4
ML
565BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
566 batadv_dat_status_update);
33af49ad 567#endif
347c80f0 568BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
347c80f0
SE
569static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
570static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
571 batadv_store_gw_mode);
6f70eb75
AQ
572BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, S_IRUGO | S_IWUSR,
573 2 * BATADV_JITTER, INT_MAX, NULL);
574BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, S_IRUGO | S_IWUSR, 0,
575 BATADV_TQ_MAX_VALUE, NULL);
576BATADV_ATTR_SIF_UINT(gw_sel_class, gw_sel_class, S_IRUGO | S_IWUSR, 1,
577 BATADV_TQ_MAX_VALUE, batadv_post_gw_reselect);
347c80f0
SE
578static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
579 batadv_store_gw_bwidth);
1d8ab8d3
LL
580#ifdef CONFIG_BATMAN_ADV_MCAST
581BATADV_ATTR_SIF_BOOL(multicast_mode, S_IRUGO | S_IWUSR, NULL);
582#endif
c6c8fea2 583#ifdef CONFIG_BATMAN_ADV_DEBUG
6f70eb75
AQ
584BATADV_ATTR_SIF_UINT(log_level, log_level, S_IRUGO | S_IWUSR, 0,
585 BATADV_DBG_ALL, NULL);
c6c8fea2 586#endif
d353d8d4 587#ifdef CONFIG_BATMAN_ADV_NC
3f4841ff
ML
588BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
589 batadv_nc_status_update);
d353d8d4 590#endif
c42edfe3
AQ
591static BATADV_ATTR(isolation_mark, S_IRUGO | S_IWUSR,
592 batadv_show_isolation_mark, batadv_store_isolation_mark);
c6c8fea2 593
b4d66b87 594static struct batadv_attribute *batadv_mesh_attrs[] = {
0ff9b86f
SE
595 &batadv_attr_aggregated_ogms,
596 &batadv_attr_bonding,
7a5cc242 597#ifdef CONFIG_BATMAN_ADV_BLA
0ff9b86f 598 &batadv_attr_bridge_loop_avoidance,
33af49ad
AQ
599#endif
600#ifdef CONFIG_BATMAN_ADV_DAT
601 &batadv_attr_distributed_arp_table,
1d8ab8d3
LL
602#endif
603#ifdef CONFIG_BATMAN_ADV_MCAST
604 &batadv_attr_multicast_mode,
7a5cc242 605#endif
0ff9b86f 606 &batadv_attr_fragmentation,
0ff9b86f
SE
607 &batadv_attr_routing_algo,
608 &batadv_attr_gw_mode,
609 &batadv_attr_orig_interval,
610 &batadv_attr_hop_penalty,
611 &batadv_attr_gw_sel_class,
612 &batadv_attr_gw_bandwidth,
c6c8fea2 613#ifdef CONFIG_BATMAN_ADV_DEBUG
0ff9b86f 614 &batadv_attr_log_level,
d353d8d4
MH
615#endif
616#ifdef CONFIG_BATMAN_ADV_NC
617 &batadv_attr_network_coding,
c6c8fea2 618#endif
c42edfe3 619 &batadv_attr_isolation_mark,
c6c8fea2
SE
620 NULL,
621};
622
b8cbd81d
AQ
623BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
624
f34ac9d4 625/* array of vlan specific sysfs attributes */
90f4435d 626static struct batadv_attribute *batadv_vlan_attrs[] = {
b8cbd81d 627 &batadv_attr_vlan_ap_isolation,
90f4435d
AQ
628 NULL,
629};
630
5853e22c 631int batadv_sysfs_add_meshif(struct net_device *dev)
c6c8fea2
SE
632{
633 struct kobject *batif_kobject = &dev->dev.kobj;
56303d34 634 struct batadv_priv *bat_priv = netdev_priv(dev);
b4d66b87 635 struct batadv_attribute **bat_attr;
c6c8fea2
SE
636 int err;
637
036cbfeb 638 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
c6c8fea2
SE
639 batif_kobject);
640 if (!bat_priv->mesh_obj) {
3e34819e 641 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
036cbfeb 642 BATADV_SYSFS_IF_MESH_SUBDIR);
c6c8fea2
SE
643 goto out;
644 }
645
0ff9b86f 646 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
c6c8fea2
SE
647 err = sysfs_create_file(bat_priv->mesh_obj,
648 &((*bat_attr)->attr));
649 if (err) {
3e34819e 650 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
036cbfeb 651 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
3e34819e 652 ((*bat_attr)->attr).name);
c6c8fea2
SE
653 goto rem_attr;
654 }
655 }
656
657 return 0;
658
659rem_attr:
0ff9b86f 660 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
c6c8fea2
SE
661 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
662
663 kobject_put(bat_priv->mesh_obj);
664 bat_priv->mesh_obj = NULL;
665out:
666 return -ENOMEM;
667}
668
5853e22c 669void batadv_sysfs_del_meshif(struct net_device *dev)
c6c8fea2 670{
56303d34 671 struct batadv_priv *bat_priv = netdev_priv(dev);
b4d66b87 672 struct batadv_attribute **bat_attr;
c6c8fea2 673
0ff9b86f 674 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
c6c8fea2
SE
675 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
676
677 kobject_put(bat_priv->mesh_obj);
678 bat_priv->mesh_obj = NULL;
679}
680
90f4435d
AQ
681/**
682 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
683 * @dev: netdev of the mesh interface
684 * @vlan: private data of the newly added VLAN interface
685 *
62fe710f 686 * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
90f4435d
AQ
687 */
688int batadv_sysfs_add_vlan(struct net_device *dev,
689 struct batadv_softif_vlan *vlan)
690{
691 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
692 struct batadv_priv *bat_priv = netdev_priv(dev);
693 struct batadv_attribute **bat_attr;
694 int err;
695
696 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
697 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
698 vlan->vid & VLAN_VID_MASK);
699
700 vlan->kobj = kobject_create_and_add(vlan_subdir,
701 bat_priv->mesh_obj);
702 if (!vlan->kobj) {
703 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
704 dev->name, vlan_subdir);
705 goto out;
706 }
707 } else {
708 /* the untagged LAN uses the root folder to store its "VLAN
709 * specific attributes"
710 */
711 vlan->kobj = bat_priv->mesh_obj;
712 kobject_get(bat_priv->mesh_obj);
713 }
714
715 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
716 err = sysfs_create_file(vlan->kobj,
717 &((*bat_attr)->attr));
718 if (err) {
719 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
720 dev->name, vlan_subdir,
721 ((*bat_attr)->attr).name);
722 goto rem_attr;
723 }
724 }
725
726 return 0;
727
728rem_attr:
729 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
730 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
731
732 kobject_put(vlan->kobj);
733 vlan->kobj = NULL;
734out:
735 return -ENOMEM;
736}
737
738/**
739 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
740 * @bat_priv: the bat priv with all the soft interface information
741 * @vlan: the private data of the VLAN to destroy
742 */
743void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
744 struct batadv_softif_vlan *vlan)
745{
746 struct batadv_attribute **bat_attr;
747
748 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
749 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
750
751 kobject_put(vlan->kobj);
752 vlan->kobj = NULL;
753}
754
0ff9b86f
SE
755static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
756 struct attribute *attr, char *buff)
c6c8fea2 757{
0ff9b86f 758 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 759 struct batadv_hard_iface *hard_iface;
c6c8fea2 760 ssize_t length;
e9a4f295 761 const char *ifname;
c6c8fea2 762
56303d34 763 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 764 if (!hard_iface)
c6c8fea2
SE
765 return 0;
766
e9a4f295
SE
767 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
768 ifname = "none";
769 else
770 ifname = hard_iface->soft_iface->name;
771
772 length = sprintf(buff, "%s\n", ifname);
c6c8fea2 773
e5d89254 774 batadv_hardif_free_ref(hard_iface);
c6c8fea2
SE
775
776 return length;
777}
778
0ff9b86f
SE
779static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
780 struct attribute *attr, char *buff,
781 size_t count)
c6c8fea2 782{
0ff9b86f 783 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 784 struct batadv_hard_iface *hard_iface;
c6c8fea2 785 int status_tmp = -1;
ed75ccbe 786 int ret = count;
c6c8fea2 787
56303d34 788 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 789 if (!hard_iface)
c6c8fea2
SE
790 return count;
791
792 if (buff[count - 1] == '\n')
793 buff[count - 1] = '\0';
794
795 if (strlen(buff) >= IFNAMSIZ) {
86ceb360
SE
796 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
797 buff);
e5d89254 798 batadv_hardif_free_ref(hard_iface);
c6c8fea2
SE
799 return -EINVAL;
800 }
801
802 if (strncmp(buff, "none", 4) == 0)
e9a4f295 803 status_tmp = BATADV_IF_NOT_IN_USE;
c6c8fea2 804 else
e9a4f295 805 status_tmp = BATADV_IF_I_WANT_YOU;
c6c8fea2 806
e6c10f43
ML
807 if (hard_iface->if_status == status_tmp)
808 goto out;
809
810 if ((hard_iface->soft_iface) &&
811 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
ed75ccbe 812 goto out;
c6c8fea2 813
ac16d148 814 rtnl_lock();
3a4375a9 815
e9a4f295 816 if (status_tmp == BATADV_IF_NOT_IN_USE) {
a15fd361
SE
817 batadv_hardif_disable_interface(hard_iface,
818 BATADV_IF_CLEANUP_AUTO);
3a4375a9 819 goto unlock;
c6c8fea2
SE
820 }
821
822 /* if the interface already is in use */
e9a4f295 823 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
a15fd361
SE
824 batadv_hardif_disable_interface(hard_iface,
825 BATADV_IF_CLEANUP_AUTO);
c6c8fea2 826
9563877e 827 ret = batadv_hardif_enable_interface(hard_iface, buff);
c6c8fea2 828
3a4375a9
SE
829unlock:
830 rtnl_unlock();
ed75ccbe 831out:
e5d89254 832 batadv_hardif_free_ref(hard_iface);
c6c8fea2
SE
833 return ret;
834}
835
0ff9b86f
SE
836static ssize_t batadv_show_iface_status(struct kobject *kobj,
837 struct attribute *attr, char *buff)
c6c8fea2 838{
0ff9b86f 839 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 840 struct batadv_hard_iface *hard_iface;
c6c8fea2
SE
841 ssize_t length;
842
56303d34 843 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 844 if (!hard_iface)
c6c8fea2
SE
845 return 0;
846
e6c10f43 847 switch (hard_iface->if_status) {
e9a4f295 848 case BATADV_IF_TO_BE_REMOVED:
c6c8fea2
SE
849 length = sprintf(buff, "disabling\n");
850 break;
e9a4f295 851 case BATADV_IF_INACTIVE:
c6c8fea2
SE
852 length = sprintf(buff, "inactive\n");
853 break;
e9a4f295 854 case BATADV_IF_ACTIVE:
c6c8fea2
SE
855 length = sprintf(buff, "active\n");
856 break;
e9a4f295 857 case BATADV_IF_TO_BE_ACTIVATED:
c6c8fea2
SE
858 length = sprintf(buff, "enabling\n");
859 break;
e9a4f295 860 case BATADV_IF_NOT_IN_USE:
c6c8fea2
SE
861 default:
862 length = sprintf(buff, "not in use\n");
863 break;
864 }
865
e5d89254 866 batadv_hardif_free_ref(hard_iface);
c6c8fea2
SE
867
868 return length;
869}
870
347c80f0
SE
871static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
872 batadv_store_mesh_iface);
873static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
c6c8fea2 874
b4d66b87 875static struct batadv_attribute *batadv_batman_attrs[] = {
0ff9b86f
SE
876 &batadv_attr_mesh_iface,
877 &batadv_attr_iface_status,
c6c8fea2
SE
878 NULL,
879};
880
5853e22c 881int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
c6c8fea2
SE
882{
883 struct kobject *hardif_kobject = &dev->dev.kobj;
b4d66b87 884 struct batadv_attribute **bat_attr;
c6c8fea2
SE
885 int err;
886
036cbfeb
SE
887 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
888 hardif_kobject);
c6c8fea2
SE
889
890 if (!*hardif_obj) {
3e34819e 891 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
036cbfeb 892 BATADV_SYSFS_IF_BAT_SUBDIR);
c6c8fea2
SE
893 goto out;
894 }
895
0ff9b86f 896 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
c6c8fea2
SE
897 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
898 if (err) {
3e34819e 899 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
036cbfeb 900 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
3e34819e 901 ((*bat_attr)->attr).name);
c6c8fea2
SE
902 goto rem_attr;
903 }
904 }
905
906 return 0;
907
908rem_attr:
0ff9b86f 909 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
c6c8fea2
SE
910 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
911out:
912 return -ENOMEM;
913}
914
5853e22c 915void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
c6c8fea2
SE
916{
917 kobject_put(*hardif_obj);
918 *hardif_obj = NULL;
919}
c6bda689 920
56303d34 921int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
39c75a51 922 enum batadv_uev_action action, const char *data)
c6bda689 923{
5346c35e 924 int ret = -ENOMEM;
c6bda689
AQ
925 struct kobject *bat_kobj;
926 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
927
736292c2 928 bat_kobj = &bat_priv->soft_iface->dev.kobj;
c6bda689 929
b98fe24c
HS
930 uevent_env[0] = kasprintf(GFP_ATOMIC,
931 "%s%s", BATADV_UEV_TYPE_VAR,
932 batadv_uev_type_str[type]);
c6bda689
AQ
933 if (!uevent_env[0])
934 goto out;
935
b98fe24c
HS
936 uevent_env[1] = kasprintf(GFP_ATOMIC,
937 "%s%s", BATADV_UEV_ACTION_VAR,
938 batadv_uev_action_str[action]);
c6bda689
AQ
939 if (!uevent_env[1])
940 goto out;
941
c6bda689 942 /* If the event is DEL, ignore the data field */
39c75a51 943 if (action != BATADV_UEV_DEL) {
b98fe24c
HS
944 uevent_env[2] = kasprintf(GFP_ATOMIC,
945 "%s%s", BATADV_UEV_DATA_VAR, data);
c6bda689
AQ
946 if (!uevent_env[2])
947 goto out;
c6bda689
AQ
948 }
949
950 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
951out:
952 kfree(uevent_env[0]);
953 kfree(uevent_env[1]);
954 kfree(uevent_env[2]);
955
c6bda689 956 if (ret)
39c75a51 957 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 958 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
0ff9b86f
SE
959 batadv_uev_type_str[type],
960 batadv_uev_action_str[action],
39c75a51 961 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
c6bda689
AQ
962 return ret;
963}
This page took 0.51423 seconds and 5 git commands to generate.