1 #ifndef __NET_NETLINK_WRAPPER_H
2 #define __NET_NETLINK_WRAPPER_H 1
4 #include <linux/version.h>
5 #include_next <net/netlink.h>
7 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
8 /* Before v2.6.29, a NLA_NESTED attribute, if it was present, was not allowed
9 * to be empty. However, OVS depends on the ability to accept empty
10 * attributes. For example, a present but empty OVS_FLOW_ATTR_ACTIONS on
11 * OVS_FLOW_CMD_SET replaces the existing set of actions by an empty "drop"
12 * action, whereas a missing OVS_FLOW_ATTR_ACTIONS leaves the existing
13 * actions, if any, unchanged.
15 * NLA_NESTED is different from NLA_UNSPEC in only two ways:
17 * - If the size of the nested attributes is zero, no further size checks
20 * - If the size of the nested attributes is not zero and no length
21 * parameter is specified the minimum size of nested attributes is
24 * nla_parse_nested() validates that there is at least enough space for
25 * NLA_HDRLEN, so neither of these conditions are important, and we might
26 * as well use NLA_UNSPEC with old kernels.
29 #define NLA_NESTED NLA_UNSPEC
32 #ifndef HAVE_NLA_GET_BE16
34 * nla_get_be16 - return payload of __be16 attribute
35 * @nla: __be16 netlink attribute
37 static inline __be16 nla_get_be16(const struct nlattr *nla)
39 return *(__be16 *) nla_data(nla);
41 #endif /* !HAVE_NLA_GET_BE16 */
43 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
45 * nla_get_be32 - return payload of __be32 attribute
46 * @nla: __be32 netlink attribute
48 static inline __be32 nla_get_be32(const struct nlattr *nla)
50 return *(__be32 *) nla_data(nla);
54 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
55 /* These functions' nlattr source arguments weren't "const" before 2.6.29, so
56 * cast their arguments to the non-"const" versions. Using macros for this
57 * isn't exactly a brilliant idea, but it seems less error-prone than copying
58 * the definitions of all umpteen functions. */
59 #define nla_get_u64(nla) (nla_get_u64) ((struct nlattr *) (nla))
60 #define nla_get_u32(nla) (nla_get_u32) ((struct nlattr *) (nla))
61 #define nla_get_u16(nla) (nla_get_u16) ((struct nlattr *) (nla))
62 #define nla_get_u8(nla) (nla_get_u8) ((struct nlattr *) (nla))
63 /* nla_get_be64 is handled separately below. */
64 #define nla_get_be32(nla) (nla_get_be32) ((struct nlattr *) (nla))
65 #define nla_get_be16(nla) (nla_get_be16) ((struct nlattr *) (nla))
66 #define nla_get_be8(nla) (nla_get_be8) ((struct nlattr *) (nla))
67 #define nla_get_flag(nla) (nla_get_flag) ((struct nlattr *) (nla))
68 #define nla_get_msecs(nla) (nla_get_msecs)((struct nlattr *) (nla))
69 #define nla_memcpy(dst, src, count) \
70 (nla_memcpy)(dst, (struct nlattr *)(src), count)
73 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)
74 /* This function was introduced in 2.6.31, but initially it performed an
75 * unaligned access, so we replace it up to 2.6.34 where it was fixed. */
76 #define nla_get_be64 rpl_nla_get_be64
77 static inline __be64 nla_get_be64(const struct nlattr *nla)
81 /* The additional cast is necessary because */
82 nla_memcpy(&tmp, (struct nlattr *) nla, sizeof(tmp));
88 #ifndef HAVE_NLA_PUT_BE16
89 static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
91 return nla_put(skb, attrtype, sizeof(__be16), &value);
95 #ifndef HAVE_NLA_PUT_BE32
96 static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
98 return nla_put(skb, attrtype, sizeof(__be32), &value);
102 #ifndef HAVE_NLA_PUT_BE64
103 static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value)
105 return nla_put(skb, attrtype, sizeof(__be64), &value);
109 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
111 * nla_type - attribute type
112 * @nla: netlink attribute
114 static inline int nla_type(const struct nlattr *nla)
116 return nla->nla_type & NLA_TYPE_MASK;
120 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
121 #define nla_parse_nested(tb, maxtype, nla, policy) \
122 nla_parse_nested(tb, maxtype, (struct nlattr *)(nla), \
123 (struct nla_policy *)(policy))
124 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
125 #define nla_parse_nested(tb, maxtype, nla, policy) \
126 nla_parse_nested(tb, maxtype, (struct nlattr *)(nla), policy)
129 #ifndef nla_for_each_nested
130 #define nla_for_each_nested(pos, nla, rem) \
131 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
134 #ifndef HAVE_NLA_FIND_NESTED
135 static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
137 return nla_find(nla_data(nla), nla_len(nla), attrtype);
141 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
143 * nlmsg_report - need to report back to application?
144 * @nlh: netlink message header
146 * Returns 1 if a report back to the application is requested.
148 static inline int nlmsg_report(const struct nlmsghdr *nlh)
150 return !!(nlh->nlmsg_flags & NLM_F_ECHO);
153 extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb,
154 u32 portid, unsigned int group, int report,
156 #endif /* linux kernel < 2.6.19 */
158 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
159 /* Before 2.6.19 the 'flags' parameter was missing, so replace it. We have to
160 * #include <net/genetlink.h> first because the 2.6.18 version of that header
161 * has an inline call to nlmsg_multicast() without, of course, any 'flags'
163 #define nlmsg_multicast rpl_nlmsg_multicast
164 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
165 u32 portid, unsigned int group, gfp_t flags)
169 NETLINK_CB(skb).dst_group = group;
171 err = netlink_broadcast(sk, skb, portid, group, flags);
177 #endif /* linux kernel < 2.6.19 */
179 #endif /* net/netlink.h */