bat_sysfs.c 20.5 KB
Newer Older
1
/* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Marek Lindner
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

#include "main.h"
#include "bat_sysfs.h"
#include "translation-table.h"
#include "originator.h"
#include "hard-interface.h"
#include "gateway_common.h"
#include "gateway_client.h"
#include "vis.h"

29
static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
30 31 32 33 34
{
	struct device *dev = container_of(obj->parent, struct device, kobj);
	return to_net_dev(dev);
}

35
static struct bat_priv *batadv_kobj_to_batpriv(struct kobject *obj)
36
{
37
	struct net_device *net_dev = batadv_kobj_to_netdev(obj);
38 39
	return netdev_priv(net_dev);
}
40

41 42 43 44
#define UEV_TYPE_VAR	"BATTYPE="
#define UEV_ACTION_VAR	"BATACTION="
#define UEV_DATA_VAR	"BATDATA="

45
static char *batadv_uev_action_str[] = {
46 47 48 49 50
	"add",
	"del",
	"change"
};

51
static char *batadv_uev_type_str[] = {
52 53 54
	"gw"
};

55 56
/* Use this, if you have customized show and store functions */
#define BAT_ATTR(_name, _mode, _show, _store)	\
57
struct bat_attribute batadv_attr_##_name = {	\
58 59 60 61 62 63
	.attr = {.name = __stringify(_name),	\
		 .mode = _mode },		\
	.show   = _show,			\
	.store  = _store,			\
};

64
#define BAT_ATTR_SIF_STORE_BOOL(_name, _post_func)			\
65 66 67
ssize_t batadv_store_##_name(struct kobject *kobj,			\
			     struct attribute *attr, char *buff,	\
			     size_t count)				\
68
{									\
69
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);	\
70
	struct bat_priv *bat_priv = netdev_priv(net_dev);		\
71 72
	return __batadv_store_bool_attr(buff, count, _post_func, attr,	\
					&bat_priv->_name, net_dev);	\
73 74
}

75
#define BAT_ATTR_SIF_SHOW_BOOL(_name)					\
76 77
ssize_t batadv_show_##_name(struct kobject *kobj,			\
			    struct attribute *attr, char *buff)		\
78
{									\
79
	struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);	\
80 81 82 83 84
	return sprintf(buff, "%s\n",					\
		       atomic_read(&bat_priv->_name) == 0 ?		\
		       "disabled" : "enabled");				\
}									\

85
/* Use this, if you are going to turn a [name] in the soft-interface
86 87
 * (bat_priv) on or off
 */
88 89 90
#define BAT_ATTR_SIF_BOOL(_name, _mode, _post_func)			\
	static BAT_ATTR_SIF_STORE_BOOL(_name, _post_func)		\
	static BAT_ATTR_SIF_SHOW_BOOL(_name)				\
91 92
	static BAT_ATTR(_name, _mode, batadv_show_##_name,		\
			batadv_store_##_name)
93 94


95
#define BAT_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)		\
96 97 98
ssize_t batadv_store_##_name(struct kobject *kobj,			\
			     struct attribute *attr, char *buff,	\
			     size_t count)				\
99
{									\
100
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);	\
101
	struct bat_priv *bat_priv = netdev_priv(net_dev);		\
102 103 104
	return __batadv_store_uint_attr(buff, count, _min, _max,	\
					_post_func, attr,		\
					&bat_priv->_name, net_dev);	\
105 106
}

107
#define BAT_ATTR_SIF_SHOW_UINT(_name)					\
108 109
ssize_t batadv_show_##_name(struct kobject *kobj,			\
			    struct attribute *attr, char *buff)		\
110
{									\
111
	struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);	\
112 113 114
	return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name));	\
}									\

115
/* Use this, if you are going to set [name] in the soft-interface
116 117
 * (bat_priv) to an unsigned integer value
 */
118 119 120
#define BAT_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func)		\
	static BAT_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)	\
	static BAT_ATTR_SIF_SHOW_UINT(_name)				\
121 122
	static BAT_ATTR(_name, _mode, batadv_show_##_name,		\
			batadv_store_##_name)
123 124


125
#define BAT_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func)		\
126 127 128
ssize_t batadv_store_##_name(struct kobject *kobj,			\
			     struct attribute *attr, char *buff,	\
			     size_t count)				\
129
{									\
130
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);	\
131
	struct hard_iface *hard_iface;					\
132 133
	ssize_t length;							\
									\
134
	hard_iface = batadv_hardif_get_by_netdev(net_dev);		\
135 136 137
	if (!hard_iface)						\
		return 0;						\
									\
138 139 140
	length = __batadv_store_uint_attr(buff, count, _min, _max,	\
					  _post_func, attr,		\
					  &hard_iface->_name, net_dev);	\
141
									\
142
	batadv_hardif_free_ref(hard_iface);				\
143 144 145 146
	return length;							\
}

#define BAT_ATTR_HIF_SHOW_UINT(_name)					\
147 148
ssize_t batadv_show_##_name(struct kobject *kobj,			\
			    struct attribute *attr, char *buff)		\
149
{									\
150
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);	\
151
	struct hard_iface *hard_iface;					\
152 153
	ssize_t length;							\
									\
154
	hard_iface = batadv_hardif_get_by_netdev(net_dev);		\
155 156 157 158 159
	if (!hard_iface)						\
		return 0;						\
									\
	length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_name));\
									\
160
	batadv_hardif_free_ref(hard_iface);				\
161 162 163 164
	return length;							\
}

/* Use this, if you are going to set [name] in hard_iface to an
165 166
 * unsigned integer value
 */
167 168 169
#define BAT_ATTR_HIF_UINT(_name, _mode, _min, _max, _post_func)		\
	static BAT_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func)	\
	static BAT_ATTR_HIF_SHOW_UINT(_name)				\
170 171
	static BAT_ATTR(_name, _mode, batadv_show_##_name,		\
			batadv_store_##_name)
172 173


174 175 176
static int batadv_store_bool_attr(char *buff, size_t count,
				  struct net_device *net_dev,
				  const char *attr_name, atomic_t *attr)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
{
	int enabled = -1;

	if (buff[count - 1] == '\n')
		buff[count - 1] = '\0';

	if ((strncmp(buff, "1", 2) == 0) ||
	    (strncmp(buff, "enable", 7) == 0) ||
	    (strncmp(buff, "enabled", 8) == 0))
		enabled = 1;

	if ((strncmp(buff, "0", 2) == 0) ||
	    (strncmp(buff, "disable", 8) == 0) ||
	    (strncmp(buff, "disabled", 9) == 0))
		enabled = 0;

	if (enabled < 0) {
194 195
		batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
			    attr_name, buff);
196 197 198 199 200 201
		return -EINVAL;
	}

	if (atomic_read(attr) == enabled)
		return count;

202 203 204
	batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
		    atomic_read(attr) == 1 ? "enabled" : "disabled",
		    enabled == 1 ? "enabled" : "disabled");
205

206
	atomic_set(attr, (unsigned int)enabled);
207 208 209
	return count;
}

210 211 212 213 214
static inline ssize_t
__batadv_store_bool_attr(char *buff, size_t count,
			 void (*post_func)(struct net_device *),
			 struct attribute *attr,
			 atomic_t *attr_store, struct net_device *net_dev)
215 216 217
{
	int ret;

218 219
	ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
				     attr_store);
220 221 222 223 224 225
	if (post_func && ret)
		post_func(net_dev);

	return ret;
}

226 227 228 229 230
static int batadv_store_uint_attr(const char *buff, size_t count,
				  struct net_device *net_dev,
				  const char *attr_name,
				  unsigned int min, unsigned int max,
				  atomic_t *attr)
231 232 233 234
{
	unsigned long uint_val;
	int ret;

235
	ret = kstrtoul(buff, 10, &uint_val);
236
	if (ret) {
237 238
		batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
			    attr_name, buff);
239 240 241 242
		return -EINVAL;
	}

	if (uint_val < min) {
243 244
		batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
			    attr_name, uint_val, min);
245 246 247 248
		return -EINVAL;
	}

	if (uint_val > max) {
249 250
		batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
			    attr_name, uint_val, max);
251 252 253 254 255 256
		return -EINVAL;
	}

	if (atomic_read(attr) == uint_val)
		return count;

257 258
	batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
		    attr_name, atomic_read(attr), uint_val);
259 260 261 262 263

	atomic_set(attr, uint_val);
	return count;
}

264 265 266 267 268 269
static inline ssize_t
__batadv_store_uint_attr(const char *buff, size_t count,
			 int min, int max,
			 void (*post_func)(struct net_device *),
			 const struct attribute *attr,
			 atomic_t *attr_store, struct net_device *net_dev)
270 271 272
{
	int ret;

273 274
	ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
				     attr_store);
275 276 277 278 279 280
	if (post_func && ret)
		post_func(net_dev);

	return ret;
}

281 282
static ssize_t batadv_show_vis_mode(struct kobject *kobj,
				    struct attribute *attr, char *buff)
283
{
284
	struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
285 286 287 288 289 290 291
	int vis_mode = atomic_read(&bat_priv->vis_mode);

	return sprintf(buff, "%s\n",
		       vis_mode == VIS_TYPE_CLIENT_UPDATE ?
							"client" : "server");
}

292 293 294
static ssize_t batadv_store_vis_mode(struct kobject *kobj,
				     struct attribute *attr, char *buff,
				     size_t count)
295
{
296
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
297 298 299
	struct bat_priv *bat_priv = netdev_priv(net_dev);
	unsigned long val;
	int ret, vis_mode_tmp = -1;
300
	const char *old_mode, *new_mode;
301

302
	ret = kstrtoul(buff, 10, &val);
303 304 305 306 307 308 309 310 311 312 313 314 315 316

	if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
	    (strncmp(buff, "client", 6) == 0) ||
	    (strncmp(buff, "off", 3) == 0))
		vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;

	if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
	    (strncmp(buff, "server", 6) == 0))
		vis_mode_tmp = VIS_TYPE_SERVER_SYNC;

	if (vis_mode_tmp < 0) {
		if (buff[count - 1] == '\n')
			buff[count - 1] = '\0';

317 318 319
		batadv_info(net_dev,
			    "Invalid parameter for 'vis mode' setting received: %s\n",
			    buff);
320 321 322 323 324 325
		return -EINVAL;
	}

	if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
		return count;

326 327 328 329 330 331 332 333 334 335 336 337
	if (atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE)
		old_mode =  "client";
	else
		old_mode = "server";

	if (vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE)
		new_mode =  "client";
	else
		new_mode = "server";

	batadv_info(net_dev, "Changing vis mode from: %s to: %s\n", old_mode,
		    new_mode);
338

339
	atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
340 341 342
	return count;
}

343 344
static ssize_t batadv_show_bat_algo(struct kobject *kobj,
				    struct attribute *attr, char *buff)
345
{
346
	struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
347 348 349
	return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
}

350
static void batadv_post_gw_deselect(struct net_device *net_dev)
351 352
{
	struct bat_priv *bat_priv = netdev_priv(net_dev);
353
	batadv_gw_deselect(bat_priv);
354 355
}

356 357
static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
				   char *buff)
358
{
359
	struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
	int bytes_written;

	switch (atomic_read(&bat_priv->gw_mode)) {
	case GW_MODE_CLIENT:
		bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
		break;
	case GW_MODE_SERVER:
		bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
		break;
	default:
		bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
		break;
	}

	return bytes_written;
}

377 378 379
static ssize_t batadv_store_gw_mode(struct kobject *kobj,
				    struct attribute *attr, char *buff,
				    size_t count)
380
{
381
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
382 383 384 385 386 387 388 389 390 391 392
	struct bat_priv *bat_priv = netdev_priv(net_dev);
	char *curr_gw_mode_str;
	int gw_mode_tmp = -1;

	if (buff[count - 1] == '\n')
		buff[count - 1] = '\0';

	if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
		gw_mode_tmp = GW_MODE_OFF;

	if (strncmp(buff, GW_MODE_CLIENT_NAME,
393
		    strlen(GW_MODE_CLIENT_NAME)) == 0)
394 395 396
		gw_mode_tmp = GW_MODE_CLIENT;

	if (strncmp(buff, GW_MODE_SERVER_NAME,
397
		    strlen(GW_MODE_SERVER_NAME)) == 0)
398 399 400
		gw_mode_tmp = GW_MODE_SERVER;

	if (gw_mode_tmp < 0) {
401 402 403
		batadv_info(net_dev,
			    "Invalid parameter for 'gw mode' setting received: %s\n",
			    buff);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
		return -EINVAL;
	}

	if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
		return count;

	switch (atomic_read(&bat_priv->gw_mode)) {
	case GW_MODE_CLIENT:
		curr_gw_mode_str = GW_MODE_CLIENT_NAME;
		break;
	case GW_MODE_SERVER:
		curr_gw_mode_str = GW_MODE_SERVER_NAME;
		break;
	default:
		curr_gw_mode_str = GW_MODE_OFF_NAME;
		break;
	}

422 423
	batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
		    curr_gw_mode_str, buff);
424

425
	batadv_gw_deselect(bat_priv);
426
	atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
427 428 429
	return count;
}

430 431
static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
				     struct attribute *attr, char *buff)
432
{
433
	struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
434 435 436
	int down, up;
	int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);

437
	batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
438 439 440 441 442 443 444
	return sprintf(buff, "%i%s/%i%s\n",
		       (down > 2048 ? down / 1024 : down),
		       (down > 2048 ? "MBit" : "KBit"),
		       (up > 2048 ? up / 1024 : up),
		       (up > 2048 ? "MBit" : "KBit"));
}

445 446 447
static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
				      struct attribute *attr, char *buff,
				      size_t count)
448
{
449
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
450 451 452 453

	if (buff[count - 1] == '\n')
		buff[count - 1] = '\0';

454
	return batadv_gw_bandwidth_set(net_dev, buff, count);
455 456
}

457 458
BAT_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
BAT_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
459
#ifdef CONFIG_BATMAN_ADV_BLA
460
BAT_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
461
#endif
462
BAT_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
463
BAT_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
464 465 466 467 468
static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
		batadv_store_vis_mode);
static BAT_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
		batadv_store_gw_mode);
469 470 471
BAT_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
BAT_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
BAT_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
472 473 474
		  batadv_post_gw_deselect);
static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
		batadv_store_gw_bwidth);
475
#ifdef CONFIG_BATMAN_ADV_DEBUG
476
BAT_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL);
477 478
#endif

479 480 481
static struct bat_attribute *batadv_mesh_attrs[] = {
	&batadv_attr_aggregated_ogms,
	&batadv_attr_bonding,
482
#ifdef CONFIG_BATMAN_ADV_BLA
483
	&batadv_attr_bridge_loop_avoidance,
484
#endif
485 486 487 488 489 490 491 492 493
	&batadv_attr_fragmentation,
	&batadv_attr_ap_isolation,
	&batadv_attr_vis_mode,
	&batadv_attr_routing_algo,
	&batadv_attr_gw_mode,
	&batadv_attr_orig_interval,
	&batadv_attr_hop_penalty,
	&batadv_attr_gw_sel_class,
	&batadv_attr_gw_bandwidth,
494
#ifdef CONFIG_BATMAN_ADV_DEBUG
495
	&batadv_attr_log_level,
496 497 498 499
#endif
	NULL,
};

500
int batadv_sysfs_add_meshif(struct net_device *dev)
501 502 503 504 505 506 507 508 509
{
	struct kobject *batif_kobject = &dev->dev.kobj;
	struct bat_priv *bat_priv = netdev_priv(dev);
	struct bat_attribute **bat_attr;
	int err;

	bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
						    batif_kobject);
	if (!bat_priv->mesh_obj) {
510 511
		batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
			   SYSFS_IF_MESH_SUBDIR);
512 513 514
		goto out;
	}

515
	for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
516 517 518
		err = sysfs_create_file(bat_priv->mesh_obj,
					&((*bat_attr)->attr));
		if (err) {
519 520 521
			batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
				   dev->name, SYSFS_IF_MESH_SUBDIR,
				   ((*bat_attr)->attr).name);
522 523 524 525 526 527 528
			goto rem_attr;
		}
	}

	return 0;

rem_attr:
529
	for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
530 531 532 533 534 535 536 537
		sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));

	kobject_put(bat_priv->mesh_obj);
	bat_priv->mesh_obj = NULL;
out:
	return -ENOMEM;
}

538
void batadv_sysfs_del_meshif(struct net_device *dev)
539 540 541 542
{
	struct bat_priv *bat_priv = netdev_priv(dev);
	struct bat_attribute **bat_attr;

543
	for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
544 545 546 547 548 549
		sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));

	kobject_put(bat_priv->mesh_obj);
	bat_priv->mesh_obj = NULL;
}

550 551
static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
				      struct attribute *attr, char *buff)
552
{
553
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
554
	struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
555 556
	ssize_t length;

557
	if (!hard_iface)
558 559
		return 0;

560 561
	length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
			 "none" : hard_iface->soft_iface->name);
562

563
	batadv_hardif_free_ref(hard_iface);
564 565 566 567

	return length;
}

568 569 570
static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
				       struct attribute *attr, char *buff,
				       size_t count)
571
{
572
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
573
	struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
574
	int status_tmp = -1;
575
	int ret = count;
576

577
	if (!hard_iface)
578 579 580 581 582 583
		return count;

	if (buff[count - 1] == '\n')
		buff[count - 1] = '\0';

	if (strlen(buff) >= IFNAMSIZ) {
584 585
		pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
		       buff);
586
		batadv_hardif_free_ref(hard_iface);
587 588 589 590 591 592 593 594
		return -EINVAL;
	}

	if (strncmp(buff, "none", 4) == 0)
		status_tmp = IF_NOT_IN_USE;
	else
		status_tmp = IF_I_WANT_YOU;

595 596 597 598 599
	if (hard_iface->if_status == status_tmp)
		goto out;

	if ((hard_iface->soft_iface) &&
	    (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
600
		goto out;
601

602 603 604 605 606
	if (!rtnl_trylock()) {
		ret = -ERESTARTSYS;
		goto out;
	}

607
	if (status_tmp == IF_NOT_IN_USE) {
608
		batadv_hardif_disable_interface(hard_iface);
609
		goto unlock;
610 611 612
	}

	/* if the interface already is in use */
613
	if (hard_iface->if_status != IF_NOT_IN_USE)
614
		batadv_hardif_disable_interface(hard_iface);
615

616
	ret = batadv_hardif_enable_interface(hard_iface, buff);
617

618 619
unlock:
	rtnl_unlock();
620
out:
621
	batadv_hardif_free_ref(hard_iface);
622 623 624
	return ret;
}

625 626
static ssize_t batadv_show_iface_status(struct kobject *kobj,
					struct attribute *attr, char *buff)
627
{
628
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
629
	struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
630 631
	ssize_t length;

632
	if (!hard_iface)
633 634
		return 0;

635
	switch (hard_iface->if_status) {
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
	case IF_TO_BE_REMOVED:
		length = sprintf(buff, "disabling\n");
		break;
	case IF_INACTIVE:
		length = sprintf(buff, "inactive\n");
		break;
	case IF_ACTIVE:
		length = sprintf(buff, "active\n");
		break;
	case IF_TO_BE_ACTIVATED:
		length = sprintf(buff, "enabling\n");
		break;
	case IF_NOT_IN_USE:
	default:
		length = sprintf(buff, "not in use\n");
		break;
	}

654
	batadv_hardif_free_ref(hard_iface);
655 656 657 658 659

	return length;
}

static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
660 661
		batadv_show_mesh_iface, batadv_store_mesh_iface);
static BAT_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
662

663 664 665
static struct bat_attribute *batadv_batman_attrs[] = {
	&batadv_attr_mesh_iface,
	&batadv_attr_iface_status,
666 667 668
	NULL,
};

669
int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
670 671 672 673 674 675 676 677 678
{
	struct kobject *hardif_kobject = &dev->dev.kobj;
	struct bat_attribute **bat_attr;
	int err;

	*hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
						    hardif_kobject);

	if (!*hardif_obj) {
679 680
		batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
			   SYSFS_IF_BAT_SUBDIR);
681 682 683
		goto out;
	}

684
	for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
685 686
		err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
		if (err) {
687 688 689
			batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
				   dev->name, SYSFS_IF_BAT_SUBDIR,
				   ((*bat_attr)->attr).name);
690 691 692 693 694 695 696
			goto rem_attr;
		}
	}

	return 0;

rem_attr:
697
	for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
698 699 700 701 702
		sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
out:
	return -ENOMEM;
}

703
void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
704 705 706 707
{
	kobject_put(*hardif_obj);
	*hardif_obj = NULL;
}
708

709 710
int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
			enum uev_action action, const char *data)
711
{
712
	int ret = -ENOMEM;
713 714 715 716
	struct hard_iface *primary_if = NULL;
	struct kobject *bat_kobj;
	char *uevent_env[4] = { NULL, NULL, NULL, NULL };

717
	primary_if = batadv_primary_if_get_selected(bat_priv);
718 719 720 721 722 723
	if (!primary_if)
		goto out;

	bat_kobj = &primary_if->soft_iface->dev.kobj;

	uevent_env[0] = kmalloc(strlen(UEV_TYPE_VAR) +
724
				strlen(batadv_uev_type_str[type]) + 1,
725 726 727 728
				GFP_ATOMIC);
	if (!uevent_env[0])
		goto out;

729
	sprintf(uevent_env[0], "%s%s", UEV_TYPE_VAR, batadv_uev_type_str[type]);
730 731

	uevent_env[1] = kmalloc(strlen(UEV_ACTION_VAR) +
732
				strlen(batadv_uev_action_str[action]) + 1,
733 734 735 736
				GFP_ATOMIC);
	if (!uevent_env[1])
		goto out;

737 738
	sprintf(uevent_env[1], "%s%s", UEV_ACTION_VAR,
		batadv_uev_action_str[action]);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756

	/* If the event is DEL, ignore the data field */
	if (action != UEV_DEL) {
		uevent_env[2] = kmalloc(strlen(UEV_DATA_VAR) +
					strlen(data) + 1, GFP_ATOMIC);
		if (!uevent_env[2])
			goto out;

		sprintf(uevent_env[2], "%s%s", UEV_DATA_VAR, data);
	}

	ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
out:
	kfree(uevent_env[0]);
	kfree(uevent_env[1]);
	kfree(uevent_env[2]);

	if (primary_if)
757
		batadv_hardif_free_ref(primary_if);
758 759

	if (ret)
760 761
		batadv_dbg(DBG_BATMAN, bat_priv,
			   "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
762 763
			   batadv_uev_type_str[type],
			   batadv_uev_action_str[action],
764
			   (action == UEV_DEL ? "NULL" : data), ret);
765 766
	return ret;
}