Skip to content

Mikrotik

5 posts with the tag “Mikrotik”

Home Network Architecture with RouterOS, OpenWrt, SmartDNS, and FRP

Home Network Architecture with RouterOS, OpenWrt, SmartDNS, and FRP

This article records a practical home network architecture built around MikroTik RouterOS, an OpenWrt bypass router, SmartDNS, Tailscale, and FRP. It is not a copy-and-paste configuration guide. It is more about ownership: which device should own routing, DNS, proxy, IPv6, public services, and recovery.

DNS-Distribution-with-SmartDNS

Mikrotik’s DNS always a confused problem. We are going to introduce SmartDNS to overcome it.

A previous post expalined how to do this.

In this section, we are going to demonstrate the DNS group and flow.

DNS records will be separate to 3 groups:

  • China List
    • resolve with public DNS server configured CHINA group in SmartDNS config file
  • GFW List
    • resolve with foreign DNS server configured GFW group in SmartDNS config file
  • Others
    • resolve with mixed DNS server configured OverSea group in SmartDNS config file
server-name SmartDNS
bind :53 -force-https-soa
cache-size 131072
cache-persist yes
cache-file /tmp/smartdns.cache
cache-checkpoint-time 120
prefetch-domain yes
serve-expired yes
serve-expired-ttl 0
serve-expired-reply-ttl 10
serve-expired-prefetch-time 86400
force-qtype-SOA 65
dualstack-ip-selection-threshold 10
dualstack-ip-allow-force-AAAA no
dualstack-ip-selection yes
rr-ttl 60
rr-ttl-min 1
rr-ttl-max 60
max-reply-ip-num 6
max-query-limit 0
response-mode fastest-response
log-level notice
log-file /var/log/smartdns/smartdns.log
log-size 128k
log-num 2
log-file-mode 644
audit-enable yes
audit-file /var/log/smartdns/smartdns-audit.log
audit-size 128k
audit-num 2
<!-- proxy-server socks5://127.0.0.1:1080 -name Proxy -->
server-https https://1.1.1.1/dns-query -group GFW -exclude-default-group -proxy Proxy -http-host one.one.one.one
server-tls 223.5.5.5:853 -group China -exclude-default-group
server-tls 8.8.8.8:853 -group OverSea
expand-ptr-from-address yes
mdns-lookup yes
speed-check-mode ping,tcp:80,tcp:443
domain-set -name chinalist -file /etc/smartdns/chinalist.conf
domain-set -name gfwlist -file /etc/smartdns/gfwlist.conf
nameserver /domain-set:chinalist/China
nameserver /domain-set:gfwlist/GFW
domain-rules /domain-set:gfwlist/ -speed-check-mode none

[1]. https://www.right.com.cn/forum/thread-8301362-1-1.html

[2]. IP-Address-List-Distribution-Based-on-RouterOS-and-Openwrt

Container on Mikrotik

From Mikrotik ROS 7.4 Beta 4, Mikrotik add new feature of Container.

Configurations are based on Mikrotik RouterOS 7.13.1 .

Login Terminal in RouterOS, with following commands:

/system/device-mode/update container=yes

You will need to confirm the device-mode with a press of the reset button, or a cold reboot, if using container on X86.

(1). Add bridge veth interface for the container:

Section titled “(1). Add bridge veth interface for the container:”
/interface/veth/add name=veth1 address=172.17.0.2/24 gateway=172.17.0.1

(2). Create a bridge for containers and add veth to it:

Section titled “(2). Create a bridge for containers and add veth to it:”
/interface/bridge/add name=containers
/ip/address/add address=172.17.0.1/24 interface=containers
/interface/bridge/port add bridge=containers interface=veth1
/ip/firewall/nat/add chain=srcnat action=masquerade src-address=172.17.0.0/24

(1). Add host veth interface for the container:

Section titled “(1). Add host veth interface for the container:”
/interface/veth/add name=veth1 address=LAN_ADDRESS gateway=GATE_WAY_OF_LAN
/interface/bridge/port add bridge=LAN_BRIDGE interface=veth1
/container/mounts/add name=smartdns src=/smartdns.conf dst=/etc/smartdns/smartdns.conf
/container/config/set registry-url=https://registry-1.docker.io
/container/add remote-image=dashdreams/smartdns:latest interface=veth1 \
root-dir=/smartdns hostname=smartdns mounts=smartdns workdir=/root
/container/add file=smartdns.tar interface=veth1 root-dir=/smartdns \
hostname=smartdns mounts=smartdns workdir=/root
/container/start 0

6. Forward ports to internal container (optional)

Section titled “6. Forward ports to internal container (optional)”

Ports can be forwarded using dst-nat (where 192.168.88.1 routers IP address):

/ip firewall nat
add action=dst-nat chain=dstnat dst-address=192.168.88.1 dst-port=5353 protocol=tcp to-addresses=172.17.0.2 to-ports=53

How to get a local container image:

docker pull dashdreams/smartdns:latest@sha256:XXXXXXXXXXXXXXXXXXXXXXX
docker save dashdreams/smartdns > smartdns.tar

[1]. https://help.mikrotik.com/docs/display/ROS/Container

[2]. http://www.irouteros.com/?p=2712

[3]. Build-Container-Image-with-Podman

IP Address List Distribution Based on RouterOS and Openwrt

IP address list distribution with CNIP.

Configurations are based on Mikrotik RouterOS 7.6 .

A bypass routre with Openwrt required.

CN IP address list can be found from

Clang.CN

IPIP

RealIPDatabase

It’s possible to get CN IP list by running scripts below on linux:

curl 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'|\
grep ipv6 | grep CN | awk -F\| \
'{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' | \
sed -e 's/^/add address=/g' -e 's/$/ list=CNIP/g'|\
sed -e $'1i\\\n/ipv6 firewall address-list' \
-e $'1i\\\nremove [/ipv6 firewall address-list find list=CNIP]'|\
sed '$a /file remove CNIPv6.rsc'>CNIPv6.rsc
curl 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'|\
grep ipv4 | grep CN | awk -F\| \
'{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' |\
sed -e 's/^/add address=/g' -e 's/$/ list=CNIP/g' \
-e 's/$/ comment="CNIP"/g' |\
sed -e $'1i\\\n/ip firewall address-list' \
-e $'1i\\\nremove [/ip firewall address-list find list=CNIP]' |\
sed '$a \/' |sed '$a /file remove CNIPv4.rsc'>CNIPv4.rsc

Contains both IPv6 and IPv4 addresses.

The address list may look like this:

/ip firewall address-list
add list=CNIP address=x.x.x.x/xx
add list=CNIP address=x.x.x.x/xx
...
...
...
add list=CNIP address=x.x.x.x/xx

Open WinBox, and upload CNIPv4.rsc to RouterOS, than open Terminal, and run import CNIPv4.rsc.

In Terminal run command: /routing/tables/add name=OP fib

Add Proxy List in Firewall:

/ip/firewall/address-list/add name=proxy address=192.168.1.1-192.168.1.200

We take IP address range 192.168.1.1–192.168.1.200 to proxy list.

Mark Non-CN IP destnation address

add action=mark-routing chain=prerouting comment="ProxyRoute" \
src-address=proxy dst-address=!CNIP dst-address-type=!local \
new-routing-mark=OP passthrough=yes

Add another rule to accept openwrt’s flow

add action=accept chain=prerouting comment="BypassOP" \
src-address=192.168.1.254

Assume openwrt’s local ip is 192.168.1.254

Config route table to openwrt:

/ip/route/add dst-address=0.0.0.0 gateway=192.168.1.254 \
check-getway=ping routing-table=OP distance=1 \
scope=30 target-scope=10

Change PPPOE’s distance to 2:

/interface/pppoe-client/set pppoe-CMCC default-route-distance=2

4. Change Distribution if Bypass Router Down

Section titled “4. Change Distribution if Bypass Router Down”
  • Shutdown distribution when openwrt down
  • Turn distribution when openwrt up
/tool/netwatch/add host=192.168.1.254 type=simple interval=60 \
up-script=OPUP.rsc down-script=OPDOWN.rsc

Up Script:

add dont-require-permissions=yes name=OPUP.rsc owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="/\
ip firewall mangle enable number=???\r\
\n/ip firewall mangle enable number=???\r\
\n/ip route enable number=0\r\
\n/ip/dns/cache/flush"

Down Script:

add dont-require-permissions=yes name=OPDOWN.rsc owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="/\
ip firewall mangle disable number=???\r\
\n/ip firewall mangle disable number=???\r\
\n/ip route disable number=0\r\
\n/ip/dns/cache/flush"

??? is index number of mangle rules set in MangleRules

[1]. https://www.shawnleetttt.cyou/posts/71e7c44b/

[2]. https://idndx.com/high-availability-when-split-routing-using-routeros-and-raspberrypi/

[3]. DNS-Distribution-with-SmartDNS

[4]. IPv6-Configure-on-Mikrotik-RouterOS-v7.x

IPv6 Configure on Mikrotik RouterOS v7.x

IPv6 configuration on Mikrotik RouterOS is much more complicated than Merlin on ASUS. We are going to config IPv6 in this aritical.

Configurations are based on Mikrotik RouterOS 7.6 .

Login Terminal in RouterOS, with following commands:

# Accept ISP RA, Get IPv6 Address with SLAAC
/ipv6 settings set accept-router-advertisements=yes
# Configure DHCPv6 client:
# - Add default route: yes
# - WAN interface: pppoe-ether1
# - IPv6 address pool: IPv6 (You can change to whatever you want)
# - Prefix length for DHCPv6 server's offering cascading routers: 60
# - Request LAN prefix from ISP
/ipv6 dhcp-client add add-default-route=no interface=pppoe-ether1 \
pool-name=IPv6 pool-prefix-length=60 \
request=prefix

CMCC in Nanjing, Jiangsu provide /60 prefix, if prefix length less than /63, it’s impossible to use DHCPv6.

# Accept DHCPv6 client prefix delegation
/ipv6 firewall filter add action=accept chain=input \
comment="Accept DHCPv6 client prefix delegation" \
dst-port=546 protocol=udp src-address=fe80::/10

This rule is default, if you configured your router with default script.

/ipv6 address add address=::1 from-pool=IPv6 interface=bridge
# Delegate IPv6 addresses from pool `delegation`
# Lease is valid for 1 day
/ipv6 dhcp-server add address-pool=IPv6 \
interface=bridge lease-time=7d name=Home-IPv6

[1]. https://wu.renjie.im/blog/network/ros-dhcpv6/zh-cn/