<?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>ipsec &#8211; Luxing Huang</title>
	<atom:link href="https://luxing.im/tag/ipsec/feed/" rel="self" type="application/rss+xml" />
	<link>https://luxing.im</link>
	<description>Thoughs and things</description>
	<lastBuildDate>Wed, 05 Jul 2017 15:57:11 +0000</lastBuildDate>
	<language>en-CA</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
<site xmlns="com-wordpress:feed-additions:1">58771605</site>	<item>
		<title>IPSec over GRE setup tutorial</title>
		<link>https://luxing.im/gre-over-ipsec-setup-tutorial/</link>
					<comments>https://luxing.im/gre-over-ipsec-setup-tutorial/#comments</comments>
		
		<dc:creator><![CDATA[Luxing Huang]]></dc:creator>
		<pubDate>Sat, 06 Aug 2016 06:40:42 +0000</pubDate>
				<category><![CDATA[Techie Stuff]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[gre]]></category>
		<category><![CDATA[ipsec]]></category>
		<category><![CDATA[tunnel]]></category>
		<guid isPermaLink="false">https://luxing.im/?p=679</guid>

					<description><![CDATA[I have done some searching on the internet and found little about this specific topic, hence this post. CentOS 7 will be my first choice of server operating system. I believe the steps for other OSes are very similar. I will also include some of the generic setup steps in this tutorial. I will not &#8230; <p class="link-more"><a href="https://luxing.im/gre-over-ipsec-setup-tutorial/" class="more-link">Continue reading<span class="screen-reader-text"> "IPSec over GRE setup tutorial"</span></a></p>]]></description>
										<content:encoded><![CDATA[<p>I have done some searching on the internet and found little about this specific topic, hence this post.</p>
<p><span id="more-679"></span></p>
<p>CentOS 7 will be my first choice of server operating system. I believe the steps for other OSes are very similar. I will also include some of the generic setup steps in this tutorial. I will not talk about the concept of GRE and IPSec because you&#8217;ll find a lot of other nice articles on the internet.</p>
<p>In IPSec concept, the source machine is called <em>left</em> and the destination is called <em>right</em>. I also will call them these names. All actions are done with root user.</p>
<p>Here is the information about the 2 servers:</p>
<h3>Information</h3>
<p>Left:<br />
Public IP: 1.2.3.4<br />
GRE internal IP: 192.168.168.1</p>
<p>Right:<br />
Public IP: 4.3.2.1<br />
GRE internal IP: 192.168.168.2</p>
<h3>Configure GRE</h3>
<p>Configuring GRE is very straightforward. First, we need to make sure we enabled the IP forwarding function in the kernel, by applying those parameters to <code>/etc/sysctl.conf</code>:</p>
<pre>
net.ipv4.conf.all.forwarding = 1
net.ipv4.conf.default.forwarding = 1
net.ipv4.ip_forward = 1
</pre>
<p><code>sysctl -p --system</code> to apply those rules.</p>
<p>Make sure you have GRE module loaded in your kernel (and when boot up). This is a shell script.</p>
<pre>
lsmod | grep ip_gre > /dev/null
if [ $? -eq 1 ]; then
    modprobe ip_gre
    echo 'ip_gre' >> /etc/modules-load.d/gre.conf # for CentOS only
fi
</pre>
<p>For CentOS users, now it&#8217;s time to write some configuration files. On <em>left</em>:</p>
<pre>
DEVICE=gre1
BOOTPROTO=none
ONBOOT=yes
TYPE=GRE
PEER_OUTER_IPADDR=4.3.2.1 #Right's public ip address
PEER_INNER_IPADDR=192.168.168.2 #Right's internal GRE ip
MY_INNER_IPADDR=192.168.168.1 #Left's internal GRE ip
</pre>
<p>For <em>right</em> server, use the same configuration structure but change the values to appropriate ones.</p>
<p>Finally, start those GRE tunnels:</p>
<pre>
ifup gre1
</pre>
<p>For non-CentOS users, you may add your tunnel by executing those commands on each ends:</p>
<pre>
iptunnel add gre1 mode gre local 1.2.3.4 remote 4.3.2.1_IP ttl 255
ip addr add 192.168.168.1/30 dev gre1
ip link set gre1 up
</pre>
<p>Remember to change values.</p>
<p>Before we test it with ping, we need to add to iptables to allow NAT between the network.</p>
<pre>
iptables -t nat -A POSTROUTING -o gre1 -j ACCEPT
iptables -A INPUT -p gre -j ACCEPT
</pre>
<p>Now we can ping from <em>left</em> to <em>right</em> or vice versa.</p>
<h3>IPSec</h3>
<p>Install libreswan:</p>
<pre>
yum install libreswan -y
</pre>
<p>Initialize a new NSS databse:</p>
<pre>
ipsec initnss
</pre>
<p>Enable auto start from boot.</p>
<pre>
systemctl enable ipsec
</pre>
<p>Add ports to firewall. IKE uses UDP 500, IKE NAT-Traversal uses UDP 4500, Encapsulated Security Payload (ESP) uses protocol 50 and Authenticated Header (AH) uses protocol 51.</p>
<pre>
# IKE
iptables -A INPUT -p udp -m state --state NEW -m udp --dport 500 -j ACCEPT
# NAT-T
iptables -A INPUT -p udp -m state --state NEW -m udp --dport 4500 -j ACCEPT
# ESP
iptables -A INPUT -p esp -j ACCEPT
# AH
iptables -A INPUT -p ah -j ACCEPT
</pre>
<p>Use <code>iptables-save > /etc/sysconfig/iptables</code> to save firewall rules.</p>
<h3>Configuration</h3>
<p>In this section, we are going to use RSA encryption to encrypt our tunnel.</p>
<p>Firstly, create key pairs on both of our servers.</p>
<pre>
ipsec newhostkey --configdir /etc/ipsec.d --output /etc/ipsec.d/mytunnel.secrets
</pre>
<p>Find our <em>left</em> and <em>right</em> key on corresponding <em>left</em> and <em>right</em> server.</p>
<pre> showhostkey --left # or right</pre>
<p>There will be something like <code>right/leftrsasigkey=0sAQOw3XLeYw3q4….</code>, copy it.</p>
<p>Write those keys into <code>/etc/ipsec/mytunnel.conf</code>, put the file on both of the servers. The content could be the same as servers will determine itself as right or left.</p>
<pre>
conn mytunnel
    leftid=@left.host
    left=192.168.168.1
    leftrsasigkey=0sAQOrlo+hOafUZDlCQmXFrje/oZm [...] W2n417C/4urYHQkCvuIQ==
    rightid=@right.host
    right=192.168.168.2
    rightrsasigkey=0sAQO3fwC6nSSGgt64DWiYZzuHbc4 [...] D/v8t5YTQ==
    authby=rsasig
    # load and initiate automatically
    auto=start
</pre>
<p>Start IPSec:</p>
<pre>systemctl start ipsec
ipsec auto --add mytunnel
ipsec auto --up mytunnel</pre>
<p>You do not need to execute the last 2 lines every time you boot the system. It is all automatic.</p>
<h3>Check IPSec</h3>
<p>On the right server, we start our <code>tcpdump</code>.</p>
<pre>
tcpdump -n -i gre1 esp or udp port 500 or udp port 4500
</pre>
<p>From <em>left</em>, ping <em>right</em>:</p>
<pre>
09:48:42.198023 IP 192.168.168.1 > 192.168.168.2: ESP(spi=0x52f7e0f4,seq=0x4), length 132
09:48:42.198271 IP 192.168.168.2 > 192.168.168.1: ESP(spi=0x1b637a20,seq=0x4), length 132
09:48:43.198544 IP 192.168.168.1 > 192.168.168.2: ESP(spi=0x52f7e0f4,seq=0x5), length 132
09:48:43.199250 IP 192.168.168.2 > 192.168.168.1: ESP(spi=0x1b637a20,seq=0x5), length 132
09:48:44.199709 IP 192.168.168.1 > 192.168.168.2: ESP(spi=0x52f7e0f4,seq=0x6), length 132
09:48:44.200260 IP 192.168.168.2 > 192.168.168.1: ESP(spi=0x1b637a20,seq=0x6), length 132
</pre>
<p>If we captured ESP packages, then our setup is successful.</p>
<h3>Routing</h3>
<p>Configure our routing policy to go through GRE tunnel rather than our default gateway. Example: route all Google DNS traffic to GRE:</p>
<pre>
ip route add 8.8.0.0/16 dev gre1
</pre>
<p>You need to add the rules to <code>/etc/rc.local</code> if you want to have persistent routing when you reboot your server.</p>
<h3>Reference</h3>
<ol>
<li>
<p>http://wiki.frantech.ca/doku.php/gre_tunnel</p>
</li>
<li>
<p>https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Securing_Virtual_Private_Networks.html</p>
</li>
</ol>
]]></content:encoded>
					
					<wfw:commentRss>https://luxing.im/gre-over-ipsec-setup-tutorial/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">679</post-id>	</item>
	</channel>
</rss>
