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