<?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>nikrivers.com &#187; ntp</title>
	<atom:link href="http://www.nikrivers.com/tag/ntp/feed" rel="self" type="application/rss+xml" />
	<link>http://www.nikrivers.com</link>
	<description></description>
	<lastBuildDate>Tue, 07 Sep 2010 16:42:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Netgear DG834G and NAT loopback</title>
		<link>http://www.nikrivers.com/2009/10/26/netgear-dg834g-and-nat-loopback</link>
		<comments>http://www.nikrivers.com/2009/10/26/netgear-dg834g-and-nat-loopback#comments</comments>
		<pubDate>Mon, 26 Oct 2009 21:13:33 +0000</pubDate>
		<dc:creator>Nik</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Techie Stuff]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[firmware]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mail server]]></category>
		<category><![CDATA[nat loopback]]></category>
		<category><![CDATA[netgear dg834g]]></category>
		<category><![CDATA[ntp]]></category>
		<category><![CDATA[pda]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[tcp port 80]]></category>
		<category><![CDATA[update]]></category>
		<category><![CDATA[upgrade]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://www.nikrivers.com/?p=591</guid>
		<description><![CDATA[UPDATE 18th April 2010: Netgear have since released a firmware update for the DG834Gv4 which supports NAT loopback.  It took them long enough! Yesterday I made the decision (read: mistake) to update my Netgear DG834G router (hardware v4, firmware v5.01.09) to firmware v5.01.14 &#8211; and, as is the way with these things, it brought trouble.  After the upgrade I [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff6600;"><strong>UPDATE 18th April 2010:</strong> Netgear have since released a </span><a href="http://kb.netgear.com/app/answers/detail/a_id/13354" target="_blank"><span style="color: #ff6600;">firmware update</span></a><span style="color: #ff6600;"> for the DG834Gv4 which supports NAT loopback.  It took them long enough!</span></p>
<p>Yesterday I made the decision (read: mistake) to update my Netgear DG834G router (hardware v4, firmware v5.01.09) to firmware v5.01.14 &#8211; and, as is the way with these things, it brought trouble.  After the upgrade I couldn&#8217;t reach <a href="http://www.nikrivers.com">www.nikrivers.com</a> from the LAN side of the router.</p>
<p>The problem is caused by the way the router handles traffic coming from an internal IP address and destined for the WAN (i.e. external) IP address.  In this situation it requires that the router first transfers the traffic from the internal network to the external network, and then immediately passes it back whilst applying any firewall or routing rules that are relevant to incoming external traffic.</p>
<p><span id="more-591"></span>This behaviour is called &#8216;NAT loopback&#8217;, and it seems the vast majority of routers built for the home market have this ability turned off, or do not have the ability at all.  It can be a big problem if you host a website and wish to access that same website using its domain name.  The domain name will resolve to the WAN IP address of your router, and any traffic headed there (such as an HTTP GET request on port 80) from the internal network will be ignored by the router.</p>
<p>There are a few ways to solve this, but none of them are ideal.</p>
<ol>
<li>Use the server name instead of the domain name to access your website</li>
<li>Modify the list of known network hosts on each client to point your domain name straight to the server in question.</li>
<li>Run your own DNS server, using a view to return the server&#8217;s local IP address to requests for your domain name originating from your network.</li>
</ol>
<p>Of course, the situation gets more complex if you&#8217;re also using your router to send TCP traffic on port 80 to your webserver and UDP traffic on port 8668 to a game server.</p>
<p>The solution is to get NAT loopback working on your router.  With some routers, such as the Touchspeed 535 as provided by Be Broadband, this feature can be enabled using a <a href="http://thicksliced.blogspot.com/2006/08/speedtouch-and-nat-loopback.html" target="_blank">simple CLI command</a>.  For other routers, such as the Netgear DG834G, it&#8217;s not quite so easy.</p>
<p>For the purposes of this post I&#8217;ll assume the internal network is on the 192.168.0.x range, the router is 192.168.0.254, and the web server is 192.168.0.1.  <strong>You will need to modify these IP addresses according to your own network setup</strong>.</p>
<p>The first thing to do is to enable debug mode on the router.  Simply go to <a href="http://192.168.0.254/setup.cgi?todo=debug">http://192.168.0.254/setup.cgi?todo=debug</a> and you&#8217;ll be rewarded with an appropriate message, &#8220;Debug Enable!&#8221;.  Nice.</p>
<p>Now connect to the router with &#8216;telnet 192.168.0.254&#8242; to gain access to the router&#8217;s cut-down installation of Linux.  All that is required is to add one additional entry to the router&#8217;s iptables (which is a standard Linux feature; <a href="http://www.google.com/search?q=iptables+tutorial" target="_blank">Google it</a> or more info).  Type the following, amending any IP addresses according to the network setup:</p>
<blockquote><p>iptables -t nat -A POSTROUTING -d 192.168.0.1 -s 192.168.0.0/24 -p tcp &#8211;dport 80 -j SNAT &#8211;to 192.168.0.254</p></blockquote>
<p>This adds a rule to the POSTROUTING chain on the nat table which applies to all TCP traffic on port 80 (HTTP) coming from the private network and headed to the router.  The rule redirects the traffic to the server, and then processing jumps to the SNAT chain.</p>
<p>If the server is more than just a simple web server, such as an NTP server or mail server as well, the above step needs to be performed (changing the -p and &#8211;dport parameters accordingly) for each port and protocol combination you require.  Alternatively, those parameters could be omitted altogether, which will allow all traffic types on all ports through:</p>
<blockquote><p>iptables -t nat -A POSTROUTING -d 192.168.0.1 -s 192.168.0.0/24 -j SNAT &#8211;to 192.168.0.254</p></blockquote>
<p>If you do this, I recommend you run a firewall on your server, with only the appropriate ports opened.</p>
<p>There is more information in <a href="http://www.netfilter.org/documentation/HOWTO/NAT-HOWTO-10.html" target="_blank">section 10</a> of Rusty Russell&#8217;s <a href="http://www.netfilter.org/documentation/HOWTO/NAT-HOWTO.html" target="_blank">Linux 2.4 NAT Howto</a>.</p>
<p>Unfortunately, the iptables change isn&#8217;t retained when the router restarts, so it is necessary to go through the process every time &#8211; which is a pain in the backside.  Fortunately, however, the Netgear support website has a download link for previous firmware versions, so I downgraded my router back to <a href="http://kb.netgear.com/app/answers/detail/a_id/271" target="_blank">firmware v5.01.09</a> and everything worked fine again&#8211;including NAT loopback&#8211;with no iptables hack required.</p>
<h3>A robust solution with DNS</h3>
<p>Simply put, proper DNS is the best way to get around a router&#8217;s lack of/poorly implemented NAT loopback.</p>
<p>If you have the resources to host a website then you most likely also have the resources to host a DNS server for your internal network.  Simply create an ACL list describing all the clients on your internal network (probably as simple as specifying the CIDR block for your network, maybe something like 192.168.1.0/24).  Then create a view whose clients match that ACL, and define that view as a master DNS server for your website domain.  You then need to create a zone file for that domain &#8211; but instead of using an external IP for your webserver, use its internal IP.</p>
<p>All requests for other domains will be routed to the DNS forwarders, but requests for your webserver&#8217;s domain will be handled locally, and internal IP addresses will be returned.</p>
<p>The benefit is that you avoid traversing your gateway router to simply come back inside your network.  It doesn&#8217;t make sense that you rely on your gateway router to access a website within your own network.  In addition, the firewall on your router can be hardened to a much greater degree: for example, you needn&#8217;t leave FTP ports open on your router if you&#8217;re only connecting locally.  Or, to put it another way, you&#8217;re likely going to want to give yourself more access to your server than you want to give to the outside world; configuring a router&#8217;s firewall rules for this kind of conditional logic is simply asking for trouble.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nikrivers.com/2009/10/26/netgear-dg834g-and-nat-loopback/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Linksys NSLU2: time server with NTP</title>
		<link>http://www.nikrivers.com/linksys-nslu2/time-server-with-ntp</link>
		<comments>http://www.nikrivers.com/linksys-nslu2/time-server-with-ntp#comments</comments>
		<pubDate>Thu, 24 Apr 2008 20:04:47 +0000</pubDate>
		<dc:creator>Nik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dovecot]]></category>
		<category><![CDATA[linksys]]></category>
		<category><![CDATA[nslu2]]></category>
		<category><![CDATA[ntp]]></category>

		<guid isPermaLink="false">http://www.nikrivers.com/?page_id=42</guid>
		<description><![CDATA[If you&#8217;re running an email server on your Slug, where accurate server time is vital, or you simply want the Slug&#8217;s system time to be accurate, you need to make a few changes. When it comes out of the factory the Slug has an inherrent bug which causes the internal clock to lose time; apparently [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re running an email server on your Slug, where accurate server time is vital, or you simply want the Slug&#8217;s system time to be accurate, you need to make a few changes.</p>
<p>When it comes out of the factory the Slug has an inherrent bug which causes the internal clock to lose time; apparently Linksys tried to fix this problem by adjusting the time regularly (using cron) but it seems the time is adjusted in the wrong direction &#8211; thus, the error is doubled. This problem is fixed in Unslung 2.6 and higher anyway (Unslung 2.10 is the latest release as at Jan 2009), so we can remove the relevant crontab entry.</p>
<p>Edit the crontab file:</p>
<blockquote><p>vi /etc/crontab</p></blockquote>
<p>Comment out the call to hwclock (the Linksys &#8216;fix&#8217;). The hash (#) symbol is used to denote a comment:</p>
<blockquote><p>#1 * * * * root /usr/sbin/hwclock -s &amp;&gt;/dev/null</p></blockquote>
<p>Now install ntpd, and edit the config file to contain time servers that are geographically close to you &#8211; you can find out what these are by visiting <a href="http://www.pool.ntp.org/">http://www.pool.ntp.org/</a>. Editing the config file is quite self-explanatory.</p>
<blockquote><p>install ntp<br />
vi /opt/etc/ntp/ntp.conf</p></blockquote>
<p>Now edit the ntp startup script; there are a few things we need to do to make sure the time is set and maintained correctly.</p>
<blockquote><p>vi /opt/etc/init.d/S77ntp</p></blockquote>
<p>Add the following lines just before the existing call to ntp:</p>
<blockquote><p>/opt/bin/tickadj 10000 &gt; /dev/null<br />
/opt/bin/ntpd -q -c /opt/etc/ntp/ntp.conf &gt; /dev/null</p></blockquote>
<p>The first line corrects the Slug&#8217;s tick value, and the second line makes ntp quickly (-q) set the time using the specified time servers, and exits. It is important to note that when ntp is running, it does not just simply set the correct time regularly; it constantly checks the time against that of the time servers, and gradually brings the system time in line. The call we just added, with the -q flag, ensures that the Slug has pretty accurate time before ntp starts properly.</p>
<p>Speaking of which, you will need to make sure the Slug has accurate time now. This can be done using the web interface, for example. It only needs to be as accurate as you can get it, and ntp will take care of the rest.</p>
<p>Now simply restart ntp:</p>
<blockquote><p>/opt/etc/init.d/S77ntp restart</p></blockquote>
<p>It will take a while for ntp to synchronise with the time servers you specified in the config file, sometimes upwards of 15 minutes &#8211; so don&#8217;t be too concerned if nothing seems to be happening straight away. You can check what&#8217;s going on with the ntpq tool:</p>
<blockquote><p>ntpq -p</p></blockquote>
<p>The command will list information about the time servers. If ntp has decided on a server&#8217;s suitability for synchronisation, the time server&#8217;s name will be preceeded by <strong>*</strong> (the chosen server), <strong>+</strong> (server is suitable), or <strong>-</strong> (server is unsuitable).</p>
<p><strong>Note:</strong> if you are running Dovecot on your Slug, and ntp adjusts the time backwards after you initially set the Slug&#8217;s time (using the web interface), Dovecot will complain about time going backwards, and die. You will need to restart Dovecot; but this only happens while you&#8217;re configuring ntp, and won&#8217;t happen during normal operation once ntp is running.</p>
<p><em>If this information was useful, please leave a comment to let me know!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nikrivers.com/linksys-nslu2/time-server-with-ntp/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
