<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linux and Virtualization &#187; iptables</title>
	<atom:link href="http://fclose.com/b/linux/tag/iptables/feed/" rel="self" type="application/rss+xml" />
	<link>http://fclose.com/b/linux</link>
	<description>Clear solutions, tutorials and tips on Linux and virtualization from the author&#039;s experience with clusters.</description>
	<lastBuildDate>Thu, 09 Feb 2012 14:49:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Setting Up VPN-like Network Between Several Clusters Using iptables</title>
		<link>http://fclose.com/b/linux/2649/setting-up-vpn-like-network-between-several-clusters-using-iptables/</link>
		<comments>http://fclose.com/b/linux/2649/setting-up-vpn-like-network-between-several-clusters-using-iptables/#comments</comments>
		<pubDate>Fri, 08 Oct 2010 15:01:55 +0000</pubDate>
		<dc:creator>Zhiqiang Ma</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Command line]]></category>
		<category><![CDATA[Download]]></category>
		<category><![CDATA[Fedora]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[Server config]]></category>
		<category><![CDATA[VPN]]></category>

		<guid isPermaLink="false">http://pkill.info/b/?p=2649</guid>
		<description><![CDATA[These slides give a brief introduction to how to set up the VPN-like network between several clusters which connect to each other through Internet: vpn_iptables_v2.pdf Animation is enabled which may help to understand it. Update history Dec. 10, 2010. Update pdf to version 2. Animation is added.]]></description>
			<content:encoded><![CDATA[<p>These slides give a brief introduction to how to set up the VPN-like network between several clusters which connect to each other through Internet:</p>
<p><a href="http://fclose.com/t/go/linux/vpn-iptables-v2.pdf/" rel="nofollow">vpn_iptables_v2.pdf</a></p>
<p>Animation is enabled which may help to understand it.</p>
<p><span style="font-size: x-small;">Update history<br />
Dec. 10, 2010. Update pdf to version 2. Animation is added.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://fclose.com/b/linux/2649/setting-up-vpn-like-network-between-several-clusters-using-iptables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Port Forwarding Using iptables</title>
		<link>http://fclose.com/b/linux/816/port-forwarding-using-iptables/</link>
		<comments>http://fclose.com/b/linux/816/port-forwarding-using-iptables/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 12:53:18 +0000</pubDate>
		<dc:creator>Zhiqiang Ma</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[portforwarding]]></category>
		<category><![CDATA[Server config]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://zhiqiangma.info/?p=816</guid>
		<description><![CDATA[Port forwarding is simple to do with iptables in a Linux box which may probably already being used as the firewall or part of the gateway operatioin. In Linux kernels, port forwarding is achieved by packet filter rules in iptables. Port forwarding Port forwarding also called &#8220;port mapping&#8221; commonly refers to the network address translator [...]]]></description>
			<content:encoded><![CDATA[<p>Port forwarding is simple to do with <em>iptables</em> in a Linux box which may probably already being used as the firewall or part of the gateway operatioin. In Linux kernels, port forwarding is achieved by packet filter rules in iptables.</p>
<h3>Port forwarding</h3>
<p>Port forwarding also called &#8220;port mapping&#8221; commonly refers to the network address translator gateway changing the destination address and/or port of the packet to reach a host within a masqueraded, typically private, network.</p>
<p>Port forwarding can be used to allow remote computers (e.g., public machines on the Internet) to connect to a specific computer within a private network such as local area network (LAN), sothat xternal hosts can communicate with services provided by hosts within a LAN. For example, running a public HTTP server (port 80) on a host within a private LAN, or permitting secure shell <em>ssh</em> (port 22) access to hosts within the private LAN from the Internet.</p>
<p>In Unix/Linux box where port numbers below 1024 can only be listened by software running as root, port forwarding is also used to redirect incoming traffic from a low numbered port to software listening on a higher port. This software can be running as a normal user, which avoids the security risk caused by running as the root user.</p>
<h3>iptables</h3>
<p>iptables is a very powerfull firewall which handles packets based on the type of packet activity and enqueues the packet in one of its builtin &#8216;tables&#8217;. In Linux box, iptables is implemented in Linux kernel as some kernel modules.</p>
<p>There are three tables in total: mangle, filter and nat. The mangle table is responsible for the alteration of service bits in the TCP header. The filter queue is responsible for packet filtering. The nat table performs Network Address Translation (NAT). Each tables may have some built-in <em>chains</em> in which firewall policy <em>rules</em> can be placed.</p>
<p>The filter table has three built-in chains:<br />
* Forward chain: Filters packets destined for networks protected by the firewall.<br />
* Input chain: Filters packets destined for the firewall.<br />
* Output chain: Filters packets originating from the firewall.</p>
<p>The nat table has the following built-in chains:<br />
* Pre-routing chain: NATs packets when the destination address of the packet needs to be changed.<br />
* Post-routing chain: NATs packets when the source address of the packet needs to be changed.<br />
* Output chain: NATs packets originating from the firewall.</p>
<p>Below is a brief view of how packets are processed by the chains:</p>
<pre>PACKET IN
    |
PREROUTING--[routing]--&gt;--FORWARD--&gt;--POSTROUTING--&gt;--OUT
 - nat (dst)   |           - filter      - nat (src)
               |                            |
               |                            |
              INPUT                       OUTPUT
              - filter                    - nat (dst)
               |                          - filter
               |                            |
               `-----&gt;-----[app]-----&gt;------'</pre>
<p>Note: if the packet is from the firewall, it will not go through the PREROUTING chain.</p>
<p>We only look into the packets that requires port forwarding which is the topic of this post.</p>
<p>The packet entering the firewall is inspected by the rules in the nat table&#8217;s PREROUTING chain to see whether it requires destination modification (DNAT). The packet is then routed by Linux router after leaving the PREROUTING chain. The packet which is destined for a &#8220;protected&#8221; network is filtered by the rules in the FORWARD chain of the filter table. The it will go through the packet undergoes SNAT in the POSTROUTING chain before arriving at the &#8220;protected&#8221; network. When the destination server decides to reply, the packet undergoes the same sequence of steps.</p>
<h3>Port forwarding using iptables</h3>
<p>A port-forwarded packet will pass the PREROUTING chain in nat table, FORWARD chain in filter table, POSTROUTING chain in nat table and other chains. We need to add rules to these chains.</p>
<p>Let&#8217;s use a senario to introduce how to configure iptables to do port forwarding. Suppose our gateway can connect to both the Internet (0.0.0.0/0) and the LAN (192.168.1.0/24). The gateway&#8217;s eth0 interface has a public IP 7.8.9.10 while the eth1 has a LAN IP 192.168.1.1. Now, suppose that we have set up a HTTP server on 192.168.1.2:8080 and we want to provides service to the Internet through the public IP. We need to configure iptables to forward packets coming to port 80 of 7.8.9.10 to 8080 of 192.168.1.2 in LAN.</p>
<p>Below is the network topology:</p>
<pre>Internet---------[router/firewall]-------------LAN
0.0.0.0/0      7.8.9.10    192.168.1.1    192.168.1.0/24</pre>
<p>Normally we deny all incoming connections to a gateway machine by default because opening up all services and ports could be a security risk. We will only open the ports for the services that we will use. In this example, we will open port 80 for HTTP service.</p>
<p>This is the rules to forward connections on port 80 of the gateway to the internal machine:</p>
<pre># iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j DNAT --to 192.168.1.2:8080
# iptables -A FORWARD -p tcp -d 192.168.1.2 --dport 8080 -j ACCEPT</pre>
<p>These two rules are straight forward. The first one specifies that all incoming tcp connections to port 80 should be sent to port 8080 of the internal machine 192.168.1.2. This rule alone doesn&#8217;t complete the job as described above that we deny all incoming connections by default. Then we accept the incoming connection to port 80 from eth0 which connect to the Internet with the publich IP by the second rule. From the process path in the &#8220;iptables&#8221; part, the packet will also pass the FORWARD chains. We add the second rule in FORWARD chain to allow forwarding the packets to port 8080 of 192.168.1.2.</p>
<p>By now, we have set up the the iptables rules for forwarding the 80 port. For other service, the method is similiar with the HTTP service.</p>
<h3>The conntrack entries</h3>
<p>The &#8220;nf_conntrack_*&#8221; kernel modules enables iptables to examine the status of connections by caching the related information for these connections. A cat of <em>/proc/net/nf_conntrack</em> (in some old Linux kernels, the file is /proc/net/ip_conntrack) will give a list of all the current entries in the conntrack database.</p>
<p>A conntrack entry looks like this:</p>
<pre>ipv4     2 tcp      6 431581 ESTABLISHED
src=7.8.9.20 dst=7.8.9.10 sport=53867 dport=80 packets=22 bytes=13861
src=192.168.1.2 dst=7.8.9.20 sport=8080 dport=53867 packets=14 bytes=3535
[ASSURED] mark=0 secmark=0 use=2</pre>
<p>This entry contains all the information that the conntrack module maintains to know the state of a specific connection. We can find the version of ip protocal version and the decimal coding, the protocol and the normal decimal coding. After this, we get how long this conntrack entry should live. Next is the actual state that this entry is in at this present point of time. Then, we get the source IP address, destination IP address, source port and destination port. After that, we get the IPs and ports of both source and destination we expect of return packets.</p>
<p>In this entry we can find that the arriving connection is:</p>
<pre>7.8.9.20:53867 --&gt; 7.8.9.10:80</pre>
<p>while the returning connection is:</p>
<pre>192.168.1.2:8080 --&gt; 7.8.9.20:53867</pre>
<p>which reflects the port forwarding which we have set.</p>
<p><span style="font-size: x-small;"><strong>Update history:<br />
</strong>Jun. 15, 2011. Add note under the graph.<strong><br />
</strong>May. 25, 2011. Add the output chain in NAT table.<br />
Jun. 30, 2010. Add the rule in FORWARD chain.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://fclose.com/b/linux/816/port-forwarding-using-iptables/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Setting Up Gateway Using iptables and  route on Linux</title>
		<link>http://fclose.com/b/linux/1372/setting-up-gateway-using-iptables-and-route-on-linux/</link>
		<comments>http://fclose.com/b/linux/1372/setting-up-gateway-using-iptables-and-route-on-linux/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 14:13:19 +0000</pubDate>
		<dc:creator>Zhiqiang Ma</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Client config]]></category>
		<category><![CDATA[Fedora]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[route]]></category>
		<category><![CDATA[Server config]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://zhiqiangma.info/?p=1372</guid>
		<description><![CDATA[Sharing the networking is important. Setting up a gateway is a good solution to it. Building up the gateway on a Linux box is easy and cost efficient, but reliable. With a Linux box, you can share the internet connection or the only cable connected to the network. The Linux box The Linux box that [...]]]></description>
			<content:encoded><![CDATA[<p>Sharing the networking is important. Setting up a gateway is a good solution to it. Building up the gateway on a Linux box is easy and cost efficient, but reliable. With a Linux box, you can share the internet connection or the only cable connected to the network.</p>
<h3>The Linux box</h3>
<p>The Linux box that we use has this configuration:</p>
<p><strong>OS</strong>: Fedora 12</p>
<p><strong>NIC1</strong>: <em>eth0</em> with ip 192.168.0.1 connected to our small local area network.</p>
<p><strong>NIC2</strong>: <em>eth1 </em>with ip 143.89.111.111 connected to the internet.</p>
<p>Now we want to share this Linux box&#8217;s connection with the other computers in the local area network with ip in 192.168.0.0/16.</p>
<h3>Setting up the gateway</h3>
<p>All the operations in this part is done under root on the Linux gateway.</p>
<h4>Manipulate the IP route table</h4>
<pre># ip route add 192.168.0.0/16 dev eth0</pre>
<p>or</p>
<pre># route add -net 192.168.0.0/16 dev eth0</pre>
<h4>Enable Linux IP forwarding</h4>
<pre># sysctl -w net.ipv4.ip_forward=1</pre>
<p>or</p>
<pre># echo 1 &gt; /proc/sys/net/ipv4/ip_forward</pre>
<h4>Permanent setting it in /etc/sysctl.conf</h4>
<p>Add a line below to <em>/etc/sysctl.conf</em>:</p>
<pre>net.ipv4.ip_forward = 1</pre>
<h4>Set up SNAT by <em>iptables</em></h4>
<p>Change the source IP of out packets to gateway&#8217;s IP. Don&#8217;t worry since iptables will automatically change the replied packet&#8217;s destination IP to the original source IP.</p>
<pre># iptables -t nat -A POSTROUTING ! -d 192.168.0.0/16 -o eth1 -j SNAT --to-source 143.89.111.111</pre>
<p>And then make sure that the other <em>iptables</em> tables do not deny these connections.</p>
<p>If you have problem in this step, you can try</p>
<pre># iptables -F
# iptables -t nat -F
# iptables -t nat -A POSTROUTING ! -d 192.168.0.0/16 -o eth1 -j SNAT --to-source 143.89.111.111</pre>
<p>to allow all connections in. But there may be security problems after open all ports to the public. Firewall should be carefully configured.</p>
<p>By now, the we have set up the Linux gateway.</p>
<h3>Client side configuration</h3>
<p>On client such as Linux or Windows with IP 192.168.0.4, set the network connection to use this profile:</p>
<p>The configuration profile:</p>
<p><strong>Gateway</strong>: <em>192.168.0.1.</em></p>
<p><strong>DNS Server</strong>: your ISP&#8217;s DNS server IP addresses.</p>
<p>The method to configure the network maybe different from using NetworkManager and network and Windows.</p>
<p>You can try this command on Linux:</p>
<pre>ip route add default via 192.168.0.1 dev eth0</pre>
<p>or</p>
<pre># route add default gw 192.168.0.1 eth0</pre>
<p>You can use this GUI/TUI tool on Fedora / RedHat / CentOS systems:</p>
<pre># system-config-network</pre>
<p>or</p>
<pre># system-config-network-tui</pre>
<p><span style="font-size: x-small;">Updated history<br />
Jun. 23, 2011. Fix a bug in client configuration.<br />
Apr. 21, 2010. Add iptables -F for debugging in case of failure.<br />
Jun. 30, 2010. Change the debugging part of iptables.<br />
Sep. 18, 2010. Update NAT iptables rule. Filter out local IPs.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://fclose.com/b/linux/1372/setting-up-gateway-using-iptables-and-route-on-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

