A thorough, organized plan for your specific WG connectivity will go a long way to establishing a working config.
- INTRO
(1) Generic Settings for WG Devices
(2) Overlapping Peers
(3) Configuring IP Routes
(4) Using IP address for WG Interfaces
(5) Configuring Firewall Rules
(6) My NETNAME or any DYNDNS URL
(7) Third Party VPN & Source-NAT
(8) Behind Another Router & Source-NAT
(9) Other Considerations: A. No Public IP, B. Firewall & Mangling, C. Crypto Key Routing Explained, D. MTU-MSS Issues
IF YOU ARE NOT NEW TO WIREGUARD GO STRAIGHT TO THE TOPIC ABOVE THAT INTERESTS YOU!!
=================================================================================================
Common Reasons to use VPN-Wireguard
1. Accessing the Internet from another location.
example - remote site to local site OR reverse (one tunnel) ------> mobile iphone or subnet on a remote device, to the internet through a local Wireguard connected device.
example - remote siteA -- to local site -- to remote siteB (two tunnels) ----> mobile iphone to local Wireguard Device and then enters second tunnel to third party providers internet.
2. Accessing Servers/Subnets at another location
example - remote site to local site or reverse (one tunnel) ---> users on remote subnet need to access local wireguard device's subnets
example - remote siteA -- to local site -- to remote siteB (two tunnels) ---->mobile iphone to local Wireguard Device and then enters second tunnel to a remote router's LAN server
3. Bundling Accessing Internet and Subnets -combination of the above!
4. Config Router by Admin any combination of tunnels as outlined above.
INTRO: If you have reached this article, you are probably starting on your first Wireguard setup for one of the reasons above. Its fun and not as complex as many other types of VPN. This article will help explain the various bits and pieces that work together to ensure traffic flows the way you want it too. If nothing else, get a piece of paper (or open word doc) and go through the exercise of filling in the information considered in Steps1-6 and the PLAN 1-5. . All the work after that will make much more sense and will lead to a successful configuration experience!
[Gather Information/Requirements]
Step1: Identify all the connecting devices involved - the ones with Wireguard configuration settings
Step2: Identify all the users, either individuals (like a smart phone or road warrior/laptop), or groups of users (aka a subnet of users).
Step3: Identify which user(s) need access to internet through WG (and thus not from their local ISP)
Step4: Identify which user(s) need access to the subnet at the other end of the tunnel (could be on an MT device or another router up or down from the MT device).
Step5: Identify as you the admin, which MT devices you wish to manage/Config from your local MT end of the WG tunnel.
Step6: Identify between applicable pairs of WG devices, WHICH device, for the initial connection, will be acting as SERVER (listening port active) and which will be CLIENT (sends original request).
[Planning Steps]
1. Plan Traffic Flows - from where to where for all use cases! (Dont forget Admin to Devices for configuration if required)
2. Plan Initial Tunnel Connection - which site will start the tunnel request (client).
3. Plan coordinated Wireguard IP address Subnet - for all Peer Wireguard Interfaces. ( see - (4) USING IP ADDRESS FOR WG INTERFACE )
4. Plan Routing of Traffic - how to get the traffic routed properly
5. Plan Firewall Rules - how to get the traffic, entering and exiting the wg interface, to where it needs to go.
[Start Configuring Devices - on paper first]
6. Configure Wireguard Interfaces based upon the complete set of requirements/gathered information.
7. Configure Router settings. Applicable for MT devices as additional router settings such as IP Routes and Firewall Rules are normally required.
[The INITIAL CONNECTION DILEMMA]
i. To establish a WG connection, at least one end of the WG tunnel should have a WG Device with a reachable public IP. If the reachable Public IP resides with an ISP Router or a different device prior to the WG device, (aka internet---->Generic Router/Modem--->WG Device) this could still be considered a USABLE connection, if one can PORT FORWARD from the ISP connected Router/Modem to the listening port on the WG capable device. Such a device can be considered for the SERVER role in the initial connection between wireguard devices.
ii. If one end of the tunnel does not have a WG device with a reachable public IP and one has no control over the ISP connected Router, then this un-configurable end of theWG tunnel is NOT ABLE to act as a Server for the initial connection. Examples of such instances are smart phones or road warriors with laptops.
iii. The WG device that cannot 'host' a connection (not reachable by public IP), must be considered as the Client in the Server/Client relationship when first establishing the tunnel, and if possible should make use of its keep alive setting (in Peer Settings).
iv. After initial connection is established the relationship is no longer Server/Client, the tunnel is now open to two way traffic.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ONCE YOU HAVE COMPLETED STEPS 1-6 & PLAN 1-5 ABOVE, YOU ARE READY TO START THE CONFIG 6-7
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
As discussed above the very basic concepts/steps for setting up any Mikrotik Device are highlighted below as we start to go into the details.....
1. The initial connection configuration ---> one device has to act as client and the other as server.
2. Coordinate an IP address subnet, that includes all the peers, so that each wireguard interface has an associated IP address. For mobile devices (single IP/32) this is the address assigned in the wireguard parameters under Interface Settings.
3. Allowed IPs at each device found in PEER Settings. It should include all destination address of local users - outbound that will enter the tunnel AND it should include all source addresses of incoming (remote) users - inbound and will exit tunnel. Additionally, It should also include the IP addresses of all other associated peer wireguard interfaces.
4. Ensuring IP routes are created manually. Not required for Mobile Devices
5. IP routes will be created dynamically (DAC) for the Allowed IPs that detail the Wireguard Interface IP addresses.
6. On Mikrotik devices, FIREWALL RULES are key to ensuring that traffic exiting the tunnel is allowed to go where it needs to go, or the right traffic is allowed to travel to the tunnel.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(1) GENERIC SETTINGS FOR WG DEVICES
Wireguard Interface Settings (not mobile)
Add name=WG-X Any name of your choosing.
Add listen port=xxxxx. Essential/required only for any Device acting as Server for the initial connection. It is normal practice for 'client' devices to simply put the same port as entered for the Server Device (the Listening Port).
Created public key= public key generated by the local device that needs to be provided to the Remote Device (for entry in its associated Peer settings).
Wireguard PEER Settings (not mobile)
Select Interface Name entered above
Endpoint IP address - the PUBLIC IP or associated DYNDNS name of the Server side of the tunnel (not normally required on WG Server device).
Endpoint Port - the listening port entered on the Server Device (not normally required on WG Server device).
Allowed IPs (addresses) per peer.
Persistent Keepalive Normally used for the client device only and can be set at the Admins discretion, common practice is anywhere between 25-45 seconds.
Important Discussion of Allowed IPs
Think of 'Allowed IPs', in the sense of IP addresses being identified on the OTHER END DEVICE, when identifying the TWO local distinct traffic flows of INBOUND and OUTBOUND Plus we normally add the wireguard interface IP addresses of all peers that may be used for pinging and troubleshooting connectivity.
A. OUTBOUND: In the case of outbound traffic from the local device to the remote device (From SOURCE) , the destination addresses are used by the local WG Device in a SELECTOR (matching) function, to be determine if any of the local users' destination addresses, being executed at any time, line-up with those IP addresses identified on the one or more PEER settings on the local Wireguard settings. More specifically, the Device will first determine if a Route exists for that traffic(destination address) and then if the IP Route associated Gateway points to the Wireguard Interface, the Router will then check the Allowed Addresses, starting with the first Peer for that Wireguard interface and then any remaining peers. The Peer selected (first match) will determine the tunnel selected (destination IP and Port). A WG interface can have multiple peers and each peer can have multiple addresses. Be careful of overlapping or duplicating such Peer Allowed IPs/destination IP addresses as that will cause issues and is a CONFIG ERROR on your part. ( see - (2) )
Normally such OUTBOUND "Allowed IPs (addresses)" consist of two types:
(i) accessing the internet on the other end of the tunnel and thus use address 0.0.0.0/0 (all possible internet addresses)
(ii) accessing specific IP addresses (subnets, server, users, Wireguard Interface IP addresses, etc.) at the other end of the tunnel and thus use applicable IP address(es) as required.
Note1: The dst-address 0.0.0.0/0 also includes subnets at the other end and thus one would only require the insertion of the internet address nomenclature to cover both needs.
Note2: The IP address or subnet to be accessed may not even exist at the other end of the tunnel but that will clearly require further config at the far end MT device to make it work. A typical example could be a Server that exists on another remote Router, where both the local user device and server on that remote devices connect to an intermediary WG Server Device (hosting both tunnels).
B. INBOUND: In the case of inbound traffic from the remote device to the local device (At RECIPIENT), the IP addresses are used by the Recipient device (local device) as a FILTER function, which will only allow traffic with source IPs that are defined on the local Peer Allowed IPs lists. If no such IPs are defined, the traffic will not be be permitted to exit the WG interface and thus not gain access to the LAN or internet etc.
FUN FACT: What you will discover with experience is that there is OFTEN duplication of inbound and outbound flows. Put simply, it is not uncommon for the destination IPs of local users (outbound) to be the same as the source address of inbound remote users (bidirectional traffic). Thus a single "Allowed IP" entry may cover both inbound and outbound traffic. The extreme case being Alllowed IPs=0.0.0.0/0 which allows all traffic (but in this case watch out for overlapping peers).
Wireguard Interface Settings (typical mobile)
Add name=WG-X Any name of your choosing.
Created public key= public key that needs to be provided to the Server Device (in its Peer settings).
Addresses= The IP address that will identify the Mobile Device on the Wireguard network. Consider this entry to be the Wireguard IP address for the mobile device. With that in mind, ensure it is planned so that it is included in the coordinated subnet plan for all the Peer Wireguard IP addresses.
MTU=Default value 1420. Normally only changed if partial connectivity issues occur. Try changing this first before mangling and other solutions.
DNS Servers= Anything can be entered here. Typically I put the same external DNS servers that my Server Router also has such as 1.1.1.1,9.9.9.9
Wireguard PEER Settings (typical mobile)
Public Key: Provided by Remote Server Device.
Endpoint address - the PUBLIC IP or associated DYNDNS name of the Server side of the tunnel and the associated port, typically either as two separate line entries or in the following format:
publicIP:port or mynetname:port etc...
Allowed IPs= Covered previously but in most cases this is the internet setting of 0.0.0.0/0 which also covers any subnet access at the remote device or the IP Wireguard interface address of the remote device. If internet is not the destination, then ensure the correct addresses are included.
On Demand: This ensures the mobile device remains connected to the Wireguard network when switching providers (aka hotel wifi to cellular).
-----> Allowed IPs Calculator: https://www.procustodibus.com/blog/2021 ... alculator/
(2) OVERLAPPING PEERS
CAUTION OVERLAPPING PEERS CAUTION: One should not have the same IP addresses identified on two or more peers of ANY SINGLE Wiregard Interface. If you find yourself in this situation, create another WG interface to separate the duplication.
Reason. The local WG device, as stated previously, when receiving a destination address from a user first locates a working route for that traffic and then attempts to match the destination address to the ALLOWED IPs lists entered into the peers of that particular interface (in our case a Wireguard interface), and if it finds a match, selects that peer and sends the traffic accordingly. It has no way of knowing which peer to send that traffic too and only matches the first available peer IP address. This issue is more frequently seen when sending out users through a remote site for internet and have several peers for this purpose, as both have the same destination address of 0.0.0.0/0. The peers do not need to be active for this error to be experienced.
(3) CONFIGURING THE IP ROUTES
When configuring IP routes at the local device, one has to consider two traffic flows. One is the traffic originating or considered to be 'starting' at the local device. This could be local users OR for example traffic that has come through and exited the wg interface (and now at the WG Server Device) and is to be relayed through either the same WG interface or a different interface (second tunnel) onwards to another device ( eg.another client MT device or a 3rd party VPN.) The other traffic to direct is any returning traffic that originated at a Remote Client, has exited the tunnel and now needs to return to that same Client. This return traffic could be coming from the internet or perhaps a server at the Local WG Server Device. In the more complex relay example noted above, the Return Traffic, lets say return internet traffic from the 3rd party VPN (via wireguard) that has exited from the Wireguard Interface, onto the Relay Device, and now needs to be directed back through the applicable wireguard interface to the originating user.
(i) Route for Subnet / Single IP - LOCAL TRAFFIC (starting traffic). At the local device, to ensure local user(s)/subnet(s) are sent through the tunnel when attempting to reach destination IP addresses at remote devices, a fairly standard IP Route directing those local users to the wireguard interface gateway is created:
/ip route
dst-address=IPsubnet/24 (or IP address/32) gwy=WG-interface table=main (where the dst address basically matches an allowed IP address entry - destination address of local users).
(ii) Route for Subnet / Single IP - REMOTE TRAFFIC (returning traffic). At the local device, to ensure remote user(s)/subnet(s) RETURN Traffic (after reaching the internet or local device servers for example) is directed BACK through the tunnel again a fairly standard IP Route is created:.
/ip route
dst-address=IPsubnet/24 (or IP address/32) gwy=WG-interface table=main (where the dst address basically matches an allowed IP address entry - source IPs of remote users).
FUN FACT: What you will discover with experience is that OFTEN, just as in 'Allowed IPs" and due to bidirectional traffic, one IP Route covers both local 'originated' traffic AND remote returning traffic. Consider the case where the local traffic needs to reach the remote subnet 192.168.50.0/24 and that same remote subnet needs to reach a server on the local device. At the Local device, the "Allowed IP" of 192.168.50.0/24 covers both outbound (destination addresses) and inbound (remote source IPs) considerations. Similarly, one only needs one IP route to ensure that local users get sent out the wireguard interface with destination address of 192.168.50.X and the remote users (with their source addresses) are directed back into the tunnel after hitting the local server.
/ip route
dst-address=192.168.50.0/24 gwy=wg-local table=main { Is a single valid IP Route for BOTH CASES! }
Note1: For Mobile Devices with fixed single IPs, that are already included in the coordinated IP wireguard interface schema, a dynamic IP routing (DAC) will already exist and thus a manually created route will not be needed.
Note2: Each Peer will require such routes and multiple IP addresses within each peer should need such routes.
Note3: Manual Routes will not be required for traffic to/fro Wireguard Interface Addresses.
Note3: It may be necessary to source-nat packets entering a tunnel so that they more easily handled at the other end (special cases).
(iii) Route for Internet Traffic . Internet traffic is a special case in terms of IP Routes for Wireguard Interfaces. Mainly because we have to identify this by stating ALL possible IP addresses/subnets - 0.0.0.0/0! As above we will consider the two cases of Return Traffic and Starting Traffic at the local device. Return is straightforward but Starting requires further work:
a. RETURN (REMOTE) TRAFFIC: Remote users are exiting the local device wireguard tunnel and using the local device WAN connection for internet. How do we deal with Traffic returning from the WWW, with source address of remote users. We have to direct that traffic back through the tunnel. It is fairly straightforward:
/ip route
dst-address=IPsubnet/24 (or IP address/32) gwy=WG-interface table=main (where the dst address basically matches an allowed IP address entry - source address of remote users).
Note: One should see that this matches the configuration for the INBOUND return traffic from local subnet/servers, example above (ii)
b. ORIGINAL (LOCAL) TRAFFIC: One or more local users (or even remote users that came in on a WG tunnel and now considered local) need to ENTER a tunnel to use the Internet at a Remote Device. However, often, there are either multiple subnets on the local Router or only a single user/device has the requirement to go elsewhere for internet. (Many possible combinations) Why is this difficult? We have to recognize and accommodate for the fact that INTERNET access at the local Router already exists for all other users/devices on the Router.
Thus if we already have an IP route dst-address=0.0.0.0/0 gwy=wanIPgateway table=main we will need to deconflict for internet access!!
Q1 - How do we take only one subnet out of two or three on the local device and point it to the tunnel
Q2 - How dow we take one device and point it to the tunnel.
Solution: We create a new table (other than main) and we point the selected traffic to that table in a routing rule. No mangling required. We "FORCE" the traffic into the tunnel so that it is not captured by the already existing route to the internet through the WANIP.
/routing table fib add name=use-WG
/routing rule src-address=IP address action=lookup-only-in-table table=use-WG
/ip route
dst-address=0.0.0.0/0 gwy=wg-interface-name table=use-WG
Note: If the admin wants the users to be able to access internet locally if WG is down then use ACTION=Lookup { akin to fail over }
(4) USING IP ADDRESS FOR WG INTERFACE
The right plan is to create a coordinated subnet to connect all the Peer Wireguard Interface IP addresses.
IP ADDRESS - INDEPENDENT
Ensure the subnet you create is independent of any subnets existing on any of the devices (excluding mobile devices). This process also ensures proper device to device communications and also aids in troubleshooting(pinging). One should include mobile devices ( where its single IP Address is assigned within the Wireguard settings), that unlike MT devices do not have separate IP address functionality and do not have subnets within the device.
Using en example of three peers and one main router, each peer device and the main router are given an IP address within the same subnet.
Looking at the table below, If the admin wants to check ping from one device to another (local to remote), the admin has to make sure that on the local device the remote WG address is included in the peer IP "allowed IPs" addresses (viable destination address for pinging).
WIREGUARD INTERFACE ADDRESSES.
/ip address
add address=10.10.5.5/24 interface=wg-local {at Local Router} SERVER FOR INITIAL CONNECTION!!
add address=10.10.5.4/24 interface=wg-peer3 {at remote device 3 - Hex}
For mobile devices the wireguard interface address is the ADDRESS you give to the wireguard connection WITHIN the wireguard settings:
Address=10.10.5.2/32 = {at remote device 1 - iphone}
Address=10.10.5.3/32 = {at remote device 2 - laptop}
PEER ALLOWED IPs
(only considering Wireguard interface IP addresses, for actual traffic add to this base/minimum setting)
Iphone (Peer=Local Router), Allowed IPs=10.10.5.0/24 (done) [note: not required if for internet access as 0.0.0.0/0 includes all addresses.]
Laptop (Peer=Local Router), Allowed IPs=10.10.5.0/24 (done) [note: not required if for internet access as 0.0.0.0/0 includes all addresses.]
Hex (Peer=Local Router), Allowed IPs=10.10.5.0/24 + any other traffic required = outbound (remote destination addresses at local) + inbound (remote source addresses from local)
Local Router : SERVER FOR CONNECTIONS
Peer1 AllowedIPs=10.10.5.2/32 (done)
Peer2 AllowedIPs=10.10.5.3/32 (done)
Peer3 AllowedIPs=10.10.5.4/32 + any other traffic required = outbound (remote destination addresses at hex) + inbound (remote source addresses from hex)
IP ROUTES
Iphone & Laptop not applicable - Mobile Devices (dependent upon the particular architecture of smart phone OS or Laptop OS apps etc.....) They can ping directly as the routing is applied internally.
Hex (created dynamically)
(dac) dst-address=10.10.10.0/24 gwy=wg-peer3 table=main (routes hex pings to Router (dest addr) and/or routes remote Router return traffic after pinging hex)
Local Router (created dynamically)
dst-address=10.10.10.0/24 gwy=wg-local table=main routes Router pings (dest addr) to all peer devices and routes remote Peer return traffic after pinging Router)
NOTE: Firewall Rules still need to be reviewed to ensure any pinging traffic is permitted (LAN/ROUTER to WG -- or -- WG to LAN/ROUTER)
(5) CONFIGURING FIREWALL RULES
If you use the default rules, there is basically nothing stopping most LAN level traffic and without realizing it, wireguard traffic will be allowed both to the router itself and the other LAN entities. It is for this reason, it is recommended that putting DROP all rules at the end of your input and forward chains is done, so that such traffic is under your control, and not automatically allowed.
Assuming one uses a drop all rule at the end of the input and forward chains, one recognizes that we have to create "allow" rules for wireguard traffic. In any Mikrotik device (client or server) and more accurately (local or remote) there are two cases that may occur.
The first is local outbound traffic from an admin, or user or subnet of users to be permitted to enter the tunnel.
The second case is to allow remote inbound traffic from remote users, to access a local server, an entire subnet, the internet, or the MT device for configuration purposes.
The following rules demonstrate the possibilities that exist at both ends of the tunnel:
add action=accept chain=input in-interface=wg-interface-name dst-port=winboxport protocol=tcp src-address=Admin_IP { allows admin to config local router remotely }
add action=accept chain=forward src-address=Subnet out-interface=wg-interface-name { to allow local subnet to enter the wireguard interface }
add action=accept chain=forward in-interface=wg-interface-name dst-address=IPofServer { to allow remote users access to local server } ***
add action=accept chain=forward in-interface=wg-interface-name out-interface-list=WAN { to allow remote users access to local WANIP (internet) } ***
Note1: In the last two examples *** use src-address or src-address-list to refine access to specific users, which may be required for multiple addresses from a peer and multiple peers.
Note2: Ensure Peer Devices software firewalls ( ex. Windows PC ) do not block WG traffic including ICMP.
(6) MYNETNAME or DYNDNS URL - SPECIAL CONSIDERATION FOR ENDPOINT (reresolve DNS)
A known issue with Wireguard occurs when the Endpoint address is DYNAMIC and has been entered via DYNDNS type name. If the wireguard tunnel is initiated before DNS has resolved the DYNDNS URL, the wireguard client will stop and will NOT restart. One has to manually disable and enable the peer to kick-start the tunnel. This can happen if the endpoint actual WANIP changes or a router is rebooted etc. In other words, there are multiple reasons why a wireguard server device is temporarily unavailable (it is rebooted or perhaps the public facing router is rebooted, changes IP address or suffers a power outage)
Sol/n 1 - CHECK LOCAL_IP NAME THROUGH TUNNEL - Use the script (courtesy of gdanov) on the Remote (originating) end of the tunnel to detect when the endpoint device is available by checking local IPs through the tunnel. In other words 'when down'. The Local_IP refers to the server end of the connection and is any local IP reachable through the tunnel. If that IP is reachable through the tunnel then the tunnel is working! If not, then disable the wireguard peer and enable it after 60 seconds. Run this as a scheduled script perhaps every 2 minutes.
...
:local wgcheckip Local_IP
:local endpointip xxxyyy.sn.mynetname.net
#:log info "wg check-ip $wgcheckip "
:if ([/ping $wgcheckip interval=1 count=5] =0) do={
:log info "WG down $wgcheckip"
/interface/wireguard/peers/disable [find endpoint-address=$endpointip];
:delay 60
/interface/wireguard/peers/enable [find endpoint-address=$endpointip];
:log info "WG up again $wgcheckip"
}
Sol/n 2 - BASIC NETWATCH - Easier method! (unknown author) Using the Netwatch Tool, using the "DOWN" tab, and the HOST IP is the Local IP ( Reachable subnet or IP address through the tunnel - best to use Wireguard Interface IP address of Server). However, this has been found not to work if the Server side is down for an extended length of time. Suggest if the Server is problematic, try a different script or perhaps extend the delay to 75 seconds. The problem occurs when the Server is still down after the time delay as Netwatch does not trigger/function again (no UP achieved) as its still down.
...
:delay 25
/interface wireguard peer disable 0
:delay 5
/interface wireguard peer enable 0
:log info "WGPeer toggled"
Sol/n 2 - ADVANCED NETWATCH VARIATION on the EASY METHOD - Here a script is used to toggle the Wireguard interface (not netwatch), but netwatch is used to enable or disable the scheduler for the script! Define script to toggle WG peer settings:
.......................
/system script
add dont-require-permissions=no name=ToggleWGPeer owner=*** policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon
source="
:delay 25
/interface wireguard peer disable 0
:delay 5
/interface wireguard peer enable 0
:log info \"WGPeer toggled\"
"
Define schedule for earlier created script:
I used 2 minute period here, adjust as needed for your own purposes.
Code: Select all
/system scheduler
add disabled=yes interval=2m name=ScheduleWGToggle
on-event="/system script run ToggleWGPeer"
policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=may/01/2022 start-time=11:34:35
Define Netwatch up/down actions:
Assuming address to be verified "on the other side" is 10.255.255.1 (change as needed for your purposes)
Assumption 2: run every minute (in most cases there is nothing to do... but I prefer not to wait too long when something does go haywire)
Code: Select all
/tool netwatch
add down-script="
log warning \"NETWATCH - WG Down\"
/system scheduler set ScheduleWGToggle disabled=no"
host=10.255.255.1
up-script="/system scheduler set ScheduleWGToggle disabled=yes"
FOR ADVANCED USERS -------
It takes all enabled peers with endpoint-address ending with letter (which means it's non-empty and not numeric address) and last handshake older than 5 minutes ("5m" on second line), takes current hostname and sets it again as endpoint-address, which makes RouterOS resolve it again. It should be safe to run it unconditionally from scheduler. In addition, this script will will trigger the endpoint-address reset for blank last-handshake as well.
{ meaning it addresses the following: If the peer has never connected (or if the peer is disabled and re-enabled while the endpoint is down), then the last-handshake is blank and the > 5m condition is false. }
:foreach i in=[/interface/wireguard/peers/find where disabled=no endpoint-address~"[a-z]\$"] do={
:local LastHandshake [/interface/wireguard/peers/get $i last-handshake]
:if (([:tostr $LastHandshake] = "") or ($LastHandshake > [:totime "5m"])) do={
/interface/wireguard/peers/set $i endpoint-address=[/interface/wireguard/peers/get $i endpoint-address]
}
}
VARIATION of ADVANCED (courtesy of msatter!)
>>>>>>>>>>>>>>>>>>>>>>>
{; # BeginOfScript
# scripted by msatter
# function: bring up stalled WireGuard interfaces after restart of the router
:local timesRetried 15 ; # how many times WireGuard is tried to be restarted
:local loopDelay 10s ; # (loopDelay * timesRetried ) = total timeout
:local restarted true ; # set default to true
:local domainResolved false ; # also checking if the endpoint domain-names could be resolved
:local retried 0 ; # set to starting value
:while ( $restarted && ($retried < $timesRetried) ) do={
# loop till all Wireguard interfaces are working or there the maximum retries is reached
:set $restarted false
/interface/wireguard
:foreach wg in=[find disabled=no] do={
:local peer [get $wg name]
# scripted by Anav looking for domain names. Adapted by msatter.
:foreach i in=[peers/find interface=$peer endpoint-address~"[a-z]\$"] do={
:if ( [:resolve [peers/get $i value-name=endpoint-address]] ) do={
:set $domainResolved true
:set $lastHandshake [peers/get $i last-handshake]
:if ( ([:tostr $lastHandshake] = "") || ($lastHandshake > [:totime [peers/get $i persistent-keepalive]]) ) do={
disable $peer; :delay 1s; enable $peer ; # restarting the WireGuard connection
:set $restarted true
}; # EndIf
}; # EndIf
}; # EndForeach
}; # EndForeach
:if ( $restarted ) do={
:put "Check loop: $retried"
:set $retried ($retried + 1)
:put "Checking loop: $retried"
:delay $loopDelay ; # waiting time till following check
}; #EndIf
}; # EndWhile
:if ( !$domainResolved ) do={
:put "One or more domains could not be resolved, all/some domain based endpoints could not be brought up in the set time of ($timesRetried * $loopDelay)"
} else={
:if ( $restarted && ($retried > $timesRestied) ) do={:put "Not all WireGuard interfaces could be brought up in the set time of ($timesRetried * $loopDelay)"}
:if ( !$restarted && ($retried > 0) ) do={:put "No WireGuard interfaces are down, after $retried retries"}
:if ( $retried = 0 ) do={:put "No WireGuard interfaces had to be restared"}
}; # EndElse
}; #EndOfScript
(7) THIRD PARTY VPN ---> WHY SOURCE-NAT
Often, MT users want to use a third party VPN for all their internet traffic. The considerations are very much standard except for one big difference. VPN providers give you a SINGLE IP address for your WG settings. This makes sense for something like an iphone, or laptop, but creates issues if applying the VPN on an MT device. This is important because the third party VPN will be expecting all traffic to be coming from this IP address as SOURCE IP! In wireguard vernacular, that means the PEER SETTINGS at the 3rd Party VPN server to describe and filter incoming traffic from your MT device for ALLOWED IPs will be set to the single IP address they gave you. Since we have LAN users from potentially multiple subnets going over the wireguard tunnel we have to change their private IPs to that of the assigned WG address. Hence we have to have a way to take all the traffic entering the tunnel locally to appear as if they are coming from that one IP address ---> - answer is SRC-NAT! (see example).
Understanding source-nat and masquerade is an important point for the beginner! SRCNAT Masquerade basically replaces the outgoing source IP address of traffic with the IP of the outgoing interface, be it the local WANIP, OR the IP address of the wireguard interface. The router will un-SRCNAT the return traffic as required.
For this example, we are assuming the 3rd Party VPN provider has assigned you an IP address=10.10.10.2/32, supplied the endpoint IP and port and you have exchanged public keys etc. Thus most of the settings are complete. Lets also assume your MT local LANIP subnet to use the tunnel is 192.168.20.0/24
MT WIREGUARD PEER SETTINGS:
1. MT Allowed IPs = 0.0.0.0/0 ( also set keep alive to something like 40 seconds)
MT RoS SETTINGS:
2. IP address - Add address=10.10.10.2/24 interface=wireguard1
3. Firewall rules -You need to ensure your subnet or subnets are allowed to enter the tunnel. (as applicable)
add chain=forward action=accept src-address=192.168.20.0/24 out-interface=wireguard1 (single subnet)
add chain=forward action=accept in-interface-list=LAN out-interface=wireguard1 (all subnets)
4. IP Route - To force all users out Wireguard for internet vice local ISP. You should have the default route already in place, either automatically because in IP DHCP Client you have YES selected for use ISP as default route, OR you should have created one manually.
add dst-address=0.0.0.0/0 gwy=ISP gateway-IP table=main.
THREE STEPS:
Add table
/routing table add name=useWG fib
Add routing rule (use as many route rules as you have subnets, use route rules for any individual exceptions on a subnet that should not go out the wireguard interface for internet)
/routing rule add src-address=192.168.20.0/24 action=lookup table=useWG
Add additional route
dst-address=0.0.0.0/0 gwy=wireguard1 table=useWG
Note: lookup means use the table indicated for traffic but if the table is not available, look for another routing (which means check table=main to see if any alive routes exist and use that one).
If you had selected lookup-only-in-table, then the router would not look else where if the wg tunnel was not available and traffic would be dropped.
5. SOURCE-NAT - Two options:
Option 1: (add wireguard interface to WAN LIST)
/interface list
add name=WAN
add name=LAN
/interface list members
add interface=bridge list=LAN
add interface=ether1 list=WAN
add interface=wireguard1 list=WAN
/ip firewall nat
add chain=srcnat action=masquerade out-interface-list=WAN
OR if you dont add the wireguard interface to the WAN interface list
Option 2:
/ip firewall nat
add chain=srcnat action=masquerade out-interface=ether1 ( assuming ether1 is the name of your WAN interface, it could be pppoe-1, or a vlan etc.)
add chain=srcnat action=masquerade out-interface=wireguard1
(8) BEHIND ANOTHER ROUTER ---> WHY SOURCE-NAT
The Problem: Remote users are exiting the tunnel locally at your MT Server for the purpose of accessing your ISP connection to the local internet. The remote traffic exits the tunnel on your MT device and by firewall rules and routing exits the MT Wan port and then gets sent out the FIrst/Main Router's WAN port to the internet. The return traffic, from remote users, hits the First/Main router and the router looks at the source address of the traffic, ( the subnet IPs of the remote users that came into the tunnel ) and does not recognize the traffic as being valid for any of the First/Main router's own subnets. Nothing is 'routable' from the router's perspective and the traffic is dropped (never reaches the MT device).
The Solution: SOURCE NAT the incoming remote traffic, exiting the wireguard tunnel, to the WANIP of the 'natted" MT device!!
Now the returning traffic from the internet will hit the First/Main router and the router will look at the source address and say, Oh yeah I recognize that, I have a subnet for that traffic that is 'routable' and thus I will send it to the MT Devices WANPORT!! The MT router detects the source-natting on this arriving traffic and then UN-SRC-NATS the traffic back to the applicable remote users IP addresses, which the MT Device further recognizes is valid return traffic and 'routable' (due to existing IP routes for remote user subnet(s)) and sends the traffic to the wireguard interface.
CASE1: MT Device is behind an MT ROUTER. This is the best case scenario as we have full flexibility to create static routes for return traffic on the First/Main router, if we choose do so. Source-NAT is also a viable option. It is assumed that one has the capacity to port forward the wireguard port via destination NAT, from the MT Router to the MT device.
CASE2: MT Device is behind ANOTHER ROUTER. Some routers allow both static routes and port forwarding and thus the solution will be predicated upon the First/Main Routers capabilities.
(Note: if the First/Main Other Router is incapable of forwarding ports see Para 9. (A).)
(1) Static Route - on ISP Router: If the ISP router is capable of static routes then the following equivalent could be be constructed:
dst=address=subnet_of_users gwy=WANIP of second router table=main
(2) Source NAT - on Server Router: With only the ability to forward ports on the ISP router, the preferred solution is to SRC NAT the remote traffic, that will exit the tunnel, to the WANIP of the Mikrotik Server device.
add chain=src-nat action=masquerade out-interface-list=WAN src-address='subnet_of_remote-users'
(3) Source NAT to a PI WG Server behind a REMOTE SERVER Router - from an MT Client Device: If you have a PI server behind an ISP router (with PI WG parameter interface IP Address = 10.6.0.5/24) and want to use your MT device as a client, with user subnet 192.168.20.0/24, to reach the remote ISP internet connection one will need to consider a route back for return traffic at the PI server. If the PI server (OR ISP Router) cannot create a static route and the PI Server cannot do source nat, for 192.168.20.0/24, all the return traffic packets to go back through the tunnel, will be dropped. Consider using source-nat at the MT Client device.
/ip firewall nat add chain=srcnat action=src-nat src-address=192.168.20.0/24 out-interface=Wireguard to-addresses=10.6.0.254
This is the problem, the PI server has no way to route the return traffic from the remote subnet (your MT Device users). Remember the ISP router the PI server is on, has no knowledge of the subnets on your MT device and there is no way for you to add any routes on the remote ISP router. Thus, if you source-nat all your local client IPs to WITHIN the wireguard structure of the wireguard pi server, then the router will assume the return traffic needs to go back to the PI Server which will then route the traffic out the PI wireguard tunnel.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(9) OTHER CONSIDERATIONS
A. UH OH - BOTH MY DEVICES DON'T HAVE A PUBLIC IP
In some rare cases you will have a situation where both Mikrotik devices are behind ISP routers that provide a public IP and worse, they are not able to be accessed to port forward the all important listening port!! Please check out this thread for ideas on how to acquire a usable Public IP - viewtopic.php?t=183427
B. USING FIREWALL ADDRESSES & MANGLING
When one needs to IDENTIFY or force traffic for a group of IPs, and not a single source address, or destination address, or interface, or subnet, the only option left to identify such a grouping is by Firewall Address List. However, this entity is not definable as a destination address on an IP route, nor is it definable on the Routing Rule.
There are TWO OPTIONS:
1- Make as many Route Rules as there are single IPs (if less than a subnet or a mix of IPs from various subnets) and use source address (no mangling)!
2-Make a single Route Rule but with mangling and USE ROUTE MARK as the entry argument in the Route rule!
BASIC EXAMPLE. MIxed bag of IP addresses go through tunnel, but too many for option1!
a. capture the traffic via marks in pre-routing
b. capture the marks via route marking in pre-routing
c. apply the route mark in the applicable Routing Rule
d. disable fast track.
ADVANCED EXAMPLE - All traffic must go through the tunnel, all IPs, subnets etc.......
In this case we want ALL SUBNET Traffic to go out the tunnel using Table=MAIN
How to ensure that the initial handshake that should go out the normal ISP WAN pathway is taken if there is no IP route for this.
Solution: Make use of Mangle and output chain (no effect on fastrack!!). The trick is to use the route-mark name as also the routing table name.
Step1: Identify the firewall address for our config
/ip/firewall/address-list add list=WG-Server-Router address= sn.mynetname.net (address=fqdn-of-remote-peer)
Step2: Create the mangle rule........
/ip firewall mangle
add action=mark-routing chain=output dst-address-list=Wireguard_Server-Router new-routing-mark=useWAN passthrough=yes
Step3: Create the Routing Table
/routing table add name=useWAN fib
Step4: Create the IP route
/ip route
dst-address=0.0.0.0/0 gwy=ISP gateway table=useWAN
C. UNDERSTANDING THE CRYPTO KEY ROUTING PROCESS (CKRP)
If you want to know when and how encryption is applied and used during Wireguard transactions this article is probably the clearest to be found. https://www.wireguard.com/#cryptokey-routing
Suffice to say Mikrotik IP routing and Wireguard Crypto Key Routing work together to ensure authorized traffic flows to where it needs to be. In a very general sense, IP Routing brings the traffic to the Wireguard Protocol or takes traffic output by the Wireguard Protocol for dissemination while the Wireguard Protocol does its internal Routing as required. A routing sandwich if you will where CKRP is the meat between the bread of IP ROUTING.

To help understand the above article this explanation may be helpful.
What the new person, beginner, should focus on is that Crypto Key Routing Process (CKRP) runs in the background and has to do mainly with two processes. However, for crypto routing to occur, it is the responsibility of the admin, to first accurately state the Allowed IPs for each peer (key word being peer=remote site). More specifically the admin should, at any local device, consider the Allowed IPs as they relate to the remote end for two distinct populations of addresses, a. the destination addresses at the remote end, for local users heading outbound and entering the tunnel, and b. the source addresses for users at the remote end, heading inbound and will be exiting the tunnel.
Now that the Allowed IPs are correctly established by the admin, Crypto Routing can carry out its two integrated processes, and the really core bit MAPPING KEYS TO PEERS, uses the admin Allowed IPs to ensure the right peers and right keys are matched to the local traffic entering the tunnel so that the right destination information is added and subsequently at the other again compare Keys and peers and then allowed IPs to ensure authorized traffic is allowed to exit the tunnel. The other process which occurs is the encryption and decryption (not as exciting). That is enough detail for the new beginner at the start of their journey.
In order to provide a bit more detail for the beginner this explanation may be useful.
For LOCAL OUTBOUND TRAFFIC.
User Traffic (payload) ---> Destination address ----> MT device -----> IP Routes
IP Routes ---- > gwy=wireguard interface ---> Crypto Key Routing Process
Wireguard Protocol ( CKRP) - think of Wireguard as a Mini Router
CKRP attemps to find a match of destination address with Allowed IP in the list of Peers starting at Peer1 (order is important!)
Match found ----> Peer Selection ----> encrypt payload (right public key) ----> add associated destination address and destination port for that peer --->create transport packet
Transport packet ----> Exit CKRP --->IP Routes
IP Routes ----> gwy= ISP gateway IP table=main ---> WWW.
FOR LOCAL INBOUND TRAFFIC,
Incoming Transport packet ---> Microtik Device ----> Incoming destination port exists on device ------> IP routing
IP Routing ---> Due to destination port on transport packet ------>Wireguard Mini Router ---> CRKP
CKRP ---> based on dest port, CRKP attempts to authenticate with available associated public keys until successful,
CKRP ---> the public key is used to decrypt transport packet into payload and reveals Source IP address
---> Compare source IP to Peer Allowed IPs from the Peer used for decryption of payload
---> Source IP exists ----->Exit CKRP---> send payload to IP Routes
IP Routes---> Route to correct interface via destination address and firewall rules.
D. MTU-MSS ISSUES
1 - Ensure ICMP is not blocked, this affects PMTUD, which apparently is some function that manages MTU and MSS settings. When I find out what they all do I will add it here.
(8) NETWORK DIAGRAM
Use a network diagram to fully describe your plan and it will be intuitively easy to understand the requirements when posting on the forum.
================================================================================================
TOC:
Scenario 1 - (HARD) Traversing Two Wireguard Interfaces on one Router and Third Party VPN
Scenario 2 - (MEDIUM) Initiating a Wireguard tunnel from Both Sites
Scenario 3 - (EASY) Initiating a Tunnel From One Site to Allow Traffic in the Opposite Direction.
Scenario 4 - (MEDIUM) Peer to Peer tunnelling with one Wireguard interface & Use of IP addresses for Wireguard interfaces.
================================================================================================
SCENARIO-1:
I have two sets of users coming from the internet through my Local MT Router. One is a subnet from a Remote MT Router 10.1.10.0/24 and the other is an IPHONE user, which we will assign an IP of 172.16.1.2/32. The remote subnet should be able to access my Local MT Router for internet access only. The Iphone user should be able to access my subnet 192.168.50.0/24 but also use an existing vpn connection through 3rd Party Wireguard provider called BiteMe for internet. I want one of my subnets on my Local MT Router 192.168.20.0/24 to go out the third party provider for internet as well. I want to be able to config the Remote Router from my admin PC, 192.168.10.15/32 and the winbox port on the Remote Router is 6969
As an aside, to access the Remote MT router by winbox, via wireguard, one needs to use any Remote Router local Ip address and thus I like to choose the IP address of one of the subnets (a trusted subnet for example). Lets say the trusted subnet of the home-lan at the remote site is 10.1.50.0/24.
..... .....
LOCAL ROUTER WIREGUARD INTERFACE1 (Server for establishing tunnel with Remote Router)
Wireguard Settings
name=WG-Local
listening port=15555
public key generated= xxx (to be used on both iphone and Remote Router peer addresses)
Peer Settings - Applicable for Remote Router
allowed IPs (addresses)=10.1.10.0/24, 10.1.50.1/24 ( the first address is for filtering traffic EXITING the tunnel, the second is the Remote Routers local IP address I am going to use as my destination address for winbox and is entered here to be used for selection to ENTER the tunnel)
public key=yyy (from Remote Router)
Peer Settings - Applicable for Iphone
allowed IPs (addresses)=172.16.1.2/32 ( traffic from this IP will be allowed to exit the tunnel at the Local Router )
public key=iii (from iphone)
---------------------------------------------------------------------------
LOCAL ROUTER WIREGUARD INTERFACE 2 (Client for establishing tunnel to third party)
Wireguard Settings
name=WG-BiteMe
listening port=n/r
public key generated= ccc (to be given to BiteMe)
Peer Settings
allowed IPs (addresses)=0.0.0.0/0 ( this will match and select all internet bound traffic, that should go through the vpn provider, to enter the tunnel )
endpoint address= publicIP address provided by BiteMe
endpoint port=port provided by BiteMe
public key=ddd ( public key provided by BiteMe )
persistent keep alive= 40 secs
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
REMOTE ROUTER
Wireguard Settings
name=WG-Remote
listening port=15555
public key generated= yyy
Peer Settings
allowed IPs=0.0.0.0/0 (this will match and select all internet bound traffic, that should go through the Local Router, to enter the tunnel, and in addition includes and thus will filter (accept) the Admin IP inbound traffic from the Local router to exit the tunnel )
public key=xxx (From Local Router)
persistent keep alive=30 secs
IPHONE
Wireguard Settings
name=mywg
address=172.16.12.2/32
public key generated=iii
DNS Servers= 1.1.1.2, 9.9.9.9
Peer Settings
allowed IPs=0.0.0.0/0 (to eventually access the BiteMe wireguard tunnel, and allows subnet access on the Server Router)
public key=xxx (From router)
===================================================================================================
ROUTER SETTINGS - ROUTES
LOCAL ROUTER (Server)
IP Address - not required for the wg interface.
IP Routes
dst=10.1.10.0/24 gwy=WG-Local table=main { ensures return traffic (replies) gets routed back to the subnet user(s) via the tunnel }
dst=172.16.1.2/32 gwy=WG-Local table=main { ensures return traffic (replies) gets routed back to the iphone user via the tunnel }
dst=10.1.50.1/24 gwy=WG-Local table=main { admin on Local Router will use winbox IP:Port of 10.1.50.1:6969 to access Remote Router }
dst=0.0.0.0/0 gwy=BiteMe table=useBM
/routing rule (order is important)
add action=lookup-only-in-table src-address=192.168.20.0/24 table=useBM { ensures local subnet users enter tunnel for 3rd party VPN provider internet }
add action=lookup-only-in-table dst-address=192.168.50.0/24 table=main { ensures iphone traffic for subnet is captured prior to third rule }
add action=lookup-only-in-table src-address=172.16.1.2/32 table=useBM
/routing table add name=useBM fib
REMOTE ROUTER
IP Address - not required for the wg interface.
IP Routes
dst-address=192.168.10.15/32 gwy=WG-Remote table=main
dst-address=0.0.0.0/0 gwy=WG-Remote table=useWG
/routing rule
add action=lookup-only-in-table src-address=10.0.1.0/24 table=useWG
/routing table add name=useWG fib
ROUTER SETTING - FIREWALL RULES
LOCAL ROUTER(server)
/input chain
add chain=input action=accept dst-port=15555 protocol=udp { if applicable assumes ISP router is port forwarding 15555 to the WANIP of the MT Router }
...
add chain=input action=drop
/forward chain
add chain=forward action=accept in-interface=WG-Local src-address=172.16.1.2/32 dst-address=192.168.50.0/24
add chain=forward action=accept in-interface=WG-Local out-interface=WG-BiteMe src-address=172.16.1.2/32
add chain=forward action=accept in-interface=WG-Local src-address=10.0.1.0/24 out-interface-list=WAN
add chain=forward action=accept src-address=192.168.20.0/24 out-interface=WG-BiteMe
...
add chain=forward action=drop
REMOTE ROUTER
/input chain
add chain=input action=accept in-interface=WG-Remote scr-address=192.168.10.15/32
...
add chain=input action=drop
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SCENARIO-2: - https://www.procustodibus.com/blog/2020 ... -wireguard
This is a simpler scenario, WHERE USER 1A, needs to wireguard tunnel to an HTTP SERVER 1B .
This scenario also demonstrates a situation where both MT devices have either a public reachable IP address or private IP where one can port forward from the ISP modem/router. This approach will support any situation where the tunnels are not operating for some reason and either side can initiate the tunnel for their needs. I have thus added to the linked scenario, a reverse requirement where a USER (2B) on Router B, needs to access an HTTPS server (2A) on Router A.
..... .....
ROUTER A -------------------------------------------------------- ROUTER B
1A- 192.168.1.11 USER on subnet 192.168.1.0/24 ... 1B- 192.168.200.22 Server on subnet 192.168.200.0/24
2A- 192.168.5.3 Server on subnet 192.168.5.0/24 ..... 2B- 192.168.60.7 User on subnet 192.168.60.0/24
ROUTER A CONFIG
INTERFACE
Interface Name = WG-A
ListenPort = 51821
Public Key Generated --> To be entered on peer settings Router B
PEERS
PublicKey ---> to be entered is from Router B
Endpoint = 203.0.113.2: ( we need endpoint information from Router B here if router A needs to originate the tunnel )
Endpoint port = 51822
Allowed IPs (to match and select local user destination IPs, to enter tunnel outbound) = 192.168.200.22/32 (SOURCE FLOW)
Allowed IPs (to filter remote user source IPs to exit the tunnel inbound) = 192.168.60.7/32 (RECEIVED FLOW)
Thus Allowed IPs=192.168.200.22,192.168.60.7
keep alive=25 seconds ( needed if originating the tunnel )
IP FIREWALL RULES - ( need to allow user 2B to access server 2A )
add chain=input action=accept dst=port=51821 protocol=udp
add chain=forward action=accept in-interface=WG-A src-address=192.168.60.7 dst-address=192.168.5.3 dst-port=443
Other rules will accomplish same with less precision...... but depending may be necessary.
add chain=forward action=accept in-interface=WG-A dst-address=192.168.5.0/24
add chain=forward action=accept src-address=192.168.60.0/24 dst-address=192.168.5.0/24
IP ROUTES
dst-address=192.168.60.7/22 gwy=WG-A table=main ( ensures replies from Server 2A go back through the tunnel to user 2B )
dst-address=192.168.200.22/32 gwy=WG-A table=main ( ensures local user get sent out tunnel to Router B )
Note: The wireguard interface WG-A and also on the other router WG-B, can be identified/selected on interface list members but cannot be added to a bridge!
ROUTER B CONFIG
INTERFACE
Interface Name = WG-B
ListenPort = 51822
Public Key Generated--> To be entered on peer settings Router A
PEERS
PublicKey ---> to be entered is from Router A
Endpoint = mynetname-Router_A: ( Router A has a private IP and a dyndns name is required for example )
Endpoint port = 51821
Allowed IPs (to match and select local user destination IPs, to enter tunnel outbound) = 192.168.5.3/32 (SOURCE FLOW)
Allowed IPs (to filter remote user source IPs to exit the tunnel inbound) = 192.168.1.11/32 (RECEIVED FLOW)
Thus Allowed IPs=192.168.5.3/32,192.168.1.11/32
keep alive=30 secs
IP FIREWALL RULES - ( need to allow user 1A to access server 1B )
add chain=input action=accept dst=port=51822 protocol=udp
add chain=forward action=accept in-interface=WG-B src-address=192.168.1.11 dst-address=192.168.200.22 dst-port=80
Other rules will accomplish same with less precision.......and may be necessary depending.....
add chain=forward action=accept in-interface=WG-B dst-address=192.168.200.0/24
add chain=forward action=accept src-address=192.168.1.0/24 dst-address=192.168.200.0/24
IP ROUTES
dst-address=192.168.1.11/32 gwy=WG-B table=main ( ensures replies from Server 1B go back through the tunnel to user 1A )
dst-address=192.168.5.3/32 gwy=WG-B table=main ( ensures local user gets sent through the tunnel to Router A)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SCENARIO-3:
This is a basic scenario where Site B has an MT Map behind an ISP router, with no access to the ISP Router. The admin, located at Site A, needs to accomplish two functions at Site B: a. to access a certain device on the Map LAN subnet and b. to be able to configure the Map remotely. To accomplish this we are going to have to establish the tunnel with the Map at Site B being the initiating Client device and the MT Router at Site A to be the receiving Server device. In other words we are establishing the tunnel in one direction but all the data traffic will originate at Site A and thus flow in the opposite direction. To make it slightly more interesting, the Site A MT Router (wireguard server) is also behind an ISP router. We are limited by this but we can port forward the listening port to the MT Router. The Map is also interesting. In this example it has no LAN subnet, it bridges the LANIP subnet from the ISP router to ether 2 and other ports.
..... .....
ROUTER A CONFIG
INTERFACE
Interface Name = WG-A
ListenPort = 80901
Public Key Generated --> To be entered on peer settings Router B
PEERS
PublicKey ---> to be entered is from Router B
Allowed IPs (to match and select local user destination IPs, to enter tunnel outbound) = 192.168.0.0/24 (Outbound Flow)
Allowed IPs (to filter remote user source IPs to exit the tunnel inbound) = none on this config (Inbound Flow)
Thus Allowed IPs=192.168.0.0/24
IP FIREWALL RULES
add chain=input action=accept dst=port=8090 protocol=udp { for initial connection from Router B }
add chain=forward action=accept src-address=192.168.0.0/24 out-interface=WG-A { to allow admin and various users to Router B }
IP ROUTES
dst-address=172.18.1.1/0/24 gwy=WG-A table=main { traffic heading for Router B will get pushed into the tunnel }
ROUTER B CONFIG
INTERFACE
Interface Name = WG-B
ListenPort = 80902 { not really required }
Public Key Generated--> To be entered on peer settings Router A
PEERS
PublicKey ---> to be entered is from Router A
Endpoint = mynetname-Router_A: ( Router A has a private IP and a dyndns name is required for example )
Endpoint port = 80901
Allowed IPs (to match and select local user destination IPs, to enter tunnel outbound) = none ( Outbound Flow)
Allowed IPs (to filter remote user source IPs to exit the tunnel inbound) = 192.168.0.0/24 (Inbound Flow)
Thus Allowed IPs=192.168.0.0/24
keep alive=30 secs
IP FIREWALL RULES - { need to allow users to access device and admin to access router }
add chain=input action=accept in-interface=WG-B src-address-list=siteA dst-port='winboxport'
add chain=forward action=accept in-interface=WG-B src-address=192.168.0.0/24 dst-address=172.18.1.22/32
IP ROUTES
dst-address=192.168.0.0/242 gwy=WG-B table=main { ensures replies from SiteB go back through the tunnel to Site A users }
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SCENARIO-4:
This is an interesting scenario where the admin can access Site A but cannot access Site B. The admin does not work from either site and must use a laptop/smartphone to connect to the Routers. The admin wants to be able to config both Routers and he wants to be able to reach a Server at Site B. The users at Site B also need to use Site A for all internet traffic.
This is the only Scenario where use of IP addresses for Wireguard Interfaces is explicitly applied.
Admin Requirements:
Needs access to site A, in order to config Router A (which has a public IP and is thus reachable).
Needs access to site B, in order to config Router B and reach a Server on Router B (which is not reachable, not a public IP and has no control/access to ISP router),
Hence, the configs must allow the admin to reach Router B FROM Router A.
User Requirements:
Users at site B must use Site A for all internet.
.... .....
ROUTER A CONFIG
INTERFACE
Interface Name = wireguardM
ListenPort = 6868
Public Key Generated --> To be entered on peer settings Router B
PEERS
PublicKey ---> to be entered is from Router B
Allowed IPs (to match and select local user destination IPs, to enter tunnel outbound) = 10.0.1.0/24 (Outbound Flow, to Router B and Server)
Allowed IPs (to filter remote user source IPs to exit the tunnel inbound) = 10.0.1.0/24, 172.16.0.0/30 (Inbound Flow) (from Router B subnet and admin devices)
Thus Allowed IPs=10.0.1.0.0/24,172.16.0.0./30
IP FIREWALL RULES
add chain=input action=accept dst=port=6868 protocol=udp { for initial connection by admin devices and Router B }
add chain=input action=accept dst-port=WINBOXPORT in-interface=wireguardM src-address=172.16.0.0/3 { access by admin to config Router A }
add chain=forward action=accept in-interface=wireguardM out-interface-list=WAN src-address=10.0.1.0/24 { allows Remote site users to access internet through Main router }
add chain=forward action=accept src-address=172.16.0.0/30 dst-address=10.0.1.0/24 { allows admin to access both Remote router for config and any device on its LAN subnet including server! }
IP ROUTES - NOT Required!!
Note: Since you have IP addresses assigned to the Wireguard Interface (see below) covering both the site B subnet and Road warriors, the router will automatically route any LAN traffic traffic or internet traffic automatically to or back through the wireguard interface and peers as required.
IP ADDRESSES
/ip address
add address=172.16.0.254/24 interface=wireguardM network=172.16.0.0
add address=10.0.1.254/24 interface=wireguardM network=10.0.1.0
ROUTER B CONFIG
INTERFACE
Interface Name = wireguardR
ListenPort = 6868 (not really required }
Public Key Generated--> To be entered on peer settings Router A
PEERS
PublicKey ---> to be entered is from Router A
Endpoint = mynetname-Router_A: ( Router A has a public dynamic IP )
Endpoint port = 6868
Allowed IPs (to match and select local user destination IPs, to enter tunnel outbound) = 0.0.0.0/0 ( Outbound Flow - internet traffic for Router B subnet )
Allowed IPs (to filter remote user source IPs to exit the tunnel inbound) = 172.168.0.0/30 (Inbound Flow - admin inbound traffic )
Thus Allowed IPs=0.0.0.0/0 ( the admin IPs are contained with the 0.0.0.0/0 )
keep alive=30 secs
IP FIREWALL RULES - { need to allow admin to access Router and Server on Router B }
add chain=input action=accept in-interface=wireguardR src-address=172.16.0.0/30 dst-port=winboxport
add chain=forward action=accept in-interface=wireguardR src-address=172.168.0.0/30 dst-address=IPofServer
Note: If the admin wanted access to the whole LAN subnet - dst-address=10.0.1.0/24.
IP ROUTES
dst-address=0.0.0.0/0 gwy=wireguardR table=useWG { required to force Router B LAN users to go out wireguard tunnel vice own ISP WAN }
/routing table add fib name=useWG
/routing rule add action=lookup-only-in-table src-address=10.0.1.0/24 table=useWG
Note: No other routes are required as the IP address (see below) for the wireguard interface will automatically route replies from the admin back through the tunnel.
IP ADDRESSES
/ip address
add address=172.16.0.254/24 interface=wireg