Commit | Line | Data |
---|---|---|
482a8524 TG |
1 | #ifndef __NET_GENERIC_NETLINK_H |
2 | #define __NET_GENERIC_NETLINK_H | |
3 | ||
4 | #include <linux/genetlink.h> | |
5 | #include <net/netlink.h> | |
134e6375 | 6 | #include <net/net_namespace.h> |
482a8524 | 7 | |
2dbba6f7 JB |
8 | /** |
9 | * struct genl_multicast_group - generic netlink multicast group | |
10 | * @name: name of the multicast group, names are per-family | |
11 | * @id: multicast group ID, assigned by the core, to use with | |
12 | * genlmsg_multicast(). | |
13 | * @list: list entry for linking | |
14 | * @family: pointer to family, need not be set before registering | |
15 | */ | |
fd2c3ef7 | 16 | struct genl_multicast_group { |
2dbba6f7 JB |
17 | struct genl_family *family; /* private */ |
18 | struct list_head list; /* private */ | |
19 | char name[GENL_NAMSIZ]; | |
20 | u32 id; | |
21 | }; | |
22 | ||
482a8524 TG |
23 | /** |
24 | * struct genl_family - generic netlink family | |
25 | * @id: protocol family idenfitier | |
26 | * @hdrsize: length of user specific header in bytes | |
27 | * @name: name of family | |
28 | * @version: protocol version | |
29 | * @maxattr: maximum number of attributes supported | |
134e6375 JB |
30 | * @netnsok: set to true if the family can handle network |
31 | * namespaces and should be presented in all of them | |
482a8524 TG |
32 | * @attrbuf: buffer to store parsed attributes |
33 | * @ops_list: list of all assigned operations | |
34 | * @family_list: family list | |
2dbba6f7 | 35 | * @mcast_groups: multicast groups list |
482a8524 | 36 | */ |
fd2c3ef7 | 37 | struct genl_family { |
482a8524 TG |
38 | unsigned int id; |
39 | unsigned int hdrsize; | |
40 | char name[GENL_NAMSIZ]; | |
41 | unsigned int version; | |
42 | unsigned int maxattr; | |
134e6375 | 43 | bool netnsok; |
482a8524 TG |
44 | struct nlattr ** attrbuf; /* private */ |
45 | struct list_head ops_list; /* private */ | |
46 | struct list_head family_list; /* private */ | |
2dbba6f7 | 47 | struct list_head mcast_groups; /* private */ |
482a8524 TG |
48 | }; |
49 | ||
482a8524 TG |
50 | /** |
51 | * struct genl_info - receiving information | |
52 | * @snd_seq: sending sequence number | |
53 | * @snd_pid: netlink pid of sender | |
54 | * @nlhdr: netlink message header | |
55 | * @genlhdr: generic netlink message header | |
56 | * @userhdr: user specific header | |
57 | * @attrs: netlink attributes | |
58 | */ | |
fd2c3ef7 | 59 | struct genl_info { |
482a8524 TG |
60 | u32 snd_seq; |
61 | u32 snd_pid; | |
62 | struct nlmsghdr * nlhdr; | |
63 | struct genlmsghdr * genlhdr; | |
64 | void * userhdr; | |
65 | struct nlattr ** attrs; | |
134e6375 JB |
66 | #ifdef CONFIG_NET_NS |
67 | struct net * _net; | |
68 | #endif | |
482a8524 TG |
69 | }; |
70 | ||
134e6375 JB |
71 | static inline struct net *genl_info_net(struct genl_info *info) |
72 | { | |
c2d9ba9b | 73 | return read_pnet(&info->_net); |
134e6375 JB |
74 | } |
75 | ||
76 | static inline void genl_info_net_set(struct genl_info *info, struct net *net) | |
77 | { | |
c2d9ba9b | 78 | write_pnet(&info->_net, net); |
134e6375 | 79 | } |
134e6375 | 80 | |
482a8524 TG |
81 | /** |
82 | * struct genl_ops - generic netlink operations | |
83 | * @cmd: command identifier | |
84 | * @flags: flags | |
85 | * @policy: attribute validation policy | |
86 | * @doit: standard command callback | |
87 | * @dumpit: callback for dumpers | |
a4d1366d | 88 | * @done: completion callback for dumps |
482a8524 TG |
89 | * @ops_list: operations list |
90 | */ | |
fd2c3ef7 | 91 | struct genl_ops { |
b461d2f2 | 92 | u8 cmd; |
482a8524 | 93 | unsigned int flags; |
ef7c79ed | 94 | const struct nla_policy *policy; |
482a8524 TG |
95 | int (*doit)(struct sk_buff *skb, |
96 | struct genl_info *info); | |
97 | int (*dumpit)(struct sk_buff *skb, | |
98 | struct netlink_callback *cb); | |
a4d1366d | 99 | int (*done)(struct netlink_callback *cb); |
482a8524 TG |
100 | struct list_head ops_list; |
101 | }; | |
102 | ||
103 | extern int genl_register_family(struct genl_family *family); | |
a7b11d73 MM |
104 | extern int genl_register_family_with_ops(struct genl_family *family, |
105 | struct genl_ops *ops, size_t n_ops); | |
482a8524 TG |
106 | extern int genl_unregister_family(struct genl_family *family); |
107 | extern int genl_register_ops(struct genl_family *, struct genl_ops *ops); | |
108 | extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops); | |
2dbba6f7 JB |
109 | extern int genl_register_mc_group(struct genl_family *family, |
110 | struct genl_multicast_group *grp); | |
111 | extern void genl_unregister_mc_group(struct genl_family *family, | |
112 | struct genl_multicast_group *grp); | |
482a8524 | 113 | |
482a8524 TG |
114 | /** |
115 | * genlmsg_put - Add generic netlink header to netlink message | |
116 | * @skb: socket buffer holding the message | |
117 | * @pid: netlink pid the message is addressed to | |
118 | * @seq: sequence number (usually the one of the sender) | |
17c157c8 | 119 | * @family: generic netlink family |
482a8524 TG |
120 | * @flags netlink message flags |
121 | * @cmd: generic netlink command | |
482a8524 TG |
122 | * |
123 | * Returns pointer to user specific header | |
124 | */ | |
125 | static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, | |
17c157c8 | 126 | struct genl_family *family, int flags, u8 cmd) |
482a8524 TG |
127 | { |
128 | struct nlmsghdr *nlh; | |
129 | struct genlmsghdr *hdr; | |
130 | ||
17c157c8 TG |
131 | nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN + |
132 | family->hdrsize, flags); | |
482a8524 TG |
133 | if (nlh == NULL) |
134 | return NULL; | |
135 | ||
136 | hdr = nlmsg_data(nlh); | |
137 | hdr->cmd = cmd; | |
17c157c8 | 138 | hdr->version = family->version; |
482a8524 TG |
139 | hdr->reserved = 0; |
140 | ||
141 | return (char *) hdr + GENL_HDRLEN; | |
142 | } | |
143 | ||
17c157c8 TG |
144 | /** |
145 | * genlmsg_put_reply - Add generic netlink header to a reply message | |
146 | * @skb: socket buffer holding the message | |
147 | * @info: receiver info | |
148 | * @family: generic netlink family | |
149 | * @flags: netlink message flags | |
150 | * @cmd: generic netlink command | |
151 | * | |
152 | * Returns pointer to user specific header | |
153 | */ | |
154 | static inline void *genlmsg_put_reply(struct sk_buff *skb, | |
155 | struct genl_info *info, | |
156 | struct genl_family *family, | |
157 | int flags, u8 cmd) | |
158 | { | |
159 | return genlmsg_put(skb, info->snd_pid, info->snd_seq, family, | |
160 | flags, cmd); | |
161 | } | |
162 | ||
482a8524 TG |
163 | /** |
164 | * genlmsg_end - Finalize a generic netlink message | |
165 | * @skb: socket buffer the message is stored in | |
166 | * @hdr: user specific header | |
167 | */ | |
168 | static inline int genlmsg_end(struct sk_buff *skb, void *hdr) | |
169 | { | |
170 | return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); | |
171 | } | |
172 | ||
173 | /** | |
174 | * genlmsg_cancel - Cancel construction of a generic netlink message | |
175 | * @skb: socket buffer the message is stored in | |
176 | * @hdr: generic netlink message header | |
177 | */ | |
bc3ed28c | 178 | static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) |
482a8524 | 179 | { |
bc3ed28c | 180 | nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); |
482a8524 TG |
181 | } |
182 | ||
183 | /** | |
134e6375 JB |
184 | * genlmsg_multicast_netns - multicast a netlink message to a specific netns |
185 | * @net: the net namespace | |
186 | * @skb: netlink message as socket buffer | |
187 | * @pid: own netlink pid to avoid sending to yourself | |
188 | * @group: multicast group id | |
189 | * @flags: allocation flags | |
190 | */ | |
191 | static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb, | |
192 | u32 pid, unsigned int group, gfp_t flags) | |
193 | { | |
194 | return nlmsg_multicast(net->genl_sock, skb, pid, group, flags); | |
195 | } | |
196 | ||
197 | /** | |
198 | * genlmsg_multicast - multicast a netlink message to the default netns | |
482a8524 TG |
199 | * @skb: netlink message as socket buffer |
200 | * @pid: own netlink pid to avoid sending to yourself | |
201 | * @group: multicast group id | |
d387f6ad | 202 | * @flags: allocation flags |
482a8524 TG |
203 | */ |
204 | static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid, | |
d387f6ad | 205 | unsigned int group, gfp_t flags) |
482a8524 | 206 | { |
134e6375 | 207 | return genlmsg_multicast_netns(&init_net, skb, pid, group, flags); |
482a8524 TG |
208 | } |
209 | ||
134e6375 JB |
210 | /** |
211 | * genlmsg_multicast_allns - multicast a netlink message to all net namespaces | |
212 | * @skb: netlink message as socket buffer | |
213 | * @pid: own netlink pid to avoid sending to yourself | |
214 | * @group: multicast group id | |
215 | * @flags: allocation flags | |
216 | * | |
217 | * This function must hold the RTNL or rcu_read_lock(). | |
218 | */ | |
219 | int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, | |
220 | unsigned int group, gfp_t flags); | |
221 | ||
482a8524 TG |
222 | /** |
223 | * genlmsg_unicast - unicast a netlink message | |
224 | * @skb: netlink message as socket buffer | |
225 | * @pid: netlink pid of the destination socket | |
226 | */ | |
134e6375 | 227 | static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid) |
482a8524 | 228 | { |
134e6375 | 229 | return nlmsg_unicast(net->genl_sock, skb, pid); |
482a8524 TG |
230 | } |
231 | ||
81878d27 TG |
232 | /** |
233 | * genlmsg_reply - reply to a request | |
234 | * @skb: netlink message to be sent back | |
235 | * @info: receiver information | |
236 | */ | |
237 | static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info) | |
238 | { | |
134e6375 | 239 | return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid); |
81878d27 TG |
240 | } |
241 | ||
fb0ba6bd BS |
242 | /** |
243 | * gennlmsg_data - head of message payload | |
244 | * @gnlh: genetlink messsage header | |
245 | */ | |
246 | static inline void *genlmsg_data(const struct genlmsghdr *gnlh) | |
247 | { | |
248 | return ((unsigned char *) gnlh + GENL_HDRLEN); | |
249 | } | |
250 | ||
251 | /** | |
252 | * genlmsg_len - length of message payload | |
253 | * @gnlh: genetlink message header | |
254 | */ | |
255 | static inline int genlmsg_len(const struct genlmsghdr *gnlh) | |
256 | { | |
257 | struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh - | |
258 | NLMSG_HDRLEN); | |
259 | return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN); | |
260 | } | |
261 | ||
17db952c BS |
262 | /** |
263 | * genlmsg_msg_size - length of genetlink message not including padding | |
264 | * @payload: length of message payload | |
265 | */ | |
266 | static inline int genlmsg_msg_size(int payload) | |
267 | { | |
268 | return GENL_HDRLEN + payload; | |
269 | } | |
270 | ||
271 | /** | |
272 | * genlmsg_total_size - length of genetlink message including padding | |
273 | * @payload: length of message payload | |
274 | */ | |
275 | static inline int genlmsg_total_size(int payload) | |
276 | { | |
277 | return NLMSG_ALIGN(genlmsg_msg_size(payload)); | |
278 | } | |
279 | ||
3dabc715 TG |
280 | /** |
281 | * genlmsg_new - Allocate a new generic netlink message | |
282 | * @payload: size of the message payload | |
283 | * @flags: the type of memory to allocate. | |
284 | */ | |
285 | static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) | |
286 | { | |
287 | return nlmsg_new(genlmsg_total_size(payload), flags); | |
288 | } | |
289 | ||
290 | ||
482a8524 | 291 | #endif /* __NET_GENERIC_NETLINK_H */ |