sysfs.c 19.7 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
 *
 * 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"
21
#include "sysfs.h"
22 23 24 25 26 27 28
#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 batadv_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
#define BATADV_UEV_TYPE_VAR	"BATTYPE="
#define BATADV_UEV_ACTION_VAR	"BATACTION="
#define BATADV_UEV_DATA_VAR	"BATDATA="
44

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
/* Use this, if you have customized show and store functions */
56
#define BATADV_ATTR(_name, _mode, _show, _store)	\
57
struct batadv_attribute batadv_attr_##_name = {		\
58 59 60 61
	.attr = {.name = __stringify(_name),		\
		 .mode = _mode },			\
	.show   = _show,				\
	.store  = _store,				\
62 63
};

64
#define BATADV_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 batadv_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 BATADV_ATTR_SIF_SHOW_BOOL(_name)				\
76 77
ssize_t batadv_show_##_name(struct kobject *kobj,			\
			    struct attribute *attr, char *buff)		\
78
{									\
79
	struct batadv_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 91 92
#define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func)			\
	static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func)		\
	static BATADV_ATTR_SIF_SHOW_BOOL(_name)				\
	static BATADV_ATTR(_name, _mode, batadv_show_##_name,		\
			   batadv_store_##_name)
93 94


95
#define BATADV_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 batadv_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 BATADV_ATTR_SIF_SHOW_UINT(_name)				\
108 109
ssize_t batadv_show_##_name(struct kobject *kobj,			\
			    struct attribute *attr, char *buff)		\
110
{									\
111
	struct batadv_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 121 122
#define BATADV_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func)	\
	static BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)\
	static BATADV_ATTR_SIF_SHOW_UINT(_name)				\
	static BATADV_ATTR(_name, _mode, batadv_show_##_name,		\
			   batadv_store_##_name)
123 124


125 126 127
static int batadv_store_bool_attr(char *buff, size_t count,
				  struct net_device *net_dev,
				  const char *attr_name, atomic_t *attr)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
{
	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) {
145 146
		batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
			    attr_name, buff);
147 148 149 150 151 152
		return -EINVAL;
	}

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

153 154 155
	batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
		    atomic_read(attr) == 1 ? "enabled" : "disabled",
		    enabled == 1 ? "enabled" : "disabled");
156

157
	atomic_set(attr, (unsigned int)enabled);
158 159 160
	return count;
}

161 162 163 164 165
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)
166 167 168
{
	int ret;

169 170
	ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
				     attr_store);
171 172 173 174 175 176
	if (post_func && ret)
		post_func(net_dev);

	return ret;
}

177 178 179 180 181
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)
182 183 184 185
{
	unsigned long uint_val;
	int ret;

186
	ret = kstrtoul(buff, 10, &uint_val);
187
	if (ret) {
188 189
		batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
			    attr_name, buff);
190 191 192 193
		return -EINVAL;
	}

	if (uint_val < min) {
194 195
		batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
			    attr_name, uint_val, min);
196 197 198 199
		return -EINVAL;
	}

	if (uint_val > max) {
200 201
		batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
			    attr_name, uint_val, max);
202 203 204 205 206 207
		return -EINVAL;
	}

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

208 209
	batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
		    attr_name, atomic_read(attr), uint_val);
210 211 212 213 214

	atomic_set(attr, uint_val);
	return count;
}

215 216 217 218 219 220
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)
221 222 223
{
	int ret;

224 225
	ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
				     attr_store);
226 227 228 229 230 231
	if (post_func && ret)
		post_func(net_dev);

	return ret;
}

232 233
static ssize_t batadv_show_vis_mode(struct kobject *kobj,
				    struct attribute *attr, char *buff)
234
{
235
	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
236
	int vis_mode = atomic_read(&bat_priv->vis_mode);
237
	const char *mode;
238

239 240 241 242 243 244
	if (vis_mode == BATADV_VIS_TYPE_CLIENT_UPDATE)
		mode = "client";
	else
		mode = "server";

	return sprintf(buff, "%s\n", mode);
245 246
}

247 248 249
static ssize_t batadv_store_vis_mode(struct kobject *kobj,
				     struct attribute *attr, char *buff,
				     size_t count)
250
{
251
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
252
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
253 254
	unsigned long val;
	int ret, vis_mode_tmp = -1;
255
	const char *old_mode, *new_mode;
256

257
	ret = kstrtoul(buff, 10, &val);
258

259 260
	if (((count == 2) && (!ret) &&
	     (val == BATADV_VIS_TYPE_CLIENT_UPDATE)) ||
261 262
	    (strncmp(buff, "client", 6) == 0) ||
	    (strncmp(buff, "off", 3) == 0))
263
		vis_mode_tmp = BATADV_VIS_TYPE_CLIENT_UPDATE;
264

265 266
	if (((count == 2) && (!ret) &&
	     (val == BATADV_VIS_TYPE_SERVER_SYNC)) ||
267
	    (strncmp(buff, "server", 6) == 0))
268
		vis_mode_tmp = BATADV_VIS_TYPE_SERVER_SYNC;
269 270 271 272 273

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

274 275 276
		batadv_info(net_dev,
			    "Invalid parameter for 'vis mode' setting received: %s\n",
			    buff);
277 278 279 280 281 282
		return -EINVAL;
	}

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

283
	if (atomic_read(&bat_priv->vis_mode) == BATADV_VIS_TYPE_CLIENT_UPDATE)
284 285 286 287
		old_mode =  "client";
	else
		old_mode = "server";

288
	if (vis_mode_tmp == BATADV_VIS_TYPE_CLIENT_UPDATE)
289 290 291 292 293 294
		new_mode =  "client";
	else
		new_mode = "server";

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

296
	atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
297 298 299
	return count;
}

300 301
static ssize_t batadv_show_bat_algo(struct kobject *kobj,
				    struct attribute *attr, char *buff)
302
{
303
	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
304 305 306
	return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
}

307
static void batadv_post_gw_deselect(struct net_device *net_dev)
308
{
309
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
310
	batadv_gw_deselect(bat_priv);
311 312
}

313 314
static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
				   char *buff)
315
{
316
	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
317 318 319
	int bytes_written;

	switch (atomic_read(&bat_priv->gw_mode)) {
320
	case BATADV_GW_MODE_CLIENT:
321 322
		bytes_written = sprintf(buff, "%s\n",
					BATADV_GW_MODE_CLIENT_NAME);
323
		break;
324
	case BATADV_GW_MODE_SERVER:
325 326
		bytes_written = sprintf(buff, "%s\n",
					BATADV_GW_MODE_SERVER_NAME);
327 328
		break;
	default:
329 330
		bytes_written = sprintf(buff, "%s\n",
					BATADV_GW_MODE_OFF_NAME);
331 332 333 334 335 336
		break;
	}

	return bytes_written;
}

337 338 339
static ssize_t batadv_store_gw_mode(struct kobject *kobj,
				    struct attribute *attr, char *buff,
				    size_t count)
340
{
341
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
342
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
343 344 345 346 347 348
	char *curr_gw_mode_str;
	int gw_mode_tmp = -1;

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

349 350
	if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
		    strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
351
		gw_mode_tmp = BATADV_GW_MODE_OFF;
352

353 354
	if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
		    strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
355
		gw_mode_tmp = BATADV_GW_MODE_CLIENT;
356

357 358
	if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
		    strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
359
		gw_mode_tmp = BATADV_GW_MODE_SERVER;
360 361

	if (gw_mode_tmp < 0) {
362 363 364
		batadv_info(net_dev,
			    "Invalid parameter for 'gw mode' setting received: %s\n",
			    buff);
365 366 367 368 369 370 371
		return -EINVAL;
	}

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

	switch (atomic_read(&bat_priv->gw_mode)) {
372
	case BATADV_GW_MODE_CLIENT:
373
		curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
374
		break;
375
	case BATADV_GW_MODE_SERVER:
376
		curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
377 378
		break;
	default:
379
		curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
380 381 382
		break;
	}

383 384
	batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
		    curr_gw_mode_str, buff);
385

386
	batadv_gw_deselect(bat_priv);
387
	atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
388 389 390
	return count;
}

391 392
static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
				     struct attribute *attr, char *buff)
393
{
394
	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
395 396 397
	int down, up;
	int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);

398
	batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
399 400 401 402 403 404 405
	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"));
}

406 407 408
static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
				      struct attribute *attr, char *buff,
				      size_t count)
409
{
410
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
411 412 413 414

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

415
	return batadv_gw_bandwidth_set(net_dev, buff, count);
416 417
}

418 419
BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
420
#ifdef CONFIG_BATMAN_ADV_BLA
421
BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
422
#endif
423 424 425 426 427 428 429
BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
		   batadv_store_vis_mode);
static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
		   batadv_store_gw_mode);
430 431 432
BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
		     INT_MAX, NULL);
BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
433
		     NULL);
434
BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
435 436 437
		     batadv_post_gw_deselect);
static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
		   batadv_store_gw_bwidth);
438
#ifdef CONFIG_BATMAN_ADV_DEBUG
439
BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
440 441
#endif

442
static struct batadv_attribute *batadv_mesh_attrs[] = {
443 444
	&batadv_attr_aggregated_ogms,
	&batadv_attr_bonding,
445
#ifdef CONFIG_BATMAN_ADV_BLA
446
	&batadv_attr_bridge_loop_avoidance,
447
#endif
448 449 450 451 452 453 454 455 456
	&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,
457
#ifdef CONFIG_BATMAN_ADV_DEBUG
458
	&batadv_attr_log_level,
459 460 461 462
#endif
	NULL,
};

463
int batadv_sysfs_add_meshif(struct net_device *dev)
464 465
{
	struct kobject *batif_kobject = &dev->dev.kobj;
466
	struct batadv_priv *bat_priv = netdev_priv(dev);
467
	struct batadv_attribute **bat_attr;
468 469
	int err;

470
	bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
471 472
						    batif_kobject);
	if (!bat_priv->mesh_obj) {
473
		batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
474
			   BATADV_SYSFS_IF_MESH_SUBDIR);
475 476 477
		goto out;
	}

478
	for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
479 480 481
		err = sysfs_create_file(bat_priv->mesh_obj,
					&((*bat_attr)->attr));
		if (err) {
482
			batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
483
				   dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
484
				   ((*bat_attr)->attr).name);
485 486 487 488 489 490 491
			goto rem_attr;
		}
	}

	return 0;

rem_attr:
492
	for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
493 494 495 496 497 498 499 500
		sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));

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

501
void batadv_sysfs_del_meshif(struct net_device *dev)
502
{
503
	struct batadv_priv *bat_priv = netdev_priv(dev);
504
	struct batadv_attribute **bat_attr;
505

506
	for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
507 508 509 510 511 512
		sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));

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

513 514
static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
				      struct attribute *attr, char *buff)
515
{
516
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
517
	struct batadv_hard_iface *hard_iface;
518
	ssize_t length;
519
	const char *ifname;
520

521
	hard_iface = batadv_hardif_get_by_netdev(net_dev);
522
	if (!hard_iface)
523 524
		return 0;

525 526 527 528 529 530
	if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
		ifname =  "none";
	else
		ifname = hard_iface->soft_iface->name;

	length = sprintf(buff, "%s\n", ifname);
531

532
	batadv_hardif_free_ref(hard_iface);
533 534 535 536

	return length;
}

537 538 539
static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
				       struct attribute *attr, char *buff,
				       size_t count)
540
{
541
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
542
	struct batadv_hard_iface *hard_iface;
543
	int status_tmp = -1;
544
	int ret = count;
545

546
	hard_iface = batadv_hardif_get_by_netdev(net_dev);
547
	if (!hard_iface)
548 549 550 551 552 553
		return count;

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

	if (strlen(buff) >= IFNAMSIZ) {
554 555
		pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
		       buff);
556
		batadv_hardif_free_ref(hard_iface);
557 558 559 560
		return -EINVAL;
	}

	if (strncmp(buff, "none", 4) == 0)
561
		status_tmp = BATADV_IF_NOT_IN_USE;
562
	else
563
		status_tmp = BATADV_IF_I_WANT_YOU;
564

565 566 567 568 569
	if (hard_iface->if_status == status_tmp)
		goto out;

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

572 573 574 575 576
	if (!rtnl_trylock()) {
		ret = -ERESTARTSYS;
		goto out;
	}

577
	if (status_tmp == BATADV_IF_NOT_IN_USE) {
578
		batadv_hardif_disable_interface(hard_iface);
579
		goto unlock;
580 581 582
	}

	/* if the interface already is in use */
583
	if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
584
		batadv_hardif_disable_interface(hard_iface);
585

586
	ret = batadv_hardif_enable_interface(hard_iface, buff);
587

588 589
unlock:
	rtnl_unlock();
590
out:
591
	batadv_hardif_free_ref(hard_iface);
592 593 594
	return ret;
}

595 596
static ssize_t batadv_show_iface_status(struct kobject *kobj,
					struct attribute *attr, char *buff)
597
{
598
	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
599
	struct batadv_hard_iface *hard_iface;
600 601
	ssize_t length;

602
	hard_iface = batadv_hardif_get_by_netdev(net_dev);
603
	if (!hard_iface)
604 605
		return 0;

606
	switch (hard_iface->if_status) {
607
	case BATADV_IF_TO_BE_REMOVED:
608 609
		length = sprintf(buff, "disabling\n");
		break;
610
	case BATADV_IF_INACTIVE:
611 612
		length = sprintf(buff, "inactive\n");
		break;
613
	case BATADV_IF_ACTIVE:
614 615
		length = sprintf(buff, "active\n");
		break;
616
	case BATADV_IF_TO_BE_ACTIVATED:
617 618
		length = sprintf(buff, "enabling\n");
		break;
619
	case BATADV_IF_NOT_IN_USE:
620 621 622 623 624
	default:
		length = sprintf(buff, "not in use\n");
		break;
	}

625
	batadv_hardif_free_ref(hard_iface);
626 627 628 629

	return length;
}

630 631 632
static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
		   batadv_store_mesh_iface);
static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
633

634
static struct batadv_attribute *batadv_batman_attrs[] = {
635 636
	&batadv_attr_mesh_iface,
	&batadv_attr_iface_status,
637 638 639
	NULL,
};

640
int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
641 642
{
	struct kobject *hardif_kobject = &dev->dev.kobj;
643
	struct batadv_attribute **bat_attr;
644 645
	int err;

646 647
	*hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
					     hardif_kobject);
648 649

	if (!*hardif_obj) {
650
		batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
651
			   BATADV_SYSFS_IF_BAT_SUBDIR);
652 653 654
		goto out;
	}

655
	for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
656 657
		err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
		if (err) {
658
			batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
659
				   dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
660
				   ((*bat_attr)->attr).name);
661 662 663 664 665 666 667
			goto rem_attr;
		}
	}

	return 0;

rem_attr:
668
	for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
669 670 671 672 673
		sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
out:
	return -ENOMEM;
}

674
void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
675 676 677 678
{
	kobject_put(*hardif_obj);
	*hardif_obj = NULL;
}
679

680
int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
681
			enum batadv_uev_action action, const char *data)
682
{
683
	int ret = -ENOMEM;
684
	struct batadv_hard_iface *primary_if = NULL;
685 686 687
	struct kobject *bat_kobj;
	char *uevent_env[4] = { NULL, NULL, NULL, NULL };

688
	primary_if = batadv_primary_if_get_selected(bat_priv);
689 690 691 692 693
	if (!primary_if)
		goto out;

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

694
	uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
695
				strlen(batadv_uev_type_str[type]) + 1,
696 697 698 699
				GFP_ATOMIC);
	if (!uevent_env[0])
		goto out;

700 701
	sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
		batadv_uev_type_str[type]);
702

703
	uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
704
				strlen(batadv_uev_action_str[action]) + 1,
705 706 707 708
				GFP_ATOMIC);
	if (!uevent_env[1])
		goto out;

709
	sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
710
		batadv_uev_action_str[action]);
711 712

	/* If the event is DEL, ignore the data field */
713
	if (action != BATADV_UEV_DEL) {
714
		uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
715 716 717 718
					strlen(data) + 1, GFP_ATOMIC);
		if (!uevent_env[2])
			goto out;

719
		sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
720 721 722 723 724 725 726 727 728
	}

	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)
729
		batadv_hardif_free_ref(primary_if);
730 731

	if (ret)
732
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
733
			   "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
734 735
			   batadv_uev_type_str[type],
			   batadv_uev_action_str[action],
736
			   (action == BATADV_UEV_DEL ? "NULL" : data), ret);
737 738
	return ret;
}