提交 079adf6c 编写于 作者: D David S. Miller

Merge branch 'bridge-sysfs-rtnl-notifications'

Xin Long says:

====================
bridge: support sending rntl info when we set attributes through sysfs/ioctl

This patchset is used to support sending rntl info to user in some places,
and ensure that whenever those attributes change internally or from sysfs,
that a netlink notification is sent out to listeners.

It also make some adjustment in bridge sysfs so that we can implement this
easily.

I've done some tests on this patchset, like:
[br_sysfs]
  1. change all the attribute values of br or brif:
  $ echo $value > /sys/class/net/br0/bridge/{*}
  $ echo $value > /sys/class/net/br0/brif/eth1/{*}

  2. meanwhile, on another terminal to observe the msg:
  $ bridge monitor

[br_ioctl]
  1. in bridge-utils package, do some changes in br_set, let brctl command
  use ioctl to set attribute:
         if ((ret = set_sysfs(path, value)) < 0) { -->
         if (1) {

  $ brctl set*

  2. meanwhile, on another terminal to observe the msg:
  $ bridge monitor

This test covers all the attributes that brctl and sysfs support to set.
====================
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
...@@ -112,7 +112,9 @@ static int add_del_if(struct net_bridge *br, int ifindex, int isadd) ...@@ -112,7 +112,9 @@ static int add_del_if(struct net_bridge *br, int ifindex, int isadd)
static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{ {
struct net_bridge *br = netdev_priv(dev); struct net_bridge *br = netdev_priv(dev);
struct net_bridge_port *p = NULL;
unsigned long args[4]; unsigned long args[4];
int ret = -EOPNOTSUPP;
if (copy_from_user(args, rq->ifr_data, sizeof(args))) if (copy_from_user(args, rq->ifr_data, sizeof(args)))
return -EFAULT; return -EFAULT;
...@@ -182,25 +184,29 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) ...@@ -182,25 +184,29 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN)) if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
return -EPERM; return -EPERM;
return br_set_forward_delay(br, args[1]); ret = br_set_forward_delay(br, args[1]);
break;
case BRCTL_SET_BRIDGE_HELLO_TIME: case BRCTL_SET_BRIDGE_HELLO_TIME:
if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN)) if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
return -EPERM; return -EPERM;
return br_set_hello_time(br, args[1]); ret = br_set_hello_time(br, args[1]);
break;
case BRCTL_SET_BRIDGE_MAX_AGE: case BRCTL_SET_BRIDGE_MAX_AGE:
if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN)) if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
return -EPERM; return -EPERM;
return br_set_max_age(br, args[1]); ret = br_set_max_age(br, args[1]);
break;
case BRCTL_SET_AGEING_TIME: case BRCTL_SET_AGEING_TIME:
if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN)) if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
return -EPERM; return -EPERM;
return br_set_ageing_time(br, args[1]); ret = br_set_ageing_time(br, args[1]);
break;
case BRCTL_GET_PORT_INFO: case BRCTL_GET_PORT_INFO:
{ {
...@@ -240,20 +246,19 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) ...@@ -240,20 +246,19 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
return -EPERM; return -EPERM;
br_stp_set_enabled(br, args[1]); br_stp_set_enabled(br, args[1]);
return 0; ret = 0;
break;
case BRCTL_SET_BRIDGE_PRIORITY: case BRCTL_SET_BRIDGE_PRIORITY:
if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN)) if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
return -EPERM; return -EPERM;
br_stp_set_bridge_priority(br, args[1]); br_stp_set_bridge_priority(br, args[1]);
return 0; ret = 0;
break;
case BRCTL_SET_PORT_PRIORITY: case BRCTL_SET_PORT_PRIORITY:
{ {
struct net_bridge_port *p;
int ret;
if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN)) if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
return -EPERM; return -EPERM;
...@@ -263,14 +268,11 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) ...@@ -263,14 +268,11 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
else else
ret = br_stp_set_port_priority(p, args[2]); ret = br_stp_set_port_priority(p, args[2]);
spin_unlock_bh(&br->lock); spin_unlock_bh(&br->lock);
return ret; break;
} }
case BRCTL_SET_PATH_COST: case BRCTL_SET_PATH_COST:
{ {
struct net_bridge_port *p;
int ret;
if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN)) if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
return -EPERM; return -EPERM;
...@@ -280,8 +282,7 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) ...@@ -280,8 +282,7 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
else else
ret = br_stp_set_path_cost(p, args[2]); ret = br_stp_set_path_cost(p, args[2]);
spin_unlock_bh(&br->lock); spin_unlock_bh(&br->lock);
break;
return ret;
} }
case BRCTL_GET_FDB_ENTRIES: case BRCTL_GET_FDB_ENTRIES:
...@@ -289,7 +290,14 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) ...@@ -289,7 +290,14 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
args[2], args[3]); args[2], args[3]);
} }
return -EOPNOTSUPP; if (!ret) {
if (p)
br_ifinfo_notify(RTM_NEWLINK, p);
else
netdev_state_change(br->dev);
}
return ret;
} }
static int old_deviceless(struct net *net, void __user *uarg) static int old_deviceless(struct net *net, void __user *uarg)
......
...@@ -43,7 +43,14 @@ static ssize_t store_bridge_parm(struct device *d, ...@@ -43,7 +43,14 @@ static ssize_t store_bridge_parm(struct device *d,
if (endp == buf) if (endp == buf)
return -EINVAL; return -EINVAL;
if (!rtnl_trylock())
return restart_syscall();
err = (*set)(br, val); err = (*set)(br, val);
if (!err)
netdev_state_change(br->dev);
rtnl_unlock();
return err ? err : len; return err ? err : len;
} }
...@@ -101,15 +108,7 @@ static ssize_t ageing_time_show(struct device *d, ...@@ -101,15 +108,7 @@ static ssize_t ageing_time_show(struct device *d,
static int set_ageing_time(struct net_bridge *br, unsigned long val) static int set_ageing_time(struct net_bridge *br, unsigned long val)
{ {
int ret; return br_set_ageing_time(br, val);
if (!rtnl_trylock())
return restart_syscall();
ret = br_set_ageing_time(br, val);
rtnl_unlock();
return ret;
} }
static ssize_t ageing_time_store(struct device *d, static ssize_t ageing_time_store(struct device *d,
...@@ -128,27 +127,18 @@ static ssize_t stp_state_show(struct device *d, ...@@ -128,27 +127,18 @@ static ssize_t stp_state_show(struct device *d,
} }
static int set_stp_state(struct net_bridge *br, unsigned long val)
{
br_stp_set_enabled(br, val);
return 0;
}
static ssize_t stp_state_store(struct device *d, static ssize_t stp_state_store(struct device *d,
struct device_attribute *attr, const char *buf, struct device_attribute *attr, const char *buf,
size_t len) size_t len)
{ {
struct net_bridge *br = to_bridge(d); return store_bridge_parm(d, buf, len, set_stp_state);
char *endp;
unsigned long val;
if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
return -EPERM;
val = simple_strtoul(buf, &endp, 0);
if (endp == buf)
return -EINVAL;
if (!rtnl_trylock())
return restart_syscall();
br_stp_set_enabled(br, val);
rtnl_unlock();
return len;
} }
static DEVICE_ATTR_RW(stp_state); static DEVICE_ATTR_RW(stp_state);
...@@ -160,29 +150,22 @@ static ssize_t group_fwd_mask_show(struct device *d, ...@@ -160,29 +150,22 @@ static ssize_t group_fwd_mask_show(struct device *d,
return sprintf(buf, "%#x\n", br->group_fwd_mask); return sprintf(buf, "%#x\n", br->group_fwd_mask);
} }
static int set_group_fwd_mask(struct net_bridge *br, unsigned long val)
static ssize_t group_fwd_mask_store(struct device *d,
struct device_attribute *attr,
const char *buf,
size_t len)
{ {
struct net_bridge *br = to_bridge(d);
char *endp;
unsigned long val;
if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
return -EPERM;
val = simple_strtoul(buf, &endp, 0);
if (endp == buf)
return -EINVAL;
if (val & BR_GROUPFWD_RESTRICTED) if (val & BR_GROUPFWD_RESTRICTED)
return -EINVAL; return -EINVAL;
br->group_fwd_mask = val; br->group_fwd_mask = val;
return len; 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);
} }
static DEVICE_ATTR_RW(group_fwd_mask); static DEVICE_ATTR_RW(group_fwd_mask);
...@@ -328,6 +311,7 @@ static ssize_t group_addr_store(struct device *d, ...@@ -328,6 +311,7 @@ static ssize_t group_addr_store(struct device *d,
br->group_addr_set = true; br->group_addr_set = true;
br_recalculate_fwd_mask(br); br_recalculate_fwd_mask(br);
netdev_state_change(br->dev);
rtnl_unlock(); rtnl_unlock();
...@@ -336,17 +320,17 @@ static ssize_t group_addr_store(struct device *d, ...@@ -336,17 +320,17 @@ static ssize_t group_addr_store(struct device *d,
static DEVICE_ATTR_RW(group_addr); static DEVICE_ATTR_RW(group_addr);
static int set_flush(struct net_bridge *br, unsigned long val)
{
br_fdb_flush(br);
return 0;
}
static ssize_t flush_store(struct device *d, static ssize_t flush_store(struct device *d,
struct device_attribute *attr, struct device_attribute *attr,
const char *buf, size_t len) const char *buf, size_t len)
{ {
struct net_bridge *br = to_bridge(d); return store_bridge_parm(d, buf, len, set_flush);
if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
return -EPERM;
br_fdb_flush(br);
return len;
} }
static DEVICE_ATTR_WO(flush); static DEVICE_ATTR_WO(flush);
......
...@@ -61,7 +61,6 @@ static int store_flag(struct net_bridge_port *p, unsigned long v, ...@@ -61,7 +61,6 @@ static int store_flag(struct net_bridge_port *p, unsigned long v,
if (flags != p->flags) { if (flags != p->flags) {
p->flags = flags; p->flags = flags;
br_port_flags_change(p, mask); br_port_flags_change(p, mask);
br_ifinfo_notify(RTM_NEWLINK, p);
} }
return 0; return 0;
} }
...@@ -253,8 +252,10 @@ static ssize_t brport_store(struct kobject *kobj, ...@@ -253,8 +252,10 @@ static ssize_t brport_store(struct kobject *kobj,
spin_lock_bh(&p->br->lock); spin_lock_bh(&p->br->lock);
ret = brport_attr->store(p, val); ret = brport_attr->store(p, val);
spin_unlock_bh(&p->br->lock); spin_unlock_bh(&p->br->lock);
if (ret == 0) if (!ret) {
br_ifinfo_notify(RTM_NEWLINK, p);
ret = count; ret = count;
}
} }
rtnl_unlock(); rtnl_unlock();
} }
......
...@@ -651,15 +651,7 @@ int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) ...@@ -651,15 +651,7 @@ int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
{ {
int err; return __br_vlan_filter_toggle(br, val);
if (!rtnl_trylock())
return restart_syscall();
err = __br_vlan_filter_toggle(br, val);
rtnl_unlock();
return err;
} }
int __br_vlan_set_proto(struct net_bridge *br, __be16 proto) int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
...@@ -713,18 +705,10 @@ int __br_vlan_set_proto(struct net_bridge *br, __be16 proto) ...@@ -713,18 +705,10 @@ int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
int br_vlan_set_proto(struct net_bridge *br, unsigned long val) int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
{ {
int err;
if (val != ETH_P_8021Q && val != ETH_P_8021AD) if (val != ETH_P_8021Q && val != ETH_P_8021AD)
return -EPROTONOSUPPORT; return -EPROTONOSUPPORT;
if (!rtnl_trylock()) return __br_vlan_set_proto(br, htons(val));
return restart_syscall();
err = __br_vlan_set_proto(br, htons(val));
rtnl_unlock();
return err;
} }
static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid) static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
...@@ -855,21 +839,17 @@ int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val) ...@@ -855,21 +839,17 @@ int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
if (val >= VLAN_VID_MASK) if (val >= VLAN_VID_MASK)
return -EINVAL; return -EINVAL;
if (!rtnl_trylock())
return restart_syscall();
if (pvid == br->default_pvid) if (pvid == br->default_pvid)
goto unlock; goto out;
/* Only allow default pvid change when filtering is disabled */ /* Only allow default pvid change when filtering is disabled */
if (br->vlan_enabled) { if (br->vlan_enabled) {
pr_info_once("Please disable vlan filtering to change default_pvid\n"); pr_info_once("Please disable vlan filtering to change default_pvid\n");
err = -EPERM; err = -EPERM;
goto unlock; goto out;
} }
err = __br_vlan_set_default_pvid(br, pvid); err = __br_vlan_set_default_pvid(br, pvid);
unlock: out:
rtnl_unlock();
return err; return err;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册