Skip to content

Commit

Permalink
Added a way to enable host-only networking through tart using --net-h…
Browse files Browse the repository at this point in the history
…ost (#32)

* Added a way to enable host-only networking through tart using SOFTNET_NET_TYPE=host

* Removed env variable and moved to Enum instead of str

* Fixed defaults & restricted publicity of host

* Fixed usage of NetType
  • Loading branch information
sparshev authored Mar 1, 2024
1 parent f5a1b1c commit 0a92c29
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
22 changes: 19 additions & 3 deletions lib/host.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use clap::ArgEnum;
use anyhow::{anyhow, Context, Result};
use std::net::Ipv4Addr;
use std::os::unix::io::{AsRawFd, RawFd};
Expand All @@ -8,6 +9,18 @@ use vmnet::mode::Mode;
use vmnet::parameters::{Parameter, ParameterKind};
use vmnet::{Events, Options};

#[derive(ArgEnum, Clone, Debug)]
pub enum NetType {
/// Shared network
///
/// Uses NAT-translation to give guests access to the global network
Nat,
/// Host network
///
/// Guests will be able to talk only to the host without access to global network
Host,
}

pub struct Host {
interface: vmnet::Interface,
new_packets_rx: UnixDatagram,
Expand All @@ -18,10 +31,13 @@ pub struct Host {
}

impl Host {
pub fn new() -> Result<Host> {
// Initialize a vmnet.framework NAT interface with isolation enabled
pub fn new(vm_net_type: NetType) -> Result<Host> {
// Initialize a vmnet.framework NAT or Host interface with isolation enabled
let mut interface = vmnet::Interface::new(
Mode::Shared(Default::default()),
match vm_net_type {
NetType::Nat => Mode::Shared(Default::default()),
NetType::Host => Mode::Host(Default::default()),
},
Options {
enable_isolation: Some(true),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod dhcp_snooper;
mod host;
pub use host::NetType;
mod poller;
pub mod proxy;
mod vm;
5 changes: 3 additions & 2 deletions lib/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod vm;

use crate::dhcp_snooper::DhcpSnooper;
use crate::host::Host;
use crate::host::NetType;
use crate::poller::Poller;
use crate::vm::VM;
use anyhow::Result;
Expand All @@ -22,9 +23,9 @@ pub struct Proxy {
}

impl Proxy {
pub fn new(vm_fd: RawFd, vm_mac_address: MacAddress) -> Result<Proxy> {
pub fn new(vm_fd: RawFd, vm_mac_address: MacAddress, vm_net_type: NetType) -> Result<Proxy> {
let vm = VM::new(vm_fd)?;
let host = Host::new()?;
let host = Host::new(vm_net_type)?;
let poller = Poller::new(vm.as_raw_fd(), host.as_raw_fd())?;

Ok(Proxy {
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{anyhow, Context};
use clap::Parser;
use nix::sys::signal::{signal, SigHandler, Signal};
use privdrop::PrivDrop;
use softnet::NetType;
use softnet::proxy::Proxy;
use std::borrow::Cow;
use std::env;
Expand All @@ -28,6 +29,9 @@ struct Args {
#[clap(long, help = "MAC address to enforce for the VM")]
vm_mac_address: mac_address::MacAddress,

#[clap(long, arg_enum, help = "type of network to use for the VM", default_value_t=NetType::Nat)]
vm_net_type: NetType,

#[clap(
long,
help = "set bootpd(8) lease time to this value (in seconds) before starting the VM",
Expand Down Expand Up @@ -140,7 +144,7 @@ fn try_main() -> anyhow::Result<()> {
set_bootpd_lease_time(args.bootpd_lease_time);

// Initialize the proxy while still having the root privileges
let mut proxy = Proxy::new(args.vm_fd as RawFd, args.vm_mac_address)
let mut proxy = Proxy::new(args.vm_fd as RawFd, args.vm_mac_address, args.vm_net_type)
.context("failed to initialize proxy")?;

// Drop effective privileges to the user
Expand Down

0 comments on commit 0a92c29

Please sign in to comment.