batman-adv: Prefix gateway defines with BATADV_
[cascardo/linux.git] / net / batman-adv / bat_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 "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
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 bat_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 bat_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 bat_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 bat_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 bat_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 bat_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 #define BATADV_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func)       \
126 ssize_t batadv_store_##_name(struct kobject *kobj,                      \
127                              struct attribute *attr, char *buff,        \
128                              size_t count)                              \
129 {                                                                       \
130         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);       \
131         struct hard_iface *hard_iface;                                  \
132         ssize_t length;                                                 \
133                                                                         \
134         hard_iface = batadv_hardif_get_by_netdev(net_dev);              \
135         if (!hard_iface)                                                \
136                 return 0;                                               \
137                                                                         \
138         length = __batadv_store_uint_attr(buff, count, _min, _max,      \
139                                           _post_func, attr,             \
140                                           &hard_iface->_name, net_dev); \
141                                                                         \
142         batadv_hardif_free_ref(hard_iface);                             \
143         return length;                                                  \
144 }
145
146 #define BATADV_ATTR_HIF_SHOW_UINT(_name)                                \
147 ssize_t batadv_show_##_name(struct kobject *kobj,                       \
148                             struct attribute *attr, char *buff)         \
149 {                                                                       \
150         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);       \
151         struct hard_iface *hard_iface;                                  \
152         ssize_t length;                                                 \
153                                                                         \
154         hard_iface = batadv_hardif_get_by_netdev(net_dev);              \
155         if (!hard_iface)                                                \
156                 return 0;                                               \
157                                                                         \
158         length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_name));\
159                                                                         \
160         batadv_hardif_free_ref(hard_iface);                             \
161         return length;                                                  \
162 }
163
164 /* Use this, if you are going to set [name] in hard_iface to an
165  * unsigned integer value
166  */
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)
172
173
174 static int batadv_store_bool_attr(char *buff, size_t count,
175                                   struct net_device *net_dev,
176                                   const char *attr_name, atomic_t *attr)
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) {
194                 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
195                             attr_name, buff);
196                 return -EINVAL;
197         }
198
199         if (atomic_read(attr) == enabled)
200                 return count;
201
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");
205
206         atomic_set(attr, (unsigned int)enabled);
207         return count;
208 }
209
210 static 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)
215 {
216         int ret;
217
218         ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
219                                      attr_store);
220         if (post_func && ret)
221                 post_func(net_dev);
222
223         return ret;
224 }
225
226 static 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)
231 {
232         unsigned long uint_val;
233         int ret;
234
235         ret = kstrtoul(buff, 10, &uint_val);
236         if (ret) {
237                 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
238                             attr_name, buff);
239                 return -EINVAL;
240         }
241
242         if (uint_val < min) {
243                 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
244                             attr_name, uint_val, min);
245                 return -EINVAL;
246         }
247
248         if (uint_val > max) {
249                 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
250                             attr_name, uint_val, max);
251                 return -EINVAL;
252         }
253
254         if (atomic_read(attr) == uint_val)
255                 return count;
256
257         batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
258                     attr_name, atomic_read(attr), uint_val);
259
260         atomic_set(attr, uint_val);
261         return count;
262 }
263
264 static 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)
270 {
271         int ret;
272
273         ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
274                                      attr_store);
275         if (post_func && ret)
276                 post_func(net_dev);
277
278         return ret;
279 }
280
281 static ssize_t batadv_show_vis_mode(struct kobject *kobj,
282                                     struct attribute *attr, char *buff)
283 {
284         struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
285         int vis_mode = atomic_read(&bat_priv->vis_mode);
286
287         return sprintf(buff, "%s\n",
288                        vis_mode == VIS_TYPE_CLIENT_UPDATE ?
289                                                         "client" : "server");
290 }
291
292 static ssize_t batadv_store_vis_mode(struct kobject *kobj,
293                                      struct attribute *attr, char *buff,
294                                      size_t count)
295 {
296         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
297         struct bat_priv *bat_priv = netdev_priv(net_dev);
298         unsigned long val;
299         int ret, vis_mode_tmp = -1;
300         const char *old_mode, *new_mode;
301
302         ret = kstrtoul(buff, 10, &val);
303
304         if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
305             (strncmp(buff, "client", 6) == 0) ||
306             (strncmp(buff, "off", 3) == 0))
307                 vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
308
309         if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
310             (strncmp(buff, "server", 6) == 0))
311                 vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
312
313         if (vis_mode_tmp < 0) {
314                 if (buff[count - 1] == '\n')
315                         buff[count - 1] = '\0';
316
317                 batadv_info(net_dev,
318                             "Invalid parameter for 'vis mode' setting received: %s\n",
319                             buff);
320                 return -EINVAL;
321         }
322
323         if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
324                 return count;
325
326         if (atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE)
327                 old_mode =  "client";
328         else
329                 old_mode = "server";
330
331         if (vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE)
332                 new_mode =  "client";
333         else
334                 new_mode = "server";
335
336         batadv_info(net_dev, "Changing vis mode from: %s to: %s\n", old_mode,
337                     new_mode);
338
339         atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
340         return count;
341 }
342
343 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
344                                     struct attribute *attr, char *buff)
345 {
346         struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
347         return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
348 }
349
350 static void batadv_post_gw_deselect(struct net_device *net_dev)
351 {
352         struct bat_priv *bat_priv = netdev_priv(net_dev);
353         batadv_gw_deselect(bat_priv);
354 }
355
356 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
357                                    char *buff)
358 {
359         struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
360         int bytes_written;
361
362         switch (atomic_read(&bat_priv->gw_mode)) {
363         case GW_MODE_CLIENT:
364                 bytes_written = sprintf(buff, "%s\n",
365                                         BATADV_GW_MODE_CLIENT_NAME);
366                 break;
367         case GW_MODE_SERVER:
368                 bytes_written = sprintf(buff, "%s\n",
369                                         BATADV_GW_MODE_SERVER_NAME);
370                 break;
371         default:
372                 bytes_written = sprintf(buff, "%s\n",
373                                         BATADV_GW_MODE_OFF_NAME);
374                 break;
375         }
376
377         return bytes_written;
378 }
379
380 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
381                                     struct attribute *attr, char *buff,
382                                     size_t count)
383 {
384         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
385         struct bat_priv *bat_priv = netdev_priv(net_dev);
386         char *curr_gw_mode_str;
387         int gw_mode_tmp = -1;
388
389         if (buff[count - 1] == '\n')
390                 buff[count - 1] = '\0';
391
392         if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
393                     strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
394                 gw_mode_tmp = GW_MODE_OFF;
395
396         if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
397                     strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
398                 gw_mode_tmp = GW_MODE_CLIENT;
399
400         if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
401                     strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
402                 gw_mode_tmp = GW_MODE_SERVER;
403
404         if (gw_mode_tmp < 0) {
405                 batadv_info(net_dev,
406                             "Invalid parameter for 'gw mode' setting received: %s\n",
407                             buff);
408                 return -EINVAL;
409         }
410
411         if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
412                 return count;
413
414         switch (atomic_read(&bat_priv->gw_mode)) {
415         case GW_MODE_CLIENT:
416                 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
417                 break;
418         case GW_MODE_SERVER:
419                 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
420                 break;
421         default:
422                 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
423                 break;
424         }
425
426         batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
427                     curr_gw_mode_str, buff);
428
429         batadv_gw_deselect(bat_priv);
430         atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
431         return count;
432 }
433
434 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
435                                      struct attribute *attr, char *buff)
436 {
437         struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
438         int down, up;
439         int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
440
441         batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
442         return sprintf(buff, "%i%s/%i%s\n",
443                        (down > 2048 ? down / 1024 : down),
444                        (down > 2048 ? "MBit" : "KBit"),
445                        (up > 2048 ? up / 1024 : up),
446                        (up > 2048 ? "MBit" : "KBit"));
447 }
448
449 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
450                                       struct attribute *attr, char *buff,
451                                       size_t count)
452 {
453         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
454
455         if (buff[count - 1] == '\n')
456                 buff[count - 1] = '\0';
457
458         return batadv_gw_bandwidth_set(net_dev, buff, count);
459 }
460
461 BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
462 BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
463 #ifdef CONFIG_BATMAN_ADV_BLA
464 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
465 #endif
466 BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
467 BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
468 static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
469                    batadv_store_vis_mode);
470 static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
471 static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
472                    batadv_store_gw_mode);
473 BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX,
474                      NULL);
475 BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
476 BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
477                      batadv_post_gw_deselect);
478 static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
479                    batadv_store_gw_bwidth);
480 #ifdef CONFIG_BATMAN_ADV_DEBUG
481 BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL);
482 #endif
483
484 static struct bat_attribute *batadv_mesh_attrs[] = {
485         &batadv_attr_aggregated_ogms,
486         &batadv_attr_bonding,
487 #ifdef CONFIG_BATMAN_ADV_BLA
488         &batadv_attr_bridge_loop_avoidance,
489 #endif
490         &batadv_attr_fragmentation,
491         &batadv_attr_ap_isolation,
492         &batadv_attr_vis_mode,
493         &batadv_attr_routing_algo,
494         &batadv_attr_gw_mode,
495         &batadv_attr_orig_interval,
496         &batadv_attr_hop_penalty,
497         &batadv_attr_gw_sel_class,
498         &batadv_attr_gw_bandwidth,
499 #ifdef CONFIG_BATMAN_ADV_DEBUG
500         &batadv_attr_log_level,
501 #endif
502         NULL,
503 };
504
505 int batadv_sysfs_add_meshif(struct net_device *dev)
506 {
507         struct kobject *batif_kobject = &dev->dev.kobj;
508         struct bat_priv *bat_priv = netdev_priv(dev);
509         struct bat_attribute **bat_attr;
510         int err;
511
512         bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
513                                                     batif_kobject);
514         if (!bat_priv->mesh_obj) {
515                 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
516                            BATADV_SYSFS_IF_MESH_SUBDIR);
517                 goto out;
518         }
519
520         for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
521                 err = sysfs_create_file(bat_priv->mesh_obj,
522                                         &((*bat_attr)->attr));
523                 if (err) {
524                         batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
525                                    dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
526                                    ((*bat_attr)->attr).name);
527                         goto rem_attr;
528                 }
529         }
530
531         return 0;
532
533 rem_attr:
534         for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
535                 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
536
537         kobject_put(bat_priv->mesh_obj);
538         bat_priv->mesh_obj = NULL;
539 out:
540         return -ENOMEM;
541 }
542
543 void batadv_sysfs_del_meshif(struct net_device *dev)
544 {
545         struct bat_priv *bat_priv = netdev_priv(dev);
546         struct bat_attribute **bat_attr;
547
548         for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
549                 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
550
551         kobject_put(bat_priv->mesh_obj);
552         bat_priv->mesh_obj = NULL;
553 }
554
555 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
556                                       struct attribute *attr, char *buff)
557 {
558         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
559         struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
560         ssize_t length;
561
562         if (!hard_iface)
563                 return 0;
564
565         length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
566                          "none" : hard_iface->soft_iface->name);
567
568         batadv_hardif_free_ref(hard_iface);
569
570         return length;
571 }
572
573 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
574                                        struct attribute *attr, char *buff,
575                                        size_t count)
576 {
577         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
578         struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
579         int status_tmp = -1;
580         int ret = count;
581
582         if (!hard_iface)
583                 return count;
584
585         if (buff[count - 1] == '\n')
586                 buff[count - 1] = '\0';
587
588         if (strlen(buff) >= IFNAMSIZ) {
589                 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
590                        buff);
591                 batadv_hardif_free_ref(hard_iface);
592                 return -EINVAL;
593         }
594
595         if (strncmp(buff, "none", 4) == 0)
596                 status_tmp = IF_NOT_IN_USE;
597         else
598                 status_tmp = IF_I_WANT_YOU;
599
600         if (hard_iface->if_status == status_tmp)
601                 goto out;
602
603         if ((hard_iface->soft_iface) &&
604             (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
605                 goto out;
606
607         if (!rtnl_trylock()) {
608                 ret = -ERESTARTSYS;
609                 goto out;
610         }
611
612         if (status_tmp == IF_NOT_IN_USE) {
613                 batadv_hardif_disable_interface(hard_iface);
614                 goto unlock;
615         }
616
617         /* if the interface already is in use */
618         if (hard_iface->if_status != IF_NOT_IN_USE)
619                 batadv_hardif_disable_interface(hard_iface);
620
621         ret = batadv_hardif_enable_interface(hard_iface, buff);
622
623 unlock:
624         rtnl_unlock();
625 out:
626         batadv_hardif_free_ref(hard_iface);
627         return ret;
628 }
629
630 static ssize_t batadv_show_iface_status(struct kobject *kobj,
631                                         struct attribute *attr, char *buff)
632 {
633         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
634         struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
635         ssize_t length;
636
637         if (!hard_iface)
638                 return 0;
639
640         switch (hard_iface->if_status) {
641         case IF_TO_BE_REMOVED:
642                 length = sprintf(buff, "disabling\n");
643                 break;
644         case IF_INACTIVE:
645                 length = sprintf(buff, "inactive\n");
646                 break;
647         case IF_ACTIVE:
648                 length = sprintf(buff, "active\n");
649                 break;
650         case IF_TO_BE_ACTIVATED:
651                 length = sprintf(buff, "enabling\n");
652                 break;
653         case IF_NOT_IN_USE:
654         default:
655                 length = sprintf(buff, "not in use\n");
656                 break;
657         }
658
659         batadv_hardif_free_ref(hard_iface);
660
661         return length;
662 }
663
664 static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
665                    batadv_store_mesh_iface);
666 static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
667
668 static struct bat_attribute *batadv_batman_attrs[] = {
669         &batadv_attr_mesh_iface,
670         &batadv_attr_iface_status,
671         NULL,
672 };
673
674 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
675 {
676         struct kobject *hardif_kobject = &dev->dev.kobj;
677         struct bat_attribute **bat_attr;
678         int err;
679
680         *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
681                                              hardif_kobject);
682
683         if (!*hardif_obj) {
684                 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
685                            BATADV_SYSFS_IF_BAT_SUBDIR);
686                 goto out;
687         }
688
689         for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
690                 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
691                 if (err) {
692                         batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
693                                    dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
694                                    ((*bat_attr)->attr).name);
695                         goto rem_attr;
696                 }
697         }
698
699         return 0;
700
701 rem_attr:
702         for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
703                 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
704 out:
705         return -ENOMEM;
706 }
707
708 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
709 {
710         kobject_put(*hardif_obj);
711         *hardif_obj = NULL;
712 }
713
714 int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
715                         enum uev_action action, const char *data)
716 {
717         int ret = -ENOMEM;
718         struct hard_iface *primary_if = NULL;
719         struct kobject *bat_kobj;
720         char *uevent_env[4] = { NULL, NULL, NULL, NULL };
721
722         primary_if = batadv_primary_if_get_selected(bat_priv);
723         if (!primary_if)
724                 goto out;
725
726         bat_kobj = &primary_if->soft_iface->dev.kobj;
727
728         uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
729                                 strlen(batadv_uev_type_str[type]) + 1,
730                                 GFP_ATOMIC);
731         if (!uevent_env[0])
732                 goto out;
733
734         sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
735                 batadv_uev_type_str[type]);
736
737         uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
738                                 strlen(batadv_uev_action_str[action]) + 1,
739                                 GFP_ATOMIC);
740         if (!uevent_env[1])
741                 goto out;
742
743         sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
744                 batadv_uev_action_str[action]);
745
746         /* If the event is DEL, ignore the data field */
747         if (action != UEV_DEL) {
748                 uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
749                                         strlen(data) + 1, GFP_ATOMIC);
750                 if (!uevent_env[2])
751                         goto out;
752
753                 sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
754         }
755
756         ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
757 out:
758         kfree(uevent_env[0]);
759         kfree(uevent_env[1]);
760         kfree(uevent_env[2]);
761
762         if (primary_if)
763                 batadv_hardif_free_ref(primary_if);
764
765         if (ret)
766                 batadv_dbg(DBG_BATMAN, bat_priv,
767                            "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
768                            batadv_uev_type_str[type],
769                            batadv_uev_action_str[action],
770                            (action == UEV_DEL ? "NULL" : data), ret);
771         return ret;
772 }