Commit 4e2fa6b9 authored by David S. Miller's avatar David S. Miller

Merge branch 'bridge-add-vlan-notifications-and-rtm-support'

Nikolay Aleksandrov says:

====================
net: bridge: add vlan notifications and rtm support

This patch-set is a prerequisite for adding per-vlan options support
because we need to be able to send vlan-only notifications and do larger
vlan netlink dumps. Per-vlan options are needed as we move the control
more to vlans and would like to add per-vlan state (needed for per-vlan
STP and EVPN), per-vlan multicast options and control, and I'm sure
there would be many more per-vlan options coming.
Now we create/delete/dump vlans with the device AF_SPEC attribute which is
fine since we support vlan ranges or use a compact bridge_vlan_info
structure, but that cannot really be extended to support per-vlan options
well. The biggest issue is dumping them - we tried using the af_spec with
a new vlan option attribute but that led to insufficient message size
quickly, also another minor problem with that is we have to dump all vlans
always when notifying which, with options present, can be huge if they have
different options set, so we decided to add new rtm message types
specifically for vlans and register handlers for them and a new bridge vlan
notification nl group for vlan-only notifications.
The new RTM NEW/DEL/GETVLAN types introduced match the current af spec
bridge functionality and in fact use the same helpers.
The new nl format is:
 [BRIDGE_VLANDB_ENTRY]
    [BRIDGE_VLANDB_ENTRY_INFO] - bridge_vlan_info (either 1 vlan or
                                                   range start)
    [BRIDGE_VLANDB_ENTRY_RANGE] - range end

This allows to encapsulate a range in a single attribute and also to
create vlans and immediately set options on all of them with a single
attribute. The GETVLAN dump can span multiple messages and dump all the
necessary information. The vlan-only notifications are sent on
NEW/DELVLAN events or when vlan options change (currently only flags),
we try hard to compress the vlans into ranges in the notifications as
well. When the per-vlan options are added we'll add helpers to check for
option equality between neighbor vlans and will keep compressing them
when possible.

Note patch 02 is not really required, it's just a nice addition to have
human-readable error messages from the different vlan checks.

iproute2 changes and selftests will be sent with the next set which adds
the first per-vlan option - per-vlan state similar to the port state.

v2: changed patch 03 and patch 04 to use nlmsg_parse() in order to
    strictly validate the msg and make sure there are no remaining bytes
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents f6310b61 f545923b
...@@ -165,6 +165,35 @@ struct bridge_stp_xstats { ...@@ -165,6 +165,35 @@ struct bridge_stp_xstats {
__u64 tx_tcn; __u64 tx_tcn;
}; };
/* Bridge vlan RTM header */
struct br_vlan_msg {
__u8 family;
__u8 reserved1;
__u16 reserved2;
__u32 ifindex;
};
/* Bridge vlan RTM attributes
* [BRIDGE_VLANDB_ENTRY] = {
* [BRIDGE_VLANDB_ENTRY_INFO]
* ...
* }
*/
enum {
BRIDGE_VLANDB_UNSPEC,
BRIDGE_VLANDB_ENTRY,
__BRIDGE_VLANDB_MAX,
};
#define BRIDGE_VLANDB_MAX (__BRIDGE_VLANDB_MAX - 1)
enum {
BRIDGE_VLANDB_ENTRY_UNSPEC,
BRIDGE_VLANDB_ENTRY_INFO,
BRIDGE_VLANDB_ENTRY_RANGE,
__BRIDGE_VLANDB_ENTRY_MAX,
};
#define BRIDGE_VLANDB_ENTRY_MAX (__BRIDGE_VLANDB_ENTRY_MAX - 1)
/* Bridge multicast database attributes /* Bridge multicast database attributes
* [MDBA_MDB] = { * [MDBA_MDB] = {
* [MDBA_MDB_ENTRY] = { * [MDBA_MDB_ENTRY] = {
......
...@@ -171,6 +171,13 @@ enum { ...@@ -171,6 +171,13 @@ enum {
RTM_GETLINKPROP, RTM_GETLINKPROP,
#define RTM_GETLINKPROP RTM_GETLINKPROP #define RTM_GETLINKPROP RTM_GETLINKPROP
RTM_NEWVLAN = 112,
#define RTM_NEWNVLAN RTM_NEWVLAN
RTM_DELVLAN,
#define RTM_DELVLAN RTM_DELVLAN
RTM_GETVLAN,
#define RTM_GETVLAN RTM_GETVLAN
__RTM_MAX, __RTM_MAX,
#define RTM_MAX (((__RTM_MAX + 3) & ~3) - 1) #define RTM_MAX (((__RTM_MAX + 3) & ~3) - 1)
}; };
...@@ -723,6 +730,8 @@ enum rtnetlink_groups { ...@@ -723,6 +730,8 @@ enum rtnetlink_groups {
#define RTNLGRP_IPV6_MROUTE_R RTNLGRP_IPV6_MROUTE_R #define RTNLGRP_IPV6_MROUTE_R RTNLGRP_IPV6_MROUTE_R
RTNLGRP_NEXTHOP, RTNLGRP_NEXTHOP,
#define RTNLGRP_NEXTHOP RTNLGRP_NEXTHOP #define RTNLGRP_NEXTHOP RTNLGRP_NEXTHOP
RTNLGRP_BRVLAN,
#define RTNLGRP_BRVLAN RTNLGRP_BRVLAN
__RTNLGRP_MAX __RTNLGRP_MAX
}; };
#define RTNLGRP_MAX (__RTNLGRP_MAX - 1) #define RTNLGRP_MAX (__RTNLGRP_MAX - 1)
......
...@@ -561,52 +561,73 @@ static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p, ...@@ -561,52 +561,73 @@ static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
return err; return err;
} }
static int br_process_vlan_info(struct net_bridge *br, int br_process_vlan_info(struct net_bridge *br,
struct net_bridge_port *p, int cmd, struct net_bridge_port *p, int cmd,
struct bridge_vlan_info *vinfo_curr, struct bridge_vlan_info *vinfo_curr,
struct bridge_vlan_info **vinfo_last, struct bridge_vlan_info **vinfo_last,
bool *changed, bool *changed,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
if (!vinfo_curr->vid || vinfo_curr->vid >= VLAN_VID_MASK) int err, rtm_cmd;
if (!br_vlan_valid_id(vinfo_curr->vid, extack))
return -EINVAL; return -EINVAL;
/* needed for vlan-only NEWVLAN/DELVLAN notifications */
rtm_cmd = br_afspec_cmd_to_rtm(cmd);
if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
/* check if we are already processing a range */ if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
if (*vinfo_last)
return -EINVAL; return -EINVAL;
*vinfo_last = vinfo_curr; *vinfo_last = vinfo_curr;
/* don't allow range of pvids */
if ((*vinfo_last)->flags & BRIDGE_VLAN_INFO_PVID)
return -EINVAL;
return 0; return 0;
} }
if (*vinfo_last) { if (*vinfo_last) {
struct bridge_vlan_info tmp_vinfo; struct bridge_vlan_info tmp_vinfo;
int v, err; int v, v_change_start = 0;
if (!(vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END)) if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
return -EINVAL;
if (vinfo_curr->vid <= (*vinfo_last)->vid)
return -EINVAL; return -EINVAL;
memcpy(&tmp_vinfo, *vinfo_last, memcpy(&tmp_vinfo, *vinfo_last,
sizeof(struct bridge_vlan_info)); sizeof(struct bridge_vlan_info));
for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) { for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
bool curr_change = false;
tmp_vinfo.vid = v; tmp_vinfo.vid = v;
err = br_vlan_info(br, p, cmd, &tmp_vinfo, changed, err = br_vlan_info(br, p, cmd, &tmp_vinfo, &curr_change,
extack); extack);
if (err) if (err)
break; break;
if (curr_change) {
*changed = curr_change;
if (!v_change_start)
v_change_start = v;
} else {
/* nothing to notify yet */
if (!v_change_start)
continue;
br_vlan_notify(br, p, v_change_start,
v - 1, rtm_cmd);
v_change_start = 0;
}
} }
/* v_change_start is set only if the last/whole range changed */
if (v_change_start)
br_vlan_notify(br, p, v_change_start,
v - 1, rtm_cmd);
*vinfo_last = NULL; *vinfo_last = NULL;
return err; return err;
} }
return br_vlan_info(br, p, cmd, vinfo_curr, changed, extack); err = br_vlan_info(br, p, cmd, vinfo_curr, changed, extack);
if (*changed)
br_vlan_notify(br, p, vinfo_curr->vid, 0, rtm_cmd);
return err;
} }
static int br_afspec(struct net_bridge *br, static int br_afspec(struct net_bridge *br,
...@@ -1664,6 +1685,7 @@ int __init br_netlink_init(void) ...@@ -1664,6 +1685,7 @@ int __init br_netlink_init(void)
int err; int err;
br_mdb_init(); br_mdb_init();
br_vlan_rtnl_init();
rtnl_af_register(&br_af_ops); rtnl_af_register(&br_af_ops);
err = rtnl_link_register(&br_link_ops); err = rtnl_link_register(&br_link_ops);
...@@ -1681,6 +1703,7 @@ int __init br_netlink_init(void) ...@@ -1681,6 +1703,7 @@ int __init br_netlink_init(void)
void br_netlink_fini(void) void br_netlink_fini(void)
{ {
br_mdb_uninit(); br_mdb_uninit();
br_vlan_rtnl_uninit();
rtnl_af_unregister(&br_af_ops); rtnl_af_unregister(&br_af_ops);
rtnl_link_unregister(&br_link_ops); rtnl_link_unregister(&br_link_ops);
} }
...@@ -507,6 +507,65 @@ static inline bool nbp_state_should_learn(const struct net_bridge_port *p) ...@@ -507,6 +507,65 @@ static inline bool nbp_state_should_learn(const struct net_bridge_port *p)
return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING; return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING;
} }
static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack)
{
bool ret = vid > 0 && vid < VLAN_VID_MASK;
if (!ret)
NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid");
return ret;
}
static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur,
const struct bridge_vlan_info *last,
struct netlink_ext_ack *extack)
{
/* pvid flag is not allowed in ranges */
if (cur->flags & BRIDGE_VLAN_INFO_PVID) {
NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range");
return false;
}
/* when cur is the range end, check if:
* - it has range start flag
* - range ids are invalid (end is equal to or before start)
*/
if (last) {
if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one");
return false;
} else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) {
NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing");
return false;
} else if (cur->vid <= last->vid) {
NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id");
return false;
}
}
/* check for required range flags */
if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
BRIDGE_VLAN_INFO_RANGE_END))) {
NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing");
return false;
}
return true;
}
static inline int br_afspec_cmd_to_rtm(int cmd)
{
switch (cmd) {
case RTM_SETLINK:
return RTM_NEWVLAN;
case RTM_DELLINK:
return RTM_DELVLAN;
}
return 0;
}
static inline int br_opt_get(const struct net_bridge *br, static inline int br_opt_get(const struct net_bridge *br,
enum net_bridge_opts opt) enum net_bridge_opts opt)
{ {
...@@ -911,6 +970,12 @@ void br_vlan_get_stats(const struct net_bridge_vlan *v, ...@@ -911,6 +970,12 @@ void br_vlan_get_stats(const struct net_bridge_vlan *v,
void br_vlan_port_event(struct net_bridge_port *p, unsigned long event); void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
int br_vlan_bridge_event(struct net_device *dev, unsigned long event, int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
void *ptr); void *ptr);
void br_vlan_rtnl_init(void);
void br_vlan_rtnl_uninit(void);
void br_vlan_notify(const struct net_bridge *br,
const struct net_bridge_port *p,
u16 vid, u16 vid_range,
int cmd);
static inline struct net_bridge_vlan_group *br_vlan_group( static inline struct net_bridge_vlan_group *br_vlan_group(
const struct net_bridge *br) const struct net_bridge *br)
...@@ -962,6 +1027,10 @@ static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg) ...@@ -962,6 +1027,10 @@ static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
return vg->pvid; return vg->pvid;
} }
static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
{
return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags;
}
#else #else
static inline bool br_allowed_ingress(const struct net_bridge *br, static inline bool br_allowed_ingress(const struct net_bridge *br,
struct net_bridge_vlan_group *vg, struct net_bridge_vlan_group *vg,
...@@ -1105,6 +1174,21 @@ static inline int br_vlan_bridge_event(struct net_device *dev, ...@@ -1105,6 +1174,21 @@ static inline int br_vlan_bridge_event(struct net_device *dev,
{ {
return 0; return 0;
} }
static inline void br_vlan_rtnl_init(void)
{
}
static inline void br_vlan_rtnl_uninit(void)
{
}
static inline void br_vlan_notify(const struct net_bridge *br,
const struct net_bridge_port *p,
u16 vid, u16 vid_range,
int cmd)
{
}
#endif #endif
struct nf_br_ops { struct nf_br_ops {
...@@ -1176,6 +1260,12 @@ int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags, ...@@ -1176,6 +1260,12 @@ int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags,
int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags); int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev, int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
u32 filter_mask, int nlflags); u32 filter_mask, int nlflags);
int br_process_vlan_info(struct net_bridge *br,
struct net_bridge_port *p, int cmd,
struct bridge_vlan_info *vinfo_curr,
struct bridge_vlan_info **vinfo_last,
bool *changed,
struct netlink_ext_ack *extack);
#ifdef CONFIG_SYSFS #ifdef CONFIG_SYSFS
/* br_sysfs_if.c */ /* br_sysfs_if.c */
......
...@@ -257,6 +257,10 @@ static int __vlan_add(struct net_bridge_vlan *v, u16 flags, ...@@ -257,6 +257,10 @@ static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
&changed, extack); &changed, extack);
if (err) if (err)
goto out_filt; goto out_filt;
if (changed)
br_vlan_notify(br, NULL, v->vid, 0,
RTM_NEWVLAN);
} }
masterv = br_vlan_get_master(br, v->vid, extack); masterv = br_vlan_get_master(br, v->vid, extack);
...@@ -380,13 +384,31 @@ static void __vlan_group_free(struct net_bridge_vlan_group *vg) ...@@ -380,13 +384,31 @@ static void __vlan_group_free(struct net_bridge_vlan_group *vg)
kfree(vg); kfree(vg);
} }
static void __vlan_flush(struct net_bridge_vlan_group *vg) static void __vlan_flush(const struct net_bridge *br,
const struct net_bridge_port *p,
struct net_bridge_vlan_group *vg)
{ {
struct net_bridge_vlan *vlan, *tmp; struct net_bridge_vlan *vlan, *tmp;
u16 v_start = 0, v_end = 0;
__vlan_delete_pvid(vg, vg->pvid); __vlan_delete_pvid(vg, vg->pvid);
list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) {
/* take care of disjoint ranges */
if (!v_start) {
v_start = vlan->vid;
} else if (vlan->vid - v_end != 1) {
/* found range end, notify and start next one */
br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
v_start = vlan->vid;
}
v_end = vlan->vid;
__vlan_del(vlan); __vlan_del(vlan);
}
/* notify about the last/whole vlan range */
if (v_start)
br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
} }
struct sk_buff *br_handle_vlan(struct net_bridge *br, struct sk_buff *br_handle_vlan(struct net_bridge *br,
...@@ -716,7 +738,7 @@ void br_vlan_flush(struct net_bridge *br) ...@@ -716,7 +738,7 @@ void br_vlan_flush(struct net_bridge *br)
ASSERT_RTNL(); ASSERT_RTNL();
vg = br_vlan_group(br); vg = br_vlan_group(br);
__vlan_flush(vg); __vlan_flush(br, NULL, vg);
RCU_INIT_POINTER(br->vlgrp, NULL); RCU_INIT_POINTER(br->vlgrp, NULL);
synchronize_rcu(); synchronize_rcu();
__vlan_group_free(vg); __vlan_group_free(vg);
...@@ -925,12 +947,15 @@ static void br_vlan_disable_default_pvid(struct net_bridge *br) ...@@ -925,12 +947,15 @@ static void br_vlan_disable_default_pvid(struct net_bridge *br)
/* Disable default_pvid on all ports where it is still /* Disable default_pvid on all ports where it is still
* configured. * configured.
*/ */
if (vlan_default_pvid(br_vlan_group(br), pvid)) if (vlan_default_pvid(br_vlan_group(br), pvid)) {
br_vlan_delete(br, pvid); if (!br_vlan_delete(br, pvid))
br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
}
list_for_each_entry(p, &br->port_list, list) { list_for_each_entry(p, &br->port_list, list) {
if (vlan_default_pvid(nbp_vlan_group(p), pvid)) if (vlan_default_pvid(nbp_vlan_group(p), pvid) &&
nbp_vlan_delete(p, pvid); !nbp_vlan_delete(p, pvid))
br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
} }
br->default_pvid = 0; br->default_pvid = 0;
...@@ -972,7 +997,10 @@ int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid, ...@@ -972,7 +997,10 @@ int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
&vlchange, extack); &vlchange, extack);
if (err) if (err)
goto out; goto out;
br_vlan_delete(br, old_pvid);
if (br_vlan_delete(br, old_pvid))
br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN);
br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN);
set_bit(0, changed); set_bit(0, changed);
} }
...@@ -992,7 +1020,9 @@ int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid, ...@@ -992,7 +1020,9 @@ int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
&vlchange, extack); &vlchange, extack);
if (err) if (err)
goto err_port; goto err_port;
nbp_vlan_delete(p, old_pvid); if (nbp_vlan_delete(p, old_pvid))
br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN);
br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN);
set_bit(p->port_no, changed); set_bit(p->port_no, changed);
} }
...@@ -1007,22 +1037,28 @@ int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid, ...@@ -1007,22 +1037,28 @@ int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
if (!test_bit(p->port_no, changed)) if (!test_bit(p->port_no, changed))
continue; continue;
if (old_pvid) if (old_pvid) {
nbp_vlan_add(p, old_pvid, nbp_vlan_add(p, old_pvid,
BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_PVID |
BRIDGE_VLAN_INFO_UNTAGGED, BRIDGE_VLAN_INFO_UNTAGGED,
&vlchange, NULL); &vlchange, NULL);
br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN);
}
nbp_vlan_delete(p, pvid); nbp_vlan_delete(p, pvid);
br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
} }
if (test_bit(0, changed)) { if (test_bit(0, changed)) {
if (old_pvid) if (old_pvid) {
br_vlan_add(br, old_pvid, br_vlan_add(br, old_pvid,
BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_PVID |
BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_UNTAGGED |
BRIDGE_VLAN_INFO_BRENTRY, BRIDGE_VLAN_INFO_BRENTRY,
&vlchange, NULL); &vlchange, NULL);
br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN);
}
br_vlan_delete(br, pvid); br_vlan_delete(br, pvid);
br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
} }
goto out; goto out;
} }
...@@ -1115,6 +1151,7 @@ int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack) ...@@ -1115,6 +1151,7 @@ int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
&changed, extack); &changed, extack);
if (ret) if (ret)
goto err_vlan_add; goto err_vlan_add;
br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN);
} }
out: out:
return ret; return ret;
...@@ -1196,7 +1233,7 @@ void nbp_vlan_flush(struct net_bridge_port *port) ...@@ -1196,7 +1233,7 @@ void nbp_vlan_flush(struct net_bridge_port *port)
ASSERT_RTNL(); ASSERT_RTNL();
vg = nbp_vlan_group(port); vg = nbp_vlan_group(port);
__vlan_flush(vg); __vlan_flush(port->br, port, vg);
RCU_INIT_POINTER(port->vlgrp, NULL); RCU_INIT_POINTER(port->vlgrp, NULL);
synchronize_rcu(); synchronize_rcu();
__vlan_group_free(vg); __vlan_group_free(vg);
...@@ -1462,8 +1499,8 @@ int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr) ...@@ -1462,8 +1499,8 @@ int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
{ {
struct netdev_notifier_changeupper_info *info; struct netdev_notifier_changeupper_info *info;
struct net_bridge *br = netdev_priv(dev); struct net_bridge *br = netdev_priv(dev);
bool changed; int vlcmd = 0, ret = 0;
int ret = 0; bool changed = false;
switch (event) { switch (event) {
case NETDEV_REGISTER: case NETDEV_REGISTER:
...@@ -1471,9 +1508,11 @@ int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr) ...@@ -1471,9 +1508,11 @@ int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_PVID |
BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_UNTAGGED |
BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL); BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
vlcmd = RTM_NEWVLAN;
break; break;
case NETDEV_UNREGISTER: case NETDEV_UNREGISTER:
br_vlan_delete(br, br->default_pvid); changed = !br_vlan_delete(br, br->default_pvid);
vlcmd = RTM_DELVLAN;
break; break;
case NETDEV_CHANGEUPPER: case NETDEV_CHANGEUPPER:
info = ptr; info = ptr;
...@@ -1487,6 +1526,8 @@ int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr) ...@@ -1487,6 +1526,8 @@ int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
br_vlan_link_state_change(dev, br); br_vlan_link_state_change(dev, br);
break; break;
} }
if (changed)
br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd);
return ret; return ret;
} }
...@@ -1505,3 +1546,405 @@ void br_vlan_port_event(struct net_bridge_port *p, unsigned long event) ...@@ -1505,3 +1546,405 @@ void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
break; break;
} }
} }
static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
u16 flags)
{
struct bridge_vlan_info info;
struct nlattr *nest;
nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
if (!nest)
return false;
memset(&info, 0, sizeof(info));
info.vid = vid;
if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
if (flags & BRIDGE_VLAN_INFO_PVID)
info.flags |= BRIDGE_VLAN_INFO_PVID;
if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
goto out_err;
if (vid_range && vid < vid_range &&
!(flags & BRIDGE_VLAN_INFO_PVID) &&
nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
goto out_err;
nla_nest_end(skb, nest);
return true;
out_err:
nla_nest_cancel(skb, nest);
return false;
}
static size_t rtnl_vlan_nlmsg_size(void)
{
return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
+ nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
+ nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
+ nla_total_size(sizeof(struct bridge_vlan_info)); /* BRIDGE_VLANDB_ENTRY_INFO */
}
void br_vlan_notify(const struct net_bridge *br,
const struct net_bridge_port *p,
u16 vid, u16 vid_range,
int cmd)
{
struct net_bridge_vlan_group *vg;
struct net_bridge_vlan *v;
struct br_vlan_msg *bvm;
struct nlmsghdr *nlh;
struct sk_buff *skb;
int err = -ENOBUFS;
struct net *net;
u16 flags = 0;
int ifindex;
/* right now notifications are done only with rtnl held */
ASSERT_RTNL();
if (p) {
ifindex = p->dev->ifindex;
vg = nbp_vlan_group(p);
net = dev_net(p->dev);
} else {
ifindex = br->dev->ifindex;
vg = br_vlan_group(br);
net = dev_net(br->dev);
}
skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
if (!skb)
goto out_err;
err = -EMSGSIZE;
nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
if (!nlh)
goto out_err;
bvm = nlmsg_data(nlh);
memset(bvm, 0, sizeof(*bvm));
bvm->family = AF_BRIDGE;
bvm->ifindex = ifindex;
switch (cmd) {
case RTM_NEWVLAN:
/* need to find the vlan due to flags/options */
v = br_vlan_find(vg, vid);
if (!v || !br_vlan_should_use(v))
goto out_kfree;
flags = v->flags;
if (br_get_pvid(vg) == v->vid)
flags |= BRIDGE_VLAN_INFO_PVID;
break;
case RTM_DELVLAN:
break;
default:
goto out_kfree;
}
if (!br_vlan_fill_vids(skb, vid, vid_range, flags))
goto out_err;
nlmsg_end(skb, nlh);
rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
return;
out_err:
rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
out_kfree:
kfree_skb(skb);
}
/* check if v_curr can enter a range ending in range_end */
static bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
const struct net_bridge_vlan *range_end)
{
return v_curr->vid - range_end->vid == 1 &&
range_end->flags == v_curr->flags;
}
static int br_vlan_dump_dev(const struct net_device *dev,
struct sk_buff *skb,
struct netlink_callback *cb)
{
struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
struct net_bridge_vlan_group *vg;
int idx = 0, s_idx = cb->args[1];
struct nlmsghdr *nlh = NULL;
struct net_bridge_port *p;
struct br_vlan_msg *bvm;
struct net_bridge *br;
int err = 0;
u16 pvid;
if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
return -EINVAL;
if (netif_is_bridge_master(dev)) {
br = netdev_priv(dev);
vg = br_vlan_group_rcu(br);
p = NULL;
} else {
p = br_port_get_rcu(dev);
if (WARN_ON(!p))
return -EINVAL;
vg = nbp_vlan_group_rcu(p);
br = p->br;
}
if (!vg)
return 0;
nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
if (!nlh)
return -EMSGSIZE;
bvm = nlmsg_data(nlh);
memset(bvm, 0, sizeof(*bvm));
bvm->family = PF_BRIDGE;
bvm->ifindex = dev->ifindex;
pvid = br_get_pvid(vg);
/* idx must stay at range's beginning until it is filled in */
list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
if (!br_vlan_should_use(v))
continue;
if (idx < s_idx) {
idx++;
continue;
}
if (!range_start) {
range_start = v;
range_end = v;
continue;
}
if (v->vid == pvid || !br_vlan_can_enter_range(v, range_end)) {
u16 flags = br_vlan_flags(range_start, pvid);
if (!br_vlan_fill_vids(skb, range_start->vid,
range_end->vid, flags)) {
err = -EMSGSIZE;
break;
}
/* advance number of filled vlans */
idx += range_end->vid - range_start->vid + 1;
range_start = v;
}
range_end = v;
}
/* err will be 0 and range_start will be set in 3 cases here:
* - first vlan (range_start == range_end)
* - last vlan (range_start == range_end, not in range)
* - last vlan range (range_start != range_end, in range)
*/
if (!err && range_start &&
!br_vlan_fill_vids(skb, range_start->vid, range_end->vid,
br_vlan_flags(range_start, pvid)))
err = -EMSGSIZE;
cb->args[1] = err ? idx : 0;
nlmsg_end(skb, nlh);
return err;
}
static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
int idx = 0, err = 0, s_idx = cb->args[0];
struct net *net = sock_net(skb->sk);
struct br_vlan_msg *bvm;
struct net_device *dev;
err = nlmsg_parse(cb->nlh, sizeof(*bvm), NULL, 0, NULL, cb->extack);
if (err < 0)
return err;
bvm = nlmsg_data(cb->nlh);
rcu_read_lock();
if (bvm->ifindex) {
dev = dev_get_by_index_rcu(net, bvm->ifindex);
if (!dev) {
err = -ENODEV;
goto out_err;
}
err = br_vlan_dump_dev(dev, skb, cb);
if (err && err != -EMSGSIZE)
goto out_err;
} else {
for_each_netdev_rcu(net, dev) {
if (idx < s_idx)
goto skip;
err = br_vlan_dump_dev(dev, skb, cb);
if (err == -EMSGSIZE)
break;
skip:
idx++;
}
}
cb->args[0] = idx;
rcu_read_unlock();
return skb->len;
out_err:
rcu_read_unlock();
return err;
}
static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
[BRIDGE_VLANDB_ENTRY_INFO] = { .type = NLA_EXACT_LEN,
.len = sizeof(struct bridge_vlan_info) },
[BRIDGE_VLANDB_ENTRY_RANGE] = { .type = NLA_U16 },
};
static int br_vlan_rtm_process_one(struct net_device *dev,
const struct nlattr *attr,
int cmd, struct netlink_ext_ack *extack)
{
struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
struct net_bridge_vlan_group *vg;
struct net_bridge_port *p = NULL;
int err = 0, cmdmap = 0;
struct net_bridge *br;
bool changed = false;
if (netif_is_bridge_master(dev)) {
br = netdev_priv(dev);
vg = br_vlan_group(br);
} else {
p = br_port_get_rtnl(dev);
if (WARN_ON(!p))
return -ENODEV;
br = p->br;
vg = nbp_vlan_group(p);
}
if (WARN_ON(!vg))
return -ENODEV;
err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
br_vlan_db_policy, extack);
if (err)
return err;
if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
return -EINVAL;
}
memset(&vrange_end, 0, sizeof(vrange_end));
vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
BRIDGE_VLAN_INFO_RANGE_END)) {
NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
return -EINVAL;
}
if (!br_vlan_valid_id(vinfo->vid, extack))
return -EINVAL;
if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
/* validate user-provided flags without RANGE_BEGIN */
vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
/* vinfo_last is the range start, vinfo the range end */
vinfo_last = vinfo;
vinfo = &vrange_end;
if (!br_vlan_valid_id(vinfo->vid, extack) ||
!br_vlan_valid_range(vinfo, vinfo_last, extack))
return -EINVAL;
}
switch (cmd) {
case RTM_NEWVLAN:
cmdmap = RTM_SETLINK;
break;
case RTM_DELVLAN:
cmdmap = RTM_DELLINK;
break;
}
err = br_process_vlan_info(br, p, cmdmap, vinfo, &vinfo_last, &changed,
extack);
if (changed)
br_ifinfo_notify(cmdmap, br, p);
return err;
}
static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
struct net *net = sock_net(skb->sk);
struct br_vlan_msg *bvm;
struct net_device *dev;
struct nlattr *attr;
int err, vlans = 0;
int rem;
/* this should validate the header and check for remaining bytes */
err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
extack);
if (err < 0)
return err;
bvm = nlmsg_data(nlh);
dev = __dev_get_by_index(net, bvm->ifindex);
if (!dev)
return -ENODEV;
if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
return -EINVAL;
}
nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
if (nla_type(attr) != BRIDGE_VLANDB_ENTRY)
continue;
vlans++;
err = br_vlan_rtm_process_one(dev, attr, nlh->nlmsg_type,
extack);
if (err)
break;
}
if (!vlans) {
NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
err = -EINVAL;
}
return err;
}
void br_vlan_rtnl_init(void)
{
rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
br_vlan_rtm_dump, 0);
rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
br_vlan_rtm_process, NULL, 0);
rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
br_vlan_rtm_process, NULL, 0);
}
void br_vlan_rtnl_uninit(void)
{
rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);
}
...@@ -85,6 +85,9 @@ static const struct nlmsg_perm nlmsg_route_perms[] = ...@@ -85,6 +85,9 @@ static const struct nlmsg_perm nlmsg_route_perms[] =
{ RTM_GETNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_READ }, { RTM_GETNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_READ },
{ RTM_NEWLINKPROP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, { RTM_NEWLINKPROP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
{ RTM_DELLINKPROP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, { RTM_DELLINKPROP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
{ RTM_NEWVLAN, NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
{ RTM_DELVLAN, NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
{ RTM_GETVLAN, NETLINK_ROUTE_SOCKET__NLMSG_READ },
}; };
static const struct nlmsg_perm nlmsg_tcpdiag_perms[] = static const struct nlmsg_perm nlmsg_tcpdiag_perms[] =
...@@ -168,7 +171,7 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm) ...@@ -168,7 +171,7 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
* structures at the top of this file with the new mappings * structures at the top of this file with the new mappings
* before updating the BUILD_BUG_ON() macro! * before updating the BUILD_BUG_ON() macro!
*/ */
BUILD_BUG_ON(RTM_MAX != (RTM_NEWLINKPROP + 3)); BUILD_BUG_ON(RTM_MAX != (RTM_NEWVLAN + 3));
err = nlmsg_perm(nlmsg_type, perm, nlmsg_route_perms, err = nlmsg_perm(nlmsg_type, perm, nlmsg_route_perms,
sizeof(nlmsg_route_perms)); sizeof(nlmsg_route_perms));
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment