Skip to content

Commit

Permalink
Fix network with rollback ebtables & fix lint in nightly test (#54)
Browse files Browse the repository at this point in the history
* try fix net

Signed-off-by: andrewmatilde <[email protected]>

* try fix net

Signed-off-by: andrewmatilde <[email protected]>

* try fix net

Signed-off-by: andrewmatilde <[email protected]>

* fix lint

Signed-off-by: andrewmatilde <[email protected]>

* update version to 0.5.2

Signed-off-by: andrewmatilde <[email protected]>

* update version to 0.5.2

Signed-off-by: andrewmatilde <[email protected]>

* rollback ip_s mac_s changes

Signed-off-by: Andrewmatilde <[email protected]>

* fix wired lint

Signed-off-by: Andrewmatilde <[email protected]>
  • Loading branch information
Andrewmatilde authored Jul 15, 2022
1 parent 00c83a0 commit 014d16a
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Andrewmatilde <[email protected]>", "Hexilee <[email protected]>"]
edition = "2018"
name = "chaos-tproxy"
version = "0.5.1"
version = "0.5.2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion chaos-tproxy-controller/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chaos-tproxy-controller"
version = "0.5.1"
version = "0.5.2"
edition = "2018"

[[bin]]
Expand Down
3 changes: 2 additions & 1 deletion chaos-tproxy-controller/src/cmd/interactive/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::convert::TryInto;
use std::future::Future;
use std::ops::DerefMut;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -131,6 +132,6 @@ impl Service<Request<Body>> for ConfigService {
#[inline]
fn call(&mut self, request: Request<Body>) -> Self::Future {
let handler = self.0.clone();
Box::pin(async move { Self::handle(&mut *handler.lock().await, request).await })
Box::pin(async move { Self::handle(handler.lock().await.deref_mut(), request).await })
}
}
7 changes: 7 additions & 0 deletions chaos-tproxy-controller/src/proxy/net/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rtnetlink::packet::RouteMessage;
use rtnetlink::Handle;
use uuid::Uuid;

use crate::proxy::net::iptables::clear_ebtables;
use crate::proxy::net::routes::{del_routes_noblock, get_routes_noblock, load_routes};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -126,6 +127,7 @@ impl NetEnv {

let cmdvv = vec![
ip_address("add", &self.ip, &self.veth4),
arp_set(&gateway_ip_s, &gateway_mac_s, &self.veth1),
arp_set(&gateway_ip_s, &gateway_mac_s, &self.veth4),
ip_netns(
&self.netns,
Expand Down Expand Up @@ -235,6 +237,7 @@ impl NetEnv {
ip_link_del_bridge(&self.bridge1),
ip_address("add", &self.ip, &self.device),
bash_c(restore_dns),
clear_ebtables(),
];
execute_all_with_log_error(cmdvv)?;

Expand All @@ -260,6 +263,10 @@ impl NetEnv {
ip_addr: gateway_ip,
} = try_get_default_gateway()?;

if gateway_mac.octets().iter().all(|&i| i == 0) {
return Ok(());
}

let gateway_ip = gateway_ip.to_string();
let gateway_mac = gateway_mac.to_string();

Expand Down
37 changes: 36 additions & 1 deletion chaos-tproxy-controller/src/proxy/net/iptables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub fn set_iptables<'a>(
net_env: &'a NetEnv,
proxy_ports: Option<&'a str>,
listen_port: &'a str,
device_mac: &'a str,
) -> Vec<Vec<&'a str>> {
let cmdv = match proxy_ports {
Some(proxy_ports) => ip_netns(
Expand Down Expand Up @@ -112,10 +113,25 @@ pub fn set_iptables<'a>(
"DROP",
],
),
vec![
"ebtables",
"-t",
"nat",
"-A",
"PREROUTING",
"-i",
&net_env.device,
"-j",
"dnat",
"--to-dst",
device_mac,
"--dnat-target",
"ACCEPT",
],
]
}

pub fn set_iptables_safe(net_env: &NetEnv) -> Vec<Vec<&str>> {
pub fn set_iptables_safe<'a>(net_env: &'a NetEnv, device_mac: &'a str) -> Vec<Vec<&'a str>> {
vec![
ip_netns(
&net_env.netns,
Expand Down Expand Up @@ -189,5 +205,24 @@ pub fn set_iptables_safe(net_env: &NetEnv) -> Vec<Vec<&str>> {
"ACCEPT",
],
),
vec![
"ebtables",
"-t",
"nat",
"-A",
"PREROUTING",
"-i",
&net_env.device,
"-j",
"dnat",
"--to-dst",
device_mac,
"--dnat-target",
"ACCEPT",
],
]
}

pub fn clear_ebtables() -> Vec<&'static str> {
vec!["ebtables", "-t", "nat", "-F"]
}
10 changes: 6 additions & 4 deletions chaos-tproxy-controller/src/proxy/net/set_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use libarp::interfaces::Interface;
use rtnetlink::Handle;

use crate::proxy::net::arp::gratuitous_arp;
use crate::proxy::net::bridge::{bash_c, execute, execute_all, NetEnv};
use crate::proxy::net::bridge::{bash_c, execute, execute_all, get_interface, NetEnv};
use crate::proxy::net::iptables::{set_iptables, set_iptables_safe};
use crate::proxy::net::ping::try_ping;

Expand All @@ -20,6 +20,8 @@ pub async fn set_net(
net_env.setenv_bridge(handle).await?;
let port = listen_port.to_string();
let restore_dns = "cp /etc/resolv.conf.bak /etc/resolv.conf";
let device_interface = get_interface(net_env.veth4.clone()).unwrap();
let device_mac = device_interface.mac.unwrap().to_string();

let arp_interface = Interface::new_by_name(net_env.veth4.clone().as_str()).unwrap();
gratuitous_arp(
Expand All @@ -29,13 +31,13 @@ pub async fn set_net(
);

if let Some(ref proxy_ports) = proxy_ports {
execute_all(set_iptables(net_env, Some(proxy_ports), &port))?;
execute_all(set_iptables(net_env, Some(proxy_ports), &port, &device_mac))?;
} else {
execute_all(set_iptables(net_env, None, &port))?;
execute_all(set_iptables(net_env, None, &port, &device_mac))?;
}

if safe {
execute_all(set_iptables_safe(net_env))?;
execute_all(set_iptables_safe(net_env, &device_mac))?;
}
let _ = execute(bash_c(restore_dns));

Expand Down
2 changes: 1 addition & 1 deletion chaos-tproxy-proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chaos-tproxy-proxy"
version = "0.5.1"
version = "0.5.2"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down

0 comments on commit 014d16a

Please sign in to comment.