aa7c09c036de3ee5f30ec177f57833aef4023fba
[sliver-openvswitch.git] / datapath / linux-2.6 / compat-2.6 / include / net / netlink.h
1 #ifndef __NET_NETLINK_WRAPPER_H
2 #define __NET_NETLINK_WRAPPER_H 1
3
4 #include <linux/version.h>
5 #include_next <net/netlink.h>
6
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 ODP_FLOW_ATTR_ACTIONS on
11  * ODP_FLOW_CMD_SET replaces the existing set of actions by an empty "drop"
12  * action, whereas a missing ODP_FLOW_ATTR_ACTIONS leaves the existing
13  * actions, if any, unchanged.
14  *
15  * NLA_NESTED is different from NLA_UNSPEC in only two ways:
16  *
17  * - If the size of the nested attributes is zero, no further size checks
18  *   are performed.
19  *
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
22  *   NLA_HDRLEN.
23  *
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.
27  */
28 #undef NLA_NESTED
29 #define NLA_NESTED NLA_UNSPEC
30 #endif
31
32 #ifndef HAVE_NLA_NUL_STRING
33 static inline int VERIFY_NUL_STRING(struct nlattr *attr, int maxlen)
34 {
35         char *s;
36         int len;
37         if (!attr)
38                 return 0;
39
40         len = nla_len(attr);
41         if (len >= maxlen)
42                 return -EINVAL;
43
44         s = nla_data(attr);
45         if (s[len - 1] != '\0')
46                 return -EINVAL;
47
48         return 0;
49 }
50 #else
51 static inline int VERIFY_NUL_STRING(struct nlattr *attr, int maxlen)
52 {
53         return 0;
54 }
55 #endif  /* !HAVE_NLA_NUL_STRING */
56
57 #ifndef NLA_PUT_BE16
58 #define NLA_PUT_BE16(skb, attrtype, value) \
59         NLA_PUT_TYPE(skb, __be16, attrtype, value)
60 #endif  /* !NLA_PUT_BE16 */
61
62 #ifndef NLA_PUT_BE32
63 #define NLA_PUT_BE32(skb, attrtype, value) \
64         NLA_PUT_TYPE(skb, __be32, attrtype, value)
65 #endif  /* !NLA_PUT_BE32 */
66
67 #ifndef NLA_PUT_BE64
68 #define NLA_PUT_BE64(skb, attrtype, value) \
69         NLA_PUT_TYPE(skb, __be64, attrtype, value)
70 #endif  /* !NLA_PUT_BE64 */
71
72 #ifndef HAVE_NLA_GET_BE16
73 /**
74  * nla_get_be16 - return payload of __be16 attribute
75  * @nla: __be16 netlink attribute
76  */
77 static inline __be16 nla_get_be16(const struct nlattr *nla)
78 {
79         return *(__be16 *) nla_data(nla);
80 }
81 #endif  /* !HAVE_NLA_GET_BE16 */
82
83 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
84 /**
85  * nla_get_be32 - return payload of __be32 attribute
86  * @nla: __be32 netlink attribute
87  */
88 static inline __be32 nla_get_be32(const struct nlattr *nla)
89 {
90         return *(__be32 *) nla_data(nla);
91 }
92 #endif
93
94 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
95 /* These functions' nlattr source arguments weren't "const" before 2.6.29, so
96  * cast their arguments to the non-"const" versions.  Using macros for this
97  * isn't exactly a brilliant idea, but it seems less error-prone than copying
98  * the definitions of all umpteen functions. */
99 #define nla_get_u64(nla)   (nla_get_u64)  ((struct nlattr *) (nla))
100 #define nla_get_u32(nla)   (nla_get_u32)  ((struct nlattr *) (nla))
101 #define nla_get_u16(nla)   (nla_get_u16)  ((struct nlattr *) (nla))
102 #define nla_get_u8(nla)    (nla_get_u8)   ((struct nlattr *) (nla))
103 /* nla_get_be64 is handled separately below. */
104 #define nla_get_be32(nla)  (nla_get_be32) ((struct nlattr *) (nla))
105 #define nla_get_be16(nla)  (nla_get_be16) ((struct nlattr *) (nla))
106 #define nla_get_be8(nla)   (nla_get_be8)  ((struct nlattr *) (nla))
107 #define nla_get_flag(nla)  (nla_get_flag) ((struct nlattr *) (nla))
108 #define nla_get_msecs(nla) (nla_get_msecs)((struct nlattr *) (nla))
109 #define nla_memcpy(dst, src, count) \
110         (nla_memcpy)(dst, (struct nlattr *)(src), count)
111 #endif
112
113 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)
114 /* This function was introduced in 2.6.31, but initially it performed an
115  * unaligned access, so we replace it up to 2.6.34 where it was fixed.  */
116 #define nla_get_be64 rpl_nla_get_be64
117 static inline __be64 nla_get_be64(const struct nlattr *nla)
118 {
119         __be64 tmp;
120
121         /* The additional cast is necessary because  */
122         nla_memcpy(&tmp, (struct nlattr *) nla, sizeof(tmp));
123
124         return tmp;
125 }
126 #endif
127
128 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
129 /**
130  * nla_type - attribute type
131  * @nla: netlink attribute
132  */
133 static inline int nla_type(const struct nlattr *nla)
134 {
135         return nla->nla_type & NLA_TYPE_MASK;
136 }
137 #endif
138
139 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
140 #define nla_parse_nested(tb, maxtype, nla, policy) \
141         nla_parse_nested(tb, maxtype, (struct nlattr *)(nla), (struct nla_policy *)(policy))
142 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
143 #define nla_parse_nested(tb, maxtype, nla, policy) \
144         nla_parse_nested(tb, maxtype, (struct nlattr *)(nla), policy)
145 #endif
146
147 #ifndef nla_for_each_nested
148 #define nla_for_each_nested(pos, nla, rem) \
149         nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
150 #endif
151
152 #ifndef HAVE_NLA_FIND_NESTED
153 static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
154 {
155         return nla_find(nla_data(nla), nla_len(nla), attrtype);
156 }
157 #endif
158
159 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
160 /**
161  * nlmsg_report - need to report back to application?
162  * @nlh: netlink message header
163  *
164  * Returns 1 if a report back to the application is requested.
165  */
166 static inline int nlmsg_report(const struct nlmsghdr *nlh)
167 {
168         return !!(nlh->nlmsg_flags & NLM_F_ECHO);
169 }
170
171 extern int              nlmsg_notify(struct sock *sk, struct sk_buff *skb,
172                                      u32 pid, unsigned int group, int report,
173                                      gfp_t flags);
174 #endif  /* linux kernel < 2.6.19 */
175
176 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
177 /* Before 2.6.19 the 'flags' parameter was missing, so replace it.  We have to
178  * #include <net/genetlink.h> first because the 2.6.18 version of that header
179  * has an inline call to nlmsg_multicast() without, of course, any 'flags'
180  * argument. */
181 #define nlmsg_multicast rpl_nlmsg_multicast
182 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
183                                   u32 pid, unsigned int group, gfp_t flags)
184 {
185         int err;
186
187         NETLINK_CB(skb).dst_group = group;
188
189         err = netlink_broadcast(sk, skb, pid, group, flags);
190         if (err > 0)
191                 err = 0;
192
193         return err;
194 }
195 #endif  /* linux kernel < 2.6.19 */
196
197 #endif /* net/netlink.h */