]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/batman-adv/sysfs.c
Merge branch 'drm-nouveau-next' of git://anongit.freedesktop.org/git/nouveau/linux...
[mirror_ubuntu-artful-kernel.git] / net / batman-adv / sysfs.c
1 /* Copyright (C) 2010-2013 B.A.T.M.A.N. contributors:
2 *
3 * Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
20 #include "main.h"
21 #include "sysfs.h"
22 #include "translation-table.h"
23 #include "distributed-arp-table.h"
24 #include "network-coding.h"
25 #include "originator.h"
26 #include "hard-interface.h"
27 #include "soft-interface.h"
28 #include "gateway_common.h"
29 #include "gateway_client.h"
30
31 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
32 {
33 struct device *dev = container_of(obj->parent, struct device, kobj);
34 return to_net_dev(dev);
35 }
36
37 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
38 {
39 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
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 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
130 &bat_priv->_name, net_dev); \
131 }
132
133 #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
134 ssize_t batadv_show_##_name(struct kobject *kobj, \
135 struct attribute *attr, char *buff) \
136 { \
137 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
138 return sprintf(buff, "%s\n", \
139 atomic_read(&bat_priv->_name) == 0 ? \
140 "disabled" : "enabled"); \
141 } \
142
143 /* Use this, if you are going to turn a [name] in the soft-interface
144 * (bat_priv) on or off
145 */
146 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
147 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
148 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
149 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
150 batadv_store_##_name)
151
152
153 #define BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
154 ssize_t batadv_store_##_name(struct kobject *kobj, \
155 struct attribute *attr, char *buff, \
156 size_t count) \
157 { \
158 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
159 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
160 return __batadv_store_uint_attr(buff, count, _min, _max, \
161 _post_func, attr, \
162 &bat_priv->_name, net_dev); \
163 }
164
165 #define BATADV_ATTR_SIF_SHOW_UINT(_name) \
166 ssize_t batadv_show_##_name(struct kobject *kobj, \
167 struct attribute *attr, char *buff) \
168 { \
169 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
170 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
171 } \
172
173 /* Use this, if you are going to set [name] in the soft-interface
174 * (bat_priv) to an unsigned integer value
175 */
176 #define BATADV_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
177 static BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)\
178 static BATADV_ATTR_SIF_SHOW_UINT(_name) \
179 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
180 batadv_store_##_name)
181
182 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
183 ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
184 struct attribute *attr, char *buff, \
185 size_t count) \
186 { \
187 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
188 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
189 kobj); \
190 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
191 attr, &vlan->_name, \
192 bat_priv->soft_iface); \
193 batadv_softif_vlan_free_ref(vlan); \
194 return res; \
195 }
196
197 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
198 ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
199 struct attribute *attr, char *buff) \
200 { \
201 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
202 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
203 kobj); \
204 size_t res = sprintf(buff, "%s\n", \
205 atomic_read(&vlan->_name) == 0 ? \
206 "disabled" : "enabled"); \
207 batadv_softif_vlan_free_ref(vlan); \
208 return res; \
209 }
210
211 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
212 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
213 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
214 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
215 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
216 batadv_store_vlan_##_name)
217
218 static int batadv_store_bool_attr(char *buff, size_t count,
219 struct net_device *net_dev,
220 const char *attr_name, atomic_t *attr)
221 {
222 int enabled = -1;
223
224 if (buff[count - 1] == '\n')
225 buff[count - 1] = '\0';
226
227 if ((strncmp(buff, "1", 2) == 0) ||
228 (strncmp(buff, "enable", 7) == 0) ||
229 (strncmp(buff, "enabled", 8) == 0))
230 enabled = 1;
231
232 if ((strncmp(buff, "0", 2) == 0) ||
233 (strncmp(buff, "disable", 8) == 0) ||
234 (strncmp(buff, "disabled", 9) == 0))
235 enabled = 0;
236
237 if (enabled < 0) {
238 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
239 attr_name, buff);
240 return -EINVAL;
241 }
242
243 if (atomic_read(attr) == enabled)
244 return count;
245
246 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
247 atomic_read(attr) == 1 ? "enabled" : "disabled",
248 enabled == 1 ? "enabled" : "disabled");
249
250 atomic_set(attr, (unsigned int)enabled);
251 return count;
252 }
253
254 static inline ssize_t
255 __batadv_store_bool_attr(char *buff, size_t count,
256 void (*post_func)(struct net_device *),
257 struct attribute *attr,
258 atomic_t *attr_store, struct net_device *net_dev)
259 {
260 int ret;
261
262 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
263 attr_store);
264 if (post_func && ret)
265 post_func(net_dev);
266
267 return ret;
268 }
269
270 static int batadv_store_uint_attr(const char *buff, size_t count,
271 struct net_device *net_dev,
272 const char *attr_name,
273 unsigned int min, unsigned int max,
274 atomic_t *attr)
275 {
276 unsigned long uint_val;
277 int ret;
278
279 ret = kstrtoul(buff, 10, &uint_val);
280 if (ret) {
281 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
282 attr_name, buff);
283 return -EINVAL;
284 }
285
286 if (uint_val < min) {
287 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
288 attr_name, uint_val, min);
289 return -EINVAL;
290 }
291
292 if (uint_val > max) {
293 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
294 attr_name, uint_val, max);
295 return -EINVAL;
296 }
297
298 if (atomic_read(attr) == uint_val)
299 return count;
300
301 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
302 attr_name, atomic_read(attr), uint_val);
303
304 atomic_set(attr, uint_val);
305 return count;
306 }
307
308 static inline ssize_t
309 __batadv_store_uint_attr(const char *buff, size_t count,
310 int min, int max,
311 void (*post_func)(struct net_device *),
312 const struct attribute *attr,
313 atomic_t *attr_store, struct net_device *net_dev)
314 {
315 int ret;
316
317 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
318 attr_store);
319 if (post_func && ret)
320 post_func(net_dev);
321
322 return ret;
323 }
324
325 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
326 struct attribute *attr, char *buff)
327 {
328 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
329 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
330 }
331
332 static void batadv_post_gw_deselect(struct net_device *net_dev)
333 {
334 struct batadv_priv *bat_priv = netdev_priv(net_dev);
335 batadv_gw_deselect(bat_priv);
336 }
337
338 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
339 char *buff)
340 {
341 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
342 int bytes_written;
343
344 switch (atomic_read(&bat_priv->gw_mode)) {
345 case BATADV_GW_MODE_CLIENT:
346 bytes_written = sprintf(buff, "%s\n",
347 BATADV_GW_MODE_CLIENT_NAME);
348 break;
349 case BATADV_GW_MODE_SERVER:
350 bytes_written = sprintf(buff, "%s\n",
351 BATADV_GW_MODE_SERVER_NAME);
352 break;
353 default:
354 bytes_written = sprintf(buff, "%s\n",
355 BATADV_GW_MODE_OFF_NAME);
356 break;
357 }
358
359 return bytes_written;
360 }
361
362 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
363 struct attribute *attr, char *buff,
364 size_t count)
365 {
366 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
367 struct batadv_priv *bat_priv = netdev_priv(net_dev);
368 char *curr_gw_mode_str;
369 int gw_mode_tmp = -1;
370
371 if (buff[count - 1] == '\n')
372 buff[count - 1] = '\0';
373
374 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
375 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
376 gw_mode_tmp = BATADV_GW_MODE_OFF;
377
378 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
379 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
380 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
381
382 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
383 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
384 gw_mode_tmp = BATADV_GW_MODE_SERVER;
385
386 if (gw_mode_tmp < 0) {
387 batadv_info(net_dev,
388 "Invalid parameter for 'gw mode' setting received: %s\n",
389 buff);
390 return -EINVAL;
391 }
392
393 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
394 return count;
395
396 switch (atomic_read(&bat_priv->gw_mode)) {
397 case BATADV_GW_MODE_CLIENT:
398 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
399 break;
400 case BATADV_GW_MODE_SERVER:
401 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
402 break;
403 default:
404 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
405 break;
406 }
407
408 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
409 curr_gw_mode_str, buff);
410
411 batadv_gw_deselect(bat_priv);
412 /* always call batadv_gw_check_client_stop() before changing the gateway
413 * state
414 */
415 batadv_gw_check_client_stop(bat_priv);
416 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
417 batadv_gw_tvlv_container_update(bat_priv);
418 return count;
419 }
420
421 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
422 struct attribute *attr, char *buff)
423 {
424 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
425 uint32_t down, up;
426
427 down = atomic_read(&bat_priv->gw.bandwidth_down);
428 up = atomic_read(&bat_priv->gw.bandwidth_up);
429
430 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
431 down % 10, up / 10, up % 10);
432 }
433
434 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
435 struct attribute *attr, char *buff,
436 size_t count)
437 {
438 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
439
440 if (buff[count - 1] == '\n')
441 buff[count - 1] = '\0';
442
443 return batadv_gw_bandwidth_set(net_dev, buff, count);
444 }
445
446 BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
447 BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
448 #ifdef CONFIG_BATMAN_ADV_BLA
449 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
450 #endif
451 #ifdef CONFIG_BATMAN_ADV_DAT
452 BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
453 batadv_dat_status_update);
454 #endif
455 BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
456 static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
457 static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
458 batadv_store_gw_mode);
459 BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
460 INT_MAX, NULL);
461 BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
462 NULL);
463 BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
464 batadv_post_gw_deselect);
465 static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
466 batadv_store_gw_bwidth);
467 #ifdef CONFIG_BATMAN_ADV_DEBUG
468 BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
469 #endif
470 #ifdef CONFIG_BATMAN_ADV_NC
471 BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
472 batadv_nc_status_update);
473 #endif
474
475 static struct batadv_attribute *batadv_mesh_attrs[] = {
476 &batadv_attr_aggregated_ogms,
477 &batadv_attr_bonding,
478 #ifdef CONFIG_BATMAN_ADV_BLA
479 &batadv_attr_bridge_loop_avoidance,
480 #endif
481 #ifdef CONFIG_BATMAN_ADV_DAT
482 &batadv_attr_distributed_arp_table,
483 #endif
484 &batadv_attr_fragmentation,
485 &batadv_attr_routing_algo,
486 &batadv_attr_gw_mode,
487 &batadv_attr_orig_interval,
488 &batadv_attr_hop_penalty,
489 &batadv_attr_gw_sel_class,
490 &batadv_attr_gw_bandwidth,
491 #ifdef CONFIG_BATMAN_ADV_DEBUG
492 &batadv_attr_log_level,
493 #endif
494 #ifdef CONFIG_BATMAN_ADV_NC
495 &batadv_attr_network_coding,
496 #endif
497 NULL,
498 };
499
500 BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
501
502 /**
503 * batadv_vlan_attrs - array of vlan specific sysfs attributes
504 */
505 static struct batadv_attribute *batadv_vlan_attrs[] = {
506 &batadv_attr_vlan_ap_isolation,
507 NULL,
508 };
509
510 int batadv_sysfs_add_meshif(struct net_device *dev)
511 {
512 struct kobject *batif_kobject = &dev->dev.kobj;
513 struct batadv_priv *bat_priv = netdev_priv(dev);
514 struct batadv_attribute **bat_attr;
515 int err;
516
517 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
518 batif_kobject);
519 if (!bat_priv->mesh_obj) {
520 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
521 BATADV_SYSFS_IF_MESH_SUBDIR);
522 goto out;
523 }
524
525 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
526 err = sysfs_create_file(bat_priv->mesh_obj,
527 &((*bat_attr)->attr));
528 if (err) {
529 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
530 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
531 ((*bat_attr)->attr).name);
532 goto rem_attr;
533 }
534 }
535
536 return 0;
537
538 rem_attr:
539 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
540 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
541
542 kobject_put(bat_priv->mesh_obj);
543 bat_priv->mesh_obj = NULL;
544 out:
545 return -ENOMEM;
546 }
547
548 void batadv_sysfs_del_meshif(struct net_device *dev)
549 {
550 struct batadv_priv *bat_priv = netdev_priv(dev);
551 struct batadv_attribute **bat_attr;
552
553 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
554 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
555
556 kobject_put(bat_priv->mesh_obj);
557 bat_priv->mesh_obj = NULL;
558 }
559
560 /**
561 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
562 * @dev: netdev of the mesh interface
563 * @vlan: private data of the newly added VLAN interface
564 *
565 * Returns 0 on success and -ENOMEM if any of the structure allocations fails.
566 */
567 int batadv_sysfs_add_vlan(struct net_device *dev,
568 struct batadv_softif_vlan *vlan)
569 {
570 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
571 struct batadv_priv *bat_priv = netdev_priv(dev);
572 struct batadv_attribute **bat_attr;
573 int err;
574
575 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
576 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
577 vlan->vid & VLAN_VID_MASK);
578
579 vlan->kobj = kobject_create_and_add(vlan_subdir,
580 bat_priv->mesh_obj);
581 if (!vlan->kobj) {
582 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
583 dev->name, vlan_subdir);
584 goto out;
585 }
586 } else {
587 /* the untagged LAN uses the root folder to store its "VLAN
588 * specific attributes"
589 */
590 vlan->kobj = bat_priv->mesh_obj;
591 kobject_get(bat_priv->mesh_obj);
592 }
593
594 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
595 err = sysfs_create_file(vlan->kobj,
596 &((*bat_attr)->attr));
597 if (err) {
598 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
599 dev->name, vlan_subdir,
600 ((*bat_attr)->attr).name);
601 goto rem_attr;
602 }
603 }
604
605 return 0;
606
607 rem_attr:
608 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
609 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
610
611 kobject_put(vlan->kobj);
612 vlan->kobj = NULL;
613 out:
614 return -ENOMEM;
615 }
616
617 /**
618 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
619 * @bat_priv: the bat priv with all the soft interface information
620 * @vlan: the private data of the VLAN to destroy
621 */
622 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
623 struct batadv_softif_vlan *vlan)
624 {
625 struct batadv_attribute **bat_attr;
626
627 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
628 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
629
630 kobject_put(vlan->kobj);
631 vlan->kobj = NULL;
632 }
633
634 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
635 struct attribute *attr, char *buff)
636 {
637 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
638 struct batadv_hard_iface *hard_iface;
639 ssize_t length;
640 const char *ifname;
641
642 hard_iface = batadv_hardif_get_by_netdev(net_dev);
643 if (!hard_iface)
644 return 0;
645
646 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
647 ifname = "none";
648 else
649 ifname = hard_iface->soft_iface->name;
650
651 length = sprintf(buff, "%s\n", ifname);
652
653 batadv_hardif_free_ref(hard_iface);
654
655 return length;
656 }
657
658 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
659 struct attribute *attr, char *buff,
660 size_t count)
661 {
662 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
663 struct batadv_hard_iface *hard_iface;
664 int status_tmp = -1;
665 int ret = count;
666
667 hard_iface = batadv_hardif_get_by_netdev(net_dev);
668 if (!hard_iface)
669 return count;
670
671 if (buff[count - 1] == '\n')
672 buff[count - 1] = '\0';
673
674 if (strlen(buff) >= IFNAMSIZ) {
675 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
676 buff);
677 batadv_hardif_free_ref(hard_iface);
678 return -EINVAL;
679 }
680
681 if (strncmp(buff, "none", 4) == 0)
682 status_tmp = BATADV_IF_NOT_IN_USE;
683 else
684 status_tmp = BATADV_IF_I_WANT_YOU;
685
686 if (hard_iface->if_status == status_tmp)
687 goto out;
688
689 if ((hard_iface->soft_iface) &&
690 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
691 goto out;
692
693 rtnl_lock();
694
695 if (status_tmp == BATADV_IF_NOT_IN_USE) {
696 batadv_hardif_disable_interface(hard_iface,
697 BATADV_IF_CLEANUP_AUTO);
698 goto unlock;
699 }
700
701 /* if the interface already is in use */
702 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
703 batadv_hardif_disable_interface(hard_iface,
704 BATADV_IF_CLEANUP_AUTO);
705
706 ret = batadv_hardif_enable_interface(hard_iface, buff);
707
708 unlock:
709 rtnl_unlock();
710 out:
711 batadv_hardif_free_ref(hard_iface);
712 return ret;
713 }
714
715 static ssize_t batadv_show_iface_status(struct kobject *kobj,
716 struct attribute *attr, char *buff)
717 {
718 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
719 struct batadv_hard_iface *hard_iface;
720 ssize_t length;
721
722 hard_iface = batadv_hardif_get_by_netdev(net_dev);
723 if (!hard_iface)
724 return 0;
725
726 switch (hard_iface->if_status) {
727 case BATADV_IF_TO_BE_REMOVED:
728 length = sprintf(buff, "disabling\n");
729 break;
730 case BATADV_IF_INACTIVE:
731 length = sprintf(buff, "inactive\n");
732 break;
733 case BATADV_IF_ACTIVE:
734 length = sprintf(buff, "active\n");
735 break;
736 case BATADV_IF_TO_BE_ACTIVATED:
737 length = sprintf(buff, "enabling\n");
738 break;
739 case BATADV_IF_NOT_IN_USE:
740 default:
741 length = sprintf(buff, "not in use\n");
742 break;
743 }
744
745 batadv_hardif_free_ref(hard_iface);
746
747 return length;
748 }
749
750 static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
751 batadv_store_mesh_iface);
752 static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
753
754 static struct batadv_attribute *batadv_batman_attrs[] = {
755 &batadv_attr_mesh_iface,
756 &batadv_attr_iface_status,
757 NULL,
758 };
759
760 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
761 {
762 struct kobject *hardif_kobject = &dev->dev.kobj;
763 struct batadv_attribute **bat_attr;
764 int err;
765
766 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
767 hardif_kobject);
768
769 if (!*hardif_obj) {
770 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
771 BATADV_SYSFS_IF_BAT_SUBDIR);
772 goto out;
773 }
774
775 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
776 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
777 if (err) {
778 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
779 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
780 ((*bat_attr)->attr).name);
781 goto rem_attr;
782 }
783 }
784
785 return 0;
786
787 rem_attr:
788 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
789 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
790 out:
791 return -ENOMEM;
792 }
793
794 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
795 {
796 kobject_put(*hardif_obj);
797 *hardif_obj = NULL;
798 }
799
800 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
801 enum batadv_uev_action action, const char *data)
802 {
803 int ret = -ENOMEM;
804 struct kobject *bat_kobj;
805 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
806
807 bat_kobj = &bat_priv->soft_iface->dev.kobj;
808
809 uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
810 strlen(batadv_uev_type_str[type]) + 1,
811 GFP_ATOMIC);
812 if (!uevent_env[0])
813 goto out;
814
815 sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
816 batadv_uev_type_str[type]);
817
818 uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
819 strlen(batadv_uev_action_str[action]) + 1,
820 GFP_ATOMIC);
821 if (!uevent_env[1])
822 goto out;
823
824 sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
825 batadv_uev_action_str[action]);
826
827 /* If the event is DEL, ignore the data field */
828 if (action != BATADV_UEV_DEL) {
829 uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
830 strlen(data) + 1, GFP_ATOMIC);
831 if (!uevent_env[2])
832 goto out;
833
834 sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
835 }
836
837 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
838 out:
839 kfree(uevent_env[0]);
840 kfree(uevent_env[1]);
841 kfree(uevent_env[2]);
842
843 if (ret)
844 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
845 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
846 batadv_uev_type_str[type],
847 batadv_uev_action_str[action],
848 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
849 return ret;
850 }