|
| 1 | +// Copyright 2017 CoreOS, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//! DHCP lease option lookup |
| 16 | +
|
| 17 | +use anyhow::{anyhow, Context, Result}; |
| 18 | +use slog_scope::{debug, trace}; |
| 19 | +use std::collections::HashMap; |
| 20 | +use std::fs::File; |
| 21 | +use std::path::Path; |
| 22 | +use std::time::Duration; |
| 23 | +use zbus::{dbus_proxy, zvariant}; |
| 24 | + |
| 25 | +use super::key_lookup; |
| 26 | +use crate::retry; |
| 27 | + |
| 28 | +pub enum DhcpOption { |
| 29 | + DhcpServerId, |
| 30 | + // avoid dead code warnings with cfg(test) |
| 31 | + #[allow(dead_code)] |
| 32 | + AzureFabricAddress, |
| 33 | +} |
| 34 | + |
| 35 | +impl DhcpOption { |
| 36 | + pub fn get_value(&self) -> Result<String> { |
| 37 | + retry::Retry::new() |
| 38 | + .initial_backoff(Duration::from_millis(50)) |
| 39 | + .max_backoff(Duration::from_millis(500)) |
| 40 | + .max_retries(60) |
| 41 | + .retry(|_| { |
| 42 | + match self.try_nm() { |
| 43 | + Ok(res) => return Ok(res), |
| 44 | + Err(e) => trace!("failed querying NetworkManager: {e:#}"), |
| 45 | + } |
| 46 | + match self.try_networkd() { |
| 47 | + Ok(res) => return Ok(res), |
| 48 | + Err(e) => trace!("failed querying networkd: {e:#}"), |
| 49 | + } |
| 50 | + Err(anyhow!("failed to acquire DHCP option")) |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + fn try_nm(&self) -> Result<String> { |
| 55 | + let key = match *self { |
| 56 | + Self::DhcpServerId => "dhcp_server_identifier", |
| 57 | + Self::AzureFabricAddress => "private_245", |
| 58 | + }; |
| 59 | + |
| 60 | + // We set up everything from scratch on every attempt. This isn't |
| 61 | + // super-efficient but is simple and clear. |
| 62 | + // |
| 63 | + // We'd like to set both `property` and `object` attributes on the |
| 64 | + // trait methods, but that fails to compile, so we create proxies by |
| 65 | + // hand. |
| 66 | + |
| 67 | + // query NM for active connections |
| 68 | + let bus = zbus::blocking::Connection::system().context("connecting to D-Bus")?; |
| 69 | + let nm = NetworkManagerProxyBlocking::new(&bus).context("creating NetworkManager proxy")?; |
| 70 | + let conn_paths = nm |
| 71 | + .active_connections() |
| 72 | + .context("listing active connections")?; |
| 73 | + |
| 74 | + // walk active connections |
| 75 | + for conn_path in conn_paths { |
| 76 | + if conn_path == "/" { |
| 77 | + continue; |
| 78 | + } |
| 79 | + trace!("found NetworkManager connection: {conn_path}"); |
| 80 | + let conn = NMActiveConnectionProxyBlocking::builder(&bus) |
| 81 | + .path(conn_path) |
| 82 | + .context("setting connection path")? |
| 83 | + .build() |
| 84 | + .context("creating connection proxy")?; |
| 85 | + |
| 86 | + // get DHCP options |
| 87 | + let dhcp_path = conn.dhcp4_config().context("getting DHCP config")?; |
| 88 | + if dhcp_path == "/" { |
| 89 | + continue; |
| 90 | + } |
| 91 | + debug!("checking DHCP config: {dhcp_path}"); |
| 92 | + let dhcp = NMDhcp4ConfigProxyBlocking::builder(&bus) |
| 93 | + .path(dhcp_path) |
| 94 | + .context("setting DHCP config path")? |
| 95 | + .build() |
| 96 | + .context("creating DHCP config proxy")?; |
| 97 | + let options = dhcp.options().context("getting DHCP options")?; |
| 98 | + |
| 99 | + // check for option |
| 100 | + if let Some(value) = options.get(key) { |
| 101 | + return value.try_into().context("reading DHCP option as string"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // not found |
| 106 | + Err(anyhow!("failed to acquire DHCP option {key}")) |
| 107 | + } |
| 108 | + |
| 109 | + fn try_networkd(&self) -> Result<String> { |
| 110 | + let key = match *self { |
| 111 | + Self::DhcpServerId => "SERVER_ADDRESS", |
| 112 | + Self::AzureFabricAddress => "OPTION_245", |
| 113 | + }; |
| 114 | + |
| 115 | + let interfaces = pnet_datalink::interfaces(); |
| 116 | + trace!("interfaces - {:?}", interfaces); |
| 117 | + |
| 118 | + for interface in interfaces { |
| 119 | + trace!("looking at interface {:?}", interface); |
| 120 | + let lease_path = format!("/run/systemd/netif/leases/{}", interface.index); |
| 121 | + let lease_path = Path::new(&lease_path); |
| 122 | + if lease_path.exists() { |
| 123 | + debug!("found lease file - {:?}", lease_path); |
| 124 | + let lease = File::open(lease_path) |
| 125 | + .with_context(|| format!("failed to open lease file ({:?})", lease_path))?; |
| 126 | + |
| 127 | + if let Some(v) = key_lookup('=', key, lease)? { |
| 128 | + return Ok(v); |
| 129 | + } |
| 130 | + |
| 131 | + debug!( |
| 132 | + "failed to get value from existing lease file '{:?}'", |
| 133 | + lease_path |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | + Err(anyhow!("failed to acquire DHCP option {key}")) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +#[dbus_proxy( |
| 142 | + default_service = "org.freedesktop.NetworkManager", |
| 143 | + default_path = "/org/freedesktop/NetworkManager", |
| 144 | + interface = "org.freedesktop.NetworkManager" |
| 145 | +)] |
| 146 | +trait NetworkManager { |
| 147 | + #[dbus_proxy(property)] |
| 148 | + fn active_connections(&self) -> zbus::Result<Vec<zvariant::ObjectPath>>; |
| 149 | +} |
| 150 | + |
| 151 | +#[dbus_proxy( |
| 152 | + default_service = "org.freedesktop.NetworkManager", |
| 153 | + interface = "org.freedesktop.NetworkManager.Connection.Active" |
| 154 | +)] |
| 155 | +trait NMActiveConnection { |
| 156 | + #[dbus_proxy(property)] |
| 157 | + fn dhcp4_config(&self) -> zbus::Result<zvariant::ObjectPath>; |
| 158 | +} |
| 159 | + |
| 160 | +#[dbus_proxy( |
| 161 | + default_service = "org.freedesktop.NetworkManager", |
| 162 | + interface = "org.freedesktop.NetworkManager.DHCP4Config" |
| 163 | +)] |
| 164 | +trait NMDhcp4Config { |
| 165 | + #[dbus_proxy(property)] |
| 166 | + fn options(&self) -> Result<HashMap<String, zvariant::Value>>; |
| 167 | +} |
0 commit comments