Skip to contentSkip to conversation

Netlink messages for multicast HW addr programming

PATCH In review Patrick Ruddy 3 participants 9 messages Jan 21, 2016 – Feb 3, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Netlink messages for multicast HW addr programming

Add RTM_NEWADDR and RTM_DELADDR netlink messages to indicate interest in specific multicast hardware addresses. These messages are sent when addressed are added or deleted from the appropriate interface driver.

net/core/dev_addr_lists.c +63 -2
index c0548d2..a0ebadd 100644
@@ -12,6 +12,7 @@
  */
 
 #include <linux/netdevice.h>
+#include <net/netlink.h>
 #include <linux/rtnetlink.h>
 #include <linux/export.h>
 #include <linux/list.h>
@@ -661,6 +662,62 @@ out:
 }
 EXPORT_SYMBOL(dev_mc_add_excl);
 
+static int fill_addr(struct sk_buff *skb, struct net_device *dev,
+		     const unsigned char *addr, int type)
+{
+	struct nlmsghdr *nlh;
+	struct ifaddrmsg *ifm;
+
+	nlh = nlmsg_put(skb, 0, 0, type, sizeof(*ifm), 0);
+	if (nlh == NULL)
+		return -EMSGSIZE;
+
+	ifm = nlmsg_data(nlh);
+	ifm->ifa_family = AF_UNSPEC;
+	ifm->ifa_prefixlen = 0;
+	ifm->ifa_flags = IFA_F_PERMANENT;
+	ifm->ifa_scope = RT_SCOPE_LINK;
+	ifm->ifa_index = dev->ifindex;
+	if (nla_put(skb, IFA_ADDRESS, dev->addr_len, addr))
+		goto nla_put_failure;
+	nlmsg_end(skb, nlh);
+	return 0;
+
+nla_put_failure:
+	nlmsg_cancel(skb, nlh);
+	return -EMSGSIZE;
+}
+
+static inline size_t addr_nlmsg_size(void)
+{
+	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
+		+ nla_total_size(MAX_ADDR_LEN);
+}
+
+static void mc_addr_notify(struct net_device *dev, const unsigned char * addr,
+			   int type)
+{
+	struct net *net = dev_net(dev);
+	struct sk_buff *skb;
+	int err = -ENOBUFS;
+
+	skb = nlmsg_new(addr_nlmsg_size(), GFP_ATOMIC);
+	if (skb == NULL)
+		goto errout;
+
+	err = fill_addr(skb, dev, addr, type);
+	if (err < 0) {
+		WARN_ON(err == -EMSGSIZE);
+		kfree_skb(skb);
+		goto errout;
+	}
+	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
+	return;
+errout:
+	if (err < 0)
+		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
+}
+
 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
 			bool global)
 {
@@ -669,8 +726,10 @@ static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
 	netif_addr_lock_bh(dev);
 	err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
 			       NETDEV_HW_ADDR_T_MULTICAST, global, false, 0);
-	if (!err)
+	if (!err) {
 		__dev_set_rx_mode(dev);
+		mc_addr_notify(dev, addr, RTM_NEWADDR);
+	}
 	netif_addr_unlock_bh(dev);
 	return err;
 }
@@ -709,8 +768,10 @@ static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
 	netif_addr_lock_bh(dev);
 	err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
 			       NETDEV_HW_ADDR_T_MULTICAST, global, false);
-	if (!err)
+	if (!err) {
 		__dev_set_rx_mode(dev);
+		mc_addr_notify(dev, addr, RTM_DELADDR);
+	}
 	netif_addr_unlock_bh(dev);
 	return err;
 }
Signature · 1 line
2.1.4
#1 lore raw
Quoted 32 lines · Thu, Jan 21, 2016 at 12:47:54PM CET, [email protected] wrote:

Add RTM_NEWADDR and RTM_DELADDR netlink messages to indicate interest in specific multicast hardware addresses. These messages are sent when addressed are added or deleted from the appropriate interface driver.

net/core/dev_addr_lists.c +10 -0
index c0548d2..a0ebadd 100644
@@ -12,6 +12,7 @@
 */
 
 #include <linux/netdevice.h>
+#include <net/netlink.h>
 #include <linux/rtnetlink.h>
 #include <linux/export.h>
 #include <linux/list.h>
@@ -661,6 +662,62 @@ out:
 }
 EXPORT_SYMBOL(dev_mc_add_excl);
 
+static int fill_addr(struct sk_buff *skb, struct net_device *dev,
+		     const unsigned char *addr, int type)
+{
+	struct nlmsghdr *nlh;
+	struct ifaddrmsg *ifm;
+
+	nlh = nlmsg_put(skb, 0, 0, type, sizeof(*ifm), 0);
+	if (nlh == NULL)
+		return -EMSGSIZE;

net/core/dev_addr_lists.c out: · line 683

Quoted 10 lines
net/core/dev_addr_lists.c +10 -0
+
+	ifm = nlmsg_data(nlh);
+	ifm->ifa_family = AF_UNSPEC;
+	ifm->ifa_prefixlen = 0;
+	ifm->ifa_flags = IFA_F_PERMANENT;
+	ifm->ifa_scope = RT_SCOPE_LINK;
+	ifm->ifa_index = dev->ifindex;
+	if (nla_put(skb, IFA_ADDRESS, dev->addr_len, addr))
+		goto nla_put_failure;
+	nlmsg_end(skb, nlh);

How can userspace tell if this is a multicast addr? Also, why not add notifications for unicast addrs so we handle both the same?

#2 lore raw
Quoted 38 lines · Jiri Pirko
Quoted 37 lines · Thu, Jan 21, 2016 at 12:47:54PM CET, [email protected] wrote:

Add RTM_NEWADDR and RTM_DELADDR netlink messages to indicate interest in specific multicast hardware addresses. These messages are sent when addressed are added or deleted from the appropriate interface driver.

net/core/dev_addr_lists.c +15 -0
index c0548d2..a0ebadd 100644
@@ -12,6 +12,7 @@
 */
 
 #include <linux/netdevice.h>
+#include <net/netlink.h>
 #include <linux/rtnetlink.h>
 #include <linux/export.h>
 #include <linux/list.h>
@@ -661,6 +662,62 @@ out:
 }
 EXPORT_SYMBOL(dev_mc_add_excl);
 
+static int fill_addr(struct sk_buff *skb, struct net_device *dev,
+		     const unsigned char *addr, int type)
+{
+	struct nlmsghdr *nlh;
+	struct ifaddrmsg *ifm;
+
+	nlh = nlmsg_put(skb, 0, 0, type, sizeof(*ifm), 0);
+	if (nlh == NULL)
+		return -EMSGSIZE;
+
+	ifm = nlmsg_data(nlh);
+	ifm->ifa_family = AF_UNSPEC;
+	ifm->ifa_prefixlen = 0;
+	ifm->ifa_flags = IFA_F_PERMANENT;
Quoted 8 lines

net/core/dev_addr_lists.c out: · line 683

Quoted 5 lines
net/core/dev_addr_lists.c +5 -0
+	ifm->ifa_scope = RT_SCOPE_LINK;
+	ifm->ifa_index = dev->ifindex;
+	if (nla_put(skb, IFA_ADDRESS, dev->addr_len, addr))
+		goto nla_put_failure;
+	nlmsg_end(skb, nlh);

How can userspace tell if this is a multicast addr? Also, why not add notifications for unicast addrs so we handle both the same?

I am only using this RTM_NEW/DELADDR AF_UNSPEC message for multicast addresses. I understand that the unicast addresses are notified in the RTM_NEW/DELLINK.

-pr

#3 lore raw
Quoted 48 lines · Fri, Jan 22, 2016 at 03:51:55PM CET, [email protected] wrote:
Quoted 38 lines · Jiri Pirko
Quoted 37 lines · Thu, Jan 21, 2016 at 12:47:54PM CET, [email protected] wrote:

Add RTM_NEWADDR and RTM_DELADDR netlink messages to indicate interest in specific multicast hardware addresses. These messages are sent when addressed are added or deleted from the appropriate interface driver.

net/core/dev_addr_lists.c +15 -0
index c0548d2..a0ebadd 100644
@@ -12,6 +12,7 @@
 */
 
 #include <linux/netdevice.h>
+#include <net/netlink.h>
 #include <linux/rtnetlink.h>
 #include <linux/export.h>
 #include <linux/list.h>
@@ -661,6 +662,62 @@ out:
 }
 EXPORT_SYMBOL(dev_mc_add_excl);
 
+static int fill_addr(struct sk_buff *skb, struct net_device *dev,
+		     const unsigned char *addr, int type)
+{
+	struct nlmsghdr *nlh;
+	struct ifaddrmsg *ifm;
+
+	nlh = nlmsg_put(skb, 0, 0, type, sizeof(*ifm), 0);
+	if (nlh == NULL)
+		return -EMSGSIZE;
+
+	ifm = nlmsg_data(nlh);
+	ifm->ifa_family = AF_UNSPEC;
+	ifm->ifa_prefixlen = 0;
+	ifm->ifa_flags = IFA_F_PERMANENT;
Quoted 8 lines

net/core/dev_addr_lists.c out: · line 683

Quoted 5 lines
net/core/dev_addr_lists.c +5 -0
+	ifm->ifa_scope = RT_SCOPE_LINK;
+	ifm->ifa_index = dev->ifindex;
+	if (nla_put(skb, IFA_ADDRESS, dev->addr_len, addr))
+		goto nla_put_failure;
+	nlmsg_end(skb, nlh);

How can userspace tell if this is a multicast addr? Also, why not add notifications for unicast addrs so we handle both the same?

Quoted 3 lines

I am only using this RTM_NEW/DELADDR AF_UNSPEC message for multicast addresses. I understand that the unicast addresses are notified in the RTM_NEW/DELLINK.

dev->dev_addr is notified by that. UC addresses do not have any notification now.

#4 lore raw
Quoted 49 lines · Jiri Pirko
Quoted 48 lines · Fri, Jan 22, 2016 at 03:51:55PM CET, [email protected] wrote:
Quoted 46 lines · Jiri Pirko
Quoted 32 lines · Thu, Jan 21, 2016 at 12:47:54PM CET, [email protected] wrote:

Add RTM_NEWADDR and RTM_DELADDR netlink messages to indicate interest in specific multicast hardware addresses. These messages are sent when addressed are added or deleted from the appropriate interface driver.

net/core/dev_addr_lists.c +10 -0
index c0548d2..a0ebadd 100644
@@ -12,6 +12,7 @@
 */
 
 #include <linux/netdevice.h>
+#include <net/netlink.h>
 #include <linux/rtnetlink.h>
 #include <linux/export.h>
 #include <linux/list.h>
@@ -661,6 +662,62 @@ out:
 }
 EXPORT_SYMBOL(dev_mc_add_excl);
 
+static int fill_addr(struct sk_buff *skb, struct net_device *dev,
+		     const unsigned char *addr, int type)
+{
+	struct nlmsghdr *nlh;
+	struct ifaddrmsg *ifm;
+
+	nlh = nlmsg_put(skb, 0, 0, type, sizeof(*ifm), 0);
+	if (nlh == NULL)
+		return -EMSGSIZE;

net/core/dev_addr_lists.c out: · line 683

Quoted 10 lines
net/core/dev_addr_lists.c +10 -0
+
+	ifm = nlmsg_data(nlh);
+	ifm->ifa_family = AF_UNSPEC;
+	ifm->ifa_prefixlen = 0;
+	ifm->ifa_flags = IFA_F_PERMANENT;
+	ifm->ifa_scope = RT_SCOPE_LINK;
+	ifm->ifa_index = dev->ifindex;
+	if (nla_put(skb, IFA_ADDRESS, dev->addr_len, addr))
+		goto nla_put_failure;
+	nlmsg_end(skb, nlh);

How can userspace tell if this is a multicast addr? Also, why not add notifications for unicast addrs so we handle both the same?

Quoted 6 lines
Quoted 3 lines

I am only using this RTM_NEW/DELADDR AF_UNSPEC message for multicast addresses. I understand that the unicast addresses are notified in the RTM_NEW/DELLINK.

dev->dev_addr is notified by that. UC addresses do not have any notification now.

It is the multicast MAC address (ie the multicast equivalent of the dev_addr) that we are notifying here right? Not L3 addresses.

-pr

#5 lore raw
Quoted 50 lines · Fri, Jan 22, 2016 at 04:49:36PM CET, [email protected] wrote:
Quoted 49 lines · Jiri Pirko
Quoted 48 lines · Fri, Jan 22, 2016 at 03:51:55PM CET, [email protected] wrote:
Quoted 46 lines · Jiri Pirko
Quoted 32 lines · Thu, Jan 21, 2016 at 12:47:54PM CET, [email protected] wrote:

Add RTM_NEWADDR and RTM_DELADDR netlink messages to indicate interest in specific multicast hardware addresses. These messages are sent when addressed are added or deleted from the appropriate interface driver.

net/core/dev_addr_lists.c +10 -0
index c0548d2..a0ebadd 100644
@@ -12,6 +12,7 @@
 */
 
 #include <linux/netdevice.h>
+#include <net/netlink.h>
 #include <linux/rtnetlink.h>
 #include <linux/export.h>
 #include <linux/list.h>
@@ -661,6 +662,62 @@ out:
 }
 EXPORT_SYMBOL(dev_mc_add_excl);
 
+static int fill_addr(struct sk_buff *skb, struct net_device *dev,
+		     const unsigned char *addr, int type)
+{
+	struct nlmsghdr *nlh;
+	struct ifaddrmsg *ifm;
+
+	nlh = nlmsg_put(skb, 0, 0, type, sizeof(*ifm), 0);
+	if (nlh == NULL)
+		return -EMSGSIZE;

net/core/dev_addr_lists.c out: · line 683

Quoted 10 lines
net/core/dev_addr_lists.c +10 -0
+
+	ifm = nlmsg_data(nlh);
+	ifm->ifa_family = AF_UNSPEC;
+	ifm->ifa_prefixlen = 0;
+	ifm->ifa_flags = IFA_F_PERMANENT;
+	ifm->ifa_scope = RT_SCOPE_LINK;
+	ifm->ifa_index = dev->ifindex;
+	if (nla_put(skb, IFA_ADDRESS, dev->addr_len, addr))
+		goto nla_put_failure;
+	nlmsg_end(skb, nlh);

How can userspace tell if this is a multicast addr? Also, why not add notifications for unicast addrs so we handle both the same?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I am only using this RTM_NEW/DELADDR AF_UNSPEC message for multicast addresses. I understand that the unicast addresses are notified in the RTM_NEW/DELLINK.

dev->dev_addr is notified by that. UC addresses do not have any notification now.

It is the multicast MAC address (ie the multicast equivalent of the dev_addr) that we are notifying here right? Not L3 addresses.

I know :)

For example if kernel code calls dev_uc_add or dev_uc_del, thise addresses are not notified to the userspace. All I'm saying is, if you add notification for dev_mc_add and dev_mc_del, add it for uc variants as well. Then you have to distinguish mc and uc in the netlink message.

Quoted 3 lines

-pr

#6 lore raw
Quoted 60 lines · Jiri Pirko
Quoted 50 lines · Fri, Jan 22, 2016 at 04:49:36PM CET, [email protected] wrote:
Quoted 49 lines · Jiri Pirko
Quoted 48 lines · Fri, Jan 22, 2016 at 03:51:55PM CET, [email protected] wrote:
Quoted 46 lines · Jiri Pirko
Quoted 32 lines · Thu, Jan 21, 2016 at 12:47:54PM CET, [email protected] wrote:

Add RTM_NEWADDR and RTM_DELADDR netlink messages to indicate interest in specific multicast hardware addresses. These messages are sent when addressed are added or deleted from the appropriate interface driver.

net/core/dev_addr_lists.c +10 -0
index c0548d2..a0ebadd 100644
@@ -12,6 +12,7 @@
 */
 
 #include <linux/netdevice.h>
+#include <net/netlink.h>
 #include <linux/rtnetlink.h>
 #include <linux/export.h>
 #include <linux/list.h>
@@ -661,6 +662,62 @@ out:
 }
 EXPORT_SYMBOL(dev_mc_add_excl);
 
+static int fill_addr(struct sk_buff *skb, struct net_device *dev,
+		     const unsigned char *addr, int type)
+{
+	struct nlmsghdr *nlh;
+	struct ifaddrmsg *ifm;
+
+	nlh = nlmsg_put(skb, 0, 0, type, sizeof(*ifm), 0);
+	if (nlh == NULL)
+		return -EMSGSIZE;

net/core/dev_addr_lists.c out: · line 683

Quoted 10 lines
net/core/dev_addr_lists.c +10 -0
+
+	ifm = nlmsg_data(nlh);
+	ifm->ifa_family = AF_UNSPEC;
+	ifm->ifa_prefixlen = 0;
+	ifm->ifa_flags = IFA_F_PERMANENT;
+	ifm->ifa_scope = RT_SCOPE_LINK;
+	ifm->ifa_index = dev->ifindex;
+	if (nla_put(skb, IFA_ADDRESS, dev->addr_len, addr))
+		goto nla_put_failure;
+	nlmsg_end(skb, nlh);

How can userspace tell if this is a multicast addr? Also, why not add notifications for unicast addrs so we handle both the same?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I am only using this RTM_NEW/DELADDR AF_UNSPEC message for multicast addresses. I understand that the unicast addresses are notified in the RTM_NEW/DELLINK.

dev->dev_addr is notified by that. UC addresses do not have any notification now.

It is the multicast MAC address (ie the multicast equivalent of the dev_addr) that we are notifying here right? Not L3 addresses.

Quoted 6 lines

I know :)

For example if kernel code calls dev_uc_add or dev_uc_del, thise addresses are not notified to the userspace. All I'm saying is, if you add notification for dev_mc_add and dev_mc_del, add it for uc variants as well. Then you have to distinguish mc and uc in the netlink message.

One more Q on this. I can use IFA_ADDRESS for unicast addresses and IFA_MULTICAST for multicast addresses but are these distinctions actually required since the multicast nature of an address is clear from the address itself - at least it is for ethernet and ifininet?

-pr

#7 lore raw
Quoted 68 lines · Wed, Feb 03, 2016 at 03:14:41PM CET, [email protected] wrote:
Quoted 60 lines · Jiri Pirko
Quoted 50 lines · Fri, Jan 22, 2016 at 04:49:36PM CET, [email protected] wrote:
Quoted 49 lines · Jiri Pirko
Quoted 48 lines · Fri, Jan 22, 2016 at 03:51:55PM CET, [email protected] wrote:
Quoted 46 lines · Jiri Pirko
Quoted 32 lines · Thu, Jan 21, 2016 at 12:47:54PM CET, [email protected] wrote:

Add RTM_NEWADDR and RTM_DELADDR netlink messages to indicate interest in specific multicast hardware addresses. These messages are sent when addressed are added or deleted from the appropriate interface driver.

net/core/dev_addr_lists.c +10 -0
index c0548d2..a0ebadd 100644
@@ -12,6 +12,7 @@
 */
 
 #include <linux/netdevice.h>
+#include <net/netlink.h>
 #include <linux/rtnetlink.h>
 #include <linux/export.h>
 #include <linux/list.h>
@@ -661,6 +662,62 @@ out:
 }
 EXPORT_SYMBOL(dev_mc_add_excl);
 
+static int fill_addr(struct sk_buff *skb, struct net_device *dev,
+		     const unsigned char *addr, int type)
+{
+	struct nlmsghdr *nlh;
+	struct ifaddrmsg *ifm;
+
+	nlh = nlmsg_put(skb, 0, 0, type, sizeof(*ifm), 0);
+	if (nlh == NULL)
+		return -EMSGSIZE;

net/core/dev_addr_lists.c out: · line 683

Quoted 10 lines
net/core/dev_addr_lists.c +10 -0
+
+	ifm = nlmsg_data(nlh);
+	ifm->ifa_family = AF_UNSPEC;
+	ifm->ifa_prefixlen = 0;
+	ifm->ifa_flags = IFA_F_PERMANENT;
+	ifm->ifa_scope = RT_SCOPE_LINK;
+	ifm->ifa_index = dev->ifindex;
+	if (nla_put(skb, IFA_ADDRESS, dev->addr_len, addr))
+		goto nla_put_failure;
+	nlmsg_end(skb, nlh);

How can userspace tell if this is a multicast addr? Also, why not add notifications for unicast addrs so we handle both the same?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I am only using this RTM_NEW/DELADDR AF_UNSPEC message for multicast addresses. I understand that the unicast addresses are notified in the RTM_NEW/DELLINK.

dev->dev_addr is notified by that. UC addresses do not have any notification now.

It is the multicast MAC address (ie the multicast equivalent of the dev_addr) that we are notifying here right? Not L3 addresses.

Quoted 6 lines

I know :)

For example if kernel code calls dev_uc_add or dev_uc_del, thise addresses are not notified to the userspace. All I'm saying is, if you add notification for dev_mc_add and dev_mc_del, add it for uc variants as well. Then you have to distinguish mc and uc in the netlink message.

Quoted 4 lines

One more Q on this. I can use IFA_ADDRESS for unicast addresses and IFA_MULTICAST for multicast addresses but are these distinctions actually required since the multicast nature of an address is clear from the address itself - at least it is for ethernet and ifininet?

Please mark them correctly. Thanks.

#8 lore raw
In reply to Patrick Ruddy
Quoted 4 lines · On Wed, 3 Feb 2016 06:14:41 -0800 Patrick Ruddy <[email protected]> wrote:

One more Q on this. I can use IFA_ADDRESS for unicast addresses and IFA_MULTICAST for multicast addresses but are these distinctions actually required since the multicast nature of an address is clear from the address itself - at least it is for ethernet and ifininet?

The flags on L2 addresses should be analagous to what happens with L3 (IP) addresses.