Skip to contentSkip to conversation

flow match

PATCH In review Josh Samuelson 4 participants 9 messages Jun 28, 2005 – Jul 13, 2005 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 flow match

Greetings all,

This patch requires Pablo Neira's conntrack event API patch. It's been many months since I've posted, but I've been tracking the changes that have been made and making the necessary revisions to the code. Thanks Pablo for the heads-up on my silly misuses of the locks, hopefully I've got those cleared up. :) I also added the ability to track new and established flows to the module. Also I used Patrick McHardy's iterative state method of looping over the hashes for the proc files. I figured it was high time to submit it again.

Quoting the man page for the match:

This module allows you to match a packet when the specified number of known protocol connections is exceeded. The matches can be made against generic IP, ICMP, TCP or UDP flow counters. This match can be used in all tables but raw.

Matches when >= n flows exist

--DIR-PROTOCOL[-TYPE] [!] n
where DIR is orig or repl for original and reply directions respectively. PROTOCOL is ip, icmp, tcp or udp. TYPE is n or e. TYPE n is for new connections. TYPE e is for established connections. Specifying no TYPE results in the comparison being made for all PROTOCOL connections regardless of TYPE.

The patch should apply to 2.6.12 and iptables v1.3.1. Though I'll be away from my email for the next week, I would appreciate any feedback. Questions? Comments? Useful/Not?

Cheers,
Josh

include/linux/netfilter_ipv4/ip_conntrack_flow.h +68 -0
diff -Nru a/include/linux/netfilter_ipv4/ip_conntrack_flow.h b/include/linux/netfilter_ipv4/ip_conntrack_flow.h
@@ -0,0 +1,68 @@
+#ifndef _IP_CONNTRACK_FLOW_H
+#define _IP_CONNTRACK_FLOW_H
+
+#define PROC_NAME_ORIG "ip_conntrack_flow_orig"
+#define PROC_NAME_REPL "ip_conntrack_flow_repl"
+#define INCREMENT_WITHOUT_OVERFLOW(c) if (c < ((1 << (sizeof(c) * 8)) - 1)) c++
+
+enum { IPCT_NONE };
+
+typedef enum {
+	INITIALIZE,
+	MISSING_ORIG_BIT = 0,
+	MISSING_ORIG = (1 << MISSING_ORIG_BIT),
+	MISSING_REPL_BIT = 1,
+	MISSING_REPL = (1 << MISSING_REPL_BIT),
+	MISSING_MASK = (MISSING_ORIG | MISSING_REPL),
+	ALLOC_ORIG_BIT = 2,
+	ALLOC_ORIG = (1 << ALLOC_ORIG_BIT),
+	ALLOC_REPL_BIT = 3,
+	ALLOC_REPL = (1 << ALLOC_REPL_BIT),
+	ALLOC_MASK = (ALLOC_ORIG | ALLOC_REPL),
+	SC_NULL_BIT = 4,
+	SC_NULL = (1 << SC_NULL_BIT)
+} ipct_flow_counter_error_t; 
+
+struct ipct_flow_status_cache
+{
+	struct ip_conntrack *ct;
+	unsigned long status;
+	struct list_head dir[IP_CT_DIR_MAX];
+};
+
+/* A `ipct_flow' is a structure containing the IP connection count
+ * on various IP protocols.
+ * _n is for new connections
+ * _e is for established connections
+ */
+
+struct ipct_flow
+{
+	struct list_head list,
+			 sc;
+	u_int32_t ip;
+	u_int16_t ip_n,
+		  ip_e,
+		  icmp_n,
+		  icmp_e,
+		  tcp_n,
+		  tcp_e,
+		  udp_n,
+		  udp_e;
+};
+
+struct ipct_flow_notifier_data
+{
+	struct ip_conntrack *ct;
+	unsigned long new_status;
+};
+
+extern struct list_head *ipct_flow_orig_hash,
+			*ipct_flow_repl_hash;
+extern unsigned int ipct_flow_htable_size;
+extern rwlock_t ipct_flow_lock;
+extern int ipct_flow_register_notifier(struct notifier_block *nb);
+extern int ipct_flow_unregister_notifier(struct notifier_block *nb);
+extern u_int32_t ip_hash_flow_ip(u_int32_t ip);
+
+#endif /* _IP_CONNTRACK_FLOW_H */
include/linux/netfilter_ipv4/ipt_flow.h +83 -0

83 lines not on this page · raw message

net/ipv4/netfilter/Kconfig +33 -0
diff -Nru a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig
@@ -381,6 +381,21 @@
 	  destination IP' or `500pps from any given source IP'  with a single
 	  IPtables rule.
 
+config IP_NF_MATCH_FLOW
+       tristate 'protocol flow counters match support (EXPERIMENTAL)'
+       depends on IP_NF_IPTABLES && IP_NF_FLOW && (IP_NF_FILTER || IP_NF_NAT || IP_NF_MANGLE) && EXPERIMENTAL
+       help
+         `flow' matching allows you to match a packet when the specified
+         number of known protocol connections is exceeded.  The matches
+	 can be made against generic IP, ICMP, TCP or UDP flow counters.
+	 This match can be used in all tables but raw.
+
+         For example, this match allows you to control the number and type
+         of connections (flows) from hosts in a known local network routing
+         through the machine.
+
+         To compile it as a module, choose M here.  If unsure, say N.
+
 # `filter', generic and specific targets
 config IP_NF_FILTER
 	tristate "Packet filtering"
@@ -708,5 +723,23 @@
 	  
 	  IF unsure, say `N'.
 
+config IP_NF_FLOW
+	tristate "protocol flow counters (EXPERIMENTAL)"
+	depends on IP_NF_CONNTRACK_EVENTS && EXPERIMENTAL
+	help
+	  This option uses the connection tracking event notifiers to
+	  keep protocol flow counters for source and destination IP address.
+	  The protocol counters include generic IP, ICMP, TCP and UDP.
+
+	  If the proc filesystem is built into the kernel
+	  "/proc/net/ip_conntrack_flow_orig" and
+	  "/proc/net/ip_conntrack_flow_repl" can be used to read the flow
+	  count tables.
+
+	  These counters can be used with the "protocol flow counters match
+	  support."
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+
 endmenu
 
net/ipv4/netfilter/Makefile +4 -0
diff -Nru a/net/ipv4/netfilter/Makefile b/net/ipv4/netfilter/Makefile
@@ -27,6 +27,9 @@
 obj-$(CONFIG_IP_NF_NAT_FTP) += ip_nat_ftp.o
 obj-$(CONFIG_IP_NF_NAT_IRC) += ip_nat_irc.o
 
+# connection tracking event objects
+obj-$(CONFIG_IP_NF_FLOW) += ip_conntrack_flow.o
+
 # generic IP tables 
 obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o
 
@@ -62,6 +65,7 @@
 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
 obj-$(CONFIG_IP_NF_MATCH_PHYSDEV) += ipt_physdev.o
 obj-$(CONFIG_IP_NF_MATCH_COMMENT) += ipt_comment.o
+obj-$(CONFIG_IP_NF_MATCH_FLOW) += ipt_flow.o
 
 # targets
 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
net/ipv4/netfilter/ip_conntrack_flow.c +907 -0

907 lines not on this page · raw message

net/ipv4/netfilter/ipt_flow.c +529 -0

529 lines not on this page · raw message

extensions/Makefile +1 -1
diff -Nru a/extensions/Makefile b/extensions/Makefile
@@ -5,7 +5,7 @@
 # header files are present in the include/linux directory of this iptables
 # package (HW)
 #
-PF_EXT_SLIB:=ah addrtype comment connlimit connmark conntrack dscp ecn esp hashlimit helper icmp iprange length limit mac mark multiport owner physdev pkttype realm rpc sctp standard state tcp tcpmss tos ttl udp unclean CLASSIFY CONNMARK DNAT DSCP ECN LOG MARK MASQUERADE MIRROR NETMAP NOTRACK REDIRECT REJECT SAME SNAT TARPIT TCPMSS TOS TRACE TTL ULOG
+PF_EXT_SLIB:=ah addrtype comment connlimit connmark conntrack dscp ecn esp flow hashlimit helper icmp iprange length limit mac mark multiport owner physdev pkttype realm rpc sctp standard state tcp tcpmss tos ttl udp unclean CLASSIFY CONNMARK DNAT DSCP ECN LOG MARK MASQUERADE MIRROR NETMAP NOTRACK REDIRECT REJECT SAME SNAT TARPIT TCPMSS TOS TRACE TTL ULOG
 PF6_EXT_SLIB:=eui64 hl icmpv6 length limit mac mark multiport owner physdev standard tcp udp HL LOG MARK TRACE
 
 # Optionals
extensions/libipt_flow.c +338 -0

338 lines not on this page · raw message

extensions/libipt_flow.man +15 -0
diff -Nru a/extensions/libipt_flow.man b/extensions/libipt_flow.man
@@ -0,0 +1,15 @@
+This module allows you to match a packet when the specified number of known
+protocol connections is exceeded.  The matches can be made against
+generic IP, ICMP, TCP or UDP flow counters.  This match can be used in
+all tables but raw.
+.TP
+.BI "Matches when >= " "n" " flows exist"
+.TP
+.BI "--" "DIR" "-" "PROTOCOL" "[-" "TYPE" "] [!] " "n"
+.IR "" "where " "DIR" " is orig or repl for original and reply directions respectively.  " "PROTOCOL" " is ip, icmp, tcp or udp.  " "TYPE" " is n or e.  " "TYPE" " n is for new connections.  " "TYPE" " e is for established connections.  Specifying no " "TYPE" " results in the comparison being made for all " "PROTOCOL" " connections regardless of " "TYPE" "."
+.TP
+.BI "--" "nm" " [!] " "n.n.n.n" "/" "mm" " or " "n.n.n.n" "/" "m.m.m.m"
+.IR "" "where " "n.n.n.n" " is a network and " "mm" " or " "m.m.m.m" " are mask.  This option creates a flow counting object for the specified subnet; the counters for the relevant protocols will consist of all the counters for the addresses that match the subnet."
+.TP
+.BI "if " "! " "is specified"
+.IR "" "The --" "DIR" "-" "PROTOCOL" "[-" "TYPE" "] matches when the flow count is < " "n" ".  The --" "nm n.n.n.n" "/" "mm" " or " "n.n.n.n" "/" "m.m.m.m" " matches when the packet is not in the specified subnet."
#1 lore raw
Quoted 11 lines · Josh Samuelson

Greetings all,

This patch requires Pablo Neira's conntrack event API patch. It's been many months since I've posted, but I've been tracking the changes that have been made and making the necessary revisions to the code. Thanks Pablo for the heads-up on my silly misuses of the locks, hopefully I've got those cleared up. :) I also added the ability to track new and established flows to the module. Also I used Patrick McHardy's iterative state method of looping over the hashes for the proc files. I figured it was high time to submit it again.

Quoted 6 lines

Quoting the man page for the match:

This module allows you to match a packet when the specified number of known protocol connections is exceeded. The matches can be made against generic IP, ICMP, TCP or UDP flow counters. This match can be used in all tables but raw.

Looks fine, I didn't have time yet to review this stuff in deep though, I'm moving to another appartament.

This stuff clearly supersedes `connlimit' that is a bit raw and very "limited".

@Patrick: Do you agree to add this to pom-ng?

--
Pablo

#2 lore raw
Quoted 4 lines · Pablo Neira

This stuff clearly supersedes `connlimit' that is a bit raw and very "limited".

@Patrick: Do you agree to add this to pom-ng?

There's lots of weird stuff in there, so basically, I don't care much. It looks nice and clean, but quite complex for something I don't even know what it is useful for. Josh, do you have any practical examples where it is useful?

Regards
Patrick

#3 lore raw
Quoted 6 lines · Patrick McHardy
Quoted 4 lines · Pablo Neira

This stuff clearly supersedes `connlimit' that is a bit raw and very "limited".

@Patrick: Do you agree to add this to pom-ng?

Quoted 7 lines

There's lots of weird stuff in there, so basically, I don't care much. It looks nice and clean, but quite complex for something I don't even know what it is useful for. Josh, do you have any practical examples where it is useful?

Regards
Patrick

Hi Patrick,

Yes, we have been using it as a metric for acceptable use on our dorm network. When a machine exceeds some threshold for our rule base, we use my TIMER target to add the machine's MAC address to a table. If a machine's MAC address is found in the TIMER table, it will be marked. All established connections for the machine are unaffected. However, the new connections coming from the machine are denied, unless they are destined for port 80, which we DNAT to an internal web server with instructions (acceptable use policy) and utilities for checking if the machine has a virus or spyware. Once the timer runs out, things are back to normal for the machine. If the machine continues to pound the network and therefore break our acceptable use policy, the TIMER just gets refreshed from the flow match. So yes, by itself, it just gives you the ability to match against the number of new or established connections per source or destination IP address. Oh yeah, also subnets, but I haven't tested that functionality much yet in a real world situation. We've had good results with this setup.

Another use would be where you have a box that mainly does routing with another that mainly does firewalling. As rogue scanning packets flow through the router, they will be dropped by the next hop firewall. With the flow module, these dropped packets will appear to be new connections to the router. Once the new count goes beyond a reasonable level, the source IP address is added to a TIMER target. If the router matches source IP addresses against that TIMER target, drop them at the router. This will allow for automated thwarting of service scanning, lasting however long the TIMER table is configured for. I haven't tried this yet but it seems plausible.

I'm not trying to plug the TIMER target/match, it just makes the flow and other matches more useful in certain transient situations. :)

I would love to answer any questions you may have to the "weird stuff" I've got in there. :) Please be patient for responses though because tomorrow morning I'm off on a big hiking trip and will be well away from my email.

Cheers,
Josh

#4 lore raw
In reply to Josh Samuelson

Thanks Josh;

Veridict:

Useful=Yes

The first patch applies OK. but the second (timer patch) gives compile errors thus:

net/ipv4/netfilter/ipt_timer.c: In function `match':
net/ipv4/netfilter/ipt_timer.c:34: structure has no member named `read_locked_map'
net/ipv4/netfilter/ipt_timer.c:34: structure has no member named `write_locked_map'
net/ipv4/netfilter/ipt_timer.c:34: structure has no member named l' net/ipv4/netfilter/ipt_timer.c:34: structure has no member named read_locked_map'
net/ipv4/netfilter/ipt_timer.c:40: structure has no member named `read_locked_map'
net/ipv4/netfilter/ipt_timer.c:40: structure has no member named `read_locked_map'
net/ipv4/netfilter/ipt_timer.c:40: structure has no member named l' net/ipv4/netfilter/ipt_timer.c: In function checkentry':
net/ipv4/netfilter/ipt_timer.c:59: structure has no member named `read_locked_map'
net/ipv4/netfilter/ipt_timer.c:59: structure has no member named `write_locked_map'
net/ipv4/netfilter/ipt_timer.c:59: structure has no member named l' net/ipv4/netfilter/ipt_timer.c:59: structure has no member named read_locked_map'
net/ipv4/netfilter/ipt_timer.c:61: structure has no member named `read_locked_map'
net/ipv4/netfilter/ipt_timer.c:61: structure has no member named `read_locked_map'
net/ipv4/netfilter/ipt_timer.c:61: structure has no member named `l' make[3]: *** [net/ipv4/netfilter/ipt_timer.o] Error 1 make[2]: *** [net/ipv4/netfilter] Error 2
make[1]: *** [net/ipv4] Error 2
make: *** [net] Error 2

amin

Quoted 30 lines · Josh Samuelson

Greetings all,

This patch requires Pablo Neira's conntrack event API patch. It's been many months since I've posted, but I've been tracking the changes that have been made and making the necessary revisions to the code. Thanks Pablo for the heads-up on my silly misuses of the locks, hopefully I've got those cleared up. :) I also added the ability to track new and established flows to the module. Also I used Patrick McHardy's iterative state method of looping over the hashes for the proc files. I figured it was high time to submit it again.

Quoting the man page for the match:

This module allows you to match a packet when the specified number of known protocol connections is exceeded. The matches can be made against generic IP, ICMP, TCP or UDP flow counters. This match can be used in all tables but raw.

Matches when >= n flows exist

--DIR-PROTOCOL[-TYPE] [!] n
where DIR is orig or repl for original and reply directions respectively. PROTOCOL is ip, icmp, tcp or udp. TYPE is n or e. TYPE n is for new connections. TYPE e is for established connections. Specifying no TYPE results in the comparison being made for all PROTOCOL connections regardless of TYPE.

The patch should apply to 2.6.12 and iptables v1.3.1. Though I'll be away from my email for the next week, I would appreciate any feedback. Questions? Comments? Useful/Not?

#5 lore raw
Quoted 10 lines · Amin Azez

Thanks Josh;

Veridict:

Useful=Yes

The first patch applies OK. but the second (timer patch) gives compile errors thus:

net/ipv4/netfilter/ipt_timer.c: In function `match':

I fixed this by changing net/ipv4/netfilter/ipt_timer.c to replace READ_LOCK with read_lock_bh and READ_UNLOCK with read_unlock_bh etc.

That did the trick

Amin

#6 lore raw

Re: [PATCH 1/2] flow match feedback

In reply to Josh Samuelson
Quoted 27 lines · Josh Samuelson

Greetings all,

This patch requires Pablo Neira's conntrack event API patch. It's been many months since I've posted, but I've been tracking the changes that have been made and making the necessary revisions to the code. Thanks Pablo for the heads-up on my silly misuses of the locks, hopefully I've got those cleared up. :) I also added the ability to track new and established flows to the module. Also I used Patrick McHardy's iterative state method of looping over the hashes for the proc files. I figured it was high time to submit it again.

Quoting the man page for the match:

This module allows you to match a packet when the specified number of known protocol connections is exceeded. The matches can be made against generic IP, ICMP, TCP or UDP flow counters. This match can be used in all tables but raw.

Matches when >= n flows exist

--DIR-PROTOCOL[-TYPE] [!] n
where DIR is orig or repl for original and reply directions respectively. PROTOCOL is ip, icmp, tcp or udp. TYPE is n or e. TYPE n is for new connections. TYPE e is for established connections. Specifying no TYPE results in the comparison being made for all PROTOCOL connections regardless of TYPE.

Quoted 3 lines

The patch should apply to 2.6.12 and iptables v1.3.1. Though I'll be away from my email for the next week, I would appreciate any feedback. Questions? Comments? Useful/Not?

iptables -t filter -A INPUT -p tcp --dport 22 -m flow --orig-tcp-n 3 -j DROP

I imagined that the -n in --orig-tcp-n would mean that the rule condition is only true when being matched against a new connection, howewever when I created my 3rd connection all packets on all connections were dropped.

I guess there should be a rule target that drops the connection and removes the conntrack entry of the new connection as well, DROP seems to permit creation of the new conntrack, hence the rule continuing to apply.

I can get what I expected doing this:
iptables -t filter -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m flow --orig-tcp-n 3 -j DROP

But I am then not sure what the -n was signifying.

By using the second rule with ctstate, I get 4 entries in /proc/net/ip_conntrack dport=22 and one of them is stuck on "unreplied", other new connections do not even enter the conntrack table.

However, I notice that when the conneciton is closed and in TIME_WAIT it still counts towards the limit. While there may be no way around this for UDP where there is no explicit close event, I feel for TCP connections this new module ought to count the connection as closed before the conntrack is destroyed, for whatever happens we expect only a few more packets on the connection and it is a shame to hold up any new connections because of that. Proper close conntracks are perhaps brief enough, but only perhaps.

Amin

#7 lore raw

Re: [PATCH 1/2] flow match for 2.6.11

I'm backporting this to 2.6.11 as I see the conntrack-event-api in pom-ng SVN for 2.6.11 is the same as for 2.6.12 apart from ftp sequence number adjusting takes a 32bit parameter in 2.6.12, and your patches apply cleanly.

I'm just tracking down why function ipct_flow_inc gets called with quite different events parameter under 2.6.11 for a connection than under 2.6.12

It gets called 4 times for a new connection, 2.6.11 (EVENTS is hex)

FLOW INC ipct_flow_inc 341 EVENTS=471[ new=1, related=2] FLOW INC ipct_flow_inc 341 EVENTS=78[ new=1, related=2] FLOW INC ipct_flow_inc 341 EVENTS=401[ new=1, related=2] FLOW INC ipct_flow_inc 341 EVENTS=18[ new=1, related=2]

compared with 2.6.12
FLOW INC ipct_flow_inc 341 EVENTS=471[ new=1, related=2] FLOW INC ipct_flow_inc 341 EVENTS=411[ new=1, related=2]

Anyway, I mention it in case you have any hints.

Amin

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw

Re: [PATCH 1/2] flow match feedback

Quoted 7 lines · Josh Samuelson

Hi Amin,

Quoted 2 lines · Amin Azez

iptables -t filter -A INPUT -p tcp --dport 22 -m flow --orig-tcp-n 3 -j DROP

Quoted 10 lines
Quoted 6 lines

I imagined that the -n in --orig-tcp-n would mean that the rule condition is only true when being matched against a new connection, howewever when I created my 3rd connection all packets on all connections were dropped.

Hmm, I can't recreate this scenario. Is that all the rules you had?

Yes.

Quoted 3 lines

How were you connecting to the machine, ie loopback or another external host?

The machine is a bridge, but I am making direct connections to it from another machine

Quoted 4 lines

Before you make your 3rd connection, what does /proc/net/ip_conntrack_flow_orig list for your orig-tcp-n IP address?

192.168.0.181 IP: 2/2/0 ICMP: 0/0/0 TCP: 2/2/0 UDP: 0/0/0

Quoted 8 lines

The format is:
x.x.x.x IP: T/N/E ICMP: T/N/E TCP: T/N/E UDP: T/N/E

x.x.x.x -- IPv4 address

T       -- Total connection count
N       -- New connection count, only seen traffic in one direction
E       -- Established connection count, seen reply
Quoted 9 lines

The listed rule would match when the number of new connections from some original TCP source >= 3 and the destination port of the connection is port 22. Perhaps it's not clear, but the TCP flow count could include other traffic that isn't destined to port 22 or for that matter the machine itself if it's acting as a router, but is also in the NEW state. NEW state connections have only seen traffic in one direction "[UNREPLIED]" according to the conntrack core.

I realise then that this is just an "all connections" count and not a "count of connections that matches this rule"

I can see that adding ports and other such things would over-complicate things and the ultimate limit would be adding to your module all the rule handling capabilities that iptables already has. It would be desirable from a user point of view to approach the limit, but to do the work would clearly be mad.

I was just about to write such a module when you came along and hoped you had done most of it for me.

My plan was that each such iptables rule would allocate a reference counted counter struct, and set its reference count to 1. This struct holds counters for all conntracks for connections whose packets match the rule, and maybe subcounters for the different ctstates

When the rule matches, the counter is incremented and added to a list whose head is held in a new hash under the conntracks originating tuple. Thus it is a simple matter to decrement all these counters when the conntrack is destroyed, as prevent the connection from being double-counted.

Because the counter is a freely allocated reference counted struct, the conntrack is able to decrease the counter, decrease the reference count and destroy it if needed, as might be the case if iptables has since been unloaded. (Or does IP tables already have such a mechanism that can cause rules to stick around till references to them are freed?)

Obscurely hidden here is that the only clauses of the rule which would affect the counting are those which come before the counting clause. It's nice, but obscure so that I missed it til now.

I can see it might be nice for other rules to remove a connection from a count even though the connection exists, this would imply named counters, rather than counters being associated with a rule.

Maybe we should just extend (or duplicate) connmark so that it also counts how many connections have a given mark? (Although I have also modified connmark so that the flow in each direction can have its own mark set by connmark, I need to submit that patch but its mixed up with some other statistics stuff at the moment)

What do you think?

Amin

Quoted 84 lines

Patrick has already commented on the complexity of the current code but I've been toying with the idea of adding port number counts per each flow, which may allow for what you were intending with the above rule, but with yeah... more complexity.

Quoted 5 lines

I can get what I expected doing this:
iptables -t filter -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m flow --orig-tcp-n 3 -j DROP

Yup, with that rule the non-NEW packets can now slip through even when there are >= 3 original TCP source connections. You could also do:

iptables -A INPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT iptables -A INPUT -p tcp --dport 22 -m flow --orig-tcp-n 3 -j DROP

But again, rule 2 isn't doing what you probably think it's doing with regards to destination port 22.

Quoted 4 lines

openBut I am then not sure what the -n was signifying.

Has my explanation cleared that up?

Quoted 6 lines

By using the second rule with ctstate, I get 4 entries in /proc/net/ip_conntrack dport=22 and one of them is stuck on "unreplied", other new connections do not even enter the conntrack table.

Well if that is the case, you should have 3 established connections and 1 new connection which shouldn't cause that rule to match. Again, I'll need to know more about your setup and what the contents of /proc/net/ip_conntrack_flow_orig says for your original source TCP address.

Quoted 9 lines

However, I notice that when the conneciton is closopened and in TIME_WAIT it still counts towards the limit. While there may be no way around this for UDP where there is no explicit close event, I feel for TCP connections this new module ought to count the connection as closed before the conntrack is destroyed, for whatever happens we expect only a few more packets on the connection and it is a shame to hold up any new connections because of that. Proper close conntracks are perhaps brief enough, but only perhaps.

Quoted 4 lines

Amin

The flow counts are based off of conntrack entries being created or destroyed, ie IPCT_NEW/IPCT_RELATED and IPCT_DESTROY events. The number of lines you see in /proc/net/ip_conntrack for some IP all tallied up should match what /proc/net/ip_conntrack_flow_{orig,repl} say for the same IP address. This was pretty much by design to keep the code as streamlined as possible. As you probably know, the TIME_WAIT timeout value can be changed via the sysctl proc file ip_conntrack_tcp_timeout_close_wait. What you bring up can probably be done with the IPCT_PROTOINFO event and keeping yet more state in this module if the connection happens to be a TCP connection. This unfortunately will deviate TCP from the other three protocols in how the flow counts are handled, adding more complexity; I've already had the "too complex" stick shaken at me with this module as it is. doh O_o

Amin, I do appreciate the feedback. Let me know if I've cleared some things up for you.

Cheers,
Josh