Crmgo Blog

ipsec (openswan)

ipsec (openswan)

Kernel configuration Add the following changes to \etc\sysctl.conf and apply them. Disable SELinux. Install OpenSwan. Configure iptables. You need to open incoming UDP ports 50, 500, and 4500. Add the following rules to iptables. Save the changes. Create a key. Create a certificate database; when prompted for a password, press Enter…

ipsec (openswan)

 

Kernel configuration

Add the following changes to \etc\sysctl.conf

vi /etc/sysctl.conf
net.ipv4.ip_forward = 1 # разрешить пересылку пакетов между интерфейсами 
net.ipv4.conf.all.send_redirects = 0 # отключаем icmp redirect
net.ipv4.conf.all.accept_redirects = 0 # отключаем icmp redirect
net.ipv4.conf.default.send_redirects = 0 # отключаем icmp redirect
net.ipv4.conf.default.accept_redirects = 0 # отключаем icmp redirect

apply the changes.

sysctl –p

Disable SELinux

vi /etc/selinux/config
SELINUX=disabled

Install OpenSwan

 yum install openswan

Configure iptables

You need to open incoming UDP ports 50, 500, and 4500. Add the following rules to iptables

iptables -A INPUT -i eth0 -p udp --dport 500 -j ACCEPT
iptables -A INPUT -i eth0 -p udp --dport 4500 -j ACCEPT
iptables -A INPUT -i eth0 -p udp --dport 50 -j ACCEPT

Save the changes

 service iptables save

Create a key.

Create a certificate database; when prompted for a password, press Enter (i.e., leave it empty).

certutil -N -d /etc/ipsec.d/

Create a certificate.

ipsec newhostkey --configdir /etc/ipsec.d/ --output /etc/ipsec.d/office1.secrets --bits 4096

This command will display the key on the screen; in the next step, you will need to insert the key into the configuration file.

ipsec showhostkey --left

Configure ipsec.

Start and verify ipsec.

service ipsec start
ipsec verify

Once everything is configured, installed, and verification shows no errors, you can proceed to configure the ipsec tunnel.
Two files are used for tunnel configuration: ipsec.conf and ipsec.secrets.
Ipsec.conf – this file defines the main tunnel settings.
Ipsec.secrets – this file stores references to key files.
Open ipsec.conf and modify it as follows.

vi /etc/ipsec.conf
version 2.0 
config setup
    protostack=netkey # указываем какой стэк протокола будем использовать, варианты auto, klips,netkey
include /etc/ipsec.d/*.conf # указываем каталог где лежат конфиги туннеля так сказать подключаем оставшуюся часть ipsec.conf

Let's configure the tunnel itself. For simplicity, anything with the prefix "left" refers to local (local), and anything with the prefix "right" refers to remote, relative to the location where you are making the configuration.

vi /etc/ipsec.d/office1.conf
conn Tunnel_office1
   authby=rsasig # указываем что авторизоваться будем RSA ключами
   type=tunnel # указываем что тип соединения туннель
   auto=start # автоматически поднимать соединение
#Описываем настройки локальной стороны.
   leftid=1.1.1.1 #здесь FQDN например gw.office1.ru или внешний ip адрес
   left=1.1.1.1 # Указываем внешний ip адрес
   leftsubnet=192.168.0.0/24 # указываем внутреннюю подсеть 
   leftsourceip=192.168.0.1 #Указываем внутренний ip адрес шлюза 
   leftnexthop=1.1.1.1 #Указываем внешний ip адрес шлюза
   leftrsasigkey= #сюда вставляем с генерированный ключ.
#Описываем настройки удаленной стороны.
   rightid=10.1.1.1
   right=2.2.2.2
   rightsubnet=10.1.1.0/24
   rightsourceip=10.1.1.1
   rightnexthop=2.2.2.2
   rightrsasigkey=#сюда вставляем с генерированный ключ, с удаленной машины.

Add a couple of rules to iptables to allow packet forwarding between subnets

iptables -A FORWARD -s 192.168.0/24 -d 10.1.1.0/24 -j ACCEPT
iptables -A FORWARD -s 10.1.1.0/24 -d 192.168.0.0/24 -j ACCEPT
service iptables save # что бы правила сохранились после перезагрузки

Start and verify
After configuring the config files, you need to restart ipsec

service ipsec restart

Bring up the tunnel

ipsec auto --add Tunnel_office1
ipsec auto –-up Tunnel_office1

A little information for diagnostics

ipsec auto status

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.

Categories: FAQ