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