net: Add helpers for 64-bit aligning netlink attributes.
authorDavid S. Miller <davem@davemloft.net>
Tue, 19 Apr 2016 23:49:29 +0000 (19:49 -0400)
committerDavid S. Miller <davem@davemloft.net>
Tue, 19 Apr 2016 23:49:29 +0000 (19:49 -0400)
Suggested-by: Eric Dumazet <eric.dumazet@gmail.com>
Suggested-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/netlink.h
net/core/rtnetlink.c

index 0e31727517556dabdc929c496e9a5e6c995de068..e644b3489acf1f6b1022232aba7b25c7154632c3 100644 (file)
@@ -1230,6 +1230,43 @@ static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
        return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
 }
 
+/**
+ * nla_align_64bit - 64-bit align the nla_data() of next attribute
+ * @skb: socket buffer the message is stored in
+ * @padattr: attribute type for the padding
+ *
+ * Conditionally emit a padding netlink attribute in order to make
+ * the next attribute we emit have a 64-bit aligned nla_data() area.
+ * This will only be done in architectures which do not have
+ * HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
+ *
+ * Returns zero on success or a negative error code.
+ */
+static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
+{
+#ifndef HAVE_EFFICIENT_UNALIGNED_ACCESS
+       if (IS_ALIGNED((unsigned long)skb->data, 8)) {
+               struct nlattr *attr = nla_reserve(skb, padattr, 0);
+               if (!attr)
+                       return -EMSGSIZE;
+       }
+#endif
+       return 0;
+}
+
+/**
+ * nla_total_size_64bit - total length of attribute including padding
+ * @payload: length of payload
+ */
+static inline int nla_total_size_64bit(int payload)
+{
+       return NLA_ALIGN(nla_attr_size(payload))
+#ifndef HAVE_EFFICIENT_UNALIGNED_ACCESS
+               + NLA_ALIGN(nla_attr_size(0))
+#endif
+               ;
+}
+
 /**
  * nla_for_each_attr - iterate over a stream of attributes
  * @pos: loop counter, set to current attribute
index 198ca2c995105bf14401b694e39fd8ce18a13974..d3694a13c85aafde74bd2c0616e6668fe3dc7c41 100644 (file)
@@ -878,10 +878,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev,
               + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
               + nla_total_size(sizeof(struct rtnl_link_ifmap))
               + nla_total_size(sizeof(struct rtnl_link_stats))
-#ifndef HAVE_EFFICIENT_UNALIGNED_ACCESS
-              + nla_total_size(0) /* IFLA_PAD */
-#endif
-              + nla_total_size(sizeof(struct rtnl_link_stats64))
+              + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
               + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
               + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
               + nla_total_size(4) /* IFLA_TXQLEN */
@@ -1054,22 +1051,11 @@ static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
 {
        struct rtnl_link_stats64 *sp;
        struct nlattr *attr;
+       int err;
 
-#ifndef HAVE_EFFICIENT_UNALIGNED_ACCESS
-       /* IF necessary, add a zero length NOP attribute so that the
-        * nla_data() of the IFLA_STATS64 will be 64-bit aligned.
-        *
-        * The nlattr header is 4 bytes in size, that's why we test
-        * if the skb->data _is_ aligned.  This NOP attribute, plus
-        * nlattr header for IFLA_STATS64, will make nla_data() 8-byte
-        * aligned.
-        */
-       if (IS_ALIGNED((unsigned long)skb->data, 8)) {
-               attr = nla_reserve(skb, IFLA_PAD, 0);
-               if (!attr)
-                       return -EMSGSIZE;
-       }
-#endif
+       err = nla_align_64bit(skb, IFLA_PAD);
+       if (err)
+               return err;
 
        attr = nla_reserve(skb, IFLA_STATS64,
                           sizeof(struct rtnl_link_stats64));