<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>khax.net &#187; iptables</title>
	<atom:link href="http://blog.khax.net/tag/iptables/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.khax.net</link>
	<description>Adventures in Ubuntu systems administration</description>
	<lastBuildDate>Thu, 08 Jul 2010 02:01:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.khax.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/6ccca40b5ab19d33d91882e4d8f9a9c4?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>khax.net &#187; iptables</title>
		<link>http://blog.khax.net</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.khax.net/osd.xml" title="khax.net" />
	<atom:link rel='hub' href='http://blog.khax.net/?pushpress=hub'/>
		<item>
		<title>Odd iproute2 gateway behavior with two gateways</title>
		<link>http://blog.khax.net/2010/01/26/odd-iproute2-gateway-behavior-with-two-gateways/</link>
		<comments>http://blog.khax.net/2010/01/26/odd-iproute2-gateway-behavior-with-two-gateways/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 23:40:35 +0000</pubDate>
		<dc:creator>agentk</dc:creator>
				<category><![CDATA[Server]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[iptroute2]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://blog.khax.net/?p=169</guid>
		<description><![CDATA[After helping a frustrated yet patient Elvar I thought I better post about the situation we came across incase anybody else finds themselves in the same situation. Elvar started with all the correct elements to setup a functioning multrouting gateway. Two connections from two different providers, eth1, eth2. Running on an Ubuntu box with eth0 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.khax.net&amp;blog=6484387&amp;post=169&amp;subd=khax&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After helping a frustrated yet patient Elvar I thought I better post about the situation we came across incase anybody else finds themselves in the same situation.</p>
<p>Elvar started with all the correct elements to setup a functioning multrouting gateway. Two connections from two different providers, eth1, eth2. Running on an Ubuntu box with eth0 as internal. Both internet connections working on their own.</p>
<p>But alas. Whenever both eth1 and eth2 were active on the host, outgoing packets just would not go out. I don&#8217;t know if incoming packets were being replied to as we were unable to check that.</p>
<p>If just eth1 or eth2 was active than everything traversed ok. But we wanted it to work with both connections.</p>
<p>After a LONG time of diagnosing we noticed there were two default routes in iproute2 (ip&#8217;s fudged, but you get the idea):</p>
<pre>firewall# ip route show
...
default via 1.1.2.1 dev eth2  metric 100
default via 1.1.3.1 dev eth1  metric 100</pre>
<p>My firewall often has two default routes listed on the main table (ppp0, and ppp1) until the cleanup script fixes it. Without any negative side effect. I may just be lucky though.</p>
<p>Upon removing the eth2 line from table main, everything started working correctly. Incoming, outgoing, forwarding, balancing.</p>
<p>I also noticed the output of ifconfig eth2 looked a bit screwed too, but there was not much we could do about that as it was assigned by dhcp.</p>
<pre>eth2      Link encap:Ethernet  HWaddr 00:11:22:33:44:55
    inet addr:1.1.2.1  Bcast:255.255.255.255  Mask:255.255.255.0</pre>
<p>See it? No not the MAC address. The broadcast address. A quick ipcalc 1.1.2.1/24 gives me a broadcast of 1.1.2.255. But once we removed the eth2 default route line, it all started working again and didn&#8217;t get to dig into it to see if the broadcast actually affected it.</p>
<p>So that&#8217;s all. Just remember there are a lot more things to go wrong in a multigateway setup, including things outside of iptables.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khax.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khax.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khax.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khax.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khax.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khax.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khax.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khax.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khax.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khax.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khax.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khax.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khax.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khax.wordpress.com/169/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.khax.net&amp;blog=6484387&amp;post=169&amp;subd=khax&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.khax.net/2010/01/26/odd-iproute2-gateway-behavior-with-two-gateways/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">agentk</media:title>
		</media:content>
	</item>
		<item>
		<title>Iptables packet flow diagram</title>
		<link>http://blog.khax.net/2009/12/07/iptables-packet-flow-diagram/</link>
		<comments>http://blog.khax.net/2009/12/07/iptables-packet-flow-diagram/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 23:39:18 +0000</pubDate>
		<dc:creator>agentk</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iproute2]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://blog.khax.net/?p=167</guid>
		<description><![CDATA[Here is the diagram I keep stuck to my wall for reference when dealing with iptables. I cannot remember what site I used as a reference when creating the diagram. The original was an ascii chart though. This is created in dia and exported to pdf too. Both attached for convenience. CC licensed as long as the original [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.khax.net&amp;blog=6484387&amp;post=167&amp;subd=khax&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is the diagram I keep stuck to my wall for reference when dealing with iptables.</p>
<p>I cannot remember what site I used as a reference when creating the diagram. The original was an ascii chart though. This is created in dia and exported to pdf too. Both attached for convenience. CC licensed as long as the original chart I copied allows it.</p>
<h2>Packet flow examples:</h2>
<h3>All packets to or from localhost travel down the left side of the chart.</h3>
<p>From external destination localhost: PREROUTING -&gt; INPUT -&gt; [Local Process] -&gt; OUTPUT -&gt; POSTROUTING.</p>
<p>From localhost destination external: OUTPUT -&gt; POSTROUTING -&gt; [External Host] -&gt; PREROUTING -&gt; INPUT.</p>
<h3>All forwarded packets travel the right side of the chart and travel all three tables coming in and then again going out.</h3>
<p>From external dest internal: PREROUTING -&gt; FORWARD -&gt; POSTROUTING.</p>
<p>Internal response to external: PREROUTING -&gt; FORWARD -&gt; POSTROUTING.</p>
<p>and so forth.</p>
<p>This is what caught me for a while. Forwarded packet travel the right side route and get out of (or in to) the network. The response then gets generated and does not start from FORWARD or POSTROUTING, but from PREROUTING again. So all NEW forwarded packets need to be marked in PREROUTING and the mark saved. Not new packets need that mark restored in PREROUTING, and all packets need that mark restored in POSTROUTING. (As demonstrated by my previous post).</p>
<p>Locally generated traffic only sees OUTPUT and POSTROUTING before hitting the network and needs to be marked before hitting POSTROUTING, hence the OUTPUT chain rules in my previous post.</p>
<p>I will accept changes to the chart too if anybody wants modifications made.</p>
<p><a href="http://dl.dropbox.com/u/173699/iptables%20routing.dia">iptables routing.dia</a></p>
<p><a href="http://dl.dropbox.com/u/173699/iptables%20routing.pdf">iptables routing.pdf</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khax.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khax.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khax.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khax.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khax.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khax.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khax.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khax.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khax.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khax.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khax.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khax.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khax.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khax.wordpress.com/167/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.khax.net&amp;blog=6484387&amp;post=167&amp;subd=khax&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.khax.net/2009/12/07/iptables-packet-flow-diagram/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">agentk</media:title>
		</media:content>
	</item>
		<item>
		<title>Multi gateway balancing with iptables</title>
		<link>http://blog.khax.net/2009/12/01/multi-gateway-balancing-with-iptables/</link>
		<comments>http://blog.khax.net/2009/12/01/multi-gateway-balancing-with-iptables/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 02:14:06 +0000</pubDate>
		<dc:creator>agentk</dc:creator>
				<category><![CDATA[Server]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[gateway]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[multi]]></category>

		<guid isPermaLink="false">http://blog.khax.net/?p=163</guid>
		<description><![CDATA[I have been testing load balancing via both iptables and ip route nexthop for a couple of days now.  They both work pretty well too. This only balances outgoing traffic as incoming traffic balanced via DNS RR and the firewall just returns the traffic on the interface it arrived on as per the previous post. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.khax.net&amp;blog=6484387&amp;post=163&amp;subd=khax&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been testing load balancing via both iptables and ip route nexthop for a couple of days now.  They both work pretty well too.</p>
<p>This only balances outgoing traffic as incoming traffic balanced via DNS RR and the firewall just returns the traffic on the interface it arrived on as per the previous post.</p>
<p>On the whole, I prefer the iptables solution. It seems to balance the traffic better. ip route balances outgoing connections based on nexthop of the route to that host is not already in it&#8217;s routing cache. While iptables balances traffic by alternate outgoing connections. The only downside I have seen is occasional connection drops to the BlackBerry servers.</p>
<p>After 24 hours of iptables balancing:</p>
<pre>ppp0      Link encap:Point-to-Point Protocol
          RX bytes:1186783900 (1.1 GB)  TX bytes:1290603327 (1.2 GB)
ppp1      Link encap:Point-to-Point Protocol
          RX bytes:1109227490 (1.1 GB)  TX bytes:1140565429 (1.1 GB)
</pre>
<p>&nbsp;</p>
<p>This is using inclusion rules for determining balanced traffic. These are the rules that ended up on the production server:</p>
<p>&nbsp;</p>
<pre># Load balancing rules (Split 50/50 between fwmark 1/2)
iptables -t mangle -A balance1 -d 192.168.0.0/16      -j RETURN
iptables -t mangle -A balance1 -d 10.0.0.0/8          -j RETURN
iptables -t mangle -A balance1 -m connmark ! --mark 0 -j RETURN
iptables -t mangle -A balance1 -m state --state ESTABLISHED,RELATED -j RETURN
iptables -t mangle -A balance1 -m statistic --mode nth --every 2 --packet 0 -j CONNMARK --set-mark 1
iptables -t mangle -A balance1 -m statistic --mode nth --every 2 --packet 1 -j CONNMARK --set-mark 2

# Check to see if we have already marked a packet
iptables -t mangle -A PREROUTING  -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
iptables -t mangle -A OUTPUT      -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark

# Mark incoming connections to return on the interface they came in on
iptables -t mangle -A PREROUTING          -i ppp0                     -m state --state NEW  -j CONNMARK --set-mark 1
iptables -t mangle -A PREROUTING          -i ppp1                     -m state --state NEW  -j CONNMARK --set-mark 2

# New outgoing packets
iptables -t mangle -A PREROUTING  -i eth0          -p tcp --dport  22 -m state --state NEW  -j balance1
iptables -t mangle -A PREROUTING  -i eth0          -p tcp --dport  25 -m state --state NEW  -j balance1
iptables -t mangle -A PREROUTING  -i eth0          -p tcp --dport  80 -m state --state NEW  -j balance1
iptables -t mangle -A PREROUTING  -i eth0          -p tcp --dport 443 -m state --state NEW  -j balance1
iptables -t mangle -A OUTPUT                       -p tcp --dport  80 -m state --state NEW  -j balance1

# Choose our route and save the mark
iptables -t mangle -A PREROUTING  -m connmark --mark 1 -j MARK --set-mark 1
iptables -t mangle -A PREROUTING  -m connmark --mark 2 -j MARK --set-mark 2
iptables -t mangle -A PREROUTING  -m state --state NEW -m connmark ! --mark 0  -j CONNMARK --save-mark
</pre>
<p>&nbsp;</p>
<p>That&#8217;s all of the CONNMARK and MARK related rules I use.</p>
<p>The new outgoing packets section is where I choose what packets should be balanced and accounts for about 95% of our outgoing traffic.</p>
<p>The balance1 chain just has some checks at the beginning to catch further traffic that should not be balanced in case some rule gets messed up.</p>
<p>Of the new outgoing packets rules, the PREROUTE lines are for forwarded traffic and the OUTPUT rule is for traffic generated on that host by a transparent squid proxy.</p>
<p>Hope somebody finds that useful one day.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khax.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khax.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khax.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khax.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khax.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khax.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khax.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khax.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khax.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khax.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khax.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khax.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khax.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khax.wordpress.com/163/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.khax.net&amp;blog=6484387&amp;post=163&amp;subd=khax&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.khax.net/2009/12/01/multi-gateway-balancing-with-iptables/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">agentk</media:title>
		</media:content>
	</item>
	</channel>
</rss>