Commit | Line | Data |
---|---|---|
f9e815b3 HW |
1 | /* Netfilter messages via netlink socket. Allows for user space |
2 | * protocol helpers and general trouble making from userspace. | |
3 | * | |
4 | * (C) 2001 by Jay Schulist <jschlst@samba.org>, | |
5 | * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org> | |
67ca3966 | 6 | * (C) 2005,2007 by Pablo Neira Ayuso <pablo@netfilter.org> |
f9e815b3 HW |
7 | * |
8 | * Initial netfilter messages via netlink development funded and | |
9 | * generally made possible by Network Robots, Inc. (www.networkrobots.com) | |
10 | * | |
11 | * Further development of this code funded by Astaro AG (http://www.astaro.com) | |
12 | * | |
13 | * This software may be used and distributed according to the terms | |
14 | * of the GNU General Public License, incorporated herein by reference. | |
15 | */ | |
16 | ||
f9e815b3 HW |
17 | #include <linux/module.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/socket.h> | |
20 | #include <linux/kernel.h> | |
f9e815b3 HW |
21 | #include <linux/string.h> |
22 | #include <linux/sockios.h> | |
23 | #include <linux/net.h> | |
f9e815b3 HW |
24 | #include <linux/skbuff.h> |
25 | #include <asm/uaccess.h> | |
f9e815b3 HW |
26 | #include <net/sock.h> |
27 | #include <linux/init.h> | |
f9e815b3 | 28 | |
573ce260 | 29 | #include <net/netlink.h> |
f9e815b3 HW |
30 | #include <linux/netfilter/nfnetlink.h> |
31 | ||
32 | MODULE_LICENSE("GPL"); | |
4fdb3bb7 HW |
33 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
34 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER); | |
f9e815b3 HW |
35 | |
36 | static char __initdata nfversion[] = "0.30"; | |
37 | ||
c14b78e7 PNA |
38 | static struct { |
39 | struct mutex mutex; | |
40 | const struct nfnetlink_subsystem __rcu *subsys; | |
41 | } table[NFNL_SUBSYS_COUNT]; | |
f9e815b3 | 42 | |
03292745 PNA |
43 | static const int nfnl_group2type[NFNLGRP_MAX+1] = { |
44 | [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK, | |
45 | [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK, | |
46 | [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK, | |
47 | [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP, | |
48 | [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP, | |
49 | [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP, | |
97840cb6 PNA |
50 | [NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES, |
51 | [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT, | |
33d5a7b1 | 52 | [NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES, |
03292745 PNA |
53 | }; |
54 | ||
c14b78e7 | 55 | void nfnl_lock(__u8 subsys_id) |
f9e815b3 | 56 | { |
c14b78e7 | 57 | mutex_lock(&table[subsys_id].mutex); |
f9e815b3 | 58 | } |
e6a7d3c0 | 59 | EXPORT_SYMBOL_GPL(nfnl_lock); |
f9e815b3 | 60 | |
c14b78e7 | 61 | void nfnl_unlock(__u8 subsys_id) |
a3c5029c | 62 | { |
c14b78e7 | 63 | mutex_unlock(&table[subsys_id].mutex); |
f9e815b3 | 64 | } |
e6a7d3c0 | 65 | EXPORT_SYMBOL_GPL(nfnl_unlock); |
f9e815b3 | 66 | |
0eb5db7a | 67 | #ifdef CONFIG_PROVE_LOCKING |
875e0829 | 68 | bool lockdep_nfnl_is_held(u8 subsys_id) |
0eb5db7a PM |
69 | { |
70 | return lockdep_is_held(&table[subsys_id].mutex); | |
71 | } | |
72 | EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held); | |
73 | #endif | |
74 | ||
7c8d4cb4 | 75 | int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n) |
f9e815b3 | 76 | { |
c14b78e7 PNA |
77 | nfnl_lock(n->subsys_id); |
78 | if (table[n->subsys_id].subsys) { | |
79 | nfnl_unlock(n->subsys_id); | |
0ab43f84 HW |
80 | return -EBUSY; |
81 | } | |
c14b78e7 PNA |
82 | rcu_assign_pointer(table[n->subsys_id].subsys, n); |
83 | nfnl_unlock(n->subsys_id); | |
f9e815b3 HW |
84 | |
85 | return 0; | |
86 | } | |
f4bc177f | 87 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_register); |
f9e815b3 | 88 | |
7c8d4cb4 | 89 | int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n) |
f9e815b3 | 90 | { |
c14b78e7 PNA |
91 | nfnl_lock(n->subsys_id); |
92 | table[n->subsys_id].subsys = NULL; | |
93 | nfnl_unlock(n->subsys_id); | |
6b75e3e8 | 94 | synchronize_rcu(); |
f9e815b3 HW |
95 | return 0; |
96 | } | |
f4bc177f | 97 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister); |
f9e815b3 | 98 | |
7c8d4cb4 | 99 | static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type) |
f9e815b3 HW |
100 | { |
101 | u_int8_t subsys_id = NFNL_SUBSYS_ID(type); | |
102 | ||
ac0f1d98 | 103 | if (subsys_id >= NFNL_SUBSYS_COUNT) |
f9e815b3 HW |
104 | return NULL; |
105 | ||
c14b78e7 | 106 | return rcu_dereference(table[subsys_id].subsys); |
f9e815b3 HW |
107 | } |
108 | ||
7c8d4cb4 PM |
109 | static inline const struct nfnl_callback * |
110 | nfnetlink_find_client(u_int16_t type, const struct nfnetlink_subsystem *ss) | |
f9e815b3 HW |
111 | { |
112 | u_int8_t cb_id = NFNL_MSG_TYPE(type); | |
601e68e1 | 113 | |
67ca3966 | 114 | if (cb_id >= ss->cb_count) |
f9e815b3 | 115 | return NULL; |
f9e815b3 HW |
116 | |
117 | return &ss->cb[cb_id]; | |
118 | } | |
119 | ||
cd8c20b6 | 120 | int nfnetlink_has_listeners(struct net *net, unsigned int group) |
a2427692 | 121 | { |
cd8c20b6 | 122 | return netlink_has_listeners(net->nfnl, group); |
a2427692 PM |
123 | } |
124 | EXPORT_SYMBOL_GPL(nfnetlink_has_listeners); | |
125 | ||
3ab1f683 PM |
126 | struct sk_buff *nfnetlink_alloc_skb(struct net *net, unsigned int size, |
127 | u32 dst_portid, gfp_t gfp_mask) | |
128 | { | |
129 | return netlink_alloc_skb(net->nfnl, size, dst_portid, gfp_mask); | |
130 | } | |
131 | EXPORT_SYMBOL_GPL(nfnetlink_alloc_skb); | |
132 | ||
ec464e5d | 133 | int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid, |
95c96174 | 134 | unsigned int group, int echo, gfp_t flags) |
f9e815b3 | 135 | { |
ec464e5d | 136 | return nlmsg_notify(net->nfnl, skb, portid, group, echo, flags); |
f9e815b3 | 137 | } |
f4bc177f | 138 | EXPORT_SYMBOL_GPL(nfnetlink_send); |
f9e815b3 | 139 | |
ec464e5d | 140 | int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error) |
dd5b6ce6 | 141 | { |
ec464e5d | 142 | return netlink_set_err(net->nfnl, portid, group, error); |
dd5b6ce6 PNA |
143 | } |
144 | EXPORT_SYMBOL_GPL(nfnetlink_set_err); | |
145 | ||
ec464e5d PM |
146 | int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid, |
147 | int flags) | |
f9e815b3 | 148 | { |
ec464e5d | 149 | return netlink_unicast(net->nfnl, skb, portid, flags); |
f9e815b3 | 150 | } |
f4bc177f | 151 | EXPORT_SYMBOL_GPL(nfnetlink_unicast); |
f9e815b3 HW |
152 | |
153 | /* Process one complete nfnetlink message. */ | |
1d00a4eb | 154 | static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
f9e815b3 | 155 | { |
cd8c20b6 | 156 | struct net *net = sock_net(skb->sk); |
7c8d4cb4 PM |
157 | const struct nfnl_callback *nc; |
158 | const struct nfnetlink_subsystem *ss; | |
1d00a4eb | 159 | int type, err; |
f9e815b3 | 160 | |
f9e815b3 | 161 | /* All the messages must at least contain nfgenmsg */ |
573ce260 | 162 | if (nlmsg_len(nlh) < sizeof(struct nfgenmsg)) |
f9e815b3 | 163 | return 0; |
f9e815b3 HW |
164 | |
165 | type = nlh->nlmsg_type; | |
e6a7d3c0 | 166 | replay: |
6b75e3e8 | 167 | rcu_read_lock(); |
f9e815b3 | 168 | ss = nfnetlink_get_subsys(type); |
0ab43f84 | 169 | if (!ss) { |
95a5afca | 170 | #ifdef CONFIG_MODULES |
6b75e3e8 | 171 | rcu_read_unlock(); |
37d2e7a2 | 172 | request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type)); |
6b75e3e8 | 173 | rcu_read_lock(); |
37d2e7a2 | 174 | ss = nfnetlink_get_subsys(type); |
0ab43f84 HW |
175 | if (!ss) |
176 | #endif | |
6b75e3e8 ED |
177 | { |
178 | rcu_read_unlock(); | |
1d00a4eb | 179 | return -EINVAL; |
6b75e3e8 | 180 | } |
0ab43f84 | 181 | } |
f9e815b3 HW |
182 | |
183 | nc = nfnetlink_find_client(type, ss); | |
6b75e3e8 ED |
184 | if (!nc) { |
185 | rcu_read_unlock(); | |
1d00a4eb | 186 | return -EINVAL; |
6b75e3e8 | 187 | } |
f9e815b3 | 188 | |
f9e815b3 | 189 | { |
573ce260 | 190 | int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); |
e3730578 | 191 | u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); |
f49c857f PNA |
192 | struct nlattr *cda[ss->cb[cb_id].attr_count + 1]; |
193 | struct nlattr *attr = (void *)nlh + min_len; | |
194 | int attrlen = nlh->nlmsg_len - min_len; | |
c14b78e7 | 195 | __u8 subsys_id = NFNL_SUBSYS_ID(type); |
f49c857f PNA |
196 | |
197 | err = nla_parse(cda, ss->cb[cb_id].attr_count, | |
198 | attr, attrlen, ss->cb[cb_id].policy); | |
4009e188 TB |
199 | if (err < 0) { |
200 | rcu_read_unlock(); | |
f49c857f | 201 | return err; |
4009e188 | 202 | } |
601e68e1 | 203 | |
6b75e3e8 ED |
204 | if (nc->call_rcu) { |
205 | err = nc->call_rcu(net->nfnl, skb, nlh, | |
206 | (const struct nlattr **)cda); | |
207 | rcu_read_unlock(); | |
208 | } else { | |
209 | rcu_read_unlock(); | |
c14b78e7 PNA |
210 | nfnl_lock(subsys_id); |
211 | if (rcu_dereference_protected(table[subsys_id].subsys, | |
9df9e783 | 212 | lockdep_is_held(&table[subsys_id].mutex)) != ss || |
6b75e3e8 ED |
213 | nfnetlink_find_client(type, ss) != nc) |
214 | err = -EAGAIN; | |
59560a38 | 215 | else if (nc->call) |
6b75e3e8 ED |
216 | err = nc->call(net->nfnl, skb, nlh, |
217 | (const struct nlattr **)cda); | |
59560a38 TB |
218 | else |
219 | err = -EINVAL; | |
c14b78e7 | 220 | nfnl_unlock(subsys_id); |
6b75e3e8 | 221 | } |
e6a7d3c0 PNA |
222 | if (err == -EAGAIN) |
223 | goto replay; | |
224 | return err; | |
f9e815b3 | 225 | } |
f9e815b3 HW |
226 | } |
227 | ||
cbb8125e PNA |
228 | struct nfnl_err { |
229 | struct list_head head; | |
230 | struct nlmsghdr *nlh; | |
231 | int err; | |
232 | }; | |
233 | ||
234 | static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err) | |
235 | { | |
236 | struct nfnl_err *nfnl_err; | |
237 | ||
238 | nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL); | |
239 | if (nfnl_err == NULL) | |
240 | return -ENOMEM; | |
241 | ||
242 | nfnl_err->nlh = nlh; | |
243 | nfnl_err->err = err; | |
244 | list_add_tail(&nfnl_err->head, list); | |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
249 | static void nfnl_err_del(struct nfnl_err *nfnl_err) | |
250 | { | |
251 | list_del(&nfnl_err->head); | |
252 | kfree(nfnl_err); | |
253 | } | |
254 | ||
255 | static void nfnl_err_reset(struct list_head *err_list) | |
256 | { | |
257 | struct nfnl_err *nfnl_err, *next; | |
258 | ||
259 | list_for_each_entry_safe(nfnl_err, next, err_list, head) | |
260 | nfnl_err_del(nfnl_err); | |
261 | } | |
262 | ||
263 | static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb) | |
264 | { | |
265 | struct nfnl_err *nfnl_err, *next; | |
266 | ||
267 | list_for_each_entry_safe(nfnl_err, next, err_list, head) { | |
268 | netlink_ack(skb, nfnl_err->nlh, nfnl_err->err); | |
269 | nfnl_err_del(nfnl_err); | |
270 | } | |
271 | } | |
272 | ||
6742b9e3 PNA |
273 | enum { |
274 | NFNL_BATCH_FAILURE = (1 << 0), | |
275 | NFNL_BATCH_DONE = (1 << 1), | |
276 | NFNL_BATCH_REPLAY = (1 << 2), | |
277 | }; | |
278 | ||
0628b123 PNA |
279 | static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, |
280 | u_int16_t subsys_id) | |
281 | { | |
0f816232 | 282 | struct sk_buff *oskb = skb; |
0628b123 PNA |
283 | struct net *net = sock_net(skb->sk); |
284 | const struct nfnetlink_subsystem *ss; | |
285 | const struct nfnl_callback *nc; | |
cbb8125e | 286 | static LIST_HEAD(err_list); |
6742b9e3 | 287 | u32 status; |
0628b123 PNA |
288 | int err; |
289 | ||
290 | if (subsys_id >= NFNL_SUBSYS_COUNT) | |
291 | return netlink_ack(skb, nlh, -EINVAL); | |
292 | replay: | |
6742b9e3 PNA |
293 | status = 0; |
294 | ||
0f816232 DJ |
295 | skb = netlink_skb_clone(oskb, GFP_KERNEL); |
296 | if (!skb) | |
0628b123 PNA |
297 | return netlink_ack(oskb, nlh, -ENOMEM); |
298 | ||
0f816232 | 299 | skb->sk = oskb->sk; |
0628b123 PNA |
300 | |
301 | nfnl_lock(subsys_id); | |
302 | ss = rcu_dereference_protected(table[subsys_id].subsys, | |
303 | lockdep_is_held(&table[subsys_id].mutex)); | |
304 | if (!ss) { | |
305 | #ifdef CONFIG_MODULES | |
306 | nfnl_unlock(subsys_id); | |
307 | request_module("nfnetlink-subsys-%d", subsys_id); | |
308 | nfnl_lock(subsys_id); | |
309 | ss = rcu_dereference_protected(table[subsys_id].subsys, | |
310 | lockdep_is_held(&table[subsys_id].mutex)); | |
311 | if (!ss) | |
312 | #endif | |
313 | { | |
314 | nfnl_unlock(subsys_id); | |
ecd15dd7 | 315 | netlink_ack(skb, nlh, -EOPNOTSUPP); |
0f816232 | 316 | return kfree_skb(skb); |
0628b123 PNA |
317 | } |
318 | } | |
319 | ||
320 | if (!ss->commit || !ss->abort) { | |
321 | nfnl_unlock(subsys_id); | |
ecd15dd7 DF |
322 | netlink_ack(skb, nlh, -EOPNOTSUPP); |
323 | return kfree_skb(skb); | |
0628b123 PNA |
324 | } |
325 | ||
326 | while (skb->len >= nlmsg_total_size(0)) { | |
327 | int msglen, type; | |
328 | ||
329 | nlh = nlmsg_hdr(skb); | |
330 | err = 0; | |
331 | ||
9ea2aa8b PNA |
332 | if (nlmsg_len(nlh) < sizeof(struct nfgenmsg) || |
333 | skb->len < nlh->nlmsg_len) { | |
0628b123 PNA |
334 | err = -EINVAL; |
335 | goto ack; | |
336 | } | |
337 | ||
338 | /* Only requests are handled by the kernel */ | |
339 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) { | |
340 | err = -EINVAL; | |
341 | goto ack; | |
342 | } | |
343 | ||
344 | type = nlh->nlmsg_type; | |
345 | if (type == NFNL_MSG_BATCH_BEGIN) { | |
346 | /* Malformed: Batch begin twice */ | |
cbb8125e | 347 | nfnl_err_reset(&err_list); |
6742b9e3 | 348 | status |= NFNL_BATCH_FAILURE; |
0628b123 PNA |
349 | goto done; |
350 | } else if (type == NFNL_MSG_BATCH_END) { | |
6742b9e3 | 351 | status |= NFNL_BATCH_DONE; |
0628b123 PNA |
352 | goto done; |
353 | } else if (type < NLMSG_MIN_TYPE) { | |
354 | err = -EINVAL; | |
355 | goto ack; | |
356 | } | |
357 | ||
358 | /* We only accept a batch with messages for the same | |
359 | * subsystem. | |
360 | */ | |
361 | if (NFNL_SUBSYS_ID(type) != subsys_id) { | |
362 | err = -EINVAL; | |
363 | goto ack; | |
364 | } | |
365 | ||
366 | nc = nfnetlink_find_client(type, ss); | |
367 | if (!nc) { | |
368 | err = -EINVAL; | |
369 | goto ack; | |
370 | } | |
371 | ||
372 | { | |
373 | int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); | |
374 | u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); | |
375 | struct nlattr *cda[ss->cb[cb_id].attr_count + 1]; | |
376 | struct nlattr *attr = (void *)nlh + min_len; | |
377 | int attrlen = nlh->nlmsg_len - min_len; | |
378 | ||
379 | err = nla_parse(cda, ss->cb[cb_id].attr_count, | |
380 | attr, attrlen, ss->cb[cb_id].policy); | |
381 | if (err < 0) | |
382 | goto ack; | |
383 | ||
384 | if (nc->call_batch) { | |
385 | err = nc->call_batch(net->nfnl, skb, nlh, | |
386 | (const struct nlattr **)cda); | |
387 | } | |
388 | ||
389 | /* The lock was released to autoload some module, we | |
390 | * have to abort and start from scratch using the | |
391 | * original skb. | |
392 | */ | |
393 | if (err == -EAGAIN) { | |
6742b9e3 PNA |
394 | status |= NFNL_BATCH_REPLAY; |
395 | goto next; | |
0628b123 PNA |
396 | } |
397 | } | |
398 | ack: | |
399 | if (nlh->nlmsg_flags & NLM_F_ACK || err) { | |
cbb8125e PNA |
400 | /* Errors are delivered once the full batch has been |
401 | * processed, this avoids that the same error is | |
402 | * reported several times when replaying the batch. | |
403 | */ | |
404 | if (nfnl_err_add(&err_list, nlh, err) < 0) { | |
405 | /* We failed to enqueue an error, reset the | |
406 | * list of errors and send OOM to userspace | |
407 | * pointing to the batch header. | |
408 | */ | |
409 | nfnl_err_reset(&err_list); | |
410 | netlink_ack(skb, nlmsg_hdr(oskb), -ENOMEM); | |
6742b9e3 | 411 | status |= NFNL_BATCH_FAILURE; |
cbb8125e PNA |
412 | goto done; |
413 | } | |
0628b123 PNA |
414 | /* We don't stop processing the batch on errors, thus, |
415 | * userspace gets all the errors that the batch | |
416 | * triggers. | |
417 | */ | |
0628b123 | 418 | if (err) |
6742b9e3 | 419 | status |= NFNL_BATCH_FAILURE; |
0628b123 | 420 | } |
6742b9e3 | 421 | next: |
0628b123 PNA |
422 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
423 | if (msglen > skb->len) | |
424 | msglen = skb->len; | |
425 | skb_pull(skb, msglen); | |
426 | } | |
427 | done: | |
6742b9e3 PNA |
428 | if (status & NFNL_BATCH_REPLAY) { |
429 | ss->abort(oskb); | |
430 | nfnl_err_reset(&err_list); | |
431 | nfnl_unlock(subsys_id); | |
432 | kfree_skb(skb); | |
433 | goto replay; | |
434 | } else if (status == NFNL_BATCH_DONE) { | |
fc04733a | 435 | ss->commit(oskb); |
6742b9e3 | 436 | } else { |
fc04733a | 437 | ss->abort(oskb); |
6742b9e3 | 438 | } |
0628b123 | 439 | |
cbb8125e | 440 | nfnl_err_deliver(&err_list, oskb); |
0628b123 | 441 | nfnl_unlock(subsys_id); |
0f816232 | 442 | kfree_skb(skb); |
0628b123 PNA |
443 | } |
444 | ||
cd40b7d3 | 445 | static void nfnetlink_rcv(struct sk_buff *skb) |
f9e815b3 | 446 | { |
0628b123 | 447 | struct nlmsghdr *nlh = nlmsg_hdr(skb); |
a9de9777 | 448 | u_int16_t res_id; |
0628b123 PNA |
449 | int msglen; |
450 | ||
0628b123 PNA |
451 | if (nlh->nlmsg_len < NLMSG_HDRLEN || |
452 | skb->len < nlh->nlmsg_len) | |
453 | return; | |
454 | ||
90f62cf3 | 455 | if (!netlink_net_capable(skb, CAP_NET_ADMIN)) { |
cdbe7c2d JB |
456 | netlink_ack(skb, nlh, -EPERM); |
457 | return; | |
458 | } | |
459 | ||
0628b123 PNA |
460 | if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN) { |
461 | struct nfgenmsg *nfgenmsg; | |
462 | ||
463 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); | |
464 | if (msglen > skb->len) | |
465 | msglen = skb->len; | |
466 | ||
467 | if (nlh->nlmsg_len < NLMSG_HDRLEN || | |
468 | skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg)) | |
469 | return; | |
470 | ||
471 | nfgenmsg = nlmsg_data(nlh); | |
472 | skb_pull(skb, msglen); | |
a9de9777 PNA |
473 | /* Work around old nft using host byte order */ |
474 | if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES) | |
475 | res_id = NFNL_SUBSYS_NFTABLES; | |
476 | else | |
477 | res_id = ntohs(nfgenmsg->res_id); | |
478 | nfnetlink_rcv_batch(skb, nlh, res_id); | |
0628b123 PNA |
479 | } else { |
480 | netlink_rcv_skb(skb, &nfnetlink_rcv_msg); | |
481 | } | |
f9e815b3 HW |
482 | } |
483 | ||
03292745 | 484 | #ifdef CONFIG_MODULES |
023e2cfa | 485 | static int nfnetlink_bind(struct net *net, int group) |
03292745 PNA |
486 | { |
487 | const struct nfnetlink_subsystem *ss; | |
97840cb6 PNA |
488 | int type; |
489 | ||
490 | if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX) | |
62924af2 | 491 | return 0; |
97840cb6 PNA |
492 | |
493 | type = nfnl_group2type[group]; | |
03292745 PNA |
494 | |
495 | rcu_read_lock(); | |
dbc3617f | 496 | ss = nfnetlink_get_subsys(type << 8); |
03292745 | 497 | rcu_read_unlock(); |
bfe4bc71 RGB |
498 | if (!ss) |
499 | request_module("nfnetlink-subsys-%d", type); | |
4f520900 | 500 | return 0; |
03292745 PNA |
501 | } |
502 | #endif | |
503 | ||
cd8c20b6 | 504 | static int __net_init nfnetlink_net_init(struct net *net) |
f9e815b3 | 505 | { |
cd8c20b6 | 506 | struct sock *nfnl; |
a31f2d17 PNA |
507 | struct netlink_kernel_cfg cfg = { |
508 | .groups = NFNLGRP_MAX, | |
509 | .input = nfnetlink_rcv, | |
03292745 PNA |
510 | #ifdef CONFIG_MODULES |
511 | .bind = nfnetlink_bind, | |
512 | #endif | |
a31f2d17 | 513 | }; |
cd8c20b6 | 514 | |
9f00d977 | 515 | nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg); |
cd8c20b6 AD |
516 | if (!nfnl) |
517 | return -ENOMEM; | |
518 | net->nfnl_stash = nfnl; | |
cf778b00 | 519 | rcu_assign_pointer(net->nfnl, nfnl); |
cd8c20b6 | 520 | return 0; |
f9e815b3 HW |
521 | } |
522 | ||
cd8c20b6 | 523 | static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list) |
f9e815b3 | 524 | { |
cd8c20b6 | 525 | struct net *net; |
f9e815b3 | 526 | |
cd8c20b6 | 527 | list_for_each_entry(net, net_exit_list, exit_list) |
a9b3cd7f | 528 | RCU_INIT_POINTER(net->nfnl, NULL); |
cd8c20b6 AD |
529 | synchronize_net(); |
530 | list_for_each_entry(net, net_exit_list, exit_list) | |
531 | netlink_kernel_release(net->nfnl_stash); | |
532 | } | |
f9e815b3 | 533 | |
cd8c20b6 AD |
534 | static struct pernet_operations nfnetlink_net_ops = { |
535 | .init = nfnetlink_net_init, | |
536 | .exit_batch = nfnetlink_net_exit_batch, | |
537 | }; | |
538 | ||
539 | static int __init nfnetlink_init(void) | |
540 | { | |
c14b78e7 PNA |
541 | int i; |
542 | ||
97840cb6 PNA |
543 | for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++) |
544 | BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE); | |
545 | ||
c14b78e7 PNA |
546 | for (i=0; i<NFNL_SUBSYS_COUNT; i++) |
547 | mutex_init(&table[i].mutex); | |
548 | ||
654d0fbd | 549 | pr_info("Netfilter messages via NETLINK v%s.\n", nfversion); |
cd8c20b6 | 550 | return register_pernet_subsys(&nfnetlink_net_ops); |
f9e815b3 HW |
551 | } |
552 | ||
cd8c20b6 AD |
553 | static void __exit nfnetlink_exit(void) |
554 | { | |
654d0fbd | 555 | pr_info("Removing netfilter NETLINK layer.\n"); |
cd8c20b6 AD |
556 | unregister_pernet_subsys(&nfnetlink_net_ops); |
557 | } | |
f9e815b3 HW |
558 | module_init(nfnetlink_init); |
559 | module_exit(nfnetlink_exit); |