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