Site-to-Site Cloudflare Tunnel & WARP Setup on Raspberry Pi

Discuss Wi-Fi setups, cybersecurity, and network troubleshooting.
User avatar
ccb056
Site Administrator
Posts: 1018
Joined: January 14th, 2004, 11:36 pm
Location: Texas

Site-to-Site Cloudflare Tunnel & WARP Setup on Raspberry Pi

Post by ccb056 »

Architecture Overview 

Site A (VCT): Subnet 192.168.11.0/24, pfSense router (192.168.11.1), Raspberry Pi 5 gateway tock.local.vct (192.168.11.13, WARP IP 100.96.0.7) running cloudflared + warp-svc. 

Site B (MBS): Subnet 192.168.4.0/22, Eero router (192.168.4.1), Raspberry Pi 5 gateway pi.local.mbs (192.168.4.55, WARP IP 100.96.0.2) running cloudflared + warp-svc + dnsmasq (port 5354). 

Why Service Tokens (mdm.xml) Instead of Shared Mesh Profiles: Identical Linux nodes sharing a single WARP Include-mode profile will self-hijack their own local LAN subnet into CloudflareWARP. Enrolling each Pi with a unique Service Token assigns separate per-site Device Profiles in the Cloudflare One WebGUI so each gateway only tunnels the remote subnet. 

Traffic & DNS Flow: 

* Inbound to each LAN: Handled by cloudflared tunnels (userspace SNAT eliminates return-route requirements on destination routers).
* Outbound from VCT LAN to MBS: pfSense routes 192.168.4.0/22 and 100.96.0.0/12 to 192.168.11.13; tock.local.vct masquerades traffic onto CloudflareWARP via nftables.
* Cross-Site DNS: pi.local.mbs scans local mDNS and NetBIOS hostnames every minute into /etc/hosts and serves *.local.mbs via dnsmasq on 100.96.0.2:5354. pfSense forwards local.mbs queries to 100.96.0.2@5354.

Step 1: Cloudflare One WebGUI Setup 

1.1 Create Per-Site Service Tokens 

* Navigate to Zero Trust > Access controls > Service credentials.
* Create two non-expiring Service Tokens: gateway-tock-vct and gateway-pi-mbs. Save both Client IDs and Client Secrets.

1.2 Allow Service Token Device Enrollment 

* Navigate to Team & Resources > Devices > Device profiles > Management > Device enrollment permissions > Manage.
* Add a policy with Action: Service Auth.
* Under Include, select Service Token and add both gateway-tock-vct and gateway-pi-mbs. Save.

1.3 Configure Tunnel Private Network Routes 

* Navigate to Networking > Tunnels (or Networking > Routes).
* Ensure both tunnels are active in the default Virtual Network:
-- tock.local.vct tunnel: advertise 192.168.11.0/24
-- pi.local.mbs tunnel: advertise 192.168.4.0/22

1.4 Create Two Per-Site Device Profiles
Navigate to Team & Resources > Devices > Device profiles > General profiles, create two profiles, and drag both above any default profiles: 

Profile 1 — VCT Gateway (tock.local.vct): 

* Selector: Service Token is gateway-tock-vct
* Tunnel Protocol: MASQUE | Service Mode: Traffic and DNS mode
* Split Tunnels (Include): 192.168.4.0/22, 100.96.0.0/12 (Do NOT include 192.168.11.0/24)
* Local Domain Fallback: local.vct -> 192.168.11.1, local.mbs -> 192.168.11.1
* DNS Search Suffixes: local.vct, local.mbs

Profile 2 — MBS Gateway (pi.local.mbs): 

* Selector: Service Token is gateway-pi-mbs
* Tunnel Protocol: MASQUE | Service Mode: Traffic and DNS mode
* Split Tunnels (Include): 192.168.11.0/24, 100.96.0.0/12 (Do NOT include 192.168.4.0/22)
* Local Domain Fallback: local.vct -> 192.168.11.1, local.mbs -> 192.168.11.1
* DNS Search Suffixes: local.mbs, local.vct

Step 2: Enroll Both Raspberry Pis via mdm.xml 

Note: Linux warp-svc requires raw dict tags without an XML/plist wrapper header. 

On tock.local.vct (and repeat on pi.local.mbs with its respective token):

Code: Select all

sudo systemctl stop warp-svc
sudo rm -rf /var/lib/cloudflare-warp/* 

sudo tee /var/lib/cloudflare-warp/mdm.xml << 'EOF'
<dict>
<key>organization</key>
<string>YOUR_TEAM_NAME</string>
<key>auth_client_id</key>
<string>YOUR_CLIENT_ID.access</string>
<key>auth_client_secret</key>
<string>YOUR_CLIENT_SECRET</string>
<key>auto_connect</key>
<integer>1</integer>
</dict>
EOF 

sudo chmod 600 /var/lib/cloudflare-warp/mdm.xml
sudo systemctl start warp-svc
sleep 5
warp-cli status
warp-cli settings | grep -E "Auth Client ID|Profile ID|192.168"
 

Step 3: Configure LAN Forwarding & Masquerade on tock.local.vct 

3.1 Enable Persistent IPv4 Forwarding

Code: Select all

echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/99-ip-forward.conf
sudo sysctl --system
 

3.2 Enable Persistent nftables Source NAT on CloudflareWARP

Code: Select all

sudo tee /etc/nftables.conf << 'EOF'
#!/usr/sbin/nft -f
table ip nat {
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
ip saddr 192.168.11.0/24 oifname "CloudflareWARP" counter masquerade
}
}
EOF 

sudo systemctl enable --now nftables
 

Step 4: Configure Local DNS & Host Discovery on pi.local.mbs 

Because the remote Eero network does not run a custom DNS forwarder, pi.local.mbs scans local mDNS (.local) and NetBIOS/SMB devices every minute, writes them to /etc/hosts, and serves them via dnsmasq on port 5354. 

4.1 Install Packages & Configure dnsmasq

Code: Select all

sudo apt update && sudo apt install -y dnsmasq avahi-utils nbtscan 

sudo tee /etc/dnsmasq.d/mbs-local.conf << 'EOF'
port=5354
listen-address=100.96.0.2
bind-dynamic
no-resolv
domain-needed
bogus-priv
EOF 

sudo systemctl enable --now dnsmasq
 

4.2 Create the Auto-Discovery Script (/usr/local/bin/update-mbs-hosts.sh)

Code: Select all

sudo tee /usr/local/bin/update-mbs-hosts.sh << 'EOF'
#!/bin/bash
DOMAIN="local.mbs"
TMP_HOSTS=$(mktemp)
TMP_FOUND=$(mktemp) 

grep -v "DOMAIN" /etc/hosts > "TMP_HOSTS" 

#### 1. Discover mDNS (.local) hosts

avahi-browse -artp 2>/dev/null | awk -F';' -v dom="$DOMAIN" '
$1 == "=" && $3 == "IPv4" && 
7

 

/𝑙̇𝑜𝑐𝑎𝑙
⁠/ && 8 ~ /^192.168./ {
host = tolower($7)
sub(/.local$/, "", host)
gsub(/[^a-z0-9-]/, "-", host)
print $8 "\t" host "." dom
}
' >> "$TMP_FOUND" 

#### 2. Discover NetBIOS/SMB hosts

nbtscan -q -s : 192.168.4.0/22 2>/dev/null | awk -F':' -v dom="$DOMAIN" '
1 ~ /^192.168./ && $2 != "" {
gsub(/ /, "", $2)
host = tolower($2)
gsub(/[^a-z0-9-]/, "-", host)
print $1 "\t" host "." dom
}
' >> "$TMP_FOUND" 

sort -u "
𝑇𝑀𝑃𝐹𝑂𝑈𝑁𝐷

"

>>

"
⁠TMP_HOSTS"
mv "$TMP_HOSTS" /etc/hosts
chmod 644 /etc/hosts
rm -f "$TMP_FOUND"
systemctl reload dnsmasq 2>/dev/null || true
EOF 

sudo chmod +x /usr/local/bin/update-mbs-hosts.sh
sudo /usr/local/bin/update-mbs-hosts.sh
 

4.3 Schedule Minute-by-Minute Updates via Root Cron

Code: Select all

(sudo crontab -l 2>/dev/null; echo "* * * * * /usr/local/bin/update-mbs-hosts.sh") | sudo crontab -
 

Step 5: Configure pfSense Router at VCT (192.168.11.1) 

* Static Routes (System > Routing > Static Routes):
-- Destination 192.168.4.0/22 -> Gateway 192.168.11.13 (tock.local.vct)
-- Destination 100.96.0.0/12 -> Gateway 192.168.11.13 (tock.local.vct)
* DNS Resolver Domain Override (Services > DNS Resolver > Domain Overrides):
-- Domain: local.mbs -> IP Address: 100.96.0.2@5354
* DNS Resolver Outgoing Interfaces: Ensure LAN (or All) is selected so forwarded queries reach 192.168.11.13.

Step 6: Verification 

Code: Select all

 

#### From tock.local.vct or any 192.168.11.x LAN client:

ping -c 3 server.local.mbs
ping -c 3 server 

#### Confirm masquerade packet counter increments on tock.local.vct:

sudo nft list table ip nat