netlink: implement nla_put_in_addr and nla_put_in6_addr
authorJiri Benc <jbenc@redhat.com>
Sun, 29 Mar 2015 14:59:25 +0000 (16:59 +0200)
committerDavid S. Miller <davem@davemloft.net>
Tue, 31 Mar 2015 17:58:35 +0000 (13:58 -0400)
IP addresses are often stored in netlink attributes. Add generic functions
to do that.

For nla_put_in_addr, it would be nicer to pass struct in_addr but this is
not used universally throughout the kernel, in way too many places __be32 is
used to store IPv4 address.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
29 files changed:
drivers/net/vxlan.c
include/linux/netfilter/ipset/ip_set.h
include/net/netlink.h
net/ipv4/devinet.c
net/ipv4/fib_rules.c
net/ipv4/fib_semantics.c
net/ipv4/ip_gre.c
net/ipv4/ip_vti.c
net/ipv4/ipip.c
net/ipv4/ipmr.c
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
net/ipv4/route.c
net/ipv4/tcp_metrics.c
net/ipv6/addrconf.c
net/ipv6/addrlabel.c
net/ipv6/fib6_rules.c
net/ipv6/ip6_gre.c
net/ipv6/ip6_tunnel.c
net/ipv6/ip6_vti.c
net/ipv6/ip6mr.c
net/ipv6/ndisc.c
net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
net/ipv6/route.c
net/ipv6/sit.c
net/l2tp/l2tp_netlink.c
net/netlabel/netlabel_mgmt.c
net/netlabel/netlabel_unlabeled.c
net/openvswitch/flow_netlink.c
net/wireless/nl80211.c

index 1c80b67..86f085f 100644 (file)
@@ -187,9 +187,9 @@ static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
                              const union vxlan_addr *ip)
 {
        if (ip->sa.sa_family == AF_INET6)
-               return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
+               return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
        else
-               return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
+               return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
 }
 
 #else /* !CONFIG_IPV6 */
@@ -226,7 +226,7 @@ static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
                              const union vxlan_addr *ip)
 {
-       return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
+       return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
 }
 #endif
 
@@ -2807,13 +2807,13 @@ static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
 
        if (!vxlan_addr_any(&dst->remote_ip)) {
                if (dst->remote_ip.sa.sa_family == AF_INET) {
-                       if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
-                                        dst->remote_ip.sin.sin_addr.s_addr))
+                       if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
+                                           dst->remote_ip.sin.sin_addr.s_addr))
                                goto nla_put_failure;
 #if IS_ENABLED(CONFIG_IPV6)
                } else {
-                       if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
-                                   &dst->remote_ip.sin6.sin6_addr))
+                       if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
+                                            &dst->remote_ip.sin6.sin6_addr))
                                goto nla_put_failure;
 #endif
                }
@@ -2824,13 +2824,13 @@ static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
 
        if (!vxlan_addr_any(&vxlan->saddr)) {
                if (vxlan->saddr.sa.sa_family == AF_INET) {
-                       if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
-                                        vxlan->saddr.sin.sin_addr.s_addr))
+                       if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
+                                           vxlan->saddr.sin.sin_addr.s_addr))
                                goto nla_put_failure;
 #if IS_ENABLED(CONFIG_IPV6)
                } else {
-                       if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
-                                   &vxlan->saddr.sin6.sin6_addr))
+                       if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
+                                            &vxlan->saddr.sin6.sin6_addr))
                                goto nla_put_failure;
 #endif
                }
index f1606fa..34b1723 100644 (file)
@@ -483,7 +483,7 @@ static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
 
        if (!__nested)
                return -EMSGSIZE;
-       ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
+       ret = nla_put_in_addr(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
        if (!ret)
                ipset_nest_end(skb, __nested);
        return ret;
@@ -497,8 +497,7 @@ static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
 
        if (!__nested)
                return -EMSGSIZE;
-       ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6,
-                     sizeof(struct in6_addr), ipaddrptr);
+       ret = nla_put_in6_addr(skb, IPSET_ATTR_IPADDR_IPV6, ipaddrptr);
        if (!ret)
                ipset_nest_end(skb, __nested);
        return ret;
index e010ee8..17fc76e 100644 (file)
@@ -4,6 +4,7 @@
 #include <linux/types.h>
 #include <linux/netlink.h>
 #include <linux/jiffies.h>
+#include <linux/in6.h>
 
 /* ========================================================================
  *         Netlink Messages and Attributes Interface (As Seen On TV)
  *   nla_put_string(skb, type, str)    add string attribute to skb
  *   nla_put_flag(skb, type)           add flag attribute to skb
  *   nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
+ *   nla_put_in_addr(skb, type, addr)  add IPv4 address attribute to skb
+ *   nla_put_in6_addr(skb, type, addr) add IPv6 address attribute to skb
  *
  * Nested Attributes Construction:
  *   nla_nest_start(skb, type)         start a nested attribute
@@ -956,6 +959,32 @@ static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
        return nla_put(skb, attrtype, sizeof(u64), &tmp);
 }
 
+/**
+ * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
+ * buffer
+ * @skb: socket buffer to add attribute to
+ * @attrtype: attribute type
+ * @addr: IPv4 address
+ */
+static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
+                                 __be32 addr)
+{
+       return nla_put_be32(skb, attrtype, addr);
+}
+
+/**
+ * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
+ * buffer
+ * @skb: socket buffer to add attribute to
+ * @attrtype: attribute type
+ * @addr: IPv6 address
+ */
+static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
+                                  const struct in6_addr *addr)
+{
+       return nla_put(skb, attrtype, sizeof(*addr), addr);
+}
+
 /**
  * nla_get_u32 - return payload of u32 attribute
  * @nla: u32 netlink attribute
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
diff --cc net/ipv4/ipip.c
Simple merge
diff --cc net/ipv4/ipmr.c
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
diff --cc net/ipv6/sit.c
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge