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