public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] iptables: how can I include multiple hosts/IPs in "-s" and "-d"?
@ 2010-04-05 17:32 Jarry
  2010-04-06  6:29 ` Kostyantyn
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jarry @ 2010-04-05 17:32 UTC (permalink / raw
  To: gentoo-user

Hi

I'd like to ask if there is some way to include multiple discrete
hosts/IP's in --source and --destination options of iptables.

I'm trying to write firewall rules for my server, but it has
12 IP's from different segments (and maybe it gets a few more
later), and the script grows up as I have to write nearly
identical rules with difference only in -s/-d IP's.

What I'm looking for is a way to define some variable at the
beginning of my script, like MY_IP="IP1 IP2 IP3 IP4..." and
later to use is in rules (iptables -A INPUT -s $MY_IP...).
But I do not know how to use it. As far as I understand it,
--source/--destination accepts only single IP's or continuous
IP-segments...

Jarry

-- 
_______________________________________________________________
This mailbox accepts e-mails only from selected mailing-lists!
Everything else is considered to be spam and therefore deleted.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [gentoo-user] iptables: how can I include multiple hosts/IPs in "-s" and "-d"?
  2010-04-05 17:32 [gentoo-user] iptables: how can I include multiple hosts/IPs in "-s" and "-d"? Jarry
@ 2010-04-06  6:29 ` Kostyantyn
  2010-04-06 10:28 ` Alex Schuster
  2010-04-09 17:24 ` Stefan Schulte
  2 siblings, 0 replies; 4+ messages in thread
From: Kostyantyn @ 2010-04-06  6:29 UTC (permalink / raw
  To: gentoo-user

On Mon, 2010-04-05 at 19:32 +0200, Jarry wrote:
> Hi
> 
> I'd like to ask if there is some way to include multiple discrete
> hosts/IP's in --source and --destination options of iptables.
> 
> I'm trying to write firewall rules for my server, but it has
> 12 IP's from different segments (and maybe it gets a few more
> later), and the script grows up as I have to write nearly
> identical rules with difference only in -s/-d IP's.
> 
> What I'm looking for is a way to define some variable at the
> beginning of my script, like MY_IP="IP1 IP2 IP3 IP4..." and
> later to use is in rules (iptables -A INPUT -s $MY_IP...).
> But I do not know how to use it. As far as I understand it,
> --source/--destination accepts only single IP's or continuous
> IP-segments...

You can do something like:
(100) iptables -N IP_SET_CHECK
(110) iptables -A IP_SET_CHECK -s $IP1 -j RETURN
(120) iptables -A IP_SET_CHECK -s $IP2 -j RETURN
(130) iptables -A IP_SET_CHECK -s $IP3/16 -j RETURN
(140) iptables -A IP_SET_CHECK -s $IP4 -j RETURN
(150) iptables -A IP_SET_CHECK -j DROP
 
(210) iptables -A INPUT -j IP_SET_CHECK
(220) iptables -A INPUT some other rules....
(230) iptables -A INPUT some other rules....

So, when it comes to the the line 210, it will start checking newly
created chain IP_SET_CHECK. If it won't find appropriate rule it will be
dropped at the line (150), but if manages to find one, it will return to
the line 220 and will continue looking for "-j ACCEPT" or "-j DROP".

The same applies for the OUTPUT chain.

> Jarry
> 





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [gentoo-user] iptables: how can I include multiple hosts/IPs in "-s" and "-d"?
  2010-04-05 17:32 [gentoo-user] iptables: how can I include multiple hosts/IPs in "-s" and "-d"? Jarry
  2010-04-06  6:29 ` Kostyantyn
@ 2010-04-06 10:28 ` Alex Schuster
  2010-04-09 17:24 ` Stefan Schulte
  2 siblings, 0 replies; 4+ messages in thread
From: Alex Schuster @ 2010-04-06 10:28 UTC (permalink / raw
  To: gentoo-user

Jarry writes:

> I'd like to ask if there is some way to include multiple discrete
> hosts/IP's in --source and --destination options of iptables.
> 
> I'm trying to write firewall rules for my server, but it has
> 12 IP's from different segments (and maybe it gets a few more
> later), and the script grows up as I have to write nearly
> identical rules with difference only in -s/-d IP's.
> 
> What I'm looking for is a way to define some variable at the
> beginning of my script, like MY_IP="IP1 IP2 IP3 IP4..." and
> later to use is in rules (iptables -A INPUT -s $MY_IP...).
> But I do not know how to use it. As far as I understand it,
> --source/--destination accepts only single IP's or continuous
> IP-segments...

Well, as your iptables script is probably written in bash, you can do 
loops as you like:

myIPs="IP1 IP2 IP3 IP4 ..."
for ip in $myIPs do   # use $myIPs here, not "$myIPs"!
	iptables -A INPUT -s $ip ...
done

	Wonko



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [gentoo-user] iptables: how can I include multiple hosts/IPs in "-s" and "-d"?
  2010-04-05 17:32 [gentoo-user] iptables: how can I include multiple hosts/IPs in "-s" and "-d"? Jarry
  2010-04-06  6:29 ` Kostyantyn
  2010-04-06 10:28 ` Alex Schuster
@ 2010-04-09 17:24 ` Stefan Schulte
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Schulte @ 2010-04-09 17:24 UTC (permalink / raw
  To: gentoo-user

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

Hi,


you can define a rule like that:

iptables -A FORWARD -s 192.168.235.43,192.168.235.46 -d
10.0.0.1,192.168.0.1 -j ACCEPT

it will create 4 rules.

be sure to activate Networking support->Networking options->Network
packet filtering framework->Core Netfilter Configuration->iprange
address range match support

Now you can do something like

iptables -A FORWARD -m iprange --src-range '10.0.0.1-10.0.0.44' -j
ACCEPT

hope it helps

-Stefan

On Mon, Apr 05, 2010 at 07:32:17PM +0200, Jarry wrote:
> Hi
> 
> I'd like to ask if there is some way to include multiple discrete
> hosts/IP's in --source and --destination options of iptables.
> 
> I'm trying to write firewall rules for my server, but it has
> 12 IP's from different segments (and maybe it gets a few more
> later), and the script grows up as I have to write nearly
> identical rules with difference only in -s/-d IP's.
> 
> What I'm looking for is a way to define some variable at the
> beginning of my script, like MY_IP="IP1 IP2 IP3 IP4..." and
> later to use is in rules (iptables -A INPUT -s $MY_IP...).
> But I do not know how to use it. As far as I understand it,
> --source/--destination accepts only single IP's or continuous
> IP-segments...
> 
> Jarry
> 
> -- 
> _______________________________________________________________
> This mailbox accepts e-mails only from selected mailing-lists!
> Everything else is considered to be spam and therefore deleted.
> 

[-- Attachment #2: Type: application/pgp-signature, Size: 665 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-04-09 17:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-05 17:32 [gentoo-user] iptables: how can I include multiple hosts/IPs in "-s" and "-d"? Jarry
2010-04-06  6:29 ` Kostyantyn
2010-04-06 10:28 ` Alex Schuster
2010-04-09 17:24 ` Stefan Schulte

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox