LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-23-2017, 02:34 AM   #1
aidylewis
LQ Newbie
 
Registered: Apr 2016
Posts: 25

Rep: Reputation: Disabled
iptables firewall


I am trying to limit TCP connections to 10 through an iptables firewall block.

sudo iptables -A OUTPUT -p tcp --syn --dport 80 -m connlimit --connlimit-upto 10 -j ACCEPT

I am running socket statistics but the TCP goes well over 10. What I am doing wrong?

Many Thanks

Aidy
 
Old 06-23-2017, 02:56 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
OUTPUT? Is that really what you intend?

What are the default policies for the intended chain? What rules appear ahead of this one?

The effect of any single iptables rule can not be understood in isolation. A better description of what you are actually trying to accomplish, and a more complete description of your rules would be necessary for anyone to provide an answer.

If not overly long, posting the output of iptables -L -n would be a good place to start.
 
1 members found this post helpful.
Old 06-23-2017, 03:06 AM   #3
aidylewis
LQ Newbie
 
Registered: Apr 2016
Posts: 25

Original Poster
Rep: Reputation: Disabled
Dear Sir/Madam

I am using a Golang load test tool called Vegeta
https://github.com/tsenart/vegeta

We have millions of users who connect to our sites but only several back-end services. This is why I would like to limit the TCP connections in order to create one realistic (back-end) test.
Code:
echo "GET http://open.stage.bbc.co.uk/loadtest/2kb" | vegeta attack -duration=20m -rate=1000 | tee results.bin | vegeta report
Code:
[ec2-user@ip-xxx-xx-xx-xxx ~]$ sudo iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 flags:0x17/0x02 #conn src/32 <= 10
Many Thanks

Aidy

Last edited by aidylewis; 06-23-2017 at 03:28 AM. Reason: Only ssh open to my machine
 
Old 06-23-2017, 03:24 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

Your description does not provide any useful information. Please see the Site FAQ, inparticular this page and the links in it for help in asking a better question.

The tools and your traffic patterns are not relevant to the question. Are you trying to limit incoming connections to your server, or outgoing connections to remote HTTP servers?

The rules you have posted will allow all incoming and outgoing traffic, nothing will be limited.

The INPUT chain ACCEPTS everything.
The OUTPUT chain ACCEPTS anything which matches your single rule, as well as anything which does not match it.

If you are not familiar with iptables usage, this Iptables How To may be a good place to start. You may also find many other resources online.
 
Old 06-23-2017, 03:51 AM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Thanks for editing your post to add code tags.

Very basically, the way iptables rules work is this:

1. Each packet is tested by each rule beginning at top of a chain (INPUT, OUTPUT, etc.). If it matches the rule it is handled by the target of that rule, ACCEPT, REJECT, DROP, another chain...
2. If it does not match a rule, it continues to the next rule, but first match determines the packet's fate.
3. If it reaches the end of the chain without matching any rule, its fate is determined by the default policy.

In your case, the default policy of the OUTPUT chain is ACCEPT, and your single rule's targe tis also ACCEPT. Hence, if it matches it is accepted, and if it does not match it is accepted.

The minimum you would need to do to enforce the rule would be to set the default policy to DROP. That way packets matching your rule would be accepted, all others would be dropped.
 
Old 06-23-2017, 03:51 AM   #6
aidylewis
LQ Newbie
 
Registered: Apr 2016
Posts: 25

Original Poster
Rep: Reputation: Disabled
# Distro RHEL 7.3

> Are you trying to limit incoming connections to your server, or outgoing connections to remote HTTP servers?

I am trying to limit outgoing connections to remote HTTP servers

Code:
# allow 15 telnet connections per client host
iptables -A OUPUT -p  tcp  --syn  --dport  80  -m connlimit --connlimit-above 15 -j REJECT
Code:
$ sudo iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 flags:0x17/0x02 #conn src/32 > 15 reject-with icmp-port-unreachable

Last edited by aidylewis; 06-23-2017 at 03:56 AM.
 
Old 06-23-2017, 04:07 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
We must have posted at the same time... see post above your last...

Using REJECT with a default policy of ACCEPT would work as well.

Selecting a default policy is very important (and the place to start). But drop-everything plus accept-what-you-want-rules is almost always the better approach.
 
Old 06-23-2017, 05:44 AM   #8
aidylewis
LQ Newbie
 
Registered: Apr 2016
Posts: 25

Original Poster
Rep: Reputation: Disabled
# doesn't go above 10 conns
Code:
sudo iptables -A OUTPUT -p tcp --syn --dport 80  -m connlimit --connlimit-above 10 -j REJECT
RPS at 5000 per second will keep connection rate at 10.

Code:
echo "GET http://open.stage.bbc.co.uk/loadtest/2kb" | vegeta attack -duration=20m -rate=5000 | tee results.bin | vegeta report
Thanks for help

Aidy
 
Old 06-23-2017, 01:34 PM   #9
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
Below will allow 10 connections to HTTP and 10 connections to HTTPS.

Since this is your network you must change --connlimit-mask 24 to match your network mask

Code:
iptables -A OUTPUT -p tcp -m tcp -m multiport --port 80,433 -m conntrack --ctstate NEW -j WEB
iptables -N WEB
iptables -A WEB -p tcp --dport 80 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j REJECT --reject-with tcp-reset
iptables -A WEB -p tcp --dport 443 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j REJECT --reject-with tcp-reset
iptables -A WEB -p tcp -j ACCEPT
 
Old 06-27-2017, 08:37 AM   #10
aidylewis
LQ Newbie
 
Registered: Apr 2016
Posts: 25

Original Poster
Rep: Reputation: Disabled
Even with a high RPS rate, my solution is not working.

Flushing iptables, then

Code:
iptables -A OUPUT -p  tcp  --syn  --dport  80  -m connlimit --connlimit-above 15 -j REJECT
I am on EC2 RHEL 7.3 and the keyword 'WEB' (last suggestion) does not seemed to be recognised.

using *ss -s* to view TCP conns

Last edited by aidylewis; 06-27-2017 at 08:44 AM.
 
Old 06-27-2017, 08:51 AM   #11
aidylewis
LQ Newbie
 
Registered: Apr 2016
Posts: 25

Original Poster
Rep: Reputation: Disabled
Socket stats may be showing me the connections but they might not be getting out.

Packets are certainly dropping

Code:
Chain OUTPUT (policy ACCEPT 3554K packets, 321M bytes)
 pkts bytes target     prot opt in     out     source               destination
3028K  182M REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80 flags:0x17/0x02 #conn src/32 > 10 reject-with icmp-port-unreachable
Not sure whether 10 connections are being used or not.

Many Thanks

Aidy
 
Old 06-27-2017, 08:54 AM   #12
aidylewis
LQ Newbie
 
Registered: Apr 2016
Posts: 25

Original Poster
Rep: Reputation: Disabled
Established in ss -s seems constant. I think it is right.
 
  


Reply

Tags
iptables firewall block



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables with iptables-firewall.conf arno's matt3333 Slackware 16 06-28-2007 07:20 AM
Firewall with iptables spank Linux - Networking 1 07-06-2006 10:57 AM
IPTABLES firewall Vs rc firewall netguy2000 Linux - Security 7 02-28-2004 04:31 AM
Need Help with Firewall, iptables!!!! jamesws Linux - Networking 2 02-11-2002 05:56 PM
IPTables Firewall bfloeagle Linux - Security 6 06-19-2001 02:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration