^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Sysfs attributes of bridge ports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Linux ethernet bridge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Stephen Hemminger <shemminger@osdl.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/if_bridge.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "br_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct brport_attribute {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct attribute attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) ssize_t (*show)(struct net_bridge_port *, char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int (*store)(struct net_bridge_port *, unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int (*store_raw)(struct net_bridge_port *, char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define BRPORT_ATTR_RAW(_name, _mode, _show, _store) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) const struct brport_attribute brport_attr_##_name = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .attr = {.name = __stringify(_name), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .mode = _mode }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .show = _show, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .store_raw = _store, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define BRPORT_ATTR(_name, _mode, _show, _store) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) const struct brport_attribute brport_attr_##_name = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .attr = {.name = __stringify(_name), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .mode = _mode }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .show = _show, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .store = _store, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define BRPORT_ATTR_FLAG(_name, _mask) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static ssize_t show_##_name(struct net_bridge_port *p, char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return sprintf(buf, "%d\n", !!(p->flags & _mask)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int store_##_name(struct net_bridge_port *p, unsigned long v) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return store_flag(p, v, _mask); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static BRPORT_ATTR(_name, 0644, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) show_##_name, store_##_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int store_flag(struct net_bridge_port *p, unsigned long v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned long flags = p->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) flags |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) flags &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (flags != p->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) err = br_switchdev_set_port_flag(p, flags, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) p->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) br_port_flags_change(p, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static ssize_t show_path_cost(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return sprintf(buf, "%d\n", p->path_cost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static BRPORT_ATTR(path_cost, 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) show_path_cost, br_stp_set_path_cost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static ssize_t show_priority(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return sprintf(buf, "%d\n", p->priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static BRPORT_ATTR(priority, 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) show_priority, br_stp_set_port_priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static ssize_t show_designated_root(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return br_show_bridge_id(buf, &p->designated_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static BRPORT_ATTR(designated_root, 0444, show_designated_root, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static ssize_t show_designated_bridge(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return br_show_bridge_id(buf, &p->designated_bridge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static BRPORT_ATTR(designated_bridge, 0444, show_designated_bridge, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static ssize_t show_designated_port(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return sprintf(buf, "%d\n", p->designated_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static BRPORT_ATTR(designated_port, 0444, show_designated_port, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static ssize_t show_designated_cost(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return sprintf(buf, "%d\n", p->designated_cost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static BRPORT_ATTR(designated_cost, 0444, show_designated_cost, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static ssize_t show_port_id(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return sprintf(buf, "0x%x\n", p->port_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static BRPORT_ATTR(port_id, 0444, show_port_id, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static ssize_t show_port_no(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return sprintf(buf, "0x%x\n", p->port_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static BRPORT_ATTR(port_no, 0444, show_port_no, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static ssize_t show_change_ack(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return sprintf(buf, "%d\n", p->topology_change_ack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static BRPORT_ATTR(change_ack, 0444, show_change_ack, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static ssize_t show_config_pending(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return sprintf(buf, "%d\n", p->config_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static BRPORT_ATTR(config_pending, 0444, show_config_pending, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static ssize_t show_port_state(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return sprintf(buf, "%d\n", p->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static BRPORT_ATTR(state, 0444, show_port_state, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static ssize_t show_message_age_timer(struct net_bridge_port *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return sprintf(buf, "%ld\n", br_timer_value(&p->message_age_timer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static BRPORT_ATTR(message_age_timer, 0444, show_message_age_timer, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static ssize_t show_forward_delay_timer(struct net_bridge_port *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return sprintf(buf, "%ld\n", br_timer_value(&p->forward_delay_timer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static BRPORT_ATTR(forward_delay_timer, 0444, show_forward_delay_timer, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static ssize_t show_hold_timer(struct net_bridge_port *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return sprintf(buf, "%ld\n", br_timer_value(&p->hold_timer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static BRPORT_ATTR(hold_timer, 0444, show_hold_timer, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int store_flush(struct net_bridge_port *p, unsigned long v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) br_fdb_delete_by_port(p->br, p, 0, 0); // Don't delete local entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static BRPORT_ATTR(flush, 0200, NULL, store_flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static ssize_t show_group_fwd_mask(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return sprintf(buf, "%#x\n", p->group_fwd_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static int store_group_fwd_mask(struct net_bridge_port *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) unsigned long v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (v & BR_GROUPFWD_MACPAUSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) p->group_fwd_mask = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static BRPORT_ATTR(group_fwd_mask, 0644, show_group_fwd_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) store_group_fwd_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static ssize_t show_backup_port(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct net_bridge_port *backup_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) backup_p = rcu_dereference(p->backup_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (backup_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ret = sprintf(buf, "%s\n", backup_p->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static int store_backup_port(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct net_device *backup_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) char *nl = strchr(buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (nl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) *nl = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (strlen(buf) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) backup_dev = __dev_get_by_name(dev_net(p->dev), buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (!backup_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return nbp_backup_change(p, backup_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static BRPORT_ATTR_RAW(backup_port, 0644, show_backup_port, store_backup_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) BRPORT_ATTR_FLAG(hairpin_mode, BR_HAIRPIN_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) BRPORT_ATTR_FLAG(bpdu_guard, BR_BPDU_GUARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) BRPORT_ATTR_FLAG(root_block, BR_ROOT_BLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) BRPORT_ATTR_FLAG(learning, BR_LEARNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) BRPORT_ATTR_FLAG(unicast_flood, BR_FLOOD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) BRPORT_ATTR_FLAG(proxyarp, BR_PROXYARP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) BRPORT_ATTR_FLAG(proxyarp_wifi, BR_PROXYARP_WIFI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) BRPORT_ATTR_FLAG(multicast_flood, BR_MCAST_FLOOD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) BRPORT_ATTR_FLAG(broadcast_flood, BR_BCAST_FLOOD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) BRPORT_ATTR_FLAG(neigh_suppress, BR_NEIGH_SUPPRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) BRPORT_ATTR_FLAG(isolated, BR_ISOLATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static ssize_t show_multicast_router(struct net_bridge_port *p, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return sprintf(buf, "%d\n", p->multicast_router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static int store_multicast_router(struct net_bridge_port *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) unsigned long v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return br_multicast_set_port_router(p, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static BRPORT_ATTR(multicast_router, 0644, show_multicast_router,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) store_multicast_router);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) BRPORT_ATTR_FLAG(multicast_fast_leave, BR_MULTICAST_FAST_LEAVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) BRPORT_ATTR_FLAG(multicast_to_unicast, BR_MULTICAST_TO_UNICAST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static const struct brport_attribute *brport_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) &brport_attr_path_cost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) &brport_attr_priority,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) &brport_attr_port_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) &brport_attr_port_no,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) &brport_attr_designated_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) &brport_attr_designated_bridge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) &brport_attr_designated_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) &brport_attr_designated_cost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) &brport_attr_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) &brport_attr_change_ack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) &brport_attr_config_pending,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) &brport_attr_message_age_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) &brport_attr_forward_delay_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) &brport_attr_hold_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) &brport_attr_flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) &brport_attr_hairpin_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) &brport_attr_bpdu_guard,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) &brport_attr_root_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) &brport_attr_learning,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) &brport_attr_unicast_flood,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) &brport_attr_multicast_router,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) &brport_attr_multicast_fast_leave,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) &brport_attr_multicast_to_unicast,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) &brport_attr_proxyarp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) &brport_attr_proxyarp_wifi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) &brport_attr_multicast_flood,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) &brport_attr_broadcast_flood,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) &brport_attr_group_fwd_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) &brport_attr_neigh_suppress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) &brport_attr_isolated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) &brport_attr_backup_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static ssize_t brport_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct brport_attribute *brport_attr = to_brport_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct net_bridge_port *p = kobj_to_brport(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!brport_attr->show)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return brport_attr->show(p, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static ssize_t brport_store(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct brport_attribute *brport_attr = to_brport_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct net_bridge_port *p = kobj_to_brport(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ssize_t ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) char *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!ns_capable(dev_net(p->dev)->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (!rtnl_trylock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return restart_syscall();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (brport_attr->store_raw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) char *buf_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) buf_copy = kstrndup(buf, count, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (!buf_copy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) spin_lock_bh(&p->br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) ret = brport_attr->store_raw(p, buf_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) spin_unlock_bh(&p->br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) kfree(buf_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) } else if (brport_attr->store) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) val = simple_strtoul(buf, &endp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (endp == buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) spin_lock_bh(&p->br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ret = brport_attr->store(p, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) spin_unlock_bh(&p->br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) br_ifinfo_notify(RTM_NEWLINK, NULL, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) const struct sysfs_ops brport_sysfs_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) .show = brport_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) .store = brport_store,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * Add sysfs entries to ethernet device added to a bridge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * Creates a brport subdirectory with bridge attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * Puts symlink in bridge's brif subdirectory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int br_sysfs_addif(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct net_bridge *br = p->br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) const struct brport_attribute **a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) err = sysfs_create_link(&p->kobj, &br->dev->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) SYSFS_BRIDGE_PORT_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) for (a = brport_attrs; *a; ++a) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) err = sysfs_create_file(&p->kobj, &((*a)->attr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return sysfs_create_link(br->ifobj, &p->kobj, p->sysfs_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* Rename bridge's brif symlink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int br_sysfs_renameif(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct net_bridge *br = p->br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) /* If a rename fails, the rollback will cause another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * rename call with the existing name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (!strncmp(p->sysfs_name, p->dev->name, IFNAMSIZ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) err = sysfs_rename_link(br->ifobj, &p->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) p->sysfs_name, p->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) netdev_notice(br->dev, "unable to rename link %s to %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) p->sysfs_name, p->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }