diff options
-rw-r--r-- | Cargo.lock | 243 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/error.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 202 |
4 files changed, 72 insertions, 377 deletions
@@ -81,46 +81,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] -name = "crossbeam-channel" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" -dependencies = [ - "cfg-if", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "filetime" -version = "0.2.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cbc844cecaee9d4443931972e1289c8ff485cb4cc2767cb03ca139ed6885153" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "windows-sys 0.48.0", -] - -[[package]] -name = "fsevent-sys" -version = "4.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2" -dependencies = [ - "libc", -] - -[[package]] name = "futures" version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -222,26 +182,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" [[package]] -name = "inotify" -version = "0.9.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff" -dependencies = [ - "bitflags", - "inotify-sys", - "libc", -] - -[[package]] -name = "inotify-sys" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb" -dependencies = [ - "libc", -] - -[[package]] name = "ipnet" version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -254,26 +194,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] -name = "kqueue" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c8fc60ba15bf51257aa9807a48a61013db043fcf3a78cb0d916e8e396dcad98" -dependencies = [ - "kqueue-sys", - "libc", -] - -[[package]] -name = "kqueue-sys" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8367585489f01bc55dd27404dcf56b95e6da061a256a666ab23be9ba96a2e587" -dependencies = [ - "bitflags", - "libc", -] - -[[package]] name = "libc" version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -307,9 +227,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" dependencies = [ "libc", - "log", "wasi", - "windows-sys 0.48.0", + "windows-sys", ] [[package]] @@ -390,24 +309,6 @@ dependencies = [ ] [[package]] -name = "notify" -version = "5.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "729f63e1ca555a43fe3efa4f3efdf4801c479da85b432242a7b726f353c88486" -dependencies = [ - "bitflags", - "crossbeam-channel", - "filetime", - "fsevent-sys", - "inotify", - "kqueue", - "libc", - "mio", - "walkdir", - "windows-sys 0.45.0", -] - -[[package]] name = "num_cpus" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -463,15 +364,6 @@ dependencies = [ ] [[package]] -name = "redox_syscall" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" -dependencies = [ - "bitflags", -] - -[[package]] name = "rsdsl_ip_config" version = "0.2.2" source = "git+https://github.com/rsdsl/ip_config.git#3239a5eeef22de4c50d4d00a9f51bebb5207633c" @@ -489,11 +381,11 @@ dependencies = [ "ipnet", "libc", "netlink-packet-route", - "notify", "rsdsl_ip_config", "rsdsl_pd_config", "rtnetlink", "serde_json", + "signal-hook", "thiserror", "tokio", ] @@ -537,15 +429,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] -name = "same-file" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" -dependencies = [ - "winapi-util", -] - -[[package]] name = "serde" version = "1.0.177" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -577,6 +460,25 @@ dependencies = [ ] [[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + +[[package]] name = "slab" version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -646,7 +548,7 @@ dependencies = [ "num_cpus", "pin-project-lite", "socket2", - "windows-sys 0.48.0", + "windows-sys", ] [[package]] @@ -656,16 +558,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" [[package]] -name = "walkdir" -version = "2.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698" -dependencies = [ - "same-file", - "winapi-util", -] - -[[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -688,15 +580,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - -[[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -704,35 +587,11 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.45.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" -dependencies = [ - "windows-targets 0.42.2", -] - -[[package]] -name = "windows-sys" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets 0.48.1", -] - -[[package]] -name = "windows-targets" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" -dependencies = [ - "windows_aarch64_gnullvm 0.42.2", - "windows_aarch64_msvc 0.42.2", - "windows_i686_gnu 0.42.2", - "windows_i686_msvc 0.42.2", - "windows_x86_64_gnu 0.42.2", - "windows_x86_64_gnullvm 0.42.2", - "windows_x86_64_msvc 0.42.2", + "windows-targets", ] [[package]] @@ -741,95 +600,53 @@ version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" dependencies = [ - "windows_aarch64_gnullvm 0.48.0", - "windows_aarch64_msvc 0.48.0", - "windows_i686_gnu 0.48.0", - "windows_i686_msvc 0.48.0", - "windows_x86_64_gnu 0.48.0", - "windows_x86_64_gnullvm 0.48.0", - "windows_x86_64_msvc 0.48.0", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" - -[[package]] -name = "windows_aarch64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" [[package]] name = "windows_aarch64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" - -[[package]] -name = "windows_aarch64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" [[package]] name = "windows_i686_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" - -[[package]] -name = "windows_i686_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" [[package]] name = "windows_i686_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" - -[[package]] -name = "windows_i686_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" [[package]] name = "windows_x86_64_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" - -[[package]] -name = "windows_x86_64_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" [[package]] name = "windows_x86_64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" - -[[package]] -name = "windows_x86_64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" [[package]] name = "windows_x86_64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" - -[[package]] -name = "windows_x86_64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" @@ -12,10 +12,10 @@ futures-util = "0.3.27" ipnet = "2.8.0" libc = "0.2.149" netlink-packet-route = "0.17.0" -notify = "5.1.0" rsdsl_ip_config = { git = "https://github.com/rsdsl/ip_config.git", version = "0.2.2" } rsdsl_pd_config = { git = "https://github.com/rsdsl/pd_config.git", version = "0.1.0" } rtnetlink = "0.13.1" serde_json = "1.0" +signal-hook = "0.3.17" thiserror = "1.0" tokio = { version = "1.26.0", features = ["rt-multi-thread"] } diff --git a/src/error.rs b/src/error.rs index 269c957..26e2444 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,8 +18,6 @@ pub enum Error { IpnetPrefixLen(#[from] ipnet::PrefixLenError), #[error("net: parse ip address: {0}")] NetAddrParseError(#[from] net::AddrParseError), - #[error("notify: {0}")] - Notify(#[from] notify::Error), #[error("rtnetlink: {0}")] RtNetlink(#[from] rtnetlink::Error), #[error("serde_json: {0}")] diff --git a/src/main.rs b/src/main.rs index 3d4123c..4e681de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,115 +3,54 @@ use rsdsl_netlinkd::{Error, Result}; use std::fs::{self, File}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; -use std::path::Path; use std::thread; use std::time::Duration; use ipnet::Ipv6Net; -use notify::event::{CreateKind, ModifyKind}; -use notify::{Event, EventKind, RecursiveMode, Watcher}; use rsdsl_ip_config::DsConfig; use rsdsl_pd_config::PdConfig; +use signal_hook::{consts::SIGUSR1, iterator::Signals}; const LINK_LOCAL: Ipv6Addr = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1); fn main() -> Result<()> { - println!("wait for eth0"); + println!("[info] wait for eth0"); link::wait_exists("eth0".into())?; - println!("detect eth0"); + println!("[info] detect eth0"); link::up("eth0".into())?; - match configure_eth0() { - Ok(_) => println!("configure eth0 statically (10.128.0.254/24, fe80::1/64)"), - Err(e) => { - println!("can't configure eth0: {}", e); - return Err(e); - } - } + configure_lan()?; + println!("[info] config eth0 10.128.0.254/24 fe80::1/64"); - match setup_vlans("eth0") { - Ok(_) => println!("setup vlans"), - Err(e) => { - println!("can't setup vlans: {}", e); - return Err(e); - } - } + create_vlans()?; + configure_vlans()?; fs::write("/proc/sys/net/ipv4/ip_forward", "1")?; - println!("enable ipv4 routing"); + println!("[info] enable ipv4 routing"); fs::write("/proc/sys/net/ipv6/conf/all/forwarding", "1")?; fs::write("/proc/sys/net/ipv6/conf/default/forwarding", "1")?; - println!("enable ipv6 routing"); + println!("[info] enable ipv6 routing"); - println!("wait for eth1"); + println!("[info] wait for eth1"); link::wait_exists("eth1".into())?; - println!("detect eth1"); + println!("[info] detect eth1"); link::up("eth1".into())?; - match enable_modem_access() { - Ok(_) => println!("configure eth1 modem (192.168.1.2/24)"), - Err(e) => { - println!("can't configure eth1 modem: {}", e); - return Err(e); - } - } - - let ip_config = Path::new(rsdsl_ip_config::LOCATION); - while !ip_config.exists() { - println!("wait for pppoe"); - thread::sleep(Duration::from_secs(8)); - } - - configure_wan(); - - let mut watcher = notify::recommended_watcher(|res: notify::Result<Event>| match res { - Ok(event) => match event.kind { - EventKind::Create(kind) if kind == CreateKind::File => { - configure_wan(); - } - EventKind::Modify(kind) if matches!(kind, ModifyKind::Data(_)) => { - configure_wan(); - } - _ => {} - }, - Err(e) => println!("watch error: {:?}", e), - })?; + configure_modem()?; + println!("[info] config eth1 192.168.1.2/24 (modem)"); - watcher.watch(ip_config, RecursiveMode::NonRecursive)?; - - let pd_config = Path::new(rsdsl_pd_config::LOCATION); - - println!("wait for dhcp6"); - while !pd_config.exists() { - thread::sleep(Duration::from_secs(8)); + let mut signals = Signals::new([SIGUSR1])?; + for _ in signals.forever() { + configure_wan_logged(); } - configure_ipv6(); - - let mut watcher = notify::recommended_watcher(|res: notify::Result<Event>| match res { - Ok(event) => match event.kind { - EventKind::Create(kind) if kind == CreateKind::File => { - configure_ipv6(); - } - EventKind::Modify(kind) if matches!(kind, ModifyKind::Data(_)) => { - configure_ipv6(); - } - _ => {} - }, - Err(e) => println!("watch error: {:?}", e), - })?; - - watcher.watch(pd_config, RecursiveMode::NonRecursive)?; - - loop { - thread::sleep(Duration::MAX) - } + Ok(()) // unreachable } -fn configure_eth0() -> Result<()> { +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)?; @@ -119,113 +58,54 @@ fn configure_eth0() -> Result<()> { Ok(()) } -fn setup_vlans(base: &str) -> Result<()> { +fn create_vlans() -> Result<()> { let zones = ["trusted", "untrusted", "isolated", "exposed"]; for (i, zone) in zones.iter().enumerate() { let vlan_id = 10 * (i + 1); - let vlan_name = format!("{}.{}", base, vlan_id); + let vlan_name = format!("eth0.{}", vlan_id); let vlan_addr = IpAddr::V4(Ipv4Addr::new(10, 128, vlan_id as u8, 254)); - link::add_vlan(vlan_name.clone(), base.to_owned(), vlan_id as u16)?; - - addr::add_link_local(vlan_name.clone(), LINK_LOCAL.into(), 64)?; - addr::add(vlan_name.clone(), vlan_addr, 24)?; - + link::add_vlan(vlan_name.clone(), "eth0".to_string(), vlan_id as u16)?; link::up(vlan_name.clone())?; - println!( - "configure {} ({}/24, fe80::1/64) zone {}", - vlan_name, vlan_addr, zone - ); + addr::flush(vlan_name.clone())?; } Ok(()) } -fn enable_modem_access() -> Result<()> { - addr::flush("eth1".into())?; - addr::add("eth1".into(), "192.168.1.2".parse()?, 24)?; +fn configure_vlans() -> Result<()> { + let zones = ["trusted", "untrusted", "isolated", "exposed"]; - Ok(()) -} + for (i, zone) in zones.iter().enumerate() { + let vlan_id = 10 * (i + 1); + let vlan_name = format!("eth0.{}", vlan_id); + let vlan_addr = IpAddr::V4(Ipv4Addr::new(10, 128, vlan_id as u8, 254)); -fn configure_wan() { - match configure_ppp0() { - Ok(_) => println!("configure ppp0 with pppoe data"), - Err(e) => println!("can't configure ppp0: {:?}", e), + addr::add_link_local(vlan_name.clone(), LINK_LOCAL.into(), 64)?; + addr::add(vlan_name.clone(), vlan_addr, 24)?; } -} -fn configure_ppp0() -> Result<()> { - link::set_mtu("ppp0".into(), 1492)?; - link::up("ppp0".into())?; - - let mut file = File::open(rsdsl_ip_config::LOCATION)?; - let ip_config: DsConfig = serde_json::from_reader(&mut file)?; - - addr::flush("ppp0".into())?; - route::flush("ppp0".into())?; - - if let Some(v4) = ip_config.v4 { - addr::add("ppp0".into(), IpAddr::V4(v4.addr), 32)?; - route::add4(Ipv4Addr::UNSPECIFIED, 0, None, "ppp0".into())?; - } + Ok(()) +} - if let Some(v6) = ip_config.v6 { - addr::add("ppp0".into(), IpAddr::V6(v6.laddr), 64)?; - route::add6(Ipv6Addr::UNSPECIFIED, 0, None, "ppp0".into())?; - } +fn configure_modem() -> Result<()> { + addr::flush("eth1".into())?; + addr::add("eth1".into(), "192.168.1.2".parse()?, 24)?; Ok(()) } -fn configure_ipv6() { - match configure_all_v6() { - Ok(_) => println!("configure ipv6"), - Err(e) => println!("can't configure ipv6: {:?}", e), +fn configure_wan_logged() { + match configure_wan() { + Ok(_) => {} + Err(e) => println!("[warn] config wan: {}", e), } } -fn configure_all_v6() -> Result<()> { - let mut file = File::open(rsdsl_pd_config::LOCATION)?; - let pdconfig: PdConfig = serde_json::from_reader(&mut file)?; - - let prefix = Ipv6Net::new(pdconfig.prefix, pdconfig.len)?.trunc(); - let mut subnets = prefix.subnets(64)?; - - addr::flush6_global()?; - addr::add("ppp0".into(), IpAddr::V6(next_ifid1(&mut subnets)?), 64)?; - - let addr = next_ifid1(&mut subnets)?; - - fs::write("/proc/sys/net/ipv6/conf/eth0/accept_ra", "0")?; - - addr::flush6("eth0".into())?; - addr::add_link_local("eth0".into(), LINK_LOCAL.into(), 64)?; - addr::add("eth0".into(), addr.into(), 64)?; - - println!("configure eth0 ({}/64)", addr); - - let zones = ["trusted", "untrusted", "isolated", "exposed"]; - for (i, zone) in zones.iter().enumerate() { - let vlan_id = 10 * (i + 1); - let vlan_name = format!("eth0.{}", vlan_id); - let vlan_addr = next_ifid1(&mut subnets)?; - - fs::write( - format!("/proc/sys/net/ipv6/conf/{}/accept_ra", vlan_name), - "0", - )?; - - addr::flush6(vlan_name.clone())?; - addr::add(vlan_name.clone(), LINK_LOCAL.into(), 64)?; - addr::add(vlan_name.clone(), vlan_addr.into(), 64)?; - - println!("configure {} ({}/64) zone {}", vlan_name, vlan_addr, zone); - } - - Ok(()) +fn configure_wan() -> Result<()> { + todo!() } fn next_ifid1<T: Iterator<Item = Ipv6Net>>(subnets: &mut T) -> Result<Ipv6Addr> { |