nf_conntrack port of conntrack-event-api
Subsystems (2)
PATCH 1/1 nf_conntrack port of conntrack-event-api
Hi! I've just committed the following patch to the nf_conntrack branch of my netfilter-2.6.14 tree:
[NETFILTER] port conntrack-events from ip_conntrack to nf_conntrack
This adds a notifier chain based event mechanism for nf_conntrack state changes. As opposed to the previous implementations in patch-o-matic, we do no longer need a field in the skb to achieve this.
Thanks to the valuable input from Patrick McHardy and Rusty on the idea of a per_cpu implementation.
---
commit c911fa4cf2d2ba954eeeb81c486a3c856afd22ba
tree 6a430a735a417cf3223fc30b2493357680824ca9
parent feece338f857e94078aac5ada027ecc35d96d4d3
author Harald Welte <[email protected]> Mi, 03 Aug 2005 22:40:10 +0200 committer Harald Welte <[email protected]> Mi, 03 Aug 2005 22:40:10 +0200
include/linux/netfilter/nf_conntrack.h | 88 +++++++++++++++++ include/linux/netfilter/nf_conntrack_core.h | 17 +++ net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 3 + net/ipv4/netfilter/nf_conntrack_proto_icmp.c | 1 net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | 3 + net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c | 1 net/netfilter/Kconfig | 10 ++ net/netfilter/nf_conntrack_core.c | 123 +++++++++++++++++++++++- net/netfilter/nf_conntrack_ftp.c | 12 ++ net/netfilter/nf_conntrack_proto_sctp.c | 2 net/netfilter/nf_conntrack_proto_tcp.c | 4 + net/netfilter/nf_conntrack_proto_udp.c | 3 - net/netfilter/nf_conntrack_standalone.c | 9 ++ 13 files changed, 261 insertions(+), 15 deletions(-)
include/linux/netfilter/nf_conntrack.h +87 -1
@@ -168,7 +168,7 @@ nf_conntrack_tuple_taken(const struct nf/* Return conntrack_info and tuple hash for given skb. */ static inline struct nf_conn * -nf_ct_get (struct sk_buff *skb,enum ip_conntrack_info *ctinfo) +nf_ct_get (const struct sk_buff *skb,enum ip_conntrack_info *ctinfo) { *ctinfo = skb->nfctinfo;return (struct nf_conn *)skb->nfct;
@@ -214,10 +214,96 @@ static inline int nf_ct_is_confirmed(strreturn test_bit (IPS_CONFIRMED_BIT , &ct->status); } +static inline int nf_ct_is_dying (struct nf_conn *ct) +{ +return test_bit (IPS_DYING_BIT , &ct->status); +} +extern unsigned int nf_conntrack_htable_size;#define NF_CT_STAT_INC (count) (__get_cpu_var(nf_conntrack_stat).count++) +#ifdef CONFIG_NF_CONNTRACK_EVENTS +#include <linux/notifier.h> + +struct nf_conntrack_ecache { +struct nf_conn *ct; +unsigned int events; +}; +DECLARE_PER_CPU(struct nf_conntrack_ecache, nf_conntrack_ecache); + +#define CONNTRACK_ECACHE (x) (__get_cpu_var(nf_conntrack_ecache).x) + +extern struct notifier_block *nf_conntrack_chain; +extern struct notifier_block *nf_conntrack_expect_chain; + +static inline int nf_conntrack_register_notifier (struct notifier_block *nb) +{ +return notifier_chain_register (&nf_conntrack_chain, nb); +} + +static inline int nf_conntrack_unregister_notifier (struct notifier_block *nb) +{ +return notifier_chain_unregister (&nf_conntrack_chain, nb); +} + +static inline int +nf_conntrack_expect_register_notifier (struct notifier_block *nb) +{ +return notifier_chain_register (&nf_conntrack_expect_chain, nb); +} + +static inline int +nf_conntrack_expect_unregister_notifier (struct notifier_block *nb) +{ +return notifier_chain_unregister (&nf_conntrack_expect_chain, nb); +} + +static inline void +nf_conntrack_event_cache (enum ip_conntrack_events event, +const struct sk_buff *skb) +{ +struct nf_conntrack_ecache *ecache = + &__get_cpu_var (nf_conntrack_ecache); + +if (unlikely ((struct nf_conn *) skb->nfct != ecache->ct)) { +if (net_ratelimit ()) { +printk (KERN_ERR "ctevent: skb->ct != ecache->ct !!!\n" ); +dump_stack (); + } + } + ecache->events |= event; +} + +extern void +nf_conntrack_deliver_cached_events_for (const struct nf_conn *ct); +extern void nf_conntrack_event_cache_init (const struct sk_buff *skb); + +static inline void nf_conntrack_event (enum ip_conntrack_events event, +struct nf_conn *ct) +{ +if (nf_ct_is_confirmed (ct) && !nf_ct_is_dying (ct)) +notifier_call_chain (&nf_conntrack_chain, event, ct); +} + +static inline void +nf_conntrack_expect_event (enum ip_conntrack_expect_events event, +struct nf_conntrack_expect *exp) +{ +notifier_call_chain (&nf_conntrack_expect_chain, event, exp); +} +#else /* CONFIG_NF_CONNTRACK_EVENTS */ +static inline void nf_conntrack_event_cache (enum ip_conntrack_events event, +const struct sk_buff *skb) {} +static inline void nf_conntrack_event (enum ip_conntrack_events event, +struct nf_conn *ct) {} +static inline void nf_conntrack_deliver_cached_events_for (struct nf_conn *ct) {} +static inline void nf_conntrack_event_cache_init (const struct sk_buff *skb) {} +static inline void +nf_conntrack_expect_event (enum ip_conntrack_expect_events event, +struct nf_conntrack_expect *exp) {} +#endif /* CONFIG_NF_CONNTRACK_EVENTS */ +/* eg. PROVIDES_CONNTRACK(ftp); */ #define PROVIDES_CONNTRACK (name) \ int needs_nf_conntrack_##name; \
include/linux/netfilter/nf_conntrack_core.h +13 -4
@@ -57,12 +57,21 @@ extern int __nf_conntrack_confirm(struct/* Confirm a connection: returns NF_DROP if packet must be dropped. */ static inline int nf_conntrack_confirm (struct sk_buff **pskb) { -if ((*pskb)->nfct - && !nf_ct_is_confirmed ((struct nf_conn *)(*pskb)->nfct)) -return __nf_conntrack_confirm (pskb); -return NF_ACCEPT ; +struct nf_conn *ct = (struct nf_conn *)(*pskb)->nfct; +int ret =NF_ACCEPT ; + +if (ct && !nf_ct_is_confirmed (ct)) + ret =__nf_conntrack_confirm (pskb); +nf_conntrack_deliver_cached_events_for (ct); + +return ret; } +#ifdef CONFIG_NF_CONNTRACK_EVENTS +struct nf_conntrack_ecache ; +extern void __nf_ct_deliver_cached_events (struct nf_conntrack_ecache *ec); +#endif +extern void __nf_conntrack_attach (struct sk_buff *nskb,struct sk_buff *skb);extern struct list_head *nf_conntrack_hash;
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +3 -0
@@ -129,6 +129,7 @@ static unsigned int ipv4_confirm(unsigne const struct net_device *out, int (*okfn)(struct sk_buff *)) { + nf_conntrack_event_cache_init(*pskb);/* We've seen it coming out the other side: confirm it */ return nf_conntrack_confirm (pskb); }
@@ -146,6 +147,7 @@ static unsigned int ipv4_conntrack_help( ct =nf_ct_get (*pskb, &ctinfo);if (ct && ct->helper) {unsigned int ret; +nf_conntrack_event_cache_init (*pskb); ret = ct->helper->help(pskb, (*pskb)->nh.raw - (*pskb)->data + (*pskb)->nh.iph->ihl*4,
@@ -506,6 +508,7 @@ static int init_or_cleanup(int init)return ret; cleanup: +synchronize_net (); ip_ct_attach =NULL ;#ifdef CONFIG_SYSCTL unregister_sysctl_table (nf_ct_ipv4_sysctl_header);
net/ipv4/netfilter/nf_conntrack_proto_icmp.c +1 -0
@@ -109,6 +109,7 @@ static int icmp_packet(struct nf_conn *c ct->timeout.function ((unsigned long )ct); }else {atomic_inc (&ct->proto.icmp.count); +nf_conntrack_event_cache (IPCT_PROTOINFO_VOLATILE , skb);nf_ct_refresh_acct (ct, ctinfo, skb, nf_ct_icmp_timeout); }
net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c +3 -0
@@ -182,6 +182,8 @@ static unsigned int ipv6_confirm(unsignestruct nf_conn *ct;enum ip_conntrack_info ctinfo; +nf_conntrack_event_cache_init (*pskb); +/* This is where we call the helper: as the packet goes out. */ ct =nf_ct_get (*pskb, &ctinfo);if (ct && ct->helper) {
@@ -499,6 +501,7 @@ static int init_or_cleanup(int init)return ret; cleanup: +synchronize_net ();#ifdef CONFIG_SYSCTL unregister_sysctl_table (nf_ct_ipv6_sysctl_header); cleanup_localinops:
net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c +1 -0
@@ -113,6 +113,7 @@ static int icmpv6_packet(struct nf_conn ct->timeout.function ((unsigned long )ct); }else {atomic_inc (&ct->proto.icmp.count); +nf_conntrack_event_cache (IPCT_PROTOINFO_VOLATILE , skb);nf_ct_refresh_acct (ct, ctinfo, skb, nf_ct_icmpv6_timeout); }
net/netfilter/Kconfig +10 -0
@@ -57,6 +57,16 @@ config NF_CONNTRACK_MARK of packets, but this mark value is kept in the conntrack session instead of the individual packets. +config NF_CONNTRACK_EVENTS + bool "Connection tracking events" + depends on NF_CONNTRACK + help + If this option is enabled, the connection tracking code will + provide a notifier chain that can be used by other kernel code + to get notified aboutchanges in the connection tracking state. + + If unsure, say `N'. + config NF_CT_PROTO_SCTP tristate 'SCTP protocol on new connection tracking support (EXPERIMENTAL)' depends on EXPERIMENTAL && NF_CONNTRACK
net/netfilter/nf_conntrack_core.c +118 -5
202 lines not on this page · raw message
net/netfilter/nf_conntrack_ftp.c +8 -4
@@ -404,7 +404,8 @@ static int find_nl_seq(u32 seq, const st }/* We don't update if it's older than what we have. */ -static void update_nl_seq (u32 nl_seq,struct ip_ct_ftp_master *info,int dir) +static void update_nl_seq (u32 nl_seq,struct ip_ct_ftp_master *info,int dir, +struct sk_buff *skb) {unsigned int i, oldest =NUM_SEQ_TO_REMEMBER ;
@@ -418,10 +419,13 @@ static void update_nl_seq(u32 nl_seq, st oldest = i; } -if (info->seq_aft_nl_num[dir] <NUM_SEQ_TO_REMEMBER ) +if (info->seq_aft_nl_num[dir] <NUM_SEQ_TO_REMEMBER ) { info->seq_aft_nl[dir][info->seq_aft_nl_num[dir]++] = nl_seq; -else if (oldest !=NUM_SEQ_TO_REMEMBER ) +nf_conntrack_event_cache (IPCT_HELPINFO_VOLATILE , skb); + }else if (oldest !=NUM_SEQ_TO_REMEMBER ) { info->seq_aft_nl[dir][oldest] = nl_seq; +nf_conntrack_event_cache (IPCT_HELPINFO_VOLATILE , skb); + } } static int help(struct sk_buff **pskb,
@@ -609,7 +613,7 @@ out_update_nl:/* Now if this ends in \n, update ftp info. Seq may have been * adjusted by NAT code. */ if (ends_in_nl) -update_nl_seq (seq, ct_ftp_info,dir); +update_nl_seq (seq, ct_ftp_info, dir, *pskb); out:spin_unlock_bh (&nf_ftp_lock);return ret;
net/netfilter/nf_conntrack_proto_sctp.c +2 -0
@@ -412,6 +412,8 @@ static int sctp_packet(struct nf_conn *c } conntrack->proto.sctp.state = newconntrack; +if (oldsctpstate != newconntrack) +nf_conntrack_event_cache (IPCT_PROTOINFO , skb);write_unlock_bh (&sctp_lock); }
net/netfilter/nf_conntrack_proto_tcp.c +4 -0
@@ -1013,6 +1013,10 @@ static int tcp_packet(struct nf_conn *co ? nf_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];write_unlock_bh (&tcp_lock); +nf_conntrack_event_cache (IPCT_PROTOINFO_VOLATILE , skb); +if (new_state != old_state) +nf_conntrack_event_cache (IPCT_PROTOINFO , skb); +if (!test_bit (IPS_SEEN_REPLY_BIT , &conntrack->status)) {/* If only reply is a RST, we can consider ourselves not to have an established connection: this is a fairly common
net/netfilter/nf_conntrack_proto_udp.c +2 -1
@@ -83,7 +83,8 @@ static int udp_packet(struct nf_conn *conf_ct_refresh_acct (conntrack, ctinfo, skb, nf_ct_udp_timeout_stream);/* Also, more likely to be important, and not a probe */ -set_bit (IPS_ASSURED_BIT , &conntrack->status); +if (!test_and_set_bit (IPS_ASSURED_BIT , &conntrack->status)) +nf_conntrack_event_cache (IPCT_STATUS , skb); }else nf_ct_refresh_acct (conntrack, ctinfo, skb, nf_ct_udp_timeout);
net/netfilter/nf_conntrack_standalone.c +9 -0
@@ -825,6 +825,15 @@ void need_nf_conntrack(void) { } +#ifdef CONFIG_NF_CONNTRACK_EVENTS +EXPORT_SYMBOL_GPL (nf_conntrack_chain); +EXPORT_SYMBOL_GPL (nf_conntrack_expect_chain); +EXPORT_SYMBOL_GPL (nf_conntrack_register_notifier); +EXPORT_SYMBOL_GPL (nf_conntrack_unregister_notifier); +EXPORT_PER_CPU_SYMBOL_GPL (nf_conntrack_ecache); +EXPORT_SYMBOL_GPL (nf_conntrack_event_cache_init); +EXPORT_SYMBOL_GPL (nf_conntrack_deliver_cached_events_for); +#endif EXPORT_SYMBOL (nf_conntrack_l3proto_register);EXPORT_SYMBOL (nf_conntrack_l3proto_unregister);EXPORT_SYMBOL (nf_conntrack_protocol_register);
Signature · 5 lines
- Harald Welte <[email protected]> http://netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
Quoted 8 lines · Harald Welte
Hi! I've just committed the following patch to the
nf_conntrackbranch of my netfilter-2.6.14 tree:[NETFILTER] port conntrack-events from
ip_conntracktonf_conntrackThis adds a notifier chain based event mechanism for
nf_conntrackstate changes. As opposed to the previous implementations in patch-o-matic, we do no longer need a field in the skb to achieve this.
The event cache also needs the fixes I sent in the "atomic counter underflow" thread two hours ago.
Regards
Patrick
Quoted 4 lines · Patrick McHardy
Quoted 3 lines · Harald Welte
Hi! I've just committed the following patch to the
nf_conntrackbranch of my netfilter-2.6.14 tree:
Quoted 8 lines
Quoted 5 lines
[NETFILTER] port conntrack-events from
ip_conntracktonf_conntrackThis adds a notifier chain based event mechanism for
nf_conntrackstate changes. As opposed to the previous implementations in patch-o-matic, we do no longer need a field in the skb to achieve this.The event cache also needs the fixes I sent in the "atomic counter underflow" thread two hours ago.
Yes, I'm aware of that. While I fully trust you on your changes, I first want to understand them, then port them to nf_conntrack. Since I'm still officially on holidays visiting my parents, my time is quite limited, but I expect it to be done by tomorrow (friday) evening.
cheers,
Harald
Signature · 5 lines
- Harald Welte <[email protected]> http://netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
Quoted 4 lines · Patrick McHardy
Quoted 3 lines · Harald Welte
Hi! I've just committed the following patch to the
nf_conntrackbranch of my netfilter-2.6.14 tree:
Quoted 8 lines
Quoted 5 lines
[NETFILTER] port conntrack-events from
ip_conntracktonf_conntrackThis adds a notifier chain based event mechanism for
nf_conntrackstate changes. As opposed to the previous implementations in patch-o-matic, we do no longer need a field in the skb to achieve this.The event cache also needs the fixes I sent in the "atomic counter underflow" thread two hours ago.
I've just committed the attached patch to netfilter-2.6.14#nf_conntrack.
Signature · 5 lines
- Harald Welte <[email protected]> http://netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
[NETFILTER]: Fix multiple problems with the conntrack event cache
refcnt underflow: the reference count is decremented when a conntrack entry is removed from the hash but it is not incremented when entering new entries.
missing protection of process context against softirq context: all cache operations need to locally disable softirqs to avoid races. Additionally the event cache can't be initialized when a packet enteres the conntrack code but needs to be initialized whenever we cache an event and the stored conntrack entry doesn't match the current one.
incorrect flushing of the event cache in ip_ct_iterate_cleanup: without real locking we can't flush the cache for different CPUs without incurring races. The cache for different CPUs can only be flushed when no packets are going through the code. ip_ct_iterate_cleanup doesn't need to drop all references, so flushing is moved to the cleanup path.
---
commit 69d90870b705ad24bfab685fc1bb56e2e5d649be
tree 1989bcc9e353b8364155158ec02a743266ae58a8
parent de3cf64608b638e5af3f232383a6eb99f80ceed4
author Harald Welte <[email protected]> Fr, 05 Aug 2005 18:46:16 +0200 committer Harald Welte <[email protected]> Fr, 05 Aug 2005 18:46:16 +0200
include/linux/netfilter/nf_conntrack.h | 26 +++--- include/linux/netfilter/nf_conntrack_core.h | 14 +-- net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 2 net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | 2 net/netfilter/nf_conntrack_core.c | 106 +++++++++--------------- net/netfilter/nf_conntrack_standalone.c | 4 - 6 files changed, 58 insertions(+), 96 deletions(-)
include/linux/netfilter/nf_conntrack.h +12 -14
@@ -228,6 +228,7 @@ extern unsigned int nf_conntrack_htable_ #ifdef CONFIG_NF_CONNTRACK_EVENTS #include <linux/notifier.h> +#include <linux/interrupt.h> struct nf_conntrack_ecache { struct nf_conn *ct;
@@ -262,26 +263,24 @@ nf_conntrack_expect_unregister_notifier(return notifier_chain_unregister (&nf_conntrack_expect_chain, nb); } +extern void nf_ct_deliver_cached_events (const struct nf_conn *ct); +extern void __nf_ct_event_cache_init (struct nf_conn *ct); +static inline void nf_conntrack_event_cache (enum ip_conntrack_events event,const struct sk_buff *skb) { -struct nf_conntrack_ecache *ecache = - &__get_cpu_var (nf_conntrack_ecache); +struct nf_conn *ct = (struct nf_conn *)skb->nfct; +struct nf_conntrack_ecache *ecache; -if (unlikely ((struct nf_conn *) skb->nfct != ecache->ct)) { -if (net_ratelimit ()) { -printk (KERN_ERR "ctevent: skb->ct != ecache->ct !!!\n" ); -dump_stack (); - } - } +local_bh_disable (); + ecache = &__get_cpu_var (nf_conntrack_ecache); +if (ct != ecache->ct) +__nf_ct_event_cache_init (ct); ecache->events |= event; +local_bh_enable (); } -extern void -nf_conntrack_deliver_cached_events_for (const struct nf_conn *ct); -extern void nf_conntrack_event_cache_init (const struct sk_buff *skb); -static inline void nf_conntrack_event (enum ip_conntrack_events event,struct nf_conn *ct) {
@@ -300,8 +299,7 @@ static inline void nf_conntrack_event_ca const struct sk_buff *skb) {}static inline void nf_conntrack_event (enum ip_conntrack_events event,struct nf_conn *ct) {} -static inline void nf_conntrack_deliver_cached_events_for (struct nf_conn *ct) {} -static inline void nf_conntrack_event_cache_init (const struct sk_buff *skb) {} +static inline void nf_ct_deliver_cached_events (const struct nf_conn *ct) {}static inline void nf_conntrack_expect_event (enum ip_conntrack_expect_events event,struct nf_conntrack_expect *exp) {}
include/linux/netfilter/nf_conntrack_core.h +5 -9
@@ -60,18 +60,14 @@ static inline int nf_conntrack_confirm(sstruct nf_conn *ct = (struct nf_conn *)(*pskb)->nfct;int ret =NF_ACCEPT ; -if (ct && !nf_ct_is_confirmed (ct)) - ret =__nf_conntrack_confirm (pskb); -nf_conntrack_deliver_cached_events_for (ct); - +if (ct) { +if (!nf_ct_is_confirmed (ct)) + ret =__nf_conntrack_confirm (pskb); +nf_ct_deliver_cached_events (ct); + }return ret; } -#ifdef CONFIG_NF_CONNTRACK_EVENTS -struct nf_conntrack_ecache ; -extern void __nf_ct_deliver_cached_events (struct nf_conntrack_ecache *ec); -#endif -extern void __nf_conntrack_attach (struct sk_buff *nskb,struct sk_buff *skb);extern struct list_head *nf_conntrack_hash;
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +0 -2
@@ -129,7 +129,6 @@ static unsigned int ipv4_confirm(unsigne const struct net_device *out, int (*okfn)(struct sk_buff *)) { - nf_conntrack_event_cache_init(*pskb); /* We've seen it coming out the other side: confirm it */ return nf_conntrack_confirm(pskb); }
@@ -147,7 +146,6 @@ static unsigned int ipv4_conntrack_help( ct =nf_ct_get (*pskb, &ctinfo);if (ct && ct->helper) {unsigned int ret; -nf_conntrack_event_cache_init (*pskb); ret = ct->helper->help(pskb, (*pskb)->nh.raw - (*pskb)->data + (*pskb)->nh.iph->ihl*4,
net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c +0 -2
@@ -182,8 +182,6 @@ static unsigned int ipv6_confirm(unsignestruct nf_conn *ct;enum ip_conntrack_info ctinfo; -nf_conntrack_event_cache_init (*pskb); -/* This is where we call the helper: as the packet goes out. */ ct =nf_ct_get (*pskb, &ctinfo);if (ct && ct->helper) {
net/netfilter/nf_conntrack_core.c +39 -67
151 lines not on this page · raw message
net/netfilter/nf_conntrack_standalone.c +2 -2
@@ -830,9 +830,9 @@ EXPORT_SYMBOL_GPL(nf_conntrack_chain);EXPORT_SYMBOL_GPL (nf_conntrack_expect_chain);EXPORT_SYMBOL_GPL (nf_conntrack_register_notifier);EXPORT_SYMBOL_GPL (nf_conntrack_unregister_notifier); +EXPORT_SYMBOL_GPL (__nf_ct_event_cache_init);EXPORT_PER_CPU_SYMBOL_GPL (nf_conntrack_ecache); -EXPORT_SYMBOL_GPL (nf_conntrack_event_cache_init); -EXPORT_SYMBOL_GPL (nf_conntrack_deliver_cached_events_for); +EXPORT_SYMBOL_GPL (nf_ct_deliver_cached_events);#endif EXPORT_SYMBOL (nf_conntrack_l3proto_register);EXPORT_SYMBOL (nf_conntrack_l3proto_unregister);