Skip to content

Commit 35c5845

Browse files
committed
net: Add helpers for 64-bit aligning netlink attributes.
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>
1 parent 1840284 commit 35c5845

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

include/net/netlink.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,43 @@ static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
12301230
return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
12311231
}
12321232

1233+
/**
1234+
* nla_align_64bit - 64-bit align the nla_data() of next attribute
1235+
* @skb: socket buffer the message is stored in
1236+
* @padattr: attribute type for the padding
1237+
*
1238+
* Conditionally emit a padding netlink attribute in order to make
1239+
* the next attribute we emit have a 64-bit aligned nla_data() area.
1240+
* This will only be done in architectures which do not have
1241+
* HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
1242+
*
1243+
* Returns zero on success or a negative error code.
1244+
*/
1245+
static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
1246+
{
1247+
#ifndef HAVE_EFFICIENT_UNALIGNED_ACCESS
1248+
if (IS_ALIGNED((unsigned long)skb->data, 8)) {
1249+
struct nlattr *attr = nla_reserve(skb, padattr, 0);
1250+
if (!attr)
1251+
return -EMSGSIZE;
1252+
}
1253+
#endif
1254+
return 0;
1255+
}
1256+
1257+
/**
1258+
* nla_total_size_64bit - total length of attribute including padding
1259+
* @payload: length of payload
1260+
*/
1261+
static inline int nla_total_size_64bit(int payload)
1262+
{
1263+
return NLA_ALIGN(nla_attr_size(payload))
1264+
#ifndef HAVE_EFFICIENT_UNALIGNED_ACCESS
1265+
+ NLA_ALIGN(nla_attr_size(0))
1266+
#endif
1267+
;
1268+
}
1269+
12331270
/**
12341271
* nla_for_each_attr - iterate over a stream of attributes
12351272
* @pos: loop counter, set to current attribute

net/core/rtnetlink.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -878,10 +878,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev,
878878
+ nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
879879
+ nla_total_size(sizeof(struct rtnl_link_ifmap))
880880
+ nla_total_size(sizeof(struct rtnl_link_stats))
881-
#ifndef HAVE_EFFICIENT_UNALIGNED_ACCESS
882-
+ nla_total_size(0) /* IFLA_PAD */
883-
#endif
884-
+ nla_total_size(sizeof(struct rtnl_link_stats64))
881+
+ nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
885882
+ nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
886883
+ nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
887884
+ nla_total_size(4) /* IFLA_TXQLEN */
@@ -1054,22 +1051,11 @@ static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
10541051
{
10551052
struct rtnl_link_stats64 *sp;
10561053
struct nlattr *attr;
1054+
int err;
10571055

1058-
#ifndef HAVE_EFFICIENT_UNALIGNED_ACCESS
1059-
/* IF necessary, add a zero length NOP attribute so that the
1060-
* nla_data() of the IFLA_STATS64 will be 64-bit aligned.
1061-
*
1062-
* The nlattr header is 4 bytes in size, that's why we test
1063-
* if the skb->data _is_ aligned. This NOP attribute, plus
1064-
* nlattr header for IFLA_STATS64, will make nla_data() 8-byte
1065-
* aligned.
1066-
*/
1067-
if (IS_ALIGNED((unsigned long)skb->data, 8)) {
1068-
attr = nla_reserve(skb, IFLA_PAD, 0);
1069-
if (!attr)
1070-
return -EMSGSIZE;
1071-
}
1072-
#endif
1056+
err = nla_align_64bit(skb, IFLA_PAD);
1057+
if (err)
1058+
return err;
10731059

10741060
attr = nla_reserve(skb, IFLA_STATS64,
10751061
sizeof(struct rtnl_link_stats64));

0 commit comments

Comments
 (0)