af7d5fd492ff8017336d31408c41087797f05e26
[sliver-openvswitch.git] / datapath / linux / compat / include / net / genetlink.h
1 #ifndef __NET_GENERIC_NETLINK_WRAPPER_H
2 #define __NET_GENERIC_NETLINK_WRAPPER_H 1
3
4 #include <linux/version.h>
5 #include <linux/netlink.h>
6 #include <net/net_namespace.h>
7
8 /* Very special super-nasty workaround here:
9  *
10  * Before 2.6.19, nlmsg_multicast() lacked a 'flags' parameter.  We work
11  * around that in our <net/netlink.h> replacement, so that nlmsg_multicast
12  * is a macro that expands to rpl_nlmsg_multicast, which in turn has the
13  * 'flags' parameter.
14  *
15  * However, also before 2.6.19, <net/genetlink.h> contains an inline definition
16  * of genlmsg_multicast() that, of course, calls it without the 'flags'
17  * parameter.  This causes a build failure.
18  *
19  * This works around the problem by temporarily renaming both nlmsg_multicast
20  * and genlmsg_multicast with a "busted_" prefix.  (Nothing actually defines
21  * busted_nlmsg_multicast(), so if anything actually tries to call it, then
22  * we'll get a link error.)
23  */
24 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
25 #undef nlmsg_multicast
26 #define nlmsg_multicast busted_nlmsg_multicast
27 #define genlmsg_multicast busted_genlmsg_multicast
28 extern int busted_nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
29                                   u32 pid, unsigned int group);
30 #endif  /* linux kernel < v2.6.19 */
31
32 #include_next <net/genetlink.h>
33
34 /* Drop the "busted_" prefix described above. */
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
36 #undef nlmsg_multicast
37 #undef genlmsg_multicast
38 #define nlmsg_multicast rpl_nlmsg_multicast
39 #endif  /* linux kernel < v2.6.19 */
40
41 #include <net/net_namespace.h>
42
43 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
44
45 #include <linux/genetlink.h>
46
47 /**
48  * struct genl_multicast_group - generic netlink multicast group
49  * @name: name of the multicast group, names are per-family
50  * @id: multicast group ID, assigned by the core, to use with
51  *        genlmsg_multicast().
52  * @list: list entry for linking
53  * @family: pointer to family, need not be set before registering
54  */
55 struct genl_multicast_group {
56         struct genl_family  *family;    /* private */
57         struct list_head        list;      /* private */
58         char name[GENL_NAMSIZ];
59         u32     id;
60 };
61
62 int genl_register_mc_group(struct genl_family *family,
63                 struct genl_multicast_group *grp);
64 #endif /* linux kernel < 2.6.23 */
65
66
67 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
68 /**
69  * genlmsg_msg_size - length of genetlink message not including padding
70  * @payload: length of message payload
71  */
72 static inline int genlmsg_msg_size(int payload)
73 {
74         return GENL_HDRLEN + payload;
75 }
76
77 /**
78  * genlmsg_total_size - length of genetlink message including padding
79  * @payload: length of message payload
80  */
81 static inline int genlmsg_total_size(int payload)
82 {
83         return NLMSG_ALIGN(genlmsg_msg_size(payload));
84 }
85
86 #define genlmsg_multicast(s, p, g, f) \
87                 genlmsg_multicast_flags((s), (p), (g), (f))
88
89 static inline int genlmsg_multicast_flags(struct sk_buff *skb, u32 pid,
90                 unsigned int group, gfp_t flags)
91 {
92         int err;
93
94         NETLINK_CB(skb).dst_group = group;
95
96         err = netlink_broadcast(genl_sock, skb, pid, group, flags);
97         if (err > 0)
98                 err = 0;
99
100         return err;
101 }
102 #endif /* linux kernel < 2.6.19 */
103
104 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
105 #define genlmsg_multicast_netns(net, skb, pid, grp, flags) \
106                 genlmsg_multicast(skb, pid, grp, flags)
107 #endif
108
109 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
110
111 #define genlmsg_put(skb, p, seq, fam, flg, c) \
112         genlmsg_put((skb), (p), (seq), (fam)->id, (fam)->hdrsize, \
113                         (flg), (c), (fam)->version)
114
115 /**
116  * genlmsg_put_reply - Add generic netlink header to a reply message
117  * @skb: socket buffer holding the message
118  * @info: receiver info
119  * @family: generic netlink family
120  * @flags: netlink message flags
121  * @cmd: generic netlink command
122  *
123  * Returns pointer to user specific header
124  */
125 static inline void *genlmsg_put_reply(struct sk_buff *skb,
126                         struct genl_info *info, struct genl_family *family,
127                         int flags, u8 cmd)
128 {
129         return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
130                                 flags, cmd);
131 }
132
133 /**
134  * genlmsg_reply - reply to a request
135  * @skb: netlink message to be sent back
136  * @info: receiver information
137  */
138 static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
139 {
140         return genlmsg_unicast(skb, info->snd_pid);
141 }
142
143 /**
144  * genlmsg_new - Allocate a new generic netlink message
145  * @payload: size of the message payload
146  * @flags: the type of memory to allocate.
147  */
148 static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
149 {
150         return nlmsg_new(genlmsg_total_size(payload), flags);
151 }
152 #endif /* linux kernel < 2.6.20 */
153
154 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)
155 int genl_register_family_with_ops(struct genl_family *family,
156         struct genl_ops *ops, size_t n_ops);
157 #endif
158
159 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
160 #define genl_notify(skb, net, pid, group, nlh, flags) \
161         genl_notify(skb, pid, group, nlh, flags)
162 #endif
163 extern void genl_notify(struct sk_buff *skb, struct net *net, u32 pid,
164                         u32 group, struct nlmsghdr *nlh, gfp_t flags);
165
166 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24) && \
167     LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
168 static inline struct net *genl_info_net(struct genl_info *info)
169 {
170         return &init_net;
171 }
172 #endif
173
174 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
175 #define genlmsg_unicast(ignore_net, skb, pid)   genlmsg_unicast(skb, pid)
176 #endif
177 #endif /* genetlink.h */