wireguard routing gondok

Sziasztok!

Van nekem egy VPN előfizetésem, amihez beállítottam wireguard klients Ubuntu Linux 22.04 gépen.

Nagyjából a következők szerint:

ip link add dev wgsurfsharkbud type wireguard
ip address add dev wgsurfsharkbud 10.14.0.2/16
wg setconf wgsurfsharkbud wireguard.conf
ip link set up wgsurfsharkbud

A wireguard config-ja ilyen egyszerű:

[Interface]
PrivateKey = Q*=
[Peer]
PublicKey = P*=
AllowedIPs = 0.0.0.0/0
Endpoint = hu-bud.prod.surfshark.com:51820

Ez majdnem az amit a szolgáltató (surfshark) adott, kivéve hogy kivettem belőle az Address és a DNS sorokat, mert azok csak window-os klienssel mennek.

Ezek után azt látom, hogy sikeresen csatlakozik:

# wg show
interface: wgsurfsharkbud
  public key: Y********=
  private key: (hidden)
  listening port: 5*****8

peer: P*****=
  endpoint: 146.70.120.27:51820
  allowed ips: 0.0.0.0/0
  latest handshake: 1 minute, 45 seconds ago
  transfer: 124 B received, 3.49 KiB sent

Természetesen még route-ok nincsenek rá, ezért ez így még nem használható. Az is világos, hogy a szogláltató 10.14.0.2 foglalt címet adott ,ezért ez bizonyára NAT-olva van.

Namost ha adok neki egy ilyen route-ot:

# host ifconfig.me
ifconfig.me has address 34.160.111.145
# ip route add 34.160.111.145/32 dev wgsurfsharkbud


Akkor azt látom, hogy működik, mert a 146.70.120.28 az nem az én címem, hanem a hu-bud.prod.surfshark.com (egyik) címe:

# curl ifconfig.me/all
ip_addr: 146.70.120.28
remote_host: unavailable
user_agent: curl/7.81.0
port: 41734
language: 
referer: 
connection: 
keep_alive: 
method: GET
encoding: 
mime: */*
charset: 
via: 1.1 google

Viszont ha default route-ot akarok hozzáadni, akkor minden elromlik, semmit nem tudok elérni.

Sőt, ha egy negyed netet adok hozzá route-nak:

ip route add 128.0.0.0/2 dev wgsurfsharkbud

Akkor is nagyjából minden elérhetetlenné válik. A fenti parancs után ilyen a route-om:

default via 192.168.14.254 dev enp4s0 proto dhcp metric 100 
10.14.0.0/16 dev wgsurfsharkbud proto kernel scope link src 10.14.0.2 
34.160.111.145 dev wgsurfsharkbud scope link 
128.0.0.0/2 dev wgsurfsharkbud scope link 
169.254.0.0/16 dev enp4s0 scope link metric 1000 
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown 
192.168.14.0/24 dev enp4s0 proto kernel scope link src 192.168.14.100 metric 100 

Mondjuk azt se értem, hogy a 34.160.111.145 hogy került oda (???).

Az a kérdés, hogy a default route, illetve akár egy 128.0.0.0/2 dst net route hozzáadása miért ront el mindent?

Mit nem látok?

Hozzászólások

Még annyit hozzáfűznék, hogy saját mikrotik vpn szerverekhez van egy csomó hasonló kapcsolatom, és mindegyik jól működik évek óta. De most először van olyan, hogy default route-nak akarom beállítani, és ez nem akar menni.

Közben rájöttem hogy a 34.160.111.145 honnan jött, azzal is próbáltam tesztelni, hogy csak egy címet, vagy kisebb címtartományokat adok hozzá route-hoz. Azt én írtam be kézzel, csak nem emlékeztem rá.

Ha átállítod a default gw-t és így a VPN fogadóhoz is VPN-en keresztül menne, az úgy nem az lesz, amit akarsz.

A hu-bud.prod.surfshark.com felé az eredeti gateway-t vedd fel, hogy oda azon menjen.

A routing tanácsot annyival kiegészíteném, hogy ha a wg-quick-kel húzod fel a konfigot (ez tud pl. systemd-s indítást is), akkor a PreUp és a PreDown utasításokkal felveheted ill. törölheted a route-t. A Torguard-ot én is így használom.

TheAdam

ha wireguard konfigban /0-t routoltatsz, akkor a wg-quick megoldja ezt roppant elegans modon egy uj routing tablaval es rpdb ruleokkal:

 

add_default() {
        local table line
        if ! get_fwmark table; then
                table=51820
                while [[ -n $(ip -4 route show table $table 2>/dev/null) || -n $(ip -6 route show table $table 2>/dev/null) ]]; do
                        ((table++))
                done
                cmd wg set "$INTERFACE" fwmark $table
        fi
        local proto=-4 iptables=iptables pf=ip
        [[ $1 == *:* ]] && proto=-6 iptables=ip6tables pf=ip6
        cmd ip $proto route add "$1" dev "$INTERFACE" table $table
        cmd ip $proto rule add not fwmark $table table $table
        cmd ip $proto rule add table main suppress_prefixlength 0

        local marker="-m comment --comment \"wg-quick(8) rule for $INTERFACE\"" restore=$'*raw\n' nftable="wg-quick-$INTERFACE" nftcmd 
        printf -v nftcmd '%sadd table %s %s\n' "$nftcmd" "$pf" "$nftable"
        printf -v nftcmd '%sadd chain %s %s preraw { type filter hook prerouting priority -300; }\n' "$nftcmd" "$pf" "$nftable"
        printf -v nftcmd '%sadd chain %s %s premangle { type filter hook prerouting priority -150; }\n' "$nftcmd" "$pf" "$nftable"
        printf -v nftcmd '%sadd chain %s %s postmangle { type filter hook postrouting priority -150; }\n' "$nftcmd" "$pf" "$nftable"
        while read -r line; do
                [[ $line =~ .*inet6?\ ([0-9a-f:.]+)/[0-9]+.* ]] || continue
                printf -v restore '%s-I PREROUTING ! -i %s -d %s -m addrtype ! --src-type LOCAL -j DROP %s\n' "$restore" "$INTERFACE" "${BASH_REMATCH[1]}" "$marker"
                printf -v nftcmd '%sadd rule %s %s preraw iifname != "%s" %s daddr %s fib saddr type != local drop\n' "$nftcmd" "$pf" "$nftable" "$INTERFACE" "$pf" "${BASH_REMATCH[1]}"
        done < <(ip -o $proto addr show dev "$INTERFACE" 2>/dev/null)
        printf -v restore '%sCOMMIT\n*mangle\n-I POSTROUTING -m mark --mark %d -p udp -j CONNMARK --save-mark %s\n-I PREROUTING -p udp -j CONNMARK --restore-mark %s\nCOMMIT\n' "$restore" $table "$marker" "$marker"
        printf -v nftcmd '%sadd rule %s %s postmangle meta l4proto udp mark %d ct mark set mark \n' "$nftcmd" "$pf" "$nftable" $table
        printf -v nftcmd '%sadd rule %s %s premangle meta l4proto udp meta mark set ct mark \n' "$nftcmd" "$pf" "$nftable"
        [[ $proto == -4 ]] && cmd sysctl -q net.ipv4.conf.all.src_valid_mark=1
        if type -p nft >/dev/null; then
                cmd nft -f <(echo -n "$nftcmd")
        else
                echo -n "$restore" | cmd $iptables-restore -n
        fi
        HAVE_SET_FIREWALL=1
        return 0
}