Skip to content

Commit

Permalink
Merge pull request #246 from firezone/5/ipv6
Browse files Browse the repository at this point in the history
Enable IPv6 support for WireGuard tunnels
  • Loading branch information
jamilbk committed Sep 23, 2021
2 parents 2f74a96 + 0216951 commit ba83630
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# We're running on a self-hosted runner, so only allow one workflow to run at a
# time.
# XXX: Remove this when self-hosted ephemeral runners are implmented.
concurrency: ci

name: CI
on:
- push
Expand Down
7 changes: 6 additions & 1 deletion apps/fz_http/lib/fz_http/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule FzHttp.Devices do
alias FzHttp.{Devices.Device, Repo, Users.User}

@ipv4_prefix "10.3.2."
@ipv6_prefix "fd00:3:2::"

def list_devices do
Repo.all(Device)
Expand Down Expand Up @@ -49,11 +50,15 @@ defmodule FzHttp.Devices do
@ipv4_prefix <> Integer.to_string(device.address)
end

def ipv6_address(%Device{} = device) do
@ipv6_prefix <> Integer.to_string(device.address)
end

def to_peer_list do
for device <- Repo.all(Device) do
%{
public_key: device.public_key,
allowed_ips: ipv4_address(device)
allowed_ips: "#{ipv4_address(device)}/32, #{ipv6_address(device)}/128"
}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<td>
<%= live_patch(device.name, to: Routes.device_show_path(@socket, :show, device)) %>
</td>
<td class="code"><%= FzHttp.Devices.ipv4_address(device) %></td>
<td class="code"><%= FzHttp.Devices.ipv4_address(device) %>, <%= FzHttp.Devices.ipv6_address(device) %></td>
<td class="code"><%= device.public_key %></td>
</tr>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule FzHttpWeb.DeviceLive.Index do
{:ok, device} ->
@events_module.device_created(
device.public_key,
Devices.ipv4_address(device)
{Devices.ipv4_address(device), Devices.ipv6_address(device)}
)

{:noreply,
Expand Down
4 changes: 2 additions & 2 deletions apps/fz_http/lib/fz_http_web/live/device_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<dt>
<strong>Interface IP:</strong>
</dt>
<dd><%= FzHttp.Devices.ipv4_address(@device) %></dd>
<dd><%= FzHttp.Devices.ipv4_address(@device) %>, <%= FzHttp.Devices.ipv6_address(@device) %></dd>

<dt>
<strong>Public key:</strong>
Expand Down Expand Up @@ -72,7 +72,7 @@
<pre><code id="wg-conf">
[Interface]
PrivateKey = <%= @device.private_key %>
Address = <%= FzHttp.Devices.ipv4_address(@device) %>
Address = <%= FzHttp.Devices.ipv4_address(@device) %>/32, <%= FzHttp.Devices.ipv6_address(@device) %>/128
DNS = 1.1.1.1, 1.0.0.1

[Peer]
Expand Down
2 changes: 1 addition & 1 deletion apps/fz_http/test/fz_http/devices_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ defmodule FzHttp.DevicesTest do
test "renders all peers", %{device: device} do
assert Devices.to_peer_list() |> List.first() == %{
public_key: device.public_key,
allowed_ips: Devices.ipv4_address(device)
allowed_ips: "#{Devices.ipv4_address(device)}/32, #{Devices.ipv6_address(device)}/128"
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions apps/fz_vpn/lib/fz_vpn/cli/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ defmodule FzVpn.CLI.Live do
{privkey, pubkey(privkey)}
end

def add_peer(pubkey, ip) do
set("peer #{pubkey} allowed-ips #{ip}")
def add_peer(pubkey, {ipv4, ipv6}) do
set("peer #{pubkey} allowed-ips #{ipv4}/32,#{ipv6}/128")
end

def delete_peer(pubkey) do
Expand Down
24 changes: 16 additions & 8 deletions omnibus/cookbooks/firezone/recipes/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@
end
end

execute 'setup_wireguard_ip' do
# XXX: Make this configurable
if_addr = '10.3.2.1/24'
command "ip address replace #{if_addr} dev #{wg_interface}"
execute 'wireguard_ipv4' do
addr = '10.3.2.1/24'
command "ip address replace #{addr} dev #{wg_interface}"
end

execute 'wireguard_ipv6' do
addr = 'fd00:3:2::1/120'
command "ip -6 address replace #{addr} dev #{wg_interface}"
end

execute 'set_wireguard_interface_private_key' do
Expand All @@ -59,15 +63,19 @@
command "#{wg_path} set #{wg_interface} listen-port #{listen_port}"
end

execute 'set_mtu' do
command "ip link set mtu 1420 up dev #{wg_interface}"
end

route '10.3.2.0/24' do
# XXX: Make this configurable
device wg_interface
end

route 'fd00:3:2::0/120' do
device wg_interface
end

execute 'set_mtu' do
command "ip link set mtu 1420 up dev #{wg_interface}"
end

replace_or_add "IPv4 packet forwarding" do
path "/etc/sysctl.conf"
pattern "^#net.ipv4.ip_forward=1"
Expand Down

0 comments on commit ba83630

Please sign in to comment.