Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

networking: module for static names #584

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
./systemd
./services
./programs
./networking
];
}
7 changes: 7 additions & 0 deletions modules/common/networking/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
# SPDX-License-Identifier: Apache-2.0
{
imports = [
./hosts.nix
];
}
74 changes: 74 additions & 0 deletions modules/common/networking/hosts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
# SPDX-License-Identifier: Apache-2.0
{
config,
lib,
...
}: let
# please note that .100. network is not
# reachable from ghaf-host. It's only reachable
# guest-to-guest.
# Use to .101. (debug) to access guests from host.
# debug network hosts are post-fixed: <hostname>-debug
ipBase = "192.168.100";
debugBase = "192.168.101";
hostsEntries = [
{
ip = 1;
vilvo marked this conversation as resolved.
Show resolved Hide resolved
name = "net-vm";
}
{
ip = 2;
name = "ghaf-host";
}
{
ip = 3;
name = "gui-vm";
}
{
ip = 4;
name = "ids-vm";
}
{
ip = 5;
name = "audio-vm";
vilvo marked this conversation as resolved.
Show resolved Hide resolved
}
{
ip = 10;
name = "admin-vm";
}
{
ip = 100;
name = "chromium-vm";
}
{
ip = 101;
name = "gala-vm";
}
{
ip = 102;
name = "zathura-vm";
}
{
ip = 103;
name = "element-vm";
vilvo marked this conversation as resolved.
Show resolved Hide resolved
}
{
ip = 104;
name = "appflowy-vm";
}
];
mkHostEntry = {
ip,
name,
}:
"${ipBase}.${toString ip}\t${name}\n"
+ lib.optionalString config.ghaf.profiles.debug.enable
"${debugBase}.${toString ip}\t${name}-debug\n";
entries = map mkHostEntry hostsEntries;
in {
environment.etc.hosts = lib.mkForce {
text = lib.foldl' (acc: x: acc + x) "127.0.0.1 localhost\n" entries;
mode = "0444";
};
}
15 changes: 5 additions & 10 deletions modules/microvm/virtualization/microvm/adminvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

adminvmBaseConfiguration = {
imports = [
(import ./common/vm-networking.nix {inherit vmName macAddress;})
(import ./common/vm-networking.nix {
inherit config lib vmName macAddress;
internalIP = 10;
brianmcgillion marked this conversation as resolved.
Show resolved Hide resolved
})
({lib, ...}: {
ghaf = {
users.accounts.enable = lib.mkDefault configHost.ghaf.users.accounts.enable;
profiles.debug.enable = lib.mkDefault configHost.ghaf.profiles.debug.enable;
development = {
# NOTE: SSH port also becomes accessible on the network interface
# that has been passed through to VM
Expand Down Expand Up @@ -44,15 +48,6 @@
enable = true;
networks."10-ethint0" = {
matchConfig.MACAddress = macAddress;
addresses = [
{
addressConfig.Address = "192.168.100.10/24";
}
{
# IP-address for debugging subnet
addressConfig.Address = "192.168.101.10/24";
}
];
linkConfig.ActivationPolicy = "always-up";
};
};
Expand Down
4 changes: 3 additions & 1 deletion modules/microvm/virtualization/microvm/appvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
appvmConfiguration = {
imports = [
(import ./common/vm-networking.nix {
inherit vmName;
inherit config lib vmName;
inherit (vm) macAddress;
internalIP = index + 100;
})
({
lib,
Expand All @@ -48,6 +49,7 @@
in {
ghaf = {
users.accounts.enable = lib.mkDefault configHost.ghaf.users.accounts.enable;
profiles.debug.enable = lib.mkDefault configHost.ghaf.profiles.debug.enable;

development = {
ssh.daemon.enable = lib.mkDefault configHost.ghaf.development.ssh.daemon.enable;
Expand Down
5 changes: 4 additions & 1 deletion modules/microvm/virtualization/microvm/audiovm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
macAddress = "02:00:00:03:03:03";
audiovmBaseConfiguration = {
imports = [
(import ./common/vm-networking.nix {inherit vmName macAddress;})
(import ./common/vm-networking.nix {
inherit config lib vmName macAddress;
internalIP = 5;
})
({
lib,
pkgs,
Expand Down
29 changes: 23 additions & 6 deletions modules/microvm/virtualization/microvm/common/vm-networking.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
# SPDX-License-Identifier: Apache-2.0
{
config,
lib,
vmName,
macAddress,
internalIP,
gateway ? ["192.168.100.1"],
...
}: let
networkName = "ethint0";
Expand Down Expand Up @@ -35,12 +39,25 @@ in {
matchConfig.PermanentMACAddress = macAddress;
linkConfig.Name = networkName;
};
networks."10-${networkName}" = {
matchConfig.MACAddress = macAddress;
DHCP = "yes";
linkConfig.RequiredForOnline = "routable";
linkConfig.ActivationPolicy = "always-up";
};
networks."10-${networkName}" =
{
matchConfig.MACAddress = macAddress;
addresses =
[
{
addressConfig.Address = "192.168.100.${toString internalIP}/24";
}
]
++ lib.optionals config.ghaf.profiles.debug.enable [
{
# IP-address for debugging subnet
addressConfig.Address = "192.168.101.${toString internalIP}/24";
}
];
linkConfig.RequiredForOnline = "routable";
linkConfig.ActivationPolicy = "always-up";
}
// lib.optionalAttrs (gateway != []) {inherit gateway;};
};

# systemd-resolved does not support local names resolution
Expand Down
36 changes: 15 additions & 21 deletions modules/microvm/virtualization/microvm/guivm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
inherit (import ../../../../lib/launcher.nix {inherit pkgs lib;}) rmDesktopEntries;
guivmBaseConfiguration = {
imports = [
(import ./common/vm-networking.nix {inherit vmName macAddress;})
(import ./common/vm-networking.nix {
inherit config lib vmName macAddress;
internalIP = 3;
})
({
lib,
pkgs,
Expand Down Expand Up @@ -127,27 +130,18 @@
];

# Waypipe service runs in the GUIVM and listens for incoming connections from AppVMs
systemd = {
user.services.waypipe = {
enable = true;
description = "waypipe";
after = ["labwc.service"];
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.waypipe}/bin/waypipe --vsock -s ${toString cfg.waypipePort} client";
Restart = "always";
RestartSec = "1";
};
startLimitIntervalSec = 0;
wantedBy = ["ghaf-session.target"];
systemd.user.services.waypipe = {
enable = true;
description = "waypipe";
after = ["labwc.service"];
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.waypipe}/bin/waypipe --vsock -s ${toString cfg.waypipePort} client";
Restart = "always";
RestartSec = "1";
};

# Fixed IP-address for debugging subnet
network.networks."10-ethint0".addresses = [
{
addressConfig.Address = "192.168.101.3/24";
}
];
startLimitIntervalSec = 0;
wantedBy = ["ghaf-session.target"];
};
})
];
Expand Down
19 changes: 5 additions & 14 deletions modules/microvm/virtualization/microvm/idsvm/idsvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
configHost = config;
vmName = "ids-vm";
macAddress = "02:00:00:01:01:02";
networkName = "ethint0";
idsvmBaseConfiguration = {
imports = [
(import ../common/vm-networking.nix {inherit vmName macAddress;})
(import ../common/vm-networking.nix {
inherit config lib vmName macAddress;
internalIP = 4;
})
({lib, ...}: {
ghaf = {
users.accounts.enable = lib.mkDefault configHost.ghaf.users.accounts.enable;
profiles.debug.enable = lib.mkDefault configHost.ghaf.profiles.debug.enable;

virtualization.microvm.idsvm.mitmproxy.enable = configHost.ghaf.virtualization.microvm.idsvm.mitmproxy.enable;

Expand All @@ -41,18 +44,6 @@
]
++ (lib.optional configHost.ghaf.profiles.debug.enable pkgs.tcpdump);

systemd.network = {
networks."10-${networkName}" = {
gateway = ["192.168.100.1"];
addresses = [
{
# IP-address for debugging subnet
addressConfig.Address = "192.168.101.4/24";
}
];
};
};

microvm = {
optimize.enable = true;
shares = [
Expand Down
48 changes: 7 additions & 41 deletions modules/microvm/virtualization/microvm/netvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@

netvmBaseConfiguration = {
imports = [
(import ./common/vm-networking.nix {inherit vmName macAddress;})
(import ./common/vm-networking.nix {
inherit config lib vmName macAddress;
internalIP = 1;
gateway = [];
})
({lib, ...}: {
ghaf = {
users.accounts.enable = lib.mkDefault configHost.ghaf.users.accounts.enable;
profiles.debug.enable = lib.mkDefault configHost.ghaf.profiles.debug.enable;
development = {
# NOTE: SSH port also becomes accessible on the network interface
# that has been passed through to NetVM
Expand All @@ -34,6 +39,7 @@
enable = true;
withName = "netvm-systemd";
withPolkit = true;
withResolved = true;
withDebug = configHost.ghaf.profiles.debug.enable;
withHardenedConfigs = true;
};
Expand All @@ -54,46 +60,6 @@
# Add simple wi-fi connection helper
environment.systemPackages = lib.mkIf config.ghaf.profiles.debug.enable [pkgs.wifi-connector];

# Dnsmasq is used as a DHCP/DNS server inside the NetVM
services = {
dnsmasq = {
enable = true;
resolveLocalQueries = true;
settings = {
server = ["8.8.8.8"];
dhcp-range = ["192.168.100.2,192.168.100.254"];
dhcp-sequential-ip = true;
dhcp-authoritative = true;
domain = "ghaf";
listen-address = ["127.0.0.1,192.168.100.1"];
expand-hosts = true;
domain-needed = true;
bogus-priv = true;
};
};

# Disable resolved since we are using Dnsmasq
resolved.enable = false;
openssh = lib.mkIf isGuiVmEnabled configHost.ghaf.security.sshKeys.sshAuthorizedKeysCommand;
};

systemd.network = {
enable = true;
networks."10-ethint0" = {
matchConfig.MACAddress = macAddress;
addresses = [
{
addressConfig.Address = "192.168.100.1/24";
}
{
# IP-address for debugging subnet
addressConfig.Address = "192.168.101.1/24";
}
];
linkConfig.ActivationPolicy = "always-up";
};
};

microvm = {
optimize.enable = true;
hypervisor = "qemu";
Expand Down
8 changes: 4 additions & 4 deletions packages/mitmweb-ui/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
...
}: let
waypipePort = 1100; # TODO: remove hardcoded port number
idsvmIP = "192.168.100.4";
idsvmIP = "ids-vm";
mitmwebUI =
pkgs.writeShellScript
"mitmweb-ui"
''
# Create ssh-tunnel between chromium-vm and ids-vm
${pkgs.openssh}/bin/ssh -i /run/waypipe-ssh/id_ed25519 \
-o StrictHostKeyChecking=no \
-t ghaf@chromium-vm.ghaf \
-t ghaf@chromium-vm \
${pkgs.openssh}/bin/ssh -M -S /tmp/control_socket \
-f -N -L 8081:localhost:8081 ghaf@${idsvmIP}
# TODO: check pipe creation failures

# Launch chromium application and open mitmweb page
${pkgs.openssh}/bin/ssh -i /run/waypipe-ssh/id_ed25519 -o StrictHostKeyChecking=no chromium-vm.ghaf \
${pkgs.openssh}/bin/ssh -i /run/waypipe-ssh/id_ed25519 -o StrictHostKeyChecking=no chromium-vm \
${pkgs.waypipe}/bin/waypipe --border=#ff5733,5 --vsock -s ${toString waypipePort} server \
chromium --enable-features=UseOzonePlatform --ozone-platform=wayland \
http://localhost:8081

# Use the control socket to close the ssh tunnel between chromium-vm and ids-vm
${pkgs.openssh}/bin/ssh -i /run/waypipe-ssh/id_ed25519 \
-o StrictHostKeyChecking=no \
-t ghaf@chromium-vm.ghaf \
-t ghaf@chromium-vm \
${pkgs.openssh}/bin/ssh -q -S /tmp/control_socket -O exit ghaf@${idsvmIP}
'';
in
Expand Down
Loading