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