diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-11-18 12:56:23 +0100 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-11-18 12:56:23 +0100 |
commit | 4bf8e5adc58f30cd880f70ebfad3144a5249e40e (patch) | |
tree | 0065e6f28aaa6dcefde67b5219c07c29fa6f460a | |
parent | 64f6f1c2aa7139ac4eeca4e5145e25d7d822ce0a (diff) |
update netlinklib0.8.2
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | src/main.rs | 96 |
3 files changed, 55 insertions, 52 deletions
@@ -390,7 +390,7 @@ dependencies = [ [[package]] name = "rsdsl_netlinkd" -version = "0.8.1" +version = "0.8.2" dependencies = [ "ipnet", "rsdsl_ip_config", @@ -404,12 +404,13 @@ dependencies = [ [[package]] name = "rsdsl_netlinklib" -version = "0.3.0" -source = "git+https://github.com/rsdsl/netlinklib.git#2c20dc2932696cb87ea7e6eb5327b7dccf0e9b1b" +version = "0.4.2" +source = "git+https://github.com/rsdsl/netlinklib.git#1954c7c7e975104fa651ccded4358826e8b3c8d8" dependencies = [ "futures", "libc", "netlink-packet-route", + "netlink-proto", "rtnetlink", "thiserror", "tokio", @@ -1,6 +1,6 @@ [package] name = "rsdsl_netlinkd" -version = "0.8.1" +version = "0.8.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -8,7 +8,7 @@ edition = "2021" [dependencies] ipnet = "2.8.0" rsdsl_ip_config = { git = "https://github.com/rsdsl/ip_config.git", version = "0.2.4" } -rsdsl_netlinklib = { git = "https://github.com/rsdsl/netlinklib.git", version = "0.3.0", features = ["blocking"] } +rsdsl_netlinklib = { git = "https://github.com/rsdsl/netlinklib.git", version = "0.4.2", features = ["blocking"] } rsdsl_pd_config = { git = "https://github.com/rsdsl/pd_config.git", version = "0.1.0" } serde_json = "1.0" signal-hook = "0.3.17" diff --git a/src/main.rs b/src/main.rs index 32f450d..8f6a061 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use rsdsl_netlinklib::blocking::{addr, link, route}; +use rsdsl_netlinklib::blocking::Connection; use std::fs::{self, File}; use std::io; @@ -34,17 +34,19 @@ enum Error { type Result<T> = std::result::Result<T, Error>; fn main() -> Result<()> { + let conn = Connection::new()?; + println!("[info] wait for eth0"); - link::wait_exists("eth0".into())?; + conn.link_wait_exists("eth0".into())?; println!("[info] detect eth0"); - link::set("eth0".into(), true)?; + conn.link_set("eth0".into(), true)?; - configure_lan()?; + configure_lan(&conn)?; println!("[info] config eth0 10.128.0.254/24 fe80::1/64"); - create_vlans()?; - configure_vlans()?; + create_vlans(&conn)?; + configure_vlans(&conn)?; println!("[info] config vlans 10.128.0.0/16 fe80::1/64"); fs::write("/proc/sys/net/ipv4/ip_forward", "1")?; @@ -55,47 +57,47 @@ fn main() -> Result<()> { println!("[info] enable ipv6 routing"); println!("[info] wait for eth1"); - link::wait_exists("eth1".into())?; + conn.link_wait_exists("eth1".into())?; println!("[info] detect eth1"); - link::set("eth1".into(), true)?; + conn.link_set("eth1".into(), true)?; - configure_modem()?; + configure_modem(&conn)?; println!("[info] config eth1 192.168.1.2/24 (modem)"); let mut signals = Signals::new([SIGUSR1])?; for _ in signals.forever() { - configure_wan_logged(); + configure_wan_logged(&conn); } Ok(()) // unreachable } -fn configure_lan() -> Result<()> { - addr::flush("eth0".into())?; - addr::add_link_local("eth0".into(), LINK_LOCAL.into(), 64)?; - addr::add("eth0".into(), "10.128.0.254".parse()?, 24)?; +fn configure_lan(conn: &Connection) -> Result<()> { + conn.address_flush("eth0".into())?; + conn.address_add_link_local("eth0".into(), LINK_LOCAL.into(), 64)?; + conn.address_add("eth0".into(), "10.128.0.254".parse()?, 24)?; Ok(()) } -fn create_vlans() -> Result<()> { +fn create_vlans(conn: &Connection) -> Result<()> { let zones = ["trusted", "untrusted", "isolated", "exposed"]; for (i, _) in zones.iter().enumerate() { let vlan_id = 10 * (i + 1); let vlan_name = format!("eth0.{}", vlan_id); - link::add_vlan(vlan_name.clone(), "eth0".to_string(), vlan_id as u16)?; - link::set(vlan_name.clone(), true)?; + conn.link_add_vlan(vlan_name.clone(), "eth0".to_string(), vlan_id as u16)?; + conn.link_set(vlan_name.clone(), true)?; - addr::flush(vlan_name.clone())?; + conn.address_flush(vlan_name.clone())?; } Ok(()) } -fn configure_vlans() -> Result<()> { +fn configure_vlans(conn: &Connection) -> Result<()> { let zones = ["trusted", "untrusted", "isolated", "exposed"]; for (i, _) in zones.iter().enumerate() { @@ -103,51 +105,51 @@ fn configure_vlans() -> Result<()> { let vlan_name = format!("eth0.{}", vlan_id); let vlan_addr = IpAddr::V4(Ipv4Addr::new(10, 128, vlan_id as u8, 254)); - addr::add_link_local(vlan_name.clone(), LINK_LOCAL.into(), 64)?; - addr::add(vlan_name.clone(), vlan_addr, 24)?; + conn.address_add_link_local(vlan_name.clone(), LINK_LOCAL.into(), 64)?; + conn.address_add(vlan_name.clone(), vlan_addr, 24)?; } Ok(()) } -fn configure_modem() -> Result<()> { - addr::flush("eth1".into())?; - addr::add("eth1".into(), "192.168.1.2".parse()?, 24)?; +fn configure_modem(conn: &Connection) -> Result<()> { + conn.address_flush("eth1".into())?; + conn.address_add("eth1".into(), "192.168.1.2".parse()?, 24)?; Ok(()) } -fn configure_wan_logged() { - match configure_wan() { +fn configure_wan_logged(conn: &Connection) { + match configure_wan(conn) { Ok(_) => {} Err(e) => println!("[warn] config wan: {}", e), } } -fn configure_wan() -> Result<()> { +fn configure_wan(conn: &Connection) -> Result<()> { if let Some(ds_config) = read_ds_config_optional() { // Only initialize the interface if an NCP is opened. // This not being the case is a good indicator // of the interface not being present due to not having a PPP session. if ds_config.v4.is_some() || ds_config.v6.is_some() { - link::set_mtu("ppp0".to_string(), 1492)?; - link::set("ppp0".to_string(), true)?; + conn.link_set_mtu("ppp0".to_string(), 1492)?; + conn.link_set("ppp0".to_string(), true)?; // Deconfigure everything, just to be safe. - addr::flush("ppp0".to_string())?; - route::flush("ppp0".to_string())?; + conn.address_flush("ppp0".to_string())?; + conn.route_flush("ppp0".to_string())?; } if let Some(v4) = ds_config.v4 { - addr::add("ppp0".to_string(), v4.addr.into(), 32)?; - route::add4(Ipv4Addr::UNSPECIFIED, 0, None, "ppp0".to_string())?; + conn.address_add("ppp0".to_string(), v4.addr.into(), 32)?; + conn.route_add4(Ipv4Addr::UNSPECIFIED, 0, None, "ppp0".to_string())?; println!("[info] config ppp0 {}/32", v4.addr); } if let Some(v6) = ds_config.v6 { - addr::add_link_local("ppp0".to_string(), v6.laddr.into(), 64)?; - route::add6(Ipv6Addr::UNSPECIFIED, 0, None, "ppp0".to_string())?; + conn.address_add_link_local("ppp0".to_string(), v6.laddr.into(), 64)?; + conn.route_add6(Ipv6Addr::UNSPECIFIED, 0, None, "ppp0".to_string())?; println!("[info] config ppp0 ll {}/64", v6.laddr); @@ -161,14 +163,14 @@ fn configure_wan() -> Result<()> { let addr_wan = next_ifid1(&mut subnets)?; - addr::add("ppp0".to_string(), addr_wan.into(), 64)?; + conn.address_add("ppp0".to_string(), addr_wan.into(), 64)?; println!("[info] config ppp0 gua {}/64", addr_wan); let addr_lan = next_ifid1(&mut subnets)?; - addr::flush6("eth0".to_string())?; - addr::add_link_local("eth0".to_string(), LINK_LOCAL.into(), 64)?; - addr::add("eth0".to_string(), addr_lan.into(), 64)?; + conn.address_flush6("eth0".to_string())?; + conn.address_add_link_local("eth0".to_string(), LINK_LOCAL.into(), 64)?; + conn.address_add("eth0".to_string(), addr_lan.into(), 64)?; println!("[info] config eth0 gua {}/64", addr_lan); @@ -178,9 +180,9 @@ fn configure_wan() -> Result<()> { let vlan_name = format!("eth0.{}", vlan_id); let vlan_addr = next_ifid1(&mut subnets)?; - addr::flush6(vlan_name.clone())?; - addr::add_link_local(vlan_name.clone(), LINK_LOCAL.into(), 64)?; - addr::add(vlan_name.clone(), vlan_addr.into(), 64)?; + conn.address_flush6(vlan_name.clone())?; + conn.address_add_link_local(vlan_name.clone(), LINK_LOCAL.into(), 64)?; + conn.address_add(vlan_name.clone(), vlan_addr.into(), 64)?; println!( "[info] config {} gua {}/64 zone {}", @@ -190,14 +192,14 @@ fn configure_wan() -> Result<()> { inform_radvd(); - if link::exists("dslite0".to_string())? { - link::set("dslite0".to_string(), true)?; + if conn.link_exists("dslite0".to_string())? { + conn.link_set("dslite0".to_string(), true)?; - addr::flush("dslite0".to_string())?; - addr::add("dslite0".to_string(), ADDR_B4.into(), 29)?; + conn.address_flush("dslite0".to_string())?; + conn.address_add("dslite0".to_string(), ADDR_B4.into(), 29)?; if ds_config.v4.is_none() { - route::add4( + conn.route_add4( Ipv4Addr::UNSPECIFIED, 0, Some(ADDR_AFTR), |