netfilter.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: lu zhongda <luzhongda@gmail.com>
To: netfilter@vger.kernel.org
Cc: "Brian J. Murrell" <brian@interlinx.bc.ca>
Subject: Re: How to drop an idle connection with iptables?
Date: Thu, 24 Nov 2011 17:46:07 +0800	[thread overview]
Message-ID: <4ECE125F.8090101@gmail.com> (raw)
In-Reply-To: <jaipdd$irr$1@dough.gmane.org>

[-- Attachment #1: Type: text/plain, Size: 2991 bytes --]

Hi Brian:
     First very sorry for confusion, I will explain it in details.
     My question is:
     " Is it possible to configure iptables to monitor activity on 
socket connections, then close or block connections that haven't had any 
activity for a specified amount of time?"

     As to the individual terms, I define them as follows:
     Connection: Socket tcp connection
     Idle:socket connections haven't had any activity for a specified 
amount of time, for example after connection established, data is 
transmitted through connection in big time intervals.
     Drop:  block and discarded the packets between two endpoints of a 
connection or just close the connection

     At least, I hope iptables can confirm whether a connection is idle 
or not by its rules, this is the key point of my problem.
     I have used conntrack of iptables, it seems not work.

     My rules for conntrack to trace and drop idle connection is:
iptables -AINPUT     -p tcp -m state --stateESTABLISHED-j ACCEPT
     iptables -AOUTPUT-p tcp -m state --stateNEW,ESTABLISHED-j ACCEPT
iptables -AINPUT-p tcp ! --syn -m state --stateNEW-j DROP

     I think from conntrack's perspective, a connection is idle once the 
ESTABLISHED typed item in //proc/net/ip_conntrack///is removed after the 
timeout interval of the item.
     The timeout is defined in 
//proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established/ , 
which is defaulted to 5 days, I change it to a short value for testing, 
such as 1 min.
     the linux shell command is: echo "60" > 
//proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established/
     The timeout for ESTABLISHED type item does works, and the item is 
removed after timeout, however the connection is not blocked or dropped 
at all.

     I used a simple echo alike client-server program to test.  The 
client sends a text message to the server, the server echo the message 
back to the client.
    The socket channel between two sides is kept alive. The listening 
port for server is 9999.

     My environment is:
     OS:  Redhat enterprise linux 5.2, kernel version 2.6.18
     Iptables: v1.3.5

     I attached my rules in /etc/sysconfig/iptables for reference, hope 
it can help.
     Thanks for your continuous help.

On 2011-11-23 20:37, Brian J. Murrell wrote:
> On 11-11-23 05:48 AM, lu zhongda wrote:
>>      I felt the scenario you described is not really what I want to know.
> Probably because your request was unclear.
>
>>      I want to know if there is any way to set up iptables so
>>      that it will drop a connection after that connection has been idle
>> for a specified period of time?
> Define "drop", "connection" and "idle".  And why do you feel you need to
> "drop" them?
>
>>     I have tried connection tracking function of iptables, however it
>> seems not work for my case.
> Please explain why it doesn't.  What are you trying to achieve exactly?
>   Perhaps a real-world use-case might help us understand.
>
> b.
>


[-- Attachment #2: iptables --]
[-- Type: text/plain, Size: 1492 bytes --]

# Generated by iptables-save v1.3.5 on Thu Nov 24 15:19:59 2011
*filter
:INPUT DROP [200:29532]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT 
-A INPUT -p tcp -m tcp --dport 9999 -j LOG --log-prefix "ACCEPT 9999::" 
-A INPUT -p tcp -m tcp --dport 9999 -j ACCEPT 
-A INPUT -p udp -m state --state ESTABLISHED -j ACCEPT 
-A INPUT -p tcp -m state --state ESTABLISHED -j LOG --log-prefix "conn established::" 
-A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT 
-A INPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j LOG --log-prefix "DROP invalid::" 
-A INPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP 
-A INPUT -i eth0 -p tcp -m tcp --dport 9999 -m state --state NEW -m recent --set --name DEFAULT --rsource 
-A INPUT -i eth0 -p tcp -m tcp --dport 9999 -m state --state NEW -m recent --update --seconds 600 --hitcount 2 --name DEFAULT --rsource -j DROP 
-A FORWARD -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP 
-A OUTPUT -p tcp -m tcp --sport 22 -j ACCEPT 
-A OUTPUT -p tcp -m tcp --sport 9999 -j ACCEPT 
-A OUTPUT -p udp -m state --state NEW,ESTABLISHED -j ACCEPT 
-A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j LOG --log-prefix "out conn established::" 
-A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT 
-A OUTPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP 
COMMIT
# Completed on Thu Nov 24 15:19:59 2011

  reply	other threads:[~2011-11-24  9:46 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-23 10:48 How to drop an idle connection with iptables? lu zhongda
2011-11-23 12:37 ` Brian J. Murrell
2011-11-24  9:46   ` lu zhongda [this message]
2011-11-24 11:30     ` Brian J. Murrell
2011-11-25  5:37       ` lu zhongda
2011-11-25 11:16         ` Brian J. Murrell
2011-11-25 13:45           ` lu zhongda
2011-11-25 14:20             ` Nikolay Kichukov
2011-11-26 11:32               ` lu zhongda
2011-12-01 10:22                 ` Anatoly Muliarski
2011-12-01 10:39                   ` Jan Engelhardt
2011-11-25 20:01             ` John Haxby
2011-11-26 11:30               ` lu zhongda
2011-11-25  1:14     ` Gao feng
2011-11-25  3:40       ` lu zhongda
2011-11-25  3:41       ` lu zhongda
2011-11-25  3:59         ` lu zhongda
2011-11-25  5:39         ` Gao feng
  -- strict thread matches above, loose matches on Subject: below --
2011-11-22 12:22 陆仲达
2011-11-23  3:27 ` Lloyd Standish

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4ECE125F.8090101@gmail.com \
    --to=luzhongda@gmail.com \
    --cc=brian@interlinx.bc.ca \
    --cc=netfilter@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).