1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sat, 14 May 2016 16:40:16 +0200
3 Subject: [PATCH] header: backport nla_put_u64_64bit and nla_put_64bit
5 Signed-off-by: Felix Fietkau <nbd@nbd.name>
8 --- a/backport-include/net/netlink.h
9 +++ b/backport-include/net/netlink.h
10 @@ -189,4 +189,148 @@ static inline __le64 nla_get_le64(const
15 +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
18 + * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
19 + * @skb: socket buffer the message is stored in
21 + * Return true if padding is needed to align the next attribute (nla_data()) to
22 + * a 64-bit aligned area.
24 +static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
26 +#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
27 + /* The nlattr header is 4 bytes in size, that's why we test
28 + * if the skb->data _is_ aligned. A NOP attribute, plus
29 + * nlattr header for next attribute, will make nla_data()
32 + if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
39 + * nla_align_64bit - 64-bit align the nla_data() of next attribute
40 + * @skb: socket buffer the message is stored in
41 + * @padattr: attribute type for the padding
43 + * Conditionally emit a padding netlink attribute in order to make
44 + * the next attribute we emit have a 64-bit aligned nla_data() area.
45 + * This will only be done in architectures which do not have
46 + * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
48 + * Returns zero on success or a negative error code.
50 +static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
52 + if (nla_need_padding_for_64bit(skb) &&
53 + !nla_reserve(skb, padattr, 0))
60 + * __nla_reserve_64bit - reserve room for attribute on the skb and align it
61 + * @skb: socket buffer to reserve room on
62 + * @attrtype: attribute type
63 + * @attrlen: length of attribute payload
64 + * @padattr: attribute type for the padding
66 + * Adds a netlink attribute header to a socket buffer and reserves
67 + * room for the payload but does not copy it. It also ensure that this
68 + * attribute will have a 64-bit aligned nla_data() area.
70 + * The caller is responsible to ensure that the skb provides enough
71 + * tailroom for the attribute header and payload.
73 +static inline struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
74 + int attrlen, int padattr)
76 + if (nla_need_padding_for_64bit(skb))
77 + nla_align_64bit(skb, padattr);
79 + return __nla_reserve(skb, attrtype, attrlen);
83 + * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
84 + * @skb: socket buffer to add attribute to
85 + * @attrtype: attribute type
86 + * @attrlen: length of attribute payload
87 + * @data: head of attribute payload
88 + * @padattr: attribute type for the padding
90 + * The caller is responsible to ensure that the skb provides enough
91 + * tailroom for the attribute header and payload.
93 +static inline void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
94 + const void *data, int padattr)
98 + nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
99 + memcpy(nla_data(nla), data, attrlen);
103 + * nla_total_size_64bit - total length of attribute including padding
104 + * @payload: length of payload
106 +static inline int nla_total_size_64bit(int payload)
108 + return NLA_ALIGN(nla_attr_size(payload))
109 +#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
110 + + NLA_ALIGN(nla_attr_size(0))
116 + * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
117 + * @skb: socket buffer to add attribute to
118 + * @attrtype: attribute type
119 + * @attrlen: length of attribute payload
120 + * @data: head of attribute payload
121 + * @padattr: attribute type for the padding
123 + * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
124 + * the attribute header and payload.
126 +static inline int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
127 + const void *data, int padattr)
131 + if (nla_need_padding_for_64bit(skb))
132 + len = nla_total_size_64bit(attrlen);
134 + len = nla_total_size(attrlen);
135 + if (unlikely(skb_tailroom(skb) < len))
138 + __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
143 + * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
144 + * @skb: socket buffer to add attribute to
145 + * @attrtype: attribute type
146 + * @value: numeric value
147 + * @padattr: attribute type for the padding
149 +static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
150 + u64 value, int padattr)
152 + return nla_put_64bit(skb, attrtype, sizeof(u64), &value, padattr);
156 +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0) */
158 #endif /* __BACKPORT_NET_NETLINK_H */