br_sysfs_br.c 24.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *	Sysfs attributes of bridge
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13
 *	Linux ethernet bridge
 *
 *	Authors:
 *	Stephen Hemminger		<shemminger@osdl.org>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

14
#include <linux/capability.h>
L
Linus Torvalds 已提交
15 16
#include <linux/kernel.h>
#include <linux/netdevice.h>
17
#include <linux/etherdevice.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24
#include <linux/if_bridge.h>
#include <linux/rtnetlink.h>
#include <linux/spinlock.h>
#include <linux/times.h>

#include "br_private.h"

25
#define to_bridge(cd)	((struct net_bridge *)netdev_priv(to_net_dev(cd)))
L
Linus Torvalds 已提交
26 27 28 29

/*
 * Common code for storing bridge parameters.
 */
30
static ssize_t store_bridge_parm(struct device *d,
L
Linus Torvalds 已提交
31
				 const char *buf, size_t len,
32
				 int (*set)(struct net_bridge *, unsigned long))
L
Linus Torvalds 已提交
33
{
34
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
35 36
	char *endp;
	unsigned long val;
37
	int err;
L
Linus Torvalds 已提交
38

39
	if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
L
Linus Torvalds 已提交
40 41 42 43 44 45
		return -EPERM;

	val = simple_strtoul(buf, &endp, 0);
	if (endp == buf)
		return -EINVAL;

46 47 48
	if (!rtnl_trylock())
		return restart_syscall();

49
	err = (*set)(br, val);
50 51 52 53
	if (!err)
		netdev_state_change(br->dev);
	rtnl_unlock();

54
	return err ? err : len;
L
Linus Torvalds 已提交
55 56 57
}


58
static ssize_t forward_delay_show(struct device *d,
59
				  struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
60
{
61
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
62 63 64
	return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
}

65
static ssize_t forward_delay_store(struct device *d,
66 67
				   struct device_attribute *attr,
				   const char *buf, size_t len)
L
Linus Torvalds 已提交
68
{
69
	return store_bridge_parm(d, buf, len, br_set_forward_delay);
L
Linus Torvalds 已提交
70
}
71
static DEVICE_ATTR_RW(forward_delay);
L
Linus Torvalds 已提交
72

73
static ssize_t hello_time_show(struct device *d, struct device_attribute *attr,
74
			       char *buf)
L
Linus Torvalds 已提交
75 76
{
	return sprintf(buf, "%lu\n",
77
		       jiffies_to_clock_t(to_bridge(d)->hello_time));
L
Linus Torvalds 已提交
78 79
}

80
static ssize_t hello_time_store(struct device *d,
81
				struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
82 83
				size_t len)
{
84
	return store_bridge_parm(d, buf, len, br_set_hello_time);
L
Linus Torvalds 已提交
85
}
86
static DEVICE_ATTR_RW(hello_time);
L
Linus Torvalds 已提交
87

88
static ssize_t max_age_show(struct device *d, struct device_attribute *attr,
89
			    char *buf)
L
Linus Torvalds 已提交
90 91
{
	return sprintf(buf, "%lu\n",
92
		       jiffies_to_clock_t(to_bridge(d)->max_age));
L
Linus Torvalds 已提交
93 94
}

95
static ssize_t max_age_store(struct device *d, struct device_attribute *attr,
96
			     const char *buf, size_t len)
L
Linus Torvalds 已提交
97
{
98
	return store_bridge_parm(d, buf, len, br_set_max_age);
L
Linus Torvalds 已提交
99
}
100
static DEVICE_ATTR_RW(max_age);
L
Linus Torvalds 已提交
101

102
static ssize_t ageing_time_show(struct device *d,
103
				struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
104
{
105
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
106 107 108
	return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
}

109
static int set_ageing_time(struct net_bridge *br, unsigned long val)
L
Linus Torvalds 已提交
110
{
111
	return br_set_ageing_time(br, val);
L
Linus Torvalds 已提交
112 113
}

114
static ssize_t ageing_time_store(struct device *d,
115 116
				 struct device_attribute *attr,
				 const char *buf, size_t len)
L
Linus Torvalds 已提交
117
{
118
	return store_bridge_parm(d, buf, len, set_ageing_time);
L
Linus Torvalds 已提交
119
}
120
static DEVICE_ATTR_RW(ageing_time);
L
Linus Torvalds 已提交
121

122
static ssize_t stp_state_show(struct device *d,
123
			      struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
124
{
125
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
126 127 128 129
	return sprintf(buf, "%d\n", br->stp_enabled);
}


130
static int set_stp_state(struct net_bridge *br, unsigned long val)
L
Linus Torvalds 已提交
131
{
S
Stephen Hemminger 已提交
132 133
	br_stp_set_enabled(br, val);

134 135 136 137 138 139 140 141
	return 0;
}

static ssize_t stp_state_store(struct device *d,
			       struct device_attribute *attr, const char *buf,
			       size_t len)
{
	return store_bridge_parm(d, buf, len, set_stp_state);
L
Linus Torvalds 已提交
142
}
143
static DEVICE_ATTR_RW(stp_state);
L
Linus Torvalds 已提交
144

145 146 147
static ssize_t group_fwd_mask_show(struct device *d,
				   struct device_attribute *attr,
				   char *buf)
148 149 150 151 152
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%#x\n", br->group_fwd_mask);
}

153
static int set_group_fwd_mask(struct net_bridge *br, unsigned long val)
154 155 156 157 158 159
{
	if (val & BR_GROUPFWD_RESTRICTED)
		return -EINVAL;

	br->group_fwd_mask = val;

160 161 162 163 164 165 166 167 168
	return 0;
}

static ssize_t group_fwd_mask_store(struct device *d,
				    struct device_attribute *attr,
				    const char *buf,
				    size_t len)
{
	return store_bridge_parm(d, buf, len, set_group_fwd_mask);
169
}
170
static DEVICE_ATTR_RW(group_fwd_mask);
171

172
static ssize_t priority_show(struct device *d, struct device_attribute *attr,
173
			     char *buf)
L
Linus Torvalds 已提交
174
{
175
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
176 177 178 179
	return sprintf(buf, "%d\n",
		       (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
}

180
static int set_priority(struct net_bridge *br, unsigned long val)
L
Linus Torvalds 已提交
181 182
{
	br_stp_set_bridge_priority(br, (u16) val);
183
	return 0;
L
Linus Torvalds 已提交
184 185
}

186 187
static ssize_t priority_store(struct device *d, struct device_attribute *attr,
			      const char *buf, size_t len)
L
Linus Torvalds 已提交
188
{
189
	return store_bridge_parm(d, buf, len, set_priority);
L
Linus Torvalds 已提交
190
}
191
static DEVICE_ATTR_RW(priority);
L
Linus Torvalds 已提交
192

193
static ssize_t root_id_show(struct device *d, struct device_attribute *attr,
194
			    char *buf)
L
Linus Torvalds 已提交
195
{
196
	return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
L
Linus Torvalds 已提交
197
}
198
static DEVICE_ATTR_RO(root_id);
L
Linus Torvalds 已提交
199

200
static ssize_t bridge_id_show(struct device *d, struct device_attribute *attr,
201
			      char *buf)
L
Linus Torvalds 已提交
202
{
203
	return br_show_bridge_id(buf, &to_bridge(d)->bridge_id);
L
Linus Torvalds 已提交
204
}
205
static DEVICE_ATTR_RO(bridge_id);
L
Linus Torvalds 已提交
206

207
static ssize_t root_port_show(struct device *d, struct device_attribute *attr,
208
			      char *buf)
L
Linus Torvalds 已提交
209
{
210
	return sprintf(buf, "%d\n", to_bridge(d)->root_port);
L
Linus Torvalds 已提交
211
}
212
static DEVICE_ATTR_RO(root_port);
L
Linus Torvalds 已提交
213

214
static ssize_t root_path_cost_show(struct device *d,
215
				   struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
216
{
217
	return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
L
Linus Torvalds 已提交
218
}
219
static DEVICE_ATTR_RO(root_path_cost);
L
Linus Torvalds 已提交
220

221
static ssize_t topology_change_show(struct device *d,
222
				    struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
223
{
224
	return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
L
Linus Torvalds 已提交
225
}
226
static DEVICE_ATTR_RO(topology_change);
L
Linus Torvalds 已提交
227

228
static ssize_t topology_change_detected_show(struct device *d,
229 230
					     struct device_attribute *attr,
					     char *buf)
L
Linus Torvalds 已提交
231
{
232
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
233 234
	return sprintf(buf, "%d\n", br->topology_change_detected);
}
235
static DEVICE_ATTR_RO(topology_change_detected);
L
Linus Torvalds 已提交
236

237
static ssize_t hello_timer_show(struct device *d,
238
				struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
239
{
240
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
241 242
	return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
}
243
static DEVICE_ATTR_RO(hello_timer);
L
Linus Torvalds 已提交
244

245
static ssize_t tcn_timer_show(struct device *d, struct device_attribute *attr,
246
			      char *buf)
L
Linus Torvalds 已提交
247
{
248
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
249 250
	return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
}
251
static DEVICE_ATTR_RO(tcn_timer);
L
Linus Torvalds 已提交
252

253
static ssize_t topology_change_timer_show(struct device *d,
254 255
					  struct device_attribute *attr,
					  char *buf)
L
Linus Torvalds 已提交
256
{
257
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
258 259
	return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
}
260
static DEVICE_ATTR_RO(topology_change_timer);
L
Linus Torvalds 已提交
261

262
static ssize_t gc_timer_show(struct device *d, struct device_attribute *attr,
263
			     char *buf)
L
Linus Torvalds 已提交
264
{
265
	struct net_bridge *br = to_bridge(d);
L
Linus Torvalds 已提交
266 267
	return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
}
268
static DEVICE_ATTR_RO(gc_timer);
L
Linus Torvalds 已提交
269

270
static ssize_t group_addr_show(struct device *d,
271
			       struct device_attribute *attr, char *buf)
272
{
273
	struct net_bridge *br = to_bridge(d);
274 275 276 277 278 279
	return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
		       br->group_addr[0], br->group_addr[1],
		       br->group_addr[2], br->group_addr[3],
		       br->group_addr[4], br->group_addr[5]);
}

280
static ssize_t group_addr_store(struct device *d,
281 282
				struct device_attribute *attr,
				const char *buf, size_t len)
283
{
284
	struct net_bridge *br = to_bridge(d);
285
	u8 new_addr[6];
286 287
	int i;

288
	if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
289 290
		return -EPERM;

291
	if (sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
292 293 294 295
		   &new_addr[0], &new_addr[1], &new_addr[2],
		   &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
		return -EINVAL;

296
	if (!is_link_local_ether_addr(new_addr))
297 298
		return -EINVAL;

299 300 301
	if (new_addr[5] == 1 ||		/* 802.3x Pause address */
	    new_addr[5] == 2 ||		/* 802.3ad Slow protocols */
	    new_addr[5] == 3)		/* 802.1X PAE address */
302 303
		return -EINVAL;

304 305 306
	if (!rtnl_trylock())
		return restart_syscall();

307 308 309 310
	spin_lock_bh(&br->lock);
	for (i = 0; i < 6; i++)
		br->group_addr[i] = new_addr[i];
	spin_unlock_bh(&br->lock);
311 312 313

	br->group_addr_set = true;
	br_recalculate_fwd_mask(br);
314
	netdev_state_change(br->dev);
315 316 317

	rtnl_unlock();

318 319 320
	return len;
}

321
static DEVICE_ATTR_RW(group_addr);
322

323 324 325 326 327 328
static int set_flush(struct net_bridge *br, unsigned long val)
{
	br_fdb_flush(br);
	return 0;
}

329
static ssize_t flush_store(struct device *d,
330 331 332
			   struct device_attribute *attr,
			   const char *buf, size_t len)
{
333
	return store_bridge_parm(d, buf, len, set_flush);
334
}
335
static DEVICE_ATTR_WO(flush);
336

337
#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
338
static ssize_t multicast_router_show(struct device *d,
339 340 341 342 343 344
				     struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%d\n", br->multicast_router);
}

345
static ssize_t multicast_router_store(struct device *d,
346 347 348 349 350
				      struct device_attribute *attr,
				      const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, br_multicast_set_router);
}
351
static DEVICE_ATTR_RW(multicast_router);
352

353
static ssize_t multicast_snooping_show(struct device *d,
354 355 356 357 358 359 360
				       struct device_attribute *attr,
				       char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%d\n", !br->multicast_disabled);
}

361
static ssize_t multicast_snooping_store(struct device *d,
362 363 364 365 366
					struct device_attribute *attr,
					const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, br_multicast_toggle);
}
367
static DEVICE_ATTR_RW(multicast_snooping);
368

369 370 371
static ssize_t multicast_query_use_ifaddr_show(struct device *d,
					       struct device_attribute *attr,
					       char *buf)
372 373 374 375 376 377 378 379 380 381 382 383
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%d\n", br->multicast_query_use_ifaddr);
}

static int set_query_use_ifaddr(struct net_bridge *br, unsigned long val)
{
	br->multicast_query_use_ifaddr = !!val;
	return 0;
}

static ssize_t
384
multicast_query_use_ifaddr_store(struct device *d,
385 386 387 388 389
				 struct device_attribute *attr,
				 const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, set_query_use_ifaddr);
}
390
static DEVICE_ATTR_RW(multicast_query_use_ifaddr);
391

392
static ssize_t multicast_querier_show(struct device *d,
393 394 395 396 397 398 399
				      struct device_attribute *attr,
				      char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%d\n", br->multicast_querier);
}

400
static ssize_t multicast_querier_store(struct device *d,
401 402 403 404 405
				       struct device_attribute *attr,
				       const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, br_multicast_set_querier);
}
406
static DEVICE_ATTR_RW(multicast_querier);
407

408
static ssize_t hash_elasticity_show(struct device *d,
409 410 411 412 413 414 415 416 417 418 419 420
				    struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%u\n", br->hash_elasticity);
}

static int set_elasticity(struct net_bridge *br, unsigned long val)
{
	br->hash_elasticity = val;
	return 0;
}

421
static ssize_t hash_elasticity_store(struct device *d,
422 423 424 425 426
				     struct device_attribute *attr,
				     const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, set_elasticity);
}
427
static DEVICE_ATTR_RW(hash_elasticity);
428

429
static ssize_t hash_max_show(struct device *d, struct device_attribute *attr,
430 431 432 433 434 435
			     char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%u\n", br->hash_max);
}

436
static ssize_t hash_max_store(struct device *d, struct device_attribute *attr,
437 438 439 440
			      const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, br_multicast_set_hash_max);
}
441
static DEVICE_ATTR_RW(hash_max);
442

443
static ssize_t multicast_last_member_count_show(struct device *d,
444 445 446 447 448 449 450 451 452 453 454 455 456
						struct device_attribute *attr,
						char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%u\n", br->multicast_last_member_count);
}

static int set_last_member_count(struct net_bridge *br, unsigned long val)
{
	br->multicast_last_member_count = val;
	return 0;
}

457
static ssize_t multicast_last_member_count_store(struct device *d,
458 459 460 461 462
						 struct device_attribute *attr,
						 const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, set_last_member_count);
}
463
static DEVICE_ATTR_RW(multicast_last_member_count);
464

465
static ssize_t multicast_startup_query_count_show(
466 467 468 469 470 471 472 473 474 475 476 477
	struct device *d, struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%u\n", br->multicast_startup_query_count);
}

static int set_startup_query_count(struct net_bridge *br, unsigned long val)
{
	br->multicast_startup_query_count = val;
	return 0;
}

478
static ssize_t multicast_startup_query_count_store(
479 480 481 482 483
	struct device *d, struct device_attribute *attr, const char *buf,
	size_t len)
{
	return store_bridge_parm(d, buf, len, set_startup_query_count);
}
484
static DEVICE_ATTR_RW(multicast_startup_query_count);
485

486
static ssize_t multicast_last_member_interval_show(
487 488 489 490 491 492 493 494 495 496 497 498 499
	struct device *d, struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%lu\n",
		       jiffies_to_clock_t(br->multicast_last_member_interval));
}

static int set_last_member_interval(struct net_bridge *br, unsigned long val)
{
	br->multicast_last_member_interval = clock_t_to_jiffies(val);
	return 0;
}

500
static ssize_t multicast_last_member_interval_store(
501 502 503 504 505
	struct device *d, struct device_attribute *attr, const char *buf,
	size_t len)
{
	return store_bridge_parm(d, buf, len, set_last_member_interval);
}
506
static DEVICE_ATTR_RW(multicast_last_member_interval);
507

508
static ssize_t multicast_membership_interval_show(
509 510 511 512 513 514 515 516 517 518 519 520 521
	struct device *d, struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%lu\n",
		       jiffies_to_clock_t(br->multicast_membership_interval));
}

static int set_membership_interval(struct net_bridge *br, unsigned long val)
{
	br->multicast_membership_interval = clock_t_to_jiffies(val);
	return 0;
}

522
static ssize_t multicast_membership_interval_store(
523 524 525 526 527
	struct device *d, struct device_attribute *attr, const char *buf,
	size_t len)
{
	return store_bridge_parm(d, buf, len, set_membership_interval);
}
528
static DEVICE_ATTR_RW(multicast_membership_interval);
529

530
static ssize_t multicast_querier_interval_show(struct device *d,
531 532 533 534 535 536 537 538 539 540 541 542 543 544
					       struct device_attribute *attr,
					       char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%lu\n",
		       jiffies_to_clock_t(br->multicast_querier_interval));
}

static int set_querier_interval(struct net_bridge *br, unsigned long val)
{
	br->multicast_querier_interval = clock_t_to_jiffies(val);
	return 0;
}

545
static ssize_t multicast_querier_interval_store(struct device *d,
546 547 548 549 550
						struct device_attribute *attr,
						const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, set_querier_interval);
}
551
static DEVICE_ATTR_RW(multicast_querier_interval);
552

553
static ssize_t multicast_query_interval_show(struct device *d,
554 555 556 557 558 559 560 561 562 563 564 565 566 567
					     struct device_attribute *attr,
					     char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%lu\n",
		       jiffies_to_clock_t(br->multicast_query_interval));
}

static int set_query_interval(struct net_bridge *br, unsigned long val)
{
	br->multicast_query_interval = clock_t_to_jiffies(val);
	return 0;
}

568
static ssize_t multicast_query_interval_store(struct device *d,
569 570 571 572 573
					      struct device_attribute *attr,
					      const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, set_query_interval);
}
574
static DEVICE_ATTR_RW(multicast_query_interval);
575

576
static ssize_t multicast_query_response_interval_show(
577 578 579 580 581 582 583 584 585 586 587 588 589 590
	struct device *d, struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(
		buf, "%lu\n",
		jiffies_to_clock_t(br->multicast_query_response_interval));
}

static int set_query_response_interval(struct net_bridge *br, unsigned long val)
{
	br->multicast_query_response_interval = clock_t_to_jiffies(val);
	return 0;
}

591
static ssize_t multicast_query_response_interval_store(
592 593 594 595 596
	struct device *d, struct device_attribute *attr, const char *buf,
	size_t len)
{
	return store_bridge_parm(d, buf, len, set_query_response_interval);
}
597
static DEVICE_ATTR_RW(multicast_query_response_interval);
598

599
static ssize_t multicast_startup_query_interval_show(
600 601 602 603 604 605 606 607 608 609 610 611 612 613
	struct device *d, struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(
		buf, "%lu\n",
		jiffies_to_clock_t(br->multicast_startup_query_interval));
}

static int set_startup_query_interval(struct net_bridge *br, unsigned long val)
{
	br->multicast_startup_query_interval = clock_t_to_jiffies(val);
	return 0;
}

614
static ssize_t multicast_startup_query_interval_store(
615 616 617 618 619
	struct device *d, struct device_attribute *attr, const char *buf,
	size_t len)
{
	return store_bridge_parm(d, buf, len, set_startup_query_interval);
}
620
static DEVICE_ATTR_RW(multicast_startup_query_interval);
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

static ssize_t multicast_stats_enabled_show(struct device *d,
					    struct device_attribute *attr,
					    char *buf)
{
	struct net_bridge *br = to_bridge(d);

	return sprintf(buf, "%u\n", br->multicast_stats_enabled);
}

static int set_stats_enabled(struct net_bridge *br, unsigned long val)
{
	br->multicast_stats_enabled = !!val;
	return 0;
}

static ssize_t multicast_stats_enabled_store(struct device *d,
					     struct device_attribute *attr,
					     const char *buf,
					     size_t len)
{
	return store_bridge_parm(d, buf, len, set_stats_enabled);
}
static DEVICE_ATTR_RW(multicast_stats_enabled);
645
#endif
646
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
647
static ssize_t nf_call_iptables_show(
648 649 650 651 652 653 654 655 656 657 658 659
	struct device *d, struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%u\n", br->nf_call_iptables);
}

static int set_nf_call_iptables(struct net_bridge *br, unsigned long val)
{
	br->nf_call_iptables = val ? true : false;
	return 0;
}

660
static ssize_t nf_call_iptables_store(
661 662 663 664 665
	struct device *d, struct device_attribute *attr, const char *buf,
	size_t len)
{
	return store_bridge_parm(d, buf, len, set_nf_call_iptables);
}
666
static DEVICE_ATTR_RW(nf_call_iptables);
667

668
static ssize_t nf_call_ip6tables_show(
669 670 671 672 673 674 675 676 677 678 679 680
	struct device *d, struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%u\n", br->nf_call_ip6tables);
}

static int set_nf_call_ip6tables(struct net_bridge *br, unsigned long val)
{
	br->nf_call_ip6tables = val ? true : false;
	return 0;
}

681
static ssize_t nf_call_ip6tables_store(
682 683 684 685 686
	struct device *d, struct device_attribute *attr, const char *buf,
	size_t len)
{
	return store_bridge_parm(d, buf, len, set_nf_call_ip6tables);
}
687
static DEVICE_ATTR_RW(nf_call_ip6tables);
688

689
static ssize_t nf_call_arptables_show(
690 691 692 693 694 695 696 697 698 699 700 701
	struct device *d, struct device_attribute *attr, char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%u\n", br->nf_call_arptables);
}

static int set_nf_call_arptables(struct net_bridge *br, unsigned long val)
{
	br->nf_call_arptables = val ? true : false;
	return 0;
}

702
static ssize_t nf_call_arptables_store(
703 704 705 706 707
	struct device *d, struct device_attribute *attr, const char *buf,
	size_t len)
{
	return store_bridge_parm(d, buf, len, set_nf_call_arptables);
}
708
static DEVICE_ATTR_RW(nf_call_arptables);
709
#endif
710
#ifdef CONFIG_BRIDGE_VLAN_FILTERING
711
static ssize_t vlan_filtering_show(struct device *d,
712 713 714 715 716 717 718
				   struct device_attribute *attr,
				   char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%d\n", br->vlan_enabled);
}

719
static ssize_t vlan_filtering_store(struct device *d,
720 721 722 723 724
				    struct device_attribute *attr,
				    const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, br_vlan_filter_toggle);
}
725
static DEVICE_ATTR_RW(vlan_filtering);
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

static ssize_t vlan_protocol_show(struct device *d,
				  struct device_attribute *attr,
				  char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%#06x\n", ntohs(br->vlan_proto));
}

static ssize_t vlan_protocol_store(struct device *d,
				   struct device_attribute *attr,
				   const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, br_vlan_set_proto);
}
static DEVICE_ATTR_RW(vlan_protocol);
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757

static ssize_t default_pvid_show(struct device *d,
				 struct device_attribute *attr,
				 char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%d\n", br->default_pvid);
}

static ssize_t default_pvid_store(struct device *d,
				  struct device_attribute *attr,
				  const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, br_vlan_set_default_pvid);
}
static DEVICE_ATTR_RW(default_pvid);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

static ssize_t vlan_stats_enabled_show(struct device *d,
				       struct device_attribute *attr,
				       char *buf)
{
	struct net_bridge *br = to_bridge(d);
	return sprintf(buf, "%u\n", br->vlan_stats_enabled);
}

static ssize_t vlan_stats_enabled_store(struct device *d,
					struct device_attribute *attr,
					const char *buf, size_t len)
{
	return store_bridge_parm(d, buf, len, br_vlan_set_stats);
}
static DEVICE_ATTR_RW(vlan_stats_enabled);
774
#endif
775

L
Linus Torvalds 已提交
776
static struct attribute *bridge_attrs[] = {
777 778 779 780 781
	&dev_attr_forward_delay.attr,
	&dev_attr_hello_time.attr,
	&dev_attr_max_age.attr,
	&dev_attr_ageing_time.attr,
	&dev_attr_stp_state.attr,
782
	&dev_attr_group_fwd_mask.attr,
783 784 785 786 787 788 789 790 791 792 793 794
	&dev_attr_priority.attr,
	&dev_attr_bridge_id.attr,
	&dev_attr_root_id.attr,
	&dev_attr_root_path_cost.attr,
	&dev_attr_root_port.attr,
	&dev_attr_topology_change.attr,
	&dev_attr_topology_change_detected.attr,
	&dev_attr_hello_timer.attr,
	&dev_attr_tcn_timer.attr,
	&dev_attr_topology_change_timer.attr,
	&dev_attr_gc_timer.attr,
	&dev_attr_group_addr.attr,
795
	&dev_attr_flush.attr,
796 797
#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
	&dev_attr_multicast_router.attr,
798
	&dev_attr_multicast_snooping.attr,
799
	&dev_attr_multicast_querier.attr,
800
	&dev_attr_multicast_query_use_ifaddr.attr,
801 802
	&dev_attr_hash_elasticity.attr,
	&dev_attr_hash_max.attr,
803 804 805 806 807 808 809 810
	&dev_attr_multicast_last_member_count.attr,
	&dev_attr_multicast_startup_query_count.attr,
	&dev_attr_multicast_last_member_interval.attr,
	&dev_attr_multicast_membership_interval.attr,
	&dev_attr_multicast_querier_interval.attr,
	&dev_attr_multicast_query_interval.attr,
	&dev_attr_multicast_query_response_interval.attr,
	&dev_attr_multicast_startup_query_interval.attr,
811
	&dev_attr_multicast_stats_enabled.attr,
812
#endif
813
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
814 815 816
	&dev_attr_nf_call_iptables.attr,
	&dev_attr_nf_call_ip6tables.attr,
	&dev_attr_nf_call_arptables.attr,
817 818 819
#endif
#ifdef CONFIG_BRIDGE_VLAN_FILTERING
	&dev_attr_vlan_filtering.attr,
820
	&dev_attr_vlan_protocol.attr,
821
	&dev_attr_default_pvid.attr,
822
	&dev_attr_vlan_stats_enabled.attr,
823
#endif
L
Linus Torvalds 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837
	NULL
};

static struct attribute_group bridge_group = {
	.name = SYSFS_BRIDGE_ATTR,
	.attrs = bridge_attrs,
};

/*
 * Export the forwarding information table as a binary file
 * The records are struct __fdb_entry.
 *
 * Returns the number of bytes read.
 */
838
static ssize_t brforward_read(struct file *filp, struct kobject *kobj,
839 840
			      struct bin_attribute *bin_attr,
			      char *buf, loff_t off, size_t count)
L
Linus Torvalds 已提交
841
{
842
	struct device *dev = kobj_to_dev(kobj);
843
	struct net_bridge *br = to_bridge(dev);
L
Linus Torvalds 已提交
844 845 846 847 848 849
	int n;

	/* must read whole records */
	if (off % sizeof(struct __fdb_entry) != 0)
		return -EINVAL;

850
	n =  br_fdb_fillbuf(br, buf,
L
Linus Torvalds 已提交
851 852 853 854 855
			    count / sizeof(struct __fdb_entry),
			    off / sizeof(struct __fdb_entry));

	if (n > 0)
		n *= sizeof(struct __fdb_entry);
856

L
Linus Torvalds 已提交
857 858 859 860 861
	return n;
}

static struct bin_attribute bridge_forward = {
	.attr = { .name = SYSFS_BRIDGE_FDB,
862
		  .mode = S_IRUGO, },
L
Linus Torvalds 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	.read = brforward_read,
};

/*
 * Add entries in sysfs onto the existing network class device
 * for the bridge.
 *   Adds a attribute group "bridge" containing tuning parameters.
 *   Binary attribute containing the forward table
 *   Sub directory to hold links to interfaces.
 *
 * Note: the ifobj exists only to be a subdirectory
 *   to hold links.  The ifobj exists in same data structure
 *   as it's parent the bridge so reference counting works.
 */
int br_sysfs_addbr(struct net_device *dev)
{
879
	struct kobject *brobj = &dev->dev.kobj;
L
Linus Torvalds 已提交
880 881 882 883 884 885
	struct net_bridge *br = netdev_priv(dev);
	int err;

	err = sysfs_create_group(brobj, &bridge_group);
	if (err) {
		pr_info("%s: can't create group %s/%s\n",
886
			__func__, dev->name, bridge_group.name);
L
Linus Torvalds 已提交
887 888 889 890 891
		goto out1;
	}

	err = sysfs_create_bin_file(brobj, &bridge_forward);
	if (err) {
892
		pr_info("%s: can't create attribute file %s/%s\n",
893
			__func__, dev->name, bridge_forward.attr.name);
L
Linus Torvalds 已提交
894 895 896
		goto out2;
	}

897 898
	br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj);
	if (!br->ifobj) {
L
Linus Torvalds 已提交
899
		pr_info("%s: can't add kobject (directory) %s/%s\n",
900
			__func__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR);
L
Linus Torvalds 已提交
901 902 903 904
		goto out3;
	}
	return 0;
 out3:
905
	sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward);
L
Linus Torvalds 已提交
906
 out2:
907
	sysfs_remove_group(&dev->dev.kobj, &bridge_group);
L
Linus Torvalds 已提交
908 909 910 911 912 913 914
 out1:
	return err;

}

void br_sysfs_delbr(struct net_device *dev)
{
915
	struct kobject *kobj = &dev->dev.kobj;
L
Linus Torvalds 已提交
916 917
	struct net_bridge *br = netdev_priv(dev);

918
	kobject_put(br->ifobj);
L
Linus Torvalds 已提交
919 920 921
	sysfs_remove_bin_file(kobj, &bridge_forward);
	sysfs_remove_group(kobj, &bridge_group);
}