Mikrotik Ipsec
Mikrotik Ipsec
Mikrotik Ipsec
Manual:IP/IPsec
Applies to RouterOS: v5.0 +
Summary
Sub-menu: /ip ipsec Package required: security Standards: RFC 4301 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.
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.
Manual:IP/IPsec 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 For comparison RB1100 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.
Manual:IP/IPsec
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 Group Name Group 1 Group 2 Group 3 Group 4 Group 5 768 bit MODP group 1024 bits MODP group Reference RFC 2409 RFC 2409
EC2N group on GP(2^155) RFC 2409 EC2N group on GP(2^185) RFC 2409 1536 bits MODP group RFC 3526
Manual:IP/IPsec
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.
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.
Peer configuration
Sub-menu: /ip ipsec peer Peer configuration settings are used to establish connections between IKE daemons (phase 1 configuration). This connection then will be used to negotiate keys and algorithms for SAs.
Property address (IP/IPv6 Prefix; Default: 0.0.0.0/0) Description If remote peer's address matches this prefix, then the peer configuration is used in authentication and establishment of Phase 1. If several peer's addresses match several configuration entries, the most specific one (i.e. the one with largest netmask) will be used. Authentication method: pre-shared-key - authenticate by a password (secret) string shared between the peers rsa-signature - authenticate using a pair of RSA certificates
Name of a certificate listed in certificate table (signing packets; the certificate must have private key). Applicable if RSA signature authentication method (auth-method=rsa-signature) is used. Short description of the peer. Diffie-Hellman group (cipher strength)
comment (string; Default: ) dh-group (ec2n155 | ec2n185 | modp1024 | modp1536 | modp2048 | modp3072 | modp4096 | modp6144 | modp768; Default: modp1024) disabled (yes | no; Default: no) dpd-interval (time | disable-dpd; Default: 2m) dpd-maximum-failures (integer: 1..100; Default: 5)
Whether peer is used to match remote peer's prefix. Dead peer detection interval. If set to disable-dpd, dead peer detection will not be used.
Maximum count of failures until peer is considered to be dead. Applicable if DPD is enabled.
enc-algorithm (3des | aes-128 | aes-192 | Encryption algorithm. aes-256 | blowfish | camellia-128 | camellia-192 | camellia-256 | des; Default: 3des) exchange-mode (aggressive | base | main; Default: main) generate-policy (yes | no; Default: no) Different ISAKMP phase 1 exchange modes according to RFC 2408. Do not use other modes then main unless you know what you are doing. Allow this peer to establish SA for non-existing policies. Such policies are created dynamically for the lifetime of SA. Automatic policies allows, for example, to create IPsec secured L2TP tunnels, or any other setup where remote peer's IP address is not known at the configuration time.
Manual:IP/IPsec
5
Hashing algorithm. SHA (Secure Hash Algorithm) is stronger, but slower.
lifebytes (Integer: 0..4294967295; Default: Phase 1 lifetime: specifies how much bytes can be transferred before SA is discarded. If set to 0, 0) SA will not be discarded due to byte count excess. lifetime (time; Default: 1d) my-id-user-fqdn (string; Default: ) Phase 1 lifetime: specifies how long the SA will be valid. By default IP address is used as ID. This parameter replaces ID with specified value. Can be used, for example, in cases if DNS name as ID is required. Use Linux NAT-T mechanism to solve IPsec incompatibility with NAT routers inbetween IPsec peers. This can only be used with ESP protocol (AH is not supported by design, as it signs the complete packet, including IP header, which is changed by NAT, rendering AH signature invalid). The method encapsulates IPsec ESP traffic into UDP streams in order to overcome some minor issues that made ESP incompatible with NAT. Communication port used for ipsec traffic. Phase 2 lifetime check logic: claim - take shortest of proposed and configured lifetimes and notify initiator about it exact - require lifetimes to be the same obey - accept whatever is sent by an initiator strict - if proposed lifetime is longer than the default then reject proposal otherwise accept proposed lifetime
port (integer:0..65535; Default: 500) proposal-check (claim | exact | obey | strict; Default: obey)
Name of a certificate (listed in certificate table) for authenticating the remote side (validating packets; no private key required). Applicable if RSA signature authentication method is used Secret string (in case pre-shared key authentication is used). If it starts with '0x', it is parsed as a hexadecimal value
send-initial-contact (yes | no; Default: Specifies whether to send initial IKE information or wait for remote side. yes)
Note: IPSec phases information is erased, when /ip ipsec peer configuration is modified on the fly, however packets are being encrypted/decrypted because of installed-sa (for example remote-peers information is erased, when peer configuration is modified.
Policy
Sub-menu: /ip ipsec policy Policy table is used to determine whether security settings should be applied to a packet.
Property action (discard | encrypt | none; Default: encrypt) Description Specifies what to do with packet matched by the policy. none - pass the packet unchanged discard - drop the packet encrypt - apply transformations specified in this policy and it's SA
comment (string; Default: ) disabled (yes | no; Default: no) dst-address (IP/IPv6 prefix; Default: 0.0.0.0/32)
Short description of the policy Whether policy is used to match packets. Destination address to be matched in packets.
dst-port (integer:0..65535 | any; Default: any) Destination port to be matched in packets. If set to any all ports will be matched
Manual:IP/IPsec
6
Specifies what combination of Authentication Header and Encapsulating Security Payload protocols you want to apply to matched traffic Specifies what to do if some of the SAs for this policy cannot be found: use - skip this transform, do not drop packet and do not acquire SA from IKE daemon require - drop packet and acquire SA unique - drop packet and acquire a unique SA that is only used with this particular policy
manual-sa (string | none; Default: none) priority (integer:-2147483646..2147483647; Default: 0) proposal (string; Default: default)
Name of the manual SA template Policy ordering classificator (signed integer). Larger number means higher priority.
Name of the proposal template that will be sent by IKE daemon to establish SAs for this policy. IP packet protocol to match.
protocol (all | egp | ggp| icmp | igmp | ...; Default: all) sa-dst-address (ip/ipv6 address; Default: ::) sa-src-address (ip/ipv6 address; Default: ::) src-address (ip/ipv6 prefix; Default: 0.0.0.0/32)
SA destination IP/IPv6 address (remote peer). SA source IP/IPv6 address (local peer). Source IP prefix
src-port (any | integer:0..65535; Default: any) Source Port of the packet tunnel (yes | no; Default: no) Specifies whether to use tunnel mode
Note: All packets are IPIP encapsulated in tunnel mode, and their new IP header's src-address and dst-address are set to sa-src-address and sa-dst-address values of this policy. If you do not use tunnel mode (id est you use transport mode), then only packets whose source and destination addresses are the same as sa-src-address and sa-dst-address can be processed by this policy. Transport mode can only work with packets that originate at and are destined for IPsec peers (hosts that established security associations). To encrypt traffic between networks (or a network and a host) you have to use tunnel mode.
Policy Stats
Command /ip ipsec policy print stats will show current status of the policy. Additional read-only parameters will be printed.
Property in-accepted (integer) in-dropped (integer) in-transformed (integer) out-accepted (integer) out-dropped (integer) out-transformed (integer) Description How many incoming packets were passed by the policy without an attempt to decrypt. How many incoming packets were dropped by the policy without an attempt to decrypt How many incoming packets were decrypted (ESP) and/or verified (AH) by the policy How many outgoing packets were passed by the policy without an attempt to encrypt. How many outgoing packets were dropped by the policy without an attempt to encrypt. How many outgoing packets were encrypted (ESP) and/or verified (AH) by the policy.
Manual:IP/IPsec
Dumping Policies
It is possible to dump policies installed into the kernel for debugging purposes with command: /ip ipsec policy dump-kernel-policies
After executing this command check the logs to see the result, there should be three policies in the kernel: forward, in and out. [admin@test-host] >/log print 07:28:34 ipsec,debug,packet policy ipsec fwd: 10.5.101.9[0] - 10.5.101.13[0] 07:28:34 ipsec,debug,packet policy ipsec in: 10.5.101.9[0] - 10.5.101.13[0] 07:28:34 ipsec,debug,packet policy ipsec out: 10.5.101.13[0] - 10.5.101.9[0]
Proposal settings
Sub-menu: /ip ipsec proposal Proposal information that will be sent by IKE daemon to establish SAs for this policy. Configured proposals are set in policy configuration.
Property auth-algorithms (md5|sha1|null; Default: sha1) Description Allowed algorithms for authorization. sha1 is stronger, but slower algorithm. Short description of an item. Whether item is disabled. Allowed algorithms and key lengths to use for SAs.
comment (string; Default: ) disabled (yes | no; Default: no) enc-algorithms (null|des|3des|aes-128|aes-192|aes-256|blowfish|camellia-128|camellia-192|camellia-256|twofish; Default: 3des) lifetime (time; Default: 30m)
How long to use SA before throwing it out. Name of the proposal template, that will be identified in other parts of ipsec configuration. Diffie-Helman group used for Perfect Forward Secrecy.
pfs-group (ec2n155 | ec2n185 | modp1024 | modp1536 | modp2048 | modp3072 | modp4096 | modp6144 | modp768 | none; Default: modp1024)
Manual SA
Sub-menu: /ip ipsec manual-sa Menu is used to configure SAs manually. Created SA template then can be used in policy configuration.
Manual:IP/IPsec
Property ah-algorithm (in/out in,out = md5|null|sha1; Default: null) ah-key (string/string; Default: ) ah-spi (0x100..FFFFFFFF/0x100..FFFFFFFF; Default: 0x100) disabled (yes | no; Default: no) esp-auth-algorithm (in/out in,out = md5|null|sha1; Default: null) esp-auth-key (string/string; Default: ) esp-enc-algorithm (in/out in,out = 3des | aes-128 | aes-192 | aes-256 | des | ...; Default: null) esp-enc-key (string/string; Default: )
Incoming-authentication-key/outgoing-authentication-key Incoming-SA-SPI/outgoing-SA-SPI Defines whether item is ignored or used Encapsulating Security Payload authentication encryption algorithm
Incoming-encryption-key/outgoing-encryption-key
esp-spi (0x100..FFFFFFFF/0x100..FFFFFFFF; Default: 0x100) Incoming-SA-SPI/outgoing-SA-SPI lifetime (time; Default: 0s) name (string; Default: ) Lifetime of this SA Name of the item for reference from policies
Installed SA
Sub-menu: /ip ipsec installed-sa This facility provides information about installed security associations including the keys.
Flushing SAs
Sometimes after incorrect/incomplete negotiations took place, it is required to flush manually the installed SA table so that SA could be renegotiated. This option is provided by the /ip ipsec installed-sa flush command. This command accepts only one property:
Property Description
sa-type (ah | all | esp; Default: all) Specifies SA types to flush. ah - delete AH protocol SAs only esp - delete ESP protocol SAs only all - delete both ESP and AH protocols SAs
Remote Peers
Sub-menu: /ip ipsec remote-peers This submenu provides you with various statistics about remote peers that currently have established phase 1 connections with this router. Note that if peer doesn't show up here, it doesn't mean that no IPsec traffic is being exchanged with it. Read only properties:
Manual:IP/IPsec
Property local-address (ip/ipv6 address) remote-address (ip/ipv6 address) side (initiator | responder) state (string)
Shows which side initiated the Phase1 negotiation. State of phase 1 negotiation with the peer. For example when phase1 and phase 2 are negotiated it will show state "established". How long peers are in established state.
established (time)
Statistics
Sub-menu: /ip ipsec statistics This menu shows various ipsec statistics
Property in-errors (integer) in-buffer-errors (integer) in-header-errors (integer) in-no-states (integer) in-state-protocol-errors (integer) in-state-mode-errors (integer) in-state-sequence-errors (integer) in-state-expired (integer) in-state-mismatches (integer) in-state-invalid (integer) in-template-mismatches (integer) in-no-policies (integer) in-policy-blocked (integer) in-policy-errors (integer) out-errors (integer) out-bundle-errors (integer) Description All inbound errors that are not matched by other counters. No free buffer. Header error No state is found i.e. Either inbound SPI, address, or IPsec protocol at SA is wrong Transformation protocol specific error, for example SA key is wrong or hardware accelerator is unable to handle amount of packets. Transformation mode specific error Sequence number is out of window
State is expired State has mismatched option, for example UDP encapsulation type is mismatched. State is invalid No matching template for states, e.g. Inbound SAs are correct but SP rule is wrong No policy is found for states, e.g. Inbound SAs are correct but no SP is found Policy discards Policy errors All outbound errors that are not matched by other counters Bundle generation error
Manual:IP/IPsec
10
Transformation protocol specific error
out-state-protocol-errors (integer) out-state-mode-errors (integer) out-state-sequence-errors (integer) out-state-expired (integer) out-policy-blocked (integer) out-policy-dead (integer) out-policy-errors (integer)
Transformation mode specific error Sequence errors, for example Sequence number overflow
Application Examples
Site to Site IpSec Tunnel
Consider setup as illustrated below
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
Manual:IP/IPsec
11
/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 \
Manual:IP/IPsec
tunnel=yes action=encrypt proposal=default
12
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 see packet 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.
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
13