|
| 1 | +use futures::TryStreamExt; |
| 2 | +use netlink_packet_route::{ |
| 3 | + route::{RouteAddress, RouteAttribute, RouteProtocol, RouteScope, RouteType}, |
| 4 | + AddressFamily, |
| 5 | +}; |
| 6 | +use rtnetlink::{new_connection, Error, Handle, IpVersion}; |
| 7 | + |
| 8 | +/// Attempts to add a route to the given subnet on the loopback interface. |
| 9 | +/// |
| 10 | +/// This function uses the `ip` command to add a route to the loopback |
| 11 | +/// interface. It checks if the current user has root privileges before |
| 12 | +/// attempting to add the route. If the user does not have root privileges, the |
| 13 | +/// function returns immediately. If the `ip` command fails, it prints an error |
| 14 | +/// message to the console. |
| 15 | +/// |
| 16 | +/// # Arguments |
| 17 | +/// |
| 18 | +/// * `subnet` - The subnet for which to add a route. |
| 19 | +/// |
| 20 | +/// # Example |
| 21 | +/// |
| 22 | +/// ``` |
| 23 | +/// let subnet = cidr::IpCidr::from_str("192.168.1.0/24").unwrap(); |
| 24 | +/// sysctl_route_add_cidr(&subnet); |
| 25 | +/// ``` |
| 26 | +pub async fn sysctl_route_add_cidr(subnet: &cidr::IpCidr) { |
| 27 | + if !nix::unistd::Uid::effective().is_root() { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + let (connection, handle, _) = new_connection().unwrap(); |
| 32 | + |
| 33 | + tokio::spawn(connection); |
| 34 | + |
| 35 | + if let Err(e) = add_route(handle.clone(), subnet).await { |
| 36 | + eprintln!("{e}"); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +async fn add_route(handle: Handle, cidr: &cidr::IpCidr) -> Result<(), Error> { |
| 41 | + let route = handle.route(); |
| 42 | + let iface_idx = handle |
| 43 | + .link() |
| 44 | + .get() |
| 45 | + .match_name("lo".to_owned()) |
| 46 | + .execute() |
| 47 | + .try_next() |
| 48 | + .await? |
| 49 | + .unwrap() |
| 50 | + .header |
| 51 | + .index; |
| 52 | + |
| 53 | + // Check if the route already exists |
| 54 | + let route_check = |ip_version: IpVersion, |
| 55 | + address_family: AddressFamily, |
| 56 | + destination_prefix_length: u8, |
| 57 | + route_address: RouteAddress| async move { |
| 58 | + let mut routes = handle.route().get(ip_version).execute(); |
| 59 | + while let Some(route) = routes.try_next().await? { |
| 60 | + let header = route.header; |
| 61 | + if header.address_family == address_family |
| 62 | + && header.destination_prefix_length == destination_prefix_length |
| 63 | + { |
| 64 | + for attr in route.attributes.iter() { |
| 65 | + if let RouteAttribute::Destination(dest) = attr { |
| 66 | + if dest == &route_address { |
| 67 | + return Ok(true); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + Ok(false) |
| 74 | + }; |
| 75 | + |
| 76 | + // Add a route to the loopback interface. |
| 77 | + match cidr { |
| 78 | + cidr::IpCidr::V4(v4) => { |
| 79 | + if route_check( |
| 80 | + IpVersion::V4, |
| 81 | + AddressFamily::Inet, |
| 82 | + v4.network_length(), |
| 83 | + RouteAddress::Inet(v4.first_address()), |
| 84 | + ) |
| 85 | + .await? |
| 86 | + { |
| 87 | + return Ok(()); |
| 88 | + } |
| 89 | + route |
| 90 | + .add() |
| 91 | + .v4() |
| 92 | + .destination_prefix(v4.first_address(), v4.network_length()) |
| 93 | + .kind(RouteType::Local) |
| 94 | + .protocol(RouteProtocol::Boot) |
| 95 | + .scope(RouteScope::Universe) |
| 96 | + .output_interface(iface_idx) |
| 97 | + .priority(1024) |
| 98 | + .execute() |
| 99 | + .await? |
| 100 | + } |
| 101 | + cidr::IpCidr::V6(v6) => { |
| 102 | + if route_check( |
| 103 | + IpVersion::V6, |
| 104 | + AddressFamily::Inet6, |
| 105 | + v6.network_length(), |
| 106 | + RouteAddress::Inet6(v6.first_address()), |
| 107 | + ) |
| 108 | + .await? |
| 109 | + { |
| 110 | + return Ok(()); |
| 111 | + } |
| 112 | + route |
| 113 | + .add() |
| 114 | + .v6() |
| 115 | + .destination_prefix(v6.first_address(), v6.network_length()) |
| 116 | + .kind(RouteType::Local) |
| 117 | + .protocol(RouteProtocol::Boot) |
| 118 | + .scope(RouteScope::Universe) |
| 119 | + .output_interface(iface_idx) |
| 120 | + .priority(1024) |
| 121 | + .execute() |
| 122 | + .await? |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + Ok(()) |
| 127 | +} |
| 128 | + |
| 129 | +/// Tries to disable local binding for IPv6. |
| 130 | +/// |
| 131 | +/// This function uses the `sysctl` command to disable local binding for IPv6. |
| 132 | +/// It checks if the current user has root privileges before attempting to |
| 133 | +/// change the setting. If the user does not have root privileges, the function |
| 134 | +/// returns immediately. If the `sysctl` command fails, it prints an error |
| 135 | +/// message to the console. |
| 136 | +/// |
| 137 | +/// # Example |
| 138 | +/// |
| 139 | +/// ``` |
| 140 | +/// sysctl_ipv6_no_local_bind(); |
| 141 | +/// ``` |
| 142 | +pub fn sysctl_ipv6_no_local_bind() { |
| 143 | + if !nix::unistd::Uid::effective().is_root() { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + use sysctl::Sysctl; |
| 148 | + const CTLNAME: &str = "net.ipv6.ip_nonlocal_bind"; |
| 149 | + |
| 150 | + let ctl = <sysctl::Ctl as Sysctl>::new(CTLNAME) |
| 151 | + .expect(&format!("could not get sysctl '{}'", CTLNAME)); |
| 152 | + let _ = ctl.name().expect("could not get sysctl name"); |
| 153 | + |
| 154 | + let old_value = ctl.value_string().expect("could not get sysctl value"); |
| 155 | + |
| 156 | + let target_value = match old_value.as_ref() { |
| 157 | + "0" => "1", |
| 158 | + "1" | _ => &old_value, |
| 159 | + }; |
| 160 | + |
| 161 | + ctl.set_value_string(target_value).unwrap_or_else(|e| { |
| 162 | + panic!( |
| 163 | + "could not set sysctl '{}' to '{}': {}", |
| 164 | + CTLNAME, target_value, e |
| 165 | + ) |
| 166 | + }); |
| 167 | +} |
0 commit comments