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