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