issue with l2tp/ipsec

Hi!
I have created an IPSEC tunnel between a headquarter and several branches.

I also created a L2TP server + ipsec for people who work remotely.

Everything is working fine until the mikrotik router gets rebooted.

What happens is that a new identity and a new peer is created under ipsec, a dynamic one, with the names of l2tp-server.
The issue is that it takes by default the proposal and the profile of the IPSEC tunnel of the site-to-site between the headquarter and the other locations, it doesn’t take the proposal I made specifically for the remote access.

This is indeed very important because the IPSEC tunnel uses sha512 and aes-256 which is not supported by the remote vpn.

So, my l2tp becomes broken and I need to enter and manually delete that peer and identity that is created by default and use the ones that I’ve created which uses sha1 and other configurations which are required to my needs.

And then it works fine again, but it’s a pain to that everytime that the power gets off or whatever.

I also cannot edit the peer and identity that are created automatically, it doesn’t let me. Thus, I must delete them and use the ones I’ve created

You would untick the use-ipsec option in the L2TP server settings and create the IPsec configuration manually - you have already defined profile and proposal, all that is left is a policy template, a peer and an identity

THank you!
but if I untick the use-ipsec option. wouldn’t i be turning of IPSEC entirely for for the l2tp conecction?

If you define everything manually, no, you won’t turn it off completely

thank you very much!!
I’ll do that.

i have another issue maybe you can help me.

I cannot ping from the remoce laptop to the stores
the l2tp conecction is to the headoffice location where the server is, the stores are connected to the server using IPSEC.
I can ping from the devices on the headoffice to the stores and viceversa, I can pingo from the remote PCs to the devices on the server but cannot ping to the devices of the stores from the remote PCs.

I think I have something wrong with the routes or the firewall, but cannot figure out what.
or maybe is not possible at all

To diagnose the issue, a full exported config would be needed:

export file=anynameyouwish (minus sensitive info like serial number, passwords, etc.)

If you paste it as plain text, make sure to surround it with code tags

Here it is, please let me know if I've obfuscated something that I shouldn't.

////////

jan/31/2025 xx:xx:xx by RouterOS 6.49.8

software id = ####-####

model = RB750Gr3

serial number =

/interface bridge
add name=lan-ofice
/interface ethernet
set [ find default-name=ether1 ] name=ether1-wan
set [ find default-name=ether2 ] name=ether2-PC1
set [ find default-name=ether3 ] name=ether3-SERVER
set [ find default-name=ether4 ] name=ether4-POS
set [ find default-name=ether5 ] name=ether5-AP
/interface wireless security-profiles
set [ find default=yes ] supplicant-identity=MikroTik
/ip ipsec peer
add address=PUBLIC-IP exchange-mode=ike2 name=store1
/ip ipsec profile
set [ find default=yes ] dh-group=modp2048 enc-algorithm=aes-256
hash-algorithm=sha512 name=ipsec1
add dh-group=modp2048 enc-algorithm=aes-256 name=remote
/ip ipsec peer
add name=l2tp-in-server passive=yes profile=remote
/ip ipsec proposal
set [ find default=yes ] disabled=yes
add enc-algorithms=aes-128-cbc lifetime=1d name=proposal1-IPSEC pfs-group=none
add auth-algorithms=sha512,sha256,sha1 name=proposal-remote pfs-group=none
/ip pool
add comment="lan-pool" name=pool1_local ranges=
xx.xx.xx.50-xx.xx.xx.100
add comment="remote-pool" name=
pool2-remote_access ranges=XX.XX.XX.100-xx.XX.xx.150
add comment="WIFI-pool" name="pool3-guests-WIFI" ranges=
xxx.xxx.x.101-xxx.xxx.x.150
/ip dhcp-server
add address-pool=pool1_local disabled=no interface=lan-ofice lease-time=
1h name=dhcp_bridge
/ppp profile
add dns-server=1.1.1.1,8.8.8.8 local-address=xxx.xxx.x.1 name=
"remote-profile" remote-address=pool2-remote_access
/interface bridge port
add bridge=lan-ofice interface=ether2-PC1
add bridge=lan-ofice interface=ether3-SERVER
add bridge=lan-ofice interface=ether4-POS
add bridge=lan-ofice interface=ether5-AP
/interface l2tp-server server
set authentication=mschap2 default-profile="remote-profile" enabled=yes
ipsec-secret=xxxxxxxxx use-ipsec=yes
/ip address
add address=publicIP-headoffice/24 comment="wan modem" interface=ether1-wan
network=publicIP-gateway
add address=IP-localred-headoffice/24 comment=bridge interface=lan-ofice network=
xxx.xxx.x.0
add address=xxx.xxx.X.2 comment="for PC1 ether2" interface=ether2-PC1
network=xxx.xxx.x.0
add address=xxx.xxx.x.3 comment="server ether3" interface=ether3-SERVER
network=XXX.xxx.x.0
add address=xxx.xxx.x.4 comment="POS ether4" interface=ether4-POS network=
xxx.xxx.x.0
add address=xxx.xxx.x.5 comment="AP TP LINK ether5" interface=
ether5-AP network=xxx.xxx.x.0
/ip dhcp-server network
add address=xxx.xxx.x.0/24 dns-server=1.1.1.1,8.8.8.8 gateway=xxx.xxx.x.1
/ip dns
set allow-remote-requests=yes servers=1.1.1.1,8.8.8.8
/ip dns static
add address=xxx.xxx.x.97 name=SERVIDOR
/ip firewall address-list
add address=194.50.16.198 list=blocked_ips
add address=194.50.16.1 list=blocked_ips
add address=103.102.230.5 list=blocked_ips
add address=185.147.124.54 list=blocked_ips
add address=xxx.xxx.x.100 list=ssh-allowed
add address=xxx.xxx.x.100 list=api-allowed
add address=xxx.xxx.x.1 list=api-allowed
add address=xxx.xxx.x.3 list=api-allowed
add address=xxx.xxx.x.1 list=ssh-allowed
add address=xxx.xxx.x.3 list=ssh-allowed
/ip firewall filter
add action=drop chain=input src-address=194.50.16.198
add action=drop chain=input src-address=194.50.16.1
add action=drop chain=input src-address=103.102.230.5
add action=drop chain=input src-address=185.147.124.54
add action=accept chain=input dst-port=x protocol=tcp src-address=
xxx.xxx.x.100
add action=accept chain=input dst-port=x protocol=tcp src-address-list=
ssh-allowed
add action=accept chain=input dst-port=xx protocol=tcp src-address-list=
api-allowed
add action=drop chain=input dst-port=xx protocol=tcp
add action=drop chain=input dst-port=x protocol=tcp
/ip firewall nat
add action=accept chain=srcnat dst-address-list=xxx.xxx.x.0/24 (local headoffice
src-address-list=xxx.xxx.x.0/24 (local headoffice)
add action=accept chain=srcnat dst-address=xxx.xxx.xx.0/24 (local store1) out-interface=
ether1-wan src-address=xxx.xxx.x.0/24 (local headoffice)
add action=masquerade chain=srcnat out-interface=ether1-wan
/ip ipsec identity
add peer=store1 secret=xxxxx
add generate-policy=port-strict peer=l2tp-in-server remote-id=ignore secret=
xxx
/ip ipsec policy
set 0 comment="don't delete, policy for l2tp" proposal="proposal remote"
add dst-address=xxx.xxx.xx.0/24 (local store1) peer=store1 proposal=proposal1-IPSEC src-address=
Xxx.xxx.x.0/24 (local headoffice) tunnel=yes
/ip route
add distance=1 gateway=public gateway headoffcice
add distance=1 dst-address=xxx.xxx.xx.0/24 (local store1) gateway=lan-ofice
/ip service
set telnet disabled=yes
/ppp secret
add name=user1 password=xxxx profile="remote-profile" service=
l2tp
add name=user2 password=xxx profile="remote-profile" service=
l2tp
add name=user3 password=xxx profile="remote-profile"
service=l2tp
/system clock
set time-zone-name=xxx
///////

I’m thinking of a nifty trick which hypothetically should work, but practically not so sure. However, you would need to shift the L2TP pool range to somewhere after the
.151 address. To the PPP profile you would add an address list name and then use it in the following NAT rule:

/ip firewall nat
add action=netmap chain=srcnat dst-address="store_IP" ipsec-policy=out,none src-address-list="PPP_address_list" to-addresses="LAN_IP_subnet"

If it doesn’t work, you can always add a second IPsec policy from the L2TP range to the store IP.

And hopefully, there is a firewall in front of the main office router because the situation would be very bad

If you create the properly set and linked peer, identity, policy template group, and policy template items manually, you do not need to set use-ipsec=yes in the L2TP server settings, the behavior of the manually created items will be the same like the one of the dynamically created ones, i.e. the L2TP transport packets will be IPsec-encrypted and encapsulated. You can even increase the security a bit by adding a static /ip ipsec policy item that says src-address=publicIP-headoffice/32 dst-address=0.0.0.0/0 protocol=udp src-port=1701 action=discard as the very last one in the list; this will prevent any L2TP transport packet to slip in or out without encryption should the IPsec session fail for any reason. Mikrotik has implemented some measures protecting the admins from their own stupidity so the src-address in that “killswitch” policy cannot be 0.0.0.0/0 as well, but that doesn’t matter in your case where the WAN address is static.

What puzzles me most is why, if the IPsec setup for L2TP is created dynamically, a power outage causes an issue. I am aware of such issuses when there are more complex setups so some NAT mapping created by the “old” connection keeps being updated by one peer whereas the other one starts knocking from behind another public address.

And as others have already mentioned, you current firewall is a joke: the default handling in RouterOS firewall is “accept”, so if a packet doesn’t match any rule, it is accepted. A combination of a PoS terminal and a leaky firewall is a direct threat to your cash flow. The fact that you did set up some rules makes me fear that the WAN is directly connected to the internet uplink, so you should have a look at how the default firewall rules look like (use /system default-configuration script print and look for the /ip firewall filter part inside) and use that as an inspiration to make your firewall more useful; actually, the right thing to do would be to export the configuration, netinstall the device, recreate the configuration by copying the contents of the export row by row or at least section by section and with a proper firewall that only lets in what you explicitly allow and drops the rest, and only then connect the device back to the internet.

The situation would be very bad if I add that or the situation is already bad?

I’m truly asking because I’m very new to networking and I don’t know if the current configuration is insecure.

Your last code would be putting me out of the IPSEC wright? in order to reach the stores.

The firewall already is a joke, no matter what you do regarding the IPsec and L2TP. So you should concentrate on fixing that first. While the devices on the LAN side are partially protected by the fcat that they are running on private addresses so cannot be attacked directly from the internet, the fcat that the router itself is not protected enough wipes out that advantage. Even if ssh and api ports are actually protected (which is not clear from your obfuscation - does xx indeed mean the same port number everywhere?), winbox seems to be open to the world. So if some malware has managed to squat on the router already, it may have become a gateway to your LAN devices so some other malware may already reside on them.

xx mean the same port and X another one.
I have no idea how to properly configure the firewall rules honestly. I’m very new to this

If you feel like that, you can follow the instruction in this post.

In short, you need to implement the following default rules:

/ip firewall filter
add action=accept chain=input comment="defconf: accept established,related,untracked" connection-state=established,related,untracked
add action=drop chain=input comment="defconf: drop invalid" connection-state=invalid
add action=accept chain=input comment="defconf: accept ICMP" protocol=icmp
add action=drop chain=input comment="defconf: drop all not coming from LAN" in-interface-list=!LAN
add action=accept chain=forward comment="defconf: accept in ipsec policy" ipsec-policy=in,ipsec
add action=accept chain=forward comment="defconf: accept out ipsec policy" ipsec-policy=out,ipsec
add action=fasttrack-connection chain=forward comment="defconf: fasttrack" connection-state=established,related
add action=accept chain=forward comment="defconf: accept established,related, untracked" connection-state=established,related,untracked
add action=drop chain=forward comment="defconf: drop invalid" connection-state=invalid
add action=drop chain=forward comment="defconf:  drop all from WAN not DSTNATed" connection-nat-state=!dstnat connection-state=new in-interface-list=WAN

And additionally the following four which should be added before the “Drop all not coming from LAN” default one:

/ip firewall filter
add action=accept chain=input comment="Allow IPsec IKE" dst-port=500 protocol=udp
add action=accept chain=input comment="Allow L2TP" dst-port=1701 protocol=udp
add action=accept chain=input comment="Allow IPsec NAT-T" dst-port=4500 protocol=udp
add action=accept chain=input comment="Allow IPsec ESP" protocol=ipsec-esp

Leaving aside that this set of forward rules would prevent the VPN clients from reaching the devices in LAN, if the OP did exactly this, they would lose the IPsec and/or L2TP connection to the router - additionally means “after”, and the rules are appended to the end of each chain unless you specify the desired position, so those four action=accept rules for the L2TP and IPsec would come after the “drop everything that did not come from LAN” and thus never get hit. So you should provide additional information about their required position.

To make things even worse, there are also no permissive rules for management access via the VPN.

So @zezeme, do not copy-paste the rules suggested above blindly.

Made an edit accordingly

hi!! thank you, i just read your messages.

I have followed your previous suggestion about using the def conf, plus I saw a lot of videos of firewall settings from the guy who makes the udemy courses i was watching about vpns on mikrotik.

i search a lot on other sources too.

So far I have this, which is a mix of the def config plus some stuff I saw.

rules 1,2 and 3 is because I saw a clever way to only allow certain access to winbox and ssh.
Plus I have changed all those ports.
and disable everything on services besides SSH and winbox.



0    ;;; block ip de hackers (this is just some IPs that i noticed were trying to get access to my router but i know isn't practical add one by one, but I was nervous)
      chain=input action=drop src-address-list=blocked_ips log=no log-prefix="" 

 1    ;;; add ip to temporal list
      chain=input action=add-src-to-address-list protocol=tcp address-list=Temporary address-list-timeout=10s 
      dst-port=xxxxx log=no log-prefix="" 

 2    ;;; ad ip to valid list
      chain=input action=add-src-to-address-list protocol=tcp src-address-list=Temporary address-list=Valid 
      address-list-timeout=1h dst-port=xxxxx log=no log-prefix="" 

 3    ;;; allow ssh and winbox if comes from valid
      chain=input action=accept protocol=tcp src-address-list=Valid dst-port=xxxxx,xxxxx log=no log-prefix="" 

 4    ;;; drop any other ssh and winbox
      chain=input action=drop protocol=tcp dst-port=xxxxx,xxxxx log=no log-prefix="" 

 5    ;;; allow IPSEC from whitelist
      chain=input action=accept protocol=udp src-address-list=IPSEC dst-port=xxxxx,xxxxx log=no log-prefix="" 

 6    ;;; block IPSEC if not in whitelist
      chain=input action=drop protocol=udp dst-port=xxxxx,xxxxx log=yes log-prefix="" 

 7    ;;; allow only 2 packets per sec on ICMP after frist 5 burst
      chain=input action=accept protocol=icmp limit=2,5:packet log=no log-prefix="" 

 8    ;;; drop any other ICMP
      chain=input action=drop protocol=icmp log=no log-prefix="" 

 9    ;;; accept forward established, related
      chain=forward action=accept connection-state=established,related log=no log-prefix="" 

10    ;;; accept input established, related
      chain=input action=accept connection-state=established,related log=no log-prefix="" 

11    ;;; drop forward invalid
      chain=forward action=drop connection-state=invalid log=no log-prefix="" 

12    ;;; drop input invalid
      chain=input action=drop connection-state=invalid log=no log-prefix="" 

13 X  ;;; defconf: drop all not coming from LAN  (THIS RIGHT NOW IS DISABLED, I just saw it was in def conf but didn't want it to be turn on in case my VPN breaks)
      chain=input action=drop in-interface-list=!LAN log=no log-prefix="" 

14    chain=input action=log protocol=udp dst-port=xxxxx log-prefix="L2TP Attempt"

I’m still watching some videos but so far I have this

Sorry to dissapoint but your firewall is still a giant mess. You still don’t have sufficient forward rules to block malicious traffic. I don’t see how rules 1, 2 and 3 “whitelist” certain IPs and you still haven’t allowed proper access for the VPN to work (I don’t know how it hasn’t broken yet). Better use the default firewall rules (or this modified for your needs version) as a base and start upgrading from there:

/ip firewall filter
add action=accept chain=input comment="defconf: accept established,related,untracked" connection-state=established,related,untracked
add action=drop chain=input comment="defconf: drop invalid" connection-state=invalid
add action=accept chain=input comment="defconf: accept ICMP" protocol=icmp
add action=accept chain=input comment="Allow IPsec IKE" dst-port=500 protocol=udp
add action=accept chain=input comment="Allow L2TP" dst-port=1701 protocol=udp
add action=accept chain=input comment="Allow IPsec NAT-T" dst-port=4500 protocol=udp
add action=accept chain=input comment="Allow IPsec ESP" protocol=ipsec-esp
add action=drop chain=input comment="defconf: drop all not coming from LAN" in-interface-list=!LAN
add action=accept chain=forward comment="defconf: accept in ipsec policy" ipsec-policy=in,ipsec
add action=accept chain=forward comment="defconf: accept out ipsec policy" ipsec-policy=out,ipsec
add action=fasttrack-connection chain=forward comment="defconf: fasttrack" connection-state=established,related
add action=accept chain=forward comment="defconf: accept established,related, untracked" connection-state=established,related,untracked
add action=drop chain=forward comment="defconf: drop invalid" connection-state=invalid
add action=accept chain=forward comment="Allow internet access from LAN in-interface-list=LAN out-interface-list=WAN
add action=accept chain=forward comment="Port forwarding" connection-nat-state=dstnat connection-state=new in-interface-list=WAN
add action=drop chain=forward comment="Drop everything else"

If you want to access the router from the WAN without VPN, suggest reading up on Port Knocking.

Learning by doing is the most efficient way, but when it comes to internet security, it has its drawbacks, so I repeat my offer for remote assistance: http://forum.mikrotik.com/t/issue-with-l2tp-ipsec/181625/13

Thank you, I’ll do that.
rules 1, 2 and 3 are in fact port knocking.

Because you have to access to one port in order to be put in a temporary list for 10s, then if you access to another port it puts you in a valid list for 1h and then if you access the winbox or ssh port you can enter.