bridge: export multicast database via netlink
Subsystems (4)
PATCH v4 1/1 bridge: export multicast database via netlink
From: Cong Wang <[email protected]>
V4: remove some useless #include
some coding style fix
V3: drop debugging printk's
update selinux perm table as well
V2: drop patch 1/2, export ifindex directly
Redesign netlink attributes
Improve netlink seq check
Handle IPv6 addr as well
This patch exports bridge multicast database via netlink message type RTM_GETMDB. Similar to fdb, but currently bridge-specific. We may need to support modify multicast database too (RTM_{ADD,DEL}MDB).
--- include/uapi/linux/if_bridge.h | 55 ++++++++++++++ include/uapi/linux/rtnetlink.h | 3 + net/bridge/Makefile | 2 +- net/bridge/br_mdb.c | 157 ++++++++++++++++++++++++++++++++++++++++ net/bridge/br_multicast.c | 2 + net/bridge/br_private.h | 2 + security/selinux/nlmsgtab.c | 1 + 7 files changed, 221 insertions(+), 1 deletions(-)
include/uapi/linux/if_bridge.h +55 -0
@@ -116,4 +116,59 @@ enum { __IFLA_BRIDGE_MAX, };#define IFLA_BRIDGE_MAX (__IFLA_BRIDGE_MAX - 1) + +/* Bridge multicast database attributes +* [MDBA_MDB] = { +* [MDBA_MDB_ENTRY] = { +* [MDBA_MDB_ENTRY_INFO] +* } +* } +* [MDBA_ROUTER] = { +* [MDBA_ROUTER_PORT] +* } +*/ +enum { +MDBA_UNSPEC , +MDBA_MDB , +MDBA_ROUTER , + __MDBA_MAX, +}; +#define MDBA_MAX (__MDBA_MAX - 1) + +enum { +MDBA_MDB_UNSPEC , +MDBA_MDB_ENTRY , + __MDBA_MDB_MAX, +}; +#define MDBA_MDB_MAX (__MDBA_MDB_MAX - 1) + +enum { +MDBA_MDB_ENTRY_UNSPEC , +MDBA_MDB_ENTRY_INFO , + __MDBA_MDB_ENTRY_MAX, +}; +#define MDBA_MDB_ENTRY_MAX (__MDBA_MDB_ENTRY_MAX - 1) + +enum { +MDBA_ROUTER_UNSPEC , +MDBA_ROUTER_PORT , + __MDBA_ROUTER_MAX, +}; +#define MDBA_ROUTER_MAX (__MDBA_ROUTER_MAX - 1) + +struct br_port_msg { +__u32 ifindex; +}; + +struct br_mdb_entry { +__u32 ifindex; +struct { +union { +__be32 ip4; +struct in6_addr ip6; + } u; +__be16 proto; + } addr; +}; +#endif /* _UAPI_LINUX_IF_BRIDGE_H */
include/uapi/linux/rtnetlink.h +3 -0
@@ -125,6 +125,9 @@ enum { RTM_GETNETCONF = 82,#define RTM_GETNETCONF RTM_GETNETCONF + RTM_GETMDB = 86, +#define RTM_GETMDB RTM_GETMDB + __RTM_MAX,#define RTM_MAX (((__RTM_MAX + 3) & ~3) - 1) };
net/bridge/Makefile +1 -1
@@ -12,6 +12,6 @@ bridge-$(CONFIG_SYSFS) += br_sysfs_if.o br_sysfs_br.o bridge-$(CONFIG_BRIDGE_NETFILTER) += br_netfilter.o -bridge-$(CONFIG_BRIDGE_IGMP_SNOOPING) += br_multicast.o +bridge-$(CONFIG_BRIDGE_IGMP_SNOOPING) += br_multicast.o br_mdb.o obj-$(CONFIG_BRIDGE_NF_EBTABLES) += netfilter/
net/bridge/br_mdb.c new file +157 -0
157 lines not on this page · raw message
net/bridge/br_multicast.c +2 -0
@@ -322,6 +322,7 @@ static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, mdb->size = old ? old->size :0 ; mdb->ver = old ? old->ver ^1 :0 ; + mdb->seq = old ? (old->seq +1 ) :0 ;if (!old || elasticity)get_random_bytes (&mdb->secret,sizeof (mdb->secret));
@@ -1605,6 +1606,7 @@ void br_multicast_init(struct net_bridge *br) br_multicast_querier_expired, (unsigned long)br);setup_timer (&br->multicast_query_timer, br_multicast_query_expired, (unsigned long )br); +br_mdb_init (); }void br_multicast_open (struct net_bridge *br)
net/bridge/br_private.h +2 -0
@@ -105,6 +105,7 @@ struct net_bridge_mdb_htableu32 max;u32 secret;u32 ver; +u32 seq; };struct net_bridge_port
@@ -433,6 +434,7 @@ extern int br_multicast_set_port_router(struct net_bridge_port *p,extern int br_multicast_toggle (struct net_bridge *br,unsigned long val);extern int br_multicast_set_querier (struct net_bridge *br,unsigned long val);extern int br_multicast_set_hash_max (struct net_bridge *br,unsigned long val); +extern void br_mdb_init (void );static inline bool br_multicast_is_router (struct net_bridge *br) {
security/selinux/nlmsgtab.c +1 -0
@@ -67,6 +67,7 @@ static struct nlmsg_perm nlmsg_route_perms[] = { RTM_GETADDRLABEL, NETLINK_ROUTE_SOCKET__NLMSG_READ }, { RTM_GETDCB, NETLINK_ROUTE_SOCKET__NLMSG_READ }, { RTM_SETDCB, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, + { RTM_GETMDB, NETLINK_ROUTE_SOCKET__NLMSG_READ }, }; static struct nlmsg_perm nlmsg_tcpdiag_perms[] =
PATCH v4 1/1 iproute2: add mdb sub-command to bridge
From: Cong Wang <[email protected]>
V4: fix filter_dev
remove some useless #include
V3: improve the output, display router info only for -d
fix router parsing code
V2: sync with the kernel patch
handle IPv6 addr
a few cleanup
Sample output:
# ./bridge/bridge mdb bridge br0: port eth0, group 224.8.8.9 port eth1, group 224.8.8.8
# ./bridge/bridge -d mdb bridge br0: port eth0, group 224.8.8.9 port eth1, group 224.8.8.8 router ports: eth0
--- bridge/Makefile | 2 +- bridge/br_common.h | 3 +- bridge/bridge.c | 1 + bridge/mdb.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 2 deletions(-)
bridge/Makefile +1 -1
@@ -1,4 +1,4 @@ -BROBJ = bridge.o fdb.o monitor.o link.o +BROBJ = bridge.o fdb.o monitor.o link.o mdb.o include ../Config
bridge/br_common.h +2 -1
@@ -5,10 +5,11 @@ extern int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg);extern int do_fdb (int argc,char **argv); +extern int do_mdb (int argc,char **argv);extern int do_monitor (int argc,char **argv);extern int preferred_family;extern int show_stats; -extern int show_detail; +extern int show_details;extern int timestamp;extern struct rtnl_handle rth;
bridge/bridge.c +1 -0
@@ -43,6 +43,7 @@ static const struct cmd {int (*func)(int argc,char **argv); } cmds[] = { { "fdb", do_fdb }, + { "mdb", do_mdb }, { "monitor", do_monitor }, { "help", do_help }, { 0 }
bridge/mdb.c new file +173 -0
173 lines not on this page · raw message
Re: [PATCH v4] iproute2: add mdb sub-command to bridge
Quoted 7 lines
# ./bridge/bridge mdb bridge br0: port eth0, group 224.8.8.9 port eth1, group 224.8.8.8
I like the ability to see what is going on. It would be good if there was also a monitor like hook to see new entries created and destroyed (later version).
The output format should be one line per entry for easier parsing by utilities. Something similar to 'ip neigh show'
Re: [PATCH v4] iproute2: add mdb sub-command to bridge
Quoted 10 lines · Stephen Hemminger
Quoted 6 lines
# ./bridge/bridge mdb bridge br0: port eth0, group 224.8.8.9 port eth1, group 224.8.8.8I like the ability to see what is going on. It would be good if there was also a monitor like hook to see new entries created and destroyed (later version).
I am working on a patch to implement RTM_NEWMDB and RTM_DELMDB, so we will have this.
Quoted 4 lines
The output format should be one line per entry for easier parsing by utilities. Something similar to 'ip neigh show'
Right, I will update this in v5.
Thanks!
net/bridge/br_mdb.c line 114
Quoted 5 lines · Cong Wang
net/bridge/br_mdb.c new file +5
-0+static int br_mdb_dump (struct sk_buff *skb,struct netlink_callback *cb) +{ +struct net_device *dev; +struct net *net =sock_net (skb->sk); +struct nlmsghdr *nlh;
Set nlh = NULL
Quoted 15 lines
net/bridge/br_mdb.c new file +15
-0+int idx =0 , s_idx; + + s_idx = cb->args[0 ]; + +rcu_read_lock (); + +for_each_netdev_rcu (net, dev) { +if (dev->priv_flags &IFF_EBRIDGE ) { +struct br_port_msg *bpm; + +if (idx < s_idx) + goto skip; + + nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, + cb->nlh->nlmsg_seq, RTM_GETMDB,
net/bridge/br_mdb.c line 139
Quoted 10 lines
net/bridge/br_mdb.c new file +10
-0+ sizeof(*bpm), NLM_F_MULTI); +if (nlh ==NULL ) +break ; + + bpm =nlmsg_data (nlh); + bpm->ifindex = dev->ifindex; +if (br_mdb_fill_info (skb, cb, dev) <0 ) + goto out; +if (br_rports_fill_info (skb, cb, dev) <0 ) + goto out;
You need to reset cb->args[1] to 0 here so that when you process the next mdb it will not skip any entries.
net/bridge/br_mdb.c line 147
Quoted 8 lines
net/bridge/br_mdb.c new file +8
-0+ +nlmsg_end (skb, nlh); + skip: + idx++; + } + } + +out:
You need to call nlmsg_end(skb, nlh) here if nlh != NULL because you need to finalize the message in case you come from the "goto out" above. Otherwise your partial message is corrupt.
net/bridge/br_mdb.c line 148
Quoted 1 line
net/bridge/br_mdb.c new file +1
-0+ cb->seq = cb->args[2 ];
This can't possibly work if you have multiple bridges unless all of them have an identical mdb->seq.
Maybe leave the consistent dumping problem out for now and just set cb->seq = net->dev_base_seq so that you at least cover all bridges.
We don't need to guarantee that no rehash has happened throughout the dump, we only need to ensure that no rehash happnened if a bridge required more than one netlink message. You could store mdb->seq in cb->args[3] and compare it with the current mdb->seq after br_rports_fill_info() finished, if they differ you could just cb->seq++. I suggst you leave this out for now and work on this in a follow-up patch to not complicate this any further.
Quoted 29 lines · Thomas Graf
net/bridge/br_mdb.c line 114
Quoted 5 lines · Cong Wang
net/bridge/br_mdb.c new file +5
-0+static int br_mdb_dump (struct sk_buff *skb,struct netlink_callback *cb) +{ +struct net_device *dev; +struct net *net =sock_net (skb->sk); +struct nlmsghdr *nlh;Set nlh = NULL
Quoted 20 lines
net/bridge/br_mdb.c new file +20
-0+int idx =0 , s_idx; + + s_idx = cb->args[0 ]; + +rcu_read_lock (); + +for_each_netdev_rcu (net, dev) { +if (dev->priv_flags &IFF_EBRIDGE ) { +struct br_port_msg *bpm; + +if (idx < s_idx) + goto skip; + + nlh =nlmsg_put (skb,NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq,RTM_GETMDB , +sizeof (*bpm),NLM_F_MULTI ); +if (nlh ==NULL ) +break ; + + bpm =nlmsg_data (nlh);
Quoted 8 lines
net/bridge/br_mdb.c line 139
Quoted 5 lines
net/bridge/br_mdb.c new file +5
-0+ bpm->ifindex = dev->ifindex; +if (br_mdb_fill_info (skb, cb, dev) <0 ) + goto out; +if (br_rports_fill_info (skb, cb, dev) <0 ) + goto out;You need to reset cb->args[1] to 0 here so that when you process the next mdb it will not skip any entries.
Good catch! Will fix it.
Quoted 14 lines
net/bridge/br_mdb.c line 147
Quoted 9 lines
net/bridge/br_mdb.c new file +8
-0+ +nlmsg_end (skb, nlh); + skip: + idx++; + } + } + +out:You need to call
nlmsg_end(skb, nlh)here if nlh != NULL because you need to finalize the message in case you come from the "goto out" above. Otherwise your partial message is corrupt.
Right... I missed this case.
Quoted 6 lines
net/bridge/br_mdb.c line 148
Quoted 2 lines
net/bridge/br_mdb.c new file +1
-0+ cb->seq = cb->args[2 ];This can't possibly work if you have multiple bridges unless all of them have an identical
mdb->seq.
I thought sequence checking is per-message, so I must be wrong, it seems to be per-dump, then we will need a global seq for all the bridges.
Quoted 11 lines
Maybe leave the consistent dumping problem out for now and just set
cb->seq=net->dev_base_seqso that you at least cover all bridges.We don't need to guarantee that no rehash has happened throughout the dump, we only need to ensure that no rehash happnened if a bridge required more than one netlink message. You could store
mdb->seqin cb->args[3] and compare it with the currentmdb->seqafterbr_rports_fill_info()finished, if they differ you could just cb->seq++. I suggst you leave this out for now and work on this in a follow-up patch to not complicate this any further.
There is one message per-bridge, but maybe more messages per-dump.
Anyway, I will leave this out for now and put some comment on the code saying we need to improve this.
Thanks for your review!