nf_nat git tree
Subsystems (2)
I've put the nf_nat stuff in a git tree. The NAT resync-patches and the FTP hookfn fix are folded into the original patch and I've ported a few more helpers (only PPtP and netbios_ns are still missing) and fixed some small bugs in the previous patches I sent. I've also added module aliases for all helpers so users can switch transparently.
The git tree contains only my changes, so you need to clone Linus' tree first and then pull my tree into it:
git-clonegit://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
git-pull http://people.netfilter.org/~kaber/nf-2.6.20-nat.git/
(BTW, 2.6.20 doesn't mean I necessarily want to get it into 2.6.20, just that its on top of my queued patched for 2.6.20).
Anyone interested in testing should apply the attached workaround for the nf_conntrack_alter_reply problem on top of the git tree.
Changelog and diffstat below.
include/linux/netfilter/nf_conntrack_amanda.h | 10 include/linux/netfilter/nf_conntrack_ftp.h | 20 include/linux/netfilter/nf_conntrack_h323.h | 92 include/linux/netfilter/nf_conntrack_helper_h323_asn1.h | 98 include/linux/netfilter/nf_conntrack_helper_h323_types.h | 951 +++++++ include/linux/netfilter/nf_conntrack_irc.h | 15 include/linux/netfilter/nf_conntrack_sip.h | 44 include/linux/netfilter/nf_conntrack_tftp.h | 20 include/linux/netfilter_ipv4/ip_conntrack_ftp.h | 40 include/net/netfilter/ipv4/nf_conntrack_ipv4.h | 20 include/net/netfilter/nf_conntrack.h | 30 include/net/netfilter/nf_conntrack_core.h | 3 include/net/netfilter/nf_conntrack_expect.h | 7 include/net/netfilter/nf_conntrack_tuple.h | 10 include/net/netfilter/nf_nat.h | 78 include/net/netfilter/nf_nat_core.h | 26 include/net/netfilter/nf_nat_helper.h | 33 include/net/netfilter/nf_nat_protocol.h | 74 include/net/netfilter/nf_nat_rule.h | 38 net/ipv4/netfilter/Kconfig | 98 net/ipv4/netfilter/Makefile | 19 net/ipv4/netfilter/ip_conntrack_helper_h323_asn1.c | 874 ------ net/ipv4/netfilter/ip_conntrack_helper_h323_types.c | 1926 -------------- net/ipv4/netfilter/ipt_MASQUERADE.c | 29 net/ipv4/netfilter/ipt_NETMAP.c | 4 net/ipv4/netfilter/ipt_REDIRECT.c | 6 net/ipv4/netfilter/ipt_SAME.c | 12 net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 7 net/ipv4/netfilter/nf_nat_amanda.c | 79 net/ipv4/netfilter/nf_nat_core.c | 647 +++++ net/ipv4/netfilter/nf_nat_ftp.c | 183 + net/ipv4/netfilter/nf_nat_h323.c | 613 ++++ net/ipv4/netfilter/nf_nat_helper.c | 455 +++ net/ipv4/netfilter/nf_nat_irc.c | 101 net/ipv4/netfilter/nf_nat_proto_icmp.c | 89 net/ipv4/netfilter/nf_nat_proto_tcp.c | 150 + net/ipv4/netfilter/nf_nat_proto_udp.c | 141 + net/ipv4/netfilter/nf_nat_proto_unknown.c | 55 net/ipv4/netfilter/nf_nat_rule.c | 343 ++ net/ipv4/netfilter/nf_nat_sip.c | 251 + net/ipv4/netfilter/nf_nat_standalone.c | 422 +++ net/ipv4/netfilter/nf_nat_tftp.c | 52 net/netfilter/Kconfig | 122 net/netfilter/Makefile | 7 net/netfilter/nf_conntrack_amanda.c | 232 + net/netfilter/nf_conntrack_core.c | 20 net/netfilter/nf_conntrack_expect.c | 45 net/netfilter/nf_conntrack_ftp.c | 21 net/netfilter/nf_conntrack_helper_h323.c | 1812 ++++++++++++++ net/netfilter/nf_conntrack_helper_h323_asn1.c | 874 ++++++ net/netfilter/nf_conntrack_helper_h323_types.c | 1927 +++++++++++++++ net/netfilter/nf_conntrack_irc.c | 277 ++ net/netfilter/nf_conntrack_netlink.c | 48 net/netfilter/nf_conntrack_proto_tcp.c | 2 net/netfilter/nf_conntrack_sip.c | 489 +++ net/netfilter/nf_conntrack_standalone.c | 5 net/netfilter/nf_conntrack_tftp.c | 156 + net/netfilter/xt_CONNMARK.c | 2 58 files changed, 11293 insertions(+), 2911 deletions(-)
Patrick McHardy:
[NETFILTER]: Add NAT support for nf_conntrack
[NETFILTER]: nf_conntrack: add helper function for expectation initialization [NETFILTER]: nf_conntrack/nf_nat: add SIP helper port [NETFILTER]: nf_conntrack/nf_nat: add TFTP helper port [NETFILTER]: nf_conntrack/nf_nat: add amanda helper port [NETFILTER]: nf_conntrack/nf_nat: add H.323 helper port [NETFILTER]: nf_conntrack/nf_nat: add IRC helper port
net/netfilter/nf_conntrack_core.c +1 -1
@@ -855,7 +855,7 @@ void nf_conntrack_alter_reply(struct nf_NF_CT_DUMP_TUPLE (newreply); conntrack->tuplehash[IP_CT_DIR_REPLY ].tuple = *newreply; -if (!conntrack->master && help->expecting ==0 ) +if (!conntrack->master &&0 && help->expecting ==0 ) help->helper =__nf_ct_helper_find (newreply);write_unlock_bh (&nf_conntrack_lock); }
Hi,
Quoted 2 lines · Patrick McHardy
Anyone interested in testing should apply the attached workaround for the
nf_conntrack_alter_replyproblem on top of the git tree.
Attached is a patch which tries to solve both the problem of nf_conntrack_alter_reply and helpers added to the conntrack entries on the fly by expect functions, instead of the workaround.
Some comments:
The flag approach of helpers added dynamically is hairy in the sense that when an expect function adds a helper to the entry *and* the flag is missing (i.e forgotten about), then it leads a clean, nice crash.
alter_reply is solved by allocating a new conntrack entry and replacing the old one with the new. This is expensive, of course: the function checks everything in order to avoid it. We can skip allocating completely if we keep the helper (whatever it is) regardless of the result of NATing the connection, which is implemented by the keep_helper module option of nf_conntrack. (It is the cleanest, but not backward compatible: in theory the helper may change only when DNAT alters the port.) Untested code, so beware!
Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics
H-1525 Budapest 114, POB. 49, Hungary
include/net/netfilter/nf_conntrack.h +1 -1
@@ -137,7 +137,7 @@#define master_ct (conntr) (conntr->master)/* Alter reply tuple (maybe alter helper). */ -extern void +extern struct nf_conn *nf_conntrack_alter_reply (struct nf_conn *conntrack,const struct nf_conntrack_tuple *newreply);
include/net/netfilter/nf_conntrack_expect.h +1 -1
@@ -48,7 +48,7 @@ };#define NF_CT_EXPECT_PERMANENT 0x1 - +#define NF_CT_EXPECT_HELPER 0x2struct nf_conntrack_expect *__nf_conntrack_expect_find (const struct nf_conntrack_tuple *tuple);
net/ipv4/netfilter/nf_nat_core.c +24 -1
@@ -311,11 +311,34 @@get_unique_tuple (&new_tuple, &curr_tuple, range, conntrack, maniptype);if (!nf_ct_tuple_equal (&new_tuple, &curr_tuple)) { +struct nf_conn *ct;struct nf_conntrack_tuple reply;/* Alter conntrack table so will recognize replies. */ nf_ct_invert_tuplepr (&reply, &new_tuple); -nf_conntrack_alter_reply (conntrack, &reply); + ct =nf_conntrack_alter_reply (conntrack, &reply); + +if (ct ==NULL ) +/* Can't do much */ +return NF_DROP ; + +if (ct != conntrack) { +struct nf_conn *tmp; +/* New conntrack with helper */ +if (!have_to_hash) { +/* Unknot the old one */ +write_lock_bh (&nf_nat_lock); +list_del (&info->bysource); +write_unlock_bh (&nf_nat_lock); + have_to_hash =1 ; + } + tmp = conntrack; + conntrack = ct; +nf_conntrack_free (tmp); + + nat =nfct_nat (conntrack); + info = &nat->info; + }/* Non-atomic: we own this at the moment. */ if (maniptype ==IP_NAT_MANIP_SRC )
net/netfilter/nf_conntrack_core.c +111 -31
218 lines not on this page · raw message
net/netfilter/nf_conntrack_helper_h323.c +6 -2
@@ -693,6 +693,7 @@ port, exp); }else {/* Conntrack only */ exp->expectfn = nf_conntrack_h245_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_q931: expect H.245 " );
@@ -805,6 +806,7 @@ taddr, port, exp); }else {/* Conntrack only */ exp->expectfn = nf_conntrack_q931_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_q931: expect Call Forwarding " );
@@ -1298,6 +1300,7 @@ port, exp); }else {/* Conntrack only */ exp->expectfn = nf_conntrack_q931_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_ras: expect Q.931 " );
@@ -1363,6 +1366,7 @@ &ct->tuplehash[!dir].tuple.src.u3, &addr, IPPROTO_UDP, NULL, &port); exp->expectfn = nf_conntrack_ras_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_ras: expect RAS " );
@@ -1558,7 +1562,7 @@nf_conntrack_expect_init (exp, ct->tuplehash[!dir].tuple.src.l3num, &ct->tuplehash[!dir].tuple.src.u3, &addr,IPPROTO_TCP ,NULL , &port); - exp->flags =NF_CT_EXPECT_PERMANENT ; + exp->flags =NF_CT_EXPECT_PERMANENT |NF_CT_EXPECT_HELPER ; exp->expectfn = nf_conntrack_q931_expect;if (nf_conntrack_expect_related (exp) ==0 ) {
@@ -1608,7 +1612,7 @@nf_conntrack_expect_init (exp, ct->tuplehash[!dir].tuple.src.l3num, &ct->tuplehash[!dir].tuple.src.u3, &addr,IPPROTO_TCP ,NULL , &port); - exp->flags =NF_CT_EXPECT_PERMANENT ; + exp->flags =NF_CT_EXPECT_PERMANENT |NF_CT_EXPECT_HELPER ; exp->expectfn = nf_conntrack_q931_expect;if (nf_conntrack_expect_related (exp) ==0 ) {
Quoted 8 lines · Jozsef Kadlecsik
alter_replyis solved by allocating a new conntrack entry and replacing the old one with the new. This is expensive, of course: the function checks everything in order to avoid it. We can skip allocating completely if we keep the helper (whatever it is) regardless of the result of NATing the connection, which is implemented by thekeep_helpermodule option ofnf_conntrack. (It is the cleanest, but not backward compatible: in theory the helper may change only when DNAT alters the port.) Untested code, so beware!
This is crap, of course, I missed a few points: a pointer to skbuff should be passed along, just to attach the new conntrack entry to it. And the whole reallocating is fragile as the new conntrack pointer should be passed upwards too. Sigh.
The big question is, how many guys out there rely on the feature that they can DNAT non-standard ports to the standard port and then the standard helper is attached to the connection by netfilter?
The attached patch fixes alter_reply by allocating large enough conntrack entry in advance (Yasuyuki's suggestion) with the price that all conntrack entries are full sized when NAT is enabled (default). I preserved the 'keep_helper' module parameter from the previous patch so that one can disable always allocating max size entries, but then conntrack won't try to find a new helper after NAT.
Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics
H-1525 Budapest 114, POB. 49, Hungary
include/net/netfilter/nf_conntrack_expect.h +1 -1
@@ -48,7 +48,7 @@ };#define NF_CT_EXPECT_PERMANENT 0x1 - +#define NF_CT_EXPECT_HELPER 0x2struct nf_conntrack_expect *__nf_conntrack_expect_find (const struct nf_conntrack_tuple *tuple);
net/netfilter/nf_conntrack_core.c +64 -33
164 lines not on this page · raw message
net/netfilter/nf_conntrack_helper_h323.c +6 -2
@@ -693,6 +693,7 @@ port, exp); }else {/* Conntrack only */ exp->expectfn = nf_conntrack_h245_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_q931: expect H.245 " );
@@ -805,6 +806,7 @@ taddr, port, exp); }else {/* Conntrack only */ exp->expectfn = nf_conntrack_q931_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_q931: expect Call Forwarding " );
@@ -1298,6 +1300,7 @@ port, exp); }else {/* Conntrack only */ exp->expectfn = nf_conntrack_q931_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_ras: expect Q.931 " );
@@ -1363,6 +1366,7 @@ &ct->tuplehash[!dir].tuple.src.u3, &addr, IPPROTO_UDP, NULL, &port); exp->expectfn = nf_conntrack_ras_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_ras: expect RAS " );
@@ -1558,7 +1562,7 @@nf_conntrack_expect_init (exp, ct->tuplehash[!dir].tuple.src.l3num, &ct->tuplehash[!dir].tuple.src.u3, &addr,IPPROTO_TCP ,NULL , &port); - exp->flags =NF_CT_EXPECT_PERMANENT ; + exp->flags =NF_CT_EXPECT_PERMANENT |NF_CT_EXPECT_HELPER ; exp->expectfn = nf_conntrack_q931_expect;if (nf_conntrack_expect_related (exp) ==0 ) {
@@ -1608,7 +1612,7 @@nf_conntrack_expect_init (exp, ct->tuplehash[!dir].tuple.src.l3num, &ct->tuplehash[!dir].tuple.src.u3, &addr,IPPROTO_TCP ,NULL , &port); - exp->flags =NF_CT_EXPECT_PERMANENT ; + exp->flags =NF_CT_EXPECT_PERMANENT |NF_CT_EXPECT_HELPER ; exp->expectfn = nf_conntrack_q931_expect;if (nf_conntrack_expect_related (exp) ==0 ) {
Quoted 13 lines · Jozsef Kadlecsik
Quoted 8 lines · Jozsef Kadlecsik
alter_replyis solved by allocating a new conntrack entry and replacing the old one with the new. This is expensive, of course: the function checks everything in order to avoid it. We can skip allocating completely if we keep the helper (whatever it is) regardless of the result of NATing the connection, which is implemented by thekeep_helpermodule option ofnf_conntrack. (It is the cleanest, but not backward compatible: in theory the helper may change only when DNAT alters the port.) Untested code, so beware!
Quoted 8 lines
This is crap, of course, I missed a few points: a pointer to skbuff should be passed along, just to attach the new conntrack entry to it. And the whole reallocating is fragile as the new conntrack pointer should be passed upwards too. Sigh.
The big question is, how many guys out there rely on the feature that they can DNAT non-standard ports to the standard port and then the standard helper is attached to the connection by netfilter?
Probably not many, but I'm pretty certain at least some people really use this feature.
Quoted 6 lines
The attached patch fixes
alter_replyby allocating large enough conntrack entry in advance (Yasuyuki's suggestion) with the price that all conntrack entries are full sized when NAT is enabled (default). I preserved the 'keep_helper' module parameter from the previous patch so that one can disable always allocating max size entries, but then conntrack won't try to find a new helper after NAT.
I think it makes sense to let helpers like H.323 specify that they want to assign a helper to an expected connection, although I would prefer a struct nf_conntrack_helper pointer in struct nf_conntrack_expect to get rid of using nf_conntrack_lock in helpers and the manual override functions (something like the attached patch, although it doesn't actually assign the helper yet because of the problems mentioned below).
Quoted 86 lines
static struct
nf_conn* __nf_conntrack_alloc(const structnf_conntrack_tuple*orig,(quoted diff) +21
-27const struct nf_conntrack_tuple *repl, - const struct nf_conntrack_l3proto *l3proto) + const struct nf_conntrack_l3proto *l3proto, + unsigned int flags) { struct nf_conn *conntrack = NULL; u_int32_t features = 0; - struct nf_conntrack_helper *helper; + struct nf_conntrack_helper *helper = NULL; if (unlikely(!nf_conntrack_hash_rnd_initted)) { get_random_bytes(&nf_conntrack_hash_rnd, 4);@@ -574,30 +605,28 @@ /* find features needed by this conntrack. */ features = l3proto->get_features(orig); - /* FIXME: protect helper list per RCU */ - read_lock_bh(&nf_conntrack_lock); - helper = __nf_ct_helper_find(repl); - if (helper) + /* Expectfn wants to set the helper */ + if (flags & NF_CT_EXPECT_HELPER) features |= NF_CT_F_HELP; - read_unlock_bh(&nf_conntrack_lock); - - DEBUGP("nf_conntrack_alloc: features=0x%x\n", features); - - read_lock_bh(&nf_ct_cache_lock); - - if (unlikely(!nf_ct_cache[features].use)) { - DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n", - features); - goto out; + else { + /* FIXME: protect helper list per RCU */ + read_lock_bh(&nf_conntrack_lock); + helper = __nf_ct_helper_find(repl); + if (helper) + features |= NF_CT_F_HELP; + read_unlock_bh(&nf_conntrack_lock); + + /* NAT may change (i.e. add) helper: + * enable helper support in general */ + if ((features & NF_CT_F_NAT) && !keep_helper) + features |= NF_CT_F_HELP; } - - conntrack = kmem_cache_alloc(nf_ct_cache[features].cachep, GFP_ATOMIC); + conntrack = __nf_ct_alloc_feature(features); if (conntrack == NULL) { - DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n"); - goto out; + atomic_dec(&nf_conntrack_count); + return conntrack; } - memset(conntrack, 0, nf_ct_cache[features].size); conntrack->features = features; if (helper) { struct nf_conn_help *help = nfct_help(conntrack);@@ -613,13 +642,8 @@ init_timer(&conntrack->timeout); conntrack->timeout.data = (unsigned long)conntrack; conntrack->timeout.function = death_by_timeout; - read_unlock_bh(&nf_ct_cache_lock); return conntrack; -out: - read_unlock_bh(&nf_ct_cache_lock); - atomic_dec(&nf_conntrack_count); - return conntrack; } struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,@@ -628,7 +652,7 @@ struct nf_conntrack_l3proto *l3proto; l3proto = __nf_ct_l3proto_find(orig->src.l3num); - return __nf_conntrack_alloc(orig, repl, l3proto); + return __nf_conntrack_alloc(orig, repl, l3proto, 0); } void nf_conntrack_free(struct nf_conn *conntrack)
Review line 688
Quoted 7 lines
(quoted diff) +3
-1@@ -659,7 +683,12 @@ return NULL; } - conntrack = __nf_conntrack_alloc(tuple, &repl_tuple, l3proto); + read_lock_bh(&nf_conntrack_lock); + exp = find_expectation(tuple); + read_unlock_bh(&nf_conntrack_lock);
This leaves a race between looking up the expectation and assigning it to the conntrack and putting it on the unconfirmed list. If the helper is unloaded during this race it won't be able to clean up the reference and we'll get a use-after-free. The same problem exists in the helper lookup in __nf_conntrack_alloc(), we need to hold the lock until the conntrack is on the unconfirmed list. A second problem with the helper lookup on allocation is that nf_conntrack_netlink will (unlike ip_conntrack_netlink) automatically assign a helper to new conntracks.
IMO this just shows the real problem with the features allocation scheme, we don't know everything in advance (especially considering nf_conntrack_netlink), and trying to work around it results in these problems. I really can't see a clean solution while keeping the features allocation. The two obvious other solutions are to either always allocate for everything as IPv4 connection tracking does, or use pointers and extra allocations (or ct_extend, but I can't really remember too much about it).
include/linux/netfilter/nf_conntrack_h323.h +0 -4
@@ -32,10 +32,6 @@ struct nf_conn;extern int get_h225_addr (struct nf_conn *ct,unsigned char *data,TransportAddress *taddr,union nf_conntrack_address *addr,__be16 *port); -extern void nf_conntrack_h245_expect (struct nf_conn *new, -struct nf_conntrack_expect *this); -extern void nf_conntrack_q931_expect (struct nf_conn *new, -struct nf_conntrack_expect *this); extern int (*set_h245_addr_hook) (struct sk_buff **pskb, unsigned char **data, int dataoff, H245_TransportAddress *taddr,
include/net/netfilter/nf_conntrack_expect.h +3 -0
@@ -22,6 +22,9 @@ struct nf_conntrack_expectvoid (*expectfn)(struct nf_conn *new,struct nf_conntrack_expect *this); +/* Helper to assign to new connection */ +struct nf_conntrack_helper *helper; +/* The conntrack of the master connection */ struct nf_conn *master;
net/ipv4/netfilter/nf_nat_h323.c +2 -19
@@ -324,18 +324,6 @@ static int nat_t120(struct sk_buff **pskreturn 0 ; } -/**************************************************************************** -* This conntrack expect function replaces nf_conntrack_h245_expect() -* which was set by nf_conntrack_helper_h323.c. It calls both -* nf_nat_follow_master() and nf_conntrack_h245_expect() -****************************************************************************/ -static void ip_nat_h245_expect (struct nf_conn *new, -struct nf_conntrack_expect *this) -{ -nf_nat_follow_master (new, this); -nf_conntrack_h245_expect (new, this); -} -/****************************************************************************/ static int nat_h245(struct sk_buff **pskb, struct nf_conn *ct, enum ip_conntrack_info ctinfo,
@@ -349,7 +337,7 @@ static int nat_h245(struct sk_buff **psk/* Set expectations for NAT */ exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port; - exp->expectfn = ip_nat_h245_expect; + exp->expectfn = nf_nat_follow_master; exp->dir = !dir;/* Check existing expects */
@@ -399,7 +387,7 @@ static void ip_nat_q931_expect(struct nfif (this->tuple.src.u3.ip !=0 ) {/* Only accept calls from GK */ nf_nat_follow_master (new, this); - goto out; +return ; }/* This must be a fresh one. */
@@ -420,9 +408,6 @@ static void ip_nat_q931_expect(struct nf/* hook doesn't matter, but it has to do destination manip */ nf_nat_setup_info (new, &range,NF_IP_PRE_ROUTING ); - - out: -nf_conntrack_q931_expect (new, this); }/****************************************************************************/
@@ -510,8 +495,6 @@ static void ip_nat_callforwarding_expect/* hook doesn't matter, but it has to do destination manip */ nf_nat_setup_info (new, &range,NF_IP_PRE_ROUTING ); - -nf_conntrack_q931_expect (new, this); }/****************************************************************************/
net/netfilter/nf_conntrack_expect.c +1 -0
@@ -211,6 +211,7 @@ void nf_conntrack_expect_init(struct nf_ exp->flags =0 ; exp->expectfn =NULL ; + exp->helper =NULL ; exp->tuple.src.l3num = family; exp->tuple.dst.protonum = proto; exp->mask.src.l3num =0xFFFF ;
net/netfilter/nf_conntrack_helper.c +2 -1
@@ -128,7 +128,8 @@ void nf_conntrack_helper_unregister(stru/* Get rid of expectations */ list_for_each_entry_safe (exp, tmp, &nf_conntrack_expect_list, list) {struct nf_conn_help *help =nfct_help (exp->master); -if (help->helper == me &&del_timer (&exp->timeout)) { +if ((exp->helper == me || help->helper == me) && +del_timer (&exp->timeout)) {nf_ct_unlink_expect (exp);nf_conntrack_expect_put (exp); }
net/netfilter/nf_conntrack_helper_h323.c +9 -45
152 lines not on this page · raw message
Quoted 6 lines · Patrick McHardy
Quoted 3 lines
The big question is, how many guys out there rely on the feature that they can DNAT non-standard ports to the standard port and then the standard helper is attached to the connection by netfilter?
Probably not many, but I'm pretty certain at least some people really use this feature.
Yes, it's a nice feature and the only "workaround" would be to register the helper on all required non-standard ports.
Quoted 7 lines
Quoted 6 lines
The attached patch fixes
alter_replyby allocating large enough conntrack entry in advance (Yasuyuki's suggestion) with the price that all conntrack entries are full sized when NAT is enabled (default). I preserved the 'keep_helper' module parameter from the previous patch so that one can disable always allocating max size entries, but then conntrack won't try to find a new helper after NAT.
Quoted 6 lines
I think it makes sense to let helpers like H.323 specify that they want to assign a helper to an expected connection, although I would prefer a struct
nf_conntrack_helperpointer in structnf_conntrack_expectto get rid of usingnf_conntrack_lockin helpers and the manual override functions (something like the attached patch, although it doesn't actually assign the helper yet because of the problems mentioned below).
In that case there is no need for the NF_CT_EXPECT_HELPER flag because the non NULL helper pointer in struct nf_conntrack_expect can be used to figure out the required conntrack features.
Quoted 8 lines
This leaves a race between looking up the expectation and assigning it to the conntrack and putting it on the unconfirmed list. If the helper is unloaded during this race it won't be able to clean up the reference and we'll get a use-after-free. The same problem exists in the helper lookup in
__nf_conntrack_alloc(), we need to hold the lock until the conntrack is on the unconfirmed list. A second problem with the helper lookup on allocation is thatnf_conntrack_netlinkwill (unlikeip_conntrack_netlink) automatically assign a helper to new conntracks.
Yes, and the present code in 2.6 suffers the same race condition in __nf_conntrack_alloc().
Quoted 8 lines
IMO this just shows the real problem with the features allocation scheme, we don't know everything in advance (especially considering
nf_conntrack_netlink), and trying to work around it results in these problems. I really can't see a clean solution while keeping the features allocation. The two obvious other solutions are to either always allocate for everything as IPv4 connection tracking does, or use pointers and extra allocations (orct_extend, but I can't really remember too much about it).
We can move the actual setting of the helper from __nf_conntrack_alloc() to the write-locked region in init_conntrack() with the price of the duplicated search on the helper list.
As of ctnetlink_new_conntrack(), we can also assign the helper when adding the entry to the hash table.
Attached is two patches which implements the above, over your git tree. The first one with the NF_CT_EXPECT_HELPER flag and the second one with the helper pointer in struct nf_conntrack_expect.
Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics
H-1525 Budapest 114, POB. 49, Hungary
include/net/netfilter/nf_conntrack_expect.h +1 -1
@@ -48,7 +48,7 @@ };#define NF_CT_EXPECT_PERMANENT 0x1 - +#define NF_CT_EXPECT_HELPER 0x2struct nf_conntrack_expect *__nf_conntrack_expect_find (const struct nf_conntrack_tuple *tuple);
net/netfilter/nf_conntrack_core.c +86 -40
211 lines not on this page · raw message
net/netfilter/nf_conntrack_helper_h323.c +6 -2
@@ -693,6 +693,7 @@ port, exp); }else {/* Conntrack only */ exp->expectfn = nf_conntrack_h245_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_q931: expect H.245 " );
@@ -805,6 +806,7 @@ taddr, port, exp); }else {/* Conntrack only */ exp->expectfn = nf_conntrack_q931_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_q931: expect Call Forwarding " );
@@ -1298,6 +1300,7 @@ port, exp); }else {/* Conntrack only */ exp->expectfn = nf_conntrack_q931_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_ras: expect Q.931 " );
@@ -1363,6 +1366,7 @@ &ct->tuplehash[!dir].tuple.src.u3, &addr, IPPROTO_UDP, NULL, &port); exp->expectfn = nf_conntrack_ras_expect; + exp->flags =NF_CT_EXPECT_HELPER ;if (nf_conntrack_expect_related (exp) ==0 ) {DEBUGP ("nf_ct_ras: expect RAS " );
@@ -1558,7 +1562,7 @@nf_conntrack_expect_init (exp, ct->tuplehash[!dir].tuple.src.l3num, &ct->tuplehash[!dir].tuple.src.u3, &addr,IPPROTO_TCP ,NULL , &port); - exp->flags =NF_CT_EXPECT_PERMANENT ; + exp->flags =NF_CT_EXPECT_PERMANENT |NF_CT_EXPECT_HELPER ; exp->expectfn = nf_conntrack_q931_expect;if (nf_conntrack_expect_related (exp) ==0 ) {
@@ -1608,7 +1612,7 @@nf_conntrack_expect_init (exp, ct->tuplehash[!dir].tuple.src.l3num, &ct->tuplehash[!dir].tuple.src.u3, &addr,IPPROTO_TCP ,NULL , &port); - exp->flags =NF_CT_EXPECT_PERMANENT ; + exp->flags =NF_CT_EXPECT_PERMANENT |NF_CT_EXPECT_HELPER ; exp->expectfn = nf_conntrack_q931_expect;if (nf_conntrack_expect_related (exp) ==0 ) {
include/linux/netfilter/nf_conntrack_h323.h +0 -4
@@ -32,10 +32,6 @@extern int get_h225_addr (struct nf_conn *ct,unsigned char *data,TransportAddress *taddr,union nf_conntrack_address *addr,__be16 *port); -extern void nf_conntrack_h245_expect (struct nf_conn *new, -struct nf_conntrack_expect *this); -extern void nf_conntrack_q931_expect (struct nf_conn *new, -struct nf_conntrack_expect *this); extern int (*set_h245_addr_hook) (struct sk_buff **pskb, unsigned char **data, int dataoff, H245_TransportAddress *taddr,
include/net/netfilter/nf_conntrack_expect.h +3 -1
@@ -22,6 +22,9 @@void (*expectfn)(struct nf_conn *new,struct nf_conntrack_expect *this); +/* Helper to assign to new connection */ +struct nf_conntrack_helper *helper; +/* The conntrack of the master connection */ struct nf_conn *master;
@@ -49,7 +52,6 @@#define NF_CT_EXPECT_PERMANENT 0x1 -struct nf_conntrack_expect *__nf_conntrack_expect_find (const struct nf_conntrack_tuple *tuple);
net/ipv4/netfilter/nf_nat_h323.c +2 -19
@@ -324,18 +324,6 @@return 0 ; } -/**************************************************************************** -* This conntrack expect function replaces nf_conntrack_h245_expect() -* which was set by nf_conntrack_helper_h323.c. It calls both -* nf_nat_follow_master() and nf_conntrack_h245_expect() -****************************************************************************/ -static void ip_nat_h245_expect (struct nf_conn *new, -struct nf_conntrack_expect *this) -{ -nf_nat_follow_master (new, this); -nf_conntrack_h245_expect (new, this); -} -/****************************************************************************/ static int nat_h245(struct sk_buff **pskb, struct nf_conn *ct, enum ip_conntrack_info ctinfo,
@@ -349,7 +337,7 @@/* Set expectations for NAT */ exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port; - exp->expectfn = ip_nat_h245_expect; + exp->expectfn = nf_nat_follow_master; exp->dir = !dir;/* Check existing expects */
@@ -399,7 +387,7 @@if (this->tuple.src.u3.ip !=0 ) {/* Only accept calls from GK */ nf_nat_follow_master (new, this); - goto out; +return ; }/* This must be a fresh one. */
@@ -420,9 +408,6 @@/* hook doesn't matter, but it has to do destination manip */ nf_nat_setup_info (new, &range,NF_IP_PRE_ROUTING ); - - out: -nf_conntrack_q931_expect (new, this); }/****************************************************************************/
@@ -510,8 +495,6 @@/* hook doesn't matter, but it has to do destination manip */ nf_nat_setup_info (new, &range,NF_IP_PRE_ROUTING ); - -nf_conntrack_q931_expect (new, this); }/****************************************************************************/
net/netfilter/nf_conntrack_core.c +89 -40
214 lines not on this page · raw message
net/netfilter/nf_conntrack_expect.c +1 -0
@@ -211,6 +211,7 @@ exp->flags =0 ; exp->expectfn =NULL ; + exp->helper =NULL ; exp->tuple.src.l3num = family; exp->tuple.dst.protonum = proto; exp->mask.src.l3num =0xFFFF ;
net/netfilter/nf_conntrack_helper.c +2 -1
@@ -128,7 +128,8 @@/* Get rid of expectations */ list_for_each_entry_safe (exp, tmp, &nf_conntrack_expect_list, list) {struct nf_conn_help *help =nfct_help (exp->master); -if (help->helper == me &&del_timer (&exp->timeout)) { +if ((exp->helper == me || help->helper == me) && +del_timer (&exp->timeout)) {nf_ct_unlink_expect (exp);nf_conntrack_expect_put (exp); }
net/netfilter/nf_conntrack_helper_h323.c +9 -45
152 lines not on this page · raw message