Wireguard ipv6 GUA

De Marmits Wiki

Configurer WireGuard avec une IPv6 unicast globale.
Un simple tunnel VPN à double pile avec prise en charge IPv6 globale.
WireGuard devient populaire auprès des utilisateurs de Linux en tant que VPN car il est intégré au noyau Linux, ce qui offre de meilleures performances. Cependant, de nombreux tutoriels et scripts qui configurent WireGuard le font avec NATing de l'adresse IPv6 et donnent à l'utilisateur une adresse IPv6 locale de lien. Cela amène le système d'exploitation à préférer IPv4 à IPv6.

⚠️ Wireguard IPV6 sur Raspberry Pi OS (bookworm) derrière une Freebox et un routeur OpenWRT

Ajout d'un sous-réseau avec /80.
Possiblité d'utiliser des sous-réseaux plus petits avec un sous-réseau approprié.

Original Block - 2000:xxx:xxx:x11:328e::/80
For the VPN Endpoint - 2000:xxx:xxx:x11:328e::123/128
For the VPN Clients - 2000:xxx:xxx:x11:328e:ffff::/96
VPN Endpoint port - 51820
pihole network interface - eth0
Gateway 2000:xxx:xxx:x11::1

Nous donnons également une seule adresse IPv6 pour chaque client, mais vous pouvez également attribuer un sous-réseau par client.
Pour une configuration IPv6 vraiment appropriée, vous pouvez attribuer à chaque client un sous-réseau /64 et configurer SLAAC.

Ajouter les IPv6 sur eth0

Avec NetworkManager

sudo nmtui

CONFIGURATION IPv6
Ajouter les adresses:

2000:xxx:xxx:x11:328e::123/128
2000:xxx:xxx:x11:328e:ffff::/96

Redémarrer le service:

sudo service NetworkManager restart

Activer l'IP forwarding

Assurez-vous d'ajouter les éléments suivants à /etc/sysctl.d/99-pivpn.conf
(Permet à l'interface d'accepter les messages IPv6 RA quand la transmission est permise)

net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
net.ipv6.conf.ens5.accept_ra = 2

puis, pour appliquer les modifications: sudo sysctl --system

Configuration de l'interface wg0

  • Dans les règles IPv4, nous effectuons un NAT pour l'adresse IPv4.
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE;
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE;
  • Dans les règles IPv6, nous ajoutons une règle pour transférer le trafic vers l'interface Internet.
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE;ip6tables -A FORWARD -i eth0 -o wg0 -j ACCEPT; ip6tables -A FORWARD -i wg0 -j ACCEPT;
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE;ip6tables -D FORWARD -i eth0 -o wg0 -j ACCEPT; ip6tables -D FORWARD -i wg0 -j ACCEPT;
  • Version nftables avant l'ipv6
PostUp = nft add table ip wireguard;nft add chain ip wireguard wireguard_chain {type nat hook postrouting priority srcnat\; policy accept\;};nft add rule ip wireguard wireguard_chain counter packets 0 bytes 0 masquerade;nft add table ip6 wireguard;nft add chain ip6 wireguard wireguard_chain {type nat hook postrouting priority srcnat\; policy accept\;};nft add rule ip6 wireguard wireguard_chain counter packets 0 bytes 0 masquerade;
PostDown = nft delete table ip wireguard; nft delete table ip6 wireguard
  • Version nftables avec l'ipv6
PostUp = nft add table ip wireguard;nft add chain ip wireguard wireguard_chain {type nat hook postrouting priority srcnat\; policy accept\;};nft add rule ip wireguard wireguard_chain counter packets 0 bytes 0 masquerade; nft add table ip6 wireguard;nft add chain ip6 wireguard wireguard_chain {type nat hook postrouting priority srcnat\; policy accept\;};nft add rule ip6 wireguard wireguard_chain counter packets 0 bytes 0 masquerade;nft add chain ip6 wireguard FORWARD { type filter hook forward priority filter\; };nft add rule ip6 wireguard FORWARD iifname "eth0" oifname "wg0" ct state related,established counter accept;nft add rule ip6 wireguard FORWARD iifname "wg0" counter accept;
PostDown = nft delete table ip wireguard; nft delete table ip6 wireguard

Pour l'IP client, nous utilisons les premières IP de la plage désignée. Dans IPv6, vous pouvez autoriser un grand sous-réseau ici et cela permettra au client de se déplacer dans différentes IP du sous-réseau.

<ode>AllowedIPs = 10.66.66.2, 2000:xxx:xxx:x11:328e:ffff::2/128

Donc dans /etc/wireguard/wg0.conf

[Interface]
Address = 10.66.66.1/24, fd42:42:42::1/64
ListenPort = 51820
PrivateKey =
PostUp = nft add table ip wireguard;nft add chain ip wireguard wireguard_chain {type nat hook postrouting priority srcnat\; policy accept\;};nft add rule ip wireguard wireguard_chain counter pac>
PostDown = nft delete table ip wireguard; nft delete table ip6 wireguard
...
### Client 1
[Peer]
PublicKey =
#PresharedKey =
AllowedIPs = 10.66.66.2, 2000:xxx:xxx:x11:328e:ffff::2/128

Configuration du client

Ici, nous autorisons une seule adresse IPv6. Si la configuration du serveur autorise un grand sous-réseau, nous pouvons la modifier en conséquence.

Adresse = 10.66.66.2, 2000:xxx:xxx:x11:328e:ffff::2/128

Configuration complète. Enregistrée dans /etc/wireguard/client1.conf

[Interface]
PrivateKey =
Address = 10.66.66.2, 2000:xxx:xxx:x11:328e:ffff::2/128
DNS = 2606:4700:4700::1001
[Peer]
PublicKey =
#PresharedKey =
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = [2000:xxx:xxx:xxx:x11::123]:51820

Redémarrer le tunel

sudo wg-quick down wg0
sudo wg-quick up wg0

Notes:

  • Traduire les régles iptables en nftables
ip6tables-translate -A FORWARD -i eth0 -o wg0 -j ACCEPT
ip6tables-translate -A FORWARD -i wg0 -j ACCEPT
=>
nft add rule ip6 wireguard wireguard_forward iifname eth0 oifname wg0 counter accept;
nft add rule ip6 filter chain forward iifname "eth0" oifname "wg0" counter accept; 
nft add chain ip6 wireguard wireguard_chain {forward iifname "wg0" counter accept\;};


Adapter et tester les règles

sudo nft add chain ip6 wireguard FORWARD { type filter hook forward priority filter\; }
sudo nft add rule ip6 wireguard FORWARD iifname "eth0" oifname "wg0" ct state related,established counter accept;
sudo nft add rule ip6 wireguard FORWARD iifname "wg0" counter accept
  • Pour effacer les règles:
sudo wg-quick down wg0
sudo wg-quick up wg0
  • Pour contrôler les règles:
sudo nft list ruleset
  • Résultat
table inet filter {
    chain input {
        type filter hook input priority filter; policy accept;
    }

    chain forward {
        type filter hook forward priority filter; policy accept;
    }

    chain output {
        type filter hook output priority filter; policy accept;
    }
}
table ip wireguard {
    chain wireguard_chain {
        type nat hook postrouting priority srcnat; policy accept;
        counter packets 4640 bytes 317839 masquerade
    }
}
table ip6 wireguard {
    chain wireguard_chain {
        type nat hook postrouting priority srcnat; policy accept;
        counter packets 1350 bytes 145915 masquerade
    }

    chain FORWARD {
        type filter hook forward priority filter; policy accept;
        iifname "eth0" oifname "wg0" ct state established,related counter packets 13820 bytes 9978553 accept
        iifname "wg0" counter packets 10031 bytes 3057534 accept
    }
}

Freebox

Dans la configuration IPv6 du serveur freebox

  • Activer le firewall IPv6
  • ⚠️ Décocher, Firewall sur les préfixes secondaires.

Routeur Firewall


Source: https://blog.miyuru.lk/setup-wireguard-with-global-ipv6/