1 /* 2 * Sysfs attributes of bridge 3 * Linux ethernet bridge 4 * 5 * Authors: 6 * Stephen Hemminger <shemminger@osdl.org> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 11 * 2 of the License, or (at your option) any later version. 12 */ 13 14 #include <linux/capability.h> 15 #include <linux/kernel.h> 16 #include <linux/netdevice.h> 17 #include <linux/etherdevice.h> 18 #include <linux/if_bridge.h> 19 #include <linux/rtnetlink.h> 20 #include <linux/spinlock.h> 21 #include <linux/times.h> 22 #include <linux/sched/signal.h> 23 24 #include "br_private.h" 25 26 #define to_bridge(cd) ((struct net_bridge *)netdev_priv(to_net_dev(cd))) 27 28 /* 29 * Common code for storing bridge parameters. 30 */ 31 static ssize_t store_bridge_parm(struct device *d, 32 const char *buf, size_t len, 33 int (*set)(struct net_bridge *, unsigned long)) 34 { 35 struct net_bridge *br = to_bridge(d); 36 char *endp; 37 unsigned long val; 38 int err; 39 40 if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN)) 41 return -EPERM; 42 43 val = simple_strtoul(buf, &endp, 0); 44 if (endp == buf) 45 return -EINVAL; 46 47 if (!rtnl_trylock()) 48 return restart_syscall(); 49 50 err = (*set)(br, val); 51 if (!err) 52 netdev_state_change(br->dev); 53 rtnl_unlock(); 54 55 return err ? err : len; 56 } 57 58 59 static ssize_t forward_delay_show(struct device *d, 60 struct device_attribute *attr, char *buf) 61 { 62 struct net_bridge *br = to_bridge(d); 63 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay)); 64 } 65 66 static ssize_t forward_delay_store(struct device *d, 67 struct device_attribute *attr, 68 const char *buf, size_t len) 69 { 70 return store_bridge_parm(d, buf, len, br_set_forward_delay); 71 } 72 static DEVICE_ATTR_RW(forward_delay); 73 74 static ssize_t hello_time_show(struct device *d, struct device_attribute *attr, 75 char *buf) 76 { 77 return sprintf(buf, "%lu\n", 78 jiffies_to_clock_t(to_bridge(d)->hello_time)); 79 } 80 81 static ssize_t hello_time_store(struct device *d, 82 struct device_attribute *attr, const char *buf, 83 size_t len) 84 { 85 return store_bridge_parm(d, buf, len, br_set_hello_time); 86 } 87 static DEVICE_ATTR_RW(hello_time); 88 89 static ssize_t max_age_show(struct device *d, struct device_attribute *attr, 90 char *buf) 91 { 92 return sprintf(buf, "%lu\n", 93 jiffies_to_clock_t(to_bridge(d)->max_age)); 94 } 95 96 static ssize_t max_age_store(struct device *d, struct device_attribute *attr, 97 const char *buf, size_t len) 98 { 99 return store_bridge_parm(d, buf, len, br_set_max_age); 100 } 101 static DEVICE_ATTR_RW(max_age); 102 103 static ssize_t ageing_time_show(struct device *d, 104 struct device_attribute *attr, char *buf) 105 { 106 struct net_bridge *br = to_bridge(d); 107 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time)); 108 } 109 110 static int set_ageing_time(struct net_bridge *br, unsigned long val) 111 { 112 return br_set_ageing_time(br, val); 113 } 114 115 static ssize_t ageing_time_store(struct device *d, 116 struct device_attribute *attr, 117 const char *buf, size_t len) 118 { 119 return store_bridge_parm(d, buf, len, set_ageing_time); 120 } 121 static DEVICE_ATTR_RW(ageing_time); 122 123 static ssize_t stp_state_show(struct device *d, 124 struct device_attribute *attr, char *buf) 125 { 126 struct net_bridge *br = to_bridge(d); 127 return sprintf(buf, "%d\n", br->stp_enabled); 128 } 129 130 131 static int set_stp_state(struct net_bridge *br, unsigned long val) 132 { 133 br_stp_set_enabled(br, val); 134 135 return 0; 136 } 137 138 static ssize_t stp_state_store(struct device *d, 139 struct device_attribute *attr, const char *buf, 140 size_t len) 141 { 142 return store_bridge_parm(d, buf, len, set_stp_state); 143 } 144 static DEVICE_ATTR_RW(stp_state); 145 146 static ssize_t group_fwd_mask_show(struct device *d, 147 struct device_attribute *attr, 148 char *buf) 149 { 150 struct net_bridge *br = to_bridge(d); 151 return sprintf(buf, "%#x\n", br->group_fwd_mask); 152 } 153 154 static int set_group_fwd_mask(struct net_bridge *br, unsigned long val) 155 { 156 if (val & BR_GROUPFWD_RESTRICTED) 157 return -EINVAL; 158 159 br->group_fwd_mask = val; 160 161 return 0; 162 } 163 164 static ssize_t group_fwd_mask_store(struct device *d, 165 struct device_attribute *attr, 166 const char *buf, 167 size_t len) 168 { 169 return store_bridge_parm(d, buf, len, set_group_fwd_mask); 170 } 171 static DEVICE_ATTR_RW(group_fwd_mask); 172 173 static ssize_t priority_show(struct device *d, struct device_attribute *attr, 174 char *buf) 175 { 176 struct net_bridge *br = to_bridge(d); 177 return sprintf(buf, "%d\n", 178 (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]); 179 } 180 181 static int set_priority(struct net_bridge *br, unsigned long val) 182 { 183 br_stp_set_bridge_priority(br, (u16) val); 184 return 0; 185 } 186 187 static ssize_t priority_store(struct device *d, struct device_attribute *attr, 188 const char *buf, size_t len) 189 { 190 return store_bridge_parm(d, buf, len, set_priority); 191 } 192 static DEVICE_ATTR_RW(priority); 193 194 static ssize_t root_id_show(struct device *d, struct device_attribute *attr, 195 char *buf) 196 { 197 return br_show_bridge_id(buf, &to_bridge(d)->designated_root); 198 } 199 static DEVICE_ATTR_RO(root_id); 200 201 static ssize_t bridge_id_show(struct device *d, struct device_attribute *attr, 202 char *buf) 203 { 204 return br_show_bridge_id(buf, &to_bridge(d)->bridge_id); 205 } 206 static DEVICE_ATTR_RO(bridge_id); 207 208 static ssize_t root_port_show(struct device *d, struct device_attribute *attr, 209 char *buf) 210 { 211 return sprintf(buf, "%d\n", to_bridge(d)->root_port); 212 } 213 static DEVICE_ATTR_RO(root_port); 214 215 static ssize_t root_path_cost_show(struct device *d, 216 struct device_attribute *attr, char *buf) 217 { 218 return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost); 219 } 220 static DEVICE_ATTR_RO(root_path_cost); 221 222 static ssize_t topology_change_show(struct device *d, 223 struct device_attribute *attr, char *buf) 224 { 225 return sprintf(buf, "%d\n", to_bridge(d)->topology_change); 226 } 227 static DEVICE_ATTR_RO(topology_change); 228 229 static ssize_t topology_change_detected_show(struct device *d, 230 struct device_attribute *attr, 231 char *buf) 232 { 233 struct net_bridge *br = to_bridge(d); 234 return sprintf(buf, "%d\n", br->topology_change_detected); 235 } 236 static DEVICE_ATTR_RO(topology_change_detected); 237 238 static ssize_t hello_timer_show(struct device *d, 239 struct device_attribute *attr, char *buf) 240 { 241 struct net_bridge *br = to_bridge(d); 242 return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer)); 243 } 244 static DEVICE_ATTR_RO(hello_timer); 245 246 static ssize_t tcn_timer_show(struct device *d, struct device_attribute *attr, 247 char *buf) 248 { 249 struct net_bridge *br = to_bridge(d); 250 return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer)); 251 } 252 static DEVICE_ATTR_RO(tcn_timer); 253 254 static ssize_t topology_change_timer_show(struct device *d, 255 struct device_attribute *attr, 256 char *buf) 257 { 258 struct net_bridge *br = to_bridge(d); 259 return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer)); 260 } 261 static DEVICE_ATTR_RO(topology_change_timer); 262 263 static ssize_t gc_timer_show(struct device *d, struct device_attribute *attr, 264 char *buf) 265 { 266 struct net_bridge *br = to_bridge(d); 267 return sprintf(buf, "%ld\n", br_timer_value(&br->gc_work.timer)); 268 } 269 static DEVICE_ATTR_RO(gc_timer); 270 271 static ssize_t group_addr_show(struct device *d, 272 struct device_attribute *attr, char *buf) 273 { 274 struct net_bridge *br = to_bridge(d); 275 return sprintf(buf, "%pM\n", br->group_addr); 276 } 277 278 static ssize_t group_addr_store(struct device *d, 279 struct device_attribute *attr, 280 const char *buf, size_t len) 281 { 282 struct net_bridge *br = to_bridge(d); 283 u8 new_addr[6]; 284 285 if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN)) 286 return -EPERM; 287 288 if (!mac_pton(buf, new_addr)) 289 return -EINVAL; 290 291 if (!is_link_local_ether_addr(new_addr)) 292 return -EINVAL; 293 294 if (new_addr[5] == 1 || /* 802.3x Pause address */ 295 new_addr[5] == 2 || /* 802.3ad Slow protocols */ 296 new_addr[5] == 3) /* 802.1X PAE address */ 297 return -EINVAL; 298 299 if (!rtnl_trylock()) 300 return restart_syscall(); 301 302 spin_lock_bh(&br->lock); 303 ether_addr_copy(br->group_addr, new_addr); 304 spin_unlock_bh(&br->lock); 305 306 br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true); 307 br_recalculate_fwd_mask(br); 308 netdev_state_change(br->dev); 309 310 rtnl_unlock(); 311 312 return len; 313 } 314 315 static DEVICE_ATTR_RW(group_addr); 316 317 static int set_flush(struct net_bridge *br, unsigned long val) 318 { 319 br_fdb_flush(br); 320 return 0; 321 } 322 323 static ssize_t flush_store(struct device *d, 324 struct device_attribute *attr, 325 const char *buf, size_t len) 326 { 327 return store_bridge_parm(d, buf, len, set_flush); 328 } 329 static DEVICE_ATTR_WO(flush); 330 331 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 332 static ssize_t multicast_router_show(struct device *d, 333 struct device_attribute *attr, char *buf) 334 { 335 struct net_bridge *br = to_bridge(d); 336 return sprintf(buf, "%d\n", br->multicast_router); 337 } 338 339 static ssize_t multicast_router_store(struct device *d, 340 struct device_attribute *attr, 341 const char *buf, size_t len) 342 { 343 return store_bridge_parm(d, buf, len, br_multicast_set_router); 344 } 345 static DEVICE_ATTR_RW(multicast_router); 346 347 static ssize_t multicast_snooping_show(struct device *d, 348 struct device_attribute *attr, 349 char *buf) 350 { 351 struct net_bridge *br = to_bridge(d); 352 return sprintf(buf, "%d\n", br_opt_get(br, BROPT_MULTICAST_ENABLED)); 353 } 354 355 static ssize_t multicast_snooping_store(struct device *d, 356 struct device_attribute *attr, 357 const char *buf, size_t len) 358 { 359 return store_bridge_parm(d, buf, len, br_multicast_toggle); 360 } 361 static DEVICE_ATTR_RW(multicast_snooping); 362 363 static ssize_t multicast_query_use_ifaddr_show(struct device *d, 364 struct device_attribute *attr, 365 char *buf) 366 { 367 struct net_bridge *br = to_bridge(d); 368 return sprintf(buf, "%d\n", 369 br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)); 370 } 371 372 static int set_query_use_ifaddr(struct net_bridge *br, unsigned long val) 373 { 374 br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val); 375 return 0; 376 } 377 378 static ssize_t 379 multicast_query_use_ifaddr_store(struct device *d, 380 struct device_attribute *attr, 381 const char *buf, size_t len) 382 { 383 return store_bridge_parm(d, buf, len, set_query_use_ifaddr); 384 } 385 static DEVICE_ATTR_RW(multicast_query_use_ifaddr); 386 387 static ssize_t multicast_querier_show(struct device *d, 388 struct device_attribute *attr, 389 char *buf) 390 { 391 struct net_bridge *br = to_bridge(d); 392 return sprintf(buf, "%d\n", br_opt_get(br, BROPT_MULTICAST_QUERIER)); 393 } 394 395 static ssize_t multicast_querier_store(struct device *d, 396 struct device_attribute *attr, 397 const char *buf, size_t len) 398 { 399 return store_bridge_parm(d, buf, len, br_multicast_set_querier); 400 } 401 static DEVICE_ATTR_RW(multicast_querier); 402 403 static ssize_t hash_elasticity_show(struct device *d, 404 struct device_attribute *attr, char *buf) 405 { 406 struct net_bridge *br = to_bridge(d); 407 return sprintf(buf, "%u\n", br->hash_elasticity); 408 } 409 410 static int set_elasticity(struct net_bridge *br, unsigned long val) 411 { 412 br->hash_elasticity = val; 413 return 0; 414 } 415 416 static ssize_t hash_elasticity_store(struct device *d, 417 struct device_attribute *attr, 418 const char *buf, size_t len) 419 { 420 return store_bridge_parm(d, buf, len, set_elasticity); 421 } 422 static DEVICE_ATTR_RW(hash_elasticity); 423 424 static ssize_t hash_max_show(struct device *d, struct device_attribute *attr, 425 char *buf) 426 { 427 struct net_bridge *br = to_bridge(d); 428 return sprintf(buf, "%u\n", br->hash_max); 429 } 430 431 static ssize_t hash_max_store(struct device *d, struct device_attribute *attr, 432 const char *buf, size_t len) 433 { 434 return store_bridge_parm(d, buf, len, br_multicast_set_hash_max); 435 } 436 static DEVICE_ATTR_RW(hash_max); 437 438 static ssize_t multicast_igmp_version_show(struct device *d, 439 struct device_attribute *attr, 440 char *buf) 441 { 442 struct net_bridge *br = to_bridge(d); 443 444 return sprintf(buf, "%u\n", br->multicast_igmp_version); 445 } 446 447 static ssize_t multicast_igmp_version_store(struct device *d, 448 struct device_attribute *attr, 449 const char *buf, size_t len) 450 { 451 return store_bridge_parm(d, buf, len, br_multicast_set_igmp_version); 452 } 453 static DEVICE_ATTR_RW(multicast_igmp_version); 454 455 static ssize_t multicast_last_member_count_show(struct device *d, 456 struct device_attribute *attr, 457 char *buf) 458 { 459 struct net_bridge *br = to_bridge(d); 460 return sprintf(buf, "%u\n", br->multicast_last_member_count); 461 } 462 463 static int set_last_member_count(struct net_bridge *br, unsigned long val) 464 { 465 br->multicast_last_member_count = val; 466 return 0; 467 } 468 469 static ssize_t multicast_last_member_count_store(struct device *d, 470 struct device_attribute *attr, 471 const char *buf, size_t len) 472 { 473 return store_bridge_parm(d, buf, len, set_last_member_count); 474 } 475 static DEVICE_ATTR_RW(multicast_last_member_count); 476 477 static ssize_t multicast_startup_query_count_show( 478 struct device *d, struct device_attribute *attr, char *buf) 479 { 480 struct net_bridge *br = to_bridge(d); 481 return sprintf(buf, "%u\n", br->multicast_startup_query_count); 482 } 483 484 static int set_startup_query_count(struct net_bridge *br, unsigned long val) 485 { 486 br->multicast_startup_query_count = val; 487 return 0; 488 } 489 490 static ssize_t multicast_startup_query_count_store( 491 struct device *d, struct device_attribute *attr, const char *buf, 492 size_t len) 493 { 494 return store_bridge_parm(d, buf, len, set_startup_query_count); 495 } 496 static DEVICE_ATTR_RW(multicast_startup_query_count); 497 498 static ssize_t multicast_last_member_interval_show( 499 struct device *d, struct device_attribute *attr, char *buf) 500 { 501 struct net_bridge *br = to_bridge(d); 502 return sprintf(buf, "%lu\n", 503 jiffies_to_clock_t(br->multicast_last_member_interval)); 504 } 505 506 static int set_last_member_interval(struct net_bridge *br, unsigned long val) 507 { 508 br->multicast_last_member_interval = clock_t_to_jiffies(val); 509 return 0; 510 } 511 512 static ssize_t multicast_last_member_interval_store( 513 struct device *d, struct device_attribute *attr, const char *buf, 514 size_t len) 515 { 516 return store_bridge_parm(d, buf, len, set_last_member_interval); 517 } 518 static DEVICE_ATTR_RW(multicast_last_member_interval); 519 520 static ssize_t multicast_membership_interval_show( 521 struct device *d, struct device_attribute *attr, char *buf) 522 { 523 struct net_bridge *br = to_bridge(d); 524 return sprintf(buf, "%lu\n", 525 jiffies_to_clock_t(br->multicast_membership_interval)); 526 } 527 528 static int set_membership_interval(struct net_bridge *br, unsigned long val) 529 { 530 br->multicast_membership_interval = clock_t_to_jiffies(val); 531 return 0; 532 } 533 534 static ssize_t multicast_membership_interval_store( 535 struct device *d, struct device_attribute *attr, const char *buf, 536 size_t len) 537 { 538 return store_bridge_parm(d, buf, len, set_membership_interval); 539 } 540 static DEVICE_ATTR_RW(multicast_membership_interval); 541 542 static ssize_t multicast_querier_interval_show(struct device *d, 543 struct device_attribute *attr, 544 char *buf) 545 { 546 struct net_bridge *br = to_bridge(d); 547 return sprintf(buf, "%lu\n", 548 jiffies_to_clock_t(br->multicast_querier_interval)); 549 } 550 551 static int set_querier_interval(struct net_bridge *br, unsigned long val) 552 { 553 br->multicast_querier_interval = clock_t_to_jiffies(val); 554 return 0; 555 } 556 557 static ssize_t multicast_querier_interval_store(struct device *d, 558 struct device_attribute *attr, 559 const char *buf, size_t len) 560 { 561 return store_bridge_parm(d, buf, len, set_querier_interval); 562 } 563 static DEVICE_ATTR_RW(multicast_querier_interval); 564 565 static ssize_t multicast_query_interval_show(struct device *d, 566 struct device_attribute *attr, 567 char *buf) 568 { 569 struct net_bridge *br = to_bridge(d); 570 return sprintf(buf, "%lu\n", 571 jiffies_to_clock_t(br->multicast_query_interval)); 572 } 573 574 static int set_query_interval(struct net_bridge *br, unsigned long val) 575 { 576 br->multicast_query_interval = clock_t_to_jiffies(val); 577 return 0; 578 } 579 580 static ssize_t multicast_query_interval_store(struct device *d, 581 struct device_attribute *attr, 582 const char *buf, size_t len) 583 { 584 return store_bridge_parm(d, buf, len, set_query_interval); 585 } 586 static DEVICE_ATTR_RW(multicast_query_interval); 587 588 static ssize_t multicast_query_response_interval_show( 589 struct device *d, struct device_attribute *attr, char *buf) 590 { 591 struct net_bridge *br = to_bridge(d); 592 return sprintf( 593 buf, "%lu\n", 594 jiffies_to_clock_t(br->multicast_query_response_interval)); 595 } 596 597 static int set_query_response_interval(struct net_bridge *br, unsigned long val) 598 { 599 br->multicast_query_response_interval = clock_t_to_jiffies(val); 600 return 0; 601 } 602 603 static ssize_t multicast_query_response_interval_store( 604 struct device *d, struct device_attribute *attr, const char *buf, 605 size_t len) 606 { 607 return store_bridge_parm(d, buf, len, set_query_response_interval); 608 } 609 static DEVICE_ATTR_RW(multicast_query_response_interval); 610 611 static ssize_t multicast_startup_query_interval_show( 612 struct device *d, struct device_attribute *attr, char *buf) 613 { 614 struct net_bridge *br = to_bridge(d); 615 return sprintf( 616 buf, "%lu\n", 617 jiffies_to_clock_t(br->multicast_startup_query_interval)); 618 } 619 620 static int set_startup_query_interval(struct net_bridge *br, unsigned long val) 621 { 622 br->multicast_startup_query_interval = clock_t_to_jiffies(val); 623 return 0; 624 } 625 626 static ssize_t multicast_startup_query_interval_store( 627 struct device *d, struct device_attribute *attr, const char *buf, 628 size_t len) 629 { 630 return store_bridge_parm(d, buf, len, set_startup_query_interval); 631 } 632 static DEVICE_ATTR_RW(multicast_startup_query_interval); 633 634 static ssize_t multicast_stats_enabled_show(struct device *d, 635 struct device_attribute *attr, 636 char *buf) 637 { 638 struct net_bridge *br = to_bridge(d); 639 640 return sprintf(buf, "%d\n", 641 br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)); 642 } 643 644 static int set_stats_enabled(struct net_bridge *br, unsigned long val) 645 { 646 br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!val); 647 return 0; 648 } 649 650 static ssize_t multicast_stats_enabled_store(struct device *d, 651 struct device_attribute *attr, 652 const char *buf, 653 size_t len) 654 { 655 return store_bridge_parm(d, buf, len, set_stats_enabled); 656 } 657 static DEVICE_ATTR_RW(multicast_stats_enabled); 658 659 #if IS_ENABLED(CONFIG_IPV6) 660 static ssize_t multicast_mld_version_show(struct device *d, 661 struct device_attribute *attr, 662 char *buf) 663 { 664 struct net_bridge *br = to_bridge(d); 665 666 return sprintf(buf, "%u\n", br->multicast_mld_version); 667 } 668 669 static ssize_t multicast_mld_version_store(struct device *d, 670 struct device_attribute *attr, 671 const char *buf, size_t len) 672 { 673 return store_bridge_parm(d, buf, len, br_multicast_set_mld_version); 674 } 675 static DEVICE_ATTR_RW(multicast_mld_version); 676 #endif 677 #endif 678 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 679 static ssize_t nf_call_iptables_show( 680 struct device *d, struct device_attribute *attr, char *buf) 681 { 682 struct net_bridge *br = to_bridge(d); 683 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_IPTABLES)); 684 } 685 686 static int set_nf_call_iptables(struct net_bridge *br, unsigned long val) 687 { 688 br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val); 689 return 0; 690 } 691 692 static ssize_t nf_call_iptables_store( 693 struct device *d, struct device_attribute *attr, const char *buf, 694 size_t len) 695 { 696 return store_bridge_parm(d, buf, len, set_nf_call_iptables); 697 } 698 static DEVICE_ATTR_RW(nf_call_iptables); 699 700 static ssize_t nf_call_ip6tables_show( 701 struct device *d, struct device_attribute *attr, char *buf) 702 { 703 struct net_bridge *br = to_bridge(d); 704 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_IP6TABLES)); 705 } 706 707 static int set_nf_call_ip6tables(struct net_bridge *br, unsigned long val) 708 { 709 br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val); 710 return 0; 711 } 712 713 static ssize_t nf_call_ip6tables_store( 714 struct device *d, struct device_attribute *attr, const char *buf, 715 size_t len) 716 { 717 return store_bridge_parm(d, buf, len, set_nf_call_ip6tables); 718 } 719 static DEVICE_ATTR_RW(nf_call_ip6tables); 720 721 static ssize_t nf_call_arptables_show( 722 struct device *d, struct device_attribute *attr, char *buf) 723 { 724 struct net_bridge *br = to_bridge(d); 725 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_ARPTABLES)); 726 } 727 728 static int set_nf_call_arptables(struct net_bridge *br, unsigned long val) 729 { 730 br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val); 731 return 0; 732 } 733 734 static ssize_t nf_call_arptables_store( 735 struct device *d, struct device_attribute *attr, const char *buf, 736 size_t len) 737 { 738 return store_bridge_parm(d, buf, len, set_nf_call_arptables); 739 } 740 static DEVICE_ATTR_RW(nf_call_arptables); 741 #endif 742 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 743 static ssize_t vlan_filtering_show(struct device *d, 744 struct device_attribute *attr, 745 char *buf) 746 { 747 struct net_bridge *br = to_bridge(d); 748 return sprintf(buf, "%d\n", br_opt_get(br, BROPT_VLAN_ENABLED)); 749 } 750 751 static ssize_t vlan_filtering_store(struct device *d, 752 struct device_attribute *attr, 753 const char *buf, size_t len) 754 { 755 return store_bridge_parm(d, buf, len, br_vlan_filter_toggle); 756 } 757 static DEVICE_ATTR_RW(vlan_filtering); 758 759 static ssize_t vlan_protocol_show(struct device *d, 760 struct device_attribute *attr, 761 char *buf) 762 { 763 struct net_bridge *br = to_bridge(d); 764 return sprintf(buf, "%#06x\n", ntohs(br->vlan_proto)); 765 } 766 767 static ssize_t vlan_protocol_store(struct device *d, 768 struct device_attribute *attr, 769 const char *buf, size_t len) 770 { 771 return store_bridge_parm(d, buf, len, br_vlan_set_proto); 772 } 773 static DEVICE_ATTR_RW(vlan_protocol); 774 775 static ssize_t default_pvid_show(struct device *d, 776 struct device_attribute *attr, 777 char *buf) 778 { 779 struct net_bridge *br = to_bridge(d); 780 return sprintf(buf, "%d\n", br->default_pvid); 781 } 782 783 static ssize_t default_pvid_store(struct device *d, 784 struct device_attribute *attr, 785 const char *buf, size_t len) 786 { 787 return store_bridge_parm(d, buf, len, br_vlan_set_default_pvid); 788 } 789 static DEVICE_ATTR_RW(default_pvid); 790 791 static ssize_t vlan_stats_enabled_show(struct device *d, 792 struct device_attribute *attr, 793 char *buf) 794 { 795 struct net_bridge *br = to_bridge(d); 796 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_VLAN_STATS_ENABLED)); 797 } 798 799 static ssize_t vlan_stats_enabled_store(struct device *d, 800 struct device_attribute *attr, 801 const char *buf, size_t len) 802 { 803 return store_bridge_parm(d, buf, len, br_vlan_set_stats); 804 } 805 static DEVICE_ATTR_RW(vlan_stats_enabled); 806 807 static ssize_t vlan_stats_per_port_show(struct device *d, 808 struct device_attribute *attr, 809 char *buf) 810 { 811 struct net_bridge *br = to_bridge(d); 812 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)); 813 } 814 815 static ssize_t vlan_stats_per_port_store(struct device *d, 816 struct device_attribute *attr, 817 const char *buf, size_t len) 818 { 819 return store_bridge_parm(d, buf, len, br_vlan_set_stats_per_port); 820 } 821 static DEVICE_ATTR_RW(vlan_stats_per_port); 822 #endif 823 824 static struct attribute *bridge_attrs[] = { 825 &dev_attr_forward_delay.attr, 826 &dev_attr_hello_time.attr, 827 &dev_attr_max_age.attr, 828 &dev_attr_ageing_time.attr, 829 &dev_attr_stp_state.attr, 830 &dev_attr_group_fwd_mask.attr, 831 &dev_attr_priority.attr, 832 &dev_attr_bridge_id.attr, 833 &dev_attr_root_id.attr, 834 &dev_attr_root_path_cost.attr, 835 &dev_attr_root_port.attr, 836 &dev_attr_topology_change.attr, 837 &dev_attr_topology_change_detected.attr, 838 &dev_attr_hello_timer.attr, 839 &dev_attr_tcn_timer.attr, 840 &dev_attr_topology_change_timer.attr, 841 &dev_attr_gc_timer.attr, 842 &dev_attr_group_addr.attr, 843 &dev_attr_flush.attr, 844 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 845 &dev_attr_multicast_router.attr, 846 &dev_attr_multicast_snooping.attr, 847 &dev_attr_multicast_querier.attr, 848 &dev_attr_multicast_query_use_ifaddr.attr, 849 &dev_attr_hash_elasticity.attr, 850 &dev_attr_hash_max.attr, 851 &dev_attr_multicast_last_member_count.attr, 852 &dev_attr_multicast_startup_query_count.attr, 853 &dev_attr_multicast_last_member_interval.attr, 854 &dev_attr_multicast_membership_interval.attr, 855 &dev_attr_multicast_querier_interval.attr, 856 &dev_attr_multicast_query_interval.attr, 857 &dev_attr_multicast_query_response_interval.attr, 858 &dev_attr_multicast_startup_query_interval.attr, 859 &dev_attr_multicast_stats_enabled.attr, 860 &dev_attr_multicast_igmp_version.attr, 861 #if IS_ENABLED(CONFIG_IPV6) 862 &dev_attr_multicast_mld_version.attr, 863 #endif 864 #endif 865 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 866 &dev_attr_nf_call_iptables.attr, 867 &dev_attr_nf_call_ip6tables.attr, 868 &dev_attr_nf_call_arptables.attr, 869 #endif 870 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 871 &dev_attr_vlan_filtering.attr, 872 &dev_attr_vlan_protocol.attr, 873 &dev_attr_default_pvid.attr, 874 &dev_attr_vlan_stats_enabled.attr, 875 &dev_attr_vlan_stats_per_port.attr, 876 #endif 877 NULL 878 }; 879 880 static const struct attribute_group bridge_group = { 881 .name = SYSFS_BRIDGE_ATTR, 882 .attrs = bridge_attrs, 883 }; 884 885 /* 886 * Export the forwarding information table as a binary file 887 * The records are struct __fdb_entry. 888 * 889 * Returns the number of bytes read. 890 */ 891 static ssize_t brforward_read(struct file *filp, struct kobject *kobj, 892 struct bin_attribute *bin_attr, 893 char *buf, loff_t off, size_t count) 894 { 895 struct device *dev = kobj_to_dev(kobj); 896 struct net_bridge *br = to_bridge(dev); 897 int n; 898 899 /* must read whole records */ 900 if (off % sizeof(struct __fdb_entry) != 0) 901 return -EINVAL; 902 903 n = br_fdb_fillbuf(br, buf, 904 count / sizeof(struct __fdb_entry), 905 off / sizeof(struct __fdb_entry)); 906 907 if (n > 0) 908 n *= sizeof(struct __fdb_entry); 909 910 return n; 911 } 912 913 static struct bin_attribute bridge_forward = { 914 .attr = { .name = SYSFS_BRIDGE_FDB, 915 .mode = 0444, }, 916 .read = brforward_read, 917 }; 918 919 /* 920 * Add entries in sysfs onto the existing network class device 921 * for the bridge. 922 * Adds a attribute group "bridge" containing tuning parameters. 923 * Binary attribute containing the forward table 924 * Sub directory to hold links to interfaces. 925 * 926 * Note: the ifobj exists only to be a subdirectory 927 * to hold links. The ifobj exists in same data structure 928 * as it's parent the bridge so reference counting works. 929 */ 930 int br_sysfs_addbr(struct net_device *dev) 931 { 932 struct kobject *brobj = &dev->dev.kobj; 933 struct net_bridge *br = netdev_priv(dev); 934 int err; 935 936 err = sysfs_create_group(brobj, &bridge_group); 937 if (err) { 938 pr_info("%s: can't create group %s/%s\n", 939 __func__, dev->name, bridge_group.name); 940 goto out1; 941 } 942 943 err = sysfs_create_bin_file(brobj, &bridge_forward); 944 if (err) { 945 pr_info("%s: can't create attribute file %s/%s\n", 946 __func__, dev->name, bridge_forward.attr.name); 947 goto out2; 948 } 949 950 br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj); 951 if (!br->ifobj) { 952 pr_info("%s: can't add kobject (directory) %s/%s\n", 953 __func__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR); 954 err = -ENOMEM; 955 goto out3; 956 } 957 return 0; 958 out3: 959 sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward); 960 out2: 961 sysfs_remove_group(&dev->dev.kobj, &bridge_group); 962 out1: 963 return err; 964 965 } 966 967 void br_sysfs_delbr(struct net_device *dev) 968 { 969 struct kobject *kobj = &dev->dev.kobj; 970 struct net_bridge *br = netdev_priv(dev); 971 972 kobject_put(br->ifobj); 973 sysfs_remove_bin_file(kobj, &bridge_forward); 974 sysfs_remove_group(kobj, &bridge_group); 975 } 976