]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/batman-adv/sysfs.c
Merge remote-tracking branch 'drm/drm-next'
[karo-tx-linux.git] / net / batman-adv / sysfs.c
1 /* Copyright (C) 2010-2016  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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "sysfs.h"
19 #include "main.h"
20
21 #include <linux/atomic.h>
22 #include <linux/compiler.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/fs.h>
26 #include <linux/if.h>
27 #include <linux/if_vlan.h>
28 #include <linux/kernel.h>
29 #include <linux/netdevice.h>
30 #include <linux/printk.h>
31 #include <linux/rculist.h>
32 #include <linux/rcupdate.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/slab.h>
35 #include <linux/stat.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/stringify.h>
39
40 #include "distributed-arp-table.h"
41 #include "gateway_client.h"
42 #include "gateway_common.h"
43 #include "bridge_loop_avoidance.h"
44 #include "hard-interface.h"
45 #include "network-coding.h"
46 #include "packet.h"
47 #include "soft-interface.h"
48
49 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
50 {
51         struct device *dev = container_of(obj->parent, struct device, kobj);
52
53         return to_net_dev(dev);
54 }
55
56 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
57 {
58         struct net_device *net_dev = batadv_kobj_to_netdev(obj);
59
60         return netdev_priv(net_dev);
61 }
62
63 /**
64  * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
65  * @obj: kobject to covert
66  *
67  * Return: the associated batadv_priv struct.
68  */
69 static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
70 {
71         /* VLAN specific attributes are located in the root sysfs folder if they
72          * refer to the untagged VLAN..
73          */
74         if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
75                 return batadv_kobj_to_batpriv(obj);
76
77         /* ..while the attributes for the tagged vlans are located in
78          * the in the corresponding "vlan%VID" subfolder
79          */
80         return batadv_kobj_to_batpriv(obj->parent);
81 }
82
83 /**
84  * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
85  * @bat_priv: the bat priv with all the soft interface information
86  * @obj: kobject to covert
87  *
88  * Return: the associated softif_vlan struct if found, NULL otherwise.
89  */
90 static struct batadv_softif_vlan *
91 batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
92 {
93         struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
94
95         rcu_read_lock();
96         hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
97                 if (vlan_tmp->kobj != obj)
98                         continue;
99
100                 if (!atomic_inc_not_zero(&vlan_tmp->refcount))
101                         continue;
102
103                 vlan = vlan_tmp;
104                 break;
105         }
106         rcu_read_unlock();
107
108         return vlan;
109 }
110
111 #define BATADV_UEV_TYPE_VAR     "BATTYPE="
112 #define BATADV_UEV_ACTION_VAR   "BATACTION="
113 #define BATADV_UEV_DATA_VAR     "BATDATA="
114
115 static char *batadv_uev_action_str[] = {
116         "add",
117         "del",
118         "change"
119 };
120
121 static char *batadv_uev_type_str[] = {
122         "gw"
123 };
124
125 /* Use this, if you have customized show and store functions for vlan attrs */
126 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store)   \
127 struct batadv_attribute batadv_attr_vlan_##_name = {    \
128         .attr = {.name = __stringify(_name),            \
129                  .mode = _mode },                       \
130         .show   = _show,                                \
131         .store  = _store,                               \
132 }
133
134 /* Use this, if you have customized show and store functions */
135 #define BATADV_ATTR(_name, _mode, _show, _store)        \
136 struct batadv_attribute batadv_attr_##_name = {         \
137         .attr = {.name = __stringify(_name),            \
138                  .mode = _mode },                       \
139         .show   = _show,                                \
140         .store  = _store,                               \
141 }
142
143 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func)                   \
144 ssize_t batadv_store_##_name(struct kobject *kobj,                      \
145                              struct attribute *attr, char *buff,        \
146                              size_t count)                              \
147 {                                                                       \
148         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);       \
149         struct batadv_priv *bat_priv = netdev_priv(net_dev);            \
150                                                                         \
151         return __batadv_store_bool_attr(buff, count, _post_func, attr,  \
152                                         &bat_priv->_name, net_dev);     \
153 }
154
155 #define BATADV_ATTR_SIF_SHOW_BOOL(_name)                                \
156 ssize_t batadv_show_##_name(struct kobject *kobj,                       \
157                             struct attribute *attr, char *buff)         \
158 {                                                                       \
159         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);    \
160                                                                         \
161         return sprintf(buff, "%s\n",                                    \
162                        atomic_read(&bat_priv->_name) == 0 ?             \
163                        "disabled" : "enabled");                         \
164 }                                                                       \
165
166 /* Use this, if you are going to turn a [name] in the soft-interface
167  * (bat_priv) on or off
168  */
169 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func)                  \
170         static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func)            \
171         static BATADV_ATTR_SIF_SHOW_BOOL(_name)                         \
172         static BATADV_ATTR(_name, _mode, batadv_show_##_name,           \
173                            batadv_store_##_name)
174
175 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
176 ssize_t batadv_store_##_name(struct kobject *kobj,                      \
177                              struct attribute *attr, char *buff,        \
178                              size_t count)                              \
179 {                                                                       \
180         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);       \
181         struct batadv_priv *bat_priv = netdev_priv(net_dev);            \
182                                                                         \
183         return __batadv_store_uint_attr(buff, count, _min, _max,        \
184                                         _post_func, attr,               \
185                                         &bat_priv->_var, net_dev);      \
186 }
187
188 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var)                          \
189 ssize_t batadv_show_##_name(struct kobject *kobj,                       \
190                             struct attribute *attr, char *buff)         \
191 {                                                                       \
192         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);    \
193                                                                         \
194         return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var));     \
195 }                                                                       \
196
197 /* Use this, if you are going to set [name] in the soft-interface
198  * (bat_priv) to an unsigned integer value
199  */
200 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
201         static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
202         static BATADV_ATTR_SIF_SHOW_UINT(_name, _var)                   \
203         static BATADV_ATTR(_name, _mode, batadv_show_##_name,           \
204                            batadv_store_##_name)
205
206 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func)                  \
207 ssize_t batadv_store_vlan_##_name(struct kobject *kobj,                 \
208                                   struct attribute *attr, char *buff,   \
209                                   size_t count)                         \
210 {                                                                       \
211         struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
212         struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
213                                                               kobj);    \
214         size_t res = __batadv_store_bool_attr(buff, count, _post_func,  \
215                                               attr, &vlan->_name,       \
216                                               bat_priv->soft_iface);    \
217                                                                         \
218         batadv_softif_vlan_free_ref(vlan);                              \
219         return res;                                                     \
220 }
221
222 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name)                               \
223 ssize_t batadv_show_vlan_##_name(struct kobject *kobj,                  \
224                                  struct attribute *attr, char *buff)    \
225 {                                                                       \
226         struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
227         struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
228                                                               kobj);    \
229         size_t res = sprintf(buff, "%s\n",                              \
230                              atomic_read(&vlan->_name) == 0 ?           \
231                              "disabled" : "enabled");                   \
232                                                                         \
233         batadv_softif_vlan_free_ref(vlan);                              \
234         return res;                                                     \
235 }
236
237 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
238 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func)                 \
239         static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func)           \
240         static BATADV_ATTR_VLAN_SHOW_BOOL(_name)                        \
241         static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
242                                 batadv_store_vlan_##_name)
243
244 static int batadv_store_bool_attr(char *buff, size_t count,
245                                   struct net_device *net_dev,
246                                   const char *attr_name, atomic_t *attr,
247                                   bool *changed)
248 {
249         int enabled = -1;
250
251         *changed = false;
252
253         if (buff[count - 1] == '\n')
254                 buff[count - 1] = '\0';
255
256         if ((strncmp(buff, "1", 2) == 0) ||
257             (strncmp(buff, "enable", 7) == 0) ||
258             (strncmp(buff, "enabled", 8) == 0))
259                 enabled = 1;
260
261         if ((strncmp(buff, "0", 2) == 0) ||
262             (strncmp(buff, "disable", 8) == 0) ||
263             (strncmp(buff, "disabled", 9) == 0))
264                 enabled = 0;
265
266         if (enabled < 0) {
267                 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
268                             attr_name, buff);
269                 return -EINVAL;
270         }
271
272         if (atomic_read(attr) == enabled)
273                 return count;
274
275         batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
276                     atomic_read(attr) == 1 ? "enabled" : "disabled",
277                     enabled == 1 ? "enabled" : "disabled");
278
279         *changed = true;
280
281         atomic_set(attr, (unsigned int)enabled);
282         return count;
283 }
284
285 static inline ssize_t
286 __batadv_store_bool_attr(char *buff, size_t count,
287                          void (*post_func)(struct net_device *),
288                          struct attribute *attr,
289                          atomic_t *attr_store, struct net_device *net_dev)
290 {
291         bool changed;
292         int ret;
293
294         ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
295                                      attr_store, &changed);
296         if (post_func && changed)
297                 post_func(net_dev);
298
299         return ret;
300 }
301
302 static int batadv_store_uint_attr(const char *buff, size_t count,
303                                   struct net_device *net_dev,
304                                   const char *attr_name,
305                                   unsigned int min, unsigned int max,
306                                   atomic_t *attr)
307 {
308         unsigned long uint_val;
309         int ret;
310
311         ret = kstrtoul(buff, 10, &uint_val);
312         if (ret) {
313                 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
314                             attr_name, buff);
315                 return -EINVAL;
316         }
317
318         if (uint_val < min) {
319                 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
320                             attr_name, uint_val, min);
321                 return -EINVAL;
322         }
323
324         if (uint_val > max) {
325                 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
326                             attr_name, uint_val, max);
327                 return -EINVAL;
328         }
329
330         if (atomic_read(attr) == uint_val)
331                 return count;
332
333         batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
334                     attr_name, atomic_read(attr), uint_val);
335
336         atomic_set(attr, uint_val);
337         return count;
338 }
339
340 static inline ssize_t
341 __batadv_store_uint_attr(const char *buff, size_t count,
342                          int min, int max,
343                          void (*post_func)(struct net_device *),
344                          const struct attribute *attr,
345                          atomic_t *attr_store, struct net_device *net_dev)
346 {
347         int ret;
348
349         ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
350                                      attr_store);
351         if (post_func && ret)
352                 post_func(net_dev);
353
354         return ret;
355 }
356
357 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
358                                     struct attribute *attr, char *buff)
359 {
360         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
361
362         return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
363 }
364
365 static void batadv_post_gw_reselect(struct net_device *net_dev)
366 {
367         struct batadv_priv *bat_priv = netdev_priv(net_dev);
368
369         batadv_gw_reselect(bat_priv);
370 }
371
372 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
373                                    char *buff)
374 {
375         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
376         int bytes_written;
377
378         switch (atomic_read(&bat_priv->gw_mode)) {
379         case BATADV_GW_MODE_CLIENT:
380                 bytes_written = sprintf(buff, "%s\n",
381                                         BATADV_GW_MODE_CLIENT_NAME);
382                 break;
383         case BATADV_GW_MODE_SERVER:
384                 bytes_written = sprintf(buff, "%s\n",
385                                         BATADV_GW_MODE_SERVER_NAME);
386                 break;
387         default:
388                 bytes_written = sprintf(buff, "%s\n",
389                                         BATADV_GW_MODE_OFF_NAME);
390                 break;
391         }
392
393         return bytes_written;
394 }
395
396 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
397                                     struct attribute *attr, char *buff,
398                                     size_t count)
399 {
400         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
401         struct batadv_priv *bat_priv = netdev_priv(net_dev);
402         char *curr_gw_mode_str;
403         int gw_mode_tmp = -1;
404
405         if (buff[count - 1] == '\n')
406                 buff[count - 1] = '\0';
407
408         if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
409                     strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
410                 gw_mode_tmp = BATADV_GW_MODE_OFF;
411
412         if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
413                     strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
414                 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
415
416         if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
417                     strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
418                 gw_mode_tmp = BATADV_GW_MODE_SERVER;
419
420         if (gw_mode_tmp < 0) {
421                 batadv_info(net_dev,
422                             "Invalid parameter for 'gw mode' setting received: %s\n",
423                             buff);
424                 return -EINVAL;
425         }
426
427         if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
428                 return count;
429
430         switch (atomic_read(&bat_priv->gw_mode)) {
431         case BATADV_GW_MODE_CLIENT:
432                 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
433                 break;
434         case BATADV_GW_MODE_SERVER:
435                 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
436                 break;
437         default:
438                 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
439                 break;
440         }
441
442         batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
443                     curr_gw_mode_str, buff);
444
445         /* Invoking batadv_gw_reselect() is not enough to really de-select the
446          * current GW. It will only instruct the gateway client code to perform
447          * a re-election the next time that this is needed.
448          *
449          * When gw client mode is being switched off the current GW must be
450          * de-selected explicitly otherwise no GW_ADD uevent is thrown on
451          * client mode re-activation. This is operation is performed in
452          * batadv_gw_check_client_stop().
453          */
454         batadv_gw_reselect(bat_priv);
455         /* always call batadv_gw_check_client_stop() before changing the gateway
456          * state
457          */
458         batadv_gw_check_client_stop(bat_priv);
459         atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
460         batadv_gw_tvlv_container_update(bat_priv);
461         return count;
462 }
463
464 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
465                                      struct attribute *attr, char *buff)
466 {
467         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
468         u32 down, up;
469
470         down = atomic_read(&bat_priv->gw.bandwidth_down);
471         up = atomic_read(&bat_priv->gw.bandwidth_up);
472
473         return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
474                        down % 10, up / 10, up % 10);
475 }
476
477 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
478                                       struct attribute *attr, char *buff,
479                                       size_t count)
480 {
481         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
482
483         if (buff[count - 1] == '\n')
484                 buff[count - 1] = '\0';
485
486         return batadv_gw_bandwidth_set(net_dev, buff, count);
487 }
488
489 /**
490  * batadv_show_isolation_mark - print the current isolation mark/mask
491  * @kobj: kobject representing the private mesh sysfs directory
492  * @attr: the batman-adv attribute the user is interacting with
493  * @buff: the buffer that will contain the data to send back to the user
494  *
495  * Return: the number of bytes written into 'buff' on success or a negative
496  * error code in case of failure
497  */
498 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
499                                           struct attribute *attr, char *buff)
500 {
501         struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
502
503         return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
504                        bat_priv->isolation_mark_mask);
505 }
506
507 /**
508  * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
509  *  by the user
510  * @kobj: kobject representing the private mesh sysfs directory
511  * @attr: the batman-adv attribute the user is interacting with
512  * @buff: the buffer containing the user data
513  * @count: number of bytes in the buffer
514  *
515  * Return: 'count' on success or a negative error code in case of failure
516  */
517 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
518                                            struct attribute *attr, char *buff,
519                                            size_t count)
520 {
521         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
522         struct batadv_priv *bat_priv = netdev_priv(net_dev);
523         u32 mark, mask;
524         char *mask_ptr;
525
526         /* parse the mask if it has been specified, otherwise assume the mask is
527          * the biggest possible
528          */
529         mask = 0xFFFFFFFF;
530         mask_ptr = strchr(buff, '/');
531         if (mask_ptr) {
532                 *mask_ptr = '\0';
533                 mask_ptr++;
534
535                 /* the mask must be entered in hex base as it is going to be a
536                  * bitmask and not a prefix length
537                  */
538                 if (kstrtou32(mask_ptr, 16, &mask) < 0)
539                         return -EINVAL;
540         }
541
542         /* the mark can be entered in any base */
543         if (kstrtou32(buff, 0, &mark) < 0)
544                 return -EINVAL;
545
546         bat_priv->isolation_mark_mask = mask;
547         /* erase bits not covered by the mask */
548         bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
549
550         batadv_info(net_dev,
551                     "New skb mark for extended isolation: %#.8x/%#.8x\n",
552                     bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
553
554         return count;
555 }
556
557 BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
558 BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
559 #ifdef CONFIG_BATMAN_ADV_BLA
560 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR,
561                      batadv_bla_status_update);
562 #endif
563 #ifdef CONFIG_BATMAN_ADV_DAT
564 BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
565                      batadv_dat_status_update);
566 #endif
567 BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
568 static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
569 static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
570                    batadv_store_gw_mode);
571 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, S_IRUGO | S_IWUSR,
572                      2 * BATADV_JITTER, INT_MAX, NULL);
573 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, S_IRUGO | S_IWUSR, 0,
574                      BATADV_TQ_MAX_VALUE, NULL);
575 BATADV_ATTR_SIF_UINT(gw_sel_class, gw_sel_class, S_IRUGO | S_IWUSR, 1,
576                      BATADV_TQ_MAX_VALUE, batadv_post_gw_reselect);
577 static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
578                    batadv_store_gw_bwidth);
579 #ifdef CONFIG_BATMAN_ADV_MCAST
580 BATADV_ATTR_SIF_BOOL(multicast_mode, S_IRUGO | S_IWUSR, NULL);
581 #endif
582 #ifdef CONFIG_BATMAN_ADV_DEBUG
583 BATADV_ATTR_SIF_UINT(log_level, log_level, S_IRUGO | S_IWUSR, 0,
584                      BATADV_DBG_ALL, NULL);
585 #endif
586 #ifdef CONFIG_BATMAN_ADV_NC
587 BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
588                      batadv_nc_status_update);
589 #endif
590 static BATADV_ATTR(isolation_mark, S_IRUGO | S_IWUSR,
591                    batadv_show_isolation_mark, batadv_store_isolation_mark);
592
593 static struct batadv_attribute *batadv_mesh_attrs[] = {
594         &batadv_attr_aggregated_ogms,
595         &batadv_attr_bonding,
596 #ifdef CONFIG_BATMAN_ADV_BLA
597         &batadv_attr_bridge_loop_avoidance,
598 #endif
599 #ifdef CONFIG_BATMAN_ADV_DAT
600         &batadv_attr_distributed_arp_table,
601 #endif
602 #ifdef CONFIG_BATMAN_ADV_MCAST
603         &batadv_attr_multicast_mode,
604 #endif
605         &batadv_attr_fragmentation,
606         &batadv_attr_routing_algo,
607         &batadv_attr_gw_mode,
608         &batadv_attr_orig_interval,
609         &batadv_attr_hop_penalty,
610         &batadv_attr_gw_sel_class,
611         &batadv_attr_gw_bandwidth,
612 #ifdef CONFIG_BATMAN_ADV_DEBUG
613         &batadv_attr_log_level,
614 #endif
615 #ifdef CONFIG_BATMAN_ADV_NC
616         &batadv_attr_network_coding,
617 #endif
618         &batadv_attr_isolation_mark,
619         NULL,
620 };
621
622 BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
623
624 /* array of vlan specific sysfs attributes */
625 static struct batadv_attribute *batadv_vlan_attrs[] = {
626         &batadv_attr_vlan_ap_isolation,
627         NULL,
628 };
629
630 int batadv_sysfs_add_meshif(struct net_device *dev)
631 {
632         struct kobject *batif_kobject = &dev->dev.kobj;
633         struct batadv_priv *bat_priv = netdev_priv(dev);
634         struct batadv_attribute **bat_attr;
635         int err;
636
637         bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
638                                                     batif_kobject);
639         if (!bat_priv->mesh_obj) {
640                 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
641                            BATADV_SYSFS_IF_MESH_SUBDIR);
642                 goto out;
643         }
644
645         for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
646                 err = sysfs_create_file(bat_priv->mesh_obj,
647                                         &((*bat_attr)->attr));
648                 if (err) {
649                         batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
650                                    dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
651                                    ((*bat_attr)->attr).name);
652                         goto rem_attr;
653                 }
654         }
655
656         return 0;
657
658 rem_attr:
659         for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
660                 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
661
662         kobject_put(bat_priv->mesh_obj);
663         bat_priv->mesh_obj = NULL;
664 out:
665         return -ENOMEM;
666 }
667
668 void batadv_sysfs_del_meshif(struct net_device *dev)
669 {
670         struct batadv_priv *bat_priv = netdev_priv(dev);
671         struct batadv_attribute **bat_attr;
672
673         for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
674                 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
675
676         kobject_put(bat_priv->mesh_obj);
677         bat_priv->mesh_obj = NULL;
678 }
679
680 /**
681  * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
682  * @dev: netdev of the mesh interface
683  * @vlan: private data of the newly added VLAN interface
684  *
685  * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
686  */
687 int batadv_sysfs_add_vlan(struct net_device *dev,
688                           struct batadv_softif_vlan *vlan)
689 {
690         char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
691         struct batadv_priv *bat_priv = netdev_priv(dev);
692         struct batadv_attribute **bat_attr;
693         int err;
694
695         if (vlan->vid & BATADV_VLAN_HAS_TAG) {
696                 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
697                         vlan->vid & VLAN_VID_MASK);
698
699                 vlan->kobj = kobject_create_and_add(vlan_subdir,
700                                                     bat_priv->mesh_obj);
701                 if (!vlan->kobj) {
702                         batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
703                                    dev->name, vlan_subdir);
704                         goto out;
705                 }
706         } else {
707                 /* the untagged LAN uses the root folder to store its "VLAN
708                  * specific attributes"
709                  */
710                 vlan->kobj = bat_priv->mesh_obj;
711                 kobject_get(bat_priv->mesh_obj);
712         }
713
714         for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
715                 err = sysfs_create_file(vlan->kobj,
716                                         &((*bat_attr)->attr));
717                 if (err) {
718                         batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
719                                    dev->name, vlan_subdir,
720                                    ((*bat_attr)->attr).name);
721                         goto rem_attr;
722                 }
723         }
724
725         return 0;
726
727 rem_attr:
728         for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
729                 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
730
731         kobject_put(vlan->kobj);
732         vlan->kobj = NULL;
733 out:
734         return -ENOMEM;
735 }
736
737 /**
738  * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
739  * @bat_priv: the bat priv with all the soft interface information
740  * @vlan: the private data of the VLAN to destroy
741  */
742 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
743                            struct batadv_softif_vlan *vlan)
744 {
745         struct batadv_attribute **bat_attr;
746
747         for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
748                 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
749
750         kobject_put(vlan->kobj);
751         vlan->kobj = NULL;
752 }
753
754 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
755                                       struct attribute *attr, char *buff)
756 {
757         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
758         struct batadv_hard_iface *hard_iface;
759         ssize_t length;
760         const char *ifname;
761
762         hard_iface = batadv_hardif_get_by_netdev(net_dev);
763         if (!hard_iface)
764                 return 0;
765
766         if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
767                 ifname =  "none";
768         else
769                 ifname = hard_iface->soft_iface->name;
770
771         length = sprintf(buff, "%s\n", ifname);
772
773         batadv_hardif_free_ref(hard_iface);
774
775         return length;
776 }
777
778 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
779                                        struct attribute *attr, char *buff,
780                                        size_t count)
781 {
782         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
783         struct batadv_hard_iface *hard_iface;
784         int status_tmp = -1;
785         int ret = count;
786
787         hard_iface = batadv_hardif_get_by_netdev(net_dev);
788         if (!hard_iface)
789                 return count;
790
791         if (buff[count - 1] == '\n')
792                 buff[count - 1] = '\0';
793
794         if (strlen(buff) >= IFNAMSIZ) {
795                 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
796                        buff);
797                 batadv_hardif_free_ref(hard_iface);
798                 return -EINVAL;
799         }
800
801         if (strncmp(buff, "none", 4) == 0)
802                 status_tmp = BATADV_IF_NOT_IN_USE;
803         else
804                 status_tmp = BATADV_IF_I_WANT_YOU;
805
806         if (hard_iface->if_status == status_tmp)
807                 goto out;
808
809         if ((hard_iface->soft_iface) &&
810             (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
811                 goto out;
812
813         rtnl_lock();
814
815         if (status_tmp == BATADV_IF_NOT_IN_USE) {
816                 batadv_hardif_disable_interface(hard_iface,
817                                                 BATADV_IF_CLEANUP_AUTO);
818                 goto unlock;
819         }
820
821         /* if the interface already is in use */
822         if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
823                 batadv_hardif_disable_interface(hard_iface,
824                                                 BATADV_IF_CLEANUP_AUTO);
825
826         ret = batadv_hardif_enable_interface(hard_iface, buff);
827
828 unlock:
829         rtnl_unlock();
830 out:
831         batadv_hardif_free_ref(hard_iface);
832         return ret;
833 }
834
835 static ssize_t batadv_show_iface_status(struct kobject *kobj,
836                                         struct attribute *attr, char *buff)
837 {
838         struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
839         struct batadv_hard_iface *hard_iface;
840         ssize_t length;
841
842         hard_iface = batadv_hardif_get_by_netdev(net_dev);
843         if (!hard_iface)
844                 return 0;
845
846         switch (hard_iface->if_status) {
847         case BATADV_IF_TO_BE_REMOVED:
848                 length = sprintf(buff, "disabling\n");
849                 break;
850         case BATADV_IF_INACTIVE:
851                 length = sprintf(buff, "inactive\n");
852                 break;
853         case BATADV_IF_ACTIVE:
854                 length = sprintf(buff, "active\n");
855                 break;
856         case BATADV_IF_TO_BE_ACTIVATED:
857                 length = sprintf(buff, "enabling\n");
858                 break;
859         case BATADV_IF_NOT_IN_USE:
860         default:
861                 length = sprintf(buff, "not in use\n");
862                 break;
863         }
864
865         batadv_hardif_free_ref(hard_iface);
866
867         return length;
868 }
869
870 static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
871                    batadv_store_mesh_iface);
872 static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
873
874 static struct batadv_attribute *batadv_batman_attrs[] = {
875         &batadv_attr_mesh_iface,
876         &batadv_attr_iface_status,
877         NULL,
878 };
879
880 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
881 {
882         struct kobject *hardif_kobject = &dev->dev.kobj;
883         struct batadv_attribute **bat_attr;
884         int err;
885
886         *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
887                                              hardif_kobject);
888
889         if (!*hardif_obj) {
890                 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
891                            BATADV_SYSFS_IF_BAT_SUBDIR);
892                 goto out;
893         }
894
895         for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
896                 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
897                 if (err) {
898                         batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
899                                    dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
900                                    ((*bat_attr)->attr).name);
901                         goto rem_attr;
902                 }
903         }
904
905         return 0;
906
907 rem_attr:
908         for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
909                 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
910 out:
911         return -ENOMEM;
912 }
913
914 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
915 {
916         kobject_put(*hardif_obj);
917         *hardif_obj = NULL;
918 }
919
920 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
921                         enum batadv_uev_action action, const char *data)
922 {
923         int ret = -ENOMEM;
924         struct kobject *bat_kobj;
925         char *uevent_env[4] = { NULL, NULL, NULL, NULL };
926
927         bat_kobj = &bat_priv->soft_iface->dev.kobj;
928
929         uevent_env[0] = kasprintf(GFP_ATOMIC,
930                                   "%s%s", BATADV_UEV_TYPE_VAR,
931                                   batadv_uev_type_str[type]);
932         if (!uevent_env[0])
933                 goto out;
934
935         uevent_env[1] = kasprintf(GFP_ATOMIC,
936                                   "%s%s", BATADV_UEV_ACTION_VAR,
937                                   batadv_uev_action_str[action]);
938         if (!uevent_env[1])
939                 goto out;
940
941         /* If the event is DEL, ignore the data field */
942         if (action != BATADV_UEV_DEL) {
943                 uevent_env[2] = kasprintf(GFP_ATOMIC,
944                                           "%s%s", BATADV_UEV_DATA_VAR, data);
945                 if (!uevent_env[2])
946                         goto out;
947         }
948
949         ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
950 out:
951         kfree(uevent_env[0]);
952         kfree(uevent_env[1]);
953         kfree(uevent_env[2]);
954
955         if (ret)
956                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
957                            "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
958                            batadv_uev_type_str[type],
959                            batadv_uev_action_str[action],
960                            (action == BATADV_UEV_DEL ? "NULL" : data), ret);
961         return ret;
962 }