diff options
author | Gao feng <gaofeng@cn.fujitsu.com> | 2013-03-24 19:50:44 -0400 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2013-04-05 15:03:43 -0400 |
commit | 355430671ad93546b34b4e91bdf720f3a704efa4 (patch) | |
tree | b5ad3a9a518ef3b3d9a072a67685727dfdd6b1d6 /net/ipv4 | |
parent | 5b175b7ebd4af6cf3e9e60fdb21b76c1c5a8d713 (diff) |
netfilter: ipt_ULOG: add net namespace support for ipt_ULOG
Add pernet support to ipt_ULOG by means of the new nf_log_set
function added in (30e0c6a netfilter: nf_log: prepare net
namespace support for loggers).
This patch also make ulog_buffers and netlink socket
nflognl per netns.
Signed-off-by: Gao feng <gaofeng@cn.fujitsu.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'net/ipv4')
-rw-r--r-- | net/ipv4/netfilter/ipt_ULOG.c | 129 |
1 files changed, 89 insertions, 40 deletions
diff --git a/net/ipv4/netfilter/ipt_ULOG.c b/net/ipv4/netfilter/ipt_ULOG.c index 7d168dcbd135..642ecfb711bc 100644 --- a/net/ipv4/netfilter/ipt_ULOG.c +++ b/net/ipv4/netfilter/ipt_ULOG.c | |||
@@ -45,6 +45,7 @@ | |||
45 | #include <linux/netfilter/x_tables.h> | 45 | #include <linux/netfilter/x_tables.h> |
46 | #include <linux/netfilter_ipv4/ipt_ULOG.h> | 46 | #include <linux/netfilter_ipv4/ipt_ULOG.h> |
47 | #include <net/netfilter/nf_log.h> | 47 | #include <net/netfilter/nf_log.h> |
48 | #include <net/netns/generic.h> | ||
48 | #include <net/sock.h> | 49 | #include <net/sock.h> |
49 | #include <linux/bitops.h> | 50 | #include <linux/bitops.h> |
50 | #include <asm/unaligned.h> | 51 | #include <asm/unaligned.h> |
@@ -78,15 +79,23 @@ typedef struct { | |||
78 | struct timer_list timer; /* the timer function */ | 79 | struct timer_list timer; /* the timer function */ |
79 | } ulog_buff_t; | 80 | } ulog_buff_t; |
80 | 81 | ||
81 | static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS]; /* array of buffers */ | 82 | static int ulog_net_id __read_mostly; |
83 | struct ulog_net { | ||
84 | unsigned int nlgroup[ULOG_MAXNLGROUPS]; | ||
85 | ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS]; | ||
86 | struct sock *nflognl; | ||
87 | spinlock_t lock; | ||
88 | }; | ||
82 | 89 | ||
83 | static struct sock *nflognl; /* our socket */ | 90 | static struct ulog_net *ulog_pernet(struct net *net) |
84 | static DEFINE_SPINLOCK(ulog_lock); /* spinlock */ | 91 | { |
92 | return net_generic(net, ulog_net_id); | ||
93 | } | ||
85 | 94 | ||
86 | /* send one ulog_buff_t to userspace */ | 95 | /* send one ulog_buff_t to userspace */ |
87 | static void ulog_send(unsigned int nlgroupnum) | 96 | static void ulog_send(struct ulog_net *ulog, unsigned int nlgroupnum) |
88 | { | 97 | { |
89 | ulog_buff_t *ub = &ulog_buffers[nlgroupnum]; | 98 | ulog_buff_t *ub = &ulog->ulog_buffers[nlgroupnum]; |
90 | 99 | ||
91 | pr_debug("ulog_send: timer is deleting\n"); | 100 | pr_debug("ulog_send: timer is deleting\n"); |
92 | del_timer(&ub->timer); | 101 | del_timer(&ub->timer); |
@@ -103,7 +112,8 @@ static void ulog_send(unsigned int nlgroupnum) | |||
103 | NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1; | 112 | NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1; |
104 | pr_debug("throwing %d packets to netlink group %u\n", | 113 | pr_debug("throwing %d packets to netlink group %u\n", |
105 | ub->qlen, nlgroupnum + 1); | 114 | ub->qlen, nlgroupnum + 1); |
106 | netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC); | 115 | netlink_broadcast(ulog->nflognl, ub->skb, 0, nlgroupnum + 1, |
116 | GFP_ATOMIC); | ||
107 | 117 | ||
108 | ub->qlen = 0; | 118 | ub->qlen = 0; |
109 | ub->skb = NULL; | 119 | ub->skb = NULL; |
@@ -114,13 +124,16 @@ static void ulog_send(unsigned int nlgroupnum) | |||
114 | /* timer function to flush queue in flushtimeout time */ | 124 | /* timer function to flush queue in flushtimeout time */ |
115 | static void ulog_timer(unsigned long data) | 125 | static void ulog_timer(unsigned long data) |
116 | { | 126 | { |
127 | struct ulog_net *ulog = container_of((void *)data, | ||
128 | struct ulog_net, | ||
129 | nlgroup[*(unsigned int *)data]); | ||
117 | pr_debug("timer function called, calling ulog_send\n"); | 130 | pr_debug("timer function called, calling ulog_send\n"); |
118 | 131 | ||
119 | /* lock to protect against somebody modifying our structure | 132 | /* lock to protect against somebody modifying our structure |
120 | * from ipt_ulog_target at the same time */ | 133 | * from ipt_ulog_target at the same time */ |
121 | spin_lock_bh(&ulog_lock); | 134 | spin_lock_bh(&ulog->lock); |
122 | ulog_send(data); | 135 | ulog_send(ulog, data); |
123 | spin_unlock_bh(&ulog_lock); | 136 | spin_unlock_bh(&ulog->lock); |
124 | } | 137 | } |
125 | 138 | ||
126 | static struct sk_buff *ulog_alloc_skb(unsigned int size) | 139 | static struct sk_buff *ulog_alloc_skb(unsigned int size) |
@@ -160,6 +173,8 @@ static void ipt_ulog_packet(unsigned int hooknum, | |||
160 | size_t size, copy_len; | 173 | size_t size, copy_len; |
161 | struct nlmsghdr *nlh; | 174 | struct nlmsghdr *nlh; |
162 | struct timeval tv; | 175 | struct timeval tv; |
176 | struct net *net = dev_net(in ? in : out); | ||
177 | struct ulog_net *ulog = ulog_pernet(net); | ||
163 | 178 | ||
164 | /* ffs == find first bit set, necessary because userspace | 179 | /* ffs == find first bit set, necessary because userspace |
165 | * is already shifting groupnumber, but we need unshifted. | 180 | * is already shifting groupnumber, but we need unshifted. |
@@ -174,9 +189,9 @@ static void ipt_ulog_packet(unsigned int hooknum, | |||
174 | 189 | ||
175 | size = NLMSG_SPACE(sizeof(*pm) + copy_len); | 190 | size = NLMSG_SPACE(sizeof(*pm) + copy_len); |
176 | 191 | ||
177 | ub = &ulog_buffers[groupnum]; | 192 | ub = &ulog->ulog_buffers[groupnum]; |
178 | 193 | ||
179 | spin_lock_bh(&ulog_lock); | 194 | spin_lock_bh(&ulog->lock); |
180 | 195 | ||
181 | if (!ub->skb) { | 196 | if (!ub->skb) { |
182 | if (!(ub->skb = ulog_alloc_skb(size))) | 197 | if (!(ub->skb = ulog_alloc_skb(size))) |
@@ -186,7 +201,7 @@ static void ipt_ulog_packet(unsigned int hooknum, | |||
186 | /* either the queue len is too high or we don't have | 201 | /* either the queue len is too high or we don't have |
187 | * enough room in nlskb left. send it to userspace. */ | 202 | * enough room in nlskb left. send it to userspace. */ |
188 | 203 | ||
189 | ulog_send(groupnum); | 204 | ulog_send(ulog, groupnum); |
190 | 205 | ||
191 | if (!(ub->skb = ulog_alloc_skb(size))) | 206 | if (!(ub->skb = ulog_alloc_skb(size))) |
192 | goto alloc_failure; | 207 | goto alloc_failure; |
@@ -260,16 +275,16 @@ static void ipt_ulog_packet(unsigned int hooknum, | |||
260 | if (ub->qlen >= loginfo->qthreshold) { | 275 | if (ub->qlen >= loginfo->qthreshold) { |
261 | if (loginfo->qthreshold > 1) | 276 | if (loginfo->qthreshold > 1) |
262 | nlh->nlmsg_type = NLMSG_DONE; | 277 | nlh->nlmsg_type = NLMSG_DONE; |
263 | ulog_send(groupnum); | 278 | ulog_send(ulog, groupnum); |
264 | } | 279 | } |
265 | out_unlock: | 280 | out_unlock: |
266 | spin_unlock_bh(&ulog_lock); | 281 | spin_unlock_bh(&ulog->lock); |
267 | 282 | ||
268 | return; | 283 | return; |
269 | 284 | ||
270 | alloc_failure: | 285 | alloc_failure: |
271 | pr_debug("Error building netlink message\n"); | 286 | pr_debug("Error building netlink message\n"); |
272 | spin_unlock_bh(&ulog_lock); | 287 | spin_unlock_bh(&ulog->lock); |
273 | } | 288 | } |
274 | 289 | ||
275 | static unsigned int | 290 | static unsigned int |
@@ -376,54 +391,43 @@ static struct nf_logger ipt_ulog_logger __read_mostly = { | |||
376 | .me = THIS_MODULE, | 391 | .me = THIS_MODULE, |
377 | }; | 392 | }; |
378 | 393 | ||
379 | static int __init ulog_tg_init(void) | 394 | static int __net_init ulog_tg_net_init(struct net *net) |
380 | { | 395 | { |
381 | int ret, i; | 396 | int i; |
397 | struct ulog_net *ulog = ulog_pernet(net); | ||
382 | struct netlink_kernel_cfg cfg = { | 398 | struct netlink_kernel_cfg cfg = { |
383 | .groups = ULOG_MAXNLGROUPS, | 399 | .groups = ULOG_MAXNLGROUPS, |
384 | }; | 400 | }; |
385 | 401 | ||
386 | pr_debug("init module\n"); | 402 | spin_lock_init(&ulog->lock); |
387 | |||
388 | if (nlbufsiz > 128*1024) { | ||
389 | pr_warning("Netlink buffer has to be <= 128kB\n"); | ||
390 | return -EINVAL; | ||
391 | } | ||
392 | |||
393 | /* initialize ulog_buffers */ | 403 | /* initialize ulog_buffers */ |
394 | for (i = 0; i < ULOG_MAXNLGROUPS; i++) | 404 | for (i = 0; i < ULOG_MAXNLGROUPS; i++) |
395 | setup_timer(&ulog_buffers[i].timer, ulog_timer, i); | 405 | setup_timer(&ulog->ulog_buffers[i].timer, ulog_timer, i); |
396 | 406 | ||
397 | nflognl = netlink_kernel_create(&init_net, NETLINK_NFLOG, &cfg); | 407 | ulog->nflognl = netlink_kernel_create(net, NETLINK_NFLOG, &cfg); |
398 | if (!nflognl) | 408 | if (!ulog->nflognl) |
399 | return -ENOMEM; | 409 | return -ENOMEM; |
400 | 410 | ||
401 | ret = xt_register_target(&ulog_tg_reg); | ||
402 | if (ret < 0) { | ||
403 | netlink_kernel_release(nflognl); | ||
404 | return ret; | ||
405 | } | ||
406 | if (nflog) | 411 | if (nflog) |
407 | nf_log_register(NFPROTO_IPV4, &ipt_ulog_logger); | 412 | nf_log_set(net, NFPROTO_IPV4, &ipt_ulog_logger); |
408 | 413 | ||
409 | return 0; | 414 | return 0; |
410 | } | 415 | } |
411 | 416 | ||
412 | static void __exit ulog_tg_exit(void) | 417 | static void __net_exit ulog_tg_net_exit(struct net *net) |
413 | { | 418 | { |
414 | ulog_buff_t *ub; | 419 | ulog_buff_t *ub; |
415 | int i; | 420 | int i; |
416 | 421 | struct ulog_net *ulog = ulog_pernet(net); | |
417 | pr_debug("cleanup_module\n"); | ||
418 | 422 | ||
419 | if (nflog) | 423 | if (nflog) |
420 | nf_log_unregister(&ipt_ulog_logger); | 424 | nf_log_unset(net, &ipt_ulog_logger); |
421 | xt_unregister_target(&ulog_tg_reg); | 425 | |
422 | netlink_kernel_release(nflognl); | 426 | netlink_kernel_release(ulog->nflognl); |
423 | 427 | ||
424 | /* remove pending timers and free allocated skb's */ | 428 | /* remove pending timers and free allocated skb's */ |
425 | for (i = 0; i < ULOG_MAXNLGROUPS; i++) { | 429 | for (i = 0; i < ULOG_MAXNLGROUPS; i++) { |
426 | ub = &ulog_buffers[i]; | 430 | ub = &ulog->ulog_buffers[i]; |
427 | pr_debug("timer is deleting\n"); | 431 | pr_debug("timer is deleting\n"); |
428 | del_timer(&ub->timer); | 432 | del_timer(&ub->timer); |
429 | 433 | ||
@@ -434,5 +438,50 @@ static void __exit ulog_tg_exit(void) | |||
434 | } | 438 | } |
435 | } | 439 | } |
436 | 440 | ||
441 | static struct pernet_operations ulog_tg_net_ops = { | ||
442 | .init = ulog_tg_net_init, | ||
443 | .exit = ulog_tg_net_exit, | ||
444 | .id = &ulog_net_id, | ||
445 | .size = sizeof(struct ulog_net), | ||
446 | }; | ||
447 | |||
448 | static int __init ulog_tg_init(void) | ||
449 | { | ||
450 | int ret; | ||
451 | pr_debug("init module\n"); | ||
452 | |||
453 | if (nlbufsiz > 128*1024) { | ||
454 | pr_warn("Netlink buffer has to be <= 128kB\n"); | ||
455 | return -EINVAL; | ||
456 | } | ||
457 | |||
458 | ret = register_pernet_subsys(&ulog_tg_net_ops); | ||
459 | if (ret) | ||
460 | goto out_pernet; | ||
461 | |||
462 | ret = xt_register_target(&ulog_tg_reg); | ||
463 | if (ret < 0) | ||
464 | goto out_target; | ||
465 | |||
466 | if (nflog) | ||
467 | nf_log_register(NFPROTO_IPV4, &ipt_ulog_logger); | ||
468 | |||
469 | return 0; | ||
470 | |||
471 | out_target: | ||
472 | unregister_pernet_subsys(&ulog_tg_net_ops); | ||
473 | out_pernet: | ||
474 | return ret; | ||
475 | } | ||
476 | |||
477 | static void __exit ulog_tg_exit(void) | ||
478 | { | ||
479 | pr_debug("cleanup_module\n"); | ||
480 | if (nflog) | ||
481 | nf_log_unregister(&ipt_ulog_logger); | ||
482 | xt_unregister_target(&ulog_tg_reg); | ||
483 | unregister_pernet_subsys(&ulog_tg_net_ops); | ||
484 | } | ||
485 | |||
437 | module_init(ulog_tg_init); | 486 | module_init(ulog_tg_init); |
438 | module_exit(ulog_tg_exit); | 487 | module_exit(ulog_tg_exit); |