]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/batman-adv/sysfs.c
Merge branch 'acpi-hotplug'
[mirror_ubuntu-bionic-kernel.git] / net / batman-adv / sysfs.c
CommitLineData
0b873931 1/* Copyright (C) 2010-2013 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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
20#include "main.h"
b706b13b 21#include "sysfs.h"
c6c8fea2 22#include "translation-table.h"
33af49ad 23#include "distributed-arp-table.h"
3f4841ff 24#include "network-coding.h"
c6c8fea2
SE
25#include "originator.h"
26#include "hard-interface.h"
90f4435d 27#include "soft-interface.h"
c6c8fea2
SE
28#include "gateway_common.h"
29#include "gateway_client.h"
c6c8fea2 30
0ff9b86f 31static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
3d222bba
SE
32{
33 struct device *dev = container_of(obj->parent, struct device, kobj);
34 return to_net_dev(dev);
35}
36
56303d34 37static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
3d222bba 38{
0ff9b86f 39 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
3d222bba
SE
40 return netdev_priv(net_dev);
41}
c6c8fea2 42
90f4435d
AQ
43/**
44 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
45 * @obj: kobject to covert
46 *
47 * Returns the associated batadv_priv struct.
48 */
49static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
50{
51 /* VLAN specific attributes are located in the root sysfs folder if they
52 * refer to the untagged VLAN..
53 */
54 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
55 return batadv_kobj_to_batpriv(obj);
56
57 /* ..while the attributes for the tagged vlans are located in
58 * the in the corresponding "vlan%VID" subfolder
59 */
60 return batadv_kobj_to_batpriv(obj->parent);
61}
62
63/**
64 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
65 * @obj: kobject to covert
66 *
67 * Returns the associated softif_vlan struct if found, NULL otherwise.
68 */
69static struct batadv_softif_vlan *
70batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
71{
72 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
73
74 rcu_read_lock();
75 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
76 if (vlan_tmp->kobj != obj)
77 continue;
78
79 if (!atomic_inc_not_zero(&vlan_tmp->refcount))
80 continue;
81
82 vlan = vlan_tmp;
83 break;
84 }
85 rcu_read_unlock();
86
87 return vlan;
88}
89
347c80f0
SE
90#define BATADV_UEV_TYPE_VAR "BATTYPE="
91#define BATADV_UEV_ACTION_VAR "BATACTION="
92#define BATADV_UEV_DATA_VAR "BATDATA="
c6bda689 93
0ff9b86f 94static char *batadv_uev_action_str[] = {
c6bda689
AQ
95 "add",
96 "del",
97 "change"
98};
99
0ff9b86f 100static char *batadv_uev_type_str[] = {
c6bda689
AQ
101 "gw"
102};
103
90f4435d
AQ
104/* Use this, if you have customized show and store functions for vlan attrs */
105#define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
106struct batadv_attribute batadv_attr_vlan_##_name = { \
107 .attr = {.name = __stringify(_name), \
108 .mode = _mode }, \
109 .show = _show, \
110 .store = _store, \
111};
112
c6c8fea2 113/* Use this, if you have customized show and store functions */
347c80f0 114#define BATADV_ATTR(_name, _mode, _show, _store) \
b4d66b87 115struct batadv_attribute batadv_attr_##_name = { \
347c80f0
SE
116 .attr = {.name = __stringify(_name), \
117 .mode = _mode }, \
118 .show = _show, \
119 .store = _store, \
c6c8fea2
SE
120};
121
347c80f0 122#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
0ff9b86f
SE
123ssize_t batadv_store_##_name(struct kobject *kobj, \
124 struct attribute *attr, char *buff, \
125 size_t count) \
c6c8fea2 126{ \
0ff9b86f 127 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
56303d34 128 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
0ff9b86f
SE
129 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
130 &bat_priv->_name, net_dev); \
c6c8fea2
SE
131}
132
347c80f0 133#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
0ff9b86f
SE
134ssize_t batadv_show_##_name(struct kobject *kobj, \
135 struct attribute *attr, char *buff) \
c6c8fea2 136{ \
56303d34 137 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
c6c8fea2
SE
138 return sprintf(buff, "%s\n", \
139 atomic_read(&bat_priv->_name) == 0 ? \
140 "disabled" : "enabled"); \
141} \
142
f245c38b 143/* Use this, if you are going to turn a [name] in the soft-interface
9cfc7bd6
SE
144 * (bat_priv) on or off
145 */
347c80f0
SE
146#define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
147 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
148 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
149 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
150 batadv_store_##_name)
c6c8fea2
SE
151
152
347c80f0 153#define BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
0ff9b86f
SE
154ssize_t batadv_store_##_name(struct kobject *kobj, \
155 struct attribute *attr, char *buff, \
156 size_t count) \
c6c8fea2 157{ \
0ff9b86f 158 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
56303d34 159 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
0ff9b86f
SE
160 return __batadv_store_uint_attr(buff, count, _min, _max, \
161 _post_func, attr, \
162 &bat_priv->_name, net_dev); \
c6c8fea2
SE
163}
164
347c80f0 165#define BATADV_ATTR_SIF_SHOW_UINT(_name) \
0ff9b86f
SE
166ssize_t batadv_show_##_name(struct kobject *kobj, \
167 struct attribute *attr, char *buff) \
c6c8fea2 168{ \
56303d34 169 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
c6c8fea2
SE
170 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
171} \
172
f245c38b 173/* Use this, if you are going to set [name] in the soft-interface
9cfc7bd6
SE
174 * (bat_priv) to an unsigned integer value
175 */
347c80f0
SE
176#define BATADV_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
177 static BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)\
178 static BATADV_ATTR_SIF_SHOW_UINT(_name) \
179 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
180 batadv_store_##_name)
c6c8fea2 181
90f4435d
AQ
182#define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
183ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
184 struct attribute *attr, char *buff, \
185 size_t count) \
186{ \
187 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
188 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
189 kobj); \
190 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
191 attr, &vlan->_name, \
192 bat_priv->soft_iface); \
193 batadv_softif_vlan_free_ref(vlan); \
194 return res; \
195}
196
197#define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
198ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
199 struct attribute *attr, char *buff) \
200{ \
201 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
202 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
203 kobj); \
204 size_t res = sprintf(buff, "%s\n", \
205 atomic_read(&vlan->_name) == 0 ? \
206 "disabled" : "enabled"); \
207 batadv_softif_vlan_free_ref(vlan); \
208 return res; \
209}
210
211/* Use this, if you are going to turn a [name] in the vlan struct on or off */
212#define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
213 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
214 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
215 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
216 batadv_store_vlan_##_name)
c6c8fea2 217
0ff9b86f
SE
218static int batadv_store_bool_attr(char *buff, size_t count,
219 struct net_device *net_dev,
220 const char *attr_name, atomic_t *attr)
c6c8fea2
SE
221{
222 int enabled = -1;
223
224 if (buff[count - 1] == '\n')
225 buff[count - 1] = '\0';
226
227 if ((strncmp(buff, "1", 2) == 0) ||
228 (strncmp(buff, "enable", 7) == 0) ||
229 (strncmp(buff, "enabled", 8) == 0))
230 enabled = 1;
231
232 if ((strncmp(buff, "0", 2) == 0) ||
233 (strncmp(buff, "disable", 8) == 0) ||
234 (strncmp(buff, "disabled", 9) == 0))
235 enabled = 0;
236
237 if (enabled < 0) {
3e34819e
SE
238 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
239 attr_name, buff);
c6c8fea2
SE
240 return -EINVAL;
241 }
242
243 if (atomic_read(attr) == enabled)
244 return count;
245
3e34819e
SE
246 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
247 atomic_read(attr) == 1 ? "enabled" : "disabled",
248 enabled == 1 ? "enabled" : "disabled");
c6c8fea2 249
95c96174 250 atomic_set(attr, (unsigned int)enabled);
c6c8fea2
SE
251 return count;
252}
253
0ff9b86f
SE
254static inline ssize_t
255__batadv_store_bool_attr(char *buff, size_t count,
256 void (*post_func)(struct net_device *),
257 struct attribute *attr,
258 atomic_t *attr_store, struct net_device *net_dev)
c6c8fea2
SE
259{
260 int ret;
261
0ff9b86f
SE
262 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
263 attr_store);
c6c8fea2
SE
264 if (post_func && ret)
265 post_func(net_dev);
266
267 return ret;
268}
269
0ff9b86f
SE
270static int batadv_store_uint_attr(const char *buff, size_t count,
271 struct net_device *net_dev,
272 const char *attr_name,
273 unsigned int min, unsigned int max,
274 atomic_t *attr)
c6c8fea2
SE
275{
276 unsigned long uint_val;
277 int ret;
278
25a92b13 279 ret = kstrtoul(buff, 10, &uint_val);
c6c8fea2 280 if (ret) {
3e34819e
SE
281 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
282 attr_name, buff);
c6c8fea2
SE
283 return -EINVAL;
284 }
285
286 if (uint_val < min) {
3e34819e
SE
287 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
288 attr_name, uint_val, min);
c6c8fea2
SE
289 return -EINVAL;
290 }
291
292 if (uint_val > max) {
3e34819e
SE
293 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
294 attr_name, uint_val, max);
c6c8fea2
SE
295 return -EINVAL;
296 }
297
298 if (atomic_read(attr) == uint_val)
299 return count;
300
3e34819e
SE
301 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
302 attr_name, atomic_read(attr), uint_val);
c6c8fea2
SE
303
304 atomic_set(attr, uint_val);
305 return count;
306}
307
0ff9b86f
SE
308static inline ssize_t
309__batadv_store_uint_attr(const char *buff, size_t count,
310 int min, int max,
311 void (*post_func)(struct net_device *),
312 const struct attribute *attr,
313 atomic_t *attr_store, struct net_device *net_dev)
c6c8fea2
SE
314{
315 int ret;
316
0ff9b86f
SE
317 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
318 attr_store);
c6c8fea2
SE
319 if (post_func && ret)
320 post_func(net_dev);
321
322 return ret;
323}
324
0ff9b86f
SE
325static ssize_t batadv_show_bat_algo(struct kobject *kobj,
326 struct attribute *attr, char *buff)
ea3d2fd1 327{
56303d34 328 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
ea3d2fd1
ML
329 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
330}
331
0ff9b86f 332static void batadv_post_gw_deselect(struct net_device *net_dev)
c6c8fea2 333{
56303d34 334 struct batadv_priv *bat_priv = netdev_priv(net_dev);
7cf06bc6 335 batadv_gw_deselect(bat_priv);
c6c8fea2
SE
336}
337
0ff9b86f
SE
338static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
339 char *buff)
c6c8fea2 340{
56303d34 341 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
c6c8fea2
SE
342 int bytes_written;
343
344 switch (atomic_read(&bat_priv->gw_mode)) {
cd646ab1 345 case BATADV_GW_MODE_CLIENT:
97ea4ba1
SE
346 bytes_written = sprintf(buff, "%s\n",
347 BATADV_GW_MODE_CLIENT_NAME);
c6c8fea2 348 break;
cd646ab1 349 case BATADV_GW_MODE_SERVER:
97ea4ba1
SE
350 bytes_written = sprintf(buff, "%s\n",
351 BATADV_GW_MODE_SERVER_NAME);
c6c8fea2
SE
352 break;
353 default:
97ea4ba1
SE
354 bytes_written = sprintf(buff, "%s\n",
355 BATADV_GW_MODE_OFF_NAME);
c6c8fea2
SE
356 break;
357 }
358
359 return bytes_written;
360}
361
0ff9b86f
SE
362static ssize_t batadv_store_gw_mode(struct kobject *kobj,
363 struct attribute *attr, char *buff,
364 size_t count)
c6c8fea2 365{
0ff9b86f 366 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 367 struct batadv_priv *bat_priv = netdev_priv(net_dev);
c6c8fea2
SE
368 char *curr_gw_mode_str;
369 int gw_mode_tmp = -1;
370
371 if (buff[count - 1] == '\n')
372 buff[count - 1] = '\0';
373
97ea4ba1
SE
374 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
375 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
cd646ab1 376 gw_mode_tmp = BATADV_GW_MODE_OFF;
c6c8fea2 377
97ea4ba1
SE
378 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
379 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
cd646ab1 380 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
c6c8fea2 381
97ea4ba1
SE
382 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
383 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
cd646ab1 384 gw_mode_tmp = BATADV_GW_MODE_SERVER;
c6c8fea2
SE
385
386 if (gw_mode_tmp < 0) {
3e34819e
SE
387 batadv_info(net_dev,
388 "Invalid parameter for 'gw mode' setting received: %s\n",
389 buff);
c6c8fea2
SE
390 return -EINVAL;
391 }
392
393 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
394 return count;
395
396 switch (atomic_read(&bat_priv->gw_mode)) {
cd646ab1 397 case BATADV_GW_MODE_CLIENT:
97ea4ba1 398 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
c6c8fea2 399 break;
cd646ab1 400 case BATADV_GW_MODE_SERVER:
97ea4ba1 401 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
c6c8fea2
SE
402 break;
403 default:
97ea4ba1 404 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
c6c8fea2
SE
405 break;
406 }
407
3e34819e
SE
408 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
409 curr_gw_mode_str, buff);
c6c8fea2 410
7cf06bc6 411 batadv_gw_deselect(bat_priv);
c6eaa3f0
AQ
412 /* always call batadv_gw_check_client_stop() before changing the gateway
413 * state
414 */
415 batadv_gw_check_client_stop(bat_priv);
95c96174 416 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
414254e3 417 batadv_gw_tvlv_container_update(bat_priv);
c6c8fea2
SE
418 return count;
419}
420
0ff9b86f
SE
421static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
422 struct attribute *attr, char *buff)
c6c8fea2 423{
56303d34 424 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
414254e3
ML
425 uint32_t down, up;
426
427 down = atomic_read(&bat_priv->gw.bandwidth_down);
428 up = atomic_read(&bat_priv->gw.bandwidth_up);
429
430 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
431 down % 10, up / 10, up % 10);
c6c8fea2
SE
432}
433
0ff9b86f
SE
434static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
435 struct attribute *attr, char *buff,
436 size_t count)
c6c8fea2 437{
0ff9b86f 438 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
c6c8fea2
SE
439
440 if (buff[count - 1] == '\n')
441 buff[count - 1] = '\0';
442
84d5e5e0 443 return batadv_gw_bandwidth_set(net_dev, buff, count);
c6c8fea2
SE
444}
445
347c80f0
SE
446BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
447BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
7a5cc242 448#ifdef CONFIG_BATMAN_ADV_BLA
347c80f0 449BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
7a5cc242 450#endif
33af49ad 451#ifdef CONFIG_BATMAN_ADV_DAT
17cf0ea4
ML
452BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
453 batadv_dat_status_update);
33af49ad 454#endif
347c80f0 455BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
347c80f0
SE
456static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
457static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
458 batadv_store_gw_mode);
42d0b044
SE
459BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
460 INT_MAX, NULL);
461BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
347c80f0 462 NULL);
42d0b044 463BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
347c80f0
SE
464 batadv_post_gw_deselect);
465static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
466 batadv_store_gw_bwidth);
c6c8fea2 467#ifdef CONFIG_BATMAN_ADV_DEBUG
39c75a51 468BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
c6c8fea2 469#endif
d353d8d4 470#ifdef CONFIG_BATMAN_ADV_NC
3f4841ff
ML
471BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
472 batadv_nc_status_update);
d353d8d4 473#endif
c6c8fea2 474
b4d66b87 475static struct batadv_attribute *batadv_mesh_attrs[] = {
0ff9b86f
SE
476 &batadv_attr_aggregated_ogms,
477 &batadv_attr_bonding,
7a5cc242 478#ifdef CONFIG_BATMAN_ADV_BLA
0ff9b86f 479 &batadv_attr_bridge_loop_avoidance,
33af49ad
AQ
480#endif
481#ifdef CONFIG_BATMAN_ADV_DAT
482 &batadv_attr_distributed_arp_table,
7a5cc242 483#endif
0ff9b86f 484 &batadv_attr_fragmentation,
0ff9b86f
SE
485 &batadv_attr_routing_algo,
486 &batadv_attr_gw_mode,
487 &batadv_attr_orig_interval,
488 &batadv_attr_hop_penalty,
489 &batadv_attr_gw_sel_class,
490 &batadv_attr_gw_bandwidth,
c6c8fea2 491#ifdef CONFIG_BATMAN_ADV_DEBUG
0ff9b86f 492 &batadv_attr_log_level,
d353d8d4
MH
493#endif
494#ifdef CONFIG_BATMAN_ADV_NC
495 &batadv_attr_network_coding,
c6c8fea2
SE
496#endif
497 NULL,
498};
499
b8cbd81d
AQ
500BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
501
90f4435d
AQ
502/**
503 * batadv_vlan_attrs - array of vlan specific sysfs attributes
504 */
505static struct batadv_attribute *batadv_vlan_attrs[] = {
b8cbd81d 506 &batadv_attr_vlan_ap_isolation,
90f4435d
AQ
507 NULL,
508};
509
5853e22c 510int batadv_sysfs_add_meshif(struct net_device *dev)
c6c8fea2
SE
511{
512 struct kobject *batif_kobject = &dev->dev.kobj;
56303d34 513 struct batadv_priv *bat_priv = netdev_priv(dev);
b4d66b87 514 struct batadv_attribute **bat_attr;
c6c8fea2
SE
515 int err;
516
036cbfeb 517 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
c6c8fea2
SE
518 batif_kobject);
519 if (!bat_priv->mesh_obj) {
3e34819e 520 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
036cbfeb 521 BATADV_SYSFS_IF_MESH_SUBDIR);
c6c8fea2
SE
522 goto out;
523 }
524
0ff9b86f 525 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
c6c8fea2
SE
526 err = sysfs_create_file(bat_priv->mesh_obj,
527 &((*bat_attr)->attr));
528 if (err) {
3e34819e 529 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
036cbfeb 530 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
3e34819e 531 ((*bat_attr)->attr).name);
c6c8fea2
SE
532 goto rem_attr;
533 }
534 }
535
536 return 0;
537
538rem_attr:
0ff9b86f 539 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
c6c8fea2
SE
540 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
541
542 kobject_put(bat_priv->mesh_obj);
543 bat_priv->mesh_obj = NULL;
544out:
545 return -ENOMEM;
546}
547
5853e22c 548void batadv_sysfs_del_meshif(struct net_device *dev)
c6c8fea2 549{
56303d34 550 struct batadv_priv *bat_priv = netdev_priv(dev);
b4d66b87 551 struct batadv_attribute **bat_attr;
c6c8fea2 552
0ff9b86f 553 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
c6c8fea2
SE
554 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
555
556 kobject_put(bat_priv->mesh_obj);
557 bat_priv->mesh_obj = NULL;
558}
559
90f4435d
AQ
560/**
561 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
562 * @dev: netdev of the mesh interface
563 * @vlan: private data of the newly added VLAN interface
564 *
565 * Returns 0 on success and -ENOMEM if any of the structure allocations fails.
566 */
567int batadv_sysfs_add_vlan(struct net_device *dev,
568 struct batadv_softif_vlan *vlan)
569{
570 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
571 struct batadv_priv *bat_priv = netdev_priv(dev);
572 struct batadv_attribute **bat_attr;
573 int err;
574
575 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
576 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
577 vlan->vid & VLAN_VID_MASK);
578
579 vlan->kobj = kobject_create_and_add(vlan_subdir,
580 bat_priv->mesh_obj);
581 if (!vlan->kobj) {
582 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
583 dev->name, vlan_subdir);
584 goto out;
585 }
586 } else {
587 /* the untagged LAN uses the root folder to store its "VLAN
588 * specific attributes"
589 */
590 vlan->kobj = bat_priv->mesh_obj;
591 kobject_get(bat_priv->mesh_obj);
592 }
593
594 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
595 err = sysfs_create_file(vlan->kobj,
596 &((*bat_attr)->attr));
597 if (err) {
598 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
599 dev->name, vlan_subdir,
600 ((*bat_attr)->attr).name);
601 goto rem_attr;
602 }
603 }
604
605 return 0;
606
607rem_attr:
608 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
609 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
610
611 kobject_put(vlan->kobj);
612 vlan->kobj = NULL;
613out:
614 return -ENOMEM;
615}
616
617/**
618 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
619 * @bat_priv: the bat priv with all the soft interface information
620 * @vlan: the private data of the VLAN to destroy
621 */
622void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
623 struct batadv_softif_vlan *vlan)
624{
625 struct batadv_attribute **bat_attr;
626
627 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
628 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
629
630 kobject_put(vlan->kobj);
631 vlan->kobj = NULL;
632}
633
0ff9b86f
SE
634static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
635 struct attribute *attr, char *buff)
c6c8fea2 636{
0ff9b86f 637 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 638 struct batadv_hard_iface *hard_iface;
c6c8fea2 639 ssize_t length;
e9a4f295 640 const char *ifname;
c6c8fea2 641
56303d34 642 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 643 if (!hard_iface)
c6c8fea2
SE
644 return 0;
645
e9a4f295
SE
646 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
647 ifname = "none";
648 else
649 ifname = hard_iface->soft_iface->name;
650
651 length = sprintf(buff, "%s\n", ifname);
c6c8fea2 652
e5d89254 653 batadv_hardif_free_ref(hard_iface);
c6c8fea2
SE
654
655 return length;
656}
657
0ff9b86f
SE
658static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
659 struct attribute *attr, char *buff,
660 size_t count)
c6c8fea2 661{
0ff9b86f 662 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 663 struct batadv_hard_iface *hard_iface;
c6c8fea2 664 int status_tmp = -1;
ed75ccbe 665 int ret = count;
c6c8fea2 666
56303d34 667 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 668 if (!hard_iface)
c6c8fea2
SE
669 return count;
670
671 if (buff[count - 1] == '\n')
672 buff[count - 1] = '\0';
673
674 if (strlen(buff) >= IFNAMSIZ) {
86ceb360
SE
675 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
676 buff);
e5d89254 677 batadv_hardif_free_ref(hard_iface);
c6c8fea2
SE
678 return -EINVAL;
679 }
680
681 if (strncmp(buff, "none", 4) == 0)
e9a4f295 682 status_tmp = BATADV_IF_NOT_IN_USE;
c6c8fea2 683 else
e9a4f295 684 status_tmp = BATADV_IF_I_WANT_YOU;
c6c8fea2 685
e6c10f43
ML
686 if (hard_iface->if_status == status_tmp)
687 goto out;
688
689 if ((hard_iface->soft_iface) &&
690 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
ed75ccbe 691 goto out;
c6c8fea2 692
ac16d148 693 rtnl_lock();
3a4375a9 694
e9a4f295 695 if (status_tmp == BATADV_IF_NOT_IN_USE) {
a15fd361
SE
696 batadv_hardif_disable_interface(hard_iface,
697 BATADV_IF_CLEANUP_AUTO);
3a4375a9 698 goto unlock;
c6c8fea2
SE
699 }
700
701 /* if the interface already is in use */
e9a4f295 702 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
a15fd361
SE
703 batadv_hardif_disable_interface(hard_iface,
704 BATADV_IF_CLEANUP_AUTO);
c6c8fea2 705
9563877e 706 ret = batadv_hardif_enable_interface(hard_iface, buff);
c6c8fea2 707
3a4375a9
SE
708unlock:
709 rtnl_unlock();
ed75ccbe 710out:
e5d89254 711 batadv_hardif_free_ref(hard_iface);
c6c8fea2
SE
712 return ret;
713}
714
0ff9b86f
SE
715static ssize_t batadv_show_iface_status(struct kobject *kobj,
716 struct attribute *attr, char *buff)
c6c8fea2 717{
0ff9b86f 718 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
56303d34 719 struct batadv_hard_iface *hard_iface;
c6c8fea2
SE
720 ssize_t length;
721
56303d34 722 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 723 if (!hard_iface)
c6c8fea2
SE
724 return 0;
725
e6c10f43 726 switch (hard_iface->if_status) {
e9a4f295 727 case BATADV_IF_TO_BE_REMOVED:
c6c8fea2
SE
728 length = sprintf(buff, "disabling\n");
729 break;
e9a4f295 730 case BATADV_IF_INACTIVE:
c6c8fea2
SE
731 length = sprintf(buff, "inactive\n");
732 break;
e9a4f295 733 case BATADV_IF_ACTIVE:
c6c8fea2
SE
734 length = sprintf(buff, "active\n");
735 break;
e9a4f295 736 case BATADV_IF_TO_BE_ACTIVATED:
c6c8fea2
SE
737 length = sprintf(buff, "enabling\n");
738 break;
e9a4f295 739 case BATADV_IF_NOT_IN_USE:
c6c8fea2
SE
740 default:
741 length = sprintf(buff, "not in use\n");
742 break;
743 }
744
e5d89254 745 batadv_hardif_free_ref(hard_iface);
c6c8fea2
SE
746
747 return length;
748}
749
347c80f0
SE
750static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
751 batadv_store_mesh_iface);
752static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
c6c8fea2 753
b4d66b87 754static struct batadv_attribute *batadv_batman_attrs[] = {
0ff9b86f
SE
755 &batadv_attr_mesh_iface,
756 &batadv_attr_iface_status,
c6c8fea2
SE
757 NULL,
758};
759
5853e22c 760int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
c6c8fea2
SE
761{
762 struct kobject *hardif_kobject = &dev->dev.kobj;
b4d66b87 763 struct batadv_attribute **bat_attr;
c6c8fea2
SE
764 int err;
765
036cbfeb
SE
766 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
767 hardif_kobject);
c6c8fea2
SE
768
769 if (!*hardif_obj) {
3e34819e 770 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
036cbfeb 771 BATADV_SYSFS_IF_BAT_SUBDIR);
c6c8fea2
SE
772 goto out;
773 }
774
0ff9b86f 775 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
c6c8fea2
SE
776 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
777 if (err) {
3e34819e 778 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
036cbfeb 779 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
3e34819e 780 ((*bat_attr)->attr).name);
c6c8fea2
SE
781 goto rem_attr;
782 }
783 }
784
785 return 0;
786
787rem_attr:
0ff9b86f 788 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
c6c8fea2
SE
789 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
790out:
791 return -ENOMEM;
792}
793
5853e22c 794void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
c6c8fea2
SE
795{
796 kobject_put(*hardif_obj);
797 *hardif_obj = NULL;
798}
c6bda689 799
56303d34 800int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
39c75a51 801 enum batadv_uev_action action, const char *data)
c6bda689 802{
5346c35e 803 int ret = -ENOMEM;
c6bda689
AQ
804 struct kobject *bat_kobj;
805 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
806
736292c2 807 bat_kobj = &bat_priv->soft_iface->dev.kobj;
c6bda689 808
347c80f0 809 uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
0ff9b86f 810 strlen(batadv_uev_type_str[type]) + 1,
c6bda689
AQ
811 GFP_ATOMIC);
812 if (!uevent_env[0])
813 goto out;
814
347c80f0
SE
815 sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
816 batadv_uev_type_str[type]);
c6bda689 817
347c80f0 818 uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
0ff9b86f 819 strlen(batadv_uev_action_str[action]) + 1,
c6bda689
AQ
820 GFP_ATOMIC);
821 if (!uevent_env[1])
822 goto out;
823
347c80f0 824 sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
0ff9b86f 825 batadv_uev_action_str[action]);
c6bda689
AQ
826
827 /* If the event is DEL, ignore the data field */
39c75a51 828 if (action != BATADV_UEV_DEL) {
347c80f0 829 uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
c6bda689
AQ
830 strlen(data) + 1, GFP_ATOMIC);
831 if (!uevent_env[2])
832 goto out;
833
347c80f0 834 sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
c6bda689
AQ
835 }
836
837 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
838out:
839 kfree(uevent_env[0]);
840 kfree(uevent_env[1]);
841 kfree(uevent_env[2]);
842
c6bda689 843 if (ret)
39c75a51 844 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 845 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
0ff9b86f
SE
846 batadv_uev_type_str[type],
847 batadv_uev_action_str[action],
39c75a51 848 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
c6bda689
AQ
849 return ret;
850}