batman-adv: Fix genl notification for throughput_override
[linux-2.6-microblaze.git] / net / batman-adv / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2019  B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "sysfs.h"
20 #include "main.h"
21
22 #include <linux/atomic.h>
23 #include <linux/compiler.h>
24 #include <linux/device.h>
25 #include <linux/errno.h>
26 #include <linux/gfp.h>
27 #include <linux/if.h>
28 #include <linux/if_vlan.h>
29 #include <linux/kernel.h>
30 #include <linux/kobject.h>
31 #include <linux/kref.h>
32 #include <linux/netdevice.h>
33 #include <linux/printk.h>
34 #include <linux/rculist.h>
35 #include <linux/rcupdate.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/slab.h>
38 #include <linux/stddef.h>
39 #include <linux/string.h>
40 #include <linux/stringify.h>
41 #include <linux/workqueue.h>
42 #include <uapi/linux/batadv_packet.h>
43 #include <uapi/linux/batman_adv.h>
44
45 #include "bridge_loop_avoidance.h"
46 #include "distributed-arp-table.h"
47 #include "gateway_client.h"
48 #include "gateway_common.h"
49 #include "hard-interface.h"
50 #include "log.h"
51 #include "netlink.h"
52 #include "network-coding.h"
53 #include "soft-interface.h"
54
55 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
56 {
57         struct device *dev = container_of(obj->parent, struct device, kobj);
58
59         return to_net_dev(dev);
60 }
61
62 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
63 {
64         struct net_device *net_dev = batadv_kobj_to_netdev(obj);
65
66         return netdev_priv(net_dev);
67 }
68
69 /**
70  * batadv_vlan_kobj_to_batpriv() - convert a vlan kobj in the associated batpriv
71  * @obj: kobject to covert
72  *
73  * Return: the associated batadv_priv struct.
74  */
75 static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
76 {
77         /* VLAN specific attributes are located in the root sysfs folder if they
78          * refer to the untagged VLAN..
79          */
80         if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
81                 return batadv_kobj_to_batpriv(obj);
82
83         /* ..while the attributes for the tagged vlans are located in
84          * the in the corresponding "vlan%VID" subfolder
85          */
86         return batadv_kobj_to_batpriv(obj->parent);
87 }
88
89 /**
90  * batadv_kobj_to_vlan() - convert a kobj in the associated softif_vlan struct
91  * @bat_priv: the bat priv with all the soft interface information
92  * @obj: kobject to covert
93  *
94  * Return: the associated softif_vlan struct if found, NULL otherwise.
95  */
96 static struct batadv_softif_vlan *
97 batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
98 {
99         struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
100
101         rcu_read_lock();
102         hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
103                 if (vlan_tmp->kobj != obj)
104                         continue;
105
106                 if (!kref_get_unless_zero(&vlan_tmp->refcount))
107                         continue;
108
109                 vlan = vlan_tmp;
110                 break;
111         }
112         rcu_read_unlock();
113
114         return vlan;
115 }
116
117 #define BATADV_UEV_TYPE_VAR     "BATTYPE="
118 #define BATADV_UEV_ACTION_VAR   "BATACTION="
119 #define BATADV_UEV_DATA_VAR     "BATDATA="
120
121 static char *batadv_uev_action_str[] = {
122         "add",
123         "del",
124         "change",
125         "loopdetect",
126 };
127
128 static char *batadv_uev_type_str[] = {
129         "gw",
130         "bla",
131 };
132
133 /* Use this, if you have customized show and store functions for vlan attrs */
134 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store)   \
135 struct batadv_attribute batadv_attr_vlan_##_name = {    \
136         .attr = {.name = __stringify(_name),            \
137                  .mode = _mode },                       \
138         .show   = _show,                                \
139         .store  = _store,                               \
140 }
141
142 /* Use this, if you have customized show and store functions */
143 #define BATADV_ATTR(_name, _mode, _show, _store)        \
144 struct batadv_attribute batadv_attr_##_name = {         \
145         .attr = {.name = __stringify(_name),            \
146                  .mode = _mode },                       \
147         .show   = _show,                                \
148         .store  = _store,                               \
149 }
150
151 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func)                   \
152 ssize_t batadv_store_##_name(struct kobject *kobj,                      \
153                              struct attribute *attr, char *buff,        \
154                              size_t count)                              \
155 {                                                                       \
156         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);       \
157         struct batadv_priv *bat_priv = netdev_priv(net_dev);            \
158         ssize_t length;                                                 \
159                                                                         \
160         length = __batadv_store_bool_attr(buff, count, _post_func, attr,\
161                                           &bat_priv->_name, net_dev);   \
162                                                                         \
163         batadv_netlink_notify_mesh(bat_priv);                           \
164                                                                         \
165         return length;                                                  \
166 }
167
168 #define BATADV_ATTR_SIF_SHOW_BOOL(_name)                                \
169 ssize_t batadv_show_##_name(struct kobject *kobj,                       \
170                             struct attribute *attr, char *buff)         \
171 {                                                                       \
172         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);    \
173                                                                         \
174         return sprintf(buff, "%s\n",                                    \
175                        atomic_read(&bat_priv->_name) == 0 ?             \
176                        "disabled" : "enabled");                         \
177 }                                                                       \
178
179 /* Use this, if you are going to turn a [name] in the soft-interface
180  * (bat_priv) on or off
181  */
182 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func)                  \
183         static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func)            \
184         static BATADV_ATTR_SIF_SHOW_BOOL(_name)                         \
185         static BATADV_ATTR(_name, _mode, batadv_show_##_name,           \
186                            batadv_store_##_name)
187
188 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
189 ssize_t batadv_store_##_name(struct kobject *kobj,                      \
190                              struct attribute *attr, char *buff,        \
191                              size_t count)                              \
192 {                                                                       \
193         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);       \
194         struct batadv_priv *bat_priv = netdev_priv(net_dev);            \
195         ssize_t length;                                                 \
196                                                                         \
197         length = __batadv_store_uint_attr(buff, count, _min, _max,      \
198                                           _post_func, attr,             \
199                                           &bat_priv->_var, net_dev,     \
200                                           NULL);                        \
201                                                                         \
202         batadv_netlink_notify_mesh(bat_priv);                           \
203                                                                         \
204         return length;                                                  \
205 }
206
207 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var)                          \
208 ssize_t batadv_show_##_name(struct kobject *kobj,                       \
209                             struct attribute *attr, char *buff)         \
210 {                                                                       \
211         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);    \
212                                                                         \
213         return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var));     \
214 }                                                                       \
215
216 /* Use this, if you are going to set [name] in the soft-interface
217  * (bat_priv) to an unsigned integer value
218  */
219 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
220         static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
221         static BATADV_ATTR_SIF_SHOW_UINT(_name, _var)                   \
222         static BATADV_ATTR(_name, _mode, batadv_show_##_name,           \
223                            batadv_store_##_name)
224
225 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func)                  \
226 ssize_t batadv_store_vlan_##_name(struct kobject *kobj,                 \
227                                   struct attribute *attr, char *buff,   \
228                                   size_t count)                         \
229 {                                                                       \
230         struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
231         struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
232                                                               kobj);    \
233         size_t res = __batadv_store_bool_attr(buff, count, _post_func,  \
234                                               attr, &vlan->_name,       \
235                                               bat_priv->soft_iface);    \
236                                                                         \
237         if (vlan->vid)                                                  \
238                 batadv_netlink_notify_vlan(bat_priv, vlan);             \
239         else                                                            \
240                 batadv_netlink_notify_mesh(bat_priv);                   \
241                                                                         \
242         batadv_softif_vlan_put(vlan);                                   \
243         return res;                                                     \
244 }
245
246 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name)                               \
247 ssize_t batadv_show_vlan_##_name(struct kobject *kobj,                  \
248                                  struct attribute *attr, char *buff)    \
249 {                                                                       \
250         struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
251         struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
252                                                               kobj);    \
253         size_t res = sprintf(buff, "%s\n",                              \
254                              atomic_read(&vlan->_name) == 0 ?           \
255                              "disabled" : "enabled");                   \
256                                                                         \
257         batadv_softif_vlan_put(vlan);                                   \
258         return res;                                                     \
259 }
260
261 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
262 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func)                 \
263         static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func)           \
264         static BATADV_ATTR_VLAN_SHOW_BOOL(_name)                        \
265         static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
266                                 batadv_store_vlan_##_name)
267
268 #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
269 ssize_t batadv_store_##_name(struct kobject *kobj,                      \
270                              struct attribute *attr, char *buff,        \
271                              size_t count)                              \
272 {                                                                       \
273         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);       \
274         struct batadv_hard_iface *hard_iface;                           \
275         struct batadv_priv *bat_priv;                                   \
276         ssize_t length;                                                 \
277                                                                         \
278         hard_iface = batadv_hardif_get_by_netdev(net_dev);              \
279         if (!hard_iface)                                                \
280                 return 0;                                               \
281                                                                         \
282         length = __batadv_store_uint_attr(buff, count, _min, _max,      \
283                                           _post_func, attr,             \
284                                           &hard_iface->_var,            \
285                                           hard_iface->soft_iface,       \
286                                           net_dev);                     \
287                                                                         \
288         if (hard_iface->soft_iface) {                                   \
289                 bat_priv = netdev_priv(hard_iface->soft_iface);         \
290                 batadv_netlink_notify_hardif(bat_priv, hard_iface);     \
291         }                                                               \
292                                                                         \
293         batadv_hardif_put(hard_iface);                          \
294         return length;                                                  \
295 }
296
297 #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var)                          \
298 ssize_t batadv_show_##_name(struct kobject *kobj,                       \
299                             struct attribute *attr, char *buff)         \
300 {                                                                       \
301         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);       \
302         struct batadv_hard_iface *hard_iface;                           \
303         ssize_t length;                                                 \
304                                                                         \
305         hard_iface = batadv_hardif_get_by_netdev(net_dev);              \
306         if (!hard_iface)                                                \
307                 return 0;                                               \
308                                                                         \
309         length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
310                                                                         \
311         batadv_hardif_put(hard_iface);                          \
312         return length;                                                  \
313 }
314
315 /* Use this, if you are going to set [name] in hard_iface to an
316  * unsigned integer value
317  */
318 #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
319         static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min,            \
320                                           _max, _post_func)             \
321         static BATADV_ATTR_HIF_SHOW_UINT(_name, _var)                   \
322         static BATADV_ATTR(_name, _mode, batadv_show_##_name,           \
323                            batadv_store_##_name)
324
325 static int batadv_store_bool_attr(char *buff, size_t count,
326                                   struct net_device *net_dev,
327                                   const char *attr_name, atomic_t *attr,
328                                   bool *changed)
329 {
330         int enabled = -1;
331
332         *changed = false;
333
334         if (buff[count - 1] == '\n')
335                 buff[count - 1] = '\0';
336
337         if ((strncmp(buff, "1", 2) == 0) ||
338             (strncmp(buff, "enable", 7) == 0) ||
339             (strncmp(buff, "enabled", 8) == 0))
340                 enabled = 1;
341
342         if ((strncmp(buff, "0", 2) == 0) ||
343             (strncmp(buff, "disable", 8) == 0) ||
344             (strncmp(buff, "disabled", 9) == 0))
345                 enabled = 0;
346
347         if (enabled < 0) {
348                 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
349                             attr_name, buff);
350                 return -EINVAL;
351         }
352
353         if (atomic_read(attr) == enabled)
354                 return count;
355
356         batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
357                     atomic_read(attr) == 1 ? "enabled" : "disabled",
358                     enabled == 1 ? "enabled" : "disabled");
359
360         *changed = true;
361
362         atomic_set(attr, (unsigned int)enabled);
363         return count;
364 }
365
366 static inline ssize_t
367 __batadv_store_bool_attr(char *buff, size_t count,
368                          void (*post_func)(struct net_device *),
369                          struct attribute *attr,
370                          atomic_t *attr_store, struct net_device *net_dev)
371 {
372         bool changed;
373         int ret;
374
375         ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
376                                      attr_store, &changed);
377         if (post_func && changed)
378                 post_func(net_dev);
379
380         return ret;
381 }
382
383 static int batadv_store_uint_attr(const char *buff, size_t count,
384                                   struct net_device *net_dev,
385                                   struct net_device *slave_dev,
386                                   const char *attr_name,
387                                   unsigned int min, unsigned int max,
388                                   atomic_t *attr)
389 {
390         char ifname[IFNAMSIZ + 3] = "";
391         unsigned long uint_val;
392         int ret;
393
394         ret = kstrtoul(buff, 10, &uint_val);
395         if (ret) {
396                 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
397                             attr_name, buff);
398                 return -EINVAL;
399         }
400
401         if (uint_val < min) {
402                 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
403                             attr_name, uint_val, min);
404                 return -EINVAL;
405         }
406
407         if (uint_val > max) {
408                 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
409                             attr_name, uint_val, max);
410                 return -EINVAL;
411         }
412
413         if (atomic_read(attr) == uint_val)
414                 return count;
415
416         if (slave_dev)
417                 snprintf(ifname, sizeof(ifname), "%s: ", slave_dev->name);
418
419         batadv_info(net_dev, "%s: %sChanging from: %i to: %lu\n",
420                     attr_name, ifname, atomic_read(attr), uint_val);
421
422         atomic_set(attr, uint_val);
423         return count;
424 }
425
426 static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
427                                         int min, int max,
428                                         void (*post_func)(struct net_device *),
429                                         const struct attribute *attr,
430                                         atomic_t *attr_store,
431                                         struct net_device *net_dev,
432                                         struct net_device *slave_dev)
433 {
434         int ret;
435
436         ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
437                                      attr->name, min, max, attr_store);
438         if (post_func && ret)
439                 post_func(net_dev);
440
441         return ret;
442 }
443
444 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
445                                     struct attribute *attr, char *buff)
446 {
447         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
448
449         return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
450 }
451
452 static void batadv_post_gw_reselect(struct net_device *net_dev)
453 {
454         struct batadv_priv *bat_priv = netdev_priv(net_dev);
455
456         batadv_gw_reselect(bat_priv);
457 }
458
459 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
460                                    char *buff)
461 {
462         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
463         int bytes_written;
464
465         /* GW mode is not available if the routing algorithm in use does not
466          * implement the GW API
467          */
468         if (!bat_priv->algo_ops->gw.get_best_gw_node ||
469             !bat_priv->algo_ops->gw.is_eligible)
470                 return -ENOENT;
471
472         switch (atomic_read(&bat_priv->gw.mode)) {
473         case BATADV_GW_MODE_CLIENT:
474                 bytes_written = sprintf(buff, "%s\n",
475                                         BATADV_GW_MODE_CLIENT_NAME);
476                 break;
477         case BATADV_GW_MODE_SERVER:
478                 bytes_written = sprintf(buff, "%s\n",
479                                         BATADV_GW_MODE_SERVER_NAME);
480                 break;
481         default:
482                 bytes_written = sprintf(buff, "%s\n",
483                                         BATADV_GW_MODE_OFF_NAME);
484                 break;
485         }
486
487         return bytes_written;
488 }
489
490 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
491                                     struct attribute *attr, char *buff,
492                                     size_t count)
493 {
494         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
495         struct batadv_priv *bat_priv = netdev_priv(net_dev);
496         char *curr_gw_mode_str;
497         int gw_mode_tmp = -1;
498
499         /* toggling GW mode is allowed only if the routing algorithm in use
500          * provides the GW API
501          */
502         if (!bat_priv->algo_ops->gw.get_best_gw_node ||
503             !bat_priv->algo_ops->gw.is_eligible)
504                 return -EINVAL;
505
506         if (buff[count - 1] == '\n')
507                 buff[count - 1] = '\0';
508
509         if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
510                     strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
511                 gw_mode_tmp = BATADV_GW_MODE_OFF;
512
513         if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
514                     strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
515                 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
516
517         if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
518                     strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
519                 gw_mode_tmp = BATADV_GW_MODE_SERVER;
520
521         if (gw_mode_tmp < 0) {
522                 batadv_info(net_dev,
523                             "Invalid parameter for 'gw mode' setting received: %s\n",
524                             buff);
525                 return -EINVAL;
526         }
527
528         if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
529                 return count;
530
531         switch (atomic_read(&bat_priv->gw.mode)) {
532         case BATADV_GW_MODE_CLIENT:
533                 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
534                 break;
535         case BATADV_GW_MODE_SERVER:
536                 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
537                 break;
538         default:
539                 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
540                 break;
541         }
542
543         batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
544                     curr_gw_mode_str, buff);
545
546         /* Invoking batadv_gw_reselect() is not enough to really de-select the
547          * current GW. It will only instruct the gateway client code to perform
548          * a re-election the next time that this is needed.
549          *
550          * When gw client mode is being switched off the current GW must be
551          * de-selected explicitly otherwise no GW_ADD uevent is thrown on
552          * client mode re-activation. This is operation is performed in
553          * batadv_gw_check_client_stop().
554          */
555         batadv_gw_reselect(bat_priv);
556         /* always call batadv_gw_check_client_stop() before changing the gateway
557          * state
558          */
559         batadv_gw_check_client_stop(bat_priv);
560         atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
561         batadv_gw_tvlv_container_update(bat_priv);
562
563         batadv_netlink_notify_mesh(bat_priv);
564
565         return count;
566 }
567
568 static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
569                                         struct attribute *attr, char *buff)
570 {
571         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
572
573         /* GW selection class is not available if the routing algorithm in use
574          * does not implement the GW API
575          */
576         if (!bat_priv->algo_ops->gw.get_best_gw_node ||
577             !bat_priv->algo_ops->gw.is_eligible)
578                 return -ENOENT;
579
580         if (bat_priv->algo_ops->gw.show_sel_class)
581                 return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
582
583         return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
584 }
585
586 static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
587                                          struct attribute *attr, char *buff,
588                                          size_t count)
589 {
590         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
591         ssize_t length;
592
593         /* setting the GW selection class is allowed only if the routing
594          * algorithm in use implements the GW API
595          */
596         if (!bat_priv->algo_ops->gw.get_best_gw_node ||
597             !bat_priv->algo_ops->gw.is_eligible)
598                 return -EINVAL;
599
600         if (buff[count - 1] == '\n')
601                 buff[count - 1] = '\0';
602
603         if (bat_priv->algo_ops->gw.store_sel_class)
604                 return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
605                                                               count);
606
607         length = __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
608                                           batadv_post_gw_reselect, attr,
609                                           &bat_priv->gw.sel_class,
610                                           bat_priv->soft_iface, NULL);
611
612         batadv_netlink_notify_mesh(bat_priv);
613
614         return length;
615 }
616
617 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
618                                      struct attribute *attr, char *buff)
619 {
620         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
621         u32 down, up;
622
623         down = atomic_read(&bat_priv->gw.bandwidth_down);
624         up = atomic_read(&bat_priv->gw.bandwidth_up);
625
626         return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
627                        down % 10, up / 10, up % 10);
628 }
629
630 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
631                                       struct attribute *attr, char *buff,
632                                       size_t count)
633 {
634         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
635         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
636         ssize_t length;
637
638         if (buff[count - 1] == '\n')
639                 buff[count - 1] = '\0';
640
641         length = batadv_gw_bandwidth_set(net_dev, buff, count);
642
643         batadv_netlink_notify_mesh(bat_priv);
644
645         return length;
646 }
647
648 /**
649  * batadv_show_isolation_mark() - print the current isolation mark/mask
650  * @kobj: kobject representing the private mesh sysfs directory
651  * @attr: the batman-adv attribute the user is interacting with
652  * @buff: the buffer that will contain the data to send back to the user
653  *
654  * Return: the number of bytes written into 'buff' on success or a negative
655  * error code in case of failure
656  */
657 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
658                                           struct attribute *attr, char *buff)
659 {
660         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
661
662         return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
663                        bat_priv->isolation_mark_mask);
664 }
665
666 /**
667  * batadv_store_isolation_mark() - parse and store the isolation mark/mask
668  *  entered by the user
669  * @kobj: kobject representing the private mesh sysfs directory
670  * @attr: the batman-adv attribute the user is interacting with
671  * @buff: the buffer containing the user data
672  * @count: number of bytes in the buffer
673  *
674  * Return: 'count' on success or a negative error code in case of failure
675  */
676 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
677                                            struct attribute *attr, char *buff,
678                                            size_t count)
679 {
680         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
681         struct batadv_priv *bat_priv = netdev_priv(net_dev);
682         u32 mark, mask;
683         char *mask_ptr;
684
685         /* parse the mask if it has been specified, otherwise assume the mask is
686          * the biggest possible
687          */
688         mask = 0xFFFFFFFF;
689         mask_ptr = strchr(buff, '/');
690         if (mask_ptr) {
691                 *mask_ptr = '\0';
692                 mask_ptr++;
693
694                 /* the mask must be entered in hex base as it is going to be a
695                  * bitmask and not a prefix length
696                  */
697                 if (kstrtou32(mask_ptr, 16, &mask) < 0)
698                         return -EINVAL;
699         }
700
701         /* the mark can be entered in any base */
702         if (kstrtou32(buff, 0, &mark) < 0)
703                 return -EINVAL;
704
705         bat_priv->isolation_mark_mask = mask;
706         /* erase bits not covered by the mask */
707         bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
708
709         batadv_info(net_dev,
710                     "New skb mark for extended isolation: %#.8x/%#.8x\n",
711                     bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
712
713         batadv_netlink_notify_mesh(bat_priv);
714
715         return count;
716 }
717
718 BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
719 BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
720 #ifdef CONFIG_BATMAN_ADV_BLA
721 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
722 #endif
723 #ifdef CONFIG_BATMAN_ADV_DAT
724 BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
725 #endif
726 BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
727 static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
728 static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
729 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
730                      INT_MAX, NULL);
731 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
732                      NULL);
733 static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
734                    batadv_store_gw_sel_class);
735 static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
736                    batadv_store_gw_bwidth);
737 #ifdef CONFIG_BATMAN_ADV_MCAST
738 BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
739 #endif
740 #ifdef CONFIG_BATMAN_ADV_DEBUG
741 BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
742 #endif
743 #ifdef CONFIG_BATMAN_ADV_NC
744 BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
745 #endif
746 static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
747                    batadv_store_isolation_mark);
748
749 static struct batadv_attribute *batadv_mesh_attrs[] = {
750         &batadv_attr_aggregated_ogms,
751         &batadv_attr_bonding,
752 #ifdef CONFIG_BATMAN_ADV_BLA
753         &batadv_attr_bridge_loop_avoidance,
754 #endif
755 #ifdef CONFIG_BATMAN_ADV_DAT
756         &batadv_attr_distributed_arp_table,
757 #endif
758 #ifdef CONFIG_BATMAN_ADV_MCAST
759         &batadv_attr_multicast_mode,
760 #endif
761         &batadv_attr_fragmentation,
762         &batadv_attr_routing_algo,
763         &batadv_attr_gw_mode,
764         &batadv_attr_orig_interval,
765         &batadv_attr_hop_penalty,
766         &batadv_attr_gw_sel_class,
767         &batadv_attr_gw_bandwidth,
768 #ifdef CONFIG_BATMAN_ADV_DEBUG
769         &batadv_attr_log_level,
770 #endif
771 #ifdef CONFIG_BATMAN_ADV_NC
772         &batadv_attr_network_coding,
773 #endif
774         &batadv_attr_isolation_mark,
775         NULL,
776 };
777
778 BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
779
780 /* array of vlan specific sysfs attributes */
781 static struct batadv_attribute *batadv_vlan_attrs[] = {
782         &batadv_attr_vlan_ap_isolation,
783         NULL,
784 };
785
786 /**
787  * batadv_sysfs_add_meshif() - Add soft interface specific sysfs entries
788  * @dev: netdev struct of the soft interface
789  *
790  * Return: 0 on success or negative error number in case of failure
791  */
792 int batadv_sysfs_add_meshif(struct net_device *dev)
793 {
794         struct kobject *batif_kobject = &dev->dev.kobj;
795         struct batadv_priv *bat_priv = netdev_priv(dev);
796         struct batadv_attribute **bat_attr;
797         int err;
798
799         bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
800                                                     batif_kobject);
801         if (!bat_priv->mesh_obj) {
802                 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
803                            BATADV_SYSFS_IF_MESH_SUBDIR);
804                 goto out;
805         }
806
807         for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
808                 err = sysfs_create_file(bat_priv->mesh_obj,
809                                         &((*bat_attr)->attr));
810                 if (err) {
811                         batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
812                                    dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
813                                    ((*bat_attr)->attr).name);
814                         goto rem_attr;
815                 }
816         }
817
818         return 0;
819
820 rem_attr:
821         for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
822                 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
823
824         kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
825         kobject_del(bat_priv->mesh_obj);
826         kobject_put(bat_priv->mesh_obj);
827         bat_priv->mesh_obj = NULL;
828 out:
829         return -ENOMEM;
830 }
831
832 /**
833  * batadv_sysfs_del_meshif() - Remove soft interface specific sysfs entries
834  * @dev: netdev struct of the soft interface
835  */
836 void batadv_sysfs_del_meshif(struct net_device *dev)
837 {
838         struct batadv_priv *bat_priv = netdev_priv(dev);
839         struct batadv_attribute **bat_attr;
840
841         for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
842                 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
843
844         kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
845         kobject_del(bat_priv->mesh_obj);
846         kobject_put(bat_priv->mesh_obj);
847         bat_priv->mesh_obj = NULL;
848 }
849
850 /**
851  * batadv_sysfs_add_vlan() - add all the needed sysfs objects for the new vlan
852  * @dev: netdev of the mesh interface
853  * @vlan: private data of the newly added VLAN interface
854  *
855  * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
856  */
857 int batadv_sysfs_add_vlan(struct net_device *dev,
858                           struct batadv_softif_vlan *vlan)
859 {
860         char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
861         struct batadv_priv *bat_priv = netdev_priv(dev);
862         struct batadv_attribute **bat_attr;
863         int err;
864
865         if (vlan->vid & BATADV_VLAN_HAS_TAG) {
866                 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
867                         vlan->vid & VLAN_VID_MASK);
868
869                 vlan->kobj = kobject_create_and_add(vlan_subdir,
870                                                     bat_priv->mesh_obj);
871                 if (!vlan->kobj) {
872                         batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
873                                    dev->name, vlan_subdir);
874                         goto out;
875                 }
876         } else {
877                 /* the untagged LAN uses the root folder to store its "VLAN
878                  * specific attributes"
879                  */
880                 vlan->kobj = bat_priv->mesh_obj;
881                 kobject_get(bat_priv->mesh_obj);
882         }
883
884         for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
885                 err = sysfs_create_file(vlan->kobj,
886                                         &((*bat_attr)->attr));
887                 if (err) {
888                         batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
889                                    dev->name, vlan_subdir,
890                                    ((*bat_attr)->attr).name);
891                         goto rem_attr;
892                 }
893         }
894
895         return 0;
896
897 rem_attr:
898         for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
899                 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
900
901         if (vlan->kobj != bat_priv->mesh_obj) {
902                 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
903                 kobject_del(vlan->kobj);
904         }
905         kobject_put(vlan->kobj);
906         vlan->kobj = NULL;
907 out:
908         return -ENOMEM;
909 }
910
911 /**
912  * batadv_sysfs_del_vlan() - remove all the sysfs objects for a given VLAN
913  * @bat_priv: the bat priv with all the soft interface information
914  * @vlan: the private data of the VLAN to destroy
915  */
916 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
917                            struct batadv_softif_vlan *vlan)
918 {
919         struct batadv_attribute **bat_attr;
920
921         for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
922                 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
923
924         if (vlan->kobj != bat_priv->mesh_obj) {
925                 kobject_uevent(vlan->kobj, KOBJ_REMOVE);
926                 kobject_del(vlan->kobj);
927         }
928         kobject_put(vlan->kobj);
929         vlan->kobj = NULL;
930 }
931
932 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
933                                       struct attribute *attr, char *buff)
934 {
935         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
936         struct batadv_hard_iface *hard_iface;
937         ssize_t length;
938         const char *ifname;
939
940         hard_iface = batadv_hardif_get_by_netdev(net_dev);
941         if (!hard_iface)
942                 return 0;
943
944         if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
945                 ifname =  "none";
946         else
947                 ifname = hard_iface->soft_iface->name;
948
949         length = sprintf(buff, "%s\n", ifname);
950
951         batadv_hardif_put(hard_iface);
952
953         return length;
954 }
955
956 /**
957  * batadv_store_mesh_iface_finish() - store new hardif mesh_iface state
958  * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
959  * @ifname: name of soft-interface to modify
960  *
961  * Changes the parts of the hard+soft interface which can not be modified under
962  * sysfs lock (to prevent deadlock situations).
963  *
964  * Return: 0 on success, 0 < on failure
965  */
966 static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
967                                           char ifname[IFNAMSIZ])
968 {
969         struct net *net = dev_net(net_dev);
970         struct batadv_hard_iface *hard_iface;
971         int status_tmp;
972         int ret = 0;
973
974         ASSERT_RTNL();
975
976         hard_iface = batadv_hardif_get_by_netdev(net_dev);
977         if (!hard_iface)
978                 return 0;
979
980         if (strncmp(ifname, "none", 4) == 0)
981                 status_tmp = BATADV_IF_NOT_IN_USE;
982         else
983                 status_tmp = BATADV_IF_I_WANT_YOU;
984
985         if (hard_iface->if_status == status_tmp)
986                 goto out;
987
988         if (hard_iface->soft_iface &&
989             strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0)
990                 goto out;
991
992         if (status_tmp == BATADV_IF_NOT_IN_USE) {
993                 batadv_hardif_disable_interface(hard_iface,
994                                                 BATADV_IF_CLEANUP_AUTO);
995                 goto out;
996         }
997
998         /* if the interface already is in use */
999         if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
1000                 batadv_hardif_disable_interface(hard_iface,
1001                                                 BATADV_IF_CLEANUP_AUTO);
1002
1003         ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
1004 out:
1005         batadv_hardif_put(hard_iface);
1006         return ret;
1007 }
1008
1009 /**
1010  * batadv_store_mesh_iface_work() - store new hardif mesh_iface state
1011  * @work: work queue item
1012  *
1013  * Changes the parts of the hard+soft interface which can not be modified under
1014  * sysfs lock (to prevent deadlock situations).
1015  */
1016 static void batadv_store_mesh_iface_work(struct work_struct *work)
1017 {
1018         struct batadv_store_mesh_work *store_work;
1019         int ret;
1020
1021         store_work = container_of(work, struct batadv_store_mesh_work, work);
1022
1023         rtnl_lock();
1024         ret = batadv_store_mesh_iface_finish(store_work->net_dev,
1025                                              store_work->soft_iface_name);
1026         rtnl_unlock();
1027
1028         if (ret < 0)
1029                 pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
1030                        store_work->soft_iface_name, store_work->net_dev->name,
1031                        ret);
1032
1033         dev_put(store_work->net_dev);
1034         kfree(store_work);
1035 }
1036
1037 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
1038                                        struct attribute *attr, char *buff,
1039                                        size_t count)
1040 {
1041         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1042         struct batadv_store_mesh_work *store_work;
1043
1044         if (buff[count - 1] == '\n')
1045                 buff[count - 1] = '\0';
1046
1047         if (strlen(buff) >= IFNAMSIZ) {
1048                 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
1049                        buff);
1050                 return -EINVAL;
1051         }
1052
1053         store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
1054         if (!store_work)
1055                 return -ENOMEM;
1056
1057         dev_hold(net_dev);
1058         INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1059         store_work->net_dev = net_dev;
1060         strlcpy(store_work->soft_iface_name, buff,
1061                 sizeof(store_work->soft_iface_name));
1062
1063         queue_work(batadv_event_workqueue, &store_work->work);
1064
1065         return count;
1066 }
1067
1068 static ssize_t batadv_show_iface_status(struct kobject *kobj,
1069                                         struct attribute *attr, char *buff)
1070 {
1071         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1072         struct batadv_hard_iface *hard_iface;
1073         ssize_t length;
1074
1075         hard_iface = batadv_hardif_get_by_netdev(net_dev);
1076         if (!hard_iface)
1077                 return 0;
1078
1079         switch (hard_iface->if_status) {
1080         case BATADV_IF_TO_BE_REMOVED:
1081                 length = sprintf(buff, "disabling\n");
1082                 break;
1083         case BATADV_IF_INACTIVE:
1084                 length = sprintf(buff, "inactive\n");
1085                 break;
1086         case BATADV_IF_ACTIVE:
1087                 length = sprintf(buff, "active\n");
1088                 break;
1089         case BATADV_IF_TO_BE_ACTIVATED:
1090                 length = sprintf(buff, "enabling\n");
1091                 break;
1092         case BATADV_IF_NOT_IN_USE:
1093         default:
1094                 length = sprintf(buff, "not in use\n");
1095                 break;
1096         }
1097
1098         batadv_hardif_put(hard_iface);
1099
1100         return length;
1101 }
1102
1103 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1104
1105 /**
1106  * batadv_store_throughput_override() - parse and store throughput override
1107  *  entered by the user
1108  * @kobj: kobject representing the private mesh sysfs directory
1109  * @attr: the batman-adv attribute the user is interacting with
1110  * @buff: the buffer containing the user data
1111  * @count: number of bytes in the buffer
1112  *
1113  * Return: 'count' on success or a negative error code in case of failure
1114  */
1115 static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1116                                                 struct attribute *attr,
1117                                                 char *buff, size_t count)
1118 {
1119         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1120         struct batadv_hard_iface *hard_iface;
1121         struct batadv_priv *bat_priv;
1122         u32 tp_override;
1123         u32 old_tp_override;
1124         bool ret;
1125
1126         hard_iface = batadv_hardif_get_by_netdev(net_dev);
1127         if (!hard_iface)
1128                 return -EINVAL;
1129
1130         if (buff[count - 1] == '\n')
1131                 buff[count - 1] = '\0';
1132
1133         ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1134                                       &tp_override);
1135         if (!ret)
1136                 return count;
1137
1138         old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1139         if (old_tp_override == tp_override)
1140                 goto out;
1141
1142         batadv_info(hard_iface->soft_iface,
1143                     "%s: %s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1144                     "throughput_override", net_dev->name,
1145                     old_tp_override / 10, old_tp_override % 10,
1146                     tp_override / 10, tp_override % 10);
1147
1148         atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1149
1150         if (hard_iface->soft_iface) {
1151                 bat_priv = netdev_priv(hard_iface->soft_iface);
1152                 batadv_netlink_notify_hardif(bat_priv, hard_iface);
1153         }
1154
1155 out:
1156         batadv_hardif_put(hard_iface);
1157         return count;
1158 }
1159
1160 static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1161                                                struct attribute *attr,
1162                                                char *buff)
1163 {
1164         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1165         struct batadv_hard_iface *hard_iface;
1166         u32 tp_override;
1167
1168         hard_iface = batadv_hardif_get_by_netdev(net_dev);
1169         if (!hard_iface)
1170                 return -EINVAL;
1171
1172         tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1173
1174         return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1175                        tp_override % 10);
1176 }
1177
1178 #endif
1179
1180 static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
1181                    batadv_store_mesh_iface);
1182 static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
1183 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1184 BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
1185                      2 * BATADV_JITTER, INT_MAX, NULL);
1186 static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
1187                    batadv_store_throughput_override);
1188 #endif
1189
1190 static struct batadv_attribute *batadv_batman_attrs[] = {
1191         &batadv_attr_mesh_iface,
1192         &batadv_attr_iface_status,
1193 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1194         &batadv_attr_elp_interval,
1195         &batadv_attr_throughput_override,
1196 #endif
1197         NULL,
1198 };
1199
1200 /**
1201  * batadv_sysfs_add_hardif() - Add hard interface specific sysfs entries
1202  * @hardif_obj: address where to store the pointer to new sysfs folder
1203  * @dev: netdev struct of the hard interface
1204  *
1205  * Return: 0 on success or negative error number in case of failure
1206  */
1207 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
1208 {
1209         struct kobject *hardif_kobject = &dev->dev.kobj;
1210         struct batadv_attribute **bat_attr;
1211         int err;
1212
1213         *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1214                                              hardif_kobject);
1215
1216         if (!*hardif_obj) {
1217                 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
1218                            BATADV_SYSFS_IF_BAT_SUBDIR);
1219                 goto out;
1220         }
1221
1222         for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
1223                 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1224                 if (err) {
1225                         batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
1226                                    dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
1227                                    ((*bat_attr)->attr).name);
1228                         goto rem_attr;
1229                 }
1230         }
1231
1232         return 0;
1233
1234 rem_attr:
1235         for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
1236                 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1237 out:
1238         return -ENOMEM;
1239 }
1240
1241 /**
1242  * batadv_sysfs_del_hardif() - Remove hard interface specific sysfs entries
1243  * @hardif_obj: address to the pointer to which stores batman-adv sysfs folder
1244  *  of the hard interface
1245  */
1246 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
1247 {
1248         kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1249         kobject_del(*hardif_obj);
1250         kobject_put(*hardif_obj);
1251         *hardif_obj = NULL;
1252 }
1253
1254 /**
1255  * batadv_throw_uevent() - Send an uevent with batman-adv specific env data
1256  * @bat_priv: the bat priv with all the soft interface information
1257  * @type: subsystem type of event. Stored in uevent's BATTYPE
1258  * @action: action type of event. Stored in uevent's BATACTION
1259  * @data: string with additional information to the event (ignored for
1260  *  BATADV_UEV_DEL). Stored in uevent's BATDATA
1261  *
1262  * Return: 0 on success or negative error number in case of failure
1263  */
1264 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
1265                         enum batadv_uev_action action, const char *data)
1266 {
1267         int ret = -ENOMEM;
1268         struct kobject *bat_kobj;
1269         char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1270
1271         bat_kobj = &bat_priv->soft_iface->dev.kobj;
1272
1273         uevent_env[0] = kasprintf(GFP_ATOMIC,
1274                                   "%s%s", BATADV_UEV_TYPE_VAR,
1275                                   batadv_uev_type_str[type]);
1276         if (!uevent_env[0])
1277                 goto out;
1278
1279         uevent_env[1] = kasprintf(GFP_ATOMIC,
1280                                   "%s%s", BATADV_UEV_ACTION_VAR,
1281                                   batadv_uev_action_str[action]);
1282         if (!uevent_env[1])
1283                 goto out;
1284
1285         /* If the event is DEL, ignore the data field */
1286         if (action != BATADV_UEV_DEL) {
1287                 uevent_env[2] = kasprintf(GFP_ATOMIC,
1288                                           "%s%s", BATADV_UEV_DATA_VAR, data);
1289                 if (!uevent_env[2])
1290                         goto out;
1291         }
1292
1293         ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1294 out:
1295         kfree(uevent_env[0]);
1296         kfree(uevent_env[1]);
1297         kfree(uevent_env[2]);
1298
1299         if (ret)
1300                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1301                            "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
1302                            batadv_uev_type_str[type],
1303                            batadv_uev_action_str[action],
1304                            (action == BATADV_UEV_DEL ? "NULL" : data), ret);
1305         return ret;
1306 }