Internet Protocol Security (IPsec) on Mikrotik

Internet Protocol Security (IPsec) is a set of protocols defined by the Internet Engineering Task Force (IETF) to secure packet exchange over unprotected IP/IPv6 networks such as Internet.

IpSec protocol suite can be divided in following groups:
  • Authentication Header (AH) RFC 4302
  • Encapsulating Security Payload (ESP) RFC 4303
  • Internet Key Exchange (IKE) protocols. Dynamically generates and distributes cryptographic keys for AH and ESP.

Authentication Header (AH)

AH is a protocol that provides authentication of either all or part of the contents of a datagram through the addition of a header that is calculated based on the values in the datagram. What parts of the datagram are used for the calculation, and the placement of the header, depends whether tunnel or transport mode is used.

The presence of the AH header allows to verify the integrity of the message, but doesn't encrypt it. Thus, AH provides authentication but not privacy. Another protocol (ESP) is considered superior, it provides data privacy and also its own authentication method.

RouterOS supports the following authentication algorithms for AH:
  • SHA1
  • MD5

Transport mode

In transport mode AH header is inserted after IP header. IP data and header is used to calculate authentication value. IP fields that might change during transit, like TTL and hop count, are set to zero values before authentication.

Tunnel mode

In tunnel mode original IP packet is encapsulated within a new IP packet. All of the original IP packet is authenticated.

Encapsulating Security Payload

Encapsulating Security Payload (ESP) uses shared key encryption to provide data privacy. ESP also supports its own authentication scheme like that used in AH, or can be used in conjunction with AH.
Icon-note.png
Note: Using ah and esp together provides double authentication which adds additional load to CPU and does not provide any significant security advantages. We suggest to switch only to ESP.


ESP packages its fields in a very different way than AH. Instead of having just a header, it divides its fields into three components:
  • ESP Header - Comes before the encrypted data and its placement depends on whether ESP is used in transport mode or tunnel mode.
  • ESP Trailer - This section is placed after the encrypted data. It contains padding that is used to align the encrypted data.
  • ESP Authentication Data - This field contains an Integrity Check Value (ICV), computed in a manner similar to how the AH protocol works, for when ESP's optional authentication feature is used.

Transport mode

In transport mode ESP header is inserted after original IP header. ESP trailer and authentication value is added to the end of the packet. In this mode only IP payload is encrypted and authenticated, IP header is not secured.

Tunnel mode

In tunnel mode original IP packet is encapsulated within a new IP packet thus securing IP payload and IP header.

Encryption algorithms

RouterOS ESP supports various encryption and authentication algorithms.
Authentication:
  • SHA1
  • MD5
Encryption:
  • DES - 56-bit DES-CBC encryption algorithm;
  • 3DES - 168-bit DES encryption algorithm;
  • AES - 128, 192 and 256-bit key AES-CBC encryption algorithm;
  • Blowfish - added since v4.5
  • Twofish - added since v4.5
  • Camellia - 128, 192 and 256-bit key Camellia encryption algorithm added since v4.5

Hardware encryption

Hardware encryption allows to do faster encryption process by using built-in encryption engine inside CPU. AES is the only algorithm that will be accelerated in hardware.
List of RouterBoards with enabled hardware support:
  • RB1000
  • RB1100AHx2
  • All CloudCoureRouter series boards
For comparison RB1000 with enabled HW support can forward up to 550Mbps encrypted traffic. When HW support is disabled it can forward only 150Mbps encrypted traffic in AES-128 mode.

Some configuration advices on how to get maximum ipsec throughput on multicore RB1100AHx2:
  • Avoid using ether12 and ethet13. Since these prots are pci-x they will be slowest ones.
  • Fastest forwarding is from switch chip ports (ether1-ether10) to ether11 (directly connected to CPU) and vice versa.
  • Set hardware queue on all interfaces
/queue interface set [find] queue=only-hardware-queue
  • Disable RPS:
/system resource irq rps disable [find]
  • Assign one CPU core to ether11 and other CPU core to everything else. Forwarding over ether11 requires more CPU that is why we are giving one core only for that interface (in IRQ setting ether11 is listed as ether12 tx,rx and error).
/system resource irq
set [find] cpu=1
set [find users="eth12 tx"] cpu=0
set [find users="eth12 rx"] cpu=0
set [find users="eth12 error"] cpu=0
  • disable connection tracking

With all above recommendations it is possible to forward 820Mbps (1470byte packets two streams).
With enabled connection tracking 700Mbps (1470 byte packets two streams).

Internet Key Exchange Protocol

The Internet Key Exchange (IKE) is a protocol that provides authenticated keying material for Internet Security Association and Key Management Protocol (ISAKMP) framework. There are other key exchange schemes that work with ISAKMP, but IKE is the most widely used one. Together they provide means for authentication of hosts and automatic management of security associations (SA).
Most of the time IKE daemon is doing nothing. There are two possible situations when it is activated:
There is some traffic caught by a policy rule which needs to become encrypted or authenticated, but the policy doesn't have any SAs. The policy notifies IKE daemon about that, and IKE daemon initiates connection to remote host. IKE daemon responds to remote connection. In both cases, peers establish connection and execute 2 phases:
  • Phase 1 - The peers agree upon algorithms they will use in the following IKE messages and authenticate. The keying material used to derive keys for all SAs and to protect following ISAKMP exchanges between hosts is generated also. This phase should match following settings:
    • authentication method
    • DH group
    • encryption algorithm
    • exchange mode
    • hash alorithm
    • NAT-T
    • DPD and lifetime (optional)
  • Phase 2 - The peers establish one or more SAs that will be used by IPsec to encrypt data. All SAs established by IKE daemon will have lifetime values (either limiting time, after which SA will become invalid, or amount of data that can be encrypted by this SA, or both). This phase should match following settings:
    • Ipsec protocol
    • mode (tunnel or transport)
    • authentication method
    • PFS (DH) group
    • lifetime
Icon-note.png
Note: There are two lifetime values - soft and hard. When SA reaches it's soft lifetime treshold, the IKE daemon receives a notice and starts another phase 2 exchange to replace this SA with fresh one. If SA reaches hard lifetime, it is discarded.

Icon-warn.png
Warning: Phase 1 is not re-keyed if DPD is disabled when lifetime expires, only phase 2 is re-keyed. To force phase 1 re-key, enable DPD.

IKE can optionally provide a Perfect Forward Secrecy (PFS), which is a property of key exchanges, that, in turn, means for IKE that compromising the long term phase 1 key will not allow to easily gain access to all IPsec data that is protected by SAs established through this phase 1. It means an additional keying material is generated for each phase 2.
Generation of keying material is computationally very expensive. Exempli gratia, the use of modp8192 group can take several seconds even on very fast computer. It usually takes place once per phase 1 exchange, which happens only once between any host pair and then is kept for long time. PFS adds this expensive operation also to each phase 2 exchange.

Diffie-Hellman Groups

Diffie-Hellman (DH) key exchange protocol allows two parties without any initial shared secret to create one securely. The following Modular Exponential (MODP) and Elliptic Curve (EC2N) Diffie-Hellman (also known as "Oakley") Groups are supported:
Diffie-Hellman GroupNameReference
Group 1768 bit MODP groupRFC 2409
Group 21024 bits MODP groupRFC 2409
Group 3EC2N group on GP(2^155)RFC 2409
Group 4EC2N group on GP(2^185)RFC 2409
Group 51536 bits MODP groupRFC 3526

IKE Traffic

To avoid problems with IKE packets hit some SPD rule and require to encrypt it with not yet established SA (that this packet perhaps is trying to establish), locally originated packets with UDP source port 500 are not processed with SPD. The same way packets with UDP destination port 500 that are to be delivered locally are not processed in incoming policy check.

Setup Procedure

To get IPsec to work with automatic keying using IKE-ISAKMP you will have to configure policy, peer and proposal (optional) entries.
Icon-warn.png
Warning: Ipsec is very sensitive to time changes. If both ends of the IpSec tunnel are not synchronizing time equally(for example, different NTP servers not updating time with the same timestamp), tunnels will break and will have to be established again.

Mode Config

Sub-menu: /ip ipsec mode-config
Note: If RouterOS client is initiator, it will always send CISCO UNITY extension, and RouterOS supports only split-include from this extension.

Application Examples

Simple Mutual PSK XAuth Config

Server side config:
/ip ipsec peer
add address=2.2.2.1 auth-method=pre-shared-key-xauth secret="123" passive=yes

/ip ipsec user
add name=test password=345
Client side config:
/ip ipsec peer
add address=2.2.2.2 auth-method=pre-shared-key-xauth secret="123" \
  xauth-login=test xauth-password=345

Icon-note.png
Note: On server side it is mandatory to set passive to yes when XAuth is used.


Road Warrior setup with Mode Conf

Consider setup where worker need to access other co-workers (workstations) and local office server remotely. Office has two subnets:
  • 192.168.55.0/24 for workstations
  • 192.168.66.0/24 network that must not be reachable by RoadWarrior clients
  • 10.5.8.0/24 for servers
And access to those networks should be secure.
Ipsec-road-warrior.png
Typically in RoadWarrior setups as this it is impossible to know from which address user will connect, so we need to set upgenerate-policy parameter on the server side. However this leads to other problems, client can generate any policy and access any network in the office. Even set 0.0.0.0/0 and deny internet access to office workers.
Mode Confpolicy group and policy templates will allow us to overcome these problems.

IpSec Server Config

At first we need a pool from which RoadWarrior will will get an address. Typically in office you set up DHCP server for local workstations, the same DHCP pool can be used.
/ip pool
add name=ipsec-RW ranges=192.168.77.2-192.168.77.254
Next we need to set up what settings to send to the client using Mode Conf.
/ip ipsec mode-config
add address-pool=ipsec-RW name=RW-cfg split-include=\
    10.5.8.0/24,192.168.55.0/24
As you can see we specified from which pool to give out address and two allowed subnets.

Now to allow only specific source/destination address in generated policies we will use policy group and create policy templates:
/ip ipsec policy group
add name=RoadWarrior

/ip ipsec policy
add dst-address=192.168.77.0/24 group=RoadWarrior src-address=10.5.8.0/24 \
    template=yes
add dst-address=10.5.8.0/24  group=RoadWarrior src-address=192.168.77.0/24 \
    template=yes
add dst-address=192.168.77.0/24 group=RoadWarrior src-address=192.168.55.0/24 \
    template=yes

Now we just add xauth users and peer with enabled Mode Conf and policy group.
/ip ipsec user
add name=user1 password=123
add name=user2 password=234

/ip ipsec peer
add auth-method=pre-shared-key-xauth generate-policy=port-strict mode-config=RW-cfg \
    policy-group=RoadWarrior secret=123 passive=yes

Apple iOS (iPhone/iPad) Client

For iOS devices to be able to connect, proposal changes are needed:
  • does not work with 3des encryption algorithm, aes-128/256 works
  • auth algorithm must be sha1
  • PFS group must be none
  • lifetime must be 8 hours
Example of valid proposal configuration for iOS devices:
/ip ipsec proposal
set default enc-algorithms=aes-128-cbc,aes-256-cbc lifetime=8h \
    pfs-group=none
Icon-note.png
Note: Iphone does not work with split-include 0.0.0.0/0. If you set 0.0.0.0/0 for older clients traffic will not be sent over the tunnel, for newer ios clients tunnel will not be established.

Android Client Notes

Android devices are trying to add policy with destination 0.0.0.0/0, so you have to make sure that correct policy template is added.
In our case we need to add:
/ip ipsec policy
add group=RoadWarrior src-address=192.168.77.0/24 dst-address=0.0.0.0/0 template=yes

RouterOS Client Config

/ip ipsec peer
add address=2.2.2.2 auth-method=pre-shared-key-xauth generate-policy=port-strict secret=123 \
     xauth-login=user1 xauth-password=123

Shrew Client Config

n:version:2
n:network-ike-port:500
n:network-mtu-size:1380
n:network-natt-port:4500
n:network-natt-rate:15
n:network-frag-size:540
n:network-dpd-enable:0
n:client-banner-enable:0
n:network-notify-enable:0
n:client-wins-used:0
n:client-wins-auto:1
n:client-dns-used:1
n:client-dns-auto:0
n:client-splitdns-used:1
n:client-splitdns-auto:0
n:phase1-dhgroup:2
n:phase1-life-secs:86400
n:phase1-life-kbytes:0
n:vendor-chkpt-enable:0
n:phase2-life-secs:300
n:phase2-life-kbytes:0
n:policy-nailed:1
n:policy-list-auto:1
n:client-addr-auto:1
s:network-host:2.2.2.2
s:client-auto-mode:pull
s:client-iface:virtual
s:network-natt-mode:disable
s:network-frag-mode:disable
s:auth-method:mutual-psk-xauth
s:ident-client-type:address
s:ident-server-type:address
b:auth-mutual-psk:MTIz
s:phase1-exchange:main
s:phase1-cipher:3des
s:phase1-hash:md5
s:phase2-transform:esp-3des
s:phase2-hmac:sha1
s:ipcomp-transform:disabled
n:phase2-pfsgroup:2
s:policy-level:require

Road Warrior setup with RSA Authentication

Creating Certificates

All certificates can be created on RouterOS server using certificate manager. See example >>

Ipsec Server Config

/ip ipsec policy group
add name=test

/ip ipsec peer
add auth-method=rsa-signature certificate=server exchange-mode=main \
    generate-policy=port-override passive=yes policy-group=test remote-certificate=none
/ip ipsec policy
add dst-address=172.16.1.0/24 group=test src-address=172.16.2.0/24 template=yes

Ipsec Client Config

Testing CRL

Now lets say client2 should not be able to connect anymore. We need to revoke its certificate so that it is excluded from CRL list.
/certificate
issued-revoke client2
Notice R flag, which means that certificate is revoked
[admin@pe0] /certificate> print 
Flags: K - private-key, D - dsa, L - crl, C - smart-card-key, 
A - authority, I - issued, R - revoked, E - expired, T - trusted 
 #         NAME               COMMON-NAME               FINGERPRINT              
 0 K L A T myCa               myCa                      7fa636e6576495fe78f1a4...
 1 K   I T server             server                    cf0650a291bf4685f2fbd3...
 2 K   I   client1            client1                   26233de30e89b203b946ab...
 3 K   R   client2            client2                   cf172b62201befaf8d8966...


Now if you kill current connection client2 will no be able to establish phase1.

Site to Site IpSec Tunnel

Consider setup as illustrated below
Site-to-site-ipsec-example.png
Two remote office routers are connected to internet and office workstations behind routers are NATed. Each office has its own local subnet, 10.1.202.0/24 for Office1 and 10.1.101.0/24 for Office2. Both remote offices needs secure tunnel to local networks behind routers.

IP Connectivity

On both routers ether1 is used as wan port and ether2 is used to connect workstations. Also NAT rules are set tu masquerade local networks.

Office1 router:
/ip address
add address=192.168.90.1/24 interface=ether1
add address=10.1.202.1/24 interface=ether2

/ip route 
add gateway=192.168.90.254

/ip firewall nat
add chain=srcnat out-interface=ether1 action=masquerade
Office2 router:
/ip address
add address=192.168.80.1/24 interface=ether1
add address=10.1.101.1/24 interface=ether2

/ip route 
add gateway=192.168.80.254

/ip firewall nat
add chain=srcnat out-interface=ether1 action=masquerade

IpSec Peer's config

Next step is to add peer's configuration. We need to specify peers address and port and pre-shared-key. Other parameters are left to default values.
Office1 router:
/ip ipsec peer
add address=192.168.80.1/32 port=500 auth-method=pre-shared-key secret="test"
Office2 router:
/ip ipsec peer
add address=192.168.90.1/32 port=500 auth-method=pre-shared-key secret="test"

Policy and proposal

It is important that proposed authentication and encryption algorithms match on both routers. In this example we can use predefined "default" proposal
[admin@MikroTik] /ip ipsec proposal> print 
Flags: X - disabled 
 0   name="default" auth-algorithms=sha1 enc-algorithms=3des lifetime=30m 
     pfs-group=modp1024 
As we already have proposal as a next step we need correct IpSec policy. We want to encrypt traffic coming form 10.1.202.0/24 to 10.1.101.0/24 and vice versa.
Office1 router:
/ip ipsec policy
add src-address=10.1.202.0/24 src-port=any dst-address=10.1.101.0/24 dst-port=any \
sa-src-address=192.168.90.1 sa-dst-address=192.168.80.1 \
tunnel=yes action=encrypt proposal=default
Office2 router:
/ip ipsec policy
add src-address=10.1.101.0/24 src-port=any dst-address=10.1.202.0/24 dst-port=any \
sa-src-address=192.168.80.1 sa-dst-address=192.168.90.1 \
tunnel=yes action=encrypt proposal=default
Note that we configured tunnel mode instead of transport, as this is site to site encryption.

NAT Bypass

At this point if you will try to establish IpSec tunnel it will not work, packets will be rejected. This is because both routers have NAT rules that is changing source address after packet is encrypted. Remote router reiceves encrypted packet but is unable to decrypt it because source address do not match address specified in policy configuration. For more information seepacket flow ipsec example.

To fix this we need to set up NAT bypass rule.
Office1 router:
/ip firewall nat
add chain=srcnat action=accept  place-before=0 \
 src-address=10.1.202.0/24 dst-address=10.1.101.0/24
Office2 router:
/ip firewall nat
add chain=srcnat action=accept  place-before=0 \
 src-address=10.1.101.0/24 dst-address=10.1.202.0/24
It is very important that bypass rule is placed at the top of all other NAT rules.

Icon-note.png
Note: If you previously tried to establish tunnel before NAT bypass rule was added, you have to clear connection table from existing connection or restart the routers


Ipsec/L2TP behind NAT

Consider setup as illustrated below
Ipsec-l2tp-example.png
Client needs secure connection to the office with public address 1.1.1.1, but server does not know what will be the source address from which client connects. It is so called road-warrior setup. Our client will also be located behind the router with enabled NAT.

For the setup RouterOS router will be used as the client device behind NAT (it can be any device: Windows PC, Smartphone, Linux PC, etc.)

IP Connectivity

On the server:
/ip address 
add address=1.1.1.1/24 interface=ether1

/ip route
add gateway=1.1.1.2
On the clients router:
/ip address 
add address=2.2.2.2/24 interface=ether1
add address=10.5.8.0/24 interface=ether2

/ip route
add gateway=2.2.2.1

/ip firewall net
add chain=srcnat action=masquerade out-interface=ether1
On the client:
/ip address
add address=10.5.8.120/24 interface=ether1

L2TP Config

On the server:
/interface l2tp-server 
set enabled=yes profil=default

/ip pool 
add name=l2tp-pool ranges=192.168.1.2-192.168.1.20

/ppp profile 
set default local-address=192.168.1.1 remote-address=l2tp-pool

/ppp secret
add name=l2tp-test password=test123456
On the client:
/interface l2tp-client
add connect-to=1.1.1.1 disabled=no name=l2tp-out1 password=password user=l2tp-test

IpSec Config

On server side:
/ip ipsec proposal
set [ find default=yes ] enc-algorithms=3des,aes-128,aes-192,aes-256
/ip ipsec peer
add generate-policy=yes hash-algorithm=sha1 nat-traversal=yes secret=test123456

RouterOS as client:
/ip ipsec proposal
set [ find default=yes ] enc-algorithms=aes-128
/ip ipsec peer
add address=1.1.1.1/32 hash-algorithm=sha1 nat-traversal=yes secret=test123456

/ip ipsec policy
add dst-address=1.1.1.1/32 protocol=udp sa-dst-address=1.1.1.1 \
      sa-src-address=10.5.8.120 src-address=10.5.8.120/32

Notice that nat-traversal is enabled. This option is required because Ipsec connection will be established through the NAT router otherwise Ipsec will not be able to establish phase2.
Icon-warn.png
Warning: Only one L2TP/IpSec connection can be established through the NAT. Which means that only one client can connect to the sever located behind the same router.


Related Post:

Comments