A thorough, organized plan for your specific WG connectivity will go a long way to establishing a working Peer to Peer 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 - Keys / Source-Nat / DNS
(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, and E. Network Diagram
(10) L2TP thru WIREGUARD for MTU Issues
...
(15) SIX SCENARIO EXAMPLES
If You are Not New To Wireguard Go Straight To The Topic Above That Interests You.
FOR OTHER WIREGUARD APPLICATIONS ADVANCED USES (EOIP, ETC) - viewtopic.php?t=194646
FOR MIKROTIK DOCS -https://help.mikrotik.com/docs/display/ROS/WireGuard
#1 Issue - Don't have an accessible public IP, commonly WISP connections, Cellular Internet, CGNAT and Starlink to name a few!
#2 Issue - You have mismatched public key entries!
#3 Issue - You have peer 'endpoint entry' which is blank, endpoint-address=" ", remove endpoint altogether!
#4 Issue - You have to sourcenat your users to the wireguard interface when using a 3rd party VPN for internet!
=================================================================================================
Common Reasons to use Wireguard - A Peer to Peer VPN Protocol
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 instance, will be acting as SERVER (listening port active) and which will be CLIENT (sends original request). Could be both if they each have an accessible Public IP.
[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). As noted, could be either device if they both have accessible Public IPs.
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. The initial connection starts on the client side when the router detects a user has requested a destination such that IP routing hits the wireguard interface. The Router then looks at the wireguard peer addresses that matches and then knows which SERVER is involved. The MT client router will establish a connection from itself (using default local WANIP) to the Server Router.
v. After initial connection is established the relationship is no longer Server/Client, the tunnel is now open to two way traffic and traffic can occur from either side (peer to peer network).
vi. Most of the rest of the discussion addresses when one end of a tunnel has limited WANIP access. If you have two MT devices, with accessible public IPs, you do not need keep alives, but more importantly both sets of users behind the tunnels (or even road warriors 'remoting' into one of the MT Routers) can initiate the tunnel creation. If the MT is acting as a client then use the persistent keep alive setting 30 seconds is fine.
For tracking/logging and config purposes it is recommended to use unique listening ports/input chain rules at each end.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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 for that instant of time ( only one input chain rule needs to be triggered ).
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 instance. If the client device (speaking about connection instance only) does not have an accessible public IP, it is normal practice to simply put the same port as entered for the Server Device (the Listening Port) and this will actually never be used. However if you have two MT devices local and remote, with both having accessible IPs, one need NOT be concerned about keep alives or which is client or server for the connection, simply config both to have unique listening ports etc. and when users initiate wireguard traffic the tunnel creation instance in either direction will occur and the peer to peer wireguard tunnel will be in play.
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 Remote side of the tunnel ( required if initiating the wireguard tunnel ).
Endpoint Port - the listening port entered on the Remote Device. ( required if initiating the wireguard tunnel )
Allowed IPs (addresses) per peer.
Persistent Keepalive Normally used for the client type devices (iphones etc), 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 Remote (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. MT recommends entering in the WG gateway IP of the server (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 Remote (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.
++++ To create another wireguard interface you will need an additional listening port and associated input chain rule on the WG Remote (Server) Router.
(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 add fib name=use-WG
/routing rule add 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 In general, the Router acting as a hub (usually the receiver of initiated connections) will have specific Peer settings for each client. In this regard each peer in allowed IPs for the wireguard IP, should be set to the specific IP/32. When one has multiple routers, ensure that on any given router, there is not the case of overlap with two wgsubnet .0/24 settings to describe allowed IPs (no two peers on any router should have.0/24). The router will not know which peer you are discerning (much like overlapped peers discussed previously). It is okay to have a mix of one wgsubnet .0/24 and multiple specific wgips/32 as /32 is a longer mask (like in routes) and will always be chosen when requested and thus the router will not be confused at any point.
For single clients its normal/okay to put the wg subnet for 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). See **** Recommended script.
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 ------- Courtesy of Sob/Dave ( called elegant by Chupaka even )
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
Lastly another script courtesy of AOAKELEY ( called Brutal by Chupaka )
# Wireguard check script
#Declare variables
:local wgport
:local wgpeer
# Disable any peers that have last handshake > 3mins
/interface/wireguard/peers/set disabled=yes [find last-handshake > [:totime "3m"]]
# Disable any peers that have tx or rx 0 (sometimes happens after router restart)
/interface/wireguard/peers/set disabled=yes [find rx=0 or tx=0]
:delay 2
# For any disabled peer get the port and clear any connections in the firewall
:foreach i in=[/interface/wireguard/peers find disabled=yes] do={
:set wgport ([/interface/wireguard/peers get $i endpoint-port])
:foreach j in=[/ip firewall connection find dst-address~":$wgport\$" protocol=udp] do={
/ip firewall connection remove $j
: log info "** Wiregiuard Check Script ** firewall connections on port $wgport cleared"
}
# and re-enable any disabled peers
/interface/wireguard/peers set $i disabled=no
# and let the log file know what has happened
:set wgpeer [/interface/wireguard/peers get $i interface]
:log info "** Wireguard Check Script ** wireguard peer $wgpeer restarted"
}
(7) THIRD PARTY VPN ---> Keys / SOURCE-NAT / DNS - See (9)D for Surfshark VPN
There are two primary methods of 'getting keys' from third party providers. The most common is for the VPN provider to give you a private KEY to use in your own WG interface settings and their public key which one puts on the router peer settings for the VPN provider. Since one only has control at one end of a third party VPN, you should be wondering, how do I give the VPN provider the public key generated by my MT router? By using the private key they gave you on the MT wireguard interface settings this is accomplished. The clue is that regardless of platform, the same private key will generate the same random public key. Thus the VPN provider will give you the Private Key to use for your interface settings as this will then generate a KNOWN (to them) public key and thus you don't have to send one to them. In the standard MT to MT scenario and some other third party providers you need to physically provide the public key generated by a random private key. Router A gives its own public key to B and keeps its own private key secret. and B gives its own public key to A and keeps its own private key secret. This way, A can verify that what has arrived has come from B, and B can verify that what has arrived has come from A.
Caution: When the VPN provider gives your their public key and your private one, whoever has intercepted the delivery of those keys from the VPN provider to you could use your account with the provider.
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
6. DNS - If one has identified a subnet (be it a vlan or not) that will be going through the wireguard tunnel, ensure you modify the IP DHCP NETWORK SERVER
/ip dhcp-server network
add address=10.10.0.0/24 comment=LAN-4-WG dns-server=3rdPartyDNS gateway=10.10.0.1
7. THIRD PARTY VPN IP ADDRESS /16 etc.. (not /32). - In many instances third party VPNs also give out an IP address on a wide ranging network. This does not pose any siginficant problems for the MT device. Simply add the address to your IP address for the wireguard interface 10.10.10.2/16 for example. The <DAC> route will be created and with source nat applied all local traffic should work fine out the tunnel.
8. TWO ACCOUNTS FROM SAME THIRD PARTY VPN - What do you do if you need two different locations for your tunnels and the provider gives you a second account but with the exact same IP address. NO PROBLEM but you will need to create a SECOND wireguard interface. Use the same IP address but with a different interface name.
/ip address
add address=10.10.10.2/16 interface=wireguard1
add address=10.10.10.2/16 interface=wireguard2
This will create two DAC routes
<DAC> dst-address=10.10.10.0/16 interface=wireguard1 table=main
<DAC> dst-address=10.10.10.0/16 interface=wireguard2 table=main
(9) Port Forwarding Interference! Sometimes the admin forgets that a port forwarding range may overlap the wireguard listening port. Ensure they are separated otherwise DST NAT will take precedence and one will not have wireguard connectivity.
(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 WAN PORT!! 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 Typically observerd when unable to browse or very very slow through wireguard tunnel.
1 - Ensure ICMP is not blocked, this affects PMTUD, which apparently is some function that manages MTU and MSS settings.
2 - Try setting the MTU value at both client and server to 1500.
3 - On the Client end ONLY: Try MSS Clamping - ref forum thread --> viewtopic.php?t=150377#p740866
/ip firewall mangle
add action=change-mss chain=forward comment="Clamp MSS to PMTU for Outgoing packets" new-mss=clamp-to-pmtu out-interface=wireguard1 passthrough=yes protocol=tcp tcp-flags=syn
OR (with mtu default of 1420)
/ip firewall mangle
add action=change-mss chain=forward new-mss=1380 out-interface=wireguard1 protocol=tcp tcp-flags=syn tcp-mss=1381-65535
4- If the above do not work go to (10) below and try an LT2P tunnel where other MTU settings can be massaged.
E. 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.
(10) L2TP thru WIREGUARD for MTU Issues
For whatever reasons, sometimes connectivity even through a Wireguard Tunnel creates, or does not solve, MTU issues. One possible solution is to use L2TP through the wireguard tunnel, not for encryption but because L2TP settings allow MRRU through MLPPP. One uses the very basic L2TP settings and ensures MRRU is set at 1504 at both ends of the L2TP connection.
The following example will detail an initiating device (RB450G) for wireguard (client for connection) and a receiver device (RB4011) for wireguard (server for connection). Currently, the RBG uses wireguard to send subnet B out the internet connection of the RB4011. Users from Subnet A, require access to servers on subnet Y on the RB4011. In addition, the admin at RB4011 uses the connection to config the router at RBG.
Problem: Users from RBG subnet B, are using IPTV and when they go out internet on RB4011 they encounter issues connecting and it is suspected that MTU issues are at play.
Solution: Funnel such traffic through a plain L2TP tunnel where one can adjust MRU to 1504 and thereby alleviate the issue (dont ask me why or for details but if you wish to provide the why would be appreciated). All user traffic will be sent through the L2TP tunnel to fully explore L2TP settings.
Transport vs Payload
The public IP addresses of the two routers are used for the wireguard transport packets, the sole payloads of the wireguard tunnel are the L2TP transport packets running between the two wireguard IP addresses, and the L2TP payload is used to carry the actual payload (whatever subnet/user traffic we push through the tunnel). The internal L2TP addresses are there to be confirm the L2TP tunnel is ready for payload traffic (both sides are reachable) and they can be used as gateway IP addresses in IP Routes.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Lets setup the RB4011 For Wireguard Connectivity Only
/RB4011 info
/interface wireguard
listening port 15551 mtu=1420 name=wireguard-home
/interface wireguard peers
add allowed-address=192.168.50.2 interface=wireguard-home public-key="---"
/ip address
add address=192.168.50.1/24 interface=wireguard-home
/ip firewall filter { input chain }
add chain=input action=accept dst-port=15551 protocol=udp
/RB450G info
/interface wireguard
listening port 10771 mtu=1420 name=wireguard-client
/interface wireguard peers
add allowed-address=192.168.50.0/24 endpoint-address=mynetnameRB4011 endpoint-port=15551 interface=wireguard-client public-key="..." \
persistant keep-alive=35sec
/ip address
add address=192.168.50.2/24 interface=wireguard-client
Now lets layer in transparently the L2TP tunnel, but connect it through *****WIREGUARD!!
L2TP Server (RB4011)
One of the first things to do, on the server router, is create an Interface List Entry so that we can statically set an interface to be used in firewall rules. It is then referenced/selectable in the Profile setting for L2TP. The equivalent to this on the Client Router is the name of the L2TP client itself (default is l2tp-out1 and in our case we are using l2tp-to-RB4011).
The two addresses we set on the Server Router, under PROFILE, for local and remote addresses are internal dynamic addresses assigned to both interfaces and are KEY as they can be used for gateway IP addresses in Routes! We ensure that the User Name on the Secrets setting on the server router matches the Client Name on the l2tp client router settings.
To connect the L2TP tunnel to Wireguard we set on the L2TP client on the connection selection, the wireguard IP address of the the server router and we set on the source address selection, the IP address of the local Wireguard interface. Since the wireguard tunnel is already established the L2TP tunnel is now piggybacking through it.
.............
/RB4011
/interface list
add name=LT2P-Interface
/interface l2tp-server server
set enabled=yes mrru=1504
/ppp profile
add interface-list=LT2P-Interface local-address=172.16.5.2 name=L2TP-Server remote-address=172.16.5.5
/ppp secret
add name= l2tp-to-RB4011 password=site1 profile=L2TP-Server service=l2tp
/ip firewall filter { input chain }
add action=accept chain=input comment="Allow L2TP Client to RB4011" dst-port=1701 protocol=udp in-interface=wireguard-home
L2TP Client (RBG)
....
/RB450G
/interface l2tp-client
add connect-to=192.168.50.1 mrru=1504 name=l2tp-out1 password=site1 src-address=192.168.50.2 user=l2tp-to-RB4011
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
RB4011 Changes/Additions
1 - Allow initiating L2TP traffic from client router to reach and connect to the L2TP Router Service, as per the input chain rule shown.
2. - Allow Subnet A (from client) to reach Subnet Y on Local Router
add chain=forward action=accept in-interface-list=L2TP_Interface src-address=10.10.20.0/24 dst-address=192.168.20.0/24
3.- Allow Subnet B (from client) to reach internet on Local Router
add chain=forward action=accept in-interface-list=L2TP_Interface src-address=10.10.10.0/24 out-interface-list=WAN
4. - Allow admin on Local Router to enter tunnel to later config client Router
add chain=forward action=accept src-address=192.168.10.15 out-interface-list=L2TP_Interface
5. Create Necessary Routes for traffic into the L2TP Tunnel
add dst-address=10.10.20.0/24 gwy=172.16.5.2 table=main
add dst-address=10.10.10.0/24 gwy=172.16.5.2 table=main
Note: The admin will be able to access the client router for config purposes using an existing route (for ex. 10.10.20.1:XXX where XXX=winbox port) and no separate routing is required.
RB450G Changes/Additions
6. Allow admin from RB4011 to exit tunnel to configure client router
add chain=input action=accept in-interface=l2tp-to-RB4011 src-address=192.168.10.15 dst-port=winbox_port protocol=udp
7. Allow Subnet A to enter the tunnel to access the Server Subnet
add chain=forward action=accept src-address=10.10.20.0/24 dst-address=192.168.20.0/24 out-interface=l2tp-to-RB4011
8. Allow Subnet B to enter the tunnel to access the internet on the Server WAN
add chain=forward action=accept src-address=10.10.10.0/24 out-interface=l2tp-to-RB4011
9. Create the necessary Routes etc. for traffic to RB4011
add dst-address=192.168.20.0/24 gwy=172.16.5.5 table=main
add dst-address=192.168.10.15 gwy=172.16.5.5 table=main
10. For the internet Bound Traffic.
/routing table add name=useL2TP fib
/routing rule dst-address=10.10.20.0/24 action=lookup table=main {optional, in case you want subnet B to be able to reach subnet A and not go out internet }
/routing rule src-address=10.10.10.0/24 action=lookup-only-in-table table=useL2TP
/ip route add dst-address=0.0.0.0/0 gwy=L2TP-client table=useL2TP
Done! Confirm the LT2P tunnel is established by checking on the RB4011 under Secrets for L2TP Client if the local and remote address are now populated.
The challenge to the reader is that you can selectively choose what traffic you want to go through both tunnel interfaces or just the wireguard tunnel interface.
In other words, you can selectively decide which traffic goes through L2TP, and just stick to the standard wireguard tunnel for the rest of the traffic.
For example in the above case, ONLY the subnetB heading out the internet of the Server Router has to really go through the L2TP tunnel.
================================================================================================
(15) SCENARIOS
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 5 - (MEDIUM) ADGUARD Through WG
Scenario 6 - (HARD) PORT FORWARD TO CGNAT CLIENT
================================================================================================
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.
Construct the Wireguard Network Primary
LOCAL WG Router - add address=172.16.1.1/24
REMOTE WG Router - add address=172.16.1.3/24
IPHone device - add address=172.16.1.2/32
Identify the Third Party VPN Network
add address=10.10.10.20/24 - LOCAL ROUTER
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)=172.16.1.3/32, 10.1.10.0/24 ( the first address is the wireguard ID for the remote router (allows admin to ping the router or access router for config purposes), and the other is the incoming remote subnet)
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, unless BiteME provides a private key to use as my private key for this interface and thus they will have the public key)
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 enter the tunnel at the remote router and exit at the Local Router and use its public WANI,P and in addition includes also any subnets remote router users may want to visit on the local route and any incoming admin traffic to config the remote router )
public key=xxx (From Local Router)
persistent keep alive=30 secs
IPHONE
Wireguard Settings
name=mywg
address=172.16.1.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 or config both routers)
public key=xxx (From router)
===================================================================================================
ROUTER SETTINGS - ROUTES
LOCAL ROUTER (Server)
IP Routes ( assumes there is a Local WANIP route alread: dst-address=0.0.0.0/0 gwy=ISP table=main )
<dac> dst-address=172.16.1.0/24 gwy=WG-Local table=main {auto generated by router and ensures all iphone data and any pinging between wireguard devices is routed}
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=0.0.0.0/0 gwy=WG-BiteMe table=useBM { to be used by LOCAL subnet 192.168.20.0/24 and IPhone user }
/routing rule (order is important)
add action=lookup-only-in-table dst-address=192.168.50.0/24 table=main { ensures iphone traffic fore example, for shared devices is captured prior to other rules }
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 src-address=172.16.1.2/32 table=useBM {ensures Iphone enters tunnerl for 3rd party VPN}
/routing table add name=useBM fib
SourceNAT - Required to satisfy the thirdparty VPN provider which is only expecting traffic from 10.10.10.20 !!, thus need
add action=masquerade chain=srcnat out-interface=WG-BiteMe
REMOTE ROUTER
IP Routes ( assumes a standard ISP route already exists dst-address=0.0.0.0/0 gwy=ISP table=main )
dst-address=0.0.0.0/0 gwy=WG-Remote table=useWG { to force subnet into tunnel }
/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 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 { allow admin access to config }
...
add chain=input action=drop
/forward chain
add chain=forward action=accept src-address=10.1.10.0/24 out-interface=WG-Remote
....
add chain=forward 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
Lets Construct the Wireguard Networks
Router A - add address=10.10.10.1/24
Router B - add address=10.10.10.2/24
ROUTER A CONFIG
INTERFACE
Interface Name = WG-A
ListenPort = 51841
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 [/b]
Allowed IPs=10.10.10.2, 192.168.200.22,192.168.60.7 { first to allow pinging, second for dst traffic, third for remote incoming traffic }
keep alive=25 seconds ( needed if originating the tunnel )
IP FIREWALL RULES -
add chain=input action=accept dst=port=51841 protocol=udp
add chain=forward action=accept src-address=192.168.60.7 dst-address=192.168.5.3 dst-port=443 in-interface=WG-A
add chain=forward action=accept src-address=192.168.1.11 dst-address=192.168.200.22 dst-port=80 out-interface=WG-A
IP ROUTES
dst-address=192.168.60.7/32 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[/b]
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 = 51841
Allowed IPs=10.10.10.1/32, 192.168.5.3/32,192.168.1.11/32
keep alive=30 secs
IP FIREWALL RULES -
add chain=input action=accept dst=port=51822 protocol=udp
add chain=forward action=accept src-address=192.168.1.11 dst-address=192.168.200.22 dst-port=80 in-interface=WG-B
add chain=forward action=accept src-address=192.168.60.7 dst-address=192.168.5.3 dst-port=443 out-interface=WG-B
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)
Admin access to config remote router from local router. - If this is desired then one would have to add either the subnet in allowed IPs that the incoming admin is located upon ( easy if its hte same subnet you already have one user coming in on, or the individual's subnet IP. A corresponding input chain firewall rule will also be needed to allow that IP, to access winbox.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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 ( to forward ports or create routes ). 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 simplly bridges the LANIP subnet from the ISP router to ether 2 and other ports.
..... .....
Configure the Wireguard Networks
WG-SiteA - add address=10.20.30.1/24
WG-SiteB - add address=10.20.30.2/24
ROUTER A CONFIG
INTERFACE
Interface Name = WG-SiteA
ListenPort = 8090
Public Key Generated --> To be entered on peer settings Router B
PEERS
PublicKey ---> to be entered is from Router B
Allowed IPs =10.20.30.2, 172.18.1.0/24 { the first to be able to ping the map and the second for a target device on the remote LAN }
We set the LAN as an allowed IP in case the admin or users may wish to access other devices on the remote LAN.
IP FIREWALL RULES
add chain=input action=accept dst=port=8090 protocol=udp { for initial connection from Router B } ( this will be port forwarded on the ISP router to the Routers OS VM, 192.168.1.250 )
add chain=input action=accept dst-port=winbox src-address=LANIPof Admin
add chain=forward action=accept src-address=192.168.0.0/24 out-interface=WG-SiteA { to allow admin and if necessary various users to Router B }
IP ROUTES
<dac> dst-address=10.20.30.0/24 gwy=WG-A table=main { auto generated by router }
dst-address=172.18.1.0/24 gwy=WG-A table=main { traffic heading for Router B will get pushed into the tunnel }
IP Cloud: mynetnameWGSite-A
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 =mynetnameWGSite-A: ( Router at Site A has a private IP and a dyndns name is required for example )
Endpoint port = 8090
Allowed IPs=10.20.30.0/24, 192.168.0.0/24 {allows pinging of other wg devices and second to allow incoming users to exit the tunnel }
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 adminLANIP dst-port='winboxport { allows admin from Site A with IP 192.168.0.x, to access MAP device }
add chain=forward action=accept in-interface=WG-B src-address=192.168.0.0/24 dst-address=172.18.1.22/32 { allow remote access to local server }
IP ROUTES
<dac> dst-address=10.20.30.0/24 gwy=WG-B table=main { auto generated }
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 }
====================================================
What is not covered/dicussed above is how to set MAP for bridging of LAN to all ports. The MAP needs only a few settings.
...............
/interface bridge
add ingress-filtering=no name=bridgemap
/interface list
add name=management
/interface bridge port
add bridge=bridgemap interface=ether1
add bridge=bridgemap interface=ether2
add bridge=bridgemap interface=ether3
add bridge=bridgemap interface=WLAN
/ip neighbor discovery-settings
set discover-interface-list=management
/interface list member
add interface=bridgemap list=management
/ip address
add address=172.18.1.28/24 interface=bridgemap network=172.18.1.0
/ip dns
set allow-remote-requests=yes servers=172.18.1.1 comment="dns through trusted subnet gateway
/ip route
add disabled=no dst-address=0.0.0.0/0 gateway=172.18.1.1 comment="ensures route avail through trusted subnet gateway"
/ip service
set winbox address=172.18.1.xx,192.168.0.x comment="local and remote winbox access"
/tool mac-server mac-winbox
set allowed-interface-list=management
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SCENARIO-4:
This is an interesting scenario where the admin can access Site A (publicly available WANIP) 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 (10.0.1.44). The users at Site B (subnet 10.0.1.0/24) also need to use Site A for all internet traffic.
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.
....
STEP1: Design the WG interface network.
/ip address
add address=172.16.0.1/24 interface=wg-server network=172.16.0.0 {Router A}
add address=172.16.0.2/32 ( wg address on admin laptop (or smartphone etc)
add address=172.16.0.3/24 interface=wg-client network=172.16.0.0 {Router B}
.....
STEP2: ROUTER A CONFIG
Router A WG Interface & Peer Interface
Interface Name = wg-server
ListenPort = 6868
Public Key Generated --> To be entered on peer settings Router B and peer setttings on admin (laptop or smartphone)
PEERS
Peer1 (Router B)
PublicKey ---> to be entered is from Router B
Allowed IPs=10.0.1.0/24,172.16.0.3/32 - remote subnet from Router B exitiing the tunnel, and RouterBs, wireguard address
Peer2 ( admin )
Public Key ---> to be entered from admin laptop (or smartphone)
Allowed IPs=172.16.0.2/32 - admin device exiting the tunnel to reach server on Router A, config Router A, to get relayed to Router B for config and reach server on Router B
Router A 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=wg-server src-address=172.16.0.2/32 { access by admin to config Router A }
***add chain=forward action=accept in-interface=wg-server 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.2 dst-address=10.0.1.0/24 out-interface=wg-server { allows admin after exiting the tunnel to re-enter the tunnel heading for the other peer if heading to the other Peers valid subnet! Note: that we will use the firewall rules at Router B to specify which IPs if required/desired }
***Alternate Option: add wg-server to interface list = LAN, in that way Router B subnet users will not require a separate wg forward chain lan to wan rule, as they can use the existing default rule:
add chain=forward action=accept in-interface-list=LAN out-interface-list=WAN
IP ROUTES General -
Note: Since you have IP addresses assigned to the Wireguard Interfaces (as per Step 1), the router automatically creates a routing for wireguard network traffic on both Routers A, B,
<dac> dst-address=172.16.0.0/24 gwy=wg-server table=main (router A)
<dac> dst-address=172.16.0.0/24 gwy=wg-client table=main (router B)
In effect this means we do NOT need to create any routes for pinging via wireguard addresses and more importantly, for any single clients (road warriors) assigned a single wireguard IP address.
HOWEVER, we do need to create routes for remote subnets that visit a local router to let the router know where the return traffic (be it from the internet, or from local servers) needs to go as those subnets are not native to the local router.
Router A Routes......
add dst=10.0.1.0/24 gwy=wg-server table=main
STEP3 ROUTER B CONFIG
INTERFACE
Interface Name = wg-client
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=0.0.0.0/0 ( includes, internet addresses, the wireguard address and not in this case but any subnets on Router A)
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=wg-client src-address=172.16.0.2 dst-port=winboxport
add chain=forward action=accept in-interface=wg-client rc-address=172.168.0.2 dst-address=IPofServer
Note: If the admin wanted access to the whole LAN subnet - dst-address=10.0.1.0/24.
IP ROUTES In this case the challenge is to ensure all subnet users enter the wg for internet and not the local WAN on Router B.
This is a three step process (create table, add an additional route, and add the associated wireguard rule~
/routing table add fib name=useWG
dst-address=0.0.0.0/0 gwy=wg-client table=useWG
/routing rule add action=lookup-only-in-table src-address=10.0.1.0/24 table=useWG
Note: If you wanted local subnet users to be able to access the local WAN if the wireguard internet was not available then change to "action=lookup"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.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SCENARIO-5:
In this scenario, we have an MT Router that has TWO wireguard interfaces, a Wireguard Remote (WG-R) interface ( to access the router remotely ) and Third Party Wireguard ( WG-3P ), which is what all subnets behind the RB should use except for one subnet. In addition, a raspberry PI is on one of the VLANs going out the WG-3rdP. Besides wireguard, which is pretty straightforward in terms of needing a table, a route and some routing rules, the additional headache of using adguard which is going out the tunnel is fun. In addition, users on one of the outgoing subnet also needs access to a printer on another subnet.
Subnets are vlan10,vlan20,vlan30 and the one subnet that will use local internet is vlan40. If wireguard does go down then all the subnets should revert to local internet. The admin will use his other wireguard interface, to remotely connect to the router and access the adguard server to RE-AIM the adguard to a reachable DNS.
........
# model = MT Device
/interface bridge
add ingress-filtering=no name=Bridge vlan-filtering=yes
/interface ethernet
set [ find default-name=ether5 ] name=ether5-emerg
/interface wireguard
add listen-port=14221 mtu=1420 name=WG-R
/interface vlan
add interface=Bridge name=VLAN10-LAN vlan-id=10
add interface=Bridge name=VLAN20-IoT vlan-id=20
add interface=Bridge name=VLAN30-Guest vlan-id=30
add interface=Bridge name=VLAN40-NoVPN vlan-id=40
/interface list
add comment=defconf name=WAN
add comment=defconf name=LAN
add name=MGMT { used to ensure all smart devices are on the same isolated VLAN }
/ip pool
add name="DHCP VLAN20" ranges=10.0.20.150-10.0.20.254
add name="DHCP VLAN30" ranges=10.0.30.150-10.0.30.254
add name="DHCP VLAN10" ranges=10.0.10.100-10.0.10.254
add name="DHCP VLAN40" ranges=10.0.40.150-10.0.40.200
/ip dhcp-server
add address-pool="DHCP VLAN20" interface=VLAN20-IoT lease-time=1d name=\
"DHCP VLAN20"
add address-pool="DHCP VLAN30" interface=VLAN30-Guest lease-time=1d name=\
"DHCP VLAN30"
add address-pool="DHCP VLAN10" interface=VLAN10-LAN lease-time=1d name=\
"DHCP VLAN10"
add address-pool="DHCP VLAN40" interface=VLAN40-NoVPN lease-time=1d30m name=\
DHCP-VLAN40
/routing table
add disabled=no fib name=use-WG
/interface bridge port
add bridge=Bridge ingress-filtering=yes frame-types=admit-only-vlan-tagged interface=ether2 { trunk port to smart switch }
add bridge=Bridge ingress-filtering=no interface=ether3 pvid=10 { hybrid port to AP }
add bridge=Bridge ingress-filtering=no interface=ether4 pvid=10 { hybrid port to AP }
add bridge=Bridge ingress-filtering=yes frame-types=admit-only-untagged-and-priority-tagged interface=ether6 pvid=20 { access port }
add bridge=Bridge ingress-filtering=yes frame-types=admit-only-untagged-and-priority-tagged interface=ether7 pvid=10 { access port }
/interface bridge vlan
add bridge=Bridge tagged=Bridge,ether2 untagged=ether3,ether4,ether7 vlan-ids=10
add bridge=Bridge tagged=Bridge,ether2,ether3,ether4 untagged=ether6 vlan-ids=20
add bridge=Bridge tagged=Bridge,ether3,ether4 vlan-ids=30
add bridge=Bridge tagged=Bridge,ether2,ether3,ether4 vlan-ids=40
/ip firewall connection tracking
set loose-tcp-tracking=no
/ip neighbor discovery-settings
set discover-interface-list=MGMT
/ip settings
set rp-filter=loose
/ipv6 settings
set disable-ipv6=yes
/interface list member
add interface=ether1 list=WAN
add interface=VLAN10-LAN list=LAN
add interface=VLAN20-IoT list=LAN
add interface=VLAN30-Guest list=LAN
add interface=VLAN40-NoVPN list=LAN
add interface=ether5-emerg list=LAN
add interface=VLAN10-LAN list=MGMT { trusted subnet }
add interface=WG-R list=MGMT
add interface=ether5-emerg list=MGMT
/interface wireguard peers
add allowed-address=192.168.0.2/32 interface=WG-R public-key="..." { admin laptop remote }
add allowed-address=192.168.0.3/32 interface=WG-R public-key="..." { admin smartphone remote }
add allowed-address=0.0.0.0/0 endpoint-address=123.456.789.12 endpoint-port=\ { to Third Party Server }
42740 interface=WG-3P persistent-keepalive=35s public-key="..."
/ip address
add address=10.0.10.1/24 interface=VLAN10-LAN network=10.0.10.0
add address=10.0.20.1/24 interface=VLAN20-IoT network=10.0.20.0
add address=10.0.30.1/24 interface=VLAN30-Guest network=10.0.30.0
add address=10.0.40.1/24 interface=VLAN40-NoVPN network=10.0.40.0
add address=192.168.55.1/24 interface=ether5-emerg network=192.168.55.0
add address=192.168.0.1/24 interface=WG-R network=192.168.0.0
add address=10.45.77.12/24 interface=wg-3P network=10.45.77.0
/ip dhcp-client
add add-default-route=no dhcp-options=clientid,hostname interface=ether1\
/ip dhcp-server network
add address=10.0.10.0/24 dns-server=10.0.10.8 gateway=10.0.10.1 { raspberry PI adguard ip=10.0.10.8, since dns-server is not 10.0.10.1 dont need hairpin nat rule }
add address=10.0.20.0/24 dns-server=10.0.10.8 gateway=10.0.20.1
add address=10.0.30.0/24 dns-server=10.0.10.8 gateway=10.0.30.1
add address=10.0.40.0/24 dns-server=10.0.40.1 gateway=10.0.40.1
/ip dns
set allow-remote-requests=yes servers=public-server-of-choice use-doh-server=\ { vlan40 goes out local DOH dns }
https://your-doh-server-of-choice verify-doh-cert=yes
/ip firewall address-list
add address=Local-IP1-on-Vlan10 list=AllowWinBox { admin desktop }
add address=Local-IP2-on-Vlan10 list=AllowWinBox { admin smartphone }
add address=192.168.55.55 list=AllowWinBox { off bridge access }
add address=192.168.0.0/24 list=AllowWinBox { admin can remote in to Router via WG-R }
add address=10.0.10.8 list=Excluded { so adguard server is excluded from dstnat rule }
add address=10.0.40.0/24 list=Excluded { so vlan40 subnet is excluded from dstnat rule }
add address=192.168.0.0/24 list=Excluded { so admin remote traffic via WG is excluded from dstnat rule }
/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=\
"defconf: accept to local loopback (for CAPsMAN)" dst-address=127.0.0.1
add action=accept chain=input comment=WireGuard dst-port=14221 protocol=udp
add action=accept chain=input comment="Allow Admin to Router" \
src-address-list=AllowWinBox
add action=accept chain=input comment="Allow Users DNS TCP" dst-port=53 \
in-interface-list=LAN protocol=tcp
add action=accept chain=input comment="Allow Users DNS UDP" dst-port=53,123 \
in-interface-list=LAN protocol=udp
add action=accept chain=input comment="DNS TCP for wg-remote" dst-port=53 \ { Allow remote user access to DNS on router }
in-interface=WG-R protocol=tcp
add action=accept chain=input comment="DNS UDP for wg-remote" dst-port=53 \ { Allow remote user access to DNS on router }
in-interface=WG-R protocol=udp
add action=drop chain=input comment="Drop all else"
add action=fasttrack-connection chain=forward comment="defconf: fasttrack" \
connection-state=established,related hw-offload=yes
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 in-interface-list=LAN out-interface=WG-3P { allows users into tunnel }
add action=accept chain=forward comment="Allow Local Internet Traffic" \
in-interface-list=LAN out-interface-list=WAN
add action=accept chain=forward comment="Local Internet for wg-remote" \
in-interface=WG-R out-interface-list=WAN
add action=accept chain=forward comment="Admin to all VLANS" \
out-interface-list=LAN src-address-list=AllowWinBox
add action=accept chain=forward comment="WG/Admin to all VLANS" in-interface=\
WG-R out-interface-list=LAN
add action=accept chain=forward comment="Allow access to print from VLAN30" \ { common printer on vlan10 - 10.0.10.13 }
dst-address=10.0.10.13 src-address=10.0.30.0/24
add action=accept chain=forward comment="Allow all users to Adguard" \
dst-address=10.0.10.8 in-interface-list=LAN
add action=drop chain=forward comment="Drop all else"
/ip firewall mangle
add action=change-mss chain=forward new-mss=clamp-to-pmtu out-interface=\ { may not be required }
WG-3P passthrough=yes protocol=tcp tcp-flags=syn
/ip firewall nat
add action=masquerade chain=srcnat out-interface=WG-3P { All user traffic assigned client wg-r IP address }
add action=masquerade chain=srcnat comment="defconf: masquerade" \
ipsec-policy=out,none out-interface-list=WAN
add action=dst-nat chain=dstnat comment="Force User out Adguard tcp" \
dst-port=53 in-interface-list=LAN protocol=tcp src-address-list=!Excluded \
to-addresses=10.0.10.8
add action=dst-nat chain=dstnat comment="Force User out Adguard udp" \
dst-port=53 in-interface-list=LAN protocol=udp src-address-list=!Excluded \
to-addresses=10.0.10.8
/ip firewall service-port
set ftp disabled=yes
set tftp disabled=yes
set h323 disabled=yes
set sip disabled=yes
set pptp disabled=yes
/ip route
add disabled=no distance=1 dst-address=0.0.0.0/0 gateway=ISPgatewayIP \
pref-src="" routing-table=main scope=30 suppress-hw-offload=no \
target-scope=10
add disabled=no dst-address=0.0.0.0/0 gateway=WG-3P routing-table=\
use-WG suppress-hw-offload=no
/ip service
set telnet disabled=yes
set ftp disabled=yes
set www disabled=yes
set ssh disabled=yes
set api disabled=yes
set winbox port=XXXXXX
set api-ssl disabled=yes
/routing rule
add action=lookup-only-in-table disabled=no dst-address=10.0.0.0/16 src-address=10.0.0.0/16 table=main \ { Ensures any permitted local traffic between vlans 10,20,30,40 occurs vice being sent out tunnel }
add action=lookup-only-in-table comment="return traffic to wg-remote" disabled=no dst-address=192.168.0.0/24 src-address=10.0.0.0/16 table=main \ { Ensures any remote Admin wg-r traffic to Vlans is returned and does not go out tunnel }
add action=lookup disabled=no src-address=10.0.10.0/24 table=use-WG { action=lookup means router will go to table main in case use-WG is not available }
add action=lookup disabled=no src-address=10.0.20.0/24 table=use-WG
add action=lookup disabled=no src-address=10.0.30.0/24 table=use-WG
/system ntp client
set enabled=yes
/system ntp client servers
add address=ca.pool.ntp.org
/tool mac-server
set allowed-interface-list=none
/tool mac-server mac-winbox
set allowed-interface-list=MGMT
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SCENARIO-6:
In this scenario we have a REMOTE MT router with only a CGNAT type internet connection ( lets say Starlink ). Behind this router we will have two subnets, one for local internet and one that contains a control device of some sort which will be tunnel accessible. VLANs 10 and 20. The other MT device is local to the admin and is behind another Brand Puke Name Router but we can forward ports and it has a publicly accessible IP. Lets say the other MT device is a hex router. Here comes the tough part, the control device is accessed via an AP.
Thus users have to access the IP of the Public IP on Brand Name router on a specific port. This port will be key to port forwarding from the HEX router into the wireguard tunnel.
Enjoy...............